aarp.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. int ct;
  286. if (event == NETDEV_DOWN) {
  287. write_lock_bh(&aarp_lock);
  288. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  289. __aarp_expire_device(&resolved[ct], ptr);
  290. __aarp_expire_device(&unresolved[ct], ptr);
  291. __aarp_expire_device(&proxies[ct], ptr);
  292. }
  293. write_unlock_bh(&aarp_lock);
  294. }
  295. return NOTIFY_DONE;
  296. }
  297. /* Expire all entries in a hash chain */
  298. static void __aarp_expire_all(struct aarp_entry **n)
  299. {
  300. struct aarp_entry *t;
  301. while (*n) {
  302. t = *n;
  303. *n = (*n)->next;
  304. __aarp_expire(t);
  305. }
  306. }
  307. /* Cleanup all hash chains -- module unloading */
  308. static void aarp_purge(void)
  309. {
  310. int ct;
  311. write_lock_bh(&aarp_lock);
  312. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  313. __aarp_expire_all(&resolved[ct]);
  314. __aarp_expire_all(&unresolved[ct]);
  315. __aarp_expire_all(&proxies[ct]);
  316. }
  317. write_unlock_bh(&aarp_lock);
  318. }
  319. /*
  320. * Create a new aarp entry. This must use GFP_ATOMIC because it
  321. * runs while holding spinlocks.
  322. */
  323. static struct aarp_entry *aarp_alloc(void)
  324. {
  325. struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
  326. if (a)
  327. skb_queue_head_init(&a->packet_queue);
  328. return a;
  329. }
  330. /*
  331. * Find an entry. We might return an expired but not yet purged entry. We
  332. * don't care as it will do no harm.
  333. *
  334. * This must run under the aarp_lock.
  335. */
  336. static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
  337. struct net_device *dev,
  338. struct atalk_addr *sat)
  339. {
  340. while (list) {
  341. if (list->target_addr.s_net == sat->s_net &&
  342. list->target_addr.s_node == sat->s_node &&
  343. list->dev == dev)
  344. break;
  345. list = list->next;
  346. }
  347. return list;
  348. }
  349. /* Called from the DDP code, and thus must be exported. */
  350. void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
  351. {
  352. int hash = sa->s_node % (AARP_HASH_SIZE - 1);
  353. struct aarp_entry *a;
  354. write_lock_bh(&aarp_lock);
  355. a = __aarp_find_entry(proxies[hash], dev, sa);
  356. if (a)
  357. a->expires_at = jiffies - 1;
  358. write_unlock_bh(&aarp_lock);
  359. }
  360. /* This must run under aarp_lock. */
  361. static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
  362. struct atalk_addr *sa)
  363. {
  364. int hash = sa->s_node % (AARP_HASH_SIZE - 1);
  365. struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
  366. return a ? sa : NULL;
  367. }
  368. /*
  369. * Probe a Phase 1 device or a device that requires its Net:Node to
  370. * be set via an ioctl.
  371. */
  372. static void aarp_send_probe_phase1(struct atalk_iface *iface)
  373. {
  374. struct ifreq atreq;
  375. struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
  376. sa->sat_addr.s_node = iface->address.s_node;
  377. sa->sat_addr.s_net = ntohs(iface->address.s_net);
  378. /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
  379. if (!(iface->dev->do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
  380. (void)iface->dev->do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
  381. if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
  382. iface->address.s_node != sa->sat_addr.s_node)
  383. iface->status |= ATIF_PROBE_FAIL;
  384. iface->address.s_net = htons(sa->sat_addr.s_net);
  385. iface->address.s_node = sa->sat_addr.s_node;
  386. }
  387. }
  388. void aarp_probe_network(struct atalk_iface *atif)
  389. {
  390. if (atif->dev->type == ARPHRD_LOCALTLK ||
  391. atif->dev->type == ARPHRD_PPP)
  392. aarp_send_probe_phase1(atif);
  393. else {
  394. unsigned int count;
  395. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  396. aarp_send_probe(atif->dev, &atif->address);
  397. /* Defer 1/10th */
  398. msleep(100);
  399. if (atif->status & ATIF_PROBE_FAIL)
  400. break;
  401. }
  402. }
  403. }
  404. int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
  405. {
  406. int hash, retval = -EPROTONOSUPPORT;
  407. struct aarp_entry *entry;
  408. unsigned int count;
  409. /*
  410. * we don't currently support LocalTalk or PPP for proxy AARP;
  411. * if someone wants to try and add it, have fun
  412. */
  413. if (atif->dev->type == ARPHRD_LOCALTLK ||
  414. atif->dev->type == ARPHRD_PPP)
  415. goto out;
  416. /*
  417. * create a new AARP entry with the flags set to be published --
  418. * we need this one to hang around even if it's in use
  419. */
  420. entry = aarp_alloc();
  421. retval = -ENOMEM;
  422. if (!entry)
  423. goto out;
  424. entry->expires_at = -1;
  425. entry->status = ATIF_PROBE;
  426. entry->target_addr.s_node = sa->s_node;
  427. entry->target_addr.s_net = sa->s_net;
  428. entry->dev = atif->dev;
  429. write_lock_bh(&aarp_lock);
  430. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  431. entry->next = proxies[hash];
  432. proxies[hash] = entry;
  433. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  434. aarp_send_probe(atif->dev, sa);
  435. /* Defer 1/10th */
  436. write_unlock_bh(&aarp_lock);
  437. msleep(100);
  438. write_lock_bh(&aarp_lock);
  439. if (entry->status & ATIF_PROBE_FAIL)
  440. break;
  441. }
  442. if (entry->status & ATIF_PROBE_FAIL) {
  443. entry->expires_at = jiffies - 1; /* free the entry */
  444. retval = -EADDRINUSE; /* return network full */
  445. } else { /* clear the probing flag */
  446. entry->status &= ~ATIF_PROBE;
  447. retval = 1;
  448. }
  449. write_unlock_bh(&aarp_lock);
  450. out:
  451. return retval;
  452. }
  453. /* Send a DDP frame */
  454. int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
  455. struct atalk_addr *sa, void *hwaddr)
  456. {
  457. static char ddp_eth_multicast[ETH_ALEN] =
  458. { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
  459. int hash;
  460. struct aarp_entry *a;
  461. skb_reset_network_header(skb);
  462. /* Check for LocalTalk first */
  463. if (dev->type == ARPHRD_LOCALTLK) {
  464. struct atalk_addr *at = atalk_find_dev_addr(dev);
  465. struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
  466. int ft = 2;
  467. /*
  468. * Compressible ?
  469. *
  470. * IFF: src_net == dest_net == device_net
  471. * (zero matches anything)
  472. */
  473. if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
  474. (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
  475. skb_pull(skb, sizeof(*ddp) - 4);
  476. /*
  477. * The upper two remaining bytes are the port
  478. * numbers we just happen to need. Now put the
  479. * length in the lower two.
  480. */
  481. *((__be16 *)skb->data) = htons(skb->len);
  482. ft = 1;
  483. }
  484. /*
  485. * Nice and easy. No AARP type protocols occur here so we can
  486. * just shovel it out with a 3 byte LLAP header
  487. */
  488. skb_push(skb, 3);
  489. skb->data[0] = sa->s_node;
  490. skb->data[1] = at->s_node;
  491. skb->data[2] = ft;
  492. skb->dev = dev;
  493. goto sendit;
  494. }
  495. /* On a PPP link we neither compress nor aarp. */
  496. if (dev->type == ARPHRD_PPP) {
  497. skb->protocol = htons(ETH_P_PPPTALK);
  498. skb->dev = dev;
  499. goto sendit;
  500. }
  501. /* Non ELAP we cannot do. */
  502. if (dev->type != ARPHRD_ETHER)
  503. return -1;
  504. skb->dev = dev;
  505. skb->protocol = htons(ETH_P_ATALK);
  506. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  507. /* Do we have a resolved entry? */
  508. if (sa->s_node == ATADDR_BCAST) {
  509. /* Send it */
  510. ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
  511. goto sent;
  512. }
  513. write_lock_bh(&aarp_lock);
  514. a = __aarp_find_entry(resolved[hash], dev, sa);
  515. if (a) { /* Return 1 and fill in the address */
  516. a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
  517. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  518. write_unlock_bh(&aarp_lock);
  519. goto sent;
  520. }
  521. /* Do we have an unresolved entry: This is the less common path */
  522. a = __aarp_find_entry(unresolved[hash], dev, sa);
  523. if (a) { /* Queue onto the unresolved queue */
  524. skb_queue_tail(&a->packet_queue, skb);
  525. goto out_unlock;
  526. }
  527. /* Allocate a new entry */
  528. a = aarp_alloc();
  529. if (!a) {
  530. /* Whoops slipped... good job it's an unreliable protocol 8) */
  531. write_unlock_bh(&aarp_lock);
  532. return -1;
  533. }
  534. /* Set up the queue */
  535. skb_queue_tail(&a->packet_queue, skb);
  536. a->expires_at = jiffies + sysctl_aarp_resolve_time;
  537. a->dev = dev;
  538. a->next = unresolved[hash];
  539. a->target_addr = *sa;
  540. a->xmit_count = 0;
  541. unresolved[hash] = a;
  542. unresolved_count++;
  543. /* Send an initial request for the address */
  544. __aarp_send_query(a);
  545. /*
  546. * Switch to fast timer if needed (That is if this is the first
  547. * unresolved entry to get added)
  548. */
  549. if (unresolved_count == 1)
  550. mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
  551. /* Now finally, it is safe to drop the lock. */
  552. out_unlock:
  553. write_unlock_bh(&aarp_lock);
  554. /* Tell the ddp layer we have taken over for this frame. */
  555. return 0;
  556. sendit:
  557. if (skb->sk)
  558. skb->priority = skb->sk->sk_priority;
  559. dev_queue_xmit(skb);
  560. sent:
  561. return 1;
  562. }
  563. /*
  564. * An entry in the aarp unresolved queue has become resolved. Send
  565. * all the frames queued under it.
  566. *
  567. * Must run under aarp_lock.
  568. */
  569. static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
  570. int hash)
  571. {
  572. struct sk_buff *skb;
  573. while (*list)
  574. if (*list == a) {
  575. unresolved_count--;
  576. *list = a->next;
  577. /* Move into the resolved list */
  578. a->next = resolved[hash];
  579. resolved[hash] = a;
  580. /* Kick frames off */
  581. while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
  582. a->expires_at = jiffies +
  583. sysctl_aarp_expiry_time * 10;
  584. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  585. }
  586. } else
  587. list = &((*list)->next);
  588. }
  589. /*
  590. * This is called by the SNAP driver whenever we see an AARP SNAP
  591. * frame. We currently only support Ethernet.
  592. */
  593. static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
  594. struct packet_type *pt, struct net_device *orig_dev)
  595. {
  596. struct elapaarp *ea = aarp_hdr(skb);
  597. int hash, ret = 0;
  598. __u16 function;
  599. struct aarp_entry *a;
  600. struct atalk_addr sa, *ma, da;
  601. struct atalk_iface *ifa;
  602. /* We only do Ethernet SNAP AARP. */
  603. if (dev->type != ARPHRD_ETHER)
  604. goto out0;
  605. /* Frame size ok? */
  606. if (!skb_pull(skb, sizeof(*ea)))
  607. goto out0;
  608. function = ntohs(ea->function);
  609. /* Sanity check fields. */
  610. if (function < AARP_REQUEST || function > AARP_PROBE ||
  611. ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
  612. ea->pa_src_zero || ea->pa_dst_zero)
  613. goto out0;
  614. /* Looks good. */
  615. hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
  616. /* Build an address. */
  617. sa.s_node = ea->pa_src_node;
  618. sa.s_net = ea->pa_src_net;
  619. /* Process the packet. Check for replies of me. */
  620. ifa = atalk_find_dev(dev);
  621. if (!ifa)
  622. goto out1;
  623. if (ifa->status & ATIF_PROBE &&
  624. ifa->address.s_node == ea->pa_dst_node &&
  625. ifa->address.s_net == ea->pa_dst_net) {
  626. ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
  627. goto out1;
  628. }
  629. /* Check for replies of proxy AARP entries */
  630. da.s_node = ea->pa_dst_node;
  631. da.s_net = ea->pa_dst_net;
  632. write_lock_bh(&aarp_lock);
  633. a = __aarp_find_entry(proxies[hash], dev, &da);
  634. if (a && a->status & ATIF_PROBE) {
  635. a->status |= ATIF_PROBE_FAIL;
  636. /*
  637. * we do not respond to probe or request packets for
  638. * this address while we are probing this address
  639. */
  640. goto unlock;
  641. }
  642. switch (function) {
  643. case AARP_REPLY:
  644. if (!unresolved_count) /* Speed up */
  645. break;
  646. /* Find the entry. */
  647. a = __aarp_find_entry(unresolved[hash], dev, &sa);
  648. if (!a || dev != a->dev)
  649. break;
  650. /* We can fill one in - this is good. */
  651. memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
  652. __aarp_resolved(&unresolved[hash], a, hash);
  653. if (!unresolved_count)
  654. mod_timer(&aarp_timer,
  655. jiffies + sysctl_aarp_expiry_time);
  656. break;
  657. case AARP_REQUEST:
  658. case AARP_PROBE:
  659. /*
  660. * If it is my address set ma to my address and reply.
  661. * We can treat probe and request the same. Probe
  662. * simply means we shouldn't cache the querying host,
  663. * as in a probe they are proposing an address not
  664. * using one.
  665. *
  666. * Support for proxy-AARP added. We check if the
  667. * address is one of our proxies before we toss the
  668. * packet out.
  669. */
  670. sa.s_node = ea->pa_dst_node;
  671. sa.s_net = ea->pa_dst_net;
  672. /* See if we have a matching proxy. */
  673. ma = __aarp_proxy_find(dev, &sa);
  674. if (!ma)
  675. ma = &ifa->address;
  676. else { /* We need to make a copy of the entry. */
  677. da.s_node = sa.s_node;
  678. da.s_net = da.s_net;
  679. ma = &da;
  680. }
  681. if (function == AARP_PROBE) {
  682. /*
  683. * A probe implies someone trying to get an
  684. * address. So as a precaution flush any
  685. * entries we have for this address.
  686. */
  687. struct aarp_entry *a;
  688. a = __aarp_find_entry(resolved[sa.s_node %
  689. (AARP_HASH_SIZE - 1)],
  690. skb->dev, &sa);
  691. /*
  692. * Make it expire next tick - that avoids us
  693. * getting into a probe/flush/learn/probe/
  694. * flush/learn cycle during probing of a slow
  695. * to respond host addr.
  696. */
  697. if (a) {
  698. a->expires_at = jiffies - 1;
  699. mod_timer(&aarp_timer, jiffies +
  700. sysctl_aarp_tick_time);
  701. }
  702. }
  703. if (sa.s_node != ma->s_node)
  704. break;
  705. if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
  706. break;
  707. sa.s_node = ea->pa_src_node;
  708. sa.s_net = ea->pa_src_net;
  709. /* aarp_my_address has found the address to use for us.
  710. */
  711. aarp_send_reply(dev, ma, &sa, ea->hw_src);
  712. break;
  713. }
  714. unlock:
  715. write_unlock_bh(&aarp_lock);
  716. out1:
  717. ret = 1;
  718. out0:
  719. kfree_skb(skb);
  720. return ret;
  721. }
  722. static struct notifier_block aarp_notifier = {
  723. .notifier_call = aarp_device_event,
  724. };
  725. static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
  726. void __init aarp_proto_init(void)
  727. {
  728. aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
  729. if (!aarp_dl)
  730. printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
  731. init_timer(&aarp_timer);
  732. aarp_timer.function = aarp_expire_timeout;
  733. aarp_timer.data = 0;
  734. aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
  735. add_timer(&aarp_timer);
  736. register_netdevice_notifier(&aarp_notifier);
  737. }
  738. /* Remove the AARP entries associated with a device. */
  739. void aarp_device_down(struct net_device *dev)
  740. {
  741. int ct;
  742. write_lock_bh(&aarp_lock);
  743. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  744. __aarp_expire_device(&resolved[ct], dev);
  745. __aarp_expire_device(&unresolved[ct], dev);
  746. __aarp_expire_device(&proxies[ct], dev);
  747. }
  748. write_unlock_bh(&aarp_lock);
  749. }
  750. #ifdef CONFIG_PROC_FS
  751. struct aarp_iter_state {
  752. int bucket;
  753. struct aarp_entry **table;
  754. };
  755. /*
  756. * Get the aarp entry that is in the chain described
  757. * by the iterator.
  758. * If pos is set then skip till that index.
  759. * pos = 1 is the first entry
  760. */
  761. static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
  762. {
  763. int ct = iter->bucket;
  764. struct aarp_entry **table = iter->table;
  765. loff_t off = 0;
  766. struct aarp_entry *entry;
  767. rescan:
  768. while(ct < AARP_HASH_SIZE) {
  769. for (entry = table[ct]; entry; entry = entry->next) {
  770. if (!pos || ++off == *pos) {
  771. iter->table = table;
  772. iter->bucket = ct;
  773. return entry;
  774. }
  775. }
  776. ++ct;
  777. }
  778. if (table == resolved) {
  779. ct = 0;
  780. table = unresolved;
  781. goto rescan;
  782. }
  783. if (table == unresolved) {
  784. ct = 0;
  785. table = proxies;
  786. goto rescan;
  787. }
  788. return NULL;
  789. }
  790. static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
  791. {
  792. struct aarp_iter_state *iter = seq->private;
  793. read_lock_bh(&aarp_lock);
  794. iter->table = resolved;
  795. iter->bucket = 0;
  796. return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
  797. }
  798. static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  799. {
  800. struct aarp_entry *entry = v;
  801. struct aarp_iter_state *iter = seq->private;
  802. ++*pos;
  803. /* first line after header */
  804. if (v == SEQ_START_TOKEN)
  805. entry = iter_next(iter, NULL);
  806. /* next entry in current bucket */
  807. else if (entry->next)
  808. entry = entry->next;
  809. /* next bucket or table */
  810. else {
  811. ++iter->bucket;
  812. entry = iter_next(iter, NULL);
  813. }
  814. return entry;
  815. }
  816. static void aarp_seq_stop(struct seq_file *seq, void *v)
  817. {
  818. read_unlock_bh(&aarp_lock);
  819. }
  820. static const char *dt2str(unsigned long ticks)
  821. {
  822. static char buf[32];
  823. sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
  824. return buf;
  825. }
  826. static int aarp_seq_show(struct seq_file *seq, void *v)
  827. {
  828. struct aarp_iter_state *iter = seq->private;
  829. struct aarp_entry *entry = v;
  830. unsigned long now = jiffies;
  831. if (v == SEQ_START_TOKEN)
  832. seq_puts(seq,
  833. "Address Interface Hardware Address"
  834. " Expires LastSend Retry Status\n");
  835. else {
  836. seq_printf(seq, "%04X:%02X %-12s",
  837. ntohs(entry->target_addr.s_net),
  838. (unsigned int) entry->target_addr.s_node,
  839. entry->dev ? entry->dev->name : "????");
  840. seq_printf(seq, "%02X:%02X:%02X:%02X:%02X:%02X",
  841. entry->hwaddr[0] & 0xFF,
  842. entry->hwaddr[1] & 0xFF,
  843. entry->hwaddr[2] & 0xFF,
  844. entry->hwaddr[3] & 0xFF,
  845. entry->hwaddr[4] & 0xFF,
  846. entry->hwaddr[5] & 0xFF);
  847. seq_printf(seq, " %8s",
  848. dt2str((long)entry->expires_at - (long)now));
  849. if (iter->table == unresolved)
  850. seq_printf(seq, " %8s %6hu",
  851. dt2str(now - entry->last_sent),
  852. entry->xmit_count);
  853. else
  854. seq_puts(seq, " ");
  855. seq_printf(seq, " %s\n",
  856. (iter->table == resolved) ? "resolved"
  857. : (iter->table == unresolved) ? "unresolved"
  858. : (iter->table == proxies) ? "proxies"
  859. : "unknown");
  860. }
  861. return 0;
  862. }
  863. static const struct seq_operations aarp_seq_ops = {
  864. .start = aarp_seq_start,
  865. .next = aarp_seq_next,
  866. .stop = aarp_seq_stop,
  867. .show = aarp_seq_show,
  868. };
  869. static int aarp_seq_open(struct inode *inode, struct file *file)
  870. {
  871. struct seq_file *seq;
  872. int rc = -ENOMEM;
  873. struct aarp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  874. if (!s)
  875. goto out;
  876. rc = seq_open(file, &aarp_seq_ops);
  877. if (rc)
  878. goto out_kfree;
  879. seq = file->private_data;
  880. seq->private = s;
  881. memset(s, 0, sizeof(*s));
  882. out:
  883. return rc;
  884. out_kfree:
  885. kfree(s);
  886. goto out;
  887. }
  888. const struct file_operations atalk_seq_arp_fops = {
  889. .owner = THIS_MODULE,
  890. .open = aarp_seq_open,
  891. .read = seq_read,
  892. .llseek = seq_lseek,
  893. .release = seq_release_private,
  894. };
  895. #endif
  896. /* General module cleanup. Called from cleanup_module() in ddp.c. */
  897. void aarp_cleanup_module(void)
  898. {
  899. del_timer_sync(&aarp_timer);
  900. unregister_netdevice_notifier(&aarp_notifier);
  901. unregister_snap_client(aarp_dl);
  902. aarp_purge();
  903. }