aarp.c 25 KB

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