Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc
[linux-2.6/linux-2.6-stable.git] / net / appletalk / aarp.c
blob1acc69576df8114689ad30175bb61755040ebec1
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>
43 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
44 int sysctl_aarp_tick_time = AARP_TICK_TIME;
45 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
46 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
48 /* Lists of aarp entries */
49 /**
50 * struct aarp_entry - AARP entry
51 * @last_sent - Last time we xmitted the aarp request
52 * @packet_queue - Queue of frames wait for resolution
53 * @status - Used for proxy AARP
54 * expires_at - Entry expiry time
55 * target_addr - DDP Address
56 * dev - Device to use
57 * hwaddr - Physical i/f address of target/router
58 * xmit_count - When this hits 10 we give up
59 * next - Next entry in chain
61 struct aarp_entry {
62 /* These first two are only used for unresolved entries */
63 unsigned long last_sent;
64 struct sk_buff_head packet_queue;
65 int status;
66 unsigned long expires_at;
67 struct atalk_addr target_addr;
68 struct net_device *dev;
69 char hwaddr[6];
70 unsigned short xmit_count;
71 struct aarp_entry *next;
74 /* Hashed list of resolved, unresolved and proxy entries */
75 static struct aarp_entry *resolved[AARP_HASH_SIZE];
76 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
77 static struct aarp_entry *proxies[AARP_HASH_SIZE];
78 static int unresolved_count;
80 /* One lock protects it all. */
81 static DEFINE_RWLOCK(aarp_lock);
83 /* Used to walk the list and purge/kick entries. */
84 static struct timer_list aarp_timer;
87 * Delete an aarp queue
89 * Must run under aarp_lock.
91 static void __aarp_expire(struct aarp_entry *a)
93 skb_queue_purge(&a->packet_queue);
94 kfree(a);
98 * Send an aarp queue entry request
100 * Must run under aarp_lock.
102 static void __aarp_send_query(struct aarp_entry *a)
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);
112 if (!skb)
113 return;
115 if (!sat) {
116 kfree_skb(skb);
117 return;
120 /* Set up the buffer */
121 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
122 skb_reset_network_header(skb);
123 skb_reset_transport_header(skb);
124 skb_put(skb, sizeof(*eah));
125 skb->protocol = htons(ETH_P_ATALK);
126 skb->dev = dev;
127 eah = aarp_hdr(skb);
129 /* Set up the ARP */
130 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
131 eah->pa_type = htons(ETH_P_ATALK);
132 eah->hw_len = ETH_ALEN;
133 eah->pa_len = AARP_PA_ALEN;
134 eah->function = htons(AARP_REQUEST);
136 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
138 eah->pa_src_zero = 0;
139 eah->pa_src_net = sat->s_net;
140 eah->pa_src_node = sat->s_node;
142 memset(eah->hw_dst, '\0', ETH_ALEN);
144 eah->pa_dst_zero = 0;
145 eah->pa_dst_net = a->target_addr.s_net;
146 eah->pa_dst_node = a->target_addr.s_node;
148 /* Send it */
149 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
150 /* Update the sending count */
151 a->xmit_count++;
152 a->last_sent = jiffies;
155 /* This runs under aarp_lock and in softint context, so only atomic memory
156 * allocations can be used. */
157 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
158 struct atalk_addr *them, unsigned char *sha)
160 struct elapaarp *eah;
161 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
162 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
164 if (!skb)
165 return;
167 /* Set up the buffer */
168 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
169 skb_reset_network_header(skb);
170 skb_reset_transport_header(skb);
171 skb_put(skb, sizeof(*eah));
172 skb->protocol = htons(ETH_P_ATALK);
173 skb->dev = dev;
174 eah = aarp_hdr(skb);
176 /* Set up the ARP */
177 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
178 eah->pa_type = htons(ETH_P_ATALK);
179 eah->hw_len = ETH_ALEN;
180 eah->pa_len = AARP_PA_ALEN;
181 eah->function = htons(AARP_REPLY);
183 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
185 eah->pa_src_zero = 0;
186 eah->pa_src_net = us->s_net;
187 eah->pa_src_node = us->s_node;
189 if (!sha)
190 memset(eah->hw_dst, '\0', ETH_ALEN);
191 else
192 memcpy(eah->hw_dst, sha, ETH_ALEN);
194 eah->pa_dst_zero = 0;
195 eah->pa_dst_net = them->s_net;
196 eah->pa_dst_node = them->s_node;
198 /* Send it */
199 aarp_dl->request(aarp_dl, skb, sha);
203 * Send probe frames. Called from aarp_probe_network and
204 * aarp_proxy_probe_network.
207 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
209 struct elapaarp *eah;
210 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
211 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
212 static unsigned char aarp_eth_multicast[ETH_ALEN] =
213 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
215 if (!skb)
216 return;
218 /* Set up the buffer */
219 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
220 skb_reset_network_header(skb);
221 skb_reset_transport_header(skb);
222 skb_put(skb, sizeof(*eah));
223 skb->protocol = htons(ETH_P_ATALK);
224 skb->dev = dev;
225 eah = aarp_hdr(skb);
227 /* Set up the ARP */
228 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
229 eah->pa_type = htons(ETH_P_ATALK);
230 eah->hw_len = ETH_ALEN;
231 eah->pa_len = AARP_PA_ALEN;
232 eah->function = htons(AARP_PROBE);
234 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
236 eah->pa_src_zero = 0;
237 eah->pa_src_net = us->s_net;
238 eah->pa_src_node = us->s_node;
240 memset(eah->hw_dst, '\0', ETH_ALEN);
242 eah->pa_dst_zero = 0;
243 eah->pa_dst_net = us->s_net;
244 eah->pa_dst_node = us->s_node;
246 /* Send it */
247 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
251 * Handle an aarp timer expire
253 * Must run under the aarp_lock.
256 static void __aarp_expire_timer(struct aarp_entry **n)
258 struct aarp_entry *t;
260 while (*n)
261 /* Expired ? */
262 if (time_after(jiffies, (*n)->expires_at)) {
263 t = *n;
264 *n = (*n)->next;
265 __aarp_expire(t);
266 } else
267 n = &((*n)->next);
271 * Kick all pending requests 5 times a second.
273 * Must run under the aarp_lock.
275 static void __aarp_kick(struct aarp_entry **n)
277 struct aarp_entry *t;
279 while (*n)
280 /* Expired: if this will be the 11th tx, we delete instead. */
281 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
282 t = *n;
283 *n = (*n)->next;
284 __aarp_expire(t);
285 } else {
286 __aarp_send_query(*n);
287 n = &((*n)->next);
292 * A device has gone down. Take all entries referring to the device
293 * and remove them.
295 * Must run under the aarp_lock.
297 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
299 struct aarp_entry *t;
301 while (*n)
302 if ((*n)->dev == dev) {
303 t = *n;
304 *n = (*n)->next;
305 __aarp_expire(t);
306 } else
307 n = &((*n)->next);
310 /* Handle the timer event */
311 static void aarp_expire_timeout(unsigned long unused)
313 int ct;
315 write_lock_bh(&aarp_lock);
317 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
318 __aarp_expire_timer(&resolved[ct]);
319 __aarp_kick(&unresolved[ct]);
320 __aarp_expire_timer(&unresolved[ct]);
321 __aarp_expire_timer(&proxies[ct]);
324 write_unlock_bh(&aarp_lock);
325 mod_timer(&aarp_timer, jiffies +
326 (unresolved_count ? sysctl_aarp_tick_time :
327 sysctl_aarp_expiry_time));
330 /* Network device notifier chain handler. */
331 static int aarp_device_event(struct notifier_block *this, unsigned long event,
332 void *ptr)
334 struct net_device *dev = ptr;
335 int ct;
337 if (!net_eq(dev_net(dev), &init_net))
338 return NOTIFY_DONE;
340 if (event == NETDEV_DOWN) {
341 write_lock_bh(&aarp_lock);
343 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
344 __aarp_expire_device(&resolved[ct], dev);
345 __aarp_expire_device(&unresolved[ct], dev);
346 __aarp_expire_device(&proxies[ct], dev);
349 write_unlock_bh(&aarp_lock);
351 return NOTIFY_DONE;
354 /* Expire all entries in a hash chain */
355 static void __aarp_expire_all(struct aarp_entry **n)
357 struct aarp_entry *t;
359 while (*n) {
360 t = *n;
361 *n = (*n)->next;
362 __aarp_expire(t);
366 /* Cleanup all hash chains -- module unloading */
367 static void aarp_purge(void)
369 int ct;
371 write_lock_bh(&aarp_lock);
372 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
373 __aarp_expire_all(&resolved[ct]);
374 __aarp_expire_all(&unresolved[ct]);
375 __aarp_expire_all(&proxies[ct]);
377 write_unlock_bh(&aarp_lock);
381 * Create a new aarp entry. This must use GFP_ATOMIC because it
382 * runs while holding spinlocks.
384 static struct aarp_entry *aarp_alloc(void)
386 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
388 if (a)
389 skb_queue_head_init(&a->packet_queue);
390 return a;
394 * Find an entry. We might return an expired but not yet purged entry. We
395 * don't care as it will do no harm.
397 * This must run under the aarp_lock.
399 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
400 struct net_device *dev,
401 struct atalk_addr *sat)
403 while (list) {
404 if (list->target_addr.s_net == sat->s_net &&
405 list->target_addr.s_node == sat->s_node &&
406 list->dev == dev)
407 break;
408 list = list->next;
411 return list;
414 /* Called from the DDP code, and thus must be exported. */
415 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
417 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
418 struct aarp_entry *a;
420 write_lock_bh(&aarp_lock);
422 a = __aarp_find_entry(proxies[hash], dev, sa);
423 if (a)
424 a->expires_at = jiffies - 1;
426 write_unlock_bh(&aarp_lock);
429 /* This must run under aarp_lock. */
430 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
431 struct atalk_addr *sa)
433 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
434 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
436 return a ? sa : NULL;
440 * Probe a Phase 1 device or a device that requires its Net:Node to
441 * be set via an ioctl.
443 static void aarp_send_probe_phase1(struct atalk_iface *iface)
445 struct ifreq atreq;
446 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
447 const struct net_device_ops *ops = iface->dev->netdev_ops;
449 sa->sat_addr.s_node = iface->address.s_node;
450 sa->sat_addr.s_net = ntohs(iface->address.s_net);
452 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
453 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
454 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
455 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
456 iface->address.s_node != sa->sat_addr.s_node)
457 iface->status |= ATIF_PROBE_FAIL;
459 iface->address.s_net = htons(sa->sat_addr.s_net);
460 iface->address.s_node = sa->sat_addr.s_node;
465 void aarp_probe_network(struct atalk_iface *atif)
467 if (atif->dev->type == ARPHRD_LOCALTLK ||
468 atif->dev->type == ARPHRD_PPP)
469 aarp_send_probe_phase1(atif);
470 else {
471 unsigned int count;
473 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
474 aarp_send_probe(atif->dev, &atif->address);
476 /* Defer 1/10th */
477 msleep(100);
479 if (atif->status & ATIF_PROBE_FAIL)
480 break;
485 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
487 int hash, retval = -EPROTONOSUPPORT;
488 struct aarp_entry *entry;
489 unsigned int count;
492 * we don't currently support LocalTalk or PPP for proxy AARP;
493 * if someone wants to try and add it, have fun
495 if (atif->dev->type == ARPHRD_LOCALTLK ||
496 atif->dev->type == ARPHRD_PPP)
497 goto out;
500 * create a new AARP entry with the flags set to be published --
501 * we need this one to hang around even if it's in use
503 entry = aarp_alloc();
504 retval = -ENOMEM;
505 if (!entry)
506 goto out;
508 entry->expires_at = -1;
509 entry->status = ATIF_PROBE;
510 entry->target_addr.s_node = sa->s_node;
511 entry->target_addr.s_net = sa->s_net;
512 entry->dev = atif->dev;
514 write_lock_bh(&aarp_lock);
516 hash = sa->s_node % (AARP_HASH_SIZE - 1);
517 entry->next = proxies[hash];
518 proxies[hash] = entry;
520 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
521 aarp_send_probe(atif->dev, sa);
523 /* Defer 1/10th */
524 write_unlock_bh(&aarp_lock);
525 msleep(100);
526 write_lock_bh(&aarp_lock);
528 if (entry->status & ATIF_PROBE_FAIL)
529 break;
532 if (entry->status & ATIF_PROBE_FAIL) {
533 entry->expires_at = jiffies - 1; /* free the entry */
534 retval = -EADDRINUSE; /* return network full */
535 } else { /* clear the probing flag */
536 entry->status &= ~ATIF_PROBE;
537 retval = 1;
540 write_unlock_bh(&aarp_lock);
541 out:
542 return retval;
545 /* Send a DDP frame */
546 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
547 struct atalk_addr *sa, void *hwaddr)
549 static char ddp_eth_multicast[ETH_ALEN] =
550 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
551 int hash;
552 struct aarp_entry *a;
554 skb_reset_network_header(skb);
556 /* Check for LocalTalk first */
557 if (dev->type == ARPHRD_LOCALTLK) {
558 struct atalk_addr *at = atalk_find_dev_addr(dev);
559 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
560 int ft = 2;
563 * Compressible ?
565 * IFF: src_net == dest_net == device_net
566 * (zero matches anything)
569 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
570 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
571 skb_pull(skb, sizeof(*ddp) - 4);
574 * The upper two remaining bytes are the port
575 * numbers we just happen to need. Now put the
576 * length in the lower two.
578 *((__be16 *)skb->data) = htons(skb->len);
579 ft = 1;
582 * Nice and easy. No AARP type protocols occur here so we can
583 * just shovel it out with a 3 byte LLAP header
586 skb_push(skb, 3);
587 skb->data[0] = sa->s_node;
588 skb->data[1] = at->s_node;
589 skb->data[2] = ft;
590 skb->dev = dev;
591 goto sendit;
594 /* On a PPP link we neither compress nor aarp. */
595 if (dev->type == ARPHRD_PPP) {
596 skb->protocol = htons(ETH_P_PPPTALK);
597 skb->dev = dev;
598 goto sendit;
601 /* Non ELAP we cannot do. */
602 if (dev->type != ARPHRD_ETHER)
603 goto free_it;
605 skb->dev = dev;
606 skb->protocol = htons(ETH_P_ATALK);
607 hash = sa->s_node % (AARP_HASH_SIZE - 1);
609 /* Do we have a resolved entry? */
610 if (sa->s_node == ATADDR_BCAST) {
611 /* Send it */
612 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
613 goto sent;
616 write_lock_bh(&aarp_lock);
617 a = __aarp_find_entry(resolved[hash], dev, sa);
619 if (a) { /* Return 1 and fill in the address */
620 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
621 ddp_dl->request(ddp_dl, skb, a->hwaddr);
622 write_unlock_bh(&aarp_lock);
623 goto sent;
626 /* Do we have an unresolved entry: This is the less common path */
627 a = __aarp_find_entry(unresolved[hash], dev, sa);
628 if (a) { /* Queue onto the unresolved queue */
629 skb_queue_tail(&a->packet_queue, skb);
630 goto out_unlock;
633 /* Allocate a new entry */
634 a = aarp_alloc();
635 if (!a) {
636 /* Whoops slipped... good job it's an unreliable protocol 8) */
637 write_unlock_bh(&aarp_lock);
638 goto free_it;
641 /* Set up the queue */
642 skb_queue_tail(&a->packet_queue, skb);
643 a->expires_at = jiffies + sysctl_aarp_resolve_time;
644 a->dev = dev;
645 a->next = unresolved[hash];
646 a->target_addr = *sa;
647 a->xmit_count = 0;
648 unresolved[hash] = a;
649 unresolved_count++;
651 /* Send an initial request for the address */
652 __aarp_send_query(a);
655 * Switch to fast timer if needed (That is if this is the first
656 * unresolved entry to get added)
659 if (unresolved_count == 1)
660 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
662 /* Now finally, it is safe to drop the lock. */
663 out_unlock:
664 write_unlock_bh(&aarp_lock);
666 /* Tell the ddp layer we have taken over for this frame. */
667 goto sent;
669 sendit:
670 if (skb->sk)
671 skb->priority = skb->sk->sk_priority;
672 if (dev_queue_xmit(skb))
673 goto drop;
674 sent:
675 return NET_XMIT_SUCCESS;
676 free_it:
677 kfree_skb(skb);
678 drop:
679 return NET_XMIT_DROP;
681 EXPORT_SYMBOL(aarp_send_ddp);
684 * An entry in the aarp unresolved queue has become resolved. Send
685 * all the frames queued under it.
687 * Must run under aarp_lock.
689 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
690 int hash)
692 struct sk_buff *skb;
694 while (*list)
695 if (*list == a) {
696 unresolved_count--;
697 *list = a->next;
699 /* Move into the resolved list */
700 a->next = resolved[hash];
701 resolved[hash] = a;
703 /* Kick frames off */
704 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
705 a->expires_at = jiffies +
706 sysctl_aarp_expiry_time * 10;
707 ddp_dl->request(ddp_dl, skb, a->hwaddr);
709 } else
710 list = &((*list)->next);
714 * This is called by the SNAP driver whenever we see an AARP SNAP
715 * frame. We currently only support Ethernet.
717 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
718 struct packet_type *pt, struct net_device *orig_dev)
720 struct elapaarp *ea = aarp_hdr(skb);
721 int hash, ret = 0;
722 __u16 function;
723 struct aarp_entry *a;
724 struct atalk_addr sa, *ma, da;
725 struct atalk_iface *ifa;
727 if (!net_eq(dev_net(dev), &init_net))
728 goto out0;
730 /* We only do Ethernet SNAP AARP. */
731 if (dev->type != ARPHRD_ETHER)
732 goto out0;
734 /* Frame size ok? */
735 if (!skb_pull(skb, sizeof(*ea)))
736 goto out0;
738 function = ntohs(ea->function);
740 /* Sanity check fields. */
741 if (function < AARP_REQUEST || function > AARP_PROBE ||
742 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
743 ea->pa_src_zero || ea->pa_dst_zero)
744 goto out0;
746 /* Looks good. */
747 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
749 /* Build an address. */
750 sa.s_node = ea->pa_src_node;
751 sa.s_net = ea->pa_src_net;
753 /* Process the packet. Check for replies of me. */
754 ifa = atalk_find_dev(dev);
755 if (!ifa)
756 goto out1;
758 if (ifa->status & ATIF_PROBE &&
759 ifa->address.s_node == ea->pa_dst_node &&
760 ifa->address.s_net == ea->pa_dst_net) {
761 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
762 goto out1;
765 /* Check for replies of proxy AARP entries */
766 da.s_node = ea->pa_dst_node;
767 da.s_net = ea->pa_dst_net;
769 write_lock_bh(&aarp_lock);
770 a = __aarp_find_entry(proxies[hash], dev, &da);
772 if (a && a->status & ATIF_PROBE) {
773 a->status |= ATIF_PROBE_FAIL;
775 * we do not respond to probe or request packets for
776 * this address while we are probing this address
778 goto unlock;
781 switch (function) {
782 case AARP_REPLY:
783 if (!unresolved_count) /* Speed up */
784 break;
786 /* Find the entry. */
787 a = __aarp_find_entry(unresolved[hash], dev, &sa);
788 if (!a || dev != a->dev)
789 break;
791 /* We can fill one in - this is good. */
792 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
793 __aarp_resolved(&unresolved[hash], a, hash);
794 if (!unresolved_count)
795 mod_timer(&aarp_timer,
796 jiffies + sysctl_aarp_expiry_time);
797 break;
799 case AARP_REQUEST:
800 case AARP_PROBE:
803 * If it is my address set ma to my address and reply.
804 * We can treat probe and request the same. Probe
805 * simply means we shouldn't cache the querying host,
806 * as in a probe they are proposing an address not
807 * using one.
809 * Support for proxy-AARP added. We check if the
810 * address is one of our proxies before we toss the
811 * packet out.
814 sa.s_node = ea->pa_dst_node;
815 sa.s_net = ea->pa_dst_net;
817 /* See if we have a matching proxy. */
818 ma = __aarp_proxy_find(dev, &sa);
819 if (!ma)
820 ma = &ifa->address;
821 else { /* We need to make a copy of the entry. */
822 da.s_node = sa.s_node;
823 da.s_net = sa.s_net;
824 ma = &da;
827 if (function == AARP_PROBE) {
829 * A probe implies someone trying to get an
830 * address. So as a precaution flush any
831 * entries we have for this address.
833 a = __aarp_find_entry(resolved[sa.s_node %
834 (AARP_HASH_SIZE - 1)],
835 skb->dev, &sa);
838 * Make it expire next tick - that avoids us
839 * getting into a probe/flush/learn/probe/
840 * flush/learn cycle during probing of a slow
841 * to respond host addr.
843 if (a) {
844 a->expires_at = jiffies - 1;
845 mod_timer(&aarp_timer, jiffies +
846 sysctl_aarp_tick_time);
850 if (sa.s_node != ma->s_node)
851 break;
853 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
854 break;
856 sa.s_node = ea->pa_src_node;
857 sa.s_net = ea->pa_src_net;
859 /* aarp_my_address has found the address to use for us.
861 aarp_send_reply(dev, ma, &sa, ea->hw_src);
862 break;
865 unlock:
866 write_unlock_bh(&aarp_lock);
867 out1:
868 ret = 1;
869 out0:
870 kfree_skb(skb);
871 return ret;
874 static struct notifier_block aarp_notifier = {
875 .notifier_call = aarp_device_event,
878 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
880 void __init aarp_proto_init(void)
882 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
883 if (!aarp_dl)
884 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
885 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
886 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
887 add_timer(&aarp_timer);
888 register_netdevice_notifier(&aarp_notifier);
891 /* Remove the AARP entries associated with a device. */
892 void aarp_device_down(struct net_device *dev)
894 int ct;
896 write_lock_bh(&aarp_lock);
898 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
899 __aarp_expire_device(&resolved[ct], dev);
900 __aarp_expire_device(&unresolved[ct], dev);
901 __aarp_expire_device(&proxies[ct], dev);
904 write_unlock_bh(&aarp_lock);
907 #ifdef CONFIG_PROC_FS
908 struct aarp_iter_state {
909 int bucket;
910 struct aarp_entry **table;
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 static 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,
1041 static int aarp_seq_open(struct inode *inode, struct file *file)
1043 return seq_open_private(file, &aarp_seq_ops,
1044 sizeof(struct aarp_iter_state));
1047 const struct file_operations atalk_seq_arp_fops = {
1048 .owner = THIS_MODULE,
1049 .open = aarp_seq_open,
1050 .read = seq_read,
1051 .llseek = seq_lseek,
1052 .release = seq_release_private,
1054 #endif
1056 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1057 void aarp_cleanup_module(void)
1059 del_timer_sync(&aarp_timer);
1060 unregister_netdevice_notifier(&aarp_notifier);
1061 unregister_snap_client(aarp_dl);
1062 aarp_purge();