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