ddp.c 49 KB

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