powerpc/time: Handle wrapping of decrementer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / appletalk / aarp.c
blob173a2e82f486b4ee19411b18842e61f57b05a58c
1 /*
2 * AARP: An implementation of the AppleTalk AARP protocol for
3 * Ethernet 'ELAP'.
5 * Alan Cox <Alan.Cox@linux.org>
7 * This doesn't fit cleanly with the IP arp. Potentially we can use
8 * the generic neighbour discovery code to clean this up.
10 * FIXME:
11 * We ought to handle the retransmits with a single list and a
12 * separate fast timer for when it is needed.
13 * Use neighbour discovery code.
14 * Token Ring Support.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
22 * References:
23 * Inside AppleTalk (2nd Ed).
24 * Fixes:
25 * Jaume Grau - flush caches on AARP_PROBE
26 * Rob Newberry - Added proxy AARP and AARP proc fs,
27 * moved probing from DDP module.
28 * Arnaldo C. Melo - don't mangle rx packets
32 #include <linux/if_arp.h>
33 #include <linux/slab.h>
34 #include <net/sock.h>
35 #include <net/datalink.h>
36 #include <net/psnap.h>
37 #include <linux/atalk.h>
38 #include <linux/delay.h>
39 #include <linux/init.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42 #include <linux/export.h>
44 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
45 int sysctl_aarp_tick_time = AARP_TICK_TIME;
46 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
47 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
49 /* Lists of aarp entries */
50 /**
51 * struct aarp_entry - AARP entry
52 * @last_sent - Last time we xmitted the aarp request
53 * @packet_queue - Queue of frames wait for resolution
54 * @status - Used for proxy AARP
55 * expires_at - Entry expiry time
56 * target_addr - DDP Address
57 * dev - Device to use
58 * hwaddr - Physical i/f address of target/router
59 * xmit_count - When this hits 10 we give up
60 * next - Next entry in chain
62 struct aarp_entry {
63 /* These first two are only used for unresolved entries */
64 unsigned long last_sent;
65 struct sk_buff_head packet_queue;
66 int status;
67 unsigned long expires_at;
68 struct atalk_addr target_addr;
69 struct net_device *dev;
70 char hwaddr[6];
71 unsigned short xmit_count;
72 struct aarp_entry *next;
75 /* Hashed list of resolved, unresolved and proxy entries */
76 static struct aarp_entry *resolved[AARP_HASH_SIZE];
77 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
78 static struct aarp_entry *proxies[AARP_HASH_SIZE];
79 static int unresolved_count;
81 /* One lock protects it all. */
82 static DEFINE_RWLOCK(aarp_lock);
84 /* Used to walk the list and purge/kick entries. */
85 static struct timer_list aarp_timer;
88 * Delete an aarp queue
90 * Must run under aarp_lock.
92 static void __aarp_expire(struct aarp_entry *a)
94 skb_queue_purge(&a->packet_queue);
95 kfree(a);
99 * Send an aarp queue entry request
101 * Must run under aarp_lock.
103 static void __aarp_send_query(struct aarp_entry *a)
105 static unsigned char aarp_eth_multicast[ETH_ALEN] =
106 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
107 struct net_device *dev = a->dev;
108 struct elapaarp *eah;
109 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
110 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
111 struct atalk_addr *sat = atalk_find_dev_addr(dev);
113 if (!skb)
114 return;
116 if (!sat) {
117 kfree_skb(skb);
118 return;
121 /* Set up the buffer */
122 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
123 skb_reset_network_header(skb);
124 skb_reset_transport_header(skb);
125 skb_put(skb, sizeof(*eah));
126 skb->protocol = htons(ETH_P_ATALK);
127 skb->dev = dev;
128 eah = aarp_hdr(skb);
130 /* Set up the ARP */
131 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
132 eah->pa_type = htons(ETH_P_ATALK);
133 eah->hw_len = ETH_ALEN;
134 eah->pa_len = AARP_PA_ALEN;
135 eah->function = htons(AARP_REQUEST);
137 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
139 eah->pa_src_zero = 0;
140 eah->pa_src_net = sat->s_net;
141 eah->pa_src_node = sat->s_node;
143 memset(eah->hw_dst, '\0', ETH_ALEN);
145 eah->pa_dst_zero = 0;
146 eah->pa_dst_net = a->target_addr.s_net;
147 eah->pa_dst_node = a->target_addr.s_node;
149 /* Send it */
150 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
151 /* Update the sending count */
152 a->xmit_count++;
153 a->last_sent = jiffies;
156 /* This runs under aarp_lock and in softint context, so only atomic memory
157 * allocations can be used. */
158 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
159 struct atalk_addr *them, unsigned char *sha)
161 struct elapaarp *eah;
162 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
163 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
165 if (!skb)
166 return;
168 /* Set up the buffer */
169 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
170 skb_reset_network_header(skb);
171 skb_reset_transport_header(skb);
172 skb_put(skb, sizeof(*eah));
173 skb->protocol = htons(ETH_P_ATALK);
174 skb->dev = dev;
175 eah = aarp_hdr(skb);
177 /* Set up the ARP */
178 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
179 eah->pa_type = htons(ETH_P_ATALK);
180 eah->hw_len = ETH_ALEN;
181 eah->pa_len = AARP_PA_ALEN;
182 eah->function = htons(AARP_REPLY);
184 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
186 eah->pa_src_zero = 0;
187 eah->pa_src_net = us->s_net;
188 eah->pa_src_node = us->s_node;
190 if (!sha)
191 memset(eah->hw_dst, '\0', ETH_ALEN);
192 else
193 memcpy(eah->hw_dst, sha, ETH_ALEN);
195 eah->pa_dst_zero = 0;
196 eah->pa_dst_net = them->s_net;
197 eah->pa_dst_node = them->s_node;
199 /* Send it */
200 aarp_dl->request(aarp_dl, skb, sha);
204 * Send probe frames. Called from aarp_probe_network and
205 * aarp_proxy_probe_network.
208 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
210 struct elapaarp *eah;
211 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
212 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
213 static unsigned char aarp_eth_multicast[ETH_ALEN] =
214 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
216 if (!skb)
217 return;
219 /* Set up the buffer */
220 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
221 skb_reset_network_header(skb);
222 skb_reset_transport_header(skb);
223 skb_put(skb, sizeof(*eah));
224 skb->protocol = htons(ETH_P_ATALK);
225 skb->dev = dev;
226 eah = aarp_hdr(skb);
228 /* Set up the ARP */
229 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
230 eah->pa_type = htons(ETH_P_ATALK);
231 eah->hw_len = ETH_ALEN;
232 eah->pa_len = AARP_PA_ALEN;
233 eah->function = htons(AARP_PROBE);
235 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
237 eah->pa_src_zero = 0;
238 eah->pa_src_net = us->s_net;
239 eah->pa_src_node = us->s_node;
241 memset(eah->hw_dst, '\0', ETH_ALEN);
243 eah->pa_dst_zero = 0;
244 eah->pa_dst_net = us->s_net;
245 eah->pa_dst_node = us->s_node;
247 /* Send it */
248 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
252 * Handle an aarp timer expire
254 * Must run under the aarp_lock.
257 static void __aarp_expire_timer(struct aarp_entry **n)
259 struct aarp_entry *t;
261 while (*n)
262 /* Expired ? */
263 if (time_after(jiffies, (*n)->expires_at)) {
264 t = *n;
265 *n = (*n)->next;
266 __aarp_expire(t);
267 } else
268 n = &((*n)->next);
272 * Kick all pending requests 5 times a second.
274 * Must run under the aarp_lock.
276 static void __aarp_kick(struct aarp_entry **n)
278 struct aarp_entry *t;
280 while (*n)
281 /* Expired: if this will be the 11th tx, we delete instead. */
282 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
283 t = *n;
284 *n = (*n)->next;
285 __aarp_expire(t);
286 } else {
287 __aarp_send_query(*n);
288 n = &((*n)->next);
293 * A device has gone down. Take all entries referring to the device
294 * and remove them.
296 * Must run under the aarp_lock.
298 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
300 struct aarp_entry *t;
302 while (*n)
303 if ((*n)->dev == dev) {
304 t = *n;
305 *n = (*n)->next;
306 __aarp_expire(t);
307 } else
308 n = &((*n)->next);
311 /* Handle the timer event */
312 static void aarp_expire_timeout(unsigned long unused)
314 int ct;
316 write_lock_bh(&aarp_lock);
318 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
319 __aarp_expire_timer(&resolved[ct]);
320 __aarp_kick(&unresolved[ct]);
321 __aarp_expire_timer(&unresolved[ct]);
322 __aarp_expire_timer(&proxies[ct]);
325 write_unlock_bh(&aarp_lock);
326 mod_timer(&aarp_timer, jiffies +
327 (unresolved_count ? sysctl_aarp_tick_time :
328 sysctl_aarp_expiry_time));
331 /* Network device notifier chain handler. */
332 static int aarp_device_event(struct notifier_block *this, unsigned long event,
333 void *ptr)
335 struct net_device *dev = ptr;
336 int ct;
338 if (!net_eq(dev_net(dev), &init_net))
339 return NOTIFY_DONE;
341 if (event == NETDEV_DOWN) {
342 write_lock_bh(&aarp_lock);
344 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
345 __aarp_expire_device(&resolved[ct], dev);
346 __aarp_expire_device(&unresolved[ct], dev);
347 __aarp_expire_device(&proxies[ct], dev);
350 write_unlock_bh(&aarp_lock);
352 return NOTIFY_DONE;
355 /* Expire all entries in a hash chain */
356 static void __aarp_expire_all(struct aarp_entry **n)
358 struct aarp_entry *t;
360 while (*n) {
361 t = *n;
362 *n = (*n)->next;
363 __aarp_expire(t);
367 /* Cleanup all hash chains -- module unloading */
368 static void aarp_purge(void)
370 int ct;
372 write_lock_bh(&aarp_lock);
373 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
374 __aarp_expire_all(&resolved[ct]);
375 __aarp_expire_all(&unresolved[ct]);
376 __aarp_expire_all(&proxies[ct]);
378 write_unlock_bh(&aarp_lock);
382 * Create a new aarp entry. This must use GFP_ATOMIC because it
383 * runs while holding spinlocks.
385 static struct aarp_entry *aarp_alloc(void)
387 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
389 if (a)
390 skb_queue_head_init(&a->packet_queue);
391 return a;
395 * Find an entry. We might return an expired but not yet purged entry. We
396 * don't care as it will do no harm.
398 * This must run under the aarp_lock.
400 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
401 struct net_device *dev,
402 struct atalk_addr *sat)
404 while (list) {
405 if (list->target_addr.s_net == sat->s_net &&
406 list->target_addr.s_node == sat->s_node &&
407 list->dev == dev)
408 break;
409 list = list->next;
412 return list;
415 /* Called from the DDP code, and thus must be exported. */
416 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
418 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
419 struct aarp_entry *a;
421 write_lock_bh(&aarp_lock);
423 a = __aarp_find_entry(proxies[hash], dev, sa);
424 if (a)
425 a->expires_at = jiffies - 1;
427 write_unlock_bh(&aarp_lock);
430 /* This must run under aarp_lock. */
431 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
432 struct atalk_addr *sa)
434 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
435 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
437 return a ? sa : NULL;
441 * Probe a Phase 1 device or a device that requires its Net:Node to
442 * be set via an ioctl.
444 static void aarp_send_probe_phase1(struct atalk_iface *iface)
446 struct ifreq atreq;
447 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
448 const struct net_device_ops *ops = iface->dev->netdev_ops;
450 sa->sat_addr.s_node = iface->address.s_node;
451 sa->sat_addr.s_net = ntohs(iface->address.s_net);
453 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
454 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
455 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
456 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
457 iface->address.s_node != sa->sat_addr.s_node)
458 iface->status |= ATIF_PROBE_FAIL;
460 iface->address.s_net = htons(sa->sat_addr.s_net);
461 iface->address.s_node = sa->sat_addr.s_node;
466 void aarp_probe_network(struct atalk_iface *atif)
468 if (atif->dev->type == ARPHRD_LOCALTLK ||
469 atif->dev->type == ARPHRD_PPP)
470 aarp_send_probe_phase1(atif);
471 else {
472 unsigned int count;
474 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
475 aarp_send_probe(atif->dev, &atif->address);
477 /* Defer 1/10th */
478 msleep(100);
480 if (atif->status & ATIF_PROBE_FAIL)
481 break;
486 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
488 int hash, retval = -EPROTONOSUPPORT;
489 struct aarp_entry *entry;
490 unsigned int count;
493 * we don't currently support LocalTalk or PPP for proxy AARP;
494 * if someone wants to try and add it, have fun
496 if (atif->dev->type == ARPHRD_LOCALTLK ||
497 atif->dev->type == ARPHRD_PPP)
498 goto out;
501 * create a new AARP entry with the flags set to be published --
502 * we need this one to hang around even if it's in use
504 entry = aarp_alloc();
505 retval = -ENOMEM;
506 if (!entry)
507 goto out;
509 entry->expires_at = -1;
510 entry->status = ATIF_PROBE;
511 entry->target_addr.s_node = sa->s_node;
512 entry->target_addr.s_net = sa->s_net;
513 entry->dev = atif->dev;
515 write_lock_bh(&aarp_lock);
517 hash = sa->s_node % (AARP_HASH_SIZE - 1);
518 entry->next = proxies[hash];
519 proxies[hash] = entry;
521 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
522 aarp_send_probe(atif->dev, sa);
524 /* Defer 1/10th */
525 write_unlock_bh(&aarp_lock);
526 msleep(100);
527 write_lock_bh(&aarp_lock);
529 if (entry->status & ATIF_PROBE_FAIL)
530 break;
533 if (entry->status & ATIF_PROBE_FAIL) {
534 entry->expires_at = jiffies - 1; /* free the entry */
535 retval = -EADDRINUSE; /* return network full */
536 } else { /* clear the probing flag */
537 entry->status &= ~ATIF_PROBE;
538 retval = 1;
541 write_unlock_bh(&aarp_lock);
542 out:
543 return retval;
546 /* Send a DDP frame */
547 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
548 struct atalk_addr *sa, void *hwaddr)
550 static char ddp_eth_multicast[ETH_ALEN] =
551 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
552 int hash;
553 struct aarp_entry *a;
555 skb_reset_network_header(skb);
557 /* Check for LocalTalk first */
558 if (dev->type == ARPHRD_LOCALTLK) {
559 struct atalk_addr *at = atalk_find_dev_addr(dev);
560 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
561 int ft = 2;
564 * Compressible ?
566 * IFF: src_net == dest_net == device_net
567 * (zero matches anything)
570 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
571 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
572 skb_pull(skb, sizeof(*ddp) - 4);
575 * The upper two remaining bytes are the port
576 * numbers we just happen to need. Now put the
577 * length in the lower two.
579 *((__be16 *)skb->data) = htons(skb->len);
580 ft = 1;
583 * Nice and easy. No AARP type protocols occur here so we can
584 * just shovel it out with a 3 byte LLAP header
587 skb_push(skb, 3);
588 skb->data[0] = sa->s_node;
589 skb->data[1] = at->s_node;
590 skb->data[2] = ft;
591 skb->dev = dev;
592 goto sendit;
595 /* On a PPP link we neither compress nor aarp. */
596 if (dev->type == ARPHRD_PPP) {
597 skb->protocol = htons(ETH_P_PPPTALK);
598 skb->dev = dev;
599 goto sendit;
602 /* Non ELAP we cannot do. */
603 if (dev->type != ARPHRD_ETHER)
604 goto free_it;
606 skb->dev = dev;
607 skb->protocol = htons(ETH_P_ATALK);
608 hash = sa->s_node % (AARP_HASH_SIZE - 1);
610 /* Do we have a resolved entry? */
611 if (sa->s_node == ATADDR_BCAST) {
612 /* Send it */
613 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
614 goto sent;
617 write_lock_bh(&aarp_lock);
618 a = __aarp_find_entry(resolved[hash], dev, sa);
620 if (a) { /* Return 1 and fill in the address */
621 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
622 ddp_dl->request(ddp_dl, skb, a->hwaddr);
623 write_unlock_bh(&aarp_lock);
624 goto sent;
627 /* Do we have an unresolved entry: This is the less common path */
628 a = __aarp_find_entry(unresolved[hash], dev, sa);
629 if (a) { /* Queue onto the unresolved queue */
630 skb_queue_tail(&a->packet_queue, skb);
631 goto out_unlock;
634 /* Allocate a new entry */
635 a = aarp_alloc();
636 if (!a) {
637 /* Whoops slipped... good job it's an unreliable protocol 8) */
638 write_unlock_bh(&aarp_lock);
639 goto free_it;
642 /* Set up the queue */
643 skb_queue_tail(&a->packet_queue, skb);
644 a->expires_at = jiffies + sysctl_aarp_resolve_time;
645 a->dev = dev;
646 a->next = unresolved[hash];
647 a->target_addr = *sa;
648 a->xmit_count = 0;
649 unresolved[hash] = a;
650 unresolved_count++;
652 /* Send an initial request for the address */
653 __aarp_send_query(a);
656 * Switch to fast timer if needed (That is if this is the first
657 * unresolved entry to get added)
660 if (unresolved_count == 1)
661 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
663 /* Now finally, it is safe to drop the lock. */
664 out_unlock:
665 write_unlock_bh(&aarp_lock);
667 /* Tell the ddp layer we have taken over for this frame. */
668 goto sent;
670 sendit:
671 if (skb->sk)
672 skb->priority = skb->sk->sk_priority;
673 if (dev_queue_xmit(skb))
674 goto drop;
675 sent:
676 return NET_XMIT_SUCCESS;
677 free_it:
678 kfree_skb(skb);
679 drop:
680 return NET_XMIT_DROP;
682 EXPORT_SYMBOL(aarp_send_ddp);
685 * An entry in the aarp unresolved queue has become resolved. Send
686 * all the frames queued under it.
688 * Must run under aarp_lock.
690 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
691 int hash)
693 struct sk_buff *skb;
695 while (*list)
696 if (*list == a) {
697 unresolved_count--;
698 *list = a->next;
700 /* Move into the resolved list */
701 a->next = resolved[hash];
702 resolved[hash] = a;
704 /* Kick frames off */
705 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
706 a->expires_at = jiffies +
707 sysctl_aarp_expiry_time * 10;
708 ddp_dl->request(ddp_dl, skb, a->hwaddr);
710 } else
711 list = &((*list)->next);
715 * This is called by the SNAP driver whenever we see an AARP SNAP
716 * frame. We currently only support Ethernet.
718 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
719 struct packet_type *pt, struct net_device *orig_dev)
721 struct elapaarp *ea = aarp_hdr(skb);
722 int hash, ret = 0;
723 __u16 function;
724 struct aarp_entry *a;
725 struct atalk_addr sa, *ma, da;
726 struct atalk_iface *ifa;
728 if (!net_eq(dev_net(dev), &init_net))
729 goto out0;
731 /* We only do Ethernet SNAP AARP. */
732 if (dev->type != ARPHRD_ETHER)
733 goto out0;
735 /* Frame size ok? */
736 if (!skb_pull(skb, sizeof(*ea)))
737 goto out0;
739 function = ntohs(ea->function);
741 /* Sanity check fields. */
742 if (function < AARP_REQUEST || function > AARP_PROBE ||
743 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
744 ea->pa_src_zero || ea->pa_dst_zero)
745 goto out0;
747 /* Looks good. */
748 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
750 /* Build an address. */
751 sa.s_node = ea->pa_src_node;
752 sa.s_net = ea->pa_src_net;
754 /* Process the packet. Check for replies of me. */
755 ifa = atalk_find_dev(dev);
756 if (!ifa)
757 goto out1;
759 if (ifa->status & ATIF_PROBE &&
760 ifa->address.s_node == ea->pa_dst_node &&
761 ifa->address.s_net == ea->pa_dst_net) {
762 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
763 goto out1;
766 /* Check for replies of proxy AARP entries */
767 da.s_node = ea->pa_dst_node;
768 da.s_net = ea->pa_dst_net;
770 write_lock_bh(&aarp_lock);
771 a = __aarp_find_entry(proxies[hash], dev, &da);
773 if (a && a->status & ATIF_PROBE) {
774 a->status |= ATIF_PROBE_FAIL;
776 * we do not respond to probe or request packets for
777 * this address while we are probing this address
779 goto unlock;
782 switch (function) {
783 case AARP_REPLY:
784 if (!unresolved_count) /* Speed up */
785 break;
787 /* Find the entry. */
788 a = __aarp_find_entry(unresolved[hash], dev, &sa);
789 if (!a || dev != a->dev)
790 break;
792 /* We can fill one in - this is good. */
793 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
794 __aarp_resolved(&unresolved[hash], a, hash);
795 if (!unresolved_count)
796 mod_timer(&aarp_timer,
797 jiffies + sysctl_aarp_expiry_time);
798 break;
800 case AARP_REQUEST:
801 case AARP_PROBE:
804 * If it is my address set ma to my address and reply.
805 * We can treat probe and request the same. Probe
806 * simply means we shouldn't cache the querying host,
807 * as in a probe they are proposing an address not
808 * using one.
810 * Support for proxy-AARP added. We check if the
811 * address is one of our proxies before we toss the
812 * packet out.
815 sa.s_node = ea->pa_dst_node;
816 sa.s_net = ea->pa_dst_net;
818 /* See if we have a matching proxy. */
819 ma = __aarp_proxy_find(dev, &sa);
820 if (!ma)
821 ma = &ifa->address;
822 else { /* We need to make a copy of the entry. */
823 da.s_node = sa.s_node;
824 da.s_net = sa.s_net;
825 ma = &da;
828 if (function == AARP_PROBE) {
830 * A probe implies someone trying to get an
831 * address. So as a precaution flush any
832 * entries we have for this address.
834 a = __aarp_find_entry(resolved[sa.s_node %
835 (AARP_HASH_SIZE - 1)],
836 skb->dev, &sa);
839 * Make it expire next tick - that avoids us
840 * getting into a probe/flush/learn/probe/
841 * flush/learn cycle during probing of a slow
842 * to respond host addr.
844 if (a) {
845 a->expires_at = jiffies - 1;
846 mod_timer(&aarp_timer, jiffies +
847 sysctl_aarp_tick_time);
851 if (sa.s_node != ma->s_node)
852 break;
854 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
855 break;
857 sa.s_node = ea->pa_src_node;
858 sa.s_net = ea->pa_src_net;
860 /* aarp_my_address has found the address to use for us.
862 aarp_send_reply(dev, ma, &sa, ea->hw_src);
863 break;
866 unlock:
867 write_unlock_bh(&aarp_lock);
868 out1:
869 ret = 1;
870 out0:
871 kfree_skb(skb);
872 return ret;
875 static struct notifier_block aarp_notifier = {
876 .notifier_call = aarp_device_event,
879 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
881 void __init aarp_proto_init(void)
883 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
884 if (!aarp_dl)
885 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
886 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
887 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
888 add_timer(&aarp_timer);
889 register_netdevice_notifier(&aarp_notifier);
892 /* Remove the AARP entries associated with a device. */
893 void aarp_device_down(struct net_device *dev)
895 int ct;
897 write_lock_bh(&aarp_lock);
899 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
900 __aarp_expire_device(&resolved[ct], dev);
901 __aarp_expire_device(&unresolved[ct], dev);
902 __aarp_expire_device(&proxies[ct], dev);
905 write_unlock_bh(&aarp_lock);
908 #ifdef CONFIG_PROC_FS
909 struct aarp_iter_state {
910 int bucket;
911 struct aarp_entry **table;
915 * Get the aarp entry that is in the chain described
916 * by the iterator.
917 * If pos is set then skip till that index.
918 * pos = 1 is the first entry
920 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
922 int ct = iter->bucket;
923 struct aarp_entry **table = iter->table;
924 loff_t off = 0;
925 struct aarp_entry *entry;
927 rescan:
928 while(ct < AARP_HASH_SIZE) {
929 for (entry = table[ct]; entry; entry = entry->next) {
930 if (!pos || ++off == *pos) {
931 iter->table = table;
932 iter->bucket = ct;
933 return entry;
936 ++ct;
939 if (table == resolved) {
940 ct = 0;
941 table = unresolved;
942 goto rescan;
944 if (table == unresolved) {
945 ct = 0;
946 table = proxies;
947 goto rescan;
949 return NULL;
952 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
953 __acquires(aarp_lock)
955 struct aarp_iter_state *iter = seq->private;
957 read_lock_bh(&aarp_lock);
958 iter->table = resolved;
959 iter->bucket = 0;
961 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
964 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
966 struct aarp_entry *entry = v;
967 struct aarp_iter_state *iter = seq->private;
969 ++*pos;
971 /* first line after header */
972 if (v == SEQ_START_TOKEN)
973 entry = iter_next(iter, NULL);
975 /* next entry in current bucket */
976 else if (entry->next)
977 entry = entry->next;
979 /* next bucket or table */
980 else {
981 ++iter->bucket;
982 entry = iter_next(iter, NULL);
984 return entry;
987 static void aarp_seq_stop(struct seq_file *seq, void *v)
988 __releases(aarp_lock)
990 read_unlock_bh(&aarp_lock);
993 static const char *dt2str(unsigned long ticks)
995 static char buf[32];
997 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
999 return buf;
1002 static int aarp_seq_show(struct seq_file *seq, void *v)
1004 struct aarp_iter_state *iter = seq->private;
1005 struct aarp_entry *entry = v;
1006 unsigned long now = jiffies;
1008 if (v == SEQ_START_TOKEN)
1009 seq_puts(seq,
1010 "Address Interface Hardware Address"
1011 " Expires LastSend Retry Status\n");
1012 else {
1013 seq_printf(seq, "%04X:%02X %-12s",
1014 ntohs(entry->target_addr.s_net),
1015 (unsigned int) entry->target_addr.s_node,
1016 entry->dev ? entry->dev->name : "????");
1017 seq_printf(seq, "%pM", entry->hwaddr);
1018 seq_printf(seq, " %8s",
1019 dt2str((long)entry->expires_at - (long)now));
1020 if (iter->table == unresolved)
1021 seq_printf(seq, " %8s %6hu",
1022 dt2str(now - entry->last_sent),
1023 entry->xmit_count);
1024 else
1025 seq_puts(seq, " ");
1026 seq_printf(seq, " %s\n",
1027 (iter->table == resolved) ? "resolved"
1028 : (iter->table == unresolved) ? "unresolved"
1029 : (iter->table == proxies) ? "proxies"
1030 : "unknown");
1032 return 0;
1035 static const struct seq_operations aarp_seq_ops = {
1036 .start = aarp_seq_start,
1037 .next = aarp_seq_next,
1038 .stop = aarp_seq_stop,
1039 .show = aarp_seq_show,
1042 static int aarp_seq_open(struct inode *inode, struct file *file)
1044 return seq_open_private(file, &aarp_seq_ops,
1045 sizeof(struct aarp_iter_state));
1048 const struct file_operations atalk_seq_arp_fops = {
1049 .owner = THIS_MODULE,
1050 .open = aarp_seq_open,
1051 .read = seq_read,
1052 .llseek = seq_lseek,
1053 .release = seq_release_private,
1055 #endif
1057 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1058 void aarp_cleanup_module(void)
1060 del_timer_sync(&aarp_timer);
1061 unregister_netdevice_notifier(&aarp_notifier);
1062 unregister_snap_client(aarp_dl);
1063 aarp_purge();