aarp.c 25 KB

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