aarp.c 24 KB

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