ddp.c 48 KB

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