affs: fix remount failure when there are no options changed
[linux-2.6-block.git] / net / appletalk / aarp.c
CommitLineData
1da177e4
LT
1/*
2 * AARP: An implementation of the AppleTalk AARP protocol for
3 * Ethernet 'ELAP'.
4 *
5 * Alan Cox <Alan.Cox@linux.org>
6 *
7 * This doesn't fit cleanly with the IP arp. Potentially we can use
8 * the generic neighbour discovery code to clean this up.
9 *
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.
15 *
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.
20 *
21 *
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
29 *
30 */
31
1da177e4 32#include <linux/if_arp.h>
5a0e3ad6 33#include <linux/slab.h>
1da177e4
LT
34#include <net/sock.h>
35#include <net/datalink.h>
36#include <net/psnap.h>
37#include <linux/atalk.h>
285b3afe 38#include <linux/delay.h>
1da177e4
LT
39#include <linux/init.h>
40#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
bc3b2d7f 42#include <linux/export.h>
9be68c1a 43#include <linux/etherdevice.h>
1da177e4
LT
44
45int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
46int sysctl_aarp_tick_time = AARP_TICK_TIME;
47int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
48int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
49
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
62 */
63struct 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;
90ccb6aa 71 char hwaddr[ETH_ALEN];
1da177e4
LT
72 unsigned short xmit_count;
73 struct aarp_entry *next;
74};
75
76/* Hashed list of resolved, unresolved and proxy entries */
77static struct aarp_entry *resolved[AARP_HASH_SIZE];
78static struct aarp_entry *unresolved[AARP_HASH_SIZE];
79static struct aarp_entry *proxies[AARP_HASH_SIZE];
80static int unresolved_count;
81
82/* One lock protects it all. */
83static DEFINE_RWLOCK(aarp_lock);
84
85/* Used to walk the list and purge/kick entries. */
86static struct timer_list aarp_timer;
87
88/*
89 * Delete an aarp queue
90 *
91 * Must run under aarp_lock.
92 */
93static void __aarp_expire(struct aarp_entry *a)
94{
95 skb_queue_purge(&a->packet_queue);
96 kfree(a);
97}
98
99/*
100 * Send an aarp queue entry request
101 *
102 * Must run under aarp_lock.
103 */
104static void __aarp_send_query(struct aarp_entry *a)
105{
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);
113
114 if (!skb)
115 return;
116
117 if (!sat) {
118 kfree_skb(skb);
119 return;
120 }
121
122 /* Set up the buffer */
123 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
7e28ecc2 124 skb_reset_network_header(skb);
badff6d0 125 skb_reset_transport_header(skb);
7e28ecc2 126 skb_put(skb, sizeof(*eah));
1da177e4
LT
127 skb->protocol = htons(ETH_P_ATALK);
128 skb->dev = dev;
129 eah = aarp_hdr(skb);
130
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);
137
90ccb6aa 138 ether_addr_copy(eah->hw_src, dev->dev_addr);
1da177e4
LT
139
140 eah->pa_src_zero = 0;
141 eah->pa_src_net = sat->s_net;
142 eah->pa_src_node = sat->s_node;
143
1a73de07 144 eth_zero_addr(eah->hw_dst);
1da177e4
LT
145
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;
149
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;
155}
156
157/* This runs under aarp_lock and in softint context, so only atomic memory
158 * allocations can be used. */
159static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
160 struct atalk_addr *them, unsigned char *sha)
161{
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);
165
166 if (!skb)
167 return;
168
169 /* Set up the buffer */
170 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
7e28ecc2 171 skb_reset_network_header(skb);
badff6d0 172 skb_reset_transport_header(skb);
7e28ecc2 173 skb_put(skb, sizeof(*eah));
1da177e4
LT
174 skb->protocol = htons(ETH_P_ATALK);
175 skb->dev = dev;
176 eah = aarp_hdr(skb);
177
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);
184
90ccb6aa 185 ether_addr_copy(eah->hw_src, dev->dev_addr);
1da177e4
LT
186
187 eah->pa_src_zero = 0;
188 eah->pa_src_net = us->s_net;
189 eah->pa_src_node = us->s_node;
190
191 if (!sha)
1a73de07 192 eth_zero_addr(eah->hw_dst);
1da177e4 193 else
90ccb6aa 194 ether_addr_copy(eah->hw_dst, sha);
1da177e4
LT
195
196 eah->pa_dst_zero = 0;
197 eah->pa_dst_net = them->s_net;
198 eah->pa_dst_node = them->s_node;
199
200 /* Send it */
201 aarp_dl->request(aarp_dl, skb, sha);
202}
203
204/*
205 * Send probe frames. Called from aarp_probe_network and
206 * aarp_proxy_probe_network.
207 */
208
209static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
210{
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 };
216
217 if (!skb)
218 return;
219
220 /* Set up the buffer */
221 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
7e28ecc2 222 skb_reset_network_header(skb);
badff6d0 223 skb_reset_transport_header(skb);
7e28ecc2 224 skb_put(skb, sizeof(*eah));
1da177e4
LT
225 skb->protocol = htons(ETH_P_ATALK);
226 skb->dev = dev;
227 eah = aarp_hdr(skb);
228
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);
235
90ccb6aa 236 ether_addr_copy(eah->hw_src, dev->dev_addr);
1da177e4
LT
237
238 eah->pa_src_zero = 0;
239 eah->pa_src_net = us->s_net;
240 eah->pa_src_node = us->s_node;
241
1a73de07 242 eth_zero_addr(eah->hw_dst);
1da177e4
LT
243
244 eah->pa_dst_zero = 0;
245 eah->pa_dst_net = us->s_net;
246 eah->pa_dst_node = us->s_node;
247
248 /* Send it */
249 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
250}
251
252/*
253 * Handle an aarp timer expire
254 *
255 * Must run under the aarp_lock.
256 */
257
258static void __aarp_expire_timer(struct aarp_entry **n)
259{
260 struct aarp_entry *t;
261
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);
270}
271
272/*
273 * Kick all pending requests 5 times a second.
274 *
275 * Must run under the aarp_lock.
276 */
277static void __aarp_kick(struct aarp_entry **n)
278{
279 struct aarp_entry *t;
280
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);
290 }
291}
292
293/*
294 * A device has gone down. Take all entries referring to the device
295 * and remove them.
296 *
297 * Must run under the aarp_lock.
298 */
299static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
300{
301 struct aarp_entry *t;
302
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);
310}
311
312/* Handle the timer event */
313static void aarp_expire_timeout(unsigned long unused)
314{
315 int ct;
316
317 write_lock_bh(&aarp_lock);
318
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]);
324 }
325
326 write_unlock_bh(&aarp_lock);
327 mod_timer(&aarp_timer, jiffies +
328 (unresolved_count ? sysctl_aarp_tick_time :
329 sysctl_aarp_expiry_time));
330}
331
332/* Network device notifier chain handler. */
333static int aarp_device_event(struct notifier_block *this, unsigned long event,
334 void *ptr)
335{
351638e7 336 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1da177e4
LT
337 int ct;
338
721499e8 339 if (!net_eq(dev_net(dev), &init_net))
e9dc8653
EB
340 return NOTIFY_DONE;
341
1da177e4
LT
342 if (event == NETDEV_DOWN) {
343 write_lock_bh(&aarp_lock);
344
345 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
890d52d3
EB
346 __aarp_expire_device(&resolved[ct], dev);
347 __aarp_expire_device(&unresolved[ct], dev);
348 __aarp_expire_device(&proxies[ct], dev);
1da177e4
LT
349 }
350
351 write_unlock_bh(&aarp_lock);
352 }
353 return NOTIFY_DONE;
354}
355
356/* Expire all entries in a hash chain */
357static void __aarp_expire_all(struct aarp_entry **n)
358{
359 struct aarp_entry *t;
360
361 while (*n) {
362 t = *n;
363 *n = (*n)->next;
364 __aarp_expire(t);
365 }
366}
367
368/* Cleanup all hash chains -- module unloading */
369static void aarp_purge(void)
370{
371 int ct;
372
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]);
378 }
379 write_unlock_bh(&aarp_lock);
380}
381
382/*
383 * Create a new aarp entry. This must use GFP_ATOMIC because it
384 * runs while holding spinlocks.
385 */
386static struct aarp_entry *aarp_alloc(void)
387{
388 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
389
390 if (a)
391 skb_queue_head_init(&a->packet_queue);
392 return a;
393}
394
395/*
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.
398 *
399 * This must run under the aarp_lock.
400 */
401static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
402 struct net_device *dev,
403 struct atalk_addr *sat)
404{
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;
411 }
412
413 return list;
414}
415
416/* Called from the DDP code, and thus must be exported. */
417void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
418{
419 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
420 struct aarp_entry *a;
421
422 write_lock_bh(&aarp_lock);
423
424 a = __aarp_find_entry(proxies[hash], dev, sa);
425 if (a)
426 a->expires_at = jiffies - 1;
427
428 write_unlock_bh(&aarp_lock);
429}
430
431/* This must run under aarp_lock. */
432static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
433 struct atalk_addr *sa)
434{
435 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
436 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
437
438 return a ? sa : NULL;
439}
440
441/*
442 * Probe a Phase 1 device or a device that requires its Net:Node to
443 * be set via an ioctl.
444 */
445static void aarp_send_probe_phase1(struct atalk_iface *iface)
446{
447 struct ifreq atreq;
448 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
03b35ccb 449 const struct net_device_ops *ops = iface->dev->netdev_ops;
1da177e4
LT
450
451 sa->sat_addr.s_node = iface->address.s_node;
452 sa->sat_addr.s_net = ntohs(iface->address.s_net);
453
454 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
03b35ccb
SH
455 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
456 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
1da177e4
LT
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;
460
461 iface->address.s_net = htons(sa->sat_addr.s_net);
462 iface->address.s_node = sa->sat_addr.s_node;
463 }
464}
465
466
467void aarp_probe_network(struct atalk_iface *atif)
468{
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;
474
475 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
476 aarp_send_probe(atif->dev, &atif->address);
477
478 /* Defer 1/10th */
285b3afe 479 msleep(100);
1da177e4
LT
480
481 if (atif->status & ATIF_PROBE_FAIL)
482 break;
483 }
484 }
485}
486
487int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
488{
489 int hash, retval = -EPROTONOSUPPORT;
490 struct aarp_entry *entry;
491 unsigned int count;
492
493 /*
494 * we don't currently support LocalTalk or PPP for proxy AARP;
495 * if someone wants to try and add it, have fun
496 */
497 if (atif->dev->type == ARPHRD_LOCALTLK ||
498 atif->dev->type == ARPHRD_PPP)
499 goto out;
500
501 /*
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
504 */
505 entry = aarp_alloc();
506 retval = -ENOMEM;
507 if (!entry)
508 goto out;
509
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;
515
516 write_lock_bh(&aarp_lock);
517
518 hash = sa->s_node % (AARP_HASH_SIZE - 1);
519 entry->next = proxies[hash];
520 proxies[hash] = entry;
521
522 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
523 aarp_send_probe(atif->dev, sa);
524
525 /* Defer 1/10th */
1da177e4 526 write_unlock_bh(&aarp_lock);
285b3afe 527 msleep(100);
1da177e4
LT
528 write_lock_bh(&aarp_lock);
529
530 if (entry->status & ATIF_PROBE_FAIL)
531 break;
532 }
533
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;
540 }
541
542 write_unlock_bh(&aarp_lock);
543out:
544 return retval;
545}
546
547/* Send a DDP frame */
548int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
549 struct atalk_addr *sa, void *hwaddr)
550{
551 static char ddp_eth_multicast[ETH_ALEN] =
552 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
553 int hash;
554 struct aarp_entry *a;
555
c1d2bbe1 556 skb_reset_network_header(skb);
1da177e4
LT
557
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;
563
564 /*
565 * Compressible ?
566 *
567 * IFF: src_net == dest_net == device_net
568 * (zero matches anything)
569 */
570
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);
574
575 /*
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.
579 */
f6e276ee 580 *((__be16 *)skb->data) = htons(skb->len);
1da177e4
LT
581 ft = 1;
582 }
583 /*
584 * Nice and easy. No AARP type protocols occur here so we can
585 * just shovel it out with a 3 byte LLAP header
586 */
587
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;
594 }
595
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;
601 }
602
603 /* Non ELAP we cannot do. */
604 if (dev->type != ARPHRD_ETHER)
ffcfb8db 605 goto free_it;
1da177e4
LT
606
607 skb->dev = dev;
608 skb->protocol = htons(ETH_P_ATALK);
609 hash = sa->s_node % (AARP_HASH_SIZE - 1);
610
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;
616 }
617
618 write_lock_bh(&aarp_lock);
619 a = __aarp_find_entry(resolved[hash], dev, sa);
620
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;
626 }
627
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;
633 }
634
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);
ffcfb8db 640 goto free_it;
1da177e4
LT
641 }
642
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++;
652
653 /* Send an initial request for the address */
654 __aarp_send_query(a);
655
656 /*
657 * Switch to fast timer if needed (That is if this is the first
658 * unresolved entry to get added)
659 */
660
661 if (unresolved_count == 1)
662 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
663
664 /* Now finally, it is safe to drop the lock. */
665out_unlock:
666 write_unlock_bh(&aarp_lock);
667
668 /* Tell the ddp layer we have taken over for this frame. */
ffcfb8db 669 goto sent;
1da177e4
LT
670
671sendit:
672 if (skb->sk)
673 skb->priority = skb->sk->sk_priority;
ffcfb8db
ACM
674 if (dev_queue_xmit(skb))
675 goto drop;
1da177e4 676sent:
ffcfb8db
ACM
677 return NET_XMIT_SUCCESS;
678free_it:
679 kfree_skb(skb);
680drop:
681 return NET_XMIT_DROP;
1da177e4 682}
ffcfb8db 683EXPORT_SYMBOL(aarp_send_ddp);
1da177e4
LT
684
685/*
686 * An entry in the aarp unresolved queue has become resolved. Send
687 * all the frames queued under it.
688 *
689 * Must run under aarp_lock.
690 */
691static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
692 int hash)
693{
694 struct sk_buff *skb;
695
696 while (*list)
697 if (*list == a) {
698 unresolved_count--;
699 *list = a->next;
700
701 /* Move into the resolved list */
702 a->next = resolved[hash];
703 resolved[hash] = a;
704
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);
710 }
711 } else
712 list = &((*list)->next);
713}
714
715/*
716 * This is called by the SNAP driver whenever we see an AARP SNAP
717 * frame. We currently only support Ethernet.
718 */
719static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
f2ccd8fa 720 struct packet_type *pt, struct net_device *orig_dev)
1da177e4
LT
721{
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;
728
721499e8 729 if (!net_eq(dev_net(dev), &init_net))
e730c155
EB
730 goto out0;
731
1da177e4
LT
732 /* We only do Ethernet SNAP AARP. */
733 if (dev->type != ARPHRD_ETHER)
734 goto out0;
735
736 /* Frame size ok? */
737 if (!skb_pull(skb, sizeof(*ea)))
738 goto out0;
739
740 function = ntohs(ea->function);
741
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;
747
748 /* Looks good. */
749 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
750
751 /* Build an address. */
752 sa.s_node = ea->pa_src_node;
753 sa.s_net = ea->pa_src_net;
754
755 /* Process the packet. Check for replies of me. */
756 ifa = atalk_find_dev(dev);
757 if (!ifa)
758 goto out1;
759
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;
765 }
766
767 /* Check for replies of proxy AARP entries */
768 da.s_node = ea->pa_dst_node;
769 da.s_net = ea->pa_dst_net;
770
771 write_lock_bh(&aarp_lock);
772 a = __aarp_find_entry(proxies[hash], dev, &da);
773
774 if (a && a->status & ATIF_PROBE) {
775 a->status |= ATIF_PROBE_FAIL;
776 /*
777 * we do not respond to probe or request packets for
778 * this address while we are probing this address
779 */
780 goto unlock;
781 }
782
783 switch (function) {
4a9e4b09
JP
784 case AARP_REPLY:
785 if (!unresolved_count) /* Speed up */
786 break;
1da177e4 787
4a9e4b09
JP
788 /* Find the entry. */
789 a = __aarp_find_entry(unresolved[hash], dev, &sa);
790 if (!a || dev != a->dev)
1da177e4
LT
791 break;
792
4a9e4b09 793 /* We can fill one in - this is good. */
90ccb6aa 794 ether_addr_copy(a->hwaddr, ea->hw_src);
4a9e4b09
JP
795 __aarp_resolved(&unresolved[hash], a, hash);
796 if (!unresolved_count)
797 mod_timer(&aarp_timer,
798 jiffies + sysctl_aarp_expiry_time);
799 break;
800
801 case AARP_REQUEST:
802 case AARP_PROBE:
803
804 /*
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.
810 *
811 * Support for proxy-AARP added. We check if the
812 * address is one of our proxies before we toss the
813 * packet out.
814 */
815
816 sa.s_node = ea->pa_dst_node;
817 sa.s_net = ea->pa_dst_net;
818
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;
827 }
1da177e4 828
4a9e4b09 829 if (function == AARP_PROBE) {
1da177e4 830 /*
4a9e4b09
JP
831 * A probe implies someone trying to get an
832 * address. So as a precaution flush any
833 * entries we have for this address.
1da177e4 834 */
4a9e4b09
JP
835 a = __aarp_find_entry(resolved[sa.s_node %
836 (AARP_HASH_SIZE - 1)],
837 skb->dev, &sa);
1da177e4 838
4a9e4b09
JP
839 /*
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.
844 */
845 if (a) {
846 a->expires_at = jiffies - 1;
847 mod_timer(&aarp_timer, jiffies +
848 sysctl_aarp_tick_time);
1da177e4 849 }
4a9e4b09 850 }
1da177e4 851
4a9e4b09
JP
852 if (sa.s_node != ma->s_node)
853 break;
1da177e4 854
4a9e4b09
JP
855 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
856 break;
1da177e4 857
4a9e4b09
JP
858 sa.s_node = ea->pa_src_node;
859 sa.s_net = ea->pa_src_net;
1da177e4 860
4a9e4b09
JP
861 /* aarp_my_address has found the address to use for us.
862 */
863 aarp_send_reply(dev, ma, &sa, ea->hw_src);
864 break;
1da177e4
LT
865 }
866
867unlock:
868 write_unlock_bh(&aarp_lock);
869out1:
870 ret = 1;
871out0:
872 kfree_skb(skb);
873 return ret;
874}
875
876static struct notifier_block aarp_notifier = {
877 .notifier_call = aarp_device_event,
878};
879
880static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
881
882void __init aarp_proto_init(void)
883{
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");
b24b8a24 887 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
1da177e4
LT
888 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
889 add_timer(&aarp_timer);
890 register_netdevice_notifier(&aarp_notifier);
891}
892
893/* Remove the AARP entries associated with a device. */
894void aarp_device_down(struct net_device *dev)
895{
896 int ct;
897
898 write_lock_bh(&aarp_lock);
899
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);
904 }
905
906 write_unlock_bh(&aarp_lock);
907}
908
909#ifdef CONFIG_PROC_FS
910struct aarp_iter_state {
911 int bucket;
912 struct aarp_entry **table;
913};
914
915/*
916 * Get the aarp entry that is in the chain described
ed4477b9 917 * by the iterator.
1da177e4
LT
918 * If pos is set then skip till that index.
919 * pos = 1 is the first entry
920 */
921static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
922{
923 int ct = iter->bucket;
924 struct aarp_entry **table = iter->table;
925 loff_t off = 0;
926 struct aarp_entry *entry;
ed4477b9 927
1da177e4 928 rescan:
fd1dc261 929 while (ct < AARP_HASH_SIZE) {
1da177e4
LT
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;
935 }
936 }
937 ++ct;
938 }
939
940 if (table == resolved) {
941 ct = 0;
942 table = unresolved;
943 goto rescan;
944 }
945 if (table == unresolved) {
946 ct = 0;
947 table = proxies;
948 goto rescan;
949 }
950 return NULL;
951}
952
953static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
ca629f24 954 __acquires(aarp_lock)
1da177e4
LT
955{
956 struct aarp_iter_state *iter = seq->private;
957
958 read_lock_bh(&aarp_lock);
959 iter->table = resolved;
960 iter->bucket = 0;
961
962 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
963}
964
965static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
966{
967 struct aarp_entry *entry = v;
968 struct aarp_iter_state *iter = seq->private;
969
970 ++*pos;
971
972 /* first line after header */
ed4477b9 973 if (v == SEQ_START_TOKEN)
1da177e4 974 entry = iter_next(iter, NULL);
ed4477b9 975
1da177e4
LT
976 /* next entry in current bucket */
977 else if (entry->next)
978 entry = entry->next;
979
980 /* next bucket or table */
981 else {
982 ++iter->bucket;
983 entry = iter_next(iter, NULL);
984 }
985 return entry;
986}
987
988static void aarp_seq_stop(struct seq_file *seq, void *v)
ca629f24 989 __releases(aarp_lock)
1da177e4
LT
990{
991 read_unlock_bh(&aarp_lock);
992}
993
994static const char *dt2str(unsigned long ticks)
995{
996 static char buf[32];
997
fd1dc261 998 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
1da177e4
LT
999
1000 return buf;
1001}
1002
1003static int aarp_seq_show(struct seq_file *seq, void *v)
1004{
1005 struct aarp_iter_state *iter = seq->private;
1006 struct aarp_entry *entry = v;
1007 unsigned long now = jiffies;
1008
1009 if (v == SEQ_START_TOKEN)
ed4477b9 1010 seq_puts(seq,
1da177e4
LT
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 : "????");
e174961c 1018 seq_printf(seq, "%pM", entry->hwaddr);
1da177e4
LT
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");
ed4477b9 1032 }
1da177e4
LT
1033 return 0;
1034}
1035
56b3d975 1036static const struct seq_operations aarp_seq_ops = {
1da177e4
LT
1037 .start = aarp_seq_start,
1038 .next = aarp_seq_next,
1039 .stop = aarp_seq_stop,
1040 .show = aarp_seq_show,
1041};
1042
1043static int aarp_seq_open(struct inode *inode, struct file *file)
1044{
c20932d2
PE
1045 return seq_open_private(file, &aarp_seq_ops,
1046 sizeof(struct aarp_iter_state));
1da177e4
LT
1047}
1048
9a32144e 1049const struct file_operations atalk_seq_arp_fops = {
1da177e4
LT
1050 .owner = THIS_MODULE,
1051 .open = aarp_seq_open,
1052 .read = seq_read,
1053 .llseek = seq_lseek,
1054 .release = seq_release_private,
1055};
1056#endif
1057
1058/* General module cleanup. Called from cleanup_module() in ddp.c. */
1059void aarp_cleanup_module(void)
1060{
1061 del_timer_sync(&aarp_timer);
1062 unregister_netdevice_notifier(&aarp_notifier);
1063 unregister_snap_client(aarp_dl);
1064 aarp_purge();
1065}