aarp.c 25 KB

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