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