ddp.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /*
  2. * DDP: An implementation of the AppleTalk DDP protocol for
  3. * Ethernet 'ELAP'.
  4. *
  5. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  6. *
  7. * With more than a little assistance from
  8. *
  9. * Wesley Craig <netatalk@umich.edu>
  10. *
  11. * Fixes:
  12. * Neil Horman : Added missing device ioctls
  13. * Michael Callahan : Made routing work
  14. * Wesley Craig : Fix probing to listen to a
  15. * passed node id.
  16. * Alan Cox : Added send/recvmsg support
  17. * Alan Cox : Moved at. to protinfo in
  18. * socket.
  19. * Alan Cox : Added firewall hooks.
  20. * Alan Cox : Supports new ARPHRD_LOOPBACK
  21. * Christer Weinigel : Routing and /proc fixes.
  22. * Bradford Johnson : LocalTalk.
  23. * Tom Dyas : Module support.
  24. * Alan Cox : Hooks for PPP (based on the
  25. * LocalTalk hook).
  26. * Alan Cox : Posix bits
  27. * Alan Cox/Mike Freeman : Possible fix to NBP problems
  28. * Bradford Johnson : IP-over-DDP (experimental)
  29. * Jay Schulist : Moved IP-over-DDP to its own
  30. * driver file. (ipddp.c & ipddp.h)
  31. * Jay Schulist : Made work as module with
  32. * AppleTalk drivers, cleaned it.
  33. * Rob Newberry : Added proxy AARP and AARP
  34. * procfs, moved probing to AARP
  35. * module.
  36. * Adrian Sun/
  37. * Michael Zuelsdorff : fix for net.0 packets. don't
  38. * allow illegal ether/tokentalk
  39. * port assignment. we lose a
  40. * valid localtalk port as a
  41. * result.
  42. * Arnaldo C. de Melo : Cleanup, in preparation for
  43. * shared skb support 8)
  44. * Arnaldo C. de Melo : Move proc stuff to atalk_proc.c,
  45. * use seq_file
  46. *
  47. * This program is free software; you can redistribute it and/or
  48. * modify it under the terms of the GNU General Public License
  49. * as published by the Free Software Foundation; either version
  50. * 2 of the License, or (at your option) any later version.
  51. *
  52. */
  53. #include <linux/capability.h>
  54. #include <linux/module.h>
  55. #include <linux/if_arp.h>
  56. #include <linux/termios.h> /* For TIOCOUTQ/INQ */
  57. #include <linux/compat.h>
  58. #include <linux/slab.h>
  59. #include <net/datalink.h>
  60. #include <net/psnap.h>
  61. #include <net/sock.h>
  62. #include <net/tcp_states.h>
  63. #include <net/route.h>
  64. #include <linux/atalk.h>
  65. #include "../core/kmap_skb.h"
  66. struct datalink_proto *ddp_dl, *aarp_dl;
  67. static const struct proto_ops atalk_dgram_ops;
  68. /**************************************************************************\
  69. * *
  70. * Handlers for the socket list. *
  71. * *
  72. \**************************************************************************/
  73. HLIST_HEAD(atalk_sockets);
  74. DEFINE_RWLOCK(atalk_sockets_lock);
  75. static inline void __atalk_insert_socket(struct sock *sk)
  76. {
  77. sk_add_node(sk, &atalk_sockets);
  78. }
  79. static inline void atalk_remove_socket(struct sock *sk)
  80. {
  81. write_lock_bh(&atalk_sockets_lock);
  82. sk_del_node_init(sk);
  83. write_unlock_bh(&atalk_sockets_lock);
  84. }
  85. static struct sock *atalk_search_socket(struct sockaddr_at *to,
  86. struct atalk_iface *atif)
  87. {
  88. struct sock *s;
  89. struct hlist_node *node;
  90. read_lock_bh(&atalk_sockets_lock);
  91. sk_for_each(s, node, &atalk_sockets) {
  92. struct atalk_sock *at = at_sk(s);
  93. if (to->sat_port != at->src_port)
  94. continue;
  95. if (to->sat_addr.s_net == ATADDR_ANYNET &&
  96. to->sat_addr.s_node == ATADDR_BCAST)
  97. goto found;
  98. if (to->sat_addr.s_net == at->src_net &&
  99. (to->sat_addr.s_node == at->src_node ||
  100. to->sat_addr.s_node == ATADDR_BCAST ||
  101. to->sat_addr.s_node == ATADDR_ANYNODE))
  102. goto found;
  103. /* XXXX.0 -- we got a request for this router. make sure
  104. * that the node is appropriately set. */
  105. if (to->sat_addr.s_node == ATADDR_ANYNODE &&
  106. to->sat_addr.s_net != ATADDR_ANYNET &&
  107. atif->address.s_node == at->src_node) {
  108. to->sat_addr.s_node = atif->address.s_node;
  109. goto found;
  110. }
  111. }
  112. s = NULL;
  113. found:
  114. read_unlock_bh(&atalk_sockets_lock);
  115. return s;
  116. }
  117. /**
  118. * atalk_find_or_insert_socket - Try to find a socket matching ADDR
  119. * @sk - socket to insert in the list if it is not there already
  120. * @sat - address to search for
  121. *
  122. * Try to find a socket matching ADDR in the socket list, if found then return
  123. * it. If not, insert SK into the socket list.
  124. *
  125. * This entire operation must execute atomically.
  126. */
  127. static struct sock *atalk_find_or_insert_socket(struct sock *sk,
  128. struct sockaddr_at *sat)
  129. {
  130. struct sock *s;
  131. struct hlist_node *node;
  132. struct atalk_sock *at;
  133. write_lock_bh(&atalk_sockets_lock);
  134. sk_for_each(s, node, &atalk_sockets) {
  135. at = at_sk(s);
  136. if (at->src_net == sat->sat_addr.s_net &&
  137. at->src_node == sat->sat_addr.s_node &&
  138. at->src_port == sat->sat_port)
  139. goto found;
  140. }
  141. s = NULL;
  142. __atalk_insert_socket(sk); /* Wheee, it's free, assign and insert. */
  143. found:
  144. write_unlock_bh(&atalk_sockets_lock);
  145. return s;
  146. }
  147. static void atalk_destroy_timer(unsigned long data)
  148. {
  149. struct sock *sk = (struct sock *)data;
  150. if (sk_has_allocations(sk)) {
  151. sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
  152. add_timer(&sk->sk_timer);
  153. } else
  154. sock_put(sk);
  155. }
  156. static inline void atalk_destroy_socket(struct sock *sk)
  157. {
  158. atalk_remove_socket(sk);
  159. skb_queue_purge(&sk->sk_receive_queue);
  160. if (sk_has_allocations(sk)) {
  161. setup_timer(&sk->sk_timer, atalk_destroy_timer,
  162. (unsigned long)sk);
  163. sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
  164. add_timer(&sk->sk_timer);
  165. } else
  166. sock_put(sk);
  167. }
  168. /**************************************************************************\
  169. * *
  170. * Routing tables for the AppleTalk socket layer. *
  171. * *
  172. \**************************************************************************/
  173. /* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
  174. struct atalk_route *atalk_routes;
  175. DEFINE_RWLOCK(atalk_routes_lock);
  176. struct atalk_iface *atalk_interfaces;
  177. DEFINE_RWLOCK(atalk_interfaces_lock);
  178. /* For probing devices or in a routerless network */
  179. struct atalk_route atrtr_default;
  180. /* AppleTalk interface control */
  181. /*
  182. * Drop a device. Doesn't drop any of its routes - that is the caller's
  183. * problem. Called when we down the interface or delete the address.
  184. */
  185. static void atif_drop_device(struct net_device *dev)
  186. {
  187. struct atalk_iface **iface = &atalk_interfaces;
  188. struct atalk_iface *tmp;
  189. write_lock_bh(&atalk_interfaces_lock);
  190. while ((tmp = *iface) != NULL) {
  191. if (tmp->dev == dev) {
  192. *iface = tmp->next;
  193. dev_put(dev);
  194. kfree(tmp);
  195. dev->atalk_ptr = NULL;
  196. } else
  197. iface = &tmp->next;
  198. }
  199. write_unlock_bh(&atalk_interfaces_lock);
  200. }
  201. static struct atalk_iface *atif_add_device(struct net_device *dev,
  202. struct atalk_addr *sa)
  203. {
  204. struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL);
  205. if (!iface)
  206. goto out;
  207. dev_hold(dev);
  208. iface->dev = dev;
  209. dev->atalk_ptr = iface;
  210. iface->address = *sa;
  211. iface->status = 0;
  212. write_lock_bh(&atalk_interfaces_lock);
  213. iface->next = atalk_interfaces;
  214. atalk_interfaces = iface;
  215. write_unlock_bh(&atalk_interfaces_lock);
  216. out:
  217. return iface;
  218. }
  219. /* Perform phase 2 AARP probing on our tentative address */
  220. static int atif_probe_device(struct atalk_iface *atif)
  221. {
  222. int netrange = ntohs(atif->nets.nr_lastnet) -
  223. ntohs(atif->nets.nr_firstnet) + 1;
  224. int probe_net = ntohs(atif->address.s_net);
  225. int probe_node = atif->address.s_node;
  226. int netct, nodect;
  227. /* Offset the network we start probing with */
  228. if (probe_net == ATADDR_ANYNET) {
  229. probe_net = ntohs(atif->nets.nr_firstnet);
  230. if (netrange)
  231. probe_net += jiffies % netrange;
  232. }
  233. if (probe_node == ATADDR_ANYNODE)
  234. probe_node = jiffies & 0xFF;
  235. /* Scan the networks */
  236. atif->status |= ATIF_PROBE;
  237. for (netct = 0; netct <= netrange; netct++) {
  238. /* Sweep the available nodes from a given start */
  239. atif->address.s_net = htons(probe_net);
  240. for (nodect = 0; nodect < 256; nodect++) {
  241. atif->address.s_node = (nodect + probe_node) & 0xFF;
  242. if (atif->address.s_node > 0 &&
  243. atif->address.s_node < 254) {
  244. /* Probe a proposed address */
  245. aarp_probe_network(atif);
  246. if (!(atif->status & ATIF_PROBE_FAIL)) {
  247. atif->status &= ~ATIF_PROBE;
  248. return 0;
  249. }
  250. }
  251. atif->status &= ~ATIF_PROBE_FAIL;
  252. }
  253. probe_net++;
  254. if (probe_net > ntohs(atif->nets.nr_lastnet))
  255. probe_net = ntohs(atif->nets.nr_firstnet);
  256. }
  257. atif->status &= ~ATIF_PROBE;
  258. return -EADDRINUSE; /* Network is full... */
  259. }
  260. /* Perform AARP probing for a proxy address */
  261. static int atif_proxy_probe_device(struct atalk_iface *atif,
  262. struct atalk_addr* proxy_addr)
  263. {
  264. int netrange = ntohs(atif->nets.nr_lastnet) -
  265. ntohs(atif->nets.nr_firstnet) + 1;
  266. /* we probe the interface's network */
  267. int probe_net = ntohs(atif->address.s_net);
  268. int probe_node = ATADDR_ANYNODE; /* we'll take anything */
  269. int netct, nodect;
  270. /* Offset the network we start probing with */
  271. if (probe_net == ATADDR_ANYNET) {
  272. probe_net = ntohs(atif->nets.nr_firstnet);
  273. if (netrange)
  274. probe_net += jiffies % netrange;
  275. }
  276. if (probe_node == ATADDR_ANYNODE)
  277. probe_node = jiffies & 0xFF;
  278. /* Scan the networks */
  279. for (netct = 0; netct <= netrange; netct++) {
  280. /* Sweep the available nodes from a given start */
  281. proxy_addr->s_net = htons(probe_net);
  282. for (nodect = 0; nodect < 256; nodect++) {
  283. proxy_addr->s_node = (nodect + probe_node) & 0xFF;
  284. if (proxy_addr->s_node > 0 &&
  285. proxy_addr->s_node < 254) {
  286. /* Tell AARP to probe a proposed address */
  287. int ret = aarp_proxy_probe_network(atif,
  288. proxy_addr);
  289. if (ret != -EADDRINUSE)
  290. return ret;
  291. }
  292. }
  293. probe_net++;
  294. if (probe_net > ntohs(atif->nets.nr_lastnet))
  295. probe_net = ntohs(atif->nets.nr_firstnet);
  296. }
  297. return -EADDRINUSE; /* Network is full... */
  298. }
  299. struct atalk_addr *atalk_find_dev_addr(struct net_device *dev)
  300. {
  301. struct atalk_iface *iface = dev->atalk_ptr;
  302. return iface ? &iface->address : NULL;
  303. }
  304. static struct atalk_addr *atalk_find_primary(void)
  305. {
  306. struct atalk_iface *fiface = NULL;
  307. struct atalk_addr *retval;
  308. struct atalk_iface *iface;
  309. /*
  310. * Return a point-to-point interface only if
  311. * there is no non-ptp interface available.
  312. */
  313. read_lock_bh(&atalk_interfaces_lock);
  314. for (iface = atalk_interfaces; iface; iface = iface->next) {
  315. if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
  316. fiface = iface;
  317. if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
  318. retval = &iface->address;
  319. goto out;
  320. }
  321. }
  322. if (fiface)
  323. retval = &fiface->address;
  324. else if (atalk_interfaces)
  325. retval = &atalk_interfaces->address;
  326. else
  327. retval = NULL;
  328. out:
  329. read_unlock_bh(&atalk_interfaces_lock);
  330. return retval;
  331. }
  332. /*
  333. * Find a match for 'any network' - ie any of our interfaces with that
  334. * node number will do just nicely.
  335. */
  336. static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
  337. {
  338. struct atalk_iface *iface = dev->atalk_ptr;
  339. if (!iface || iface->status & ATIF_PROBE)
  340. goto out_err;
  341. if (node != ATADDR_BCAST &&
  342. iface->address.s_node != node &&
  343. node != ATADDR_ANYNODE)
  344. goto out_err;
  345. out:
  346. return iface;
  347. out_err:
  348. iface = NULL;
  349. goto out;
  350. }
  351. /* Find a match for a specific network:node pair */
  352. static struct atalk_iface *atalk_find_interface(__be16 net, int node)
  353. {
  354. struct atalk_iface *iface;
  355. read_lock_bh(&atalk_interfaces_lock);
  356. for (iface = atalk_interfaces; iface; iface = iface->next) {
  357. if ((node == ATADDR_BCAST ||
  358. node == ATADDR_ANYNODE ||
  359. iface->address.s_node == node) &&
  360. iface->address.s_net == net &&
  361. !(iface->status & ATIF_PROBE))
  362. break;
  363. /* XXXX.0 -- net.0 returns the iface associated with net */
  364. if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
  365. ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
  366. ntohs(net) <= ntohs(iface->nets.nr_lastnet))
  367. break;
  368. }
  369. read_unlock_bh(&atalk_interfaces_lock);
  370. return iface;
  371. }
  372. /*
  373. * Find a route for an AppleTalk packet. This ought to get cached in
  374. * the socket (later on...). We know about host routes and the fact
  375. * that a route must be direct to broadcast.
  376. */
  377. static struct atalk_route *atrtr_find(struct atalk_addr *target)
  378. {
  379. /*
  380. * we must search through all routes unless we find a
  381. * host route, because some host routes might overlap
  382. * network routes
  383. */
  384. struct atalk_route *net_route = NULL;
  385. struct atalk_route *r;
  386. read_lock_bh(&atalk_routes_lock);
  387. for (r = atalk_routes; r; r = r->next) {
  388. if (!(r->flags & RTF_UP))
  389. continue;
  390. if (r->target.s_net == target->s_net) {
  391. if (r->flags & RTF_HOST) {
  392. /*
  393. * if this host route is for the target,
  394. * the we're done
  395. */
  396. if (r->target.s_node == target->s_node)
  397. goto out;
  398. } else
  399. /*
  400. * this route will work if there isn't a
  401. * direct host route, so cache it
  402. */
  403. net_route = r;
  404. }
  405. }
  406. /*
  407. * if we found a network route but not a direct host
  408. * route, then return it
  409. */
  410. if (net_route)
  411. r = net_route;
  412. else if (atrtr_default.dev)
  413. r = &atrtr_default;
  414. else /* No route can be found */
  415. r = NULL;
  416. out:
  417. read_unlock_bh(&atalk_routes_lock);
  418. return r;
  419. }
  420. /*
  421. * Given an AppleTalk network, find the device to use. This can be
  422. * a simple lookup.
  423. */
  424. struct net_device *atrtr_get_dev(struct atalk_addr *sa)
  425. {
  426. struct atalk_route *atr = atrtr_find(sa);
  427. return atr ? atr->dev : NULL;
  428. }
  429. /* Set up a default router */
  430. static void atrtr_set_default(struct net_device *dev)
  431. {
  432. atrtr_default.dev = dev;
  433. atrtr_default.flags = RTF_UP;
  434. atrtr_default.gateway.s_net = htons(0);
  435. atrtr_default.gateway.s_node = 0;
  436. }
  437. /*
  438. * Add a router. Basically make sure it looks valid and stuff the
  439. * entry in the list. While it uses netranges we always set them to one
  440. * entry to work like netatalk.
  441. */
  442. static int atrtr_create(struct rtentry *r, struct net_device *devhint)
  443. {
  444. struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
  445. struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
  446. struct atalk_route *rt;
  447. struct atalk_iface *iface, *riface;
  448. int retval = -EINVAL;
  449. /*
  450. * Fixme: Raise/Lower a routing change semaphore for these
  451. * operations.
  452. */
  453. /* Validate the request */
  454. if (ta->sat_family != AF_APPLETALK ||
  455. (!devhint && ga->sat_family != AF_APPLETALK))
  456. goto out;
  457. /* Now walk the routing table and make our decisions */
  458. write_lock_bh(&atalk_routes_lock);
  459. for (rt = atalk_routes; rt; rt = rt->next) {
  460. if (r->rt_flags != rt->flags)
  461. continue;
  462. if (ta->sat_addr.s_net == rt->target.s_net) {
  463. if (!(rt->flags & RTF_HOST))
  464. break;
  465. if (ta->sat_addr.s_node == rt->target.s_node)
  466. break;
  467. }
  468. }
  469. if (!devhint) {
  470. riface = NULL;
  471. read_lock_bh(&atalk_interfaces_lock);
  472. for (iface = atalk_interfaces; iface; iface = iface->next) {
  473. if (!riface &&
  474. ntohs(ga->sat_addr.s_net) >=
  475. ntohs(iface->nets.nr_firstnet) &&
  476. ntohs(ga->sat_addr.s_net) <=
  477. ntohs(iface->nets.nr_lastnet))
  478. riface = iface;
  479. if (ga->sat_addr.s_net == iface->address.s_net &&
  480. ga->sat_addr.s_node == iface->address.s_node)
  481. riface = iface;
  482. }
  483. read_unlock_bh(&atalk_interfaces_lock);
  484. retval = -ENETUNREACH;
  485. if (!riface)
  486. goto out_unlock;
  487. devhint = riface->dev;
  488. }
  489. if (!rt) {
  490. rt = kzalloc(sizeof(*rt), GFP_ATOMIC);
  491. retval = -ENOBUFS;
  492. if (!rt)
  493. goto out_unlock;
  494. rt->next = atalk_routes;
  495. atalk_routes = rt;
  496. }
  497. /* Fill in the routing entry */
  498. rt->target = ta->sat_addr;
  499. dev_hold(devhint);
  500. rt->dev = devhint;
  501. rt->flags = r->rt_flags;
  502. rt->gateway = ga->sat_addr;
  503. retval = 0;
  504. out_unlock:
  505. write_unlock_bh(&atalk_routes_lock);
  506. out:
  507. return retval;
  508. }
  509. /* Delete a route. Find it and discard it */
  510. static int atrtr_delete(struct atalk_addr * addr)
  511. {
  512. struct atalk_route **r = &atalk_routes;
  513. int retval = 0;
  514. struct atalk_route *tmp;
  515. write_lock_bh(&atalk_routes_lock);
  516. while ((tmp = *r) != NULL) {
  517. if (tmp->target.s_net == addr->s_net &&
  518. (!(tmp->flags&RTF_GATEWAY) ||
  519. tmp->target.s_node == addr->s_node)) {
  520. *r = tmp->next;
  521. dev_put(tmp->dev);
  522. kfree(tmp);
  523. goto out;
  524. }
  525. r = &tmp->next;
  526. }
  527. retval = -ENOENT;
  528. out:
  529. write_unlock_bh(&atalk_routes_lock);
  530. return retval;
  531. }
  532. /*
  533. * Called when a device is downed. Just throw away any routes
  534. * via it.
  535. */
  536. static void atrtr_device_down(struct net_device *dev)
  537. {
  538. struct atalk_route **r = &atalk_routes;
  539. struct atalk_route *tmp;
  540. write_lock_bh(&atalk_routes_lock);
  541. while ((tmp = *r) != NULL) {
  542. if (tmp->dev == dev) {
  543. *r = tmp->next;
  544. dev_put(dev);
  545. kfree(tmp);
  546. } else
  547. r = &tmp->next;
  548. }
  549. write_unlock_bh(&atalk_routes_lock);
  550. if (atrtr_default.dev == dev)
  551. atrtr_set_default(NULL);
  552. }
  553. /* Actually down the interface */
  554. static inline void atalk_dev_down(struct net_device *dev)
  555. {
  556. atrtr_device_down(dev); /* Remove all routes for the device */
  557. aarp_device_down(dev); /* Remove AARP entries for the device */
  558. atif_drop_device(dev); /* Remove the device */
  559. }
  560. /*
  561. * A device event has occurred. Watch for devices going down and
  562. * delete our use of them (iface and route).
  563. */
  564. static int ddp_device_event(struct notifier_block *this, unsigned long event,
  565. void *ptr)
  566. {
  567. struct net_device *dev = ptr;
  568. if (!net_eq(dev_net(dev), &init_net))
  569. return NOTIFY_DONE;
  570. if (event == NETDEV_DOWN)
  571. /* Discard any use of this */
  572. atalk_dev_down(dev);
  573. return NOTIFY_DONE;
  574. }
  575. /* ioctl calls. Shouldn't even need touching */
  576. /* Device configuration ioctl calls */
  577. static int atif_ioctl(int cmd, void __user *arg)
  578. {
  579. static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
  580. struct ifreq atreq;
  581. struct atalk_netrange *nr;
  582. struct sockaddr_at *sa;
  583. struct net_device *dev;
  584. struct atalk_iface *atif;
  585. int ct;
  586. int limit;
  587. struct rtentry rtdef;
  588. int add_route;
  589. if (copy_from_user(&atreq, arg, sizeof(atreq)))
  590. return -EFAULT;
  591. dev = __dev_get_by_name(&init_net, atreq.ifr_name);
  592. if (!dev)
  593. return -ENODEV;
  594. sa = (struct sockaddr_at *)&atreq.ifr_addr;
  595. atif = atalk_find_dev(dev);
  596. switch (cmd) {
  597. case SIOCSIFADDR:
  598. if (!capable(CAP_NET_ADMIN))
  599. return -EPERM;
  600. if (sa->sat_family != AF_APPLETALK)
  601. return -EINVAL;
  602. if (dev->type != ARPHRD_ETHER &&
  603. dev->type != ARPHRD_LOOPBACK &&
  604. dev->type != ARPHRD_LOCALTLK &&
  605. dev->type != ARPHRD_PPP)
  606. return -EPROTONOSUPPORT;
  607. nr = (struct atalk_netrange *)&sa->sat_zero[0];
  608. add_route = 1;
  609. /*
  610. * if this is a point-to-point iface, and we already
  611. * have an iface for this AppleTalk address, then we
  612. * should not add a route
  613. */
  614. if ((dev->flags & IFF_POINTOPOINT) &&
  615. atalk_find_interface(sa->sat_addr.s_net,
  616. sa->sat_addr.s_node)) {
  617. printk(KERN_DEBUG "AppleTalk: point-to-point "
  618. "interface added with "
  619. "existing address\n");
  620. add_route = 0;
  621. }
  622. /*
  623. * Phase 1 is fine on LocalTalk but we don't do
  624. * EtherTalk phase 1. Anyone wanting to add it go ahead.
  625. */
  626. if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
  627. return -EPROTONOSUPPORT;
  628. if (sa->sat_addr.s_node == ATADDR_BCAST ||
  629. sa->sat_addr.s_node == 254)
  630. return -EINVAL;
  631. if (atif) {
  632. /* Already setting address */
  633. if (atif->status & ATIF_PROBE)
  634. return -EBUSY;
  635. atif->address.s_net = sa->sat_addr.s_net;
  636. atif->address.s_node = sa->sat_addr.s_node;
  637. atrtr_device_down(dev); /* Flush old routes */
  638. } else {
  639. atif = atif_add_device(dev, &sa->sat_addr);
  640. if (!atif)
  641. return -ENOMEM;
  642. }
  643. atif->nets = *nr;
  644. /*
  645. * Check if the chosen address is used. If so we
  646. * error and atalkd will try another.
  647. */
  648. if (!(dev->flags & IFF_LOOPBACK) &&
  649. !(dev->flags & IFF_POINTOPOINT) &&
  650. atif_probe_device(atif) < 0) {
  651. atif_drop_device(dev);
  652. return -EADDRINUSE;
  653. }
  654. /* Hey it worked - add the direct routes */
  655. sa = (struct sockaddr_at *)&rtdef.rt_gateway;
  656. sa->sat_family = AF_APPLETALK;
  657. sa->sat_addr.s_net = atif->address.s_net;
  658. sa->sat_addr.s_node = atif->address.s_node;
  659. sa = (struct sockaddr_at *)&rtdef.rt_dst;
  660. rtdef.rt_flags = RTF_UP;
  661. sa->sat_family = AF_APPLETALK;
  662. sa->sat_addr.s_node = ATADDR_ANYNODE;
  663. if (dev->flags & IFF_LOOPBACK ||
  664. dev->flags & IFF_POINTOPOINT)
  665. rtdef.rt_flags |= RTF_HOST;
  666. /* Routerless initial state */
  667. if (nr->nr_firstnet == htons(0) &&
  668. nr->nr_lastnet == htons(0xFFFE)) {
  669. sa->sat_addr.s_net = atif->address.s_net;
  670. atrtr_create(&rtdef, dev);
  671. atrtr_set_default(dev);
  672. } else {
  673. limit = ntohs(nr->nr_lastnet);
  674. if (limit - ntohs(nr->nr_firstnet) > 4096) {
  675. printk(KERN_WARNING "Too many routes/"
  676. "iface.\n");
  677. return -EINVAL;
  678. }
  679. if (add_route)
  680. for (ct = ntohs(nr->nr_firstnet);
  681. ct <= limit; ct++) {
  682. sa->sat_addr.s_net = htons(ct);
  683. atrtr_create(&rtdef, dev);
  684. }
  685. }
  686. dev_mc_add_global(dev, aarp_mcast);
  687. return 0;
  688. case SIOCGIFADDR:
  689. if (!atif)
  690. return -EADDRNOTAVAIL;
  691. sa->sat_family = AF_APPLETALK;
  692. sa->sat_addr = atif->address;
  693. break;
  694. case SIOCGIFBRDADDR:
  695. if (!atif)
  696. return -EADDRNOTAVAIL;
  697. sa->sat_family = AF_APPLETALK;
  698. sa->sat_addr.s_net = atif->address.s_net;
  699. sa->sat_addr.s_node = ATADDR_BCAST;
  700. break;
  701. case SIOCATALKDIFADDR:
  702. case SIOCDIFADDR:
  703. if (!capable(CAP_NET_ADMIN))
  704. return -EPERM;
  705. if (sa->sat_family != AF_APPLETALK)
  706. return -EINVAL;
  707. atalk_dev_down(dev);
  708. break;
  709. case SIOCSARP:
  710. if (!capable(CAP_NET_ADMIN))
  711. return -EPERM;
  712. if (sa->sat_family != AF_APPLETALK)
  713. return -EINVAL;
  714. /*
  715. * for now, we only support proxy AARP on ELAP;
  716. * we should be able to do it for LocalTalk, too.
  717. */
  718. if (dev->type != ARPHRD_ETHER)
  719. return -EPROTONOSUPPORT;
  720. /*
  721. * atif points to the current interface on this network;
  722. * we aren't concerned about its current status (at
  723. * least for now), but it has all the settings about
  724. * the network we're going to probe. Consequently, it
  725. * must exist.
  726. */
  727. if (!atif)
  728. return -EADDRNOTAVAIL;
  729. nr = (struct atalk_netrange *)&(atif->nets);
  730. /*
  731. * Phase 1 is fine on Localtalk but we don't do
  732. * Ethertalk phase 1. Anyone wanting to add it go ahead.
  733. */
  734. if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
  735. return -EPROTONOSUPPORT;
  736. if (sa->sat_addr.s_node == ATADDR_BCAST ||
  737. sa->sat_addr.s_node == 254)
  738. return -EINVAL;
  739. /*
  740. * Check if the chosen address is used. If so we
  741. * error and ATCP will try another.
  742. */
  743. if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
  744. return -EADDRINUSE;
  745. /*
  746. * We now have an address on the local network, and
  747. * the AARP code will defend it for us until we take it
  748. * down. We don't set up any routes right now, because
  749. * ATCP will install them manually via SIOCADDRT.
  750. */
  751. break;
  752. case SIOCDARP:
  753. if (!capable(CAP_NET_ADMIN))
  754. return -EPERM;
  755. if (sa->sat_family != AF_APPLETALK)
  756. return -EINVAL;
  757. if (!atif)
  758. return -EADDRNOTAVAIL;
  759. /* give to aarp module to remove proxy entry */
  760. aarp_proxy_remove(atif->dev, &(sa->sat_addr));
  761. return 0;
  762. }
  763. return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
  764. }
  765. /* Routing ioctl() calls */
  766. static int atrtr_ioctl(unsigned int cmd, void __user *arg)
  767. {
  768. struct rtentry rt;
  769. if (copy_from_user(&rt, arg, sizeof(rt)))
  770. return -EFAULT;
  771. switch (cmd) {
  772. case SIOCDELRT:
  773. if (rt.rt_dst.sa_family != AF_APPLETALK)
  774. return -EINVAL;
  775. return atrtr_delete(&((struct sockaddr_at *)
  776. &rt.rt_dst)->sat_addr);
  777. case SIOCADDRT: {
  778. struct net_device *dev = NULL;
  779. if (rt.rt_dev) {
  780. char name[IFNAMSIZ];
  781. if (copy_from_user(name, rt.rt_dev, IFNAMSIZ-1))
  782. return -EFAULT;
  783. name[IFNAMSIZ-1] = '\0';
  784. dev = __dev_get_by_name(&init_net, name);
  785. if (!dev)
  786. return -ENODEV;
  787. }
  788. return atrtr_create(&rt, dev);
  789. }
  790. }
  791. return -EINVAL;
  792. }
  793. /**************************************************************************\
  794. * *
  795. * Handling for system calls applied via the various interfaces to an *
  796. * AppleTalk socket object. *
  797. * *
  798. \**************************************************************************/
  799. /*
  800. * Checksum: This is 'optional'. It's quite likely also a good
  801. * candidate for assembler hackery 8)
  802. */
  803. static unsigned long atalk_sum_partial(const unsigned char *data,
  804. int len, unsigned long sum)
  805. {
  806. /* This ought to be unwrapped neatly. I'll trust gcc for now */
  807. while (len--) {
  808. sum += *data++;
  809. sum = rol16(sum, 1);
  810. }
  811. return sum;
  812. }
  813. /* Checksum skb data -- similar to skb_checksum */
  814. static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
  815. int len, unsigned long sum)
  816. {
  817. int start = skb_headlen(skb);
  818. struct sk_buff *frag_iter;
  819. int i, copy;
  820. /* checksum stuff in header space */
  821. if ( (copy = start - offset) > 0) {
  822. if (copy > len)
  823. copy = len;
  824. sum = atalk_sum_partial(skb->data + offset, copy, sum);
  825. if ( (len -= copy) == 0)
  826. return sum;
  827. offset += copy;
  828. }
  829. /* checksum stuff in frags */
  830. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  831. int end;
  832. WARN_ON(start > offset + len);
  833. end = start + skb_shinfo(skb)->frags[i].size;
  834. if ((copy = end - offset) > 0) {
  835. u8 *vaddr;
  836. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  837. if (copy > len)
  838. copy = len;
  839. vaddr = kmap_skb_frag(frag);
  840. sum = atalk_sum_partial(vaddr + frag->page_offset +
  841. offset - start, copy, sum);
  842. kunmap_skb_frag(vaddr);
  843. if (!(len -= copy))
  844. return sum;
  845. offset += copy;
  846. }
  847. start = end;
  848. }
  849. skb_walk_frags(skb, frag_iter) {
  850. int end;
  851. WARN_ON(start > offset + len);
  852. end = start + frag_iter->len;
  853. if ((copy = end - offset) > 0) {
  854. if (copy > len)
  855. copy = len;
  856. sum = atalk_sum_skb(frag_iter, offset - start,
  857. copy, sum);
  858. if ((len -= copy) == 0)
  859. return sum;
  860. offset += copy;
  861. }
  862. start = end;
  863. }
  864. BUG_ON(len > 0);
  865. return sum;
  866. }
  867. static __be16 atalk_checksum(const struct sk_buff *skb, int len)
  868. {
  869. unsigned long sum;
  870. /* skip header 4 bytes */
  871. sum = atalk_sum_skb(skb, 4, len-4, 0);
  872. /* Use 0xFFFF for 0. 0 itself means none */
  873. return sum ? htons((unsigned short)sum) : htons(0xFFFF);
  874. }
  875. static struct proto ddp_proto = {
  876. .name = "DDP",
  877. .owner = THIS_MODULE,
  878. .obj_size = sizeof(struct atalk_sock),
  879. };
  880. /*
  881. * Create a socket. Initialise the socket, blank the addresses
  882. * set the state.
  883. */
  884. static int atalk_create(struct net *net, struct socket *sock, int protocol,
  885. int kern)
  886. {
  887. struct sock *sk;
  888. int rc = -ESOCKTNOSUPPORT;
  889. if (!net_eq(net, &init_net))
  890. return -EAFNOSUPPORT;
  891. /*
  892. * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
  893. * and gives you the full ELAP frame. Should be handy for CAP 8)
  894. */
  895. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  896. goto out;
  897. rc = -ENOMEM;
  898. sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto);
  899. if (!sk)
  900. goto out;
  901. rc = 0;
  902. sock->ops = &atalk_dgram_ops;
  903. sock_init_data(sock, sk);
  904. /* Checksums on by default */
  905. sock_set_flag(sk, SOCK_ZAPPED);
  906. out:
  907. return rc;
  908. }
  909. /* Free a socket. No work needed */
  910. static int atalk_release(struct socket *sock)
  911. {
  912. struct sock *sk = sock->sk;
  913. if (sk) {
  914. sock_hold(sk);
  915. lock_sock(sk);
  916. sock_orphan(sk);
  917. sock->sk = NULL;
  918. atalk_destroy_socket(sk);
  919. release_sock(sk);
  920. sock_put(sk);
  921. }
  922. return 0;
  923. }
  924. /**
  925. * atalk_pick_and_bind_port - Pick a source port when one is not given
  926. * @sk - socket to insert into the tables
  927. * @sat - address to search for
  928. *
  929. * Pick a source port when one is not given. If we can find a suitable free
  930. * one, we insert the socket into the tables using it.
  931. *
  932. * This whole operation must be atomic.
  933. */
  934. static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
  935. {
  936. int retval;
  937. write_lock_bh(&atalk_sockets_lock);
  938. for (sat->sat_port = ATPORT_RESERVED;
  939. sat->sat_port < ATPORT_LAST;
  940. sat->sat_port++) {
  941. struct sock *s;
  942. struct hlist_node *node;
  943. sk_for_each(s, node, &atalk_sockets) {
  944. struct atalk_sock *at = at_sk(s);
  945. if (at->src_net == sat->sat_addr.s_net &&
  946. at->src_node == sat->sat_addr.s_node &&
  947. at->src_port == sat->sat_port)
  948. goto try_next_port;
  949. }
  950. /* Wheee, it's free, assign and insert. */
  951. __atalk_insert_socket(sk);
  952. at_sk(sk)->src_port = sat->sat_port;
  953. retval = 0;
  954. goto out;
  955. try_next_port:;
  956. }
  957. retval = -EBUSY;
  958. out:
  959. write_unlock_bh(&atalk_sockets_lock);
  960. return retval;
  961. }
  962. static int atalk_autobind(struct sock *sk)
  963. {
  964. struct atalk_sock *at = at_sk(sk);
  965. struct sockaddr_at sat;
  966. struct atalk_addr *ap = atalk_find_primary();
  967. int n = -EADDRNOTAVAIL;
  968. if (!ap || ap->s_net == htons(ATADDR_ANYNET))
  969. goto out;
  970. at->src_net = sat.sat_addr.s_net = ap->s_net;
  971. at->src_node = sat.sat_addr.s_node = ap->s_node;
  972. n = atalk_pick_and_bind_port(sk, &sat);
  973. if (!n)
  974. sock_reset_flag(sk, SOCK_ZAPPED);
  975. out:
  976. return n;
  977. }
  978. /* Set the address 'our end' of the connection */
  979. static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  980. {
  981. struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
  982. struct sock *sk = sock->sk;
  983. struct atalk_sock *at = at_sk(sk);
  984. int err;
  985. if (!sock_flag(sk, SOCK_ZAPPED) ||
  986. addr_len != sizeof(struct sockaddr_at))
  987. return -EINVAL;
  988. if (addr->sat_family != AF_APPLETALK)
  989. return -EAFNOSUPPORT;
  990. lock_sock(sk);
  991. if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
  992. struct atalk_addr *ap = atalk_find_primary();
  993. err = -EADDRNOTAVAIL;
  994. if (!ap)
  995. goto out;
  996. at->src_net = addr->sat_addr.s_net = ap->s_net;
  997. at->src_node = addr->sat_addr.s_node= ap->s_node;
  998. } else {
  999. err = -EADDRNOTAVAIL;
  1000. if (!atalk_find_interface(addr->sat_addr.s_net,
  1001. addr->sat_addr.s_node))
  1002. goto out;
  1003. at->src_net = addr->sat_addr.s_net;
  1004. at->src_node = addr->sat_addr.s_node;
  1005. }
  1006. if (addr->sat_port == ATADDR_ANYPORT) {
  1007. err = atalk_pick_and_bind_port(sk, addr);
  1008. if (err < 0)
  1009. goto out;
  1010. } else {
  1011. at->src_port = addr->sat_port;
  1012. err = -EADDRINUSE;
  1013. if (atalk_find_or_insert_socket(sk, addr))
  1014. goto out;
  1015. }
  1016. sock_reset_flag(sk, SOCK_ZAPPED);
  1017. err = 0;
  1018. out:
  1019. release_sock(sk);
  1020. return err;
  1021. }
  1022. /* Set the address we talk to */
  1023. static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
  1024. int addr_len, int flags)
  1025. {
  1026. struct sock *sk = sock->sk;
  1027. struct atalk_sock *at = at_sk(sk);
  1028. struct sockaddr_at *addr;
  1029. int err;
  1030. sk->sk_state = TCP_CLOSE;
  1031. sock->state = SS_UNCONNECTED;
  1032. if (addr_len != sizeof(*addr))
  1033. return -EINVAL;
  1034. addr = (struct sockaddr_at *)uaddr;
  1035. if (addr->sat_family != AF_APPLETALK)
  1036. return -EAFNOSUPPORT;
  1037. if (addr->sat_addr.s_node == ATADDR_BCAST &&
  1038. !sock_flag(sk, SOCK_BROADCAST)) {
  1039. #if 1
  1040. printk(KERN_WARNING "%s is broken and did not set "
  1041. "SO_BROADCAST. It will break when 2.2 is "
  1042. "released.\n",
  1043. current->comm);
  1044. #else
  1045. return -EACCES;
  1046. #endif
  1047. }
  1048. lock_sock(sk);
  1049. err = -EBUSY;
  1050. if (sock_flag(sk, SOCK_ZAPPED))
  1051. if (atalk_autobind(sk) < 0)
  1052. goto out;
  1053. err = -ENETUNREACH;
  1054. if (!atrtr_get_dev(&addr->sat_addr))
  1055. goto out;
  1056. at->dest_port = addr->sat_port;
  1057. at->dest_net = addr->sat_addr.s_net;
  1058. at->dest_node = addr->sat_addr.s_node;
  1059. sock->state = SS_CONNECTED;
  1060. sk->sk_state = TCP_ESTABLISHED;
  1061. err = 0;
  1062. out:
  1063. release_sock(sk);
  1064. return err;
  1065. }
  1066. /*
  1067. * Find the name of an AppleTalk socket. Just copy the right
  1068. * fields into the sockaddr.
  1069. */
  1070. static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
  1071. int *uaddr_len, int peer)
  1072. {
  1073. struct sockaddr_at sat;
  1074. struct sock *sk = sock->sk;
  1075. struct atalk_sock *at = at_sk(sk);
  1076. int err;
  1077. lock_sock(sk);
  1078. err = -ENOBUFS;
  1079. if (sock_flag(sk, SOCK_ZAPPED))
  1080. if (atalk_autobind(sk) < 0)
  1081. goto out;
  1082. *uaddr_len = sizeof(struct sockaddr_at);
  1083. memset(&sat.sat_zero, 0, sizeof(sat.sat_zero));
  1084. if (peer) {
  1085. err = -ENOTCONN;
  1086. if (sk->sk_state != TCP_ESTABLISHED)
  1087. goto out;
  1088. sat.sat_addr.s_net = at->dest_net;
  1089. sat.sat_addr.s_node = at->dest_node;
  1090. sat.sat_port = at->dest_port;
  1091. } else {
  1092. sat.sat_addr.s_net = at->src_net;
  1093. sat.sat_addr.s_node = at->src_node;
  1094. sat.sat_port = at->src_port;
  1095. }
  1096. err = 0;
  1097. sat.sat_family = AF_APPLETALK;
  1098. memcpy(uaddr, &sat, sizeof(sat));
  1099. out:
  1100. release_sock(sk);
  1101. return err;
  1102. }
  1103. #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
  1104. static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
  1105. {
  1106. return skb->data[12] == 22;
  1107. }
  1108. static int handle_ip_over_ddp(struct sk_buff *skb)
  1109. {
  1110. struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
  1111. struct net_device_stats *stats;
  1112. /* This needs to be able to handle ipddp"N" devices */
  1113. if (!dev) {
  1114. kfree_skb(skb);
  1115. return NET_RX_DROP;
  1116. }
  1117. skb->protocol = htons(ETH_P_IP);
  1118. skb_pull(skb, 13);
  1119. skb->dev = dev;
  1120. skb_reset_transport_header(skb);
  1121. stats = netdev_priv(dev);
  1122. stats->rx_packets++;
  1123. stats->rx_bytes += skb->len + 13;
  1124. return netif_rx(skb); /* Send the SKB up to a higher place. */
  1125. }
  1126. #else
  1127. /* make it easy for gcc to optimize this test out, i.e. kill the code */
  1128. #define is_ip_over_ddp(skb) 0
  1129. #define handle_ip_over_ddp(skb) 0
  1130. #endif
  1131. static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
  1132. struct ddpehdr *ddp, __u16 len_hops, int origlen)
  1133. {
  1134. struct atalk_route *rt;
  1135. struct atalk_addr ta;
  1136. /*
  1137. * Don't route multicast, etc., packets, or packets sent to "this
  1138. * network"
  1139. */
  1140. if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
  1141. /*
  1142. * FIXME:
  1143. *
  1144. * Can it ever happen that a packet is from a PPP iface and
  1145. * needs to be broadcast onto the default network?
  1146. */
  1147. if (dev->type == ARPHRD_PPP)
  1148. printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
  1149. "packet received from PPP iface\n");
  1150. goto free_it;
  1151. }
  1152. ta.s_net = ddp->deh_dnet;
  1153. ta.s_node = ddp->deh_dnode;
  1154. /* Route the packet */
  1155. rt = atrtr_find(&ta);
  1156. /* increment hops count */
  1157. len_hops += 1 << 10;
  1158. if (!rt || !(len_hops & (15 << 10)))
  1159. goto free_it;
  1160. /* FIXME: use skb->cb to be able to use shared skbs */
  1161. /*
  1162. * Route goes through another gateway, so set the target to the
  1163. * gateway instead.
  1164. */
  1165. if (rt->flags & RTF_GATEWAY) {
  1166. ta.s_net = rt->gateway.s_net;
  1167. ta.s_node = rt->gateway.s_node;
  1168. }
  1169. /* Fix up skb->len field */
  1170. skb_trim(skb, min_t(unsigned int, origlen,
  1171. (rt->dev->hard_header_len +
  1172. ddp_dl->header_length + (len_hops & 1023))));
  1173. /* FIXME: use skb->cb to be able to use shared skbs */
  1174. ddp->deh_len_hops = htons(len_hops);
  1175. /*
  1176. * Send the buffer onwards
  1177. *
  1178. * Now we must always be careful. If it's come from LocalTalk to
  1179. * EtherTalk it might not fit
  1180. *
  1181. * Order matters here: If a packet has to be copied to make a new
  1182. * headroom (rare hopefully) then it won't need unsharing.
  1183. *
  1184. * Note. ddp-> becomes invalid at the realloc.
  1185. */
  1186. if (skb_headroom(skb) < 22) {
  1187. /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
  1188. struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
  1189. kfree_skb(skb);
  1190. skb = nskb;
  1191. } else
  1192. skb = skb_unshare(skb, GFP_ATOMIC);
  1193. /*
  1194. * If the buffer didn't vanish into the lack of space bitbucket we can
  1195. * send it.
  1196. */
  1197. if (skb == NULL)
  1198. goto drop;
  1199. if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
  1200. return NET_RX_DROP;
  1201. return NET_RX_SUCCESS;
  1202. free_it:
  1203. kfree_skb(skb);
  1204. drop:
  1205. return NET_RX_DROP;
  1206. }
  1207. /**
  1208. * atalk_rcv - Receive a packet (in skb) from device dev
  1209. * @skb - packet received
  1210. * @dev - network device where the packet comes from
  1211. * @pt - packet type
  1212. *
  1213. * Receive a packet (in skb) from device dev. This has come from the SNAP
  1214. * decoder, and on entry skb->transport_header is the DDP header, skb->len
  1215. * is the DDP header, skb->len is the DDP length. The physical headers
  1216. * have been extracted. PPP should probably pass frames marked as for this
  1217. * layer. [ie ARPHRD_ETHERTALK]
  1218. */
  1219. static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
  1220. struct packet_type *pt, struct net_device *orig_dev)
  1221. {
  1222. struct ddpehdr *ddp;
  1223. struct sock *sock;
  1224. struct atalk_iface *atif;
  1225. struct sockaddr_at tosat;
  1226. int origlen;
  1227. __u16 len_hops;
  1228. if (!net_eq(dev_net(dev), &init_net))
  1229. goto drop;
  1230. /* Don't mangle buffer if shared */
  1231. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  1232. goto out;
  1233. /* Size check and make sure header is contiguous */
  1234. if (!pskb_may_pull(skb, sizeof(*ddp)))
  1235. goto drop;
  1236. ddp = ddp_hdr(skb);
  1237. len_hops = ntohs(ddp->deh_len_hops);
  1238. /* Trim buffer in case of stray trailing data */
  1239. origlen = skb->len;
  1240. skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
  1241. /*
  1242. * Size check to see if ddp->deh_len was crap
  1243. * (Otherwise we'll detonate most spectacularly
  1244. * in the middle of atalk_checksum() or recvmsg()).
  1245. */
  1246. if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
  1247. pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
  1248. "skb->len=%u)\n", len_hops & 1023, skb->len);
  1249. goto drop;
  1250. }
  1251. /*
  1252. * Any checksums. Note we don't do htons() on this == is assumed to be
  1253. * valid for net byte orders all over the networking code...
  1254. */
  1255. if (ddp->deh_sum &&
  1256. atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
  1257. /* Not a valid AppleTalk frame - dustbin time */
  1258. goto drop;
  1259. /* Check the packet is aimed at us */
  1260. if (!ddp->deh_dnet) /* Net 0 is 'this network' */
  1261. atif = atalk_find_anynet(ddp->deh_dnode, dev);
  1262. else
  1263. atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
  1264. if (!atif) {
  1265. /* Not ours, so we route the packet via the correct
  1266. * AppleTalk iface
  1267. */
  1268. return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
  1269. }
  1270. /* if IP over DDP is not selected this code will be optimized out */
  1271. if (is_ip_over_ddp(skb))
  1272. return handle_ip_over_ddp(skb);
  1273. /*
  1274. * Which socket - atalk_search_socket() looks for a *full match*
  1275. * of the <net, node, port> tuple.
  1276. */
  1277. tosat.sat_addr.s_net = ddp->deh_dnet;
  1278. tosat.sat_addr.s_node = ddp->deh_dnode;
  1279. tosat.sat_port = ddp->deh_dport;
  1280. sock = atalk_search_socket(&tosat, atif);
  1281. if (!sock) /* But not one of our sockets */
  1282. goto drop;
  1283. /* Queue packet (standard) */
  1284. skb->sk = sock;
  1285. if (sock_queue_rcv_skb(sock, skb) < 0)
  1286. goto drop;
  1287. return NET_RX_SUCCESS;
  1288. drop:
  1289. kfree_skb(skb);
  1290. out:
  1291. return NET_RX_DROP;
  1292. }
  1293. /*
  1294. * Receive a LocalTalk frame. We make some demands on the caller here.
  1295. * Caller must provide enough headroom on the packet to pull the short
  1296. * header and append a long one.
  1297. */
  1298. static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
  1299. struct packet_type *pt, struct net_device *orig_dev)
  1300. {
  1301. if (!net_eq(dev_net(dev), &init_net))
  1302. goto freeit;
  1303. /* Expand any short form frames */
  1304. if (skb_mac_header(skb)[2] == 1) {
  1305. struct ddpehdr *ddp;
  1306. /* Find our address */
  1307. struct atalk_addr *ap = atalk_find_dev_addr(dev);
  1308. if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
  1309. goto freeit;
  1310. /* Don't mangle buffer if shared */
  1311. if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
  1312. return 0;
  1313. /*
  1314. * The push leaves us with a ddephdr not an shdr, and
  1315. * handily the port bytes in the right place preset.
  1316. */
  1317. ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4);
  1318. /* Now fill in the long header */
  1319. /*
  1320. * These two first. The mac overlays the new source/dest
  1321. * network information so we MUST copy these before
  1322. * we write the network numbers !
  1323. */
  1324. ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */
  1325. ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */
  1326. ddp->deh_dnet = ap->s_net; /* Network number */
  1327. ddp->deh_snet = ap->s_net;
  1328. ddp->deh_sum = 0; /* No checksum */
  1329. /*
  1330. * Not sure about this bit...
  1331. */
  1332. /* Non routable, so force a drop if we slip up later */
  1333. ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
  1334. }
  1335. skb_reset_transport_header(skb);
  1336. return atalk_rcv(skb, dev, pt, orig_dev);
  1337. freeit:
  1338. kfree_skb(skb);
  1339. return 0;
  1340. }
  1341. static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  1342. size_t len)
  1343. {
  1344. struct sock *sk = sock->sk;
  1345. struct atalk_sock *at = at_sk(sk);
  1346. struct sockaddr_at *usat = (struct sockaddr_at *)msg->msg_name;
  1347. int flags = msg->msg_flags;
  1348. int loopback = 0;
  1349. struct sockaddr_at local_satalk, gsat;
  1350. struct sk_buff *skb;
  1351. struct net_device *dev;
  1352. struct ddpehdr *ddp;
  1353. int size;
  1354. struct atalk_route *rt;
  1355. int err;
  1356. if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
  1357. return -EINVAL;
  1358. if (len > DDP_MAXSZ)
  1359. return -EMSGSIZE;
  1360. lock_sock(sk);
  1361. if (usat) {
  1362. err = -EBUSY;
  1363. if (sock_flag(sk, SOCK_ZAPPED))
  1364. if (atalk_autobind(sk) < 0)
  1365. goto out;
  1366. err = -EINVAL;
  1367. if (msg->msg_namelen < sizeof(*usat) ||
  1368. usat->sat_family != AF_APPLETALK)
  1369. goto out;
  1370. err = -EPERM;
  1371. /* netatalk didn't implement this check */
  1372. if (usat->sat_addr.s_node == ATADDR_BCAST &&
  1373. !sock_flag(sk, SOCK_BROADCAST)) {
  1374. goto out;
  1375. }
  1376. } else {
  1377. err = -ENOTCONN;
  1378. if (sk->sk_state != TCP_ESTABLISHED)
  1379. goto out;
  1380. usat = &local_satalk;
  1381. usat->sat_family = AF_APPLETALK;
  1382. usat->sat_port = at->dest_port;
  1383. usat->sat_addr.s_node = at->dest_node;
  1384. usat->sat_addr.s_net = at->dest_net;
  1385. }
  1386. /* Build a packet */
  1387. SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);
  1388. /* For headers */
  1389. size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
  1390. if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
  1391. rt = atrtr_find(&usat->sat_addr);
  1392. } else {
  1393. struct atalk_addr at_hint;
  1394. at_hint.s_node = 0;
  1395. at_hint.s_net = at->src_net;
  1396. rt = atrtr_find(&at_hint);
  1397. }
  1398. err = ENETUNREACH;
  1399. if (!rt)
  1400. goto out;
  1401. dev = rt->dev;
  1402. SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
  1403. sk, size, dev->name);
  1404. size += dev->hard_header_len;
  1405. release_sock(sk);
  1406. skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
  1407. lock_sock(sk);
  1408. if (!skb)
  1409. goto out;
  1410. skb->sk = sk;
  1411. skb_reserve(skb, ddp_dl->header_length);
  1412. skb_reserve(skb, dev->hard_header_len);
  1413. skb->dev = dev;
  1414. SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
  1415. ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
  1416. ddp->deh_len_hops = htons(len + sizeof(*ddp));
  1417. ddp->deh_dnet = usat->sat_addr.s_net;
  1418. ddp->deh_snet = at->src_net;
  1419. ddp->deh_dnode = usat->sat_addr.s_node;
  1420. ddp->deh_snode = at->src_node;
  1421. ddp->deh_dport = usat->sat_port;
  1422. ddp->deh_sport = at->src_port;
  1423. SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len);
  1424. err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  1425. if (err) {
  1426. kfree_skb(skb);
  1427. err = -EFAULT;
  1428. goto out;
  1429. }
  1430. if (sk->sk_no_check == 1)
  1431. ddp->deh_sum = 0;
  1432. else
  1433. ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
  1434. /*
  1435. * Loopback broadcast packets to non gateway targets (ie routes
  1436. * to group we are in)
  1437. */
  1438. if (ddp->deh_dnode == ATADDR_BCAST &&
  1439. !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
  1440. struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
  1441. if (skb2) {
  1442. loopback = 1;
  1443. SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
  1444. /*
  1445. * If it fails it is queued/sent above in the aarp queue
  1446. */
  1447. aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
  1448. }
  1449. }
  1450. if (dev->flags & IFF_LOOPBACK || loopback) {
  1451. SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
  1452. /* loop back */
  1453. skb_orphan(skb);
  1454. if (ddp->deh_dnode == ATADDR_BCAST) {
  1455. struct atalk_addr at_lo;
  1456. at_lo.s_node = 0;
  1457. at_lo.s_net = 0;
  1458. rt = atrtr_find(&at_lo);
  1459. if (!rt) {
  1460. kfree_skb(skb);
  1461. err = -ENETUNREACH;
  1462. goto out;
  1463. }
  1464. dev = rt->dev;
  1465. skb->dev = dev;
  1466. }
  1467. ddp_dl->request(ddp_dl, skb, dev->dev_addr);
  1468. } else {
  1469. SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
  1470. if (rt->flags & RTF_GATEWAY) {
  1471. gsat.sat_addr = rt->gateway;
  1472. usat = &gsat;
  1473. }
  1474. /*
  1475. * If it fails it is queued/sent above in the aarp queue
  1476. */
  1477. aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
  1478. }
  1479. SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len);
  1480. out:
  1481. release_sock(sk);
  1482. return err ? : len;
  1483. }
  1484. static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  1485. size_t size, int flags)
  1486. {
  1487. struct sock *sk = sock->sk;
  1488. struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name;
  1489. struct ddpehdr *ddp;
  1490. int copied = 0;
  1491. int offset = 0;
  1492. int err = 0;
  1493. struct sk_buff *skb;
  1494. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  1495. flags & MSG_DONTWAIT, &err);
  1496. lock_sock(sk);
  1497. if (!skb)
  1498. goto out;
  1499. /* FIXME: use skb->cb to be able to use shared skbs */
  1500. ddp = ddp_hdr(skb);
  1501. copied = ntohs(ddp->deh_len_hops) & 1023;
  1502. if (sk->sk_type != SOCK_RAW) {
  1503. offset = sizeof(*ddp);
  1504. copied -= offset;
  1505. }
  1506. if (copied > size) {
  1507. copied = size;
  1508. msg->msg_flags |= MSG_TRUNC;
  1509. }
  1510. err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied);
  1511. if (!err) {
  1512. if (sat) {
  1513. sat->sat_family = AF_APPLETALK;
  1514. sat->sat_port = ddp->deh_sport;
  1515. sat->sat_addr.s_node = ddp->deh_snode;
  1516. sat->sat_addr.s_net = ddp->deh_snet;
  1517. }
  1518. msg->msg_namelen = sizeof(*sat);
  1519. }
  1520. skb_free_datagram(sk, skb); /* Free the datagram. */
  1521. out:
  1522. release_sock(sk);
  1523. return err ? : copied;
  1524. }
  1525. /*
  1526. * AppleTalk ioctl calls.
  1527. */
  1528. static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1529. {
  1530. int rc = -ENOIOCTLCMD;
  1531. struct sock *sk = sock->sk;
  1532. void __user *argp = (void __user *)arg;
  1533. switch (cmd) {
  1534. /* Protocol layer */
  1535. case TIOCOUTQ: {
  1536. long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1537. if (amount < 0)
  1538. amount = 0;
  1539. rc = put_user(amount, (int __user *)argp);
  1540. break;
  1541. }
  1542. case TIOCINQ: {
  1543. /*
  1544. * These two are safe on a single CPU system as only
  1545. * user tasks fiddle here
  1546. */
  1547. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  1548. long amount = 0;
  1549. if (skb)
  1550. amount = skb->len - sizeof(struct ddpehdr);
  1551. rc = put_user(amount, (int __user *)argp);
  1552. break;
  1553. }
  1554. case SIOCGSTAMP:
  1555. rc = sock_get_timestamp(sk, argp);
  1556. break;
  1557. case SIOCGSTAMPNS:
  1558. rc = sock_get_timestampns(sk, argp);
  1559. break;
  1560. /* Routing */
  1561. case SIOCADDRT:
  1562. case SIOCDELRT:
  1563. rc = -EPERM;
  1564. if (capable(CAP_NET_ADMIN))
  1565. rc = atrtr_ioctl(cmd, argp);
  1566. break;
  1567. /* Interface */
  1568. case SIOCGIFADDR:
  1569. case SIOCSIFADDR:
  1570. case SIOCGIFBRDADDR:
  1571. case SIOCATALKDIFADDR:
  1572. case SIOCDIFADDR:
  1573. case SIOCSARP: /* proxy AARP */
  1574. case SIOCDARP: /* proxy AARP */
  1575. rtnl_lock();
  1576. rc = atif_ioctl(cmd, argp);
  1577. rtnl_unlock();
  1578. break;
  1579. }
  1580. return rc;
  1581. }
  1582. #ifdef CONFIG_COMPAT
  1583. static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1584. {
  1585. /*
  1586. * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
  1587. * cannot handle it in common code. The data we access if ifreq
  1588. * here is compatible, so we can simply call the native
  1589. * handler.
  1590. */
  1591. if (cmd == SIOCATALKDIFADDR)
  1592. return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
  1593. return -ENOIOCTLCMD;
  1594. }
  1595. #endif
  1596. static const struct net_proto_family atalk_family_ops = {
  1597. .family = PF_APPLETALK,
  1598. .create = atalk_create,
  1599. .owner = THIS_MODULE,
  1600. };
  1601. static const struct proto_ops atalk_dgram_ops = {
  1602. .family = PF_APPLETALK,
  1603. .owner = THIS_MODULE,
  1604. .release = atalk_release,
  1605. .bind = atalk_bind,
  1606. .connect = atalk_connect,
  1607. .socketpair = sock_no_socketpair,
  1608. .accept = sock_no_accept,
  1609. .getname = atalk_getname,
  1610. .poll = datagram_poll,
  1611. .ioctl = atalk_ioctl,
  1612. #ifdef CONFIG_COMPAT
  1613. .compat_ioctl = atalk_compat_ioctl,
  1614. #endif
  1615. .listen = sock_no_listen,
  1616. .shutdown = sock_no_shutdown,
  1617. .setsockopt = sock_no_setsockopt,
  1618. .getsockopt = sock_no_getsockopt,
  1619. .sendmsg = atalk_sendmsg,
  1620. .recvmsg = atalk_recvmsg,
  1621. .mmap = sock_no_mmap,
  1622. .sendpage = sock_no_sendpage,
  1623. };
  1624. static struct notifier_block ddp_notifier = {
  1625. .notifier_call = ddp_device_event,
  1626. };
  1627. static struct packet_type ltalk_packet_type __read_mostly = {
  1628. .type = cpu_to_be16(ETH_P_LOCALTALK),
  1629. .func = ltalk_rcv,
  1630. };
  1631. static struct packet_type ppptalk_packet_type __read_mostly = {
  1632. .type = cpu_to_be16(ETH_P_PPPTALK),
  1633. .func = atalk_rcv,
  1634. };
  1635. static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
  1636. /* Export symbols for use by drivers when AppleTalk is a module */
  1637. EXPORT_SYMBOL(atrtr_get_dev);
  1638. EXPORT_SYMBOL(atalk_find_dev_addr);
  1639. static const char atalk_err_snap[] __initconst =
  1640. KERN_CRIT "Unable to register DDP with SNAP.\n";
  1641. /* Called by proto.c on kernel start up */
  1642. static int __init atalk_init(void)
  1643. {
  1644. int rc = proto_register(&ddp_proto, 0);
  1645. if (rc != 0)
  1646. goto out;
  1647. (void)sock_register(&atalk_family_ops);
  1648. ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
  1649. if (!ddp_dl)
  1650. printk(atalk_err_snap);
  1651. dev_add_pack(&ltalk_packet_type);
  1652. dev_add_pack(&ppptalk_packet_type);
  1653. register_netdevice_notifier(&ddp_notifier);
  1654. aarp_proto_init();
  1655. atalk_proc_init();
  1656. atalk_register_sysctl();
  1657. out:
  1658. return rc;
  1659. }
  1660. module_init(atalk_init);
  1661. /*
  1662. * No explicit module reference count manipulation is needed in the
  1663. * protocol. Socket layer sets module reference count for us
  1664. * and interfaces reference counting is done
  1665. * by the network device layer.
  1666. *
  1667. * Ergo, before the AppleTalk module can be removed, all AppleTalk
  1668. * sockets be closed from user space.
  1669. */
  1670. static void __exit atalk_exit(void)
  1671. {
  1672. #ifdef CONFIG_SYSCTL
  1673. atalk_unregister_sysctl();
  1674. #endif /* CONFIG_SYSCTL */
  1675. atalk_proc_exit();
  1676. aarp_cleanup_module(); /* General aarp clean-up. */
  1677. unregister_netdevice_notifier(&ddp_notifier);
  1678. dev_remove_pack(&ltalk_packet_type);
  1679. dev_remove_pack(&ppptalk_packet_type);
  1680. unregister_snap_client(ddp_dl);
  1681. sock_unregister(PF_APPLETALK);
  1682. proto_unregister(&ddp_proto);
  1683. }
  1684. module_exit(atalk_exit);
  1685. MODULE_LICENSE("GPL");
  1686. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  1687. MODULE_DESCRIPTION("AppleTalk 0.20\n");
  1688. MODULE_ALIAS_NETPROTO(PF_APPLETALK);