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