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