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