xref: /linux/net/appletalk/aarp.c (revision 44414d82cfe0f68cb59d0a42f599ccd893ae0032)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	AARP:		An implementation of the AppleTalk AARP protocol for
31da177e4SLinus Torvalds  *			Ethernet 'ELAP'.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *		Alan Cox  <Alan.Cox@linux.org>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This doesn't fit cleanly with the IP arp. Potentially we can use
81da177e4SLinus Torvalds  *	the generic neighbour discovery code to clean this up.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	FIXME:
111da177e4SLinus Torvalds  *		We ought to handle the retransmits with a single list and a
121da177e4SLinus Torvalds  *	separate fast timer for when it is needed.
131da177e4SLinus Torvalds  *		Use neighbour discovery code.
141da177e4SLinus Torvalds  *		Token Ring Support.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
171da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
181da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
191da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *	References:
231da177e4SLinus Torvalds  *		Inside AppleTalk (2nd Ed).
241da177e4SLinus Torvalds  *	Fixes:
251da177e4SLinus Torvalds  *		Jaume Grau	-	flush caches on AARP_PROBE
261da177e4SLinus Torvalds  *		Rob Newberry	-	Added proxy AARP and AARP proc fs,
271da177e4SLinus Torvalds  *					moved probing from DDP module.
281da177e4SLinus Torvalds  *		Arnaldo C. Melo -	don't mangle rx packets
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <linux/if_arp.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
341da177e4SLinus Torvalds #include <net/sock.h>
351da177e4SLinus Torvalds #include <net/datalink.h>
361da177e4SLinus Torvalds #include <net/psnap.h>
371da177e4SLinus Torvalds #include <linux/atalk.h>
38285b3afeSNishanth Aravamudan #include <linux/delay.h>
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/seq_file.h>
42bc3b2d7fSPaul Gortmaker #include <linux/export.h>
439be68c1aSDavid S. Miller #include <linux/etherdevice.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
461da177e4SLinus Torvalds int sysctl_aarp_tick_time = AARP_TICK_TIME;
471da177e4SLinus Torvalds int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
481da177e4SLinus Torvalds int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* Lists of aarp entries */
511da177e4SLinus Torvalds /**
521da177e4SLinus Torvalds  *	struct aarp_entry - AARP entry
531da177e4SLinus Torvalds  *	@last_sent - Last time we xmitted the aarp request
541da177e4SLinus Torvalds  *	@packet_queue - Queue of frames wait for resolution
551da177e4SLinus Torvalds  *	@status - Used for proxy AARP
561da177e4SLinus Torvalds  *	expires_at - Entry expiry time
571da177e4SLinus Torvalds  *	target_addr - DDP Address
581da177e4SLinus Torvalds  *	dev - Device to use
591da177e4SLinus Torvalds  *	hwaddr - Physical i/f address of target/router
601da177e4SLinus Torvalds  *	xmit_count - When this hits 10 we give up
611da177e4SLinus Torvalds  *	next - Next entry in chain
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds struct aarp_entry {
641da177e4SLinus Torvalds 	/* These first two are only used for unresolved entries */
651da177e4SLinus Torvalds 	unsigned long		last_sent;
661da177e4SLinus Torvalds 	struct sk_buff_head	packet_queue;
671da177e4SLinus Torvalds 	int			status;
681da177e4SLinus Torvalds 	unsigned long		expires_at;
691da177e4SLinus Torvalds 	struct atalk_addr	target_addr;
701da177e4SLinus Torvalds 	struct net_device	*dev;
7190ccb6aaSJoe Perches 	char			hwaddr[ETH_ALEN];
721da177e4SLinus Torvalds 	unsigned short		xmit_count;
731da177e4SLinus Torvalds 	struct aarp_entry	*next;
741da177e4SLinus Torvalds };
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* Hashed list of resolved, unresolved and proxy entries */
771da177e4SLinus Torvalds static struct aarp_entry *resolved[AARP_HASH_SIZE];
781da177e4SLinus Torvalds static struct aarp_entry *unresolved[AARP_HASH_SIZE];
791da177e4SLinus Torvalds static struct aarp_entry *proxies[AARP_HASH_SIZE];
801da177e4SLinus Torvalds static int unresolved_count;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* One lock protects it all. */
831da177e4SLinus Torvalds static DEFINE_RWLOCK(aarp_lock);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Used to walk the list and purge/kick entries.  */
861da177e4SLinus Torvalds static struct timer_list aarp_timer;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  *	Delete an aarp queue
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  *	Must run under aarp_lock.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds static void __aarp_expire(struct aarp_entry *a)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	skb_queue_purge(&a->packet_queue);
961da177e4SLinus Torvalds 	kfree(a);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  *	Send an aarp queue entry request
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  *	Must run under aarp_lock.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds static void __aarp_send_query(struct aarp_entry *a)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	static unsigned char aarp_eth_multicast[ETH_ALEN] =
1071da177e4SLinus Torvalds 					{ 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
1081da177e4SLinus Torvalds 	struct net_device *dev = a->dev;
1091da177e4SLinus Torvalds 	struct elapaarp *eah;
1101da177e4SLinus Torvalds 	int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
1111da177e4SLinus Torvalds 	struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
1121da177e4SLinus Torvalds 	struct atalk_addr *sat = atalk_find_dev_addr(dev);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	if (!skb)
1151da177e4SLinus Torvalds 		return;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (!sat) {
1181da177e4SLinus Torvalds 		kfree_skb(skb);
1191da177e4SLinus Torvalds 		return;
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	/* Set up the buffer */
1231da177e4SLinus Torvalds 	skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
1247e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
125badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
1267e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(*eah));
1271da177e4SLinus Torvalds 	skb->protocol    = htons(ETH_P_ATALK);
1281da177e4SLinus Torvalds 	skb->dev	 = dev;
1291da177e4SLinus Torvalds 	eah		 = aarp_hdr(skb);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	/* Set up the ARP */
1321da177e4SLinus Torvalds 	eah->hw_type	 = htons(AARP_HW_TYPE_ETHERNET);
1331da177e4SLinus Torvalds 	eah->pa_type	 = htons(ETH_P_ATALK);
1341da177e4SLinus Torvalds 	eah->hw_len	 = ETH_ALEN;
1351da177e4SLinus Torvalds 	eah->pa_len	 = AARP_PA_ALEN;
1361da177e4SLinus Torvalds 	eah->function	 = htons(AARP_REQUEST);
1371da177e4SLinus Torvalds 
13890ccb6aaSJoe Perches 	ether_addr_copy(eah->hw_src, dev->dev_addr);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	eah->pa_src_zero = 0;
1411da177e4SLinus Torvalds 	eah->pa_src_net	 = sat->s_net;
1421da177e4SLinus Torvalds 	eah->pa_src_node = sat->s_node;
1431da177e4SLinus Torvalds 
1441a73de07SJoe Perches 	eth_zero_addr(eah->hw_dst);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	eah->pa_dst_zero = 0;
1471da177e4SLinus Torvalds 	eah->pa_dst_net	 = a->target_addr.s_net;
1481da177e4SLinus Torvalds 	eah->pa_dst_node = a->target_addr.s_node;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	/* Send it */
1511da177e4SLinus Torvalds 	aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
1521da177e4SLinus Torvalds 	/* Update the sending count */
1531da177e4SLinus Torvalds 	a->xmit_count++;
1541da177e4SLinus Torvalds 	a->last_sent = jiffies;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /* This runs under aarp_lock and in softint context, so only atomic memory
1581da177e4SLinus Torvalds  * allocations can be used. */
1591da177e4SLinus Torvalds static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
1601da177e4SLinus Torvalds 			    struct atalk_addr *them, unsigned char *sha)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	struct elapaarp *eah;
1631da177e4SLinus Torvalds 	int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
1641da177e4SLinus Torvalds 	struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	if (!skb)
1671da177e4SLinus Torvalds 		return;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Set up the buffer */
1701da177e4SLinus Torvalds 	skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
1717e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
172badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
1737e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(*eah));
1741da177e4SLinus Torvalds 	skb->protocol    = htons(ETH_P_ATALK);
1751da177e4SLinus Torvalds 	skb->dev	 = dev;
1761da177e4SLinus Torvalds 	eah		 = aarp_hdr(skb);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/* Set up the ARP */
1791da177e4SLinus Torvalds 	eah->hw_type	 = htons(AARP_HW_TYPE_ETHERNET);
1801da177e4SLinus Torvalds 	eah->pa_type	 = htons(ETH_P_ATALK);
1811da177e4SLinus Torvalds 	eah->hw_len	 = ETH_ALEN;
1821da177e4SLinus Torvalds 	eah->pa_len	 = AARP_PA_ALEN;
1831da177e4SLinus Torvalds 	eah->function	 = htons(AARP_REPLY);
1841da177e4SLinus Torvalds 
18590ccb6aaSJoe Perches 	ether_addr_copy(eah->hw_src, dev->dev_addr);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	eah->pa_src_zero = 0;
1881da177e4SLinus Torvalds 	eah->pa_src_net	 = us->s_net;
1891da177e4SLinus Torvalds 	eah->pa_src_node = us->s_node;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	if (!sha)
1921a73de07SJoe Perches 		eth_zero_addr(eah->hw_dst);
1931da177e4SLinus Torvalds 	else
19490ccb6aaSJoe Perches 		ether_addr_copy(eah->hw_dst, sha);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	eah->pa_dst_zero = 0;
1971da177e4SLinus Torvalds 	eah->pa_dst_net	 = them->s_net;
1981da177e4SLinus Torvalds 	eah->pa_dst_node = them->s_node;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	/* Send it */
2011da177e4SLinus Torvalds 	aarp_dl->request(aarp_dl, skb, sha);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /*
2051da177e4SLinus Torvalds  *	Send probe frames. Called from aarp_probe_network and
2061da177e4SLinus Torvalds  *	aarp_proxy_probe_network.
2071da177e4SLinus Torvalds  */
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct elapaarp *eah;
2121da177e4SLinus Torvalds 	int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
2131da177e4SLinus Torvalds 	struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
2141da177e4SLinus Torvalds 	static unsigned char aarp_eth_multicast[ETH_ALEN] =
2151da177e4SLinus Torvalds 					{ 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (!skb)
2181da177e4SLinus Torvalds 		return;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	/* Set up the buffer */
2211da177e4SLinus Torvalds 	skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
2227e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
223badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
2247e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(*eah));
2251da177e4SLinus Torvalds 	skb->protocol    = htons(ETH_P_ATALK);
2261da177e4SLinus Torvalds 	skb->dev	 = dev;
2271da177e4SLinus Torvalds 	eah		 = aarp_hdr(skb);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* Set up the ARP */
2301da177e4SLinus Torvalds 	eah->hw_type	 = htons(AARP_HW_TYPE_ETHERNET);
2311da177e4SLinus Torvalds 	eah->pa_type	 = htons(ETH_P_ATALK);
2321da177e4SLinus Torvalds 	eah->hw_len	 = ETH_ALEN;
2331da177e4SLinus Torvalds 	eah->pa_len	 = AARP_PA_ALEN;
2341da177e4SLinus Torvalds 	eah->function	 = htons(AARP_PROBE);
2351da177e4SLinus Torvalds 
23690ccb6aaSJoe Perches 	ether_addr_copy(eah->hw_src, dev->dev_addr);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	eah->pa_src_zero = 0;
2391da177e4SLinus Torvalds 	eah->pa_src_net	 = us->s_net;
2401da177e4SLinus Torvalds 	eah->pa_src_node = us->s_node;
2411da177e4SLinus Torvalds 
2421a73de07SJoe Perches 	eth_zero_addr(eah->hw_dst);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	eah->pa_dst_zero = 0;
2451da177e4SLinus Torvalds 	eah->pa_dst_net	 = us->s_net;
2461da177e4SLinus Torvalds 	eah->pa_dst_node = us->s_node;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/* Send it */
2491da177e4SLinus Torvalds 	aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds /*
2531da177e4SLinus Torvalds  *	Handle an aarp timer expire
2541da177e4SLinus Torvalds  *
2551da177e4SLinus Torvalds  *	Must run under the aarp_lock.
2561da177e4SLinus Torvalds  */
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static void __aarp_expire_timer(struct aarp_entry **n)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	struct aarp_entry *t;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	while (*n)
2631da177e4SLinus Torvalds 		/* Expired ? */
2641da177e4SLinus Torvalds 		if (time_after(jiffies, (*n)->expires_at)) {
2651da177e4SLinus Torvalds 			t = *n;
2661da177e4SLinus Torvalds 			*n = (*n)->next;
2671da177e4SLinus Torvalds 			__aarp_expire(t);
2681da177e4SLinus Torvalds 		} else
2691da177e4SLinus Torvalds 			n = &((*n)->next);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /*
2731da177e4SLinus Torvalds  *	Kick all pending requests 5 times a second.
2741da177e4SLinus Torvalds  *
2751da177e4SLinus Torvalds  *	Must run under the aarp_lock.
2761da177e4SLinus Torvalds  */
2771da177e4SLinus Torvalds static void __aarp_kick(struct aarp_entry **n)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct aarp_entry *t;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	while (*n)
2821da177e4SLinus Torvalds 		/* Expired: if this will be the 11th tx, we delete instead. */
2831da177e4SLinus Torvalds 		if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
2841da177e4SLinus Torvalds 			t = *n;
2851da177e4SLinus Torvalds 			*n = (*n)->next;
2861da177e4SLinus Torvalds 			__aarp_expire(t);
2871da177e4SLinus Torvalds 		} else {
2881da177e4SLinus Torvalds 			__aarp_send_query(*n);
2891da177e4SLinus Torvalds 			n = &((*n)->next);
2901da177e4SLinus Torvalds 		}
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /*
2941da177e4SLinus Torvalds  *	A device has gone down. Take all entries referring to the device
2951da177e4SLinus Torvalds  *	and remove them.
2961da177e4SLinus Torvalds  *
2971da177e4SLinus Torvalds  *	Must run under the aarp_lock.
2981da177e4SLinus Torvalds  */
2991da177e4SLinus Torvalds static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct aarp_entry *t;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	while (*n)
3041da177e4SLinus Torvalds 		if ((*n)->dev == dev) {
3051da177e4SLinus Torvalds 			t = *n;
3061da177e4SLinus Torvalds 			*n = (*n)->next;
3071da177e4SLinus Torvalds 			__aarp_expire(t);
3081da177e4SLinus Torvalds 		} else
3091da177e4SLinus Torvalds 			n = &((*n)->next);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /* Handle the timer event */
313e99e88a9SKees Cook static void aarp_expire_timeout(struct timer_list *unused)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	int ct;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
3201da177e4SLinus Torvalds 		__aarp_expire_timer(&resolved[ct]);
3211da177e4SLinus Torvalds 		__aarp_kick(&unresolved[ct]);
3221da177e4SLinus Torvalds 		__aarp_expire_timer(&unresolved[ct]);
3231da177e4SLinus Torvalds 		__aarp_expire_timer(&proxies[ct]);
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
3271da177e4SLinus Torvalds 	mod_timer(&aarp_timer, jiffies +
3281da177e4SLinus Torvalds 			       (unresolved_count ? sysctl_aarp_tick_time :
3291da177e4SLinus Torvalds 				sysctl_aarp_expiry_time));
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /* Network device notifier chain handler. */
3331da177e4SLinus Torvalds static int aarp_device_event(struct notifier_block *this, unsigned long event,
3341da177e4SLinus Torvalds 			     void *ptr)
3351da177e4SLinus Torvalds {
336351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3371da177e4SLinus Torvalds 	int ct;
3381da177e4SLinus Torvalds 
339721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
340e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
341e9dc8653SEric W. Biederman 
3421da177e4SLinus Torvalds 	if (event == NETDEV_DOWN) {
3431da177e4SLinus Torvalds 		write_lock_bh(&aarp_lock);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
346890d52d3SEric W. Biederman 			__aarp_expire_device(&resolved[ct], dev);
347890d52d3SEric W. Biederman 			__aarp_expire_device(&unresolved[ct], dev);
348890d52d3SEric W. Biederman 			__aarp_expire_device(&proxies[ct], dev);
3491da177e4SLinus Torvalds 		}
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		write_unlock_bh(&aarp_lock);
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 	return NOTIFY_DONE;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds /* Expire all entries in a hash chain */
3571da177e4SLinus Torvalds static void __aarp_expire_all(struct aarp_entry **n)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	struct aarp_entry *t;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	while (*n) {
3621da177e4SLinus Torvalds 		t = *n;
3631da177e4SLinus Torvalds 		*n = (*n)->next;
3641da177e4SLinus Torvalds 		__aarp_expire(t);
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /* Cleanup all hash chains -- module unloading */
3691da177e4SLinus Torvalds static void aarp_purge(void)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	int ct;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
3741da177e4SLinus Torvalds 	for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
3751da177e4SLinus Torvalds 		__aarp_expire_all(&resolved[ct]);
3761da177e4SLinus Torvalds 		__aarp_expire_all(&unresolved[ct]);
3771da177e4SLinus Torvalds 		__aarp_expire_all(&proxies[ct]);
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds /*
3831da177e4SLinus Torvalds  *	Create a new aarp entry.  This must use GFP_ATOMIC because it
3841da177e4SLinus Torvalds  *	runs while holding spinlocks.
3851da177e4SLinus Torvalds  */
3861da177e4SLinus Torvalds static struct aarp_entry *aarp_alloc(void)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (a)
3911da177e4SLinus Torvalds 		skb_queue_head_init(&a->packet_queue);
3921da177e4SLinus Torvalds 	return a;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /*
3961da177e4SLinus Torvalds  * Find an entry. We might return an expired but not yet purged entry. We
3971da177e4SLinus Torvalds  * don't care as it will do no harm.
3981da177e4SLinus Torvalds  *
3991da177e4SLinus Torvalds  * This must run under the aarp_lock.
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
4021da177e4SLinus Torvalds 					    struct net_device *dev,
4031da177e4SLinus Torvalds 					    struct atalk_addr *sat)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	while (list) {
4061da177e4SLinus Torvalds 		if (list->target_addr.s_net == sat->s_net &&
4071da177e4SLinus Torvalds 		    list->target_addr.s_node == sat->s_node &&
4081da177e4SLinus Torvalds 		    list->dev == dev)
4091da177e4SLinus Torvalds 			break;
4101da177e4SLinus Torvalds 		list = list->next;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	return list;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds /* Called from the DDP code, and thus must be exported. */
4171da177e4SLinus Torvalds void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	int hash = sa->s_node % (AARP_HASH_SIZE - 1);
4201da177e4SLinus Torvalds 	struct aarp_entry *a;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	a = __aarp_find_entry(proxies[hash], dev, sa);
4251da177e4SLinus Torvalds 	if (a)
4261da177e4SLinus Torvalds 		a->expires_at = jiffies - 1;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /* This must run under aarp_lock. */
4321da177e4SLinus Torvalds static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
4331da177e4SLinus Torvalds 					    struct atalk_addr *sa)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	int hash = sa->s_node % (AARP_HASH_SIZE - 1);
4361da177e4SLinus Torvalds 	struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	return a ? sa : NULL;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds /*
4421da177e4SLinus Torvalds  * Probe a Phase 1 device or a device that requires its Net:Node to
4431da177e4SLinus Torvalds  * be set via an ioctl.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds static void aarp_send_probe_phase1(struct atalk_iface *iface)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct ifreq atreq;
4481da177e4SLinus Torvalds 	struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
44903b35ccbSStephen Hemminger 	const struct net_device_ops *ops = iface->dev->netdev_ops;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	sa->sat_addr.s_node = iface->address.s_node;
4521da177e4SLinus Torvalds 	sa->sat_addr.s_net = ntohs(iface->address.s_net);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* We pass the Net:Node to the drivers/cards by a Device ioctl. */
45503b35ccbSStephen Hemminger 	if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
45603b35ccbSStephen Hemminger 		ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
4571da177e4SLinus Torvalds 		if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
4581da177e4SLinus Torvalds 		    iface->address.s_node != sa->sat_addr.s_node)
4591da177e4SLinus Torvalds 			iface->status |= ATIF_PROBE_FAIL;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 		iface->address.s_net  = htons(sa->sat_addr.s_net);
4621da177e4SLinus Torvalds 		iface->address.s_node = sa->sat_addr.s_node;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds void aarp_probe_network(struct atalk_iface *atif)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	if (atif->dev->type == ARPHRD_LOCALTLK ||
4701da177e4SLinus Torvalds 	    atif->dev->type == ARPHRD_PPP)
4711da177e4SLinus Torvalds 		aarp_send_probe_phase1(atif);
4721da177e4SLinus Torvalds 	else {
4731da177e4SLinus Torvalds 		unsigned int count;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 		for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
4761da177e4SLinus Torvalds 			aarp_send_probe(atif->dev, &atif->address);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 			/* Defer 1/10th */
479285b3afeSNishanth Aravamudan 			msleep(100);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 			if (atif->status & ATIF_PROBE_FAIL)
4821da177e4SLinus Torvalds 				break;
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	int hash, retval = -EPROTONOSUPPORT;
4901da177e4SLinus Torvalds 	struct aarp_entry *entry;
4911da177e4SLinus Torvalds 	unsigned int count;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	/*
4941da177e4SLinus Torvalds 	 * we don't currently support LocalTalk or PPP for proxy AARP;
4951da177e4SLinus Torvalds 	 * if someone wants to try and add it, have fun
4961da177e4SLinus Torvalds 	 */
4971da177e4SLinus Torvalds 	if (atif->dev->type == ARPHRD_LOCALTLK ||
4981da177e4SLinus Torvalds 	    atif->dev->type == ARPHRD_PPP)
4991da177e4SLinus Torvalds 		goto out;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	/*
5021da177e4SLinus Torvalds 	 * create a new AARP entry with the flags set to be published --
5031da177e4SLinus Torvalds 	 * we need this one to hang around even if it's in use
5041da177e4SLinus Torvalds 	 */
5051da177e4SLinus Torvalds 	entry = aarp_alloc();
5061da177e4SLinus Torvalds 	retval = -ENOMEM;
5071da177e4SLinus Torvalds 	if (!entry)
5081da177e4SLinus Torvalds 		goto out;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	entry->expires_at = -1;
5111da177e4SLinus Torvalds 	entry->status = ATIF_PROBE;
5121da177e4SLinus Torvalds 	entry->target_addr.s_node = sa->s_node;
5131da177e4SLinus Torvalds 	entry->target_addr.s_net = sa->s_net;
5141da177e4SLinus Torvalds 	entry->dev = atif->dev;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	hash = sa->s_node % (AARP_HASH_SIZE - 1);
5191da177e4SLinus Torvalds 	entry->next = proxies[hash];
5201da177e4SLinus Torvalds 	proxies[hash] = entry;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
5231da177e4SLinus Torvalds 		aarp_send_probe(atif->dev, sa);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		/* Defer 1/10th */
5261da177e4SLinus Torvalds 		write_unlock_bh(&aarp_lock);
527285b3afeSNishanth Aravamudan 		msleep(100);
5281da177e4SLinus Torvalds 		write_lock_bh(&aarp_lock);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 		if (entry->status & ATIF_PROBE_FAIL)
5311da177e4SLinus Torvalds 			break;
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	if (entry->status & ATIF_PROBE_FAIL) {
5351da177e4SLinus Torvalds 		entry->expires_at = jiffies - 1; /* free the entry */
5361da177e4SLinus Torvalds 		retval = -EADDRINUSE; /* return network full */
5371da177e4SLinus Torvalds 	} else { /* clear the probing flag */
5381da177e4SLinus Torvalds 		entry->status &= ~ATIF_PROBE;
5391da177e4SLinus Torvalds 		retval = 1;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
5431da177e4SLinus Torvalds out:
5441da177e4SLinus Torvalds 	return retval;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* Send a DDP frame */
5481da177e4SLinus Torvalds int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
5491da177e4SLinus Torvalds 		  struct atalk_addr *sa, void *hwaddr)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	static char ddp_eth_multicast[ETH_ALEN] =
5521da177e4SLinus Torvalds 		{ 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
5531da177e4SLinus Torvalds 	int hash;
5541da177e4SLinus Torvalds 	struct aarp_entry *a;
5551da177e4SLinus Torvalds 
556c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	/* Check for LocalTalk first */
5591da177e4SLinus Torvalds 	if (dev->type == ARPHRD_LOCALTLK) {
5601da177e4SLinus Torvalds 		struct atalk_addr *at = atalk_find_dev_addr(dev);
5611da177e4SLinus Torvalds 		struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
5621da177e4SLinus Torvalds 		int ft = 2;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 		/*
5651da177e4SLinus Torvalds 		 * Compressible ?
5661da177e4SLinus Torvalds 		 *
5671da177e4SLinus Torvalds 		 * IFF: src_net == dest_net == device_net
5681da177e4SLinus Torvalds 		 * (zero matches anything)
5691da177e4SLinus Torvalds 		 */
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 		if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
5721da177e4SLinus Torvalds 		    (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
5731da177e4SLinus Torvalds 			skb_pull(skb, sizeof(*ddp) - 4);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 			/*
5761da177e4SLinus Torvalds 			 *	The upper two remaining bytes are the port
5771da177e4SLinus Torvalds 			 *	numbers	we just happen to need. Now put the
5781da177e4SLinus Torvalds 			 *	length in the lower two.
5791da177e4SLinus Torvalds 			 */
580f6e276eeSAlexey Dobriyan 			*((__be16 *)skb->data) = htons(skb->len);
5811da177e4SLinus Torvalds 			ft = 1;
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 		/*
5841da177e4SLinus Torvalds 		 * Nice and easy. No AARP type protocols occur here so we can
5851da177e4SLinus Torvalds 		 * just shovel it out with a 3 byte LLAP header
5861da177e4SLinus Torvalds 		 */
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 		skb_push(skb, 3);
5891da177e4SLinus Torvalds 		skb->data[0] = sa->s_node;
5901da177e4SLinus Torvalds 		skb->data[1] = at->s_node;
5911da177e4SLinus Torvalds 		skb->data[2] = ft;
5921da177e4SLinus Torvalds 		skb->dev     = dev;
5931da177e4SLinus Torvalds 		goto sendit;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	/* On a PPP link we neither compress nor aarp.  */
5971da177e4SLinus Torvalds 	if (dev->type == ARPHRD_PPP) {
5981da177e4SLinus Torvalds 		skb->protocol = htons(ETH_P_PPPTALK);
5991da177e4SLinus Torvalds 		skb->dev = dev;
6001da177e4SLinus Torvalds 		goto sendit;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	/* Non ELAP we cannot do. */
6041da177e4SLinus Torvalds 	if (dev->type != ARPHRD_ETHER)
605ffcfb8dbSArnaldo Carvalho de Melo 		goto free_it;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	skb->dev = dev;
6081da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_ATALK);
6091da177e4SLinus Torvalds 	hash = sa->s_node % (AARP_HASH_SIZE - 1);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	/* Do we have a resolved entry? */
6121da177e4SLinus Torvalds 	if (sa->s_node == ATADDR_BCAST) {
6131da177e4SLinus Torvalds 		/* Send it */
6141da177e4SLinus Torvalds 		ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
6151da177e4SLinus Torvalds 		goto sent;
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
6191da177e4SLinus Torvalds 	a = __aarp_find_entry(resolved[hash], dev, sa);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (a) { /* Return 1 and fill in the address */
6221da177e4SLinus Torvalds 		a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
6231da177e4SLinus Torvalds 		ddp_dl->request(ddp_dl, skb, a->hwaddr);
6241da177e4SLinus Torvalds 		write_unlock_bh(&aarp_lock);
6251da177e4SLinus Torvalds 		goto sent;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/* Do we have an unresolved entry: This is the less common path */
6291da177e4SLinus Torvalds 	a = __aarp_find_entry(unresolved[hash], dev, sa);
6301da177e4SLinus Torvalds 	if (a) { /* Queue onto the unresolved queue */
6311da177e4SLinus Torvalds 		skb_queue_tail(&a->packet_queue, skb);
6321da177e4SLinus Torvalds 		goto out_unlock;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	/* Allocate a new entry */
6361da177e4SLinus Torvalds 	a = aarp_alloc();
6371da177e4SLinus Torvalds 	if (!a) {
6381da177e4SLinus Torvalds 		/* Whoops slipped... good job it's an unreliable protocol 8) */
6391da177e4SLinus Torvalds 		write_unlock_bh(&aarp_lock);
640ffcfb8dbSArnaldo Carvalho de Melo 		goto free_it;
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	/* Set up the queue */
6441da177e4SLinus Torvalds 	skb_queue_tail(&a->packet_queue, skb);
6451da177e4SLinus Torvalds 	a->expires_at	 = jiffies + sysctl_aarp_resolve_time;
6461da177e4SLinus Torvalds 	a->dev		 = dev;
6471da177e4SLinus Torvalds 	a->next		 = unresolved[hash];
6481da177e4SLinus Torvalds 	a->target_addr	 = *sa;
6491da177e4SLinus Torvalds 	a->xmit_count	 = 0;
6501da177e4SLinus Torvalds 	unresolved[hash] = a;
6511da177e4SLinus Torvalds 	unresolved_count++;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/* Send an initial request for the address */
6541da177e4SLinus Torvalds 	__aarp_send_query(a);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	/*
6571da177e4SLinus Torvalds 	 * Switch to fast timer if needed (That is if this is the first
6581da177e4SLinus Torvalds 	 * unresolved entry to get added)
6591da177e4SLinus Torvalds 	 */
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (unresolved_count == 1)
6621da177e4SLinus Torvalds 		mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	/* Now finally, it is safe to drop the lock. */
6651da177e4SLinus Torvalds out_unlock:
6661da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	/* Tell the ddp layer we have taken over for this frame. */
669ffcfb8dbSArnaldo Carvalho de Melo 	goto sent;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds sendit:
6721da177e4SLinus Torvalds 	if (skb->sk)
6731da177e4SLinus Torvalds 		skb->priority = skb->sk->sk_priority;
674ffcfb8dbSArnaldo Carvalho de Melo 	if (dev_queue_xmit(skb))
675ffcfb8dbSArnaldo Carvalho de Melo 		goto drop;
6761da177e4SLinus Torvalds sent:
677ffcfb8dbSArnaldo Carvalho de Melo 	return NET_XMIT_SUCCESS;
678ffcfb8dbSArnaldo Carvalho de Melo free_it:
679ffcfb8dbSArnaldo Carvalho de Melo 	kfree_skb(skb);
680ffcfb8dbSArnaldo Carvalho de Melo drop:
681ffcfb8dbSArnaldo Carvalho de Melo 	return NET_XMIT_DROP;
6821da177e4SLinus Torvalds }
683ffcfb8dbSArnaldo Carvalho de Melo EXPORT_SYMBOL(aarp_send_ddp);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /*
6861da177e4SLinus Torvalds  *	An entry in the aarp unresolved queue has become resolved. Send
6871da177e4SLinus Torvalds  *	all the frames queued under it.
6881da177e4SLinus Torvalds  *
6891da177e4SLinus Torvalds  *	Must run under aarp_lock.
6901da177e4SLinus Torvalds  */
6911da177e4SLinus Torvalds static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
6921da177e4SLinus Torvalds 			    int hash)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct sk_buff *skb;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	while (*list)
6971da177e4SLinus Torvalds 		if (*list == a) {
6981da177e4SLinus Torvalds 			unresolved_count--;
6991da177e4SLinus Torvalds 			*list = a->next;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 			/* Move into the resolved list */
7021da177e4SLinus Torvalds 			a->next = resolved[hash];
7031da177e4SLinus Torvalds 			resolved[hash] = a;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 			/* Kick frames off */
7061da177e4SLinus Torvalds 			while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
7071da177e4SLinus Torvalds 				a->expires_at = jiffies +
7081da177e4SLinus Torvalds 						sysctl_aarp_expiry_time * 10;
7091da177e4SLinus Torvalds 				ddp_dl->request(ddp_dl, skb, a->hwaddr);
7101da177e4SLinus Torvalds 			}
7111da177e4SLinus Torvalds 		} else
7121da177e4SLinus Torvalds 			list = &((*list)->next);
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /*
7161da177e4SLinus Torvalds  *	This is called by the SNAP driver whenever we see an AARP SNAP
7171da177e4SLinus Torvalds  *	frame. We currently only support Ethernet.
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
720f2ccd8faSDavid S. Miller 		    struct packet_type *pt, struct net_device *orig_dev)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	struct elapaarp *ea = aarp_hdr(skb);
7231da177e4SLinus Torvalds 	int hash, ret = 0;
7241da177e4SLinus Torvalds 	__u16 function;
7251da177e4SLinus Torvalds 	struct aarp_entry *a;
7261da177e4SLinus Torvalds 	struct atalk_addr sa, *ma, da;
7271da177e4SLinus Torvalds 	struct atalk_iface *ifa;
7281da177e4SLinus Torvalds 
729721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
730e730c155SEric W. Biederman 		goto out0;
731e730c155SEric W. Biederman 
7321da177e4SLinus Torvalds 	/* We only do Ethernet SNAP AARP. */
7331da177e4SLinus Torvalds 	if (dev->type != ARPHRD_ETHER)
7341da177e4SLinus Torvalds 		goto out0;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	/* Frame size ok? */
7371da177e4SLinus Torvalds 	if (!skb_pull(skb, sizeof(*ea)))
7381da177e4SLinus Torvalds 		goto out0;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	function = ntohs(ea->function);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* Sanity check fields. */
7431da177e4SLinus Torvalds 	if (function < AARP_REQUEST || function > AARP_PROBE ||
7441da177e4SLinus Torvalds 	    ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
7451da177e4SLinus Torvalds 	    ea->pa_src_zero || ea->pa_dst_zero)
7461da177e4SLinus Torvalds 		goto out0;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	/* Looks good. */
7491da177e4SLinus Torvalds 	hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	/* Build an address. */
7521da177e4SLinus Torvalds 	sa.s_node = ea->pa_src_node;
7531da177e4SLinus Torvalds 	sa.s_net = ea->pa_src_net;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/* Process the packet. Check for replies of me. */
7561da177e4SLinus Torvalds 	ifa = atalk_find_dev(dev);
7571da177e4SLinus Torvalds 	if (!ifa)
7581da177e4SLinus Torvalds 		goto out1;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (ifa->status & ATIF_PROBE &&
7611da177e4SLinus Torvalds 	    ifa->address.s_node == ea->pa_dst_node &&
7621da177e4SLinus Torvalds 	    ifa->address.s_net == ea->pa_dst_net) {
7631da177e4SLinus Torvalds 		ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
7641da177e4SLinus Torvalds 		goto out1;
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/* Check for replies of proxy AARP entries */
7681da177e4SLinus Torvalds 	da.s_node = ea->pa_dst_node;
7691da177e4SLinus Torvalds 	da.s_net  = ea->pa_dst_net;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
7721da177e4SLinus Torvalds 	a = __aarp_find_entry(proxies[hash], dev, &da);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (a && a->status & ATIF_PROBE) {
7751da177e4SLinus Torvalds 		a->status |= ATIF_PROBE_FAIL;
7761da177e4SLinus Torvalds 		/*
7771da177e4SLinus Torvalds 		 * we do not respond to probe or request packets for
7781da177e4SLinus Torvalds 		 * this address while we are probing this address
7791da177e4SLinus Torvalds 		 */
7801da177e4SLinus Torvalds 		goto unlock;
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	switch (function) {
7841da177e4SLinus Torvalds 	case AARP_REPLY:
7851da177e4SLinus Torvalds 		if (!unresolved_count)	/* Speed up */
7861da177e4SLinus Torvalds 			break;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 		/* Find the entry.  */
7891da177e4SLinus Torvalds 		a = __aarp_find_entry(unresolved[hash], dev, &sa);
7901da177e4SLinus Torvalds 		if (!a || dev != a->dev)
7911da177e4SLinus Torvalds 			break;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 		/* We can fill one in - this is good. */
79490ccb6aaSJoe Perches 		ether_addr_copy(a->hwaddr, ea->hw_src);
7951da177e4SLinus Torvalds 		__aarp_resolved(&unresolved[hash], a, hash);
7961da177e4SLinus Torvalds 		if (!unresolved_count)
7971da177e4SLinus Torvalds 			mod_timer(&aarp_timer,
7981da177e4SLinus Torvalds 				  jiffies + sysctl_aarp_expiry_time);
7991da177e4SLinus Torvalds 		break;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	case AARP_REQUEST:
8021da177e4SLinus Torvalds 	case AARP_PROBE:
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 		/*
8051da177e4SLinus Torvalds 		 * If it is my address set ma to my address and reply.
8061da177e4SLinus Torvalds 		 * We can treat probe and request the same.  Probe
8071da177e4SLinus Torvalds 		 * simply means we shouldn't cache the querying host,
8081da177e4SLinus Torvalds 		 * as in a probe they are proposing an address not
8091da177e4SLinus Torvalds 		 * using one.
8101da177e4SLinus Torvalds 		 *
8111da177e4SLinus Torvalds 		 * Support for proxy-AARP added. We check if the
8121da177e4SLinus Torvalds 		 * address is one of our proxies before we toss the
8131da177e4SLinus Torvalds 		 * packet out.
8141da177e4SLinus Torvalds 		 */
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 		sa.s_node = ea->pa_dst_node;
8171da177e4SLinus Torvalds 		sa.s_net  = ea->pa_dst_net;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		/* See if we have a matching proxy. */
8201da177e4SLinus Torvalds 		ma = __aarp_proxy_find(dev, &sa);
8211da177e4SLinus Torvalds 		if (!ma)
8221da177e4SLinus Torvalds 			ma = &ifa->address;
8231da177e4SLinus Torvalds 		else { /* We need to make a copy of the entry. */
8241da177e4SLinus Torvalds 			da.s_node = sa.s_node;
8252a04cd4cSRoel Kluin 			da.s_net = sa.s_net;
8261da177e4SLinus Torvalds 			ma = &da;
8271da177e4SLinus Torvalds 		}
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 		if (function == AARP_PROBE) {
8301da177e4SLinus Torvalds 			/*
8311da177e4SLinus Torvalds 			 * A probe implies someone trying to get an
8321da177e4SLinus Torvalds 			 * address. So as a precaution flush any
8331da177e4SLinus Torvalds 			 * entries we have for this address.
8341da177e4SLinus Torvalds 			 */
8351da177e4SLinus Torvalds 			a = __aarp_find_entry(resolved[sa.s_node %
8361da177e4SLinus Torvalds 						       (AARP_HASH_SIZE - 1)],
8371da177e4SLinus Torvalds 					      skb->dev, &sa);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 			/*
8401da177e4SLinus Torvalds 			 * Make it expire next tick - that avoids us
8411da177e4SLinus Torvalds 			 * getting into a probe/flush/learn/probe/
8421da177e4SLinus Torvalds 			 * flush/learn cycle during probing of a slow
8431da177e4SLinus Torvalds 			 * to respond host addr.
8441da177e4SLinus Torvalds 			 */
8451da177e4SLinus Torvalds 			if (a) {
8461da177e4SLinus Torvalds 				a->expires_at = jiffies - 1;
8471da177e4SLinus Torvalds 				mod_timer(&aarp_timer, jiffies +
8481da177e4SLinus Torvalds 					  sysctl_aarp_tick_time);
8491da177e4SLinus Torvalds 			}
8501da177e4SLinus Torvalds 		}
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 		if (sa.s_node != ma->s_node)
8531da177e4SLinus Torvalds 			break;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 		if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
8561da177e4SLinus Torvalds 			break;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		sa.s_node = ea->pa_src_node;
8591da177e4SLinus Torvalds 		sa.s_net = ea->pa_src_net;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		/* aarp_my_address has found the address to use for us.
8621da177e4SLinus Torvalds 		 */
8631da177e4SLinus Torvalds 		aarp_send_reply(dev, ma, &sa, ea->hw_src);
8641da177e4SLinus Torvalds 		break;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds unlock:
8681da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
8691da177e4SLinus Torvalds out1:
8701da177e4SLinus Torvalds 	ret = 1;
8711da177e4SLinus Torvalds out0:
8721da177e4SLinus Torvalds 	kfree_skb(skb);
8731da177e4SLinus Torvalds 	return ret;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds static struct notifier_block aarp_notifier = {
8771da177e4SLinus Torvalds 	.notifier_call = aarp_device_event,
8781da177e4SLinus Torvalds };
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds void __init aarp_proto_init(void)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
8851da177e4SLinus Torvalds 	if (!aarp_dl)
8861da177e4SLinus Torvalds 		printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
887e99e88a9SKees Cook 	timer_setup(&aarp_timer, aarp_expire_timeout, 0);
8881da177e4SLinus Torvalds 	aarp_timer.expires  = jiffies + sysctl_aarp_expiry_time;
8891da177e4SLinus Torvalds 	add_timer(&aarp_timer);
8901da177e4SLinus Torvalds 	register_netdevice_notifier(&aarp_notifier);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds /* Remove the AARP entries associated with a device. */
8941da177e4SLinus Torvalds void aarp_device_down(struct net_device *dev)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	int ct;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	write_lock_bh(&aarp_lock);
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
9011da177e4SLinus Torvalds 		__aarp_expire_device(&resolved[ct], dev);
9021da177e4SLinus Torvalds 		__aarp_expire_device(&unresolved[ct], dev);
9031da177e4SLinus Torvalds 		__aarp_expire_device(&proxies[ct], dev);
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	write_unlock_bh(&aarp_lock);
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
9101da177e4SLinus Torvalds /*
9111da177e4SLinus Torvalds  * Get the aarp entry that is in the chain described
9121da177e4SLinus Torvalds  * by the iterator.
9131da177e4SLinus Torvalds  * If pos is set then skip till that index.
9141da177e4SLinus Torvalds  * pos = 1 is the first entry
9151da177e4SLinus Torvalds  */
9161da177e4SLinus Torvalds static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	int ct = iter->bucket;
9191da177e4SLinus Torvalds 	struct aarp_entry **table = iter->table;
9201da177e4SLinus Torvalds 	loff_t off = 0;
9211da177e4SLinus Torvalds 	struct aarp_entry *entry;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds  rescan:
9241da177e4SLinus Torvalds 	while (ct < AARP_HASH_SIZE) {
9251da177e4SLinus Torvalds 		for (entry = table[ct]; entry; entry = entry->next) {
9261da177e4SLinus Torvalds 			if (!pos || ++off == *pos) {
9271da177e4SLinus Torvalds 				iter->table = table;
9281da177e4SLinus Torvalds 				iter->bucket = ct;
9291da177e4SLinus Torvalds 				return entry;
9301da177e4SLinus Torvalds 			}
9311da177e4SLinus Torvalds 		}
9321da177e4SLinus Torvalds 		++ct;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if (table == resolved) {
9361da177e4SLinus Torvalds 		ct = 0;
9371da177e4SLinus Torvalds 		table = unresolved;
9381da177e4SLinus Torvalds 		goto rescan;
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds 	if (table == unresolved) {
9411da177e4SLinus Torvalds 		ct = 0;
9421da177e4SLinus Torvalds 		table = proxies;
9431da177e4SLinus Torvalds 		goto rescan;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 	return NULL;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
949ca629f24SEric Dumazet 	__acquires(aarp_lock)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	struct aarp_iter_state *iter = seq->private;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	read_lock_bh(&aarp_lock);
9541da177e4SLinus Torvalds 	iter->table     = resolved;
9551da177e4SLinus Torvalds 	iter->bucket    = 0;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	struct aarp_entry *entry = v;
9631da177e4SLinus Torvalds 	struct aarp_iter_state *iter = seq->private;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	++*pos;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	/* first line after header */
9681da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
9691da177e4SLinus Torvalds 		entry = iter_next(iter, NULL);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	/* next entry in current bucket */
9721da177e4SLinus Torvalds 	else if (entry->next)
9731da177e4SLinus Torvalds 		entry = entry->next;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	/* next bucket or table */
9761da177e4SLinus Torvalds 	else {
9771da177e4SLinus Torvalds 		++iter->bucket;
9781da177e4SLinus Torvalds 		entry = iter_next(iter, NULL);
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 	return entry;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds static void aarp_seq_stop(struct seq_file *seq, void *v)
984ca629f24SEric Dumazet 	__releases(aarp_lock)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	read_unlock_bh(&aarp_lock);
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds static const char *dt2str(unsigned long ticks)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	static char buf[32];
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	return buf;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds static int aarp_seq_show(struct seq_file *seq, void *v)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	struct aarp_iter_state *iter = seq->private;
10011da177e4SLinus Torvalds 	struct aarp_entry *entry = v;
10021da177e4SLinus Torvalds 	unsigned long now = jiffies;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
10051da177e4SLinus Torvalds 		seq_puts(seq,
10061da177e4SLinus Torvalds 			 "Address  Interface   Hardware Address"
10071da177e4SLinus Torvalds 			 "   Expires LastSend  Retry Status\n");
10081da177e4SLinus Torvalds 	else {
10091da177e4SLinus Torvalds 		seq_printf(seq, "%04X:%02X  %-12s",
10101da177e4SLinus Torvalds 			   ntohs(entry->target_addr.s_net),
10111da177e4SLinus Torvalds 			   (unsigned int) entry->target_addr.s_node,
10121da177e4SLinus Torvalds 			   entry->dev ? entry->dev->name : "????");
1013e174961cSJohannes Berg 		seq_printf(seq, "%pM", entry->hwaddr);
10141da177e4SLinus Torvalds 		seq_printf(seq, " %8s",
10151da177e4SLinus Torvalds 			   dt2str((long)entry->expires_at - (long)now));
10161da177e4SLinus Torvalds 		if (iter->table == unresolved)
10171da177e4SLinus Torvalds 			seq_printf(seq, " %8s %6hu",
10181da177e4SLinus Torvalds 				   dt2str(now - entry->last_sent),
10191da177e4SLinus Torvalds 				   entry->xmit_count);
10201da177e4SLinus Torvalds 		else
10211da177e4SLinus Torvalds 			seq_puts(seq, "                ");
10221da177e4SLinus Torvalds 		seq_printf(seq, " %s\n",
10231da177e4SLinus Torvalds 			   (iter->table == resolved) ? "resolved"
10241da177e4SLinus Torvalds 			   : (iter->table == unresolved) ? "unresolved"
10251da177e4SLinus Torvalds 			   : (iter->table == proxies) ? "proxies"
10261da177e4SLinus Torvalds 			   : "unknown");
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 	return 0;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
1031*44414d82SChristoph Hellwig const struct seq_operations aarp_seq_ops = {
10321da177e4SLinus Torvalds 	.start  = aarp_seq_start,
10331da177e4SLinus Torvalds 	.next   = aarp_seq_next,
10341da177e4SLinus Torvalds 	.stop   = aarp_seq_stop,
10351da177e4SLinus Torvalds 	.show   = aarp_seq_show,
10361da177e4SLinus Torvalds };
10371da177e4SLinus Torvalds #endif
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds /* General module cleanup. Called from cleanup_module() in ddp.c. */
10401da177e4SLinus Torvalds void aarp_cleanup_module(void)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds 	del_timer_sync(&aarp_timer);
10431da177e4SLinus Torvalds 	unregister_netdevice_notifier(&aarp_notifier);
10441da177e4SLinus Torvalds 	unregister_snap_client(aarp_dl);
10451da177e4SLinus Torvalds 	aarp_purge();
10461da177e4SLinus Torvalds }
1047