aarp.c 25 KB

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