xref: /openbmc/linux/net/core/neighbour.c (revision 95c96174)
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 
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/types.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/socket.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>
29457c4cbcSEric W. Biederman #include <net/net_namespace.h>
301da177e4SLinus Torvalds #include <net/neighbour.h>
311da177e4SLinus Torvalds #include <net/dst.h>
321da177e4SLinus Torvalds #include <net/sock.h>
338d71740cSTom Tucker #include <net/netevent.h>
34a14a49d2SThomas Graf #include <net/netlink.h>
351da177e4SLinus Torvalds #include <linux/rtnetlink.h>
361da177e4SLinus Torvalds #include <linux/random.h>
37543537bdSPaulo Marques #include <linux/string.h>
38c3609d51Svignesh babu #include <linux/log2.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define NEIGH_DEBUG 1
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define NEIGH_PRINTK(x...) printk(x)
431da177e4SLinus Torvalds #define NEIGH_NOPRINTK(x...) do { ; } while(0)
441da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_NOPRINTK
451da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_NOPRINTK
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #if NEIGH_DEBUG >= 1
481da177e4SLinus Torvalds #undef NEIGH_PRINTK1
491da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_PRINTK
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds #if NEIGH_DEBUG >= 2
521da177e4SLinus Torvalds #undef NEIGH_PRINTK2
531da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_PRINTK
541da177e4SLinus Torvalds #endif
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define PNEIGH_HASHMASK		0xF
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg);
59d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags);
60d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh);
611da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds static struct neigh_table *neigh_tables;
6445fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS
659a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops;
6645fc3b11SAmos Waterland #endif
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds    Neighbour hash table buckets are protected with rwlock tbl->lock.
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds    - All the scans/updates to hash buckets MUST be made under this lock.
721da177e4SLinus Torvalds    - NOTHING clever should be made under this lock: no callbacks
731da177e4SLinus Torvalds      to protocol backends, no attempts to send something to network.
741da177e4SLinus Torvalds      It will result in deadlocks, if backend/driver wants to use neighbour
751da177e4SLinus Torvalds      cache.
761da177e4SLinus Torvalds    - If the entry requires some non-trivial actions, increase
771da177e4SLinus Torvalds      its reference count and release table lock.
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds    Neighbour entries are protected:
801da177e4SLinus Torvalds    - with reference count.
811da177e4SLinus Torvalds    - with rwlock neigh->lock
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds    Reference count prevents destruction.
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds    neigh->lock mainly serializes ll address data and its validity state.
861da177e4SLinus Torvalds    However, the same lock is used to protect another entry fields:
871da177e4SLinus Torvalds     - timer
881da177e4SLinus Torvalds     - resolution queue
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds    Again, nothing clever shall be made under neigh->lock,
911da177e4SLinus Torvalds    the most complicated procedure, which we allow is dev->hard_header.
921da177e4SLinus Torvalds    It is supposed, that dev->hard_header is simplistic and does
931da177e4SLinus Torvalds    not make callbacks to neighbour tables.
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds    The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
961da177e4SLinus Torvalds    list of neighbour tables. This list is used only in process context,
971da177e4SLinus Torvalds  */
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock);
1001da177e4SLinus Torvalds 
1018f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	kfree_skb(skb);
1041da177e4SLinus Torvalds 	return -ENETDOWN;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1074f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh)
1084f494554SThomas Graf {
1094f494554SThomas Graf 	if (neigh->parms->neigh_cleanup)
1104f494554SThomas Graf 		neigh->parms->neigh_cleanup(neigh);
1114f494554SThomas Graf 
112d961db35SThomas Graf 	__neigh_notify(neigh, RTM_DELNEIGH, 0);
1134f494554SThomas Graf 	neigh_release(neigh);
1144f494554SThomas Graf }
1154f494554SThomas Graf 
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * It is random distribution in the interval (1/2)*base...(3/2)*base.
1181da177e4SLinus Torvalds  * It corresponds to default IPv6 settings and is not overridable,
1191da177e4SLinus Torvalds  * because it is really reasonable choice.
1201da177e4SLinus Torvalds  */
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base)
1231da177e4SLinus Torvalds {
124a02cec21SEric Dumazet 	return base ? (net_random() % base) + (base >> 1) : 0;
1251da177e4SLinus Torvalds }
1260a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	int shrunk = 0;
1321da177e4SLinus Torvalds 	int i;
133d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
138d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
139d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
140cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
141767e97e1SEric Dumazet 		struct neighbour *n;
142767e97e1SEric Dumazet 		struct neighbour __rcu **np;
1431da177e4SLinus Torvalds 
144d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
145767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
146767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
1471da177e4SLinus Torvalds 			/* Neighbour record may be discarded if:
1481da177e4SLinus Torvalds 			 * - nobody refers to it.
1491da177e4SLinus Torvalds 			 * - it is not permanent
1501da177e4SLinus Torvalds 			 */
1511da177e4SLinus Torvalds 			write_lock(&n->lock);
1521da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
1531da177e4SLinus Torvalds 			    !(n->nud_state & NUD_PERMANENT)) {
154767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
155767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
156767e97e1SEric Dumazet 						  lockdep_is_held(&tbl->lock)));
1571da177e4SLinus Torvalds 				n->dead = 1;
1581da177e4SLinus Torvalds 				shrunk	= 1;
1591da177e4SLinus Torvalds 				write_unlock(&n->lock);
1604f494554SThomas Graf 				neigh_cleanup_and_release(n);
1611da177e4SLinus Torvalds 				continue;
1621da177e4SLinus Torvalds 			}
1631da177e4SLinus Torvalds 			write_unlock(&n->lock);
1641da177e4SLinus Torvalds 			np = &n->next;
1651da177e4SLinus Torvalds 		}
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	tbl->last_flush = jiffies;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	return shrunk;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
175a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when)
176a43d8994SPavel Emelyanov {
177a43d8994SPavel Emelyanov 	neigh_hold(n);
178a43d8994SPavel Emelyanov 	if (unlikely(mod_timer(&n->timer, when))) {
179a43d8994SPavel Emelyanov 		printk("NEIGH: BUG, double timer add, state is %x\n",
180a43d8994SPavel Emelyanov 		       n->nud_state);
181a43d8994SPavel Emelyanov 		dump_stack();
182a43d8994SPavel Emelyanov 	}
183a43d8994SPavel Emelyanov }
184a43d8994SPavel Emelyanov 
1851da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	if ((n->nud_state & NUD_IN_TIMER) &&
1881da177e4SLinus Torvalds 	    del_timer(&n->timer)) {
1891da177e4SLinus Torvalds 		neigh_release(n);
1901da177e4SLinus Torvalds 		return 1;
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 	return 0;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct sk_buff *skb;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL) {
2001da177e4SLinus Torvalds 		dev_put(skb->dev);
2011da177e4SLinus Torvalds 		kfree_skb(skb);
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
20549636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	int i;
208d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
2091da177e4SLinus Torvalds 
210d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
211d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
212d6bf7817SEric Dumazet 
213cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
214767e97e1SEric Dumazet 		struct neighbour *n;
215767e97e1SEric Dumazet 		struct neighbour __rcu **np = &nht->hash_buckets[i];
2161da177e4SLinus Torvalds 
217767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
218767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
2191da177e4SLinus Torvalds 			if (dev && n->dev != dev) {
2201da177e4SLinus Torvalds 				np = &n->next;
2211da177e4SLinus Torvalds 				continue;
2221da177e4SLinus Torvalds 			}
223767e97e1SEric Dumazet 			rcu_assign_pointer(*np,
224767e97e1SEric Dumazet 				   rcu_dereference_protected(n->next,
225767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
2261da177e4SLinus Torvalds 			write_lock(&n->lock);
2271da177e4SLinus Torvalds 			neigh_del_timer(n);
2281da177e4SLinus Torvalds 			n->dead = 1;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) != 1) {
2311da177e4SLinus Torvalds 				/* The most unpleasant situation.
2321da177e4SLinus Torvalds 				   We must destroy neighbour entry,
2331da177e4SLinus Torvalds 				   but someone still uses it.
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 				   The destroy will be delayed until
2361da177e4SLinus Torvalds 				   the last user releases us, but
2371da177e4SLinus Torvalds 				   we must kill timers etc. and move
2381da177e4SLinus Torvalds 				   it to safe state.
2391da177e4SLinus Torvalds 				 */
2401da177e4SLinus Torvalds 				skb_queue_purge(&n->arp_queue);
2418b5c171bSEric Dumazet 				n->arp_queue_len_bytes = 0;
2421da177e4SLinus Torvalds 				n->output = neigh_blackhole;
2431da177e4SLinus Torvalds 				if (n->nud_state & NUD_VALID)
2441da177e4SLinus Torvalds 					n->nud_state = NUD_NOARP;
2451da177e4SLinus Torvalds 				else
2461da177e4SLinus Torvalds 					n->nud_state = NUD_NONE;
2471da177e4SLinus Torvalds 				NEIGH_PRINTK2("neigh %p is stray.\n", n);
2481da177e4SLinus Torvalds 			}
2491da177e4SLinus Torvalds 			write_unlock(&n->lock);
2504f494554SThomas Graf 			neigh_cleanup_and_release(n);
2511da177e4SLinus Torvalds 		}
2521da177e4SLinus Torvalds 	}
25349636bb1SHerbert Xu }
2541da177e4SLinus Torvalds 
25549636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
25649636bb1SHerbert Xu {
25749636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
25849636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
25949636bb1SHerbert Xu 	write_unlock_bh(&tbl->lock);
26049636bb1SHerbert Xu }
2610a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr);
26249636bb1SHerbert Xu 
26349636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
26449636bb1SHerbert Xu {
26549636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
26649636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
2671da177e4SLinus Torvalds 	pneigh_ifdown(tbl, dev);
2681da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
2711da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
2721da177e4SLinus Torvalds 	return 0;
2731da177e4SLinus Torvalds }
2740a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown);
2751da177e4SLinus Torvalds 
276596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	struct neighbour *n = NULL;
2791da177e4SLinus Torvalds 	unsigned long now = jiffies;
2801da177e4SLinus Torvalds 	int entries;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	entries = atomic_inc_return(&tbl->entries) - 1;
2831da177e4SLinus Torvalds 	if (entries >= tbl->gc_thresh3 ||
2841da177e4SLinus Torvalds 	    (entries >= tbl->gc_thresh2 &&
2851da177e4SLinus Torvalds 	     time_after(now, tbl->last_flush + 5 * HZ))) {
2861da177e4SLinus Torvalds 		if (!neigh_forced_gc(tbl) &&
2871da177e4SLinus Torvalds 		    entries >= tbl->gc_thresh3)
2881da177e4SLinus Torvalds 			goto out_entries;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
291596b9b68SDavid Miller 	if (tbl->entry_size)
2925b8b0060SDavid Miller 		n = kzalloc(tbl->entry_size, GFP_ATOMIC);
293596b9b68SDavid Miller 	else {
294596b9b68SDavid Miller 		int sz = sizeof(*n) + tbl->key_len;
295596b9b68SDavid Miller 
296596b9b68SDavid Miller 		sz = ALIGN(sz, NEIGH_PRIV_ALIGN);
297596b9b68SDavid Miller 		sz += dev->neigh_priv_len;
298596b9b68SDavid Miller 		n = kzalloc(sz, GFP_ATOMIC);
299596b9b68SDavid Miller 	}
3001da177e4SLinus Torvalds 	if (!n)
3011da177e4SLinus Torvalds 		goto out_entries;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	skb_queue_head_init(&n->arp_queue);
3041da177e4SLinus Torvalds 	rwlock_init(&n->lock);
3050ed8ddf4SEric Dumazet 	seqlock_init(&n->ha_lock);
3061da177e4SLinus Torvalds 	n->updated	  = n->used = now;
3071da177e4SLinus Torvalds 	n->nud_state	  = NUD_NONE;
3081da177e4SLinus Torvalds 	n->output	  = neigh_blackhole;
309f6b72b62SDavid S. Miller 	seqlock_init(&n->hh.hh_lock);
3101da177e4SLinus Torvalds 	n->parms	  = neigh_parms_clone(&tbl->parms);
311b24b8a24SPavel Emelyanov 	setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, allocs);
3141da177e4SLinus Torvalds 	n->tbl		  = tbl;
3151da177e4SLinus Torvalds 	atomic_set(&n->refcnt, 1);
3161da177e4SLinus Torvalds 	n->dead		  = 1;
3171da177e4SLinus Torvalds out:
3181da177e4SLinus Torvalds 	return n;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds out_entries:
3211da177e4SLinus Torvalds 	atomic_dec(&tbl->entries);
3221da177e4SLinus Torvalds 	goto out;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3252c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x)
3262c2aba6cSDavid S. Miller {
3272c2aba6cSDavid S. Miller 	get_random_bytes(x, sizeof(*x));
3282c2aba6cSDavid S. Miller 	*x |= 1;
3292c2aba6cSDavid S. Miller }
3302c2aba6cSDavid S. Miller 
331cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
3321da177e4SLinus Torvalds {
333cd089336SDavid S. Miller 	size_t size = (1 << shift) * sizeof(struct neighbour *);
334d6bf7817SEric Dumazet 	struct neigh_hash_table *ret;
3356193d2beSEric Dumazet 	struct neighbour __rcu **buckets;
3362c2aba6cSDavid S. Miller 	int i;
3371da177e4SLinus Torvalds 
338d6bf7817SEric Dumazet 	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
339d6bf7817SEric Dumazet 	if (!ret)
340d6bf7817SEric Dumazet 		return NULL;
341d6bf7817SEric Dumazet 	if (size <= PAGE_SIZE)
342d6bf7817SEric Dumazet 		buckets = kzalloc(size, GFP_ATOMIC);
343d6bf7817SEric Dumazet 	else
3446193d2beSEric Dumazet 		buckets = (struct neighbour __rcu **)
345d6bf7817SEric Dumazet 			  __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
346d6bf7817SEric Dumazet 					   get_order(size));
347d6bf7817SEric Dumazet 	if (!buckets) {
348d6bf7817SEric Dumazet 		kfree(ret);
349d6bf7817SEric Dumazet 		return NULL;
3501da177e4SLinus Torvalds 	}
3516193d2beSEric Dumazet 	ret->hash_buckets = buckets;
352cd089336SDavid S. Miller 	ret->hash_shift = shift;
3532c2aba6cSDavid S. Miller 	for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
3542c2aba6cSDavid S. Miller 		neigh_get_hash_rnd(&ret->hash_rnd[i]);
3551da177e4SLinus Torvalds 	return ret;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
358d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head)
3591da177e4SLinus Torvalds {
360d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = container_of(head,
361d6bf7817SEric Dumazet 						    struct neigh_hash_table,
362d6bf7817SEric Dumazet 						    rcu);
363cd089336SDavid S. Miller 	size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
3646193d2beSEric Dumazet 	struct neighbour __rcu **buckets = nht->hash_buckets;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (size <= PAGE_SIZE)
367d6bf7817SEric Dumazet 		kfree(buckets);
3681da177e4SLinus Torvalds 	else
369d6bf7817SEric Dumazet 		free_pages((unsigned long)buckets, get_order(size));
370d6bf7817SEric Dumazet 	kfree(nht);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
373d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
374cd089336SDavid S. Miller 						unsigned long new_shift)
3751da177e4SLinus Torvalds {
376d6bf7817SEric Dumazet 	unsigned int i, hash;
377d6bf7817SEric Dumazet 	struct neigh_hash_table *new_nht, *old_nht;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
3801da177e4SLinus Torvalds 
381d6bf7817SEric Dumazet 	old_nht = rcu_dereference_protected(tbl->nht,
382d6bf7817SEric Dumazet 					    lockdep_is_held(&tbl->lock));
383cd089336SDavid S. Miller 	new_nht = neigh_hash_alloc(new_shift);
384d6bf7817SEric Dumazet 	if (!new_nht)
385d6bf7817SEric Dumazet 		return old_nht;
3861da177e4SLinus Torvalds 
387cd089336SDavid S. Miller 	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
3881da177e4SLinus Torvalds 		struct neighbour *n, *next;
3891da177e4SLinus Torvalds 
390767e97e1SEric Dumazet 		for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
391767e97e1SEric Dumazet 						   lockdep_is_held(&tbl->lock));
392d6bf7817SEric Dumazet 		     n != NULL;
393d6bf7817SEric Dumazet 		     n = next) {
394d6bf7817SEric Dumazet 			hash = tbl->hash(n->primary_key, n->dev,
395d6bf7817SEric Dumazet 					 new_nht->hash_rnd);
3961da177e4SLinus Torvalds 
397cd089336SDavid S. Miller 			hash >>= (32 - new_nht->hash_shift);
398767e97e1SEric Dumazet 			next = rcu_dereference_protected(n->next,
399767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock));
4001da177e4SLinus Torvalds 
401767e97e1SEric Dumazet 			rcu_assign_pointer(n->next,
402767e97e1SEric Dumazet 					   rcu_dereference_protected(
403767e97e1SEric Dumazet 						new_nht->hash_buckets[hash],
404767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
405767e97e1SEric Dumazet 			rcu_assign_pointer(new_nht->hash_buckets[hash], n);
4061da177e4SLinus Torvalds 		}
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
409d6bf7817SEric Dumazet 	rcu_assign_pointer(tbl->nht, new_nht);
410d6bf7817SEric Dumazet 	call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
411d6bf7817SEric Dumazet 	return new_nht;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
4151da177e4SLinus Torvalds 			       struct net_device *dev)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct neighbour *n;
4181da177e4SLinus Torvalds 	int key_len = tbl->key_len;
419bc4bf5f3SPavel Emelyanov 	u32 hash_val;
420d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4231da177e4SLinus Torvalds 
424d6bf7817SEric Dumazet 	rcu_read_lock_bh();
425d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
426cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
427767e97e1SEric Dumazet 
428767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
429767e97e1SEric Dumazet 	     n != NULL;
430767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
4311da177e4SLinus Torvalds 		if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
432767e97e1SEric Dumazet 			if (!atomic_inc_not_zero(&n->refcnt))
433767e97e1SEric Dumazet 				n = NULL;
4341da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
4351da177e4SLinus Torvalds 			break;
4361da177e4SLinus Torvalds 		}
4371da177e4SLinus Torvalds 	}
438767e97e1SEric Dumazet 
439d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4401da177e4SLinus Torvalds 	return n;
4411da177e4SLinus Torvalds }
4420a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup);
4431da177e4SLinus Torvalds 
444426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
445426b5303SEric W. Biederman 				     const void *pkey)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct neighbour *n;
4481da177e4SLinus Torvalds 	int key_len = tbl->key_len;
449bc4bf5f3SPavel Emelyanov 	u32 hash_val;
450d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4531da177e4SLinus Torvalds 
454d6bf7817SEric Dumazet 	rcu_read_lock_bh();
455d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
456cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
457767e97e1SEric Dumazet 
458767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
459767e97e1SEric Dumazet 	     n != NULL;
460767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
461426b5303SEric W. Biederman 		if (!memcmp(n->primary_key, pkey, key_len) &&
462878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(n->dev), net)) {
463767e97e1SEric Dumazet 			if (!atomic_inc_not_zero(&n->refcnt))
464767e97e1SEric Dumazet 				n = NULL;
4651da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
4661da177e4SLinus Torvalds 			break;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 	}
469767e97e1SEric Dumazet 
470d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4711da177e4SLinus Torvalds 	return n;
4721da177e4SLinus Torvalds }
4730a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
4761da177e4SLinus Torvalds 			       struct net_device *dev)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	u32 hash_val;
4791da177e4SLinus Torvalds 	int key_len = tbl->key_len;
4801da177e4SLinus Torvalds 	int error;
481596b9b68SDavid Miller 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
482d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	if (!n) {
4851da177e4SLinus Torvalds 		rc = ERR_PTR(-ENOBUFS);
4861da177e4SLinus Torvalds 		goto out;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	memcpy(n->primary_key, pkey, key_len);
4901da177e4SLinus Torvalds 	n->dev = dev;
4911da177e4SLinus Torvalds 	dev_hold(dev);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	/* Protocol specific setup. */
4941da177e4SLinus Torvalds 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
4951da177e4SLinus Torvalds 		rc = ERR_PTR(error);
4961da177e4SLinus Torvalds 		goto out_neigh_release;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 
499da6a8fa0SDavid Miller 	if (dev->netdev_ops->ndo_neigh_construct) {
500da6a8fa0SDavid Miller 		error = dev->netdev_ops->ndo_neigh_construct(n);
501da6a8fa0SDavid Miller 		if (error < 0) {
502da6a8fa0SDavid Miller 			rc = ERR_PTR(error);
503da6a8fa0SDavid Miller 			goto out_neigh_release;
504da6a8fa0SDavid Miller 		}
505da6a8fa0SDavid Miller 	}
506da6a8fa0SDavid Miller 
507447f2191SDavid S. Miller 	/* Device specific setup. */
508447f2191SDavid S. Miller 	if (n->parms->neigh_setup &&
509447f2191SDavid S. Miller 	    (error = n->parms->neigh_setup(n)) < 0) {
510447f2191SDavid S. Miller 		rc = ERR_PTR(error);
511447f2191SDavid S. Miller 		goto out_neigh_release;
512447f2191SDavid S. Miller 	}
513447f2191SDavid S. Miller 
5141da177e4SLinus Torvalds 	n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
517d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
518d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
5191da177e4SLinus Torvalds 
520cd089336SDavid S. Miller 	if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
521cd089336SDavid S. Miller 		nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
5221da177e4SLinus Torvalds 
523cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	if (n->parms->dead) {
5261da177e4SLinus Torvalds 		rc = ERR_PTR(-EINVAL);
5271da177e4SLinus Torvalds 		goto out_tbl_unlock;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 
530767e97e1SEric Dumazet 	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
531767e97e1SEric Dumazet 					    lockdep_is_held(&tbl->lock));
532767e97e1SEric Dumazet 	     n1 != NULL;
533767e97e1SEric Dumazet 	     n1 = rcu_dereference_protected(n1->next,
534767e97e1SEric Dumazet 			lockdep_is_held(&tbl->lock))) {
5351da177e4SLinus Torvalds 		if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
5361da177e4SLinus Torvalds 			neigh_hold(n1);
5371da177e4SLinus Torvalds 			rc = n1;
5381da177e4SLinus Torvalds 			goto out_tbl_unlock;
5391da177e4SLinus Torvalds 		}
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	n->dead = 0;
5431da177e4SLinus Torvalds 	neigh_hold(n);
544767e97e1SEric Dumazet 	rcu_assign_pointer(n->next,
545767e97e1SEric Dumazet 			   rcu_dereference_protected(nht->hash_buckets[hash_val],
546767e97e1SEric Dumazet 						     lockdep_is_held(&tbl->lock)));
547767e97e1SEric Dumazet 	rcu_assign_pointer(nht->hash_buckets[hash_val], n);
5481da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
5491da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is created.\n", n);
5501da177e4SLinus Torvalds 	rc = n;
5511da177e4SLinus Torvalds out:
5521da177e4SLinus Torvalds 	return rc;
5531da177e4SLinus Torvalds out_tbl_unlock:
5541da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
5551da177e4SLinus Torvalds out_neigh_release:
5561da177e4SLinus Torvalds 	neigh_release(n);
5571da177e4SLinus Torvalds 	goto out;
5581da177e4SLinus Torvalds }
5590a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_create);
5601da177e4SLinus Torvalds 
561be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len)
562fa86d322SPavel Emelyanov {
563fa86d322SPavel Emelyanov 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
564fa86d322SPavel Emelyanov 	hash_val ^= (hash_val >> 16);
565fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 8;
566fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 4;
567fa86d322SPavel Emelyanov 	hash_val &= PNEIGH_HASHMASK;
568be01d655SYOSHIFUJI Hideaki 	return hash_val;
569fa86d322SPavel Emelyanov }
570fa86d322SPavel Emelyanov 
571be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
572be01d655SYOSHIFUJI Hideaki 					      struct net *net,
573be01d655SYOSHIFUJI Hideaki 					      const void *pkey,
574be01d655SYOSHIFUJI Hideaki 					      int key_len,
575be01d655SYOSHIFUJI Hideaki 					      struct net_device *dev)
576be01d655SYOSHIFUJI Hideaki {
577be01d655SYOSHIFUJI Hideaki 	while (n) {
578be01d655SYOSHIFUJI Hideaki 		if (!memcmp(n->key, pkey, key_len) &&
579be01d655SYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net) &&
580be01d655SYOSHIFUJI Hideaki 		    (n->dev == dev || !n->dev))
581fa86d322SPavel Emelyanov 			return n;
582be01d655SYOSHIFUJI Hideaki 		n = n->next;
583be01d655SYOSHIFUJI Hideaki 	}
584be01d655SYOSHIFUJI Hideaki 	return NULL;
585be01d655SYOSHIFUJI Hideaki }
586be01d655SYOSHIFUJI Hideaki 
587be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
588be01d655SYOSHIFUJI Hideaki 		struct net *net, const void *pkey, struct net_device *dev)
589be01d655SYOSHIFUJI Hideaki {
590be01d655SYOSHIFUJI Hideaki 	int key_len = tbl->key_len;
591be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
592be01d655SYOSHIFUJI Hideaki 
593be01d655SYOSHIFUJI Hideaki 	return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
594be01d655SYOSHIFUJI Hideaki 				 net, pkey, key_len, dev);
595fa86d322SPavel Emelyanov }
5960a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup);
597fa86d322SPavel Emelyanov 
598426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
599426b5303SEric W. Biederman 				    struct net *net, const void *pkey,
6001da177e4SLinus Torvalds 				    struct net_device *dev, int creat)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	struct pneigh_entry *n;
6031da177e4SLinus Torvalds 	int key_len = tbl->key_len;
604be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
607be01d655SYOSHIFUJI Hideaki 	n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
608be01d655SYOSHIFUJI Hideaki 			      net, pkey, key_len, dev);
609be01d655SYOSHIFUJI Hideaki 	read_unlock_bh(&tbl->lock);
6101da177e4SLinus Torvalds 
611be01d655SYOSHIFUJI Hideaki 	if (n || !creat)
6121da177e4SLinus Torvalds 		goto out;
6131da177e4SLinus Torvalds 
6144ae28944SPavel Emelyanov 	ASSERT_RTNL();
6154ae28944SPavel Emelyanov 
6161da177e4SLinus Torvalds 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
6171da177e4SLinus Torvalds 	if (!n)
6181da177e4SLinus Torvalds 		goto out;
6191da177e4SLinus Torvalds 
620e42ea986SEric Dumazet 	write_pnet(&n->net, hold_net(net));
6211da177e4SLinus Torvalds 	memcpy(n->key, pkey, key_len);
6221da177e4SLinus Torvalds 	n->dev = dev;
6231da177e4SLinus Torvalds 	if (dev)
6241da177e4SLinus Torvalds 		dev_hold(dev);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	if (tbl->pconstructor && tbl->pconstructor(n)) {
6271da177e4SLinus Torvalds 		if (dev)
6281da177e4SLinus Torvalds 			dev_put(dev);
629da12f735SDenis V. Lunev 		release_net(net);
6301da177e4SLinus Torvalds 		kfree(n);
6311da177e4SLinus Torvalds 		n = NULL;
6321da177e4SLinus Torvalds 		goto out;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6361da177e4SLinus Torvalds 	n->next = tbl->phash_buckets[hash_val];
6371da177e4SLinus Torvalds 	tbl->phash_buckets[hash_val] = n;
6381da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6391da177e4SLinus Torvalds out:
6401da177e4SLinus Torvalds 	return n;
6411da177e4SLinus Torvalds }
6420a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 
645426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
6461da177e4SLinus Torvalds 		  struct net_device *dev)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6491da177e4SLinus Torvalds 	int key_len = tbl->key_len;
650be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6531da177e4SLinus Torvalds 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
6541da177e4SLinus Torvalds 	     np = &n->next) {
655426b5303SEric W. Biederman 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
656878628fbSYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net)) {
6571da177e4SLinus Torvalds 			*np = n->next;
6581da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
6591da177e4SLinus Torvalds 			if (tbl->pdestructor)
6601da177e4SLinus Torvalds 				tbl->pdestructor(n);
6611da177e4SLinus Torvalds 			if (n->dev)
6621da177e4SLinus Torvalds 				dev_put(n->dev);
66357da52c1SYOSHIFUJI Hideaki 			release_net(pneigh_net(n));
6641da177e4SLinus Torvalds 			kfree(n);
6651da177e4SLinus Torvalds 			return 0;
6661da177e4SLinus Torvalds 		}
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6691da177e4SLinus Torvalds 	return -ENOENT;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
6731da177e4SLinus Torvalds {
6741da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6751da177e4SLinus Torvalds 	u32 h;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
6781da177e4SLinus Torvalds 		np = &tbl->phash_buckets[h];
6791da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
6801da177e4SLinus Torvalds 			if (!dev || n->dev == dev) {
6811da177e4SLinus Torvalds 				*np = n->next;
6821da177e4SLinus Torvalds 				if (tbl->pdestructor)
6831da177e4SLinus Torvalds 					tbl->pdestructor(n);
6841da177e4SLinus Torvalds 				if (n->dev)
6851da177e4SLinus Torvalds 					dev_put(n->dev);
68657da52c1SYOSHIFUJI Hideaki 				release_net(pneigh_net(n));
6871da177e4SLinus Torvalds 				kfree(n);
6881da177e4SLinus Torvalds 				continue;
6891da177e4SLinus Torvalds 			}
6901da177e4SLinus Torvalds 			np = &n->next;
6911da177e4SLinus Torvalds 		}
6921da177e4SLinus Torvalds 	}
6931da177e4SLinus Torvalds 	return -ENOENT;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
69606f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms);
69706f0511dSDenis V. Lunev 
69806f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms)
69906f0511dSDenis V. Lunev {
70006f0511dSDenis V. Lunev 	if (atomic_dec_and_test(&parms->refcnt))
70106f0511dSDenis V. Lunev 		neigh_parms_destroy(parms);
70206f0511dSDenis V. Lunev }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  *	neighbour must already be out of the table;
7061da177e4SLinus Torvalds  *
7071da177e4SLinus Torvalds  */
7081da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh)
7091da177e4SLinus Torvalds {
710da6a8fa0SDavid Miller 	struct net_device *dev = neigh->dev;
711da6a8fa0SDavid Miller 
7121da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	if (!neigh->dead) {
7151da177e4SLinus Torvalds 		printk(KERN_WARNING
7161da177e4SLinus Torvalds 		       "Destroying alive neighbour %p\n", neigh);
7171da177e4SLinus Torvalds 		dump_stack();
7181da177e4SLinus Torvalds 		return;
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (neigh_del_timer(neigh))
7221da177e4SLinus Torvalds 		printk(KERN_WARNING "Impossible event.\n");
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	skb_queue_purge(&neigh->arp_queue);
7258b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
7261da177e4SLinus Torvalds 
727447f2191SDavid S. Miller 	if (dev->netdev_ops->ndo_neigh_destroy)
728447f2191SDavid S. Miller 		dev->netdev_ops->ndo_neigh_destroy(neigh);
729447f2191SDavid S. Miller 
730da6a8fa0SDavid Miller 	dev_put(dev);
7311da177e4SLinus Torvalds 	neigh_parms_put(neigh->parms);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	atomic_dec(&neigh->tbl->entries);
7365b8b0060SDavid Miller 	kfree_rcu(neigh, rcu);
7371da177e4SLinus Torvalds }
7380a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /* Neighbour state is suspicious;
7411da177e4SLinus Torvalds    disable fast path.
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds    Called with write_locked neigh.
7441da177e4SLinus Torvalds  */
7451da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	neigh->output = neigh->ops->output;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /* Neighbour state is OK;
7531da177e4SLinus Torvalds    enable fast path.
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds    Called with write_locked neigh.
7561da177e4SLinus Torvalds  */
7571da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	neigh->output = neigh->ops->connected_output;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
764e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work)
7651da177e4SLinus Torvalds {
766e4c4e448SEric Dumazet 	struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
767767e97e1SEric Dumazet 	struct neighbour *n;
768767e97e1SEric Dumazet 	struct neighbour __rcu **np;
769e4c4e448SEric Dumazet 	unsigned int i;
770d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
7731da177e4SLinus Torvalds 
774e4c4e448SEric Dumazet 	write_lock_bh(&tbl->lock);
775d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
776d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/*
7791da177e4SLinus Torvalds 	 *	periodically recompute ReachableTime from random function
7801da177e4SLinus Torvalds 	 */
7811da177e4SLinus Torvalds 
782e4c4e448SEric Dumazet 	if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
7831da177e4SLinus Torvalds 		struct neigh_parms *p;
784e4c4e448SEric Dumazet 		tbl->last_rand = jiffies;
7851da177e4SLinus Torvalds 		for (p = &tbl->parms; p; p = p->next)
7861da177e4SLinus Torvalds 			p->reachable_time =
7871da177e4SLinus Torvalds 				neigh_rand_reach_time(p->base_reachable_time);
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
790cd089336SDavid S. Miller 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
791d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
7921da177e4SLinus Torvalds 
793767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
794767e97e1SEric Dumazet 				lockdep_is_held(&tbl->lock))) != NULL) {
7951da177e4SLinus Torvalds 			unsigned int state;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 			write_lock(&n->lock);
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 			state = n->nud_state;
8001da177e4SLinus Torvalds 			if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
8011da177e4SLinus Torvalds 				write_unlock(&n->lock);
8021da177e4SLinus Torvalds 				goto next_elt;
8031da177e4SLinus Torvalds 			}
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 			if (time_before(n->used, n->confirmed))
8061da177e4SLinus Torvalds 				n->used = n->confirmed;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
8091da177e4SLinus Torvalds 			    (state == NUD_FAILED ||
810e4c4e448SEric Dumazet 			     time_after(jiffies, n->used + n->parms->gc_staletime))) {
8111da177e4SLinus Torvalds 				*np = n->next;
8121da177e4SLinus Torvalds 				n->dead = 1;
8131da177e4SLinus Torvalds 				write_unlock(&n->lock);
8144f494554SThomas Graf 				neigh_cleanup_and_release(n);
8151da177e4SLinus Torvalds 				continue;
8161da177e4SLinus Torvalds 			}
8171da177e4SLinus Torvalds 			write_unlock(&n->lock);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds next_elt:
8201da177e4SLinus Torvalds 			np = &n->next;
8211da177e4SLinus Torvalds 		}
822e4c4e448SEric Dumazet 		/*
823e4c4e448SEric Dumazet 		 * It's fine to release lock here, even if hash table
824e4c4e448SEric Dumazet 		 * grows while we are preempted.
825e4c4e448SEric Dumazet 		 */
826e4c4e448SEric Dumazet 		write_unlock_bh(&tbl->lock);
827e4c4e448SEric Dumazet 		cond_resched();
828e4c4e448SEric Dumazet 		write_lock_bh(&tbl->lock);
82984338a6cSMichel Machado 		nht = rcu_dereference_protected(tbl->nht,
83084338a6cSMichel Machado 						lockdep_is_held(&tbl->lock));
831e4c4e448SEric Dumazet 	}
8321da177e4SLinus Torvalds 	/* Cycle through all hash buckets every base_reachable_time/2 ticks.
8331da177e4SLinus Torvalds 	 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
8341da177e4SLinus Torvalds 	 * base_reachable_time.
8351da177e4SLinus Torvalds 	 */
836e4c4e448SEric Dumazet 	schedule_delayed_work(&tbl->gc_work,
837e4c4e448SEric Dumazet 			      tbl->parms.base_reachable_time >> 1);
838e4c4e448SEric Dumazet 	write_unlock_bh(&tbl->lock);
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	struct neigh_parms *p = n->parms;
844a02cec21SEric Dumazet 	return (n->nud_state & NUD_PROBE) ?
8451da177e4SLinus Torvalds 		p->ucast_probes :
846a02cec21SEric Dumazet 		p->ucast_probes + p->app_probes + p->mcast_probes;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8495ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh)
8500a141509SEric Dumazet 	__releases(neigh->lock)
8510a141509SEric Dumazet 	__acquires(neigh->lock)
8525ef12d98STimo Teras {
8535ef12d98STimo Teras 	struct sk_buff *skb;
8545ef12d98STimo Teras 
8555ef12d98STimo Teras 	NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
8565ef12d98STimo Teras 	NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
8575ef12d98STimo Teras 	neigh->updated = jiffies;
8585ef12d98STimo Teras 
8595ef12d98STimo Teras 	/* It is very thin place. report_unreachable is very complicated
8605ef12d98STimo Teras 	   routine. Particularly, it can hit the same neighbour entry!
8615ef12d98STimo Teras 
8625ef12d98STimo Teras 	   So that, we try to be accurate and avoid dead loop. --ANK
8635ef12d98STimo Teras 	 */
8645ef12d98STimo Teras 	while (neigh->nud_state == NUD_FAILED &&
8655ef12d98STimo Teras 	       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
8665ef12d98STimo Teras 		write_unlock(&neigh->lock);
8675ef12d98STimo Teras 		neigh->ops->error_report(neigh, skb);
8685ef12d98STimo Teras 		write_lock(&neigh->lock);
8695ef12d98STimo Teras 	}
8705ef12d98STimo Teras 	skb_queue_purge(&neigh->arp_queue);
8718b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
8725ef12d98STimo Teras }
8735ef12d98STimo Teras 
874cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh)
875cd28ca0aSEric Dumazet 	__releases(neigh->lock)
876cd28ca0aSEric Dumazet {
877cd28ca0aSEric Dumazet 	struct sk_buff *skb = skb_peek(&neigh->arp_queue);
878cd28ca0aSEric Dumazet 	/* keep skb alive even if arp_queue overflows */
879cd28ca0aSEric Dumazet 	if (skb)
880cd28ca0aSEric Dumazet 		skb = skb_copy(skb, GFP_ATOMIC);
881cd28ca0aSEric Dumazet 	write_unlock(&neigh->lock);
882cd28ca0aSEric Dumazet 	neigh->ops->solicit(neigh, skb);
883cd28ca0aSEric Dumazet 	atomic_inc(&neigh->probes);
884cd28ca0aSEric Dumazet 	kfree_skb(skb);
885cd28ca0aSEric Dumazet }
886cd28ca0aSEric Dumazet 
8871da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	unsigned long now, next;
8921da177e4SLinus Torvalds 	struct neighbour *neigh = (struct neighbour *)arg;
89395c96174SEric Dumazet 	unsigned int state;
8941da177e4SLinus Torvalds 	int notify = 0;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	write_lock(&neigh->lock);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	state = neigh->nud_state;
8991da177e4SLinus Torvalds 	now = jiffies;
9001da177e4SLinus Torvalds 	next = now + HZ;
9011da177e4SLinus Torvalds 
902045f7b3bSDavid S. Miller 	if (!(state & NUD_IN_TIMER))
9031da177e4SLinus Torvalds 		goto out;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (state & NUD_REACHABLE) {
9061da177e4SLinus Torvalds 		if (time_before_eq(now,
9071da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->reachable_time)) {
9081da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
9091da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9101da177e4SLinus Torvalds 		} else if (time_before_eq(now,
9111da177e4SLinus Torvalds 					  neigh->used + neigh->parms->delay_probe_time)) {
9121da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
9131da177e4SLinus Torvalds 			neigh->nud_state = NUD_DELAY;
914955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9151da177e4SLinus Torvalds 			neigh_suspect(neigh);
9161da177e4SLinus Torvalds 			next = now + neigh->parms->delay_probe_time;
9171da177e4SLinus Torvalds 		} else {
9181da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
9191da177e4SLinus Torvalds 			neigh->nud_state = NUD_STALE;
920955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9211da177e4SLinus Torvalds 			neigh_suspect(neigh);
9228d71740cSTom Tucker 			notify = 1;
9231da177e4SLinus Torvalds 		}
9241da177e4SLinus Torvalds 	} else if (state & NUD_DELAY) {
9251da177e4SLinus Torvalds 		if (time_before_eq(now,
9261da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->delay_probe_time)) {
9271da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh);
9281da177e4SLinus Torvalds 			neigh->nud_state = NUD_REACHABLE;
929955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9301da177e4SLinus Torvalds 			neigh_connect(neigh);
9318d71740cSTom Tucker 			notify = 1;
9321da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9331da177e4SLinus Torvalds 		} else {
9341da177e4SLinus Torvalds 			NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
9351da177e4SLinus Torvalds 			neigh->nud_state = NUD_PROBE;
936955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9371da177e4SLinus Torvalds 			atomic_set(&neigh->probes, 0);
9381da177e4SLinus Torvalds 			next = now + neigh->parms->retrans_time;
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 	} else {
9411da177e4SLinus Torvalds 		/* NUD_PROBE|NUD_INCOMPLETE */
9421da177e4SLinus Torvalds 		next = now + neigh->parms->retrans_time;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
9461da177e4SLinus Torvalds 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
9471da177e4SLinus Torvalds 		neigh->nud_state = NUD_FAILED;
9481da177e4SLinus Torvalds 		notify = 1;
9495ef12d98STimo Teras 		neigh_invalidate(neigh);
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	if (neigh->nud_state & NUD_IN_TIMER) {
9531da177e4SLinus Torvalds 		if (time_before(next, jiffies + HZ/2))
9541da177e4SLinus Torvalds 			next = jiffies + HZ/2;
9556fb9974fSHerbert Xu 		if (!mod_timer(&neigh->timer, next))
9566fb9974fSHerbert Xu 			neigh_hold(neigh);
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
959cd28ca0aSEric Dumazet 		neigh_probe(neigh);
9609ff56607SDavid S. Miller 	} else {
9611da177e4SLinus Torvalds out:
9621da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
9639ff56607SDavid S. Miller 	}
9641da177e4SLinus Torvalds 
965d961db35SThomas Graf 	if (notify)
966d961db35SThomas Graf 		neigh_update_notify(neigh);
967d961db35SThomas Graf 
9681da177e4SLinus Torvalds 	neigh_release(neigh);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
9721da177e4SLinus Torvalds {
9731da177e4SLinus Torvalds 	int rc;
974cd28ca0aSEric Dumazet 	bool immediate_probe = false;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	rc = 0;
9791da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
9801da177e4SLinus Torvalds 		goto out_unlock_bh;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
9831da177e4SLinus Torvalds 		if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
984cd28ca0aSEric Dumazet 			unsigned long next, now = jiffies;
985cd28ca0aSEric Dumazet 
9861da177e4SLinus Torvalds 			atomic_set(&neigh->probes, neigh->parms->ucast_probes);
9871da177e4SLinus Torvalds 			neigh->nud_state     = NUD_INCOMPLETE;
988cd28ca0aSEric Dumazet 			neigh->updated = now;
989cd28ca0aSEric Dumazet 			next = now + max(neigh->parms->retrans_time, HZ/2);
990cd28ca0aSEric Dumazet 			neigh_add_timer(neigh, next);
991cd28ca0aSEric Dumazet 			immediate_probe = true;
9921da177e4SLinus Torvalds 		} else {
9931da177e4SLinus Torvalds 			neigh->nud_state = NUD_FAILED;
994955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9951da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 			kfree_skb(skb);
9981da177e4SLinus Torvalds 			return 1;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 	} else if (neigh->nud_state & NUD_STALE) {
10011da177e4SLinus Torvalds 		NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
10021da177e4SLinus Torvalds 		neigh->nud_state = NUD_DELAY;
1003955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
1004667347f1SDavid S. Miller 		neigh_add_timer(neigh,
1005667347f1SDavid S. Miller 				jiffies + neigh->parms->delay_probe_time);
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if (neigh->nud_state == NUD_INCOMPLETE) {
10091da177e4SLinus Torvalds 		if (skb) {
10108b5c171bSEric Dumazet 			while (neigh->arp_queue_len_bytes + skb->truesize >
10118b5c171bSEric Dumazet 			       neigh->parms->queue_len_bytes) {
10121da177e4SLinus Torvalds 				struct sk_buff *buff;
10138b5c171bSEric Dumazet 
1014f72051b0SDavid S. Miller 				buff = __skb_dequeue(&neigh->arp_queue);
10158b5c171bSEric Dumazet 				if (!buff)
10168b5c171bSEric Dumazet 					break;
10178b5c171bSEric Dumazet 				neigh->arp_queue_len_bytes -= buff->truesize;
10181da177e4SLinus Torvalds 				kfree_skb(buff);
10199a6d276eSNeil Horman 				NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
10201da177e4SLinus Torvalds 			}
1021a4731138SEric Dumazet 			skb_dst_force(skb);
10221da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->arp_queue, skb);
10238b5c171bSEric Dumazet 			neigh->arp_queue_len_bytes += skb->truesize;
10241da177e4SLinus Torvalds 		}
10251da177e4SLinus Torvalds 		rc = 1;
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds out_unlock_bh:
1028cd28ca0aSEric Dumazet 	if (immediate_probe)
1029cd28ca0aSEric Dumazet 		neigh_probe(neigh);
1030cd28ca0aSEric Dumazet 	else
1031cd28ca0aSEric Dumazet 		write_unlock(&neigh->lock);
1032cd28ca0aSEric Dumazet 	local_bh_enable();
10331da177e4SLinus Torvalds 	return rc;
10341da177e4SLinus Torvalds }
10350a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send);
10361da177e4SLinus Torvalds 
1037f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	struct hh_cache *hh;
10403b04dddeSStephen Hemminger 	void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
104191a72a70SDoug Kehn 		= NULL;
104291a72a70SDoug Kehn 
104391a72a70SDoug Kehn 	if (neigh->dev->header_ops)
104491a72a70SDoug Kehn 		update = neigh->dev->header_ops->cache_update;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	if (update) {
1047f6b72b62SDavid S. Miller 		hh = &neigh->hh;
1048f6b72b62SDavid S. Miller 		if (hh->hh_len) {
10493644f0ceSStephen Hemminger 			write_seqlock_bh(&hh->hh_lock);
10501da177e4SLinus Torvalds 			update(hh, neigh->dev, neigh->ha);
10513644f0ceSStephen Hemminger 			write_sequnlock_bh(&hh->hh_lock);
10521da177e4SLinus Torvalds 		}
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /* Generic update routine.
10591da177e4SLinus Torvalds    -- lladdr is new lladdr or NULL, if it is not supplied.
10601da177e4SLinus Torvalds    -- new    is new state.
10611da177e4SLinus Torvalds    -- flags
10621da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
10631da177e4SLinus Torvalds 				if it is different.
10641da177e4SLinus Torvalds 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
10651da177e4SLinus Torvalds 				lladdr instead of overriding it
10661da177e4SLinus Torvalds 				if it is different.
10671da177e4SLinus Torvalds 				It also allows to retain current state
10681da177e4SLinus Torvalds 				if lladdr is unchanged.
10691da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
10721da177e4SLinus Torvalds 				NTF_ROUTER flag.
10731da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
10741da177e4SLinus Torvalds 				a router.
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds    Caller MUST hold reference count on the entry.
10771da177e4SLinus Torvalds  */
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
10801da177e4SLinus Torvalds 		 u32 flags)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	u8 old;
10831da177e4SLinus Torvalds 	int err;
10841da177e4SLinus Torvalds 	int notify = 0;
10851da177e4SLinus Torvalds 	struct net_device *dev;
10861da177e4SLinus Torvalds 	int update_isrouter = 0;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	dev    = neigh->dev;
10911da177e4SLinus Torvalds 	old    = neigh->nud_state;
10921da177e4SLinus Torvalds 	err    = -EPERM;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
10951da177e4SLinus Torvalds 	    (old & (NUD_NOARP | NUD_PERMANENT)))
10961da177e4SLinus Torvalds 		goto out;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	if (!(new & NUD_VALID)) {
10991da177e4SLinus Torvalds 		neigh_del_timer(neigh);
11001da177e4SLinus Torvalds 		if (old & NUD_CONNECTED)
11011da177e4SLinus Torvalds 			neigh_suspect(neigh);
11021da177e4SLinus Torvalds 		neigh->nud_state = new;
11031da177e4SLinus Torvalds 		err = 0;
11041da177e4SLinus Torvalds 		notify = old & NUD_VALID;
11055ef12d98STimo Teras 		if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
11065ef12d98STimo Teras 		    (new & NUD_FAILED)) {
11075ef12d98STimo Teras 			neigh_invalidate(neigh);
11085ef12d98STimo Teras 			notify = 1;
11095ef12d98STimo Teras 		}
11101da177e4SLinus Torvalds 		goto out;
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	/* Compare new lladdr with cached one */
11141da177e4SLinus Torvalds 	if (!dev->addr_len) {
11151da177e4SLinus Torvalds 		/* First case: device needs no address. */
11161da177e4SLinus Torvalds 		lladdr = neigh->ha;
11171da177e4SLinus Torvalds 	} else if (lladdr) {
11181da177e4SLinus Torvalds 		/* The second case: if something is already cached
11191da177e4SLinus Torvalds 		   and a new address is proposed:
11201da177e4SLinus Torvalds 		   - compare new & old
11211da177e4SLinus Torvalds 		   - if they are different, check override flag
11221da177e4SLinus Torvalds 		 */
11231da177e4SLinus Torvalds 		if ((old & NUD_VALID) &&
11241da177e4SLinus Torvalds 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
11251da177e4SLinus Torvalds 			lladdr = neigh->ha;
11261da177e4SLinus Torvalds 	} else {
11271da177e4SLinus Torvalds 		/* No address is supplied; if we know something,
11281da177e4SLinus Torvalds 		   use it, otherwise discard the request.
11291da177e4SLinus Torvalds 		 */
11301da177e4SLinus Torvalds 		err = -EINVAL;
11311da177e4SLinus Torvalds 		if (!(old & NUD_VALID))
11321da177e4SLinus Torvalds 			goto out;
11331da177e4SLinus Torvalds 		lladdr = neigh->ha;
11341da177e4SLinus Torvalds 	}
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
11371da177e4SLinus Torvalds 		neigh->confirmed = jiffies;
11381da177e4SLinus Torvalds 	neigh->updated = jiffies;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	/* If entry was valid and address is not changed,
11411da177e4SLinus Torvalds 	   do not change entry state, if new one is STALE.
11421da177e4SLinus Torvalds 	 */
11431da177e4SLinus Torvalds 	err = 0;
11441da177e4SLinus Torvalds 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
11451da177e4SLinus Torvalds 	if (old & NUD_VALID) {
11461da177e4SLinus Torvalds 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
11471da177e4SLinus Torvalds 			update_isrouter = 0;
11481da177e4SLinus Torvalds 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
11491da177e4SLinus Torvalds 			    (old & NUD_CONNECTED)) {
11501da177e4SLinus Torvalds 				lladdr = neigh->ha;
11511da177e4SLinus Torvalds 				new = NUD_STALE;
11521da177e4SLinus Torvalds 			} else
11531da177e4SLinus Torvalds 				goto out;
11541da177e4SLinus Torvalds 		} else {
11551da177e4SLinus Torvalds 			if (lladdr == neigh->ha && new == NUD_STALE &&
11561da177e4SLinus Torvalds 			    ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
11571da177e4SLinus Torvalds 			     (old & NUD_CONNECTED))
11581da177e4SLinus Torvalds 			    )
11591da177e4SLinus Torvalds 				new = old;
11601da177e4SLinus Torvalds 		}
11611da177e4SLinus Torvalds 	}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	if (new != old) {
11641da177e4SLinus Torvalds 		neigh_del_timer(neigh);
1165a43d8994SPavel Emelyanov 		if (new & NUD_IN_TIMER)
1166667347f1SDavid S. Miller 			neigh_add_timer(neigh, (jiffies +
11671da177e4SLinus Torvalds 						((new & NUD_REACHABLE) ?
1168667347f1SDavid S. Miller 						 neigh->parms->reachable_time :
1169667347f1SDavid S. Miller 						 0)));
11701da177e4SLinus Torvalds 		neigh->nud_state = new;
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	if (lladdr != neigh->ha) {
11740ed8ddf4SEric Dumazet 		write_seqlock(&neigh->ha_lock);
11751da177e4SLinus Torvalds 		memcpy(&neigh->ha, lladdr, dev->addr_len);
11760ed8ddf4SEric Dumazet 		write_sequnlock(&neigh->ha_lock);
11771da177e4SLinus Torvalds 		neigh_update_hhs(neigh);
11781da177e4SLinus Torvalds 		if (!(new & NUD_CONNECTED))
11791da177e4SLinus Torvalds 			neigh->confirmed = jiffies -
11801da177e4SLinus Torvalds 				      (neigh->parms->base_reachable_time << 1);
11811da177e4SLinus Torvalds 		notify = 1;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 	if (new == old)
11841da177e4SLinus Torvalds 		goto out;
11851da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
11861da177e4SLinus Torvalds 		neigh_connect(neigh);
11871da177e4SLinus Torvalds 	else
11881da177e4SLinus Torvalds 		neigh_suspect(neigh);
11891da177e4SLinus Torvalds 	if (!(old & NUD_VALID)) {
11901da177e4SLinus Torvalds 		struct sk_buff *skb;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 		/* Again: avoid dead loop if something went wrong */
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		while (neigh->nud_state & NUD_VALID &&
11951da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
119669cce1d1SDavid S. Miller 			struct dst_entry *dst = skb_dst(skb);
119769cce1d1SDavid S. Miller 			struct neighbour *n2, *n1 = neigh;
11981da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
1199e049f288Sroy.qing.li@gmail.com 
1200e049f288Sroy.qing.li@gmail.com 			rcu_read_lock();
12011da177e4SLinus Torvalds 			/* On shaper/eql skb->dst->neighbour != neigh :( */
120227217455SDavid Miller 			if (dst && (n2 = dst_get_neighbour_noref(dst)) != NULL)
120369cce1d1SDavid S. Miller 				n1 = n2;
12048f40b161SDavid S. Miller 			n1->output(n1, skb);
1205e049f288Sroy.qing.li@gmail.com 			rcu_read_unlock();
1206e049f288Sroy.qing.li@gmail.com 
12071da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
12081da177e4SLinus Torvalds 		}
12091da177e4SLinus Torvalds 		skb_queue_purge(&neigh->arp_queue);
12108b5c171bSEric Dumazet 		neigh->arp_queue_len_bytes = 0;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds out:
12131da177e4SLinus Torvalds 	if (update_isrouter) {
12141da177e4SLinus Torvalds 		neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
12151da177e4SLinus Torvalds 			(neigh->flags | NTF_ROUTER) :
12161da177e4SLinus Torvalds 			(neigh->flags & ~NTF_ROUTER);
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
12198d71740cSTom Tucker 
12208d71740cSTom Tucker 	if (notify)
1221d961db35SThomas Graf 		neigh_update_notify(neigh);
1222d961db35SThomas Graf 
12231da177e4SLinus Torvalds 	return err;
12241da177e4SLinus Torvalds }
12250a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl,
12281da177e4SLinus Torvalds 				 u8 *lladdr, void *saddr,
12291da177e4SLinus Torvalds 				 struct net_device *dev)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
12321da177e4SLinus Torvalds 						 lladdr || !dev->addr_len);
12331da177e4SLinus Torvalds 	if (neigh)
12341da177e4SLinus Torvalds 		neigh_update(neigh, lladdr, NUD_STALE,
12351da177e4SLinus Torvalds 			     NEIGH_UPDATE_F_OVERRIDE);
12361da177e4SLinus Torvalds 	return neigh;
12371da177e4SLinus Torvalds }
12380a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns);
12391da177e4SLinus Torvalds 
124034d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */
1241f6b72b62SDavid S. Miller static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
1244f6b72b62SDavid S. Miller 	__be16 prot = dst->ops->protocol;
1245f6b72b62SDavid S. Miller 	struct hh_cache	*hh = &n->hh;
12460ed8ddf4SEric Dumazet 
12470ed8ddf4SEric Dumazet 	write_lock_bh(&n->lock);
124834d101ddSEric Dumazet 
1249f6b72b62SDavid S. Miller 	/* Only one thread can come in here and initialize the
1250f6b72b62SDavid S. Miller 	 * hh_cache entry.
1251f6b72b62SDavid S. Miller 	 */
1252b23b5455SDavid S. Miller 	if (!hh->hh_len)
1253b23b5455SDavid S. Miller 		dev->header_ops->cache(n, hh, prot);
1254f6b72b62SDavid S. Miller 
12550ed8ddf4SEric Dumazet 	write_unlock_bh(&n->lock);
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit
1259767e97e1SEric Dumazet  * worked, f.e. if you want to override normal output path (eql, shaper),
1260767e97e1SEric Dumazet  * but resolution is not made yet.
12611da177e4SLinus Torvalds  */
12621da177e4SLinus Torvalds 
12638f40b161SDavid S. Miller int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb)
12641da177e4SLinus Torvalds {
12651da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
12661da177e4SLinus Torvalds 
1267bbe735e4SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_offset(skb));
12681da177e4SLinus Torvalds 
12690c4e8581SStephen Hemminger 	if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
12701da177e4SLinus Torvalds 			    skb->len) < 0 &&
12713b04dddeSStephen Hemminger 	    dev->header_ops->rebuild(skb))
12721da177e4SLinus Torvalds 		return 0;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	return dev_queue_xmit(skb);
12751da177e4SLinus Torvalds }
12760a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output);
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds /* Slow and careful. */
12791da177e4SLinus Torvalds 
12808f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
12811da177e4SLinus Torvalds {
1282adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
12831da177e4SLinus Torvalds 	int rc = 0;
12841da177e4SLinus Torvalds 
12858f40b161SDavid S. Miller 	if (!dst)
12861da177e4SLinus Torvalds 		goto discard;
12871da177e4SLinus Torvalds 
1288bbe735e4SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_offset(skb));
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	if (!neigh_event_send(neigh, skb)) {
12911da177e4SLinus Torvalds 		int err;
12921da177e4SLinus Torvalds 		struct net_device *dev = neigh->dev;
12930ed8ddf4SEric Dumazet 		unsigned int seq;
129434d101ddSEric Dumazet 
1295f6b72b62SDavid S. Miller 		if (dev->header_ops->cache && !neigh->hh.hh_len)
1296f6b72b62SDavid S. Miller 			neigh_hh_init(neigh, dst);
129734d101ddSEric Dumazet 
12980ed8ddf4SEric Dumazet 		do {
12990ed8ddf4SEric Dumazet 			seq = read_seqbegin(&neigh->ha_lock);
13000c4e8581SStephen Hemminger 			err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13011da177e4SLinus Torvalds 					      neigh->ha, NULL, skb->len);
13020ed8ddf4SEric Dumazet 		} while (read_seqretry(&neigh->ha_lock, seq));
130334d101ddSEric Dumazet 
13041da177e4SLinus Torvalds 		if (err >= 0)
1305542d4d68SDavid S. Miller 			rc = dev_queue_xmit(skb);
13061da177e4SLinus Torvalds 		else
13071da177e4SLinus Torvalds 			goto out_kfree_skb;
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds out:
13101da177e4SLinus Torvalds 	return rc;
13111da177e4SLinus Torvalds discard:
13121da177e4SLinus Torvalds 	NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
13138f40b161SDavid S. Miller 		      dst, neigh);
13141da177e4SLinus Torvalds out_kfree_skb:
13151da177e4SLinus Torvalds 	rc = -EINVAL;
13161da177e4SLinus Torvalds 	kfree_skb(skb);
13171da177e4SLinus Torvalds 	goto out;
13181da177e4SLinus Torvalds }
13190a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds /* As fast as possible without hh cache */
13221da177e4SLinus Torvalds 
13238f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
13241da177e4SLinus Torvalds {
13251da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
13260ed8ddf4SEric Dumazet 	unsigned int seq;
13278f40b161SDavid S. Miller 	int err;
13281da177e4SLinus Torvalds 
1329bbe735e4SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_offset(skb));
13301da177e4SLinus Torvalds 
13310ed8ddf4SEric Dumazet 	do {
13320ed8ddf4SEric Dumazet 		seq = read_seqbegin(&neigh->ha_lock);
13330c4e8581SStephen Hemminger 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13341da177e4SLinus Torvalds 				      neigh->ha, NULL, skb->len);
13350ed8ddf4SEric Dumazet 	} while (read_seqretry(&neigh->ha_lock, seq));
13360ed8ddf4SEric Dumazet 
13371da177e4SLinus Torvalds 	if (err >= 0)
1338542d4d68SDavid S. Miller 		err = dev_queue_xmit(skb);
13391da177e4SLinus Torvalds 	else {
13401da177e4SLinus Torvalds 		err = -EINVAL;
13411da177e4SLinus Torvalds 		kfree_skb(skb);
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 	return err;
13441da177e4SLinus Torvalds }
13450a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output);
13461da177e4SLinus Torvalds 
13478f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
13488f40b161SDavid S. Miller {
13498f40b161SDavid S. Miller 	return dev_queue_xmit(skb);
13508f40b161SDavid S. Miller }
13518f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output);
13528f40b161SDavid S. Miller 
13531da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg)
13541da177e4SLinus Torvalds {
13551da177e4SLinus Torvalds 	struct neigh_table *tbl = (struct neigh_table *)arg;
13561da177e4SLinus Torvalds 	long sched_next = 0;
13571da177e4SLinus Torvalds 	unsigned long now = jiffies;
1358f72051b0SDavid S. Miller 	struct sk_buff *skb, *n;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
13611da177e4SLinus Torvalds 
1362f72051b0SDavid S. Miller 	skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1363f72051b0SDavid S. Miller 		long tdif = NEIGH_CB(skb)->sched_next - now;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 		if (tdif <= 0) {
1366f72051b0SDavid S. Miller 			struct net_device *dev = skb->dev;
136720e6074eSEric Dumazet 
1368f72051b0SDavid S. Miller 			__skb_unlink(skb, &tbl->proxy_queue);
136920e6074eSEric Dumazet 			if (tbl->proxy_redo && netif_running(dev)) {
137020e6074eSEric Dumazet 				rcu_read_lock();
1371f72051b0SDavid S. Miller 				tbl->proxy_redo(skb);
137220e6074eSEric Dumazet 				rcu_read_unlock();
137320e6074eSEric Dumazet 			} else {
1374f72051b0SDavid S. Miller 				kfree_skb(skb);
137520e6074eSEric Dumazet 			}
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 			dev_put(dev);
13781da177e4SLinus Torvalds 		} else if (!sched_next || tdif < sched_next)
13791da177e4SLinus Torvalds 			sched_next = tdif;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 	del_timer(&tbl->proxy_timer);
13821da177e4SLinus Torvalds 	if (sched_next)
13831da177e4SLinus Torvalds 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
13841da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
13881da177e4SLinus Torvalds 		    struct sk_buff *skb)
13891da177e4SLinus Torvalds {
13901da177e4SLinus Torvalds 	unsigned long now = jiffies;
13911da177e4SLinus Torvalds 	unsigned long sched_next = now + (net_random() % p->proxy_delay);
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	if (tbl->proxy_queue.qlen > p->proxy_qlen) {
13941da177e4SLinus Torvalds 		kfree_skb(skb);
13951da177e4SLinus Torvalds 		return;
13961da177e4SLinus Torvalds 	}
1397a61bbcf2SPatrick McHardy 
1398a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->sched_next = sched_next;
1399a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
14021da177e4SLinus Torvalds 	if (del_timer(&tbl->proxy_timer)) {
14031da177e4SLinus Torvalds 		if (time_before(tbl->proxy_timer.expires, sched_next))
14041da177e4SLinus Torvalds 			sched_next = tbl->proxy_timer.expires;
14051da177e4SLinus Torvalds 	}
1406adf30907SEric Dumazet 	skb_dst_drop(skb);
14071da177e4SLinus Torvalds 	dev_hold(skb->dev);
14081da177e4SLinus Torvalds 	__skb_queue_tail(&tbl->proxy_queue, skb);
14091da177e4SLinus Torvalds 	mod_timer(&tbl->proxy_timer, sched_next);
14101da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
14111da177e4SLinus Torvalds }
14120a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue);
14131da177e4SLinus Torvalds 
141497fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1415426b5303SEric W. Biederman 						      struct net *net, int ifindex)
1416426b5303SEric W. Biederman {
1417426b5303SEric W. Biederman 	struct neigh_parms *p;
1418426b5303SEric W. Biederman 
1419426b5303SEric W. Biederman 	for (p = &tbl->parms; p; p = p->next) {
1420878628fbSYOSHIFUJI Hideaki 		if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1421426b5303SEric W. Biederman 		    (!p->dev && !ifindex))
1422426b5303SEric W. Biederman 			return p;
1423426b5303SEric W. Biederman 	}
1424426b5303SEric W. Biederman 
1425426b5303SEric W. Biederman 	return NULL;
1426426b5303SEric W. Biederman }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
14291da177e4SLinus Torvalds 				      struct neigh_table *tbl)
14301da177e4SLinus Torvalds {
1431426b5303SEric W. Biederman 	struct neigh_parms *p, *ref;
143200829823SStephen Hemminger 	struct net *net = dev_net(dev);
143300829823SStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14341da177e4SLinus Torvalds 
143597fd5bc7STobias Klauser 	ref = lookup_neigh_parms(tbl, net, 0);
1436426b5303SEric W. Biederman 	if (!ref)
1437426b5303SEric W. Biederman 		return NULL;
1438426b5303SEric W. Biederman 
1439426b5303SEric W. Biederman 	p = kmemdup(ref, sizeof(*p), GFP_KERNEL);
14401da177e4SLinus Torvalds 	if (p) {
14411da177e4SLinus Torvalds 		p->tbl		  = tbl;
14421da177e4SLinus Torvalds 		atomic_set(&p->refcnt, 1);
14431da177e4SLinus Torvalds 		p->reachable_time =
14441da177e4SLinus Torvalds 				neigh_rand_reach_time(p->base_reachable_time);
1445486b51d3SDenis V. Lunev 
144600829823SStephen Hemminger 		if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
14471da177e4SLinus Torvalds 			kfree(p);
14481da177e4SLinus Torvalds 			return NULL;
14491da177e4SLinus Torvalds 		}
1450c7fb64dbSThomas Graf 
1451c7fb64dbSThomas Graf 		dev_hold(dev);
1452c7fb64dbSThomas Graf 		p->dev = dev;
1453e42ea986SEric Dumazet 		write_pnet(&p->net, hold_net(net));
14541da177e4SLinus Torvalds 		p->sysctl_table = NULL;
14551da177e4SLinus Torvalds 		write_lock_bh(&tbl->lock);
14561da177e4SLinus Torvalds 		p->next		= tbl->parms.next;
14571da177e4SLinus Torvalds 		tbl->parms.next = p;
14581da177e4SLinus Torvalds 		write_unlock_bh(&tbl->lock);
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 	return p;
14611da177e4SLinus Torvalds }
14620a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc);
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head)
14651da177e4SLinus Torvalds {
14661da177e4SLinus Torvalds 	struct neigh_parms *parms =
14671da177e4SLinus Torvalds 		container_of(head, struct neigh_parms, rcu_head);
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	neigh_parms_put(parms);
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	struct neigh_parms **p;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	if (!parms || parms == &tbl->parms)
14771da177e4SLinus Torvalds 		return;
14781da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
14791da177e4SLinus Torvalds 	for (p = &tbl->parms.next; *p; p = &(*p)->next) {
14801da177e4SLinus Torvalds 		if (*p == parms) {
14811da177e4SLinus Torvalds 			*p = parms->next;
14821da177e4SLinus Torvalds 			parms->dead = 1;
14831da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
1484cecbb639SDavid S. Miller 			if (parms->dev)
1485cecbb639SDavid S. Miller 				dev_put(parms->dev);
14861da177e4SLinus Torvalds 			call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
14871da177e4SLinus Torvalds 			return;
14881da177e4SLinus Torvalds 		}
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
14911da177e4SLinus Torvalds 	NEIGH_PRINTK1("neigh_parms_release: not found\n");
14921da177e4SLinus Torvalds }
14930a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release);
14941da177e4SLinus Torvalds 
149506f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms)
14961da177e4SLinus Torvalds {
149757da52c1SYOSHIFUJI Hideaki 	release_net(neigh_parms_net(parms));
14981da177e4SLinus Torvalds 	kfree(parms);
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
1501c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class;
1502c2ecba71SPavel Emelianov 
1503dcd2ba92SHiroaki SHIMODA static void neigh_table_init_no_netlink(struct neigh_table *tbl)
15041da177e4SLinus Torvalds {
15051da177e4SLinus Torvalds 	unsigned long now = jiffies;
15061da177e4SLinus Torvalds 	unsigned long phsize;
15071da177e4SLinus Torvalds 
1508e42ea986SEric Dumazet 	write_pnet(&tbl->parms.net, &init_net);
15091da177e4SLinus Torvalds 	atomic_set(&tbl->parms.refcnt, 1);
15101da177e4SLinus Torvalds 	tbl->parms.reachable_time =
15111da177e4SLinus Torvalds 			  neigh_rand_reach_time(tbl->parms.base_reachable_time);
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	tbl->stats = alloc_percpu(struct neigh_statistics);
15141da177e4SLinus Torvalds 	if (!tbl->stats)
15151da177e4SLinus Torvalds 		panic("cannot create neighbour cache statistics");
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
15189b739ba5SAlexey Dobriyan 	if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
15199b739ba5SAlexey Dobriyan 			      &neigh_stat_seq_fops, tbl))
15201da177e4SLinus Torvalds 		panic("cannot create neighbour proc dir entry");
15211da177e4SLinus Torvalds #endif
15221da177e4SLinus Torvalds 
1523cd089336SDavid S. Miller 	RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
152677d04bd9SAndrew Morton 	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
15271da177e4SLinus Torvalds 
1528d6bf7817SEric Dumazet 	if (!tbl->nht || !tbl->phash_buckets)
15291da177e4SLinus Torvalds 		panic("cannot allocate neighbour cache hashes");
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	rwlock_init(&tbl->lock);
1532e4c4e448SEric Dumazet 	INIT_DELAYED_WORK_DEFERRABLE(&tbl->gc_work, neigh_periodic_work);
1533e4c4e448SEric Dumazet 	schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time);
1534b24b8a24SPavel Emelyanov 	setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
1535c2ecba71SPavel Emelianov 	skb_queue_head_init_class(&tbl->proxy_queue,
1536c2ecba71SPavel Emelianov 			&neigh_table_proxy_queue_class);
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	tbl->last_flush = now;
15391da177e4SLinus Torvalds 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1540bd89efc5SSimon Kelley }
1541bd89efc5SSimon Kelley 
1542bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl)
1543bd89efc5SSimon Kelley {
1544bd89efc5SSimon Kelley 	struct neigh_table *tmp;
1545bd89efc5SSimon Kelley 
1546bd89efc5SSimon Kelley 	neigh_table_init_no_netlink(tbl);
15471da177e4SLinus Torvalds 	write_lock(&neigh_tbl_lock);
1548bd89efc5SSimon Kelley 	for (tmp = neigh_tables; tmp; tmp = tmp->next) {
1549bd89efc5SSimon Kelley 		if (tmp->family == tbl->family)
1550bd89efc5SSimon Kelley 			break;
1551bd89efc5SSimon Kelley 	}
15521da177e4SLinus Torvalds 	tbl->next	= neigh_tables;
15531da177e4SLinus Torvalds 	neigh_tables	= tbl;
15541da177e4SLinus Torvalds 	write_unlock(&neigh_tbl_lock);
1555bd89efc5SSimon Kelley 
1556bd89efc5SSimon Kelley 	if (unlikely(tmp)) {
1557bd89efc5SSimon Kelley 		printk(KERN_ERR "NEIGH: Registering multiple tables for "
1558bd89efc5SSimon Kelley 		       "family %d\n", tbl->family);
1559bd89efc5SSimon Kelley 		dump_stack();
1560bd89efc5SSimon Kelley 	}
15611da177e4SLinus Torvalds }
15620a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init);
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	struct neigh_table **tp;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	/* It is not clean... Fix it to unload IPv6 module safely */
1569a5c30b34STejun Heo 	cancel_delayed_work_sync(&tbl->gc_work);
15701da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
15711da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
15721da177e4SLinus Torvalds 	neigh_ifdown(tbl, NULL);
15731da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries))
15741da177e4SLinus Torvalds 		printk(KERN_CRIT "neighbour leakage\n");
15751da177e4SLinus Torvalds 	write_lock(&neigh_tbl_lock);
15761da177e4SLinus Torvalds 	for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
15771da177e4SLinus Torvalds 		if (*tp == tbl) {
15781da177e4SLinus Torvalds 			*tp = tbl->next;
15791da177e4SLinus Torvalds 			break;
15801da177e4SLinus Torvalds 		}
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 	write_unlock(&neigh_tbl_lock);
15831da177e4SLinus Torvalds 
15846193d2beSEric Dumazet 	call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
15856193d2beSEric Dumazet 		 neigh_hash_free_rcu);
1586d6bf7817SEric Dumazet 	tbl->nht = NULL;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	kfree(tbl->phash_buckets);
15891da177e4SLinus Torvalds 	tbl->phash_buckets = NULL;
15901da177e4SLinus Torvalds 
15913f192b5cSAlexey Dobriyan 	remove_proc_entry(tbl->id, init_net.proc_net_stat);
15923f192b5cSAlexey Dobriyan 
15933fcde74bSKirill Korotaev 	free_percpu(tbl->stats);
15943fcde74bSKirill Korotaev 	tbl->stats = NULL;
15953fcde74bSKirill Korotaev 
15961da177e4SLinus Torvalds 	return 0;
15971da177e4SLinus Torvalds }
15980a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear);
15991da177e4SLinus Torvalds 
1600c8822a4eSThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
16011da177e4SLinus Torvalds {
16023b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1603a14a49d2SThomas Graf 	struct ndmsg *ndm;
1604a14a49d2SThomas Graf 	struct nlattr *dst_attr;
16051da177e4SLinus Torvalds 	struct neigh_table *tbl;
16061da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1607a14a49d2SThomas Graf 	int err = -EINVAL;
16081da177e4SLinus Torvalds 
1609110b2499SEric Dumazet 	ASSERT_RTNL();
1610a14a49d2SThomas Graf 	if (nlmsg_len(nlh) < sizeof(*ndm))
16111da177e4SLinus Torvalds 		goto out;
16121da177e4SLinus Torvalds 
1613a14a49d2SThomas Graf 	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1614a14a49d2SThomas Graf 	if (dst_attr == NULL)
1615a14a49d2SThomas Graf 		goto out;
1616a14a49d2SThomas Graf 
1617a14a49d2SThomas Graf 	ndm = nlmsg_data(nlh);
1618a14a49d2SThomas Graf 	if (ndm->ndm_ifindex) {
1619110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1620a14a49d2SThomas Graf 		if (dev == NULL) {
1621a14a49d2SThomas Graf 			err = -ENODEV;
1622a14a49d2SThomas Graf 			goto out;
1623a14a49d2SThomas Graf 		}
1624a14a49d2SThomas Graf 	}
1625a14a49d2SThomas Graf 
16261da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
16271da177e4SLinus Torvalds 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1628a14a49d2SThomas Graf 		struct neighbour *neigh;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 		if (tbl->family != ndm->ndm_family)
16311da177e4SLinus Torvalds 			continue;
16321da177e4SLinus Torvalds 		read_unlock(&neigh_tbl_lock);
16331da177e4SLinus Torvalds 
1634a14a49d2SThomas Graf 		if (nla_len(dst_attr) < tbl->key_len)
1635110b2499SEric Dumazet 			goto out;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 		if (ndm->ndm_flags & NTF_PROXY) {
1638426b5303SEric W. Biederman 			err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1639110b2499SEric Dumazet 			goto out;
16401da177e4SLinus Torvalds 		}
16411da177e4SLinus Torvalds 
1642a14a49d2SThomas Graf 		if (dev == NULL)
1643110b2499SEric Dumazet 			goto out;
16441da177e4SLinus Torvalds 
1645a14a49d2SThomas Graf 		neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1646a14a49d2SThomas Graf 		if (neigh == NULL) {
1647a14a49d2SThomas Graf 			err = -ENOENT;
1648110b2499SEric Dumazet 			goto out;
1649a14a49d2SThomas Graf 		}
1650a14a49d2SThomas Graf 
1651a14a49d2SThomas Graf 		err = neigh_update(neigh, NULL, NUD_FAILED,
16521da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_OVERRIDE |
16531da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_ADMIN);
1654a14a49d2SThomas Graf 		neigh_release(neigh);
1655110b2499SEric Dumazet 		goto out;
16561da177e4SLinus Torvalds 	}
16571da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
1658a14a49d2SThomas Graf 	err = -EAFNOSUPPORT;
1659a14a49d2SThomas Graf 
16601da177e4SLinus Torvalds out:
16611da177e4SLinus Torvalds 	return err;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
1664c8822a4eSThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
16651da177e4SLinus Torvalds {
16663b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
16675208debdSThomas Graf 	struct ndmsg *ndm;
16685208debdSThomas Graf 	struct nlattr *tb[NDA_MAX+1];
16691da177e4SLinus Torvalds 	struct neigh_table *tbl;
16701da177e4SLinus Torvalds 	struct net_device *dev = NULL;
16715208debdSThomas Graf 	int err;
16721da177e4SLinus Torvalds 
1673110b2499SEric Dumazet 	ASSERT_RTNL();
16745208debdSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
16755208debdSThomas Graf 	if (err < 0)
16761da177e4SLinus Torvalds 		goto out;
16771da177e4SLinus Torvalds 
16785208debdSThomas Graf 	err = -EINVAL;
16795208debdSThomas Graf 	if (tb[NDA_DST] == NULL)
16805208debdSThomas Graf 		goto out;
16815208debdSThomas Graf 
16825208debdSThomas Graf 	ndm = nlmsg_data(nlh);
16835208debdSThomas Graf 	if (ndm->ndm_ifindex) {
1684110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
16855208debdSThomas Graf 		if (dev == NULL) {
16865208debdSThomas Graf 			err = -ENODEV;
16875208debdSThomas Graf 			goto out;
16885208debdSThomas Graf 		}
16895208debdSThomas Graf 
16905208debdSThomas Graf 		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1691110b2499SEric Dumazet 			goto out;
16925208debdSThomas Graf 	}
16935208debdSThomas Graf 
16941da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
16951da177e4SLinus Torvalds 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
16965208debdSThomas Graf 		int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
16975208debdSThomas Graf 		struct neighbour *neigh;
16985208debdSThomas Graf 		void *dst, *lladdr;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 		if (tbl->family != ndm->ndm_family)
17011da177e4SLinus Torvalds 			continue;
17021da177e4SLinus Torvalds 		read_unlock(&neigh_tbl_lock);
17031da177e4SLinus Torvalds 
17045208debdSThomas Graf 		if (nla_len(tb[NDA_DST]) < tbl->key_len)
1705110b2499SEric Dumazet 			goto out;
17065208debdSThomas Graf 		dst = nla_data(tb[NDA_DST]);
17075208debdSThomas Graf 		lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 		if (ndm->ndm_flags & NTF_PROXY) {
171062dd9318SVille Nuorvala 			struct pneigh_entry *pn;
171162dd9318SVille Nuorvala 
17125208debdSThomas Graf 			err = -ENOBUFS;
1713426b5303SEric W. Biederman 			pn = pneigh_lookup(tbl, net, dst, dev, 1);
171462dd9318SVille Nuorvala 			if (pn) {
171562dd9318SVille Nuorvala 				pn->flags = ndm->ndm_flags;
171662dd9318SVille Nuorvala 				err = 0;
171762dd9318SVille Nuorvala 			}
1718110b2499SEric Dumazet 			goto out;
17191da177e4SLinus Torvalds 		}
17201da177e4SLinus Torvalds 
17215208debdSThomas Graf 		if (dev == NULL)
1722110b2499SEric Dumazet 			goto out;
17231da177e4SLinus Torvalds 
17245208debdSThomas Graf 		neigh = neigh_lookup(tbl, dst, dev);
17255208debdSThomas Graf 		if (neigh == NULL) {
17265208debdSThomas Graf 			if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
17271da177e4SLinus Torvalds 				err = -ENOENT;
1728110b2499SEric Dumazet 				goto out;
17295208debdSThomas Graf 			}
17305208debdSThomas Graf 
17315208debdSThomas Graf 			neigh = __neigh_lookup_errno(tbl, dst, dev);
17325208debdSThomas Graf 			if (IS_ERR(neigh)) {
17335208debdSThomas Graf 				err = PTR_ERR(neigh);
1734110b2499SEric Dumazet 				goto out;
17351da177e4SLinus Torvalds 			}
17365208debdSThomas Graf 		} else {
17375208debdSThomas Graf 			if (nlh->nlmsg_flags & NLM_F_EXCL) {
17385208debdSThomas Graf 				err = -EEXIST;
17395208debdSThomas Graf 				neigh_release(neigh);
1740110b2499SEric Dumazet 				goto out;
17411da177e4SLinus Torvalds 			}
17421da177e4SLinus Torvalds 
17435208debdSThomas Graf 			if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
17445208debdSThomas Graf 				flags &= ~NEIGH_UPDATE_F_OVERRIDE;
17455208debdSThomas Graf 		}
17461da177e4SLinus Torvalds 
17470c5c2d30SEric Biederman 		if (ndm->ndm_flags & NTF_USE) {
17480c5c2d30SEric Biederman 			neigh_event_send(neigh, NULL);
17490c5c2d30SEric Biederman 			err = 0;
17500c5c2d30SEric Biederman 		} else
17515208debdSThomas Graf 			err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
17525208debdSThomas Graf 		neigh_release(neigh);
1753110b2499SEric Dumazet 		goto out;
17541da177e4SLinus Torvalds 	}
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
17575208debdSThomas Graf 	err = -EAFNOSUPPORT;
17581da177e4SLinus Torvalds out:
17591da177e4SLinus Torvalds 	return err;
17601da177e4SLinus Torvalds }
17611da177e4SLinus Torvalds 
1762c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1763c7fb64dbSThomas Graf {
1764ca860fb3SThomas Graf 	struct nlattr *nest;
1765e386c6ebSThomas Graf 
1766ca860fb3SThomas Graf 	nest = nla_nest_start(skb, NDTA_PARMS);
1767ca860fb3SThomas Graf 	if (nest == NULL)
1768ca860fb3SThomas Graf 		return -ENOBUFS;
1769c7fb64dbSThomas Graf 
17709a6308d7SDavid S. Miller 	if ((parms->dev &&
17719a6308d7SDavid S. Miller 	     nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
17729a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) ||
17739a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_QUEUE_LENBYTES, parms->queue_len_bytes) ||
17748b5c171bSEric Dumazet 	    /* approximative value for deprecated QUEUE_LEN (in packets) */
17759a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_QUEUE_LEN,
17768b5c171bSEric Dumazet 			DIV_ROUND_UP(parms->queue_len_bytes,
17779a6308d7SDavid S. Miller 				     SKB_TRUESIZE(ETH_FRAME_LEN))) ||
17789a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen) ||
17799a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_APP_PROBES, parms->app_probes) ||
17809a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes) ||
17819a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes) ||
17829a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) ||
17839a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
17849a6308d7SDavid S. Miller 			  parms->base_reachable_time) ||
17859a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_GC_STALETIME, parms->gc_staletime) ||
17869a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
17879a6308d7SDavid S. Miller 			  parms->delay_probe_time) ||
17889a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_RETRANS_TIME, parms->retrans_time) ||
17899a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay) ||
17909a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_PROXY_DELAY, parms->proxy_delay) ||
17919a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_LOCKTIME, parms->locktime))
17929a6308d7SDavid S. Miller 		goto nla_put_failure;
1793ca860fb3SThomas Graf 	return nla_nest_end(skb, nest);
1794c7fb64dbSThomas Graf 
1795ca860fb3SThomas Graf nla_put_failure:
1796bc3ed28cSThomas Graf 	nla_nest_cancel(skb, nest);
1797bc3ed28cSThomas Graf 	return -EMSGSIZE;
1798c7fb64dbSThomas Graf }
1799c7fb64dbSThomas Graf 
1800ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1801ca860fb3SThomas Graf 			      u32 pid, u32 seq, int type, int flags)
1802c7fb64dbSThomas Graf {
1803c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1804c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1805c7fb64dbSThomas Graf 
1806ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1807ca860fb3SThomas Graf 	if (nlh == NULL)
180826932566SPatrick McHardy 		return -EMSGSIZE;
1809c7fb64dbSThomas Graf 
1810ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1811c7fb64dbSThomas Graf 
1812c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1813c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
18149ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
18159ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1816c7fb64dbSThomas Graf 
18179a6308d7SDavid S. Miller 	if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
18189a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval) ||
18199a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
18209a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
18219a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
18229a6308d7SDavid S. Miller 		goto nla_put_failure;
1823c7fb64dbSThomas Graf 	{
1824c7fb64dbSThomas Graf 		unsigned long now = jiffies;
1825c7fb64dbSThomas Graf 		unsigned int flush_delta = now - tbl->last_flush;
1826c7fb64dbSThomas Graf 		unsigned int rand_delta = now - tbl->last_rand;
1827d6bf7817SEric Dumazet 		struct neigh_hash_table *nht;
1828c7fb64dbSThomas Graf 		struct ndt_config ndc = {
1829c7fb64dbSThomas Graf 			.ndtc_key_len		= tbl->key_len,
1830c7fb64dbSThomas Graf 			.ndtc_entry_size	= tbl->entry_size,
1831c7fb64dbSThomas Graf 			.ndtc_entries		= atomic_read(&tbl->entries),
1832c7fb64dbSThomas Graf 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
1833c7fb64dbSThomas Graf 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
1834c7fb64dbSThomas Graf 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
1835c7fb64dbSThomas Graf 		};
1836c7fb64dbSThomas Graf 
1837d6bf7817SEric Dumazet 		rcu_read_lock_bh();
1838d6bf7817SEric Dumazet 		nht = rcu_dereference_bh(tbl->nht);
18392c2aba6cSDavid S. Miller 		ndc.ndtc_hash_rnd = nht->hash_rnd[0];
1840cd089336SDavid S. Miller 		ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
1841d6bf7817SEric Dumazet 		rcu_read_unlock_bh();
1842d6bf7817SEric Dumazet 
18439a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
18449a6308d7SDavid S. Miller 			goto nla_put_failure;
1845c7fb64dbSThomas Graf 	}
1846c7fb64dbSThomas Graf 
1847c7fb64dbSThomas Graf 	{
1848c7fb64dbSThomas Graf 		int cpu;
1849c7fb64dbSThomas Graf 		struct ndt_stats ndst;
1850c7fb64dbSThomas Graf 
1851c7fb64dbSThomas Graf 		memset(&ndst, 0, sizeof(ndst));
1852c7fb64dbSThomas Graf 
18536f912042SKAMEZAWA Hiroyuki 		for_each_possible_cpu(cpu) {
1854c7fb64dbSThomas Graf 			struct neigh_statistics	*st;
1855c7fb64dbSThomas Graf 
1856c7fb64dbSThomas Graf 			st = per_cpu_ptr(tbl->stats, cpu);
1857c7fb64dbSThomas Graf 			ndst.ndts_allocs		+= st->allocs;
1858c7fb64dbSThomas Graf 			ndst.ndts_destroys		+= st->destroys;
1859c7fb64dbSThomas Graf 			ndst.ndts_hash_grows		+= st->hash_grows;
1860c7fb64dbSThomas Graf 			ndst.ndts_res_failed		+= st->res_failed;
1861c7fb64dbSThomas Graf 			ndst.ndts_lookups		+= st->lookups;
1862c7fb64dbSThomas Graf 			ndst.ndts_hits			+= st->hits;
1863c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
1864c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
1865c7fb64dbSThomas Graf 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
1866c7fb64dbSThomas Graf 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
1867c7fb64dbSThomas Graf 		}
1868c7fb64dbSThomas Graf 
18699a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_STATS, sizeof(ndst), &ndst))
18709a6308d7SDavid S. Miller 			goto nla_put_failure;
1871c7fb64dbSThomas Graf 	}
1872c7fb64dbSThomas Graf 
1873c7fb64dbSThomas Graf 	BUG_ON(tbl->parms.dev);
1874c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1875ca860fb3SThomas Graf 		goto nla_put_failure;
1876c7fb64dbSThomas Graf 
1877c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1878ca860fb3SThomas Graf 	return nlmsg_end(skb, nlh);
1879c7fb64dbSThomas Graf 
1880ca860fb3SThomas Graf nla_put_failure:
1881c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
188226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
188326932566SPatrick McHardy 	return -EMSGSIZE;
1884c7fb64dbSThomas Graf }
1885c7fb64dbSThomas Graf 
1886ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb,
1887ca860fb3SThomas Graf 				    struct neigh_table *tbl,
1888c7fb64dbSThomas Graf 				    struct neigh_parms *parms,
1889ca860fb3SThomas Graf 				    u32 pid, u32 seq, int type,
1890ca860fb3SThomas Graf 				    unsigned int flags)
1891c7fb64dbSThomas Graf {
1892c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1893c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1894c7fb64dbSThomas Graf 
1895ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1896ca860fb3SThomas Graf 	if (nlh == NULL)
189726932566SPatrick McHardy 		return -EMSGSIZE;
1898c7fb64dbSThomas Graf 
1899ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1900c7fb64dbSThomas Graf 
1901c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1902c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
19039ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
19049ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1905c7fb64dbSThomas Graf 
1906ca860fb3SThomas Graf 	if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1907ca860fb3SThomas Graf 	    neightbl_fill_parms(skb, parms) < 0)
1908ca860fb3SThomas Graf 		goto errout;
1909c7fb64dbSThomas Graf 
1910c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1911ca860fb3SThomas Graf 	return nlmsg_end(skb, nlh);
1912ca860fb3SThomas Graf errout:
1913c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
191426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
191526932566SPatrick McHardy 	return -EMSGSIZE;
1916c7fb64dbSThomas Graf }
1917c7fb64dbSThomas Graf 
1918ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
19196b3f8674SThomas Graf 	[NDTA_NAME]		= { .type = NLA_STRING },
19206b3f8674SThomas Graf 	[NDTA_THRESH1]		= { .type = NLA_U32 },
19216b3f8674SThomas Graf 	[NDTA_THRESH2]		= { .type = NLA_U32 },
19226b3f8674SThomas Graf 	[NDTA_THRESH3]		= { .type = NLA_U32 },
19236b3f8674SThomas Graf 	[NDTA_GC_INTERVAL]	= { .type = NLA_U64 },
19246b3f8674SThomas Graf 	[NDTA_PARMS]		= { .type = NLA_NESTED },
19256b3f8674SThomas Graf };
19266b3f8674SThomas Graf 
1927ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
19286b3f8674SThomas Graf 	[NDTPA_IFINDEX]			= { .type = NLA_U32 },
19296b3f8674SThomas Graf 	[NDTPA_QUEUE_LEN]		= { .type = NLA_U32 },
19306b3f8674SThomas Graf 	[NDTPA_PROXY_QLEN]		= { .type = NLA_U32 },
19316b3f8674SThomas Graf 	[NDTPA_APP_PROBES]		= { .type = NLA_U32 },
19326b3f8674SThomas Graf 	[NDTPA_UCAST_PROBES]		= { .type = NLA_U32 },
19336b3f8674SThomas Graf 	[NDTPA_MCAST_PROBES]		= { .type = NLA_U32 },
19346b3f8674SThomas Graf 	[NDTPA_BASE_REACHABLE_TIME]	= { .type = NLA_U64 },
19356b3f8674SThomas Graf 	[NDTPA_GC_STALETIME]		= { .type = NLA_U64 },
19366b3f8674SThomas Graf 	[NDTPA_DELAY_PROBE_TIME]	= { .type = NLA_U64 },
19376b3f8674SThomas Graf 	[NDTPA_RETRANS_TIME]		= { .type = NLA_U64 },
19386b3f8674SThomas Graf 	[NDTPA_ANYCAST_DELAY]		= { .type = NLA_U64 },
19396b3f8674SThomas Graf 	[NDTPA_PROXY_DELAY]		= { .type = NLA_U64 },
19406b3f8674SThomas Graf 	[NDTPA_LOCKTIME]		= { .type = NLA_U64 },
19416b3f8674SThomas Graf };
19426b3f8674SThomas Graf 
1943c8822a4eSThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1944c7fb64dbSThomas Graf {
19453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1946c7fb64dbSThomas Graf 	struct neigh_table *tbl;
19476b3f8674SThomas Graf 	struct ndtmsg *ndtmsg;
19486b3f8674SThomas Graf 	struct nlattr *tb[NDTA_MAX+1];
19496b3f8674SThomas Graf 	int err;
1950c7fb64dbSThomas Graf 
19516b3f8674SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
19526b3f8674SThomas Graf 			  nl_neightbl_policy);
19536b3f8674SThomas Graf 	if (err < 0)
19546b3f8674SThomas Graf 		goto errout;
1955c7fb64dbSThomas Graf 
19566b3f8674SThomas Graf 	if (tb[NDTA_NAME] == NULL) {
19576b3f8674SThomas Graf 		err = -EINVAL;
19586b3f8674SThomas Graf 		goto errout;
19596b3f8674SThomas Graf 	}
19606b3f8674SThomas Graf 
19616b3f8674SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1962c7fb64dbSThomas Graf 	read_lock(&neigh_tbl_lock);
1963c7fb64dbSThomas Graf 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1964c7fb64dbSThomas Graf 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1965c7fb64dbSThomas Graf 			continue;
1966c7fb64dbSThomas Graf 
19676b3f8674SThomas Graf 		if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0)
1968c7fb64dbSThomas Graf 			break;
1969c7fb64dbSThomas Graf 	}
1970c7fb64dbSThomas Graf 
1971c7fb64dbSThomas Graf 	if (tbl == NULL) {
1972c7fb64dbSThomas Graf 		err = -ENOENT;
19736b3f8674SThomas Graf 		goto errout_locked;
1974c7fb64dbSThomas Graf 	}
1975c7fb64dbSThomas Graf 
1976c7fb64dbSThomas Graf 	/*
1977c7fb64dbSThomas Graf 	 * We acquire tbl->lock to be nice to the periodic timers and
1978c7fb64dbSThomas Graf 	 * make sure they always see a consistent set of values.
1979c7fb64dbSThomas Graf 	 */
1980c7fb64dbSThomas Graf 	write_lock_bh(&tbl->lock);
1981c7fb64dbSThomas Graf 
19826b3f8674SThomas Graf 	if (tb[NDTA_PARMS]) {
19836b3f8674SThomas Graf 		struct nlattr *tbp[NDTPA_MAX+1];
1984c7fb64dbSThomas Graf 		struct neigh_parms *p;
19856b3f8674SThomas Graf 		int i, ifindex = 0;
1986c7fb64dbSThomas Graf 
19876b3f8674SThomas Graf 		err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
19886b3f8674SThomas Graf 				       nl_ntbl_parm_policy);
19896b3f8674SThomas Graf 		if (err < 0)
19906b3f8674SThomas Graf 			goto errout_tbl_lock;
1991c7fb64dbSThomas Graf 
19926b3f8674SThomas Graf 		if (tbp[NDTPA_IFINDEX])
19936b3f8674SThomas Graf 			ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
1994c7fb64dbSThomas Graf 
199597fd5bc7STobias Klauser 		p = lookup_neigh_parms(tbl, net, ifindex);
1996c7fb64dbSThomas Graf 		if (p == NULL) {
1997c7fb64dbSThomas Graf 			err = -ENOENT;
19986b3f8674SThomas Graf 			goto errout_tbl_lock;
1999c7fb64dbSThomas Graf 		}
2000c7fb64dbSThomas Graf 
20016b3f8674SThomas Graf 		for (i = 1; i <= NDTPA_MAX; i++) {
20026b3f8674SThomas Graf 			if (tbp[i] == NULL)
20036b3f8674SThomas Graf 				continue;
2004c7fb64dbSThomas Graf 
20056b3f8674SThomas Graf 			switch (i) {
20066b3f8674SThomas Graf 			case NDTPA_QUEUE_LEN:
20078b5c171bSEric Dumazet 				p->queue_len_bytes = nla_get_u32(tbp[i]) *
20088b5c171bSEric Dumazet 						     SKB_TRUESIZE(ETH_FRAME_LEN);
20098b5c171bSEric Dumazet 				break;
20108b5c171bSEric Dumazet 			case NDTPA_QUEUE_LENBYTES:
20118b5c171bSEric Dumazet 				p->queue_len_bytes = nla_get_u32(tbp[i]);
20126b3f8674SThomas Graf 				break;
20136b3f8674SThomas Graf 			case NDTPA_PROXY_QLEN:
20146b3f8674SThomas Graf 				p->proxy_qlen = nla_get_u32(tbp[i]);
20156b3f8674SThomas Graf 				break;
20166b3f8674SThomas Graf 			case NDTPA_APP_PROBES:
20176b3f8674SThomas Graf 				p->app_probes = nla_get_u32(tbp[i]);
20186b3f8674SThomas Graf 				break;
20196b3f8674SThomas Graf 			case NDTPA_UCAST_PROBES:
20206b3f8674SThomas Graf 				p->ucast_probes = nla_get_u32(tbp[i]);
20216b3f8674SThomas Graf 				break;
20226b3f8674SThomas Graf 			case NDTPA_MCAST_PROBES:
20236b3f8674SThomas Graf 				p->mcast_probes = nla_get_u32(tbp[i]);
20246b3f8674SThomas Graf 				break;
20256b3f8674SThomas Graf 			case NDTPA_BASE_REACHABLE_TIME:
20266b3f8674SThomas Graf 				p->base_reachable_time = nla_get_msecs(tbp[i]);
20276b3f8674SThomas Graf 				break;
20286b3f8674SThomas Graf 			case NDTPA_GC_STALETIME:
20296b3f8674SThomas Graf 				p->gc_staletime = nla_get_msecs(tbp[i]);
20306b3f8674SThomas Graf 				break;
20316b3f8674SThomas Graf 			case NDTPA_DELAY_PROBE_TIME:
20326b3f8674SThomas Graf 				p->delay_probe_time = nla_get_msecs(tbp[i]);
20336b3f8674SThomas Graf 				break;
20346b3f8674SThomas Graf 			case NDTPA_RETRANS_TIME:
20356b3f8674SThomas Graf 				p->retrans_time = nla_get_msecs(tbp[i]);
20366b3f8674SThomas Graf 				break;
20376b3f8674SThomas Graf 			case NDTPA_ANYCAST_DELAY:
20386b3f8674SThomas Graf 				p->anycast_delay = nla_get_msecs(tbp[i]);
20396b3f8674SThomas Graf 				break;
20406b3f8674SThomas Graf 			case NDTPA_PROXY_DELAY:
20416b3f8674SThomas Graf 				p->proxy_delay = nla_get_msecs(tbp[i]);
20426b3f8674SThomas Graf 				break;
20436b3f8674SThomas Graf 			case NDTPA_LOCKTIME:
20446b3f8674SThomas Graf 				p->locktime = nla_get_msecs(tbp[i]);
20456b3f8674SThomas Graf 				break;
2046c7fb64dbSThomas Graf 			}
20476b3f8674SThomas Graf 		}
20486b3f8674SThomas Graf 	}
20496b3f8674SThomas Graf 
20506b3f8674SThomas Graf 	if (tb[NDTA_THRESH1])
20516b3f8674SThomas Graf 		tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
20526b3f8674SThomas Graf 
20536b3f8674SThomas Graf 	if (tb[NDTA_THRESH2])
20546b3f8674SThomas Graf 		tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
20556b3f8674SThomas Graf 
20566b3f8674SThomas Graf 	if (tb[NDTA_THRESH3])
20576b3f8674SThomas Graf 		tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
20586b3f8674SThomas Graf 
20596b3f8674SThomas Graf 	if (tb[NDTA_GC_INTERVAL])
20606b3f8674SThomas Graf 		tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2061c7fb64dbSThomas Graf 
2062c7fb64dbSThomas Graf 	err = 0;
2063c7fb64dbSThomas Graf 
20646b3f8674SThomas Graf errout_tbl_lock:
2065c7fb64dbSThomas Graf 	write_unlock_bh(&tbl->lock);
20666b3f8674SThomas Graf errout_locked:
2067c7fb64dbSThomas Graf 	read_unlock(&neigh_tbl_lock);
20686b3f8674SThomas Graf errout:
2069c7fb64dbSThomas Graf 	return err;
2070c7fb64dbSThomas Graf }
2071c7fb64dbSThomas Graf 
2072c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2073c7fb64dbSThomas Graf {
20743b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2075ca860fb3SThomas Graf 	int family, tidx, nidx = 0;
2076ca860fb3SThomas Graf 	int tbl_skip = cb->args[0];
2077ca860fb3SThomas Graf 	int neigh_skip = cb->args[1];
2078c7fb64dbSThomas Graf 	struct neigh_table *tbl;
2079c7fb64dbSThomas Graf 
2080ca860fb3SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2081c7fb64dbSThomas Graf 
2082c7fb64dbSThomas Graf 	read_lock(&neigh_tbl_lock);
2083ca860fb3SThomas Graf 	for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) {
2084c7fb64dbSThomas Graf 		struct neigh_parms *p;
2085c7fb64dbSThomas Graf 
2086ca860fb3SThomas Graf 		if (tidx < tbl_skip || (family && tbl->family != family))
2087c7fb64dbSThomas Graf 			continue;
2088c7fb64dbSThomas Graf 
2089ca860fb3SThomas Graf 		if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid,
2090ca860fb3SThomas Graf 				       cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2091ca860fb3SThomas Graf 				       NLM_F_MULTI) <= 0)
2092c7fb64dbSThomas Graf 			break;
2093c7fb64dbSThomas Graf 
2094426b5303SEric W. Biederman 		for (nidx = 0, p = tbl->parms.next; p; p = p->next) {
2095878628fbSYOSHIFUJI Hideaki 			if (!net_eq(neigh_parms_net(p), net))
2096426b5303SEric W. Biederman 				continue;
2097426b5303SEric W. Biederman 
2098efc683fcSGautam Kachroo 			if (nidx < neigh_skip)
2099efc683fcSGautam Kachroo 				goto next;
2100c7fb64dbSThomas Graf 
2101ca860fb3SThomas Graf 			if (neightbl_fill_param_info(skb, tbl, p,
2102ca860fb3SThomas Graf 						     NETLINK_CB(cb->skb).pid,
2103ca860fb3SThomas Graf 						     cb->nlh->nlmsg_seq,
2104ca860fb3SThomas Graf 						     RTM_NEWNEIGHTBL,
2105ca860fb3SThomas Graf 						     NLM_F_MULTI) <= 0)
2106c7fb64dbSThomas Graf 				goto out;
2107efc683fcSGautam Kachroo 		next:
2108efc683fcSGautam Kachroo 			nidx++;
2109c7fb64dbSThomas Graf 		}
2110c7fb64dbSThomas Graf 
2111ca860fb3SThomas Graf 		neigh_skip = 0;
2112c7fb64dbSThomas Graf 	}
2113c7fb64dbSThomas Graf out:
2114c7fb64dbSThomas Graf 	read_unlock(&neigh_tbl_lock);
2115ca860fb3SThomas Graf 	cb->args[0] = tidx;
2116ca860fb3SThomas Graf 	cb->args[1] = nidx;
2117c7fb64dbSThomas Graf 
2118c7fb64dbSThomas Graf 	return skb->len;
2119c7fb64dbSThomas Graf }
21201da177e4SLinus Torvalds 
21218b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
21228b8aec50SThomas Graf 			   u32 pid, u32 seq, int type, unsigned int flags)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	unsigned long now = jiffies;
21251da177e4SLinus Torvalds 	struct nda_cacheinfo ci;
21268b8aec50SThomas Graf 	struct nlmsghdr *nlh;
21278b8aec50SThomas Graf 	struct ndmsg *ndm;
21281da177e4SLinus Torvalds 
21298b8aec50SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
21308b8aec50SThomas Graf 	if (nlh == NULL)
213126932566SPatrick McHardy 		return -EMSGSIZE;
21328b8aec50SThomas Graf 
21338b8aec50SThomas Graf 	ndm = nlmsg_data(nlh);
21348b8aec50SThomas Graf 	ndm->ndm_family	 = neigh->ops->family;
21359ef1d4c7SPatrick McHardy 	ndm->ndm_pad1    = 0;
21369ef1d4c7SPatrick McHardy 	ndm->ndm_pad2    = 0;
21378b8aec50SThomas Graf 	ndm->ndm_flags	 = neigh->flags;
21388b8aec50SThomas Graf 	ndm->ndm_type	 = neigh->type;
21398b8aec50SThomas Graf 	ndm->ndm_ifindex = neigh->dev->ifindex;
21401da177e4SLinus Torvalds 
21419a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
21429a6308d7SDavid S. Miller 		goto nla_put_failure;
21438b8aec50SThomas Graf 
21448b8aec50SThomas Graf 	read_lock_bh(&neigh->lock);
21458b8aec50SThomas Graf 	ndm->ndm_state	 = neigh->nud_state;
21460ed8ddf4SEric Dumazet 	if (neigh->nud_state & NUD_VALID) {
21470ed8ddf4SEric Dumazet 		char haddr[MAX_ADDR_LEN];
21480ed8ddf4SEric Dumazet 
21490ed8ddf4SEric Dumazet 		neigh_ha_snapshot(haddr, neigh, neigh->dev);
21500ed8ddf4SEric Dumazet 		if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
21518b8aec50SThomas Graf 			read_unlock_bh(&neigh->lock);
21528b8aec50SThomas Graf 			goto nla_put_failure;
21538b8aec50SThomas Graf 		}
21540ed8ddf4SEric Dumazet 	}
21558b8aec50SThomas Graf 
2156b9f5f52cSStephen Hemminger 	ci.ndm_used	 = jiffies_to_clock_t(now - neigh->used);
2157b9f5f52cSStephen Hemminger 	ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2158b9f5f52cSStephen Hemminger 	ci.ndm_updated	 = jiffies_to_clock_t(now - neigh->updated);
21598b8aec50SThomas Graf 	ci.ndm_refcnt	 = atomic_read(&neigh->refcnt) - 1;
21608b8aec50SThomas Graf 	read_unlock_bh(&neigh->lock);
21618b8aec50SThomas Graf 
21629a6308d7SDavid S. Miller 	if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
21639a6308d7SDavid S. Miller 	    nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
21649a6308d7SDavid S. Miller 		goto nla_put_failure;
21658b8aec50SThomas Graf 
21668b8aec50SThomas Graf 	return nlmsg_end(skb, nlh);
21678b8aec50SThomas Graf 
21688b8aec50SThomas Graf nla_put_failure:
216926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
217026932566SPatrick McHardy 	return -EMSGSIZE;
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
217384920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
217484920c14STony Zelenoff 			    u32 pid, u32 seq, int type, unsigned int flags,
217584920c14STony Zelenoff 			    struct neigh_table *tbl)
217684920c14STony Zelenoff {
217784920c14STony Zelenoff 	struct nlmsghdr *nlh;
217884920c14STony Zelenoff 	struct ndmsg *ndm;
217984920c14STony Zelenoff 
218084920c14STony Zelenoff 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
218184920c14STony Zelenoff 	if (nlh == NULL)
218284920c14STony Zelenoff 		return -EMSGSIZE;
218384920c14STony Zelenoff 
218484920c14STony Zelenoff 	ndm = nlmsg_data(nlh);
218584920c14STony Zelenoff 	ndm->ndm_family	 = tbl->family;
218684920c14STony Zelenoff 	ndm->ndm_pad1    = 0;
218784920c14STony Zelenoff 	ndm->ndm_pad2    = 0;
218884920c14STony Zelenoff 	ndm->ndm_flags	 = pn->flags | NTF_PROXY;
218984920c14STony Zelenoff 	ndm->ndm_type	 = NDA_DST;
219084920c14STony Zelenoff 	ndm->ndm_ifindex = pn->dev->ifindex;
219184920c14STony Zelenoff 	ndm->ndm_state	 = NUD_NONE;
219284920c14STony Zelenoff 
21939a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
21949a6308d7SDavid S. Miller 		goto nla_put_failure;
219584920c14STony Zelenoff 
219684920c14STony Zelenoff 	return nlmsg_end(skb, nlh);
219784920c14STony Zelenoff 
219884920c14STony Zelenoff nla_put_failure:
219984920c14STony Zelenoff 	nlmsg_cancel(skb, nlh);
220084920c14STony Zelenoff 	return -EMSGSIZE;
220184920c14STony Zelenoff }
220284920c14STony Zelenoff 
2203d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh)
2204d961db35SThomas Graf {
2205d961db35SThomas Graf 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2206d961db35SThomas Graf 	__neigh_notify(neigh, RTM_NEWNEIGH, 0);
2207d961db35SThomas Graf }
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
22101da177e4SLinus Torvalds 			    struct netlink_callback *cb)
22111da177e4SLinus Torvalds {
22123b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
22131da177e4SLinus Torvalds 	struct neighbour *n;
22141da177e4SLinus Torvalds 	int rc, h, s_h = cb->args[1];
22151da177e4SLinus Torvalds 	int idx, s_idx = idx = cb->args[2];
2216d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
22171da177e4SLinus Torvalds 
2218d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2219d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2220d6bf7817SEric Dumazet 
2221cd089336SDavid S. Miller 	for (h = 0; h < (1 << nht->hash_shift); h++) {
22221da177e4SLinus Torvalds 		if (h < s_h)
22231da177e4SLinus Torvalds 			continue;
22241da177e4SLinus Torvalds 		if (h > s_h)
22251da177e4SLinus Torvalds 			s_idx = 0;
2226767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2227767e97e1SEric Dumazet 		     n != NULL;
2228767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next)) {
222909ad9bc7SOctavian Purdila 			if (!net_eq(dev_net(n->dev), net))
2230426b5303SEric W. Biederman 				continue;
2231efc683fcSGautam Kachroo 			if (idx < s_idx)
2232efc683fcSGautam Kachroo 				goto next;
22331da177e4SLinus Torvalds 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
22341da177e4SLinus Torvalds 					    cb->nlh->nlmsg_seq,
2235b6544c0bSJamal Hadi Salim 					    RTM_NEWNEIGH,
2236b6544c0bSJamal Hadi Salim 					    NLM_F_MULTI) <= 0) {
22371da177e4SLinus Torvalds 				rc = -1;
22381da177e4SLinus Torvalds 				goto out;
22391da177e4SLinus Torvalds 			}
2240efc683fcSGautam Kachroo next:
2241efc683fcSGautam Kachroo 			idx++;
22421da177e4SLinus Torvalds 		}
22431da177e4SLinus Torvalds 	}
22441da177e4SLinus Torvalds 	rc = skb->len;
22451da177e4SLinus Torvalds out:
2246d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
22471da177e4SLinus Torvalds 	cb->args[1] = h;
22481da177e4SLinus Torvalds 	cb->args[2] = idx;
22491da177e4SLinus Torvalds 	return rc;
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
225284920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
225384920c14STony Zelenoff 			     struct netlink_callback *cb)
225484920c14STony Zelenoff {
225584920c14STony Zelenoff 	struct pneigh_entry *n;
225684920c14STony Zelenoff 	struct net *net = sock_net(skb->sk);
225784920c14STony Zelenoff 	int rc, h, s_h = cb->args[3];
225884920c14STony Zelenoff 	int idx, s_idx = idx = cb->args[4];
225984920c14STony Zelenoff 
226084920c14STony Zelenoff 	read_lock_bh(&tbl->lock);
226184920c14STony Zelenoff 
226284920c14STony Zelenoff 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
226384920c14STony Zelenoff 		if (h < s_h)
226484920c14STony Zelenoff 			continue;
226584920c14STony Zelenoff 		if (h > s_h)
226684920c14STony Zelenoff 			s_idx = 0;
226784920c14STony Zelenoff 		for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
226884920c14STony Zelenoff 			if (dev_net(n->dev) != net)
226984920c14STony Zelenoff 				continue;
227084920c14STony Zelenoff 			if (idx < s_idx)
227184920c14STony Zelenoff 				goto next;
227284920c14STony Zelenoff 			if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
227384920c14STony Zelenoff 					    cb->nlh->nlmsg_seq,
227484920c14STony Zelenoff 					    RTM_NEWNEIGH,
227584920c14STony Zelenoff 					    NLM_F_MULTI, tbl) <= 0) {
227684920c14STony Zelenoff 				read_unlock_bh(&tbl->lock);
227784920c14STony Zelenoff 				rc = -1;
227884920c14STony Zelenoff 				goto out;
227984920c14STony Zelenoff 			}
228084920c14STony Zelenoff 		next:
228184920c14STony Zelenoff 			idx++;
228284920c14STony Zelenoff 		}
228384920c14STony Zelenoff 	}
228484920c14STony Zelenoff 
228584920c14STony Zelenoff 	read_unlock_bh(&tbl->lock);
228684920c14STony Zelenoff 	rc = skb->len;
228784920c14STony Zelenoff out:
228884920c14STony Zelenoff 	cb->args[3] = h;
228984920c14STony Zelenoff 	cb->args[4] = idx;
229084920c14STony Zelenoff 	return rc;
229184920c14STony Zelenoff 
229284920c14STony Zelenoff }
229384920c14STony Zelenoff 
2294c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
22951da177e4SLinus Torvalds {
22961da177e4SLinus Torvalds 	struct neigh_table *tbl;
22971da177e4SLinus Torvalds 	int t, family, s_t;
229884920c14STony Zelenoff 	int proxy = 0;
229984920c14STony Zelenoff 	int err = 0;
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
23028b8aec50SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
230384920c14STony Zelenoff 
230484920c14STony Zelenoff 	/* check for full ndmsg structure presence, family member is
230584920c14STony Zelenoff 	 * the same for both structures
230684920c14STony Zelenoff 	 */
230784920c14STony Zelenoff 	if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
230884920c14STony Zelenoff 	    ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
230984920c14STony Zelenoff 		proxy = 1;
231084920c14STony Zelenoff 
23111da177e4SLinus Torvalds 	s_t = cb->args[0];
23121da177e4SLinus Torvalds 
231384920c14STony Zelenoff 	for (tbl = neigh_tables, t = 0; tbl && (err >= 0);
231484920c14STony Zelenoff 	     tbl = tbl->next, t++) {
23151da177e4SLinus Torvalds 		if (t < s_t || (family && tbl->family != family))
23161da177e4SLinus Torvalds 			continue;
23171da177e4SLinus Torvalds 		if (t > s_t)
23181da177e4SLinus Torvalds 			memset(&cb->args[1], 0, sizeof(cb->args) -
23191da177e4SLinus Torvalds 						sizeof(cb->args[0]));
232084920c14STony Zelenoff 		if (proxy)
232184920c14STony Zelenoff 			err = pneigh_dump_table(tbl, skb, cb);
232284920c14STony Zelenoff 		else
232384920c14STony Zelenoff 			err = neigh_dump_table(tbl, skb, cb);
23241da177e4SLinus Torvalds 	}
23251da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds 	cb->args[0] = t;
23281da177e4SLinus Torvalds 	return skb->len;
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
23321da177e4SLinus Torvalds {
23331da177e4SLinus Torvalds 	int chain;
2334d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
23351da177e4SLinus Torvalds 
2336d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2337d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2338d6bf7817SEric Dumazet 
2339767e97e1SEric Dumazet 	read_lock(&tbl->lock); /* avoid resizes */
2340cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
23411da177e4SLinus Torvalds 		struct neighbour *n;
23421da177e4SLinus Torvalds 
2343767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2344767e97e1SEric Dumazet 		     n != NULL;
2345767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next))
23461da177e4SLinus Torvalds 			cb(n, cookie);
23471da177e4SLinus Torvalds 	}
2348d6bf7817SEric Dumazet 	read_unlock(&tbl->lock);
2349d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each);
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */
23541da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl,
23551da177e4SLinus Torvalds 			      int (*cb)(struct neighbour *))
23561da177e4SLinus Torvalds {
23571da177e4SLinus Torvalds 	int chain;
2358d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
23591da177e4SLinus Torvalds 
2360d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
2361d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
2362cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2363767e97e1SEric Dumazet 		struct neighbour *n;
2364767e97e1SEric Dumazet 		struct neighbour __rcu **np;
23651da177e4SLinus Torvalds 
2366d6bf7817SEric Dumazet 		np = &nht->hash_buckets[chain];
2367767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
2368767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
23691da177e4SLinus Torvalds 			int release;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 			write_lock(&n->lock);
23721da177e4SLinus Torvalds 			release = cb(n);
23731da177e4SLinus Torvalds 			if (release) {
2374767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
2375767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
2376767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
23771da177e4SLinus Torvalds 				n->dead = 1;
23781da177e4SLinus Torvalds 			} else
23791da177e4SLinus Torvalds 				np = &n->next;
23801da177e4SLinus Torvalds 			write_unlock(&n->lock);
23814f494554SThomas Graf 			if (release)
23824f494554SThomas Graf 				neigh_cleanup_and_release(n);
23831da177e4SLinus Torvalds 		}
23841da177e4SLinus Torvalds 	}
2385ecbb4169SAlexey Kuznetsov }
23861da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release);
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
23891da177e4SLinus Torvalds 
23901da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq)
23911da177e4SLinus Torvalds {
23921da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
23931218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2394d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
23951da177e4SLinus Torvalds 	struct neighbour *n = NULL;
23961da177e4SLinus Torvalds 	int bucket = state->bucket;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2399cd089336SDavid S. Miller 	for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
2400767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[bucket]);
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 		while (n) {
2403878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2404426b5303SEric W. Biederman 				goto next;
24051da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
24061da177e4SLinus Torvalds 				loff_t fakep = 0;
24071da177e4SLinus Torvalds 				void *v;
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 				v = state->neigh_sub_iter(state, n, &fakep);
24101da177e4SLinus Torvalds 				if (!v)
24111da177e4SLinus Torvalds 					goto next;
24121da177e4SLinus Torvalds 			}
24131da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
24141da177e4SLinus Torvalds 				break;
24151da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
24161da177e4SLinus Torvalds 				break;
24171da177e4SLinus Torvalds next:
2418767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
24191da177e4SLinus Torvalds 		}
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 		if (n)
24221da177e4SLinus Torvalds 			break;
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 	state->bucket = bucket;
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	return n;
24271da177e4SLinus Torvalds }
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq,
24301da177e4SLinus Torvalds 					struct neighbour *n,
24311da177e4SLinus Torvalds 					loff_t *pos)
24321da177e4SLinus Torvalds {
24331da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
24341218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2435d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	if (state->neigh_sub_iter) {
24381da177e4SLinus Torvalds 		void *v = state->neigh_sub_iter(state, n, pos);
24391da177e4SLinus Torvalds 		if (v)
24401da177e4SLinus Torvalds 			return n;
24411da177e4SLinus Torvalds 	}
2442767e97e1SEric Dumazet 	n = rcu_dereference_bh(n->next);
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	while (1) {
24451da177e4SLinus Torvalds 		while (n) {
2446878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2447426b5303SEric W. Biederman 				goto next;
24481da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
24491da177e4SLinus Torvalds 				void *v = state->neigh_sub_iter(state, n, pos);
24501da177e4SLinus Torvalds 				if (v)
24511da177e4SLinus Torvalds 					return n;
24521da177e4SLinus Torvalds 				goto next;
24531da177e4SLinus Torvalds 			}
24541da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
24551da177e4SLinus Torvalds 				break;
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
24581da177e4SLinus Torvalds 				break;
24591da177e4SLinus Torvalds next:
2460767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
24611da177e4SLinus Torvalds 		}
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 		if (n)
24641da177e4SLinus Torvalds 			break;
24651da177e4SLinus Torvalds 
2466cd089336SDavid S. Miller 		if (++state->bucket >= (1 << nht->hash_shift))
24671da177e4SLinus Torvalds 			break;
24681da177e4SLinus Torvalds 
2469767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
24701da177e4SLinus Torvalds 	}
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	if (n && pos)
24731da177e4SLinus Torvalds 		--(*pos);
24741da177e4SLinus Torvalds 	return n;
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
24781da177e4SLinus Torvalds {
24791da177e4SLinus Torvalds 	struct neighbour *n = neigh_get_first(seq);
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	if (n) {
2482745e2031SChris Larson 		--(*pos);
24831da177e4SLinus Torvalds 		while (*pos) {
24841da177e4SLinus Torvalds 			n = neigh_get_next(seq, n, pos);
24851da177e4SLinus Torvalds 			if (!n)
24861da177e4SLinus Torvalds 				break;
24871da177e4SLinus Torvalds 		}
24881da177e4SLinus Torvalds 	}
24891da177e4SLinus Torvalds 	return *pos ? NULL : n;
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
24931da177e4SLinus Torvalds {
24941da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
24951218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
24961da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
24971da177e4SLinus Torvalds 	struct pneigh_entry *pn = NULL;
24981da177e4SLinus Torvalds 	int bucket = state->bucket;
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
25011da177e4SLinus Torvalds 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
25021da177e4SLinus Torvalds 		pn = tbl->phash_buckets[bucket];
2503878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2504426b5303SEric W. Biederman 			pn = pn->next;
25051da177e4SLinus Torvalds 		if (pn)
25061da177e4SLinus Torvalds 			break;
25071da177e4SLinus Torvalds 	}
25081da177e4SLinus Torvalds 	state->bucket = bucket;
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	return pn;
25111da177e4SLinus Torvalds }
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
25141da177e4SLinus Torvalds 					    struct pneigh_entry *pn,
25151da177e4SLinus Torvalds 					    loff_t *pos)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25181218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
25191da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
25201da177e4SLinus Torvalds 
2521df07a94cSJorge Boncompte [DTI2] 	do {
25221da177e4SLinus Torvalds 		pn = pn->next;
2523df07a94cSJorge Boncompte [DTI2] 	} while (pn && !net_eq(pneigh_net(pn), net));
2524df07a94cSJorge Boncompte [DTI2] 
25251da177e4SLinus Torvalds 	while (!pn) {
25261da177e4SLinus Torvalds 		if (++state->bucket > PNEIGH_HASHMASK)
25271da177e4SLinus Torvalds 			break;
25281da177e4SLinus Torvalds 		pn = tbl->phash_buckets[state->bucket];
2529878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2530426b5303SEric W. Biederman 			pn = pn->next;
25311da177e4SLinus Torvalds 		if (pn)
25321da177e4SLinus Torvalds 			break;
25331da177e4SLinus Torvalds 	}
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	if (pn && pos)
25361da177e4SLinus Torvalds 		--(*pos);
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	return pn;
25391da177e4SLinus Torvalds }
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
25421da177e4SLinus Torvalds {
25431da177e4SLinus Torvalds 	struct pneigh_entry *pn = pneigh_get_first(seq);
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	if (pn) {
2546745e2031SChris Larson 		--(*pos);
25471da177e4SLinus Torvalds 		while (*pos) {
25481da177e4SLinus Torvalds 			pn = pneigh_get_next(seq, pn, pos);
25491da177e4SLinus Torvalds 			if (!pn)
25501da177e4SLinus Torvalds 				break;
25511da177e4SLinus Torvalds 		}
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 	return *pos ? NULL : pn;
25541da177e4SLinus Torvalds }
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
25571da177e4SLinus Torvalds {
25581da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25591da177e4SLinus Torvalds 	void *rc;
2560745e2031SChris Larson 	loff_t idxpos = *pos;
25611da177e4SLinus Torvalds 
2562745e2031SChris Larson 	rc = neigh_get_idx(seq, &idxpos);
25631da177e4SLinus Torvalds 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2564745e2031SChris Larson 		rc = pneigh_get_idx(seq, &idxpos);
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	return rc;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2570d6bf7817SEric Dumazet 	__acquires(rcu_bh)
25711da177e4SLinus Torvalds {
25721da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	state->tbl = tbl;
25751da177e4SLinus Torvalds 	state->bucket = 0;
25761da177e4SLinus Torvalds 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
25771da177e4SLinus Torvalds 
2578d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2579d6bf7817SEric Dumazet 	state->nht = rcu_dereference_bh(tbl->nht);
2580767e97e1SEric Dumazet 
2581745e2031SChris Larson 	return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start);
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
25861da177e4SLinus Torvalds {
25871da177e4SLinus Torvalds 	struct neigh_seq_state *state;
25881da177e4SLinus Torvalds 	void *rc;
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2591bff69732SChris Larson 		rc = neigh_get_first(seq);
25921da177e4SLinus Torvalds 		goto out;
25931da177e4SLinus Torvalds 	}
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	state = seq->private;
25961da177e4SLinus Torvalds 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
25971da177e4SLinus Torvalds 		rc = neigh_get_next(seq, v, NULL);
25981da177e4SLinus Torvalds 		if (rc)
25991da177e4SLinus Torvalds 			goto out;
26001da177e4SLinus Torvalds 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
26011da177e4SLinus Torvalds 			rc = pneigh_get_first(seq);
26021da177e4SLinus Torvalds 	} else {
26031da177e4SLinus Torvalds 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
26041da177e4SLinus Torvalds 		rc = pneigh_get_next(seq, v, NULL);
26051da177e4SLinus Torvalds 	}
26061da177e4SLinus Torvalds out:
26071da177e4SLinus Torvalds 	++(*pos);
26081da177e4SLinus Torvalds 	return rc;
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next);
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v)
2613d6bf7817SEric Dumazet 	__releases(rcu_bh)
26141da177e4SLinus Torvalds {
2615d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop);
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds /* statistics via seq_file */
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
26221da177e4SLinus Torvalds {
262381c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
26241da177e4SLinus Torvalds 	int cpu;
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	if (*pos == 0)
26271da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
26281da177e4SLinus Torvalds 
26290f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
26301da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
26311da177e4SLinus Torvalds 			continue;
26321da177e4SLinus Torvalds 		*pos = cpu+1;
26331da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
26341da177e4SLinus Torvalds 	}
26351da177e4SLinus Torvalds 	return NULL;
26361da177e4SLinus Torvalds }
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26391da177e4SLinus Torvalds {
264081c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
26411da177e4SLinus Torvalds 	int cpu;
26421da177e4SLinus Torvalds 
26430f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
26441da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
26451da177e4SLinus Torvalds 			continue;
26461da177e4SLinus Torvalds 		*pos = cpu+1;
26471da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
26481da177e4SLinus Torvalds 	}
26491da177e4SLinus Torvalds 	return NULL;
26501da177e4SLinus Torvalds }
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
26531da177e4SLinus Torvalds {
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v)
26581da177e4SLinus Torvalds {
265981c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
26601da177e4SLinus Torvalds 	struct neigh_statistics *st = v;
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
26639a6d276eSNeil Horman 		seq_printf(seq, "entries  allocs destroys hash_grows  lookups hits  res_failed  rcv_probes_mcast rcv_probes_ucast  periodic_gc_runs forced_gc_runs unresolved_discards\n");
26641da177e4SLinus Torvalds 		return 0;
26651da177e4SLinus Torvalds 	}
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
26689a6d276eSNeil Horman 			"%08lx %08lx  %08lx %08lx %08lx\n",
26691da177e4SLinus Torvalds 		   atomic_read(&tbl->entries),
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 		   st->allocs,
26721da177e4SLinus Torvalds 		   st->destroys,
26731da177e4SLinus Torvalds 		   st->hash_grows,
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds 		   st->lookups,
26761da177e4SLinus Torvalds 		   st->hits,
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 		   st->res_failed,
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds 		   st->rcv_probes_mcast,
26811da177e4SLinus Torvalds 		   st->rcv_probes_ucast,
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 		   st->periodic_gc_runs,
26849a6d276eSNeil Horman 		   st->forced_gc_runs,
26859a6d276eSNeil Horman 		   st->unres_discards
26861da177e4SLinus Torvalds 		   );
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	return 0;
26891da177e4SLinus Torvalds }
26901da177e4SLinus Torvalds 
2691f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = {
26921da177e4SLinus Torvalds 	.start	= neigh_stat_seq_start,
26931da177e4SLinus Torvalds 	.next	= neigh_stat_seq_next,
26941da177e4SLinus Torvalds 	.stop	= neigh_stat_seq_stop,
26951da177e4SLinus Torvalds 	.show	= neigh_stat_seq_show,
26961da177e4SLinus Torvalds };
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file)
26991da177e4SLinus Torvalds {
27001da177e4SLinus Torvalds 	int ret = seq_open(file, &neigh_stat_seq_ops);
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	if (!ret) {
27031da177e4SLinus Torvalds 		struct seq_file *sf = file->private_data;
270481c1ebfcSAlexey Dobriyan 		sf->private = PDE(inode)->data;
27051da177e4SLinus Torvalds 	}
27061da177e4SLinus Torvalds 	return ret;
27071da177e4SLinus Torvalds };
27081da177e4SLinus Torvalds 
27099a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = {
27101da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27111da177e4SLinus Torvalds 	.open 	 = neigh_stat_seq_open,
27121da177e4SLinus Torvalds 	.read	 = seq_read,
27131da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
27141da177e4SLinus Torvalds 	.release = seq_release,
27151da177e4SLinus Torvalds };
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
27181da177e4SLinus Torvalds 
2719339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void)
2720339bf98fSThomas Graf {
2721339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ndmsg))
2722339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2723339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2724339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct nda_cacheinfo))
2725339bf98fSThomas Graf 	       + nla_total_size(4); /* NDA_PROBES */
2726339bf98fSThomas Graf }
2727339bf98fSThomas Graf 
2728b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags)
27291da177e4SLinus Torvalds {
2730c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(n->dev);
27318b8aec50SThomas Graf 	struct sk_buff *skb;
2732b8673311SThomas Graf 	int err = -ENOBUFS;
27331da177e4SLinus Torvalds 
2734339bf98fSThomas Graf 	skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
27358b8aec50SThomas Graf 	if (skb == NULL)
2736b8673311SThomas Graf 		goto errout;
27371da177e4SLinus Torvalds 
2738b8673311SThomas Graf 	err = neigh_fill_info(skb, n, 0, 0, type, flags);
273926932566SPatrick McHardy 	if (err < 0) {
274026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
274126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
274226932566SPatrick McHardy 		kfree_skb(skb);
274326932566SPatrick McHardy 		goto errout;
274426932566SPatrick McHardy 	}
27451ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
27461ce85fe4SPablo Neira Ayuso 	return;
2747b8673311SThomas Graf errout:
2748b8673311SThomas Graf 	if (err < 0)
2749426b5303SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2750b8673311SThomas Graf }
2751b8673311SThomas Graf 
2752d961db35SThomas Graf #ifdef CONFIG_ARPD
2753b8673311SThomas Graf void neigh_app_ns(struct neighbour *n)
2754b8673311SThomas Graf {
2755b8673311SThomas Graf 	__neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
27568b8aec50SThomas Graf }
27570a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns);
27581da177e4SLinus Torvalds #endif /* CONFIG_ARPD */
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27611da177e4SLinus Torvalds 
27628b5c171bSEric Dumazet static int proc_unres_qlen(ctl_table *ctl, int write, void __user *buffer,
27638b5c171bSEric Dumazet 			   size_t *lenp, loff_t *ppos)
27648b5c171bSEric Dumazet {
27658b5c171bSEric Dumazet 	int size, ret;
27668b5c171bSEric Dumazet 	ctl_table tmp = *ctl;
27678b5c171bSEric Dumazet 
27688b5c171bSEric Dumazet 	tmp.data = &size;
27698b5c171bSEric Dumazet 	size = DIV_ROUND_UP(*(int *)ctl->data, SKB_TRUESIZE(ETH_FRAME_LEN));
27708b5c171bSEric Dumazet 	ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
27718b5c171bSEric Dumazet 	if (write && !ret)
27728b5c171bSEric Dumazet 		*(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
27738b5c171bSEric Dumazet 	return ret;
27748b5c171bSEric Dumazet }
27758b5c171bSEric Dumazet 
27768b5c171bSEric Dumazet enum {
27778b5c171bSEric Dumazet 	NEIGH_VAR_MCAST_PROBE,
27788b5c171bSEric Dumazet 	NEIGH_VAR_UCAST_PROBE,
27798b5c171bSEric Dumazet 	NEIGH_VAR_APP_PROBE,
27808b5c171bSEric Dumazet 	NEIGH_VAR_RETRANS_TIME,
27818b5c171bSEric Dumazet 	NEIGH_VAR_BASE_REACHABLE_TIME,
27828b5c171bSEric Dumazet 	NEIGH_VAR_DELAY_PROBE_TIME,
27838b5c171bSEric Dumazet 	NEIGH_VAR_GC_STALETIME,
27848b5c171bSEric Dumazet 	NEIGH_VAR_QUEUE_LEN,
27858b5c171bSEric Dumazet 	NEIGH_VAR_QUEUE_LEN_BYTES,
27868b5c171bSEric Dumazet 	NEIGH_VAR_PROXY_QLEN,
27878b5c171bSEric Dumazet 	NEIGH_VAR_ANYCAST_DELAY,
27888b5c171bSEric Dumazet 	NEIGH_VAR_PROXY_DELAY,
27898b5c171bSEric Dumazet 	NEIGH_VAR_LOCKTIME,
27908b5c171bSEric Dumazet 	NEIGH_VAR_RETRANS_TIME_MS,
27918b5c171bSEric Dumazet 	NEIGH_VAR_BASE_REACHABLE_TIME_MS,
27928b5c171bSEric Dumazet 	NEIGH_VAR_GC_INTERVAL,
27938b5c171bSEric Dumazet 	NEIGH_VAR_GC_THRESH1,
27948b5c171bSEric Dumazet 	NEIGH_VAR_GC_THRESH2,
27958b5c171bSEric Dumazet 	NEIGH_VAR_GC_THRESH3,
27968b5c171bSEric Dumazet 	NEIGH_VAR_MAX
27978b5c171bSEric Dumazet };
279854716e3bSEric W. Biederman 
27991da177e4SLinus Torvalds static struct neigh_sysctl_table {
28001da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
28018b5c171bSEric Dumazet 	struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
2802c3bac5a7SPavel Emelyanov 	char *dev_name;
2803ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = {
28041da177e4SLinus Torvalds 	.neigh_vars = {
28058b5c171bSEric Dumazet 		[NEIGH_VAR_MCAST_PROBE] = {
28061da177e4SLinus Torvalds 			.procname	= "mcast_solicit",
28071da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28081da177e4SLinus Torvalds 			.mode		= 0644,
28096d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
28101da177e4SLinus Torvalds 		},
28118b5c171bSEric Dumazet 		[NEIGH_VAR_UCAST_PROBE] = {
28121da177e4SLinus Torvalds 			.procname	= "ucast_solicit",
28131da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28141da177e4SLinus Torvalds 			.mode		= 0644,
28156d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
28161da177e4SLinus Torvalds 		},
28178b5c171bSEric Dumazet 		[NEIGH_VAR_APP_PROBE] = {
28181da177e4SLinus Torvalds 			.procname	= "app_solicit",
28191da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28201da177e4SLinus Torvalds 			.mode		= 0644,
28216d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
28221da177e4SLinus Torvalds 		},
28238b5c171bSEric Dumazet 		[NEIGH_VAR_RETRANS_TIME] = {
28241da177e4SLinus Torvalds 			.procname	= "retrans_time",
28251da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28261da177e4SLinus Torvalds 			.mode		= 0644,
28276d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_userhz_jiffies,
28281da177e4SLinus Torvalds 		},
28298b5c171bSEric Dumazet 		[NEIGH_VAR_BASE_REACHABLE_TIME] = {
28301da177e4SLinus Torvalds 			.procname	= "base_reachable_time",
28311da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28321da177e4SLinus Torvalds 			.mode		= 0644,
28336d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
28341da177e4SLinus Torvalds 		},
28358b5c171bSEric Dumazet 		[NEIGH_VAR_DELAY_PROBE_TIME] = {
28361da177e4SLinus Torvalds 			.procname	= "delay_first_probe_time",
28371da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28381da177e4SLinus Torvalds 			.mode		= 0644,
28396d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
28401da177e4SLinus Torvalds 		},
28418b5c171bSEric Dumazet 		[NEIGH_VAR_GC_STALETIME] = {
28421da177e4SLinus Torvalds 			.procname	= "gc_stale_time",
28431da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28441da177e4SLinus Torvalds 			.mode		= 0644,
28456d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
28461da177e4SLinus Torvalds 		},
28478b5c171bSEric Dumazet 		[NEIGH_VAR_QUEUE_LEN] = {
28481da177e4SLinus Torvalds 			.procname	= "unres_qlen",
28491da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28501da177e4SLinus Torvalds 			.mode		= 0644,
28518b5c171bSEric Dumazet 			.proc_handler	= proc_unres_qlen,
28528b5c171bSEric Dumazet 		},
28538b5c171bSEric Dumazet 		[NEIGH_VAR_QUEUE_LEN_BYTES] = {
28548b5c171bSEric Dumazet 			.procname	= "unres_qlen_bytes",
28558b5c171bSEric Dumazet 			.maxlen		= sizeof(int),
28568b5c171bSEric Dumazet 			.mode		= 0644,
28576d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
28581da177e4SLinus Torvalds 		},
28598b5c171bSEric Dumazet 		[NEIGH_VAR_PROXY_QLEN] = {
28601da177e4SLinus Torvalds 			.procname	= "proxy_qlen",
28611da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28621da177e4SLinus Torvalds 			.mode		= 0644,
28636d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
28641da177e4SLinus Torvalds 		},
28658b5c171bSEric Dumazet 		[NEIGH_VAR_ANYCAST_DELAY] = {
28661da177e4SLinus Torvalds 			.procname	= "anycast_delay",
28671da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28681da177e4SLinus Torvalds 			.mode		= 0644,
28696d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_userhz_jiffies,
28701da177e4SLinus Torvalds 		},
28718b5c171bSEric Dumazet 		[NEIGH_VAR_PROXY_DELAY] = {
28721da177e4SLinus Torvalds 			.procname	= "proxy_delay",
28731da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28741da177e4SLinus Torvalds 			.mode		= 0644,
28756d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_userhz_jiffies,
28761da177e4SLinus Torvalds 		},
28778b5c171bSEric Dumazet 		[NEIGH_VAR_LOCKTIME] = {
28781da177e4SLinus Torvalds 			.procname	= "locktime",
28791da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28801da177e4SLinus Torvalds 			.mode		= 0644,
28816d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_userhz_jiffies,
28821da177e4SLinus Torvalds 		},
28838b5c171bSEric Dumazet 		[NEIGH_VAR_RETRANS_TIME_MS] = {
2884d12af679SEric W. Biederman 			.procname	= "retrans_time_ms",
2885d12af679SEric W. Biederman 			.maxlen		= sizeof(int),
2886d12af679SEric W. Biederman 			.mode		= 0644,
28876d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_ms_jiffies,
2888d12af679SEric W. Biederman 		},
28898b5c171bSEric Dumazet 		[NEIGH_VAR_BASE_REACHABLE_TIME_MS] = {
2890d12af679SEric W. Biederman 			.procname	= "base_reachable_time_ms",
2891d12af679SEric W. Biederman 			.maxlen		= sizeof(int),
2892d12af679SEric W. Biederman 			.mode		= 0644,
28936d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_ms_jiffies,
2894d12af679SEric W. Biederman 		},
28958b5c171bSEric Dumazet 		[NEIGH_VAR_GC_INTERVAL] = {
28961da177e4SLinus Torvalds 			.procname	= "gc_interval",
28971da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
28981da177e4SLinus Torvalds 			.mode		= 0644,
28996d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
29001da177e4SLinus Torvalds 		},
29018b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH1] = {
29021da177e4SLinus Torvalds 			.procname	= "gc_thresh1",
29031da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
29041da177e4SLinus Torvalds 			.mode		= 0644,
29056d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
29061da177e4SLinus Torvalds 		},
29078b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH2] = {
29081da177e4SLinus Torvalds 			.procname	= "gc_thresh2",
29091da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
29101da177e4SLinus Torvalds 			.mode		= 0644,
29116d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
29121da177e4SLinus Torvalds 		},
29138b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH3] = {
29141da177e4SLinus Torvalds 			.procname	= "gc_thresh3",
29151da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
29161da177e4SLinus Torvalds 			.mode		= 0644,
29176d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
29181da177e4SLinus Torvalds 		},
2919c3bac5a7SPavel Emelyanov 		{},
29201da177e4SLinus Torvalds 	},
29211da177e4SLinus Torvalds };
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
292454716e3bSEric W. Biederman 			  char *p_name, proc_handler *handler)
29251da177e4SLinus Torvalds {
29263c607bbbSPavel Emelyanov 	struct neigh_sysctl_table *t;
29271da177e4SLinus Torvalds 	const char *dev_name_source = NULL;
2928c3bac5a7SPavel Emelyanov 
2929c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_ROOT	0
2930c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_PROTO	1
2931c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_NEIGH	2
2932c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_DEV	3
2933c3bac5a7SPavel Emelyanov 
2934c3bac5a7SPavel Emelyanov 	struct ctl_path neigh_path[] = {
2935f8572d8fSEric W. Biederman 		{ .procname = "net",	 },
2936f8572d8fSEric W. Biederman 		{ .procname = "proto",	 },
2937f8572d8fSEric W. Biederman 		{ .procname = "neigh",	 },
2938f8572d8fSEric W. Biederman 		{ .procname = "default", },
2939c3bac5a7SPavel Emelyanov 		{ },
2940c3bac5a7SPavel Emelyanov 	};
29411da177e4SLinus Torvalds 
29423c607bbbSPavel Emelyanov 	t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
29431da177e4SLinus Torvalds 	if (!t)
29443c607bbbSPavel Emelyanov 		goto err;
29453c607bbbSPavel Emelyanov 
29468b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_MCAST_PROBE].data  = &p->mcast_probes;
29478b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_UCAST_PROBE].data  = &p->ucast_probes;
29488b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_APP_PROBE].data  = &p->app_probes;
29498b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_RETRANS_TIME].data  = &p->retrans_time;
29508b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].data  = &p->base_reachable_time;
29518b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_DELAY_PROBE_TIME].data  = &p->delay_probe_time;
29528b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_GC_STALETIME].data  = &p->gc_staletime;
29538b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_QUEUE_LEN].data  = &p->queue_len_bytes;
29548b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_QUEUE_LEN_BYTES].data  = &p->queue_len_bytes;
29558b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_PROXY_QLEN].data  = &p->proxy_qlen;
29568b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_ANYCAST_DELAY].data  = &p->anycast_delay;
29578b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_PROXY_DELAY].data = &p->proxy_delay;
29588b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_LOCKTIME].data = &p->locktime;
29598b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].data  = &p->retrans_time;
29608b5c171bSEric Dumazet 	t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].data  = &p->base_reachable_time;
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 	if (dev) {
29631da177e4SLinus Torvalds 		dev_name_source = dev->name;
2964d12af679SEric W. Biederman 		/* Terminate the table early */
29658b5c171bSEric Dumazet 		memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
29668b5c171bSEric Dumazet 		       sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
29671da177e4SLinus Torvalds 	} else {
2968c3bac5a7SPavel Emelyanov 		dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname;
29698b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = (int *)(p + 1);
29708b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = (int *)(p + 1) + 1;
29718b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = (int *)(p + 1) + 2;
29728b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = (int *)(p + 1) + 3;
29731da177e4SLinus Torvalds 	}
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 
2976f8572d8fSEric W. Biederman 	if (handler) {
29771da177e4SLinus Torvalds 		/* RetransTime */
29788b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
29798b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].extra1 = dev;
29801da177e4SLinus Torvalds 		/* ReachableTime */
29818b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
29828b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].extra1 = dev;
29831da177e4SLinus Torvalds 		/* RetransTime (in milliseconds)*/
29848b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
29858b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].extra1 = dev;
29861da177e4SLinus Torvalds 		/* ReachableTime (in milliseconds) */
29878b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
29888b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].extra1 = dev;
29891da177e4SLinus Torvalds 	}
29901da177e4SLinus Torvalds 
2991c3bac5a7SPavel Emelyanov 	t->dev_name = kstrdup(dev_name_source, GFP_KERNEL);
2992c3bac5a7SPavel Emelyanov 	if (!t->dev_name)
29931da177e4SLinus Torvalds 		goto free;
29941da177e4SLinus Torvalds 
2995c3bac5a7SPavel Emelyanov 	neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name;
2996c3bac5a7SPavel Emelyanov 	neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name;
29971da177e4SLinus Torvalds 
29984ab438fcSDenis V. Lunev 	t->sysctl_header =
299957da52c1SYOSHIFUJI Hideaki 		register_net_sysctl_table(neigh_parms_net(p), neigh_path, t->neigh_vars);
30003c607bbbSPavel Emelyanov 	if (!t->sysctl_header)
30011da177e4SLinus Torvalds 		goto free_procname;
30023c607bbbSPavel Emelyanov 
30031da177e4SLinus Torvalds 	p->sysctl_table = t;
30041da177e4SLinus Torvalds 	return 0;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds free_procname:
3007c3bac5a7SPavel Emelyanov 	kfree(t->dev_name);
30081da177e4SLinus Torvalds free:
30091da177e4SLinus Torvalds 	kfree(t);
30103c607bbbSPavel Emelyanov err:
30113c607bbbSPavel Emelyanov 	return -ENOBUFS;
30121da177e4SLinus Torvalds }
30130a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register);
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p)
30161da177e4SLinus Torvalds {
30171da177e4SLinus Torvalds 	if (p->sysctl_table) {
30181da177e4SLinus Torvalds 		struct neigh_sysctl_table *t = p->sysctl_table;
30191da177e4SLinus Torvalds 		p->sysctl_table = NULL;
30201da177e4SLinus Torvalds 		unregister_sysctl_table(t->sysctl_header);
3021c3bac5a7SPavel Emelyanov 		kfree(t->dev_name);
30221da177e4SLinus Torvalds 		kfree(t);
30231da177e4SLinus Torvalds 	}
30241da177e4SLinus Torvalds }
30250a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister);
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds #endif	/* CONFIG_SYSCTL */
30281da177e4SLinus Torvalds 
3029c8822a4eSThomas Graf static int __init neigh_init(void)
3030c8822a4eSThomas Graf {
3031c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
3032c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
3033c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
3034c8822a4eSThomas Graf 
3035c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3036c7ac8679SGreg Rose 		      NULL);
3037c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
3038c8822a4eSThomas Graf 
3039c8822a4eSThomas Graf 	return 0;
3040c8822a4eSThomas Graf }
3041c8822a4eSThomas Graf 
3042c8822a4eSThomas Graf subsys_initcall(neigh_init);
3043c8822a4eSThomas Graf 
3044