xref: /openbmc/linux/net/core/neighbour.c (revision 5208debd)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Generic address resolution entity
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Authors:
51da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
61da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *	Fixes:
141da177e4SLinus Torvalds  *	Vitaly E. Lavrov	releasing NULL neighbor in neigh_add.
151da177e4SLinus Torvalds  *	Harald Welte		Add neighbour cache statistics like rtstat
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/socket.h>
221da177e4SLinus Torvalds #include <linux/sched.h>
231da177e4SLinus Torvalds #include <linux/netdevice.h>
241da177e4SLinus Torvalds #include <linux/proc_fs.h>
251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
261da177e4SLinus Torvalds #include <linux/sysctl.h>
271da177e4SLinus Torvalds #endif
281da177e4SLinus Torvalds #include <linux/times.h>
291da177e4SLinus Torvalds #include <net/neighbour.h>
301da177e4SLinus Torvalds #include <net/dst.h>
311da177e4SLinus Torvalds #include <net/sock.h>
328d71740cSTom Tucker #include <net/netevent.h>
33a14a49d2SThomas Graf #include <net/netlink.h>
341da177e4SLinus Torvalds #include <linux/rtnetlink.h>
351da177e4SLinus Torvalds #include <linux/random.h>
36543537bdSPaulo Marques #include <linux/string.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define NEIGH_DEBUG 1
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define NEIGH_PRINTK(x...) printk(x)
411da177e4SLinus Torvalds #define NEIGH_NOPRINTK(x...) do { ; } while(0)
421da177e4SLinus Torvalds #define NEIGH_PRINTK0 NEIGH_PRINTK
431da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_NOPRINTK
441da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_NOPRINTK
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #if NEIGH_DEBUG >= 1
471da177e4SLinus Torvalds #undef NEIGH_PRINTK1
481da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_PRINTK
491da177e4SLinus Torvalds #endif
501da177e4SLinus Torvalds #if NEIGH_DEBUG >= 2
511da177e4SLinus Torvalds #undef NEIGH_PRINTK2
521da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_PRINTK
531da177e4SLinus Torvalds #endif
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #define PNEIGH_HASHMASK		0xF
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg);
581da177e4SLinus Torvalds #ifdef CONFIG_ARPD
591da177e4SLinus Torvalds static void neigh_app_notify(struct neighbour *n);
601da177e4SLinus Torvalds #endif
611da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
621da177e4SLinus Torvalds void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static struct neigh_table *neigh_tables;
6545fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS
661da177e4SLinus Torvalds static struct file_operations neigh_stat_seq_fops;
6745fc3b11SAmos Waterland #endif
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds    Neighbour hash table buckets are protected with rwlock tbl->lock.
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds    - All the scans/updates to hash buckets MUST be made under this lock.
731da177e4SLinus Torvalds    - NOTHING clever should be made under this lock: no callbacks
741da177e4SLinus Torvalds      to protocol backends, no attempts to send something to network.
751da177e4SLinus Torvalds      It will result in deadlocks, if backend/driver wants to use neighbour
761da177e4SLinus Torvalds      cache.
771da177e4SLinus Torvalds    - If the entry requires some non-trivial actions, increase
781da177e4SLinus Torvalds      its reference count and release table lock.
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds    Neighbour entries are protected:
811da177e4SLinus Torvalds    - with reference count.
821da177e4SLinus Torvalds    - with rwlock neigh->lock
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds    Reference count prevents destruction.
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds    neigh->lock mainly serializes ll address data and its validity state.
871da177e4SLinus Torvalds    However, the same lock is used to protect another entry fields:
881da177e4SLinus Torvalds     - timer
891da177e4SLinus Torvalds     - resolution queue
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds    Again, nothing clever shall be made under neigh->lock,
921da177e4SLinus Torvalds    the most complicated procedure, which we allow is dev->hard_header.
931da177e4SLinus Torvalds    It is supposed, that dev->hard_header is simplistic and does
941da177e4SLinus Torvalds    not make callbacks to neighbour tables.
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds    The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
971da177e4SLinus Torvalds    list of neighbour tables. This list is used only in process context,
981da177e4SLinus Torvalds  */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static int neigh_blackhole(struct sk_buff *skb)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	kfree_skb(skb);
1051da177e4SLinus Torvalds 	return -ENETDOWN;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * It is random distribution in the interval (1/2)*base...(3/2)*base.
1101da177e4SLinus Torvalds  * It corresponds to default IPv6 settings and is not overridable,
1111da177e4SLinus Torvalds  * because it is really reasonable choice.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	return (base ? (net_random() % base) + (base >> 1) : 0);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	int shrunk = 0;
1231da177e4SLinus Torvalds 	int i;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
1281da177e4SLinus Torvalds 	for (i = 0; i <= tbl->hash_mask; i++) {
1291da177e4SLinus Torvalds 		struct neighbour *n, **np;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 		np = &tbl->hash_buckets[i];
1321da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
1331da177e4SLinus Torvalds 			/* Neighbour record may be discarded if:
1341da177e4SLinus Torvalds 			 * - nobody refers to it.
1351da177e4SLinus Torvalds 			 * - it is not permanent
1361da177e4SLinus Torvalds 			 */
1371da177e4SLinus Torvalds 			write_lock(&n->lock);
1381da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
1391da177e4SLinus Torvalds 			    !(n->nud_state & NUD_PERMANENT)) {
1401da177e4SLinus Torvalds 				*np	= n->next;
1411da177e4SLinus Torvalds 				n->dead = 1;
1421da177e4SLinus Torvalds 				shrunk	= 1;
1431da177e4SLinus Torvalds 				write_unlock(&n->lock);
1441da177e4SLinus Torvalds 				neigh_release(n);
1451da177e4SLinus Torvalds 				continue;
1461da177e4SLinus Torvalds 			}
1471da177e4SLinus Torvalds 			write_unlock(&n->lock);
1481da177e4SLinus Torvalds 			np = &n->next;
1491da177e4SLinus Torvalds 		}
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	tbl->last_flush = jiffies;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	return shrunk;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	if ((n->nud_state & NUD_IN_TIMER) &&
1621da177e4SLinus Torvalds 	    del_timer(&n->timer)) {
1631da177e4SLinus Torvalds 		neigh_release(n);
1641da177e4SLinus Torvalds 		return 1;
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 	return 0;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct sk_buff *skb;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL) {
1741da177e4SLinus Torvalds 		dev_put(skb->dev);
1751da177e4SLinus Torvalds 		kfree_skb(skb);
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
17949636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	int i;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	for (i = 0; i <= tbl->hash_mask; i++) {
1841da177e4SLinus Torvalds 		struct neighbour *n, **np = &tbl->hash_buckets[i];
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
1871da177e4SLinus Torvalds 			if (dev && n->dev != dev) {
1881da177e4SLinus Torvalds 				np = &n->next;
1891da177e4SLinus Torvalds 				continue;
1901da177e4SLinus Torvalds 			}
1911da177e4SLinus Torvalds 			*np = n->next;
1921da177e4SLinus Torvalds 			write_lock(&n->lock);
1931da177e4SLinus Torvalds 			neigh_del_timer(n);
1941da177e4SLinus Torvalds 			n->dead = 1;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) != 1) {
1971da177e4SLinus Torvalds 				/* The most unpleasant situation.
1981da177e4SLinus Torvalds 				   We must destroy neighbour entry,
1991da177e4SLinus Torvalds 				   but someone still uses it.
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 				   The destroy will be delayed until
2021da177e4SLinus Torvalds 				   the last user releases us, but
2031da177e4SLinus Torvalds 				   we must kill timers etc. and move
2041da177e4SLinus Torvalds 				   it to safe state.
2051da177e4SLinus Torvalds 				 */
2061da177e4SLinus Torvalds 				skb_queue_purge(&n->arp_queue);
2071da177e4SLinus Torvalds 				n->output = neigh_blackhole;
2081da177e4SLinus Torvalds 				if (n->nud_state & NUD_VALID)
2091da177e4SLinus Torvalds 					n->nud_state = NUD_NOARP;
2101da177e4SLinus Torvalds 				else
2111da177e4SLinus Torvalds 					n->nud_state = NUD_NONE;
2121da177e4SLinus Torvalds 				NEIGH_PRINTK2("neigh %p is stray.\n", n);
2131da177e4SLinus Torvalds 			}
2141da177e4SLinus Torvalds 			write_unlock(&n->lock);
2151da177e4SLinus Torvalds 			neigh_release(n);
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 	}
21849636bb1SHerbert Xu }
2191da177e4SLinus Torvalds 
22049636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
22149636bb1SHerbert Xu {
22249636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
22349636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
22449636bb1SHerbert Xu 	write_unlock_bh(&tbl->lock);
22549636bb1SHerbert Xu }
22649636bb1SHerbert Xu 
22749636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
22849636bb1SHerbert Xu {
22949636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
23049636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
2311da177e4SLinus Torvalds 	pneigh_ifdown(tbl, dev);
2321da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
2351da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
2361da177e4SLinus Torvalds 	return 0;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static struct neighbour *neigh_alloc(struct neigh_table *tbl)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	struct neighbour *n = NULL;
2421da177e4SLinus Torvalds 	unsigned long now = jiffies;
2431da177e4SLinus Torvalds 	int entries;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	entries = atomic_inc_return(&tbl->entries) - 1;
2461da177e4SLinus Torvalds 	if (entries >= tbl->gc_thresh3 ||
2471da177e4SLinus Torvalds 	    (entries >= tbl->gc_thresh2 &&
2481da177e4SLinus Torvalds 	     time_after(now, tbl->last_flush + 5 * HZ))) {
2491da177e4SLinus Torvalds 		if (!neigh_forced_gc(tbl) &&
2501da177e4SLinus Torvalds 		    entries >= tbl->gc_thresh3)
2511da177e4SLinus Torvalds 			goto out_entries;
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	n = kmem_cache_alloc(tbl->kmem_cachep, SLAB_ATOMIC);
2551da177e4SLinus Torvalds 	if (!n)
2561da177e4SLinus Torvalds 		goto out_entries;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	memset(n, 0, tbl->entry_size);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	skb_queue_head_init(&n->arp_queue);
2611da177e4SLinus Torvalds 	rwlock_init(&n->lock);
2621da177e4SLinus Torvalds 	n->updated	  = n->used = now;
2631da177e4SLinus Torvalds 	n->nud_state	  = NUD_NONE;
2641da177e4SLinus Torvalds 	n->output	  = neigh_blackhole;
2651da177e4SLinus Torvalds 	n->parms	  = neigh_parms_clone(&tbl->parms);
2661da177e4SLinus Torvalds 	init_timer(&n->timer);
2671da177e4SLinus Torvalds 	n->timer.function = neigh_timer_handler;
2681da177e4SLinus Torvalds 	n->timer.data	  = (unsigned long)n;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, allocs);
2711da177e4SLinus Torvalds 	n->tbl		  = tbl;
2721da177e4SLinus Torvalds 	atomic_set(&n->refcnt, 1);
2731da177e4SLinus Torvalds 	n->dead		  = 1;
2741da177e4SLinus Torvalds out:
2751da177e4SLinus Torvalds 	return n;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds out_entries:
2781da177e4SLinus Torvalds 	atomic_dec(&tbl->entries);
2791da177e4SLinus Torvalds 	goto out;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static struct neighbour **neigh_hash_alloc(unsigned int entries)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	unsigned long size = entries * sizeof(struct neighbour *);
2851da177e4SLinus Torvalds 	struct neighbour **ret;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	if (size <= PAGE_SIZE) {
28877d04bd9SAndrew Morton 		ret = kzalloc(size, GFP_ATOMIC);
2891da177e4SLinus Torvalds 	} else {
2901da177e4SLinus Torvalds 		ret = (struct neighbour **)
29177d04bd9SAndrew Morton 		      __get_free_pages(GFP_ATOMIC|__GFP_ZERO, get_order(size));
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 	return ret;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static void neigh_hash_free(struct neighbour **hash, unsigned int entries)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	unsigned long size = entries * sizeof(struct neighbour *);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	if (size <= PAGE_SIZE)
3011da177e4SLinus Torvalds 		kfree(hash);
3021da177e4SLinus Torvalds 	else
3031da177e4SLinus Torvalds 		free_pages((unsigned long)hash, get_order(size));
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	struct neighbour **new_hash, **old_hash;
3091da177e4SLinus Torvalds 	unsigned int i, new_hash_mask, old_entries;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	BUG_ON(new_entries & (new_entries - 1));
3141da177e4SLinus Torvalds 	new_hash = neigh_hash_alloc(new_entries);
3151da177e4SLinus Torvalds 	if (!new_hash)
3161da177e4SLinus Torvalds 		return;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	old_entries = tbl->hash_mask + 1;
3191da177e4SLinus Torvalds 	new_hash_mask = new_entries - 1;
3201da177e4SLinus Torvalds 	old_hash = tbl->hash_buckets;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
3231da177e4SLinus Torvalds 	for (i = 0; i < old_entries; i++) {
3241da177e4SLinus Torvalds 		struct neighbour *n, *next;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		for (n = old_hash[i]; n; n = next) {
3271da177e4SLinus Torvalds 			unsigned int hash_val = tbl->hash(n->primary_key, n->dev);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 			hash_val &= new_hash_mask;
3301da177e4SLinus Torvalds 			next = n->next;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 			n->next = new_hash[hash_val];
3331da177e4SLinus Torvalds 			new_hash[hash_val] = n;
3341da177e4SLinus Torvalds 		}
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	tbl->hash_buckets = new_hash;
3371da177e4SLinus Torvalds 	tbl->hash_mask = new_hash_mask;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	neigh_hash_free(old_hash, old_entries);
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
3431da177e4SLinus Torvalds 			       struct net_device *dev)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	struct neighbour *n;
3461da177e4SLinus Torvalds 	int key_len = tbl->key_len;
3471da177e4SLinus Torvalds 	u32 hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
3521da177e4SLinus Torvalds 	for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
3531da177e4SLinus Torvalds 		if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
3541da177e4SLinus Torvalds 			neigh_hold(n);
3551da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
3561da177e4SLinus Torvalds 			break;
3571da177e4SLinus Torvalds 		}
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 	read_unlock_bh(&tbl->lock);
3601da177e4SLinus Torvalds 	return n;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, const void *pkey)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	struct neighbour *n;
3661da177e4SLinus Torvalds 	int key_len = tbl->key_len;
3671da177e4SLinus Torvalds 	u32 hash_val = tbl->hash(pkey, NULL) & tbl->hash_mask;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
3721da177e4SLinus Torvalds 	for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
3731da177e4SLinus Torvalds 		if (!memcmp(n->primary_key, pkey, key_len)) {
3741da177e4SLinus Torvalds 			neigh_hold(n);
3751da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
3761da177e4SLinus Torvalds 			break;
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 	read_unlock_bh(&tbl->lock);
3801da177e4SLinus Torvalds 	return n;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
3841da177e4SLinus Torvalds 			       struct net_device *dev)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	u32 hash_val;
3871da177e4SLinus Torvalds 	int key_len = tbl->key_len;
3881da177e4SLinus Torvalds 	int error;
3891da177e4SLinus Torvalds 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if (!n) {
3921da177e4SLinus Torvalds 		rc = ERR_PTR(-ENOBUFS);
3931da177e4SLinus Torvalds 		goto out;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	memcpy(n->primary_key, pkey, key_len);
3971da177e4SLinus Torvalds 	n->dev = dev;
3981da177e4SLinus Torvalds 	dev_hold(dev);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	/* Protocol specific setup. */
4011da177e4SLinus Torvalds 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
4021da177e4SLinus Torvalds 		rc = ERR_PTR(error);
4031da177e4SLinus Torvalds 		goto out_neigh_release;
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	/* Device specific setup. */
4071da177e4SLinus Torvalds 	if (n->parms->neigh_setup &&
4081da177e4SLinus Torvalds 	    (error = n->parms->neigh_setup(n)) < 0) {
4091da177e4SLinus Torvalds 		rc = ERR_PTR(error);
4101da177e4SLinus Torvalds 		goto out_neigh_release;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries) > (tbl->hash_mask + 1))
4181da177e4SLinus Torvalds 		neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	if (n->parms->dead) {
4231da177e4SLinus Torvalds 		rc = ERR_PTR(-EINVAL);
4241da177e4SLinus Torvalds 		goto out_tbl_unlock;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) {
4281da177e4SLinus Torvalds 		if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
4291da177e4SLinus Torvalds 			neigh_hold(n1);
4301da177e4SLinus Torvalds 			rc = n1;
4311da177e4SLinus Torvalds 			goto out_tbl_unlock;
4321da177e4SLinus Torvalds 		}
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	n->next = tbl->hash_buckets[hash_val];
4361da177e4SLinus Torvalds 	tbl->hash_buckets[hash_val] = n;
4371da177e4SLinus Torvalds 	n->dead = 0;
4381da177e4SLinus Torvalds 	neigh_hold(n);
4391da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
4401da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is created.\n", n);
4411da177e4SLinus Torvalds 	rc = n;
4421da177e4SLinus Torvalds out:
4431da177e4SLinus Torvalds 	return rc;
4441da177e4SLinus Torvalds out_tbl_unlock:
4451da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
4461da177e4SLinus Torvalds out_neigh_release:
4471da177e4SLinus Torvalds 	neigh_release(n);
4481da177e4SLinus Torvalds 	goto out;
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, const void *pkey,
4521da177e4SLinus Torvalds 				    struct net_device *dev, int creat)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct pneigh_entry *n;
4551da177e4SLinus Torvalds 	int key_len = tbl->key_len;
4561da177e4SLinus Torvalds 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	hash_val ^= (hash_val >> 16);
4591da177e4SLinus Torvalds 	hash_val ^= hash_val >> 8;
4601da177e4SLinus Torvalds 	hash_val ^= hash_val >> 4;
4611da177e4SLinus Torvalds 	hash_val &= PNEIGH_HASHMASK;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	for (n = tbl->phash_buckets[hash_val]; n; n = n->next) {
4661da177e4SLinus Torvalds 		if (!memcmp(n->key, pkey, key_len) &&
4671da177e4SLinus Torvalds 		    (n->dev == dev || !n->dev)) {
4681da177e4SLinus Torvalds 			read_unlock_bh(&tbl->lock);
4691da177e4SLinus Torvalds 			goto out;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 	read_unlock_bh(&tbl->lock);
4731da177e4SLinus Torvalds 	n = NULL;
4741da177e4SLinus Torvalds 	if (!creat)
4751da177e4SLinus Torvalds 		goto out;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
4781da177e4SLinus Torvalds 	if (!n)
4791da177e4SLinus Torvalds 		goto out;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	memcpy(n->key, pkey, key_len);
4821da177e4SLinus Torvalds 	n->dev = dev;
4831da177e4SLinus Torvalds 	if (dev)
4841da177e4SLinus Torvalds 		dev_hold(dev);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	if (tbl->pconstructor && tbl->pconstructor(n)) {
4871da177e4SLinus Torvalds 		if (dev)
4881da177e4SLinus Torvalds 			dev_put(dev);
4891da177e4SLinus Torvalds 		kfree(n);
4901da177e4SLinus Torvalds 		n = NULL;
4911da177e4SLinus Torvalds 		goto out;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
4951da177e4SLinus Torvalds 	n->next = tbl->phash_buckets[hash_val];
4961da177e4SLinus Torvalds 	tbl->phash_buckets[hash_val] = n;
4971da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
4981da177e4SLinus Torvalds out:
4991da177e4SLinus Torvalds 	return n;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds int pneigh_delete(struct neigh_table *tbl, const void *pkey,
5041da177e4SLinus Torvalds 		  struct net_device *dev)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
5071da177e4SLinus Torvalds 	int key_len = tbl->key_len;
5081da177e4SLinus Torvalds 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	hash_val ^= (hash_val >> 16);
5111da177e4SLinus Torvalds 	hash_val ^= hash_val >> 8;
5121da177e4SLinus Torvalds 	hash_val ^= hash_val >> 4;
5131da177e4SLinus Torvalds 	hash_val &= PNEIGH_HASHMASK;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
5161da177e4SLinus Torvalds 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
5171da177e4SLinus Torvalds 	     np = &n->next) {
5181da177e4SLinus Torvalds 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev) {
5191da177e4SLinus Torvalds 			*np = n->next;
5201da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
5211da177e4SLinus Torvalds 			if (tbl->pdestructor)
5221da177e4SLinus Torvalds 				tbl->pdestructor(n);
5231da177e4SLinus Torvalds 			if (n->dev)
5241da177e4SLinus Torvalds 				dev_put(n->dev);
5251da177e4SLinus Torvalds 			kfree(n);
5261da177e4SLinus Torvalds 			return 0;
5271da177e4SLinus Torvalds 		}
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
5301da177e4SLinus Torvalds 	return -ENOENT;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
5361da177e4SLinus Torvalds 	u32 h;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
5391da177e4SLinus Torvalds 		np = &tbl->phash_buckets[h];
5401da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
5411da177e4SLinus Torvalds 			if (!dev || n->dev == dev) {
5421da177e4SLinus Torvalds 				*np = n->next;
5431da177e4SLinus Torvalds 				if (tbl->pdestructor)
5441da177e4SLinus Torvalds 					tbl->pdestructor(n);
5451da177e4SLinus Torvalds 				if (n->dev)
5461da177e4SLinus Torvalds 					dev_put(n->dev);
5471da177e4SLinus Torvalds 				kfree(n);
5481da177e4SLinus Torvalds 				continue;
5491da177e4SLinus Torvalds 			}
5501da177e4SLinus Torvalds 			np = &n->next;
5511da177e4SLinus Torvalds 		}
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 	return -ENOENT;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
5581da177e4SLinus Torvalds  *	neighbour must already be out of the table;
5591da177e4SLinus Torvalds  *
5601da177e4SLinus Torvalds  */
5611da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	struct hh_cache *hh;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	if (!neigh->dead) {
5681da177e4SLinus Torvalds 		printk(KERN_WARNING
5691da177e4SLinus Torvalds 		       "Destroying alive neighbour %p\n", neigh);
5701da177e4SLinus Torvalds 		dump_stack();
5711da177e4SLinus Torvalds 		return;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if (neigh_del_timer(neigh))
5751da177e4SLinus Torvalds 		printk(KERN_WARNING "Impossible event.\n");
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	while ((hh = neigh->hh) != NULL) {
5781da177e4SLinus Torvalds 		neigh->hh = hh->hh_next;
5791da177e4SLinus Torvalds 		hh->hh_next = NULL;
5801da177e4SLinus Torvalds 		write_lock_bh(&hh->hh_lock);
5811da177e4SLinus Torvalds 		hh->hh_output = neigh_blackhole;
5821da177e4SLinus Torvalds 		write_unlock_bh(&hh->hh_lock);
5831da177e4SLinus Torvalds 		if (atomic_dec_and_test(&hh->hh_refcnt))
5841da177e4SLinus Torvalds 			kfree(hh);
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
587c5ecd62cSMichael S. Tsirkin 	if (neigh->parms->neigh_destructor)
588c5ecd62cSMichael S. Tsirkin 		(neigh->parms->neigh_destructor)(neigh);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	skb_queue_purge(&neigh->arp_queue);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	dev_put(neigh->dev);
5931da177e4SLinus Torvalds 	neigh_parms_put(neigh->parms);
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	atomic_dec(&neigh->tbl->entries);
5981da177e4SLinus Torvalds 	kmem_cache_free(neigh->tbl->kmem_cachep, neigh);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds /* Neighbour state is suspicious;
6021da177e4SLinus Torvalds    disable fast path.
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds    Called with write_locked neigh.
6051da177e4SLinus Torvalds  */
6061da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	struct hh_cache *hh;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	neigh->output = neigh->ops->output;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	for (hh = neigh->hh; hh; hh = hh->hh_next)
6151da177e4SLinus Torvalds 		hh->hh_output = neigh->ops->output;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds /* Neighbour state is OK;
6191da177e4SLinus Torvalds    enable fast path.
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds    Called with write_locked neigh.
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	struct hh_cache *hh;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	neigh->output = neigh->ops->connected_output;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	for (hh = neigh->hh; hh; hh = hh->hh_next)
6321da177e4SLinus Torvalds 		hh->hh_output = neigh->ops->hh_output;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds static void neigh_periodic_timer(unsigned long arg)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	struct neigh_table *tbl = (struct neigh_table *)arg;
6381da177e4SLinus Torvalds 	struct neighbour *n, **np;
6391da177e4SLinus Torvalds 	unsigned long expire, now = jiffies;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	write_lock(&tbl->lock);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/*
6461da177e4SLinus Torvalds 	 *	periodically recompute ReachableTime from random function
6471da177e4SLinus Torvalds 	 */
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	if (time_after(now, tbl->last_rand + 300 * HZ)) {
6501da177e4SLinus Torvalds 		struct neigh_parms *p;
6511da177e4SLinus Torvalds 		tbl->last_rand = now;
6521da177e4SLinus Torvalds 		for (p = &tbl->parms; p; p = p->next)
6531da177e4SLinus Torvalds 			p->reachable_time =
6541da177e4SLinus Torvalds 				neigh_rand_reach_time(p->base_reachable_time);
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	np = &tbl->hash_buckets[tbl->hash_chain_gc];
6581da177e4SLinus Torvalds 	tbl->hash_chain_gc = ((tbl->hash_chain_gc + 1) & tbl->hash_mask);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	while ((n = *np) != NULL) {
6611da177e4SLinus Torvalds 		unsigned int state;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 		write_lock(&n->lock);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 		state = n->nud_state;
6661da177e4SLinus Torvalds 		if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
6671da177e4SLinus Torvalds 			write_unlock(&n->lock);
6681da177e4SLinus Torvalds 			goto next_elt;
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		if (time_before(n->used, n->confirmed))
6721da177e4SLinus Torvalds 			n->used = n->confirmed;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		if (atomic_read(&n->refcnt) == 1 &&
6751da177e4SLinus Torvalds 		    (state == NUD_FAILED ||
6761da177e4SLinus Torvalds 		     time_after(now, n->used + n->parms->gc_staletime))) {
6771da177e4SLinus Torvalds 			*np = n->next;
6781da177e4SLinus Torvalds 			n->dead = 1;
6791da177e4SLinus Torvalds 			write_unlock(&n->lock);
6801da177e4SLinus Torvalds 			neigh_release(n);
6811da177e4SLinus Torvalds 			continue;
6821da177e4SLinus Torvalds 		}
6831da177e4SLinus Torvalds 		write_unlock(&n->lock);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds next_elt:
6861da177e4SLinus Torvalds 		np = &n->next;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds  	/* Cycle through all hash buckets every base_reachable_time/2 ticks.
6901da177e4SLinus Torvalds  	 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
6911da177e4SLinus Torvalds  	 * base_reachable_time.
6921da177e4SLinus Torvalds 	 */
6931da177e4SLinus Torvalds 	expire = tbl->parms.base_reachable_time >> 1;
6941da177e4SLinus Torvalds 	expire /= (tbl->hash_mask + 1);
6951da177e4SLinus Torvalds 	if (!expire)
6961da177e4SLinus Torvalds 		expire = 1;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds  	mod_timer(&tbl->gc_timer, now + expire);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	write_unlock(&tbl->lock);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n)
7041da177e4SLinus Torvalds {
7051da177e4SLinus Torvalds 	struct neigh_parms *p = n->parms;
7061da177e4SLinus Torvalds 	return (n->nud_state & NUD_PROBE ?
7071da177e4SLinus Torvalds 		p->ucast_probes :
7081da177e4SLinus Torvalds 		p->ucast_probes + p->app_probes + p->mcast_probes);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
711667347f1SDavid S. Miller static inline void neigh_add_timer(struct neighbour *n, unsigned long when)
712667347f1SDavid S. Miller {
713667347f1SDavid S. Miller 	if (unlikely(mod_timer(&n->timer, when))) {
714667347f1SDavid S. Miller 		printk("NEIGH: BUG, double timer add, state is %x\n",
715667347f1SDavid S. Miller 		       n->nud_state);
71620375502SHerbert Xu 		dump_stack();
717667347f1SDavid S. Miller 	}
718667347f1SDavid S. Miller }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	unsigned long now, next;
7251da177e4SLinus Torvalds 	struct neighbour *neigh = (struct neighbour *)arg;
7261da177e4SLinus Torvalds 	unsigned state;
7271da177e4SLinus Torvalds 	int notify = 0;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	write_lock(&neigh->lock);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	state = neigh->nud_state;
7321da177e4SLinus Torvalds 	now = jiffies;
7331da177e4SLinus Torvalds 	next = now + HZ;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (!(state & NUD_IN_TIMER)) {
7361da177e4SLinus Torvalds #ifndef CONFIG_SMP
7371da177e4SLinus Torvalds 		printk(KERN_WARNING "neigh: timer & !nud_in_timer\n");
7381da177e4SLinus Torvalds #endif
7391da177e4SLinus Torvalds 		goto out;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	if (state & NUD_REACHABLE) {
7431da177e4SLinus Torvalds 		if (time_before_eq(now,
7441da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->reachable_time)) {
7451da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
7461da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
7471da177e4SLinus Torvalds 		} else if (time_before_eq(now,
7481da177e4SLinus Torvalds 					  neigh->used + neigh->parms->delay_probe_time)) {
7491da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
7501da177e4SLinus Torvalds 			neigh->nud_state = NUD_DELAY;
751955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
7521da177e4SLinus Torvalds 			neigh_suspect(neigh);
7531da177e4SLinus Torvalds 			next = now + neigh->parms->delay_probe_time;
7541da177e4SLinus Torvalds 		} else {
7551da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
7561da177e4SLinus Torvalds 			neigh->nud_state = NUD_STALE;
757955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
7581da177e4SLinus Torvalds 			neigh_suspect(neigh);
7598d71740cSTom Tucker 			notify = 1;
7601da177e4SLinus Torvalds 		}
7611da177e4SLinus Torvalds 	} else if (state & NUD_DELAY) {
7621da177e4SLinus Torvalds 		if (time_before_eq(now,
7631da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->delay_probe_time)) {
7641da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh);
7651da177e4SLinus Torvalds 			neigh->nud_state = NUD_REACHABLE;
766955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
7671da177e4SLinus Torvalds 			neigh_connect(neigh);
7688d71740cSTom Tucker 			notify = 1;
7691da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
7701da177e4SLinus Torvalds 		} else {
7711da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
7721da177e4SLinus Torvalds 			neigh->nud_state = NUD_PROBE;
773955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
7741da177e4SLinus Torvalds 			atomic_set(&neigh->probes, 0);
7751da177e4SLinus Torvalds 			next = now + neigh->parms->retrans_time;
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 	} else {
7781da177e4SLinus Torvalds 		/* NUD_PROBE|NUD_INCOMPLETE */
7791da177e4SLinus Torvalds 		next = now + neigh->parms->retrans_time;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
7831da177e4SLinus Torvalds 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
7841da177e4SLinus Torvalds 		struct sk_buff *skb;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 		neigh->nud_state = NUD_FAILED;
787955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
7881da177e4SLinus Torvalds 		notify = 1;
7891da177e4SLinus Torvalds 		NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
7901da177e4SLinus Torvalds 		NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 		/* It is very thin place. report_unreachable is very complicated
7931da177e4SLinus Torvalds 		   routine. Particularly, it can hit the same neighbour entry!
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		   So that, we try to be accurate and avoid dead loop. --ANK
7961da177e4SLinus Torvalds 		 */
7971da177e4SLinus Torvalds 		while (neigh->nud_state == NUD_FAILED &&
7981da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
7991da177e4SLinus Torvalds 			write_unlock(&neigh->lock);
8001da177e4SLinus Torvalds 			neigh->ops->error_report(neigh, skb);
8011da177e4SLinus Torvalds 			write_lock(&neigh->lock);
8021da177e4SLinus Torvalds 		}
8031da177e4SLinus Torvalds 		skb_queue_purge(&neigh->arp_queue);
8041da177e4SLinus Torvalds 	}
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	if (neigh->nud_state & NUD_IN_TIMER) {
8071da177e4SLinus Torvalds 		if (time_before(next, jiffies + HZ/2))
8081da177e4SLinus Torvalds 			next = jiffies + HZ/2;
8096fb9974fSHerbert Xu 		if (!mod_timer(&neigh->timer, next))
8106fb9974fSHerbert Xu 			neigh_hold(neigh);
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
8131da177e4SLinus Torvalds 		struct sk_buff *skb = skb_peek(&neigh->arp_queue);
8141da177e4SLinus Torvalds 		/* keep skb alive even if arp_queue overflows */
8151da177e4SLinus Torvalds 		if (skb)
8161da177e4SLinus Torvalds 			skb_get(skb);
8171da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
8181da177e4SLinus Torvalds 		neigh->ops->solicit(neigh, skb);
8191da177e4SLinus Torvalds 		atomic_inc(&neigh->probes);
8201da177e4SLinus Torvalds 		if (skb)
8211da177e4SLinus Torvalds 			kfree_skb(skb);
8221da177e4SLinus Torvalds 	} else {
8231da177e4SLinus Torvalds out:
8241da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
8251da177e4SLinus Torvalds 	}
8268d71740cSTom Tucker 	if (notify)
8278d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds #ifdef CONFIG_ARPD
8301da177e4SLinus Torvalds 	if (notify && neigh->parms->app_probes)
8311da177e4SLinus Torvalds 		neigh_app_notify(neigh);
8321da177e4SLinus Torvalds #endif
8331da177e4SLinus Torvalds 	neigh_release(neigh);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	int rc;
8391da177e4SLinus Torvalds 	unsigned long now;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	rc = 0;
8441da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
8451da177e4SLinus Torvalds 		goto out_unlock_bh;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	now = jiffies;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
8501da177e4SLinus Torvalds 		if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
8511da177e4SLinus Torvalds 			atomic_set(&neigh->probes, neigh->parms->ucast_probes);
8521da177e4SLinus Torvalds 			neigh->nud_state     = NUD_INCOMPLETE;
853955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
8541da177e4SLinus Torvalds 			neigh_hold(neigh);
855667347f1SDavid S. Miller 			neigh_add_timer(neigh, now + 1);
8561da177e4SLinus Torvalds 		} else {
8571da177e4SLinus Torvalds 			neigh->nud_state = NUD_FAILED;
858955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
8591da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 			if (skb)
8621da177e4SLinus Torvalds 				kfree_skb(skb);
8631da177e4SLinus Torvalds 			return 1;
8641da177e4SLinus Torvalds 		}
8651da177e4SLinus Torvalds 	} else if (neigh->nud_state & NUD_STALE) {
8661da177e4SLinus Torvalds 		NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
8671da177e4SLinus Torvalds 		neigh_hold(neigh);
8681da177e4SLinus Torvalds 		neigh->nud_state = NUD_DELAY;
869955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
870667347f1SDavid S. Miller 		neigh_add_timer(neigh,
871667347f1SDavid S. Miller 				jiffies + neigh->parms->delay_probe_time);
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	if (neigh->nud_state == NUD_INCOMPLETE) {
8751da177e4SLinus Torvalds 		if (skb) {
8761da177e4SLinus Torvalds 			if (skb_queue_len(&neigh->arp_queue) >=
8771da177e4SLinus Torvalds 			    neigh->parms->queue_len) {
8781da177e4SLinus Torvalds 				struct sk_buff *buff;
8791da177e4SLinus Torvalds 				buff = neigh->arp_queue.next;
8801da177e4SLinus Torvalds 				__skb_unlink(buff, &neigh->arp_queue);
8811da177e4SLinus Torvalds 				kfree_skb(buff);
8821da177e4SLinus Torvalds 			}
8831da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->arp_queue, skb);
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 		rc = 1;
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds out_unlock_bh:
8881da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
8891da177e4SLinus Torvalds 	return rc;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds static __inline__ void neigh_update_hhs(struct neighbour *neigh)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	struct hh_cache *hh;
8951da177e4SLinus Torvalds 	void (*update)(struct hh_cache*, struct net_device*, unsigned char *) =
8961da177e4SLinus Torvalds 		neigh->dev->header_cache_update;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	if (update) {
8991da177e4SLinus Torvalds 		for (hh = neigh->hh; hh; hh = hh->hh_next) {
9001da177e4SLinus Torvalds 			write_lock_bh(&hh->hh_lock);
9011da177e4SLinus Torvalds 			update(hh, neigh->dev, neigh->ha);
9021da177e4SLinus Torvalds 			write_unlock_bh(&hh->hh_lock);
9031da177e4SLinus Torvalds 		}
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds /* Generic update routine.
9101da177e4SLinus Torvalds    -- lladdr is new lladdr or NULL, if it is not supplied.
9111da177e4SLinus Torvalds    -- new    is new state.
9121da177e4SLinus Torvalds    -- flags
9131da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
9141da177e4SLinus Torvalds 				if it is different.
9151da177e4SLinus Torvalds 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
9161da177e4SLinus Torvalds 				lladdr instead of overriding it
9171da177e4SLinus Torvalds 				if it is different.
9181da177e4SLinus Torvalds 				It also allows to retain current state
9191da177e4SLinus Torvalds 				if lladdr is unchanged.
9201da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
9231da177e4SLinus Torvalds 				NTF_ROUTER flag.
9241da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
9251da177e4SLinus Torvalds 				a router.
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds    Caller MUST hold reference count on the entry.
9281da177e4SLinus Torvalds  */
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
9311da177e4SLinus Torvalds 		 u32 flags)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	u8 old;
9341da177e4SLinus Torvalds 	int err;
9351da177e4SLinus Torvalds 	int notify = 0;
9361da177e4SLinus Torvalds 	struct net_device *dev;
9371da177e4SLinus Torvalds 	int update_isrouter = 0;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	dev    = neigh->dev;
9421da177e4SLinus Torvalds 	old    = neigh->nud_state;
9431da177e4SLinus Torvalds 	err    = -EPERM;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
9461da177e4SLinus Torvalds 	    (old & (NUD_NOARP | NUD_PERMANENT)))
9471da177e4SLinus Torvalds 		goto out;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (!(new & NUD_VALID)) {
9501da177e4SLinus Torvalds 		neigh_del_timer(neigh);
9511da177e4SLinus Torvalds 		if (old & NUD_CONNECTED)
9521da177e4SLinus Torvalds 			neigh_suspect(neigh);
9531da177e4SLinus Torvalds 		neigh->nud_state = new;
9541da177e4SLinus Torvalds 		err = 0;
9551da177e4SLinus Torvalds 		notify = old & NUD_VALID;
9561da177e4SLinus Torvalds 		goto out;
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/* Compare new lladdr with cached one */
9601da177e4SLinus Torvalds 	if (!dev->addr_len) {
9611da177e4SLinus Torvalds 		/* First case: device needs no address. */
9621da177e4SLinus Torvalds 		lladdr = neigh->ha;
9631da177e4SLinus Torvalds 	} else if (lladdr) {
9641da177e4SLinus Torvalds 		/* The second case: if something is already cached
9651da177e4SLinus Torvalds 		   and a new address is proposed:
9661da177e4SLinus Torvalds 		   - compare new & old
9671da177e4SLinus Torvalds 		   - if they are different, check override flag
9681da177e4SLinus Torvalds 		 */
9691da177e4SLinus Torvalds 		if ((old & NUD_VALID) &&
9701da177e4SLinus Torvalds 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
9711da177e4SLinus Torvalds 			lladdr = neigh->ha;
9721da177e4SLinus Torvalds 	} else {
9731da177e4SLinus Torvalds 		/* No address is supplied; if we know something,
9741da177e4SLinus Torvalds 		   use it, otherwise discard the request.
9751da177e4SLinus Torvalds 		 */
9761da177e4SLinus Torvalds 		err = -EINVAL;
9771da177e4SLinus Torvalds 		if (!(old & NUD_VALID))
9781da177e4SLinus Torvalds 			goto out;
9791da177e4SLinus Torvalds 		lladdr = neigh->ha;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
9831da177e4SLinus Torvalds 		neigh->confirmed = jiffies;
9841da177e4SLinus Torvalds 	neigh->updated = jiffies;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	/* If entry was valid and address is not changed,
9871da177e4SLinus Torvalds 	   do not change entry state, if new one is STALE.
9881da177e4SLinus Torvalds 	 */
9891da177e4SLinus Torvalds 	err = 0;
9901da177e4SLinus Torvalds 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
9911da177e4SLinus Torvalds 	if (old & NUD_VALID) {
9921da177e4SLinus Torvalds 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
9931da177e4SLinus Torvalds 			update_isrouter = 0;
9941da177e4SLinus Torvalds 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
9951da177e4SLinus Torvalds 			    (old & NUD_CONNECTED)) {
9961da177e4SLinus Torvalds 				lladdr = neigh->ha;
9971da177e4SLinus Torvalds 				new = NUD_STALE;
9981da177e4SLinus Torvalds 			} else
9991da177e4SLinus Torvalds 				goto out;
10001da177e4SLinus Torvalds 		} else {
10011da177e4SLinus Torvalds 			if (lladdr == neigh->ha && new == NUD_STALE &&
10021da177e4SLinus Torvalds 			    ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
10031da177e4SLinus Torvalds 			     (old & NUD_CONNECTED))
10041da177e4SLinus Torvalds 			    )
10051da177e4SLinus Torvalds 				new = old;
10061da177e4SLinus Torvalds 		}
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	if (new != old) {
10101da177e4SLinus Torvalds 		neigh_del_timer(neigh);
10111da177e4SLinus Torvalds 		if (new & NUD_IN_TIMER) {
10121da177e4SLinus Torvalds 			neigh_hold(neigh);
1013667347f1SDavid S. Miller 			neigh_add_timer(neigh, (jiffies +
10141da177e4SLinus Torvalds 						((new & NUD_REACHABLE) ?
1015667347f1SDavid S. Miller 						 neigh->parms->reachable_time :
1016667347f1SDavid S. Miller 						 0)));
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 		neigh->nud_state = new;
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	if (lladdr != neigh->ha) {
10221da177e4SLinus Torvalds 		memcpy(&neigh->ha, lladdr, dev->addr_len);
10231da177e4SLinus Torvalds 		neigh_update_hhs(neigh);
10241da177e4SLinus Torvalds 		if (!(new & NUD_CONNECTED))
10251da177e4SLinus Torvalds 			neigh->confirmed = jiffies -
10261da177e4SLinus Torvalds 				      (neigh->parms->base_reachable_time << 1);
10271da177e4SLinus Torvalds 		notify = 1;
10281da177e4SLinus Torvalds 	}
10291da177e4SLinus Torvalds 	if (new == old)
10301da177e4SLinus Torvalds 		goto out;
10311da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
10321da177e4SLinus Torvalds 		neigh_connect(neigh);
10331da177e4SLinus Torvalds 	else
10341da177e4SLinus Torvalds 		neigh_suspect(neigh);
10351da177e4SLinus Torvalds 	if (!(old & NUD_VALID)) {
10361da177e4SLinus Torvalds 		struct sk_buff *skb;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 		/* Again: avoid dead loop if something went wrong */
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 		while (neigh->nud_state & NUD_VALID &&
10411da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
10421da177e4SLinus Torvalds 			struct neighbour *n1 = neigh;
10431da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
10441da177e4SLinus Torvalds 			/* On shaper/eql skb->dst->neighbour != neigh :( */
10451da177e4SLinus Torvalds 			if (skb->dst && skb->dst->neighbour)
10461da177e4SLinus Torvalds 				n1 = skb->dst->neighbour;
10471da177e4SLinus Torvalds 			n1->output(skb);
10481da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 		skb_queue_purge(&neigh->arp_queue);
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds out:
10531da177e4SLinus Torvalds 	if (update_isrouter) {
10541da177e4SLinus Torvalds 		neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
10551da177e4SLinus Torvalds 			(neigh->flags | NTF_ROUTER) :
10561da177e4SLinus Torvalds 			(neigh->flags & ~NTF_ROUTER);
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
10598d71740cSTom Tucker 
10608d71740cSTom Tucker 	if (notify)
10618d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
10621da177e4SLinus Torvalds #ifdef CONFIG_ARPD
10631da177e4SLinus Torvalds 	if (notify && neigh->parms->app_probes)
10641da177e4SLinus Torvalds 		neigh_app_notify(neigh);
10651da177e4SLinus Torvalds #endif
10661da177e4SLinus Torvalds 	return err;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl,
10701da177e4SLinus Torvalds 				 u8 *lladdr, void *saddr,
10711da177e4SLinus Torvalds 				 struct net_device *dev)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
10741da177e4SLinus Torvalds 						 lladdr || !dev->addr_len);
10751da177e4SLinus Torvalds 	if (neigh)
10761da177e4SLinus Torvalds 		neigh_update(neigh, lladdr, NUD_STALE,
10771da177e4SLinus Torvalds 			     NEIGH_UPDATE_F_OVERRIDE);
10781da177e4SLinus Torvalds 	return neigh;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst,
10821da177e4SLinus Torvalds 			  u16 protocol)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	struct hh_cache	*hh;
10851da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	for (hh = n->hh; hh; hh = hh->hh_next)
10881da177e4SLinus Torvalds 		if (hh->hh_type == protocol)
10891da177e4SLinus Torvalds 			break;
10901da177e4SLinus Torvalds 
109177d04bd9SAndrew Morton 	if (!hh && (hh = kzalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) {
10921da177e4SLinus Torvalds 		rwlock_init(&hh->hh_lock);
10931da177e4SLinus Torvalds 		hh->hh_type = protocol;
10941da177e4SLinus Torvalds 		atomic_set(&hh->hh_refcnt, 0);
10951da177e4SLinus Torvalds 		hh->hh_next = NULL;
10961da177e4SLinus Torvalds 		if (dev->hard_header_cache(n, hh)) {
10971da177e4SLinus Torvalds 			kfree(hh);
10981da177e4SLinus Torvalds 			hh = NULL;
10991da177e4SLinus Torvalds 		} else {
11001da177e4SLinus Torvalds 			atomic_inc(&hh->hh_refcnt);
11011da177e4SLinus Torvalds 			hh->hh_next = n->hh;
11021da177e4SLinus Torvalds 			n->hh	    = hh;
11031da177e4SLinus Torvalds 			if (n->nud_state & NUD_CONNECTED)
11041da177e4SLinus Torvalds 				hh->hh_output = n->ops->hh_output;
11051da177e4SLinus Torvalds 			else
11061da177e4SLinus Torvalds 				hh->hh_output = n->ops->output;
11071da177e4SLinus Torvalds 		}
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 	if (hh)	{
11101da177e4SLinus Torvalds 		atomic_inc(&hh->hh_refcnt);
11111da177e4SLinus Torvalds 		dst->hh = hh;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit
11161da177e4SLinus Torvalds    worked, f.e. if you want to override normal output path (eql, shaper),
11171da177e4SLinus Torvalds    but resolution is not made yet.
11181da177e4SLinus Torvalds  */
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds int neigh_compat_output(struct sk_buff *skb)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	__skb_pull(skb, skb->nh.raw - skb->data);
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	if (dev->hard_header &&
11271da177e4SLinus Torvalds 	    dev->hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
11281da177e4SLinus Torvalds 		    	     skb->len) < 0 &&
11291da177e4SLinus Torvalds 	    dev->rebuild_header(skb))
11301da177e4SLinus Torvalds 		return 0;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	return dev_queue_xmit(skb);
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds /* Slow and careful. */
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds int neigh_resolve_output(struct sk_buff *skb)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	struct dst_entry *dst = skb->dst;
11401da177e4SLinus Torvalds 	struct neighbour *neigh;
11411da177e4SLinus Torvalds 	int rc = 0;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	if (!dst || !(neigh = dst->neighbour))
11441da177e4SLinus Torvalds 		goto discard;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	__skb_pull(skb, skb->nh.raw - skb->data);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	if (!neigh_event_send(neigh, skb)) {
11491da177e4SLinus Torvalds 		int err;
11501da177e4SLinus Torvalds 		struct net_device *dev = neigh->dev;
11511da177e4SLinus Torvalds 		if (dev->hard_header_cache && !dst->hh) {
11521da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
11531da177e4SLinus Torvalds 			if (!dst->hh)
11541da177e4SLinus Torvalds 				neigh_hh_init(neigh, dst, dst->ops->protocol);
11551da177e4SLinus Torvalds 			err = dev->hard_header(skb, dev, ntohs(skb->protocol),
11561da177e4SLinus Torvalds 					       neigh->ha, NULL, skb->len);
11571da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
11581da177e4SLinus Torvalds 		} else {
11591da177e4SLinus Torvalds 			read_lock_bh(&neigh->lock);
11601da177e4SLinus Torvalds 			err = dev->hard_header(skb, dev, ntohs(skb->protocol),
11611da177e4SLinus Torvalds 					       neigh->ha, NULL, skb->len);
11621da177e4SLinus Torvalds 			read_unlock_bh(&neigh->lock);
11631da177e4SLinus Torvalds 		}
11641da177e4SLinus Torvalds 		if (err >= 0)
11651da177e4SLinus Torvalds 			rc = neigh->ops->queue_xmit(skb);
11661da177e4SLinus Torvalds 		else
11671da177e4SLinus Torvalds 			goto out_kfree_skb;
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds out:
11701da177e4SLinus Torvalds 	return rc;
11711da177e4SLinus Torvalds discard:
11721da177e4SLinus Torvalds 	NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
11731da177e4SLinus Torvalds 		      dst, dst ? dst->neighbour : NULL);
11741da177e4SLinus Torvalds out_kfree_skb:
11751da177e4SLinus Torvalds 	rc = -EINVAL;
11761da177e4SLinus Torvalds 	kfree_skb(skb);
11771da177e4SLinus Torvalds 	goto out;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds /* As fast as possible without hh cache */
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds int neigh_connected_output(struct sk_buff *skb)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	int err;
11851da177e4SLinus Torvalds 	struct dst_entry *dst = skb->dst;
11861da177e4SLinus Torvalds 	struct neighbour *neigh = dst->neighbour;
11871da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	__skb_pull(skb, skb->nh.raw - skb->data);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	read_lock_bh(&neigh->lock);
11921da177e4SLinus Torvalds 	err = dev->hard_header(skb, dev, ntohs(skb->protocol),
11931da177e4SLinus Torvalds 			       neigh->ha, NULL, skb->len);
11941da177e4SLinus Torvalds 	read_unlock_bh(&neigh->lock);
11951da177e4SLinus Torvalds 	if (err >= 0)
11961da177e4SLinus Torvalds 		err = neigh->ops->queue_xmit(skb);
11971da177e4SLinus Torvalds 	else {
11981da177e4SLinus Torvalds 		err = -EINVAL;
11991da177e4SLinus Torvalds 		kfree_skb(skb);
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 	return err;
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	struct neigh_table *tbl = (struct neigh_table *)arg;
12071da177e4SLinus Torvalds 	long sched_next = 0;
12081da177e4SLinus Torvalds 	unsigned long now = jiffies;
12091da177e4SLinus Torvalds 	struct sk_buff *skb;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	skb = tbl->proxy_queue.next;
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	while (skb != (struct sk_buff *)&tbl->proxy_queue) {
12161da177e4SLinus Torvalds 		struct sk_buff *back = skb;
1217a61bbcf2SPatrick McHardy 		long tdif = NEIGH_CB(back)->sched_next - now;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 		skb = skb->next;
12201da177e4SLinus Torvalds 		if (tdif <= 0) {
12211da177e4SLinus Torvalds 			struct net_device *dev = back->dev;
12221da177e4SLinus Torvalds 			__skb_unlink(back, &tbl->proxy_queue);
12231da177e4SLinus Torvalds 			if (tbl->proxy_redo && netif_running(dev))
12241da177e4SLinus Torvalds 				tbl->proxy_redo(back);
12251da177e4SLinus Torvalds 			else
12261da177e4SLinus Torvalds 				kfree_skb(back);
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 			dev_put(dev);
12291da177e4SLinus Torvalds 		} else if (!sched_next || tdif < sched_next)
12301da177e4SLinus Torvalds 			sched_next = tdif;
12311da177e4SLinus Torvalds 	}
12321da177e4SLinus Torvalds 	del_timer(&tbl->proxy_timer);
12331da177e4SLinus Torvalds 	if (sched_next)
12341da177e4SLinus Torvalds 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
12351da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
12391da177e4SLinus Torvalds 		    struct sk_buff *skb)
12401da177e4SLinus Torvalds {
12411da177e4SLinus Torvalds 	unsigned long now = jiffies;
12421da177e4SLinus Torvalds 	unsigned long sched_next = now + (net_random() % p->proxy_delay);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	if (tbl->proxy_queue.qlen > p->proxy_qlen) {
12451da177e4SLinus Torvalds 		kfree_skb(skb);
12461da177e4SLinus Torvalds 		return;
12471da177e4SLinus Torvalds 	}
1248a61bbcf2SPatrick McHardy 
1249a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->sched_next = sched_next;
1250a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
12531da177e4SLinus Torvalds 	if (del_timer(&tbl->proxy_timer)) {
12541da177e4SLinus Torvalds 		if (time_before(tbl->proxy_timer.expires, sched_next))
12551da177e4SLinus Torvalds 			sched_next = tbl->proxy_timer.expires;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 	dst_release(skb->dst);
12581da177e4SLinus Torvalds 	skb->dst = NULL;
12591da177e4SLinus Torvalds 	dev_hold(skb->dev);
12601da177e4SLinus Torvalds 	__skb_queue_tail(&tbl->proxy_queue, skb);
12611da177e4SLinus Torvalds 	mod_timer(&tbl->proxy_timer, sched_next);
12621da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
12671da177e4SLinus Torvalds 				      struct neigh_table *tbl)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	struct neigh_parms *p = kmalloc(sizeof(*p), GFP_KERNEL);
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	if (p) {
12721da177e4SLinus Torvalds 		memcpy(p, &tbl->parms, sizeof(*p));
12731da177e4SLinus Torvalds 		p->tbl		  = tbl;
12741da177e4SLinus Torvalds 		atomic_set(&p->refcnt, 1);
12751da177e4SLinus Torvalds 		INIT_RCU_HEAD(&p->rcu_head);
12761da177e4SLinus Torvalds 		p->reachable_time =
12771da177e4SLinus Torvalds 				neigh_rand_reach_time(p->base_reachable_time);
1278c7fb64dbSThomas Graf 		if (dev) {
1279c7fb64dbSThomas Graf 			if (dev->neigh_setup && dev->neigh_setup(dev, p)) {
12801da177e4SLinus Torvalds 				kfree(p);
12811da177e4SLinus Torvalds 				return NULL;
12821da177e4SLinus Torvalds 			}
1283c7fb64dbSThomas Graf 
1284c7fb64dbSThomas Graf 			dev_hold(dev);
1285c7fb64dbSThomas Graf 			p->dev = dev;
1286c7fb64dbSThomas Graf 		}
12871da177e4SLinus Torvalds 		p->sysctl_table = NULL;
12881da177e4SLinus Torvalds 		write_lock_bh(&tbl->lock);
12891da177e4SLinus Torvalds 		p->next		= tbl->parms.next;
12901da177e4SLinus Torvalds 		tbl->parms.next = p;
12911da177e4SLinus Torvalds 		write_unlock_bh(&tbl->lock);
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 	return p;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds 	struct neigh_parms *parms =
12991da177e4SLinus Torvalds 		container_of(head, struct neigh_parms, rcu_head);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	neigh_parms_put(parms);
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
13051da177e4SLinus Torvalds {
13061da177e4SLinus Torvalds 	struct neigh_parms **p;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	if (!parms || parms == &tbl->parms)
13091da177e4SLinus Torvalds 		return;
13101da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
13111da177e4SLinus Torvalds 	for (p = &tbl->parms.next; *p; p = &(*p)->next) {
13121da177e4SLinus Torvalds 		if (*p == parms) {
13131da177e4SLinus Torvalds 			*p = parms->next;
13141da177e4SLinus Torvalds 			parms->dead = 1;
13151da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
1316c7fb64dbSThomas Graf 			if (parms->dev)
1317c7fb64dbSThomas Graf 				dev_put(parms->dev);
13181da177e4SLinus Torvalds 			call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
13191da177e4SLinus Torvalds 			return;
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
13231da177e4SLinus Torvalds 	NEIGH_PRINTK1("neigh_parms_release: not found\n");
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds void neigh_parms_destroy(struct neigh_parms *parms)
13271da177e4SLinus Torvalds {
13281da177e4SLinus Torvalds 	kfree(parms);
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
1331bd89efc5SSimon Kelley void neigh_table_init_no_netlink(struct neigh_table *tbl)
13321da177e4SLinus Torvalds {
13331da177e4SLinus Torvalds 	unsigned long now = jiffies;
13341da177e4SLinus Torvalds 	unsigned long phsize;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	atomic_set(&tbl->parms.refcnt, 1);
13371da177e4SLinus Torvalds 	INIT_RCU_HEAD(&tbl->parms.rcu_head);
13381da177e4SLinus Torvalds 	tbl->parms.reachable_time =
13391da177e4SLinus Torvalds 			  neigh_rand_reach_time(tbl->parms.base_reachable_time);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	if (!tbl->kmem_cachep)
13421da177e4SLinus Torvalds 		tbl->kmem_cachep = kmem_cache_create(tbl->id,
13431da177e4SLinus Torvalds 						     tbl->entry_size,
13441da177e4SLinus Torvalds 						     0, SLAB_HWCACHE_ALIGN,
13451da177e4SLinus Torvalds 						     NULL, NULL);
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	if (!tbl->kmem_cachep)
13481da177e4SLinus Torvalds 		panic("cannot create neighbour cache");
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	tbl->stats = alloc_percpu(struct neigh_statistics);
13511da177e4SLinus Torvalds 	if (!tbl->stats)
13521da177e4SLinus Torvalds 		panic("cannot create neighbour cache statistics");
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
13551da177e4SLinus Torvalds 	tbl->pde = create_proc_entry(tbl->id, 0, proc_net_stat);
13561da177e4SLinus Torvalds 	if (!tbl->pde)
13571da177e4SLinus Torvalds 		panic("cannot create neighbour proc dir entry");
13581da177e4SLinus Torvalds 	tbl->pde->proc_fops = &neigh_stat_seq_fops;
13591da177e4SLinus Torvalds 	tbl->pde->data = tbl;
13601da177e4SLinus Torvalds #endif
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	tbl->hash_mask = 1;
13631da177e4SLinus Torvalds 	tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
136677d04bd9SAndrew Morton 	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	if (!tbl->hash_buckets || !tbl->phash_buckets)
13691da177e4SLinus Torvalds 		panic("cannot allocate neighbour cache hashes");
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	rwlock_init(&tbl->lock);
13741da177e4SLinus Torvalds 	init_timer(&tbl->gc_timer);
13751da177e4SLinus Torvalds 	tbl->gc_timer.data     = (unsigned long)tbl;
13761da177e4SLinus Torvalds 	tbl->gc_timer.function = neigh_periodic_timer;
13771da177e4SLinus Torvalds 	tbl->gc_timer.expires  = now + 1;
13781da177e4SLinus Torvalds 	add_timer(&tbl->gc_timer);
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	init_timer(&tbl->proxy_timer);
13811da177e4SLinus Torvalds 	tbl->proxy_timer.data	  = (unsigned long)tbl;
13821da177e4SLinus Torvalds 	tbl->proxy_timer.function = neigh_proxy_process;
13831da177e4SLinus Torvalds 	skb_queue_head_init(&tbl->proxy_queue);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	tbl->last_flush = now;
13861da177e4SLinus Torvalds 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1387bd89efc5SSimon Kelley }
1388bd89efc5SSimon Kelley 
1389bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl)
1390bd89efc5SSimon Kelley {
1391bd89efc5SSimon Kelley 	struct neigh_table *tmp;
1392bd89efc5SSimon Kelley 
1393bd89efc5SSimon Kelley 	neigh_table_init_no_netlink(tbl);
13941da177e4SLinus Torvalds 	write_lock(&neigh_tbl_lock);
1395bd89efc5SSimon Kelley 	for (tmp = neigh_tables; tmp; tmp = tmp->next) {
1396bd89efc5SSimon Kelley 		if (tmp->family == tbl->family)
1397bd89efc5SSimon Kelley 			break;
1398bd89efc5SSimon Kelley 	}
13991da177e4SLinus Torvalds 	tbl->next	= neigh_tables;
14001da177e4SLinus Torvalds 	neigh_tables	= tbl;
14011da177e4SLinus Torvalds 	write_unlock(&neigh_tbl_lock);
1402bd89efc5SSimon Kelley 
1403bd89efc5SSimon Kelley 	if (unlikely(tmp)) {
1404bd89efc5SSimon Kelley 		printk(KERN_ERR "NEIGH: Registering multiple tables for "
1405bd89efc5SSimon Kelley 		       "family %d\n", tbl->family);
1406bd89efc5SSimon Kelley 		dump_stack();
1407bd89efc5SSimon Kelley 	}
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	struct neigh_table **tp;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	/* It is not clean... Fix it to unload IPv6 module safely */
14151da177e4SLinus Torvalds 	del_timer_sync(&tbl->gc_timer);
14161da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
14171da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
14181da177e4SLinus Torvalds 	neigh_ifdown(tbl, NULL);
14191da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries))
14201da177e4SLinus Torvalds 		printk(KERN_CRIT "neighbour leakage\n");
14211da177e4SLinus Torvalds 	write_lock(&neigh_tbl_lock);
14221da177e4SLinus Torvalds 	for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
14231da177e4SLinus Torvalds 		if (*tp == tbl) {
14241da177e4SLinus Torvalds 			*tp = tbl->next;
14251da177e4SLinus Torvalds 			break;
14261da177e4SLinus Torvalds 		}
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 	write_unlock(&neigh_tbl_lock);
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1);
14311da177e4SLinus Torvalds 	tbl->hash_buckets = NULL;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	kfree(tbl->phash_buckets);
14341da177e4SLinus Torvalds 	tbl->phash_buckets = NULL;
14351da177e4SLinus Torvalds 
14363fcde74bSKirill Korotaev 	free_percpu(tbl->stats);
14373fcde74bSKirill Korotaev 	tbl->stats = NULL;
14383fcde74bSKirill Korotaev 
14391da177e4SLinus Torvalds 	return 0;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
14431da177e4SLinus Torvalds {
1444a14a49d2SThomas Graf 	struct ndmsg *ndm;
1445a14a49d2SThomas Graf 	struct nlattr *dst_attr;
14461da177e4SLinus Torvalds 	struct neigh_table *tbl;
14471da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1448a14a49d2SThomas Graf 	int err = -EINVAL;
14491da177e4SLinus Torvalds 
1450a14a49d2SThomas Graf 	if (nlmsg_len(nlh) < sizeof(*ndm))
14511da177e4SLinus Torvalds 		goto out;
14521da177e4SLinus Torvalds 
1453a14a49d2SThomas Graf 	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1454a14a49d2SThomas Graf 	if (dst_attr == NULL)
1455a14a49d2SThomas Graf 		goto out;
1456a14a49d2SThomas Graf 
1457a14a49d2SThomas Graf 	ndm = nlmsg_data(nlh);
1458a14a49d2SThomas Graf 	if (ndm->ndm_ifindex) {
1459a14a49d2SThomas Graf 		dev = dev_get_by_index(ndm->ndm_ifindex);
1460a14a49d2SThomas Graf 		if (dev == NULL) {
1461a14a49d2SThomas Graf 			err = -ENODEV;
1462a14a49d2SThomas Graf 			goto out;
1463a14a49d2SThomas Graf 		}
1464a14a49d2SThomas Graf 	}
1465a14a49d2SThomas Graf 
14661da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
14671da177e4SLinus Torvalds 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1468a14a49d2SThomas Graf 		struct neighbour *neigh;
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 		if (tbl->family != ndm->ndm_family)
14711da177e4SLinus Torvalds 			continue;
14721da177e4SLinus Torvalds 		read_unlock(&neigh_tbl_lock);
14731da177e4SLinus Torvalds 
1474a14a49d2SThomas Graf 		if (nla_len(dst_attr) < tbl->key_len)
14751da177e4SLinus Torvalds 			goto out_dev_put;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 		if (ndm->ndm_flags & NTF_PROXY) {
1478a14a49d2SThomas Graf 			err = pneigh_delete(tbl, nla_data(dst_attr), dev);
14791da177e4SLinus Torvalds 			goto out_dev_put;
14801da177e4SLinus Torvalds 		}
14811da177e4SLinus Torvalds 
1482a14a49d2SThomas Graf 		if (dev == NULL)
1483a14a49d2SThomas Graf 			goto out_dev_put;
14841da177e4SLinus Torvalds 
1485a14a49d2SThomas Graf 		neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1486a14a49d2SThomas Graf 		if (neigh == NULL) {
1487a14a49d2SThomas Graf 			err = -ENOENT;
1488a14a49d2SThomas Graf 			goto out_dev_put;
1489a14a49d2SThomas Graf 		}
1490a14a49d2SThomas Graf 
1491a14a49d2SThomas Graf 		err = neigh_update(neigh, NULL, NUD_FAILED,
14921da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_OVERRIDE |
14931da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_ADMIN);
1494a14a49d2SThomas Graf 		neigh_release(neigh);
14951da177e4SLinus Torvalds 		goto out_dev_put;
14961da177e4SLinus Torvalds 	}
14971da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
1498a14a49d2SThomas Graf 	err = -EAFNOSUPPORT;
1499a14a49d2SThomas Graf 
15001da177e4SLinus Torvalds out_dev_put:
15011da177e4SLinus Torvalds 	if (dev)
15021da177e4SLinus Torvalds 		dev_put(dev);
15031da177e4SLinus Torvalds out:
15041da177e4SLinus Torvalds 	return err;
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
15081da177e4SLinus Torvalds {
15095208debdSThomas Graf 	struct ndmsg *ndm;
15105208debdSThomas Graf 	struct nlattr *tb[NDA_MAX+1];
15111da177e4SLinus Torvalds 	struct neigh_table *tbl;
15121da177e4SLinus Torvalds 	struct net_device *dev = NULL;
15135208debdSThomas Graf 	int err;
15141da177e4SLinus Torvalds 
15155208debdSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
15165208debdSThomas Graf 	if (err < 0)
15171da177e4SLinus Torvalds 		goto out;
15181da177e4SLinus Torvalds 
15195208debdSThomas Graf 	err = -EINVAL;
15205208debdSThomas Graf 	if (tb[NDA_DST] == NULL)
15215208debdSThomas Graf 		goto out;
15225208debdSThomas Graf 
15235208debdSThomas Graf 	ndm = nlmsg_data(nlh);
15245208debdSThomas Graf 	if (ndm->ndm_ifindex) {
15255208debdSThomas Graf 		dev = dev_get_by_index(ndm->ndm_ifindex);
15265208debdSThomas Graf 		if (dev == NULL) {
15275208debdSThomas Graf 			err = -ENODEV;
15285208debdSThomas Graf 			goto out;
15295208debdSThomas Graf 		}
15305208debdSThomas Graf 
15315208debdSThomas Graf 		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
15325208debdSThomas Graf 			goto out_dev_put;
15335208debdSThomas Graf 	}
15345208debdSThomas Graf 
15351da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
15361da177e4SLinus Torvalds 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
15375208debdSThomas Graf 		int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
15385208debdSThomas Graf 		struct neighbour *neigh;
15395208debdSThomas Graf 		void *dst, *lladdr;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 		if (tbl->family != ndm->ndm_family)
15421da177e4SLinus Torvalds 			continue;
15431da177e4SLinus Torvalds 		read_unlock(&neigh_tbl_lock);
15441da177e4SLinus Torvalds 
15455208debdSThomas Graf 		if (nla_len(tb[NDA_DST]) < tbl->key_len)
15461da177e4SLinus Torvalds 			goto out_dev_put;
15475208debdSThomas Graf 		dst = nla_data(tb[NDA_DST]);
15485208debdSThomas Graf 		lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 		if (ndm->ndm_flags & NTF_PROXY) {
15511da177e4SLinus Torvalds 			err = 0;
15525208debdSThomas Graf 			if (pneigh_lookup(tbl, dst, dev, 1) == NULL)
15535208debdSThomas Graf 				err = -ENOBUFS;
15541da177e4SLinus Torvalds 			goto out_dev_put;
15551da177e4SLinus Torvalds 		}
15561da177e4SLinus Torvalds 
15575208debdSThomas Graf 		if (dev == NULL)
15581da177e4SLinus Torvalds 			goto out_dev_put;
15591da177e4SLinus Torvalds 
15605208debdSThomas Graf 		neigh = neigh_lookup(tbl, dst, dev);
15615208debdSThomas Graf 		if (neigh == NULL) {
15625208debdSThomas Graf 			if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
15631da177e4SLinus Torvalds 				err = -ENOENT;
15641da177e4SLinus Torvalds 				goto out_dev_put;
15655208debdSThomas Graf 			}
15665208debdSThomas Graf 
15675208debdSThomas Graf 			neigh = __neigh_lookup_errno(tbl, dst, dev);
15685208debdSThomas Graf 			if (IS_ERR(neigh)) {
15695208debdSThomas Graf 				err = PTR_ERR(neigh);
15701da177e4SLinus Torvalds 				goto out_dev_put;
15711da177e4SLinus Torvalds 			}
15725208debdSThomas Graf 		} else {
15735208debdSThomas Graf 			if (nlh->nlmsg_flags & NLM_F_EXCL) {
15745208debdSThomas Graf 				err = -EEXIST;
15755208debdSThomas Graf 				neigh_release(neigh);
15765208debdSThomas Graf 				goto out_dev_put;
15771da177e4SLinus Torvalds 			}
15781da177e4SLinus Torvalds 
15795208debdSThomas Graf 			if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
15805208debdSThomas Graf 				flags &= ~NEIGH_UPDATE_F_OVERRIDE;
15815208debdSThomas Graf 		}
15821da177e4SLinus Torvalds 
15835208debdSThomas Graf 		err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
15845208debdSThomas Graf 		neigh_release(neigh);
15851da177e4SLinus Torvalds 		goto out_dev_put;
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
15895208debdSThomas Graf 	err = -EAFNOSUPPORT;
15905208debdSThomas Graf 
15911da177e4SLinus Torvalds out_dev_put:
15921da177e4SLinus Torvalds 	if (dev)
15931da177e4SLinus Torvalds 		dev_put(dev);
15941da177e4SLinus Torvalds out:
15951da177e4SLinus Torvalds 	return err;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
1598c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1599c7fb64dbSThomas Graf {
1600e386c6ebSThomas Graf 	struct rtattr *nest = NULL;
1601e386c6ebSThomas Graf 
1602e386c6ebSThomas Graf 	nest = RTA_NEST(skb, NDTA_PARMS);
1603c7fb64dbSThomas Graf 
1604c7fb64dbSThomas Graf 	if (parms->dev)
1605c7fb64dbSThomas Graf 		RTA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex);
1606c7fb64dbSThomas Graf 
1607c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt));
1608c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len);
1609c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen);
1610c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes);
1611c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes);
1612c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes);
1613c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time);
1614c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME,
1615c7fb64dbSThomas Graf 		      parms->base_reachable_time);
1616c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime);
1617c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time);
1618c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time);
1619c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay);
1620c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay);
1621c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime);
1622c7fb64dbSThomas Graf 
1623c7fb64dbSThomas Graf 	return RTA_NEST_END(skb, nest);
1624c7fb64dbSThomas Graf 
1625c7fb64dbSThomas Graf rtattr_failure:
1626c7fb64dbSThomas Graf 	return RTA_NEST_CANCEL(skb, nest);
1627c7fb64dbSThomas Graf }
1628c7fb64dbSThomas Graf 
1629c7fb64dbSThomas Graf static int neightbl_fill_info(struct neigh_table *tbl, struct sk_buff *skb,
1630c7fb64dbSThomas Graf 			      struct netlink_callback *cb)
1631c7fb64dbSThomas Graf {
1632c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1633c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1634c7fb64dbSThomas Graf 
16351797754eSThomas Graf 	nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg),
16361797754eSThomas Graf 			       NLM_F_MULTI);
1637c7fb64dbSThomas Graf 
16384b6ea82dSThomas Graf 	ndtmsg = NLMSG_DATA(nlh);
1639c7fb64dbSThomas Graf 
1640c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1641c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
16429ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
16439ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1644c7fb64dbSThomas Graf 
1645c7fb64dbSThomas Graf 	RTA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1646c7fb64dbSThomas Graf 	RTA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval);
1647c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1);
1648c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2);
1649c7fb64dbSThomas Graf 	RTA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3);
1650c7fb64dbSThomas Graf 
1651c7fb64dbSThomas Graf 	{
1652c7fb64dbSThomas Graf 		unsigned long now = jiffies;
1653c7fb64dbSThomas Graf 		unsigned int flush_delta = now - tbl->last_flush;
1654c7fb64dbSThomas Graf 		unsigned int rand_delta = now - tbl->last_rand;
1655c7fb64dbSThomas Graf 
1656c7fb64dbSThomas Graf 		struct ndt_config ndc = {
1657c7fb64dbSThomas Graf 			.ndtc_key_len		= tbl->key_len,
1658c7fb64dbSThomas Graf 			.ndtc_entry_size	= tbl->entry_size,
1659c7fb64dbSThomas Graf 			.ndtc_entries		= atomic_read(&tbl->entries),
1660c7fb64dbSThomas Graf 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
1661c7fb64dbSThomas Graf 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
1662c7fb64dbSThomas Graf 			.ndtc_hash_rnd		= tbl->hash_rnd,
1663c7fb64dbSThomas Graf 			.ndtc_hash_mask		= tbl->hash_mask,
1664c7fb64dbSThomas Graf 			.ndtc_hash_chain_gc	= tbl->hash_chain_gc,
1665c7fb64dbSThomas Graf 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
1666c7fb64dbSThomas Graf 		};
1667c7fb64dbSThomas Graf 
1668c7fb64dbSThomas Graf 		RTA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc);
1669c7fb64dbSThomas Graf 	}
1670c7fb64dbSThomas Graf 
1671c7fb64dbSThomas Graf 	{
1672c7fb64dbSThomas Graf 		int cpu;
1673c7fb64dbSThomas Graf 		struct ndt_stats ndst;
1674c7fb64dbSThomas Graf 
1675c7fb64dbSThomas Graf 		memset(&ndst, 0, sizeof(ndst));
1676c7fb64dbSThomas Graf 
16776f912042SKAMEZAWA Hiroyuki 		for_each_possible_cpu(cpu) {
1678c7fb64dbSThomas Graf 			struct neigh_statistics	*st;
1679c7fb64dbSThomas Graf 
1680c7fb64dbSThomas Graf 			st = per_cpu_ptr(tbl->stats, cpu);
1681c7fb64dbSThomas Graf 			ndst.ndts_allocs		+= st->allocs;
1682c7fb64dbSThomas Graf 			ndst.ndts_destroys		+= st->destroys;
1683c7fb64dbSThomas Graf 			ndst.ndts_hash_grows		+= st->hash_grows;
1684c7fb64dbSThomas Graf 			ndst.ndts_res_failed		+= st->res_failed;
1685c7fb64dbSThomas Graf 			ndst.ndts_lookups		+= st->lookups;
1686c7fb64dbSThomas Graf 			ndst.ndts_hits			+= st->hits;
1687c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
1688c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
1689c7fb64dbSThomas Graf 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
1690c7fb64dbSThomas Graf 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
1691c7fb64dbSThomas Graf 		}
1692c7fb64dbSThomas Graf 
1693c7fb64dbSThomas Graf 		RTA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst);
1694c7fb64dbSThomas Graf 	}
1695c7fb64dbSThomas Graf 
1696c7fb64dbSThomas Graf 	BUG_ON(tbl->parms.dev);
1697c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1698c7fb64dbSThomas Graf 		goto rtattr_failure;
1699c7fb64dbSThomas Graf 
1700c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1701c7fb64dbSThomas Graf 	return NLMSG_END(skb, nlh);
1702c7fb64dbSThomas Graf 
1703c7fb64dbSThomas Graf rtattr_failure:
1704c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1705c7fb64dbSThomas Graf 	return NLMSG_CANCEL(skb, nlh);
1706c7fb64dbSThomas Graf 
1707c7fb64dbSThomas Graf nlmsg_failure:
1708c7fb64dbSThomas Graf 	return -1;
1709c7fb64dbSThomas Graf }
1710c7fb64dbSThomas Graf 
1711c7fb64dbSThomas Graf static int neightbl_fill_param_info(struct neigh_table *tbl,
1712c7fb64dbSThomas Graf 				    struct neigh_parms *parms,
1713c7fb64dbSThomas Graf 				    struct sk_buff *skb,
1714c7fb64dbSThomas Graf 				    struct netlink_callback *cb)
1715c7fb64dbSThomas Graf {
1716c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1717c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1718c7fb64dbSThomas Graf 
17191797754eSThomas Graf 	nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg),
17201797754eSThomas Graf 			       NLM_F_MULTI);
1721c7fb64dbSThomas Graf 
17224b6ea82dSThomas Graf 	ndtmsg = NLMSG_DATA(nlh);
1723c7fb64dbSThomas Graf 
1724c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1725c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
17269ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
17279ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1728c7fb64dbSThomas Graf 	RTA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1729c7fb64dbSThomas Graf 
1730c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, parms) < 0)
1731c7fb64dbSThomas Graf 		goto rtattr_failure;
1732c7fb64dbSThomas Graf 
1733c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1734c7fb64dbSThomas Graf 	return NLMSG_END(skb, nlh);
1735c7fb64dbSThomas Graf 
1736c7fb64dbSThomas Graf rtattr_failure:
1737c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1738c7fb64dbSThomas Graf 	return NLMSG_CANCEL(skb, nlh);
1739c7fb64dbSThomas Graf 
1740c7fb64dbSThomas Graf nlmsg_failure:
1741c7fb64dbSThomas Graf 	return -1;
1742c7fb64dbSThomas Graf }
1743c7fb64dbSThomas Graf 
1744c7fb64dbSThomas Graf static inline struct neigh_parms *lookup_neigh_params(struct neigh_table *tbl,
1745c7fb64dbSThomas Graf 						      int ifindex)
1746c7fb64dbSThomas Graf {
1747c7fb64dbSThomas Graf 	struct neigh_parms *p;
1748c7fb64dbSThomas Graf 
1749c7fb64dbSThomas Graf 	for (p = &tbl->parms; p; p = p->next)
1750c7fb64dbSThomas Graf 		if ((p->dev && p->dev->ifindex == ifindex) ||
1751c7fb64dbSThomas Graf 		    (!p->dev && !ifindex))
1752c7fb64dbSThomas Graf 			return p;
1753c7fb64dbSThomas Graf 
1754c7fb64dbSThomas Graf 	return NULL;
1755c7fb64dbSThomas Graf }
1756c7fb64dbSThomas Graf 
1757c7fb64dbSThomas Graf int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1758c7fb64dbSThomas Graf {
1759c7fb64dbSThomas Graf 	struct neigh_table *tbl;
1760c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg = NLMSG_DATA(nlh);
1761c7fb64dbSThomas Graf 	struct rtattr **tb = arg;
1762c7fb64dbSThomas Graf 	int err = -EINVAL;
1763c7fb64dbSThomas Graf 
1764c7fb64dbSThomas Graf 	if (!tb[NDTA_NAME - 1] || !RTA_PAYLOAD(tb[NDTA_NAME - 1]))
1765c7fb64dbSThomas Graf 		return -EINVAL;
1766c7fb64dbSThomas Graf 
1767c7fb64dbSThomas Graf 	read_lock(&neigh_tbl_lock);
1768c7fb64dbSThomas Graf 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1769c7fb64dbSThomas Graf 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1770c7fb64dbSThomas Graf 			continue;
1771c7fb64dbSThomas Graf 
1772c7fb64dbSThomas Graf 		if (!rtattr_strcmp(tb[NDTA_NAME - 1], tbl->id))
1773c7fb64dbSThomas Graf 			break;
1774c7fb64dbSThomas Graf 	}
1775c7fb64dbSThomas Graf 
1776c7fb64dbSThomas Graf 	if (tbl == NULL) {
1777c7fb64dbSThomas Graf 		err = -ENOENT;
1778c7fb64dbSThomas Graf 		goto errout;
1779c7fb64dbSThomas Graf 	}
1780c7fb64dbSThomas Graf 
1781c7fb64dbSThomas Graf 	/*
1782c7fb64dbSThomas Graf 	 * We acquire tbl->lock to be nice to the periodic timers and
1783c7fb64dbSThomas Graf 	 * make sure they always see a consistent set of values.
1784c7fb64dbSThomas Graf 	 */
1785c7fb64dbSThomas Graf 	write_lock_bh(&tbl->lock);
1786c7fb64dbSThomas Graf 
1787c7fb64dbSThomas Graf 	if (tb[NDTA_THRESH1 - 1])
1788c7fb64dbSThomas Graf 		tbl->gc_thresh1 = RTA_GET_U32(tb[NDTA_THRESH1 - 1]);
1789c7fb64dbSThomas Graf 
1790c7fb64dbSThomas Graf 	if (tb[NDTA_THRESH2 - 1])
1791c7fb64dbSThomas Graf 		tbl->gc_thresh2 = RTA_GET_U32(tb[NDTA_THRESH2 - 1]);
1792c7fb64dbSThomas Graf 
1793c7fb64dbSThomas Graf 	if (tb[NDTA_THRESH3 - 1])
1794c7fb64dbSThomas Graf 		tbl->gc_thresh3 = RTA_GET_U32(tb[NDTA_THRESH3 - 1]);
1795c7fb64dbSThomas Graf 
1796c7fb64dbSThomas Graf 	if (tb[NDTA_GC_INTERVAL - 1])
1797c7fb64dbSThomas Graf 		tbl->gc_interval = RTA_GET_MSECS(tb[NDTA_GC_INTERVAL - 1]);
1798c7fb64dbSThomas Graf 
1799c7fb64dbSThomas Graf 	if (tb[NDTA_PARMS - 1]) {
1800c7fb64dbSThomas Graf 		struct rtattr *tbp[NDTPA_MAX];
1801c7fb64dbSThomas Graf 		struct neigh_parms *p;
1802c7fb64dbSThomas Graf 		u32 ifindex = 0;
1803c7fb64dbSThomas Graf 
1804c7fb64dbSThomas Graf 		if (rtattr_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS - 1]) < 0)
1805c7fb64dbSThomas Graf 			goto rtattr_failure;
1806c7fb64dbSThomas Graf 
1807c7fb64dbSThomas Graf 		if (tbp[NDTPA_IFINDEX - 1])
1808c7fb64dbSThomas Graf 			ifindex = RTA_GET_U32(tbp[NDTPA_IFINDEX - 1]);
1809c7fb64dbSThomas Graf 
1810c7fb64dbSThomas Graf 		p = lookup_neigh_params(tbl, ifindex);
1811c7fb64dbSThomas Graf 		if (p == NULL) {
1812c7fb64dbSThomas Graf 			err = -ENOENT;
1813c7fb64dbSThomas Graf 			goto rtattr_failure;
1814c7fb64dbSThomas Graf 		}
1815c7fb64dbSThomas Graf 
1816c7fb64dbSThomas Graf 		if (tbp[NDTPA_QUEUE_LEN - 1])
1817c7fb64dbSThomas Graf 			p->queue_len = RTA_GET_U32(tbp[NDTPA_QUEUE_LEN - 1]);
1818c7fb64dbSThomas Graf 
1819c7fb64dbSThomas Graf 		if (tbp[NDTPA_PROXY_QLEN - 1])
1820c7fb64dbSThomas Graf 			p->proxy_qlen = RTA_GET_U32(tbp[NDTPA_PROXY_QLEN - 1]);
1821c7fb64dbSThomas Graf 
1822c7fb64dbSThomas Graf 		if (tbp[NDTPA_APP_PROBES - 1])
1823c7fb64dbSThomas Graf 			p->app_probes = RTA_GET_U32(tbp[NDTPA_APP_PROBES - 1]);
1824c7fb64dbSThomas Graf 
1825c7fb64dbSThomas Graf 		if (tbp[NDTPA_UCAST_PROBES - 1])
1826c7fb64dbSThomas Graf 			p->ucast_probes =
1827c7fb64dbSThomas Graf 			   RTA_GET_U32(tbp[NDTPA_UCAST_PROBES - 1]);
1828c7fb64dbSThomas Graf 
1829c7fb64dbSThomas Graf 		if (tbp[NDTPA_MCAST_PROBES - 1])
1830c7fb64dbSThomas Graf 			p->mcast_probes =
1831c7fb64dbSThomas Graf 			   RTA_GET_U32(tbp[NDTPA_MCAST_PROBES - 1]);
1832c7fb64dbSThomas Graf 
1833c7fb64dbSThomas Graf 		if (tbp[NDTPA_BASE_REACHABLE_TIME - 1])
1834c7fb64dbSThomas Graf 			p->base_reachable_time =
1835c7fb64dbSThomas Graf 			   RTA_GET_MSECS(tbp[NDTPA_BASE_REACHABLE_TIME - 1]);
1836c7fb64dbSThomas Graf 
1837c7fb64dbSThomas Graf 		if (tbp[NDTPA_GC_STALETIME - 1])
1838c7fb64dbSThomas Graf 			p->gc_staletime =
1839c7fb64dbSThomas Graf 			   RTA_GET_MSECS(tbp[NDTPA_GC_STALETIME - 1]);
1840c7fb64dbSThomas Graf 
1841c7fb64dbSThomas Graf 		if (tbp[NDTPA_DELAY_PROBE_TIME - 1])
1842c7fb64dbSThomas Graf 			p->delay_probe_time =
1843c7fb64dbSThomas Graf 			   RTA_GET_MSECS(tbp[NDTPA_DELAY_PROBE_TIME - 1]);
1844c7fb64dbSThomas Graf 
1845c7fb64dbSThomas Graf 		if (tbp[NDTPA_RETRANS_TIME - 1])
1846c7fb64dbSThomas Graf 			p->retrans_time =
1847c7fb64dbSThomas Graf 			   RTA_GET_MSECS(tbp[NDTPA_RETRANS_TIME - 1]);
1848c7fb64dbSThomas Graf 
1849c7fb64dbSThomas Graf 		if (tbp[NDTPA_ANYCAST_DELAY - 1])
1850c7fb64dbSThomas Graf 			p->anycast_delay =
1851c7fb64dbSThomas Graf 			   RTA_GET_MSECS(tbp[NDTPA_ANYCAST_DELAY - 1]);
1852c7fb64dbSThomas Graf 
1853c7fb64dbSThomas Graf 		if (tbp[NDTPA_PROXY_DELAY - 1])
1854c7fb64dbSThomas Graf 			p->proxy_delay =
1855c7fb64dbSThomas Graf 			   RTA_GET_MSECS(tbp[NDTPA_PROXY_DELAY - 1]);
1856c7fb64dbSThomas Graf 
1857c7fb64dbSThomas Graf 		if (tbp[NDTPA_LOCKTIME - 1])
1858c7fb64dbSThomas Graf 			p->locktime = RTA_GET_MSECS(tbp[NDTPA_LOCKTIME - 1]);
1859c7fb64dbSThomas Graf 	}
1860c7fb64dbSThomas Graf 
1861c7fb64dbSThomas Graf 	err = 0;
1862c7fb64dbSThomas Graf 
1863c7fb64dbSThomas Graf rtattr_failure:
1864c7fb64dbSThomas Graf 	write_unlock_bh(&tbl->lock);
1865c7fb64dbSThomas Graf errout:
1866c7fb64dbSThomas Graf 	read_unlock(&neigh_tbl_lock);
1867c7fb64dbSThomas Graf 	return err;
1868c7fb64dbSThomas Graf }
1869c7fb64dbSThomas Graf 
1870c7fb64dbSThomas Graf int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1871c7fb64dbSThomas Graf {
1872c7fb64dbSThomas Graf 	int idx, family;
1873c7fb64dbSThomas Graf 	int s_idx = cb->args[0];
1874c7fb64dbSThomas Graf 	struct neigh_table *tbl;
1875c7fb64dbSThomas Graf 
1876c7fb64dbSThomas Graf 	family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
1877c7fb64dbSThomas Graf 
1878c7fb64dbSThomas Graf 	read_lock(&neigh_tbl_lock);
1879c7fb64dbSThomas Graf 	for (tbl = neigh_tables, idx = 0; tbl; tbl = tbl->next) {
1880c7fb64dbSThomas Graf 		struct neigh_parms *p;
1881c7fb64dbSThomas Graf 
1882c7fb64dbSThomas Graf 		if (idx < s_idx || (family && tbl->family != family))
1883c7fb64dbSThomas Graf 			continue;
1884c7fb64dbSThomas Graf 
1885c7fb64dbSThomas Graf 		if (neightbl_fill_info(tbl, skb, cb) <= 0)
1886c7fb64dbSThomas Graf 			break;
1887c7fb64dbSThomas Graf 
1888c7fb64dbSThomas Graf 		for (++idx, p = tbl->parms.next; p; p = p->next, idx++) {
1889c7fb64dbSThomas Graf 			if (idx < s_idx)
1890c7fb64dbSThomas Graf 				continue;
1891c7fb64dbSThomas Graf 
1892c7fb64dbSThomas Graf 			if (neightbl_fill_param_info(tbl, p, skb, cb) <= 0)
1893c7fb64dbSThomas Graf 				goto out;
1894c7fb64dbSThomas Graf 		}
1895c7fb64dbSThomas Graf 
1896c7fb64dbSThomas Graf 	}
1897c7fb64dbSThomas Graf out:
1898c7fb64dbSThomas Graf 	read_unlock(&neigh_tbl_lock);
1899c7fb64dbSThomas Graf 	cb->args[0] = idx;
1900c7fb64dbSThomas Graf 
1901c7fb64dbSThomas Graf 	return skb->len;
1902c7fb64dbSThomas Graf }
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds static int neigh_fill_info(struct sk_buff *skb, struct neighbour *n,
1905b6544c0bSJamal Hadi Salim 			   u32 pid, u32 seq, int event, unsigned int flags)
19061da177e4SLinus Torvalds {
19071da177e4SLinus Torvalds 	unsigned long now = jiffies;
19081da177e4SLinus Torvalds 	unsigned char *b = skb->tail;
19091da177e4SLinus Torvalds 	struct nda_cacheinfo ci;
19101da177e4SLinus Torvalds 	int locked = 0;
19111da177e4SLinus Torvalds 	u32 probes;
1912b6544c0bSJamal Hadi Salim 	struct nlmsghdr *nlh = NLMSG_NEW(skb, pid, seq, event,
1913b6544c0bSJamal Hadi Salim 					 sizeof(struct ndmsg), flags);
19141da177e4SLinus Torvalds 	struct ndmsg *ndm = NLMSG_DATA(nlh);
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	ndm->ndm_family	 = n->ops->family;
19179ef1d4c7SPatrick McHardy 	ndm->ndm_pad1    = 0;
19189ef1d4c7SPatrick McHardy 	ndm->ndm_pad2    = 0;
19191da177e4SLinus Torvalds 	ndm->ndm_flags	 = n->flags;
19201da177e4SLinus Torvalds 	ndm->ndm_type	 = n->type;
19211da177e4SLinus Torvalds 	ndm->ndm_ifindex = n->dev->ifindex;
19221da177e4SLinus Torvalds 	RTA_PUT(skb, NDA_DST, n->tbl->key_len, n->primary_key);
19231da177e4SLinus Torvalds 	read_lock_bh(&n->lock);
19241da177e4SLinus Torvalds 	locked		 = 1;
19251da177e4SLinus Torvalds 	ndm->ndm_state	 = n->nud_state;
19261da177e4SLinus Torvalds 	if (n->nud_state & NUD_VALID)
19271da177e4SLinus Torvalds 		RTA_PUT(skb, NDA_LLADDR, n->dev->addr_len, n->ha);
19281da177e4SLinus Torvalds 	ci.ndm_used	 = now - n->used;
19291da177e4SLinus Torvalds 	ci.ndm_confirmed = now - n->confirmed;
19301da177e4SLinus Torvalds 	ci.ndm_updated	 = now - n->updated;
19311da177e4SLinus Torvalds 	ci.ndm_refcnt	 = atomic_read(&n->refcnt) - 1;
19321da177e4SLinus Torvalds 	probes = atomic_read(&n->probes);
19331da177e4SLinus Torvalds 	read_unlock_bh(&n->lock);
19341da177e4SLinus Torvalds 	locked		 = 0;
19351da177e4SLinus Torvalds 	RTA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
19361da177e4SLinus Torvalds 	RTA_PUT(skb, NDA_PROBES, sizeof(probes), &probes);
19371da177e4SLinus Torvalds 	nlh->nlmsg_len	 = skb->tail - b;
19381da177e4SLinus Torvalds 	return skb->len;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds nlmsg_failure:
19411da177e4SLinus Torvalds rtattr_failure:
19421da177e4SLinus Torvalds 	if (locked)
19431da177e4SLinus Torvalds 		read_unlock_bh(&n->lock);
19441da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
19451da177e4SLinus Torvalds 	return -1;
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
19501da177e4SLinus Torvalds 			    struct netlink_callback *cb)
19511da177e4SLinus Torvalds {
19521da177e4SLinus Torvalds 	struct neighbour *n;
19531da177e4SLinus Torvalds 	int rc, h, s_h = cb->args[1];
19541da177e4SLinus Torvalds 	int idx, s_idx = idx = cb->args[2];
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	for (h = 0; h <= tbl->hash_mask; h++) {
19571da177e4SLinus Torvalds 		if (h < s_h)
19581da177e4SLinus Torvalds 			continue;
19591da177e4SLinus Torvalds 		if (h > s_h)
19601da177e4SLinus Torvalds 			s_idx = 0;
19611da177e4SLinus Torvalds 		read_lock_bh(&tbl->lock);
19621da177e4SLinus Torvalds 		for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next, idx++) {
19631da177e4SLinus Torvalds 			if (idx < s_idx)
19641da177e4SLinus Torvalds 				continue;
19651da177e4SLinus Torvalds 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
19661da177e4SLinus Torvalds 					    cb->nlh->nlmsg_seq,
1967b6544c0bSJamal Hadi Salim 					    RTM_NEWNEIGH,
1968b6544c0bSJamal Hadi Salim 					    NLM_F_MULTI) <= 0) {
19691da177e4SLinus Torvalds 				read_unlock_bh(&tbl->lock);
19701da177e4SLinus Torvalds 				rc = -1;
19711da177e4SLinus Torvalds 				goto out;
19721da177e4SLinus Torvalds 			}
19731da177e4SLinus Torvalds 		}
19741da177e4SLinus Torvalds 		read_unlock_bh(&tbl->lock);
19751da177e4SLinus Torvalds 	}
19761da177e4SLinus Torvalds 	rc = skb->len;
19771da177e4SLinus Torvalds out:
19781da177e4SLinus Torvalds 	cb->args[1] = h;
19791da177e4SLinus Torvalds 	cb->args[2] = idx;
19801da177e4SLinus Torvalds 	return rc;
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	struct neigh_table *tbl;
19861da177e4SLinus Torvalds 	int t, family, s_t;
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
19891da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
19901da177e4SLinus Torvalds 	s_t = cb->args[0];
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) {
19931da177e4SLinus Torvalds 		if (t < s_t || (family && tbl->family != family))
19941da177e4SLinus Torvalds 			continue;
19951da177e4SLinus Torvalds 		if (t > s_t)
19961da177e4SLinus Torvalds 			memset(&cb->args[1], 0, sizeof(cb->args) -
19971da177e4SLinus Torvalds 						sizeof(cb->args[0]));
19981da177e4SLinus Torvalds 		if (neigh_dump_table(tbl, skb, cb) < 0)
19991da177e4SLinus Torvalds 			break;
20001da177e4SLinus Torvalds 	}
20011da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	cb->args[0] = t;
20041da177e4SLinus Torvalds 	return skb->len;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
20081da177e4SLinus Torvalds {
20091da177e4SLinus Torvalds 	int chain;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
20121da177e4SLinus Torvalds 	for (chain = 0; chain <= tbl->hash_mask; chain++) {
20131da177e4SLinus Torvalds 		struct neighbour *n;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 		for (n = tbl->hash_buckets[chain]; n; n = n->next)
20161da177e4SLinus Torvalds 			cb(n, cookie);
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 	read_unlock_bh(&tbl->lock);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each);
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */
20231da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl,
20241da177e4SLinus Torvalds 			      int (*cb)(struct neighbour *))
20251da177e4SLinus Torvalds {
20261da177e4SLinus Torvalds 	int chain;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	for (chain = 0; chain <= tbl->hash_mask; chain++) {
20291da177e4SLinus Torvalds 		struct neighbour *n, **np;
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 		np = &tbl->hash_buckets[chain];
20321da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
20331da177e4SLinus Torvalds 			int release;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 			write_lock(&n->lock);
20361da177e4SLinus Torvalds 			release = cb(n);
20371da177e4SLinus Torvalds 			if (release) {
20381da177e4SLinus Torvalds 				*np = n->next;
20391da177e4SLinus Torvalds 				n->dead = 1;
20401da177e4SLinus Torvalds 			} else
20411da177e4SLinus Torvalds 				np = &n->next;
20421da177e4SLinus Torvalds 			write_unlock(&n->lock);
20431da177e4SLinus Torvalds 			if (release)
20441da177e4SLinus Torvalds 				neigh_release(n);
20451da177e4SLinus Torvalds 		}
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
20551da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
20561da177e4SLinus Torvalds 	struct neighbour *n = NULL;
20571da177e4SLinus Torvalds 	int bucket = state->bucket;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
20601da177e4SLinus Torvalds 	for (bucket = 0; bucket <= tbl->hash_mask; bucket++) {
20611da177e4SLinus Torvalds 		n = tbl->hash_buckets[bucket];
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 		while (n) {
20641da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
20651da177e4SLinus Torvalds 				loff_t fakep = 0;
20661da177e4SLinus Torvalds 				void *v;
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 				v = state->neigh_sub_iter(state, n, &fakep);
20691da177e4SLinus Torvalds 				if (!v)
20701da177e4SLinus Torvalds 					goto next;
20711da177e4SLinus Torvalds 			}
20721da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
20731da177e4SLinus Torvalds 				break;
20741da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
20751da177e4SLinus Torvalds 				break;
20761da177e4SLinus Torvalds 		next:
20771da177e4SLinus Torvalds 			n = n->next;
20781da177e4SLinus Torvalds 		}
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 		if (n)
20811da177e4SLinus Torvalds 			break;
20821da177e4SLinus Torvalds 	}
20831da177e4SLinus Torvalds 	state->bucket = bucket;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	return n;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq,
20891da177e4SLinus Torvalds 					struct neighbour *n,
20901da177e4SLinus Torvalds 					loff_t *pos)
20911da177e4SLinus Torvalds {
20921da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
20931da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	if (state->neigh_sub_iter) {
20961da177e4SLinus Torvalds 		void *v = state->neigh_sub_iter(state, n, pos);
20971da177e4SLinus Torvalds 		if (v)
20981da177e4SLinus Torvalds 			return n;
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 	n = n->next;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	while (1) {
21031da177e4SLinus Torvalds 		while (n) {
21041da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
21051da177e4SLinus Torvalds 				void *v = state->neigh_sub_iter(state, n, pos);
21061da177e4SLinus Torvalds 				if (v)
21071da177e4SLinus Torvalds 					return n;
21081da177e4SLinus Torvalds 				goto next;
21091da177e4SLinus Torvalds 			}
21101da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
21111da177e4SLinus Torvalds 				break;
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
21141da177e4SLinus Torvalds 				break;
21151da177e4SLinus Torvalds 		next:
21161da177e4SLinus Torvalds 			n = n->next;
21171da177e4SLinus Torvalds 		}
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 		if (n)
21201da177e4SLinus Torvalds 			break;
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 		if (++state->bucket > tbl->hash_mask)
21231da177e4SLinus Torvalds 			break;
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 		n = tbl->hash_buckets[state->bucket];
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	if (n && pos)
21291da177e4SLinus Torvalds 		--(*pos);
21301da177e4SLinus Torvalds 	return n;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	struct neighbour *n = neigh_get_first(seq);
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	if (n) {
21381da177e4SLinus Torvalds 		while (*pos) {
21391da177e4SLinus Torvalds 			n = neigh_get_next(seq, n, pos);
21401da177e4SLinus Torvalds 			if (!n)
21411da177e4SLinus Torvalds 				break;
21421da177e4SLinus Torvalds 		}
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds 	return *pos ? NULL : n;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
21481da177e4SLinus Torvalds {
21491da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
21501da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
21511da177e4SLinus Torvalds 	struct pneigh_entry *pn = NULL;
21521da177e4SLinus Torvalds 	int bucket = state->bucket;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
21551da177e4SLinus Torvalds 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
21561da177e4SLinus Torvalds 		pn = tbl->phash_buckets[bucket];
21571da177e4SLinus Torvalds 		if (pn)
21581da177e4SLinus Torvalds 			break;
21591da177e4SLinus Torvalds 	}
21601da177e4SLinus Torvalds 	state->bucket = bucket;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	return pn;
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
21661da177e4SLinus Torvalds 					    struct pneigh_entry *pn,
21671da177e4SLinus Torvalds 					    loff_t *pos)
21681da177e4SLinus Torvalds {
21691da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
21701da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	pn = pn->next;
21731da177e4SLinus Torvalds 	while (!pn) {
21741da177e4SLinus Torvalds 		if (++state->bucket > PNEIGH_HASHMASK)
21751da177e4SLinus Torvalds 			break;
21761da177e4SLinus Torvalds 		pn = tbl->phash_buckets[state->bucket];
21771da177e4SLinus Torvalds 		if (pn)
21781da177e4SLinus Torvalds 			break;
21791da177e4SLinus Torvalds 	}
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	if (pn && pos)
21821da177e4SLinus Torvalds 		--(*pos);
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	return pn;
21851da177e4SLinus Torvalds }
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
21881da177e4SLinus Torvalds {
21891da177e4SLinus Torvalds 	struct pneigh_entry *pn = pneigh_get_first(seq);
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	if (pn) {
21921da177e4SLinus Torvalds 		while (*pos) {
21931da177e4SLinus Torvalds 			pn = pneigh_get_next(seq, pn, pos);
21941da177e4SLinus Torvalds 			if (!pn)
21951da177e4SLinus Torvalds 				break;
21961da177e4SLinus Torvalds 		}
21971da177e4SLinus Torvalds 	}
21981da177e4SLinus Torvalds 	return *pos ? NULL : pn;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
22041da177e4SLinus Torvalds 	void *rc;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	rc = neigh_get_idx(seq, pos);
22071da177e4SLinus Torvalds 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
22081da177e4SLinus Torvalds 		rc = pneigh_get_idx(seq, pos);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	return rc;
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
22141da177e4SLinus Torvalds {
22151da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
22161da177e4SLinus Torvalds 	loff_t pos_minus_one;
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	state->tbl = tbl;
22191da177e4SLinus Torvalds 	state->bucket = 0;
22201da177e4SLinus Torvalds 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	pos_minus_one = *pos - 1;
22251da177e4SLinus Torvalds 	return *pos ? neigh_get_idx_any(seq, &pos_minus_one) : SEQ_START_TOKEN;
22261da177e4SLinus Torvalds }
22271da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start);
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
22301da177e4SLinus Torvalds {
22311da177e4SLinus Torvalds 	struct neigh_seq_state *state;
22321da177e4SLinus Torvalds 	void *rc;
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
22351da177e4SLinus Torvalds 		rc = neigh_get_idx(seq, pos);
22361da177e4SLinus Torvalds 		goto out;
22371da177e4SLinus Torvalds 	}
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	state = seq->private;
22401da177e4SLinus Torvalds 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
22411da177e4SLinus Torvalds 		rc = neigh_get_next(seq, v, NULL);
22421da177e4SLinus Torvalds 		if (rc)
22431da177e4SLinus Torvalds 			goto out;
22441da177e4SLinus Torvalds 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
22451da177e4SLinus Torvalds 			rc = pneigh_get_first(seq);
22461da177e4SLinus Torvalds 	} else {
22471da177e4SLinus Torvalds 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
22481da177e4SLinus Torvalds 		rc = pneigh_get_next(seq, v, NULL);
22491da177e4SLinus Torvalds 	}
22501da177e4SLinus Torvalds out:
22511da177e4SLinus Torvalds 	++(*pos);
22521da177e4SLinus Torvalds 	return rc;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next);
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v)
22571da177e4SLinus Torvalds {
22581da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
22591da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds 	read_unlock_bh(&tbl->lock);
22621da177e4SLinus Torvalds }
22631da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds /* statistics via seq_file */
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
22681da177e4SLinus Torvalds {
22691da177e4SLinus Torvalds 	struct proc_dir_entry *pde = seq->private;
22701da177e4SLinus Torvalds 	struct neigh_table *tbl = pde->data;
22711da177e4SLinus Torvalds 	int cpu;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	if (*pos == 0)
22741da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
22771da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
22781da177e4SLinus Torvalds 			continue;
22791da177e4SLinus Torvalds 		*pos = cpu+1;
22801da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
22811da177e4SLinus Torvalds 	}
22821da177e4SLinus Torvalds 	return NULL;
22831da177e4SLinus Torvalds }
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
22861da177e4SLinus Torvalds {
22871da177e4SLinus Torvalds 	struct proc_dir_entry *pde = seq->private;
22881da177e4SLinus Torvalds 	struct neigh_table *tbl = pde->data;
22891da177e4SLinus Torvalds 	int cpu;
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
22921da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
22931da177e4SLinus Torvalds 			continue;
22941da177e4SLinus Torvalds 		*pos = cpu+1;
22951da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
22961da177e4SLinus Torvalds 	}
22971da177e4SLinus Torvalds 	return NULL;
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
23011da177e4SLinus Torvalds {
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v)
23061da177e4SLinus Torvalds {
23071da177e4SLinus Torvalds 	struct proc_dir_entry *pde = seq->private;
23081da177e4SLinus Torvalds 	struct neigh_table *tbl = pde->data;
23091da177e4SLinus Torvalds 	struct neigh_statistics *st = v;
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
23125bec0039SOlaf Rempel 		seq_printf(seq, "entries  allocs destroys hash_grows  lookups hits  res_failed  rcv_probes_mcast rcv_probes_ucast  periodic_gc_runs forced_gc_runs\n");
23131da177e4SLinus Torvalds 		return 0;
23141da177e4SLinus Torvalds 	}
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
23171da177e4SLinus Torvalds 			"%08lx %08lx  %08lx %08lx\n",
23181da177e4SLinus Torvalds 		   atomic_read(&tbl->entries),
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 		   st->allocs,
23211da177e4SLinus Torvalds 		   st->destroys,
23221da177e4SLinus Torvalds 		   st->hash_grows,
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 		   st->lookups,
23251da177e4SLinus Torvalds 		   st->hits,
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds 		   st->res_failed,
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 		   st->rcv_probes_mcast,
23301da177e4SLinus Torvalds 		   st->rcv_probes_ucast,
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 		   st->periodic_gc_runs,
23331da177e4SLinus Torvalds 		   st->forced_gc_runs
23341da177e4SLinus Torvalds 		   );
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	return 0;
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds static struct seq_operations neigh_stat_seq_ops = {
23401da177e4SLinus Torvalds 	.start	= neigh_stat_seq_start,
23411da177e4SLinus Torvalds 	.next	= neigh_stat_seq_next,
23421da177e4SLinus Torvalds 	.stop	= neigh_stat_seq_stop,
23431da177e4SLinus Torvalds 	.show	= neigh_stat_seq_show,
23441da177e4SLinus Torvalds };
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file)
23471da177e4SLinus Torvalds {
23481da177e4SLinus Torvalds 	int ret = seq_open(file, &neigh_stat_seq_ops);
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds 	if (!ret) {
23511da177e4SLinus Torvalds 		struct seq_file *sf = file->private_data;
23521da177e4SLinus Torvalds 		sf->private = PDE(inode);
23531da177e4SLinus Torvalds 	}
23541da177e4SLinus Torvalds 	return ret;
23551da177e4SLinus Torvalds };
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds static struct file_operations neigh_stat_seq_fops = {
23581da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
23591da177e4SLinus Torvalds 	.open 	 = neigh_stat_seq_open,
23601da177e4SLinus Torvalds 	.read	 = seq_read,
23611da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
23621da177e4SLinus Torvalds 	.release = seq_release,
23631da177e4SLinus Torvalds };
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds #ifdef CONFIG_ARPD
23681da177e4SLinus Torvalds void neigh_app_ns(struct neighbour *n)
23691da177e4SLinus Torvalds {
23701da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
23711da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
23721da177e4SLinus Torvalds 	struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	if (!skb)
23751da177e4SLinus Torvalds 		return;
23761da177e4SLinus Torvalds 
2377b6544c0bSJamal Hadi Salim 	if (neigh_fill_info(skb, n, 0, 0, RTM_GETNEIGH, 0) < 0) {
23781da177e4SLinus Torvalds 		kfree_skb(skb);
23791da177e4SLinus Torvalds 		return;
23801da177e4SLinus Torvalds 	}
23811da177e4SLinus Torvalds 	nlh			   = (struct nlmsghdr *)skb->data;
23821da177e4SLinus Torvalds 	nlh->nlmsg_flags	   = NLM_F_REQUEST;
2383ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group  = RTNLGRP_NEIGH;
2384ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_NEIGH, GFP_ATOMIC);
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds static void neigh_app_notify(struct neighbour *n)
23881da177e4SLinus Torvalds {
23891da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
23901da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
23911da177e4SLinus Torvalds 	struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	if (!skb)
23941da177e4SLinus Torvalds 		return;
23951da177e4SLinus Torvalds 
2396b6544c0bSJamal Hadi Salim 	if (neigh_fill_info(skb, n, 0, 0, RTM_NEWNEIGH, 0) < 0) {
23971da177e4SLinus Torvalds 		kfree_skb(skb);
23981da177e4SLinus Torvalds 		return;
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 	nlh			   = (struct nlmsghdr *)skb->data;
2401ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group  = RTNLGRP_NEIGH;
2402ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_NEIGH, GFP_ATOMIC);
24031da177e4SLinus Torvalds }
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds #endif /* CONFIG_ARPD */
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds static struct neigh_sysctl_table {
24101da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
24111da177e4SLinus Torvalds 	ctl_table		neigh_vars[__NET_NEIGH_MAX];
24121da177e4SLinus Torvalds 	ctl_table		neigh_dev[2];
24131da177e4SLinus Torvalds 	ctl_table		neigh_neigh_dir[2];
24141da177e4SLinus Torvalds 	ctl_table		neigh_proto_dir[2];
24151da177e4SLinus Torvalds 	ctl_table		neigh_root_dir[2];
24161da177e4SLinus Torvalds } neigh_sysctl_template = {
24171da177e4SLinus Torvalds 	.neigh_vars = {
24181da177e4SLinus Torvalds 		{
24191da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_MCAST_SOLICIT,
24201da177e4SLinus Torvalds 			.procname	= "mcast_solicit",
24211da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24221da177e4SLinus Torvalds 			.mode		= 0644,
24231da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
24241da177e4SLinus Torvalds 		},
24251da177e4SLinus Torvalds 		{
24261da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_UCAST_SOLICIT,
24271da177e4SLinus Torvalds 			.procname	= "ucast_solicit",
24281da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24291da177e4SLinus Torvalds 			.mode		= 0644,
24301da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
24311da177e4SLinus Torvalds 		},
24321da177e4SLinus Torvalds 		{
24331da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_APP_SOLICIT,
24341da177e4SLinus Torvalds 			.procname	= "app_solicit",
24351da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24361da177e4SLinus Torvalds 			.mode		= 0644,
24371da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
24381da177e4SLinus Torvalds 		},
24391da177e4SLinus Torvalds 		{
24401da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_RETRANS_TIME,
24411da177e4SLinus Torvalds 			.procname	= "retrans_time",
24421da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24431da177e4SLinus Torvalds 			.mode		= 0644,
24441da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_userhz_jiffies,
24451da177e4SLinus Torvalds 		},
24461da177e4SLinus Torvalds 		{
24471da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_REACHABLE_TIME,
24481da177e4SLinus Torvalds 			.procname	= "base_reachable_time",
24491da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24501da177e4SLinus Torvalds 			.mode		= 0644,
24511da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_jiffies,
24521da177e4SLinus Torvalds 			.strategy	= &sysctl_jiffies,
24531da177e4SLinus Torvalds 		},
24541da177e4SLinus Torvalds 		{
24551da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_DELAY_PROBE_TIME,
24561da177e4SLinus Torvalds 			.procname	= "delay_first_probe_time",
24571da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24581da177e4SLinus Torvalds 			.mode		= 0644,
24591da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_jiffies,
24601da177e4SLinus Torvalds 			.strategy	= &sysctl_jiffies,
24611da177e4SLinus Torvalds 		},
24621da177e4SLinus Torvalds 		{
24631da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_GC_STALE_TIME,
24641da177e4SLinus Torvalds 			.procname	= "gc_stale_time",
24651da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24661da177e4SLinus Torvalds 			.mode		= 0644,
24671da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_jiffies,
24681da177e4SLinus Torvalds 			.strategy	= &sysctl_jiffies,
24691da177e4SLinus Torvalds 		},
24701da177e4SLinus Torvalds 		{
24711da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_UNRES_QLEN,
24721da177e4SLinus Torvalds 			.procname	= "unres_qlen",
24731da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24741da177e4SLinus Torvalds 			.mode		= 0644,
24751da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
24761da177e4SLinus Torvalds 		},
24771da177e4SLinus Torvalds 		{
24781da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_PROXY_QLEN,
24791da177e4SLinus Torvalds 			.procname	= "proxy_qlen",
24801da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24811da177e4SLinus Torvalds 			.mode		= 0644,
24821da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
24831da177e4SLinus Torvalds 		},
24841da177e4SLinus Torvalds 		{
24851da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_ANYCAST_DELAY,
24861da177e4SLinus Torvalds 			.procname	= "anycast_delay",
24871da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24881da177e4SLinus Torvalds 			.mode		= 0644,
24891da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_userhz_jiffies,
24901da177e4SLinus Torvalds 		},
24911da177e4SLinus Torvalds 		{
24921da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_PROXY_DELAY,
24931da177e4SLinus Torvalds 			.procname	= "proxy_delay",
24941da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
24951da177e4SLinus Torvalds 			.mode		= 0644,
24961da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_userhz_jiffies,
24971da177e4SLinus Torvalds 		},
24981da177e4SLinus Torvalds 		{
24991da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_LOCKTIME,
25001da177e4SLinus Torvalds 			.procname	= "locktime",
25011da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25021da177e4SLinus Torvalds 			.mode		= 0644,
25031da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_userhz_jiffies,
25041da177e4SLinus Torvalds 		},
25051da177e4SLinus Torvalds 		{
25061da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_GC_INTERVAL,
25071da177e4SLinus Torvalds 			.procname	= "gc_interval",
25081da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25091da177e4SLinus Torvalds 			.mode		= 0644,
25101da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_jiffies,
25111da177e4SLinus Torvalds 			.strategy	= &sysctl_jiffies,
25121da177e4SLinus Torvalds 		},
25131da177e4SLinus Torvalds 		{
25141da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_GC_THRESH1,
25151da177e4SLinus Torvalds 			.procname	= "gc_thresh1",
25161da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25171da177e4SLinus Torvalds 			.mode		= 0644,
25181da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
25191da177e4SLinus Torvalds 		},
25201da177e4SLinus Torvalds 		{
25211da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_GC_THRESH2,
25221da177e4SLinus Torvalds 			.procname	= "gc_thresh2",
25231da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25241da177e4SLinus Torvalds 			.mode		= 0644,
25251da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
25261da177e4SLinus Torvalds 		},
25271da177e4SLinus Torvalds 		{
25281da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_GC_THRESH3,
25291da177e4SLinus Torvalds 			.procname	= "gc_thresh3",
25301da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25311da177e4SLinus Torvalds 			.mode		= 0644,
25321da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec,
25331da177e4SLinus Torvalds 		},
25341da177e4SLinus Torvalds 		{
25351da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_RETRANS_TIME_MS,
25361da177e4SLinus Torvalds 			.procname	= "retrans_time_ms",
25371da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25381da177e4SLinus Torvalds 			.mode		= 0644,
25391da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_ms_jiffies,
25401da177e4SLinus Torvalds 			.strategy	= &sysctl_ms_jiffies,
25411da177e4SLinus Torvalds 		},
25421da177e4SLinus Torvalds 		{
25431da177e4SLinus Torvalds 			.ctl_name	= NET_NEIGH_REACHABLE_TIME_MS,
25441da177e4SLinus Torvalds 			.procname	= "base_reachable_time_ms",
25451da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
25461da177e4SLinus Torvalds 			.mode		= 0644,
25471da177e4SLinus Torvalds 			.proc_handler	= &proc_dointvec_ms_jiffies,
25481da177e4SLinus Torvalds 			.strategy	= &sysctl_ms_jiffies,
25491da177e4SLinus Torvalds 		},
25501da177e4SLinus Torvalds 	},
25511da177e4SLinus Torvalds 	.neigh_dev = {
25521da177e4SLinus Torvalds 		{
25531da177e4SLinus Torvalds 			.ctl_name	= NET_PROTO_CONF_DEFAULT,
25541da177e4SLinus Torvalds 			.procname	= "default",
25551da177e4SLinus Torvalds 			.mode		= 0555,
25561da177e4SLinus Torvalds 		},
25571da177e4SLinus Torvalds 	},
25581da177e4SLinus Torvalds 	.neigh_neigh_dir = {
25591da177e4SLinus Torvalds 		{
25601da177e4SLinus Torvalds 			.procname	= "neigh",
25611da177e4SLinus Torvalds 			.mode		= 0555,
25621da177e4SLinus Torvalds 		},
25631da177e4SLinus Torvalds 	},
25641da177e4SLinus Torvalds 	.neigh_proto_dir = {
25651da177e4SLinus Torvalds 		{
25661da177e4SLinus Torvalds 			.mode		= 0555,
25671da177e4SLinus Torvalds 		},
25681da177e4SLinus Torvalds 	},
25691da177e4SLinus Torvalds 	.neigh_root_dir = {
25701da177e4SLinus Torvalds 		{
25711da177e4SLinus Torvalds 			.ctl_name	= CTL_NET,
25721da177e4SLinus Torvalds 			.procname	= "net",
25731da177e4SLinus Torvalds 			.mode		= 0555,
25741da177e4SLinus Torvalds 		},
25751da177e4SLinus Torvalds 	},
25761da177e4SLinus Torvalds };
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
25791da177e4SLinus Torvalds 			  int p_id, int pdev_id, char *p_name,
25801da177e4SLinus Torvalds 			  proc_handler *handler, ctl_handler *strategy)
25811da177e4SLinus Torvalds {
25821da177e4SLinus Torvalds 	struct neigh_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
25831da177e4SLinus Torvalds 	const char *dev_name_source = NULL;
25841da177e4SLinus Torvalds 	char *dev_name = NULL;
25851da177e4SLinus Torvalds 	int err = 0;
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	if (!t)
25881da177e4SLinus Torvalds 		return -ENOBUFS;
25891da177e4SLinus Torvalds 	memcpy(t, &neigh_sysctl_template, sizeof(*t));
25901da177e4SLinus Torvalds 	t->neigh_vars[0].data  = &p->mcast_probes;
25911da177e4SLinus Torvalds 	t->neigh_vars[1].data  = &p->ucast_probes;
25921da177e4SLinus Torvalds 	t->neigh_vars[2].data  = &p->app_probes;
25931da177e4SLinus Torvalds 	t->neigh_vars[3].data  = &p->retrans_time;
25941da177e4SLinus Torvalds 	t->neigh_vars[4].data  = &p->base_reachable_time;
25951da177e4SLinus Torvalds 	t->neigh_vars[5].data  = &p->delay_probe_time;
25961da177e4SLinus Torvalds 	t->neigh_vars[6].data  = &p->gc_staletime;
25971da177e4SLinus Torvalds 	t->neigh_vars[7].data  = &p->queue_len;
25981da177e4SLinus Torvalds 	t->neigh_vars[8].data  = &p->proxy_qlen;
25991da177e4SLinus Torvalds 	t->neigh_vars[9].data  = &p->anycast_delay;
26001da177e4SLinus Torvalds 	t->neigh_vars[10].data = &p->proxy_delay;
26011da177e4SLinus Torvalds 	t->neigh_vars[11].data = &p->locktime;
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	if (dev) {
26041da177e4SLinus Torvalds 		dev_name_source = dev->name;
26051da177e4SLinus Torvalds 		t->neigh_dev[0].ctl_name = dev->ifindex;
26061da177e4SLinus Torvalds 		t->neigh_vars[12].procname = NULL;
26071da177e4SLinus Torvalds 		t->neigh_vars[13].procname = NULL;
26081da177e4SLinus Torvalds 		t->neigh_vars[14].procname = NULL;
26091da177e4SLinus Torvalds 		t->neigh_vars[15].procname = NULL;
26101da177e4SLinus Torvalds 	} else {
26111da177e4SLinus Torvalds  		dev_name_source = t->neigh_dev[0].procname;
26121da177e4SLinus Torvalds 		t->neigh_vars[12].data = (int *)(p + 1);
26131da177e4SLinus Torvalds 		t->neigh_vars[13].data = (int *)(p + 1) + 1;
26141da177e4SLinus Torvalds 		t->neigh_vars[14].data = (int *)(p + 1) + 2;
26151da177e4SLinus Torvalds 		t->neigh_vars[15].data = (int *)(p + 1) + 3;
26161da177e4SLinus Torvalds 	}
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds 	t->neigh_vars[16].data  = &p->retrans_time;
26191da177e4SLinus Torvalds 	t->neigh_vars[17].data  = &p->base_reachable_time;
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	if (handler || strategy) {
26221da177e4SLinus Torvalds 		/* RetransTime */
26231da177e4SLinus Torvalds 		t->neigh_vars[3].proc_handler = handler;
26241da177e4SLinus Torvalds 		t->neigh_vars[3].strategy = strategy;
26251da177e4SLinus Torvalds 		t->neigh_vars[3].extra1 = dev;
26261da177e4SLinus Torvalds 		/* ReachableTime */
26271da177e4SLinus Torvalds 		t->neigh_vars[4].proc_handler = handler;
26281da177e4SLinus Torvalds 		t->neigh_vars[4].strategy = strategy;
26291da177e4SLinus Torvalds 		t->neigh_vars[4].extra1 = dev;
26301da177e4SLinus Torvalds 		/* RetransTime (in milliseconds)*/
26311da177e4SLinus Torvalds 		t->neigh_vars[16].proc_handler = handler;
26321da177e4SLinus Torvalds 		t->neigh_vars[16].strategy = strategy;
26331da177e4SLinus Torvalds 		t->neigh_vars[16].extra1 = dev;
26341da177e4SLinus Torvalds 		/* ReachableTime (in milliseconds) */
26351da177e4SLinus Torvalds 		t->neigh_vars[17].proc_handler = handler;
26361da177e4SLinus Torvalds 		t->neigh_vars[17].strategy = strategy;
26371da177e4SLinus Torvalds 		t->neigh_vars[17].extra1 = dev;
26381da177e4SLinus Torvalds 	}
26391da177e4SLinus Torvalds 
2640543537bdSPaulo Marques 	dev_name = kstrdup(dev_name_source, GFP_KERNEL);
26411da177e4SLinus Torvalds 	if (!dev_name) {
26421da177e4SLinus Torvalds 		err = -ENOBUFS;
26431da177e4SLinus Torvalds 		goto free;
26441da177e4SLinus Torvalds 	}
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds  	t->neigh_dev[0].procname = dev_name;
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	t->neigh_neigh_dir[0].ctl_name = pdev_id;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	t->neigh_proto_dir[0].procname = p_name;
26511da177e4SLinus Torvalds 	t->neigh_proto_dir[0].ctl_name = p_id;
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	t->neigh_dev[0].child	       = t->neigh_vars;
26541da177e4SLinus Torvalds 	t->neigh_neigh_dir[0].child    = t->neigh_dev;
26551da177e4SLinus Torvalds 	t->neigh_proto_dir[0].child    = t->neigh_neigh_dir;
26561da177e4SLinus Torvalds 	t->neigh_root_dir[0].child     = t->neigh_proto_dir;
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 	t->sysctl_header = register_sysctl_table(t->neigh_root_dir, 0);
26591da177e4SLinus Torvalds 	if (!t->sysctl_header) {
26601da177e4SLinus Torvalds 		err = -ENOBUFS;
26611da177e4SLinus Torvalds 		goto free_procname;
26621da177e4SLinus Torvalds 	}
26631da177e4SLinus Torvalds 	p->sysctl_table = t;
26641da177e4SLinus Torvalds 	return 0;
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	/* error path */
26671da177e4SLinus Torvalds  free_procname:
26681da177e4SLinus Torvalds 	kfree(dev_name);
26691da177e4SLinus Torvalds  free:
26701da177e4SLinus Torvalds 	kfree(t);
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 	return err;
26731da177e4SLinus Torvalds }
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p)
26761da177e4SLinus Torvalds {
26771da177e4SLinus Torvalds 	if (p->sysctl_table) {
26781da177e4SLinus Torvalds 		struct neigh_sysctl_table *t = p->sysctl_table;
26791da177e4SLinus Torvalds 		p->sysctl_table = NULL;
26801da177e4SLinus Torvalds 		unregister_sysctl_table(t->sysctl_header);
26811da177e4SLinus Torvalds 		kfree(t->neigh_dev[0].procname);
26821da177e4SLinus Torvalds 		kfree(t);
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds #endif	/* CONFIG_SYSCTL */
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_event_send);
26891da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_add);
26901da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_changeaddr);
26911da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_compat_output);
26921da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_connected_output);
26931da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_create);
26941da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_delete);
26951da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_destroy);
26961da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_dump_info);
26971da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_event_ns);
26981da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_ifdown);
26991da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_lookup);
27001da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_lookup_nodev);
27011da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_parms_alloc);
27021da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_parms_release);
27031da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_rand_reach_time);
27041da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_resolve_output);
27051da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_table_clear);
27061da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_table_init);
2707bd89efc5SSimon Kelley EXPORT_SYMBOL(neigh_table_init_no_netlink);
27081da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_update);
27091da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_update_hhs);
27101da177e4SLinus Torvalds EXPORT_SYMBOL(pneigh_enqueue);
27111da177e4SLinus Torvalds EXPORT_SYMBOL(pneigh_lookup);
2712c7fb64dbSThomas Graf EXPORT_SYMBOL(neightbl_dump_info);
2713c7fb64dbSThomas Graf EXPORT_SYMBOL(neightbl_set);
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds #ifdef CONFIG_ARPD
27161da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_app_ns);
27171da177e4SLinus Torvalds #endif
27181da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27191da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_sysctl_register);
27201da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_sysctl_unregister);
27211da177e4SLinus Torvalds #endif
2722