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. struct net_device *dev = ptr;
  286. int ct;
  287. if (dev->nd_net != &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 (dev->nd_net != &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. init_timer(&aarp_timer);
  736. aarp_timer.function = aarp_expire_timeout;
  737. aarp_timer.data = 0;
  738. aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
  739. add_timer(&aarp_timer);
  740. register_netdevice_notifier(&aarp_notifier);
  741. }
  742. /* Remove the AARP entries associated with a device. */
  743. void aarp_device_down(struct net_device *dev)
  744. {
  745. int ct;
  746. write_lock_bh(&aarp_lock);
  747. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  748. __aarp_expire_device(&resolved[ct], dev);
  749. __aarp_expire_device(&unresolved[ct], dev);
  750. __aarp_expire_device(&proxies[ct], dev);
  751. }
  752. write_unlock_bh(&aarp_lock);
  753. }
  754. #ifdef CONFIG_PROC_FS
  755. struct aarp_iter_state {
  756. int bucket;
  757. struct aarp_entry **table;
  758. };
  759. /*
  760. * Get the aarp entry that is in the chain described
  761. * by the iterator.
  762. * If pos is set then skip till that index.
  763. * pos = 1 is the first entry
  764. */
  765. static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
  766. {
  767. int ct = iter->bucket;
  768. struct aarp_entry **table = iter->table;
  769. loff_t off = 0;
  770. struct aarp_entry *entry;
  771. rescan:
  772. while(ct < AARP_HASH_SIZE) {
  773. for (entry = table[ct]; entry; entry = entry->next) {
  774. if (!pos || ++off == *pos) {
  775. iter->table = table;
  776. iter->bucket = ct;
  777. return entry;
  778. }
  779. }
  780. ++ct;
  781. }
  782. if (table == resolved) {
  783. ct = 0;
  784. table = unresolved;
  785. goto rescan;
  786. }
  787. if (table == unresolved) {
  788. ct = 0;
  789. table = proxies;
  790. goto rescan;
  791. }
  792. return NULL;
  793. }
  794. static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
  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. {
  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. struct seq_file *seq;
  871. int rc = -ENOMEM;
  872. struct aarp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  873. if (!s)
  874. goto out;
  875. rc = seq_open(file, &aarp_seq_ops);
  876. if (rc)
  877. goto out_kfree;
  878. seq = file->private_data;
  879. seq->private = s;
  880. memset(s, 0, sizeof(*s));
  881. out:
  882. return rc;
  883. out_kfree:
  884. kfree(s);
  885. goto out;
  886. }
  887. const struct file_operations atalk_seq_arp_fops = {
  888. .owner = THIS_MODULE,
  889. .open = aarp_seq_open,
  890. .read = seq_read,
  891. .llseek = seq_lseek,
  892. .release = seq_release_private,
  893. };
  894. #endif
  895. /* General module cleanup. Called from cleanup_module() in ddp.c. */
  896. void aarp_cleanup_module(void)
  897. {
  898. del_timer_sync(&aarp_timer);
  899. unregister_netdevice_notifier(&aarp_notifier);
  900. unregister_snap_client(aarp_dl);
  901. aarp_purge();
  902. }