GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / appletalk / aarp.c
blobc8bbe697c0aafa2b41f569bf826b04b5297c5403
3 #include <linux/if_arp.h>
4 #include <linux/slab.h>
5 #include <net/sock.h>
6 #include <net/datalink.h>
7 #include <net/psnap.h>
8 #include <linux/atalk.h>
9 #include <linux/delay.h>
10 #include <linux/init.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
14 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
15 int sysctl_aarp_tick_time = AARP_TICK_TIME;
16 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
17 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
19 /* Lists of aarp entries */
20 /**
21 * struct aarp_entry - AARP entry
22 * @last_sent - Last time we xmitted the aarp request
23 * @packet_queue - Queue of frames wait for resolution
24 * @status - Used for proxy AARP
25 * expires_at - Entry expiry time
26 * target_addr - DDP Address
27 * dev - Device to use
28 * hwaddr - Physical i/f address of target/router
29 * xmit_count - When this hits 10 we give up
30 * next - Next entry in chain
32 struct aarp_entry {
33 /* These first two are only used for unresolved entries */
34 unsigned long last_sent;
35 struct sk_buff_head packet_queue;
36 int status;
37 unsigned long expires_at;
38 struct atalk_addr target_addr;
39 struct net_device *dev;
40 char hwaddr[6];
41 unsigned short xmit_count;
42 struct aarp_entry *next;
45 /* Hashed list of resolved, unresolved and proxy entries */
46 static struct aarp_entry *resolved[AARP_HASH_SIZE];
47 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
48 static struct aarp_entry *proxies[AARP_HASH_SIZE];
49 static int unresolved_count;
51 /* One lock protects it all. */
52 static DEFINE_RWLOCK(aarp_lock);
54 /* Used to walk the list and purge/kick entries. */
55 static struct timer_list aarp_timer;
58 * Delete an aarp queue
60 * Must run under aarp_lock.
62 static void __aarp_expire(struct aarp_entry *a)
64 skb_queue_purge(&a->packet_queue);
65 kfree(a);
69 * Send an aarp queue entry request
71 * Must run under aarp_lock.
73 static void __aarp_send_query(struct aarp_entry *a)
75 static unsigned char aarp_eth_multicast[ETH_ALEN] =
76 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
77 struct net_device *dev = a->dev;
78 struct elapaarp *eah;
79 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
80 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
81 struct atalk_addr *sat = atalk_find_dev_addr(dev);
83 if (!skb)
84 return;
86 if (!sat) {
87 kfree_skb(skb);
88 return;
91 /* Set up the buffer */
92 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
93 skb_reset_network_header(skb);
94 skb_reset_transport_header(skb);
95 skb_put(skb, sizeof(*eah));
96 skb->protocol = htons(ETH_P_ATALK);
97 skb->dev = dev;
98 eah = aarp_hdr(skb);
100 /* Set up the ARP */
101 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
102 eah->pa_type = htons(ETH_P_ATALK);
103 eah->hw_len = ETH_ALEN;
104 eah->pa_len = AARP_PA_ALEN;
105 eah->function = htons(AARP_REQUEST);
107 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
109 eah->pa_src_zero = 0;
110 eah->pa_src_net = sat->s_net;
111 eah->pa_src_node = sat->s_node;
113 memset(eah->hw_dst, '\0', ETH_ALEN);
115 eah->pa_dst_zero = 0;
116 eah->pa_dst_net = a->target_addr.s_net;
117 eah->pa_dst_node = a->target_addr.s_node;
119 /* Send it */
120 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
121 /* Update the sending count */
122 a->xmit_count++;
123 a->last_sent = jiffies;
126 /* This runs under aarp_lock and in softint context, so only atomic memory
127 * allocations can be used. */
128 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
129 struct atalk_addr *them, unsigned char *sha)
131 struct elapaarp *eah;
132 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
133 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
135 if (!skb)
136 return;
138 /* Set up the buffer */
139 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
140 skb_reset_network_header(skb);
141 skb_reset_transport_header(skb);
142 skb_put(skb, sizeof(*eah));
143 skb->protocol = htons(ETH_P_ATALK);
144 skb->dev = dev;
145 eah = aarp_hdr(skb);
147 /* Set up the ARP */
148 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
149 eah->pa_type = htons(ETH_P_ATALK);
150 eah->hw_len = ETH_ALEN;
151 eah->pa_len = AARP_PA_ALEN;
152 eah->function = htons(AARP_REPLY);
154 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
156 eah->pa_src_zero = 0;
157 eah->pa_src_net = us->s_net;
158 eah->pa_src_node = us->s_node;
160 if (!sha)
161 memset(eah->hw_dst, '\0', ETH_ALEN);
162 else
163 memcpy(eah->hw_dst, sha, ETH_ALEN);
165 eah->pa_dst_zero = 0;
166 eah->pa_dst_net = them->s_net;
167 eah->pa_dst_node = them->s_node;
169 /* Send it */
170 aarp_dl->request(aarp_dl, skb, sha);
174 * Send probe frames. Called from aarp_probe_network and
175 * aarp_proxy_probe_network.
178 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
180 struct elapaarp *eah;
181 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
182 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
183 static unsigned char aarp_eth_multicast[ETH_ALEN] =
184 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
186 if (!skb)
187 return;
189 /* Set up the buffer */
190 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
191 skb_reset_network_header(skb);
192 skb_reset_transport_header(skb);
193 skb_put(skb, sizeof(*eah));
194 skb->protocol = htons(ETH_P_ATALK);
195 skb->dev = dev;
196 eah = aarp_hdr(skb);
198 /* Set up the ARP */
199 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
200 eah->pa_type = htons(ETH_P_ATALK);
201 eah->hw_len = ETH_ALEN;
202 eah->pa_len = AARP_PA_ALEN;
203 eah->function = htons(AARP_PROBE);
205 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
207 eah->pa_src_zero = 0;
208 eah->pa_src_net = us->s_net;
209 eah->pa_src_node = us->s_node;
211 memset(eah->hw_dst, '\0', ETH_ALEN);
213 eah->pa_dst_zero = 0;
214 eah->pa_dst_net = us->s_net;
215 eah->pa_dst_node = us->s_node;
217 /* Send it */
218 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
222 * Handle an aarp timer expire
224 * Must run under the aarp_lock.
227 static void __aarp_expire_timer(struct aarp_entry **n)
229 struct aarp_entry *t;
231 while (*n)
232 /* Expired ? */
233 if (time_after(jiffies, (*n)->expires_at)) {
234 t = *n;
235 *n = (*n)->next;
236 __aarp_expire(t);
237 } else
238 n = &((*n)->next);
242 * Kick all pending requests 5 times a second.
244 * Must run under the aarp_lock.
246 static void __aarp_kick(struct aarp_entry **n)
248 struct aarp_entry *t;
250 while (*n)
251 /* Expired: if this will be the 11th tx, we delete instead. */
252 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
253 t = *n;
254 *n = (*n)->next;
255 __aarp_expire(t);
256 } else {
257 __aarp_send_query(*n);
258 n = &((*n)->next);
263 * A device has gone down. Take all entries referring to the device
264 * and remove them.
266 * Must run under the aarp_lock.
268 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
270 struct aarp_entry *t;
272 while (*n)
273 if ((*n)->dev == dev) {
274 t = *n;
275 *n = (*n)->next;
276 __aarp_expire(t);
277 } else
278 n = &((*n)->next);
281 /* Handle the timer event */
282 static void aarp_expire_timeout(unsigned long unused)
284 int ct;
286 write_lock_bh(&aarp_lock);
288 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
289 __aarp_expire_timer(&resolved[ct]);
290 __aarp_kick(&unresolved[ct]);
291 __aarp_expire_timer(&unresolved[ct]);
292 __aarp_expire_timer(&proxies[ct]);
295 write_unlock_bh(&aarp_lock);
296 mod_timer(&aarp_timer, jiffies +
297 (unresolved_count ? sysctl_aarp_tick_time :
298 sysctl_aarp_expiry_time));
301 /* Network device notifier chain handler. */
302 static int aarp_device_event(struct notifier_block *this, unsigned long event,
303 void *ptr)
305 struct net_device *dev = ptr;
306 int ct;
308 if (!net_eq(dev_net(dev), &init_net))
309 return NOTIFY_DONE;
311 if (event == NETDEV_DOWN) {
312 write_lock_bh(&aarp_lock);
314 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
315 __aarp_expire_device(&resolved[ct], dev);
316 __aarp_expire_device(&unresolved[ct], dev);
317 __aarp_expire_device(&proxies[ct], dev);
320 write_unlock_bh(&aarp_lock);
322 return NOTIFY_DONE;
325 /* Expire all entries in a hash chain */
326 static void __aarp_expire_all(struct aarp_entry **n)
328 struct aarp_entry *t;
330 while (*n) {
331 t = *n;
332 *n = (*n)->next;
333 __aarp_expire(t);
337 /* Cleanup all hash chains -- module unloading */
338 static void aarp_purge(void)
340 int ct;
342 write_lock_bh(&aarp_lock);
343 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
344 __aarp_expire_all(&resolved[ct]);
345 __aarp_expire_all(&unresolved[ct]);
346 __aarp_expire_all(&proxies[ct]);
348 write_unlock_bh(&aarp_lock);
352 * Create a new aarp entry. This must use GFP_ATOMIC because it
353 * runs while holding spinlocks.
355 static struct aarp_entry *aarp_alloc(void)
357 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
359 if (a)
360 skb_queue_head_init(&a->packet_queue);
361 return a;
365 * Find an entry. We might return an expired but not yet purged entry. We
366 * don't care as it will do no harm.
368 * This must run under the aarp_lock.
370 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
371 struct net_device *dev,
372 struct atalk_addr *sat)
374 while (list) {
375 if (list->target_addr.s_net == sat->s_net &&
376 list->target_addr.s_node == sat->s_node &&
377 list->dev == dev)
378 break;
379 list = list->next;
382 return list;
385 /* Called from the DDP code, and thus must be exported. */
386 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
388 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
389 struct aarp_entry *a;
391 write_lock_bh(&aarp_lock);
393 a = __aarp_find_entry(proxies[hash], dev, sa);
394 if (a)
395 a->expires_at = jiffies - 1;
397 write_unlock_bh(&aarp_lock);
400 /* This must run under aarp_lock. */
401 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
402 struct atalk_addr *sa)
404 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
405 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
407 return a ? sa : NULL;
411 * Probe a Phase 1 device or a device that requires its Net:Node to
412 * be set via an ioctl.
414 static void aarp_send_probe_phase1(struct atalk_iface *iface)
416 struct ifreq atreq;
417 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
418 const struct net_device_ops *ops = iface->dev->netdev_ops;
420 sa->sat_addr.s_node = iface->address.s_node;
421 sa->sat_addr.s_net = ntohs(iface->address.s_net);
423 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
424 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
425 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
426 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
427 iface->address.s_node != sa->sat_addr.s_node)
428 iface->status |= ATIF_PROBE_FAIL;
430 iface->address.s_net = htons(sa->sat_addr.s_net);
431 iface->address.s_node = sa->sat_addr.s_node;
436 void aarp_probe_network(struct atalk_iface *atif)
438 if (atif->dev->type == ARPHRD_LOCALTLK ||
439 atif->dev->type == ARPHRD_PPP)
440 aarp_send_probe_phase1(atif);
441 else {
442 unsigned int count;
444 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
445 aarp_send_probe(atif->dev, &atif->address);
447 /* Defer 1/10th */
448 msleep(100);
450 if (atif->status & ATIF_PROBE_FAIL)
451 break;
456 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
458 int hash, retval = -EPROTONOSUPPORT;
459 struct aarp_entry *entry;
460 unsigned int count;
463 * we don't currently support LocalTalk or PPP for proxy AARP;
464 * if someone wants to try and add it, have fun
466 if (atif->dev->type == ARPHRD_LOCALTLK ||
467 atif->dev->type == ARPHRD_PPP)
468 goto out;
471 * create a new AARP entry with the flags set to be published --
472 * we need this one to hang around even if it's in use
474 entry = aarp_alloc();
475 retval = -ENOMEM;
476 if (!entry)
477 goto out;
479 entry->expires_at = -1;
480 entry->status = ATIF_PROBE;
481 entry->target_addr.s_node = sa->s_node;
482 entry->target_addr.s_net = sa->s_net;
483 entry->dev = atif->dev;
485 write_lock_bh(&aarp_lock);
487 hash = sa->s_node % (AARP_HASH_SIZE - 1);
488 entry->next = proxies[hash];
489 proxies[hash] = entry;
491 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
492 aarp_send_probe(atif->dev, sa);
494 /* Defer 1/10th */
495 write_unlock_bh(&aarp_lock);
496 msleep(100);
497 write_lock_bh(&aarp_lock);
499 if (entry->status & ATIF_PROBE_FAIL)
500 break;
503 if (entry->status & ATIF_PROBE_FAIL) {
504 entry->expires_at = jiffies - 1; /* free the entry */
505 retval = -EADDRINUSE; /* return network full */
506 } else { /* clear the probing flag */
507 entry->status &= ~ATIF_PROBE;
508 retval = 1;
511 write_unlock_bh(&aarp_lock);
512 out:
513 return retval;
516 /* Send a DDP frame */
517 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
518 struct atalk_addr *sa, void *hwaddr)
520 static char ddp_eth_multicast[ETH_ALEN] =
521 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
522 int hash;
523 struct aarp_entry *a;
525 skb_reset_network_header(skb);
527 /* Check for LocalTalk first */
528 if (dev->type == ARPHRD_LOCALTLK) {
529 struct atalk_addr *at = atalk_find_dev_addr(dev);
530 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
531 int ft = 2;
534 * Compressible ?
536 * IFF: src_net == dest_net == device_net
537 * (zero matches anything)
540 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
541 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
542 skb_pull(skb, sizeof(*ddp) - 4);
545 * The upper two remaining bytes are the port
546 * numbers we just happen to need. Now put the
547 * length in the lower two.
549 *((__be16 *)skb->data) = htons(skb->len);
550 ft = 1;
553 * Nice and easy. No AARP type protocols occur here so we can
554 * just shovel it out with a 3 byte LLAP header
557 skb_push(skb, 3);
558 skb->data[0] = sa->s_node;
559 skb->data[1] = at->s_node;
560 skb->data[2] = ft;
561 skb->dev = dev;
562 goto sendit;
565 /* On a PPP link we neither compress nor aarp. */
566 if (dev->type == ARPHRD_PPP) {
567 skb->protocol = htons(ETH_P_PPPTALK);
568 skb->dev = dev;
569 goto sendit;
572 /* Non ELAP we cannot do. */
573 if (dev->type != ARPHRD_ETHER)
574 goto free_it;
576 skb->dev = dev;
577 skb->protocol = htons(ETH_P_ATALK);
578 hash = sa->s_node % (AARP_HASH_SIZE - 1);
580 /* Do we have a resolved entry? */
581 if (sa->s_node == ATADDR_BCAST) {
582 /* Send it */
583 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
584 goto sent;
587 write_lock_bh(&aarp_lock);
588 a = __aarp_find_entry(resolved[hash], dev, sa);
590 if (a) { /* Return 1 and fill in the address */
591 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
592 ddp_dl->request(ddp_dl, skb, a->hwaddr);
593 write_unlock_bh(&aarp_lock);
594 goto sent;
597 /* Do we have an unresolved entry: This is the less common path */
598 a = __aarp_find_entry(unresolved[hash], dev, sa);
599 if (a) { /* Queue onto the unresolved queue */
600 skb_queue_tail(&a->packet_queue, skb);
601 goto out_unlock;
604 /* Allocate a new entry */
605 a = aarp_alloc();
606 if (!a) {
607 /* Whoops slipped... good job it's an unreliable protocol 8) */
608 write_unlock_bh(&aarp_lock);
609 goto free_it;
612 /* Set up the queue */
613 skb_queue_tail(&a->packet_queue, skb);
614 a->expires_at = jiffies + sysctl_aarp_resolve_time;
615 a->dev = dev;
616 a->next = unresolved[hash];
617 a->target_addr = *sa;
618 a->xmit_count = 0;
619 unresolved[hash] = a;
620 unresolved_count++;
622 /* Send an initial request for the address */
623 __aarp_send_query(a);
626 * Switch to fast timer if needed (That is if this is the first
627 * unresolved entry to get added)
630 if (unresolved_count == 1)
631 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
633 /* Now finally, it is safe to drop the lock. */
634 out_unlock:
635 write_unlock_bh(&aarp_lock);
637 /* Tell the ddp layer we have taken over for this frame. */
638 goto sent;
640 sendit:
641 if (skb->sk)
642 skb->priority = skb->sk->sk_priority;
643 if (dev_queue_xmit(skb))
644 goto drop;
645 sent:
646 return NET_XMIT_SUCCESS;
647 free_it:
648 kfree_skb(skb);
649 drop:
650 return NET_XMIT_DROP;
652 EXPORT_SYMBOL(aarp_send_ddp);
655 * An entry in the aarp unresolved queue has become resolved. Send
656 * all the frames queued under it.
658 * Must run under aarp_lock.
660 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
661 int hash)
663 struct sk_buff *skb;
665 while (*list)
666 if (*list == a) {
667 unresolved_count--;
668 *list = a->next;
670 /* Move into the resolved list */
671 a->next = resolved[hash];
672 resolved[hash] = a;
674 /* Kick frames off */
675 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
676 a->expires_at = jiffies +
677 sysctl_aarp_expiry_time * 10;
678 ddp_dl->request(ddp_dl, skb, a->hwaddr);
680 } else
681 list = &((*list)->next);
685 * This is called by the SNAP driver whenever we see an AARP SNAP
686 * frame. We currently only support Ethernet.
688 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
689 struct packet_type *pt, struct net_device *orig_dev)
691 struct elapaarp *ea = aarp_hdr(skb);
692 int hash, ret = 0;
693 __u16 function;
694 struct aarp_entry *a;
695 struct atalk_addr sa, *ma, da;
696 struct atalk_iface *ifa;
698 if (!net_eq(dev_net(dev), &init_net))
699 goto out0;
701 /* We only do Ethernet SNAP AARP. */
702 if (dev->type != ARPHRD_ETHER)
703 goto out0;
705 /* Frame size ok? */
706 if (!skb_pull(skb, sizeof(*ea)))
707 goto out0;
709 function = ntohs(ea->function);
711 /* Sanity check fields. */
712 if (function < AARP_REQUEST || function > AARP_PROBE ||
713 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
714 ea->pa_src_zero || ea->pa_dst_zero)
715 goto out0;
717 /* Looks good. */
718 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
720 /* Build an address. */
721 sa.s_node = ea->pa_src_node;
722 sa.s_net = ea->pa_src_net;
724 /* Process the packet. Check for replies of me. */
725 ifa = atalk_find_dev(dev);
726 if (!ifa)
727 goto out1;
729 if (ifa->status & ATIF_PROBE &&
730 ifa->address.s_node == ea->pa_dst_node &&
731 ifa->address.s_net == ea->pa_dst_net) {
732 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
733 goto out1;
736 /* Check for replies of proxy AARP entries */
737 da.s_node = ea->pa_dst_node;
738 da.s_net = ea->pa_dst_net;
740 write_lock_bh(&aarp_lock);
741 a = __aarp_find_entry(proxies[hash], dev, &da);
743 if (a && a->status & ATIF_PROBE) {
744 a->status |= ATIF_PROBE_FAIL;
746 * we do not respond to probe or request packets for
747 * this address while we are probing this address
749 goto unlock;
752 switch (function) {
753 case AARP_REPLY:
754 if (!unresolved_count) /* Speed up */
755 break;
757 /* Find the entry. */
758 a = __aarp_find_entry(unresolved[hash], dev, &sa);
759 if (!a || dev != a->dev)
760 break;
762 /* We can fill one in - this is good. */
763 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
764 __aarp_resolved(&unresolved[hash], a, hash);
765 if (!unresolved_count)
766 mod_timer(&aarp_timer,
767 jiffies + sysctl_aarp_expiry_time);
768 break;
770 case AARP_REQUEST:
771 case AARP_PROBE:
774 * If it is my address set ma to my address and reply.
775 * We can treat probe and request the same. Probe
776 * simply means we shouldn't cache the querying host,
777 * as in a probe they are proposing an address not
778 * using one.
780 * Support for proxy-AARP added. We check if the
781 * address is one of our proxies before we toss the
782 * packet out.
785 sa.s_node = ea->pa_dst_node;
786 sa.s_net = ea->pa_dst_net;
788 /* See if we have a matching proxy. */
789 ma = __aarp_proxy_find(dev, &sa);
790 if (!ma)
791 ma = &ifa->address;
792 else { /* We need to make a copy of the entry. */
793 da.s_node = sa.s_node;
794 da.s_net = sa.s_net;
795 ma = &da;
798 if (function == AARP_PROBE) {
800 * A probe implies someone trying to get an
801 * address. So as a precaution flush any
802 * entries we have for this address.
804 a = __aarp_find_entry(resolved[sa.s_node %
805 (AARP_HASH_SIZE - 1)],
806 skb->dev, &sa);
809 * Make it expire next tick - that avoids us
810 * getting into a probe/flush/learn/probe/
811 * flush/learn cycle during probing of a slow
812 * to respond host addr.
814 if (a) {
815 a->expires_at = jiffies - 1;
816 mod_timer(&aarp_timer, jiffies +
817 sysctl_aarp_tick_time);
821 if (sa.s_node != ma->s_node)
822 break;
824 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
825 break;
827 sa.s_node = ea->pa_src_node;
828 sa.s_net = ea->pa_src_net;
830 /* aarp_my_address has found the address to use for us.
832 aarp_send_reply(dev, ma, &sa, ea->hw_src);
833 break;
836 unlock:
837 write_unlock_bh(&aarp_lock);
838 out1:
839 ret = 1;
840 out0:
841 kfree_skb(skb);
842 return ret;
845 static struct notifier_block aarp_notifier = {
846 .notifier_call = aarp_device_event,
849 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
851 void __init aarp_proto_init(void)
853 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
854 if (!aarp_dl)
855 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
856 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
857 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
858 add_timer(&aarp_timer);
859 register_netdevice_notifier(&aarp_notifier);
862 /* Remove the AARP entries associated with a device. */
863 void aarp_device_down(struct net_device *dev)
865 int ct;
867 write_lock_bh(&aarp_lock);
869 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
870 __aarp_expire_device(&resolved[ct], dev);
871 __aarp_expire_device(&unresolved[ct], dev);
872 __aarp_expire_device(&proxies[ct], dev);
875 write_unlock_bh(&aarp_lock);
878 #ifdef CONFIG_PROC_FS
879 struct aarp_iter_state {
880 int bucket;
881 struct aarp_entry **table;
885 * Get the aarp entry that is in the chain described
886 * by the iterator.
887 * If pos is set then skip till that index.
888 * pos = 1 is the first entry
890 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
892 int ct = iter->bucket;
893 struct aarp_entry **table = iter->table;
894 loff_t off = 0;
895 struct aarp_entry *entry;
897 rescan:
898 while(ct < AARP_HASH_SIZE) {
899 for (entry = table[ct]; entry; entry = entry->next) {
900 if (!pos || ++off == *pos) {
901 iter->table = table;
902 iter->bucket = ct;
903 return entry;
906 ++ct;
909 if (table == resolved) {
910 ct = 0;
911 table = unresolved;
912 goto rescan;
914 if (table == unresolved) {
915 ct = 0;
916 table = proxies;
917 goto rescan;
919 return NULL;
922 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
923 __acquires(aarp_lock)
925 struct aarp_iter_state *iter = seq->private;
927 read_lock_bh(&aarp_lock);
928 iter->table = resolved;
929 iter->bucket = 0;
931 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
934 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
936 struct aarp_entry *entry = v;
937 struct aarp_iter_state *iter = seq->private;
939 ++*pos;
941 /* first line after header */
942 if (v == SEQ_START_TOKEN)
943 entry = iter_next(iter, NULL);
945 /* next entry in current bucket */
946 else if (entry->next)
947 entry = entry->next;
949 /* next bucket or table */
950 else {
951 ++iter->bucket;
952 entry = iter_next(iter, NULL);
954 return entry;
957 static void aarp_seq_stop(struct seq_file *seq, void *v)
958 __releases(aarp_lock)
960 read_unlock_bh(&aarp_lock);
963 static const char *dt2str(unsigned long ticks)
965 static char buf[32];
967 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
969 return buf;
972 static int aarp_seq_show(struct seq_file *seq, void *v)
974 struct aarp_iter_state *iter = seq->private;
975 struct aarp_entry *entry = v;
976 unsigned long now = jiffies;
978 if (v == SEQ_START_TOKEN)
979 seq_puts(seq,
980 "Address Interface Hardware Address"
981 " Expires LastSend Retry Status\n");
982 else {
983 seq_printf(seq, "%04X:%02X %-12s",
984 ntohs(entry->target_addr.s_net),
985 (unsigned int) entry->target_addr.s_node,
986 entry->dev ? entry->dev->name : "????");
987 seq_printf(seq, "%pM", entry->hwaddr);
988 seq_printf(seq, " %8s",
989 dt2str((long)entry->expires_at - (long)now));
990 if (iter->table == unresolved)
991 seq_printf(seq, " %8s %6hu",
992 dt2str(now - entry->last_sent),
993 entry->xmit_count);
994 else
995 seq_puts(seq, " ");
996 seq_printf(seq, " %s\n",
997 (iter->table == resolved) ? "resolved"
998 : (iter->table == unresolved) ? "unresolved"
999 : (iter->table == proxies) ? "proxies"
1000 : "unknown");
1002 return 0;
1005 static const struct seq_operations aarp_seq_ops = {
1006 .start = aarp_seq_start,
1007 .next = aarp_seq_next,
1008 .stop = aarp_seq_stop,
1009 .show = aarp_seq_show,
1012 static int aarp_seq_open(struct inode *inode, struct file *file)
1014 return seq_open_private(file, &aarp_seq_ops,
1015 sizeof(struct aarp_iter_state));
1018 const struct file_operations atalk_seq_arp_fops = {
1019 .owner = THIS_MODULE,
1020 .open = aarp_seq_open,
1021 .read = seq_read,
1022 .llseek = seq_lseek,
1023 .release = seq_release_private,
1025 #endif
1027 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1028 void aarp_cleanup_module(void)
1030 del_timer_sync(&aarp_timer);
1031 unregister_netdevice_notifier(&aarp_notifier);
1032 unregister_snap_client(aarp_dl);
1033 aarp_purge();