Merge tag 'v3.10.55' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
1da177e4
LT
43
44int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
45int sysctl_aarp_tick_time = AARP_TICK_TIME;
46int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
47int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
48
49/* Lists of aarp entries */
50/**
51 * struct aarp_entry - AARP entry
52 * @last_sent - Last time we xmitted the aarp request
53 * @packet_queue - Queue of frames wait for resolution
54 * @status - Used for proxy AARP
55 * expires_at - Entry expiry time
56 * target_addr - DDP Address
57 * dev - Device to use
58 * hwaddr - Physical i/f address of target/router
59 * xmit_count - When this hits 10 we give up
60 * next - Next entry in chain
61 */
62struct aarp_entry {
63 /* These first two are only used for unresolved entries */
64 unsigned long last_sent;
65 struct sk_buff_head packet_queue;
66 int status;
67 unsigned long expires_at;
68 struct atalk_addr target_addr;
69 struct net_device *dev;
70 char hwaddr[6];
71 unsigned short xmit_count;
72 struct aarp_entry *next;
73};
74
75/* Hashed list of resolved, unresolved and proxy entries */
76static struct aarp_entry *resolved[AARP_HASH_SIZE];
77static struct aarp_entry *unresolved[AARP_HASH_SIZE];
78static struct aarp_entry *proxies[AARP_HASH_SIZE];
79static int unresolved_count;
80
81/* One lock protects it all. */
82static DEFINE_RWLOCK(aarp_lock);
83
84/* Used to walk the list and purge/kick entries. */
85static struct timer_list aarp_timer;
86
87/*
88 * Delete an aarp queue
89 *
90 * Must run under aarp_lock.
91 */
92static void __aarp_expire(struct aarp_entry *a)
93{
94 skb_queue_purge(&a->packet_queue);
95 kfree(a);
96}
97
98/*
99 * Send an aarp queue entry request
100 *
101 * Must run under aarp_lock.
102 */
103static void __aarp_send_query(struct aarp_entry *a)
104{
105 static unsigned char aarp_eth_multicast[ETH_ALEN] =
106 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
107 struct net_device *dev = a->dev;
108 struct elapaarp *eah;
109 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
110 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
111 struct atalk_addr *sat = atalk_find_dev_addr(dev);
112
113 if (!skb)
114 return;
115
116 if (!sat) {
117 kfree_skb(skb);
118 return;
119 }
120
121 /* Set up the buffer */
122 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
7e28ecc2 123 skb_reset_network_header(skb);
badff6d0 124 skb_reset_transport_header(skb);
7e28ecc2 125 skb_put(skb, sizeof(*eah));
1da177e4
LT
126 skb->protocol = htons(ETH_P_ATALK);
127 skb->dev = dev;
128 eah = aarp_hdr(skb);
129
130 /* Set up the ARP */
131 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
132 eah->pa_type = htons(ETH_P_ATALK);
133 eah->hw_len = ETH_ALEN;
134 eah->pa_len = AARP_PA_ALEN;
135 eah->function = htons(AARP_REQUEST);
136
137 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
138
139 eah->pa_src_zero = 0;
140 eah->pa_src_net = sat->s_net;
141 eah->pa_src_node = sat->s_node;
142
143 memset(eah->hw_dst, '\0', ETH_ALEN);
144
145 eah->pa_dst_zero = 0;
146 eah->pa_dst_net = a->target_addr.s_net;
147 eah->pa_dst_node = a->target_addr.s_node;
148
149 /* Send it */
150 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
151 /* Update the sending count */
152 a->xmit_count++;
153 a->last_sent = jiffies;
154}
155
156/* This runs under aarp_lock and in softint context, so only atomic memory
157 * allocations can be used. */
158static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
159 struct atalk_addr *them, unsigned char *sha)
160{
161 struct elapaarp *eah;
162 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
163 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
164
165 if (!skb)
166 return;
167
168 /* Set up the buffer */
169 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
7e28ecc2 170 skb_reset_network_header(skb);
badff6d0 171 skb_reset_transport_header(skb);
7e28ecc2 172 skb_put(skb, sizeof(*eah));
1da177e4
LT
173 skb->protocol = htons(ETH_P_ATALK);
174 skb->dev = dev;
175 eah = aarp_hdr(skb);
176
177 /* Set up the ARP */
178 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
179 eah->pa_type = htons(ETH_P_ATALK);
180 eah->hw_len = ETH_ALEN;
181 eah->pa_len = AARP_PA_ALEN;
182 eah->function = htons(AARP_REPLY);
183
184 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
185
186 eah->pa_src_zero = 0;
187 eah->pa_src_net = us->s_net;
188 eah->pa_src_node = us->s_node;
189
190 if (!sha)
191 memset(eah->hw_dst, '\0', ETH_ALEN);
192 else
193 memcpy(eah->hw_dst, sha, ETH_ALEN);
194
195 eah->pa_dst_zero = 0;
196 eah->pa_dst_net = them->s_net;
197 eah->pa_dst_node = them->s_node;
198
199 /* Send it */
200 aarp_dl->request(aarp_dl, skb, sha);
201}
202
203/*
204 * Send probe frames. Called from aarp_probe_network and
205 * aarp_proxy_probe_network.
206 */
207
208static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
209{
210 struct elapaarp *eah;
211 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
212 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
213 static unsigned char aarp_eth_multicast[ETH_ALEN] =
214 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
215
216 if (!skb)
217 return;
218
219 /* Set up the buffer */
220 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
7e28ecc2 221 skb_reset_network_header(skb);
badff6d0 222 skb_reset_transport_header(skb);
7e28ecc2 223 skb_put(skb, sizeof(*eah));
1da177e4
LT
224 skb->protocol = htons(ETH_P_ATALK);
225 skb->dev = dev;
226 eah = aarp_hdr(skb);
227
228 /* Set up the ARP */
229 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
230 eah->pa_type = htons(ETH_P_ATALK);
231 eah->hw_len = ETH_ALEN;
232 eah->pa_len = AARP_PA_ALEN;
233 eah->function = htons(AARP_PROBE);
234
235 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
236
237 eah->pa_src_zero = 0;
238 eah->pa_src_net = us->s_net;
239 eah->pa_src_node = us->s_node;
240
241 memset(eah->hw_dst, '\0', ETH_ALEN);
242
243 eah->pa_dst_zero = 0;
244 eah->pa_dst_net = us->s_net;
245 eah->pa_dst_node = us->s_node;
246
247 /* Send it */
248 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
249}
250
251/*
252 * Handle an aarp timer expire
253 *
254 * Must run under the aarp_lock.
255 */
256
257static void __aarp_expire_timer(struct aarp_entry **n)
258{
259 struct aarp_entry *t;
260
261 while (*n)
262 /* Expired ? */
263 if (time_after(jiffies, (*n)->expires_at)) {
264 t = *n;
265 *n = (*n)->next;
266 __aarp_expire(t);
267 } else
268 n = &((*n)->next);
269}
270
271/*
272 * Kick all pending requests 5 times a second.
273 *
274 * Must run under the aarp_lock.
275 */
276static void __aarp_kick(struct aarp_entry **n)
277{
278 struct aarp_entry *t;
279
280 while (*n)
281 /* Expired: if this will be the 11th tx, we delete instead. */
282 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
283 t = *n;
284 *n = (*n)->next;
285 __aarp_expire(t);
286 } else {
287 __aarp_send_query(*n);
288 n = &((*n)->next);
289 }
290}
291
292/*
293 * A device has gone down. Take all entries referring to the device
294 * and remove them.
295 *
296 * Must run under the aarp_lock.
297 */
298static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
299{
300 struct aarp_entry *t;
301
302 while (*n)
303 if ((*n)->dev == dev) {
304 t = *n;
305 *n = (*n)->next;
306 __aarp_expire(t);
307 } else
308 n = &((*n)->next);
309}
310
311/* Handle the timer event */
312static void aarp_expire_timeout(unsigned long unused)
313{
314 int ct;
315
316 write_lock_bh(&aarp_lock);
317
318 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
319 __aarp_expire_timer(&resolved[ct]);
320 __aarp_kick(&unresolved[ct]);
321 __aarp_expire_timer(&unresolved[ct]);
322 __aarp_expire_timer(&proxies[ct]);
323 }
324
325 write_unlock_bh(&aarp_lock);
326 mod_timer(&aarp_timer, jiffies +
327 (unresolved_count ? sysctl_aarp_tick_time :
328 sysctl_aarp_expiry_time));
329}
330
331/* Network device notifier chain handler. */
332static int aarp_device_event(struct notifier_block *this, unsigned long event,
333 void *ptr)
334{
890d52d3 335 struct net_device *dev = ptr;
1da177e4
LT
336 int ct;
337
721499e8 338 if (!net_eq(dev_net(dev), &init_net))
e9dc8653
EB
339 return NOTIFY_DONE;
340
1da177e4
LT
341 if (event == NETDEV_DOWN) {
342 write_lock_bh(&aarp_lock);
343
344 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
890d52d3
EB
345 __aarp_expire_device(&resolved[ct], dev);
346 __aarp_expire_device(&unresolved[ct], dev);
347 __aarp_expire_device(&proxies[ct], dev);
1da177e4
LT
348 }
349
350 write_unlock_bh(&aarp_lock);
351 }
352 return NOTIFY_DONE;
353}
354
355/* Expire all entries in a hash chain */
356static void __aarp_expire_all(struct aarp_entry **n)
357{
358 struct aarp_entry *t;
359
360 while (*n) {
361 t = *n;
362 *n = (*n)->next;
363 __aarp_expire(t);
364 }
365}
366
367/* Cleanup all hash chains -- module unloading */
368static void aarp_purge(void)
369{
370 int ct;
371
372 write_lock_bh(&aarp_lock);
373 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
374 __aarp_expire_all(&resolved[ct]);
375 __aarp_expire_all(&unresolved[ct]);
376 __aarp_expire_all(&proxies[ct]);
377 }
378 write_unlock_bh(&aarp_lock);
379}
380
381/*
382 * Create a new aarp entry. This must use GFP_ATOMIC because it
383 * runs while holding spinlocks.
384 */
385static struct aarp_entry *aarp_alloc(void)
386{
387 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
388
389 if (a)
390 skb_queue_head_init(&a->packet_queue);
391 return a;
392}
393
394/*
395 * Find an entry. We might return an expired but not yet purged entry. We
396 * don't care as it will do no harm.
397 *
398 * This must run under the aarp_lock.
399 */
400static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
401 struct net_device *dev,
402 struct atalk_addr *sat)
403{
404 while (list) {
405 if (list->target_addr.s_net == sat->s_net &&
406 list->target_addr.s_node == sat->s_node &&
407 list->dev == dev)
408 break;
409 list = list->next;
410 }
411
412 return list;
413}
414
415/* Called from the DDP code, and thus must be exported. */
416void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
417{
418 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
419 struct aarp_entry *a;
420
421 write_lock_bh(&aarp_lock);
422
423 a = __aarp_find_entry(proxies[hash], dev, sa);
424 if (a)
425 a->expires_at = jiffies - 1;
426
427 write_unlock_bh(&aarp_lock);
428}
429
430/* This must run under aarp_lock. */
431static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
432 struct atalk_addr *sa)
433{
434 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
435 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
436
437 return a ? sa : NULL;
438}
439
440/*
441 * Probe a Phase 1 device or a device that requires its Net:Node to
442 * be set via an ioctl.
443 */
444static void aarp_send_probe_phase1(struct atalk_iface *iface)
445{
446 struct ifreq atreq;
447 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
03b35ccb 448 const struct net_device_ops *ops = iface->dev->netdev_ops;
1da177e4
LT
449
450 sa->sat_addr.s_node = iface->address.s_node;
451 sa->sat_addr.s_net = ntohs(iface->address.s_net);
452
453 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
03b35ccb
SH
454 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
455 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
1da177e4
LT
456 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
457 iface->address.s_node != sa->sat_addr.s_node)
458 iface->status |= ATIF_PROBE_FAIL;
459
460 iface->address.s_net = htons(sa->sat_addr.s_net);
461 iface->address.s_node = sa->sat_addr.s_node;
462 }
463}
464
465
466void aarp_probe_network(struct atalk_iface *atif)
467{
468 if (atif->dev->type == ARPHRD_LOCALTLK ||
469 atif->dev->type == ARPHRD_PPP)
470 aarp_send_probe_phase1(atif);
471 else {
472 unsigned int count;
473
474 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
475 aarp_send_probe(atif->dev, &atif->address);
476
477 /* Defer 1/10th */
285b3afe 478 msleep(100);
1da177e4
LT
479
480 if (atif->status & ATIF_PROBE_FAIL)
481 break;
482 }
483 }
484}
485
486int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
487{
488 int hash, retval = -EPROTONOSUPPORT;
489 struct aarp_entry *entry;
490 unsigned int count;
491
492 /*
493 * we don't currently support LocalTalk or PPP for proxy AARP;
494 * if someone wants to try and add it, have fun
495 */
496 if (atif->dev->type == ARPHRD_LOCALTLK ||
497 atif->dev->type == ARPHRD_PPP)
498 goto out;
499
500 /*
501 * create a new AARP entry with the flags set to be published --
502 * we need this one to hang around even if it's in use
503 */
504 entry = aarp_alloc();
505 retval = -ENOMEM;
506 if (!entry)
507 goto out;
508
509 entry->expires_at = -1;
510 entry->status = ATIF_PROBE;
511 entry->target_addr.s_node = sa->s_node;
512 entry->target_addr.s_net = sa->s_net;
513 entry->dev = atif->dev;
514
515 write_lock_bh(&aarp_lock);
516
517 hash = sa->s_node % (AARP_HASH_SIZE - 1);
518 entry->next = proxies[hash];
519 proxies[hash] = entry;
520
521 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
522 aarp_send_probe(atif->dev, sa);
523
524 /* Defer 1/10th */
1da177e4 525 write_unlock_bh(&aarp_lock);
285b3afe 526 msleep(100);
1da177e4
LT
527 write_lock_bh(&aarp_lock);
528
529 if (entry->status & ATIF_PROBE_FAIL)
530 break;
531 }
532
533 if (entry->status & ATIF_PROBE_FAIL) {
534 entry->expires_at = jiffies - 1; /* free the entry */
535 retval = -EADDRINUSE; /* return network full */
536 } else { /* clear the probing flag */
537 entry->status &= ~ATIF_PROBE;
538 retval = 1;
539 }
540
541 write_unlock_bh(&aarp_lock);
542out:
543 return retval;
544}
545
546/* Send a DDP frame */
547int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
548 struct atalk_addr *sa, void *hwaddr)
549{
550 static char ddp_eth_multicast[ETH_ALEN] =
551 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
552 int hash;
553 struct aarp_entry *a;
554
c1d2bbe1 555 skb_reset_network_header(skb);
1da177e4
LT
556
557 /* Check for LocalTalk first */
558 if (dev->type == ARPHRD_LOCALTLK) {
559 struct atalk_addr *at = atalk_find_dev_addr(dev);
560 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
561 int ft = 2;
562
563 /*
564 * Compressible ?
565 *
566 * IFF: src_net == dest_net == device_net
567 * (zero matches anything)
568 */
569
570 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
571 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
572 skb_pull(skb, sizeof(*ddp) - 4);
573
574 /*
575 * The upper two remaining bytes are the port
576 * numbers we just happen to need. Now put the
577 * length in the lower two.
578 */
f6e276ee 579 *((__be16 *)skb->data) = htons(skb->len);
1da177e4
LT
580 ft = 1;
581 }
582 /*
583 * Nice and easy. No AARP type protocols occur here so we can
584 * just shovel it out with a 3 byte LLAP header
585 */
586
587 skb_push(skb, 3);
588 skb->data[0] = sa->s_node;
589 skb->data[1] = at->s_node;
590 skb->data[2] = ft;
591 skb->dev = dev;
592 goto sendit;
593 }
594
595 /* On a PPP link we neither compress nor aarp. */
596 if (dev->type == ARPHRD_PPP) {
597 skb->protocol = htons(ETH_P_PPPTALK);
598 skb->dev = dev;
599 goto sendit;
600 }
601
602 /* Non ELAP we cannot do. */
603 if (dev->type != ARPHRD_ETHER)
ffcfb8db 604 goto free_it;
1da177e4
LT
605
606 skb->dev = dev;
607 skb->protocol = htons(ETH_P_ATALK);
608 hash = sa->s_node % (AARP_HASH_SIZE - 1);
609
610 /* Do we have a resolved entry? */
611 if (sa->s_node == ATADDR_BCAST) {
612 /* Send it */
613 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
614 goto sent;
615 }
616
617 write_lock_bh(&aarp_lock);
618 a = __aarp_find_entry(resolved[hash], dev, sa);
619
620 if (a) { /* Return 1 and fill in the address */
621 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
622 ddp_dl->request(ddp_dl, skb, a->hwaddr);
623 write_unlock_bh(&aarp_lock);
624 goto sent;
625 }
626
627 /* Do we have an unresolved entry: This is the less common path */
628 a = __aarp_find_entry(unresolved[hash], dev, sa);
629 if (a) { /* Queue onto the unresolved queue */
630 skb_queue_tail(&a->packet_queue, skb);
631 goto out_unlock;
632 }
633
634 /* Allocate a new entry */
635 a = aarp_alloc();
636 if (!a) {
637 /* Whoops slipped... good job it's an unreliable protocol 8) */
638 write_unlock_bh(&aarp_lock);
ffcfb8db 639 goto free_it;
1da177e4
LT
640 }
641
642 /* Set up the queue */
643 skb_queue_tail(&a->packet_queue, skb);
644 a->expires_at = jiffies + sysctl_aarp_resolve_time;
645 a->dev = dev;
646 a->next = unresolved[hash];
647 a->target_addr = *sa;
648 a->xmit_count = 0;
649 unresolved[hash] = a;
650 unresolved_count++;
651
652 /* Send an initial request for the address */
653 __aarp_send_query(a);
654
655 /*
656 * Switch to fast timer if needed (That is if this is the first
657 * unresolved entry to get added)
658 */
659
660 if (unresolved_count == 1)
661 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
662
663 /* Now finally, it is safe to drop the lock. */
664out_unlock:
665 write_unlock_bh(&aarp_lock);
666
667 /* Tell the ddp layer we have taken over for this frame. */
ffcfb8db 668 goto sent;
1da177e4
LT
669
670sendit:
671 if (skb->sk)
672 skb->priority = skb->sk->sk_priority;
ffcfb8db
ACM
673 if (dev_queue_xmit(skb))
674 goto drop;
1da177e4 675sent:
ffcfb8db
ACM
676 return NET_XMIT_SUCCESS;
677free_it:
678 kfree_skb(skb);
679drop:
680 return NET_XMIT_DROP;
1da177e4 681}
ffcfb8db 682EXPORT_SYMBOL(aarp_send_ddp);
1da177e4
LT
683
684/*
685 * An entry in the aarp unresolved queue has become resolved. Send
686 * all the frames queued under it.
687 *
688 * Must run under aarp_lock.
689 */
690static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
691 int hash)
692{
693 struct sk_buff *skb;
694
695 while (*list)
696 if (*list == a) {
697 unresolved_count--;
698 *list = a->next;
699
700 /* Move into the resolved list */
701 a->next = resolved[hash];
702 resolved[hash] = a;
703
704 /* Kick frames off */
705 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
706 a->expires_at = jiffies +
707 sysctl_aarp_expiry_time * 10;
708 ddp_dl->request(ddp_dl, skb, a->hwaddr);
709 }
710 } else
711 list = &((*list)->next);
712}
713
714/*
715 * This is called by the SNAP driver whenever we see an AARP SNAP
716 * frame. We currently only support Ethernet.
717 */
718static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
f2ccd8fa 719 struct packet_type *pt, struct net_device *orig_dev)
1da177e4
LT
720{
721 struct elapaarp *ea = aarp_hdr(skb);
722 int hash, ret = 0;
723 __u16 function;
724 struct aarp_entry *a;
725 struct atalk_addr sa, *ma, da;
726 struct atalk_iface *ifa;
727
721499e8 728 if (!net_eq(dev_net(dev), &init_net))
e730c155
EB
729 goto out0;
730
1da177e4
LT
731 /* We only do Ethernet SNAP AARP. */
732 if (dev->type != ARPHRD_ETHER)
733 goto out0;
734
735 /* Frame size ok? */
736 if (!skb_pull(skb, sizeof(*ea)))
737 goto out0;
738
739 function = ntohs(ea->function);
740
741 /* Sanity check fields. */
742 if (function < AARP_REQUEST || function > AARP_PROBE ||
743 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
744 ea->pa_src_zero || ea->pa_dst_zero)
745 goto out0;
746
747 /* Looks good. */
748 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
749
750 /* Build an address. */
751 sa.s_node = ea->pa_src_node;
752 sa.s_net = ea->pa_src_net;
753
754 /* Process the packet. Check for replies of me. */
755 ifa = atalk_find_dev(dev);
756 if (!ifa)
757 goto out1;
758
759 if (ifa->status & ATIF_PROBE &&
760 ifa->address.s_node == ea->pa_dst_node &&
761 ifa->address.s_net == ea->pa_dst_net) {
762 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
763 goto out1;
764 }
765
766 /* Check for replies of proxy AARP entries */
767 da.s_node = ea->pa_dst_node;
768 da.s_net = ea->pa_dst_net;
769
770 write_lock_bh(&aarp_lock);
771 a = __aarp_find_entry(proxies[hash], dev, &da);
772
773 if (a && a->status & ATIF_PROBE) {
774 a->status |= ATIF_PROBE_FAIL;
775 /*
776 * we do not respond to probe or request packets for
777 * this address while we are probing this address
778 */
779 goto unlock;
780 }
781
782 switch (function) {
4a9e4b09
JP
783 case AARP_REPLY:
784 if (!unresolved_count) /* Speed up */
785 break;
1da177e4 786
4a9e4b09
JP
787 /* Find the entry. */
788 a = __aarp_find_entry(unresolved[hash], dev, &sa);
789 if (!a || dev != a->dev)
1da177e4
LT
790 break;
791
4a9e4b09
JP
792 /* We can fill one in - this is good. */
793 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
794 __aarp_resolved(&unresolved[hash], a, hash);
795 if (!unresolved_count)
796 mod_timer(&aarp_timer,
797 jiffies + sysctl_aarp_expiry_time);
798 break;
799
800 case AARP_REQUEST:
801 case AARP_PROBE:
802
803 /*
804 * If it is my address set ma to my address and reply.
805 * We can treat probe and request the same. Probe
806 * simply means we shouldn't cache the querying host,
807 * as in a probe they are proposing an address not
808 * using one.
809 *
810 * Support for proxy-AARP added. We check if the
811 * address is one of our proxies before we toss the
812 * packet out.
813 */
814
815 sa.s_node = ea->pa_dst_node;
816 sa.s_net = ea->pa_dst_net;
817
818 /* See if we have a matching proxy. */
819 ma = __aarp_proxy_find(dev, &sa);
820 if (!ma)
821 ma = &ifa->address;
822 else { /* We need to make a copy of the entry. */
823 da.s_node = sa.s_node;
824 da.s_net = sa.s_net;
825 ma = &da;
826 }
1da177e4 827
4a9e4b09 828 if (function == AARP_PROBE) {
1da177e4 829 /*
4a9e4b09
JP
830 * A probe implies someone trying to get an
831 * address. So as a precaution flush any
832 * entries we have for this address.
1da177e4 833 */
4a9e4b09
JP
834 a = __aarp_find_entry(resolved[sa.s_node %
835 (AARP_HASH_SIZE - 1)],
836 skb->dev, &sa);
1da177e4 837
4a9e4b09
JP
838 /*
839 * Make it expire next tick - that avoids us
840 * getting into a probe/flush/learn/probe/
841 * flush/learn cycle during probing of a slow
842 * to respond host addr.
843 */
844 if (a) {
845 a->expires_at = jiffies - 1;
846 mod_timer(&aarp_timer, jiffies +
847 sysctl_aarp_tick_time);
1da177e4 848 }
4a9e4b09 849 }
1da177e4 850
4a9e4b09
JP
851 if (sa.s_node != ma->s_node)
852 break;
1da177e4 853
4a9e4b09
JP
854 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
855 break;
1da177e4 856
4a9e4b09
JP
857 sa.s_node = ea->pa_src_node;
858 sa.s_net = ea->pa_src_net;
1da177e4 859
4a9e4b09
JP
860 /* aarp_my_address has found the address to use for us.
861 */
862 aarp_send_reply(dev, ma, &sa, ea->hw_src);
863 break;
1da177e4
LT
864 }
865
866unlock:
867 write_unlock_bh(&aarp_lock);
868out1:
869 ret = 1;
870out0:
871 kfree_skb(skb);
872 return ret;
873}
874
875static struct notifier_block aarp_notifier = {
876 .notifier_call = aarp_device_event,
877};
878
879static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
880
881void __init aarp_proto_init(void)
882{
883 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
884 if (!aarp_dl)
885 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
b24b8a24 886 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
1da177e4
LT
887 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
888 add_timer(&aarp_timer);
889 register_netdevice_notifier(&aarp_notifier);
890}
891
892/* Remove the AARP entries associated with a device. */
893void aarp_device_down(struct net_device *dev)
894{
895 int ct;
896
897 write_lock_bh(&aarp_lock);
898
899 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
900 __aarp_expire_device(&resolved[ct], dev);
901 __aarp_expire_device(&unresolved[ct], dev);
902 __aarp_expire_device(&proxies[ct], dev);
903 }
904
905 write_unlock_bh(&aarp_lock);
906}
907
908#ifdef CONFIG_PROC_FS
909struct aarp_iter_state {
910 int bucket;
911 struct aarp_entry **table;
912};
913
914/*
915 * Get the aarp entry that is in the chain described
ed4477b9 916 * by the iterator.
1da177e4
LT
917 * If pos is set then skip till that index.
918 * pos = 1 is the first entry
919 */
920static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
921{
922 int ct = iter->bucket;
923 struct aarp_entry **table = iter->table;
924 loff_t off = 0;
925 struct aarp_entry *entry;
ed4477b9 926
1da177e4
LT
927 rescan:
928 while(ct < AARP_HASH_SIZE) {
929 for (entry = table[ct]; entry; entry = entry->next) {
930 if (!pos || ++off == *pos) {
931 iter->table = table;
932 iter->bucket = ct;
933 return entry;
934 }
935 }
936 ++ct;
937 }
938
939 if (table == resolved) {
940 ct = 0;
941 table = unresolved;
942 goto rescan;
943 }
944 if (table == unresolved) {
945 ct = 0;
946 table = proxies;
947 goto rescan;
948 }
949 return NULL;
950}
951
952static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
ca629f24 953 __acquires(aarp_lock)
1da177e4
LT
954{
955 struct aarp_iter_state *iter = seq->private;
956
957 read_lock_bh(&aarp_lock);
958 iter->table = resolved;
959 iter->bucket = 0;
960
961 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
962}
963
964static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
965{
966 struct aarp_entry *entry = v;
967 struct aarp_iter_state *iter = seq->private;
968
969 ++*pos;
970
971 /* first line after header */
ed4477b9 972 if (v == SEQ_START_TOKEN)
1da177e4 973 entry = iter_next(iter, NULL);
ed4477b9 974
1da177e4
LT
975 /* next entry in current bucket */
976 else if (entry->next)
977 entry = entry->next;
978
979 /* next bucket or table */
980 else {
981 ++iter->bucket;
982 entry = iter_next(iter, NULL);
983 }
984 return entry;
985}
986
987static void aarp_seq_stop(struct seq_file *seq, void *v)
ca629f24 988 __releases(aarp_lock)
1da177e4
LT
989{
990 read_unlock_bh(&aarp_lock);
991}
992
993static const char *dt2str(unsigned long ticks)
994{
995 static char buf[32];
996
997 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
998
999 return buf;
1000}
1001
1002static int aarp_seq_show(struct seq_file *seq, void *v)
1003{
1004 struct aarp_iter_state *iter = seq->private;
1005 struct aarp_entry *entry = v;
1006 unsigned long now = jiffies;
1007
1008 if (v == SEQ_START_TOKEN)
ed4477b9 1009 seq_puts(seq,
1da177e4
LT
1010 "Address Interface Hardware Address"
1011 " Expires LastSend Retry Status\n");
1012 else {
1013 seq_printf(seq, "%04X:%02X %-12s",
1014 ntohs(entry->target_addr.s_net),
1015 (unsigned int) entry->target_addr.s_node,
1016 entry->dev ? entry->dev->name : "????");
e174961c 1017 seq_printf(seq, "%pM", entry->hwaddr);
1da177e4
LT
1018 seq_printf(seq, " %8s",
1019 dt2str((long)entry->expires_at - (long)now));
1020 if (iter->table == unresolved)
1021 seq_printf(seq, " %8s %6hu",
1022 dt2str(now - entry->last_sent),
1023 entry->xmit_count);
1024 else
1025 seq_puts(seq, " ");
1026 seq_printf(seq, " %s\n",
1027 (iter->table == resolved) ? "resolved"
1028 : (iter->table == unresolved) ? "unresolved"
1029 : (iter->table == proxies) ? "proxies"
1030 : "unknown");
ed4477b9 1031 }
1da177e4
LT
1032 return 0;
1033}
1034
56b3d975 1035static const struct seq_operations aarp_seq_ops = {
1da177e4
LT
1036 .start = aarp_seq_start,
1037 .next = aarp_seq_next,
1038 .stop = aarp_seq_stop,
1039 .show = aarp_seq_show,
1040};
1041
1042static int aarp_seq_open(struct inode *inode, struct file *file)
1043{
c20932d2
PE
1044 return seq_open_private(file, &aarp_seq_ops,
1045 sizeof(struct aarp_iter_state));
1da177e4
LT
1046}
1047
9a32144e 1048const struct file_operations atalk_seq_arp_fops = {
1da177e4
LT
1049 .owner = THIS_MODULE,
1050 .open = aarp_seq_open,
1051 .read = seq_read,
1052 .llseek = seq_lseek,
1053 .release = seq_release_private,
1054};
1055#endif
1056
1057/* General module cleanup. Called from cleanup_module() in ddp.c. */
1058void aarp_cleanup_module(void)
1059{
1060 del_timer_sync(&aarp_timer);
1061 unregister_netdevice_notifier(&aarp_notifier);
1062 unregister_snap_client(aarp_dl);
1063 aarp_purge();
1064}