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