aarp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * AARP: An implementation of the AppleTalk AARP protocol for
  3. * Ethernet 'ELAP'.
  4. *
  5. * Alan Cox <Alan.Cox@linux.org>
  6. *
  7. * This doesn't fit cleanly with the IP arp. Potentially we can use
  8. * the generic neighbour discovery code to clean this up.
  9. *
  10. * FIXME:
  11. * We ought to handle the retransmits with a single list and a
  12. * separate fast timer for when it is needed.
  13. * Use neighbour discovery code.
  14. * Token Ring Support.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. *
  22. * References:
  23. * Inside AppleTalk (2nd Ed).
  24. * Fixes:
  25. * Jaume Grau - flush caches on AARP_PROBE
  26. * Rob Newberry - Added proxy AARP and AARP proc fs,
  27. * moved probing from DDP module.
  28. * Arnaldo C. Melo - don't mangle rx packets
  29. *
  30. */
  31. #include <linux/if_arp.h>
  32. #include <linux/slab.h>
  33. #include <net/sock.h>
  34. #include <net/datalink.h>
  35. #include <net/psnap.h>
  36. #include <linux/atalk.h>
  37. #include <linux/delay.h>
  38. #include <linux/init.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/seq_file.h>
  41. #include <linux/export.h>
  42. #include <linux/etherdevice.h>
  43. int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
  44. int sysctl_aarp_tick_time = AARP_TICK_TIME;
  45. int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
  46. int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
  47. /* Lists of aarp entries */
  48. /**
  49. * struct aarp_entry - AARP entry
  50. * @last_sent - Last time we xmitted the aarp request
  51. * @packet_queue - Queue of frames wait for resolution
  52. * @status - Used for proxy AARP
  53. * expires_at - Entry expiry time
  54. * target_addr - DDP Address
  55. * dev - Device to use
  56. * hwaddr - Physical i/f address of target/router
  57. * xmit_count - When this hits 10 we give up
  58. * next - Next entry in chain
  59. */
  60. struct aarp_entry {
  61. /* These first two are only used for unresolved entries */
  62. unsigned long last_sent;
  63. struct sk_buff_head packet_queue;
  64. int status;
  65. unsigned long expires_at;
  66. struct atalk_addr target_addr;
  67. struct net_device *dev;
  68. char hwaddr[ETH_ALEN];
  69. unsigned short xmit_count;
  70. struct aarp_entry *next;
  71. };
  72. /* Hashed list of resolved, unresolved and proxy entries */
  73. static struct aarp_entry *resolved[AARP_HASH_SIZE];
  74. static struct aarp_entry *unresolved[AARP_HASH_SIZE];
  75. static struct aarp_entry *proxies[AARP_HASH_SIZE];
  76. static int unresolved_count;
  77. /* One lock protects it all. */
  78. static DEFINE_RWLOCK(aarp_lock);
  79. /* Used to walk the list and purge/kick entries. */
  80. static struct timer_list aarp_timer;
  81. /*
  82. * Delete an aarp queue
  83. *
  84. * Must run under aarp_lock.
  85. */
  86. static void __aarp_expire(struct aarp_entry *a)
  87. {
  88. skb_queue_purge(&a->packet_queue);
  89. kfree(a);
  90. }
  91. /*
  92. * Send an aarp queue entry request
  93. *
  94. * Must run under aarp_lock.
  95. */
  96. static void __aarp_send_query(struct aarp_entry *a)
  97. {
  98. static unsigned char aarp_eth_multicast[ETH_ALEN] =
  99. { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
  100. struct net_device *dev = a->dev;
  101. struct elapaarp *eah;
  102. int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
  103. struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
  104. struct atalk_addr *sat = atalk_find_dev_addr(dev);
  105. if (!skb)
  106. return;
  107. if (!sat) {
  108. kfree_skb(skb);
  109. return;
  110. }
  111. /* Set up the buffer */
  112. skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
  113. skb_reset_network_header(skb);
  114. skb_reset_transport_header(skb);
  115. skb_put(skb, sizeof(*eah));
  116. skb->protocol = htons(ETH_P_ATALK);
  117. skb->dev = dev;
  118. eah = aarp_hdr(skb);
  119. /* Set up the ARP */
  120. eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
  121. eah->pa_type = htons(ETH_P_ATALK);
  122. eah->hw_len = ETH_ALEN;
  123. eah->pa_len = AARP_PA_ALEN;
  124. eah->function = htons(AARP_REQUEST);
  125. ether_addr_copy(eah->hw_src, dev->dev_addr);
  126. eah->pa_src_zero = 0;
  127. eah->pa_src_net = sat->s_net;
  128. eah->pa_src_node = sat->s_node;
  129. eth_zero_addr(eah->hw_dst);
  130. eah->pa_dst_zero = 0;
  131. eah->pa_dst_net = a->target_addr.s_net;
  132. eah->pa_dst_node = a->target_addr.s_node;
  133. /* Send it */
  134. aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
  135. /* Update the sending count */
  136. a->xmit_count++;
  137. a->last_sent = jiffies;
  138. }
  139. /* This runs under aarp_lock and in softint context, so only atomic memory
  140. * allocations can be used. */
  141. static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
  142. struct atalk_addr *them, unsigned char *sha)
  143. {
  144. struct elapaarp *eah;
  145. int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
  146. struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
  147. if (!skb)
  148. return;
  149. /* Set up the buffer */
  150. skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
  151. skb_reset_network_header(skb);
  152. skb_reset_transport_header(skb);
  153. skb_put(skb, sizeof(*eah));
  154. skb->protocol = htons(ETH_P_ATALK);
  155. skb->dev = dev;
  156. eah = aarp_hdr(skb);
  157. /* Set up the ARP */
  158. eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
  159. eah->pa_type = htons(ETH_P_ATALK);
  160. eah->hw_len = ETH_ALEN;
  161. eah->pa_len = AARP_PA_ALEN;
  162. eah->function = htons(AARP_REPLY);
  163. ether_addr_copy(eah->hw_src, dev->dev_addr);
  164. eah->pa_src_zero = 0;
  165. eah->pa_src_net = us->s_net;
  166. eah->pa_src_node = us->s_node;
  167. if (!sha)
  168. eth_zero_addr(eah->hw_dst);
  169. else
  170. ether_addr_copy(eah->hw_dst, sha);
  171. eah->pa_dst_zero = 0;
  172. eah->pa_dst_net = them->s_net;
  173. eah->pa_dst_node = them->s_node;
  174. /* Send it */
  175. aarp_dl->request(aarp_dl, skb, sha);
  176. }
  177. /*
  178. * Send probe frames. Called from aarp_probe_network and
  179. * aarp_proxy_probe_network.
  180. */
  181. static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
  182. {
  183. struct elapaarp *eah;
  184. int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
  185. struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
  186. static unsigned char aarp_eth_multicast[ETH_ALEN] =
  187. { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
  188. if (!skb)
  189. return;
  190. /* Set up the buffer */
  191. skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
  192. skb_reset_network_header(skb);
  193. skb_reset_transport_header(skb);
  194. skb_put(skb, sizeof(*eah));
  195. skb->protocol = htons(ETH_P_ATALK);
  196. skb->dev = dev;
  197. eah = aarp_hdr(skb);
  198. /* Set up the ARP */
  199. eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
  200. eah->pa_type = htons(ETH_P_ATALK);
  201. eah->hw_len = ETH_ALEN;
  202. eah->pa_len = AARP_PA_ALEN;
  203. eah->function = htons(AARP_PROBE);
  204. ether_addr_copy(eah->hw_src, dev->dev_addr);
  205. eah->pa_src_zero = 0;
  206. eah->pa_src_net = us->s_net;
  207. eah->pa_src_node = us->s_node;
  208. eth_zero_addr(eah->hw_dst);
  209. eah->pa_dst_zero = 0;
  210. eah->pa_dst_net = us->s_net;
  211. eah->pa_dst_node = us->s_node;
  212. /* Send it */
  213. aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
  214. }
  215. /*
  216. * Handle an aarp timer expire
  217. *
  218. * Must run under the aarp_lock.
  219. */
  220. static void __aarp_expire_timer(struct aarp_entry **n)
  221. {
  222. struct aarp_entry *t;
  223. while (*n)
  224. /* Expired ? */
  225. if (time_after(jiffies, (*n)->expires_at)) {
  226. t = *n;
  227. *n = (*n)->next;
  228. __aarp_expire(t);
  229. } else
  230. n = &((*n)->next);
  231. }
  232. /*
  233. * Kick all pending requests 5 times a second.
  234. *
  235. * Must run under the aarp_lock.
  236. */
  237. static void __aarp_kick(struct aarp_entry **n)
  238. {
  239. struct aarp_entry *t;
  240. while (*n)
  241. /* Expired: if this will be the 11th tx, we delete instead. */
  242. if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
  243. t = *n;
  244. *n = (*n)->next;
  245. __aarp_expire(t);
  246. } else {
  247. __aarp_send_query(*n);
  248. n = &((*n)->next);
  249. }
  250. }
  251. /*
  252. * A device has gone down. Take all entries referring to the device
  253. * and remove them.
  254. *
  255. * Must run under the aarp_lock.
  256. */
  257. static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
  258. {
  259. struct aarp_entry *t;
  260. while (*n)
  261. if ((*n)->dev == dev) {
  262. t = *n;
  263. *n = (*n)->next;
  264. __aarp_expire(t);
  265. } else
  266. n = &((*n)->next);
  267. }
  268. /* Handle the timer event */
  269. static void aarp_expire_timeout(unsigned long unused)
  270. {
  271. int ct;
  272. write_lock_bh(&aarp_lock);
  273. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  274. __aarp_expire_timer(&resolved[ct]);
  275. __aarp_kick(&unresolved[ct]);
  276. __aarp_expire_timer(&unresolved[ct]);
  277. __aarp_expire_timer(&proxies[ct]);
  278. }
  279. write_unlock_bh(&aarp_lock);
  280. mod_timer(&aarp_timer, jiffies +
  281. (unresolved_count ? sysctl_aarp_tick_time :
  282. sysctl_aarp_expiry_time));
  283. }
  284. /* Network device notifier chain handler. */
  285. static int aarp_device_event(struct notifier_block *this, unsigned long event,
  286. void *ptr)
  287. {
  288. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  289. int ct;
  290. if (!net_eq(dev_net(dev), &init_net))
  291. return NOTIFY_DONE;
  292. if (event == NETDEV_DOWN) {
  293. write_lock_bh(&aarp_lock);
  294. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  295. __aarp_expire_device(&resolved[ct], dev);
  296. __aarp_expire_device(&unresolved[ct], dev);
  297. __aarp_expire_device(&proxies[ct], dev);
  298. }
  299. write_unlock_bh(&aarp_lock);
  300. }
  301. return NOTIFY_DONE;
  302. }
  303. /* Expire all entries in a hash chain */
  304. static void __aarp_expire_all(struct aarp_entry **n)
  305. {
  306. struct aarp_entry *t;
  307. while (*n) {
  308. t = *n;
  309. *n = (*n)->next;
  310. __aarp_expire(t);
  311. }
  312. }
  313. /* Cleanup all hash chains -- module unloading */
  314. static void aarp_purge(void)
  315. {
  316. int ct;
  317. write_lock_bh(&aarp_lock);
  318. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  319. __aarp_expire_all(&resolved[ct]);
  320. __aarp_expire_all(&unresolved[ct]);
  321. __aarp_expire_all(&proxies[ct]);
  322. }
  323. write_unlock_bh(&aarp_lock);
  324. }
  325. /*
  326. * Create a new aarp entry. This must use GFP_ATOMIC because it
  327. * runs while holding spinlocks.
  328. */
  329. static struct aarp_entry *aarp_alloc(void)
  330. {
  331. struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
  332. if (a)
  333. skb_queue_head_init(&a->packet_queue);
  334. return a;
  335. }
  336. /*
  337. * Find an entry. We might return an expired but not yet purged entry. We
  338. * don't care as it will do no harm.
  339. *
  340. * This must run under the aarp_lock.
  341. */
  342. static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
  343. struct net_device *dev,
  344. struct atalk_addr *sat)
  345. {
  346. while (list) {
  347. if (list->target_addr.s_net == sat->s_net &&
  348. list->target_addr.s_node == sat->s_node &&
  349. list->dev == dev)
  350. break;
  351. list = list->next;
  352. }
  353. return list;
  354. }
  355. /* Called from the DDP code, and thus must be exported. */
  356. void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
  357. {
  358. int hash = sa->s_node % (AARP_HASH_SIZE - 1);
  359. struct aarp_entry *a;
  360. write_lock_bh(&aarp_lock);
  361. a = __aarp_find_entry(proxies[hash], dev, sa);
  362. if (a)
  363. a->expires_at = jiffies - 1;
  364. write_unlock_bh(&aarp_lock);
  365. }
  366. /* This must run under aarp_lock. */
  367. static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
  368. struct atalk_addr *sa)
  369. {
  370. int hash = sa->s_node % (AARP_HASH_SIZE - 1);
  371. struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
  372. return a ? sa : NULL;
  373. }
  374. /*
  375. * Probe a Phase 1 device or a device that requires its Net:Node to
  376. * be set via an ioctl.
  377. */
  378. static void aarp_send_probe_phase1(struct atalk_iface *iface)
  379. {
  380. struct ifreq atreq;
  381. struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
  382. const struct net_device_ops *ops = iface->dev->netdev_ops;
  383. sa->sat_addr.s_node = iface->address.s_node;
  384. sa->sat_addr.s_net = ntohs(iface->address.s_net);
  385. /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
  386. if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
  387. ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
  388. if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
  389. iface->address.s_node != sa->sat_addr.s_node)
  390. iface->status |= ATIF_PROBE_FAIL;
  391. iface->address.s_net = htons(sa->sat_addr.s_net);
  392. iface->address.s_node = sa->sat_addr.s_node;
  393. }
  394. }
  395. void aarp_probe_network(struct atalk_iface *atif)
  396. {
  397. if (atif->dev->type == ARPHRD_LOCALTLK ||
  398. atif->dev->type == ARPHRD_PPP)
  399. aarp_send_probe_phase1(atif);
  400. else {
  401. unsigned int count;
  402. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  403. aarp_send_probe(atif->dev, &atif->address);
  404. /* Defer 1/10th */
  405. msleep(100);
  406. if (atif->status & ATIF_PROBE_FAIL)
  407. break;
  408. }
  409. }
  410. }
  411. int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
  412. {
  413. int hash, retval = -EPROTONOSUPPORT;
  414. struct aarp_entry *entry;
  415. unsigned int count;
  416. /*
  417. * we don't currently support LocalTalk or PPP for proxy AARP;
  418. * if someone wants to try and add it, have fun
  419. */
  420. if (atif->dev->type == ARPHRD_LOCALTLK ||
  421. atif->dev->type == ARPHRD_PPP)
  422. goto out;
  423. /*
  424. * create a new AARP entry with the flags set to be published --
  425. * we need this one to hang around even if it's in use
  426. */
  427. entry = aarp_alloc();
  428. retval = -ENOMEM;
  429. if (!entry)
  430. goto out;
  431. entry->expires_at = -1;
  432. entry->status = ATIF_PROBE;
  433. entry->target_addr.s_node = sa->s_node;
  434. entry->target_addr.s_net = sa->s_net;
  435. entry->dev = atif->dev;
  436. write_lock_bh(&aarp_lock);
  437. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  438. entry->next = proxies[hash];
  439. proxies[hash] = entry;
  440. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  441. aarp_send_probe(atif->dev, sa);
  442. /* Defer 1/10th */
  443. write_unlock_bh(&aarp_lock);
  444. msleep(100);
  445. write_lock_bh(&aarp_lock);
  446. if (entry->status & ATIF_PROBE_FAIL)
  447. break;
  448. }
  449. if (entry->status & ATIF_PROBE_FAIL) {
  450. entry->expires_at = jiffies - 1; /* free the entry */
  451. retval = -EADDRINUSE; /* return network full */
  452. } else { /* clear the probing flag */
  453. entry->status &= ~ATIF_PROBE;
  454. retval = 1;
  455. }
  456. write_unlock_bh(&aarp_lock);
  457. out:
  458. return retval;
  459. }
  460. /* Send a DDP frame */
  461. int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
  462. struct atalk_addr *sa, void *hwaddr)
  463. {
  464. static char ddp_eth_multicast[ETH_ALEN] =
  465. { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
  466. int hash;
  467. struct aarp_entry *a;
  468. skb_reset_network_header(skb);
  469. /* Check for LocalTalk first */
  470. if (dev->type == ARPHRD_LOCALTLK) {
  471. struct atalk_addr *at = atalk_find_dev_addr(dev);
  472. struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
  473. int ft = 2;
  474. /*
  475. * Compressible ?
  476. *
  477. * IFF: src_net == dest_net == device_net
  478. * (zero matches anything)
  479. */
  480. if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
  481. (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
  482. skb_pull(skb, sizeof(*ddp) - 4);
  483. /*
  484. * The upper two remaining bytes are the port
  485. * numbers we just happen to need. Now put the
  486. * length in the lower two.
  487. */
  488. *((__be16 *)skb->data) = htons(skb->len);
  489. ft = 1;
  490. }
  491. /*
  492. * Nice and easy. No AARP type protocols occur here so we can
  493. * just shovel it out with a 3 byte LLAP header
  494. */
  495. skb_push(skb, 3);
  496. skb->data[0] = sa->s_node;
  497. skb->data[1] = at->s_node;
  498. skb->data[2] = ft;
  499. skb->dev = dev;
  500. goto sendit;
  501. }
  502. /* On a PPP link we neither compress nor aarp. */
  503. if (dev->type == ARPHRD_PPP) {
  504. skb->protocol = htons(ETH_P_PPPTALK);
  505. skb->dev = dev;
  506. goto sendit;
  507. }
  508. /* Non ELAP we cannot do. */
  509. if (dev->type != ARPHRD_ETHER)
  510. goto free_it;
  511. skb->dev = dev;
  512. skb->protocol = htons(ETH_P_ATALK);
  513. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  514. /* Do we have a resolved entry? */
  515. if (sa->s_node == ATADDR_BCAST) {
  516. /* Send it */
  517. ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
  518. goto sent;
  519. }
  520. write_lock_bh(&aarp_lock);
  521. a = __aarp_find_entry(resolved[hash], dev, sa);
  522. if (a) { /* Return 1 and fill in the address */
  523. a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
  524. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  525. write_unlock_bh(&aarp_lock);
  526. goto sent;
  527. }
  528. /* Do we have an unresolved entry: This is the less common path */
  529. a = __aarp_find_entry(unresolved[hash], dev, sa);
  530. if (a) { /* Queue onto the unresolved queue */
  531. skb_queue_tail(&a->packet_queue, skb);
  532. goto out_unlock;
  533. }
  534. /* Allocate a new entry */
  535. a = aarp_alloc();
  536. if (!a) {
  537. /* Whoops slipped... good job it's an unreliable protocol 8) */
  538. write_unlock_bh(&aarp_lock);
  539. goto free_it;
  540. }
  541. /* Set up the queue */
  542. skb_queue_tail(&a->packet_queue, skb);
  543. a->expires_at = jiffies + sysctl_aarp_resolve_time;
  544. a->dev = dev;
  545. a->next = unresolved[hash];
  546. a->target_addr = *sa;
  547. a->xmit_count = 0;
  548. unresolved[hash] = a;
  549. unresolved_count++;
  550. /* Send an initial request for the address */
  551. __aarp_send_query(a);
  552. /*
  553. * Switch to fast timer if needed (That is if this is the first
  554. * unresolved entry to get added)
  555. */
  556. if (unresolved_count == 1)
  557. mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
  558. /* Now finally, it is safe to drop the lock. */
  559. out_unlock:
  560. write_unlock_bh(&aarp_lock);
  561. /* Tell the ddp layer we have taken over for this frame. */
  562. goto sent;
  563. sendit:
  564. if (skb->sk)
  565. skb->priority = skb->sk->sk_priority;
  566. if (dev_queue_xmit(skb))
  567. goto drop;
  568. sent:
  569. return NET_XMIT_SUCCESS;
  570. free_it:
  571. kfree_skb(skb);
  572. drop:
  573. return NET_XMIT_DROP;
  574. }
  575. EXPORT_SYMBOL(aarp_send_ddp);
  576. /*
  577. * An entry in the aarp unresolved queue has become resolved. Send
  578. * all the frames queued under it.
  579. *
  580. * Must run under aarp_lock.
  581. */
  582. static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
  583. int hash)
  584. {
  585. struct sk_buff *skb;
  586. while (*list)
  587. if (*list == a) {
  588. unresolved_count--;
  589. *list = a->next;
  590. /* Move into the resolved list */
  591. a->next = resolved[hash];
  592. resolved[hash] = a;
  593. /* Kick frames off */
  594. while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
  595. a->expires_at = jiffies +
  596. sysctl_aarp_expiry_time * 10;
  597. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  598. }
  599. } else
  600. list = &((*list)->next);
  601. }
  602. /*
  603. * This is called by the SNAP driver whenever we see an AARP SNAP
  604. * frame. We currently only support Ethernet.
  605. */
  606. static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
  607. struct packet_type *pt, struct net_device *orig_dev)
  608. {
  609. struct elapaarp *ea = aarp_hdr(skb);
  610. int hash, ret = 0;
  611. __u16 function;
  612. struct aarp_entry *a;
  613. struct atalk_addr sa, *ma, da;
  614. struct atalk_iface *ifa;
  615. if (!net_eq(dev_net(dev), &init_net))
  616. goto out0;
  617. /* We only do Ethernet SNAP AARP. */
  618. if (dev->type != ARPHRD_ETHER)
  619. goto out0;
  620. /* Frame size ok? */
  621. if (!skb_pull(skb, sizeof(*ea)))
  622. goto out0;
  623. function = ntohs(ea->function);
  624. /* Sanity check fields. */
  625. if (function < AARP_REQUEST || function > AARP_PROBE ||
  626. ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
  627. ea->pa_src_zero || ea->pa_dst_zero)
  628. goto out0;
  629. /* Looks good. */
  630. hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
  631. /* Build an address. */
  632. sa.s_node = ea->pa_src_node;
  633. sa.s_net = ea->pa_src_net;
  634. /* Process the packet. Check for replies of me. */
  635. ifa = atalk_find_dev(dev);
  636. if (!ifa)
  637. goto out1;
  638. if (ifa->status & ATIF_PROBE &&
  639. ifa->address.s_node == ea->pa_dst_node &&
  640. ifa->address.s_net == ea->pa_dst_net) {
  641. ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
  642. goto out1;
  643. }
  644. /* Check for replies of proxy AARP entries */
  645. da.s_node = ea->pa_dst_node;
  646. da.s_net = ea->pa_dst_net;
  647. write_lock_bh(&aarp_lock);
  648. a = __aarp_find_entry(proxies[hash], dev, &da);
  649. if (a && a->status & ATIF_PROBE) {
  650. a->status |= ATIF_PROBE_FAIL;
  651. /*
  652. * we do not respond to probe or request packets for
  653. * this address while we are probing this address
  654. */
  655. goto unlock;
  656. }
  657. switch (function) {
  658. case AARP_REPLY:
  659. if (!unresolved_count) /* Speed up */
  660. break;
  661. /* Find the entry. */
  662. a = __aarp_find_entry(unresolved[hash], dev, &sa);
  663. if (!a || dev != a->dev)
  664. break;
  665. /* We can fill one in - this is good. */
  666. ether_addr_copy(a->hwaddr, ea->hw_src);
  667. __aarp_resolved(&unresolved[hash], a, hash);
  668. if (!unresolved_count)
  669. mod_timer(&aarp_timer,
  670. jiffies + sysctl_aarp_expiry_time);
  671. break;
  672. case AARP_REQUEST:
  673. case AARP_PROBE:
  674. /*
  675. * If it is my address set ma to my address and reply.
  676. * We can treat probe and request the same. Probe
  677. * simply means we shouldn't cache the querying host,
  678. * as in a probe they are proposing an address not
  679. * using one.
  680. *
  681. * Support for proxy-AARP added. We check if the
  682. * address is one of our proxies before we toss the
  683. * packet out.
  684. */
  685. sa.s_node = ea->pa_dst_node;
  686. sa.s_net = ea->pa_dst_net;
  687. /* See if we have a matching proxy. */
  688. ma = __aarp_proxy_find(dev, &sa);
  689. if (!ma)
  690. ma = &ifa->address;
  691. else { /* We need to make a copy of the entry. */
  692. da.s_node = sa.s_node;
  693. da.s_net = sa.s_net;
  694. ma = &da;
  695. }
  696. if (function == AARP_PROBE) {
  697. /*
  698. * A probe implies someone trying to get an
  699. * address. So as a precaution flush any
  700. * entries we have for this address.
  701. */
  702. a = __aarp_find_entry(resolved[sa.s_node %
  703. (AARP_HASH_SIZE - 1)],
  704. skb->dev, &sa);
  705. /*
  706. * Make it expire next tick - that avoids us
  707. * getting into a probe/flush/learn/probe/
  708. * flush/learn cycle during probing of a slow
  709. * to respond host addr.
  710. */
  711. if (a) {
  712. a->expires_at = jiffies - 1;
  713. mod_timer(&aarp_timer, jiffies +
  714. sysctl_aarp_tick_time);
  715. }
  716. }
  717. if (sa.s_node != ma->s_node)
  718. break;
  719. if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
  720. break;
  721. sa.s_node = ea->pa_src_node;
  722. sa.s_net = ea->pa_src_net;
  723. /* aarp_my_address has found the address to use for us.
  724. */
  725. aarp_send_reply(dev, ma, &sa, ea->hw_src);
  726. break;
  727. }
  728. unlock:
  729. write_unlock_bh(&aarp_lock);
  730. out1:
  731. ret = 1;
  732. out0:
  733. kfree_skb(skb);
  734. return ret;
  735. }
  736. static struct notifier_block aarp_notifier = {
  737. .notifier_call = aarp_device_event,
  738. };
  739. static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
  740. void __init aarp_proto_init(void)
  741. {
  742. aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
  743. if (!aarp_dl)
  744. printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
  745. setup_timer(&aarp_timer, aarp_expire_timeout, 0);
  746. aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
  747. add_timer(&aarp_timer);
  748. register_netdevice_notifier(&aarp_notifier);
  749. }
  750. /* Remove the AARP entries associated with a device. */
  751. void aarp_device_down(struct net_device *dev)
  752. {
  753. int ct;
  754. write_lock_bh(&aarp_lock);
  755. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  756. __aarp_expire_device(&resolved[ct], dev);
  757. __aarp_expire_device(&unresolved[ct], dev);
  758. __aarp_expire_device(&proxies[ct], dev);
  759. }
  760. write_unlock_bh(&aarp_lock);
  761. }
  762. #ifdef CONFIG_PROC_FS
  763. struct aarp_iter_state {
  764. int bucket;
  765. struct aarp_entry **table;
  766. };
  767. /*
  768. * Get the aarp entry that is in the chain described
  769. * by the iterator.
  770. * If pos is set then skip till that index.
  771. * pos = 1 is the first entry
  772. */
  773. static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
  774. {
  775. int ct = iter->bucket;
  776. struct aarp_entry **table = iter->table;
  777. loff_t off = 0;
  778. struct aarp_entry *entry;
  779. rescan:
  780. while (ct < AARP_HASH_SIZE) {
  781. for (entry = table[ct]; entry; entry = entry->next) {
  782. if (!pos || ++off == *pos) {
  783. iter->table = table;
  784. iter->bucket = ct;
  785. return entry;
  786. }
  787. }
  788. ++ct;
  789. }
  790. if (table == resolved) {
  791. ct = 0;
  792. table = unresolved;
  793. goto rescan;
  794. }
  795. if (table == unresolved) {
  796. ct = 0;
  797. table = proxies;
  798. goto rescan;
  799. }
  800. return NULL;
  801. }
  802. static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
  803. __acquires(aarp_lock)
  804. {
  805. struct aarp_iter_state *iter = seq->private;
  806. read_lock_bh(&aarp_lock);
  807. iter->table = resolved;
  808. iter->bucket = 0;
  809. return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
  810. }
  811. static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  812. {
  813. struct aarp_entry *entry = v;
  814. struct aarp_iter_state *iter = seq->private;
  815. ++*pos;
  816. /* first line after header */
  817. if (v == SEQ_START_TOKEN)
  818. entry = iter_next(iter, NULL);
  819. /* next entry in current bucket */
  820. else if (entry->next)
  821. entry = entry->next;
  822. /* next bucket or table */
  823. else {
  824. ++iter->bucket;
  825. entry = iter_next(iter, NULL);
  826. }
  827. return entry;
  828. }
  829. static void aarp_seq_stop(struct seq_file *seq, void *v)
  830. __releases(aarp_lock)
  831. {
  832. read_unlock_bh(&aarp_lock);
  833. }
  834. static const char *dt2str(unsigned long ticks)
  835. {
  836. static char buf[32];
  837. sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
  838. return buf;
  839. }
  840. static int aarp_seq_show(struct seq_file *seq, void *v)
  841. {
  842. struct aarp_iter_state *iter = seq->private;
  843. struct aarp_entry *entry = v;
  844. unsigned long now = jiffies;
  845. if (v == SEQ_START_TOKEN)
  846. seq_puts(seq,
  847. "Address Interface Hardware Address"
  848. " Expires LastSend Retry Status\n");
  849. else {
  850. seq_printf(seq, "%04X:%02X %-12s",
  851. ntohs(entry->target_addr.s_net),
  852. (unsigned int) entry->target_addr.s_node,
  853. entry->dev ? entry->dev->name : "????");
  854. seq_printf(seq, "%pM", entry->hwaddr);
  855. seq_printf(seq, " %8s",
  856. dt2str((long)entry->expires_at - (long)now));
  857. if (iter->table == unresolved)
  858. seq_printf(seq, " %8s %6hu",
  859. dt2str(now - entry->last_sent),
  860. entry->xmit_count);
  861. else
  862. seq_puts(seq, " ");
  863. seq_printf(seq, " %s\n",
  864. (iter->table == resolved) ? "resolved"
  865. : (iter->table == unresolved) ? "unresolved"
  866. : (iter->table == proxies) ? "proxies"
  867. : "unknown");
  868. }
  869. return 0;
  870. }
  871. static const struct seq_operations aarp_seq_ops = {
  872. .start = aarp_seq_start,
  873. .next = aarp_seq_next,
  874. .stop = aarp_seq_stop,
  875. .show = aarp_seq_show,
  876. };
  877. static int aarp_seq_open(struct inode *inode, struct file *file)
  878. {
  879. return seq_open_private(file, &aarp_seq_ops,
  880. sizeof(struct aarp_iter_state));
  881. }
  882. const struct file_operations atalk_seq_arp_fops = {
  883. .owner = THIS_MODULE,
  884. .open = aarp_seq_open,
  885. .read = seq_read,
  886. .llseek = seq_lseek,
  887. .release = seq_release_private,
  888. };
  889. #endif
  890. /* General module cleanup. Called from cleanup_module() in ddp.c. */
  891. void aarp_cleanup_module(void)
  892. {
  893. del_timer_sync(&aarp_timer);
  894. unregister_netdevice_notifier(&aarp_notifier);
  895. unregister_snap_client(aarp_dl);
  896. aarp_purge();
  897. }