aarp.c 25 KB

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