Linux 5.7.7
[linux/fpc-iii.git] / net / appletalk / aarp.c
blob45f584171de79f0356812dc552620a39d9c85b94
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AARP: An implementation of the AppleTalk AARP protocol for
4 * Ethernet 'ELAP'.
6 * Alan Cox <Alan.Cox@linux.org>
8 * This doesn't fit cleanly with the IP arp. Potentially we can use
9 * the generic neighbour discovery code to clean this up.
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.
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
26 #include <linux/if_arp.h>
27 #include <linux/slab.h>
28 #include <net/sock.h>
29 #include <net/datalink.h>
30 #include <net/psnap.h>
31 #include <linux/atalk.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/export.h>
37 #include <linux/etherdevice.h>
39 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
40 int sysctl_aarp_tick_time = AARP_TICK_TIME;
41 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
42 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
44 /* Lists of aarp entries */
45 /**
46 * struct aarp_entry - AARP entry
47 * @last_sent - Last time we xmitted the aarp request
48 * @packet_queue - Queue of frames wait for resolution
49 * @status - Used for proxy AARP
50 * expires_at - Entry expiry time
51 * target_addr - DDP Address
52 * dev - Device to use
53 * hwaddr - Physical i/f address of target/router
54 * xmit_count - When this hits 10 we give up
55 * next - Next entry in chain
57 struct aarp_entry {
58 /* These first two are only used for unresolved entries */
59 unsigned long last_sent;
60 struct sk_buff_head packet_queue;
61 int status;
62 unsigned long expires_at;
63 struct atalk_addr target_addr;
64 struct net_device *dev;
65 char hwaddr[ETH_ALEN];
66 unsigned short xmit_count;
67 struct aarp_entry *next;
70 /* Hashed list of resolved, unresolved and proxy entries */
71 static struct aarp_entry *resolved[AARP_HASH_SIZE];
72 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
73 static struct aarp_entry *proxies[AARP_HASH_SIZE];
74 static int unresolved_count;
76 /* One lock protects it all. */
77 static DEFINE_RWLOCK(aarp_lock);
79 /* Used to walk the list and purge/kick entries. */
80 static struct timer_list aarp_timer;
83 * Delete an aarp queue
85 * Must run under aarp_lock.
87 static void __aarp_expire(struct aarp_entry *a)
89 skb_queue_purge(&a->packet_queue);
90 kfree(a);
94 * Send an aarp queue entry request
96 * Must run under aarp_lock.
98 static void __aarp_send_query(struct aarp_entry *a)
100 static unsigned char aarp_eth_multicast[ETH_ALEN] =
101 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
102 struct net_device *dev = a->dev;
103 struct elapaarp *eah;
104 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
105 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
106 struct atalk_addr *sat = atalk_find_dev_addr(dev);
108 if (!skb)
109 return;
111 if (!sat) {
112 kfree_skb(skb);
113 return;
116 /* Set up the buffer */
117 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
118 skb_reset_network_header(skb);
119 skb_reset_transport_header(skb);
120 skb_put(skb, sizeof(*eah));
121 skb->protocol = htons(ETH_P_ATALK);
122 skb->dev = dev;
123 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);
132 ether_addr_copy(eah->hw_src, dev->dev_addr);
134 eah->pa_src_zero = 0;
135 eah->pa_src_net = sat->s_net;
136 eah->pa_src_node = sat->s_node;
138 eth_zero_addr(eah->hw_dst);
140 eah->pa_dst_zero = 0;
141 eah->pa_dst_net = a->target_addr.s_net;
142 eah->pa_dst_node = a->target_addr.s_node;
144 /* Send it */
145 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
146 /* Update the sending count */
147 a->xmit_count++;
148 a->last_sent = jiffies;
151 /* This runs under aarp_lock and in softint context, so only atomic memory
152 * allocations can be used. */
153 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
154 struct atalk_addr *them, unsigned char *sha)
156 struct elapaarp *eah;
157 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
158 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
160 if (!skb)
161 return;
163 /* Set up the buffer */
164 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
165 skb_reset_network_header(skb);
166 skb_reset_transport_header(skb);
167 skb_put(skb, sizeof(*eah));
168 skb->protocol = htons(ETH_P_ATALK);
169 skb->dev = dev;
170 eah = aarp_hdr(skb);
172 /* Set up the ARP */
173 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
174 eah->pa_type = htons(ETH_P_ATALK);
175 eah->hw_len = ETH_ALEN;
176 eah->pa_len = AARP_PA_ALEN;
177 eah->function = htons(AARP_REPLY);
179 ether_addr_copy(eah->hw_src, dev->dev_addr);
181 eah->pa_src_zero = 0;
182 eah->pa_src_net = us->s_net;
183 eah->pa_src_node = us->s_node;
185 if (!sha)
186 eth_zero_addr(eah->hw_dst);
187 else
188 ether_addr_copy(eah->hw_dst, sha);
190 eah->pa_dst_zero = 0;
191 eah->pa_dst_net = them->s_net;
192 eah->pa_dst_node = them->s_node;
194 /* Send it */
195 aarp_dl->request(aarp_dl, skb, sha);
199 * Send probe frames. Called from aarp_probe_network and
200 * aarp_proxy_probe_network.
203 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
205 struct elapaarp *eah;
206 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
207 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
208 static unsigned char aarp_eth_multicast[ETH_ALEN] =
209 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
211 if (!skb)
212 return;
214 /* Set up the buffer */
215 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
216 skb_reset_network_header(skb);
217 skb_reset_transport_header(skb);
218 skb_put(skb, sizeof(*eah));
219 skb->protocol = htons(ETH_P_ATALK);
220 skb->dev = dev;
221 eah = aarp_hdr(skb);
223 /* Set up the ARP */
224 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
225 eah->pa_type = htons(ETH_P_ATALK);
226 eah->hw_len = ETH_ALEN;
227 eah->pa_len = AARP_PA_ALEN;
228 eah->function = htons(AARP_PROBE);
230 ether_addr_copy(eah->hw_src, dev->dev_addr);
232 eah->pa_src_zero = 0;
233 eah->pa_src_net = us->s_net;
234 eah->pa_src_node = us->s_node;
236 eth_zero_addr(eah->hw_dst);
238 eah->pa_dst_zero = 0;
239 eah->pa_dst_net = us->s_net;
240 eah->pa_dst_node = us->s_node;
242 /* Send it */
243 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
247 * Handle an aarp timer expire
249 * Must run under the aarp_lock.
252 static void __aarp_expire_timer(struct aarp_entry **n)
254 struct aarp_entry *t;
256 while (*n)
257 /* Expired ? */
258 if (time_after(jiffies, (*n)->expires_at)) {
259 t = *n;
260 *n = (*n)->next;
261 __aarp_expire(t);
262 } else
263 n = &((*n)->next);
267 * Kick all pending requests 5 times a second.
269 * Must run under the aarp_lock.
271 static void __aarp_kick(struct aarp_entry **n)
273 struct aarp_entry *t;
275 while (*n)
276 /* Expired: if this will be the 11th tx, we delete instead. */
277 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
278 t = *n;
279 *n = (*n)->next;
280 __aarp_expire(t);
281 } else {
282 __aarp_send_query(*n);
283 n = &((*n)->next);
288 * A device has gone down. Take all entries referring to the device
289 * and remove them.
291 * Must run under the aarp_lock.
293 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
295 struct aarp_entry *t;
297 while (*n)
298 if ((*n)->dev == dev) {
299 t = *n;
300 *n = (*n)->next;
301 __aarp_expire(t);
302 } else
303 n = &((*n)->next);
306 /* Handle the timer event */
307 static void aarp_expire_timeout(struct timer_list *unused)
309 int ct;
311 write_lock_bh(&aarp_lock);
313 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
314 __aarp_expire_timer(&resolved[ct]);
315 __aarp_kick(&unresolved[ct]);
316 __aarp_expire_timer(&unresolved[ct]);
317 __aarp_expire_timer(&proxies[ct]);
320 write_unlock_bh(&aarp_lock);
321 mod_timer(&aarp_timer, jiffies +
322 (unresolved_count ? sysctl_aarp_tick_time :
323 sysctl_aarp_expiry_time));
326 /* Network device notifier chain handler. */
327 static int aarp_device_event(struct notifier_block *this, unsigned long event,
328 void *ptr)
330 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
331 int ct;
333 if (!net_eq(dev_net(dev), &init_net))
334 return NOTIFY_DONE;
336 if (event == NETDEV_DOWN) {
337 write_lock_bh(&aarp_lock);
339 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
340 __aarp_expire_device(&resolved[ct], dev);
341 __aarp_expire_device(&unresolved[ct], dev);
342 __aarp_expire_device(&proxies[ct], dev);
345 write_unlock_bh(&aarp_lock);
347 return NOTIFY_DONE;
350 /* Expire all entries in a hash chain */
351 static void __aarp_expire_all(struct aarp_entry **n)
353 struct aarp_entry *t;
355 while (*n) {
356 t = *n;
357 *n = (*n)->next;
358 __aarp_expire(t);
362 /* Cleanup all hash chains -- module unloading */
363 static void aarp_purge(void)
365 int ct;
367 write_lock_bh(&aarp_lock);
368 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
369 __aarp_expire_all(&resolved[ct]);
370 __aarp_expire_all(&unresolved[ct]);
371 __aarp_expire_all(&proxies[ct]);
373 write_unlock_bh(&aarp_lock);
377 * Create a new aarp entry. This must use GFP_ATOMIC because it
378 * runs while holding spinlocks.
380 static struct aarp_entry *aarp_alloc(void)
382 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
384 if (a)
385 skb_queue_head_init(&a->packet_queue);
386 return a;
390 * Find an entry. We might return an expired but not yet purged entry. We
391 * don't care as it will do no harm.
393 * This must run under the aarp_lock.
395 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
396 struct net_device *dev,
397 struct atalk_addr *sat)
399 while (list) {
400 if (list->target_addr.s_net == sat->s_net &&
401 list->target_addr.s_node == sat->s_node &&
402 list->dev == dev)
403 break;
404 list = list->next;
407 return list;
410 /* Called from the DDP code, and thus must be exported. */
411 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
413 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
414 struct aarp_entry *a;
416 write_lock_bh(&aarp_lock);
418 a = __aarp_find_entry(proxies[hash], dev, sa);
419 if (a)
420 a->expires_at = jiffies - 1;
422 write_unlock_bh(&aarp_lock);
425 /* This must run under aarp_lock. */
426 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
427 struct atalk_addr *sa)
429 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
430 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
432 return a ? sa : NULL;
436 * Probe a Phase 1 device or a device that requires its Net:Node to
437 * be set via an ioctl.
439 static void aarp_send_probe_phase1(struct atalk_iface *iface)
441 struct ifreq atreq;
442 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
443 const struct net_device_ops *ops = iface->dev->netdev_ops;
445 sa->sat_addr.s_node = iface->address.s_node;
446 sa->sat_addr.s_net = ntohs(iface->address.s_net);
448 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
449 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
450 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
451 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
452 iface->address.s_node != sa->sat_addr.s_node)
453 iface->status |= ATIF_PROBE_FAIL;
455 iface->address.s_net = htons(sa->sat_addr.s_net);
456 iface->address.s_node = sa->sat_addr.s_node;
461 void aarp_probe_network(struct atalk_iface *atif)
463 if (atif->dev->type == ARPHRD_LOCALTLK ||
464 atif->dev->type == ARPHRD_PPP)
465 aarp_send_probe_phase1(atif);
466 else {
467 unsigned int count;
469 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
470 aarp_send_probe(atif->dev, &atif->address);
472 /* Defer 1/10th */
473 msleep(100);
475 if (atif->status & ATIF_PROBE_FAIL)
476 break;
481 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
483 int hash, retval = -EPROTONOSUPPORT;
484 struct aarp_entry *entry;
485 unsigned int count;
488 * we don't currently support LocalTalk or PPP for proxy AARP;
489 * if someone wants to try and add it, have fun
491 if (atif->dev->type == ARPHRD_LOCALTLK ||
492 atif->dev->type == ARPHRD_PPP)
493 goto out;
496 * create a new AARP entry with the flags set to be published --
497 * we need this one to hang around even if it's in use
499 entry = aarp_alloc();
500 retval = -ENOMEM;
501 if (!entry)
502 goto out;
504 entry->expires_at = -1;
505 entry->status = ATIF_PROBE;
506 entry->target_addr.s_node = sa->s_node;
507 entry->target_addr.s_net = sa->s_net;
508 entry->dev = atif->dev;
510 write_lock_bh(&aarp_lock);
512 hash = sa->s_node % (AARP_HASH_SIZE - 1);
513 entry->next = proxies[hash];
514 proxies[hash] = entry;
516 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
517 aarp_send_probe(atif->dev, sa);
519 /* Defer 1/10th */
520 write_unlock_bh(&aarp_lock);
521 msleep(100);
522 write_lock_bh(&aarp_lock);
524 if (entry->status & ATIF_PROBE_FAIL)
525 break;
528 if (entry->status & ATIF_PROBE_FAIL) {
529 entry->expires_at = jiffies - 1; /* free the entry */
530 retval = -EADDRINUSE; /* return network full */
531 } else { /* clear the probing flag */
532 entry->status &= ~ATIF_PROBE;
533 retval = 1;
536 write_unlock_bh(&aarp_lock);
537 out:
538 return retval;
541 /* Send a DDP frame */
542 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
543 struct atalk_addr *sa, void *hwaddr)
545 static char ddp_eth_multicast[ETH_ALEN] =
546 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
547 int hash;
548 struct aarp_entry *a;
550 skb_reset_network_header(skb);
552 /* Check for LocalTalk first */
553 if (dev->type == ARPHRD_LOCALTLK) {
554 struct atalk_addr *at = atalk_find_dev_addr(dev);
555 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
556 int ft = 2;
559 * Compressible ?
561 * IFF: src_net == dest_net == device_net
562 * (zero matches anything)
565 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
566 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
567 skb_pull(skb, sizeof(*ddp) - 4);
570 * The upper two remaining bytes are the port
571 * numbers we just happen to need. Now put the
572 * length in the lower two.
574 *((__be16 *)skb->data) = htons(skb->len);
575 ft = 1;
578 * Nice and easy. No AARP type protocols occur here so we can
579 * just shovel it out with a 3 byte LLAP header
582 skb_push(skb, 3);
583 skb->data[0] = sa->s_node;
584 skb->data[1] = at->s_node;
585 skb->data[2] = ft;
586 skb->dev = dev;
587 goto sendit;
590 /* On a PPP link we neither compress nor aarp. */
591 if (dev->type == ARPHRD_PPP) {
592 skb->protocol = htons(ETH_P_PPPTALK);
593 skb->dev = dev;
594 goto sendit;
597 /* Non ELAP we cannot do. */
598 if (dev->type != ARPHRD_ETHER)
599 goto free_it;
601 skb->dev = dev;
602 skb->protocol = htons(ETH_P_ATALK);
603 hash = sa->s_node % (AARP_HASH_SIZE - 1);
605 /* Do we have a resolved entry? */
606 if (sa->s_node == ATADDR_BCAST) {
607 /* Send it */
608 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
609 goto sent;
612 write_lock_bh(&aarp_lock);
613 a = __aarp_find_entry(resolved[hash], dev, sa);
615 if (a) { /* Return 1 and fill in the address */
616 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
617 ddp_dl->request(ddp_dl, skb, a->hwaddr);
618 write_unlock_bh(&aarp_lock);
619 goto sent;
622 /* Do we have an unresolved entry: This is the less common path */
623 a = __aarp_find_entry(unresolved[hash], dev, sa);
624 if (a) { /* Queue onto the unresolved queue */
625 skb_queue_tail(&a->packet_queue, skb);
626 goto out_unlock;
629 /* Allocate a new entry */
630 a = aarp_alloc();
631 if (!a) {
632 /* Whoops slipped... good job it's an unreliable protocol 8) */
633 write_unlock_bh(&aarp_lock);
634 goto free_it;
637 /* Set up the queue */
638 skb_queue_tail(&a->packet_queue, skb);
639 a->expires_at = jiffies + sysctl_aarp_resolve_time;
640 a->dev = dev;
641 a->next = unresolved[hash];
642 a->target_addr = *sa;
643 a->xmit_count = 0;
644 unresolved[hash] = a;
645 unresolved_count++;
647 /* Send an initial request for the address */
648 __aarp_send_query(a);
651 * Switch to fast timer if needed (That is if this is the first
652 * unresolved entry to get added)
655 if (unresolved_count == 1)
656 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
658 /* Now finally, it is safe to drop the lock. */
659 out_unlock:
660 write_unlock_bh(&aarp_lock);
662 /* Tell the ddp layer we have taken over for this frame. */
663 goto sent;
665 sendit:
666 if (skb->sk)
667 skb->priority = skb->sk->sk_priority;
668 if (dev_queue_xmit(skb))
669 goto drop;
670 sent:
671 return NET_XMIT_SUCCESS;
672 free_it:
673 kfree_skb(skb);
674 drop:
675 return NET_XMIT_DROP;
677 EXPORT_SYMBOL(aarp_send_ddp);
680 * An entry in the aarp unresolved queue has become resolved. Send
681 * all the frames queued under it.
683 * Must run under aarp_lock.
685 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
686 int hash)
688 struct sk_buff *skb;
690 while (*list)
691 if (*list == a) {
692 unresolved_count--;
693 *list = a->next;
695 /* Move into the resolved list */
696 a->next = resolved[hash];
697 resolved[hash] = a;
699 /* Kick frames off */
700 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
701 a->expires_at = jiffies +
702 sysctl_aarp_expiry_time * 10;
703 ddp_dl->request(ddp_dl, skb, a->hwaddr);
705 } else
706 list = &((*list)->next);
710 * This is called by the SNAP driver whenever we see an AARP SNAP
711 * frame. We currently only support Ethernet.
713 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
714 struct packet_type *pt, struct net_device *orig_dev)
716 struct elapaarp *ea = aarp_hdr(skb);
717 int hash, ret = 0;
718 __u16 function;
719 struct aarp_entry *a;
720 struct atalk_addr sa, *ma, da;
721 struct atalk_iface *ifa;
723 if (!net_eq(dev_net(dev), &init_net))
724 goto out0;
726 /* We only do Ethernet SNAP AARP. */
727 if (dev->type != ARPHRD_ETHER)
728 goto out0;
730 /* Frame size ok? */
731 if (!skb_pull(skb, sizeof(*ea)))
732 goto out0;
734 function = ntohs(ea->function);
736 /* Sanity check fields. */
737 if (function < AARP_REQUEST || function > AARP_PROBE ||
738 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
739 ea->pa_src_zero || ea->pa_dst_zero)
740 goto out0;
742 /* Looks good. */
743 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
745 /* Build an address. */
746 sa.s_node = ea->pa_src_node;
747 sa.s_net = ea->pa_src_net;
749 /* Process the packet. Check for replies of me. */
750 ifa = atalk_find_dev(dev);
751 if (!ifa)
752 goto out1;
754 if (ifa->status & ATIF_PROBE &&
755 ifa->address.s_node == ea->pa_dst_node &&
756 ifa->address.s_net == ea->pa_dst_net) {
757 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
758 goto out1;
761 /* Check for replies of proxy AARP entries */
762 da.s_node = ea->pa_dst_node;
763 da.s_net = ea->pa_dst_net;
765 write_lock_bh(&aarp_lock);
766 a = __aarp_find_entry(proxies[hash], dev, &da);
768 if (a && a->status & ATIF_PROBE) {
769 a->status |= ATIF_PROBE_FAIL;
771 * we do not respond to probe or request packets for
772 * this address while we are probing this address
774 goto unlock;
777 switch (function) {
778 case AARP_REPLY:
779 if (!unresolved_count) /* Speed up */
780 break;
782 /* Find the entry. */
783 a = __aarp_find_entry(unresolved[hash], dev, &sa);
784 if (!a || dev != a->dev)
785 break;
787 /* We can fill one in - this is good. */
788 ether_addr_copy(a->hwaddr, ea->hw_src);
789 __aarp_resolved(&unresolved[hash], a, hash);
790 if (!unresolved_count)
791 mod_timer(&aarp_timer,
792 jiffies + sysctl_aarp_expiry_time);
793 break;
795 case AARP_REQUEST:
796 case AARP_PROBE:
799 * If it is my address set ma to my address and reply.
800 * We can treat probe and request the same. Probe
801 * simply means we shouldn't cache the querying host,
802 * as in a probe they are proposing an address not
803 * using one.
805 * Support for proxy-AARP added. We check if the
806 * address is one of our proxies before we toss the
807 * packet out.
810 sa.s_node = ea->pa_dst_node;
811 sa.s_net = ea->pa_dst_net;
813 /* See if we have a matching proxy. */
814 ma = __aarp_proxy_find(dev, &sa);
815 if (!ma)
816 ma = &ifa->address;
817 else { /* We need to make a copy of the entry. */
818 da.s_node = sa.s_node;
819 da.s_net = sa.s_net;
820 ma = &da;
823 if (function == AARP_PROBE) {
825 * A probe implies someone trying to get an
826 * address. So as a precaution flush any
827 * entries we have for this address.
829 a = __aarp_find_entry(resolved[sa.s_node %
830 (AARP_HASH_SIZE - 1)],
831 skb->dev, &sa);
834 * Make it expire next tick - that avoids us
835 * getting into a probe/flush/learn/probe/
836 * flush/learn cycle during probing of a slow
837 * to respond host addr.
839 if (a) {
840 a->expires_at = jiffies - 1;
841 mod_timer(&aarp_timer, jiffies +
842 sysctl_aarp_tick_time);
846 if (sa.s_node != ma->s_node)
847 break;
849 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
850 break;
852 sa.s_node = ea->pa_src_node;
853 sa.s_net = ea->pa_src_net;
855 /* aarp_my_address has found the address to use for us.
857 aarp_send_reply(dev, ma, &sa, ea->hw_src);
858 break;
861 unlock:
862 write_unlock_bh(&aarp_lock);
863 out1:
864 ret = 1;
865 out0:
866 kfree_skb(skb);
867 return ret;
870 static struct notifier_block aarp_notifier = {
871 .notifier_call = aarp_device_event,
874 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
876 int __init aarp_proto_init(void)
878 int rc;
880 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
881 if (!aarp_dl) {
882 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
883 return -ENOMEM;
885 timer_setup(&aarp_timer, aarp_expire_timeout, 0);
886 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
887 add_timer(&aarp_timer);
888 rc = register_netdevice_notifier(&aarp_notifier);
889 if (rc) {
890 del_timer_sync(&aarp_timer);
891 unregister_snap_client(aarp_dl);
893 return rc;
896 /* Remove the AARP entries associated with a device. */
897 void aarp_device_down(struct net_device *dev)
899 int ct;
901 write_lock_bh(&aarp_lock);
903 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
904 __aarp_expire_device(&resolved[ct], dev);
905 __aarp_expire_device(&unresolved[ct], dev);
906 __aarp_expire_device(&proxies[ct], dev);
909 write_unlock_bh(&aarp_lock);
912 #ifdef CONFIG_PROC_FS
914 * Get the aarp entry that is in the chain described
915 * by the iterator.
916 * If pos is set then skip till that index.
917 * pos = 1 is the first entry
919 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
921 int ct = iter->bucket;
922 struct aarp_entry **table = iter->table;
923 loff_t off = 0;
924 struct aarp_entry *entry;
926 rescan:
927 while (ct < AARP_HASH_SIZE) {
928 for (entry = table[ct]; entry; entry = entry->next) {
929 if (!pos || ++off == *pos) {
930 iter->table = table;
931 iter->bucket = ct;
932 return entry;
935 ++ct;
938 if (table == resolved) {
939 ct = 0;
940 table = unresolved;
941 goto rescan;
943 if (table == unresolved) {
944 ct = 0;
945 table = proxies;
946 goto rescan;
948 return NULL;
951 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
952 __acquires(aarp_lock)
954 struct aarp_iter_state *iter = seq->private;
956 read_lock_bh(&aarp_lock);
957 iter->table = resolved;
958 iter->bucket = 0;
960 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
963 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
965 struct aarp_entry *entry = v;
966 struct aarp_iter_state *iter = seq->private;
968 ++*pos;
970 /* first line after header */
971 if (v == SEQ_START_TOKEN)
972 entry = iter_next(iter, NULL);
974 /* next entry in current bucket */
975 else if (entry->next)
976 entry = entry->next;
978 /* next bucket or table */
979 else {
980 ++iter->bucket;
981 entry = iter_next(iter, NULL);
983 return entry;
986 static void aarp_seq_stop(struct seq_file *seq, void *v)
987 __releases(aarp_lock)
989 read_unlock_bh(&aarp_lock);
992 static const char *dt2str(unsigned long ticks)
994 static char buf[32];
996 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
998 return buf;
1001 static int aarp_seq_show(struct seq_file *seq, void *v)
1003 struct aarp_iter_state *iter = seq->private;
1004 struct aarp_entry *entry = v;
1005 unsigned long now = jiffies;
1007 if (v == SEQ_START_TOKEN)
1008 seq_puts(seq,
1009 "Address Interface Hardware Address"
1010 " Expires LastSend Retry Status\n");
1011 else {
1012 seq_printf(seq, "%04X:%02X %-12s",
1013 ntohs(entry->target_addr.s_net),
1014 (unsigned int) entry->target_addr.s_node,
1015 entry->dev ? entry->dev->name : "????");
1016 seq_printf(seq, "%pM", entry->hwaddr);
1017 seq_printf(seq, " %8s",
1018 dt2str((long)entry->expires_at - (long)now));
1019 if (iter->table == unresolved)
1020 seq_printf(seq, " %8s %6hu",
1021 dt2str(now - entry->last_sent),
1022 entry->xmit_count);
1023 else
1024 seq_puts(seq, " ");
1025 seq_printf(seq, " %s\n",
1026 (iter->table == resolved) ? "resolved"
1027 : (iter->table == unresolved) ? "unresolved"
1028 : (iter->table == proxies) ? "proxies"
1029 : "unknown");
1031 return 0;
1034 const struct seq_operations aarp_seq_ops = {
1035 .start = aarp_seq_start,
1036 .next = aarp_seq_next,
1037 .stop = aarp_seq_stop,
1038 .show = aarp_seq_show,
1040 #endif
1042 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1043 void aarp_cleanup_module(void)
1045 del_timer_sync(&aarp_timer);
1046 unregister_netdevice_notifier(&aarp_notifier);
1047 unregister_snap_client(aarp_dl);
1048 aarp_purge();