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