aarp.c 24 KB

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