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