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