ddp.c 48 KB

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