Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / appletalk / aarp.c
blob54640c01b50cddcbf3f42321361c08b9ebe911c4
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/config.h>
33 #include <linux/if_arp.h>
34 #include <net/sock.h>
35 #include <net/datalink.h>
36 #include <net/psnap.h>
37 #include <linux/atalk.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->nh.raw = skb->h.raw = skb_put(skb, sizeof(*eah));
122 skb->protocol = htons(ETH_P_ATALK);
123 skb->dev = dev;
124 eah = aarp_hdr(skb);
126 /* Set up the ARP */
127 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
128 eah->pa_type = htons(ETH_P_ATALK);
129 eah->hw_len = ETH_ALEN;
130 eah->pa_len = AARP_PA_ALEN;
131 eah->function = htons(AARP_REQUEST);
133 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
135 eah->pa_src_zero = 0;
136 eah->pa_src_net = sat->s_net;
137 eah->pa_src_node = sat->s_node;
139 memset(eah->hw_dst, '\0', ETH_ALEN);
141 eah->pa_dst_zero = 0;
142 eah->pa_dst_net = a->target_addr.s_net;
143 eah->pa_dst_node = a->target_addr.s_node;
145 /* Send it */
146 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
147 /* Update the sending count */
148 a->xmit_count++;
149 a->last_sent = jiffies;
152 /* This runs under aarp_lock and in softint context, so only atomic memory
153 * allocations can be used. */
154 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
155 struct atalk_addr *them, unsigned char *sha)
157 struct elapaarp *eah;
158 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
159 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
161 if (!skb)
162 return;
164 /* Set up the buffer */
165 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
166 skb->nh.raw = skb->h.raw = skb_put(skb, sizeof(*eah));
167 skb->protocol = htons(ETH_P_ATALK);
168 skb->dev = dev;
169 eah = aarp_hdr(skb);
171 /* Set up the ARP */
172 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
173 eah->pa_type = htons(ETH_P_ATALK);
174 eah->hw_len = ETH_ALEN;
175 eah->pa_len = AARP_PA_ALEN;
176 eah->function = htons(AARP_REPLY);
178 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
180 eah->pa_src_zero = 0;
181 eah->pa_src_net = us->s_net;
182 eah->pa_src_node = us->s_node;
184 if (!sha)
185 memset(eah->hw_dst, '\0', ETH_ALEN);
186 else
187 memcpy(eah->hw_dst, sha, ETH_ALEN);
189 eah->pa_dst_zero = 0;
190 eah->pa_dst_net = them->s_net;
191 eah->pa_dst_node = them->s_node;
193 /* Send it */
194 aarp_dl->request(aarp_dl, skb, sha);
198 * Send probe frames. Called from aarp_probe_network and
199 * aarp_proxy_probe_network.
202 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
204 struct elapaarp *eah;
205 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
206 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
207 static unsigned char aarp_eth_multicast[ETH_ALEN] =
208 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
210 if (!skb)
211 return;
213 /* Set up the buffer */
214 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
215 skb->nh.raw = skb->h.raw = skb_put(skb, sizeof(*eah));
216 skb->protocol = htons(ETH_P_ATALK);
217 skb->dev = dev;
218 eah = aarp_hdr(skb);
220 /* Set up the ARP */
221 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
222 eah->pa_type = htons(ETH_P_ATALK);
223 eah->hw_len = ETH_ALEN;
224 eah->pa_len = AARP_PA_ALEN;
225 eah->function = htons(AARP_PROBE);
227 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
229 eah->pa_src_zero = 0;
230 eah->pa_src_net = us->s_net;
231 eah->pa_src_node = us->s_node;
233 memset(eah->hw_dst, '\0', ETH_ALEN);
235 eah->pa_dst_zero = 0;
236 eah->pa_dst_net = us->s_net;
237 eah->pa_dst_node = us->s_node;
239 /* Send it */
240 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
244 * Handle an aarp timer expire
246 * Must run under the aarp_lock.
249 static void __aarp_expire_timer(struct aarp_entry **n)
251 struct aarp_entry *t;
253 while (*n)
254 /* Expired ? */
255 if (time_after(jiffies, (*n)->expires_at)) {
256 t = *n;
257 *n = (*n)->next;
258 __aarp_expire(t);
259 } else
260 n = &((*n)->next);
264 * Kick all pending requests 5 times a second.
266 * Must run under the aarp_lock.
268 static void __aarp_kick(struct aarp_entry **n)
270 struct aarp_entry *t;
272 while (*n)
273 /* Expired: if this will be the 11th tx, we delete instead. */
274 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
275 t = *n;
276 *n = (*n)->next;
277 __aarp_expire(t);
278 } else {
279 __aarp_send_query(*n);
280 n = &((*n)->next);
285 * A device has gone down. Take all entries referring to the device
286 * and remove them.
288 * Must run under the aarp_lock.
290 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
292 struct aarp_entry *t;
294 while (*n)
295 if ((*n)->dev == dev) {
296 t = *n;
297 *n = (*n)->next;
298 __aarp_expire(t);
299 } else
300 n = &((*n)->next);
303 /* Handle the timer event */
304 static void aarp_expire_timeout(unsigned long unused)
306 int ct;
308 write_lock_bh(&aarp_lock);
310 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
311 __aarp_expire_timer(&resolved[ct]);
312 __aarp_kick(&unresolved[ct]);
313 __aarp_expire_timer(&unresolved[ct]);
314 __aarp_expire_timer(&proxies[ct]);
317 write_unlock_bh(&aarp_lock);
318 mod_timer(&aarp_timer, jiffies +
319 (unresolved_count ? sysctl_aarp_tick_time :
320 sysctl_aarp_expiry_time));
323 /* Network device notifier chain handler. */
324 static int aarp_device_event(struct notifier_block *this, unsigned long event,
325 void *ptr)
327 int ct;
329 if (event == NETDEV_DOWN) {
330 write_lock_bh(&aarp_lock);
332 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
333 __aarp_expire_device(&resolved[ct], ptr);
334 __aarp_expire_device(&unresolved[ct], ptr);
335 __aarp_expire_device(&proxies[ct], ptr);
338 write_unlock_bh(&aarp_lock);
340 return NOTIFY_DONE;
343 /* Expire all entries in a hash chain */
344 static void __aarp_expire_all(struct aarp_entry **n)
346 struct aarp_entry *t;
348 while (*n) {
349 t = *n;
350 *n = (*n)->next;
351 __aarp_expire(t);
355 /* Cleanup all hash chains -- module unloading */
356 static void aarp_purge(void)
358 int ct;
360 write_lock_bh(&aarp_lock);
361 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
362 __aarp_expire_all(&resolved[ct]);
363 __aarp_expire_all(&unresolved[ct]);
364 __aarp_expire_all(&proxies[ct]);
366 write_unlock_bh(&aarp_lock);
370 * Create a new aarp entry. This must use GFP_ATOMIC because it
371 * runs while holding spinlocks.
373 static struct aarp_entry *aarp_alloc(void)
375 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
377 if (a)
378 skb_queue_head_init(&a->packet_queue);
379 return a;
383 * Find an entry. We might return an expired but not yet purged entry. We
384 * don't care as it will do no harm.
386 * This must run under the aarp_lock.
388 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
389 struct net_device *dev,
390 struct atalk_addr *sat)
392 while (list) {
393 if (list->target_addr.s_net == sat->s_net &&
394 list->target_addr.s_node == sat->s_node &&
395 list->dev == dev)
396 break;
397 list = list->next;
400 return list;
403 /* Called from the DDP code, and thus must be exported. */
404 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
406 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
407 struct aarp_entry *a;
409 write_lock_bh(&aarp_lock);
411 a = __aarp_find_entry(proxies[hash], dev, sa);
412 if (a)
413 a->expires_at = jiffies - 1;
415 write_unlock_bh(&aarp_lock);
418 /* This must run under aarp_lock. */
419 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
420 struct atalk_addr *sa)
422 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
423 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
425 return a ? sa : NULL;
429 * Probe a Phase 1 device or a device that requires its Net:Node to
430 * be set via an ioctl.
432 static void aarp_send_probe_phase1(struct atalk_iface *iface)
434 struct ifreq atreq;
435 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
437 sa->sat_addr.s_node = iface->address.s_node;
438 sa->sat_addr.s_net = ntohs(iface->address.s_net);
440 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
441 if (!(iface->dev->do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
442 (void)iface->dev->do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
443 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
444 iface->address.s_node != sa->sat_addr.s_node)
445 iface->status |= ATIF_PROBE_FAIL;
447 iface->address.s_net = htons(sa->sat_addr.s_net);
448 iface->address.s_node = sa->sat_addr.s_node;
453 void aarp_probe_network(struct atalk_iface *atif)
455 if (atif->dev->type == ARPHRD_LOCALTLK ||
456 atif->dev->type == ARPHRD_PPP)
457 aarp_send_probe_phase1(atif);
458 else {
459 unsigned int count;
461 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
462 aarp_send_probe(atif->dev, &atif->address);
464 /* Defer 1/10th */
465 current->state = TASK_INTERRUPTIBLE;
466 schedule_timeout(HZ / 10);
468 if (atif->status & ATIF_PROBE_FAIL)
469 break;
474 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
476 int hash, retval = -EPROTONOSUPPORT;
477 struct aarp_entry *entry;
478 unsigned int count;
481 * we don't currently support LocalTalk or PPP for proxy AARP;
482 * if someone wants to try and add it, have fun
484 if (atif->dev->type == ARPHRD_LOCALTLK ||
485 atif->dev->type == ARPHRD_PPP)
486 goto out;
489 * create a new AARP entry with the flags set to be published --
490 * we need this one to hang around even if it's in use
492 entry = aarp_alloc();
493 retval = -ENOMEM;
494 if (!entry)
495 goto out;
497 entry->expires_at = -1;
498 entry->status = ATIF_PROBE;
499 entry->target_addr.s_node = sa->s_node;
500 entry->target_addr.s_net = sa->s_net;
501 entry->dev = atif->dev;
503 write_lock_bh(&aarp_lock);
505 hash = sa->s_node % (AARP_HASH_SIZE - 1);
506 entry->next = proxies[hash];
507 proxies[hash] = entry;
509 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
510 aarp_send_probe(atif->dev, sa);
512 /* Defer 1/10th */
513 current->state = TASK_INTERRUPTIBLE;
514 write_unlock_bh(&aarp_lock);
515 schedule_timeout(HZ / 10);
516 write_lock_bh(&aarp_lock);
518 if (entry->status & ATIF_PROBE_FAIL)
519 break;
522 if (entry->status & ATIF_PROBE_FAIL) {
523 entry->expires_at = jiffies - 1; /* free the entry */
524 retval = -EADDRINUSE; /* return network full */
525 } else { /* clear the probing flag */
526 entry->status &= ~ATIF_PROBE;
527 retval = 1;
530 write_unlock_bh(&aarp_lock);
531 out:
532 return retval;
535 /* Send a DDP frame */
536 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
537 struct atalk_addr *sa, void *hwaddr)
539 static char ddp_eth_multicast[ETH_ALEN] =
540 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
541 int hash;
542 struct aarp_entry *a;
544 skb->nh.raw = skb->data;
546 /* Check for LocalTalk first */
547 if (dev->type == ARPHRD_LOCALTLK) {
548 struct atalk_addr *at = atalk_find_dev_addr(dev);
549 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
550 int ft = 2;
553 * Compressible ?
555 * IFF: src_net == dest_net == device_net
556 * (zero matches anything)
559 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
560 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
561 skb_pull(skb, sizeof(*ddp) - 4);
564 * The upper two remaining bytes are the port
565 * numbers we just happen to need. Now put the
566 * length in the lower two.
568 *((__u16 *)skb->data) = htons(skb->len);
569 ft = 1;
572 * Nice and easy. No AARP type protocols occur here so we can
573 * just shovel it out with a 3 byte LLAP header
576 skb_push(skb, 3);
577 skb->data[0] = sa->s_node;
578 skb->data[1] = at->s_node;
579 skb->data[2] = ft;
580 skb->dev = dev;
581 goto sendit;
584 /* On a PPP link we neither compress nor aarp. */
585 if (dev->type == ARPHRD_PPP) {
586 skb->protocol = htons(ETH_P_PPPTALK);
587 skb->dev = dev;
588 goto sendit;
591 /* Non ELAP we cannot do. */
592 if (dev->type != ARPHRD_ETHER)
593 return -1;
595 skb->dev = dev;
596 skb->protocol = htons(ETH_P_ATALK);
597 hash = sa->s_node % (AARP_HASH_SIZE - 1);
599 /* Do we have a resolved entry? */
600 if (sa->s_node == ATADDR_BCAST) {
601 /* Send it */
602 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
603 goto sent;
606 write_lock_bh(&aarp_lock);
607 a = __aarp_find_entry(resolved[hash], dev, sa);
609 if (a) { /* Return 1 and fill in the address */
610 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
611 ddp_dl->request(ddp_dl, skb, a->hwaddr);
612 write_unlock_bh(&aarp_lock);
613 goto sent;
616 /* Do we have an unresolved entry: This is the less common path */
617 a = __aarp_find_entry(unresolved[hash], dev, sa);
618 if (a) { /* Queue onto the unresolved queue */
619 skb_queue_tail(&a->packet_queue, skb);
620 goto out_unlock;
623 /* Allocate a new entry */
624 a = aarp_alloc();
625 if (!a) {
626 /* Whoops slipped... good job it's an unreliable protocol 8) */
627 write_unlock_bh(&aarp_lock);
628 return -1;
631 /* Set up the queue */
632 skb_queue_tail(&a->packet_queue, skb);
633 a->expires_at = jiffies + sysctl_aarp_resolve_time;
634 a->dev = dev;
635 a->next = unresolved[hash];
636 a->target_addr = *sa;
637 a->xmit_count = 0;
638 unresolved[hash] = a;
639 unresolved_count++;
641 /* Send an initial request for the address */
642 __aarp_send_query(a);
645 * Switch to fast timer if needed (That is if this is the first
646 * unresolved entry to get added)
649 if (unresolved_count == 1)
650 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
652 /* Now finally, it is safe to drop the lock. */
653 out_unlock:
654 write_unlock_bh(&aarp_lock);
656 /* Tell the ddp layer we have taken over for this frame. */
657 return 0;
659 sendit:
660 if (skb->sk)
661 skb->priority = skb->sk->sk_priority;
662 dev_queue_xmit(skb);
663 sent:
664 return 1;
668 * An entry in the aarp unresolved queue has become resolved. Send
669 * all the frames queued under it.
671 * Must run under aarp_lock.
673 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
674 int hash)
676 struct sk_buff *skb;
678 while (*list)
679 if (*list == a) {
680 unresolved_count--;
681 *list = a->next;
683 /* Move into the resolved list */
684 a->next = resolved[hash];
685 resolved[hash] = a;
687 /* Kick frames off */
688 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
689 a->expires_at = jiffies +
690 sysctl_aarp_expiry_time * 10;
691 ddp_dl->request(ddp_dl, skb, a->hwaddr);
693 } else
694 list = &((*list)->next);
698 * This is called by the SNAP driver whenever we see an AARP SNAP
699 * frame. We currently only support Ethernet.
701 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
702 struct packet_type *pt)
704 struct elapaarp *ea = aarp_hdr(skb);
705 int hash, ret = 0;
706 __u16 function;
707 struct aarp_entry *a;
708 struct atalk_addr sa, *ma, da;
709 struct atalk_iface *ifa;
711 /* We only do Ethernet SNAP AARP. */
712 if (dev->type != ARPHRD_ETHER)
713 goto out0;
715 /* Frame size ok? */
716 if (!skb_pull(skb, sizeof(*ea)))
717 goto out0;
719 function = ntohs(ea->function);
721 /* Sanity check fields. */
722 if (function < AARP_REQUEST || function > AARP_PROBE ||
723 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
724 ea->pa_src_zero || ea->pa_dst_zero)
725 goto out0;
727 /* Looks good. */
728 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
730 /* Build an address. */
731 sa.s_node = ea->pa_src_node;
732 sa.s_net = ea->pa_src_net;
734 /* Process the packet. Check for replies of me. */
735 ifa = atalk_find_dev(dev);
736 if (!ifa)
737 goto out1;
739 if (ifa->status & ATIF_PROBE &&
740 ifa->address.s_node == ea->pa_dst_node &&
741 ifa->address.s_net == ea->pa_dst_net) {
742 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
743 goto out1;
746 /* Check for replies of proxy AARP entries */
747 da.s_node = ea->pa_dst_node;
748 da.s_net = ea->pa_dst_net;
750 write_lock_bh(&aarp_lock);
751 a = __aarp_find_entry(proxies[hash], dev, &da);
753 if (a && a->status & ATIF_PROBE) {
754 a->status |= ATIF_PROBE_FAIL;
756 * we do not respond to probe or request packets for
757 * this address while we are probing this address
759 goto unlock;
762 switch (function) {
763 case AARP_REPLY:
764 if (!unresolved_count) /* Speed up */
765 break;
767 /* Find the entry. */
768 a = __aarp_find_entry(unresolved[hash], dev, &sa);
769 if (!a || dev != a->dev)
770 break;
772 /* We can fill one in - this is good. */
773 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
774 __aarp_resolved(&unresolved[hash], a, hash);
775 if (!unresolved_count)
776 mod_timer(&aarp_timer,
777 jiffies + sysctl_aarp_expiry_time);
778 break;
780 case AARP_REQUEST:
781 case AARP_PROBE:
784 * If it is my address set ma to my address and reply.
785 * We can treat probe and request the same. Probe
786 * simply means we shouldn't cache the querying host,
787 * as in a probe they are proposing an address not
788 * using one.
790 * Support for proxy-AARP added. We check if the
791 * address is one of our proxies before we toss the
792 * packet out.
795 sa.s_node = ea->pa_dst_node;
796 sa.s_net = ea->pa_dst_net;
798 /* See if we have a matching proxy. */
799 ma = __aarp_proxy_find(dev, &sa);
800 if (!ma)
801 ma = &ifa->address;
802 else { /* We need to make a copy of the entry. */
803 da.s_node = sa.s_node;
804 da.s_net = da.s_net;
805 ma = &da;
808 if (function == AARP_PROBE) {
810 * A probe implies someone trying to get an
811 * address. So as a precaution flush any
812 * entries we have for this address.
814 struct aarp_entry *a;
816 a = __aarp_find_entry(resolved[sa.s_node %
817 (AARP_HASH_SIZE - 1)],
818 skb->dev, &sa);
821 * Make it expire next tick - that avoids us
822 * getting into a probe/flush/learn/probe/
823 * flush/learn cycle during probing of a slow
824 * to respond host addr.
826 if (a) {
827 a->expires_at = jiffies - 1;
828 mod_timer(&aarp_timer, jiffies +
829 sysctl_aarp_tick_time);
833 if (sa.s_node != ma->s_node)
834 break;
836 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
837 break;
839 sa.s_node = ea->pa_src_node;
840 sa.s_net = ea->pa_src_net;
842 /* aarp_my_address has found the address to use for us.
844 aarp_send_reply(dev, ma, &sa, ea->hw_src);
845 break;
848 unlock:
849 write_unlock_bh(&aarp_lock);
850 out1:
851 ret = 1;
852 out0:
853 kfree_skb(skb);
854 return ret;
857 static struct notifier_block aarp_notifier = {
858 .notifier_call = aarp_device_event,
861 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
863 void __init aarp_proto_init(void)
865 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
866 if (!aarp_dl)
867 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
868 init_timer(&aarp_timer);
869 aarp_timer.function = aarp_expire_timeout;
870 aarp_timer.data = 0;
871 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
872 add_timer(&aarp_timer);
873 register_netdevice_notifier(&aarp_notifier);
876 /* Remove the AARP entries associated with a device. */
877 void aarp_device_down(struct net_device *dev)
879 int ct;
881 write_lock_bh(&aarp_lock);
883 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
884 __aarp_expire_device(&resolved[ct], dev);
885 __aarp_expire_device(&unresolved[ct], dev);
886 __aarp_expire_device(&proxies[ct], dev);
889 write_unlock_bh(&aarp_lock);
892 #ifdef CONFIG_PROC_FS
893 struct aarp_iter_state {
894 int bucket;
895 struct aarp_entry **table;
899 * Get the aarp entry that is in the chain described
900 * by the iterator.
901 * If pos is set then skip till that index.
902 * pos = 1 is the first entry
904 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
906 int ct = iter->bucket;
907 struct aarp_entry **table = iter->table;
908 loff_t off = 0;
909 struct aarp_entry *entry;
911 rescan:
912 while(ct < AARP_HASH_SIZE) {
913 for (entry = table[ct]; entry; entry = entry->next) {
914 if (!pos || ++off == *pos) {
915 iter->table = table;
916 iter->bucket = ct;
917 return entry;
920 ++ct;
923 if (table == resolved) {
924 ct = 0;
925 table = unresolved;
926 goto rescan;
928 if (table == unresolved) {
929 ct = 0;
930 table = proxies;
931 goto rescan;
933 return NULL;
936 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
938 struct aarp_iter_state *iter = seq->private;
940 read_lock_bh(&aarp_lock);
941 iter->table = resolved;
942 iter->bucket = 0;
944 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
947 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
949 struct aarp_entry *entry = v;
950 struct aarp_iter_state *iter = seq->private;
952 ++*pos;
954 /* first line after header */
955 if (v == SEQ_START_TOKEN)
956 entry = iter_next(iter, NULL);
958 /* next entry in current bucket */
959 else if (entry->next)
960 entry = entry->next;
962 /* next bucket or table */
963 else {
964 ++iter->bucket;
965 entry = iter_next(iter, NULL);
967 return entry;
970 static void aarp_seq_stop(struct seq_file *seq, void *v)
972 read_unlock_bh(&aarp_lock);
975 static const char *dt2str(unsigned long ticks)
977 static char buf[32];
979 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
981 return buf;
984 static int aarp_seq_show(struct seq_file *seq, void *v)
986 struct aarp_iter_state *iter = seq->private;
987 struct aarp_entry *entry = v;
988 unsigned long now = jiffies;
990 if (v == SEQ_START_TOKEN)
991 seq_puts(seq,
992 "Address Interface Hardware Address"
993 " Expires LastSend Retry Status\n");
994 else {
995 seq_printf(seq, "%04X:%02X %-12s",
996 ntohs(entry->target_addr.s_net),
997 (unsigned int) entry->target_addr.s_node,
998 entry->dev ? entry->dev->name : "????");
999 seq_printf(seq, "%02X:%02X:%02X:%02X:%02X:%02X",
1000 entry->hwaddr[0] & 0xFF,
1001 entry->hwaddr[1] & 0xFF,
1002 entry->hwaddr[2] & 0xFF,
1003 entry->hwaddr[3] & 0xFF,
1004 entry->hwaddr[4] & 0xFF,
1005 entry->hwaddr[5] & 0xFF);
1006 seq_printf(seq, " %8s",
1007 dt2str((long)entry->expires_at - (long)now));
1008 if (iter->table == unresolved)
1009 seq_printf(seq, " %8s %6hu",
1010 dt2str(now - entry->last_sent),
1011 entry->xmit_count);
1012 else
1013 seq_puts(seq, " ");
1014 seq_printf(seq, " %s\n",
1015 (iter->table == resolved) ? "resolved"
1016 : (iter->table == unresolved) ? "unresolved"
1017 : (iter->table == proxies) ? "proxies"
1018 : "unknown");
1020 return 0;
1023 static struct seq_operations aarp_seq_ops = {
1024 .start = aarp_seq_start,
1025 .next = aarp_seq_next,
1026 .stop = aarp_seq_stop,
1027 .show = aarp_seq_show,
1030 static int aarp_seq_open(struct inode *inode, struct file *file)
1032 struct seq_file *seq;
1033 int rc = -ENOMEM;
1034 struct aarp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1036 if (!s)
1037 goto out;
1039 rc = seq_open(file, &aarp_seq_ops);
1040 if (rc)
1041 goto out_kfree;
1043 seq = file->private_data;
1044 seq->private = s;
1045 memset(s, 0, sizeof(*s));
1046 out:
1047 return rc;
1048 out_kfree:
1049 kfree(s);
1050 goto out;
1053 struct file_operations atalk_seq_arp_fops = {
1054 .owner = THIS_MODULE,
1055 .open = aarp_seq_open,
1056 .read = seq_read,
1057 .llseek = seq_lseek,
1058 .release = seq_release_private,
1060 #endif
1062 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1063 void aarp_cleanup_module(void)
1065 del_timer_sync(&aarp_timer);
1066 unregister_netdevice_notifier(&aarp_notifier);
1067 unregister_snap_client(aarp_dl);
1068 aarp_purge();