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. sa->sat_addr.s_node = iface->address.s_node;
  380. sa->sat_addr.s_net = ntohs(iface->address.s_net);
  381. /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
  382. if (!(iface->dev->do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
  383. (void)iface->dev->do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
  384. if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
  385. iface->address.s_node != sa->sat_addr.s_node)
  386. iface->status |= ATIF_PROBE_FAIL;
  387. iface->address.s_net = htons(sa->sat_addr.s_net);
  388. iface->address.s_node = sa->sat_addr.s_node;
  389. }
  390. }
  391. void aarp_probe_network(struct atalk_iface *atif)
  392. {
  393. if (atif->dev->type == ARPHRD_LOCALTLK ||
  394. atif->dev->type == ARPHRD_PPP)
  395. aarp_send_probe_phase1(atif);
  396. else {
  397. unsigned int count;
  398. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  399. aarp_send_probe(atif->dev, &atif->address);
  400. /* Defer 1/10th */
  401. msleep(100);
  402. if (atif->status & ATIF_PROBE_FAIL)
  403. break;
  404. }
  405. }
  406. }
  407. int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
  408. {
  409. int hash, retval = -EPROTONOSUPPORT;
  410. struct aarp_entry *entry;
  411. unsigned int count;
  412. /*
  413. * we don't currently support LocalTalk or PPP for proxy AARP;
  414. * if someone wants to try and add it, have fun
  415. */
  416. if (atif->dev->type == ARPHRD_LOCALTLK ||
  417. atif->dev->type == ARPHRD_PPP)
  418. goto out;
  419. /*
  420. * create a new AARP entry with the flags set to be published --
  421. * we need this one to hang around even if it's in use
  422. */
  423. entry = aarp_alloc();
  424. retval = -ENOMEM;
  425. if (!entry)
  426. goto out;
  427. entry->expires_at = -1;
  428. entry->status = ATIF_PROBE;
  429. entry->target_addr.s_node = sa->s_node;
  430. entry->target_addr.s_net = sa->s_net;
  431. entry->dev = atif->dev;
  432. write_lock_bh(&aarp_lock);
  433. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  434. entry->next = proxies[hash];
  435. proxies[hash] = entry;
  436. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  437. aarp_send_probe(atif->dev, sa);
  438. /* Defer 1/10th */
  439. write_unlock_bh(&aarp_lock);
  440. msleep(100);
  441. write_lock_bh(&aarp_lock);
  442. if (entry->status & ATIF_PROBE_FAIL)
  443. break;
  444. }
  445. if (entry->status & ATIF_PROBE_FAIL) {
  446. entry->expires_at = jiffies - 1; /* free the entry */
  447. retval = -EADDRINUSE; /* return network full */
  448. } else { /* clear the probing flag */
  449. entry->status &= ~ATIF_PROBE;
  450. retval = 1;
  451. }
  452. write_unlock_bh(&aarp_lock);
  453. out:
  454. return retval;
  455. }
  456. /* Send a DDP frame */
  457. int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
  458. struct atalk_addr *sa, void *hwaddr)
  459. {
  460. static char ddp_eth_multicast[ETH_ALEN] =
  461. { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
  462. int hash;
  463. struct aarp_entry *a;
  464. skb_reset_network_header(skb);
  465. /* Check for LocalTalk first */
  466. if (dev->type == ARPHRD_LOCALTLK) {
  467. struct atalk_addr *at = atalk_find_dev_addr(dev);
  468. struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
  469. int ft = 2;
  470. /*
  471. * Compressible ?
  472. *
  473. * IFF: src_net == dest_net == device_net
  474. * (zero matches anything)
  475. */
  476. if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
  477. (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
  478. skb_pull(skb, sizeof(*ddp) - 4);
  479. /*
  480. * The upper two remaining bytes are the port
  481. * numbers we just happen to need. Now put the
  482. * length in the lower two.
  483. */
  484. *((__be16 *)skb->data) = htons(skb->len);
  485. ft = 1;
  486. }
  487. /*
  488. * Nice and easy. No AARP type protocols occur here so we can
  489. * just shovel it out with a 3 byte LLAP header
  490. */
  491. skb_push(skb, 3);
  492. skb->data[0] = sa->s_node;
  493. skb->data[1] = at->s_node;
  494. skb->data[2] = ft;
  495. skb->dev = dev;
  496. goto sendit;
  497. }
  498. /* On a PPP link we neither compress nor aarp. */
  499. if (dev->type == ARPHRD_PPP) {
  500. skb->protocol = htons(ETH_P_PPPTALK);
  501. skb->dev = dev;
  502. goto sendit;
  503. }
  504. /* Non ELAP we cannot do. */
  505. if (dev->type != ARPHRD_ETHER)
  506. return -1;
  507. skb->dev = dev;
  508. skb->protocol = htons(ETH_P_ATALK);
  509. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  510. /* Do we have a resolved entry? */
  511. if (sa->s_node == ATADDR_BCAST) {
  512. /* Send it */
  513. ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
  514. goto sent;
  515. }
  516. write_lock_bh(&aarp_lock);
  517. a = __aarp_find_entry(resolved[hash], dev, sa);
  518. if (a) { /* Return 1 and fill in the address */
  519. a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
  520. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  521. write_unlock_bh(&aarp_lock);
  522. goto sent;
  523. }
  524. /* Do we have an unresolved entry: This is the less common path */
  525. a = __aarp_find_entry(unresolved[hash], dev, sa);
  526. if (a) { /* Queue onto the unresolved queue */
  527. skb_queue_tail(&a->packet_queue, skb);
  528. goto out_unlock;
  529. }
  530. /* Allocate a new entry */
  531. a = aarp_alloc();
  532. if (!a) {
  533. /* Whoops slipped... good job it's an unreliable protocol 8) */
  534. write_unlock_bh(&aarp_lock);
  535. return -1;
  536. }
  537. /* Set up the queue */
  538. skb_queue_tail(&a->packet_queue, skb);
  539. a->expires_at = jiffies + sysctl_aarp_resolve_time;
  540. a->dev = dev;
  541. a->next = unresolved[hash];
  542. a->target_addr = *sa;
  543. a->xmit_count = 0;
  544. unresolved[hash] = a;
  545. unresolved_count++;
  546. /* Send an initial request for the address */
  547. __aarp_send_query(a);
  548. /*
  549. * Switch to fast timer if needed (That is if this is the first
  550. * unresolved entry to get added)
  551. */
  552. if (unresolved_count == 1)
  553. mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
  554. /* Now finally, it is safe to drop the lock. */
  555. out_unlock:
  556. write_unlock_bh(&aarp_lock);
  557. /* Tell the ddp layer we have taken over for this frame. */
  558. return 0;
  559. sendit:
  560. if (skb->sk)
  561. skb->priority = skb->sk->sk_priority;
  562. dev_queue_xmit(skb);
  563. sent:
  564. return 1;
  565. }
  566. /*
  567. * An entry in the aarp unresolved queue has become resolved. Send
  568. * all the frames queued under it.
  569. *
  570. * Must run under aarp_lock.
  571. */
  572. static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
  573. int hash)
  574. {
  575. struct sk_buff *skb;
  576. while (*list)
  577. if (*list == a) {
  578. unresolved_count--;
  579. *list = a->next;
  580. /* Move into the resolved list */
  581. a->next = resolved[hash];
  582. resolved[hash] = a;
  583. /* Kick frames off */
  584. while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
  585. a->expires_at = jiffies +
  586. sysctl_aarp_expiry_time * 10;
  587. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  588. }
  589. } else
  590. list = &((*list)->next);
  591. }
  592. /*
  593. * This is called by the SNAP driver whenever we see an AARP SNAP
  594. * frame. We currently only support Ethernet.
  595. */
  596. static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
  597. struct packet_type *pt, struct net_device *orig_dev)
  598. {
  599. struct elapaarp *ea = aarp_hdr(skb);
  600. int hash, ret = 0;
  601. __u16 function;
  602. struct aarp_entry *a;
  603. struct atalk_addr sa, *ma, da;
  604. struct atalk_iface *ifa;
  605. if (!net_eq(dev_net(dev), &init_net))
  606. goto out0;
  607. /* We only do Ethernet SNAP AARP. */
  608. if (dev->type != ARPHRD_ETHER)
  609. goto out0;
  610. /* Frame size ok? */
  611. if (!skb_pull(skb, sizeof(*ea)))
  612. goto out0;
  613. function = ntohs(ea->function);
  614. /* Sanity check fields. */
  615. if (function < AARP_REQUEST || function > AARP_PROBE ||
  616. ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
  617. ea->pa_src_zero || ea->pa_dst_zero)
  618. goto out0;
  619. /* Looks good. */
  620. hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
  621. /* Build an address. */
  622. sa.s_node = ea->pa_src_node;
  623. sa.s_net = ea->pa_src_net;
  624. /* Process the packet. Check for replies of me. */
  625. ifa = atalk_find_dev(dev);
  626. if (!ifa)
  627. goto out1;
  628. if (ifa->status & ATIF_PROBE &&
  629. ifa->address.s_node == ea->pa_dst_node &&
  630. ifa->address.s_net == ea->pa_dst_net) {
  631. ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
  632. goto out1;
  633. }
  634. /* Check for replies of proxy AARP entries */
  635. da.s_node = ea->pa_dst_node;
  636. da.s_net = ea->pa_dst_net;
  637. write_lock_bh(&aarp_lock);
  638. a = __aarp_find_entry(proxies[hash], dev, &da);
  639. if (a && a->status & ATIF_PROBE) {
  640. a->status |= ATIF_PROBE_FAIL;
  641. /*
  642. * we do not respond to probe or request packets for
  643. * this address while we are probing this address
  644. */
  645. goto unlock;
  646. }
  647. switch (function) {
  648. case AARP_REPLY:
  649. if (!unresolved_count) /* Speed up */
  650. break;
  651. /* Find the entry. */
  652. a = __aarp_find_entry(unresolved[hash], dev, &sa);
  653. if (!a || dev != a->dev)
  654. break;
  655. /* We can fill one in - this is good. */
  656. memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
  657. __aarp_resolved(&unresolved[hash], a, hash);
  658. if (!unresolved_count)
  659. mod_timer(&aarp_timer,
  660. jiffies + sysctl_aarp_expiry_time);
  661. break;
  662. case AARP_REQUEST:
  663. case AARP_PROBE:
  664. /*
  665. * If it is my address set ma to my address and reply.
  666. * We can treat probe and request the same. Probe
  667. * simply means we shouldn't cache the querying host,
  668. * as in a probe they are proposing an address not
  669. * using one.
  670. *
  671. * Support for proxy-AARP added. We check if the
  672. * address is one of our proxies before we toss the
  673. * packet out.
  674. */
  675. sa.s_node = ea->pa_dst_node;
  676. sa.s_net = ea->pa_dst_net;
  677. /* See if we have a matching proxy. */
  678. ma = __aarp_proxy_find(dev, &sa);
  679. if (!ma)
  680. ma = &ifa->address;
  681. else { /* We need to make a copy of the entry. */
  682. da.s_node = sa.s_node;
  683. da.s_net = da.s_net;
  684. ma = &da;
  685. }
  686. if (function == AARP_PROBE) {
  687. /*
  688. * A probe implies someone trying to get an
  689. * address. So as a precaution flush any
  690. * entries we have for this address.
  691. */
  692. a = __aarp_find_entry(resolved[sa.s_node %
  693. (AARP_HASH_SIZE - 1)],
  694. skb->dev, &sa);
  695. /*
  696. * Make it expire next tick - that avoids us
  697. * getting into a probe/flush/learn/probe/
  698. * flush/learn cycle during probing of a slow
  699. * to respond host addr.
  700. */
  701. if (a) {
  702. a->expires_at = jiffies - 1;
  703. mod_timer(&aarp_timer, jiffies +
  704. sysctl_aarp_tick_time);
  705. }
  706. }
  707. if (sa.s_node != ma->s_node)
  708. break;
  709. if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
  710. break;
  711. sa.s_node = ea->pa_src_node;
  712. sa.s_net = ea->pa_src_net;
  713. /* aarp_my_address has found the address to use for us.
  714. */
  715. aarp_send_reply(dev, ma, &sa, ea->hw_src);
  716. break;
  717. }
  718. unlock:
  719. write_unlock_bh(&aarp_lock);
  720. out1:
  721. ret = 1;
  722. out0:
  723. kfree_skb(skb);
  724. return ret;
  725. }
  726. static struct notifier_block aarp_notifier = {
  727. .notifier_call = aarp_device_event,
  728. };
  729. static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
  730. void __init aarp_proto_init(void)
  731. {
  732. aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
  733. if (!aarp_dl)
  734. printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
  735. setup_timer(&aarp_timer, aarp_expire_timeout, 0);
  736. aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
  737. add_timer(&aarp_timer);
  738. register_netdevice_notifier(&aarp_notifier);
  739. }
  740. /* Remove the AARP entries associated with a device. */
  741. void aarp_device_down(struct net_device *dev)
  742. {
  743. int ct;
  744. write_lock_bh(&aarp_lock);
  745. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  746. __aarp_expire_device(&resolved[ct], dev);
  747. __aarp_expire_device(&unresolved[ct], dev);
  748. __aarp_expire_device(&proxies[ct], dev);
  749. }
  750. write_unlock_bh(&aarp_lock);
  751. }
  752. #ifdef CONFIG_PROC_FS
  753. struct aarp_iter_state {
  754. int bucket;
  755. struct aarp_entry **table;
  756. };
  757. /*
  758. * Get the aarp entry that is in the chain described
  759. * by the iterator.
  760. * If pos is set then skip till that index.
  761. * pos = 1 is the first entry
  762. */
  763. static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
  764. {
  765. int ct = iter->bucket;
  766. struct aarp_entry **table = iter->table;
  767. loff_t off = 0;
  768. struct aarp_entry *entry;
  769. rescan:
  770. while(ct < AARP_HASH_SIZE) {
  771. for (entry = table[ct]; entry; entry = entry->next) {
  772. if (!pos || ++off == *pos) {
  773. iter->table = table;
  774. iter->bucket = ct;
  775. return entry;
  776. }
  777. }
  778. ++ct;
  779. }
  780. if (table == resolved) {
  781. ct = 0;
  782. table = unresolved;
  783. goto rescan;
  784. }
  785. if (table == unresolved) {
  786. ct = 0;
  787. table = proxies;
  788. goto rescan;
  789. }
  790. return NULL;
  791. }
  792. static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
  793. __acquires(aarp_lock)
  794. {
  795. struct aarp_iter_state *iter = seq->private;
  796. read_lock_bh(&aarp_lock);
  797. iter->table = resolved;
  798. iter->bucket = 0;
  799. return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
  800. }
  801. static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  802. {
  803. struct aarp_entry *entry = v;
  804. struct aarp_iter_state *iter = seq->private;
  805. ++*pos;
  806. /* first line after header */
  807. if (v == SEQ_START_TOKEN)
  808. entry = iter_next(iter, NULL);
  809. /* next entry in current bucket */
  810. else if (entry->next)
  811. entry = entry->next;
  812. /* next bucket or table */
  813. else {
  814. ++iter->bucket;
  815. entry = iter_next(iter, NULL);
  816. }
  817. return entry;
  818. }
  819. static void aarp_seq_stop(struct seq_file *seq, void *v)
  820. __releases(aarp_lock)
  821. {
  822. read_unlock_bh(&aarp_lock);
  823. }
  824. static const char *dt2str(unsigned long ticks)
  825. {
  826. static char buf[32];
  827. sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
  828. return buf;
  829. }
  830. static int aarp_seq_show(struct seq_file *seq, void *v)
  831. {
  832. struct aarp_iter_state *iter = seq->private;
  833. struct aarp_entry *entry = v;
  834. unsigned long now = jiffies;
  835. DECLARE_MAC_BUF(mac);
  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, "%s", print_mac(mac, 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. }