aarp.c 25 KB

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