Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6/linux-2.6-openrd.git] / net / appletalk / aarp.c
blobf2b3b56aa779305623fc4c919bae37a7a24c0b41
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 <net/sock.h>
34 #include <net/datalink.h>
35 #include <net/psnap.h>
36 #include <linux/atalk.h>
37 #include <linux/delay.h>
38 #include <linux/init.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
42 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
43 int sysctl_aarp_tick_time = AARP_TICK_TIME;
44 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
45 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
47 /* Lists of aarp entries */
48 /**
49 * struct aarp_entry - AARP entry
50 * @last_sent - Last time we xmitted the aarp request
51 * @packet_queue - Queue of frames wait for resolution
52 * @status - Used for proxy AARP
53 * expires_at - Entry expiry time
54 * target_addr - DDP Address
55 * dev - Device to use
56 * hwaddr - Physical i/f address of target/router
57 * xmit_count - When this hits 10 we give up
58 * next - Next entry in chain
60 struct aarp_entry {
61 /* These first two are only used for unresolved entries */
62 unsigned long last_sent;
63 struct sk_buff_head packet_queue;
64 int status;
65 unsigned long expires_at;
66 struct atalk_addr target_addr;
67 struct net_device *dev;
68 char hwaddr[6];
69 unsigned short xmit_count;
70 struct aarp_entry *next;
73 /* Hashed list of resolved, unresolved and proxy entries */
74 static struct aarp_entry *resolved[AARP_HASH_SIZE];
75 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
76 static struct aarp_entry *proxies[AARP_HASH_SIZE];
77 static int unresolved_count;
79 /* One lock protects it all. */
80 static DEFINE_RWLOCK(aarp_lock);
82 /* Used to walk the list and purge/kick entries. */
83 static struct timer_list aarp_timer;
86 * Delete an aarp queue
88 * Must run under aarp_lock.
90 static void __aarp_expire(struct aarp_entry *a)
92 skb_queue_purge(&a->packet_queue);
93 kfree(a);
97 * Send an aarp queue entry request
99 * Must run under aarp_lock.
101 static void __aarp_send_query(struct aarp_entry *a)
103 static unsigned char aarp_eth_multicast[ETH_ALEN] =
104 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
105 struct net_device *dev = a->dev;
106 struct elapaarp *eah;
107 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
108 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
109 struct atalk_addr *sat = atalk_find_dev_addr(dev);
111 if (!skb)
112 return;
114 if (!sat) {
115 kfree_skb(skb);
116 return;
119 /* Set up the buffer */
120 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
121 skb_reset_network_header(skb);
122 skb_reset_transport_header(skb);
123 skb_put(skb, sizeof(*eah));
124 skb->protocol = htons(ETH_P_ATALK);
125 skb->dev = dev;
126 eah = aarp_hdr(skb);
128 /* Set up the ARP */
129 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
130 eah->pa_type = htons(ETH_P_ATALK);
131 eah->hw_len = ETH_ALEN;
132 eah->pa_len = AARP_PA_ALEN;
133 eah->function = htons(AARP_REQUEST);
135 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
137 eah->pa_src_zero = 0;
138 eah->pa_src_net = sat->s_net;
139 eah->pa_src_node = sat->s_node;
141 memset(eah->hw_dst, '\0', ETH_ALEN);
143 eah->pa_dst_zero = 0;
144 eah->pa_dst_net = a->target_addr.s_net;
145 eah->pa_dst_node = a->target_addr.s_node;
147 /* Send it */
148 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
149 /* Update the sending count */
150 a->xmit_count++;
151 a->last_sent = jiffies;
154 /* This runs under aarp_lock and in softint context, so only atomic memory
155 * allocations can be used. */
156 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
157 struct atalk_addr *them, unsigned char *sha)
159 struct elapaarp *eah;
160 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
161 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
163 if (!skb)
164 return;
166 /* Set up the buffer */
167 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
168 skb_reset_network_header(skb);
169 skb_reset_transport_header(skb);
170 skb_put(skb, sizeof(*eah));
171 skb->protocol = htons(ETH_P_ATALK);
172 skb->dev = dev;
173 eah = aarp_hdr(skb);
175 /* Set up the ARP */
176 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
177 eah->pa_type = htons(ETH_P_ATALK);
178 eah->hw_len = ETH_ALEN;
179 eah->pa_len = AARP_PA_ALEN;
180 eah->function = htons(AARP_REPLY);
182 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
184 eah->pa_src_zero = 0;
185 eah->pa_src_net = us->s_net;
186 eah->pa_src_node = us->s_node;
188 if (!sha)
189 memset(eah->hw_dst, '\0', ETH_ALEN);
190 else
191 memcpy(eah->hw_dst, sha, ETH_ALEN);
193 eah->pa_dst_zero = 0;
194 eah->pa_dst_net = them->s_net;
195 eah->pa_dst_node = them->s_node;
197 /* Send it */
198 aarp_dl->request(aarp_dl, skb, sha);
202 * Send probe frames. Called from aarp_probe_network and
203 * aarp_proxy_probe_network.
206 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
208 struct elapaarp *eah;
209 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
210 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
211 static unsigned char aarp_eth_multicast[ETH_ALEN] =
212 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
214 if (!skb)
215 return;
217 /* Set up the buffer */
218 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
219 skb_reset_network_header(skb);
220 skb_reset_transport_header(skb);
221 skb_put(skb, sizeof(*eah));
222 skb->protocol = htons(ETH_P_ATALK);
223 skb->dev = dev;
224 eah = aarp_hdr(skb);
226 /* Set up the ARP */
227 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
228 eah->pa_type = htons(ETH_P_ATALK);
229 eah->hw_len = ETH_ALEN;
230 eah->pa_len = AARP_PA_ALEN;
231 eah->function = htons(AARP_PROBE);
233 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
235 eah->pa_src_zero = 0;
236 eah->pa_src_net = us->s_net;
237 eah->pa_src_node = us->s_node;
239 memset(eah->hw_dst, '\0', ETH_ALEN);
241 eah->pa_dst_zero = 0;
242 eah->pa_dst_net = us->s_net;
243 eah->pa_dst_node = us->s_node;
245 /* Send it */
246 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
250 * Handle an aarp timer expire
252 * Must run under the aarp_lock.
255 static void __aarp_expire_timer(struct aarp_entry **n)
257 struct aarp_entry *t;
259 while (*n)
260 /* Expired ? */
261 if (time_after(jiffies, (*n)->expires_at)) {
262 t = *n;
263 *n = (*n)->next;
264 __aarp_expire(t);
265 } else
266 n = &((*n)->next);
270 * Kick all pending requests 5 times a second.
272 * Must run under the aarp_lock.
274 static void __aarp_kick(struct aarp_entry **n)
276 struct aarp_entry *t;
278 while (*n)
279 /* Expired: if this will be the 11th tx, we delete instead. */
280 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
281 t = *n;
282 *n = (*n)->next;
283 __aarp_expire(t);
284 } else {
285 __aarp_send_query(*n);
286 n = &((*n)->next);
291 * A device has gone down. Take all entries referring to the device
292 * and remove them.
294 * Must run under the aarp_lock.
296 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
298 struct aarp_entry *t;
300 while (*n)
301 if ((*n)->dev == dev) {
302 t = *n;
303 *n = (*n)->next;
304 __aarp_expire(t);
305 } else
306 n = &((*n)->next);
309 /* Handle the timer event */
310 static void aarp_expire_timeout(unsigned long unused)
312 int ct;
314 write_lock_bh(&aarp_lock);
316 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
317 __aarp_expire_timer(&resolved[ct]);
318 __aarp_kick(&unresolved[ct]);
319 __aarp_expire_timer(&unresolved[ct]);
320 __aarp_expire_timer(&proxies[ct]);
323 write_unlock_bh(&aarp_lock);
324 mod_timer(&aarp_timer, jiffies +
325 (unresolved_count ? sysctl_aarp_tick_time :
326 sysctl_aarp_expiry_time));
329 /* Network device notifier chain handler. */
330 static int aarp_device_event(struct notifier_block *this, unsigned long event,
331 void *ptr)
333 struct net_device *dev = ptr;
334 int ct;
336 if (!net_eq(dev_net(dev), &init_net))
337 return NOTIFY_DONE;
339 if (event == NETDEV_DOWN) {
340 write_lock_bh(&aarp_lock);
342 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
343 __aarp_expire_device(&resolved[ct], dev);
344 __aarp_expire_device(&unresolved[ct], dev);
345 __aarp_expire_device(&proxies[ct], dev);
348 write_unlock_bh(&aarp_lock);
350 return NOTIFY_DONE;
353 /* Expire all entries in a hash chain */
354 static void __aarp_expire_all(struct aarp_entry **n)
356 struct aarp_entry *t;
358 while (*n) {
359 t = *n;
360 *n = (*n)->next;
361 __aarp_expire(t);
365 /* Cleanup all hash chains -- module unloading */
366 static void aarp_purge(void)
368 int ct;
370 write_lock_bh(&aarp_lock);
371 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
372 __aarp_expire_all(&resolved[ct]);
373 __aarp_expire_all(&unresolved[ct]);
374 __aarp_expire_all(&proxies[ct]);
376 write_unlock_bh(&aarp_lock);
380 * Create a new aarp entry. This must use GFP_ATOMIC because it
381 * runs while holding spinlocks.
383 static struct aarp_entry *aarp_alloc(void)
385 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
387 if (a)
388 skb_queue_head_init(&a->packet_queue);
389 return a;
393 * Find an entry. We might return an expired but not yet purged entry. We
394 * don't care as it will do no harm.
396 * This must run under the aarp_lock.
398 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
399 struct net_device *dev,
400 struct atalk_addr *sat)
402 while (list) {
403 if (list->target_addr.s_net == sat->s_net &&
404 list->target_addr.s_node == sat->s_node &&
405 list->dev == dev)
406 break;
407 list = list->next;
410 return list;
413 /* Called from the DDP code, and thus must be exported. */
414 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
416 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
417 struct aarp_entry *a;
419 write_lock_bh(&aarp_lock);
421 a = __aarp_find_entry(proxies[hash], dev, sa);
422 if (a)
423 a->expires_at = jiffies - 1;
425 write_unlock_bh(&aarp_lock);
428 /* This must run under aarp_lock. */
429 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
430 struct atalk_addr *sa)
432 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
433 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
435 return a ? sa : NULL;
439 * Probe a Phase 1 device or a device that requires its Net:Node to
440 * be set via an ioctl.
442 static void aarp_send_probe_phase1(struct atalk_iface *iface)
444 struct ifreq atreq;
445 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
446 const struct net_device_ops *ops = iface->dev->netdev_ops;
448 sa->sat_addr.s_node = iface->address.s_node;
449 sa->sat_addr.s_net = ntohs(iface->address.s_net);
451 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
452 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
453 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
454 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
455 iface->address.s_node != sa->sat_addr.s_node)
456 iface->status |= ATIF_PROBE_FAIL;
458 iface->address.s_net = htons(sa->sat_addr.s_net);
459 iface->address.s_node = sa->sat_addr.s_node;
464 void aarp_probe_network(struct atalk_iface *atif)
466 if (atif->dev->type == ARPHRD_LOCALTLK ||
467 atif->dev->type == ARPHRD_PPP)
468 aarp_send_probe_phase1(atif);
469 else {
470 unsigned int count;
472 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
473 aarp_send_probe(atif->dev, &atif->address);
475 /* Defer 1/10th */
476 msleep(100);
478 if (atif->status & ATIF_PROBE_FAIL)
479 break;
484 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
486 int hash, retval = -EPROTONOSUPPORT;
487 struct aarp_entry *entry;
488 unsigned int count;
491 * we don't currently support LocalTalk or PPP for proxy AARP;
492 * if someone wants to try and add it, have fun
494 if (atif->dev->type == ARPHRD_LOCALTLK ||
495 atif->dev->type == ARPHRD_PPP)
496 goto out;
499 * create a new AARP entry with the flags set to be published --
500 * we need this one to hang around even if it's in use
502 entry = aarp_alloc();
503 retval = -ENOMEM;
504 if (!entry)
505 goto out;
507 entry->expires_at = -1;
508 entry->status = ATIF_PROBE;
509 entry->target_addr.s_node = sa->s_node;
510 entry->target_addr.s_net = sa->s_net;
511 entry->dev = atif->dev;
513 write_lock_bh(&aarp_lock);
515 hash = sa->s_node % (AARP_HASH_SIZE - 1);
516 entry->next = proxies[hash];
517 proxies[hash] = entry;
519 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
520 aarp_send_probe(atif->dev, sa);
522 /* Defer 1/10th */
523 write_unlock_bh(&aarp_lock);
524 msleep(100);
525 write_lock_bh(&aarp_lock);
527 if (entry->status & ATIF_PROBE_FAIL)
528 break;
531 if (entry->status & ATIF_PROBE_FAIL) {
532 entry->expires_at = jiffies - 1; /* free the entry */
533 retval = -EADDRINUSE; /* return network full */
534 } else { /* clear the probing flag */
535 entry->status &= ~ATIF_PROBE;
536 retval = 1;
539 write_unlock_bh(&aarp_lock);
540 out:
541 return retval;
544 /* Send a DDP frame */
545 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
546 struct atalk_addr *sa, void *hwaddr)
548 static char ddp_eth_multicast[ETH_ALEN] =
549 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
550 int hash;
551 struct aarp_entry *a;
553 skb_reset_network_header(skb);
555 /* Check for LocalTalk first */
556 if (dev->type == ARPHRD_LOCALTLK) {
557 struct atalk_addr *at = atalk_find_dev_addr(dev);
558 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
559 int ft = 2;
562 * Compressible ?
564 * IFF: src_net == dest_net == device_net
565 * (zero matches anything)
568 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
569 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
570 skb_pull(skb, sizeof(*ddp) - 4);
573 * The upper two remaining bytes are the port
574 * numbers we just happen to need. Now put the
575 * length in the lower two.
577 *((__be16 *)skb->data) = htons(skb->len);
578 ft = 1;
581 * Nice and easy. No AARP type protocols occur here so we can
582 * just shovel it out with a 3 byte LLAP header
585 skb_push(skb, 3);
586 skb->data[0] = sa->s_node;
587 skb->data[1] = at->s_node;
588 skb->data[2] = ft;
589 skb->dev = dev;
590 goto sendit;
593 /* On a PPP link we neither compress nor aarp. */
594 if (dev->type == ARPHRD_PPP) {
595 skb->protocol = htons(ETH_P_PPPTALK);
596 skb->dev = dev;
597 goto sendit;
600 /* Non ELAP we cannot do. */
601 if (dev->type != ARPHRD_ETHER)
602 goto free_it;
604 skb->dev = dev;
605 skb->protocol = htons(ETH_P_ATALK);
606 hash = sa->s_node % (AARP_HASH_SIZE - 1);
608 /* Do we have a resolved entry? */
609 if (sa->s_node == ATADDR_BCAST) {
610 /* Send it */
611 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
612 goto sent;
615 write_lock_bh(&aarp_lock);
616 a = __aarp_find_entry(resolved[hash], dev, sa);
618 if (a) { /* Return 1 and fill in the address */
619 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
620 ddp_dl->request(ddp_dl, skb, a->hwaddr);
621 write_unlock_bh(&aarp_lock);
622 goto sent;
625 /* Do we have an unresolved entry: This is the less common path */
626 a = __aarp_find_entry(unresolved[hash], dev, sa);
627 if (a) { /* Queue onto the unresolved queue */
628 skb_queue_tail(&a->packet_queue, skb);
629 goto out_unlock;
632 /* Allocate a new entry */
633 a = aarp_alloc();
634 if (!a) {
635 /* Whoops slipped... good job it's an unreliable protocol 8) */
636 write_unlock_bh(&aarp_lock);
637 goto free_it;
640 /* Set up the queue */
641 skb_queue_tail(&a->packet_queue, skb);
642 a->expires_at = jiffies + sysctl_aarp_resolve_time;
643 a->dev = dev;
644 a->next = unresolved[hash];
645 a->target_addr = *sa;
646 a->xmit_count = 0;
647 unresolved[hash] = a;
648 unresolved_count++;
650 /* Send an initial request for the address */
651 __aarp_send_query(a);
654 * Switch to fast timer if needed (That is if this is the first
655 * unresolved entry to get added)
658 if (unresolved_count == 1)
659 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
661 /* Now finally, it is safe to drop the lock. */
662 out_unlock:
663 write_unlock_bh(&aarp_lock);
665 /* Tell the ddp layer we have taken over for this frame. */
666 goto sent;
668 sendit:
669 if (skb->sk)
670 skb->priority = skb->sk->sk_priority;
671 if (dev_queue_xmit(skb))
672 goto drop;
673 sent:
674 return NET_XMIT_SUCCESS;
675 free_it:
676 kfree_skb(skb);
677 drop:
678 return NET_XMIT_DROP;
680 EXPORT_SYMBOL(aarp_send_ddp);
683 * An entry in the aarp unresolved queue has become resolved. Send
684 * all the frames queued under it.
686 * Must run under aarp_lock.
688 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
689 int hash)
691 struct sk_buff *skb;
693 while (*list)
694 if (*list == a) {
695 unresolved_count--;
696 *list = a->next;
698 /* Move into the resolved list */
699 a->next = resolved[hash];
700 resolved[hash] = a;
702 /* Kick frames off */
703 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
704 a->expires_at = jiffies +
705 sysctl_aarp_expiry_time * 10;
706 ddp_dl->request(ddp_dl, skb, a->hwaddr);
708 } else
709 list = &((*list)->next);
713 * This is called by the SNAP driver whenever we see an AARP SNAP
714 * frame. We currently only support Ethernet.
716 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
717 struct packet_type *pt, struct net_device *orig_dev)
719 struct elapaarp *ea = aarp_hdr(skb);
720 int hash, ret = 0;
721 __u16 function;
722 struct aarp_entry *a;
723 struct atalk_addr sa, *ma, da;
724 struct atalk_iface *ifa;
726 if (!net_eq(dev_net(dev), &init_net))
727 goto out0;
729 /* We only do Ethernet SNAP AARP. */
730 if (dev->type != ARPHRD_ETHER)
731 goto out0;
733 /* Frame size ok? */
734 if (!skb_pull(skb, sizeof(*ea)))
735 goto out0;
737 function = ntohs(ea->function);
739 /* Sanity check fields. */
740 if (function < AARP_REQUEST || function > AARP_PROBE ||
741 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
742 ea->pa_src_zero || ea->pa_dst_zero)
743 goto out0;
745 /* Looks good. */
746 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
748 /* Build an address. */
749 sa.s_node = ea->pa_src_node;
750 sa.s_net = ea->pa_src_net;
752 /* Process the packet. Check for replies of me. */
753 ifa = atalk_find_dev(dev);
754 if (!ifa)
755 goto out1;
757 if (ifa->status & ATIF_PROBE &&
758 ifa->address.s_node == ea->pa_dst_node &&
759 ifa->address.s_net == ea->pa_dst_net) {
760 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
761 goto out1;
764 /* Check for replies of proxy AARP entries */
765 da.s_node = ea->pa_dst_node;
766 da.s_net = ea->pa_dst_net;
768 write_lock_bh(&aarp_lock);
769 a = __aarp_find_entry(proxies[hash], dev, &da);
771 if (a && a->status & ATIF_PROBE) {
772 a->status |= ATIF_PROBE_FAIL;
774 * we do not respond to probe or request packets for
775 * this address while we are probing this address
777 goto unlock;
780 switch (function) {
781 case AARP_REPLY:
782 if (!unresolved_count) /* Speed up */
783 break;
785 /* Find the entry. */
786 a = __aarp_find_entry(unresolved[hash], dev, &sa);
787 if (!a || dev != a->dev)
788 break;
790 /* We can fill one in - this is good. */
791 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
792 __aarp_resolved(&unresolved[hash], a, hash);
793 if (!unresolved_count)
794 mod_timer(&aarp_timer,
795 jiffies + sysctl_aarp_expiry_time);
796 break;
798 case AARP_REQUEST:
799 case AARP_PROBE:
802 * If it is my address set ma to my address and reply.
803 * We can treat probe and request the same. Probe
804 * simply means we shouldn't cache the querying host,
805 * as in a probe they are proposing an address not
806 * using one.
808 * Support for proxy-AARP added. We check if the
809 * address is one of our proxies before we toss the
810 * packet out.
813 sa.s_node = ea->pa_dst_node;
814 sa.s_net = ea->pa_dst_net;
816 /* See if we have a matching proxy. */
817 ma = __aarp_proxy_find(dev, &sa);
818 if (!ma)
819 ma = &ifa->address;
820 else { /* We need to make a copy of the entry. */
821 da.s_node = sa.s_node;
822 da.s_net = sa.s_net;
823 ma = &da;
826 if (function == AARP_PROBE) {
828 * A probe implies someone trying to get an
829 * address. So as a precaution flush any
830 * entries we have for this address.
832 a = __aarp_find_entry(resolved[sa.s_node %
833 (AARP_HASH_SIZE - 1)],
834 skb->dev, &sa);
837 * Make it expire next tick - that avoids us
838 * getting into a probe/flush/learn/probe/
839 * flush/learn cycle during probing of a slow
840 * to respond host addr.
842 if (a) {
843 a->expires_at = jiffies - 1;
844 mod_timer(&aarp_timer, jiffies +
845 sysctl_aarp_tick_time);
849 if (sa.s_node != ma->s_node)
850 break;
852 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
853 break;
855 sa.s_node = ea->pa_src_node;
856 sa.s_net = ea->pa_src_net;
858 /* aarp_my_address has found the address to use for us.
860 aarp_send_reply(dev, ma, &sa, ea->hw_src);
861 break;
864 unlock:
865 write_unlock_bh(&aarp_lock);
866 out1:
867 ret = 1;
868 out0:
869 kfree_skb(skb);
870 return ret;
873 static struct notifier_block aarp_notifier = {
874 .notifier_call = aarp_device_event,
877 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
879 void __init aarp_proto_init(void)
881 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
882 if (!aarp_dl)
883 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
884 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
885 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
886 add_timer(&aarp_timer);
887 register_netdevice_notifier(&aarp_notifier);
890 /* Remove the AARP entries associated with a device. */
891 void aarp_device_down(struct net_device *dev)
893 int ct;
895 write_lock_bh(&aarp_lock);
897 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
898 __aarp_expire_device(&resolved[ct], dev);
899 __aarp_expire_device(&unresolved[ct], dev);
900 __aarp_expire_device(&proxies[ct], dev);
903 write_unlock_bh(&aarp_lock);
906 #ifdef CONFIG_PROC_FS
907 struct aarp_iter_state {
908 int bucket;
909 struct aarp_entry **table;
913 * Get the aarp entry that is in the chain described
914 * by the iterator.
915 * If pos is set then skip till that index.
916 * pos = 1 is the first entry
918 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
920 int ct = iter->bucket;
921 struct aarp_entry **table = iter->table;
922 loff_t off = 0;
923 struct aarp_entry *entry;
925 rescan:
926 while(ct < AARP_HASH_SIZE) {
927 for (entry = table[ct]; entry; entry = entry->next) {
928 if (!pos || ++off == *pos) {
929 iter->table = table;
930 iter->bucket = ct;
931 return entry;
934 ++ct;
937 if (table == resolved) {
938 ct = 0;
939 table = unresolved;
940 goto rescan;
942 if (table == unresolved) {
943 ct = 0;
944 table = proxies;
945 goto rescan;
947 return NULL;
950 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
951 __acquires(aarp_lock)
953 struct aarp_iter_state *iter = seq->private;
955 read_lock_bh(&aarp_lock);
956 iter->table = resolved;
957 iter->bucket = 0;
959 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
962 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
964 struct aarp_entry *entry = v;
965 struct aarp_iter_state *iter = seq->private;
967 ++*pos;
969 /* first line after header */
970 if (v == SEQ_START_TOKEN)
971 entry = iter_next(iter, NULL);
973 /* next entry in current bucket */
974 else if (entry->next)
975 entry = entry->next;
977 /* next bucket or table */
978 else {
979 ++iter->bucket;
980 entry = iter_next(iter, NULL);
982 return entry;
985 static void aarp_seq_stop(struct seq_file *seq, void *v)
986 __releases(aarp_lock)
988 read_unlock_bh(&aarp_lock);
991 static const char *dt2str(unsigned long ticks)
993 static char buf[32];
995 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
997 return buf;
1000 static int aarp_seq_show(struct seq_file *seq, void *v)
1002 struct aarp_iter_state *iter = seq->private;
1003 struct aarp_entry *entry = v;
1004 unsigned long now = jiffies;
1006 if (v == SEQ_START_TOKEN)
1007 seq_puts(seq,
1008 "Address Interface Hardware Address"
1009 " Expires LastSend Retry Status\n");
1010 else {
1011 seq_printf(seq, "%04X:%02X %-12s",
1012 ntohs(entry->target_addr.s_net),
1013 (unsigned int) entry->target_addr.s_node,
1014 entry->dev ? entry->dev->name : "????");
1015 seq_printf(seq, "%pM", entry->hwaddr);
1016 seq_printf(seq, " %8s",
1017 dt2str((long)entry->expires_at - (long)now));
1018 if (iter->table == unresolved)
1019 seq_printf(seq, " %8s %6hu",
1020 dt2str(now - entry->last_sent),
1021 entry->xmit_count);
1022 else
1023 seq_puts(seq, " ");
1024 seq_printf(seq, " %s\n",
1025 (iter->table == resolved) ? "resolved"
1026 : (iter->table == unresolved) ? "unresolved"
1027 : (iter->table == proxies) ? "proxies"
1028 : "unknown");
1030 return 0;
1033 static const struct seq_operations aarp_seq_ops = {
1034 .start = aarp_seq_start,
1035 .next = aarp_seq_next,
1036 .stop = aarp_seq_stop,
1037 .show = aarp_seq_show,
1040 static int aarp_seq_open(struct inode *inode, struct file *file)
1042 return seq_open_private(file, &aarp_seq_ops,
1043 sizeof(struct aarp_iter_state));
1046 const struct file_operations atalk_seq_arp_fops = {
1047 .owner = THIS_MODULE,
1048 .open = aarp_seq_open,
1049 .read = seq_read,
1050 .llseek = seq_lseek,
1051 .release = seq_release_private,
1053 #endif
1055 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1056 void aarp_cleanup_module(void)
1058 del_timer_sync(&aarp_timer);
1059 unregister_netdevice_notifier(&aarp_notifier);
1060 unregister_snap_client(aarp_dl);
1061 aarp_purge();