aarp.c 25 KB

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