ddp.c 49 KB

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