ddp.c 49 KB

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