Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / net / appletalk / aarp.c
blobd1c55d8dd0a2538eaabe403ceeb26896c00adf66
1 /*
2 * AARP: An implementation of the AppleTalk AARP protocol for
3 * Ethernet 'ELAP'.
5 * Alan Cox <Alan.Cox@linux.org>
7 * This doesn't fit cleanly with the IP arp. Potentially we can use
8 * the generic neighbour discovery code to clean this up.
10 * FIXME:
11 * We ought to handle the retransmits with a single list and a
12 * separate fast timer for when it is needed.
13 * Use neighbour discovery code.
14 * Token Ring Support.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
22 * References:
23 * Inside AppleTalk (2nd Ed).
24 * Fixes:
25 * Jaume Grau - flush caches on AARP_PROBE
26 * Rob Newberry - Added proxy AARP and AARP proc fs,
27 * moved probing from DDP module.
28 * Arnaldo C. Melo - don't mangle rx packets
32 #include <linux/if_arp.h>
33 #include <linux/slab.h>
34 #include <net/sock.h>
35 #include <net/datalink.h>
36 #include <net/psnap.h>
37 #include <linux/atalk.h>
38 #include <linux/delay.h>
39 #include <linux/init.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42 #include <linux/export.h>
43 #include <linux/etherdevice.h>
45 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
46 int sysctl_aarp_tick_time = AARP_TICK_TIME;
47 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
48 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
50 /* Lists of aarp entries */
51 /**
52 * struct aarp_entry - AARP entry
53 * @last_sent - Last time we xmitted the aarp request
54 * @packet_queue - Queue of frames wait for resolution
55 * @status - Used for proxy AARP
56 * expires_at - Entry expiry time
57 * target_addr - DDP Address
58 * dev - Device to use
59 * hwaddr - Physical i/f address of target/router
60 * xmit_count - When this hits 10 we give up
61 * next - Next entry in chain
63 struct aarp_entry {
64 /* These first two are only used for unresolved entries */
65 unsigned long last_sent;
66 struct sk_buff_head packet_queue;
67 int status;
68 unsigned long expires_at;
69 struct atalk_addr target_addr;
70 struct net_device *dev;
71 char hwaddr[ETH_ALEN];
72 unsigned short xmit_count;
73 struct aarp_entry *next;
76 /* Hashed list of resolved, unresolved and proxy entries */
77 static struct aarp_entry *resolved[AARP_HASH_SIZE];
78 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
79 static struct aarp_entry *proxies[AARP_HASH_SIZE];
80 static int unresolved_count;
82 /* One lock protects it all. */
83 static DEFINE_RWLOCK(aarp_lock);
85 /* Used to walk the list and purge/kick entries. */
86 static struct timer_list aarp_timer;
89 * Delete an aarp queue
91 * Must run under aarp_lock.
93 static void __aarp_expire(struct aarp_entry *a)
95 skb_queue_purge(&a->packet_queue);
96 kfree(a);
100 * Send an aarp queue entry request
102 * Must run under aarp_lock.
104 static void __aarp_send_query(struct aarp_entry *a)
106 static unsigned char aarp_eth_multicast[ETH_ALEN] =
107 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
108 struct net_device *dev = a->dev;
109 struct elapaarp *eah;
110 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
111 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
112 struct atalk_addr *sat = atalk_find_dev_addr(dev);
114 if (!skb)
115 return;
117 if (!sat) {
118 kfree_skb(skb);
119 return;
122 /* Set up the buffer */
123 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
124 skb_reset_network_header(skb);
125 skb_reset_transport_header(skb);
126 skb_put(skb, sizeof(*eah));
127 skb->protocol = htons(ETH_P_ATALK);
128 skb->dev = dev;
129 eah = aarp_hdr(skb);
131 /* Set up the ARP */
132 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
133 eah->pa_type = htons(ETH_P_ATALK);
134 eah->hw_len = ETH_ALEN;
135 eah->pa_len = AARP_PA_ALEN;
136 eah->function = htons(AARP_REQUEST);
138 ether_addr_copy(eah->hw_src, dev->dev_addr);
140 eah->pa_src_zero = 0;
141 eah->pa_src_net = sat->s_net;
142 eah->pa_src_node = sat->s_node;
144 memset(eah->hw_dst, '\0', ETH_ALEN);
146 eah->pa_dst_zero = 0;
147 eah->pa_dst_net = a->target_addr.s_net;
148 eah->pa_dst_node = a->target_addr.s_node;
150 /* Send it */
151 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
152 /* Update the sending count */
153 a->xmit_count++;
154 a->last_sent = jiffies;
157 /* This runs under aarp_lock and in softint context, so only atomic memory
158 * allocations can be used. */
159 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
160 struct atalk_addr *them, unsigned char *sha)
162 struct elapaarp *eah;
163 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
164 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
166 if (!skb)
167 return;
169 /* Set up the buffer */
170 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
171 skb_reset_network_header(skb);
172 skb_reset_transport_header(skb);
173 skb_put(skb, sizeof(*eah));
174 skb->protocol = htons(ETH_P_ATALK);
175 skb->dev = dev;
176 eah = aarp_hdr(skb);
178 /* Set up the ARP */
179 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
180 eah->pa_type = htons(ETH_P_ATALK);
181 eah->hw_len = ETH_ALEN;
182 eah->pa_len = AARP_PA_ALEN;
183 eah->function = htons(AARP_REPLY);
185 ether_addr_copy(eah->hw_src, dev->dev_addr);
187 eah->pa_src_zero = 0;
188 eah->pa_src_net = us->s_net;
189 eah->pa_src_node = us->s_node;
191 if (!sha)
192 memset(eah->hw_dst, '\0', ETH_ALEN);
193 else
194 ether_addr_copy(eah->hw_dst, sha);
196 eah->pa_dst_zero = 0;
197 eah->pa_dst_net = them->s_net;
198 eah->pa_dst_node = them->s_node;
200 /* Send it */
201 aarp_dl->request(aarp_dl, skb, sha);
205 * Send probe frames. Called from aarp_probe_network and
206 * aarp_proxy_probe_network.
209 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
211 struct elapaarp *eah;
212 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
213 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
214 static unsigned char aarp_eth_multicast[ETH_ALEN] =
215 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
217 if (!skb)
218 return;
220 /* Set up the buffer */
221 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
222 skb_reset_network_header(skb);
223 skb_reset_transport_header(skb);
224 skb_put(skb, sizeof(*eah));
225 skb->protocol = htons(ETH_P_ATALK);
226 skb->dev = dev;
227 eah = aarp_hdr(skb);
229 /* Set up the ARP */
230 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
231 eah->pa_type = htons(ETH_P_ATALK);
232 eah->hw_len = ETH_ALEN;
233 eah->pa_len = AARP_PA_ALEN;
234 eah->function = htons(AARP_PROBE);
236 ether_addr_copy(eah->hw_src, dev->dev_addr);
238 eah->pa_src_zero = 0;
239 eah->pa_src_net = us->s_net;
240 eah->pa_src_node = us->s_node;
242 memset(eah->hw_dst, '\0', ETH_ALEN);
244 eah->pa_dst_zero = 0;
245 eah->pa_dst_net = us->s_net;
246 eah->pa_dst_node = us->s_node;
248 /* Send it */
249 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
253 * Handle an aarp timer expire
255 * Must run under the aarp_lock.
258 static void __aarp_expire_timer(struct aarp_entry **n)
260 struct aarp_entry *t;
262 while (*n)
263 /* Expired ? */
264 if (time_after(jiffies, (*n)->expires_at)) {
265 t = *n;
266 *n = (*n)->next;
267 __aarp_expire(t);
268 } else
269 n = &((*n)->next);
273 * Kick all pending requests 5 times a second.
275 * Must run under the aarp_lock.
277 static void __aarp_kick(struct aarp_entry **n)
279 struct aarp_entry *t;
281 while (*n)
282 /* Expired: if this will be the 11th tx, we delete instead. */
283 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
284 t = *n;
285 *n = (*n)->next;
286 __aarp_expire(t);
287 } else {
288 __aarp_send_query(*n);
289 n = &((*n)->next);
294 * A device has gone down. Take all entries referring to the device
295 * and remove them.
297 * Must run under the aarp_lock.
299 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
301 struct aarp_entry *t;
303 while (*n)
304 if ((*n)->dev == dev) {
305 t = *n;
306 *n = (*n)->next;
307 __aarp_expire(t);
308 } else
309 n = &((*n)->next);
312 /* Handle the timer event */
313 static void aarp_expire_timeout(unsigned long unused)
315 int ct;
317 write_lock_bh(&aarp_lock);
319 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
320 __aarp_expire_timer(&resolved[ct]);
321 __aarp_kick(&unresolved[ct]);
322 __aarp_expire_timer(&unresolved[ct]);
323 __aarp_expire_timer(&proxies[ct]);
326 write_unlock_bh(&aarp_lock);
327 mod_timer(&aarp_timer, jiffies +
328 (unresolved_count ? sysctl_aarp_tick_time :
329 sysctl_aarp_expiry_time));
332 /* Network device notifier chain handler. */
333 static int aarp_device_event(struct notifier_block *this, unsigned long event,
334 void *ptr)
336 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
337 int ct;
339 if (!net_eq(dev_net(dev), &init_net))
340 return NOTIFY_DONE;
342 if (event == NETDEV_DOWN) {
343 write_lock_bh(&aarp_lock);
345 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
346 __aarp_expire_device(&resolved[ct], dev);
347 __aarp_expire_device(&unresolved[ct], dev);
348 __aarp_expire_device(&proxies[ct], dev);
351 write_unlock_bh(&aarp_lock);
353 return NOTIFY_DONE;
356 /* Expire all entries in a hash chain */
357 static void __aarp_expire_all(struct aarp_entry **n)
359 struct aarp_entry *t;
361 while (*n) {
362 t = *n;
363 *n = (*n)->next;
364 __aarp_expire(t);
368 /* Cleanup all hash chains -- module unloading */
369 static void aarp_purge(void)
371 int ct;
373 write_lock_bh(&aarp_lock);
374 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
375 __aarp_expire_all(&resolved[ct]);
376 __aarp_expire_all(&unresolved[ct]);
377 __aarp_expire_all(&proxies[ct]);
379 write_unlock_bh(&aarp_lock);
383 * Create a new aarp entry. This must use GFP_ATOMIC because it
384 * runs while holding spinlocks.
386 static struct aarp_entry *aarp_alloc(void)
388 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
390 if (a)
391 skb_queue_head_init(&a->packet_queue);
392 return a;
396 * Find an entry. We might return an expired but not yet purged entry. We
397 * don't care as it will do no harm.
399 * This must run under the aarp_lock.
401 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
402 struct net_device *dev,
403 struct atalk_addr *sat)
405 while (list) {
406 if (list->target_addr.s_net == sat->s_net &&
407 list->target_addr.s_node == sat->s_node &&
408 list->dev == dev)
409 break;
410 list = list->next;
413 return list;
416 /* Called from the DDP code, and thus must be exported. */
417 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
419 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
420 struct aarp_entry *a;
422 write_lock_bh(&aarp_lock);
424 a = __aarp_find_entry(proxies[hash], dev, sa);
425 if (a)
426 a->expires_at = jiffies - 1;
428 write_unlock_bh(&aarp_lock);
431 /* This must run under aarp_lock. */
432 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
433 struct atalk_addr *sa)
435 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
436 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
438 return a ? sa : NULL;
442 * Probe a Phase 1 device or a device that requires its Net:Node to
443 * be set via an ioctl.
445 static void aarp_send_probe_phase1(struct atalk_iface *iface)
447 struct ifreq atreq;
448 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
449 const struct net_device_ops *ops = iface->dev->netdev_ops;
451 sa->sat_addr.s_node = iface->address.s_node;
452 sa->sat_addr.s_net = ntohs(iface->address.s_net);
454 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
455 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
456 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
457 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
458 iface->address.s_node != sa->sat_addr.s_node)
459 iface->status |= ATIF_PROBE_FAIL;
461 iface->address.s_net = htons(sa->sat_addr.s_net);
462 iface->address.s_node = sa->sat_addr.s_node;
467 void aarp_probe_network(struct atalk_iface *atif)
469 if (atif->dev->type == ARPHRD_LOCALTLK ||
470 atif->dev->type == ARPHRD_PPP)
471 aarp_send_probe_phase1(atif);
472 else {
473 unsigned int count;
475 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
476 aarp_send_probe(atif->dev, &atif->address);
478 /* Defer 1/10th */
479 msleep(100);
481 if (atif->status & ATIF_PROBE_FAIL)
482 break;
487 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
489 int hash, retval = -EPROTONOSUPPORT;
490 struct aarp_entry *entry;
491 unsigned int count;
494 * we don't currently support LocalTalk or PPP for proxy AARP;
495 * if someone wants to try and add it, have fun
497 if (atif->dev->type == ARPHRD_LOCALTLK ||
498 atif->dev->type == ARPHRD_PPP)
499 goto out;
502 * create a new AARP entry with the flags set to be published --
503 * we need this one to hang around even if it's in use
505 entry = aarp_alloc();
506 retval = -ENOMEM;
507 if (!entry)
508 goto out;
510 entry->expires_at = -1;
511 entry->status = ATIF_PROBE;
512 entry->target_addr.s_node = sa->s_node;
513 entry->target_addr.s_net = sa->s_net;
514 entry->dev = atif->dev;
516 write_lock_bh(&aarp_lock);
518 hash = sa->s_node % (AARP_HASH_SIZE - 1);
519 entry->next = proxies[hash];
520 proxies[hash] = entry;
522 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
523 aarp_send_probe(atif->dev, sa);
525 /* Defer 1/10th */
526 write_unlock_bh(&aarp_lock);
527 msleep(100);
528 write_lock_bh(&aarp_lock);
530 if (entry->status & ATIF_PROBE_FAIL)
531 break;
534 if (entry->status & ATIF_PROBE_FAIL) {
535 entry->expires_at = jiffies - 1; /* free the entry */
536 retval = -EADDRINUSE; /* return network full */
537 } else { /* clear the probing flag */
538 entry->status &= ~ATIF_PROBE;
539 retval = 1;
542 write_unlock_bh(&aarp_lock);
543 out:
544 return retval;
547 /* Send a DDP frame */
548 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
549 struct atalk_addr *sa, void *hwaddr)
551 static char ddp_eth_multicast[ETH_ALEN] =
552 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
553 int hash;
554 struct aarp_entry *a;
556 skb_reset_network_header(skb);
558 /* Check for LocalTalk first */
559 if (dev->type == ARPHRD_LOCALTLK) {
560 struct atalk_addr *at = atalk_find_dev_addr(dev);
561 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
562 int ft = 2;
565 * Compressible ?
567 * IFF: src_net == dest_net == device_net
568 * (zero matches anything)
571 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
572 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
573 skb_pull(skb, sizeof(*ddp) - 4);
576 * The upper two remaining bytes are the port
577 * numbers we just happen to need. Now put the
578 * length in the lower two.
580 *((__be16 *)skb->data) = htons(skb->len);
581 ft = 1;
584 * Nice and easy. No AARP type protocols occur here so we can
585 * just shovel it out with a 3 byte LLAP header
588 skb_push(skb, 3);
589 skb->data[0] = sa->s_node;
590 skb->data[1] = at->s_node;
591 skb->data[2] = ft;
592 skb->dev = dev;
593 goto sendit;
596 /* On a PPP link we neither compress nor aarp. */
597 if (dev->type == ARPHRD_PPP) {
598 skb->protocol = htons(ETH_P_PPPTALK);
599 skb->dev = dev;
600 goto sendit;
603 /* Non ELAP we cannot do. */
604 if (dev->type != ARPHRD_ETHER)
605 goto free_it;
607 skb->dev = dev;
608 skb->protocol = htons(ETH_P_ATALK);
609 hash = sa->s_node % (AARP_HASH_SIZE - 1);
611 /* Do we have a resolved entry? */
612 if (sa->s_node == ATADDR_BCAST) {
613 /* Send it */
614 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
615 goto sent;
618 write_lock_bh(&aarp_lock);
619 a = __aarp_find_entry(resolved[hash], dev, sa);
621 if (a) { /* Return 1 and fill in the address */
622 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
623 ddp_dl->request(ddp_dl, skb, a->hwaddr);
624 write_unlock_bh(&aarp_lock);
625 goto sent;
628 /* Do we have an unresolved entry: This is the less common path */
629 a = __aarp_find_entry(unresolved[hash], dev, sa);
630 if (a) { /* Queue onto the unresolved queue */
631 skb_queue_tail(&a->packet_queue, skb);
632 goto out_unlock;
635 /* Allocate a new entry */
636 a = aarp_alloc();
637 if (!a) {
638 /* Whoops slipped... good job it's an unreliable protocol 8) */
639 write_unlock_bh(&aarp_lock);
640 goto free_it;
643 /* Set up the queue */
644 skb_queue_tail(&a->packet_queue, skb);
645 a->expires_at = jiffies + sysctl_aarp_resolve_time;
646 a->dev = dev;
647 a->next = unresolved[hash];
648 a->target_addr = *sa;
649 a->xmit_count = 0;
650 unresolved[hash] = a;
651 unresolved_count++;
653 /* Send an initial request for the address */
654 __aarp_send_query(a);
657 * Switch to fast timer if needed (That is if this is the first
658 * unresolved entry to get added)
661 if (unresolved_count == 1)
662 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
664 /* Now finally, it is safe to drop the lock. */
665 out_unlock:
666 write_unlock_bh(&aarp_lock);
668 /* Tell the ddp layer we have taken over for this frame. */
669 goto sent;
671 sendit:
672 if (skb->sk)
673 skb->priority = skb->sk->sk_priority;
674 if (dev_queue_xmit(skb))
675 goto drop;
676 sent:
677 return NET_XMIT_SUCCESS;
678 free_it:
679 kfree_skb(skb);
680 drop:
681 return NET_XMIT_DROP;
683 EXPORT_SYMBOL(aarp_send_ddp);
686 * An entry in the aarp unresolved queue has become resolved. Send
687 * all the frames queued under it.
689 * Must run under aarp_lock.
691 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
692 int hash)
694 struct sk_buff *skb;
696 while (*list)
697 if (*list == a) {
698 unresolved_count--;
699 *list = a->next;
701 /* Move into the resolved list */
702 a->next = resolved[hash];
703 resolved[hash] = a;
705 /* Kick frames off */
706 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
707 a->expires_at = jiffies +
708 sysctl_aarp_expiry_time * 10;
709 ddp_dl->request(ddp_dl, skb, a->hwaddr);
711 } else
712 list = &((*list)->next);
716 * This is called by the SNAP driver whenever we see an AARP SNAP
717 * frame. We currently only support Ethernet.
719 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
720 struct packet_type *pt, struct net_device *orig_dev)
722 struct elapaarp *ea = aarp_hdr(skb);
723 int hash, ret = 0;
724 __u16 function;
725 struct aarp_entry *a;
726 struct atalk_addr sa, *ma, da;
727 struct atalk_iface *ifa;
729 if (!net_eq(dev_net(dev), &init_net))
730 goto out0;
732 /* We only do Ethernet SNAP AARP. */
733 if (dev->type != ARPHRD_ETHER)
734 goto out0;
736 /* Frame size ok? */
737 if (!skb_pull(skb, sizeof(*ea)))
738 goto out0;
740 function = ntohs(ea->function);
742 /* Sanity check fields. */
743 if (function < AARP_REQUEST || function > AARP_PROBE ||
744 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
745 ea->pa_src_zero || ea->pa_dst_zero)
746 goto out0;
748 /* Looks good. */
749 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
751 /* Build an address. */
752 sa.s_node = ea->pa_src_node;
753 sa.s_net = ea->pa_src_net;
755 /* Process the packet. Check for replies of me. */
756 ifa = atalk_find_dev(dev);
757 if (!ifa)
758 goto out1;
760 if (ifa->status & ATIF_PROBE &&
761 ifa->address.s_node == ea->pa_dst_node &&
762 ifa->address.s_net == ea->pa_dst_net) {
763 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
764 goto out1;
767 /* Check for replies of proxy AARP entries */
768 da.s_node = ea->pa_dst_node;
769 da.s_net = ea->pa_dst_net;
771 write_lock_bh(&aarp_lock);
772 a = __aarp_find_entry(proxies[hash], dev, &da);
774 if (a && a->status & ATIF_PROBE) {
775 a->status |= ATIF_PROBE_FAIL;
777 * we do not respond to probe or request packets for
778 * this address while we are probing this address
780 goto unlock;
783 switch (function) {
784 case AARP_REPLY:
785 if (!unresolved_count) /* Speed up */
786 break;
788 /* Find the entry. */
789 a = __aarp_find_entry(unresolved[hash], dev, &sa);
790 if (!a || dev != a->dev)
791 break;
793 /* We can fill one in - this is good. */
794 ether_addr_copy(a->hwaddr, ea->hw_src);
795 __aarp_resolved(&unresolved[hash], a, hash);
796 if (!unresolved_count)
797 mod_timer(&aarp_timer,
798 jiffies + sysctl_aarp_expiry_time);
799 break;
801 case AARP_REQUEST:
802 case AARP_PROBE:
805 * If it is my address set ma to my address and reply.
806 * We can treat probe and request the same. Probe
807 * simply means we shouldn't cache the querying host,
808 * as in a probe they are proposing an address not
809 * using one.
811 * Support for proxy-AARP added. We check if the
812 * address is one of our proxies before we toss the
813 * packet out.
816 sa.s_node = ea->pa_dst_node;
817 sa.s_net = ea->pa_dst_net;
819 /* See if we have a matching proxy. */
820 ma = __aarp_proxy_find(dev, &sa);
821 if (!ma)
822 ma = &ifa->address;
823 else { /* We need to make a copy of the entry. */
824 da.s_node = sa.s_node;
825 da.s_net = sa.s_net;
826 ma = &da;
829 if (function == AARP_PROBE) {
831 * A probe implies someone trying to get an
832 * address. So as a precaution flush any
833 * entries we have for this address.
835 a = __aarp_find_entry(resolved[sa.s_node %
836 (AARP_HASH_SIZE - 1)],
837 skb->dev, &sa);
840 * Make it expire next tick - that avoids us
841 * getting into a probe/flush/learn/probe/
842 * flush/learn cycle during probing of a slow
843 * to respond host addr.
845 if (a) {
846 a->expires_at = jiffies - 1;
847 mod_timer(&aarp_timer, jiffies +
848 sysctl_aarp_tick_time);
852 if (sa.s_node != ma->s_node)
853 break;
855 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
856 break;
858 sa.s_node = ea->pa_src_node;
859 sa.s_net = ea->pa_src_net;
861 /* aarp_my_address has found the address to use for us.
863 aarp_send_reply(dev, ma, &sa, ea->hw_src);
864 break;
867 unlock:
868 write_unlock_bh(&aarp_lock);
869 out1:
870 ret = 1;
871 out0:
872 kfree_skb(skb);
873 return ret;
876 static struct notifier_block aarp_notifier = {
877 .notifier_call = aarp_device_event,
880 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
882 void __init aarp_proto_init(void)
884 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
885 if (!aarp_dl)
886 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
887 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
888 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
889 add_timer(&aarp_timer);
890 register_netdevice_notifier(&aarp_notifier);
893 /* Remove the AARP entries associated with a device. */
894 void aarp_device_down(struct net_device *dev)
896 int ct;
898 write_lock_bh(&aarp_lock);
900 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
901 __aarp_expire_device(&resolved[ct], dev);
902 __aarp_expire_device(&unresolved[ct], dev);
903 __aarp_expire_device(&proxies[ct], dev);
906 write_unlock_bh(&aarp_lock);
909 #ifdef CONFIG_PROC_FS
910 struct aarp_iter_state {
911 int bucket;
912 struct aarp_entry **table;
916 * Get the aarp entry that is in the chain described
917 * by the iterator.
918 * If pos is set then skip till that index.
919 * pos = 1 is the first entry
921 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
923 int ct = iter->bucket;
924 struct aarp_entry **table = iter->table;
925 loff_t off = 0;
926 struct aarp_entry *entry;
928 rescan:
929 while (ct < AARP_HASH_SIZE) {
930 for (entry = table[ct]; entry; entry = entry->next) {
931 if (!pos || ++off == *pos) {
932 iter->table = table;
933 iter->bucket = ct;
934 return entry;
937 ++ct;
940 if (table == resolved) {
941 ct = 0;
942 table = unresolved;
943 goto rescan;
945 if (table == unresolved) {
946 ct = 0;
947 table = proxies;
948 goto rescan;
950 return NULL;
953 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
954 __acquires(aarp_lock)
956 struct aarp_iter_state *iter = seq->private;
958 read_lock_bh(&aarp_lock);
959 iter->table = resolved;
960 iter->bucket = 0;
962 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
965 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
967 struct aarp_entry *entry = v;
968 struct aarp_iter_state *iter = seq->private;
970 ++*pos;
972 /* first line after header */
973 if (v == SEQ_START_TOKEN)
974 entry = iter_next(iter, NULL);
976 /* next entry in current bucket */
977 else if (entry->next)
978 entry = entry->next;
980 /* next bucket or table */
981 else {
982 ++iter->bucket;
983 entry = iter_next(iter, NULL);
985 return entry;
988 static void aarp_seq_stop(struct seq_file *seq, void *v)
989 __releases(aarp_lock)
991 read_unlock_bh(&aarp_lock);
994 static const char *dt2str(unsigned long ticks)
996 static char buf[32];
998 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
1000 return buf;
1003 static int aarp_seq_show(struct seq_file *seq, void *v)
1005 struct aarp_iter_state *iter = seq->private;
1006 struct aarp_entry *entry = v;
1007 unsigned long now = jiffies;
1009 if (v == SEQ_START_TOKEN)
1010 seq_puts(seq,
1011 "Address Interface Hardware Address"
1012 " Expires LastSend Retry Status\n");
1013 else {
1014 seq_printf(seq, "%04X:%02X %-12s",
1015 ntohs(entry->target_addr.s_net),
1016 (unsigned int) entry->target_addr.s_node,
1017 entry->dev ? entry->dev->name : "????");
1018 seq_printf(seq, "%pM", entry->hwaddr);
1019 seq_printf(seq, " %8s",
1020 dt2str((long)entry->expires_at - (long)now));
1021 if (iter->table == unresolved)
1022 seq_printf(seq, " %8s %6hu",
1023 dt2str(now - entry->last_sent),
1024 entry->xmit_count);
1025 else
1026 seq_puts(seq, " ");
1027 seq_printf(seq, " %s\n",
1028 (iter->table == resolved) ? "resolved"
1029 : (iter->table == unresolved) ? "unresolved"
1030 : (iter->table == proxies) ? "proxies"
1031 : "unknown");
1033 return 0;
1036 static const struct seq_operations aarp_seq_ops = {
1037 .start = aarp_seq_start,
1038 .next = aarp_seq_next,
1039 .stop = aarp_seq_stop,
1040 .show = aarp_seq_show,
1043 static int aarp_seq_open(struct inode *inode, struct file *file)
1045 return seq_open_private(file, &aarp_seq_ops,
1046 sizeof(struct aarp_iter_state));
1049 const struct file_operations atalk_seq_arp_fops = {
1050 .owner = THIS_MODULE,
1051 .open = aarp_seq_open,
1052 .read = seq_read,
1053 .llseek = seq_lseek,
1054 .release = seq_release_private,
1056 #endif
1058 /* General module cleanup. Called from cleanup_module() in ddp.c. */
1059 void aarp_cleanup_module(void)
1061 del_timer_sync(&aarp_timer);
1062 unregister_netdevice_notifier(&aarp_notifier);
1063 unregister_snap_client(aarp_dl);
1064 aarp_purge();