xref: /openbmc/linux/net/core/neighbour.c (revision b194c1f1)
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 
18e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19e005d193SJoe Perches 
205a0e3ad6STejun Heo #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/socket.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
261da177e4SLinus Torvalds #include <linux/proc_fs.h>
271da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
281da177e4SLinus Torvalds #include <linux/sysctl.h>
291da177e4SLinus Torvalds #endif
301da177e4SLinus Torvalds #include <linux/times.h>
31457c4cbcSEric W. Biederman #include <net/net_namespace.h>
321da177e4SLinus Torvalds #include <net/neighbour.h>
331da177e4SLinus Torvalds #include <net/dst.h>
341da177e4SLinus Torvalds #include <net/sock.h>
358d71740cSTom Tucker #include <net/netevent.h>
36a14a49d2SThomas Graf #include <net/netlink.h>
371da177e4SLinus Torvalds #include <linux/rtnetlink.h>
381da177e4SLinus Torvalds #include <linux/random.h>
39543537bdSPaulo Marques #include <linux/string.h>
40c3609d51Svignesh babu #include <linux/log2.h>
411d4c8c29SJiri Pirko #include <linux/inetdevice.h>
42bba24896SJiri Pirko #include <net/addrconf.h>
431da177e4SLinus Torvalds 
44d5d427cdSJoe Perches #define DEBUG
451da177e4SLinus Torvalds #define NEIGH_DEBUG 1
46d5d427cdSJoe Perches #define neigh_dbg(level, fmt, ...)		\
47d5d427cdSJoe Perches do {						\
48d5d427cdSJoe Perches 	if (level <= NEIGH_DEBUG)		\
49d5d427cdSJoe Perches 		pr_debug(fmt, ##__VA_ARGS__);	\
50d5d427cdSJoe Perches } while (0)
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define PNEIGH_HASHMASK		0xF
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg);
55d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags);
56d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh);
571da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static struct neigh_table *neigh_tables;
6045fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS
619a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops;
6245fc3b11SAmos Waterland #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds    Neighbour hash table buckets are protected with rwlock tbl->lock.
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds    - All the scans/updates to hash buckets MUST be made under this lock.
681da177e4SLinus Torvalds    - NOTHING clever should be made under this lock: no callbacks
691da177e4SLinus Torvalds      to protocol backends, no attempts to send something to network.
701da177e4SLinus Torvalds      It will result in deadlocks, if backend/driver wants to use neighbour
711da177e4SLinus Torvalds      cache.
721da177e4SLinus Torvalds    - If the entry requires some non-trivial actions, increase
731da177e4SLinus Torvalds      its reference count and release table lock.
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds    Neighbour entries are protected:
761da177e4SLinus Torvalds    - with reference count.
771da177e4SLinus Torvalds    - with rwlock neigh->lock
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds    Reference count prevents destruction.
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds    neigh->lock mainly serializes ll address data and its validity state.
821da177e4SLinus Torvalds    However, the same lock is used to protect another entry fields:
831da177e4SLinus Torvalds     - timer
841da177e4SLinus Torvalds     - resolution queue
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds    Again, nothing clever shall be made under neigh->lock,
871da177e4SLinus Torvalds    the most complicated procedure, which we allow is dev->hard_header.
881da177e4SLinus Torvalds    It is supposed, that dev->hard_header is simplistic and does
891da177e4SLinus Torvalds    not make callbacks to neighbour tables.
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds    The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
921da177e4SLinus Torvalds    list of neighbour tables. This list is used only in process context,
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock);
961da177e4SLinus Torvalds 
978f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	kfree_skb(skb);
1001da177e4SLinus Torvalds 	return -ENETDOWN;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1034f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh)
1044f494554SThomas Graf {
1054f494554SThomas Graf 	if (neigh->parms->neigh_cleanup)
1064f494554SThomas Graf 		neigh->parms->neigh_cleanup(neigh);
1074f494554SThomas Graf 
108d961db35SThomas Graf 	__neigh_notify(neigh, RTM_DELNEIGH, 0);
1094f494554SThomas Graf 	neigh_release(neigh);
1104f494554SThomas Graf }
1114f494554SThomas Graf 
1121da177e4SLinus Torvalds /*
1131da177e4SLinus Torvalds  * It is random distribution in the interval (1/2)*base...(3/2)*base.
1141da177e4SLinus Torvalds  * It corresponds to default IPv6 settings and is not overridable,
1151da177e4SLinus Torvalds  * because it is really reasonable choice.
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base)
1191da177e4SLinus Torvalds {
12063862b5bSAruna-Hewapathirane 	return base ? (prandom_u32() % base) + (base >> 1) : 0;
1211da177e4SLinus Torvalds }
1220a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	int shrunk = 0;
1281da177e4SLinus Torvalds 	int i;
129d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
134d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
135d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
136cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
137767e97e1SEric Dumazet 		struct neighbour *n;
138767e97e1SEric Dumazet 		struct neighbour __rcu **np;
1391da177e4SLinus Torvalds 
140d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
141767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
142767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
1431da177e4SLinus Torvalds 			/* Neighbour record may be discarded if:
1441da177e4SLinus Torvalds 			 * - nobody refers to it.
1451da177e4SLinus Torvalds 			 * - it is not permanent
1461da177e4SLinus Torvalds 			 */
1471da177e4SLinus Torvalds 			write_lock(&n->lock);
1481da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
1491da177e4SLinus Torvalds 			    !(n->nud_state & NUD_PERMANENT)) {
150767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
151767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
152767e97e1SEric Dumazet 						  lockdep_is_held(&tbl->lock)));
1531da177e4SLinus Torvalds 				n->dead = 1;
1541da177e4SLinus Torvalds 				shrunk	= 1;
1551da177e4SLinus Torvalds 				write_unlock(&n->lock);
1564f494554SThomas Graf 				neigh_cleanup_and_release(n);
1571da177e4SLinus Torvalds 				continue;
1581da177e4SLinus Torvalds 			}
1591da177e4SLinus Torvalds 			write_unlock(&n->lock);
1601da177e4SLinus Torvalds 			np = &n->next;
1611da177e4SLinus Torvalds 		}
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	tbl->last_flush = jiffies;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	return shrunk;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
171a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when)
172a43d8994SPavel Emelyanov {
173a43d8994SPavel Emelyanov 	neigh_hold(n);
174a43d8994SPavel Emelyanov 	if (unlikely(mod_timer(&n->timer, when))) {
175a43d8994SPavel Emelyanov 		printk("NEIGH: BUG, double timer add, state is %x\n",
176a43d8994SPavel Emelyanov 		       n->nud_state);
177a43d8994SPavel Emelyanov 		dump_stack();
178a43d8994SPavel Emelyanov 	}
179a43d8994SPavel Emelyanov }
180a43d8994SPavel Emelyanov 
1811da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	if ((n->nud_state & NUD_IN_TIMER) &&
1841da177e4SLinus Torvalds 	    del_timer(&n->timer)) {
1851da177e4SLinus Torvalds 		neigh_release(n);
1861da177e4SLinus Torvalds 		return 1;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	struct sk_buff *skb;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL) {
1961da177e4SLinus Torvalds 		dev_put(skb->dev);
1971da177e4SLinus Torvalds 		kfree_skb(skb);
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
20149636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	int i;
204d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
2051da177e4SLinus Torvalds 
206d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
207d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
208d6bf7817SEric Dumazet 
209cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
210767e97e1SEric Dumazet 		struct neighbour *n;
211767e97e1SEric Dumazet 		struct neighbour __rcu **np = &nht->hash_buckets[i];
2121da177e4SLinus Torvalds 
213767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
214767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
2151da177e4SLinus Torvalds 			if (dev && n->dev != dev) {
2161da177e4SLinus Torvalds 				np = &n->next;
2171da177e4SLinus Torvalds 				continue;
2181da177e4SLinus Torvalds 			}
219767e97e1SEric Dumazet 			rcu_assign_pointer(*np,
220767e97e1SEric Dumazet 				   rcu_dereference_protected(n->next,
221767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
2221da177e4SLinus Torvalds 			write_lock(&n->lock);
2231da177e4SLinus Torvalds 			neigh_del_timer(n);
2241da177e4SLinus Torvalds 			n->dead = 1;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) != 1) {
2271da177e4SLinus Torvalds 				/* The most unpleasant situation.
2281da177e4SLinus Torvalds 				   We must destroy neighbour entry,
2291da177e4SLinus Torvalds 				   but someone still uses it.
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 				   The destroy will be delayed until
2321da177e4SLinus Torvalds 				   the last user releases us, but
2331da177e4SLinus Torvalds 				   we must kill timers etc. and move
2341da177e4SLinus Torvalds 				   it to safe state.
2351da177e4SLinus Torvalds 				 */
236c9ab4d85SEric Dumazet 				__skb_queue_purge(&n->arp_queue);
2378b5c171bSEric Dumazet 				n->arp_queue_len_bytes = 0;
2381da177e4SLinus Torvalds 				n->output = neigh_blackhole;
2391da177e4SLinus Torvalds 				if (n->nud_state & NUD_VALID)
2401da177e4SLinus Torvalds 					n->nud_state = NUD_NOARP;
2411da177e4SLinus Torvalds 				else
2421da177e4SLinus Torvalds 					n->nud_state = NUD_NONE;
243d5d427cdSJoe Perches 				neigh_dbg(2, "neigh %p is stray\n", n);
2441da177e4SLinus Torvalds 			}
2451da177e4SLinus Torvalds 			write_unlock(&n->lock);
2464f494554SThomas Graf 			neigh_cleanup_and_release(n);
2471da177e4SLinus Torvalds 		}
2481da177e4SLinus Torvalds 	}
24949636bb1SHerbert Xu }
2501da177e4SLinus Torvalds 
25149636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
25249636bb1SHerbert Xu {
25349636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
25449636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
25549636bb1SHerbert Xu 	write_unlock_bh(&tbl->lock);
25649636bb1SHerbert Xu }
2570a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr);
25849636bb1SHerbert Xu 
25949636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
26049636bb1SHerbert Xu {
26149636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
26249636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
2631da177e4SLinus Torvalds 	pneigh_ifdown(tbl, dev);
2641da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
2671da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
2681da177e4SLinus Torvalds 	return 0;
2691da177e4SLinus Torvalds }
2700a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown);
2711da177e4SLinus Torvalds 
272596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct neighbour *n = NULL;
2751da177e4SLinus Torvalds 	unsigned long now = jiffies;
2761da177e4SLinus Torvalds 	int entries;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	entries = atomic_inc_return(&tbl->entries) - 1;
2791da177e4SLinus Torvalds 	if (entries >= tbl->gc_thresh3 ||
2801da177e4SLinus Torvalds 	    (entries >= tbl->gc_thresh2 &&
2811da177e4SLinus Torvalds 	     time_after(now, tbl->last_flush + 5 * HZ))) {
2821da177e4SLinus Torvalds 		if (!neigh_forced_gc(tbl) &&
2831da177e4SLinus Torvalds 		    entries >= tbl->gc_thresh3)
2841da177e4SLinus Torvalds 			goto out_entries;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
28708433effSYOSHIFUJI Hideaki / 吉藤英明 	n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
2881da177e4SLinus Torvalds 	if (!n)
2891da177e4SLinus Torvalds 		goto out_entries;
2901da177e4SLinus Torvalds 
291c9ab4d85SEric Dumazet 	__skb_queue_head_init(&n->arp_queue);
2921da177e4SLinus Torvalds 	rwlock_init(&n->lock);
2930ed8ddf4SEric Dumazet 	seqlock_init(&n->ha_lock);
2941da177e4SLinus Torvalds 	n->updated	  = n->used = now;
2951da177e4SLinus Torvalds 	n->nud_state	  = NUD_NONE;
2961da177e4SLinus Torvalds 	n->output	  = neigh_blackhole;
297f6b72b62SDavid S. Miller 	seqlock_init(&n->hh.hh_lock);
2981da177e4SLinus Torvalds 	n->parms	  = neigh_parms_clone(&tbl->parms);
299b24b8a24SPavel Emelyanov 	setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, allocs);
3021da177e4SLinus Torvalds 	n->tbl		  = tbl;
3031da177e4SLinus Torvalds 	atomic_set(&n->refcnt, 1);
3041da177e4SLinus Torvalds 	n->dead		  = 1;
3051da177e4SLinus Torvalds out:
3061da177e4SLinus Torvalds 	return n;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds out_entries:
3091da177e4SLinus Torvalds 	atomic_dec(&tbl->entries);
3101da177e4SLinus Torvalds 	goto out;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3132c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x)
3142c2aba6cSDavid S. Miller {
3152c2aba6cSDavid S. Miller 	get_random_bytes(x, sizeof(*x));
3162c2aba6cSDavid S. Miller 	*x |= 1;
3172c2aba6cSDavid S. Miller }
3182c2aba6cSDavid S. Miller 
319cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
3201da177e4SLinus Torvalds {
321cd089336SDavid S. Miller 	size_t size = (1 << shift) * sizeof(struct neighbour *);
322d6bf7817SEric Dumazet 	struct neigh_hash_table *ret;
3236193d2beSEric Dumazet 	struct neighbour __rcu **buckets;
3242c2aba6cSDavid S. Miller 	int i;
3251da177e4SLinus Torvalds 
326d6bf7817SEric Dumazet 	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
327d6bf7817SEric Dumazet 	if (!ret)
328d6bf7817SEric Dumazet 		return NULL;
329d6bf7817SEric Dumazet 	if (size <= PAGE_SIZE)
330d6bf7817SEric Dumazet 		buckets = kzalloc(size, GFP_ATOMIC);
331d6bf7817SEric Dumazet 	else
3326193d2beSEric Dumazet 		buckets = (struct neighbour __rcu **)
333d6bf7817SEric Dumazet 			  __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
334d6bf7817SEric Dumazet 					   get_order(size));
335d6bf7817SEric Dumazet 	if (!buckets) {
336d6bf7817SEric Dumazet 		kfree(ret);
337d6bf7817SEric Dumazet 		return NULL;
3381da177e4SLinus Torvalds 	}
3396193d2beSEric Dumazet 	ret->hash_buckets = buckets;
340cd089336SDavid S. Miller 	ret->hash_shift = shift;
3412c2aba6cSDavid S. Miller 	for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
3422c2aba6cSDavid S. Miller 		neigh_get_hash_rnd(&ret->hash_rnd[i]);
3431da177e4SLinus Torvalds 	return ret;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
346d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head)
3471da177e4SLinus Torvalds {
348d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = container_of(head,
349d6bf7817SEric Dumazet 						    struct neigh_hash_table,
350d6bf7817SEric Dumazet 						    rcu);
351cd089336SDavid S. Miller 	size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
3526193d2beSEric Dumazet 	struct neighbour __rcu **buckets = nht->hash_buckets;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	if (size <= PAGE_SIZE)
355d6bf7817SEric Dumazet 		kfree(buckets);
3561da177e4SLinus Torvalds 	else
357d6bf7817SEric Dumazet 		free_pages((unsigned long)buckets, get_order(size));
358d6bf7817SEric Dumazet 	kfree(nht);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
361d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
362cd089336SDavid S. Miller 						unsigned long new_shift)
3631da177e4SLinus Torvalds {
364d6bf7817SEric Dumazet 	unsigned int i, hash;
365d6bf7817SEric Dumazet 	struct neigh_hash_table *new_nht, *old_nht;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
3681da177e4SLinus Torvalds 
369d6bf7817SEric Dumazet 	old_nht = rcu_dereference_protected(tbl->nht,
370d6bf7817SEric Dumazet 					    lockdep_is_held(&tbl->lock));
371cd089336SDavid S. Miller 	new_nht = neigh_hash_alloc(new_shift);
372d6bf7817SEric Dumazet 	if (!new_nht)
373d6bf7817SEric Dumazet 		return old_nht;
3741da177e4SLinus Torvalds 
375cd089336SDavid S. Miller 	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
3761da177e4SLinus Torvalds 		struct neighbour *n, *next;
3771da177e4SLinus Torvalds 
378767e97e1SEric Dumazet 		for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
379767e97e1SEric Dumazet 						   lockdep_is_held(&tbl->lock));
380d6bf7817SEric Dumazet 		     n != NULL;
381d6bf7817SEric Dumazet 		     n = next) {
382d6bf7817SEric Dumazet 			hash = tbl->hash(n->primary_key, n->dev,
383d6bf7817SEric Dumazet 					 new_nht->hash_rnd);
3841da177e4SLinus Torvalds 
385cd089336SDavid S. Miller 			hash >>= (32 - new_nht->hash_shift);
386767e97e1SEric Dumazet 			next = rcu_dereference_protected(n->next,
387767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock));
3881da177e4SLinus Torvalds 
389767e97e1SEric Dumazet 			rcu_assign_pointer(n->next,
390767e97e1SEric Dumazet 					   rcu_dereference_protected(
391767e97e1SEric Dumazet 						new_nht->hash_buckets[hash],
392767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
393767e97e1SEric Dumazet 			rcu_assign_pointer(new_nht->hash_buckets[hash], n);
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
397d6bf7817SEric Dumazet 	rcu_assign_pointer(tbl->nht, new_nht);
398d6bf7817SEric Dumazet 	call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
399d6bf7817SEric Dumazet 	return new_nht;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
4031da177e4SLinus Torvalds 			       struct net_device *dev)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct neighbour *n;
4061da177e4SLinus Torvalds 	int key_len = tbl->key_len;
407bc4bf5f3SPavel Emelyanov 	u32 hash_val;
408d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4111da177e4SLinus Torvalds 
412d6bf7817SEric Dumazet 	rcu_read_lock_bh();
413d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
414cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
415767e97e1SEric Dumazet 
416767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
417767e97e1SEric Dumazet 	     n != NULL;
418767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
4191da177e4SLinus Torvalds 		if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
420767e97e1SEric Dumazet 			if (!atomic_inc_not_zero(&n->refcnt))
421767e97e1SEric Dumazet 				n = NULL;
4221da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
4231da177e4SLinus Torvalds 			break;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 	}
426767e97e1SEric Dumazet 
427d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4281da177e4SLinus Torvalds 	return n;
4291da177e4SLinus Torvalds }
4300a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup);
4311da177e4SLinus Torvalds 
432426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
433426b5303SEric W. Biederman 				     const void *pkey)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	struct neighbour *n;
4361da177e4SLinus Torvalds 	int key_len = tbl->key_len;
437bc4bf5f3SPavel Emelyanov 	u32 hash_val;
438d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4411da177e4SLinus Torvalds 
442d6bf7817SEric Dumazet 	rcu_read_lock_bh();
443d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
444cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
445767e97e1SEric Dumazet 
446767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
447767e97e1SEric Dumazet 	     n != NULL;
448767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
449426b5303SEric W. Biederman 		if (!memcmp(n->primary_key, pkey, key_len) &&
450878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(n->dev), net)) {
451767e97e1SEric Dumazet 			if (!atomic_inc_not_zero(&n->refcnt))
452767e97e1SEric Dumazet 				n = NULL;
4531da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
4541da177e4SLinus Torvalds 			break;
4551da177e4SLinus Torvalds 		}
4561da177e4SLinus Torvalds 	}
457767e97e1SEric Dumazet 
458d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4591da177e4SLinus Torvalds 	return n;
4601da177e4SLinus Torvalds }
4610a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev);
4621da177e4SLinus Torvalds 
463a263b309SDavid S. Miller struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
464a263b309SDavid S. Miller 				 struct net_device *dev, bool want_ref)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	u32 hash_val;
4671da177e4SLinus Torvalds 	int key_len = tbl->key_len;
4681da177e4SLinus Torvalds 	int error;
469596b9b68SDavid Miller 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
470d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (!n) {
4731da177e4SLinus Torvalds 		rc = ERR_PTR(-ENOBUFS);
4741da177e4SLinus Torvalds 		goto out;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	memcpy(n->primary_key, pkey, key_len);
4781da177e4SLinus Torvalds 	n->dev = dev;
4791da177e4SLinus Torvalds 	dev_hold(dev);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* Protocol specific setup. */
4821da177e4SLinus Torvalds 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
4831da177e4SLinus Torvalds 		rc = ERR_PTR(error);
4841da177e4SLinus Torvalds 		goto out_neigh_release;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 
487da6a8fa0SDavid Miller 	if (dev->netdev_ops->ndo_neigh_construct) {
488da6a8fa0SDavid Miller 		error = dev->netdev_ops->ndo_neigh_construct(n);
489da6a8fa0SDavid Miller 		if (error < 0) {
490da6a8fa0SDavid Miller 			rc = ERR_PTR(error);
491da6a8fa0SDavid Miller 			goto out_neigh_release;
492da6a8fa0SDavid Miller 		}
493da6a8fa0SDavid Miller 	}
494da6a8fa0SDavid Miller 
495447f2191SDavid S. Miller 	/* Device specific setup. */
496447f2191SDavid S. Miller 	if (n->parms->neigh_setup &&
497447f2191SDavid S. Miller 	    (error = n->parms->neigh_setup(n)) < 0) {
498447f2191SDavid S. Miller 		rc = ERR_PTR(error);
499447f2191SDavid S. Miller 		goto out_neigh_release;
500447f2191SDavid S. Miller 	}
501447f2191SDavid S. Miller 
5021f9248e5SJiri Pirko 	n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
505d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
506d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
5071da177e4SLinus Torvalds 
508cd089336SDavid S. Miller 	if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
509cd089336SDavid S. Miller 		nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
5101da177e4SLinus Torvalds 
511cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if (n->parms->dead) {
5141da177e4SLinus Torvalds 		rc = ERR_PTR(-EINVAL);
5151da177e4SLinus Torvalds 		goto out_tbl_unlock;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 
518767e97e1SEric Dumazet 	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
519767e97e1SEric Dumazet 					    lockdep_is_held(&tbl->lock));
520767e97e1SEric Dumazet 	     n1 != NULL;
521767e97e1SEric Dumazet 	     n1 = rcu_dereference_protected(n1->next,
522767e97e1SEric Dumazet 			lockdep_is_held(&tbl->lock))) {
5231da177e4SLinus Torvalds 		if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
524a263b309SDavid S. Miller 			if (want_ref)
5251da177e4SLinus Torvalds 				neigh_hold(n1);
5261da177e4SLinus Torvalds 			rc = n1;
5271da177e4SLinus Torvalds 			goto out_tbl_unlock;
5281da177e4SLinus Torvalds 		}
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	n->dead = 0;
532a263b309SDavid S. Miller 	if (want_ref)
5331da177e4SLinus Torvalds 		neigh_hold(n);
534767e97e1SEric Dumazet 	rcu_assign_pointer(n->next,
535767e97e1SEric Dumazet 			   rcu_dereference_protected(nht->hash_buckets[hash_val],
536767e97e1SEric Dumazet 						     lockdep_is_held(&tbl->lock)));
537767e97e1SEric Dumazet 	rcu_assign_pointer(nht->hash_buckets[hash_val], n);
5381da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
539d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is created\n", n);
5401da177e4SLinus Torvalds 	rc = n;
5411da177e4SLinus Torvalds out:
5421da177e4SLinus Torvalds 	return rc;
5431da177e4SLinus Torvalds out_tbl_unlock:
5441da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
5451da177e4SLinus Torvalds out_neigh_release:
5461da177e4SLinus Torvalds 	neigh_release(n);
5471da177e4SLinus Torvalds 	goto out;
5481da177e4SLinus Torvalds }
549a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create);
5501da177e4SLinus Torvalds 
551be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len)
552fa86d322SPavel Emelyanov {
553fa86d322SPavel Emelyanov 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
554fa86d322SPavel Emelyanov 	hash_val ^= (hash_val >> 16);
555fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 8;
556fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 4;
557fa86d322SPavel Emelyanov 	hash_val &= PNEIGH_HASHMASK;
558be01d655SYOSHIFUJI Hideaki 	return hash_val;
559fa86d322SPavel Emelyanov }
560fa86d322SPavel Emelyanov 
561be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
562be01d655SYOSHIFUJI Hideaki 					      struct net *net,
563be01d655SYOSHIFUJI Hideaki 					      const void *pkey,
564be01d655SYOSHIFUJI Hideaki 					      int key_len,
565be01d655SYOSHIFUJI Hideaki 					      struct net_device *dev)
566be01d655SYOSHIFUJI Hideaki {
567be01d655SYOSHIFUJI Hideaki 	while (n) {
568be01d655SYOSHIFUJI Hideaki 		if (!memcmp(n->key, pkey, key_len) &&
569be01d655SYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net) &&
570be01d655SYOSHIFUJI Hideaki 		    (n->dev == dev || !n->dev))
571fa86d322SPavel Emelyanov 			return n;
572be01d655SYOSHIFUJI Hideaki 		n = n->next;
573be01d655SYOSHIFUJI Hideaki 	}
574be01d655SYOSHIFUJI Hideaki 	return NULL;
575be01d655SYOSHIFUJI Hideaki }
576be01d655SYOSHIFUJI Hideaki 
577be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
578be01d655SYOSHIFUJI Hideaki 		struct net *net, const void *pkey, struct net_device *dev)
579be01d655SYOSHIFUJI Hideaki {
580be01d655SYOSHIFUJI Hideaki 	int key_len = tbl->key_len;
581be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
582be01d655SYOSHIFUJI Hideaki 
583be01d655SYOSHIFUJI Hideaki 	return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
584be01d655SYOSHIFUJI Hideaki 				 net, pkey, key_len, dev);
585fa86d322SPavel Emelyanov }
5860a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup);
587fa86d322SPavel Emelyanov 
588426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
589426b5303SEric W. Biederman 				    struct net *net, const void *pkey,
5901da177e4SLinus Torvalds 				    struct net_device *dev, int creat)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	struct pneigh_entry *n;
5931da177e4SLinus Torvalds 	int key_len = tbl->key_len;
594be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
597be01d655SYOSHIFUJI Hideaki 	n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
598be01d655SYOSHIFUJI Hideaki 			      net, pkey, key_len, dev);
599be01d655SYOSHIFUJI Hideaki 	read_unlock_bh(&tbl->lock);
6001da177e4SLinus Torvalds 
601be01d655SYOSHIFUJI Hideaki 	if (n || !creat)
6021da177e4SLinus Torvalds 		goto out;
6031da177e4SLinus Torvalds 
6044ae28944SPavel Emelyanov 	ASSERT_RTNL();
6054ae28944SPavel Emelyanov 
6061da177e4SLinus Torvalds 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
6071da177e4SLinus Torvalds 	if (!n)
6081da177e4SLinus Torvalds 		goto out;
6091da177e4SLinus Torvalds 
610e42ea986SEric Dumazet 	write_pnet(&n->net, hold_net(net));
6111da177e4SLinus Torvalds 	memcpy(n->key, pkey, key_len);
6121da177e4SLinus Torvalds 	n->dev = dev;
6131da177e4SLinus Torvalds 	if (dev)
6141da177e4SLinus Torvalds 		dev_hold(dev);
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	if (tbl->pconstructor && tbl->pconstructor(n)) {
6171da177e4SLinus Torvalds 		if (dev)
6181da177e4SLinus Torvalds 			dev_put(dev);
619da12f735SDenis V. Lunev 		release_net(net);
6201da177e4SLinus Torvalds 		kfree(n);
6211da177e4SLinus Torvalds 		n = NULL;
6221da177e4SLinus Torvalds 		goto out;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6261da177e4SLinus Torvalds 	n->next = tbl->phash_buckets[hash_val];
6271da177e4SLinus Torvalds 	tbl->phash_buckets[hash_val] = n;
6281da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6291da177e4SLinus Torvalds out:
6301da177e4SLinus Torvalds 	return n;
6311da177e4SLinus Torvalds }
6320a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 
635426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
6361da177e4SLinus Torvalds 		  struct net_device *dev)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6391da177e4SLinus Torvalds 	int key_len = tbl->key_len;
640be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6431da177e4SLinus Torvalds 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
6441da177e4SLinus Torvalds 	     np = &n->next) {
645426b5303SEric W. Biederman 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
646878628fbSYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net)) {
6471da177e4SLinus Torvalds 			*np = n->next;
6481da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
6491da177e4SLinus Torvalds 			if (tbl->pdestructor)
6501da177e4SLinus Torvalds 				tbl->pdestructor(n);
6511da177e4SLinus Torvalds 			if (n->dev)
6521da177e4SLinus Torvalds 				dev_put(n->dev);
65357da52c1SYOSHIFUJI Hideaki 			release_net(pneigh_net(n));
6541da177e4SLinus Torvalds 			kfree(n);
6551da177e4SLinus Torvalds 			return 0;
6561da177e4SLinus Torvalds 		}
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6591da177e4SLinus Torvalds 	return -ENOENT;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6651da177e4SLinus Torvalds 	u32 h;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
6681da177e4SLinus Torvalds 		np = &tbl->phash_buckets[h];
6691da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
6701da177e4SLinus Torvalds 			if (!dev || n->dev == dev) {
6711da177e4SLinus Torvalds 				*np = n->next;
6721da177e4SLinus Torvalds 				if (tbl->pdestructor)
6731da177e4SLinus Torvalds 					tbl->pdestructor(n);
6741da177e4SLinus Torvalds 				if (n->dev)
6751da177e4SLinus Torvalds 					dev_put(n->dev);
67657da52c1SYOSHIFUJI Hideaki 				release_net(pneigh_net(n));
6771da177e4SLinus Torvalds 				kfree(n);
6781da177e4SLinus Torvalds 				continue;
6791da177e4SLinus Torvalds 			}
6801da177e4SLinus Torvalds 			np = &n->next;
6811da177e4SLinus Torvalds 		}
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds 	return -ENOENT;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
68606f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms);
68706f0511dSDenis V. Lunev 
68806f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms)
68906f0511dSDenis V. Lunev {
69006f0511dSDenis V. Lunev 	if (atomic_dec_and_test(&parms->refcnt))
69106f0511dSDenis V. Lunev 		neigh_parms_destroy(parms);
69206f0511dSDenis V. Lunev }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /*
6951da177e4SLinus Torvalds  *	neighbour must already be out of the table;
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh)
6991da177e4SLinus Torvalds {
700da6a8fa0SDavid Miller 	struct net_device *dev = neigh->dev;
701da6a8fa0SDavid Miller 
7021da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	if (!neigh->dead) {
705e005d193SJoe Perches 		pr_warn("Destroying alive neighbour %p\n", neigh);
7061da177e4SLinus Torvalds 		dump_stack();
7071da177e4SLinus Torvalds 		return;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if (neigh_del_timer(neigh))
711e005d193SJoe Perches 		pr_warn("Impossible event\n");
7121da177e4SLinus Torvalds 
713c9ab4d85SEric Dumazet 	write_lock_bh(&neigh->lock);
714c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
715c9ab4d85SEric Dumazet 	write_unlock_bh(&neigh->lock);
7168b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
7171da177e4SLinus Torvalds 
718447f2191SDavid S. Miller 	if (dev->netdev_ops->ndo_neigh_destroy)
719447f2191SDavid S. Miller 		dev->netdev_ops->ndo_neigh_destroy(neigh);
720447f2191SDavid S. Miller 
721da6a8fa0SDavid Miller 	dev_put(dev);
7221da177e4SLinus Torvalds 	neigh_parms_put(neigh->parms);
7231da177e4SLinus Torvalds 
724d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is destroyed\n", neigh);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	atomic_dec(&neigh->tbl->entries);
7275b8b0060SDavid Miller 	kfree_rcu(neigh, rcu);
7281da177e4SLinus Torvalds }
7290a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /* Neighbour state is suspicious;
7321da177e4SLinus Torvalds    disable fast path.
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds    Called with write_locked neigh.
7351da177e4SLinus Torvalds  */
7361da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh)
7371da177e4SLinus Torvalds {
738d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is suspected\n", neigh);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	neigh->output = neigh->ops->output;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /* Neighbour state is OK;
7441da177e4SLinus Torvalds    enable fast path.
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds    Called with write_locked neigh.
7471da177e4SLinus Torvalds  */
7481da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh)
7491da177e4SLinus Torvalds {
750d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is connected\n", neigh);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	neigh->output = neigh->ops->connected_output;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
755e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work)
7561da177e4SLinus Torvalds {
757e4c4e448SEric Dumazet 	struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
758767e97e1SEric Dumazet 	struct neighbour *n;
759767e97e1SEric Dumazet 	struct neighbour __rcu **np;
760e4c4e448SEric Dumazet 	unsigned int i;
761d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
7641da177e4SLinus Torvalds 
765e4c4e448SEric Dumazet 	write_lock_bh(&tbl->lock);
766d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
767d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
7681da177e4SLinus Torvalds 
7692724680bSYOSHIFUJI Hideaki / 吉藤英明 	if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
7702724680bSYOSHIFUJI Hideaki / 吉藤英明 		goto out;
7712724680bSYOSHIFUJI Hideaki / 吉藤英明 
7721da177e4SLinus Torvalds 	/*
7731da177e4SLinus Torvalds 	 *	periodically recompute ReachableTime from random function
7741da177e4SLinus Torvalds 	 */
7751da177e4SLinus Torvalds 
776e4c4e448SEric Dumazet 	if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
7771da177e4SLinus Torvalds 		struct neigh_parms *p;
778e4c4e448SEric Dumazet 		tbl->last_rand = jiffies;
7791da177e4SLinus Torvalds 		for (p = &tbl->parms; p; p = p->next)
7801da177e4SLinus Torvalds 			p->reachable_time =
7811f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
784cd089336SDavid S. Miller 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
785d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
7861da177e4SLinus Torvalds 
787767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
788767e97e1SEric Dumazet 				lockdep_is_held(&tbl->lock))) != NULL) {
7891da177e4SLinus Torvalds 			unsigned int state;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 			write_lock(&n->lock);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 			state = n->nud_state;
7941da177e4SLinus Torvalds 			if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
7951da177e4SLinus Torvalds 				write_unlock(&n->lock);
7961da177e4SLinus Torvalds 				goto next_elt;
7971da177e4SLinus Torvalds 			}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 			if (time_before(n->used, n->confirmed))
8001da177e4SLinus Torvalds 				n->used = n->confirmed;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
8031da177e4SLinus Torvalds 			    (state == NUD_FAILED ||
8041f9248e5SJiri Pirko 			     time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
8051da177e4SLinus Torvalds 				*np = n->next;
8061da177e4SLinus Torvalds 				n->dead = 1;
8071da177e4SLinus Torvalds 				write_unlock(&n->lock);
8084f494554SThomas Graf 				neigh_cleanup_and_release(n);
8091da177e4SLinus Torvalds 				continue;
8101da177e4SLinus Torvalds 			}
8111da177e4SLinus Torvalds 			write_unlock(&n->lock);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds next_elt:
8141da177e4SLinus Torvalds 			np = &n->next;
8151da177e4SLinus Torvalds 		}
816e4c4e448SEric Dumazet 		/*
817e4c4e448SEric Dumazet 		 * It's fine to release lock here, even if hash table
818e4c4e448SEric Dumazet 		 * grows while we are preempted.
819e4c4e448SEric Dumazet 		 */
820e4c4e448SEric Dumazet 		write_unlock_bh(&tbl->lock);
821e4c4e448SEric Dumazet 		cond_resched();
822e4c4e448SEric Dumazet 		write_lock_bh(&tbl->lock);
82384338a6cSMichel Machado 		nht = rcu_dereference_protected(tbl->nht,
82484338a6cSMichel Machado 						lockdep_is_held(&tbl->lock));
825e4c4e448SEric Dumazet 	}
8262724680bSYOSHIFUJI Hideaki / 吉藤英明 out:
8271f9248e5SJiri Pirko 	/* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
8281f9248e5SJiri Pirko 	 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
8291f9248e5SJiri Pirko 	 * BASE_REACHABLE_TIME.
8301da177e4SLinus Torvalds 	 */
831f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
8321f9248e5SJiri Pirko 			      NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
833e4c4e448SEric Dumazet 	write_unlock_bh(&tbl->lock);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	struct neigh_parms *p = n->parms;
839a02cec21SEric Dumazet 	return (n->nud_state & NUD_PROBE) ?
8401f9248e5SJiri Pirko 		NEIGH_VAR(p, UCAST_PROBES) :
8411f9248e5SJiri Pirko 		NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
8421f9248e5SJiri Pirko 		NEIGH_VAR(p, MCAST_PROBES);
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8455ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh)
8460a141509SEric Dumazet 	__releases(neigh->lock)
8470a141509SEric Dumazet 	__acquires(neigh->lock)
8485ef12d98STimo Teras {
8495ef12d98STimo Teras 	struct sk_buff *skb;
8505ef12d98STimo Teras 
8515ef12d98STimo Teras 	NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
852d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is failed\n", neigh);
8535ef12d98STimo Teras 	neigh->updated = jiffies;
8545ef12d98STimo Teras 
8555ef12d98STimo Teras 	/* It is very thin place. report_unreachable is very complicated
8565ef12d98STimo Teras 	   routine. Particularly, it can hit the same neighbour entry!
8575ef12d98STimo Teras 
8585ef12d98STimo Teras 	   So that, we try to be accurate and avoid dead loop. --ANK
8595ef12d98STimo Teras 	 */
8605ef12d98STimo Teras 	while (neigh->nud_state == NUD_FAILED &&
8615ef12d98STimo Teras 	       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
8625ef12d98STimo Teras 		write_unlock(&neigh->lock);
8635ef12d98STimo Teras 		neigh->ops->error_report(neigh, skb);
8645ef12d98STimo Teras 		write_lock(&neigh->lock);
8655ef12d98STimo Teras 	}
866c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
8678b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
8685ef12d98STimo Teras }
8695ef12d98STimo Teras 
870cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh)
871cd28ca0aSEric Dumazet 	__releases(neigh->lock)
872cd28ca0aSEric Dumazet {
8734ed377e3SHannes Frederic Sowa 	struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
874cd28ca0aSEric Dumazet 	/* keep skb alive even if arp_queue overflows */
875cd28ca0aSEric Dumazet 	if (skb)
876cd28ca0aSEric Dumazet 		skb = skb_copy(skb, GFP_ATOMIC);
877cd28ca0aSEric Dumazet 	write_unlock(&neigh->lock);
878cd28ca0aSEric Dumazet 	neigh->ops->solicit(neigh, skb);
879cd28ca0aSEric Dumazet 	atomic_inc(&neigh->probes);
880cd28ca0aSEric Dumazet 	kfree_skb(skb);
881cd28ca0aSEric Dumazet }
882cd28ca0aSEric Dumazet 
8831da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	unsigned long now, next;
8881da177e4SLinus Torvalds 	struct neighbour *neigh = (struct neighbour *)arg;
88995c96174SEric Dumazet 	unsigned int state;
8901da177e4SLinus Torvalds 	int notify = 0;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	write_lock(&neigh->lock);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	state = neigh->nud_state;
8951da177e4SLinus Torvalds 	now = jiffies;
8961da177e4SLinus Torvalds 	next = now + HZ;
8971da177e4SLinus Torvalds 
898045f7b3bSDavid S. Miller 	if (!(state & NUD_IN_TIMER))
8991da177e4SLinus Torvalds 		goto out;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	if (state & NUD_REACHABLE) {
9021da177e4SLinus Torvalds 		if (time_before_eq(now,
9031da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->reachable_time)) {
904d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is still alive\n", neigh);
9051da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9061da177e4SLinus Torvalds 		} else if (time_before_eq(now,
9071f9248e5SJiri Pirko 					  neigh->used +
9081f9248e5SJiri Pirko 					  NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
909d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is delayed\n", neigh);
9101da177e4SLinus Torvalds 			neigh->nud_state = NUD_DELAY;
911955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9121da177e4SLinus Torvalds 			neigh_suspect(neigh);
9131f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
9141da177e4SLinus Torvalds 		} else {
915d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is suspected\n", neigh);
9161da177e4SLinus Torvalds 			neigh->nud_state = NUD_STALE;
917955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9181da177e4SLinus Torvalds 			neigh_suspect(neigh);
9198d71740cSTom Tucker 			notify = 1;
9201da177e4SLinus Torvalds 		}
9211da177e4SLinus Torvalds 	} else if (state & NUD_DELAY) {
9221da177e4SLinus Torvalds 		if (time_before_eq(now,
9231f9248e5SJiri Pirko 				   neigh->confirmed +
9241f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
925d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is now reachable\n", neigh);
9261da177e4SLinus Torvalds 			neigh->nud_state = NUD_REACHABLE;
927955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9281da177e4SLinus Torvalds 			neigh_connect(neigh);
9298d71740cSTom Tucker 			notify = 1;
9301da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9311da177e4SLinus Torvalds 		} else {
932d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is probed\n", neigh);
9331da177e4SLinus Torvalds 			neigh->nud_state = NUD_PROBE;
934955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9351da177e4SLinus Torvalds 			atomic_set(&neigh->probes, 0);
9361f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
9371da177e4SLinus Torvalds 		}
9381da177e4SLinus Torvalds 	} else {
9391da177e4SLinus Torvalds 		/* NUD_PROBE|NUD_INCOMPLETE */
9401f9248e5SJiri Pirko 		next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
9441da177e4SLinus Torvalds 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
9451da177e4SLinus Torvalds 		neigh->nud_state = NUD_FAILED;
9461da177e4SLinus Torvalds 		notify = 1;
9475ef12d98STimo Teras 		neigh_invalidate(neigh);
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	if (neigh->nud_state & NUD_IN_TIMER) {
9511da177e4SLinus Torvalds 		if (time_before(next, jiffies + HZ/2))
9521da177e4SLinus Torvalds 			next = jiffies + HZ/2;
9536fb9974fSHerbert Xu 		if (!mod_timer(&neigh->timer, next))
9546fb9974fSHerbert Xu 			neigh_hold(neigh);
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
957cd28ca0aSEric Dumazet 		neigh_probe(neigh);
9589ff56607SDavid S. Miller 	} else {
9591da177e4SLinus Torvalds out:
9601da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
9619ff56607SDavid S. Miller 	}
9621da177e4SLinus Torvalds 
963d961db35SThomas Graf 	if (notify)
964d961db35SThomas Graf 		neigh_update_notify(neigh);
965d961db35SThomas Graf 
9661da177e4SLinus Torvalds 	neigh_release(neigh);
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	int rc;
972cd28ca0aSEric Dumazet 	bool immediate_probe = false;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	rc = 0;
9771da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
9781da177e4SLinus Torvalds 		goto out_unlock_bh;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
9811f9248e5SJiri Pirko 		if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
9821f9248e5SJiri Pirko 		    NEIGH_VAR(neigh->parms, APP_PROBES)) {
983cd28ca0aSEric Dumazet 			unsigned long next, now = jiffies;
984cd28ca0aSEric Dumazet 
9851f9248e5SJiri Pirko 			atomic_set(&neigh->probes,
9861f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, UCAST_PROBES));
9871da177e4SLinus Torvalds 			neigh->nud_state     = NUD_INCOMPLETE;
988cd28ca0aSEric Dumazet 			neigh->updated = now;
9891f9248e5SJiri Pirko 			next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
9901f9248e5SJiri Pirko 					 HZ/2);
991cd28ca0aSEric Dumazet 			neigh_add_timer(neigh, next);
992cd28ca0aSEric Dumazet 			immediate_probe = true;
9931da177e4SLinus Torvalds 		} else {
9941da177e4SLinus Torvalds 			neigh->nud_state = NUD_FAILED;
995955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9961da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 			kfree_skb(skb);
9991da177e4SLinus Torvalds 			return 1;
10001da177e4SLinus Torvalds 		}
10011da177e4SLinus Torvalds 	} else if (neigh->nud_state & NUD_STALE) {
1002d5d427cdSJoe Perches 		neigh_dbg(2, "neigh %p is delayed\n", neigh);
10031da177e4SLinus Torvalds 		neigh->nud_state = NUD_DELAY;
1004955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
10051f9248e5SJiri Pirko 		neigh_add_timer(neigh, jiffies +
10061f9248e5SJiri Pirko 				NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	if (neigh->nud_state == NUD_INCOMPLETE) {
10101da177e4SLinus Torvalds 		if (skb) {
10118b5c171bSEric Dumazet 			while (neigh->arp_queue_len_bytes + skb->truesize >
10121f9248e5SJiri Pirko 			       NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
10131da177e4SLinus Torvalds 				struct sk_buff *buff;
10148b5c171bSEric Dumazet 
1015f72051b0SDavid S. Miller 				buff = __skb_dequeue(&neigh->arp_queue);
10168b5c171bSEric Dumazet 				if (!buff)
10178b5c171bSEric Dumazet 					break;
10188b5c171bSEric Dumazet 				neigh->arp_queue_len_bytes -= buff->truesize;
10191da177e4SLinus Torvalds 				kfree_skb(buff);
10209a6d276eSNeil Horman 				NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
10211da177e4SLinus Torvalds 			}
1022a4731138SEric Dumazet 			skb_dst_force(skb);
10231da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->arp_queue, skb);
10248b5c171bSEric Dumazet 			neigh->arp_queue_len_bytes += skb->truesize;
10251da177e4SLinus Torvalds 		}
10261da177e4SLinus Torvalds 		rc = 1;
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds out_unlock_bh:
1029cd28ca0aSEric Dumazet 	if (immediate_probe)
1030cd28ca0aSEric Dumazet 		neigh_probe(neigh);
1031cd28ca0aSEric Dumazet 	else
1032cd28ca0aSEric Dumazet 		write_unlock(&neigh->lock);
1033cd28ca0aSEric Dumazet 	local_bh_enable();
10341da177e4SLinus Torvalds 	return rc;
10351da177e4SLinus Torvalds }
10360a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send);
10371da177e4SLinus Torvalds 
1038f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	struct hh_cache *hh;
10413b04dddeSStephen Hemminger 	void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
104291a72a70SDoug Kehn 		= NULL;
104391a72a70SDoug Kehn 
104491a72a70SDoug Kehn 	if (neigh->dev->header_ops)
104591a72a70SDoug Kehn 		update = neigh->dev->header_ops->cache_update;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	if (update) {
1048f6b72b62SDavid S. Miller 		hh = &neigh->hh;
1049f6b72b62SDavid S. Miller 		if (hh->hh_len) {
10503644f0ceSStephen Hemminger 			write_seqlock_bh(&hh->hh_lock);
10511da177e4SLinus Torvalds 			update(hh, neigh->dev, neigh->ha);
10523644f0ceSStephen Hemminger 			write_sequnlock_bh(&hh->hh_lock);
10531da177e4SLinus Torvalds 		}
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds /* Generic update routine.
10601da177e4SLinus Torvalds    -- lladdr is new lladdr or NULL, if it is not supplied.
10611da177e4SLinus Torvalds    -- new    is new state.
10621da177e4SLinus Torvalds    -- flags
10631da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
10641da177e4SLinus Torvalds 				if it is different.
10651da177e4SLinus Torvalds 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
10661da177e4SLinus Torvalds 				lladdr instead of overriding it
10671da177e4SLinus Torvalds 				if it is different.
10681da177e4SLinus Torvalds 				It also allows to retain current state
10691da177e4SLinus Torvalds 				if lladdr is unchanged.
10701da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
10731da177e4SLinus Torvalds 				NTF_ROUTER flag.
10741da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
10751da177e4SLinus Torvalds 				a router.
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds    Caller MUST hold reference count on the entry.
10781da177e4SLinus Torvalds  */
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
10811da177e4SLinus Torvalds 		 u32 flags)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	u8 old;
10841da177e4SLinus Torvalds 	int err;
10851da177e4SLinus Torvalds 	int notify = 0;
10861da177e4SLinus Torvalds 	struct net_device *dev;
10871da177e4SLinus Torvalds 	int update_isrouter = 0;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	dev    = neigh->dev;
10921da177e4SLinus Torvalds 	old    = neigh->nud_state;
10931da177e4SLinus Torvalds 	err    = -EPERM;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
10961da177e4SLinus Torvalds 	    (old & (NUD_NOARP | NUD_PERMANENT)))
10971da177e4SLinus Torvalds 		goto out;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	if (!(new & NUD_VALID)) {
11001da177e4SLinus Torvalds 		neigh_del_timer(neigh);
11011da177e4SLinus Torvalds 		if (old & NUD_CONNECTED)
11021da177e4SLinus Torvalds 			neigh_suspect(neigh);
11031da177e4SLinus Torvalds 		neigh->nud_state = new;
11041da177e4SLinus Torvalds 		err = 0;
11051da177e4SLinus Torvalds 		notify = old & NUD_VALID;
11065ef12d98STimo Teras 		if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
11075ef12d98STimo Teras 		    (new & NUD_FAILED)) {
11085ef12d98STimo Teras 			neigh_invalidate(neigh);
11095ef12d98STimo Teras 			notify = 1;
11105ef12d98STimo Teras 		}
11111da177e4SLinus Torvalds 		goto out;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 	/* Compare new lladdr with cached one */
11151da177e4SLinus Torvalds 	if (!dev->addr_len) {
11161da177e4SLinus Torvalds 		/* First case: device needs no address. */
11171da177e4SLinus Torvalds 		lladdr = neigh->ha;
11181da177e4SLinus Torvalds 	} else if (lladdr) {
11191da177e4SLinus Torvalds 		/* The second case: if something is already cached
11201da177e4SLinus Torvalds 		   and a new address is proposed:
11211da177e4SLinus Torvalds 		   - compare new & old
11221da177e4SLinus Torvalds 		   - if they are different, check override flag
11231da177e4SLinus Torvalds 		 */
11241da177e4SLinus Torvalds 		if ((old & NUD_VALID) &&
11251da177e4SLinus Torvalds 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
11261da177e4SLinus Torvalds 			lladdr = neigh->ha;
11271da177e4SLinus Torvalds 	} else {
11281da177e4SLinus Torvalds 		/* No address is supplied; if we know something,
11291da177e4SLinus Torvalds 		   use it, otherwise discard the request.
11301da177e4SLinus Torvalds 		 */
11311da177e4SLinus Torvalds 		err = -EINVAL;
11321da177e4SLinus Torvalds 		if (!(old & NUD_VALID))
11331da177e4SLinus Torvalds 			goto out;
11341da177e4SLinus Torvalds 		lladdr = neigh->ha;
11351da177e4SLinus Torvalds 	}
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
11381da177e4SLinus Torvalds 		neigh->confirmed = jiffies;
11391da177e4SLinus Torvalds 	neigh->updated = jiffies;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	/* If entry was valid and address is not changed,
11421da177e4SLinus Torvalds 	   do not change entry state, if new one is STALE.
11431da177e4SLinus Torvalds 	 */
11441da177e4SLinus Torvalds 	err = 0;
11451da177e4SLinus Torvalds 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
11461da177e4SLinus Torvalds 	if (old & NUD_VALID) {
11471da177e4SLinus Torvalds 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
11481da177e4SLinus Torvalds 			update_isrouter = 0;
11491da177e4SLinus Torvalds 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
11501da177e4SLinus Torvalds 			    (old & NUD_CONNECTED)) {
11511da177e4SLinus Torvalds 				lladdr = neigh->ha;
11521da177e4SLinus Torvalds 				new = NUD_STALE;
11531da177e4SLinus Torvalds 			} else
11541da177e4SLinus Torvalds 				goto out;
11551da177e4SLinus Torvalds 		} else {
11561da177e4SLinus Torvalds 			if (lladdr == neigh->ha && new == NUD_STALE &&
11571da177e4SLinus Torvalds 			    ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
11581da177e4SLinus Torvalds 			     (old & NUD_CONNECTED))
11591da177e4SLinus Torvalds 			    )
11601da177e4SLinus Torvalds 				new = old;
11611da177e4SLinus Torvalds 		}
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (new != old) {
11651da177e4SLinus Torvalds 		neigh_del_timer(neigh);
1166a43d8994SPavel Emelyanov 		if (new & NUD_IN_TIMER)
1167667347f1SDavid S. Miller 			neigh_add_timer(neigh, (jiffies +
11681da177e4SLinus Torvalds 						((new & NUD_REACHABLE) ?
1169667347f1SDavid S. Miller 						 neigh->parms->reachable_time :
1170667347f1SDavid S. Miller 						 0)));
11711da177e4SLinus Torvalds 		neigh->nud_state = new;
117253385d2dSBob Gilligan 		notify = 1;
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	if (lladdr != neigh->ha) {
11760ed8ddf4SEric Dumazet 		write_seqlock(&neigh->ha_lock);
11771da177e4SLinus Torvalds 		memcpy(&neigh->ha, lladdr, dev->addr_len);
11780ed8ddf4SEric Dumazet 		write_sequnlock(&neigh->ha_lock);
11791da177e4SLinus Torvalds 		neigh_update_hhs(neigh);
11801da177e4SLinus Torvalds 		if (!(new & NUD_CONNECTED))
11811da177e4SLinus Torvalds 			neigh->confirmed = jiffies -
11821f9248e5SJiri Pirko 				      (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
11831da177e4SLinus Torvalds 		notify = 1;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 	if (new == old)
11861da177e4SLinus Torvalds 		goto out;
11871da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
11881da177e4SLinus Torvalds 		neigh_connect(neigh);
11891da177e4SLinus Torvalds 	else
11901da177e4SLinus Torvalds 		neigh_suspect(neigh);
11911da177e4SLinus Torvalds 	if (!(old & NUD_VALID)) {
11921da177e4SLinus Torvalds 		struct sk_buff *skb;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		/* Again: avoid dead loop if something went wrong */
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 		while (neigh->nud_state & NUD_VALID &&
11971da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
119869cce1d1SDavid S. Miller 			struct dst_entry *dst = skb_dst(skb);
119969cce1d1SDavid S. Miller 			struct neighbour *n2, *n1 = neigh;
12001da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
1201e049f288Sroy.qing.li@gmail.com 
1202e049f288Sroy.qing.li@gmail.com 			rcu_read_lock();
120313a43d94SDavid S. Miller 
120413a43d94SDavid S. Miller 			/* Why not just use 'neigh' as-is?  The problem is that
120513a43d94SDavid S. Miller 			 * things such as shaper, eql, and sch_teql can end up
120613a43d94SDavid S. Miller 			 * using alternative, different, neigh objects to output
120713a43d94SDavid S. Miller 			 * the packet in the output path.  So what we need to do
120813a43d94SDavid S. Miller 			 * here is re-lookup the top-level neigh in the path so
120913a43d94SDavid S. Miller 			 * we can reinject the packet there.
121013a43d94SDavid S. Miller 			 */
121113a43d94SDavid S. Miller 			n2 = NULL;
121213a43d94SDavid S. Miller 			if (dst) {
121313a43d94SDavid S. Miller 				n2 = dst_neigh_lookup_skb(dst, skb);
121413a43d94SDavid S. Miller 				if (n2)
121569cce1d1SDavid S. Miller 					n1 = n2;
121613a43d94SDavid S. Miller 			}
12178f40b161SDavid S. Miller 			n1->output(n1, skb);
121813a43d94SDavid S. Miller 			if (n2)
121913a43d94SDavid S. Miller 				neigh_release(n2);
1220e049f288Sroy.qing.li@gmail.com 			rcu_read_unlock();
1221e049f288Sroy.qing.li@gmail.com 
12221da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
12231da177e4SLinus Torvalds 		}
1224c9ab4d85SEric Dumazet 		__skb_queue_purge(&neigh->arp_queue);
12258b5c171bSEric Dumazet 		neigh->arp_queue_len_bytes = 0;
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds out:
12281da177e4SLinus Torvalds 	if (update_isrouter) {
12291da177e4SLinus Torvalds 		neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
12301da177e4SLinus Torvalds 			(neigh->flags | NTF_ROUTER) :
12311da177e4SLinus Torvalds 			(neigh->flags & ~NTF_ROUTER);
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
12348d71740cSTom Tucker 
12358d71740cSTom Tucker 	if (notify)
1236d961db35SThomas Graf 		neigh_update_notify(neigh);
1237d961db35SThomas Graf 
12381da177e4SLinus Torvalds 	return err;
12391da177e4SLinus Torvalds }
12400a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update);
12411da177e4SLinus Torvalds 
12427e980569SJiri Benc /* Update the neigh to listen temporarily for probe responses, even if it is
12437e980569SJiri Benc  * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
12447e980569SJiri Benc  */
12457e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh)
12467e980569SJiri Benc {
12477e980569SJiri Benc 	neigh->updated = jiffies;
12487e980569SJiri Benc 	if (!(neigh->nud_state & NUD_FAILED))
12497e980569SJiri Benc 		return;
12507e980569SJiri Benc 	neigh->nud_state = NUD_PROBE;
12517e980569SJiri Benc 	atomic_set(&neigh->probes, NEIGH_VAR(neigh->parms, UCAST_PROBES));
12527e980569SJiri Benc 	neigh_add_timer(neigh,
12537e980569SJiri Benc 			jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
12547e980569SJiri Benc }
12557e980569SJiri Benc EXPORT_SYMBOL(__neigh_set_probe_once);
12567e980569SJiri Benc 
12571da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl,
12581da177e4SLinus Torvalds 				 u8 *lladdr, void *saddr,
12591da177e4SLinus Torvalds 				 struct net_device *dev)
12601da177e4SLinus Torvalds {
12611da177e4SLinus Torvalds 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
12621da177e4SLinus Torvalds 						 lladdr || !dev->addr_len);
12631da177e4SLinus Torvalds 	if (neigh)
12641da177e4SLinus Torvalds 		neigh_update(neigh, lladdr, NUD_STALE,
12651da177e4SLinus Torvalds 			     NEIGH_UPDATE_F_OVERRIDE);
12661da177e4SLinus Torvalds 	return neigh;
12671da177e4SLinus Torvalds }
12680a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns);
12691da177e4SLinus Torvalds 
127034d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */
1271f6b72b62SDavid S. Miller static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst)
12721da177e4SLinus Torvalds {
12731da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
1274f6b72b62SDavid S. Miller 	__be16 prot = dst->ops->protocol;
1275f6b72b62SDavid S. Miller 	struct hh_cache	*hh = &n->hh;
12760ed8ddf4SEric Dumazet 
12770ed8ddf4SEric Dumazet 	write_lock_bh(&n->lock);
127834d101ddSEric Dumazet 
1279f6b72b62SDavid S. Miller 	/* Only one thread can come in here and initialize the
1280f6b72b62SDavid S. Miller 	 * hh_cache entry.
1281f6b72b62SDavid S. Miller 	 */
1282b23b5455SDavid S. Miller 	if (!hh->hh_len)
1283b23b5455SDavid S. Miller 		dev->header_ops->cache(n, hh, prot);
1284f6b72b62SDavid S. Miller 
12850ed8ddf4SEric Dumazet 	write_unlock_bh(&n->lock);
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit
1289767e97e1SEric Dumazet  * worked, f.e. if you want to override normal output path (eql, shaper),
1290767e97e1SEric Dumazet  * but resolution is not made yet.
12911da177e4SLinus Torvalds  */
12921da177e4SLinus Torvalds 
12938f40b161SDavid S. Miller int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
12961da177e4SLinus Torvalds 
1297bbe735e4SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_offset(skb));
12981da177e4SLinus Torvalds 
12990c4e8581SStephen Hemminger 	if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
13001da177e4SLinus Torvalds 			    skb->len) < 0 &&
13012205369aSDavid S. Miller 	    dev_rebuild_header(skb))
13021da177e4SLinus Torvalds 		return 0;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	return dev_queue_xmit(skb);
13051da177e4SLinus Torvalds }
13060a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds /* Slow and careful. */
13091da177e4SLinus Torvalds 
13108f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
13111da177e4SLinus Torvalds {
1312adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
13131da177e4SLinus Torvalds 	int rc = 0;
13141da177e4SLinus Torvalds 
13158f40b161SDavid S. Miller 	if (!dst)
13161da177e4SLinus Torvalds 		goto discard;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	if (!neigh_event_send(neigh, skb)) {
13191da177e4SLinus Torvalds 		int err;
13201da177e4SLinus Torvalds 		struct net_device *dev = neigh->dev;
13210ed8ddf4SEric Dumazet 		unsigned int seq;
132234d101ddSEric Dumazet 
1323f6b72b62SDavid S. Miller 		if (dev->header_ops->cache && !neigh->hh.hh_len)
1324f6b72b62SDavid S. Miller 			neigh_hh_init(neigh, dst);
132534d101ddSEric Dumazet 
13260ed8ddf4SEric Dumazet 		do {
1327e1f16503Sramesh.nagappa@gmail.com 			__skb_pull(skb, skb_network_offset(skb));
13280ed8ddf4SEric Dumazet 			seq = read_seqbegin(&neigh->ha_lock);
13290c4e8581SStephen Hemminger 			err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13301da177e4SLinus Torvalds 					      neigh->ha, NULL, skb->len);
13310ed8ddf4SEric Dumazet 		} while (read_seqretry(&neigh->ha_lock, seq));
133234d101ddSEric Dumazet 
13331da177e4SLinus Torvalds 		if (err >= 0)
1334542d4d68SDavid S. Miller 			rc = dev_queue_xmit(skb);
13351da177e4SLinus Torvalds 		else
13361da177e4SLinus Torvalds 			goto out_kfree_skb;
13371da177e4SLinus Torvalds 	}
13381da177e4SLinus Torvalds out:
13391da177e4SLinus Torvalds 	return rc;
13401da177e4SLinus Torvalds discard:
1341d5d427cdSJoe Perches 	neigh_dbg(1, "%s: dst=%p neigh=%p\n", __func__, dst, neigh);
13421da177e4SLinus Torvalds out_kfree_skb:
13431da177e4SLinus Torvalds 	rc = -EINVAL;
13441da177e4SLinus Torvalds 	kfree_skb(skb);
13451da177e4SLinus Torvalds 	goto out;
13461da177e4SLinus Torvalds }
13470a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output);
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds /* As fast as possible without hh cache */
13501da177e4SLinus Torvalds 
13518f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
13540ed8ddf4SEric Dumazet 	unsigned int seq;
13558f40b161SDavid S. Miller 	int err;
13561da177e4SLinus Torvalds 
13570ed8ddf4SEric Dumazet 	do {
1358e1f16503Sramesh.nagappa@gmail.com 		__skb_pull(skb, skb_network_offset(skb));
13590ed8ddf4SEric Dumazet 		seq = read_seqbegin(&neigh->ha_lock);
13600c4e8581SStephen Hemminger 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13611da177e4SLinus Torvalds 				      neigh->ha, NULL, skb->len);
13620ed8ddf4SEric Dumazet 	} while (read_seqretry(&neigh->ha_lock, seq));
13630ed8ddf4SEric Dumazet 
13641da177e4SLinus Torvalds 	if (err >= 0)
1365542d4d68SDavid S. Miller 		err = dev_queue_xmit(skb);
13661da177e4SLinus Torvalds 	else {
13671da177e4SLinus Torvalds 		err = -EINVAL;
13681da177e4SLinus Torvalds 		kfree_skb(skb);
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds 	return err;
13711da177e4SLinus Torvalds }
13720a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output);
13731da177e4SLinus Torvalds 
13748f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
13758f40b161SDavid S. Miller {
13768f40b161SDavid S. Miller 	return dev_queue_xmit(skb);
13778f40b161SDavid S. Miller }
13788f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output);
13798f40b161SDavid S. Miller 
13801da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	struct neigh_table *tbl = (struct neigh_table *)arg;
13831da177e4SLinus Torvalds 	long sched_next = 0;
13841da177e4SLinus Torvalds 	unsigned long now = jiffies;
1385f72051b0SDavid S. Miller 	struct sk_buff *skb, *n;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
13881da177e4SLinus Torvalds 
1389f72051b0SDavid S. Miller 	skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1390f72051b0SDavid S. Miller 		long tdif = NEIGH_CB(skb)->sched_next - now;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 		if (tdif <= 0) {
1393f72051b0SDavid S. Miller 			struct net_device *dev = skb->dev;
139420e6074eSEric Dumazet 
1395f72051b0SDavid S. Miller 			__skb_unlink(skb, &tbl->proxy_queue);
139620e6074eSEric Dumazet 			if (tbl->proxy_redo && netif_running(dev)) {
139720e6074eSEric Dumazet 				rcu_read_lock();
1398f72051b0SDavid S. Miller 				tbl->proxy_redo(skb);
139920e6074eSEric Dumazet 				rcu_read_unlock();
140020e6074eSEric Dumazet 			} else {
1401f72051b0SDavid S. Miller 				kfree_skb(skb);
140220e6074eSEric Dumazet 			}
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 			dev_put(dev);
14051da177e4SLinus Torvalds 		} else if (!sched_next || tdif < sched_next)
14061da177e4SLinus Torvalds 			sched_next = tdif;
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds 	del_timer(&tbl->proxy_timer);
14091da177e4SLinus Torvalds 	if (sched_next)
14101da177e4SLinus Torvalds 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
14111da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
14151da177e4SLinus Torvalds 		    struct sk_buff *skb)
14161da177e4SLinus Torvalds {
14171da177e4SLinus Torvalds 	unsigned long now = jiffies;
141863862b5bSAruna-Hewapathirane 
141963862b5bSAruna-Hewapathirane 	unsigned long sched_next = now + (prandom_u32() %
14201f9248e5SJiri Pirko 					  NEIGH_VAR(p, PROXY_DELAY));
14211da177e4SLinus Torvalds 
14221f9248e5SJiri Pirko 	if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
14231da177e4SLinus Torvalds 		kfree_skb(skb);
14241da177e4SLinus Torvalds 		return;
14251da177e4SLinus Torvalds 	}
1426a61bbcf2SPatrick McHardy 
1427a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->sched_next = sched_next;
1428a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
14311da177e4SLinus Torvalds 	if (del_timer(&tbl->proxy_timer)) {
14321da177e4SLinus Torvalds 		if (time_before(tbl->proxy_timer.expires, sched_next))
14331da177e4SLinus Torvalds 			sched_next = tbl->proxy_timer.expires;
14341da177e4SLinus Torvalds 	}
1435adf30907SEric Dumazet 	skb_dst_drop(skb);
14361da177e4SLinus Torvalds 	dev_hold(skb->dev);
14371da177e4SLinus Torvalds 	__skb_queue_tail(&tbl->proxy_queue, skb);
14381da177e4SLinus Torvalds 	mod_timer(&tbl->proxy_timer, sched_next);
14391da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
14401da177e4SLinus Torvalds }
14410a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue);
14421da177e4SLinus Torvalds 
144397fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1444426b5303SEric W. Biederman 						      struct net *net, int ifindex)
1445426b5303SEric W. Biederman {
1446426b5303SEric W. Biederman 	struct neigh_parms *p;
1447426b5303SEric W. Biederman 
1448426b5303SEric W. Biederman 	for (p = &tbl->parms; p; p = p->next) {
1449878628fbSYOSHIFUJI Hideaki 		if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1450170d6f99SGao feng 		    (!p->dev && !ifindex && net_eq(net, &init_net)))
1451426b5303SEric W. Biederman 			return p;
1452426b5303SEric W. Biederman 	}
1453426b5303SEric W. Biederman 
1454426b5303SEric W. Biederman 	return NULL;
1455426b5303SEric W. Biederman }
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
14581da177e4SLinus Torvalds 				      struct neigh_table *tbl)
14591da177e4SLinus Torvalds {
1460cf89d6b2SGao feng 	struct neigh_parms *p;
146100829823SStephen Hemminger 	struct net *net = dev_net(dev);
146200829823SStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14631da177e4SLinus Torvalds 
1464cf89d6b2SGao feng 	p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
14651da177e4SLinus Torvalds 	if (p) {
14661da177e4SLinus Torvalds 		p->tbl		  = tbl;
14671da177e4SLinus Torvalds 		atomic_set(&p->refcnt, 1);
14681da177e4SLinus Torvalds 		p->reachable_time =
14691f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1470c7fb64dbSThomas Graf 		dev_hold(dev);
1471c7fb64dbSThomas Graf 		p->dev = dev;
1472e42ea986SEric Dumazet 		write_pnet(&p->net, hold_net(net));
14731da177e4SLinus Torvalds 		p->sysctl_table = NULL;
147463134803SVeaceslav Falico 
147563134803SVeaceslav Falico 		if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
147663134803SVeaceslav Falico 			release_net(net);
147763134803SVeaceslav Falico 			dev_put(dev);
147863134803SVeaceslav Falico 			kfree(p);
147963134803SVeaceslav Falico 			return NULL;
148063134803SVeaceslav Falico 		}
148163134803SVeaceslav Falico 
14821da177e4SLinus Torvalds 		write_lock_bh(&tbl->lock);
14831da177e4SLinus Torvalds 		p->next		= tbl->parms.next;
14841da177e4SLinus Torvalds 		tbl->parms.next = p;
14851da177e4SLinus Torvalds 		write_unlock_bh(&tbl->lock);
14861d4c8c29SJiri Pirko 
14871d4c8c29SJiri Pirko 		neigh_parms_data_state_cleanall(p);
14881da177e4SLinus Torvalds 	}
14891da177e4SLinus Torvalds 	return p;
14901da177e4SLinus Torvalds }
14910a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	struct neigh_parms *parms =
14961da177e4SLinus Torvalds 		container_of(head, struct neigh_parms, rcu_head);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	neigh_parms_put(parms);
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	struct neigh_parms **p;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (!parms || parms == &tbl->parms)
15061da177e4SLinus Torvalds 		return;
15071da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
15081da177e4SLinus Torvalds 	for (p = &tbl->parms.next; *p; p = &(*p)->next) {
15091da177e4SLinus Torvalds 		if (*p == parms) {
15101da177e4SLinus Torvalds 			*p = parms->next;
15111da177e4SLinus Torvalds 			parms->dead = 1;
15121da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
1513cecbb639SDavid S. Miller 			if (parms->dev)
1514cecbb639SDavid S. Miller 				dev_put(parms->dev);
15151da177e4SLinus Torvalds 			call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
15161da177e4SLinus Torvalds 			return;
15171da177e4SLinus Torvalds 		}
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1520d5d427cdSJoe Perches 	neigh_dbg(1, "%s: not found\n", __func__);
15211da177e4SLinus Torvalds }
15220a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release);
15231da177e4SLinus Torvalds 
152406f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms)
15251da177e4SLinus Torvalds {
152657da52c1SYOSHIFUJI Hideaki 	release_net(neigh_parms_net(parms));
15271da177e4SLinus Torvalds 	kfree(parms);
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
1530c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class;
1531c2ecba71SPavel Emelianov 
1532dcd2ba92SHiroaki SHIMODA static void neigh_table_init_no_netlink(struct neigh_table *tbl)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds 	unsigned long now = jiffies;
15351da177e4SLinus Torvalds 	unsigned long phsize;
15361da177e4SLinus Torvalds 
1537e42ea986SEric Dumazet 	write_pnet(&tbl->parms.net, &init_net);
15381da177e4SLinus Torvalds 	atomic_set(&tbl->parms.refcnt, 1);
15391da177e4SLinus Torvalds 	tbl->parms.reachable_time =
15401f9248e5SJiri Pirko 			  neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	tbl->stats = alloc_percpu(struct neigh_statistics);
15431da177e4SLinus Torvalds 	if (!tbl->stats)
15441da177e4SLinus Torvalds 		panic("cannot create neighbour cache statistics");
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
15479b739ba5SAlexey Dobriyan 	if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
15489b739ba5SAlexey Dobriyan 			      &neigh_stat_seq_fops, tbl))
15491da177e4SLinus Torvalds 		panic("cannot create neighbour proc dir entry");
15501da177e4SLinus Torvalds #endif
15511da177e4SLinus Torvalds 
1552cd089336SDavid S. Miller 	RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
155577d04bd9SAndrew Morton 	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
15561da177e4SLinus Torvalds 
1557d6bf7817SEric Dumazet 	if (!tbl->nht || !tbl->phash_buckets)
15581da177e4SLinus Torvalds 		panic("cannot allocate neighbour cache hashes");
15591da177e4SLinus Torvalds 
156008433effSYOSHIFUJI Hideaki / 吉藤英明 	if (!tbl->entry_size)
156108433effSYOSHIFUJI Hideaki / 吉藤英明 		tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
156208433effSYOSHIFUJI Hideaki / 吉藤英明 					tbl->key_len, NEIGH_PRIV_ALIGN);
156308433effSYOSHIFUJI Hideaki / 吉藤英明 	else
156408433effSYOSHIFUJI Hideaki / 吉藤英明 		WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
156508433effSYOSHIFUJI Hideaki / 吉藤英明 
15661da177e4SLinus Torvalds 	rwlock_init(&tbl->lock);
1567203b42f7STejun Heo 	INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1568f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1569f618002bSviresh kumar 			tbl->parms.reachable_time);
1570b24b8a24SPavel Emelyanov 	setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
1571c2ecba71SPavel Emelianov 	skb_queue_head_init_class(&tbl->proxy_queue,
1572c2ecba71SPavel Emelianov 			&neigh_table_proxy_queue_class);
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	tbl->last_flush = now;
15751da177e4SLinus Torvalds 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1576bd89efc5SSimon Kelley }
1577bd89efc5SSimon Kelley 
1578bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl)
1579bd89efc5SSimon Kelley {
1580bd89efc5SSimon Kelley 	struct neigh_table *tmp;
1581bd89efc5SSimon Kelley 
1582bd89efc5SSimon Kelley 	neigh_table_init_no_netlink(tbl);
15831da177e4SLinus Torvalds 	write_lock(&neigh_tbl_lock);
1584bd89efc5SSimon Kelley 	for (tmp = neigh_tables; tmp; tmp = tmp->next) {
1585bd89efc5SSimon Kelley 		if (tmp->family == tbl->family)
1586bd89efc5SSimon Kelley 			break;
1587bd89efc5SSimon Kelley 	}
15881da177e4SLinus Torvalds 	tbl->next	= neigh_tables;
15891da177e4SLinus Torvalds 	neigh_tables	= tbl;
15901da177e4SLinus Torvalds 	write_unlock(&neigh_tbl_lock);
1591bd89efc5SSimon Kelley 
1592bd89efc5SSimon Kelley 	if (unlikely(tmp)) {
1593e005d193SJoe Perches 		pr_err("Registering multiple tables for family %d\n",
1594e005d193SJoe Perches 		       tbl->family);
1595bd89efc5SSimon Kelley 		dump_stack();
1596bd89efc5SSimon Kelley 	}
15971da177e4SLinus Torvalds }
15980a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl)
16011da177e4SLinus Torvalds {
16021da177e4SLinus Torvalds 	struct neigh_table **tp;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	/* It is not clean... Fix it to unload IPv6 module safely */
1605a5c30b34STejun Heo 	cancel_delayed_work_sync(&tbl->gc_work);
16061da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
16071da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
16081da177e4SLinus Torvalds 	neigh_ifdown(tbl, NULL);
16091da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries))
1610e005d193SJoe Perches 		pr_crit("neighbour leakage\n");
16111da177e4SLinus Torvalds 	write_lock(&neigh_tbl_lock);
16121da177e4SLinus Torvalds 	for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
16131da177e4SLinus Torvalds 		if (*tp == tbl) {
16141da177e4SLinus Torvalds 			*tp = tbl->next;
16151da177e4SLinus Torvalds 			break;
16161da177e4SLinus Torvalds 		}
16171da177e4SLinus Torvalds 	}
16181da177e4SLinus Torvalds 	write_unlock(&neigh_tbl_lock);
16191da177e4SLinus Torvalds 
16206193d2beSEric Dumazet 	call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
16216193d2beSEric Dumazet 		 neigh_hash_free_rcu);
1622d6bf7817SEric Dumazet 	tbl->nht = NULL;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	kfree(tbl->phash_buckets);
16251da177e4SLinus Torvalds 	tbl->phash_buckets = NULL;
16261da177e4SLinus Torvalds 
16273f192b5cSAlexey Dobriyan 	remove_proc_entry(tbl->id, init_net.proc_net_stat);
16283f192b5cSAlexey Dobriyan 
16293fcde74bSKirill Korotaev 	free_percpu(tbl->stats);
16303fcde74bSKirill Korotaev 	tbl->stats = NULL;
16313fcde74bSKirill Korotaev 
16321da177e4SLinus Torvalds 	return 0;
16331da177e4SLinus Torvalds }
16340a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear);
16351da177e4SLinus Torvalds 
1636661d2967SThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh)
16371da177e4SLinus Torvalds {
16383b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1639a14a49d2SThomas Graf 	struct ndmsg *ndm;
1640a14a49d2SThomas Graf 	struct nlattr *dst_attr;
16411da177e4SLinus Torvalds 	struct neigh_table *tbl;
16421da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1643a14a49d2SThomas Graf 	int err = -EINVAL;
16441da177e4SLinus Torvalds 
1645110b2499SEric Dumazet 	ASSERT_RTNL();
1646a14a49d2SThomas Graf 	if (nlmsg_len(nlh) < sizeof(*ndm))
16471da177e4SLinus Torvalds 		goto out;
16481da177e4SLinus Torvalds 
1649a14a49d2SThomas Graf 	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1650a14a49d2SThomas Graf 	if (dst_attr == NULL)
1651a14a49d2SThomas Graf 		goto out;
1652a14a49d2SThomas Graf 
1653a14a49d2SThomas Graf 	ndm = nlmsg_data(nlh);
1654a14a49d2SThomas Graf 	if (ndm->ndm_ifindex) {
1655110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1656a14a49d2SThomas Graf 		if (dev == NULL) {
1657a14a49d2SThomas Graf 			err = -ENODEV;
1658a14a49d2SThomas Graf 			goto out;
1659a14a49d2SThomas Graf 		}
1660a14a49d2SThomas Graf 	}
1661a14a49d2SThomas Graf 
16621da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
16631da177e4SLinus Torvalds 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1664a14a49d2SThomas Graf 		struct neighbour *neigh;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 		if (tbl->family != ndm->ndm_family)
16671da177e4SLinus Torvalds 			continue;
16681da177e4SLinus Torvalds 		read_unlock(&neigh_tbl_lock);
16691da177e4SLinus Torvalds 
1670a14a49d2SThomas Graf 		if (nla_len(dst_attr) < tbl->key_len)
1671110b2499SEric Dumazet 			goto out;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 		if (ndm->ndm_flags & NTF_PROXY) {
1674426b5303SEric W. Biederman 			err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1675110b2499SEric Dumazet 			goto out;
16761da177e4SLinus Torvalds 		}
16771da177e4SLinus Torvalds 
1678a14a49d2SThomas Graf 		if (dev == NULL)
1679110b2499SEric Dumazet 			goto out;
16801da177e4SLinus Torvalds 
1681a14a49d2SThomas Graf 		neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1682a14a49d2SThomas Graf 		if (neigh == NULL) {
1683a14a49d2SThomas Graf 			err = -ENOENT;
1684110b2499SEric Dumazet 			goto out;
1685a14a49d2SThomas Graf 		}
1686a14a49d2SThomas Graf 
1687a14a49d2SThomas Graf 		err = neigh_update(neigh, NULL, NUD_FAILED,
16881da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_OVERRIDE |
16891da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_ADMIN);
1690a14a49d2SThomas Graf 		neigh_release(neigh);
1691110b2499SEric Dumazet 		goto out;
16921da177e4SLinus Torvalds 	}
16931da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
1694a14a49d2SThomas Graf 	err = -EAFNOSUPPORT;
1695a14a49d2SThomas Graf 
16961da177e4SLinus Torvalds out:
16971da177e4SLinus Torvalds 	return err;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
1700661d2967SThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh)
17011da177e4SLinus Torvalds {
17023b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
17035208debdSThomas Graf 	struct ndmsg *ndm;
17045208debdSThomas Graf 	struct nlattr *tb[NDA_MAX+1];
17051da177e4SLinus Torvalds 	struct neigh_table *tbl;
17061da177e4SLinus Torvalds 	struct net_device *dev = NULL;
17075208debdSThomas Graf 	int err;
17081da177e4SLinus Torvalds 
1709110b2499SEric Dumazet 	ASSERT_RTNL();
17105208debdSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
17115208debdSThomas Graf 	if (err < 0)
17121da177e4SLinus Torvalds 		goto out;
17131da177e4SLinus Torvalds 
17145208debdSThomas Graf 	err = -EINVAL;
17155208debdSThomas Graf 	if (tb[NDA_DST] == NULL)
17165208debdSThomas Graf 		goto out;
17175208debdSThomas Graf 
17185208debdSThomas Graf 	ndm = nlmsg_data(nlh);
17195208debdSThomas Graf 	if (ndm->ndm_ifindex) {
1720110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
17215208debdSThomas Graf 		if (dev == NULL) {
17225208debdSThomas Graf 			err = -ENODEV;
17235208debdSThomas Graf 			goto out;
17245208debdSThomas Graf 		}
17255208debdSThomas Graf 
17265208debdSThomas Graf 		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1727110b2499SEric Dumazet 			goto out;
17285208debdSThomas Graf 	}
17295208debdSThomas Graf 
17301da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
17311da177e4SLinus Torvalds 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
17325208debdSThomas Graf 		int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
17335208debdSThomas Graf 		struct neighbour *neigh;
17345208debdSThomas Graf 		void *dst, *lladdr;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		if (tbl->family != ndm->ndm_family)
17371da177e4SLinus Torvalds 			continue;
17381da177e4SLinus Torvalds 		read_unlock(&neigh_tbl_lock);
17391da177e4SLinus Torvalds 
17405208debdSThomas Graf 		if (nla_len(tb[NDA_DST]) < tbl->key_len)
1741110b2499SEric Dumazet 			goto out;
17425208debdSThomas Graf 		dst = nla_data(tb[NDA_DST]);
17435208debdSThomas Graf 		lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 		if (ndm->ndm_flags & NTF_PROXY) {
174662dd9318SVille Nuorvala 			struct pneigh_entry *pn;
174762dd9318SVille Nuorvala 
17485208debdSThomas Graf 			err = -ENOBUFS;
1749426b5303SEric W. Biederman 			pn = pneigh_lookup(tbl, net, dst, dev, 1);
175062dd9318SVille Nuorvala 			if (pn) {
175162dd9318SVille Nuorvala 				pn->flags = ndm->ndm_flags;
175262dd9318SVille Nuorvala 				err = 0;
175362dd9318SVille Nuorvala 			}
1754110b2499SEric Dumazet 			goto out;
17551da177e4SLinus Torvalds 		}
17561da177e4SLinus Torvalds 
17575208debdSThomas Graf 		if (dev == NULL)
1758110b2499SEric Dumazet 			goto out;
17591da177e4SLinus Torvalds 
17605208debdSThomas Graf 		neigh = neigh_lookup(tbl, dst, dev);
17615208debdSThomas Graf 		if (neigh == NULL) {
17625208debdSThomas Graf 			if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
17631da177e4SLinus Torvalds 				err = -ENOENT;
1764110b2499SEric Dumazet 				goto out;
17655208debdSThomas Graf 			}
17665208debdSThomas Graf 
17675208debdSThomas Graf 			neigh = __neigh_lookup_errno(tbl, dst, dev);
17685208debdSThomas Graf 			if (IS_ERR(neigh)) {
17695208debdSThomas Graf 				err = PTR_ERR(neigh);
1770110b2499SEric Dumazet 				goto out;
17711da177e4SLinus Torvalds 			}
17725208debdSThomas Graf 		} else {
17735208debdSThomas Graf 			if (nlh->nlmsg_flags & NLM_F_EXCL) {
17745208debdSThomas Graf 				err = -EEXIST;
17755208debdSThomas Graf 				neigh_release(neigh);
1776110b2499SEric Dumazet 				goto out;
17771da177e4SLinus Torvalds 			}
17781da177e4SLinus Torvalds 
17795208debdSThomas Graf 			if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
17805208debdSThomas Graf 				flags &= ~NEIGH_UPDATE_F_OVERRIDE;
17815208debdSThomas Graf 		}
17821da177e4SLinus Torvalds 
17830c5c2d30SEric Biederman 		if (ndm->ndm_flags & NTF_USE) {
17840c5c2d30SEric Biederman 			neigh_event_send(neigh, NULL);
17850c5c2d30SEric Biederman 			err = 0;
17860c5c2d30SEric Biederman 		} else
17875208debdSThomas Graf 			err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
17885208debdSThomas Graf 		neigh_release(neigh);
1789110b2499SEric Dumazet 		goto out;
17901da177e4SLinus Torvalds 	}
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
17935208debdSThomas Graf 	err = -EAFNOSUPPORT;
17941da177e4SLinus Torvalds out:
17951da177e4SLinus Torvalds 	return err;
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
1798c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1799c7fb64dbSThomas Graf {
1800ca860fb3SThomas Graf 	struct nlattr *nest;
1801e386c6ebSThomas Graf 
1802ca860fb3SThomas Graf 	nest = nla_nest_start(skb, NDTA_PARMS);
1803ca860fb3SThomas Graf 	if (nest == NULL)
1804ca860fb3SThomas Graf 		return -ENOBUFS;
1805c7fb64dbSThomas Graf 
18069a6308d7SDavid S. Miller 	if ((parms->dev &&
18079a6308d7SDavid S. Miller 	     nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
18089a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) ||
18091f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
18101f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
18118b5c171bSEric Dumazet 	    /* approximative value for deprecated QUEUE_LEN (in packets) */
18129a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_QUEUE_LEN,
18131f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
18141f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
18151f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
18161f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_UCAST_PROBES,
18171f9248e5SJiri Pirko 			NEIGH_VAR(parms, UCAST_PROBES)) ||
18181f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_MCAST_PROBES,
18191f9248e5SJiri Pirko 			NEIGH_VAR(parms, MCAST_PROBES)) ||
18209a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) ||
18219a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
18221f9248e5SJiri Pirko 			  NEIGH_VAR(parms, BASE_REACHABLE_TIME)) ||
18231f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_GC_STALETIME,
18241f9248e5SJiri Pirko 			  NEIGH_VAR(parms, GC_STALETIME)) ||
18259a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
18261f9248e5SJiri Pirko 			  NEIGH_VAR(parms, DELAY_PROBE_TIME)) ||
18271f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_RETRANS_TIME,
18281f9248e5SJiri Pirko 			  NEIGH_VAR(parms, RETRANS_TIME)) ||
18291f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
18301f9248e5SJiri Pirko 			  NEIGH_VAR(parms, ANYCAST_DELAY)) ||
18311f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_PROXY_DELAY,
18321f9248e5SJiri Pirko 			  NEIGH_VAR(parms, PROXY_DELAY)) ||
18331f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_LOCKTIME,
18341f9248e5SJiri Pirko 			  NEIGH_VAR(parms, LOCKTIME)))
18359a6308d7SDavid S. Miller 		goto nla_put_failure;
1836ca860fb3SThomas Graf 	return nla_nest_end(skb, nest);
1837c7fb64dbSThomas Graf 
1838ca860fb3SThomas Graf nla_put_failure:
1839bc3ed28cSThomas Graf 	nla_nest_cancel(skb, nest);
1840bc3ed28cSThomas Graf 	return -EMSGSIZE;
1841c7fb64dbSThomas Graf }
1842c7fb64dbSThomas Graf 
1843ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1844ca860fb3SThomas Graf 			      u32 pid, u32 seq, int type, int flags)
1845c7fb64dbSThomas Graf {
1846c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1847c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1848c7fb64dbSThomas Graf 
1849ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1850ca860fb3SThomas Graf 	if (nlh == NULL)
185126932566SPatrick McHardy 		return -EMSGSIZE;
1852c7fb64dbSThomas Graf 
1853ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1854c7fb64dbSThomas Graf 
1855c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1856c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
18579ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
18589ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1859c7fb64dbSThomas Graf 
18609a6308d7SDavid S. Miller 	if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
18619a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval) ||
18629a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
18639a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
18649a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
18659a6308d7SDavid S. Miller 		goto nla_put_failure;
1866c7fb64dbSThomas Graf 	{
1867c7fb64dbSThomas Graf 		unsigned long now = jiffies;
1868c7fb64dbSThomas Graf 		unsigned int flush_delta = now - tbl->last_flush;
1869c7fb64dbSThomas Graf 		unsigned int rand_delta = now - tbl->last_rand;
1870d6bf7817SEric Dumazet 		struct neigh_hash_table *nht;
1871c7fb64dbSThomas Graf 		struct ndt_config ndc = {
1872c7fb64dbSThomas Graf 			.ndtc_key_len		= tbl->key_len,
1873c7fb64dbSThomas Graf 			.ndtc_entry_size	= tbl->entry_size,
1874c7fb64dbSThomas Graf 			.ndtc_entries		= atomic_read(&tbl->entries),
1875c7fb64dbSThomas Graf 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
1876c7fb64dbSThomas Graf 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
1877c7fb64dbSThomas Graf 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
1878c7fb64dbSThomas Graf 		};
1879c7fb64dbSThomas Graf 
1880d6bf7817SEric Dumazet 		rcu_read_lock_bh();
1881d6bf7817SEric Dumazet 		nht = rcu_dereference_bh(tbl->nht);
18822c2aba6cSDavid S. Miller 		ndc.ndtc_hash_rnd = nht->hash_rnd[0];
1883cd089336SDavid S. Miller 		ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
1884d6bf7817SEric Dumazet 		rcu_read_unlock_bh();
1885d6bf7817SEric Dumazet 
18869a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
18879a6308d7SDavid S. Miller 			goto nla_put_failure;
1888c7fb64dbSThomas Graf 	}
1889c7fb64dbSThomas Graf 
1890c7fb64dbSThomas Graf 	{
1891c7fb64dbSThomas Graf 		int cpu;
1892c7fb64dbSThomas Graf 		struct ndt_stats ndst;
1893c7fb64dbSThomas Graf 
1894c7fb64dbSThomas Graf 		memset(&ndst, 0, sizeof(ndst));
1895c7fb64dbSThomas Graf 
18966f912042SKAMEZAWA Hiroyuki 		for_each_possible_cpu(cpu) {
1897c7fb64dbSThomas Graf 			struct neigh_statistics	*st;
1898c7fb64dbSThomas Graf 
1899c7fb64dbSThomas Graf 			st = per_cpu_ptr(tbl->stats, cpu);
1900c7fb64dbSThomas Graf 			ndst.ndts_allocs		+= st->allocs;
1901c7fb64dbSThomas Graf 			ndst.ndts_destroys		+= st->destroys;
1902c7fb64dbSThomas Graf 			ndst.ndts_hash_grows		+= st->hash_grows;
1903c7fb64dbSThomas Graf 			ndst.ndts_res_failed		+= st->res_failed;
1904c7fb64dbSThomas Graf 			ndst.ndts_lookups		+= st->lookups;
1905c7fb64dbSThomas Graf 			ndst.ndts_hits			+= st->hits;
1906c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
1907c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
1908c7fb64dbSThomas Graf 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
1909c7fb64dbSThomas Graf 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
1910c7fb64dbSThomas Graf 		}
1911c7fb64dbSThomas Graf 
19129a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_STATS, sizeof(ndst), &ndst))
19139a6308d7SDavid S. Miller 			goto nla_put_failure;
1914c7fb64dbSThomas Graf 	}
1915c7fb64dbSThomas Graf 
1916c7fb64dbSThomas Graf 	BUG_ON(tbl->parms.dev);
1917c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1918ca860fb3SThomas Graf 		goto nla_put_failure;
1919c7fb64dbSThomas Graf 
1920c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1921ca860fb3SThomas Graf 	return nlmsg_end(skb, nlh);
1922c7fb64dbSThomas Graf 
1923ca860fb3SThomas Graf nla_put_failure:
1924c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
192526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
192626932566SPatrick McHardy 	return -EMSGSIZE;
1927c7fb64dbSThomas Graf }
1928c7fb64dbSThomas Graf 
1929ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb,
1930ca860fb3SThomas Graf 				    struct neigh_table *tbl,
1931c7fb64dbSThomas Graf 				    struct neigh_parms *parms,
1932ca860fb3SThomas Graf 				    u32 pid, u32 seq, int type,
1933ca860fb3SThomas Graf 				    unsigned int flags)
1934c7fb64dbSThomas Graf {
1935c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1936c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1937c7fb64dbSThomas Graf 
1938ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1939ca860fb3SThomas Graf 	if (nlh == NULL)
194026932566SPatrick McHardy 		return -EMSGSIZE;
1941c7fb64dbSThomas Graf 
1942ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1943c7fb64dbSThomas Graf 
1944c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1945c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
19469ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
19479ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1948c7fb64dbSThomas Graf 
1949ca860fb3SThomas Graf 	if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1950ca860fb3SThomas Graf 	    neightbl_fill_parms(skb, parms) < 0)
1951ca860fb3SThomas Graf 		goto errout;
1952c7fb64dbSThomas Graf 
1953c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1954ca860fb3SThomas Graf 	return nlmsg_end(skb, nlh);
1955ca860fb3SThomas Graf errout:
1956c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
195726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
195826932566SPatrick McHardy 	return -EMSGSIZE;
1959c7fb64dbSThomas Graf }
1960c7fb64dbSThomas Graf 
1961ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
19626b3f8674SThomas Graf 	[NDTA_NAME]		= { .type = NLA_STRING },
19636b3f8674SThomas Graf 	[NDTA_THRESH1]		= { .type = NLA_U32 },
19646b3f8674SThomas Graf 	[NDTA_THRESH2]		= { .type = NLA_U32 },
19656b3f8674SThomas Graf 	[NDTA_THRESH3]		= { .type = NLA_U32 },
19666b3f8674SThomas Graf 	[NDTA_GC_INTERVAL]	= { .type = NLA_U64 },
19676b3f8674SThomas Graf 	[NDTA_PARMS]		= { .type = NLA_NESTED },
19686b3f8674SThomas Graf };
19696b3f8674SThomas Graf 
1970ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
19716b3f8674SThomas Graf 	[NDTPA_IFINDEX]			= { .type = NLA_U32 },
19726b3f8674SThomas Graf 	[NDTPA_QUEUE_LEN]		= { .type = NLA_U32 },
19736b3f8674SThomas Graf 	[NDTPA_PROXY_QLEN]		= { .type = NLA_U32 },
19746b3f8674SThomas Graf 	[NDTPA_APP_PROBES]		= { .type = NLA_U32 },
19756b3f8674SThomas Graf 	[NDTPA_UCAST_PROBES]		= { .type = NLA_U32 },
19766b3f8674SThomas Graf 	[NDTPA_MCAST_PROBES]		= { .type = NLA_U32 },
19776b3f8674SThomas Graf 	[NDTPA_BASE_REACHABLE_TIME]	= { .type = NLA_U64 },
19786b3f8674SThomas Graf 	[NDTPA_GC_STALETIME]		= { .type = NLA_U64 },
19796b3f8674SThomas Graf 	[NDTPA_DELAY_PROBE_TIME]	= { .type = NLA_U64 },
19806b3f8674SThomas Graf 	[NDTPA_RETRANS_TIME]		= { .type = NLA_U64 },
19816b3f8674SThomas Graf 	[NDTPA_ANYCAST_DELAY]		= { .type = NLA_U64 },
19826b3f8674SThomas Graf 	[NDTPA_PROXY_DELAY]		= { .type = NLA_U64 },
19836b3f8674SThomas Graf 	[NDTPA_LOCKTIME]		= { .type = NLA_U64 },
19846b3f8674SThomas Graf };
19856b3f8674SThomas Graf 
1986661d2967SThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh)
1987c7fb64dbSThomas Graf {
19883b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1989c7fb64dbSThomas Graf 	struct neigh_table *tbl;
19906b3f8674SThomas Graf 	struct ndtmsg *ndtmsg;
19916b3f8674SThomas Graf 	struct nlattr *tb[NDTA_MAX+1];
19926b3f8674SThomas Graf 	int err;
1993c7fb64dbSThomas Graf 
19946b3f8674SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
19956b3f8674SThomas Graf 			  nl_neightbl_policy);
19966b3f8674SThomas Graf 	if (err < 0)
19976b3f8674SThomas Graf 		goto errout;
1998c7fb64dbSThomas Graf 
19996b3f8674SThomas Graf 	if (tb[NDTA_NAME] == NULL) {
20006b3f8674SThomas Graf 		err = -EINVAL;
20016b3f8674SThomas Graf 		goto errout;
20026b3f8674SThomas Graf 	}
20036b3f8674SThomas Graf 
20046b3f8674SThomas Graf 	ndtmsg = nlmsg_data(nlh);
2005c7fb64dbSThomas Graf 	read_lock(&neigh_tbl_lock);
2006c7fb64dbSThomas Graf 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
2007c7fb64dbSThomas Graf 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2008c7fb64dbSThomas Graf 			continue;
2009c7fb64dbSThomas Graf 
20106b3f8674SThomas Graf 		if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0)
2011c7fb64dbSThomas Graf 			break;
2012c7fb64dbSThomas Graf 	}
2013c7fb64dbSThomas Graf 
2014c7fb64dbSThomas Graf 	if (tbl == NULL) {
2015c7fb64dbSThomas Graf 		err = -ENOENT;
20166b3f8674SThomas Graf 		goto errout_locked;
2017c7fb64dbSThomas Graf 	}
2018c7fb64dbSThomas Graf 
2019c7fb64dbSThomas Graf 	/*
2020c7fb64dbSThomas Graf 	 * We acquire tbl->lock to be nice to the periodic timers and
2021c7fb64dbSThomas Graf 	 * make sure they always see a consistent set of values.
2022c7fb64dbSThomas Graf 	 */
2023c7fb64dbSThomas Graf 	write_lock_bh(&tbl->lock);
2024c7fb64dbSThomas Graf 
20256b3f8674SThomas Graf 	if (tb[NDTA_PARMS]) {
20266b3f8674SThomas Graf 		struct nlattr *tbp[NDTPA_MAX+1];
2027c7fb64dbSThomas Graf 		struct neigh_parms *p;
20286b3f8674SThomas Graf 		int i, ifindex = 0;
2029c7fb64dbSThomas Graf 
20306b3f8674SThomas Graf 		err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
20316b3f8674SThomas Graf 				       nl_ntbl_parm_policy);
20326b3f8674SThomas Graf 		if (err < 0)
20336b3f8674SThomas Graf 			goto errout_tbl_lock;
2034c7fb64dbSThomas Graf 
20356b3f8674SThomas Graf 		if (tbp[NDTPA_IFINDEX])
20366b3f8674SThomas Graf 			ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2037c7fb64dbSThomas Graf 
203897fd5bc7STobias Klauser 		p = lookup_neigh_parms(tbl, net, ifindex);
2039c7fb64dbSThomas Graf 		if (p == NULL) {
2040c7fb64dbSThomas Graf 			err = -ENOENT;
20416b3f8674SThomas Graf 			goto errout_tbl_lock;
2042c7fb64dbSThomas Graf 		}
2043c7fb64dbSThomas Graf 
20446b3f8674SThomas Graf 		for (i = 1; i <= NDTPA_MAX; i++) {
20456b3f8674SThomas Graf 			if (tbp[i] == NULL)
20466b3f8674SThomas Graf 				continue;
2047c7fb64dbSThomas Graf 
20486b3f8674SThomas Graf 			switch (i) {
20496b3f8674SThomas Graf 			case NDTPA_QUEUE_LEN:
20501f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
20511f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]) *
20521f9248e5SJiri Pirko 					      SKB_TRUESIZE(ETH_FRAME_LEN));
20538b5c171bSEric Dumazet 				break;
20548b5c171bSEric Dumazet 			case NDTPA_QUEUE_LENBYTES:
20551f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
20561f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20576b3f8674SThomas Graf 				break;
20586b3f8674SThomas Graf 			case NDTPA_PROXY_QLEN:
20591f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, PROXY_QLEN,
20601f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20616b3f8674SThomas Graf 				break;
20626b3f8674SThomas Graf 			case NDTPA_APP_PROBES:
20631f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, APP_PROBES,
20641f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20656b3f8674SThomas Graf 				break;
20666b3f8674SThomas Graf 			case NDTPA_UCAST_PROBES:
20671f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, UCAST_PROBES,
20681f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20696b3f8674SThomas Graf 				break;
20706b3f8674SThomas Graf 			case NDTPA_MCAST_PROBES:
20711f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, MCAST_PROBES,
20721f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20736b3f8674SThomas Graf 				break;
20746b3f8674SThomas Graf 			case NDTPA_BASE_REACHABLE_TIME:
20751f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
20761f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20776b3f8674SThomas Graf 				break;
20786b3f8674SThomas Graf 			case NDTPA_GC_STALETIME:
20791f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, GC_STALETIME,
20801f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20816b3f8674SThomas Graf 				break;
20826b3f8674SThomas Graf 			case NDTPA_DELAY_PROBE_TIME:
20831f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
20841f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20856b3f8674SThomas Graf 				break;
20866b3f8674SThomas Graf 			case NDTPA_RETRANS_TIME:
20871f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, RETRANS_TIME,
20881f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20896b3f8674SThomas Graf 				break;
20906b3f8674SThomas Graf 			case NDTPA_ANYCAST_DELAY:
20913977458cSJiri Pirko 				NEIGH_VAR_SET(p, ANYCAST_DELAY,
20923977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
20936b3f8674SThomas Graf 				break;
20946b3f8674SThomas Graf 			case NDTPA_PROXY_DELAY:
20953977458cSJiri Pirko 				NEIGH_VAR_SET(p, PROXY_DELAY,
20963977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
20976b3f8674SThomas Graf 				break;
20986b3f8674SThomas Graf 			case NDTPA_LOCKTIME:
20993977458cSJiri Pirko 				NEIGH_VAR_SET(p, LOCKTIME,
21003977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
21016b3f8674SThomas Graf 				break;
2102c7fb64dbSThomas Graf 			}
21036b3f8674SThomas Graf 		}
21046b3f8674SThomas Graf 	}
21056b3f8674SThomas Graf 
2106dc25c676SGao feng 	err = -ENOENT;
2107dc25c676SGao feng 	if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2108dc25c676SGao feng 	     tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2109dc25c676SGao feng 	    !net_eq(net, &init_net))
2110dc25c676SGao feng 		goto errout_tbl_lock;
2111dc25c676SGao feng 
21126b3f8674SThomas Graf 	if (tb[NDTA_THRESH1])
21136b3f8674SThomas Graf 		tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
21146b3f8674SThomas Graf 
21156b3f8674SThomas Graf 	if (tb[NDTA_THRESH2])
21166b3f8674SThomas Graf 		tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
21176b3f8674SThomas Graf 
21186b3f8674SThomas Graf 	if (tb[NDTA_THRESH3])
21196b3f8674SThomas Graf 		tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
21206b3f8674SThomas Graf 
21216b3f8674SThomas Graf 	if (tb[NDTA_GC_INTERVAL])
21226b3f8674SThomas Graf 		tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2123c7fb64dbSThomas Graf 
2124c7fb64dbSThomas Graf 	err = 0;
2125c7fb64dbSThomas Graf 
21266b3f8674SThomas Graf errout_tbl_lock:
2127c7fb64dbSThomas Graf 	write_unlock_bh(&tbl->lock);
21286b3f8674SThomas Graf errout_locked:
2129c7fb64dbSThomas Graf 	read_unlock(&neigh_tbl_lock);
21306b3f8674SThomas Graf errout:
2131c7fb64dbSThomas Graf 	return err;
2132c7fb64dbSThomas Graf }
2133c7fb64dbSThomas Graf 
2134c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2135c7fb64dbSThomas Graf {
21363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2137ca860fb3SThomas Graf 	int family, tidx, nidx = 0;
2138ca860fb3SThomas Graf 	int tbl_skip = cb->args[0];
2139ca860fb3SThomas Graf 	int neigh_skip = cb->args[1];
2140c7fb64dbSThomas Graf 	struct neigh_table *tbl;
2141c7fb64dbSThomas Graf 
2142ca860fb3SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2143c7fb64dbSThomas Graf 
2144c7fb64dbSThomas Graf 	read_lock(&neigh_tbl_lock);
2145ca860fb3SThomas Graf 	for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) {
2146c7fb64dbSThomas Graf 		struct neigh_parms *p;
2147c7fb64dbSThomas Graf 
2148ca860fb3SThomas Graf 		if (tidx < tbl_skip || (family && tbl->family != family))
2149c7fb64dbSThomas Graf 			continue;
2150c7fb64dbSThomas Graf 
215115e47304SEric W. Biederman 		if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2152ca860fb3SThomas Graf 				       cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2153ca860fb3SThomas Graf 				       NLM_F_MULTI) <= 0)
2154c7fb64dbSThomas Graf 			break;
2155c7fb64dbSThomas Graf 
2156426b5303SEric W. Biederman 		for (nidx = 0, p = tbl->parms.next; p; p = p->next) {
2157878628fbSYOSHIFUJI Hideaki 			if (!net_eq(neigh_parms_net(p), net))
2158426b5303SEric W. Biederman 				continue;
2159426b5303SEric W. Biederman 
2160efc683fcSGautam Kachroo 			if (nidx < neigh_skip)
2161efc683fcSGautam Kachroo 				goto next;
2162c7fb64dbSThomas Graf 
2163ca860fb3SThomas Graf 			if (neightbl_fill_param_info(skb, tbl, p,
216415e47304SEric W. Biederman 						     NETLINK_CB(cb->skb).portid,
2165ca860fb3SThomas Graf 						     cb->nlh->nlmsg_seq,
2166ca860fb3SThomas Graf 						     RTM_NEWNEIGHTBL,
2167ca860fb3SThomas Graf 						     NLM_F_MULTI) <= 0)
2168c7fb64dbSThomas Graf 				goto out;
2169efc683fcSGautam Kachroo 		next:
2170efc683fcSGautam Kachroo 			nidx++;
2171c7fb64dbSThomas Graf 		}
2172c7fb64dbSThomas Graf 
2173ca860fb3SThomas Graf 		neigh_skip = 0;
2174c7fb64dbSThomas Graf 	}
2175c7fb64dbSThomas Graf out:
2176c7fb64dbSThomas Graf 	read_unlock(&neigh_tbl_lock);
2177ca860fb3SThomas Graf 	cb->args[0] = tidx;
2178ca860fb3SThomas Graf 	cb->args[1] = nidx;
2179c7fb64dbSThomas Graf 
2180c7fb64dbSThomas Graf 	return skb->len;
2181c7fb64dbSThomas Graf }
21821da177e4SLinus Torvalds 
21838b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
21848b8aec50SThomas Graf 			   u32 pid, u32 seq, int type, unsigned int flags)
21851da177e4SLinus Torvalds {
21861da177e4SLinus Torvalds 	unsigned long now = jiffies;
21871da177e4SLinus Torvalds 	struct nda_cacheinfo ci;
21888b8aec50SThomas Graf 	struct nlmsghdr *nlh;
21898b8aec50SThomas Graf 	struct ndmsg *ndm;
21901da177e4SLinus Torvalds 
21918b8aec50SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
21928b8aec50SThomas Graf 	if (nlh == NULL)
219326932566SPatrick McHardy 		return -EMSGSIZE;
21948b8aec50SThomas Graf 
21958b8aec50SThomas Graf 	ndm = nlmsg_data(nlh);
21968b8aec50SThomas Graf 	ndm->ndm_family	 = neigh->ops->family;
21979ef1d4c7SPatrick McHardy 	ndm->ndm_pad1    = 0;
21989ef1d4c7SPatrick McHardy 	ndm->ndm_pad2    = 0;
21998b8aec50SThomas Graf 	ndm->ndm_flags	 = neigh->flags;
22008b8aec50SThomas Graf 	ndm->ndm_type	 = neigh->type;
22018b8aec50SThomas Graf 	ndm->ndm_ifindex = neigh->dev->ifindex;
22021da177e4SLinus Torvalds 
22039a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
22049a6308d7SDavid S. Miller 		goto nla_put_failure;
22058b8aec50SThomas Graf 
22068b8aec50SThomas Graf 	read_lock_bh(&neigh->lock);
22078b8aec50SThomas Graf 	ndm->ndm_state	 = neigh->nud_state;
22080ed8ddf4SEric Dumazet 	if (neigh->nud_state & NUD_VALID) {
22090ed8ddf4SEric Dumazet 		char haddr[MAX_ADDR_LEN];
22100ed8ddf4SEric Dumazet 
22110ed8ddf4SEric Dumazet 		neigh_ha_snapshot(haddr, neigh, neigh->dev);
22120ed8ddf4SEric Dumazet 		if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
22138b8aec50SThomas Graf 			read_unlock_bh(&neigh->lock);
22148b8aec50SThomas Graf 			goto nla_put_failure;
22158b8aec50SThomas Graf 		}
22160ed8ddf4SEric Dumazet 	}
22178b8aec50SThomas Graf 
2218b9f5f52cSStephen Hemminger 	ci.ndm_used	 = jiffies_to_clock_t(now - neigh->used);
2219b9f5f52cSStephen Hemminger 	ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2220b9f5f52cSStephen Hemminger 	ci.ndm_updated	 = jiffies_to_clock_t(now - neigh->updated);
22218b8aec50SThomas Graf 	ci.ndm_refcnt	 = atomic_read(&neigh->refcnt) - 1;
22228b8aec50SThomas Graf 	read_unlock_bh(&neigh->lock);
22238b8aec50SThomas Graf 
22249a6308d7SDavid S. Miller 	if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
22259a6308d7SDavid S. Miller 	    nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
22269a6308d7SDavid S. Miller 		goto nla_put_failure;
22278b8aec50SThomas Graf 
22288b8aec50SThomas Graf 	return nlmsg_end(skb, nlh);
22298b8aec50SThomas Graf 
22308b8aec50SThomas Graf nla_put_failure:
223126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
223226932566SPatrick McHardy 	return -EMSGSIZE;
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
223584920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
223684920c14STony Zelenoff 			    u32 pid, u32 seq, int type, unsigned int flags,
223784920c14STony Zelenoff 			    struct neigh_table *tbl)
223884920c14STony Zelenoff {
223984920c14STony Zelenoff 	struct nlmsghdr *nlh;
224084920c14STony Zelenoff 	struct ndmsg *ndm;
224184920c14STony Zelenoff 
224284920c14STony Zelenoff 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
224384920c14STony Zelenoff 	if (nlh == NULL)
224484920c14STony Zelenoff 		return -EMSGSIZE;
224584920c14STony Zelenoff 
224684920c14STony Zelenoff 	ndm = nlmsg_data(nlh);
224784920c14STony Zelenoff 	ndm->ndm_family	 = tbl->family;
224884920c14STony Zelenoff 	ndm->ndm_pad1    = 0;
224984920c14STony Zelenoff 	ndm->ndm_pad2    = 0;
225084920c14STony Zelenoff 	ndm->ndm_flags	 = pn->flags | NTF_PROXY;
225184920c14STony Zelenoff 	ndm->ndm_type	 = NDA_DST;
225284920c14STony Zelenoff 	ndm->ndm_ifindex = pn->dev->ifindex;
225384920c14STony Zelenoff 	ndm->ndm_state	 = NUD_NONE;
225484920c14STony Zelenoff 
22559a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
22569a6308d7SDavid S. Miller 		goto nla_put_failure;
225784920c14STony Zelenoff 
225884920c14STony Zelenoff 	return nlmsg_end(skb, nlh);
225984920c14STony Zelenoff 
226084920c14STony Zelenoff nla_put_failure:
226184920c14STony Zelenoff 	nlmsg_cancel(skb, nlh);
226284920c14STony Zelenoff 	return -EMSGSIZE;
226384920c14STony Zelenoff }
226484920c14STony Zelenoff 
2265d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh)
2266d961db35SThomas Graf {
2267d961db35SThomas Graf 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2268d961db35SThomas Graf 	__neigh_notify(neigh, RTM_NEWNEIGH, 0);
2269d961db35SThomas Graf }
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
22721da177e4SLinus Torvalds 			    struct netlink_callback *cb)
22731da177e4SLinus Torvalds {
22743b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
22751da177e4SLinus Torvalds 	struct neighbour *n;
22761da177e4SLinus Torvalds 	int rc, h, s_h = cb->args[1];
22771da177e4SLinus Torvalds 	int idx, s_idx = idx = cb->args[2];
2278d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
22791da177e4SLinus Torvalds 
2280d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2281d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2282d6bf7817SEric Dumazet 
22834bd6683bSEric Dumazet 	for (h = s_h; h < (1 << nht->hash_shift); h++) {
22841da177e4SLinus Torvalds 		if (h > s_h)
22851da177e4SLinus Torvalds 			s_idx = 0;
2286767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2287767e97e1SEric Dumazet 		     n != NULL;
2288767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next)) {
228909ad9bc7SOctavian Purdila 			if (!net_eq(dev_net(n->dev), net))
2290426b5303SEric W. Biederman 				continue;
2291efc683fcSGautam Kachroo 			if (idx < s_idx)
2292efc683fcSGautam Kachroo 				goto next;
229315e47304SEric W. Biederman 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
22941da177e4SLinus Torvalds 					    cb->nlh->nlmsg_seq,
2295b6544c0bSJamal Hadi Salim 					    RTM_NEWNEIGH,
2296b6544c0bSJamal Hadi Salim 					    NLM_F_MULTI) <= 0) {
22971da177e4SLinus Torvalds 				rc = -1;
22981da177e4SLinus Torvalds 				goto out;
22991da177e4SLinus Torvalds 			}
2300efc683fcSGautam Kachroo next:
2301efc683fcSGautam Kachroo 			idx++;
23021da177e4SLinus Torvalds 		}
23031da177e4SLinus Torvalds 	}
23041da177e4SLinus Torvalds 	rc = skb->len;
23051da177e4SLinus Torvalds out:
2306d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
23071da177e4SLinus Torvalds 	cb->args[1] = h;
23081da177e4SLinus Torvalds 	cb->args[2] = idx;
23091da177e4SLinus Torvalds 	return rc;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
231284920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
231384920c14STony Zelenoff 			     struct netlink_callback *cb)
231484920c14STony Zelenoff {
231584920c14STony Zelenoff 	struct pneigh_entry *n;
231684920c14STony Zelenoff 	struct net *net = sock_net(skb->sk);
231784920c14STony Zelenoff 	int rc, h, s_h = cb->args[3];
231884920c14STony Zelenoff 	int idx, s_idx = idx = cb->args[4];
231984920c14STony Zelenoff 
232084920c14STony Zelenoff 	read_lock_bh(&tbl->lock);
232184920c14STony Zelenoff 
23224bd6683bSEric Dumazet 	for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
232384920c14STony Zelenoff 		if (h > s_h)
232484920c14STony Zelenoff 			s_idx = 0;
232584920c14STony Zelenoff 		for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
232684920c14STony Zelenoff 			if (dev_net(n->dev) != net)
232784920c14STony Zelenoff 				continue;
232884920c14STony Zelenoff 			if (idx < s_idx)
232984920c14STony Zelenoff 				goto next;
233015e47304SEric W. Biederman 			if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
233184920c14STony Zelenoff 					    cb->nlh->nlmsg_seq,
233284920c14STony Zelenoff 					    RTM_NEWNEIGH,
233384920c14STony Zelenoff 					    NLM_F_MULTI, tbl) <= 0) {
233484920c14STony Zelenoff 				read_unlock_bh(&tbl->lock);
233584920c14STony Zelenoff 				rc = -1;
233684920c14STony Zelenoff 				goto out;
233784920c14STony Zelenoff 			}
233884920c14STony Zelenoff 		next:
233984920c14STony Zelenoff 			idx++;
234084920c14STony Zelenoff 		}
234184920c14STony Zelenoff 	}
234284920c14STony Zelenoff 
234384920c14STony Zelenoff 	read_unlock_bh(&tbl->lock);
234484920c14STony Zelenoff 	rc = skb->len;
234584920c14STony Zelenoff out:
234684920c14STony Zelenoff 	cb->args[3] = h;
234784920c14STony Zelenoff 	cb->args[4] = idx;
234884920c14STony Zelenoff 	return rc;
234984920c14STony Zelenoff 
235084920c14STony Zelenoff }
235184920c14STony Zelenoff 
2352c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
23531da177e4SLinus Torvalds {
23541da177e4SLinus Torvalds 	struct neigh_table *tbl;
23551da177e4SLinus Torvalds 	int t, family, s_t;
235684920c14STony Zelenoff 	int proxy = 0;
23574bd6683bSEric Dumazet 	int err;
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	read_lock(&neigh_tbl_lock);
23608b8aec50SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
236184920c14STony Zelenoff 
236284920c14STony Zelenoff 	/* check for full ndmsg structure presence, family member is
236384920c14STony Zelenoff 	 * the same for both structures
236484920c14STony Zelenoff 	 */
236584920c14STony Zelenoff 	if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
236684920c14STony Zelenoff 	    ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
236784920c14STony Zelenoff 		proxy = 1;
236884920c14STony Zelenoff 
23691da177e4SLinus Torvalds 	s_t = cb->args[0];
23701da177e4SLinus Torvalds 
23714bd6683bSEric Dumazet 	for (tbl = neigh_tables, t = 0; tbl;
237284920c14STony Zelenoff 	     tbl = tbl->next, t++) {
23731da177e4SLinus Torvalds 		if (t < s_t || (family && tbl->family != family))
23741da177e4SLinus Torvalds 			continue;
23751da177e4SLinus Torvalds 		if (t > s_t)
23761da177e4SLinus Torvalds 			memset(&cb->args[1], 0, sizeof(cb->args) -
23771da177e4SLinus Torvalds 						sizeof(cb->args[0]));
237884920c14STony Zelenoff 		if (proxy)
237984920c14STony Zelenoff 			err = pneigh_dump_table(tbl, skb, cb);
238084920c14STony Zelenoff 		else
238184920c14STony Zelenoff 			err = neigh_dump_table(tbl, skb, cb);
23824bd6683bSEric Dumazet 		if (err < 0)
23834bd6683bSEric Dumazet 			break;
23841da177e4SLinus Torvalds 	}
23851da177e4SLinus Torvalds 	read_unlock(&neigh_tbl_lock);
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	cb->args[0] = t;
23881da177e4SLinus Torvalds 	return skb->len;
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
23921da177e4SLinus Torvalds {
23931da177e4SLinus Torvalds 	int chain;
2394d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
23951da177e4SLinus Torvalds 
2396d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2397d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2398d6bf7817SEric Dumazet 
2399767e97e1SEric Dumazet 	read_lock(&tbl->lock); /* avoid resizes */
2400cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
24011da177e4SLinus Torvalds 		struct neighbour *n;
24021da177e4SLinus Torvalds 
2403767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2404767e97e1SEric Dumazet 		     n != NULL;
2405767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next))
24061da177e4SLinus Torvalds 			cb(n, cookie);
24071da177e4SLinus Torvalds 	}
2408d6bf7817SEric Dumazet 	read_unlock(&tbl->lock);
2409d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each);
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */
24141da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl,
24151da177e4SLinus Torvalds 			      int (*cb)(struct neighbour *))
24161da177e4SLinus Torvalds {
24171da177e4SLinus Torvalds 	int chain;
2418d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
24191da177e4SLinus Torvalds 
2420d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
2421d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
2422cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2423767e97e1SEric Dumazet 		struct neighbour *n;
2424767e97e1SEric Dumazet 		struct neighbour __rcu **np;
24251da177e4SLinus Torvalds 
2426d6bf7817SEric Dumazet 		np = &nht->hash_buckets[chain];
2427767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
2428767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
24291da177e4SLinus Torvalds 			int release;
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 			write_lock(&n->lock);
24321da177e4SLinus Torvalds 			release = cb(n);
24331da177e4SLinus Torvalds 			if (release) {
2434767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
2435767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
2436767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
24371da177e4SLinus Torvalds 				n->dead = 1;
24381da177e4SLinus Torvalds 			} else
24391da177e4SLinus Torvalds 				np = &n->next;
24401da177e4SLinus Torvalds 			write_unlock(&n->lock);
24414f494554SThomas Graf 			if (release)
24424f494554SThomas Graf 				neigh_cleanup_and_release(n);
24431da177e4SLinus Torvalds 		}
24441da177e4SLinus Torvalds 	}
2445ecbb4169SAlexey Kuznetsov }
24461da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release);
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq)
24511da177e4SLinus Torvalds {
24521da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
24531218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2454d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
24551da177e4SLinus Torvalds 	struct neighbour *n = NULL;
24561da177e4SLinus Torvalds 	int bucket = state->bucket;
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2459cd089336SDavid S. Miller 	for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
2460767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[bucket]);
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 		while (n) {
2463878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2464426b5303SEric W. Biederman 				goto next;
24651da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
24661da177e4SLinus Torvalds 				loff_t fakep = 0;
24671da177e4SLinus Torvalds 				void *v;
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 				v = state->neigh_sub_iter(state, n, &fakep);
24701da177e4SLinus Torvalds 				if (!v)
24711da177e4SLinus Torvalds 					goto next;
24721da177e4SLinus Torvalds 			}
24731da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
24741da177e4SLinus Torvalds 				break;
24751da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
24761da177e4SLinus Torvalds 				break;
24771da177e4SLinus Torvalds next:
2478767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
24791da177e4SLinus Torvalds 		}
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 		if (n)
24821da177e4SLinus Torvalds 			break;
24831da177e4SLinus Torvalds 	}
24841da177e4SLinus Torvalds 	state->bucket = bucket;
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 	return n;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq,
24901da177e4SLinus Torvalds 					struct neighbour *n,
24911da177e4SLinus Torvalds 					loff_t *pos)
24921da177e4SLinus Torvalds {
24931da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
24941218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2495d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	if (state->neigh_sub_iter) {
24981da177e4SLinus Torvalds 		void *v = state->neigh_sub_iter(state, n, pos);
24991da177e4SLinus Torvalds 		if (v)
25001da177e4SLinus Torvalds 			return n;
25011da177e4SLinus Torvalds 	}
2502767e97e1SEric Dumazet 	n = rcu_dereference_bh(n->next);
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	while (1) {
25051da177e4SLinus Torvalds 		while (n) {
2506878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2507426b5303SEric W. Biederman 				goto next;
25081da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
25091da177e4SLinus Torvalds 				void *v = state->neigh_sub_iter(state, n, pos);
25101da177e4SLinus Torvalds 				if (v)
25111da177e4SLinus Torvalds 					return n;
25121da177e4SLinus Torvalds 				goto next;
25131da177e4SLinus Torvalds 			}
25141da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
25151da177e4SLinus Torvalds 				break;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
25181da177e4SLinus Torvalds 				break;
25191da177e4SLinus Torvalds next:
2520767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
25211da177e4SLinus Torvalds 		}
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 		if (n)
25241da177e4SLinus Torvalds 			break;
25251da177e4SLinus Torvalds 
2526cd089336SDavid S. Miller 		if (++state->bucket >= (1 << nht->hash_shift))
25271da177e4SLinus Torvalds 			break;
25281da177e4SLinus Torvalds 
2529767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
25301da177e4SLinus Torvalds 	}
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	if (n && pos)
25331da177e4SLinus Torvalds 		--(*pos);
25341da177e4SLinus Torvalds 	return n;
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
25381da177e4SLinus Torvalds {
25391da177e4SLinus Torvalds 	struct neighbour *n = neigh_get_first(seq);
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	if (n) {
2542745e2031SChris Larson 		--(*pos);
25431da177e4SLinus Torvalds 		while (*pos) {
25441da177e4SLinus Torvalds 			n = neigh_get_next(seq, n, pos);
25451da177e4SLinus Torvalds 			if (!n)
25461da177e4SLinus Torvalds 				break;
25471da177e4SLinus Torvalds 		}
25481da177e4SLinus Torvalds 	}
25491da177e4SLinus Torvalds 	return *pos ? NULL : n;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
25531da177e4SLinus Torvalds {
25541da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25551218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
25561da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
25571da177e4SLinus Torvalds 	struct pneigh_entry *pn = NULL;
25581da177e4SLinus Torvalds 	int bucket = state->bucket;
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
25611da177e4SLinus Torvalds 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
25621da177e4SLinus Torvalds 		pn = tbl->phash_buckets[bucket];
2563878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2564426b5303SEric W. Biederman 			pn = pn->next;
25651da177e4SLinus Torvalds 		if (pn)
25661da177e4SLinus Torvalds 			break;
25671da177e4SLinus Torvalds 	}
25681da177e4SLinus Torvalds 	state->bucket = bucket;
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	return pn;
25711da177e4SLinus Torvalds }
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
25741da177e4SLinus Torvalds 					    struct pneigh_entry *pn,
25751da177e4SLinus Torvalds 					    loff_t *pos)
25761da177e4SLinus Torvalds {
25771da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25781218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
25791da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
25801da177e4SLinus Torvalds 
2581df07a94cSJorge Boncompte [DTI2] 	do {
25821da177e4SLinus Torvalds 		pn = pn->next;
2583df07a94cSJorge Boncompte [DTI2] 	} while (pn && !net_eq(pneigh_net(pn), net));
2584df07a94cSJorge Boncompte [DTI2] 
25851da177e4SLinus Torvalds 	while (!pn) {
25861da177e4SLinus Torvalds 		if (++state->bucket > PNEIGH_HASHMASK)
25871da177e4SLinus Torvalds 			break;
25881da177e4SLinus Torvalds 		pn = tbl->phash_buckets[state->bucket];
2589878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2590426b5303SEric W. Biederman 			pn = pn->next;
25911da177e4SLinus Torvalds 		if (pn)
25921da177e4SLinus Torvalds 			break;
25931da177e4SLinus Torvalds 	}
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	if (pn && pos)
25961da177e4SLinus Torvalds 		--(*pos);
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	return pn;
25991da177e4SLinus Torvalds }
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
26021da177e4SLinus Torvalds {
26031da177e4SLinus Torvalds 	struct pneigh_entry *pn = pneigh_get_first(seq);
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds 	if (pn) {
2606745e2031SChris Larson 		--(*pos);
26071da177e4SLinus Torvalds 		while (*pos) {
26081da177e4SLinus Torvalds 			pn = pneigh_get_next(seq, pn, pos);
26091da177e4SLinus Torvalds 			if (!pn)
26101da177e4SLinus Torvalds 				break;
26111da177e4SLinus Torvalds 		}
26121da177e4SLinus Torvalds 	}
26131da177e4SLinus Torvalds 	return *pos ? NULL : pn;
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
26171da177e4SLinus Torvalds {
26181da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26191da177e4SLinus Torvalds 	void *rc;
2620745e2031SChris Larson 	loff_t idxpos = *pos;
26211da177e4SLinus Torvalds 
2622745e2031SChris Larson 	rc = neigh_get_idx(seq, &idxpos);
26231da177e4SLinus Torvalds 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2624745e2031SChris Larson 		rc = pneigh_get_idx(seq, &idxpos);
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	return rc;
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2630d6bf7817SEric Dumazet 	__acquires(rcu_bh)
26311da177e4SLinus Torvalds {
26321da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	state->tbl = tbl;
26351da177e4SLinus Torvalds 	state->bucket = 0;
26361da177e4SLinus Torvalds 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
26371da177e4SLinus Torvalds 
2638d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2639d6bf7817SEric Dumazet 	state->nht = rcu_dereference_bh(tbl->nht);
2640767e97e1SEric Dumazet 
2641745e2031SChris Larson 	return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start);
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26461da177e4SLinus Torvalds {
26471da177e4SLinus Torvalds 	struct neigh_seq_state *state;
26481da177e4SLinus Torvalds 	void *rc;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2651bff69732SChris Larson 		rc = neigh_get_first(seq);
26521da177e4SLinus Torvalds 		goto out;
26531da177e4SLinus Torvalds 	}
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds 	state = seq->private;
26561da177e4SLinus Torvalds 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
26571da177e4SLinus Torvalds 		rc = neigh_get_next(seq, v, NULL);
26581da177e4SLinus Torvalds 		if (rc)
26591da177e4SLinus Torvalds 			goto out;
26601da177e4SLinus Torvalds 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
26611da177e4SLinus Torvalds 			rc = pneigh_get_first(seq);
26621da177e4SLinus Torvalds 	} else {
26631da177e4SLinus Torvalds 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
26641da177e4SLinus Torvalds 		rc = pneigh_get_next(seq, v, NULL);
26651da177e4SLinus Torvalds 	}
26661da177e4SLinus Torvalds out:
26671da177e4SLinus Torvalds 	++(*pos);
26681da177e4SLinus Torvalds 	return rc;
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next);
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v)
2673d6bf7817SEric Dumazet 	__releases(rcu_bh)
26741da177e4SLinus Torvalds {
2675d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
26761da177e4SLinus Torvalds }
26771da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop);
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds /* statistics via seq_file */
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
26821da177e4SLinus Torvalds {
268381c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
26841da177e4SLinus Torvalds 	int cpu;
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 	if (*pos == 0)
26871da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
26881da177e4SLinus Torvalds 
26890f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
26901da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
26911da177e4SLinus Torvalds 			continue;
26921da177e4SLinus Torvalds 		*pos = cpu+1;
26931da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
26941da177e4SLinus Torvalds 	}
26951da177e4SLinus Torvalds 	return NULL;
26961da177e4SLinus Torvalds }
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26991da177e4SLinus Torvalds {
270081c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27011da177e4SLinus Torvalds 	int cpu;
27021da177e4SLinus Torvalds 
27030f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
27041da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
27051da177e4SLinus Torvalds 			continue;
27061da177e4SLinus Torvalds 		*pos = cpu+1;
27071da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
27081da177e4SLinus Torvalds 	}
27091da177e4SLinus Torvalds 	return NULL;
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
27131da177e4SLinus Torvalds {
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v)
27181da177e4SLinus Torvalds {
271981c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27201da177e4SLinus Torvalds 	struct neigh_statistics *st = v;
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
27239a6d276eSNeil 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");
27241da177e4SLinus Torvalds 		return 0;
27251da177e4SLinus Torvalds 	}
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
27289a6d276eSNeil Horman 			"%08lx %08lx  %08lx %08lx %08lx\n",
27291da177e4SLinus Torvalds 		   atomic_read(&tbl->entries),
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 		   st->allocs,
27321da177e4SLinus Torvalds 		   st->destroys,
27331da177e4SLinus Torvalds 		   st->hash_grows,
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 		   st->lookups,
27361da177e4SLinus Torvalds 		   st->hits,
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 		   st->res_failed,
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 		   st->rcv_probes_mcast,
27411da177e4SLinus Torvalds 		   st->rcv_probes_ucast,
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds 		   st->periodic_gc_runs,
27449a6d276eSNeil Horman 		   st->forced_gc_runs,
27459a6d276eSNeil Horman 		   st->unres_discards
27461da177e4SLinus Torvalds 		   );
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	return 0;
27491da177e4SLinus Torvalds }
27501da177e4SLinus Torvalds 
2751f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = {
27521da177e4SLinus Torvalds 	.start	= neigh_stat_seq_start,
27531da177e4SLinus Torvalds 	.next	= neigh_stat_seq_next,
27541da177e4SLinus Torvalds 	.stop	= neigh_stat_seq_stop,
27551da177e4SLinus Torvalds 	.show	= neigh_stat_seq_show,
27561da177e4SLinus Torvalds };
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file)
27591da177e4SLinus Torvalds {
27601da177e4SLinus Torvalds 	int ret = seq_open(file, &neigh_stat_seq_ops);
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds 	if (!ret) {
27631da177e4SLinus Torvalds 		struct seq_file *sf = file->private_data;
2764d9dda78bSAl Viro 		sf->private = PDE_DATA(inode);
27651da177e4SLinus Torvalds 	}
27661da177e4SLinus Torvalds 	return ret;
27671da177e4SLinus Torvalds };
27681da177e4SLinus Torvalds 
27699a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = {
27701da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27711da177e4SLinus Torvalds 	.open 	 = neigh_stat_seq_open,
27721da177e4SLinus Torvalds 	.read	 = seq_read,
27731da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
27741da177e4SLinus Torvalds 	.release = seq_release,
27751da177e4SLinus Torvalds };
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
27781da177e4SLinus Torvalds 
2779339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void)
2780339bf98fSThomas Graf {
2781339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ndmsg))
2782339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2783339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2784339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct nda_cacheinfo))
2785339bf98fSThomas Graf 	       + nla_total_size(4); /* NDA_PROBES */
2786339bf98fSThomas Graf }
2787339bf98fSThomas Graf 
2788b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags)
27891da177e4SLinus Torvalds {
2790c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(n->dev);
27918b8aec50SThomas Graf 	struct sk_buff *skb;
2792b8673311SThomas Graf 	int err = -ENOBUFS;
27931da177e4SLinus Torvalds 
2794339bf98fSThomas Graf 	skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
27958b8aec50SThomas Graf 	if (skb == NULL)
2796b8673311SThomas Graf 		goto errout;
27971da177e4SLinus Torvalds 
2798b8673311SThomas Graf 	err = neigh_fill_info(skb, n, 0, 0, type, flags);
279926932566SPatrick McHardy 	if (err < 0) {
280026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
280126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
280226932566SPatrick McHardy 		kfree_skb(skb);
280326932566SPatrick McHardy 		goto errout;
280426932566SPatrick McHardy 	}
28051ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
28061ce85fe4SPablo Neira Ayuso 	return;
2807b8673311SThomas Graf errout:
2808b8673311SThomas Graf 	if (err < 0)
2809426b5303SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2810b8673311SThomas Graf }
2811b8673311SThomas Graf 
2812b8673311SThomas Graf void neigh_app_ns(struct neighbour *n)
2813b8673311SThomas Graf {
2814b8673311SThomas Graf 	__neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
28158b8aec50SThomas Graf }
28160a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns);
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
2819b93196dcSCong Wang static int zero;
2820555445cdSFrancesco Fusco static int int_max = INT_MAX;
2821b93196dcSCong Wang static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
28221da177e4SLinus Torvalds 
2823fe2c6338SJoe Perches static int proc_unres_qlen(struct ctl_table *ctl, int write,
2824fe2c6338SJoe Perches 			   void __user *buffer, size_t *lenp, loff_t *ppos)
28258b5c171bSEric Dumazet {
28268b5c171bSEric Dumazet 	int size, ret;
2827fe2c6338SJoe Perches 	struct ctl_table tmp = *ctl;
28288b5c171bSEric Dumazet 
2829ce46cc64SShan Wei 	tmp.extra1 = &zero;
2830ce46cc64SShan Wei 	tmp.extra2 = &unres_qlen_max;
28318b5c171bSEric Dumazet 	tmp.data = &size;
2832ce46cc64SShan Wei 
2833ce46cc64SShan Wei 	size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
2834ce46cc64SShan Wei 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2835ce46cc64SShan Wei 
28368b5c171bSEric Dumazet 	if (write && !ret)
28378b5c171bSEric Dumazet 		*(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
28388b5c171bSEric Dumazet 	return ret;
28398b5c171bSEric Dumazet }
28408b5c171bSEric Dumazet 
28411d4c8c29SJiri Pirko static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
28421d4c8c29SJiri Pirko 						   int family)
28431d4c8c29SJiri Pirko {
2844bba24896SJiri Pirko 	switch (family) {
2845bba24896SJiri Pirko 	case AF_INET:
28461d4c8c29SJiri Pirko 		return __in_dev_arp_parms_get_rcu(dev);
2847bba24896SJiri Pirko 	case AF_INET6:
2848bba24896SJiri Pirko 		return __in6_dev_nd_parms_get_rcu(dev);
2849bba24896SJiri Pirko 	}
28501d4c8c29SJiri Pirko 	return NULL;
28511d4c8c29SJiri Pirko }
28521d4c8c29SJiri Pirko 
28531d4c8c29SJiri Pirko static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
28541d4c8c29SJiri Pirko 				  int index)
28551d4c8c29SJiri Pirko {
28561d4c8c29SJiri Pirko 	struct net_device *dev;
28571d4c8c29SJiri Pirko 	int family = neigh_parms_family(p);
28581d4c8c29SJiri Pirko 
28591d4c8c29SJiri Pirko 	rcu_read_lock();
28601d4c8c29SJiri Pirko 	for_each_netdev_rcu(net, dev) {
28611d4c8c29SJiri Pirko 		struct neigh_parms *dst_p =
28621d4c8c29SJiri Pirko 				neigh_get_dev_parms_rcu(dev, family);
28631d4c8c29SJiri Pirko 
28641d4c8c29SJiri Pirko 		if (dst_p && !test_bit(index, dst_p->data_state))
28651d4c8c29SJiri Pirko 			dst_p->data[index] = p->data[index];
28661d4c8c29SJiri Pirko 	}
28671d4c8c29SJiri Pirko 	rcu_read_unlock();
28681d4c8c29SJiri Pirko }
28691d4c8c29SJiri Pirko 
28701d4c8c29SJiri Pirko static void neigh_proc_update(struct ctl_table *ctl, int write)
28711d4c8c29SJiri Pirko {
28721d4c8c29SJiri Pirko 	struct net_device *dev = ctl->extra1;
28731d4c8c29SJiri Pirko 	struct neigh_parms *p = ctl->extra2;
287477d47afbSJiri Pirko 	struct net *net = neigh_parms_net(p);
28751d4c8c29SJiri Pirko 	int index = (int *) ctl->data - p->data;
28761d4c8c29SJiri Pirko 
28771d4c8c29SJiri Pirko 	if (!write)
28781d4c8c29SJiri Pirko 		return;
28791d4c8c29SJiri Pirko 
28801d4c8c29SJiri Pirko 	set_bit(index, p->data_state);
28811d4c8c29SJiri Pirko 	if (!dev) /* NULL dev means this is default value */
28821d4c8c29SJiri Pirko 		neigh_copy_dflt_parms(net, p, index);
28831d4c8c29SJiri Pirko }
28841d4c8c29SJiri Pirko 
28851f9248e5SJiri Pirko static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
28861f9248e5SJiri Pirko 					   void __user *buffer,
28871f9248e5SJiri Pirko 					   size_t *lenp, loff_t *ppos)
28881f9248e5SJiri Pirko {
28891f9248e5SJiri Pirko 	struct ctl_table tmp = *ctl;
28901d4c8c29SJiri Pirko 	int ret;
28911f9248e5SJiri Pirko 
28921f9248e5SJiri Pirko 	tmp.extra1 = &zero;
28931f9248e5SJiri Pirko 	tmp.extra2 = &int_max;
28941f9248e5SJiri Pirko 
28951d4c8c29SJiri Pirko 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
28961d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
28971d4c8c29SJiri Pirko 	return ret;
28981f9248e5SJiri Pirko }
28991f9248e5SJiri Pirko 
2900cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write,
2901cb5b09c1SJiri Pirko 			void __user *buffer, size_t *lenp, loff_t *ppos)
2902cb5b09c1SJiri Pirko {
29031d4c8c29SJiri Pirko 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
29041d4c8c29SJiri Pirko 
29051d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29061d4c8c29SJiri Pirko 	return ret;
2907cb5b09c1SJiri Pirko }
2908cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec);
2909cb5b09c1SJiri Pirko 
2910cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
2911cb5b09c1SJiri Pirko 				void __user *buffer,
2912cb5b09c1SJiri Pirko 				size_t *lenp, loff_t *ppos)
2913cb5b09c1SJiri Pirko {
29141d4c8c29SJiri Pirko 	int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
29151d4c8c29SJiri Pirko 
29161d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29171d4c8c29SJiri Pirko 	return ret;
2918cb5b09c1SJiri Pirko }
2919cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
2920cb5b09c1SJiri Pirko 
2921cb5b09c1SJiri Pirko static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
2922cb5b09c1SJiri Pirko 					      void __user *buffer,
2923cb5b09c1SJiri Pirko 					      size_t *lenp, loff_t *ppos)
2924cb5b09c1SJiri Pirko {
29251d4c8c29SJiri Pirko 	int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
29261d4c8c29SJiri Pirko 
29271d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29281d4c8c29SJiri Pirko 	return ret;
2929cb5b09c1SJiri Pirko }
2930cb5b09c1SJiri Pirko 
2931cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
2932cb5b09c1SJiri Pirko 				   void __user *buffer,
2933cb5b09c1SJiri Pirko 				   size_t *lenp, loff_t *ppos)
2934cb5b09c1SJiri Pirko {
29351d4c8c29SJiri Pirko 	int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
29361d4c8c29SJiri Pirko 
29371d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29381d4c8c29SJiri Pirko 	return ret;
2939cb5b09c1SJiri Pirko }
2940cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
2941cb5b09c1SJiri Pirko 
2942cb5b09c1SJiri Pirko static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
2943cb5b09c1SJiri Pirko 					  void __user *buffer,
2944cb5b09c1SJiri Pirko 					  size_t *lenp, loff_t *ppos)
2945cb5b09c1SJiri Pirko {
29461d4c8c29SJiri Pirko 	int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
29471d4c8c29SJiri Pirko 
29481d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29491d4c8c29SJiri Pirko 	return ret;
2950cb5b09c1SJiri Pirko }
2951cb5b09c1SJiri Pirko 
29521f9248e5SJiri Pirko #define NEIGH_PARMS_DATA_OFFSET(index)	\
29531f9248e5SJiri Pirko 	(&((struct neigh_parms *) 0)->data[index])
29541f9248e5SJiri Pirko 
29551f9248e5SJiri Pirko #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
29561f9248e5SJiri Pirko 	[NEIGH_VAR_ ## attr] = { \
29571f9248e5SJiri Pirko 		.procname	= name, \
29581f9248e5SJiri Pirko 		.data		= NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
29591f9248e5SJiri Pirko 		.maxlen		= sizeof(int), \
29601f9248e5SJiri Pirko 		.mode		= mval, \
29611f9248e5SJiri Pirko 		.proc_handler	= proc, \
29621f9248e5SJiri Pirko 	}
29631f9248e5SJiri Pirko 
29641f9248e5SJiri Pirko #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
29651f9248e5SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
29661f9248e5SJiri Pirko 
29671f9248e5SJiri Pirko #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
2968cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
29691f9248e5SJiri Pirko 
29701f9248e5SJiri Pirko #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
2971cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
29721f9248e5SJiri Pirko 
29731f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
2974cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
29751f9248e5SJiri Pirko 
29761f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
2977cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
29781f9248e5SJiri Pirko 
29791f9248e5SJiri Pirko #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
2980cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
298154716e3bSEric W. Biederman 
29821da177e4SLinus Torvalds static struct neigh_sysctl_table {
29831da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
29848b5c171bSEric Dumazet 	struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
2985ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = {
29861da177e4SLinus Torvalds 	.neigh_vars = {
29871f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
29881f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
29891f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
29901f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
29911f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
29921f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
29931f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
29941f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
29951f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
29961f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
29971f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
29981f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
29991f9248e5SJiri Pirko 		NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
30001f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
30011f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
30028b5c171bSEric Dumazet 		[NEIGH_VAR_GC_INTERVAL] = {
30031da177e4SLinus Torvalds 			.procname	= "gc_interval",
30041da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
30051da177e4SLinus Torvalds 			.mode		= 0644,
30066d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
30071da177e4SLinus Torvalds 		},
30088b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH1] = {
30091da177e4SLinus Torvalds 			.procname	= "gc_thresh1",
30101da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
30111da177e4SLinus Torvalds 			.mode		= 0644,
3012555445cdSFrancesco Fusco 			.extra1 	= &zero,
3013555445cdSFrancesco Fusco 			.extra2		= &int_max,
3014555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
30151da177e4SLinus Torvalds 		},
30168b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH2] = {
30171da177e4SLinus Torvalds 			.procname	= "gc_thresh2",
30181da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
30191da177e4SLinus Torvalds 			.mode		= 0644,
3020555445cdSFrancesco Fusco 			.extra1 	= &zero,
3021555445cdSFrancesco Fusco 			.extra2		= &int_max,
3022555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
30231da177e4SLinus Torvalds 		},
30248b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH3] = {
30251da177e4SLinus Torvalds 			.procname	= "gc_thresh3",
30261da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
30271da177e4SLinus Torvalds 			.mode		= 0644,
3028555445cdSFrancesco Fusco 			.extra1 	= &zero,
3029555445cdSFrancesco Fusco 			.extra2		= &int_max,
3030555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
30311da177e4SLinus Torvalds 		},
3032c3bac5a7SPavel Emelyanov 		{},
30331da177e4SLinus Torvalds 	},
30341da177e4SLinus Torvalds };
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
303773af614aSJiri Pirko 			  proc_handler *handler)
30381da177e4SLinus Torvalds {
30391f9248e5SJiri Pirko 	int i;
30403c607bbbSPavel Emelyanov 	struct neigh_sysctl_table *t;
30411f9248e5SJiri Pirko 	const char *dev_name_source;
30428f40a1f9SEric W. Biederman 	char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
304373af614aSJiri Pirko 	char *p_name;
30441da177e4SLinus Torvalds 
30453c607bbbSPavel Emelyanov 	t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
30461da177e4SLinus Torvalds 	if (!t)
30473c607bbbSPavel Emelyanov 		goto err;
30483c607bbbSPavel Emelyanov 
3049b194c1f1SJiri Pirko 	for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
30501f9248e5SJiri Pirko 		t->neigh_vars[i].data += (long) p;
3051cb5b09c1SJiri Pirko 		t->neigh_vars[i].extra1 = dev;
30521d4c8c29SJiri Pirko 		t->neigh_vars[i].extra2 = p;
3053cb5b09c1SJiri Pirko 	}
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds 	if (dev) {
30561da177e4SLinus Torvalds 		dev_name_source = dev->name;
3057d12af679SEric W. Biederman 		/* Terminate the table early */
30588b5c171bSEric Dumazet 		memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
30598b5c171bSEric Dumazet 		       sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
30601da177e4SLinus Torvalds 	} else {
30618f40a1f9SEric W. Biederman 		dev_name_source = "default";
30628b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = (int *)(p + 1);
30638b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = (int *)(p + 1) + 1;
30648b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = (int *)(p + 1) + 2;
30658b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = (int *)(p + 1) + 3;
30661da177e4SLinus Torvalds 	}
30671da177e4SLinus Torvalds 
3068f8572d8fSEric W. Biederman 	if (handler) {
30691da177e4SLinus Torvalds 		/* RetransTime */
30708b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
30711da177e4SLinus Torvalds 		/* ReachableTime */
30728b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
30731da177e4SLinus Torvalds 		/* RetransTime (in milliseconds)*/
30748b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
30751da177e4SLinus Torvalds 		/* ReachableTime (in milliseconds) */
30768b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
30771da177e4SLinus Torvalds 	}
30781da177e4SLinus Torvalds 
3079464dc801SEric W. Biederman 	/* Don't export sysctls to unprivileged users */
3080464dc801SEric W. Biederman 	if (neigh_parms_net(p)->user_ns != &init_user_ns)
3081464dc801SEric W. Biederman 		t->neigh_vars[0].procname = NULL;
3082464dc801SEric W. Biederman 
308373af614aSJiri Pirko 	switch (neigh_parms_family(p)) {
308473af614aSJiri Pirko 	case AF_INET:
308573af614aSJiri Pirko 	      p_name = "ipv4";
308673af614aSJiri Pirko 	      break;
308773af614aSJiri Pirko 	case AF_INET6:
308873af614aSJiri Pirko 	      p_name = "ipv6";
308973af614aSJiri Pirko 	      break;
309073af614aSJiri Pirko 	default:
309173af614aSJiri Pirko 	      BUG();
309273af614aSJiri Pirko 	}
309373af614aSJiri Pirko 
30948f40a1f9SEric W. Biederman 	snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
30958f40a1f9SEric W. Biederman 		p_name, dev_name_source);
30964ab438fcSDenis V. Lunev 	t->sysctl_header =
30978f40a1f9SEric W. Biederman 		register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
30983c607bbbSPavel Emelyanov 	if (!t->sysctl_header)
30998f40a1f9SEric W. Biederman 		goto free;
31003c607bbbSPavel Emelyanov 
31011da177e4SLinus Torvalds 	p->sysctl_table = t;
31021da177e4SLinus Torvalds 	return 0;
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds free:
31051da177e4SLinus Torvalds 	kfree(t);
31063c607bbbSPavel Emelyanov err:
31073c607bbbSPavel Emelyanov 	return -ENOBUFS;
31081da177e4SLinus Torvalds }
31090a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register);
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p)
31121da177e4SLinus Torvalds {
31131da177e4SLinus Torvalds 	if (p->sysctl_table) {
31141da177e4SLinus Torvalds 		struct neigh_sysctl_table *t = p->sysctl_table;
31151da177e4SLinus Torvalds 		p->sysctl_table = NULL;
31165dd3df10SEric W. Biederman 		unregister_net_sysctl_table(t->sysctl_header);
31171da177e4SLinus Torvalds 		kfree(t);
31181da177e4SLinus Torvalds 	}
31191da177e4SLinus Torvalds }
31200a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister);
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds #endif	/* CONFIG_SYSCTL */
31231da177e4SLinus Torvalds 
3124c8822a4eSThomas Graf static int __init neigh_init(void)
3125c8822a4eSThomas Graf {
3126c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
3127c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
3128c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
3129c8822a4eSThomas Graf 
3130c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3131c7ac8679SGreg Rose 		      NULL);
3132c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
3133c8822a4eSThomas Graf 
3134c8822a4eSThomas Graf 	return 0;
3135c8822a4eSThomas Graf }
3136c8822a4eSThomas Graf 
3137c8822a4eSThomas Graf subsys_initcall(neigh_init);
3138c8822a4eSThomas Graf 
3139