aarp.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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(sizeof(*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. /*
  383. * Probe a Phase 1 device or a device that requires its Net:Node to
  384. * be set via an ioctl.
  385. */
  386. static void aarp_send_probe_phase1(struct atalk_iface *iface)
  387. {
  388. struct ifreq atreq;
  389. struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
  390. const struct net_device_ops *ops = iface->dev->netdev_ops;
  391. sa->sat_addr.s_node = iface->address.s_node;
  392. sa->sat_addr.s_net = ntohs(iface->address.s_net);
  393. /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
  394. if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
  395. ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
  396. if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
  397. iface->address.s_node != sa->sat_addr.s_node)
  398. iface->status |= ATIF_PROBE_FAIL;
  399. iface->address.s_net = htons(sa->sat_addr.s_net);
  400. iface->address.s_node = sa->sat_addr.s_node;
  401. }
  402. }
  403. void aarp_probe_network(struct atalk_iface *atif)
  404. {
  405. if (atif->dev->type == ARPHRD_LOCALTLK ||
  406. atif->dev->type == ARPHRD_PPP)
  407. aarp_send_probe_phase1(atif);
  408. else {
  409. unsigned int count;
  410. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  411. aarp_send_probe(atif->dev, &atif->address);
  412. /* Defer 1/10th */
  413. msleep(100);
  414. if (atif->status & ATIF_PROBE_FAIL)
  415. break;
  416. }
  417. }
  418. }
  419. int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
  420. {
  421. int hash, retval = -EPROTONOSUPPORT;
  422. struct aarp_entry *entry;
  423. unsigned int count;
  424. /*
  425. * we don't currently support LocalTalk or PPP for proxy AARP;
  426. * if someone wants to try and add it, have fun
  427. */
  428. if (atif->dev->type == ARPHRD_LOCALTLK ||
  429. atif->dev->type == ARPHRD_PPP)
  430. goto out;
  431. /*
  432. * create a new AARP entry with the flags set to be published --
  433. * we need this one to hang around even if it's in use
  434. */
  435. entry = aarp_alloc();
  436. retval = -ENOMEM;
  437. if (!entry)
  438. goto out;
  439. entry->expires_at = -1;
  440. entry->status = ATIF_PROBE;
  441. entry->target_addr.s_node = sa->s_node;
  442. entry->target_addr.s_net = sa->s_net;
  443. entry->dev = atif->dev;
  444. write_lock_bh(&aarp_lock);
  445. aarp_entry_get(entry);
  446. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  447. entry->next = proxies[hash];
  448. proxies[hash] = entry;
  449. for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
  450. aarp_send_probe(atif->dev, sa);
  451. /* Defer 1/10th */
  452. write_unlock_bh(&aarp_lock);
  453. msleep(100);
  454. write_lock_bh(&aarp_lock);
  455. if (entry->status & ATIF_PROBE_FAIL)
  456. break;
  457. }
  458. if (entry->status & ATIF_PROBE_FAIL) {
  459. entry->expires_at = jiffies - 1; /* free the entry */
  460. retval = -EADDRINUSE; /* return network full */
  461. } else { /* clear the probing flag */
  462. entry->status &= ~ATIF_PROBE;
  463. retval = 1;
  464. }
  465. aarp_entry_put(entry);
  466. write_unlock_bh(&aarp_lock);
  467. out:
  468. return retval;
  469. }
  470. /* Send a DDP frame */
  471. int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
  472. struct atalk_addr *sa, void *hwaddr)
  473. {
  474. static char ddp_eth_multicast[ETH_ALEN] =
  475. { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
  476. int hash;
  477. struct aarp_entry *a;
  478. skb_reset_network_header(skb);
  479. /* Check for LocalTalk first */
  480. if (dev->type == ARPHRD_LOCALTLK) {
  481. struct atalk_addr *at = atalk_find_dev_addr(dev);
  482. struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
  483. int ft = 2;
  484. /*
  485. * Compressible ?
  486. *
  487. * IFF: src_net == dest_net == device_net
  488. * (zero matches anything)
  489. */
  490. if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
  491. (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
  492. skb_pull(skb, sizeof(*ddp) - 4);
  493. /*
  494. * The upper two remaining bytes are the port
  495. * numbers we just happen to need. Now put the
  496. * length in the lower two.
  497. */
  498. *((__be16 *)skb->data) = htons(skb->len);
  499. ft = 1;
  500. }
  501. /*
  502. * Nice and easy. No AARP type protocols occur here so we can
  503. * just shovel it out with a 3 byte LLAP header
  504. */
  505. skb_push(skb, 3);
  506. skb->data[0] = sa->s_node;
  507. skb->data[1] = at->s_node;
  508. skb->data[2] = ft;
  509. skb->dev = dev;
  510. goto sendit;
  511. }
  512. /* On a PPP link we neither compress nor aarp. */
  513. if (dev->type == ARPHRD_PPP) {
  514. skb->protocol = htons(ETH_P_PPPTALK);
  515. skb->dev = dev;
  516. goto sendit;
  517. }
  518. /* Non ELAP we cannot do. */
  519. if (dev->type != ARPHRD_ETHER)
  520. goto free_it;
  521. skb->dev = dev;
  522. skb->protocol = htons(ETH_P_ATALK);
  523. hash = sa->s_node % (AARP_HASH_SIZE - 1);
  524. /* Do we have a resolved entry? */
  525. if (sa->s_node == ATADDR_BCAST) {
  526. /* Send it */
  527. ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
  528. goto sent;
  529. }
  530. write_lock_bh(&aarp_lock);
  531. a = __aarp_find_entry(resolved[hash], dev, sa);
  532. if (a) { /* Return 1 and fill in the address */
  533. a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
  534. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  535. write_unlock_bh(&aarp_lock);
  536. goto sent;
  537. }
  538. /* Do we have an unresolved entry: This is the less common path */
  539. a = __aarp_find_entry(unresolved[hash], dev, sa);
  540. if (a) { /* Queue onto the unresolved queue */
  541. skb_queue_tail(&a->packet_queue, skb);
  542. goto out_unlock;
  543. }
  544. /* Allocate a new entry */
  545. a = aarp_alloc();
  546. if (!a) {
  547. /* Whoops slipped... good job it's an unreliable protocol 8) */
  548. write_unlock_bh(&aarp_lock);
  549. goto free_it;
  550. }
  551. /* Set up the queue */
  552. skb_queue_tail(&a->packet_queue, skb);
  553. a->expires_at = jiffies + sysctl_aarp_resolve_time;
  554. a->dev = dev;
  555. a->next = unresolved[hash];
  556. a->target_addr = *sa;
  557. a->xmit_count = 0;
  558. unresolved[hash] = a;
  559. unresolved_count++;
  560. /* Send an initial request for the address */
  561. __aarp_send_query(a);
  562. /*
  563. * Switch to fast timer if needed (That is if this is the first
  564. * unresolved entry to get added)
  565. */
  566. if (unresolved_count == 1)
  567. mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
  568. /* Now finally, it is safe to drop the lock. */
  569. out_unlock:
  570. write_unlock_bh(&aarp_lock);
  571. /* Tell the ddp layer we have taken over for this frame. */
  572. goto sent;
  573. sendit:
  574. if (skb->sk)
  575. skb->priority = READ_ONCE(skb->sk->sk_priority);
  576. if (dev_queue_xmit(skb))
  577. goto drop;
  578. sent:
  579. return NET_XMIT_SUCCESS;
  580. free_it:
  581. kfree_skb(skb);
  582. drop:
  583. return NET_XMIT_DROP;
  584. }
  585. EXPORT_SYMBOL(aarp_send_ddp);
  586. /*
  587. * An entry in the aarp unresolved queue has become resolved. Send
  588. * all the frames queued under it.
  589. *
  590. * Must run under aarp_lock.
  591. */
  592. static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
  593. int hash)
  594. {
  595. struct sk_buff *skb;
  596. while (*list)
  597. if (*list == a) {
  598. unresolved_count--;
  599. *list = a->next;
  600. /* Move into the resolved list */
  601. a->next = resolved[hash];
  602. resolved[hash] = a;
  603. /* Kick frames off */
  604. while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
  605. a->expires_at = jiffies +
  606. sysctl_aarp_expiry_time * 10;
  607. ddp_dl->request(ddp_dl, skb, a->hwaddr);
  608. }
  609. } else
  610. list = &((*list)->next);
  611. }
  612. /*
  613. * This is called by the SNAP driver whenever we see an AARP SNAP
  614. * frame. We currently only support Ethernet.
  615. */
  616. static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
  617. struct packet_type *pt, struct net_device *orig_dev)
  618. {
  619. struct elapaarp *ea = aarp_hdr(skb);
  620. int hash, ret = 0;
  621. __u16 function;
  622. struct aarp_entry *a;
  623. struct atalk_addr sa, *ma, da;
  624. struct atalk_iface *ifa;
  625. if (!net_eq(dev_net(dev), &init_net))
  626. goto out0;
  627. /* We only do Ethernet SNAP AARP. */
  628. if (dev->type != ARPHRD_ETHER)
  629. goto out0;
  630. /* Frame size ok? */
  631. if (!skb_pull(skb, sizeof(*ea)))
  632. goto out0;
  633. function = ntohs(ea->function);
  634. /* Sanity check fields. */
  635. if (function < AARP_REQUEST || function > AARP_PROBE ||
  636. ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
  637. ea->pa_src_zero || ea->pa_dst_zero)
  638. goto out0;
  639. /* Looks good. */
  640. hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
  641. /* Build an address. */
  642. sa.s_node = ea->pa_src_node;
  643. sa.s_net = ea->pa_src_net;
  644. /* Process the packet. Check for replies of me. */
  645. ifa = atalk_find_dev(dev);
  646. if (!ifa)
  647. goto out1;
  648. if (ifa->status & ATIF_PROBE &&
  649. ifa->address.s_node == ea->pa_dst_node &&
  650. ifa->address.s_net == ea->pa_dst_net) {
  651. ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
  652. goto out1;
  653. }
  654. /* Check for replies of proxy AARP entries */
  655. da.s_node = ea->pa_dst_node;
  656. da.s_net = ea->pa_dst_net;
  657. write_lock_bh(&aarp_lock);
  658. a = __aarp_find_entry(proxies[hash], dev, &da);
  659. if (a && a->status & ATIF_PROBE) {
  660. a->status |= ATIF_PROBE_FAIL;
  661. /*
  662. * we do not respond to probe or request packets of
  663. * this address while we are probing this address
  664. */
  665. goto unlock;
  666. }
  667. switch (function) {
  668. case AARP_REPLY:
  669. if (!unresolved_count) /* Speed up */
  670. break;
  671. /* Find the entry. */
  672. a = __aarp_find_entry(unresolved[hash], dev, &sa);
  673. if (!a || dev != a->dev)
  674. break;
  675. /* We can fill one in - this is good. */
  676. ether_addr_copy(a->hwaddr, ea->hw_src);
  677. __aarp_resolved(&unresolved[hash], a, hash);
  678. if (!unresolved_count)
  679. mod_timer(&aarp_timer,
  680. jiffies + sysctl_aarp_expiry_time);
  681. break;
  682. case AARP_REQUEST:
  683. case AARP_PROBE:
  684. /*
  685. * If it is my address set ma to my address and reply.
  686. * We can treat probe and request the same. Probe
  687. * simply means we shouldn't cache the querying host,
  688. * as in a probe they are proposing an address not
  689. * using one.
  690. *
  691. * Support for proxy-AARP added. We check if the
  692. * address is one of our proxies before we toss the
  693. * packet out.
  694. */
  695. sa.s_node = ea->pa_dst_node;
  696. sa.s_net = ea->pa_dst_net;
  697. /* See if we have a matching proxy. */
  698. ma = __aarp_proxy_find(dev, &sa);
  699. if (!ma)
  700. ma = &ifa->address;
  701. else { /* We need to make a copy of the entry. */
  702. da.s_node = sa.s_node;
  703. da.s_net = sa.s_net;
  704. ma = &da;
  705. }
  706. if (function == AARP_PROBE) {
  707. /*
  708. * A probe implies someone trying to get an
  709. * address. So as a precaution flush any
  710. * entries we have for this address.
  711. */
  712. a = __aarp_find_entry(resolved[sa.s_node %
  713. (AARP_HASH_SIZE - 1)],
  714. skb->dev, &sa);
  715. /*
  716. * Make it expire next tick - that avoids us
  717. * getting into a probe/flush/learn/probe/
  718. * flush/learn cycle during probing of a slow
  719. * to respond host addr.
  720. */
  721. if (a) {
  722. a->expires_at = jiffies - 1;
  723. mod_timer(&aarp_timer, jiffies +
  724. sysctl_aarp_tick_time);
  725. }
  726. }
  727. if (sa.s_node != ma->s_node)
  728. break;
  729. if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
  730. break;
  731. sa.s_node = ea->pa_src_node;
  732. sa.s_net = ea->pa_src_net;
  733. /* aarp_my_address has found the address to use for us.
  734. */
  735. aarp_send_reply(dev, ma, &sa, ea->hw_src);
  736. break;
  737. }
  738. unlock:
  739. write_unlock_bh(&aarp_lock);
  740. out1:
  741. ret = 1;
  742. out0:
  743. kfree_skb(skb);
  744. return ret;
  745. }
  746. static struct notifier_block aarp_notifier = {
  747. .notifier_call = aarp_device_event,
  748. };
  749. static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
  750. int __init aarp_proto_init(void)
  751. {
  752. int rc;
  753. aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
  754. if (!aarp_dl) {
  755. printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
  756. return -ENOMEM;
  757. }
  758. timer_setup(&aarp_timer, aarp_expire_timeout, 0);
  759. aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
  760. add_timer(&aarp_timer);
  761. rc = register_netdevice_notifier(&aarp_notifier);
  762. if (rc) {
  763. del_timer_sync(&aarp_timer);
  764. unregister_snap_client(aarp_dl);
  765. }
  766. return rc;
  767. }
  768. /* Remove the AARP entries associated with a device. */
  769. void aarp_device_down(struct net_device *dev)
  770. {
  771. int ct;
  772. write_lock_bh(&aarp_lock);
  773. for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
  774. __aarp_expire_device(&resolved[ct], dev);
  775. __aarp_expire_device(&unresolved[ct], dev);
  776. __aarp_expire_device(&proxies[ct], dev);
  777. }
  778. write_unlock_bh(&aarp_lock);
  779. }
  780. #ifdef CONFIG_PROC_FS
  781. /*
  782. * Get the aarp entry that is in the chain described
  783. * by the iterator.
  784. * If pos is set then skip till that index.
  785. * pos = 1 is the first entry
  786. */
  787. static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
  788. {
  789. int ct = iter->bucket;
  790. struct aarp_entry **table = iter->table;
  791. loff_t off = 0;
  792. struct aarp_entry *entry;
  793. rescan:
  794. while (ct < AARP_HASH_SIZE) {
  795. for (entry = table[ct]; entry; entry = entry->next) {
  796. if (!pos || ++off == *pos) {
  797. iter->table = table;
  798. iter->bucket = ct;
  799. return entry;
  800. }
  801. }
  802. ++ct;
  803. }
  804. if (table == resolved) {
  805. ct = 0;
  806. table = unresolved;
  807. goto rescan;
  808. }
  809. if (table == unresolved) {
  810. ct = 0;
  811. table = proxies;
  812. goto rescan;
  813. }
  814. return NULL;
  815. }
  816. static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
  817. __acquires(aarp_lock)
  818. {
  819. struct aarp_iter_state *iter = seq->private;
  820. read_lock_bh(&aarp_lock);
  821. iter->table = resolved;
  822. iter->bucket = 0;
  823. return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
  824. }
  825. static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  826. {
  827. struct aarp_entry *entry = v;
  828. struct aarp_iter_state *iter = seq->private;
  829. ++*pos;
  830. /* first line after header */
  831. if (v == SEQ_START_TOKEN)
  832. entry = iter_next(iter, NULL);
  833. /* next entry in current bucket */
  834. else if (entry->next)
  835. entry = entry->next;
  836. /* next bucket or table */
  837. else {
  838. ++iter->bucket;
  839. entry = iter_next(iter, NULL);
  840. }
  841. return entry;
  842. }
  843. static void aarp_seq_stop(struct seq_file *seq, void *v)
  844. __releases(aarp_lock)
  845. {
  846. read_unlock_bh(&aarp_lock);
  847. }
  848. static const char *dt2str(unsigned long ticks)
  849. {
  850. static char buf[32];
  851. sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
  852. return buf;
  853. }
  854. static int aarp_seq_show(struct seq_file *seq, void *v)
  855. {
  856. struct aarp_iter_state *iter = seq->private;
  857. struct aarp_entry *entry = v;
  858. unsigned long now = jiffies;
  859. if (v == SEQ_START_TOKEN)
  860. seq_puts(seq,
  861. "Address Interface Hardware Address"
  862. " Expires LastSend Retry Status\n");
  863. else {
  864. seq_printf(seq, "%04X:%02X %-12s",
  865. ntohs(entry->target_addr.s_net),
  866. (unsigned int) entry->target_addr.s_node,
  867. entry->dev ? entry->dev->name : "????");
  868. seq_printf(seq, "%pM", entry->hwaddr);
  869. seq_printf(seq, " %8s",
  870. dt2str((long)entry->expires_at - (long)now));
  871. if (iter->table == unresolved)
  872. seq_printf(seq, " %8s %6hu",
  873. dt2str(now - entry->last_sent),
  874. entry->xmit_count);
  875. else
  876. seq_puts(seq, " ");
  877. seq_printf(seq, " %s\n",
  878. (iter->table == resolved) ? "resolved"
  879. : (iter->table == unresolved) ? "unresolved"
  880. : (iter->table == proxies) ? "proxies"
  881. : "unknown");
  882. }
  883. return 0;
  884. }
  885. const struct seq_operations aarp_seq_ops = {
  886. .start = aarp_seq_start,
  887. .next = aarp_seq_next,
  888. .stop = aarp_seq_stop,
  889. .show = aarp_seq_show,
  890. };
  891. #endif
  892. /* General module cleanup. Called from cleanup_module() in ddp.c. */
  893. void aarp_cleanup_module(void)
  894. {
  895. del_timer_sync(&aarp_timer);
  896. unregister_netdevice_notifier(&aarp_notifier);
  897. unregister_snap_client(aarp_dl);
  898. aarp_purge();
  899. }