xref: /openbmc/linux/net/core/neighbour.c (revision e99e88a9)
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 
54e99e88a9SKees Cook static void neigh_timer_handler(struct timer_list *t);
557b8f7a40SRoopa Prabhu static void __neigh_notify(struct neighbour *n, int type, int flags,
567b8f7a40SRoopa Prabhu 			   u32 pid);
577b8f7a40SRoopa Prabhu static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid);
581da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
591da177e4SLinus Torvalds 
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 
928f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	kfree_skb(skb);
951da177e4SLinus Torvalds 	return -ENETDOWN;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
984f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh)
994f494554SThomas Graf {
1004f494554SThomas Graf 	if (neigh->parms->neigh_cleanup)
1014f494554SThomas Graf 		neigh->parms->neigh_cleanup(neigh);
1024f494554SThomas Graf 
1037b8f7a40SRoopa Prabhu 	__neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
10453f800e3SIdo Schimmel 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
1054f494554SThomas Graf 	neigh_release(neigh);
1064f494554SThomas Graf }
1074f494554SThomas Graf 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * It is random distribution in the interval (1/2)*base...(3/2)*base.
1101da177e4SLinus Torvalds  * It corresponds to default IPv6 settings and is not overridable,
1111da177e4SLinus Torvalds  * because it is really reasonable choice.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base)
1151da177e4SLinus Torvalds {
11663862b5bSAruna-Hewapathirane 	return base ? (prandom_u32() % base) + (base >> 1) : 0;
1171da177e4SLinus Torvalds }
1180a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 
1215071034eSSowmini Varadhan static bool neigh_del(struct neighbour *n, __u8 state,
1225071034eSSowmini Varadhan 		      struct neighbour __rcu **np, struct neigh_table *tbl)
1235071034eSSowmini Varadhan {
1245071034eSSowmini Varadhan 	bool retval = false;
1255071034eSSowmini Varadhan 
1265071034eSSowmini Varadhan 	write_lock(&n->lock);
1279f237430SReshetova, Elena 	if (refcount_read(&n->refcnt) == 1 && !(n->nud_state & state)) {
1285071034eSSowmini Varadhan 		struct neighbour *neigh;
1295071034eSSowmini Varadhan 
1305071034eSSowmini Varadhan 		neigh = rcu_dereference_protected(n->next,
1315071034eSSowmini Varadhan 						  lockdep_is_held(&tbl->lock));
1325071034eSSowmini Varadhan 		rcu_assign_pointer(*np, neigh);
1335071034eSSowmini Varadhan 		n->dead = 1;
1345071034eSSowmini Varadhan 		retval = true;
1355071034eSSowmini Varadhan 	}
1365071034eSSowmini Varadhan 	write_unlock(&n->lock);
1375071034eSSowmini Varadhan 	if (retval)
1385071034eSSowmini Varadhan 		neigh_cleanup_and_release(n);
1395071034eSSowmini Varadhan 	return retval;
1405071034eSSowmini Varadhan }
1415071034eSSowmini Varadhan 
1425071034eSSowmini Varadhan bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
1435071034eSSowmini Varadhan {
1445071034eSSowmini Varadhan 	struct neigh_hash_table *nht;
1455071034eSSowmini Varadhan 	void *pkey = ndel->primary_key;
1465071034eSSowmini Varadhan 	u32 hash_val;
1475071034eSSowmini Varadhan 	struct neighbour *n;
1485071034eSSowmini Varadhan 	struct neighbour __rcu **np;
1495071034eSSowmini Varadhan 
1505071034eSSowmini Varadhan 	nht = rcu_dereference_protected(tbl->nht,
1515071034eSSowmini Varadhan 					lockdep_is_held(&tbl->lock));
1525071034eSSowmini Varadhan 	hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
1535071034eSSowmini Varadhan 	hash_val = hash_val >> (32 - nht->hash_shift);
1545071034eSSowmini Varadhan 
1555071034eSSowmini Varadhan 	np = &nht->hash_buckets[hash_val];
1565071034eSSowmini Varadhan 	while ((n = rcu_dereference_protected(*np,
1575071034eSSowmini Varadhan 					      lockdep_is_held(&tbl->lock)))) {
1585071034eSSowmini Varadhan 		if (n == ndel)
1595071034eSSowmini Varadhan 			return neigh_del(n, 0, np, tbl);
1605071034eSSowmini Varadhan 		np = &n->next;
1615071034eSSowmini Varadhan 	}
1625071034eSSowmini Varadhan 	return false;
1635071034eSSowmini Varadhan }
1645071034eSSowmini Varadhan 
1651da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	int shrunk = 0;
1681da177e4SLinus Torvalds 	int i;
169d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
174d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
175d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
176cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
177767e97e1SEric Dumazet 		struct neighbour *n;
178767e97e1SEric Dumazet 		struct neighbour __rcu **np;
1791da177e4SLinus Torvalds 
180d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
181767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
182767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
1831da177e4SLinus Torvalds 			/* Neighbour record may be discarded if:
1841da177e4SLinus Torvalds 			 * - nobody refers to it.
1851da177e4SLinus Torvalds 			 * - it is not permanent
1861da177e4SLinus Torvalds 			 */
1875071034eSSowmini Varadhan 			if (neigh_del(n, NUD_PERMANENT, np, tbl)) {
1881da177e4SLinus Torvalds 				shrunk = 1;
1891da177e4SLinus Torvalds 				continue;
1901da177e4SLinus Torvalds 			}
1911da177e4SLinus Torvalds 			np = &n->next;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	tbl->last_flush = jiffies;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	return shrunk;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
202a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when)
203a43d8994SPavel Emelyanov {
204a43d8994SPavel Emelyanov 	neigh_hold(n);
205a43d8994SPavel Emelyanov 	if (unlikely(mod_timer(&n->timer, when))) {
206a43d8994SPavel Emelyanov 		printk("NEIGH: BUG, double timer add, state is %x\n",
207a43d8994SPavel Emelyanov 		       n->nud_state);
208a43d8994SPavel Emelyanov 		dump_stack();
209a43d8994SPavel Emelyanov 	}
210a43d8994SPavel Emelyanov }
211a43d8994SPavel Emelyanov 
2121da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	if ((n->nud_state & NUD_IN_TIMER) &&
2151da177e4SLinus Torvalds 	    del_timer(&n->timer)) {
2161da177e4SLinus Torvalds 		neigh_release(n);
2171da177e4SLinus Torvalds 		return 1;
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 	return 0;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	struct sk_buff *skb;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL) {
2271da177e4SLinus Torvalds 		dev_put(skb->dev);
2281da177e4SLinus Torvalds 		kfree_skb(skb);
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
23249636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	int i;
235d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
2361da177e4SLinus Torvalds 
237d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
238d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
239d6bf7817SEric Dumazet 
240cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
241767e97e1SEric Dumazet 		struct neighbour *n;
242767e97e1SEric Dumazet 		struct neighbour __rcu **np = &nht->hash_buckets[i];
2431da177e4SLinus Torvalds 
244767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
245767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
2461da177e4SLinus Torvalds 			if (dev && n->dev != dev) {
2471da177e4SLinus Torvalds 				np = &n->next;
2481da177e4SLinus Torvalds 				continue;
2491da177e4SLinus Torvalds 			}
250767e97e1SEric Dumazet 			rcu_assign_pointer(*np,
251767e97e1SEric Dumazet 				   rcu_dereference_protected(n->next,
252767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
2531da177e4SLinus Torvalds 			write_lock(&n->lock);
2541da177e4SLinus Torvalds 			neigh_del_timer(n);
2551da177e4SLinus Torvalds 			n->dead = 1;
2561da177e4SLinus Torvalds 
2579f237430SReshetova, Elena 			if (refcount_read(&n->refcnt) != 1) {
2581da177e4SLinus Torvalds 				/* The most unpleasant situation.
2591da177e4SLinus Torvalds 				   We must destroy neighbour entry,
2601da177e4SLinus Torvalds 				   but someone still uses it.
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 				   The destroy will be delayed until
2631da177e4SLinus Torvalds 				   the last user releases us, but
2641da177e4SLinus Torvalds 				   we must kill timers etc. and move
2651da177e4SLinus Torvalds 				   it to safe state.
2661da177e4SLinus Torvalds 				 */
267c9ab4d85SEric Dumazet 				__skb_queue_purge(&n->arp_queue);
2688b5c171bSEric Dumazet 				n->arp_queue_len_bytes = 0;
2691da177e4SLinus Torvalds 				n->output = neigh_blackhole;
2701da177e4SLinus Torvalds 				if (n->nud_state & NUD_VALID)
2711da177e4SLinus Torvalds 					n->nud_state = NUD_NOARP;
2721da177e4SLinus Torvalds 				else
2731da177e4SLinus Torvalds 					n->nud_state = NUD_NONE;
274d5d427cdSJoe Perches 				neigh_dbg(2, "neigh %p is stray\n", n);
2751da177e4SLinus Torvalds 			}
2761da177e4SLinus Torvalds 			write_unlock(&n->lock);
2774f494554SThomas Graf 			neigh_cleanup_and_release(n);
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 	}
28049636bb1SHerbert Xu }
2811da177e4SLinus Torvalds 
28249636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
28349636bb1SHerbert Xu {
28449636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
28549636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
28649636bb1SHerbert Xu 	write_unlock_bh(&tbl->lock);
28749636bb1SHerbert Xu }
2880a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr);
28949636bb1SHerbert Xu 
29049636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
29149636bb1SHerbert Xu {
29249636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
29349636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
2941da177e4SLinus Torvalds 	pneigh_ifdown(tbl, dev);
2951da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
2981da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
2991da177e4SLinus Torvalds 	return 0;
3001da177e4SLinus Torvalds }
3010a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown);
3021da177e4SLinus Torvalds 
303596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	struct neighbour *n = NULL;
3061da177e4SLinus Torvalds 	unsigned long now = jiffies;
3071da177e4SLinus Torvalds 	int entries;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	entries = atomic_inc_return(&tbl->entries) - 1;
3101da177e4SLinus Torvalds 	if (entries >= tbl->gc_thresh3 ||
3111da177e4SLinus Torvalds 	    (entries >= tbl->gc_thresh2 &&
3121da177e4SLinus Torvalds 	     time_after(now, tbl->last_flush + 5 * HZ))) {
3131da177e4SLinus Torvalds 		if (!neigh_forced_gc(tbl) &&
314fb811395SRick Jones 		    entries >= tbl->gc_thresh3) {
315fb811395SRick Jones 			net_info_ratelimited("%s: neighbor table overflow!\n",
316fb811395SRick Jones 					     tbl->id);
317fb811395SRick Jones 			NEIGH_CACHE_STAT_INC(tbl, table_fulls);
3181da177e4SLinus Torvalds 			goto out_entries;
3191da177e4SLinus Torvalds 		}
320fb811395SRick Jones 	}
3211da177e4SLinus Torvalds 
32208433effSYOSHIFUJI Hideaki / 吉藤英明 	n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
3231da177e4SLinus Torvalds 	if (!n)
3241da177e4SLinus Torvalds 		goto out_entries;
3251da177e4SLinus Torvalds 
326c9ab4d85SEric Dumazet 	__skb_queue_head_init(&n->arp_queue);
3271da177e4SLinus Torvalds 	rwlock_init(&n->lock);
3280ed8ddf4SEric Dumazet 	seqlock_init(&n->ha_lock);
3291da177e4SLinus Torvalds 	n->updated	  = n->used = now;
3301da177e4SLinus Torvalds 	n->nud_state	  = NUD_NONE;
3311da177e4SLinus Torvalds 	n->output	  = neigh_blackhole;
332f6b72b62SDavid S. Miller 	seqlock_init(&n->hh.hh_lock);
3331da177e4SLinus Torvalds 	n->parms	  = neigh_parms_clone(&tbl->parms);
334e99e88a9SKees Cook 	timer_setup(&n->timer, neigh_timer_handler, 0);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, allocs);
3371da177e4SLinus Torvalds 	n->tbl		  = tbl;
3389f237430SReshetova, Elena 	refcount_set(&n->refcnt, 1);
3391da177e4SLinus Torvalds 	n->dead		  = 1;
3401da177e4SLinus Torvalds out:
3411da177e4SLinus Torvalds 	return n;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds out_entries:
3441da177e4SLinus Torvalds 	atomic_dec(&tbl->entries);
3451da177e4SLinus Torvalds 	goto out;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3482c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x)
3492c2aba6cSDavid S. Miller {
350b3d0f789SJason A. Donenfeld 	*x = get_random_u32() | 1;
3512c2aba6cSDavid S. Miller }
3522c2aba6cSDavid S. Miller 
353cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
3541da177e4SLinus Torvalds {
355cd089336SDavid S. Miller 	size_t size = (1 << shift) * sizeof(struct neighbour *);
356d6bf7817SEric Dumazet 	struct neigh_hash_table *ret;
3576193d2beSEric Dumazet 	struct neighbour __rcu **buckets;
3582c2aba6cSDavid S. Miller 	int i;
3591da177e4SLinus Torvalds 
360d6bf7817SEric Dumazet 	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
361d6bf7817SEric Dumazet 	if (!ret)
362d6bf7817SEric Dumazet 		return NULL;
363d6bf7817SEric Dumazet 	if (size <= PAGE_SIZE)
364d6bf7817SEric Dumazet 		buckets = kzalloc(size, GFP_ATOMIC);
365d6bf7817SEric Dumazet 	else
3666193d2beSEric Dumazet 		buckets = (struct neighbour __rcu **)
367d6bf7817SEric Dumazet 			  __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
368d6bf7817SEric Dumazet 					   get_order(size));
369d6bf7817SEric Dumazet 	if (!buckets) {
370d6bf7817SEric Dumazet 		kfree(ret);
371d6bf7817SEric Dumazet 		return NULL;
3721da177e4SLinus Torvalds 	}
3736193d2beSEric Dumazet 	ret->hash_buckets = buckets;
374cd089336SDavid S. Miller 	ret->hash_shift = shift;
3752c2aba6cSDavid S. Miller 	for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
3762c2aba6cSDavid S. Miller 		neigh_get_hash_rnd(&ret->hash_rnd[i]);
3771da177e4SLinus Torvalds 	return ret;
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
380d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head)
3811da177e4SLinus Torvalds {
382d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = container_of(head,
383d6bf7817SEric Dumazet 						    struct neigh_hash_table,
384d6bf7817SEric Dumazet 						    rcu);
385cd089336SDavid S. Miller 	size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
3866193d2beSEric Dumazet 	struct neighbour __rcu **buckets = nht->hash_buckets;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	if (size <= PAGE_SIZE)
389d6bf7817SEric Dumazet 		kfree(buckets);
3901da177e4SLinus Torvalds 	else
391d6bf7817SEric Dumazet 		free_pages((unsigned long)buckets, get_order(size));
392d6bf7817SEric Dumazet 	kfree(nht);
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
395d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
396cd089336SDavid S. Miller 						unsigned long new_shift)
3971da177e4SLinus Torvalds {
398d6bf7817SEric Dumazet 	unsigned int i, hash;
399d6bf7817SEric Dumazet 	struct neigh_hash_table *new_nht, *old_nht;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
4021da177e4SLinus Torvalds 
403d6bf7817SEric Dumazet 	old_nht = rcu_dereference_protected(tbl->nht,
404d6bf7817SEric Dumazet 					    lockdep_is_held(&tbl->lock));
405cd089336SDavid S. Miller 	new_nht = neigh_hash_alloc(new_shift);
406d6bf7817SEric Dumazet 	if (!new_nht)
407d6bf7817SEric Dumazet 		return old_nht;
4081da177e4SLinus Torvalds 
409cd089336SDavid S. Miller 	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
4101da177e4SLinus Torvalds 		struct neighbour *n, *next;
4111da177e4SLinus Torvalds 
412767e97e1SEric Dumazet 		for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
413767e97e1SEric Dumazet 						   lockdep_is_held(&tbl->lock));
414d6bf7817SEric Dumazet 		     n != NULL;
415d6bf7817SEric Dumazet 		     n = next) {
416d6bf7817SEric Dumazet 			hash = tbl->hash(n->primary_key, n->dev,
417d6bf7817SEric Dumazet 					 new_nht->hash_rnd);
4181da177e4SLinus Torvalds 
419cd089336SDavid S. Miller 			hash >>= (32 - new_nht->hash_shift);
420767e97e1SEric Dumazet 			next = rcu_dereference_protected(n->next,
421767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock));
4221da177e4SLinus Torvalds 
423767e97e1SEric Dumazet 			rcu_assign_pointer(n->next,
424767e97e1SEric Dumazet 					   rcu_dereference_protected(
425767e97e1SEric Dumazet 						new_nht->hash_buckets[hash],
426767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
427767e97e1SEric Dumazet 			rcu_assign_pointer(new_nht->hash_buckets[hash], n);
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
431d6bf7817SEric Dumazet 	rcu_assign_pointer(tbl->nht, new_nht);
432d6bf7817SEric Dumazet 	call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
433d6bf7817SEric Dumazet 	return new_nht;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
4371da177e4SLinus Torvalds 			       struct net_device *dev)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	struct neighbour *n;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4421da177e4SLinus Torvalds 
443d6bf7817SEric Dumazet 	rcu_read_lock_bh();
44460395a20SEric W. Biederman 	n = __neigh_lookup_noref(tbl, pkey, dev);
44560395a20SEric W. Biederman 	if (n) {
4469f237430SReshetova, Elena 		if (!refcount_inc_not_zero(&n->refcnt))
447767e97e1SEric Dumazet 			n = NULL;
4481da177e4SLinus Torvalds 		NEIGH_CACHE_STAT_INC(tbl, hits);
4491da177e4SLinus Torvalds 	}
450767e97e1SEric Dumazet 
451d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4521da177e4SLinus Torvalds 	return n;
4531da177e4SLinus Torvalds }
4540a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup);
4551da177e4SLinus Torvalds 
456426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
457426b5303SEric W. Biederman 				     const void *pkey)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	struct neighbour *n;
46001ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
461bc4bf5f3SPavel Emelyanov 	u32 hash_val;
462d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4651da177e4SLinus Torvalds 
466d6bf7817SEric Dumazet 	rcu_read_lock_bh();
467d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
468cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
469767e97e1SEric Dumazet 
470767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
471767e97e1SEric Dumazet 	     n != NULL;
472767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
473426b5303SEric W. Biederman 		if (!memcmp(n->primary_key, pkey, key_len) &&
474878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(n->dev), net)) {
4759f237430SReshetova, Elena 			if (!refcount_inc_not_zero(&n->refcnt))
476767e97e1SEric Dumazet 				n = NULL;
4771da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
4781da177e4SLinus Torvalds 			break;
4791da177e4SLinus Torvalds 		}
4801da177e4SLinus Torvalds 	}
481767e97e1SEric Dumazet 
482d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4831da177e4SLinus Torvalds 	return n;
4841da177e4SLinus Torvalds }
4850a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev);
4861da177e4SLinus Torvalds 
487a263b309SDavid S. Miller struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
488a263b309SDavid S. Miller 				 struct net_device *dev, bool want_ref)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	u32 hash_val;
49101ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
4921da177e4SLinus Torvalds 	int error;
493596b9b68SDavid Miller 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
494d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (!n) {
4971da177e4SLinus Torvalds 		rc = ERR_PTR(-ENOBUFS);
4981da177e4SLinus Torvalds 		goto out;
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	memcpy(n->primary_key, pkey, key_len);
5021da177e4SLinus Torvalds 	n->dev = dev;
5031da177e4SLinus Torvalds 	dev_hold(dev);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	/* Protocol specific setup. */
5061da177e4SLinus Torvalds 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
5071da177e4SLinus Torvalds 		rc = ERR_PTR(error);
5081da177e4SLinus Torvalds 		goto out_neigh_release;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
511da6a8fa0SDavid Miller 	if (dev->netdev_ops->ndo_neigh_construct) {
512503eebc2SJiri Pirko 		error = dev->netdev_ops->ndo_neigh_construct(dev, n);
513da6a8fa0SDavid Miller 		if (error < 0) {
514da6a8fa0SDavid Miller 			rc = ERR_PTR(error);
515da6a8fa0SDavid Miller 			goto out_neigh_release;
516da6a8fa0SDavid Miller 		}
517da6a8fa0SDavid Miller 	}
518da6a8fa0SDavid Miller 
519447f2191SDavid S. Miller 	/* Device specific setup. */
520447f2191SDavid S. Miller 	if (n->parms->neigh_setup &&
521447f2191SDavid S. Miller 	    (error = n->parms->neigh_setup(n)) < 0) {
522447f2191SDavid S. Miller 		rc = ERR_PTR(error);
523447f2191SDavid S. Miller 		goto out_neigh_release;
524447f2191SDavid S. Miller 	}
525447f2191SDavid S. Miller 
5261f9248e5SJiri Pirko 	n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
529d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
530d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
5311da177e4SLinus Torvalds 
532cd089336SDavid S. Miller 	if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
533cd089336SDavid S. Miller 		nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
5341da177e4SLinus Torvalds 
535cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	if (n->parms->dead) {
5381da177e4SLinus Torvalds 		rc = ERR_PTR(-EINVAL);
5391da177e4SLinus Torvalds 		goto out_tbl_unlock;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
542767e97e1SEric Dumazet 	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
543767e97e1SEric Dumazet 					    lockdep_is_held(&tbl->lock));
544767e97e1SEric Dumazet 	     n1 != NULL;
545767e97e1SEric Dumazet 	     n1 = rcu_dereference_protected(n1->next,
546767e97e1SEric Dumazet 			lockdep_is_held(&tbl->lock))) {
5471da177e4SLinus Torvalds 		if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
548a263b309SDavid S. Miller 			if (want_ref)
5491da177e4SLinus Torvalds 				neigh_hold(n1);
5501da177e4SLinus Torvalds 			rc = n1;
5511da177e4SLinus Torvalds 			goto out_tbl_unlock;
5521da177e4SLinus Torvalds 		}
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	n->dead = 0;
556a263b309SDavid S. Miller 	if (want_ref)
5571da177e4SLinus Torvalds 		neigh_hold(n);
558767e97e1SEric Dumazet 	rcu_assign_pointer(n->next,
559767e97e1SEric Dumazet 			   rcu_dereference_protected(nht->hash_buckets[hash_val],
560767e97e1SEric Dumazet 						     lockdep_is_held(&tbl->lock)));
561767e97e1SEric Dumazet 	rcu_assign_pointer(nht->hash_buckets[hash_val], n);
5621da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
563d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is created\n", n);
5641da177e4SLinus Torvalds 	rc = n;
5651da177e4SLinus Torvalds out:
5661da177e4SLinus Torvalds 	return rc;
5671da177e4SLinus Torvalds out_tbl_unlock:
5681da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
5691da177e4SLinus Torvalds out_neigh_release:
5701da177e4SLinus Torvalds 	neigh_release(n);
5711da177e4SLinus Torvalds 	goto out;
5721da177e4SLinus Torvalds }
573a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create);
5741da177e4SLinus Torvalds 
57501ccdf12SAlexey Dobriyan static u32 pneigh_hash(const void *pkey, unsigned int key_len)
576fa86d322SPavel Emelyanov {
577fa86d322SPavel Emelyanov 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
578fa86d322SPavel Emelyanov 	hash_val ^= (hash_val >> 16);
579fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 8;
580fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 4;
581fa86d322SPavel Emelyanov 	hash_val &= PNEIGH_HASHMASK;
582be01d655SYOSHIFUJI Hideaki 	return hash_val;
583fa86d322SPavel Emelyanov }
584fa86d322SPavel Emelyanov 
585be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
586be01d655SYOSHIFUJI Hideaki 					      struct net *net,
587be01d655SYOSHIFUJI Hideaki 					      const void *pkey,
58801ccdf12SAlexey Dobriyan 					      unsigned int key_len,
589be01d655SYOSHIFUJI Hideaki 					      struct net_device *dev)
590be01d655SYOSHIFUJI Hideaki {
591be01d655SYOSHIFUJI Hideaki 	while (n) {
592be01d655SYOSHIFUJI Hideaki 		if (!memcmp(n->key, pkey, key_len) &&
593be01d655SYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net) &&
594be01d655SYOSHIFUJI Hideaki 		    (n->dev == dev || !n->dev))
595fa86d322SPavel Emelyanov 			return n;
596be01d655SYOSHIFUJI Hideaki 		n = n->next;
597be01d655SYOSHIFUJI Hideaki 	}
598be01d655SYOSHIFUJI Hideaki 	return NULL;
599be01d655SYOSHIFUJI Hideaki }
600be01d655SYOSHIFUJI Hideaki 
601be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
602be01d655SYOSHIFUJI Hideaki 		struct net *net, const void *pkey, struct net_device *dev)
603be01d655SYOSHIFUJI Hideaki {
60401ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
605be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
606be01d655SYOSHIFUJI Hideaki 
607be01d655SYOSHIFUJI Hideaki 	return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
608be01d655SYOSHIFUJI Hideaki 				 net, pkey, key_len, dev);
609fa86d322SPavel Emelyanov }
6100a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup);
611fa86d322SPavel Emelyanov 
612426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
613426b5303SEric W. Biederman 				    struct net *net, const void *pkey,
6141da177e4SLinus Torvalds 				    struct net_device *dev, int creat)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct pneigh_entry *n;
61701ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
618be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
621be01d655SYOSHIFUJI Hideaki 	n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
622be01d655SYOSHIFUJI Hideaki 			      net, pkey, key_len, dev);
623be01d655SYOSHIFUJI Hideaki 	read_unlock_bh(&tbl->lock);
6241da177e4SLinus Torvalds 
625be01d655SYOSHIFUJI Hideaki 	if (n || !creat)
6261da177e4SLinus Torvalds 		goto out;
6271da177e4SLinus Torvalds 
6284ae28944SPavel Emelyanov 	ASSERT_RTNL();
6294ae28944SPavel Emelyanov 
6301da177e4SLinus Torvalds 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
6311da177e4SLinus Torvalds 	if (!n)
6321da177e4SLinus Torvalds 		goto out;
6331da177e4SLinus Torvalds 
634efd7ef1cSEric W. Biederman 	write_pnet(&n->net, net);
6351da177e4SLinus Torvalds 	memcpy(n->key, pkey, key_len);
6361da177e4SLinus Torvalds 	n->dev = dev;
6371da177e4SLinus Torvalds 	if (dev)
6381da177e4SLinus Torvalds 		dev_hold(dev);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	if (tbl->pconstructor && tbl->pconstructor(n)) {
6411da177e4SLinus Torvalds 		if (dev)
6421da177e4SLinus Torvalds 			dev_put(dev);
6431da177e4SLinus Torvalds 		kfree(n);
6441da177e4SLinus Torvalds 		n = NULL;
6451da177e4SLinus Torvalds 		goto out;
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6491da177e4SLinus Torvalds 	n->next = tbl->phash_buckets[hash_val];
6501da177e4SLinus Torvalds 	tbl->phash_buckets[hash_val] = n;
6511da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6521da177e4SLinus Torvalds out:
6531da177e4SLinus Torvalds 	return n;
6541da177e4SLinus Torvalds }
6550a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 
658426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
6591da177e4SLinus Torvalds 		  struct net_device *dev)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
66201ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
663be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6661da177e4SLinus Torvalds 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
6671da177e4SLinus Torvalds 	     np = &n->next) {
668426b5303SEric W. Biederman 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
669878628fbSYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net)) {
6701da177e4SLinus Torvalds 			*np = n->next;
6711da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
6721da177e4SLinus Torvalds 			if (tbl->pdestructor)
6731da177e4SLinus Torvalds 				tbl->pdestructor(n);
6741da177e4SLinus Torvalds 			if (n->dev)
6751da177e4SLinus Torvalds 				dev_put(n->dev);
6761da177e4SLinus Torvalds 			kfree(n);
6771da177e4SLinus Torvalds 			return 0;
6781da177e4SLinus Torvalds 		}
6791da177e4SLinus Torvalds 	}
6801da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6811da177e4SLinus Torvalds 	return -ENOENT;
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6871da177e4SLinus Torvalds 	u32 h;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
6901da177e4SLinus Torvalds 		np = &tbl->phash_buckets[h];
6911da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
6921da177e4SLinus Torvalds 			if (!dev || n->dev == dev) {
6931da177e4SLinus Torvalds 				*np = n->next;
6941da177e4SLinus Torvalds 				if (tbl->pdestructor)
6951da177e4SLinus Torvalds 					tbl->pdestructor(n);
6961da177e4SLinus Torvalds 				if (n->dev)
6971da177e4SLinus Torvalds 					dev_put(n->dev);
6981da177e4SLinus Torvalds 				kfree(n);
6991da177e4SLinus Torvalds 				continue;
7001da177e4SLinus Torvalds 			}
7011da177e4SLinus Torvalds 			np = &n->next;
7021da177e4SLinus Torvalds 		}
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 	return -ENOENT;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
70706f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms);
70806f0511dSDenis V. Lunev 
70906f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms)
71006f0511dSDenis V. Lunev {
7116343944bSReshetova, Elena 	if (refcount_dec_and_test(&parms->refcnt))
71206f0511dSDenis V. Lunev 		neigh_parms_destroy(parms);
71306f0511dSDenis V. Lunev }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /*
7161da177e4SLinus Torvalds  *	neighbour must already be out of the table;
7171da177e4SLinus Torvalds  *
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh)
7201da177e4SLinus Torvalds {
721da6a8fa0SDavid Miller 	struct net_device *dev = neigh->dev;
722da6a8fa0SDavid Miller 
7231da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	if (!neigh->dead) {
726e005d193SJoe Perches 		pr_warn("Destroying alive neighbour %p\n", neigh);
7271da177e4SLinus Torvalds 		dump_stack();
7281da177e4SLinus Torvalds 		return;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	if (neigh_del_timer(neigh))
732e005d193SJoe Perches 		pr_warn("Impossible event\n");
7331da177e4SLinus Torvalds 
734c9ab4d85SEric Dumazet 	write_lock_bh(&neigh->lock);
735c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
736c9ab4d85SEric Dumazet 	write_unlock_bh(&neigh->lock);
7378b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
7381da177e4SLinus Torvalds 
739447f2191SDavid S. Miller 	if (dev->netdev_ops->ndo_neigh_destroy)
740503eebc2SJiri Pirko 		dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
741447f2191SDavid S. Miller 
742da6a8fa0SDavid Miller 	dev_put(dev);
7431da177e4SLinus Torvalds 	neigh_parms_put(neigh->parms);
7441da177e4SLinus Torvalds 
745d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is destroyed\n", neigh);
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	atomic_dec(&neigh->tbl->entries);
7485b8b0060SDavid Miller 	kfree_rcu(neigh, rcu);
7491da177e4SLinus Torvalds }
7500a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /* Neighbour state is suspicious;
7531da177e4SLinus Torvalds    disable fast path.
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds    Called with write_locked neigh.
7561da177e4SLinus Torvalds  */
7571da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh)
7581da177e4SLinus Torvalds {
759d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is suspected\n", neigh);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	neigh->output = neigh->ops->output;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /* Neighbour state is OK;
7651da177e4SLinus Torvalds    enable fast path.
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds    Called with write_locked neigh.
7681da177e4SLinus Torvalds  */
7691da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh)
7701da177e4SLinus Torvalds {
771d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is connected\n", neigh);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	neigh->output = neigh->ops->connected_output;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
776e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work)
7771da177e4SLinus Torvalds {
778e4c4e448SEric Dumazet 	struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
779767e97e1SEric Dumazet 	struct neighbour *n;
780767e97e1SEric Dumazet 	struct neighbour __rcu **np;
781e4c4e448SEric Dumazet 	unsigned int i;
782d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
7851da177e4SLinus Torvalds 
786e4c4e448SEric Dumazet 	write_lock_bh(&tbl->lock);
787d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
788d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	/*
7911da177e4SLinus Torvalds 	 *	periodically recompute ReachableTime from random function
7921da177e4SLinus Torvalds 	 */
7931da177e4SLinus Torvalds 
794e4c4e448SEric Dumazet 	if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
7951da177e4SLinus Torvalds 		struct neigh_parms *p;
796e4c4e448SEric Dumazet 		tbl->last_rand = jiffies;
79775fbfd33SNicolas Dichtel 		list_for_each_entry(p, &tbl->parms_list, list)
7981da177e4SLinus Torvalds 			p->reachable_time =
7991f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
8001da177e4SLinus Torvalds 	}
8011da177e4SLinus Torvalds 
802feff9ab2SDuan Jiong 	if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
803feff9ab2SDuan Jiong 		goto out;
804feff9ab2SDuan Jiong 
805cd089336SDavid S. Miller 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
806d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
8071da177e4SLinus Torvalds 
808767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
809767e97e1SEric Dumazet 				lockdep_is_held(&tbl->lock))) != NULL) {
8101da177e4SLinus Torvalds 			unsigned int state;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 			write_lock(&n->lock);
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 			state = n->nud_state;
8151da177e4SLinus Torvalds 			if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
8161da177e4SLinus Torvalds 				write_unlock(&n->lock);
8171da177e4SLinus Torvalds 				goto next_elt;
8181da177e4SLinus Torvalds 			}
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 			if (time_before(n->used, n->confirmed))
8211da177e4SLinus Torvalds 				n->used = n->confirmed;
8221da177e4SLinus Torvalds 
8239f237430SReshetova, Elena 			if (refcount_read(&n->refcnt) == 1 &&
8241da177e4SLinus Torvalds 			    (state == NUD_FAILED ||
8251f9248e5SJiri Pirko 			     time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
8261da177e4SLinus Torvalds 				*np = n->next;
8271da177e4SLinus Torvalds 				n->dead = 1;
8281da177e4SLinus Torvalds 				write_unlock(&n->lock);
8294f494554SThomas Graf 				neigh_cleanup_and_release(n);
8301da177e4SLinus Torvalds 				continue;
8311da177e4SLinus Torvalds 			}
8321da177e4SLinus Torvalds 			write_unlock(&n->lock);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds next_elt:
8351da177e4SLinus Torvalds 			np = &n->next;
8361da177e4SLinus Torvalds 		}
837e4c4e448SEric Dumazet 		/*
838e4c4e448SEric Dumazet 		 * It's fine to release lock here, even if hash table
839e4c4e448SEric Dumazet 		 * grows while we are preempted.
840e4c4e448SEric Dumazet 		 */
841e4c4e448SEric Dumazet 		write_unlock_bh(&tbl->lock);
842e4c4e448SEric Dumazet 		cond_resched();
843e4c4e448SEric Dumazet 		write_lock_bh(&tbl->lock);
84484338a6cSMichel Machado 		nht = rcu_dereference_protected(tbl->nht,
84584338a6cSMichel Machado 						lockdep_is_held(&tbl->lock));
846e4c4e448SEric Dumazet 	}
8472724680bSYOSHIFUJI Hideaki / 吉藤英明 out:
8481f9248e5SJiri Pirko 	/* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
8491f9248e5SJiri Pirko 	 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
8501f9248e5SJiri Pirko 	 * BASE_REACHABLE_TIME.
8511da177e4SLinus Torvalds 	 */
852f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
8531f9248e5SJiri Pirko 			      NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
854e4c4e448SEric Dumazet 	write_unlock_bh(&tbl->lock);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	struct neigh_parms *p = n->parms;
8608da86466SYOSHIFUJI Hideaki/吉藤英明 	return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
8618da86466SYOSHIFUJI Hideaki/吉藤英明 	       (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
8628da86466SYOSHIFUJI Hideaki/吉藤英明 	        NEIGH_VAR(p, MCAST_PROBES));
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8655ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh)
8660a141509SEric Dumazet 	__releases(neigh->lock)
8670a141509SEric Dumazet 	__acquires(neigh->lock)
8685ef12d98STimo Teras {
8695ef12d98STimo Teras 	struct sk_buff *skb;
8705ef12d98STimo Teras 
8715ef12d98STimo Teras 	NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
872d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is failed\n", neigh);
8735ef12d98STimo Teras 	neigh->updated = jiffies;
8745ef12d98STimo Teras 
8755ef12d98STimo Teras 	/* It is very thin place. report_unreachable is very complicated
8765ef12d98STimo Teras 	   routine. Particularly, it can hit the same neighbour entry!
8775ef12d98STimo Teras 
8785ef12d98STimo Teras 	   So that, we try to be accurate and avoid dead loop. --ANK
8795ef12d98STimo Teras 	 */
8805ef12d98STimo Teras 	while (neigh->nud_state == NUD_FAILED &&
8815ef12d98STimo Teras 	       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
8825ef12d98STimo Teras 		write_unlock(&neigh->lock);
8835ef12d98STimo Teras 		neigh->ops->error_report(neigh, skb);
8845ef12d98STimo Teras 		write_lock(&neigh->lock);
8855ef12d98STimo Teras 	}
886c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
8878b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
8885ef12d98STimo Teras }
8895ef12d98STimo Teras 
890cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh)
891cd28ca0aSEric Dumazet 	__releases(neigh->lock)
892cd28ca0aSEric Dumazet {
8934ed377e3SHannes Frederic Sowa 	struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
894cd28ca0aSEric Dumazet 	/* keep skb alive even if arp_queue overflows */
895cd28ca0aSEric Dumazet 	if (skb)
89619125c1aSMartin Zhang 		skb = skb_clone(skb, GFP_ATOMIC);
897cd28ca0aSEric Dumazet 	write_unlock(&neigh->lock);
89848481c8fSEric Dumazet 	if (neigh->ops->solicit)
899cd28ca0aSEric Dumazet 		neigh->ops->solicit(neigh, skb);
900cd28ca0aSEric Dumazet 	atomic_inc(&neigh->probes);
901cd28ca0aSEric Dumazet 	kfree_skb(skb);
902cd28ca0aSEric Dumazet }
903cd28ca0aSEric Dumazet 
9041da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */
9051da177e4SLinus Torvalds 
906e99e88a9SKees Cook static void neigh_timer_handler(struct timer_list *t)
9071da177e4SLinus Torvalds {
9081da177e4SLinus Torvalds 	unsigned long now, next;
909e99e88a9SKees Cook 	struct neighbour *neigh = from_timer(neigh, t, timer);
91095c96174SEric Dumazet 	unsigned int state;
9111da177e4SLinus Torvalds 	int notify = 0;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	write_lock(&neigh->lock);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	state = neigh->nud_state;
9161da177e4SLinus Torvalds 	now = jiffies;
9171da177e4SLinus Torvalds 	next = now + HZ;
9181da177e4SLinus Torvalds 
919045f7b3bSDavid S. Miller 	if (!(state & NUD_IN_TIMER))
9201da177e4SLinus Torvalds 		goto out;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	if (state & NUD_REACHABLE) {
9231da177e4SLinus Torvalds 		if (time_before_eq(now,
9241da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->reachable_time)) {
925d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is still alive\n", neigh);
9261da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9271da177e4SLinus Torvalds 		} else if (time_before_eq(now,
9281f9248e5SJiri Pirko 					  neigh->used +
9291f9248e5SJiri Pirko 					  NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
930d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is delayed\n", neigh);
9311da177e4SLinus Torvalds 			neigh->nud_state = NUD_DELAY;
932955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9331da177e4SLinus Torvalds 			neigh_suspect(neigh);
9341f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
9351da177e4SLinus Torvalds 		} else {
936d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is suspected\n", neigh);
9371da177e4SLinus Torvalds 			neigh->nud_state = NUD_STALE;
938955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9391da177e4SLinus Torvalds 			neigh_suspect(neigh);
9408d71740cSTom Tucker 			notify = 1;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 	} else if (state & NUD_DELAY) {
9431da177e4SLinus Torvalds 		if (time_before_eq(now,
9441f9248e5SJiri Pirko 				   neigh->confirmed +
9451f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
946d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is now reachable\n", neigh);
9471da177e4SLinus Torvalds 			neigh->nud_state = NUD_REACHABLE;
948955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9491da177e4SLinus Torvalds 			neigh_connect(neigh);
9508d71740cSTom Tucker 			notify = 1;
9511da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9521da177e4SLinus Torvalds 		} else {
953d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is probed\n", neigh);
9541da177e4SLinus Torvalds 			neigh->nud_state = NUD_PROBE;
955955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9561da177e4SLinus Torvalds 			atomic_set(&neigh->probes, 0);
957765c9c63SErik Kline 			notify = 1;
9581f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
9591da177e4SLinus Torvalds 		}
9601da177e4SLinus Torvalds 	} else {
9611da177e4SLinus Torvalds 		/* NUD_PROBE|NUD_INCOMPLETE */
9621f9248e5SJiri Pirko 		next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
9661da177e4SLinus Torvalds 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
9671da177e4SLinus Torvalds 		neigh->nud_state = NUD_FAILED;
9681da177e4SLinus Torvalds 		notify = 1;
9695ef12d98STimo Teras 		neigh_invalidate(neigh);
9705e2c21dcSDuan Jiong 		goto out;
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	if (neigh->nud_state & NUD_IN_TIMER) {
9741da177e4SLinus Torvalds 		if (time_before(next, jiffies + HZ/2))
9751da177e4SLinus Torvalds 			next = jiffies + HZ/2;
9766fb9974fSHerbert Xu 		if (!mod_timer(&neigh->timer, next))
9776fb9974fSHerbert Xu 			neigh_hold(neigh);
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
980cd28ca0aSEric Dumazet 		neigh_probe(neigh);
9819ff56607SDavid S. Miller 	} else {
9821da177e4SLinus Torvalds out:
9831da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
9849ff56607SDavid S. Miller 	}
9851da177e4SLinus Torvalds 
986d961db35SThomas Graf 	if (notify)
9877b8f7a40SRoopa Prabhu 		neigh_update_notify(neigh, 0);
988d961db35SThomas Graf 
9891da177e4SLinus Torvalds 	neigh_release(neigh);
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	int rc;
995cd28ca0aSEric Dumazet 	bool immediate_probe = false;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	rc = 0;
10001da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
10011da177e4SLinus Torvalds 		goto out_unlock_bh;
10022c51a97fSJulian Anastasov 	if (neigh->dead)
10032c51a97fSJulian Anastasov 		goto out_dead;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
10061f9248e5SJiri Pirko 		if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
10071f9248e5SJiri Pirko 		    NEIGH_VAR(neigh->parms, APP_PROBES)) {
1008cd28ca0aSEric Dumazet 			unsigned long next, now = jiffies;
1009cd28ca0aSEric Dumazet 
10101f9248e5SJiri Pirko 			atomic_set(&neigh->probes,
10111f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, UCAST_PROBES));
10121da177e4SLinus Torvalds 			neigh->nud_state     = NUD_INCOMPLETE;
1013cd28ca0aSEric Dumazet 			neigh->updated = now;
10141f9248e5SJiri Pirko 			next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
10151f9248e5SJiri Pirko 					 HZ/2);
1016cd28ca0aSEric Dumazet 			neigh_add_timer(neigh, next);
1017cd28ca0aSEric Dumazet 			immediate_probe = true;
10181da177e4SLinus Torvalds 		} else {
10191da177e4SLinus Torvalds 			neigh->nud_state = NUD_FAILED;
1020955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
10211da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 			kfree_skb(skb);
10241da177e4SLinus Torvalds 			return 1;
10251da177e4SLinus Torvalds 		}
10261da177e4SLinus Torvalds 	} else if (neigh->nud_state & NUD_STALE) {
1027d5d427cdSJoe Perches 		neigh_dbg(2, "neigh %p is delayed\n", neigh);
10281da177e4SLinus Torvalds 		neigh->nud_state = NUD_DELAY;
1029955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
10301f9248e5SJiri Pirko 		neigh_add_timer(neigh, jiffies +
10311f9248e5SJiri Pirko 				NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
10321da177e4SLinus Torvalds 	}
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	if (neigh->nud_state == NUD_INCOMPLETE) {
10351da177e4SLinus Torvalds 		if (skb) {
10368b5c171bSEric Dumazet 			while (neigh->arp_queue_len_bytes + skb->truesize >
10371f9248e5SJiri Pirko 			       NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
10381da177e4SLinus Torvalds 				struct sk_buff *buff;
10398b5c171bSEric Dumazet 
1040f72051b0SDavid S. Miller 				buff = __skb_dequeue(&neigh->arp_queue);
10418b5c171bSEric Dumazet 				if (!buff)
10428b5c171bSEric Dumazet 					break;
10438b5c171bSEric Dumazet 				neigh->arp_queue_len_bytes -= buff->truesize;
10441da177e4SLinus Torvalds 				kfree_skb(buff);
10459a6d276eSNeil Horman 				NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
10461da177e4SLinus Torvalds 			}
1047a4731138SEric Dumazet 			skb_dst_force(skb);
10481da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->arp_queue, skb);
10498b5c171bSEric Dumazet 			neigh->arp_queue_len_bytes += skb->truesize;
10501da177e4SLinus Torvalds 		}
10511da177e4SLinus Torvalds 		rc = 1;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds out_unlock_bh:
1054cd28ca0aSEric Dumazet 	if (immediate_probe)
1055cd28ca0aSEric Dumazet 		neigh_probe(neigh);
1056cd28ca0aSEric Dumazet 	else
1057cd28ca0aSEric Dumazet 		write_unlock(&neigh->lock);
1058cd28ca0aSEric Dumazet 	local_bh_enable();
10591da177e4SLinus Torvalds 	return rc;
10602c51a97fSJulian Anastasov 
10612c51a97fSJulian Anastasov out_dead:
10622c51a97fSJulian Anastasov 	if (neigh->nud_state & NUD_STALE)
10632c51a97fSJulian Anastasov 		goto out_unlock_bh;
10642c51a97fSJulian Anastasov 	write_unlock_bh(&neigh->lock);
10652c51a97fSJulian Anastasov 	kfree_skb(skb);
10662c51a97fSJulian Anastasov 	return 1;
10671da177e4SLinus Torvalds }
10680a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send);
10691da177e4SLinus Torvalds 
1070f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	struct hh_cache *hh;
10733b04dddeSStephen Hemminger 	void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
107491a72a70SDoug Kehn 		= NULL;
107591a72a70SDoug Kehn 
107691a72a70SDoug Kehn 	if (neigh->dev->header_ops)
107791a72a70SDoug Kehn 		update = neigh->dev->header_ops->cache_update;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	if (update) {
1080f6b72b62SDavid S. Miller 		hh = &neigh->hh;
1081f6b72b62SDavid S. Miller 		if (hh->hh_len) {
10823644f0ceSStephen Hemminger 			write_seqlock_bh(&hh->hh_lock);
10831da177e4SLinus Torvalds 			update(hh, neigh->dev, neigh->ha);
10843644f0ceSStephen Hemminger 			write_sequnlock_bh(&hh->hh_lock);
10851da177e4SLinus Torvalds 		}
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds /* Generic update routine.
10921da177e4SLinus Torvalds    -- lladdr is new lladdr or NULL, if it is not supplied.
10931da177e4SLinus Torvalds    -- new    is new state.
10941da177e4SLinus Torvalds    -- flags
10951da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
10961da177e4SLinus Torvalds 				if it is different.
10971da177e4SLinus Torvalds 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
10981da177e4SLinus Torvalds 				lladdr instead of overriding it
10991da177e4SLinus Torvalds 				if it is different.
11001da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
11031da177e4SLinus Torvalds 				NTF_ROUTER flag.
11041da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
11051da177e4SLinus Torvalds 				a router.
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds    Caller MUST hold reference count on the entry.
11081da177e4SLinus Torvalds  */
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
11117b8f7a40SRoopa Prabhu 		 u32 flags, u32 nlmsg_pid)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	u8 old;
11141da177e4SLinus Torvalds 	int err;
11151da177e4SLinus Torvalds 	int notify = 0;
11161da177e4SLinus Torvalds 	struct net_device *dev;
11171da177e4SLinus Torvalds 	int update_isrouter = 0;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	dev    = neigh->dev;
11221da177e4SLinus Torvalds 	old    = neigh->nud_state;
11231da177e4SLinus Torvalds 	err    = -EPERM;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
11261da177e4SLinus Torvalds 	    (old & (NUD_NOARP | NUD_PERMANENT)))
11271da177e4SLinus Torvalds 		goto out;
11282c51a97fSJulian Anastasov 	if (neigh->dead)
11292c51a97fSJulian Anastasov 		goto out;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	if (!(new & NUD_VALID)) {
11321da177e4SLinus Torvalds 		neigh_del_timer(neigh);
11331da177e4SLinus Torvalds 		if (old & NUD_CONNECTED)
11341da177e4SLinus Torvalds 			neigh_suspect(neigh);
11351da177e4SLinus Torvalds 		neigh->nud_state = new;
11361da177e4SLinus Torvalds 		err = 0;
11371da177e4SLinus Torvalds 		notify = old & NUD_VALID;
11385ef12d98STimo Teras 		if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
11395ef12d98STimo Teras 		    (new & NUD_FAILED)) {
11405ef12d98STimo Teras 			neigh_invalidate(neigh);
11415ef12d98STimo Teras 			notify = 1;
11425ef12d98STimo Teras 		}
11431da177e4SLinus Torvalds 		goto out;
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* Compare new lladdr with cached one */
11471da177e4SLinus Torvalds 	if (!dev->addr_len) {
11481da177e4SLinus Torvalds 		/* First case: device needs no address. */
11491da177e4SLinus Torvalds 		lladdr = neigh->ha;
11501da177e4SLinus Torvalds 	} else if (lladdr) {
11511da177e4SLinus Torvalds 		/* The second case: if something is already cached
11521da177e4SLinus Torvalds 		   and a new address is proposed:
11531da177e4SLinus Torvalds 		   - compare new & old
11541da177e4SLinus Torvalds 		   - if they are different, check override flag
11551da177e4SLinus Torvalds 		 */
11561da177e4SLinus Torvalds 		if ((old & NUD_VALID) &&
11571da177e4SLinus Torvalds 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
11581da177e4SLinus Torvalds 			lladdr = neigh->ha;
11591da177e4SLinus Torvalds 	} else {
11601da177e4SLinus Torvalds 		/* No address is supplied; if we know something,
11611da177e4SLinus Torvalds 		   use it, otherwise discard the request.
11621da177e4SLinus Torvalds 		 */
11631da177e4SLinus Torvalds 		err = -EINVAL;
11641da177e4SLinus Torvalds 		if (!(old & NUD_VALID))
11651da177e4SLinus Torvalds 			goto out;
11661da177e4SLinus Torvalds 		lladdr = neigh->ha;
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	/* If entry was valid and address is not changed,
11701da177e4SLinus Torvalds 	   do not change entry state, if new one is STALE.
11711da177e4SLinus Torvalds 	 */
11721da177e4SLinus Torvalds 	err = 0;
11731da177e4SLinus Torvalds 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
11741da177e4SLinus Torvalds 	if (old & NUD_VALID) {
11751da177e4SLinus Torvalds 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
11761da177e4SLinus Torvalds 			update_isrouter = 0;
11771da177e4SLinus Torvalds 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
11781da177e4SLinus Torvalds 			    (old & NUD_CONNECTED)) {
11791da177e4SLinus Torvalds 				lladdr = neigh->ha;
11801da177e4SLinus Torvalds 				new = NUD_STALE;
11811da177e4SLinus Torvalds 			} else
11821da177e4SLinus Torvalds 				goto out;
11831da177e4SLinus Torvalds 		} else {
11840e7bbcc1SJulian Anastasov 			if (lladdr == neigh->ha && new == NUD_STALE &&
11850e7bbcc1SJulian Anastasov 			    !(flags & NEIGH_UPDATE_F_ADMIN))
11861da177e4SLinus Torvalds 				new = old;
11871da177e4SLinus Torvalds 		}
11881da177e4SLinus Torvalds 	}
11891da177e4SLinus Torvalds 
119077d71233SIhar Hrachyshka 	/* Update timestamps only once we know we will make a change to the
119177d71233SIhar Hrachyshka 	 * neighbour entry. Otherwise we risk to move the locktime window with
119277d71233SIhar Hrachyshka 	 * noop updates and ignore relevant ARP updates.
119377d71233SIhar Hrachyshka 	 */
119477d71233SIhar Hrachyshka 	if (new != old || lladdr != neigh->ha) {
119577d71233SIhar Hrachyshka 		if (new & NUD_CONNECTED)
119677d71233SIhar Hrachyshka 			neigh->confirmed = jiffies;
119777d71233SIhar Hrachyshka 		neigh->updated = jiffies;
119877d71233SIhar Hrachyshka 	}
119977d71233SIhar Hrachyshka 
12001da177e4SLinus Torvalds 	if (new != old) {
12011da177e4SLinus Torvalds 		neigh_del_timer(neigh);
1202765c9c63SErik Kline 		if (new & NUD_PROBE)
1203765c9c63SErik Kline 			atomic_set(&neigh->probes, 0);
1204a43d8994SPavel Emelyanov 		if (new & NUD_IN_TIMER)
1205667347f1SDavid S. Miller 			neigh_add_timer(neigh, (jiffies +
12061da177e4SLinus Torvalds 						((new & NUD_REACHABLE) ?
1207667347f1SDavid S. Miller 						 neigh->parms->reachable_time :
1208667347f1SDavid S. Miller 						 0)));
12091da177e4SLinus Torvalds 		neigh->nud_state = new;
121053385d2dSBob Gilligan 		notify = 1;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	if (lladdr != neigh->ha) {
12140ed8ddf4SEric Dumazet 		write_seqlock(&neigh->ha_lock);
12151da177e4SLinus Torvalds 		memcpy(&neigh->ha, lladdr, dev->addr_len);
12160ed8ddf4SEric Dumazet 		write_sequnlock(&neigh->ha_lock);
12171da177e4SLinus Torvalds 		neigh_update_hhs(neigh);
12181da177e4SLinus Torvalds 		if (!(new & NUD_CONNECTED))
12191da177e4SLinus Torvalds 			neigh->confirmed = jiffies -
12201f9248e5SJiri Pirko 				      (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
12211da177e4SLinus Torvalds 		notify = 1;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 	if (new == old)
12241da177e4SLinus Torvalds 		goto out;
12251da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
12261da177e4SLinus Torvalds 		neigh_connect(neigh);
12271da177e4SLinus Torvalds 	else
12281da177e4SLinus Torvalds 		neigh_suspect(neigh);
12291da177e4SLinus Torvalds 	if (!(old & NUD_VALID)) {
12301da177e4SLinus Torvalds 		struct sk_buff *skb;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		/* Again: avoid dead loop if something went wrong */
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 		while (neigh->nud_state & NUD_VALID &&
12351da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
123669cce1d1SDavid S. Miller 			struct dst_entry *dst = skb_dst(skb);
123769cce1d1SDavid S. Miller 			struct neighbour *n2, *n1 = neigh;
12381da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
1239e049f288Sroy.qing.li@gmail.com 
1240e049f288Sroy.qing.li@gmail.com 			rcu_read_lock();
124113a43d94SDavid S. Miller 
124213a43d94SDavid S. Miller 			/* Why not just use 'neigh' as-is?  The problem is that
124313a43d94SDavid S. Miller 			 * things such as shaper, eql, and sch_teql can end up
124413a43d94SDavid S. Miller 			 * using alternative, different, neigh objects to output
124513a43d94SDavid S. Miller 			 * the packet in the output path.  So what we need to do
124613a43d94SDavid S. Miller 			 * here is re-lookup the top-level neigh in the path so
124713a43d94SDavid S. Miller 			 * we can reinject the packet there.
124813a43d94SDavid S. Miller 			 */
124913a43d94SDavid S. Miller 			n2 = NULL;
125013a43d94SDavid S. Miller 			if (dst) {
125113a43d94SDavid S. Miller 				n2 = dst_neigh_lookup_skb(dst, skb);
125213a43d94SDavid S. Miller 				if (n2)
125369cce1d1SDavid S. Miller 					n1 = n2;
125413a43d94SDavid S. Miller 			}
12558f40b161SDavid S. Miller 			n1->output(n1, skb);
125613a43d94SDavid S. Miller 			if (n2)
125713a43d94SDavid S. Miller 				neigh_release(n2);
1258e049f288Sroy.qing.li@gmail.com 			rcu_read_unlock();
1259e049f288Sroy.qing.li@gmail.com 
12601da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
12611da177e4SLinus Torvalds 		}
1262c9ab4d85SEric Dumazet 		__skb_queue_purge(&neigh->arp_queue);
12638b5c171bSEric Dumazet 		neigh->arp_queue_len_bytes = 0;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds out:
12661da177e4SLinus Torvalds 	if (update_isrouter) {
12671da177e4SLinus Torvalds 		neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
12681da177e4SLinus Torvalds 			(neigh->flags | NTF_ROUTER) :
12691da177e4SLinus Torvalds 			(neigh->flags & ~NTF_ROUTER);
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
12728d71740cSTom Tucker 
12738d71740cSTom Tucker 	if (notify)
12747b8f7a40SRoopa Prabhu 		neigh_update_notify(neigh, nlmsg_pid);
1275d961db35SThomas Graf 
12761da177e4SLinus Torvalds 	return err;
12771da177e4SLinus Torvalds }
12780a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update);
12791da177e4SLinus Torvalds 
12807e980569SJiri Benc /* Update the neigh to listen temporarily for probe responses, even if it is
12817e980569SJiri Benc  * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
12827e980569SJiri Benc  */
12837e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh)
12847e980569SJiri Benc {
12852c51a97fSJulian Anastasov 	if (neigh->dead)
12862c51a97fSJulian Anastasov 		return;
12877e980569SJiri Benc 	neigh->updated = jiffies;
12887e980569SJiri Benc 	if (!(neigh->nud_state & NUD_FAILED))
12897e980569SJiri Benc 		return;
12902176d5d4SDuan Jiong 	neigh->nud_state = NUD_INCOMPLETE;
12912176d5d4SDuan Jiong 	atomic_set(&neigh->probes, neigh_max_probes(neigh));
12927e980569SJiri Benc 	neigh_add_timer(neigh,
12937e980569SJiri Benc 			jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
12947e980569SJiri Benc }
12957e980569SJiri Benc EXPORT_SYMBOL(__neigh_set_probe_once);
12967e980569SJiri Benc 
12971da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl,
12981da177e4SLinus Torvalds 				 u8 *lladdr, void *saddr,
12991da177e4SLinus Torvalds 				 struct net_device *dev)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
13021da177e4SLinus Torvalds 						 lladdr || !dev->addr_len);
13031da177e4SLinus Torvalds 	if (neigh)
13041da177e4SLinus Torvalds 		neigh_update(neigh, lladdr, NUD_STALE,
13057b8f7a40SRoopa Prabhu 			     NEIGH_UPDATE_F_OVERRIDE, 0);
13061da177e4SLinus Torvalds 	return neigh;
13071da177e4SLinus Torvalds }
13080a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns);
13091da177e4SLinus Torvalds 
131034d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */
1311bdf53c58SEric W. Biederman static void neigh_hh_init(struct neighbour *n)
13121da177e4SLinus Torvalds {
1313bdf53c58SEric W. Biederman 	struct net_device *dev = n->dev;
1314bdf53c58SEric W. Biederman 	__be16 prot = n->tbl->protocol;
1315f6b72b62SDavid S. Miller 	struct hh_cache	*hh = &n->hh;
13160ed8ddf4SEric Dumazet 
13170ed8ddf4SEric Dumazet 	write_lock_bh(&n->lock);
131834d101ddSEric Dumazet 
1319f6b72b62SDavid S. Miller 	/* Only one thread can come in here and initialize the
1320f6b72b62SDavid S. Miller 	 * hh_cache entry.
1321f6b72b62SDavid S. Miller 	 */
1322b23b5455SDavid S. Miller 	if (!hh->hh_len)
1323b23b5455SDavid S. Miller 		dev->header_ops->cache(n, hh, prot);
1324f6b72b62SDavid S. Miller 
13250ed8ddf4SEric Dumazet 	write_unlock_bh(&n->lock);
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds /* Slow and careful. */
13291da177e4SLinus Torvalds 
13308f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
13311da177e4SLinus Torvalds {
13321da177e4SLinus Torvalds 	int rc = 0;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	if (!neigh_event_send(neigh, skb)) {
13351da177e4SLinus Torvalds 		int err;
13361da177e4SLinus Torvalds 		struct net_device *dev = neigh->dev;
13370ed8ddf4SEric Dumazet 		unsigned int seq;
133834d101ddSEric Dumazet 
1339f6b72b62SDavid S. Miller 		if (dev->header_ops->cache && !neigh->hh.hh_len)
1340bdf53c58SEric W. Biederman 			neigh_hh_init(neigh);
134134d101ddSEric Dumazet 
13420ed8ddf4SEric Dumazet 		do {
1343e1f16503Sramesh.nagappa@gmail.com 			__skb_pull(skb, skb_network_offset(skb));
13440ed8ddf4SEric Dumazet 			seq = read_seqbegin(&neigh->ha_lock);
13450c4e8581SStephen Hemminger 			err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13461da177e4SLinus Torvalds 					      neigh->ha, NULL, skb->len);
13470ed8ddf4SEric Dumazet 		} while (read_seqretry(&neigh->ha_lock, seq));
134834d101ddSEric Dumazet 
13491da177e4SLinus Torvalds 		if (err >= 0)
1350542d4d68SDavid S. Miller 			rc = dev_queue_xmit(skb);
13511da177e4SLinus Torvalds 		else
13521da177e4SLinus Torvalds 			goto out_kfree_skb;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds out:
13551da177e4SLinus Torvalds 	return rc;
13561da177e4SLinus Torvalds out_kfree_skb:
13571da177e4SLinus Torvalds 	rc = -EINVAL;
13581da177e4SLinus Torvalds 	kfree_skb(skb);
13591da177e4SLinus Torvalds 	goto out;
13601da177e4SLinus Torvalds }
13610a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds /* As fast as possible without hh cache */
13641da177e4SLinus Torvalds 
13658f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
13680ed8ddf4SEric Dumazet 	unsigned int seq;
13698f40b161SDavid S. Miller 	int err;
13701da177e4SLinus Torvalds 
13710ed8ddf4SEric Dumazet 	do {
1372e1f16503Sramesh.nagappa@gmail.com 		__skb_pull(skb, skb_network_offset(skb));
13730ed8ddf4SEric Dumazet 		seq = read_seqbegin(&neigh->ha_lock);
13740c4e8581SStephen Hemminger 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13751da177e4SLinus Torvalds 				      neigh->ha, NULL, skb->len);
13760ed8ddf4SEric Dumazet 	} while (read_seqretry(&neigh->ha_lock, seq));
13770ed8ddf4SEric Dumazet 
13781da177e4SLinus Torvalds 	if (err >= 0)
1379542d4d68SDavid S. Miller 		err = dev_queue_xmit(skb);
13801da177e4SLinus Torvalds 	else {
13811da177e4SLinus Torvalds 		err = -EINVAL;
13821da177e4SLinus Torvalds 		kfree_skb(skb);
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 	return err;
13851da177e4SLinus Torvalds }
13860a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output);
13871da177e4SLinus Torvalds 
13888f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
13898f40b161SDavid S. Miller {
13908f40b161SDavid S. Miller 	return dev_queue_xmit(skb);
13918f40b161SDavid S. Miller }
13928f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output);
13938f40b161SDavid S. Miller 
1394e99e88a9SKees Cook static void neigh_proxy_process(struct timer_list *t)
13951da177e4SLinus Torvalds {
1396e99e88a9SKees Cook 	struct neigh_table *tbl = from_timer(tbl, t, proxy_timer);
13971da177e4SLinus Torvalds 	long sched_next = 0;
13981da177e4SLinus Torvalds 	unsigned long now = jiffies;
1399f72051b0SDavid S. Miller 	struct sk_buff *skb, *n;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
14021da177e4SLinus Torvalds 
1403f72051b0SDavid S. Miller 	skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1404f72051b0SDavid S. Miller 		long tdif = NEIGH_CB(skb)->sched_next - now;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 		if (tdif <= 0) {
1407f72051b0SDavid S. Miller 			struct net_device *dev = skb->dev;
140820e6074eSEric Dumazet 
1409f72051b0SDavid S. Miller 			__skb_unlink(skb, &tbl->proxy_queue);
141020e6074eSEric Dumazet 			if (tbl->proxy_redo && netif_running(dev)) {
141120e6074eSEric Dumazet 				rcu_read_lock();
1412f72051b0SDavid S. Miller 				tbl->proxy_redo(skb);
141320e6074eSEric Dumazet 				rcu_read_unlock();
141420e6074eSEric Dumazet 			} else {
1415f72051b0SDavid S. Miller 				kfree_skb(skb);
141620e6074eSEric Dumazet 			}
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 			dev_put(dev);
14191da177e4SLinus Torvalds 		} else if (!sched_next || tdif < sched_next)
14201da177e4SLinus Torvalds 			sched_next = tdif;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 	del_timer(&tbl->proxy_timer);
14231da177e4SLinus Torvalds 	if (sched_next)
14241da177e4SLinus Torvalds 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
14251da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
14291da177e4SLinus Torvalds 		    struct sk_buff *skb)
14301da177e4SLinus Torvalds {
14311da177e4SLinus Torvalds 	unsigned long now = jiffies;
143263862b5bSAruna-Hewapathirane 
143363862b5bSAruna-Hewapathirane 	unsigned long sched_next = now + (prandom_u32() %
14341f9248e5SJiri Pirko 					  NEIGH_VAR(p, PROXY_DELAY));
14351da177e4SLinus Torvalds 
14361f9248e5SJiri Pirko 	if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
14371da177e4SLinus Torvalds 		kfree_skb(skb);
14381da177e4SLinus Torvalds 		return;
14391da177e4SLinus Torvalds 	}
1440a61bbcf2SPatrick McHardy 
1441a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->sched_next = sched_next;
1442a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
14451da177e4SLinus Torvalds 	if (del_timer(&tbl->proxy_timer)) {
14461da177e4SLinus Torvalds 		if (time_before(tbl->proxy_timer.expires, sched_next))
14471da177e4SLinus Torvalds 			sched_next = tbl->proxy_timer.expires;
14481da177e4SLinus Torvalds 	}
1449adf30907SEric Dumazet 	skb_dst_drop(skb);
14501da177e4SLinus Torvalds 	dev_hold(skb->dev);
14511da177e4SLinus Torvalds 	__skb_queue_tail(&tbl->proxy_queue, skb);
14521da177e4SLinus Torvalds 	mod_timer(&tbl->proxy_timer, sched_next);
14531da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
14541da177e4SLinus Torvalds }
14550a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue);
14561da177e4SLinus Torvalds 
145797fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1458426b5303SEric W. Biederman 						      struct net *net, int ifindex)
1459426b5303SEric W. Biederman {
1460426b5303SEric W. Biederman 	struct neigh_parms *p;
1461426b5303SEric W. Biederman 
146275fbfd33SNicolas Dichtel 	list_for_each_entry(p, &tbl->parms_list, list) {
1463878628fbSYOSHIFUJI Hideaki 		if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1464170d6f99SGao feng 		    (!p->dev && !ifindex && net_eq(net, &init_net)))
1465426b5303SEric W. Biederman 			return p;
1466426b5303SEric W. Biederman 	}
1467426b5303SEric W. Biederman 
1468426b5303SEric W. Biederman 	return NULL;
1469426b5303SEric W. Biederman }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
14721da177e4SLinus Torvalds 				      struct neigh_table *tbl)
14731da177e4SLinus Torvalds {
1474cf89d6b2SGao feng 	struct neigh_parms *p;
147500829823SStephen Hemminger 	struct net *net = dev_net(dev);
147600829823SStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14771da177e4SLinus Torvalds 
1478cf89d6b2SGao feng 	p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
14791da177e4SLinus Torvalds 	if (p) {
14801da177e4SLinus Torvalds 		p->tbl		  = tbl;
14816343944bSReshetova, Elena 		refcount_set(&p->refcnt, 1);
14821da177e4SLinus Torvalds 		p->reachable_time =
14831f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1484c7fb64dbSThomas Graf 		dev_hold(dev);
1485c7fb64dbSThomas Graf 		p->dev = dev;
1486efd7ef1cSEric W. Biederman 		write_pnet(&p->net, net);
14871da177e4SLinus Torvalds 		p->sysctl_table = NULL;
148863134803SVeaceslav Falico 
148963134803SVeaceslav Falico 		if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
149063134803SVeaceslav Falico 			dev_put(dev);
149163134803SVeaceslav Falico 			kfree(p);
149263134803SVeaceslav Falico 			return NULL;
149363134803SVeaceslav Falico 		}
149463134803SVeaceslav Falico 
14951da177e4SLinus Torvalds 		write_lock_bh(&tbl->lock);
149675fbfd33SNicolas Dichtel 		list_add(&p->list, &tbl->parms.list);
14971da177e4SLinus Torvalds 		write_unlock_bh(&tbl->lock);
14981d4c8c29SJiri Pirko 
14991d4c8c29SJiri Pirko 		neigh_parms_data_state_cleanall(p);
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds 	return p;
15021da177e4SLinus Torvalds }
15030a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc);
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head)
15061da177e4SLinus Torvalds {
15071da177e4SLinus Torvalds 	struct neigh_parms *parms =
15081da177e4SLinus Torvalds 		container_of(head, struct neigh_parms, rcu_head);
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	neigh_parms_put(parms);
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
15141da177e4SLinus Torvalds {
15151da177e4SLinus Torvalds 	if (!parms || parms == &tbl->parms)
15161da177e4SLinus Torvalds 		return;
15171da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
151875fbfd33SNicolas Dichtel 	list_del(&parms->list);
15191da177e4SLinus Torvalds 	parms->dead = 1;
15201da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1521cecbb639SDavid S. Miller 	if (parms->dev)
1522cecbb639SDavid S. Miller 		dev_put(parms->dev);
15231da177e4SLinus Torvalds 	call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
15241da177e4SLinus Torvalds }
15250a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release);
15261da177e4SLinus Torvalds 
152706f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	kfree(parms);
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
1532c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class;
1533c2ecba71SPavel Emelianov 
1534d7480fd3SWANG Cong static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1535d7480fd3SWANG Cong 
1536d7480fd3SWANG Cong void neigh_table_init(int index, struct neigh_table *tbl)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	unsigned long now = jiffies;
15391da177e4SLinus Torvalds 	unsigned long phsize;
15401da177e4SLinus Torvalds 
154175fbfd33SNicolas Dichtel 	INIT_LIST_HEAD(&tbl->parms_list);
154275fbfd33SNicolas Dichtel 	list_add(&tbl->parms.list, &tbl->parms_list);
1543e42ea986SEric Dumazet 	write_pnet(&tbl->parms.net, &init_net);
15446343944bSReshetova, Elena 	refcount_set(&tbl->parms.refcnt, 1);
15451da177e4SLinus Torvalds 	tbl->parms.reachable_time =
15461f9248e5SJiri Pirko 			  neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	tbl->stats = alloc_percpu(struct neigh_statistics);
15491da177e4SLinus Torvalds 	if (!tbl->stats)
15501da177e4SLinus Torvalds 		panic("cannot create neighbour cache statistics");
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
15539b739ba5SAlexey Dobriyan 	if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
15549b739ba5SAlexey Dobriyan 			      &neigh_stat_seq_fops, tbl))
15551da177e4SLinus Torvalds 		panic("cannot create neighbour proc dir entry");
15561da177e4SLinus Torvalds #endif
15571da177e4SLinus Torvalds 
1558cd089336SDavid S. Miller 	RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
156177d04bd9SAndrew Morton 	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
15621da177e4SLinus Torvalds 
1563d6bf7817SEric Dumazet 	if (!tbl->nht || !tbl->phash_buckets)
15641da177e4SLinus Torvalds 		panic("cannot allocate neighbour cache hashes");
15651da177e4SLinus Torvalds 
156608433effSYOSHIFUJI Hideaki / 吉藤英明 	if (!tbl->entry_size)
156708433effSYOSHIFUJI Hideaki / 吉藤英明 		tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
156808433effSYOSHIFUJI Hideaki / 吉藤英明 					tbl->key_len, NEIGH_PRIV_ALIGN);
156908433effSYOSHIFUJI Hideaki / 吉藤英明 	else
157008433effSYOSHIFUJI Hideaki / 吉藤英明 		WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
157108433effSYOSHIFUJI Hideaki / 吉藤英明 
15721da177e4SLinus Torvalds 	rwlock_init(&tbl->lock);
1573203b42f7STejun Heo 	INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1574f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1575f618002bSviresh kumar 			tbl->parms.reachable_time);
1576e99e88a9SKees Cook 	timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
1577c2ecba71SPavel Emelianov 	skb_queue_head_init_class(&tbl->proxy_queue,
1578c2ecba71SPavel Emelianov 			&neigh_table_proxy_queue_class);
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	tbl->last_flush = now;
15811da177e4SLinus Torvalds 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1582bd89efc5SSimon Kelley 
1583d7480fd3SWANG Cong 	neigh_tables[index] = tbl;
15841da177e4SLinus Torvalds }
15850a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init);
15861da177e4SLinus Torvalds 
1587d7480fd3SWANG Cong int neigh_table_clear(int index, struct neigh_table *tbl)
15881da177e4SLinus Torvalds {
1589d7480fd3SWANG Cong 	neigh_tables[index] = NULL;
15901da177e4SLinus Torvalds 	/* It is not clean... Fix it to unload IPv6 module safely */
1591a5c30b34STejun Heo 	cancel_delayed_work_sync(&tbl->gc_work);
15921da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
15931da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
15941da177e4SLinus Torvalds 	neigh_ifdown(tbl, NULL);
15951da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries))
1596e005d193SJoe Perches 		pr_crit("neighbour leakage\n");
15971da177e4SLinus Torvalds 
15986193d2beSEric Dumazet 	call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
15996193d2beSEric Dumazet 		 neigh_hash_free_rcu);
1600d6bf7817SEric Dumazet 	tbl->nht = NULL;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	kfree(tbl->phash_buckets);
16031da177e4SLinus Torvalds 	tbl->phash_buckets = NULL;
16041da177e4SLinus Torvalds 
16053f192b5cSAlexey Dobriyan 	remove_proc_entry(tbl->id, init_net.proc_net_stat);
16063f192b5cSAlexey Dobriyan 
16073fcde74bSKirill Korotaev 	free_percpu(tbl->stats);
16083fcde74bSKirill Korotaev 	tbl->stats = NULL;
16093fcde74bSKirill Korotaev 
16101da177e4SLinus Torvalds 	return 0;
16111da177e4SLinus Torvalds }
16120a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear);
16131da177e4SLinus Torvalds 
1614d7480fd3SWANG Cong static struct neigh_table *neigh_find_table(int family)
1615d7480fd3SWANG Cong {
1616d7480fd3SWANG Cong 	struct neigh_table *tbl = NULL;
1617d7480fd3SWANG Cong 
1618d7480fd3SWANG Cong 	switch (family) {
1619d7480fd3SWANG Cong 	case AF_INET:
1620d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_ARP_TABLE];
1621d7480fd3SWANG Cong 		break;
1622d7480fd3SWANG Cong 	case AF_INET6:
1623d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_ND_TABLE];
1624d7480fd3SWANG Cong 		break;
1625d7480fd3SWANG Cong 	case AF_DECnet:
1626d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_DN_TABLE];
1627d7480fd3SWANG Cong 		break;
1628d7480fd3SWANG Cong 	}
1629d7480fd3SWANG Cong 
1630d7480fd3SWANG Cong 	return tbl;
1631d7480fd3SWANG Cong }
1632d7480fd3SWANG Cong 
1633c21ef3e3SDavid Ahern static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1634c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
16351da177e4SLinus Torvalds {
16363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1637a14a49d2SThomas Graf 	struct ndmsg *ndm;
1638a14a49d2SThomas Graf 	struct nlattr *dst_attr;
16391da177e4SLinus Torvalds 	struct neigh_table *tbl;
1640d7480fd3SWANG Cong 	struct neighbour *neigh;
16411da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1642a14a49d2SThomas Graf 	int err = -EINVAL;
16431da177e4SLinus Torvalds 
1644110b2499SEric Dumazet 	ASSERT_RTNL();
1645a14a49d2SThomas Graf 	if (nlmsg_len(nlh) < sizeof(*ndm))
16461da177e4SLinus Torvalds 		goto out;
16471da177e4SLinus Torvalds 
1648a14a49d2SThomas Graf 	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1649a14a49d2SThomas Graf 	if (dst_attr == NULL)
1650a14a49d2SThomas Graf 		goto out;
1651a14a49d2SThomas Graf 
1652a14a49d2SThomas Graf 	ndm = nlmsg_data(nlh);
1653a14a49d2SThomas Graf 	if (ndm->ndm_ifindex) {
1654110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1655a14a49d2SThomas Graf 		if (dev == NULL) {
1656a14a49d2SThomas Graf 			err = -ENODEV;
1657a14a49d2SThomas Graf 			goto out;
1658a14a49d2SThomas Graf 		}
1659a14a49d2SThomas Graf 	}
1660a14a49d2SThomas Graf 
1661d7480fd3SWANG Cong 	tbl = neigh_find_table(ndm->ndm_family);
1662d7480fd3SWANG Cong 	if (tbl == NULL)
1663d7480fd3SWANG Cong 		return -EAFNOSUPPORT;
16641da177e4SLinus Torvalds 
166501ccdf12SAlexey Dobriyan 	if (nla_len(dst_attr) < (int)tbl->key_len)
1666110b2499SEric Dumazet 		goto out;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	if (ndm->ndm_flags & NTF_PROXY) {
1669426b5303SEric W. Biederman 		err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1670110b2499SEric Dumazet 		goto out;
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 
1673a14a49d2SThomas Graf 	if (dev == NULL)
1674110b2499SEric Dumazet 		goto out;
16751da177e4SLinus Torvalds 
1676a14a49d2SThomas Graf 	neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1677a14a49d2SThomas Graf 	if (neigh == NULL) {
1678a14a49d2SThomas Graf 		err = -ENOENT;
1679110b2499SEric Dumazet 		goto out;
1680a14a49d2SThomas Graf 	}
1681a14a49d2SThomas Graf 
1682a14a49d2SThomas Graf 	err = neigh_update(neigh, NULL, NUD_FAILED,
16831da177e4SLinus Torvalds 			   NEIGH_UPDATE_F_OVERRIDE |
16847b8f7a40SRoopa Prabhu 			   NEIGH_UPDATE_F_ADMIN,
16857b8f7a40SRoopa Prabhu 			   NETLINK_CB(skb).portid);
16865071034eSSowmini Varadhan 	write_lock_bh(&tbl->lock);
1687a14a49d2SThomas Graf 	neigh_release(neigh);
16885071034eSSowmini Varadhan 	neigh_remove_one(neigh, tbl);
16895071034eSSowmini Varadhan 	write_unlock_bh(&tbl->lock);
1690a14a49d2SThomas Graf 
16911da177e4SLinus Torvalds out:
16921da177e4SLinus Torvalds 	return err;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
1695c21ef3e3SDavid Ahern static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1696c21ef3e3SDavid Ahern 		     struct netlink_ext_ack *extack)
16971da177e4SLinus Torvalds {
1698d7480fd3SWANG Cong 	int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
16993b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
17005208debdSThomas Graf 	struct ndmsg *ndm;
17015208debdSThomas Graf 	struct nlattr *tb[NDA_MAX+1];
17021da177e4SLinus Torvalds 	struct neigh_table *tbl;
17031da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1704d7480fd3SWANG Cong 	struct neighbour *neigh;
1705d7480fd3SWANG Cong 	void *dst, *lladdr;
17065208debdSThomas Graf 	int err;
17071da177e4SLinus Torvalds 
1708110b2499SEric Dumazet 	ASSERT_RTNL();
1709c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
17105208debdSThomas Graf 	if (err < 0)
17111da177e4SLinus Torvalds 		goto out;
17121da177e4SLinus Torvalds 
17135208debdSThomas Graf 	err = -EINVAL;
17145208debdSThomas Graf 	if (tb[NDA_DST] == NULL)
17155208debdSThomas Graf 		goto out;
17165208debdSThomas Graf 
17175208debdSThomas Graf 	ndm = nlmsg_data(nlh);
17185208debdSThomas Graf 	if (ndm->ndm_ifindex) {
1719110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
17205208debdSThomas Graf 		if (dev == NULL) {
17215208debdSThomas Graf 			err = -ENODEV;
17225208debdSThomas Graf 			goto out;
17235208debdSThomas Graf 		}
17245208debdSThomas Graf 
17255208debdSThomas Graf 		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1726110b2499SEric Dumazet 			goto out;
17275208debdSThomas Graf 	}
17285208debdSThomas Graf 
1729d7480fd3SWANG Cong 	tbl = neigh_find_table(ndm->ndm_family);
1730d7480fd3SWANG Cong 	if (tbl == NULL)
1731d7480fd3SWANG Cong 		return -EAFNOSUPPORT;
17321da177e4SLinus Torvalds 
173301ccdf12SAlexey Dobriyan 	if (nla_len(tb[NDA_DST]) < (int)tbl->key_len)
1734110b2499SEric Dumazet 		goto out;
17355208debdSThomas Graf 	dst = nla_data(tb[NDA_DST]);
17365208debdSThomas Graf 	lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	if (ndm->ndm_flags & NTF_PROXY) {
173962dd9318SVille Nuorvala 		struct pneigh_entry *pn;
174062dd9318SVille Nuorvala 
17415208debdSThomas Graf 		err = -ENOBUFS;
1742426b5303SEric W. Biederman 		pn = pneigh_lookup(tbl, net, dst, dev, 1);
174362dd9318SVille Nuorvala 		if (pn) {
174462dd9318SVille Nuorvala 			pn->flags = ndm->ndm_flags;
174562dd9318SVille Nuorvala 			err = 0;
174662dd9318SVille Nuorvala 		}
1747110b2499SEric Dumazet 		goto out;
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds 
17505208debdSThomas Graf 	if (dev == NULL)
1751110b2499SEric Dumazet 		goto out;
17521da177e4SLinus Torvalds 
17535208debdSThomas Graf 	neigh = neigh_lookup(tbl, dst, dev);
17545208debdSThomas Graf 	if (neigh == NULL) {
17555208debdSThomas Graf 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
17561da177e4SLinus Torvalds 			err = -ENOENT;
1757110b2499SEric Dumazet 			goto out;
17585208debdSThomas Graf 		}
17595208debdSThomas Graf 
17605208debdSThomas Graf 		neigh = __neigh_lookup_errno(tbl, dst, dev);
17615208debdSThomas Graf 		if (IS_ERR(neigh)) {
17625208debdSThomas Graf 			err = PTR_ERR(neigh);
1763110b2499SEric Dumazet 			goto out;
17641da177e4SLinus Torvalds 		}
17655208debdSThomas Graf 	} else {
17665208debdSThomas Graf 		if (nlh->nlmsg_flags & NLM_F_EXCL) {
17675208debdSThomas Graf 			err = -EEXIST;
17685208debdSThomas Graf 			neigh_release(neigh);
1769110b2499SEric Dumazet 			goto out;
17701da177e4SLinus Torvalds 		}
17711da177e4SLinus Torvalds 
17725208debdSThomas Graf 		if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
17735208debdSThomas Graf 			flags &= ~NEIGH_UPDATE_F_OVERRIDE;
17745208debdSThomas Graf 	}
17751da177e4SLinus Torvalds 
17760c5c2d30SEric Biederman 	if (ndm->ndm_flags & NTF_USE) {
17770c5c2d30SEric Biederman 		neigh_event_send(neigh, NULL);
17780c5c2d30SEric Biederman 		err = 0;
17790c5c2d30SEric Biederman 	} else
17807b8f7a40SRoopa Prabhu 		err = neigh_update(neigh, lladdr, ndm->ndm_state, flags,
17817b8f7a40SRoopa Prabhu 				   NETLINK_CB(skb).portid);
17825208debdSThomas Graf 	neigh_release(neigh);
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds out:
17851da177e4SLinus Torvalds 	return err;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
1788c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1789c7fb64dbSThomas Graf {
1790ca860fb3SThomas Graf 	struct nlattr *nest;
1791e386c6ebSThomas Graf 
1792ca860fb3SThomas Graf 	nest = nla_nest_start(skb, NDTA_PARMS);
1793ca860fb3SThomas Graf 	if (nest == NULL)
1794ca860fb3SThomas Graf 		return -ENOBUFS;
1795c7fb64dbSThomas Graf 
17969a6308d7SDavid S. Miller 	if ((parms->dev &&
17979a6308d7SDavid S. Miller 	     nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
17986343944bSReshetova, Elena 	    nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
17991f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
18001f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
18018b5c171bSEric Dumazet 	    /* approximative value for deprecated QUEUE_LEN (in packets) */
18029a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_QUEUE_LEN,
18031f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
18041f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
18051f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
18061f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_UCAST_PROBES,
18071f9248e5SJiri Pirko 			NEIGH_VAR(parms, UCAST_PROBES)) ||
18081f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_MCAST_PROBES,
18091f9248e5SJiri Pirko 			NEIGH_VAR(parms, MCAST_PROBES)) ||
18108da86466SYOSHIFUJI Hideaki/吉藤英明 	    nla_put_u32(skb, NDTPA_MCAST_REPROBES,
18118da86466SYOSHIFUJI Hideaki/吉藤英明 			NEIGH_VAR(parms, MCAST_REPROBES)) ||
18122175d87cSNicolas Dichtel 	    nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
18132175d87cSNicolas Dichtel 			  NDTPA_PAD) ||
18149a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
18152175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
18161f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_GC_STALETIME,
18172175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
18189a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
18192175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
18201f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_RETRANS_TIME,
18212175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
18221f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
18232175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
18241f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_PROXY_DELAY,
18252175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
18261f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_LOCKTIME,
18272175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
18289a6308d7SDavid S. Miller 		goto nla_put_failure;
1829ca860fb3SThomas Graf 	return nla_nest_end(skb, nest);
1830c7fb64dbSThomas Graf 
1831ca860fb3SThomas Graf nla_put_failure:
1832bc3ed28cSThomas Graf 	nla_nest_cancel(skb, nest);
1833bc3ed28cSThomas Graf 	return -EMSGSIZE;
1834c7fb64dbSThomas Graf }
1835c7fb64dbSThomas Graf 
1836ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1837ca860fb3SThomas Graf 			      u32 pid, u32 seq, int type, int flags)
1838c7fb64dbSThomas Graf {
1839c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1840c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1841c7fb64dbSThomas Graf 
1842ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1843ca860fb3SThomas Graf 	if (nlh == NULL)
184426932566SPatrick McHardy 		return -EMSGSIZE;
1845c7fb64dbSThomas Graf 
1846ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1847c7fb64dbSThomas Graf 
1848c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1849c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
18509ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
18519ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1852c7fb64dbSThomas Graf 
18539a6308d7SDavid S. Miller 	if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
18542175d87cSNicolas Dichtel 	    nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
18559a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
18569a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
18579a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
18589a6308d7SDavid S. Miller 		goto nla_put_failure;
1859c7fb64dbSThomas Graf 	{
1860c7fb64dbSThomas Graf 		unsigned long now = jiffies;
1861c7fb64dbSThomas Graf 		unsigned int flush_delta = now - tbl->last_flush;
1862c7fb64dbSThomas Graf 		unsigned int rand_delta = now - tbl->last_rand;
1863d6bf7817SEric Dumazet 		struct neigh_hash_table *nht;
1864c7fb64dbSThomas Graf 		struct ndt_config ndc = {
1865c7fb64dbSThomas Graf 			.ndtc_key_len		= tbl->key_len,
1866c7fb64dbSThomas Graf 			.ndtc_entry_size	= tbl->entry_size,
1867c7fb64dbSThomas Graf 			.ndtc_entries		= atomic_read(&tbl->entries),
1868c7fb64dbSThomas Graf 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
1869c7fb64dbSThomas Graf 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
1870c7fb64dbSThomas Graf 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
1871c7fb64dbSThomas Graf 		};
1872c7fb64dbSThomas Graf 
1873d6bf7817SEric Dumazet 		rcu_read_lock_bh();
1874d6bf7817SEric Dumazet 		nht = rcu_dereference_bh(tbl->nht);
18752c2aba6cSDavid S. Miller 		ndc.ndtc_hash_rnd = nht->hash_rnd[0];
1876cd089336SDavid S. Miller 		ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
1877d6bf7817SEric Dumazet 		rcu_read_unlock_bh();
1878d6bf7817SEric Dumazet 
18799a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
18809a6308d7SDavid S. Miller 			goto nla_put_failure;
1881c7fb64dbSThomas Graf 	}
1882c7fb64dbSThomas Graf 
1883c7fb64dbSThomas Graf 	{
1884c7fb64dbSThomas Graf 		int cpu;
1885c7fb64dbSThomas Graf 		struct ndt_stats ndst;
1886c7fb64dbSThomas Graf 
1887c7fb64dbSThomas Graf 		memset(&ndst, 0, sizeof(ndst));
1888c7fb64dbSThomas Graf 
18896f912042SKAMEZAWA Hiroyuki 		for_each_possible_cpu(cpu) {
1890c7fb64dbSThomas Graf 			struct neigh_statistics	*st;
1891c7fb64dbSThomas Graf 
1892c7fb64dbSThomas Graf 			st = per_cpu_ptr(tbl->stats, cpu);
1893c7fb64dbSThomas Graf 			ndst.ndts_allocs		+= st->allocs;
1894c7fb64dbSThomas Graf 			ndst.ndts_destroys		+= st->destroys;
1895c7fb64dbSThomas Graf 			ndst.ndts_hash_grows		+= st->hash_grows;
1896c7fb64dbSThomas Graf 			ndst.ndts_res_failed		+= st->res_failed;
1897c7fb64dbSThomas Graf 			ndst.ndts_lookups		+= st->lookups;
1898c7fb64dbSThomas Graf 			ndst.ndts_hits			+= st->hits;
1899c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
1900c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
1901c7fb64dbSThomas Graf 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
1902c7fb64dbSThomas Graf 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
1903fb811395SRick Jones 			ndst.ndts_table_fulls		+= st->table_fulls;
1904c7fb64dbSThomas Graf 		}
1905c7fb64dbSThomas Graf 
1906b676338fSNicolas Dichtel 		if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
1907b676338fSNicolas Dichtel 				  NDTA_PAD))
19089a6308d7SDavid S. Miller 			goto nla_put_failure;
1909c7fb64dbSThomas Graf 	}
1910c7fb64dbSThomas Graf 
1911c7fb64dbSThomas Graf 	BUG_ON(tbl->parms.dev);
1912c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1913ca860fb3SThomas Graf 		goto nla_put_failure;
1914c7fb64dbSThomas Graf 
1915c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1916053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1917053c095aSJohannes Berg 	return 0;
1918c7fb64dbSThomas Graf 
1919ca860fb3SThomas Graf nla_put_failure:
1920c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
192126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
192226932566SPatrick McHardy 	return -EMSGSIZE;
1923c7fb64dbSThomas Graf }
1924c7fb64dbSThomas Graf 
1925ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb,
1926ca860fb3SThomas Graf 				    struct neigh_table *tbl,
1927c7fb64dbSThomas Graf 				    struct neigh_parms *parms,
1928ca860fb3SThomas Graf 				    u32 pid, u32 seq, int type,
1929ca860fb3SThomas Graf 				    unsigned int flags)
1930c7fb64dbSThomas Graf {
1931c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1932c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1933c7fb64dbSThomas Graf 
1934ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1935ca860fb3SThomas Graf 	if (nlh == NULL)
193626932566SPatrick McHardy 		return -EMSGSIZE;
1937c7fb64dbSThomas Graf 
1938ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1939c7fb64dbSThomas Graf 
1940c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1941c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
19429ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
19439ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1944c7fb64dbSThomas Graf 
1945ca860fb3SThomas Graf 	if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1946ca860fb3SThomas Graf 	    neightbl_fill_parms(skb, parms) < 0)
1947ca860fb3SThomas Graf 		goto errout;
1948c7fb64dbSThomas Graf 
1949c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1950053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1951053c095aSJohannes Berg 	return 0;
1952ca860fb3SThomas Graf errout:
1953c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
195426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
195526932566SPatrick McHardy 	return -EMSGSIZE;
1956c7fb64dbSThomas Graf }
1957c7fb64dbSThomas Graf 
1958ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
19596b3f8674SThomas Graf 	[NDTA_NAME]		= { .type = NLA_STRING },
19606b3f8674SThomas Graf 	[NDTA_THRESH1]		= { .type = NLA_U32 },
19616b3f8674SThomas Graf 	[NDTA_THRESH2]		= { .type = NLA_U32 },
19626b3f8674SThomas Graf 	[NDTA_THRESH3]		= { .type = NLA_U32 },
19636b3f8674SThomas Graf 	[NDTA_GC_INTERVAL]	= { .type = NLA_U64 },
19646b3f8674SThomas Graf 	[NDTA_PARMS]		= { .type = NLA_NESTED },
19656b3f8674SThomas Graf };
19666b3f8674SThomas Graf 
1967ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
19686b3f8674SThomas Graf 	[NDTPA_IFINDEX]			= { .type = NLA_U32 },
19696b3f8674SThomas Graf 	[NDTPA_QUEUE_LEN]		= { .type = NLA_U32 },
19706b3f8674SThomas Graf 	[NDTPA_PROXY_QLEN]		= { .type = NLA_U32 },
19716b3f8674SThomas Graf 	[NDTPA_APP_PROBES]		= { .type = NLA_U32 },
19726b3f8674SThomas Graf 	[NDTPA_UCAST_PROBES]		= { .type = NLA_U32 },
19736b3f8674SThomas Graf 	[NDTPA_MCAST_PROBES]		= { .type = NLA_U32 },
19748da86466SYOSHIFUJI Hideaki/吉藤英明 	[NDTPA_MCAST_REPROBES]		= { .type = NLA_U32 },
19756b3f8674SThomas Graf 	[NDTPA_BASE_REACHABLE_TIME]	= { .type = NLA_U64 },
19766b3f8674SThomas Graf 	[NDTPA_GC_STALETIME]		= { .type = NLA_U64 },
19776b3f8674SThomas Graf 	[NDTPA_DELAY_PROBE_TIME]	= { .type = NLA_U64 },
19786b3f8674SThomas Graf 	[NDTPA_RETRANS_TIME]		= { .type = NLA_U64 },
19796b3f8674SThomas Graf 	[NDTPA_ANYCAST_DELAY]		= { .type = NLA_U64 },
19806b3f8674SThomas Graf 	[NDTPA_PROXY_DELAY]		= { .type = NLA_U64 },
19816b3f8674SThomas Graf 	[NDTPA_LOCKTIME]		= { .type = NLA_U64 },
19826b3f8674SThomas Graf };
19836b3f8674SThomas Graf 
1984c21ef3e3SDavid Ahern static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
1985c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
1986c7fb64dbSThomas Graf {
19873b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1988c7fb64dbSThomas Graf 	struct neigh_table *tbl;
19896b3f8674SThomas Graf 	struct ndtmsg *ndtmsg;
19906b3f8674SThomas Graf 	struct nlattr *tb[NDTA_MAX+1];
1991d7480fd3SWANG Cong 	bool found = false;
1992d7480fd3SWANG Cong 	int err, tidx;
1993c7fb64dbSThomas Graf 
19946b3f8674SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
1995c21ef3e3SDavid Ahern 			  nl_neightbl_policy, extack);
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);
2005d7480fd3SWANG Cong 
2006d7480fd3SWANG Cong 	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2007d7480fd3SWANG Cong 		tbl = neigh_tables[tidx];
2008d7480fd3SWANG Cong 		if (!tbl)
2009d7480fd3SWANG Cong 			continue;
2010c7fb64dbSThomas Graf 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2011c7fb64dbSThomas Graf 			continue;
2012d7480fd3SWANG Cong 		if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2013d7480fd3SWANG Cong 			found = true;
2014c7fb64dbSThomas Graf 			break;
2015c7fb64dbSThomas Graf 		}
2016c7fb64dbSThomas Graf 	}
2017c7fb64dbSThomas Graf 
2018d7480fd3SWANG Cong 	if (!found)
2019d7480fd3SWANG Cong 		return -ENOENT;
2020d7480fd3SWANG Cong 
2021c7fb64dbSThomas Graf 	/*
2022c7fb64dbSThomas Graf 	 * We acquire tbl->lock to be nice to the periodic timers and
2023c7fb64dbSThomas Graf 	 * make sure they always see a consistent set of values.
2024c7fb64dbSThomas Graf 	 */
2025c7fb64dbSThomas Graf 	write_lock_bh(&tbl->lock);
2026c7fb64dbSThomas Graf 
20276b3f8674SThomas Graf 	if (tb[NDTA_PARMS]) {
20286b3f8674SThomas Graf 		struct nlattr *tbp[NDTPA_MAX+1];
2029c7fb64dbSThomas Graf 		struct neigh_parms *p;
20306b3f8674SThomas Graf 		int i, ifindex = 0;
2031c7fb64dbSThomas Graf 
20326b3f8674SThomas Graf 		err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
2033c21ef3e3SDavid Ahern 				       nl_ntbl_parm_policy, extack);
20346b3f8674SThomas Graf 		if (err < 0)
20356b3f8674SThomas Graf 			goto errout_tbl_lock;
2036c7fb64dbSThomas Graf 
20376b3f8674SThomas Graf 		if (tbp[NDTPA_IFINDEX])
20386b3f8674SThomas Graf 			ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2039c7fb64dbSThomas Graf 
204097fd5bc7STobias Klauser 		p = lookup_neigh_parms(tbl, net, ifindex);
2041c7fb64dbSThomas Graf 		if (p == NULL) {
2042c7fb64dbSThomas Graf 			err = -ENOENT;
20436b3f8674SThomas Graf 			goto errout_tbl_lock;
2044c7fb64dbSThomas Graf 		}
2045c7fb64dbSThomas Graf 
20466b3f8674SThomas Graf 		for (i = 1; i <= NDTPA_MAX; i++) {
20476b3f8674SThomas Graf 			if (tbp[i] == NULL)
20486b3f8674SThomas Graf 				continue;
2049c7fb64dbSThomas Graf 
20506b3f8674SThomas Graf 			switch (i) {
20516b3f8674SThomas Graf 			case NDTPA_QUEUE_LEN:
20521f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
20531f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]) *
20541f9248e5SJiri Pirko 					      SKB_TRUESIZE(ETH_FRAME_LEN));
20558b5c171bSEric Dumazet 				break;
20568b5c171bSEric Dumazet 			case NDTPA_QUEUE_LENBYTES:
20571f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
20581f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20596b3f8674SThomas Graf 				break;
20606b3f8674SThomas Graf 			case NDTPA_PROXY_QLEN:
20611f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, PROXY_QLEN,
20621f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20636b3f8674SThomas Graf 				break;
20646b3f8674SThomas Graf 			case NDTPA_APP_PROBES:
20651f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, APP_PROBES,
20661f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20676b3f8674SThomas Graf 				break;
20686b3f8674SThomas Graf 			case NDTPA_UCAST_PROBES:
20691f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, UCAST_PROBES,
20701f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20716b3f8674SThomas Graf 				break;
20726b3f8674SThomas Graf 			case NDTPA_MCAST_PROBES:
20731f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, MCAST_PROBES,
20741f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20756b3f8674SThomas Graf 				break;
20768da86466SYOSHIFUJI Hideaki/吉藤英明 			case NDTPA_MCAST_REPROBES:
20778da86466SYOSHIFUJI Hideaki/吉藤英明 				NEIGH_VAR_SET(p, MCAST_REPROBES,
20788da86466SYOSHIFUJI Hideaki/吉藤英明 					      nla_get_u32(tbp[i]));
20798da86466SYOSHIFUJI Hideaki/吉藤英明 				break;
20806b3f8674SThomas Graf 			case NDTPA_BASE_REACHABLE_TIME:
20811f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
20821f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20834bf6980dSJean-Francois Remy 				/* update reachable_time as well, otherwise, the change will
20844bf6980dSJean-Francois Remy 				 * only be effective after the next time neigh_periodic_work
20854bf6980dSJean-Francois Remy 				 * decides to recompute it (can be multiple minutes)
20864bf6980dSJean-Francois Remy 				 */
20874bf6980dSJean-Francois Remy 				p->reachable_time =
20884bf6980dSJean-Francois Remy 					neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
20896b3f8674SThomas Graf 				break;
20906b3f8674SThomas Graf 			case NDTPA_GC_STALETIME:
20911f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, GC_STALETIME,
20921f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20936b3f8674SThomas Graf 				break;
20946b3f8674SThomas Graf 			case NDTPA_DELAY_PROBE_TIME:
20951f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
20961f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20972a4501aeSIdo Schimmel 				call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
20986b3f8674SThomas Graf 				break;
20996b3f8674SThomas Graf 			case NDTPA_RETRANS_TIME:
21001f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, RETRANS_TIME,
21011f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
21026b3f8674SThomas Graf 				break;
21036b3f8674SThomas Graf 			case NDTPA_ANYCAST_DELAY:
21043977458cSJiri Pirko 				NEIGH_VAR_SET(p, ANYCAST_DELAY,
21053977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
21066b3f8674SThomas Graf 				break;
21076b3f8674SThomas Graf 			case NDTPA_PROXY_DELAY:
21083977458cSJiri Pirko 				NEIGH_VAR_SET(p, PROXY_DELAY,
21093977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
21106b3f8674SThomas Graf 				break;
21116b3f8674SThomas Graf 			case NDTPA_LOCKTIME:
21123977458cSJiri Pirko 				NEIGH_VAR_SET(p, LOCKTIME,
21133977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
21146b3f8674SThomas Graf 				break;
2115c7fb64dbSThomas Graf 			}
21166b3f8674SThomas Graf 		}
21176b3f8674SThomas Graf 	}
21186b3f8674SThomas Graf 
2119dc25c676SGao feng 	err = -ENOENT;
2120dc25c676SGao feng 	if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2121dc25c676SGao feng 	     tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2122dc25c676SGao feng 	    !net_eq(net, &init_net))
2123dc25c676SGao feng 		goto errout_tbl_lock;
2124dc25c676SGao feng 
21256b3f8674SThomas Graf 	if (tb[NDTA_THRESH1])
21266b3f8674SThomas Graf 		tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
21276b3f8674SThomas Graf 
21286b3f8674SThomas Graf 	if (tb[NDTA_THRESH2])
21296b3f8674SThomas Graf 		tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
21306b3f8674SThomas Graf 
21316b3f8674SThomas Graf 	if (tb[NDTA_THRESH3])
21326b3f8674SThomas Graf 		tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
21336b3f8674SThomas Graf 
21346b3f8674SThomas Graf 	if (tb[NDTA_GC_INTERVAL])
21356b3f8674SThomas Graf 		tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2136c7fb64dbSThomas Graf 
2137c7fb64dbSThomas Graf 	err = 0;
2138c7fb64dbSThomas Graf 
21396b3f8674SThomas Graf errout_tbl_lock:
2140c7fb64dbSThomas Graf 	write_unlock_bh(&tbl->lock);
21416b3f8674SThomas Graf errout:
2142c7fb64dbSThomas Graf 	return err;
2143c7fb64dbSThomas Graf }
2144c7fb64dbSThomas Graf 
2145c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2146c7fb64dbSThomas Graf {
21473b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2148ca860fb3SThomas Graf 	int family, tidx, nidx = 0;
2149ca860fb3SThomas Graf 	int tbl_skip = cb->args[0];
2150ca860fb3SThomas Graf 	int neigh_skip = cb->args[1];
2151c7fb64dbSThomas Graf 	struct neigh_table *tbl;
2152c7fb64dbSThomas Graf 
2153ca860fb3SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2154c7fb64dbSThomas Graf 
2155d7480fd3SWANG Cong 	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2156c7fb64dbSThomas Graf 		struct neigh_parms *p;
2157c7fb64dbSThomas Graf 
2158d7480fd3SWANG Cong 		tbl = neigh_tables[tidx];
2159d7480fd3SWANG Cong 		if (!tbl)
2160d7480fd3SWANG Cong 			continue;
2161d7480fd3SWANG Cong 
2162ca860fb3SThomas Graf 		if (tidx < tbl_skip || (family && tbl->family != family))
2163c7fb64dbSThomas Graf 			continue;
2164c7fb64dbSThomas Graf 
216515e47304SEric W. Biederman 		if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2166ca860fb3SThomas Graf 				       cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
21677b46a644SDavid S. Miller 				       NLM_F_MULTI) < 0)
2168c7fb64dbSThomas Graf 			break;
2169c7fb64dbSThomas Graf 
217075fbfd33SNicolas Dichtel 		nidx = 0;
217175fbfd33SNicolas Dichtel 		p = list_next_entry(&tbl->parms, list);
217275fbfd33SNicolas Dichtel 		list_for_each_entry_from(p, &tbl->parms_list, list) {
2173878628fbSYOSHIFUJI Hideaki 			if (!net_eq(neigh_parms_net(p), net))
2174426b5303SEric W. Biederman 				continue;
2175426b5303SEric W. Biederman 
2176efc683fcSGautam Kachroo 			if (nidx < neigh_skip)
2177efc683fcSGautam Kachroo 				goto next;
2178c7fb64dbSThomas Graf 
2179ca860fb3SThomas Graf 			if (neightbl_fill_param_info(skb, tbl, p,
218015e47304SEric W. Biederman 						     NETLINK_CB(cb->skb).portid,
2181ca860fb3SThomas Graf 						     cb->nlh->nlmsg_seq,
2182ca860fb3SThomas Graf 						     RTM_NEWNEIGHTBL,
21837b46a644SDavid S. Miller 						     NLM_F_MULTI) < 0)
2184c7fb64dbSThomas Graf 				goto out;
2185efc683fcSGautam Kachroo 		next:
2186efc683fcSGautam Kachroo 			nidx++;
2187c7fb64dbSThomas Graf 		}
2188c7fb64dbSThomas Graf 
2189ca860fb3SThomas Graf 		neigh_skip = 0;
2190c7fb64dbSThomas Graf 	}
2191c7fb64dbSThomas Graf out:
2192ca860fb3SThomas Graf 	cb->args[0] = tidx;
2193ca860fb3SThomas Graf 	cb->args[1] = nidx;
2194c7fb64dbSThomas Graf 
2195c7fb64dbSThomas Graf 	return skb->len;
2196c7fb64dbSThomas Graf }
21971da177e4SLinus Torvalds 
21988b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
21998b8aec50SThomas Graf 			   u32 pid, u32 seq, int type, unsigned int flags)
22001da177e4SLinus Torvalds {
22011da177e4SLinus Torvalds 	unsigned long now = jiffies;
22021da177e4SLinus Torvalds 	struct nda_cacheinfo ci;
22038b8aec50SThomas Graf 	struct nlmsghdr *nlh;
22048b8aec50SThomas Graf 	struct ndmsg *ndm;
22051da177e4SLinus Torvalds 
22068b8aec50SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
22078b8aec50SThomas Graf 	if (nlh == NULL)
220826932566SPatrick McHardy 		return -EMSGSIZE;
22098b8aec50SThomas Graf 
22108b8aec50SThomas Graf 	ndm = nlmsg_data(nlh);
22118b8aec50SThomas Graf 	ndm->ndm_family	 = neigh->ops->family;
22129ef1d4c7SPatrick McHardy 	ndm->ndm_pad1    = 0;
22139ef1d4c7SPatrick McHardy 	ndm->ndm_pad2    = 0;
22148b8aec50SThomas Graf 	ndm->ndm_flags	 = neigh->flags;
22158b8aec50SThomas Graf 	ndm->ndm_type	 = neigh->type;
22168b8aec50SThomas Graf 	ndm->ndm_ifindex = neigh->dev->ifindex;
22171da177e4SLinus Torvalds 
22189a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
22199a6308d7SDavid S. Miller 		goto nla_put_failure;
22208b8aec50SThomas Graf 
22218b8aec50SThomas Graf 	read_lock_bh(&neigh->lock);
22228b8aec50SThomas Graf 	ndm->ndm_state	 = neigh->nud_state;
22230ed8ddf4SEric Dumazet 	if (neigh->nud_state & NUD_VALID) {
22240ed8ddf4SEric Dumazet 		char haddr[MAX_ADDR_LEN];
22250ed8ddf4SEric Dumazet 
22260ed8ddf4SEric Dumazet 		neigh_ha_snapshot(haddr, neigh, neigh->dev);
22270ed8ddf4SEric Dumazet 		if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
22288b8aec50SThomas Graf 			read_unlock_bh(&neigh->lock);
22298b8aec50SThomas Graf 			goto nla_put_failure;
22308b8aec50SThomas Graf 		}
22310ed8ddf4SEric Dumazet 	}
22328b8aec50SThomas Graf 
2233b9f5f52cSStephen Hemminger 	ci.ndm_used	 = jiffies_to_clock_t(now - neigh->used);
2234b9f5f52cSStephen Hemminger 	ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2235b9f5f52cSStephen Hemminger 	ci.ndm_updated	 = jiffies_to_clock_t(now - neigh->updated);
22369f237430SReshetova, Elena 	ci.ndm_refcnt	 = refcount_read(&neigh->refcnt) - 1;
22378b8aec50SThomas Graf 	read_unlock_bh(&neigh->lock);
22388b8aec50SThomas Graf 
22399a6308d7SDavid S. Miller 	if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
22409a6308d7SDavid S. Miller 	    nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
22419a6308d7SDavid S. Miller 		goto nla_put_failure;
22428b8aec50SThomas Graf 
2243053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2244053c095aSJohannes Berg 	return 0;
22458b8aec50SThomas Graf 
22468b8aec50SThomas Graf nla_put_failure:
224726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
224826932566SPatrick McHardy 	return -EMSGSIZE;
22491da177e4SLinus Torvalds }
22501da177e4SLinus Torvalds 
225184920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
225284920c14STony Zelenoff 			    u32 pid, u32 seq, int type, unsigned int flags,
225384920c14STony Zelenoff 			    struct neigh_table *tbl)
225484920c14STony Zelenoff {
225584920c14STony Zelenoff 	struct nlmsghdr *nlh;
225684920c14STony Zelenoff 	struct ndmsg *ndm;
225784920c14STony Zelenoff 
225884920c14STony Zelenoff 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
225984920c14STony Zelenoff 	if (nlh == NULL)
226084920c14STony Zelenoff 		return -EMSGSIZE;
226184920c14STony Zelenoff 
226284920c14STony Zelenoff 	ndm = nlmsg_data(nlh);
226384920c14STony Zelenoff 	ndm->ndm_family	 = tbl->family;
226484920c14STony Zelenoff 	ndm->ndm_pad1    = 0;
226584920c14STony Zelenoff 	ndm->ndm_pad2    = 0;
226684920c14STony Zelenoff 	ndm->ndm_flags	 = pn->flags | NTF_PROXY;
2267545469f7SJun Zhao 	ndm->ndm_type	 = RTN_UNICAST;
22686adc5fd6SKonstantin Khlebnikov 	ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
226984920c14STony Zelenoff 	ndm->ndm_state	 = NUD_NONE;
227084920c14STony Zelenoff 
22719a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
22729a6308d7SDavid S. Miller 		goto nla_put_failure;
227384920c14STony Zelenoff 
2274053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2275053c095aSJohannes Berg 	return 0;
227684920c14STony Zelenoff 
227784920c14STony Zelenoff nla_put_failure:
227884920c14STony Zelenoff 	nlmsg_cancel(skb, nlh);
227984920c14STony Zelenoff 	return -EMSGSIZE;
228084920c14STony Zelenoff }
228184920c14STony Zelenoff 
22827b8f7a40SRoopa Prabhu static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
2283d961db35SThomas Graf {
2284d961db35SThomas Graf 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
22857b8f7a40SRoopa Prabhu 	__neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
2286d961db35SThomas Graf }
22871da177e4SLinus Torvalds 
228821fdd092SDavid Ahern static bool neigh_master_filtered(struct net_device *dev, int master_idx)
228921fdd092SDavid Ahern {
229021fdd092SDavid Ahern 	struct net_device *master;
229121fdd092SDavid Ahern 
229221fdd092SDavid Ahern 	if (!master_idx)
229321fdd092SDavid Ahern 		return false;
229421fdd092SDavid Ahern 
229521fdd092SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
229621fdd092SDavid Ahern 	if (!master || master->ifindex != master_idx)
229721fdd092SDavid Ahern 		return true;
229821fdd092SDavid Ahern 
229921fdd092SDavid Ahern 	return false;
230021fdd092SDavid Ahern }
230121fdd092SDavid Ahern 
230216660f0bSDavid Ahern static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
230316660f0bSDavid Ahern {
230416660f0bSDavid Ahern 	if (filter_idx && dev->ifindex != filter_idx)
230516660f0bSDavid Ahern 		return true;
230616660f0bSDavid Ahern 
230716660f0bSDavid Ahern 	return false;
230816660f0bSDavid Ahern }
230916660f0bSDavid Ahern 
23101da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
23111da177e4SLinus Torvalds 			    struct netlink_callback *cb)
23121da177e4SLinus Torvalds {
23133b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
231421fdd092SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
231521fdd092SDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
23161da177e4SLinus Torvalds 	struct neighbour *n;
23171da177e4SLinus Torvalds 	int rc, h, s_h = cb->args[1];
23181da177e4SLinus Torvalds 	int idx, s_idx = idx = cb->args[2];
2319d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
232016660f0bSDavid Ahern 	int filter_master_idx = 0, filter_idx = 0;
232121fdd092SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
232221fdd092SDavid Ahern 	int err;
232321fdd092SDavid Ahern 
2324fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL, NULL);
232521fdd092SDavid Ahern 	if (!err) {
232616660f0bSDavid Ahern 		if (tb[NDA_IFINDEX])
232716660f0bSDavid Ahern 			filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
232816660f0bSDavid Ahern 
232921fdd092SDavid Ahern 		if (tb[NDA_MASTER])
233021fdd092SDavid Ahern 			filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
233121fdd092SDavid Ahern 
233216660f0bSDavid Ahern 		if (filter_idx || filter_master_idx)
233321fdd092SDavid Ahern 			flags |= NLM_F_DUMP_FILTERED;
233421fdd092SDavid Ahern 	}
23351da177e4SLinus Torvalds 
2336d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2337d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2338d6bf7817SEric Dumazet 
23394bd6683bSEric Dumazet 	for (h = s_h; h < (1 << nht->hash_shift); h++) {
23401da177e4SLinus Torvalds 		if (h > s_h)
23411da177e4SLinus Torvalds 			s_idx = 0;
2342767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2343767e97e1SEric Dumazet 		     n != NULL;
2344767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next)) {
234518502acdSZhang Shengju 			if (idx < s_idx || !net_eq(dev_net(n->dev), net))
234618502acdSZhang Shengju 				goto next;
234718502acdSZhang Shengju 			if (neigh_ifindex_filtered(n->dev, filter_idx) ||
234818502acdSZhang Shengju 			    neigh_master_filtered(n->dev, filter_master_idx))
2349efc683fcSGautam Kachroo 				goto next;
235015e47304SEric W. Biederman 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
23511da177e4SLinus Torvalds 					    cb->nlh->nlmsg_seq,
2352b6544c0bSJamal Hadi Salim 					    RTM_NEWNEIGH,
235321fdd092SDavid Ahern 					    flags) < 0) {
23541da177e4SLinus Torvalds 				rc = -1;
23551da177e4SLinus Torvalds 				goto out;
23561da177e4SLinus Torvalds 			}
2357efc683fcSGautam Kachroo next:
2358efc683fcSGautam Kachroo 			idx++;
23591da177e4SLinus Torvalds 		}
23601da177e4SLinus Torvalds 	}
23611da177e4SLinus Torvalds 	rc = skb->len;
23621da177e4SLinus Torvalds out:
2363d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
23641da177e4SLinus Torvalds 	cb->args[1] = h;
23651da177e4SLinus Torvalds 	cb->args[2] = idx;
23661da177e4SLinus Torvalds 	return rc;
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds 
236984920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
237084920c14STony Zelenoff 			     struct netlink_callback *cb)
237184920c14STony Zelenoff {
237284920c14STony Zelenoff 	struct pneigh_entry *n;
237384920c14STony Zelenoff 	struct net *net = sock_net(skb->sk);
237484920c14STony Zelenoff 	int rc, h, s_h = cb->args[3];
237584920c14STony Zelenoff 	int idx, s_idx = idx = cb->args[4];
237684920c14STony Zelenoff 
237784920c14STony Zelenoff 	read_lock_bh(&tbl->lock);
237884920c14STony Zelenoff 
23794bd6683bSEric Dumazet 	for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
238084920c14STony Zelenoff 		if (h > s_h)
238184920c14STony Zelenoff 			s_idx = 0;
238284920c14STony Zelenoff 		for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
238318502acdSZhang Shengju 			if (idx < s_idx || pneigh_net(n) != net)
238484920c14STony Zelenoff 				goto next;
238515e47304SEric W. Biederman 			if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
238684920c14STony Zelenoff 					    cb->nlh->nlmsg_seq,
238784920c14STony Zelenoff 					    RTM_NEWNEIGH,
23887b46a644SDavid S. Miller 					    NLM_F_MULTI, tbl) < 0) {
238984920c14STony Zelenoff 				read_unlock_bh(&tbl->lock);
239084920c14STony Zelenoff 				rc = -1;
239184920c14STony Zelenoff 				goto out;
239284920c14STony Zelenoff 			}
239384920c14STony Zelenoff 		next:
239484920c14STony Zelenoff 			idx++;
239584920c14STony Zelenoff 		}
239684920c14STony Zelenoff 	}
239784920c14STony Zelenoff 
239884920c14STony Zelenoff 	read_unlock_bh(&tbl->lock);
239984920c14STony Zelenoff 	rc = skb->len;
240084920c14STony Zelenoff out:
240184920c14STony Zelenoff 	cb->args[3] = h;
240284920c14STony Zelenoff 	cb->args[4] = idx;
240384920c14STony Zelenoff 	return rc;
240484920c14STony Zelenoff 
240584920c14STony Zelenoff }
240684920c14STony Zelenoff 
2407c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
24081da177e4SLinus Torvalds {
24091da177e4SLinus Torvalds 	struct neigh_table *tbl;
24101da177e4SLinus Torvalds 	int t, family, s_t;
241184920c14STony Zelenoff 	int proxy = 0;
24124bd6683bSEric Dumazet 	int err;
24131da177e4SLinus Torvalds 
24148b8aec50SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
241584920c14STony Zelenoff 
241684920c14STony Zelenoff 	/* check for full ndmsg structure presence, family member is
241784920c14STony Zelenoff 	 * the same for both structures
241884920c14STony Zelenoff 	 */
241984920c14STony Zelenoff 	if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
242084920c14STony Zelenoff 	    ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
242184920c14STony Zelenoff 		proxy = 1;
242284920c14STony Zelenoff 
24231da177e4SLinus Torvalds 	s_t = cb->args[0];
24241da177e4SLinus Torvalds 
2425d7480fd3SWANG Cong 	for (t = 0; t < NEIGH_NR_TABLES; t++) {
2426d7480fd3SWANG Cong 		tbl = neigh_tables[t];
2427d7480fd3SWANG Cong 
2428d7480fd3SWANG Cong 		if (!tbl)
2429d7480fd3SWANG Cong 			continue;
24301da177e4SLinus Torvalds 		if (t < s_t || (family && tbl->family != family))
24311da177e4SLinus Torvalds 			continue;
24321da177e4SLinus Torvalds 		if (t > s_t)
24331da177e4SLinus Torvalds 			memset(&cb->args[1], 0, sizeof(cb->args) -
24341da177e4SLinus Torvalds 						sizeof(cb->args[0]));
243584920c14STony Zelenoff 		if (proxy)
243684920c14STony Zelenoff 			err = pneigh_dump_table(tbl, skb, cb);
243784920c14STony Zelenoff 		else
243884920c14STony Zelenoff 			err = neigh_dump_table(tbl, skb, cb);
24394bd6683bSEric Dumazet 		if (err < 0)
24404bd6683bSEric Dumazet 			break;
24411da177e4SLinus Torvalds 	}
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	cb->args[0] = t;
24441da177e4SLinus Torvalds 	return skb->len;
24451da177e4SLinus Torvalds }
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
24481da177e4SLinus Torvalds {
24491da177e4SLinus Torvalds 	int chain;
2450d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
24511da177e4SLinus Torvalds 
2452d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2453d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2454d6bf7817SEric Dumazet 
2455767e97e1SEric Dumazet 	read_lock(&tbl->lock); /* avoid resizes */
2456cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
24571da177e4SLinus Torvalds 		struct neighbour *n;
24581da177e4SLinus Torvalds 
2459767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2460767e97e1SEric Dumazet 		     n != NULL;
2461767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next))
24621da177e4SLinus Torvalds 			cb(n, cookie);
24631da177e4SLinus Torvalds 	}
2464d6bf7817SEric Dumazet 	read_unlock(&tbl->lock);
2465d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each);
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */
24701da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl,
24711da177e4SLinus Torvalds 			      int (*cb)(struct neighbour *))
24721da177e4SLinus Torvalds {
24731da177e4SLinus Torvalds 	int chain;
2474d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
24751da177e4SLinus Torvalds 
2476d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
2477d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
2478cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2479767e97e1SEric Dumazet 		struct neighbour *n;
2480767e97e1SEric Dumazet 		struct neighbour __rcu **np;
24811da177e4SLinus Torvalds 
2482d6bf7817SEric Dumazet 		np = &nht->hash_buckets[chain];
2483767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
2484767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
24851da177e4SLinus Torvalds 			int release;
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 			write_lock(&n->lock);
24881da177e4SLinus Torvalds 			release = cb(n);
24891da177e4SLinus Torvalds 			if (release) {
2490767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
2491767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
2492767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
24931da177e4SLinus Torvalds 				n->dead = 1;
24941da177e4SLinus Torvalds 			} else
24951da177e4SLinus Torvalds 				np = &n->next;
24961da177e4SLinus Torvalds 			write_unlock(&n->lock);
24974f494554SThomas Graf 			if (release)
24984f494554SThomas Graf 				neigh_cleanup_and_release(n);
24991da177e4SLinus Torvalds 		}
25001da177e4SLinus Torvalds 	}
2501ecbb4169SAlexey Kuznetsov }
25021da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release);
25031da177e4SLinus Torvalds 
2504b79bda3dSEric W. Biederman int neigh_xmit(int index, struct net_device *dev,
25054fd3d7d9SEric W. Biederman 	       const void *addr, struct sk_buff *skb)
25064fd3d7d9SEric W. Biederman {
2507b79bda3dSEric W. Biederman 	int err = -EAFNOSUPPORT;
2508b79bda3dSEric W. Biederman 	if (likely(index < NEIGH_NR_TABLES)) {
25094fd3d7d9SEric W. Biederman 		struct neigh_table *tbl;
25104fd3d7d9SEric W. Biederman 		struct neighbour *neigh;
25114fd3d7d9SEric W. Biederman 
2512b79bda3dSEric W. Biederman 		tbl = neigh_tables[index];
25134fd3d7d9SEric W. Biederman 		if (!tbl)
25144fd3d7d9SEric W. Biederman 			goto out;
2515b560f03dSDavid Barroso 		rcu_read_lock_bh();
25164fd3d7d9SEric W. Biederman 		neigh = __neigh_lookup_noref(tbl, addr, dev);
25174fd3d7d9SEric W. Biederman 		if (!neigh)
25184fd3d7d9SEric W. Biederman 			neigh = __neigh_create(tbl, addr, dev, false);
25194fd3d7d9SEric W. Biederman 		err = PTR_ERR(neigh);
2520b560f03dSDavid Barroso 		if (IS_ERR(neigh)) {
2521b560f03dSDavid Barroso 			rcu_read_unlock_bh();
25224fd3d7d9SEric W. Biederman 			goto out_kfree_skb;
2523b560f03dSDavid Barroso 		}
25244fd3d7d9SEric W. Biederman 		err = neigh->output(neigh, skb);
2525b560f03dSDavid Barroso 		rcu_read_unlock_bh();
25264fd3d7d9SEric W. Biederman 	}
2527b79bda3dSEric W. Biederman 	else if (index == NEIGH_LINK_TABLE) {
2528b79bda3dSEric W. Biederman 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2529b79bda3dSEric W. Biederman 				      addr, NULL, skb->len);
2530b79bda3dSEric W. Biederman 		if (err < 0)
2531b79bda3dSEric W. Biederman 			goto out_kfree_skb;
2532b79bda3dSEric W. Biederman 		err = dev_queue_xmit(skb);
2533b79bda3dSEric W. Biederman 	}
25344fd3d7d9SEric W. Biederman out:
25354fd3d7d9SEric W. Biederman 	return err;
25364fd3d7d9SEric W. Biederman out_kfree_skb:
25374fd3d7d9SEric W. Biederman 	kfree_skb(skb);
25384fd3d7d9SEric W. Biederman 	goto out;
25394fd3d7d9SEric W. Biederman }
25404fd3d7d9SEric W. Biederman EXPORT_SYMBOL(neigh_xmit);
25414fd3d7d9SEric W. Biederman 
25421da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq)
25451da177e4SLinus Torvalds {
25461da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25471218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2548d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
25491da177e4SLinus Torvalds 	struct neighbour *n = NULL;
25501da177e4SLinus Torvalds 	int bucket = state->bucket;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2553cd089336SDavid S. Miller 	for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
2554767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[bucket]);
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds 		while (n) {
2557878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2558426b5303SEric W. Biederman 				goto next;
25591da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
25601da177e4SLinus Torvalds 				loff_t fakep = 0;
25611da177e4SLinus Torvalds 				void *v;
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 				v = state->neigh_sub_iter(state, n, &fakep);
25641da177e4SLinus Torvalds 				if (!v)
25651da177e4SLinus Torvalds 					goto next;
25661da177e4SLinus Torvalds 			}
25671da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
25681da177e4SLinus Torvalds 				break;
25691da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
25701da177e4SLinus Torvalds 				break;
25711da177e4SLinus Torvalds next:
2572767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
25731da177e4SLinus Torvalds 		}
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 		if (n)
25761da177e4SLinus Torvalds 			break;
25771da177e4SLinus Torvalds 	}
25781da177e4SLinus Torvalds 	state->bucket = bucket;
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	return n;
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq,
25841da177e4SLinus Torvalds 					struct neighbour *n,
25851da177e4SLinus Torvalds 					loff_t *pos)
25861da177e4SLinus Torvalds {
25871da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25881218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2589d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	if (state->neigh_sub_iter) {
25921da177e4SLinus Torvalds 		void *v = state->neigh_sub_iter(state, n, pos);
25931da177e4SLinus Torvalds 		if (v)
25941da177e4SLinus Torvalds 			return n;
25951da177e4SLinus Torvalds 	}
2596767e97e1SEric Dumazet 	n = rcu_dereference_bh(n->next);
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	while (1) {
25991da177e4SLinus Torvalds 		while (n) {
2600878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2601426b5303SEric W. Biederman 				goto next;
26021da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
26031da177e4SLinus Torvalds 				void *v = state->neigh_sub_iter(state, n, pos);
26041da177e4SLinus Torvalds 				if (v)
26051da177e4SLinus Torvalds 					return n;
26061da177e4SLinus Torvalds 				goto next;
26071da177e4SLinus Torvalds 			}
26081da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
26091da177e4SLinus Torvalds 				break;
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
26121da177e4SLinus Torvalds 				break;
26131da177e4SLinus Torvalds next:
2614767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
26151da177e4SLinus Torvalds 		}
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 		if (n)
26181da177e4SLinus Torvalds 			break;
26191da177e4SLinus Torvalds 
2620cd089336SDavid S. Miller 		if (++state->bucket >= (1 << nht->hash_shift))
26211da177e4SLinus Torvalds 			break;
26221da177e4SLinus Torvalds 
2623767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
26241da177e4SLinus Torvalds 	}
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	if (n && pos)
26271da177e4SLinus Torvalds 		--(*pos);
26281da177e4SLinus Torvalds 	return n;
26291da177e4SLinus Torvalds }
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
26321da177e4SLinus Torvalds {
26331da177e4SLinus Torvalds 	struct neighbour *n = neigh_get_first(seq);
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds 	if (n) {
2636745e2031SChris Larson 		--(*pos);
26371da177e4SLinus Torvalds 		while (*pos) {
26381da177e4SLinus Torvalds 			n = neigh_get_next(seq, n, pos);
26391da177e4SLinus Torvalds 			if (!n)
26401da177e4SLinus Torvalds 				break;
26411da177e4SLinus Torvalds 		}
26421da177e4SLinus Torvalds 	}
26431da177e4SLinus Torvalds 	return *pos ? NULL : n;
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
26471da177e4SLinus Torvalds {
26481da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26491218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
26501da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
26511da177e4SLinus Torvalds 	struct pneigh_entry *pn = NULL;
26521da177e4SLinus Torvalds 	int bucket = state->bucket;
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
26551da177e4SLinus Torvalds 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
26561da177e4SLinus Torvalds 		pn = tbl->phash_buckets[bucket];
2657878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2658426b5303SEric W. Biederman 			pn = pn->next;
26591da177e4SLinus Torvalds 		if (pn)
26601da177e4SLinus Torvalds 			break;
26611da177e4SLinus Torvalds 	}
26621da177e4SLinus Torvalds 	state->bucket = bucket;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 	return pn;
26651da177e4SLinus Torvalds }
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
26681da177e4SLinus Torvalds 					    struct pneigh_entry *pn,
26691da177e4SLinus Torvalds 					    loff_t *pos)
26701da177e4SLinus Torvalds {
26711da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26721218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
26731da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
26741da177e4SLinus Torvalds 
2675df07a94cSJorge Boncompte [DTI2] 	do {
26761da177e4SLinus Torvalds 		pn = pn->next;
2677df07a94cSJorge Boncompte [DTI2] 	} while (pn && !net_eq(pneigh_net(pn), net));
2678df07a94cSJorge Boncompte [DTI2] 
26791da177e4SLinus Torvalds 	while (!pn) {
26801da177e4SLinus Torvalds 		if (++state->bucket > PNEIGH_HASHMASK)
26811da177e4SLinus Torvalds 			break;
26821da177e4SLinus Torvalds 		pn = tbl->phash_buckets[state->bucket];
2683878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2684426b5303SEric W. Biederman 			pn = pn->next;
26851da177e4SLinus Torvalds 		if (pn)
26861da177e4SLinus Torvalds 			break;
26871da177e4SLinus Torvalds 	}
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 	if (pn && pos)
26901da177e4SLinus Torvalds 		--(*pos);
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	return pn;
26931da177e4SLinus Torvalds }
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	struct pneigh_entry *pn = pneigh_get_first(seq);
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds 	if (pn) {
2700745e2031SChris Larson 		--(*pos);
27011da177e4SLinus Torvalds 		while (*pos) {
27021da177e4SLinus Torvalds 			pn = pneigh_get_next(seq, pn, pos);
27031da177e4SLinus Torvalds 			if (!pn)
27041da177e4SLinus Torvalds 				break;
27051da177e4SLinus Torvalds 		}
27061da177e4SLinus Torvalds 	}
27071da177e4SLinus Torvalds 	return *pos ? NULL : pn;
27081da177e4SLinus Torvalds }
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
27111da177e4SLinus Torvalds {
27121da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
27131da177e4SLinus Torvalds 	void *rc;
2714745e2031SChris Larson 	loff_t idxpos = *pos;
27151da177e4SLinus Torvalds 
2716745e2031SChris Larson 	rc = neigh_get_idx(seq, &idxpos);
27171da177e4SLinus Torvalds 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2718745e2031SChris Larson 		rc = pneigh_get_idx(seq, &idxpos);
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 	return rc;
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2724d6bf7817SEric Dumazet 	__acquires(rcu_bh)
27251da177e4SLinus Torvalds {
27261da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 	state->tbl = tbl;
27291da177e4SLinus Torvalds 	state->bucket = 0;
27301da177e4SLinus Torvalds 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
27311da177e4SLinus Torvalds 
2732d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2733d6bf7817SEric Dumazet 	state->nht = rcu_dereference_bh(tbl->nht);
2734767e97e1SEric Dumazet 
2735745e2031SChris Larson 	return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
27361da177e4SLinus Torvalds }
27371da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start);
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
27401da177e4SLinus Torvalds {
27411da177e4SLinus Torvalds 	struct neigh_seq_state *state;
27421da177e4SLinus Torvalds 	void *rc;
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2745bff69732SChris Larson 		rc = neigh_get_first(seq);
27461da177e4SLinus Torvalds 		goto out;
27471da177e4SLinus Torvalds 	}
27481da177e4SLinus Torvalds 
27491da177e4SLinus Torvalds 	state = seq->private;
27501da177e4SLinus Torvalds 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
27511da177e4SLinus Torvalds 		rc = neigh_get_next(seq, v, NULL);
27521da177e4SLinus Torvalds 		if (rc)
27531da177e4SLinus Torvalds 			goto out;
27541da177e4SLinus Torvalds 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
27551da177e4SLinus Torvalds 			rc = pneigh_get_first(seq);
27561da177e4SLinus Torvalds 	} else {
27571da177e4SLinus Torvalds 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
27581da177e4SLinus Torvalds 		rc = pneigh_get_next(seq, v, NULL);
27591da177e4SLinus Torvalds 	}
27601da177e4SLinus Torvalds out:
27611da177e4SLinus Torvalds 	++(*pos);
27621da177e4SLinus Torvalds 	return rc;
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next);
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v)
2767d6bf7817SEric Dumazet 	__releases(rcu_bh)
27681da177e4SLinus Torvalds {
2769d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop);
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds /* statistics via seq_file */
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
27761da177e4SLinus Torvalds {
277781c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27781da177e4SLinus Torvalds 	int cpu;
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 	if (*pos == 0)
27811da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
27821da177e4SLinus Torvalds 
27830f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
27841da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
27851da177e4SLinus Torvalds 			continue;
27861da177e4SLinus Torvalds 		*pos = cpu+1;
27871da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
27881da177e4SLinus Torvalds 	}
27891da177e4SLinus Torvalds 	return NULL;
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
27931da177e4SLinus Torvalds {
279481c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27951da177e4SLinus Torvalds 	int cpu;
27961da177e4SLinus Torvalds 
27970f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
27981da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
27991da177e4SLinus Torvalds 			continue;
28001da177e4SLinus Torvalds 		*pos = cpu+1;
28011da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
28021da177e4SLinus Torvalds 	}
28031da177e4SLinus Torvalds 	return NULL;
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
28071da177e4SLinus Torvalds {
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v)
28121da177e4SLinus Torvalds {
281381c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
28141da177e4SLinus Torvalds 	struct neigh_statistics *st = v;
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2817fb811395SRick Jones 		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 table_fulls\n");
28181da177e4SLinus Torvalds 		return 0;
28191da177e4SLinus Torvalds 	}
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
2822fb811395SRick Jones 			"%08lx %08lx  %08lx %08lx %08lx %08lx\n",
28231da177e4SLinus Torvalds 		   atomic_read(&tbl->entries),
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 		   st->allocs,
28261da177e4SLinus Torvalds 		   st->destroys,
28271da177e4SLinus Torvalds 		   st->hash_grows,
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds 		   st->lookups,
28301da177e4SLinus Torvalds 		   st->hits,
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 		   st->res_failed,
28331da177e4SLinus Torvalds 
28341da177e4SLinus Torvalds 		   st->rcv_probes_mcast,
28351da177e4SLinus Torvalds 		   st->rcv_probes_ucast,
28361da177e4SLinus Torvalds 
28371da177e4SLinus Torvalds 		   st->periodic_gc_runs,
28389a6d276eSNeil Horman 		   st->forced_gc_runs,
2839fb811395SRick Jones 		   st->unres_discards,
2840fb811395SRick Jones 		   st->table_fulls
28411da177e4SLinus Torvalds 		   );
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 	return 0;
28441da177e4SLinus Torvalds }
28451da177e4SLinus Torvalds 
2846f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = {
28471da177e4SLinus Torvalds 	.start	= neigh_stat_seq_start,
28481da177e4SLinus Torvalds 	.next	= neigh_stat_seq_next,
28491da177e4SLinus Torvalds 	.stop	= neigh_stat_seq_stop,
28501da177e4SLinus Torvalds 	.show	= neigh_stat_seq_show,
28511da177e4SLinus Torvalds };
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file)
28541da177e4SLinus Torvalds {
28551da177e4SLinus Torvalds 	int ret = seq_open(file, &neigh_stat_seq_ops);
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	if (!ret) {
28581da177e4SLinus Torvalds 		struct seq_file *sf = file->private_data;
2859d9dda78bSAl Viro 		sf->private = PDE_DATA(inode);
28601da177e4SLinus Torvalds 	}
28611da177e4SLinus Torvalds 	return ret;
28621da177e4SLinus Torvalds };
28631da177e4SLinus Torvalds 
28649a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = {
28651da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28661da177e4SLinus Torvalds 	.open 	 = neigh_stat_seq_open,
28671da177e4SLinus Torvalds 	.read	 = seq_read,
28681da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
28691da177e4SLinus Torvalds 	.release = seq_release,
28701da177e4SLinus Torvalds };
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
28731da177e4SLinus Torvalds 
2874339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void)
2875339bf98fSThomas Graf {
2876339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ndmsg))
2877339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2878339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2879339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct nda_cacheinfo))
2880339bf98fSThomas Graf 	       + nla_total_size(4); /* NDA_PROBES */
2881339bf98fSThomas Graf }
2882339bf98fSThomas Graf 
28837b8f7a40SRoopa Prabhu static void __neigh_notify(struct neighbour *n, int type, int flags,
28847b8f7a40SRoopa Prabhu 			   u32 pid)
28851da177e4SLinus Torvalds {
2886c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(n->dev);
28878b8aec50SThomas Graf 	struct sk_buff *skb;
2888b8673311SThomas Graf 	int err = -ENOBUFS;
28891da177e4SLinus Torvalds 
2890339bf98fSThomas Graf 	skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
28918b8aec50SThomas Graf 	if (skb == NULL)
2892b8673311SThomas Graf 		goto errout;
28931da177e4SLinus Torvalds 
28947b8f7a40SRoopa Prabhu 	err = neigh_fill_info(skb, n, pid, 0, type, flags);
289526932566SPatrick McHardy 	if (err < 0) {
289626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
289726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
289826932566SPatrick McHardy 		kfree_skb(skb);
289926932566SPatrick McHardy 		goto errout;
290026932566SPatrick McHardy 	}
29011ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
29021ce85fe4SPablo Neira Ayuso 	return;
2903b8673311SThomas Graf errout:
2904b8673311SThomas Graf 	if (err < 0)
2905426b5303SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2906b8673311SThomas Graf }
2907b8673311SThomas Graf 
2908b8673311SThomas Graf void neigh_app_ns(struct neighbour *n)
2909b8673311SThomas Graf {
29107b8f7a40SRoopa Prabhu 	__neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
29118b8aec50SThomas Graf }
29120a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns);
29131da177e4SLinus Torvalds 
29141da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
2915b93196dcSCong Wang static int zero;
2916555445cdSFrancesco Fusco static int int_max = INT_MAX;
2917b93196dcSCong Wang static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
29181da177e4SLinus Torvalds 
2919fe2c6338SJoe Perches static int proc_unres_qlen(struct ctl_table *ctl, int write,
2920fe2c6338SJoe Perches 			   void __user *buffer, size_t *lenp, loff_t *ppos)
29218b5c171bSEric Dumazet {
29228b5c171bSEric Dumazet 	int size, ret;
2923fe2c6338SJoe Perches 	struct ctl_table tmp = *ctl;
29248b5c171bSEric Dumazet 
2925ce46cc64SShan Wei 	tmp.extra1 = &zero;
2926ce46cc64SShan Wei 	tmp.extra2 = &unres_qlen_max;
29278b5c171bSEric Dumazet 	tmp.data = &size;
2928ce46cc64SShan Wei 
2929ce46cc64SShan Wei 	size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
2930ce46cc64SShan Wei 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2931ce46cc64SShan Wei 
29328b5c171bSEric Dumazet 	if (write && !ret)
29338b5c171bSEric Dumazet 		*(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
29348b5c171bSEric Dumazet 	return ret;
29358b5c171bSEric Dumazet }
29368b5c171bSEric Dumazet 
29371d4c8c29SJiri Pirko static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
29381d4c8c29SJiri Pirko 						   int family)
29391d4c8c29SJiri Pirko {
2940bba24896SJiri Pirko 	switch (family) {
2941bba24896SJiri Pirko 	case AF_INET:
29421d4c8c29SJiri Pirko 		return __in_dev_arp_parms_get_rcu(dev);
2943bba24896SJiri Pirko 	case AF_INET6:
2944bba24896SJiri Pirko 		return __in6_dev_nd_parms_get_rcu(dev);
2945bba24896SJiri Pirko 	}
29461d4c8c29SJiri Pirko 	return NULL;
29471d4c8c29SJiri Pirko }
29481d4c8c29SJiri Pirko 
29491d4c8c29SJiri Pirko static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
29501d4c8c29SJiri Pirko 				  int index)
29511d4c8c29SJiri Pirko {
29521d4c8c29SJiri Pirko 	struct net_device *dev;
29531d4c8c29SJiri Pirko 	int family = neigh_parms_family(p);
29541d4c8c29SJiri Pirko 
29551d4c8c29SJiri Pirko 	rcu_read_lock();
29561d4c8c29SJiri Pirko 	for_each_netdev_rcu(net, dev) {
29571d4c8c29SJiri Pirko 		struct neigh_parms *dst_p =
29581d4c8c29SJiri Pirko 				neigh_get_dev_parms_rcu(dev, family);
29591d4c8c29SJiri Pirko 
29601d4c8c29SJiri Pirko 		if (dst_p && !test_bit(index, dst_p->data_state))
29611d4c8c29SJiri Pirko 			dst_p->data[index] = p->data[index];
29621d4c8c29SJiri Pirko 	}
29631d4c8c29SJiri Pirko 	rcu_read_unlock();
29641d4c8c29SJiri Pirko }
29651d4c8c29SJiri Pirko 
29661d4c8c29SJiri Pirko static void neigh_proc_update(struct ctl_table *ctl, int write)
29671d4c8c29SJiri Pirko {
29681d4c8c29SJiri Pirko 	struct net_device *dev = ctl->extra1;
29691d4c8c29SJiri Pirko 	struct neigh_parms *p = ctl->extra2;
297077d47afbSJiri Pirko 	struct net *net = neigh_parms_net(p);
29711d4c8c29SJiri Pirko 	int index = (int *) ctl->data - p->data;
29721d4c8c29SJiri Pirko 
29731d4c8c29SJiri Pirko 	if (!write)
29741d4c8c29SJiri Pirko 		return;
29751d4c8c29SJiri Pirko 
29761d4c8c29SJiri Pirko 	set_bit(index, p->data_state);
29777627ae60SMarcus Huewe 	if (index == NEIGH_VAR_DELAY_PROBE_TIME)
29782a4501aeSIdo Schimmel 		call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
29791d4c8c29SJiri Pirko 	if (!dev) /* NULL dev means this is default value */
29801d4c8c29SJiri Pirko 		neigh_copy_dflt_parms(net, p, index);
29811d4c8c29SJiri Pirko }
29821d4c8c29SJiri Pirko 
29831f9248e5SJiri Pirko static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
29841f9248e5SJiri Pirko 					   void __user *buffer,
29851f9248e5SJiri Pirko 					   size_t *lenp, loff_t *ppos)
29861f9248e5SJiri Pirko {
29871f9248e5SJiri Pirko 	struct ctl_table tmp = *ctl;
29881d4c8c29SJiri Pirko 	int ret;
29891f9248e5SJiri Pirko 
29901f9248e5SJiri Pirko 	tmp.extra1 = &zero;
29911f9248e5SJiri Pirko 	tmp.extra2 = &int_max;
29921f9248e5SJiri Pirko 
29931d4c8c29SJiri Pirko 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
29941d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29951d4c8c29SJiri Pirko 	return ret;
29961f9248e5SJiri Pirko }
29971f9248e5SJiri Pirko 
2998cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write,
2999cb5b09c1SJiri Pirko 			void __user *buffer, size_t *lenp, loff_t *ppos)
3000cb5b09c1SJiri Pirko {
30011d4c8c29SJiri Pirko 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
30021d4c8c29SJiri Pirko 
30031d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
30041d4c8c29SJiri Pirko 	return ret;
3005cb5b09c1SJiri Pirko }
3006cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec);
3007cb5b09c1SJiri Pirko 
3008cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3009cb5b09c1SJiri Pirko 				void __user *buffer,
3010cb5b09c1SJiri Pirko 				size_t *lenp, loff_t *ppos)
3011cb5b09c1SJiri Pirko {
30121d4c8c29SJiri Pirko 	int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
30131d4c8c29SJiri Pirko 
30141d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
30151d4c8c29SJiri Pirko 	return ret;
3016cb5b09c1SJiri Pirko }
3017cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3018cb5b09c1SJiri Pirko 
3019cb5b09c1SJiri Pirko static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3020cb5b09c1SJiri Pirko 					      void __user *buffer,
3021cb5b09c1SJiri Pirko 					      size_t *lenp, loff_t *ppos)
3022cb5b09c1SJiri Pirko {
30231d4c8c29SJiri Pirko 	int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
30241d4c8c29SJiri Pirko 
30251d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
30261d4c8c29SJiri Pirko 	return ret;
3027cb5b09c1SJiri Pirko }
3028cb5b09c1SJiri Pirko 
3029cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3030cb5b09c1SJiri Pirko 				   void __user *buffer,
3031cb5b09c1SJiri Pirko 				   size_t *lenp, loff_t *ppos)
3032cb5b09c1SJiri Pirko {
30331d4c8c29SJiri Pirko 	int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
30341d4c8c29SJiri Pirko 
30351d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
30361d4c8c29SJiri Pirko 	return ret;
3037cb5b09c1SJiri Pirko }
3038cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3039cb5b09c1SJiri Pirko 
3040cb5b09c1SJiri Pirko static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3041cb5b09c1SJiri Pirko 					  void __user *buffer,
3042cb5b09c1SJiri Pirko 					  size_t *lenp, loff_t *ppos)
3043cb5b09c1SJiri Pirko {
30441d4c8c29SJiri Pirko 	int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
30451d4c8c29SJiri Pirko 
30461d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
30471d4c8c29SJiri Pirko 	return ret;
3048cb5b09c1SJiri Pirko }
3049cb5b09c1SJiri Pirko 
30504bf6980dSJean-Francois Remy static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
30514bf6980dSJean-Francois Remy 					  void __user *buffer,
30524bf6980dSJean-Francois Remy 					  size_t *lenp, loff_t *ppos)
30534bf6980dSJean-Francois Remy {
30544bf6980dSJean-Francois Remy 	struct neigh_parms *p = ctl->extra2;
30554bf6980dSJean-Francois Remy 	int ret;
30564bf6980dSJean-Francois Remy 
30574bf6980dSJean-Francois Remy 	if (strcmp(ctl->procname, "base_reachable_time") == 0)
30584bf6980dSJean-Francois Remy 		ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
30594bf6980dSJean-Francois Remy 	else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
30604bf6980dSJean-Francois Remy 		ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
30614bf6980dSJean-Francois Remy 	else
30624bf6980dSJean-Francois Remy 		ret = -1;
30634bf6980dSJean-Francois Remy 
30644bf6980dSJean-Francois Remy 	if (write && ret == 0) {
30654bf6980dSJean-Francois Remy 		/* update reachable_time as well, otherwise, the change will
30664bf6980dSJean-Francois Remy 		 * only be effective after the next time neigh_periodic_work
30674bf6980dSJean-Francois Remy 		 * decides to recompute it
30684bf6980dSJean-Francois Remy 		 */
30694bf6980dSJean-Francois Remy 		p->reachable_time =
30704bf6980dSJean-Francois Remy 			neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
30714bf6980dSJean-Francois Remy 	}
30724bf6980dSJean-Francois Remy 	return ret;
30734bf6980dSJean-Francois Remy }
30744bf6980dSJean-Francois Remy 
30751f9248e5SJiri Pirko #define NEIGH_PARMS_DATA_OFFSET(index)	\
30761f9248e5SJiri Pirko 	(&((struct neigh_parms *) 0)->data[index])
30771f9248e5SJiri Pirko 
30781f9248e5SJiri Pirko #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
30791f9248e5SJiri Pirko 	[NEIGH_VAR_ ## attr] = { \
30801f9248e5SJiri Pirko 		.procname	= name, \
30811f9248e5SJiri Pirko 		.data		= NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
30821f9248e5SJiri Pirko 		.maxlen		= sizeof(int), \
30831f9248e5SJiri Pirko 		.mode		= mval, \
30841f9248e5SJiri Pirko 		.proc_handler	= proc, \
30851f9248e5SJiri Pirko 	}
30861f9248e5SJiri Pirko 
30871f9248e5SJiri Pirko #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
30881f9248e5SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
30891f9248e5SJiri Pirko 
30901f9248e5SJiri Pirko #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3091cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
30921f9248e5SJiri Pirko 
30931f9248e5SJiri Pirko #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3094cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
30951f9248e5SJiri Pirko 
30961f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
3097cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
30981f9248e5SJiri Pirko 
30991f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3100cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
31011f9248e5SJiri Pirko 
31021f9248e5SJiri Pirko #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3103cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
310454716e3bSEric W. Biederman 
31051da177e4SLinus Torvalds static struct neigh_sysctl_table {
31061da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
31078b5c171bSEric Dumazet 	struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3108ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = {
31091da177e4SLinus Torvalds 	.neigh_vars = {
31101f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
31111f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
31121f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
31138da86466SYOSHIFUJI Hideaki/吉藤英明 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
31141f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
31151f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
31161f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
31171f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
31181f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
31191f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
31201f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
31211f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
31221f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
31231f9248e5SJiri Pirko 		NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
31241f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
31251f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
31268b5c171bSEric Dumazet 		[NEIGH_VAR_GC_INTERVAL] = {
31271da177e4SLinus Torvalds 			.procname	= "gc_interval",
31281da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
31291da177e4SLinus Torvalds 			.mode		= 0644,
31306d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
31311da177e4SLinus Torvalds 		},
31328b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH1] = {
31331da177e4SLinus Torvalds 			.procname	= "gc_thresh1",
31341da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
31351da177e4SLinus Torvalds 			.mode		= 0644,
3136555445cdSFrancesco Fusco 			.extra1 	= &zero,
3137555445cdSFrancesco Fusco 			.extra2		= &int_max,
3138555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
31391da177e4SLinus Torvalds 		},
31408b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH2] = {
31411da177e4SLinus Torvalds 			.procname	= "gc_thresh2",
31421da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
31431da177e4SLinus Torvalds 			.mode		= 0644,
3144555445cdSFrancesco Fusco 			.extra1 	= &zero,
3145555445cdSFrancesco Fusco 			.extra2		= &int_max,
3146555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
31471da177e4SLinus Torvalds 		},
31488b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH3] = {
31491da177e4SLinus Torvalds 			.procname	= "gc_thresh3",
31501da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
31511da177e4SLinus Torvalds 			.mode		= 0644,
3152555445cdSFrancesco Fusco 			.extra1 	= &zero,
3153555445cdSFrancesco Fusco 			.extra2		= &int_max,
3154555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
31551da177e4SLinus Torvalds 		},
3156c3bac5a7SPavel Emelyanov 		{},
31571da177e4SLinus Torvalds 	},
31581da177e4SLinus Torvalds };
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
316173af614aSJiri Pirko 			  proc_handler *handler)
31621da177e4SLinus Torvalds {
31631f9248e5SJiri Pirko 	int i;
31643c607bbbSPavel Emelyanov 	struct neigh_sysctl_table *t;
31651f9248e5SJiri Pirko 	const char *dev_name_source;
31668f40a1f9SEric W. Biederman 	char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
316773af614aSJiri Pirko 	char *p_name;
31681da177e4SLinus Torvalds 
31693c607bbbSPavel Emelyanov 	t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
31701da177e4SLinus Torvalds 	if (!t)
31713c607bbbSPavel Emelyanov 		goto err;
31723c607bbbSPavel Emelyanov 
3173b194c1f1SJiri Pirko 	for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
31741f9248e5SJiri Pirko 		t->neigh_vars[i].data += (long) p;
3175cb5b09c1SJiri Pirko 		t->neigh_vars[i].extra1 = dev;
31761d4c8c29SJiri Pirko 		t->neigh_vars[i].extra2 = p;
3177cb5b09c1SJiri Pirko 	}
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	if (dev) {
31801da177e4SLinus Torvalds 		dev_name_source = dev->name;
3181d12af679SEric W. Biederman 		/* Terminate the table early */
31828b5c171bSEric Dumazet 		memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
31838b5c171bSEric Dumazet 		       sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
31841da177e4SLinus Torvalds 	} else {
31859ecf07a1SMathias Krause 		struct neigh_table *tbl = p->tbl;
31868f40a1f9SEric W. Biederman 		dev_name_source = "default";
31879ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
31889ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
31899ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
31909ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
31911da177e4SLinus Torvalds 	}
31921da177e4SLinus Torvalds 
3193f8572d8fSEric W. Biederman 	if (handler) {
31941da177e4SLinus Torvalds 		/* RetransTime */
31958b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
31961da177e4SLinus Torvalds 		/* ReachableTime */
31978b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
31981da177e4SLinus Torvalds 		/* RetransTime (in milliseconds)*/
31998b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
32001da177e4SLinus Torvalds 		/* ReachableTime (in milliseconds) */
32018b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
32024bf6980dSJean-Francois Remy 	} else {
32034bf6980dSJean-Francois Remy 		/* Those handlers will update p->reachable_time after
32044bf6980dSJean-Francois Remy 		 * base_reachable_time(_ms) is set to ensure the new timer starts being
32054bf6980dSJean-Francois Remy 		 * applied after the next neighbour update instead of waiting for
32064bf6980dSJean-Francois Remy 		 * neigh_periodic_work to update its value (can be multiple minutes)
32074bf6980dSJean-Francois Remy 		 * So any handler that replaces them should do this as well
32084bf6980dSJean-Francois Remy 		 */
32094bf6980dSJean-Francois Remy 		/* ReachableTime */
32104bf6980dSJean-Francois Remy 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
32114bf6980dSJean-Francois Remy 			neigh_proc_base_reachable_time;
32124bf6980dSJean-Francois Remy 		/* ReachableTime (in milliseconds) */
32134bf6980dSJean-Francois Remy 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
32144bf6980dSJean-Francois Remy 			neigh_proc_base_reachable_time;
32151da177e4SLinus Torvalds 	}
32161da177e4SLinus Torvalds 
3217464dc801SEric W. Biederman 	/* Don't export sysctls to unprivileged users */
3218464dc801SEric W. Biederman 	if (neigh_parms_net(p)->user_ns != &init_user_ns)
3219464dc801SEric W. Biederman 		t->neigh_vars[0].procname = NULL;
3220464dc801SEric W. Biederman 
322173af614aSJiri Pirko 	switch (neigh_parms_family(p)) {
322273af614aSJiri Pirko 	case AF_INET:
322373af614aSJiri Pirko 	      p_name = "ipv4";
322473af614aSJiri Pirko 	      break;
322573af614aSJiri Pirko 	case AF_INET6:
322673af614aSJiri Pirko 	      p_name = "ipv6";
322773af614aSJiri Pirko 	      break;
322873af614aSJiri Pirko 	default:
322973af614aSJiri Pirko 	      BUG();
323073af614aSJiri Pirko 	}
323173af614aSJiri Pirko 
32328f40a1f9SEric W. Biederman 	snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
32338f40a1f9SEric W. Biederman 		p_name, dev_name_source);
32344ab438fcSDenis V. Lunev 	t->sysctl_header =
32358f40a1f9SEric W. Biederman 		register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
32363c607bbbSPavel Emelyanov 	if (!t->sysctl_header)
32378f40a1f9SEric W. Biederman 		goto free;
32383c607bbbSPavel Emelyanov 
32391da177e4SLinus Torvalds 	p->sysctl_table = t;
32401da177e4SLinus Torvalds 	return 0;
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds free:
32431da177e4SLinus Torvalds 	kfree(t);
32443c607bbbSPavel Emelyanov err:
32453c607bbbSPavel Emelyanov 	return -ENOBUFS;
32461da177e4SLinus Torvalds }
32470a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register);
32481da177e4SLinus Torvalds 
32491da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p)
32501da177e4SLinus Torvalds {
32511da177e4SLinus Torvalds 	if (p->sysctl_table) {
32521da177e4SLinus Torvalds 		struct neigh_sysctl_table *t = p->sysctl_table;
32531da177e4SLinus Torvalds 		p->sysctl_table = NULL;
32545dd3df10SEric W. Biederman 		unregister_net_sysctl_table(t->sysctl_header);
32551da177e4SLinus Torvalds 		kfree(t);
32561da177e4SLinus Torvalds 	}
32571da177e4SLinus Torvalds }
32580a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister);
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds #endif	/* CONFIG_SYSCTL */
32611da177e4SLinus Torvalds 
3262c8822a4eSThomas Graf static int __init neigh_init(void)
3263c8822a4eSThomas Graf {
3264b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3265b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
3266b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, 0);
3267c8822a4eSThomas Graf 
3268c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3269b97bac64SFlorian Westphal 		      0);
3270b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
3271c8822a4eSThomas Graf 
3272c8822a4eSThomas Graf 	return 0;
3273c8822a4eSThomas Graf }
3274c8822a4eSThomas Graf 
3275c8822a4eSThomas Graf subsys_initcall(neigh_init);
3276c8822a4eSThomas Graf 
3277