xref: /openbmc/linux/net/core/neighbour.c (revision a9cd3439)
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);
5853b76cdfSWolfgang Bumiller static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
5953b76cdfSWolfgang Bumiller 				    struct net_device *dev);
601da177e4SLinus Torvalds 
6145fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS
6271a5053aSChristoph Hellwig static const struct seq_operations neigh_stat_seq_ops;
6345fc3b11SAmos Waterland #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds    Neighbour hash table buckets are protected with rwlock tbl->lock.
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds    - All the scans/updates to hash buckets MUST be made under this lock.
691da177e4SLinus Torvalds    - NOTHING clever should be made under this lock: no callbacks
701da177e4SLinus Torvalds      to protocol backends, no attempts to send something to network.
711da177e4SLinus Torvalds      It will result in deadlocks, if backend/driver wants to use neighbour
721da177e4SLinus Torvalds      cache.
731da177e4SLinus Torvalds    - If the entry requires some non-trivial actions, increase
741da177e4SLinus Torvalds      its reference count and release table lock.
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds    Neighbour entries are protected:
771da177e4SLinus Torvalds    - with reference count.
781da177e4SLinus Torvalds    - with rwlock neigh->lock
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds    Reference count prevents destruction.
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds    neigh->lock mainly serializes ll address data and its validity state.
831da177e4SLinus Torvalds    However, the same lock is used to protect another entry fields:
841da177e4SLinus Torvalds     - timer
851da177e4SLinus Torvalds     - resolution queue
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds    Again, nothing clever shall be made under neigh->lock,
881da177e4SLinus Torvalds    the most complicated procedure, which we allow is dev->hard_header.
891da177e4SLinus Torvalds    It is supposed, that dev->hard_header is simplistic and does
901da177e4SLinus Torvalds    not make callbacks to neighbour tables.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
938f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	kfree_skb(skb);
961da177e4SLinus Torvalds 	return -ENETDOWN;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
994f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh)
1004f494554SThomas Graf {
1014f494554SThomas Graf 	if (neigh->parms->neigh_cleanup)
1024f494554SThomas Graf 		neigh->parms->neigh_cleanup(neigh);
1034f494554SThomas Graf 
1047b8f7a40SRoopa Prabhu 	__neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
10553f800e3SIdo Schimmel 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
1064f494554SThomas Graf 	neigh_release(neigh);
1074f494554SThomas Graf }
1084f494554SThomas Graf 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * It is random distribution in the interval (1/2)*base...(3/2)*base.
1111da177e4SLinus Torvalds  * It corresponds to default IPv6 settings and is not overridable,
1121da177e4SLinus Torvalds  * because it is really reasonable choice.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base)
1161da177e4SLinus Torvalds {
11763862b5bSAruna-Hewapathirane 	return base ? (prandom_u32() % base) + (base >> 1) : 0;
1181da177e4SLinus Torvalds }
1190a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time);
1201da177e4SLinus Torvalds 
12158956317SDavid Ahern static void neigh_mark_dead(struct neighbour *n)
12258956317SDavid Ahern {
12358956317SDavid Ahern 	n->dead = 1;
12458956317SDavid Ahern 	if (!list_empty(&n->gc_list)) {
12558956317SDavid Ahern 		list_del_init(&n->gc_list);
12658956317SDavid Ahern 		atomic_dec(&n->tbl->gc_entries);
12758956317SDavid Ahern 	}
12858956317SDavid Ahern }
12958956317SDavid Ahern 
1309c29a2f5SDavid Ahern static void neigh_update_gc_list(struct neighbour *n)
13158956317SDavid Ahern {
132e997f8a2SDavid Ahern 	bool on_gc_list, exempt_from_gc;
13358956317SDavid Ahern 
1349c29a2f5SDavid Ahern 	write_lock_bh(&n->tbl->lock);
1359c29a2f5SDavid Ahern 	write_lock(&n->lock);
13658956317SDavid Ahern 
137e997f8a2SDavid Ahern 	/* remove from the gc list if new state is permanent or if neighbor
138e997f8a2SDavid Ahern 	 * is externally learned; otherwise entry should be on the gc list
13958956317SDavid Ahern 	 */
140e997f8a2SDavid Ahern 	exempt_from_gc = n->nud_state & NUD_PERMANENT ||
141e997f8a2SDavid Ahern 			 n->flags & NTF_EXT_LEARNED;
1429c29a2f5SDavid Ahern 	on_gc_list = !list_empty(&n->gc_list);
1438cc196d6SDavid Ahern 
144e997f8a2SDavid Ahern 	if (exempt_from_gc && on_gc_list) {
1459c29a2f5SDavid Ahern 		list_del_init(&n->gc_list);
14658956317SDavid Ahern 		atomic_dec(&n->tbl->gc_entries);
147e997f8a2SDavid Ahern 	} else if (!exempt_from_gc && !on_gc_list) {
14858956317SDavid Ahern 		/* add entries to the tail; cleaning removes from the front */
14958956317SDavid Ahern 		list_add_tail(&n->gc_list, &n->tbl->gc_list);
15058956317SDavid Ahern 		atomic_inc(&n->tbl->gc_entries);
15158956317SDavid Ahern 	}
1529c29a2f5SDavid Ahern 
1539c29a2f5SDavid Ahern 	write_unlock(&n->lock);
1549c29a2f5SDavid Ahern 	write_unlock_bh(&n->tbl->lock);
15558956317SDavid Ahern }
1561da177e4SLinus Torvalds 
157e997f8a2SDavid Ahern static bool neigh_update_ext_learned(struct neighbour *neigh, u32 flags,
158526f1b58SDavid Ahern 				     int *notify)
159526f1b58SDavid Ahern {
160e997f8a2SDavid Ahern 	bool rc = false;
161526f1b58SDavid Ahern 	u8 ndm_flags;
162526f1b58SDavid Ahern 
163526f1b58SDavid Ahern 	if (!(flags & NEIGH_UPDATE_F_ADMIN))
164e997f8a2SDavid Ahern 		return rc;
165526f1b58SDavid Ahern 
166526f1b58SDavid Ahern 	ndm_flags = (flags & NEIGH_UPDATE_F_EXT_LEARNED) ? NTF_EXT_LEARNED : 0;
167526f1b58SDavid Ahern 	if ((neigh->flags ^ ndm_flags) & NTF_EXT_LEARNED) {
168526f1b58SDavid Ahern 		if (ndm_flags & NTF_EXT_LEARNED)
169526f1b58SDavid Ahern 			neigh->flags |= NTF_EXT_LEARNED;
170526f1b58SDavid Ahern 		else
171526f1b58SDavid Ahern 			neigh->flags &= ~NTF_EXT_LEARNED;
172e997f8a2SDavid Ahern 		rc = true;
173526f1b58SDavid Ahern 		*notify = 1;
174526f1b58SDavid Ahern 	}
175e997f8a2SDavid Ahern 
176e997f8a2SDavid Ahern 	return rc;
177526f1b58SDavid Ahern }
178526f1b58SDavid Ahern 
1797e6f182bSDavid Ahern static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
1807e6f182bSDavid Ahern 		      struct neigh_table *tbl)
1815071034eSSowmini Varadhan {
1825071034eSSowmini Varadhan 	bool retval = false;
1835071034eSSowmini Varadhan 
1845071034eSSowmini Varadhan 	write_lock(&n->lock);
1857e6f182bSDavid Ahern 	if (refcount_read(&n->refcnt) == 1) {
1865071034eSSowmini Varadhan 		struct neighbour *neigh;
1875071034eSSowmini Varadhan 
1885071034eSSowmini Varadhan 		neigh = rcu_dereference_protected(n->next,
1895071034eSSowmini Varadhan 						  lockdep_is_held(&tbl->lock));
1905071034eSSowmini Varadhan 		rcu_assign_pointer(*np, neigh);
19158956317SDavid Ahern 		neigh_mark_dead(n);
1925071034eSSowmini Varadhan 		retval = true;
1935071034eSSowmini Varadhan 	}
1945071034eSSowmini Varadhan 	write_unlock(&n->lock);
1955071034eSSowmini Varadhan 	if (retval)
1965071034eSSowmini Varadhan 		neigh_cleanup_and_release(n);
1975071034eSSowmini Varadhan 	return retval;
1985071034eSSowmini Varadhan }
1995071034eSSowmini Varadhan 
2005071034eSSowmini Varadhan bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
2015071034eSSowmini Varadhan {
2025071034eSSowmini Varadhan 	struct neigh_hash_table *nht;
2035071034eSSowmini Varadhan 	void *pkey = ndel->primary_key;
2045071034eSSowmini Varadhan 	u32 hash_val;
2055071034eSSowmini Varadhan 	struct neighbour *n;
2065071034eSSowmini Varadhan 	struct neighbour __rcu **np;
2075071034eSSowmini Varadhan 
2085071034eSSowmini Varadhan 	nht = rcu_dereference_protected(tbl->nht,
2095071034eSSowmini Varadhan 					lockdep_is_held(&tbl->lock));
2105071034eSSowmini Varadhan 	hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
2115071034eSSowmini Varadhan 	hash_val = hash_val >> (32 - nht->hash_shift);
2125071034eSSowmini Varadhan 
2135071034eSSowmini Varadhan 	np = &nht->hash_buckets[hash_val];
2145071034eSSowmini Varadhan 	while ((n = rcu_dereference_protected(*np,
2155071034eSSowmini Varadhan 					      lockdep_is_held(&tbl->lock)))) {
2165071034eSSowmini Varadhan 		if (n == ndel)
2177e6f182bSDavid Ahern 			return neigh_del(n, np, tbl);
2185071034eSSowmini Varadhan 		np = &n->next;
2195071034eSSowmini Varadhan 	}
2205071034eSSowmini Varadhan 	return false;
2215071034eSSowmini Varadhan }
2225071034eSSowmini Varadhan 
2231da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl)
2241da177e4SLinus Torvalds {
22558956317SDavid Ahern 	int max_clean = atomic_read(&tbl->gc_entries) - tbl->gc_thresh2;
22658956317SDavid Ahern 	unsigned long tref = jiffies - 5 * HZ;
22758956317SDavid Ahern 	struct neighbour *n, *tmp;
2281da177e4SLinus Torvalds 	int shrunk = 0;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
2331da177e4SLinus Torvalds 
23458956317SDavid Ahern 	list_for_each_entry_safe(n, tmp, &tbl->gc_list, gc_list) {
23558956317SDavid Ahern 		if (refcount_read(&n->refcnt) == 1) {
23658956317SDavid Ahern 			bool remove = false;
23758956317SDavid Ahern 
23858956317SDavid Ahern 			write_lock(&n->lock);
239758a7f0bSDavid Ahern 			if ((n->nud_state == NUD_FAILED) ||
240e997f8a2SDavid Ahern 			    time_after(tref, n->updated))
24158956317SDavid Ahern 				remove = true;
24258956317SDavid Ahern 			write_unlock(&n->lock);
24358956317SDavid Ahern 
24458956317SDavid Ahern 			if (remove && neigh_remove_one(n, tbl))
24558956317SDavid Ahern 				shrunk++;
24658956317SDavid Ahern 			if (shrunk >= max_clean)
24758956317SDavid Ahern 				break;
2481da177e4SLinus Torvalds 		}
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	tbl->last_flush = jiffies;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	return shrunk;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
258a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when)
259a43d8994SPavel Emelyanov {
260a43d8994SPavel Emelyanov 	neigh_hold(n);
261a43d8994SPavel Emelyanov 	if (unlikely(mod_timer(&n->timer, when))) {
262a43d8994SPavel Emelyanov 		printk("NEIGH: BUG, double timer add, state is %x\n",
263a43d8994SPavel Emelyanov 		       n->nud_state);
264a43d8994SPavel Emelyanov 		dump_stack();
265a43d8994SPavel Emelyanov 	}
266a43d8994SPavel Emelyanov }
267a43d8994SPavel Emelyanov 
2681da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	if ((n->nud_state & NUD_IN_TIMER) &&
2711da177e4SLinus Torvalds 	    del_timer(&n->timer)) {
2721da177e4SLinus Torvalds 		neigh_release(n);
2731da177e4SLinus Torvalds 		return 1;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	return 0;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct sk_buff *skb;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL) {
2831da177e4SLinus Torvalds 		dev_put(skb->dev);
2841da177e4SLinus Torvalds 		kfree_skb(skb);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
288859bd2efSDavid Ahern static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
289859bd2efSDavid Ahern 			    bool skip_perm)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	int i;
292d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
2931da177e4SLinus Torvalds 
294d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
295d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
296d6bf7817SEric Dumazet 
297cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
298767e97e1SEric Dumazet 		struct neighbour *n;
299767e97e1SEric Dumazet 		struct neighbour __rcu **np = &nht->hash_buckets[i];
3001da177e4SLinus Torvalds 
301767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
302767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
3031da177e4SLinus Torvalds 			if (dev && n->dev != dev) {
3041da177e4SLinus Torvalds 				np = &n->next;
3051da177e4SLinus Torvalds 				continue;
3061da177e4SLinus Torvalds 			}
307859bd2efSDavid Ahern 			if (skip_perm && n->nud_state & NUD_PERMANENT) {
308859bd2efSDavid Ahern 				np = &n->next;
309859bd2efSDavid Ahern 				continue;
310859bd2efSDavid Ahern 			}
311767e97e1SEric Dumazet 			rcu_assign_pointer(*np,
312767e97e1SEric Dumazet 				   rcu_dereference_protected(n->next,
313767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
3141da177e4SLinus Torvalds 			write_lock(&n->lock);
3151da177e4SLinus Torvalds 			neigh_del_timer(n);
31658956317SDavid Ahern 			neigh_mark_dead(n);
3179f237430SReshetova, Elena 			if (refcount_read(&n->refcnt) != 1) {
3181da177e4SLinus Torvalds 				/* The most unpleasant situation.
3191da177e4SLinus Torvalds 				   We must destroy neighbour entry,
3201da177e4SLinus Torvalds 				   but someone still uses it.
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 				   The destroy will be delayed until
3231da177e4SLinus Torvalds 				   the last user releases us, but
3241da177e4SLinus Torvalds 				   we must kill timers etc. and move
3251da177e4SLinus Torvalds 				   it to safe state.
3261da177e4SLinus Torvalds 				 */
327c9ab4d85SEric Dumazet 				__skb_queue_purge(&n->arp_queue);
3288b5c171bSEric Dumazet 				n->arp_queue_len_bytes = 0;
3291da177e4SLinus Torvalds 				n->output = neigh_blackhole;
3301da177e4SLinus Torvalds 				if (n->nud_state & NUD_VALID)
3311da177e4SLinus Torvalds 					n->nud_state = NUD_NOARP;
3321da177e4SLinus Torvalds 				else
3331da177e4SLinus Torvalds 					n->nud_state = NUD_NONE;
334d5d427cdSJoe Perches 				neigh_dbg(2, "neigh %p is stray\n", n);
3351da177e4SLinus Torvalds 			}
3361da177e4SLinus Torvalds 			write_unlock(&n->lock);
3374f494554SThomas Graf 			neigh_cleanup_and_release(n);
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 	}
34049636bb1SHerbert Xu }
3411da177e4SLinus Torvalds 
34249636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
34349636bb1SHerbert Xu {
34449636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
345859bd2efSDavid Ahern 	neigh_flush_dev(tbl, dev, false);
34649636bb1SHerbert Xu 	write_unlock_bh(&tbl->lock);
34749636bb1SHerbert Xu }
3480a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr);
34949636bb1SHerbert Xu 
350859bd2efSDavid Ahern static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
351859bd2efSDavid Ahern 			  bool skip_perm)
35249636bb1SHerbert Xu {
35349636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
354859bd2efSDavid Ahern 	neigh_flush_dev(tbl, dev, skip_perm);
35553b76cdfSWolfgang Bumiller 	pneigh_ifdown_and_unlock(tbl, dev);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
3581da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
3591da177e4SLinus Torvalds 	return 0;
3601da177e4SLinus Torvalds }
361859bd2efSDavid Ahern 
362859bd2efSDavid Ahern int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
363859bd2efSDavid Ahern {
364859bd2efSDavid Ahern 	__neigh_ifdown(tbl, dev, true);
365859bd2efSDavid Ahern 	return 0;
366859bd2efSDavid Ahern }
367859bd2efSDavid Ahern EXPORT_SYMBOL(neigh_carrier_down);
368859bd2efSDavid Ahern 
369859bd2efSDavid Ahern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
370859bd2efSDavid Ahern {
371859bd2efSDavid Ahern 	__neigh_ifdown(tbl, dev, false);
372859bd2efSDavid Ahern 	return 0;
373859bd2efSDavid Ahern }
3740a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown);
3751da177e4SLinus Torvalds 
37658956317SDavid Ahern static struct neighbour *neigh_alloc(struct neigh_table *tbl,
37758956317SDavid Ahern 				     struct net_device *dev,
378e997f8a2SDavid Ahern 				     bool exempt_from_gc)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct neighbour *n = NULL;
3811da177e4SLinus Torvalds 	unsigned long now = jiffies;
3821da177e4SLinus Torvalds 	int entries;
3831da177e4SLinus Torvalds 
384e997f8a2SDavid Ahern 	if (exempt_from_gc)
38558956317SDavid Ahern 		goto do_alloc;
38658956317SDavid Ahern 
38758956317SDavid Ahern 	entries = atomic_inc_return(&tbl->gc_entries) - 1;
3881da177e4SLinus Torvalds 	if (entries >= tbl->gc_thresh3 ||
3891da177e4SLinus Torvalds 	    (entries >= tbl->gc_thresh2 &&
3901da177e4SLinus Torvalds 	     time_after(now, tbl->last_flush + 5 * HZ))) {
3911da177e4SLinus Torvalds 		if (!neigh_forced_gc(tbl) &&
392fb811395SRick Jones 		    entries >= tbl->gc_thresh3) {
393fb811395SRick Jones 			net_info_ratelimited("%s: neighbor table overflow!\n",
394fb811395SRick Jones 					     tbl->id);
395fb811395SRick Jones 			NEIGH_CACHE_STAT_INC(tbl, table_fulls);
3961da177e4SLinus Torvalds 			goto out_entries;
3971da177e4SLinus Torvalds 		}
398fb811395SRick Jones 	}
3991da177e4SLinus Torvalds 
40058956317SDavid Ahern do_alloc:
40108433effSYOSHIFUJI Hideaki / 吉藤英明 	n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
4021da177e4SLinus Torvalds 	if (!n)
4031da177e4SLinus Torvalds 		goto out_entries;
4041da177e4SLinus Torvalds 
405c9ab4d85SEric Dumazet 	__skb_queue_head_init(&n->arp_queue);
4061da177e4SLinus Torvalds 	rwlock_init(&n->lock);
4070ed8ddf4SEric Dumazet 	seqlock_init(&n->ha_lock);
4081da177e4SLinus Torvalds 	n->updated	  = n->used = now;
4091da177e4SLinus Torvalds 	n->nud_state	  = NUD_NONE;
4101da177e4SLinus Torvalds 	n->output	  = neigh_blackhole;
411f6b72b62SDavid S. Miller 	seqlock_init(&n->hh.hh_lock);
4121da177e4SLinus Torvalds 	n->parms	  = neigh_parms_clone(&tbl->parms);
413e99e88a9SKees Cook 	timer_setup(&n->timer, neigh_timer_handler, 0);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, allocs);
4161da177e4SLinus Torvalds 	n->tbl		  = tbl;
4179f237430SReshetova, Elena 	refcount_set(&n->refcnt, 1);
4181da177e4SLinus Torvalds 	n->dead		  = 1;
41958956317SDavid Ahern 	INIT_LIST_HEAD(&n->gc_list);
42058956317SDavid Ahern 
42158956317SDavid Ahern 	atomic_inc(&tbl->entries);
4221da177e4SLinus Torvalds out:
4231da177e4SLinus Torvalds 	return n;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds out_entries:
426e997f8a2SDavid Ahern 	if (!exempt_from_gc)
42758956317SDavid Ahern 		atomic_dec(&tbl->gc_entries);
4281da177e4SLinus Torvalds 	goto out;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4312c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x)
4322c2aba6cSDavid S. Miller {
433b3d0f789SJason A. Donenfeld 	*x = get_random_u32() | 1;
4342c2aba6cSDavid S. Miller }
4352c2aba6cSDavid S. Miller 
436cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
4371da177e4SLinus Torvalds {
438cd089336SDavid S. Miller 	size_t size = (1 << shift) * sizeof(struct neighbour *);
439d6bf7817SEric Dumazet 	struct neigh_hash_table *ret;
4406193d2beSEric Dumazet 	struct neighbour __rcu **buckets;
4412c2aba6cSDavid S. Miller 	int i;
4421da177e4SLinus Torvalds 
443d6bf7817SEric Dumazet 	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
444d6bf7817SEric Dumazet 	if (!ret)
445d6bf7817SEric Dumazet 		return NULL;
446d6bf7817SEric Dumazet 	if (size <= PAGE_SIZE)
447d6bf7817SEric Dumazet 		buckets = kzalloc(size, GFP_ATOMIC);
448d6bf7817SEric Dumazet 	else
4496193d2beSEric Dumazet 		buckets = (struct neighbour __rcu **)
450d6bf7817SEric Dumazet 			  __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
451d6bf7817SEric Dumazet 					   get_order(size));
452d6bf7817SEric Dumazet 	if (!buckets) {
453d6bf7817SEric Dumazet 		kfree(ret);
454d6bf7817SEric Dumazet 		return NULL;
4551da177e4SLinus Torvalds 	}
4566193d2beSEric Dumazet 	ret->hash_buckets = buckets;
457cd089336SDavid S. Miller 	ret->hash_shift = shift;
4582c2aba6cSDavid S. Miller 	for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
4592c2aba6cSDavid S. Miller 		neigh_get_hash_rnd(&ret->hash_rnd[i]);
4601da177e4SLinus Torvalds 	return ret;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
463d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head)
4641da177e4SLinus Torvalds {
465d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = container_of(head,
466d6bf7817SEric Dumazet 						    struct neigh_hash_table,
467d6bf7817SEric Dumazet 						    rcu);
468cd089336SDavid S. Miller 	size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
4696193d2beSEric Dumazet 	struct neighbour __rcu **buckets = nht->hash_buckets;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (size <= PAGE_SIZE)
472d6bf7817SEric Dumazet 		kfree(buckets);
4731da177e4SLinus Torvalds 	else
474d6bf7817SEric Dumazet 		free_pages((unsigned long)buckets, get_order(size));
475d6bf7817SEric Dumazet 	kfree(nht);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
479cd089336SDavid S. Miller 						unsigned long new_shift)
4801da177e4SLinus Torvalds {
481d6bf7817SEric Dumazet 	unsigned int i, hash;
482d6bf7817SEric Dumazet 	struct neigh_hash_table *new_nht, *old_nht;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
4851da177e4SLinus Torvalds 
486d6bf7817SEric Dumazet 	old_nht = rcu_dereference_protected(tbl->nht,
487d6bf7817SEric Dumazet 					    lockdep_is_held(&tbl->lock));
488cd089336SDavid S. Miller 	new_nht = neigh_hash_alloc(new_shift);
489d6bf7817SEric Dumazet 	if (!new_nht)
490d6bf7817SEric Dumazet 		return old_nht;
4911da177e4SLinus Torvalds 
492cd089336SDavid S. Miller 	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
4931da177e4SLinus Torvalds 		struct neighbour *n, *next;
4941da177e4SLinus Torvalds 
495767e97e1SEric Dumazet 		for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
496767e97e1SEric Dumazet 						   lockdep_is_held(&tbl->lock));
497d6bf7817SEric Dumazet 		     n != NULL;
498d6bf7817SEric Dumazet 		     n = next) {
499d6bf7817SEric Dumazet 			hash = tbl->hash(n->primary_key, n->dev,
500d6bf7817SEric Dumazet 					 new_nht->hash_rnd);
5011da177e4SLinus Torvalds 
502cd089336SDavid S. Miller 			hash >>= (32 - new_nht->hash_shift);
503767e97e1SEric Dumazet 			next = rcu_dereference_protected(n->next,
504767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock));
5051da177e4SLinus Torvalds 
506767e97e1SEric Dumazet 			rcu_assign_pointer(n->next,
507767e97e1SEric Dumazet 					   rcu_dereference_protected(
508767e97e1SEric Dumazet 						new_nht->hash_buckets[hash],
509767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
510767e97e1SEric Dumazet 			rcu_assign_pointer(new_nht->hash_buckets[hash], n);
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 
514d6bf7817SEric Dumazet 	rcu_assign_pointer(tbl->nht, new_nht);
515d6bf7817SEric Dumazet 	call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
516d6bf7817SEric Dumazet 	return new_nht;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
5201da177e4SLinus Torvalds 			       struct net_device *dev)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	struct neighbour *n;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
5251da177e4SLinus Torvalds 
526d6bf7817SEric Dumazet 	rcu_read_lock_bh();
52760395a20SEric W. Biederman 	n = __neigh_lookup_noref(tbl, pkey, dev);
52860395a20SEric W. Biederman 	if (n) {
5299f237430SReshetova, Elena 		if (!refcount_inc_not_zero(&n->refcnt))
530767e97e1SEric Dumazet 			n = NULL;
5311da177e4SLinus Torvalds 		NEIGH_CACHE_STAT_INC(tbl, hits);
5321da177e4SLinus Torvalds 	}
533767e97e1SEric Dumazet 
534d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
5351da177e4SLinus Torvalds 	return n;
5361da177e4SLinus Torvalds }
5370a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup);
5381da177e4SLinus Torvalds 
539426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
540426b5303SEric W. Biederman 				     const void *pkey)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	struct neighbour *n;
54301ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
544bc4bf5f3SPavel Emelyanov 	u32 hash_val;
545d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
5481da177e4SLinus Torvalds 
549d6bf7817SEric Dumazet 	rcu_read_lock_bh();
550d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
551cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
552767e97e1SEric Dumazet 
553767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
554767e97e1SEric Dumazet 	     n != NULL;
555767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
556426b5303SEric W. Biederman 		if (!memcmp(n->primary_key, pkey, key_len) &&
557878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(n->dev), net)) {
5589f237430SReshetova, Elena 			if (!refcount_inc_not_zero(&n->refcnt))
559767e97e1SEric Dumazet 				n = NULL;
5601da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
5611da177e4SLinus Torvalds 			break;
5621da177e4SLinus Torvalds 		}
5631da177e4SLinus Torvalds 	}
564767e97e1SEric Dumazet 
565d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
5661da177e4SLinus Torvalds 	return n;
5671da177e4SLinus Torvalds }
5680a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev);
5691da177e4SLinus Torvalds 
57058956317SDavid Ahern static struct neighbour *___neigh_create(struct neigh_table *tbl,
57158956317SDavid Ahern 					 const void *pkey,
57258956317SDavid Ahern 					 struct net_device *dev,
573e997f8a2SDavid Ahern 					 bool exempt_from_gc, bool want_ref)
5741da177e4SLinus Torvalds {
575e997f8a2SDavid Ahern 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev, exempt_from_gc);
5761da177e4SLinus Torvalds 	u32 hash_val;
57701ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
5781da177e4SLinus Torvalds 	int error;
579d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	if (!n) {
5821da177e4SLinus Torvalds 		rc = ERR_PTR(-ENOBUFS);
5831da177e4SLinus Torvalds 		goto out;
5841da177e4SLinus Torvalds 	}
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	memcpy(n->primary_key, pkey, key_len);
5871da177e4SLinus Torvalds 	n->dev = dev;
5881da177e4SLinus Torvalds 	dev_hold(dev);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/* Protocol specific setup. */
5911da177e4SLinus Torvalds 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
5921da177e4SLinus Torvalds 		rc = ERR_PTR(error);
5931da177e4SLinus Torvalds 		goto out_neigh_release;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
596da6a8fa0SDavid Miller 	if (dev->netdev_ops->ndo_neigh_construct) {
597503eebc2SJiri Pirko 		error = dev->netdev_ops->ndo_neigh_construct(dev, n);
598da6a8fa0SDavid Miller 		if (error < 0) {
599da6a8fa0SDavid Miller 			rc = ERR_PTR(error);
600da6a8fa0SDavid Miller 			goto out_neigh_release;
601da6a8fa0SDavid Miller 		}
602da6a8fa0SDavid Miller 	}
603da6a8fa0SDavid Miller 
604447f2191SDavid S. Miller 	/* Device specific setup. */
605447f2191SDavid S. Miller 	if (n->parms->neigh_setup &&
606447f2191SDavid S. Miller 	    (error = n->parms->neigh_setup(n)) < 0) {
607447f2191SDavid S. Miller 		rc = ERR_PTR(error);
608447f2191SDavid S. Miller 		goto out_neigh_release;
609447f2191SDavid S. Miller 	}
610447f2191SDavid S. Miller 
6111f9248e5SJiri Pirko 	n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
614d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
615d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
6161da177e4SLinus Torvalds 
617cd089336SDavid S. Miller 	if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
618cd089336SDavid S. Miller 		nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
6191da177e4SLinus Torvalds 
620096b9854SJim Westfall 	hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (n->parms->dead) {
6231da177e4SLinus Torvalds 		rc = ERR_PTR(-EINVAL);
6241da177e4SLinus Torvalds 		goto out_tbl_unlock;
6251da177e4SLinus Torvalds 	}
6261da177e4SLinus Torvalds 
627767e97e1SEric Dumazet 	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
628767e97e1SEric Dumazet 					    lockdep_is_held(&tbl->lock));
629767e97e1SEric Dumazet 	     n1 != NULL;
630767e97e1SEric Dumazet 	     n1 = rcu_dereference_protected(n1->next,
631767e97e1SEric Dumazet 			lockdep_is_held(&tbl->lock))) {
632096b9854SJim Westfall 		if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
633a263b309SDavid S. Miller 			if (want_ref)
6341da177e4SLinus Torvalds 				neigh_hold(n1);
6351da177e4SLinus Torvalds 			rc = n1;
6361da177e4SLinus Torvalds 			goto out_tbl_unlock;
6371da177e4SLinus Torvalds 		}
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	n->dead = 0;
641e997f8a2SDavid Ahern 	if (!exempt_from_gc)
6428cc196d6SDavid Ahern 		list_add_tail(&n->gc_list, &n->tbl->gc_list);
6438cc196d6SDavid Ahern 
644a263b309SDavid S. Miller 	if (want_ref)
6451da177e4SLinus Torvalds 		neigh_hold(n);
646767e97e1SEric Dumazet 	rcu_assign_pointer(n->next,
647767e97e1SEric Dumazet 			   rcu_dereference_protected(nht->hash_buckets[hash_val],
648767e97e1SEric Dumazet 						     lockdep_is_held(&tbl->lock)));
649767e97e1SEric Dumazet 	rcu_assign_pointer(nht->hash_buckets[hash_val], n);
6501da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
651d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is created\n", n);
6521da177e4SLinus Torvalds 	rc = n;
6531da177e4SLinus Torvalds out:
6541da177e4SLinus Torvalds 	return rc;
6551da177e4SLinus Torvalds out_tbl_unlock:
6561da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6571da177e4SLinus Torvalds out_neigh_release:
6581da177e4SLinus Torvalds 	neigh_release(n);
6591da177e4SLinus Torvalds 	goto out;
6601da177e4SLinus Torvalds }
66158956317SDavid Ahern 
66258956317SDavid Ahern struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
66358956317SDavid Ahern 				 struct net_device *dev, bool want_ref)
66458956317SDavid Ahern {
66558956317SDavid Ahern 	return ___neigh_create(tbl, pkey, dev, false, want_ref);
66658956317SDavid Ahern }
667a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create);
6681da177e4SLinus Torvalds 
66901ccdf12SAlexey Dobriyan static u32 pneigh_hash(const void *pkey, unsigned int key_len)
670fa86d322SPavel Emelyanov {
671fa86d322SPavel Emelyanov 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
672fa86d322SPavel Emelyanov 	hash_val ^= (hash_val >> 16);
673fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 8;
674fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 4;
675fa86d322SPavel Emelyanov 	hash_val &= PNEIGH_HASHMASK;
676be01d655SYOSHIFUJI Hideaki 	return hash_val;
677fa86d322SPavel Emelyanov }
678fa86d322SPavel Emelyanov 
679be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
680be01d655SYOSHIFUJI Hideaki 					      struct net *net,
681be01d655SYOSHIFUJI Hideaki 					      const void *pkey,
68201ccdf12SAlexey Dobriyan 					      unsigned int key_len,
683be01d655SYOSHIFUJI Hideaki 					      struct net_device *dev)
684be01d655SYOSHIFUJI Hideaki {
685be01d655SYOSHIFUJI Hideaki 	while (n) {
686be01d655SYOSHIFUJI Hideaki 		if (!memcmp(n->key, pkey, key_len) &&
687be01d655SYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net) &&
688be01d655SYOSHIFUJI Hideaki 		    (n->dev == dev || !n->dev))
689fa86d322SPavel Emelyanov 			return n;
690be01d655SYOSHIFUJI Hideaki 		n = n->next;
691be01d655SYOSHIFUJI Hideaki 	}
692be01d655SYOSHIFUJI Hideaki 	return NULL;
693be01d655SYOSHIFUJI Hideaki }
694be01d655SYOSHIFUJI Hideaki 
695be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
696be01d655SYOSHIFUJI Hideaki 		struct net *net, const void *pkey, struct net_device *dev)
697be01d655SYOSHIFUJI Hideaki {
69801ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
699be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
700be01d655SYOSHIFUJI Hideaki 
701be01d655SYOSHIFUJI Hideaki 	return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
702be01d655SYOSHIFUJI Hideaki 				 net, pkey, key_len, dev);
703fa86d322SPavel Emelyanov }
7040a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup);
705fa86d322SPavel Emelyanov 
706426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
707426b5303SEric W. Biederman 				    struct net *net, const void *pkey,
7081da177e4SLinus Torvalds 				    struct net_device *dev, int creat)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	struct pneigh_entry *n;
71101ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
712be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
715be01d655SYOSHIFUJI Hideaki 	n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
716be01d655SYOSHIFUJI Hideaki 			      net, pkey, key_len, dev);
717be01d655SYOSHIFUJI Hideaki 	read_unlock_bh(&tbl->lock);
7181da177e4SLinus Torvalds 
719be01d655SYOSHIFUJI Hideaki 	if (n || !creat)
7201da177e4SLinus Torvalds 		goto out;
7211da177e4SLinus Torvalds 
7224ae28944SPavel Emelyanov 	ASSERT_RTNL();
7234ae28944SPavel Emelyanov 
7241da177e4SLinus Torvalds 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
7251da177e4SLinus Torvalds 	if (!n)
7261da177e4SLinus Torvalds 		goto out;
7271da177e4SLinus Torvalds 
728754d5da6SDavid Ahern 	n->protocol = 0;
729efd7ef1cSEric W. Biederman 	write_pnet(&n->net, net);
7301da177e4SLinus Torvalds 	memcpy(n->key, pkey, key_len);
7311da177e4SLinus Torvalds 	n->dev = dev;
7321da177e4SLinus Torvalds 	if (dev)
7331da177e4SLinus Torvalds 		dev_hold(dev);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (tbl->pconstructor && tbl->pconstructor(n)) {
7361da177e4SLinus Torvalds 		if (dev)
7371da177e4SLinus Torvalds 			dev_put(dev);
7381da177e4SLinus Torvalds 		kfree(n);
7391da177e4SLinus Torvalds 		n = NULL;
7401da177e4SLinus Torvalds 		goto out;
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
7441da177e4SLinus Torvalds 	n->next = tbl->phash_buckets[hash_val];
7451da177e4SLinus Torvalds 	tbl->phash_buckets[hash_val] = n;
7461da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
7471da177e4SLinus Torvalds out:
7481da177e4SLinus Torvalds 	return n;
7491da177e4SLinus Torvalds }
7500a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 
753426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
7541da177e4SLinus Torvalds 		  struct net_device *dev)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
75701ccdf12SAlexey Dobriyan 	unsigned int key_len = tbl->key_len;
758be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
7611da177e4SLinus Torvalds 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
7621da177e4SLinus Torvalds 	     np = &n->next) {
763426b5303SEric W. Biederman 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
764878628fbSYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net)) {
7651da177e4SLinus Torvalds 			*np = n->next;
7661da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
7671da177e4SLinus Torvalds 			if (tbl->pdestructor)
7681da177e4SLinus Torvalds 				tbl->pdestructor(n);
7691da177e4SLinus Torvalds 			if (n->dev)
7701da177e4SLinus Torvalds 				dev_put(n->dev);
7711da177e4SLinus Torvalds 			kfree(n);
7721da177e4SLinus Torvalds 			return 0;
7731da177e4SLinus Torvalds 		}
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
7761da177e4SLinus Torvalds 	return -ENOENT;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
77953b76cdfSWolfgang Bumiller static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
78053b76cdfSWolfgang Bumiller 				    struct net_device *dev)
7811da177e4SLinus Torvalds {
78253b76cdfSWolfgang Bumiller 	struct pneigh_entry *n, **np, *freelist = NULL;
7831da177e4SLinus Torvalds 	u32 h;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
7861da177e4SLinus Torvalds 		np = &tbl->phash_buckets[h];
7871da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
7881da177e4SLinus Torvalds 			if (!dev || n->dev == dev) {
7891da177e4SLinus Torvalds 				*np = n->next;
79053b76cdfSWolfgang Bumiller 				n->next = freelist;
79153b76cdfSWolfgang Bumiller 				freelist = n;
79253b76cdfSWolfgang Bumiller 				continue;
79353b76cdfSWolfgang Bumiller 			}
79453b76cdfSWolfgang Bumiller 			np = &n->next;
79553b76cdfSWolfgang Bumiller 		}
79653b76cdfSWolfgang Bumiller 	}
79753b76cdfSWolfgang Bumiller 	write_unlock_bh(&tbl->lock);
79853b76cdfSWolfgang Bumiller 	while ((n = freelist)) {
79953b76cdfSWolfgang Bumiller 		freelist = n->next;
80053b76cdfSWolfgang Bumiller 		n->next = NULL;
8011da177e4SLinus Torvalds 		if (tbl->pdestructor)
8021da177e4SLinus Torvalds 			tbl->pdestructor(n);
8031da177e4SLinus Torvalds 		if (n->dev)
8041da177e4SLinus Torvalds 			dev_put(n->dev);
8051da177e4SLinus Torvalds 		kfree(n);
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	return -ENOENT;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
81006f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms);
81106f0511dSDenis V. Lunev 
81206f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms)
81306f0511dSDenis V. Lunev {
8146343944bSReshetova, Elena 	if (refcount_dec_and_test(&parms->refcnt))
81506f0511dSDenis V. Lunev 		neigh_parms_destroy(parms);
81606f0511dSDenis V. Lunev }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /*
8191da177e4SLinus Torvalds  *	neighbour must already be out of the table;
8201da177e4SLinus Torvalds  *
8211da177e4SLinus Torvalds  */
8221da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh)
8231da177e4SLinus Torvalds {
824da6a8fa0SDavid Miller 	struct net_device *dev = neigh->dev;
825da6a8fa0SDavid Miller 
8261da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	if (!neigh->dead) {
829e005d193SJoe Perches 		pr_warn("Destroying alive neighbour %p\n", neigh);
8301da177e4SLinus Torvalds 		dump_stack();
8311da177e4SLinus Torvalds 		return;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	if (neigh_del_timer(neigh))
835e005d193SJoe Perches 		pr_warn("Impossible event\n");
8361da177e4SLinus Torvalds 
837c9ab4d85SEric Dumazet 	write_lock_bh(&neigh->lock);
838c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
839c9ab4d85SEric Dumazet 	write_unlock_bh(&neigh->lock);
8408b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
8411da177e4SLinus Torvalds 
842447f2191SDavid S. Miller 	if (dev->netdev_ops->ndo_neigh_destroy)
843503eebc2SJiri Pirko 		dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
844447f2191SDavid S. Miller 
845da6a8fa0SDavid Miller 	dev_put(dev);
8461da177e4SLinus Torvalds 	neigh_parms_put(neigh->parms);
8471da177e4SLinus Torvalds 
848d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is destroyed\n", neigh);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	atomic_dec(&neigh->tbl->entries);
8515b8b0060SDavid Miller 	kfree_rcu(neigh, rcu);
8521da177e4SLinus Torvalds }
8530a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds /* Neighbour state is suspicious;
8561da177e4SLinus Torvalds    disable fast path.
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds    Called with write_locked neigh.
8591da177e4SLinus Torvalds  */
8601da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh)
8611da177e4SLinus Torvalds {
862d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is suspected\n", neigh);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	neigh->output = neigh->ops->output;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds /* Neighbour state is OK;
8681da177e4SLinus Torvalds    enable fast path.
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds    Called with write_locked neigh.
8711da177e4SLinus Torvalds  */
8721da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh)
8731da177e4SLinus Torvalds {
874d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is connected\n", neigh);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	neigh->output = neigh->ops->connected_output;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
879e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work)
8801da177e4SLinus Torvalds {
881e4c4e448SEric Dumazet 	struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
882767e97e1SEric Dumazet 	struct neighbour *n;
883767e97e1SEric Dumazet 	struct neighbour __rcu **np;
884e4c4e448SEric Dumazet 	unsigned int i;
885d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
8881da177e4SLinus Torvalds 
889e4c4e448SEric Dumazet 	write_lock_bh(&tbl->lock);
890d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
891d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	/*
8941da177e4SLinus Torvalds 	 *	periodically recompute ReachableTime from random function
8951da177e4SLinus Torvalds 	 */
8961da177e4SLinus Torvalds 
897e4c4e448SEric Dumazet 	if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
8981da177e4SLinus Torvalds 		struct neigh_parms *p;
899e4c4e448SEric Dumazet 		tbl->last_rand = jiffies;
90075fbfd33SNicolas Dichtel 		list_for_each_entry(p, &tbl->parms_list, list)
9011da177e4SLinus Torvalds 			p->reachable_time =
9021f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 
905feff9ab2SDuan Jiong 	if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
906feff9ab2SDuan Jiong 		goto out;
907feff9ab2SDuan Jiong 
908cd089336SDavid S. Miller 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
909d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
9101da177e4SLinus Torvalds 
911767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
912767e97e1SEric Dumazet 				lockdep_is_held(&tbl->lock))) != NULL) {
9131da177e4SLinus Torvalds 			unsigned int state;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 			write_lock(&n->lock);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 			state = n->nud_state;
9189ce33e46SRoopa Prabhu 			if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
9199ce33e46SRoopa Prabhu 			    (n->flags & NTF_EXT_LEARNED)) {
9201da177e4SLinus Torvalds 				write_unlock(&n->lock);
9211da177e4SLinus Torvalds 				goto next_elt;
9221da177e4SLinus Torvalds 			}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 			if (time_before(n->used, n->confirmed))
9251da177e4SLinus Torvalds 				n->used = n->confirmed;
9261da177e4SLinus Torvalds 
9279f237430SReshetova, Elena 			if (refcount_read(&n->refcnt) == 1 &&
9281da177e4SLinus Torvalds 			    (state == NUD_FAILED ||
9291f9248e5SJiri Pirko 			     time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
9301da177e4SLinus Torvalds 				*np = n->next;
93158956317SDavid Ahern 				neigh_mark_dead(n);
9321da177e4SLinus Torvalds 				write_unlock(&n->lock);
9334f494554SThomas Graf 				neigh_cleanup_and_release(n);
9341da177e4SLinus Torvalds 				continue;
9351da177e4SLinus Torvalds 			}
9361da177e4SLinus Torvalds 			write_unlock(&n->lock);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds next_elt:
9391da177e4SLinus Torvalds 			np = &n->next;
9401da177e4SLinus Torvalds 		}
941e4c4e448SEric Dumazet 		/*
942e4c4e448SEric Dumazet 		 * It's fine to release lock here, even if hash table
943e4c4e448SEric Dumazet 		 * grows while we are preempted.
944e4c4e448SEric Dumazet 		 */
945e4c4e448SEric Dumazet 		write_unlock_bh(&tbl->lock);
946e4c4e448SEric Dumazet 		cond_resched();
947e4c4e448SEric Dumazet 		write_lock_bh(&tbl->lock);
94884338a6cSMichel Machado 		nht = rcu_dereference_protected(tbl->nht,
94984338a6cSMichel Machado 						lockdep_is_held(&tbl->lock));
950e4c4e448SEric Dumazet 	}
9512724680bSYOSHIFUJI Hideaki / 吉藤英明 out:
9521f9248e5SJiri Pirko 	/* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
9531f9248e5SJiri Pirko 	 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
9541f9248e5SJiri Pirko 	 * BASE_REACHABLE_TIME.
9551da177e4SLinus Torvalds 	 */
956f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
9571f9248e5SJiri Pirko 			      NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
958e4c4e448SEric Dumazet 	write_unlock_bh(&tbl->lock);
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n)
9621da177e4SLinus Torvalds {
9631da177e4SLinus Torvalds 	struct neigh_parms *p = n->parms;
9648da86466SYOSHIFUJI Hideaki/吉藤英明 	return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
9658da86466SYOSHIFUJI Hideaki/吉藤英明 	       (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
9668da86466SYOSHIFUJI Hideaki/吉藤英明 	        NEIGH_VAR(p, MCAST_PROBES));
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9695ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh)
9700a141509SEric Dumazet 	__releases(neigh->lock)
9710a141509SEric Dumazet 	__acquires(neigh->lock)
9725ef12d98STimo Teras {
9735ef12d98STimo Teras 	struct sk_buff *skb;
9745ef12d98STimo Teras 
9755ef12d98STimo Teras 	NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
976d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is failed\n", neigh);
9775ef12d98STimo Teras 	neigh->updated = jiffies;
9785ef12d98STimo Teras 
9795ef12d98STimo Teras 	/* It is very thin place. report_unreachable is very complicated
9805ef12d98STimo Teras 	   routine. Particularly, it can hit the same neighbour entry!
9815ef12d98STimo Teras 
9825ef12d98STimo Teras 	   So that, we try to be accurate and avoid dead loop. --ANK
9835ef12d98STimo Teras 	 */
9845ef12d98STimo Teras 	while (neigh->nud_state == NUD_FAILED &&
9855ef12d98STimo Teras 	       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
9865ef12d98STimo Teras 		write_unlock(&neigh->lock);
9875ef12d98STimo Teras 		neigh->ops->error_report(neigh, skb);
9885ef12d98STimo Teras 		write_lock(&neigh->lock);
9895ef12d98STimo Teras 	}
990c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
9918b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
9925ef12d98STimo Teras }
9935ef12d98STimo Teras 
994cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh)
995cd28ca0aSEric Dumazet 	__releases(neigh->lock)
996cd28ca0aSEric Dumazet {
9974ed377e3SHannes Frederic Sowa 	struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
998cd28ca0aSEric Dumazet 	/* keep skb alive even if arp_queue overflows */
999cd28ca0aSEric Dumazet 	if (skb)
100019125c1aSMartin Zhang 		skb = skb_clone(skb, GFP_ATOMIC);
1001cd28ca0aSEric Dumazet 	write_unlock(&neigh->lock);
100248481c8fSEric Dumazet 	if (neigh->ops->solicit)
1003cd28ca0aSEric Dumazet 		neigh->ops->solicit(neigh, skb);
1004cd28ca0aSEric Dumazet 	atomic_inc(&neigh->probes);
1005cd28ca0aSEric Dumazet 	kfree_skb(skb);
1006cd28ca0aSEric Dumazet }
1007cd28ca0aSEric Dumazet 
10081da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */
10091da177e4SLinus Torvalds 
1010e99e88a9SKees Cook static void neigh_timer_handler(struct timer_list *t)
10111da177e4SLinus Torvalds {
10121da177e4SLinus Torvalds 	unsigned long now, next;
1013e99e88a9SKees Cook 	struct neighbour *neigh = from_timer(neigh, t, timer);
101495c96174SEric Dumazet 	unsigned int state;
10151da177e4SLinus Torvalds 	int notify = 0;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	write_lock(&neigh->lock);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	state = neigh->nud_state;
10201da177e4SLinus Torvalds 	now = jiffies;
10211da177e4SLinus Torvalds 	next = now + HZ;
10221da177e4SLinus Torvalds 
1023045f7b3bSDavid S. Miller 	if (!(state & NUD_IN_TIMER))
10241da177e4SLinus Torvalds 		goto out;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	if (state & NUD_REACHABLE) {
10271da177e4SLinus Torvalds 		if (time_before_eq(now,
10281da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->reachable_time)) {
1029d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is still alive\n", neigh);
10301da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
10311da177e4SLinus Torvalds 		} else if (time_before_eq(now,
10321f9248e5SJiri Pirko 					  neigh->used +
10331f9248e5SJiri Pirko 					  NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
1034d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is delayed\n", neigh);
10351da177e4SLinus Torvalds 			neigh->nud_state = NUD_DELAY;
1036955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
10371da177e4SLinus Torvalds 			neigh_suspect(neigh);
10381f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
10391da177e4SLinus Torvalds 		} else {
1040d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is suspected\n", neigh);
10411da177e4SLinus Torvalds 			neigh->nud_state = NUD_STALE;
1042955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
10431da177e4SLinus Torvalds 			neigh_suspect(neigh);
10448d71740cSTom Tucker 			notify = 1;
10451da177e4SLinus Torvalds 		}
10461da177e4SLinus Torvalds 	} else if (state & NUD_DELAY) {
10471da177e4SLinus Torvalds 		if (time_before_eq(now,
10481f9248e5SJiri Pirko 				   neigh->confirmed +
10491f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
1050d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is now reachable\n", neigh);
10511da177e4SLinus Torvalds 			neigh->nud_state = NUD_REACHABLE;
1052955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
10531da177e4SLinus Torvalds 			neigh_connect(neigh);
10548d71740cSTom Tucker 			notify = 1;
10551da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
10561da177e4SLinus Torvalds 		} else {
1057d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is probed\n", neigh);
10581da177e4SLinus Torvalds 			neigh->nud_state = NUD_PROBE;
1059955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
10601da177e4SLinus Torvalds 			atomic_set(&neigh->probes, 0);
1061765c9c63SErik Kline 			notify = 1;
10621f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
10631da177e4SLinus Torvalds 		}
10641da177e4SLinus Torvalds 	} else {
10651da177e4SLinus Torvalds 		/* NUD_PROBE|NUD_INCOMPLETE */
10661f9248e5SJiri Pirko 		next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
10701da177e4SLinus Torvalds 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
10711da177e4SLinus Torvalds 		neigh->nud_state = NUD_FAILED;
10721da177e4SLinus Torvalds 		notify = 1;
10735ef12d98STimo Teras 		neigh_invalidate(neigh);
10745e2c21dcSDuan Jiong 		goto out;
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	if (neigh->nud_state & NUD_IN_TIMER) {
10781da177e4SLinus Torvalds 		if (time_before(next, jiffies + HZ/2))
10791da177e4SLinus Torvalds 			next = jiffies + HZ/2;
10806fb9974fSHerbert Xu 		if (!mod_timer(&neigh->timer, next))
10816fb9974fSHerbert Xu 			neigh_hold(neigh);
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
1084cd28ca0aSEric Dumazet 		neigh_probe(neigh);
10859ff56607SDavid S. Miller 	} else {
10861da177e4SLinus Torvalds out:
10871da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
10889ff56607SDavid S. Miller 	}
10891da177e4SLinus Torvalds 
1090d961db35SThomas Graf 	if (notify)
10917b8f7a40SRoopa Prabhu 		neigh_update_notify(neigh, 0);
1092d961db35SThomas Graf 
10931da177e4SLinus Torvalds 	neigh_release(neigh);
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
10971da177e4SLinus Torvalds {
10981da177e4SLinus Torvalds 	int rc;
1099cd28ca0aSEric Dumazet 	bool immediate_probe = false;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	rc = 0;
11041da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
11051da177e4SLinus Torvalds 		goto out_unlock_bh;
11062c51a97fSJulian Anastasov 	if (neigh->dead)
11072c51a97fSJulian Anastasov 		goto out_dead;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
11101f9248e5SJiri Pirko 		if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
11111f9248e5SJiri Pirko 		    NEIGH_VAR(neigh->parms, APP_PROBES)) {
1112cd28ca0aSEric Dumazet 			unsigned long next, now = jiffies;
1113cd28ca0aSEric Dumazet 
11141f9248e5SJiri Pirko 			atomic_set(&neigh->probes,
11151f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, UCAST_PROBES));
11161da177e4SLinus Torvalds 			neigh->nud_state     = NUD_INCOMPLETE;
1117cd28ca0aSEric Dumazet 			neigh->updated = now;
11181f9248e5SJiri Pirko 			next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
11191f9248e5SJiri Pirko 					 HZ/2);
1120cd28ca0aSEric Dumazet 			neigh_add_timer(neigh, next);
1121cd28ca0aSEric Dumazet 			immediate_probe = true;
11221da177e4SLinus Torvalds 		} else {
11231da177e4SLinus Torvalds 			neigh->nud_state = NUD_FAILED;
1124955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
11251da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 			kfree_skb(skb);
11281da177e4SLinus Torvalds 			return 1;
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 	} else if (neigh->nud_state & NUD_STALE) {
1131d5d427cdSJoe Perches 		neigh_dbg(2, "neigh %p is delayed\n", neigh);
11321da177e4SLinus Torvalds 		neigh->nud_state = NUD_DELAY;
1133955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
11341f9248e5SJiri Pirko 		neigh_add_timer(neigh, jiffies +
11351f9248e5SJiri Pirko 				NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	if (neigh->nud_state == NUD_INCOMPLETE) {
11391da177e4SLinus Torvalds 		if (skb) {
11408b5c171bSEric Dumazet 			while (neigh->arp_queue_len_bytes + skb->truesize >
11411f9248e5SJiri Pirko 			       NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
11421da177e4SLinus Torvalds 				struct sk_buff *buff;
11438b5c171bSEric Dumazet 
1144f72051b0SDavid S. Miller 				buff = __skb_dequeue(&neigh->arp_queue);
11458b5c171bSEric Dumazet 				if (!buff)
11468b5c171bSEric Dumazet 					break;
11478b5c171bSEric Dumazet 				neigh->arp_queue_len_bytes -= buff->truesize;
11481da177e4SLinus Torvalds 				kfree_skb(buff);
11499a6d276eSNeil Horman 				NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
11501da177e4SLinus Torvalds 			}
1151a4731138SEric Dumazet 			skb_dst_force(skb);
11521da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->arp_queue, skb);
11538b5c171bSEric Dumazet 			neigh->arp_queue_len_bytes += skb->truesize;
11541da177e4SLinus Torvalds 		}
11551da177e4SLinus Torvalds 		rc = 1;
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds out_unlock_bh:
1158cd28ca0aSEric Dumazet 	if (immediate_probe)
1159cd28ca0aSEric Dumazet 		neigh_probe(neigh);
1160cd28ca0aSEric Dumazet 	else
1161cd28ca0aSEric Dumazet 		write_unlock(&neigh->lock);
1162cd28ca0aSEric Dumazet 	local_bh_enable();
11631da177e4SLinus Torvalds 	return rc;
11642c51a97fSJulian Anastasov 
11652c51a97fSJulian Anastasov out_dead:
11662c51a97fSJulian Anastasov 	if (neigh->nud_state & NUD_STALE)
11672c51a97fSJulian Anastasov 		goto out_unlock_bh;
11682c51a97fSJulian Anastasov 	write_unlock_bh(&neigh->lock);
11692c51a97fSJulian Anastasov 	kfree_skb(skb);
11702c51a97fSJulian Anastasov 	return 1;
11711da177e4SLinus Torvalds }
11720a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send);
11731da177e4SLinus Torvalds 
1174f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh)
11751da177e4SLinus Torvalds {
11761da177e4SLinus Torvalds 	struct hh_cache *hh;
11773b04dddeSStephen Hemminger 	void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
117891a72a70SDoug Kehn 		= NULL;
117991a72a70SDoug Kehn 
118091a72a70SDoug Kehn 	if (neigh->dev->header_ops)
118191a72a70SDoug Kehn 		update = neigh->dev->header_ops->cache_update;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	if (update) {
1184f6b72b62SDavid S. Miller 		hh = &neigh->hh;
1185f6b72b62SDavid S. Miller 		if (hh->hh_len) {
11863644f0ceSStephen Hemminger 			write_seqlock_bh(&hh->hh_lock);
11871da177e4SLinus Torvalds 			update(hh, neigh->dev, neigh->ha);
11883644f0ceSStephen Hemminger 			write_sequnlock_bh(&hh->hh_lock);
11891da177e4SLinus Torvalds 		}
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds /* Generic update routine.
11961da177e4SLinus Torvalds    -- lladdr is new lladdr or NULL, if it is not supplied.
11971da177e4SLinus Torvalds    -- new    is new state.
11981da177e4SLinus Torvalds    -- flags
11991da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
12001da177e4SLinus Torvalds 				if it is different.
12011da177e4SLinus Torvalds 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
12021da177e4SLinus Torvalds 				lladdr instead of overriding it
12031da177e4SLinus Torvalds 				if it is different.
12041da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
12071da177e4SLinus Torvalds 				NTF_ROUTER flag.
12081da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
12091da177e4SLinus Torvalds 				a router.
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds    Caller MUST hold reference count on the entry.
12121da177e4SLinus Torvalds  */
12131da177e4SLinus Torvalds 
12147a35a50dSDavid Ahern static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
12157a35a50dSDavid Ahern 			  u8 new, u32 flags, u32 nlmsg_pid,
12167a35a50dSDavid Ahern 			  struct netlink_ext_ack *extack)
12171da177e4SLinus Torvalds {
1218e997f8a2SDavid Ahern 	bool ext_learn_change = false;
12191da177e4SLinus Torvalds 	u8 old;
12201da177e4SLinus Torvalds 	int err;
12211da177e4SLinus Torvalds 	int notify = 0;
12221da177e4SLinus Torvalds 	struct net_device *dev;
12231da177e4SLinus Torvalds 	int update_isrouter = 0;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	dev    = neigh->dev;
12281da177e4SLinus Torvalds 	old    = neigh->nud_state;
12291da177e4SLinus Torvalds 	err    = -EPERM;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
12321da177e4SLinus Torvalds 	    (old & (NUD_NOARP | NUD_PERMANENT)))
12331da177e4SLinus Torvalds 		goto out;
12347a35a50dSDavid Ahern 	if (neigh->dead) {
12357a35a50dSDavid Ahern 		NL_SET_ERR_MSG(extack, "Neighbor entry is now dead");
12362c51a97fSJulian Anastasov 		goto out;
12377a35a50dSDavid Ahern 	}
12381da177e4SLinus Torvalds 
1239e997f8a2SDavid Ahern 	ext_learn_change = neigh_update_ext_learned(neigh, flags, &notify);
12409ce33e46SRoopa Prabhu 
12411da177e4SLinus Torvalds 	if (!(new & NUD_VALID)) {
12421da177e4SLinus Torvalds 		neigh_del_timer(neigh);
12431da177e4SLinus Torvalds 		if (old & NUD_CONNECTED)
12441da177e4SLinus Torvalds 			neigh_suspect(neigh);
12459c29a2f5SDavid Ahern 		neigh->nud_state = new;
12461da177e4SLinus Torvalds 		err = 0;
12471da177e4SLinus Torvalds 		notify = old & NUD_VALID;
1248d2fb4fb8SRoopa Prabhu 		if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
12495ef12d98STimo Teras 		    (new & NUD_FAILED)) {
12505ef12d98STimo Teras 			neigh_invalidate(neigh);
12515ef12d98STimo Teras 			notify = 1;
12525ef12d98STimo Teras 		}
12531da177e4SLinus Torvalds 		goto out;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	/* Compare new lladdr with cached one */
12571da177e4SLinus Torvalds 	if (!dev->addr_len) {
12581da177e4SLinus Torvalds 		/* First case: device needs no address. */
12591da177e4SLinus Torvalds 		lladdr = neigh->ha;
12601da177e4SLinus Torvalds 	} else if (lladdr) {
12611da177e4SLinus Torvalds 		/* The second case: if something is already cached
12621da177e4SLinus Torvalds 		   and a new address is proposed:
12631da177e4SLinus Torvalds 		   - compare new & old
12641da177e4SLinus Torvalds 		   - if they are different, check override flag
12651da177e4SLinus Torvalds 		 */
12661da177e4SLinus Torvalds 		if ((old & NUD_VALID) &&
12671da177e4SLinus Torvalds 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
12681da177e4SLinus Torvalds 			lladdr = neigh->ha;
12691da177e4SLinus Torvalds 	} else {
12701da177e4SLinus Torvalds 		/* No address is supplied; if we know something,
12711da177e4SLinus Torvalds 		   use it, otherwise discard the request.
12721da177e4SLinus Torvalds 		 */
12731da177e4SLinus Torvalds 		err = -EINVAL;
12747a35a50dSDavid Ahern 		if (!(old & NUD_VALID)) {
12757a35a50dSDavid Ahern 			NL_SET_ERR_MSG(extack, "No link layer address given");
12761da177e4SLinus Torvalds 			goto out;
12777a35a50dSDavid Ahern 		}
12781da177e4SLinus Torvalds 		lladdr = neigh->ha;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 
1281f0e0d044SVasily Khoruzhick 	/* Update confirmed timestamp for neighbour entry after we
1282f0e0d044SVasily Khoruzhick 	 * received ARP packet even if it doesn't change IP to MAC binding.
1283f0e0d044SVasily Khoruzhick 	 */
1284f0e0d044SVasily Khoruzhick 	if (new & NUD_CONNECTED)
1285f0e0d044SVasily Khoruzhick 		neigh->confirmed = jiffies;
1286f0e0d044SVasily Khoruzhick 
12871da177e4SLinus Torvalds 	/* If entry was valid and address is not changed,
12881da177e4SLinus Torvalds 	   do not change entry state, if new one is STALE.
12891da177e4SLinus Torvalds 	 */
12901da177e4SLinus Torvalds 	err = 0;
12911da177e4SLinus Torvalds 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
12921da177e4SLinus Torvalds 	if (old & NUD_VALID) {
12931da177e4SLinus Torvalds 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
12941da177e4SLinus Torvalds 			update_isrouter = 0;
12951da177e4SLinus Torvalds 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
12961da177e4SLinus Torvalds 			    (old & NUD_CONNECTED)) {
12971da177e4SLinus Torvalds 				lladdr = neigh->ha;
12981da177e4SLinus Torvalds 				new = NUD_STALE;
12991da177e4SLinus Torvalds 			} else
13001da177e4SLinus Torvalds 				goto out;
13011da177e4SLinus Torvalds 		} else {
13020e7bbcc1SJulian Anastasov 			if (lladdr == neigh->ha && new == NUD_STALE &&
13030e7bbcc1SJulian Anastasov 			    !(flags & NEIGH_UPDATE_F_ADMIN))
13041da177e4SLinus Torvalds 				new = old;
13051da177e4SLinus Torvalds 		}
13061da177e4SLinus Torvalds 	}
13071da177e4SLinus Torvalds 
1308f0e0d044SVasily Khoruzhick 	/* Update timestamp only once we know we will make a change to the
130977d71233SIhar Hrachyshka 	 * neighbour entry. Otherwise we risk to move the locktime window with
131077d71233SIhar Hrachyshka 	 * noop updates and ignore relevant ARP updates.
131177d71233SIhar Hrachyshka 	 */
1312f0e0d044SVasily Khoruzhick 	if (new != old || lladdr != neigh->ha)
131377d71233SIhar Hrachyshka 		neigh->updated = jiffies;
131477d71233SIhar Hrachyshka 
13151da177e4SLinus Torvalds 	if (new != old) {
13161da177e4SLinus Torvalds 		neigh_del_timer(neigh);
1317765c9c63SErik Kline 		if (new & NUD_PROBE)
1318765c9c63SErik Kline 			atomic_set(&neigh->probes, 0);
1319a43d8994SPavel Emelyanov 		if (new & NUD_IN_TIMER)
1320667347f1SDavid S. Miller 			neigh_add_timer(neigh, (jiffies +
13211da177e4SLinus Torvalds 						((new & NUD_REACHABLE) ?
1322667347f1SDavid S. Miller 						 neigh->parms->reachable_time :
1323667347f1SDavid S. Miller 						 0)));
13249c29a2f5SDavid Ahern 		neigh->nud_state = new;
132553385d2dSBob Gilligan 		notify = 1;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	if (lladdr != neigh->ha) {
13290ed8ddf4SEric Dumazet 		write_seqlock(&neigh->ha_lock);
13301da177e4SLinus Torvalds 		memcpy(&neigh->ha, lladdr, dev->addr_len);
13310ed8ddf4SEric Dumazet 		write_sequnlock(&neigh->ha_lock);
13321da177e4SLinus Torvalds 		neigh_update_hhs(neigh);
13331da177e4SLinus Torvalds 		if (!(new & NUD_CONNECTED))
13341da177e4SLinus Torvalds 			neigh->confirmed = jiffies -
13351f9248e5SJiri Pirko 				      (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
13361da177e4SLinus Torvalds 		notify = 1;
13371da177e4SLinus Torvalds 	}
13381da177e4SLinus Torvalds 	if (new == old)
13391da177e4SLinus Torvalds 		goto out;
13401da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
13411da177e4SLinus Torvalds 		neigh_connect(neigh);
13421da177e4SLinus Torvalds 	else
13431da177e4SLinus Torvalds 		neigh_suspect(neigh);
13441da177e4SLinus Torvalds 	if (!(old & NUD_VALID)) {
13451da177e4SLinus Torvalds 		struct sk_buff *skb;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 		/* Again: avoid dead loop if something went wrong */
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 		while (neigh->nud_state & NUD_VALID &&
13501da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
135169cce1d1SDavid S. Miller 			struct dst_entry *dst = skb_dst(skb);
135269cce1d1SDavid S. Miller 			struct neighbour *n2, *n1 = neigh;
13531da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
1354e049f288Sroy.qing.li@gmail.com 
1355e049f288Sroy.qing.li@gmail.com 			rcu_read_lock();
135613a43d94SDavid S. Miller 
135713a43d94SDavid S. Miller 			/* Why not just use 'neigh' as-is?  The problem is that
135813a43d94SDavid S. Miller 			 * things such as shaper, eql, and sch_teql can end up
135913a43d94SDavid S. Miller 			 * using alternative, different, neigh objects to output
136013a43d94SDavid S. Miller 			 * the packet in the output path.  So what we need to do
136113a43d94SDavid S. Miller 			 * here is re-lookup the top-level neigh in the path so
136213a43d94SDavid S. Miller 			 * we can reinject the packet there.
136313a43d94SDavid S. Miller 			 */
136413a43d94SDavid S. Miller 			n2 = NULL;
136513a43d94SDavid S. Miller 			if (dst) {
136613a43d94SDavid S. Miller 				n2 = dst_neigh_lookup_skb(dst, skb);
136713a43d94SDavid S. Miller 				if (n2)
136869cce1d1SDavid S. Miller 					n1 = n2;
136913a43d94SDavid S. Miller 			}
13708f40b161SDavid S. Miller 			n1->output(n1, skb);
137113a43d94SDavid S. Miller 			if (n2)
137213a43d94SDavid S. Miller 				neigh_release(n2);
1373e049f288Sroy.qing.li@gmail.com 			rcu_read_unlock();
1374e049f288Sroy.qing.li@gmail.com 
13751da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
13761da177e4SLinus Torvalds 		}
1377c9ab4d85SEric Dumazet 		__skb_queue_purge(&neigh->arp_queue);
13788b5c171bSEric Dumazet 		neigh->arp_queue_len_bytes = 0;
13791da177e4SLinus Torvalds 	}
13801da177e4SLinus Torvalds out:
1381fc6e8073SRoopa Prabhu 	if (update_isrouter)
1382fc6e8073SRoopa Prabhu 		neigh_update_is_router(neigh, flags, &notify);
13831da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
13848d71740cSTom Tucker 
1385e997f8a2SDavid Ahern 	if (((new ^ old) & NUD_PERMANENT) || ext_learn_change)
13869c29a2f5SDavid Ahern 		neigh_update_gc_list(neigh);
13879c29a2f5SDavid Ahern 
13888d71740cSTom Tucker 	if (notify)
13897b8f7a40SRoopa Prabhu 		neigh_update_notify(neigh, nlmsg_pid);
1390d961db35SThomas Graf 
13911da177e4SLinus Torvalds 	return err;
13921da177e4SLinus Torvalds }
13937a35a50dSDavid Ahern 
13947a35a50dSDavid Ahern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
13957a35a50dSDavid Ahern 		 u32 flags, u32 nlmsg_pid)
13967a35a50dSDavid Ahern {
13977a35a50dSDavid Ahern 	return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL);
13987a35a50dSDavid Ahern }
13990a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update);
14001da177e4SLinus Torvalds 
14017e980569SJiri Benc /* Update the neigh to listen temporarily for probe responses, even if it is
14027e980569SJiri Benc  * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
14037e980569SJiri Benc  */
14047e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh)
14057e980569SJiri Benc {
14062c51a97fSJulian Anastasov 	if (neigh->dead)
14072c51a97fSJulian Anastasov 		return;
14087e980569SJiri Benc 	neigh->updated = jiffies;
14097e980569SJiri Benc 	if (!(neigh->nud_state & NUD_FAILED))
14107e980569SJiri Benc 		return;
14112176d5d4SDuan Jiong 	neigh->nud_state = NUD_INCOMPLETE;
14122176d5d4SDuan Jiong 	atomic_set(&neigh->probes, neigh_max_probes(neigh));
14137e980569SJiri Benc 	neigh_add_timer(neigh,
14147e980569SJiri Benc 			jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
14157e980569SJiri Benc }
14167e980569SJiri Benc EXPORT_SYMBOL(__neigh_set_probe_once);
14177e980569SJiri Benc 
14181da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl,
14191da177e4SLinus Torvalds 				 u8 *lladdr, void *saddr,
14201da177e4SLinus Torvalds 				 struct net_device *dev)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
14231da177e4SLinus Torvalds 						 lladdr || !dev->addr_len);
14241da177e4SLinus Torvalds 	if (neigh)
14251da177e4SLinus Torvalds 		neigh_update(neigh, lladdr, NUD_STALE,
14267b8f7a40SRoopa Prabhu 			     NEIGH_UPDATE_F_OVERRIDE, 0);
14271da177e4SLinus Torvalds 	return neigh;
14281da177e4SLinus Torvalds }
14290a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns);
14301da177e4SLinus Torvalds 
143134d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */
1432bdf53c58SEric W. Biederman static void neigh_hh_init(struct neighbour *n)
14331da177e4SLinus Torvalds {
1434bdf53c58SEric W. Biederman 	struct net_device *dev = n->dev;
1435bdf53c58SEric W. Biederman 	__be16 prot = n->tbl->protocol;
1436f6b72b62SDavid S. Miller 	struct hh_cache	*hh = &n->hh;
14370ed8ddf4SEric Dumazet 
14380ed8ddf4SEric Dumazet 	write_lock_bh(&n->lock);
143934d101ddSEric Dumazet 
1440f6b72b62SDavid S. Miller 	/* Only one thread can come in here and initialize the
1441f6b72b62SDavid S. Miller 	 * hh_cache entry.
1442f6b72b62SDavid S. Miller 	 */
1443b23b5455SDavid S. Miller 	if (!hh->hh_len)
1444b23b5455SDavid S. Miller 		dev->header_ops->cache(n, hh, prot);
1445f6b72b62SDavid S. Miller 
14460ed8ddf4SEric Dumazet 	write_unlock_bh(&n->lock);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds /* Slow and careful. */
14501da177e4SLinus Torvalds 
14518f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	int rc = 0;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	if (!neigh_event_send(neigh, skb)) {
14561da177e4SLinus Torvalds 		int err;
14571da177e4SLinus Torvalds 		struct net_device *dev = neigh->dev;
14580ed8ddf4SEric Dumazet 		unsigned int seq;
145934d101ddSEric Dumazet 
1460f6b72b62SDavid S. Miller 		if (dev->header_ops->cache && !neigh->hh.hh_len)
1461bdf53c58SEric W. Biederman 			neigh_hh_init(neigh);
146234d101ddSEric Dumazet 
14630ed8ddf4SEric Dumazet 		do {
1464e1f16503Sramesh.nagappa@gmail.com 			__skb_pull(skb, skb_network_offset(skb));
14650ed8ddf4SEric Dumazet 			seq = read_seqbegin(&neigh->ha_lock);
14660c4e8581SStephen Hemminger 			err = dev_hard_header(skb, dev, ntohs(skb->protocol),
14671da177e4SLinus Torvalds 					      neigh->ha, NULL, skb->len);
14680ed8ddf4SEric Dumazet 		} while (read_seqretry(&neigh->ha_lock, seq));
146934d101ddSEric Dumazet 
14701da177e4SLinus Torvalds 		if (err >= 0)
1471542d4d68SDavid S. Miller 			rc = dev_queue_xmit(skb);
14721da177e4SLinus Torvalds 		else
14731da177e4SLinus Torvalds 			goto out_kfree_skb;
14741da177e4SLinus Torvalds 	}
14751da177e4SLinus Torvalds out:
14761da177e4SLinus Torvalds 	return rc;
14771da177e4SLinus Torvalds out_kfree_skb:
14781da177e4SLinus Torvalds 	rc = -EINVAL;
14791da177e4SLinus Torvalds 	kfree_skb(skb);
14801da177e4SLinus Torvalds 	goto out;
14811da177e4SLinus Torvalds }
14820a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output);
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds /* As fast as possible without hh cache */
14851da177e4SLinus Torvalds 
14868f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
14890ed8ddf4SEric Dumazet 	unsigned int seq;
14908f40b161SDavid S. Miller 	int err;
14911da177e4SLinus Torvalds 
14920ed8ddf4SEric Dumazet 	do {
1493e1f16503Sramesh.nagappa@gmail.com 		__skb_pull(skb, skb_network_offset(skb));
14940ed8ddf4SEric Dumazet 		seq = read_seqbegin(&neigh->ha_lock);
14950c4e8581SStephen Hemminger 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
14961da177e4SLinus Torvalds 				      neigh->ha, NULL, skb->len);
14970ed8ddf4SEric Dumazet 	} while (read_seqretry(&neigh->ha_lock, seq));
14980ed8ddf4SEric Dumazet 
14991da177e4SLinus Torvalds 	if (err >= 0)
1500542d4d68SDavid S. Miller 		err = dev_queue_xmit(skb);
15011da177e4SLinus Torvalds 	else {
15021da177e4SLinus Torvalds 		err = -EINVAL;
15031da177e4SLinus Torvalds 		kfree_skb(skb);
15041da177e4SLinus Torvalds 	}
15051da177e4SLinus Torvalds 	return err;
15061da177e4SLinus Torvalds }
15070a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output);
15081da177e4SLinus Torvalds 
15098f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
15108f40b161SDavid S. Miller {
15118f40b161SDavid S. Miller 	return dev_queue_xmit(skb);
15128f40b161SDavid S. Miller }
15138f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output);
15148f40b161SDavid S. Miller 
1515e99e88a9SKees Cook static void neigh_proxy_process(struct timer_list *t)
15161da177e4SLinus Torvalds {
1517e99e88a9SKees Cook 	struct neigh_table *tbl = from_timer(tbl, t, proxy_timer);
15181da177e4SLinus Torvalds 	long sched_next = 0;
15191da177e4SLinus Torvalds 	unsigned long now = jiffies;
1520f72051b0SDavid S. Miller 	struct sk_buff *skb, *n;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
15231da177e4SLinus Torvalds 
1524f72051b0SDavid S. Miller 	skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1525f72051b0SDavid S. Miller 		long tdif = NEIGH_CB(skb)->sched_next - now;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 		if (tdif <= 0) {
1528f72051b0SDavid S. Miller 			struct net_device *dev = skb->dev;
152920e6074eSEric Dumazet 
1530f72051b0SDavid S. Miller 			__skb_unlink(skb, &tbl->proxy_queue);
153120e6074eSEric Dumazet 			if (tbl->proxy_redo && netif_running(dev)) {
153220e6074eSEric Dumazet 				rcu_read_lock();
1533f72051b0SDavid S. Miller 				tbl->proxy_redo(skb);
153420e6074eSEric Dumazet 				rcu_read_unlock();
153520e6074eSEric Dumazet 			} else {
1536f72051b0SDavid S. Miller 				kfree_skb(skb);
153720e6074eSEric Dumazet 			}
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 			dev_put(dev);
15401da177e4SLinus Torvalds 		} else if (!sched_next || tdif < sched_next)
15411da177e4SLinus Torvalds 			sched_next = tdif;
15421da177e4SLinus Torvalds 	}
15431da177e4SLinus Torvalds 	del_timer(&tbl->proxy_timer);
15441da177e4SLinus Torvalds 	if (sched_next)
15451da177e4SLinus Torvalds 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
15461da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
15501da177e4SLinus Torvalds 		    struct sk_buff *skb)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	unsigned long now = jiffies;
155363862b5bSAruna-Hewapathirane 
155463862b5bSAruna-Hewapathirane 	unsigned long sched_next = now + (prandom_u32() %
15551f9248e5SJiri Pirko 					  NEIGH_VAR(p, PROXY_DELAY));
15561da177e4SLinus Torvalds 
15571f9248e5SJiri Pirko 	if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
15581da177e4SLinus Torvalds 		kfree_skb(skb);
15591da177e4SLinus Torvalds 		return;
15601da177e4SLinus Torvalds 	}
1561a61bbcf2SPatrick McHardy 
1562a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->sched_next = sched_next;
1563a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
15661da177e4SLinus Torvalds 	if (del_timer(&tbl->proxy_timer)) {
15671da177e4SLinus Torvalds 		if (time_before(tbl->proxy_timer.expires, sched_next))
15681da177e4SLinus Torvalds 			sched_next = tbl->proxy_timer.expires;
15691da177e4SLinus Torvalds 	}
1570adf30907SEric Dumazet 	skb_dst_drop(skb);
15711da177e4SLinus Torvalds 	dev_hold(skb->dev);
15721da177e4SLinus Torvalds 	__skb_queue_tail(&tbl->proxy_queue, skb);
15731da177e4SLinus Torvalds 	mod_timer(&tbl->proxy_timer, sched_next);
15741da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
15751da177e4SLinus Torvalds }
15760a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue);
15771da177e4SLinus Torvalds 
157897fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1579426b5303SEric W. Biederman 						      struct net *net, int ifindex)
1580426b5303SEric W. Biederman {
1581426b5303SEric W. Biederman 	struct neigh_parms *p;
1582426b5303SEric W. Biederman 
158375fbfd33SNicolas Dichtel 	list_for_each_entry(p, &tbl->parms_list, list) {
1584878628fbSYOSHIFUJI Hideaki 		if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1585170d6f99SGao feng 		    (!p->dev && !ifindex && net_eq(net, &init_net)))
1586426b5303SEric W. Biederman 			return p;
1587426b5303SEric W. Biederman 	}
1588426b5303SEric W. Biederman 
1589426b5303SEric W. Biederman 	return NULL;
1590426b5303SEric W. Biederman }
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
15931da177e4SLinus Torvalds 				      struct neigh_table *tbl)
15941da177e4SLinus Torvalds {
1595cf89d6b2SGao feng 	struct neigh_parms *p;
159600829823SStephen Hemminger 	struct net *net = dev_net(dev);
159700829823SStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
15981da177e4SLinus Torvalds 
1599cf89d6b2SGao feng 	p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
16001da177e4SLinus Torvalds 	if (p) {
16011da177e4SLinus Torvalds 		p->tbl		  = tbl;
16026343944bSReshetova, Elena 		refcount_set(&p->refcnt, 1);
16031da177e4SLinus Torvalds 		p->reachable_time =
16041f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1605c7fb64dbSThomas Graf 		dev_hold(dev);
1606c7fb64dbSThomas Graf 		p->dev = dev;
1607efd7ef1cSEric W. Biederman 		write_pnet(&p->net, net);
16081da177e4SLinus Torvalds 		p->sysctl_table = NULL;
160963134803SVeaceslav Falico 
161063134803SVeaceslav Falico 		if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
161163134803SVeaceslav Falico 			dev_put(dev);
161263134803SVeaceslav Falico 			kfree(p);
161363134803SVeaceslav Falico 			return NULL;
161463134803SVeaceslav Falico 		}
161563134803SVeaceslav Falico 
16161da177e4SLinus Torvalds 		write_lock_bh(&tbl->lock);
161775fbfd33SNicolas Dichtel 		list_add(&p->list, &tbl->parms.list);
16181da177e4SLinus Torvalds 		write_unlock_bh(&tbl->lock);
16191d4c8c29SJiri Pirko 
16201d4c8c29SJiri Pirko 		neigh_parms_data_state_cleanall(p);
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds 	return p;
16231da177e4SLinus Torvalds }
16240a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head)
16271da177e4SLinus Torvalds {
16281da177e4SLinus Torvalds 	struct neigh_parms *parms =
16291da177e4SLinus Torvalds 		container_of(head, struct neigh_parms, rcu_head);
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	neigh_parms_put(parms);
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds 	if (!parms || parms == &tbl->parms)
16371da177e4SLinus Torvalds 		return;
16381da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
163975fbfd33SNicolas Dichtel 	list_del(&parms->list);
16401da177e4SLinus Torvalds 	parms->dead = 1;
16411da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1642cecbb639SDavid S. Miller 	if (parms->dev)
1643cecbb639SDavid S. Miller 		dev_put(parms->dev);
16441da177e4SLinus Torvalds 	call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
16451da177e4SLinus Torvalds }
16460a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release);
16471da177e4SLinus Torvalds 
164806f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms)
16491da177e4SLinus Torvalds {
16501da177e4SLinus Torvalds 	kfree(parms);
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
1653c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class;
1654c2ecba71SPavel Emelianov 
1655d7480fd3SWANG Cong static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1656d7480fd3SWANG Cong 
1657d7480fd3SWANG Cong void neigh_table_init(int index, struct neigh_table *tbl)
16581da177e4SLinus Torvalds {
16591da177e4SLinus Torvalds 	unsigned long now = jiffies;
16601da177e4SLinus Torvalds 	unsigned long phsize;
16611da177e4SLinus Torvalds 
166275fbfd33SNicolas Dichtel 	INIT_LIST_HEAD(&tbl->parms_list);
166358956317SDavid Ahern 	INIT_LIST_HEAD(&tbl->gc_list);
166475fbfd33SNicolas Dichtel 	list_add(&tbl->parms.list, &tbl->parms_list);
1665e42ea986SEric Dumazet 	write_pnet(&tbl->parms.net, &init_net);
16666343944bSReshetova, Elena 	refcount_set(&tbl->parms.refcnt, 1);
16671da177e4SLinus Torvalds 	tbl->parms.reachable_time =
16681f9248e5SJiri Pirko 			  neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	tbl->stats = alloc_percpu(struct neigh_statistics);
16711da177e4SLinus Torvalds 	if (!tbl->stats)
16721da177e4SLinus Torvalds 		panic("cannot create neighbour cache statistics");
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
167571a5053aSChristoph Hellwig 	if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
167671a5053aSChristoph Hellwig 			      &neigh_stat_seq_ops, tbl))
16771da177e4SLinus Torvalds 		panic("cannot create neighbour proc dir entry");
16781da177e4SLinus Torvalds #endif
16791da177e4SLinus Torvalds 
1680cd089336SDavid S. Miller 	RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
168377d04bd9SAndrew Morton 	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
16841da177e4SLinus Torvalds 
1685d6bf7817SEric Dumazet 	if (!tbl->nht || !tbl->phash_buckets)
16861da177e4SLinus Torvalds 		panic("cannot allocate neighbour cache hashes");
16871da177e4SLinus Torvalds 
168808433effSYOSHIFUJI Hideaki / 吉藤英明 	if (!tbl->entry_size)
168908433effSYOSHIFUJI Hideaki / 吉藤英明 		tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
169008433effSYOSHIFUJI Hideaki / 吉藤英明 					tbl->key_len, NEIGH_PRIV_ALIGN);
169108433effSYOSHIFUJI Hideaki / 吉藤英明 	else
169208433effSYOSHIFUJI Hideaki / 吉藤英明 		WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
169308433effSYOSHIFUJI Hideaki / 吉藤英明 
16941da177e4SLinus Torvalds 	rwlock_init(&tbl->lock);
1695203b42f7STejun Heo 	INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1696f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1697f618002bSviresh kumar 			tbl->parms.reachable_time);
1698e99e88a9SKees Cook 	timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
1699c2ecba71SPavel Emelianov 	skb_queue_head_init_class(&tbl->proxy_queue,
1700c2ecba71SPavel Emelianov 			&neigh_table_proxy_queue_class);
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	tbl->last_flush = now;
17031da177e4SLinus Torvalds 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1704bd89efc5SSimon Kelley 
1705d7480fd3SWANG Cong 	neigh_tables[index] = tbl;
17061da177e4SLinus Torvalds }
17070a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init);
17081da177e4SLinus Torvalds 
1709d7480fd3SWANG Cong int neigh_table_clear(int index, struct neigh_table *tbl)
17101da177e4SLinus Torvalds {
1711d7480fd3SWANG Cong 	neigh_tables[index] = NULL;
17121da177e4SLinus Torvalds 	/* It is not clean... Fix it to unload IPv6 module safely */
1713a5c30b34STejun Heo 	cancel_delayed_work_sync(&tbl->gc_work);
17141da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
17151da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
17161da177e4SLinus Torvalds 	neigh_ifdown(tbl, NULL);
17171da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries))
1718e005d193SJoe Perches 		pr_crit("neighbour leakage\n");
17191da177e4SLinus Torvalds 
17206193d2beSEric Dumazet 	call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
17216193d2beSEric Dumazet 		 neigh_hash_free_rcu);
1722d6bf7817SEric Dumazet 	tbl->nht = NULL;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	kfree(tbl->phash_buckets);
17251da177e4SLinus Torvalds 	tbl->phash_buckets = NULL;
17261da177e4SLinus Torvalds 
17273f192b5cSAlexey Dobriyan 	remove_proc_entry(tbl->id, init_net.proc_net_stat);
17283f192b5cSAlexey Dobriyan 
17293fcde74bSKirill Korotaev 	free_percpu(tbl->stats);
17303fcde74bSKirill Korotaev 	tbl->stats = NULL;
17313fcde74bSKirill Korotaev 
17321da177e4SLinus Torvalds 	return 0;
17331da177e4SLinus Torvalds }
17340a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear);
17351da177e4SLinus Torvalds 
1736d7480fd3SWANG Cong static struct neigh_table *neigh_find_table(int family)
1737d7480fd3SWANG Cong {
1738d7480fd3SWANG Cong 	struct neigh_table *tbl = NULL;
1739d7480fd3SWANG Cong 
1740d7480fd3SWANG Cong 	switch (family) {
1741d7480fd3SWANG Cong 	case AF_INET:
1742d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_ARP_TABLE];
1743d7480fd3SWANG Cong 		break;
1744d7480fd3SWANG Cong 	case AF_INET6:
1745d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_ND_TABLE];
1746d7480fd3SWANG Cong 		break;
1747d7480fd3SWANG Cong 	case AF_DECnet:
1748d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_DN_TABLE];
1749d7480fd3SWANG Cong 		break;
1750d7480fd3SWANG Cong 	}
1751d7480fd3SWANG Cong 
1752d7480fd3SWANG Cong 	return tbl;
1753d7480fd3SWANG Cong }
1754d7480fd3SWANG Cong 
175582cbb5c6SRoopa Prabhu const struct nla_policy nda_policy[NDA_MAX+1] = {
175682cbb5c6SRoopa Prabhu 	[NDA_DST]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
175782cbb5c6SRoopa Prabhu 	[NDA_LLADDR]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
175882cbb5c6SRoopa Prabhu 	[NDA_CACHEINFO]		= { .len = sizeof(struct nda_cacheinfo) },
175982cbb5c6SRoopa Prabhu 	[NDA_PROBES]		= { .type = NLA_U32 },
176082cbb5c6SRoopa Prabhu 	[NDA_VLAN]		= { .type = NLA_U16 },
176182cbb5c6SRoopa Prabhu 	[NDA_PORT]		= { .type = NLA_U16 },
176282cbb5c6SRoopa Prabhu 	[NDA_VNI]		= { .type = NLA_U32 },
176382cbb5c6SRoopa Prabhu 	[NDA_IFINDEX]		= { .type = NLA_U32 },
176482cbb5c6SRoopa Prabhu 	[NDA_MASTER]		= { .type = NLA_U32 },
1765a9cd3439SDavid Ahern 	[NDA_PROTOCOL]		= { .type = NLA_U8 },
176682cbb5c6SRoopa Prabhu };
176782cbb5c6SRoopa Prabhu 
1768c21ef3e3SDavid Ahern static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1769c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
17701da177e4SLinus Torvalds {
17713b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1772a14a49d2SThomas Graf 	struct ndmsg *ndm;
1773a14a49d2SThomas Graf 	struct nlattr *dst_attr;
17741da177e4SLinus Torvalds 	struct neigh_table *tbl;
1775d7480fd3SWANG Cong 	struct neighbour *neigh;
17761da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1777a14a49d2SThomas Graf 	int err = -EINVAL;
17781da177e4SLinus Torvalds 
1779110b2499SEric Dumazet 	ASSERT_RTNL();
1780a14a49d2SThomas Graf 	if (nlmsg_len(nlh) < sizeof(*ndm))
17811da177e4SLinus Torvalds 		goto out;
17821da177e4SLinus Torvalds 
1783a14a49d2SThomas Graf 	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
17847a35a50dSDavid Ahern 	if (!dst_attr) {
17857a35a50dSDavid Ahern 		NL_SET_ERR_MSG(extack, "Network address not specified");
1786a14a49d2SThomas Graf 		goto out;
17877a35a50dSDavid Ahern 	}
1788a14a49d2SThomas Graf 
1789a14a49d2SThomas Graf 	ndm = nlmsg_data(nlh);
1790a14a49d2SThomas Graf 	if (ndm->ndm_ifindex) {
1791110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1792a14a49d2SThomas Graf 		if (dev == NULL) {
1793a14a49d2SThomas Graf 			err = -ENODEV;
1794a14a49d2SThomas Graf 			goto out;
1795a14a49d2SThomas Graf 		}
1796a14a49d2SThomas Graf 	}
1797a14a49d2SThomas Graf 
1798d7480fd3SWANG Cong 	tbl = neigh_find_table(ndm->ndm_family);
1799d7480fd3SWANG Cong 	if (tbl == NULL)
1800d7480fd3SWANG Cong 		return -EAFNOSUPPORT;
18011da177e4SLinus Torvalds 
18027a35a50dSDavid Ahern 	if (nla_len(dst_attr) < (int)tbl->key_len) {
18037a35a50dSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid network address");
1804110b2499SEric Dumazet 		goto out;
18057a35a50dSDavid Ahern 	}
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	if (ndm->ndm_flags & NTF_PROXY) {
1808426b5303SEric W. Biederman 		err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1809110b2499SEric Dumazet 		goto out;
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
1812a14a49d2SThomas Graf 	if (dev == NULL)
1813110b2499SEric Dumazet 		goto out;
18141da177e4SLinus Torvalds 
1815a14a49d2SThomas Graf 	neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1816a14a49d2SThomas Graf 	if (neigh == NULL) {
1817a14a49d2SThomas Graf 		err = -ENOENT;
1818110b2499SEric Dumazet 		goto out;
1819a14a49d2SThomas Graf 	}
1820a14a49d2SThomas Graf 
18217a35a50dSDavid Ahern 	err = __neigh_update(neigh, NULL, NUD_FAILED,
18227a35a50dSDavid Ahern 			     NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN,
18237a35a50dSDavid Ahern 			     NETLINK_CB(skb).portid, extack);
18245071034eSSowmini Varadhan 	write_lock_bh(&tbl->lock);
1825a14a49d2SThomas Graf 	neigh_release(neigh);
18265071034eSSowmini Varadhan 	neigh_remove_one(neigh, tbl);
18275071034eSSowmini Varadhan 	write_unlock_bh(&tbl->lock);
1828a14a49d2SThomas Graf 
18291da177e4SLinus Torvalds out:
18301da177e4SLinus Torvalds 	return err;
18311da177e4SLinus Torvalds }
18321da177e4SLinus Torvalds 
1833c21ef3e3SDavid Ahern static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1834c21ef3e3SDavid Ahern 		     struct netlink_ext_ack *extack)
18351da177e4SLinus Torvalds {
1836f7aa74e4SRoopa Prabhu 	int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
1837f7aa74e4SRoopa Prabhu 		NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
18383b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
18395208debdSThomas Graf 	struct ndmsg *ndm;
18405208debdSThomas Graf 	struct nlattr *tb[NDA_MAX+1];
18411da177e4SLinus Torvalds 	struct neigh_table *tbl;
18421da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1843d7480fd3SWANG Cong 	struct neighbour *neigh;
1844d7480fd3SWANG Cong 	void *dst, *lladdr;
1845df9b0e30SDavid Ahern 	u8 protocol = 0;
18465208debdSThomas Graf 	int err;
18471da177e4SLinus Torvalds 
1848110b2499SEric Dumazet 	ASSERT_RTNL();
1849a9cd3439SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nda_policy, extack);
18505208debdSThomas Graf 	if (err < 0)
18511da177e4SLinus Torvalds 		goto out;
18521da177e4SLinus Torvalds 
18535208debdSThomas Graf 	err = -EINVAL;
18547a35a50dSDavid Ahern 	if (!tb[NDA_DST]) {
18557a35a50dSDavid Ahern 		NL_SET_ERR_MSG(extack, "Network address not specified");
18565208debdSThomas Graf 		goto out;
18577a35a50dSDavid Ahern 	}
18585208debdSThomas Graf 
18595208debdSThomas Graf 	ndm = nlmsg_data(nlh);
18605208debdSThomas Graf 	if (ndm->ndm_ifindex) {
1861110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
18625208debdSThomas Graf 		if (dev == NULL) {
18635208debdSThomas Graf 			err = -ENODEV;
18645208debdSThomas Graf 			goto out;
18655208debdSThomas Graf 		}
18665208debdSThomas Graf 
18677a35a50dSDavid Ahern 		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) {
18687a35a50dSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid link address");
1869110b2499SEric Dumazet 			goto out;
18705208debdSThomas Graf 		}
18717a35a50dSDavid Ahern 	}
18725208debdSThomas Graf 
1873d7480fd3SWANG Cong 	tbl = neigh_find_table(ndm->ndm_family);
1874d7480fd3SWANG Cong 	if (tbl == NULL)
1875d7480fd3SWANG Cong 		return -EAFNOSUPPORT;
18761da177e4SLinus Torvalds 
18777a35a50dSDavid Ahern 	if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) {
18787a35a50dSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid network address");
1879110b2499SEric Dumazet 		goto out;
18807a35a50dSDavid Ahern 	}
18817a35a50dSDavid Ahern 
18825208debdSThomas Graf 	dst = nla_data(tb[NDA_DST]);
18835208debdSThomas Graf 	lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
18841da177e4SLinus Torvalds 
1885a9cd3439SDavid Ahern 	if (tb[NDA_PROTOCOL])
1886df9b0e30SDavid Ahern 		protocol = nla_get_u8(tb[NDA_PROTOCOL]);
1887df9b0e30SDavid Ahern 
18881da177e4SLinus Torvalds 	if (ndm->ndm_flags & NTF_PROXY) {
188962dd9318SVille Nuorvala 		struct pneigh_entry *pn;
189062dd9318SVille Nuorvala 
18915208debdSThomas Graf 		err = -ENOBUFS;
1892426b5303SEric W. Biederman 		pn = pneigh_lookup(tbl, net, dst, dev, 1);
189362dd9318SVille Nuorvala 		if (pn) {
189462dd9318SVille Nuorvala 			pn->flags = ndm->ndm_flags;
1895df9b0e30SDavid Ahern 			if (protocol)
1896df9b0e30SDavid Ahern 				pn->protocol = protocol;
189762dd9318SVille Nuorvala 			err = 0;
189862dd9318SVille Nuorvala 		}
1899110b2499SEric Dumazet 		goto out;
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds 
19027a35a50dSDavid Ahern 	if (!dev) {
19037a35a50dSDavid Ahern 		NL_SET_ERR_MSG(extack, "Device not specified");
1904110b2499SEric Dumazet 		goto out;
19057a35a50dSDavid Ahern 	}
19061da177e4SLinus Torvalds 
19075208debdSThomas Graf 	neigh = neigh_lookup(tbl, dst, dev);
19085208debdSThomas Graf 	if (neigh == NULL) {
1909e997f8a2SDavid Ahern 		bool exempt_from_gc;
1910e997f8a2SDavid Ahern 
19115208debdSThomas Graf 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
19121da177e4SLinus Torvalds 			err = -ENOENT;
1913110b2499SEric Dumazet 			goto out;
19145208debdSThomas Graf 		}
19155208debdSThomas Graf 
1916e997f8a2SDavid Ahern 		exempt_from_gc = ndm->ndm_state & NUD_PERMANENT ||
1917e997f8a2SDavid Ahern 				 ndm->ndm_flags & NTF_EXT_LEARNED;
1918e997f8a2SDavid Ahern 		neigh = ___neigh_create(tbl, dst, dev, exempt_from_gc, true);
19195208debdSThomas Graf 		if (IS_ERR(neigh)) {
19205208debdSThomas Graf 			err = PTR_ERR(neigh);
1921110b2499SEric Dumazet 			goto out;
19221da177e4SLinus Torvalds 		}
19235208debdSThomas Graf 	} else {
19245208debdSThomas Graf 		if (nlh->nlmsg_flags & NLM_F_EXCL) {
19255208debdSThomas Graf 			err = -EEXIST;
19265208debdSThomas Graf 			neigh_release(neigh);
1927110b2499SEric Dumazet 			goto out;
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 
19305208debdSThomas Graf 		if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
1931f7aa74e4SRoopa Prabhu 			flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
1932f7aa74e4SRoopa Prabhu 				   NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
19335208debdSThomas Graf 	}
19341da177e4SLinus Torvalds 
19359ce33e46SRoopa Prabhu 	if (ndm->ndm_flags & NTF_EXT_LEARNED)
19369ce33e46SRoopa Prabhu 		flags |= NEIGH_UPDATE_F_EXT_LEARNED;
19379ce33e46SRoopa Prabhu 
1938f7aa74e4SRoopa Prabhu 	if (ndm->ndm_flags & NTF_ROUTER)
1939f7aa74e4SRoopa Prabhu 		flags |= NEIGH_UPDATE_F_ISROUTER;
1940f7aa74e4SRoopa Prabhu 
19410c5c2d30SEric Biederman 	if (ndm->ndm_flags & NTF_USE) {
19420c5c2d30SEric Biederman 		neigh_event_send(neigh, NULL);
19430c5c2d30SEric Biederman 		err = 0;
19440c5c2d30SEric Biederman 	} else
19457a35a50dSDavid Ahern 		err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags,
19467a35a50dSDavid Ahern 				     NETLINK_CB(skb).portid, extack);
1947df9b0e30SDavid Ahern 
1948df9b0e30SDavid Ahern 	if (protocol)
1949df9b0e30SDavid Ahern 		neigh->protocol = protocol;
1950df9b0e30SDavid Ahern 
19515208debdSThomas Graf 	neigh_release(neigh);
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds out:
19541da177e4SLinus Torvalds 	return err;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
1957c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1958c7fb64dbSThomas Graf {
1959ca860fb3SThomas Graf 	struct nlattr *nest;
1960e386c6ebSThomas Graf 
1961ca860fb3SThomas Graf 	nest = nla_nest_start(skb, NDTA_PARMS);
1962ca860fb3SThomas Graf 	if (nest == NULL)
1963ca860fb3SThomas Graf 		return -ENOBUFS;
1964c7fb64dbSThomas Graf 
19659a6308d7SDavid S. Miller 	if ((parms->dev &&
19669a6308d7SDavid S. Miller 	     nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
19676343944bSReshetova, Elena 	    nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
19681f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
19691f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
19708b5c171bSEric Dumazet 	    /* approximative value for deprecated QUEUE_LEN (in packets) */
19719a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_QUEUE_LEN,
19721f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
19731f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
19741f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
19751f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_UCAST_PROBES,
19761f9248e5SJiri Pirko 			NEIGH_VAR(parms, UCAST_PROBES)) ||
19771f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_MCAST_PROBES,
19781f9248e5SJiri Pirko 			NEIGH_VAR(parms, MCAST_PROBES)) ||
19798da86466SYOSHIFUJI Hideaki/吉藤英明 	    nla_put_u32(skb, NDTPA_MCAST_REPROBES,
19808da86466SYOSHIFUJI Hideaki/吉藤英明 			NEIGH_VAR(parms, MCAST_REPROBES)) ||
19812175d87cSNicolas Dichtel 	    nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
19822175d87cSNicolas Dichtel 			  NDTPA_PAD) ||
19839a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
19842175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
19851f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_GC_STALETIME,
19862175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
19879a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
19882175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
19891f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_RETRANS_TIME,
19902175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
19911f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
19922175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
19931f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_PROXY_DELAY,
19942175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
19951f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_LOCKTIME,
19962175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
19979a6308d7SDavid S. Miller 		goto nla_put_failure;
1998ca860fb3SThomas Graf 	return nla_nest_end(skb, nest);
1999c7fb64dbSThomas Graf 
2000ca860fb3SThomas Graf nla_put_failure:
2001bc3ed28cSThomas Graf 	nla_nest_cancel(skb, nest);
2002bc3ed28cSThomas Graf 	return -EMSGSIZE;
2003c7fb64dbSThomas Graf }
2004c7fb64dbSThomas Graf 
2005ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
2006ca860fb3SThomas Graf 			      u32 pid, u32 seq, int type, int flags)
2007c7fb64dbSThomas Graf {
2008c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
2009c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
2010c7fb64dbSThomas Graf 
2011ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2012ca860fb3SThomas Graf 	if (nlh == NULL)
201326932566SPatrick McHardy 		return -EMSGSIZE;
2014c7fb64dbSThomas Graf 
2015ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
2016c7fb64dbSThomas Graf 
2017c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
2018c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
20199ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
20209ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
2021c7fb64dbSThomas Graf 
20229a6308d7SDavid S. Miller 	if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
20232175d87cSNicolas Dichtel 	    nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
20249a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
20259a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
20269a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
20279a6308d7SDavid S. Miller 		goto nla_put_failure;
2028c7fb64dbSThomas Graf 	{
2029c7fb64dbSThomas Graf 		unsigned long now = jiffies;
2030c7fb64dbSThomas Graf 		unsigned int flush_delta = now - tbl->last_flush;
2031c7fb64dbSThomas Graf 		unsigned int rand_delta = now - tbl->last_rand;
2032d6bf7817SEric Dumazet 		struct neigh_hash_table *nht;
2033c7fb64dbSThomas Graf 		struct ndt_config ndc = {
2034c7fb64dbSThomas Graf 			.ndtc_key_len		= tbl->key_len,
2035c7fb64dbSThomas Graf 			.ndtc_entry_size	= tbl->entry_size,
2036c7fb64dbSThomas Graf 			.ndtc_entries		= atomic_read(&tbl->entries),
2037c7fb64dbSThomas Graf 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
2038c7fb64dbSThomas Graf 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
2039c7fb64dbSThomas Graf 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
2040c7fb64dbSThomas Graf 		};
2041c7fb64dbSThomas Graf 
2042d6bf7817SEric Dumazet 		rcu_read_lock_bh();
2043d6bf7817SEric Dumazet 		nht = rcu_dereference_bh(tbl->nht);
20442c2aba6cSDavid S. Miller 		ndc.ndtc_hash_rnd = nht->hash_rnd[0];
2045cd089336SDavid S. Miller 		ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
2046d6bf7817SEric Dumazet 		rcu_read_unlock_bh();
2047d6bf7817SEric Dumazet 
20489a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
20499a6308d7SDavid S. Miller 			goto nla_put_failure;
2050c7fb64dbSThomas Graf 	}
2051c7fb64dbSThomas Graf 
2052c7fb64dbSThomas Graf 	{
2053c7fb64dbSThomas Graf 		int cpu;
2054c7fb64dbSThomas Graf 		struct ndt_stats ndst;
2055c7fb64dbSThomas Graf 
2056c7fb64dbSThomas Graf 		memset(&ndst, 0, sizeof(ndst));
2057c7fb64dbSThomas Graf 
20586f912042SKAMEZAWA Hiroyuki 		for_each_possible_cpu(cpu) {
2059c7fb64dbSThomas Graf 			struct neigh_statistics	*st;
2060c7fb64dbSThomas Graf 
2061c7fb64dbSThomas Graf 			st = per_cpu_ptr(tbl->stats, cpu);
2062c7fb64dbSThomas Graf 			ndst.ndts_allocs		+= st->allocs;
2063c7fb64dbSThomas Graf 			ndst.ndts_destroys		+= st->destroys;
2064c7fb64dbSThomas Graf 			ndst.ndts_hash_grows		+= st->hash_grows;
2065c7fb64dbSThomas Graf 			ndst.ndts_res_failed		+= st->res_failed;
2066c7fb64dbSThomas Graf 			ndst.ndts_lookups		+= st->lookups;
2067c7fb64dbSThomas Graf 			ndst.ndts_hits			+= st->hits;
2068c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
2069c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
2070c7fb64dbSThomas Graf 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
2071c7fb64dbSThomas Graf 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
2072fb811395SRick Jones 			ndst.ndts_table_fulls		+= st->table_fulls;
2073c7fb64dbSThomas Graf 		}
2074c7fb64dbSThomas Graf 
2075b676338fSNicolas Dichtel 		if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
2076b676338fSNicolas Dichtel 				  NDTA_PAD))
20779a6308d7SDavid S. Miller 			goto nla_put_failure;
2078c7fb64dbSThomas Graf 	}
2079c7fb64dbSThomas Graf 
2080c7fb64dbSThomas Graf 	BUG_ON(tbl->parms.dev);
2081c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
2082ca860fb3SThomas Graf 		goto nla_put_failure;
2083c7fb64dbSThomas Graf 
2084c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
2085053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2086053c095aSJohannes Berg 	return 0;
2087c7fb64dbSThomas Graf 
2088ca860fb3SThomas Graf nla_put_failure:
2089c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
209026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
209126932566SPatrick McHardy 	return -EMSGSIZE;
2092c7fb64dbSThomas Graf }
2093c7fb64dbSThomas Graf 
2094ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb,
2095ca860fb3SThomas Graf 				    struct neigh_table *tbl,
2096c7fb64dbSThomas Graf 				    struct neigh_parms *parms,
2097ca860fb3SThomas Graf 				    u32 pid, u32 seq, int type,
2098ca860fb3SThomas Graf 				    unsigned int flags)
2099c7fb64dbSThomas Graf {
2100c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
2101c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
2102c7fb64dbSThomas Graf 
2103ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2104ca860fb3SThomas Graf 	if (nlh == NULL)
210526932566SPatrick McHardy 		return -EMSGSIZE;
2106c7fb64dbSThomas Graf 
2107ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
2108c7fb64dbSThomas Graf 
2109c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
2110c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
21119ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
21129ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
2113c7fb64dbSThomas Graf 
2114ca860fb3SThomas Graf 	if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
2115ca860fb3SThomas Graf 	    neightbl_fill_parms(skb, parms) < 0)
2116ca860fb3SThomas Graf 		goto errout;
2117c7fb64dbSThomas Graf 
2118c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
2119053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2120053c095aSJohannes Berg 	return 0;
2121ca860fb3SThomas Graf errout:
2122c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
212326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
212426932566SPatrick McHardy 	return -EMSGSIZE;
2125c7fb64dbSThomas Graf }
2126c7fb64dbSThomas Graf 
2127ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
21286b3f8674SThomas Graf 	[NDTA_NAME]		= { .type = NLA_STRING },
21296b3f8674SThomas Graf 	[NDTA_THRESH1]		= { .type = NLA_U32 },
21306b3f8674SThomas Graf 	[NDTA_THRESH2]		= { .type = NLA_U32 },
21316b3f8674SThomas Graf 	[NDTA_THRESH3]		= { .type = NLA_U32 },
21326b3f8674SThomas Graf 	[NDTA_GC_INTERVAL]	= { .type = NLA_U64 },
21336b3f8674SThomas Graf 	[NDTA_PARMS]		= { .type = NLA_NESTED },
21346b3f8674SThomas Graf };
21356b3f8674SThomas Graf 
2136ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
21376b3f8674SThomas Graf 	[NDTPA_IFINDEX]			= { .type = NLA_U32 },
21386b3f8674SThomas Graf 	[NDTPA_QUEUE_LEN]		= { .type = NLA_U32 },
21396b3f8674SThomas Graf 	[NDTPA_PROXY_QLEN]		= { .type = NLA_U32 },
21406b3f8674SThomas Graf 	[NDTPA_APP_PROBES]		= { .type = NLA_U32 },
21416b3f8674SThomas Graf 	[NDTPA_UCAST_PROBES]		= { .type = NLA_U32 },
21426b3f8674SThomas Graf 	[NDTPA_MCAST_PROBES]		= { .type = NLA_U32 },
21438da86466SYOSHIFUJI Hideaki/吉藤英明 	[NDTPA_MCAST_REPROBES]		= { .type = NLA_U32 },
21446b3f8674SThomas Graf 	[NDTPA_BASE_REACHABLE_TIME]	= { .type = NLA_U64 },
21456b3f8674SThomas Graf 	[NDTPA_GC_STALETIME]		= { .type = NLA_U64 },
21466b3f8674SThomas Graf 	[NDTPA_DELAY_PROBE_TIME]	= { .type = NLA_U64 },
21476b3f8674SThomas Graf 	[NDTPA_RETRANS_TIME]		= { .type = NLA_U64 },
21486b3f8674SThomas Graf 	[NDTPA_ANYCAST_DELAY]		= { .type = NLA_U64 },
21496b3f8674SThomas Graf 	[NDTPA_PROXY_DELAY]		= { .type = NLA_U64 },
21506b3f8674SThomas Graf 	[NDTPA_LOCKTIME]		= { .type = NLA_U64 },
21516b3f8674SThomas Graf };
21526b3f8674SThomas Graf 
2153c21ef3e3SDavid Ahern static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
2154c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2155c7fb64dbSThomas Graf {
21563b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2157c7fb64dbSThomas Graf 	struct neigh_table *tbl;
21586b3f8674SThomas Graf 	struct ndtmsg *ndtmsg;
21596b3f8674SThomas Graf 	struct nlattr *tb[NDTA_MAX+1];
2160d7480fd3SWANG Cong 	bool found = false;
2161d7480fd3SWANG Cong 	int err, tidx;
2162c7fb64dbSThomas Graf 
21636b3f8674SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
2164c21ef3e3SDavid Ahern 			  nl_neightbl_policy, extack);
21656b3f8674SThomas Graf 	if (err < 0)
21666b3f8674SThomas Graf 		goto errout;
2167c7fb64dbSThomas Graf 
21686b3f8674SThomas Graf 	if (tb[NDTA_NAME] == NULL) {
21696b3f8674SThomas Graf 		err = -EINVAL;
21706b3f8674SThomas Graf 		goto errout;
21716b3f8674SThomas Graf 	}
21726b3f8674SThomas Graf 
21736b3f8674SThomas Graf 	ndtmsg = nlmsg_data(nlh);
2174d7480fd3SWANG Cong 
2175d7480fd3SWANG Cong 	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2176d7480fd3SWANG Cong 		tbl = neigh_tables[tidx];
2177d7480fd3SWANG Cong 		if (!tbl)
2178d7480fd3SWANG Cong 			continue;
2179c7fb64dbSThomas Graf 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2180c7fb64dbSThomas Graf 			continue;
2181d7480fd3SWANG Cong 		if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2182d7480fd3SWANG Cong 			found = true;
2183c7fb64dbSThomas Graf 			break;
2184c7fb64dbSThomas Graf 		}
2185c7fb64dbSThomas Graf 	}
2186c7fb64dbSThomas Graf 
2187d7480fd3SWANG Cong 	if (!found)
2188d7480fd3SWANG Cong 		return -ENOENT;
2189d7480fd3SWANG Cong 
2190c7fb64dbSThomas Graf 	/*
2191c7fb64dbSThomas Graf 	 * We acquire tbl->lock to be nice to the periodic timers and
2192c7fb64dbSThomas Graf 	 * make sure they always see a consistent set of values.
2193c7fb64dbSThomas Graf 	 */
2194c7fb64dbSThomas Graf 	write_lock_bh(&tbl->lock);
2195c7fb64dbSThomas Graf 
21966b3f8674SThomas Graf 	if (tb[NDTA_PARMS]) {
21976b3f8674SThomas Graf 		struct nlattr *tbp[NDTPA_MAX+1];
2198c7fb64dbSThomas Graf 		struct neigh_parms *p;
21996b3f8674SThomas Graf 		int i, ifindex = 0;
2200c7fb64dbSThomas Graf 
22016b3f8674SThomas Graf 		err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
2202c21ef3e3SDavid Ahern 				       nl_ntbl_parm_policy, extack);
22036b3f8674SThomas Graf 		if (err < 0)
22046b3f8674SThomas Graf 			goto errout_tbl_lock;
2205c7fb64dbSThomas Graf 
22066b3f8674SThomas Graf 		if (tbp[NDTPA_IFINDEX])
22076b3f8674SThomas Graf 			ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2208c7fb64dbSThomas Graf 
220997fd5bc7STobias Klauser 		p = lookup_neigh_parms(tbl, net, ifindex);
2210c7fb64dbSThomas Graf 		if (p == NULL) {
2211c7fb64dbSThomas Graf 			err = -ENOENT;
22126b3f8674SThomas Graf 			goto errout_tbl_lock;
2213c7fb64dbSThomas Graf 		}
2214c7fb64dbSThomas Graf 
22156b3f8674SThomas Graf 		for (i = 1; i <= NDTPA_MAX; i++) {
22166b3f8674SThomas Graf 			if (tbp[i] == NULL)
22176b3f8674SThomas Graf 				continue;
2218c7fb64dbSThomas Graf 
22196b3f8674SThomas Graf 			switch (i) {
22206b3f8674SThomas Graf 			case NDTPA_QUEUE_LEN:
22211f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
22221f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]) *
22231f9248e5SJiri Pirko 					      SKB_TRUESIZE(ETH_FRAME_LEN));
22248b5c171bSEric Dumazet 				break;
22258b5c171bSEric Dumazet 			case NDTPA_QUEUE_LENBYTES:
22261f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
22271f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
22286b3f8674SThomas Graf 				break;
22296b3f8674SThomas Graf 			case NDTPA_PROXY_QLEN:
22301f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, PROXY_QLEN,
22311f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
22326b3f8674SThomas Graf 				break;
22336b3f8674SThomas Graf 			case NDTPA_APP_PROBES:
22341f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, APP_PROBES,
22351f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
22366b3f8674SThomas Graf 				break;
22376b3f8674SThomas Graf 			case NDTPA_UCAST_PROBES:
22381f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, UCAST_PROBES,
22391f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
22406b3f8674SThomas Graf 				break;
22416b3f8674SThomas Graf 			case NDTPA_MCAST_PROBES:
22421f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, MCAST_PROBES,
22431f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
22446b3f8674SThomas Graf 				break;
22458da86466SYOSHIFUJI Hideaki/吉藤英明 			case NDTPA_MCAST_REPROBES:
22468da86466SYOSHIFUJI Hideaki/吉藤英明 				NEIGH_VAR_SET(p, MCAST_REPROBES,
22478da86466SYOSHIFUJI Hideaki/吉藤英明 					      nla_get_u32(tbp[i]));
22488da86466SYOSHIFUJI Hideaki/吉藤英明 				break;
22496b3f8674SThomas Graf 			case NDTPA_BASE_REACHABLE_TIME:
22501f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
22511f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
22524bf6980dSJean-Francois Remy 				/* update reachable_time as well, otherwise, the change will
22534bf6980dSJean-Francois Remy 				 * only be effective after the next time neigh_periodic_work
22544bf6980dSJean-Francois Remy 				 * decides to recompute it (can be multiple minutes)
22554bf6980dSJean-Francois Remy 				 */
22564bf6980dSJean-Francois Remy 				p->reachable_time =
22574bf6980dSJean-Francois Remy 					neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
22586b3f8674SThomas Graf 				break;
22596b3f8674SThomas Graf 			case NDTPA_GC_STALETIME:
22601f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, GC_STALETIME,
22611f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
22626b3f8674SThomas Graf 				break;
22636b3f8674SThomas Graf 			case NDTPA_DELAY_PROBE_TIME:
22641f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
22651f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
22662a4501aeSIdo Schimmel 				call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
22676b3f8674SThomas Graf 				break;
22686b3f8674SThomas Graf 			case NDTPA_RETRANS_TIME:
22691f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, RETRANS_TIME,
22701f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
22716b3f8674SThomas Graf 				break;
22726b3f8674SThomas Graf 			case NDTPA_ANYCAST_DELAY:
22733977458cSJiri Pirko 				NEIGH_VAR_SET(p, ANYCAST_DELAY,
22743977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
22756b3f8674SThomas Graf 				break;
22766b3f8674SThomas Graf 			case NDTPA_PROXY_DELAY:
22773977458cSJiri Pirko 				NEIGH_VAR_SET(p, PROXY_DELAY,
22783977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
22796b3f8674SThomas Graf 				break;
22806b3f8674SThomas Graf 			case NDTPA_LOCKTIME:
22813977458cSJiri Pirko 				NEIGH_VAR_SET(p, LOCKTIME,
22823977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
22836b3f8674SThomas Graf 				break;
2284c7fb64dbSThomas Graf 			}
22856b3f8674SThomas Graf 		}
22866b3f8674SThomas Graf 	}
22876b3f8674SThomas Graf 
2288dc25c676SGao feng 	err = -ENOENT;
2289dc25c676SGao feng 	if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2290dc25c676SGao feng 	     tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2291dc25c676SGao feng 	    !net_eq(net, &init_net))
2292dc25c676SGao feng 		goto errout_tbl_lock;
2293dc25c676SGao feng 
22946b3f8674SThomas Graf 	if (tb[NDTA_THRESH1])
22956b3f8674SThomas Graf 		tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
22966b3f8674SThomas Graf 
22976b3f8674SThomas Graf 	if (tb[NDTA_THRESH2])
22986b3f8674SThomas Graf 		tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
22996b3f8674SThomas Graf 
23006b3f8674SThomas Graf 	if (tb[NDTA_THRESH3])
23016b3f8674SThomas Graf 		tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
23026b3f8674SThomas Graf 
23036b3f8674SThomas Graf 	if (tb[NDTA_GC_INTERVAL])
23046b3f8674SThomas Graf 		tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2305c7fb64dbSThomas Graf 
2306c7fb64dbSThomas Graf 	err = 0;
2307c7fb64dbSThomas Graf 
23086b3f8674SThomas Graf errout_tbl_lock:
2309c7fb64dbSThomas Graf 	write_unlock_bh(&tbl->lock);
23106b3f8674SThomas Graf errout:
2311c7fb64dbSThomas Graf 	return err;
2312c7fb64dbSThomas Graf }
2313c7fb64dbSThomas Graf 
23149632d47fSDavid Ahern static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
23159632d47fSDavid Ahern 				    struct netlink_ext_ack *extack)
23169632d47fSDavid Ahern {
23179632d47fSDavid Ahern 	struct ndtmsg *ndtm;
23189632d47fSDavid Ahern 
23199632d47fSDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndtm))) {
23209632d47fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
23219632d47fSDavid Ahern 		return -EINVAL;
23229632d47fSDavid Ahern 	}
23239632d47fSDavid Ahern 
23249632d47fSDavid Ahern 	ndtm = nlmsg_data(nlh);
23259632d47fSDavid Ahern 	if (ndtm->ndtm_pad1  || ndtm->ndtm_pad2) {
23269632d47fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
23279632d47fSDavid Ahern 		return -EINVAL;
23289632d47fSDavid Ahern 	}
23299632d47fSDavid Ahern 
23309632d47fSDavid Ahern 	if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
23319632d47fSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
23329632d47fSDavid Ahern 		return -EINVAL;
23339632d47fSDavid Ahern 	}
23349632d47fSDavid Ahern 
23359632d47fSDavid Ahern 	return 0;
23369632d47fSDavid Ahern }
23379632d47fSDavid Ahern 
2338c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2339c7fb64dbSThomas Graf {
23409632d47fSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
23413b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2342ca860fb3SThomas Graf 	int family, tidx, nidx = 0;
2343ca860fb3SThomas Graf 	int tbl_skip = cb->args[0];
2344ca860fb3SThomas Graf 	int neigh_skip = cb->args[1];
2345c7fb64dbSThomas Graf 	struct neigh_table *tbl;
2346c7fb64dbSThomas Graf 
23479632d47fSDavid Ahern 	if (cb->strict_check) {
23489632d47fSDavid Ahern 		int err = neightbl_valid_dump_info(nlh, cb->extack);
23499632d47fSDavid Ahern 
23509632d47fSDavid Ahern 		if (err < 0)
23519632d47fSDavid Ahern 			return err;
23529632d47fSDavid Ahern 	}
23539632d47fSDavid Ahern 
23549632d47fSDavid Ahern 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
2355c7fb64dbSThomas Graf 
2356d7480fd3SWANG Cong 	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2357c7fb64dbSThomas Graf 		struct neigh_parms *p;
2358c7fb64dbSThomas Graf 
2359d7480fd3SWANG Cong 		tbl = neigh_tables[tidx];
2360d7480fd3SWANG Cong 		if (!tbl)
2361d7480fd3SWANG Cong 			continue;
2362d7480fd3SWANG Cong 
2363ca860fb3SThomas Graf 		if (tidx < tbl_skip || (family && tbl->family != family))
2364c7fb64dbSThomas Graf 			continue;
2365c7fb64dbSThomas Graf 
236615e47304SEric W. Biederman 		if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
23679632d47fSDavid Ahern 				       nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
23687b46a644SDavid S. Miller 				       NLM_F_MULTI) < 0)
2369c7fb64dbSThomas Graf 			break;
2370c7fb64dbSThomas Graf 
237175fbfd33SNicolas Dichtel 		nidx = 0;
237275fbfd33SNicolas Dichtel 		p = list_next_entry(&tbl->parms, list);
237375fbfd33SNicolas Dichtel 		list_for_each_entry_from(p, &tbl->parms_list, list) {
2374878628fbSYOSHIFUJI Hideaki 			if (!net_eq(neigh_parms_net(p), net))
2375426b5303SEric W. Biederman 				continue;
2376426b5303SEric W. Biederman 
2377efc683fcSGautam Kachroo 			if (nidx < neigh_skip)
2378efc683fcSGautam Kachroo 				goto next;
2379c7fb64dbSThomas Graf 
2380ca860fb3SThomas Graf 			if (neightbl_fill_param_info(skb, tbl, p,
238115e47304SEric W. Biederman 						     NETLINK_CB(cb->skb).portid,
23829632d47fSDavid Ahern 						     nlh->nlmsg_seq,
2383ca860fb3SThomas Graf 						     RTM_NEWNEIGHTBL,
23847b46a644SDavid S. Miller 						     NLM_F_MULTI) < 0)
2385c7fb64dbSThomas Graf 				goto out;
2386efc683fcSGautam Kachroo 		next:
2387efc683fcSGautam Kachroo 			nidx++;
2388c7fb64dbSThomas Graf 		}
2389c7fb64dbSThomas Graf 
2390ca860fb3SThomas Graf 		neigh_skip = 0;
2391c7fb64dbSThomas Graf 	}
2392c7fb64dbSThomas Graf out:
2393ca860fb3SThomas Graf 	cb->args[0] = tidx;
2394ca860fb3SThomas Graf 	cb->args[1] = nidx;
2395c7fb64dbSThomas Graf 
2396c7fb64dbSThomas Graf 	return skb->len;
2397c7fb64dbSThomas Graf }
23981da177e4SLinus Torvalds 
23998b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
24008b8aec50SThomas Graf 			   u32 pid, u32 seq, int type, unsigned int flags)
24011da177e4SLinus Torvalds {
24021da177e4SLinus Torvalds 	unsigned long now = jiffies;
24031da177e4SLinus Torvalds 	struct nda_cacheinfo ci;
24048b8aec50SThomas Graf 	struct nlmsghdr *nlh;
24058b8aec50SThomas Graf 	struct ndmsg *ndm;
24061da177e4SLinus Torvalds 
24078b8aec50SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
24088b8aec50SThomas Graf 	if (nlh == NULL)
240926932566SPatrick McHardy 		return -EMSGSIZE;
24108b8aec50SThomas Graf 
24118b8aec50SThomas Graf 	ndm = nlmsg_data(nlh);
24128b8aec50SThomas Graf 	ndm->ndm_family	 = neigh->ops->family;
24139ef1d4c7SPatrick McHardy 	ndm->ndm_pad1    = 0;
24149ef1d4c7SPatrick McHardy 	ndm->ndm_pad2    = 0;
24158b8aec50SThomas Graf 	ndm->ndm_flags	 = neigh->flags;
24168b8aec50SThomas Graf 	ndm->ndm_type	 = neigh->type;
24178b8aec50SThomas Graf 	ndm->ndm_ifindex = neigh->dev->ifindex;
24181da177e4SLinus Torvalds 
24199a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
24209a6308d7SDavid S. Miller 		goto nla_put_failure;
24218b8aec50SThomas Graf 
24228b8aec50SThomas Graf 	read_lock_bh(&neigh->lock);
24238b8aec50SThomas Graf 	ndm->ndm_state	 = neigh->nud_state;
24240ed8ddf4SEric Dumazet 	if (neigh->nud_state & NUD_VALID) {
24250ed8ddf4SEric Dumazet 		char haddr[MAX_ADDR_LEN];
24260ed8ddf4SEric Dumazet 
24270ed8ddf4SEric Dumazet 		neigh_ha_snapshot(haddr, neigh, neigh->dev);
24280ed8ddf4SEric Dumazet 		if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
24298b8aec50SThomas Graf 			read_unlock_bh(&neigh->lock);
24308b8aec50SThomas Graf 			goto nla_put_failure;
24318b8aec50SThomas Graf 		}
24320ed8ddf4SEric Dumazet 	}
24338b8aec50SThomas Graf 
2434b9f5f52cSStephen Hemminger 	ci.ndm_used	 = jiffies_to_clock_t(now - neigh->used);
2435b9f5f52cSStephen Hemminger 	ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2436b9f5f52cSStephen Hemminger 	ci.ndm_updated	 = jiffies_to_clock_t(now - neigh->updated);
24379f237430SReshetova, Elena 	ci.ndm_refcnt	 = refcount_read(&neigh->refcnt) - 1;
24388b8aec50SThomas Graf 	read_unlock_bh(&neigh->lock);
24398b8aec50SThomas Graf 
24409a6308d7SDavid S. Miller 	if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
24419a6308d7SDavid S. Miller 	    nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
24429a6308d7SDavid S. Miller 		goto nla_put_failure;
24438b8aec50SThomas Graf 
2444df9b0e30SDavid Ahern 	if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol))
2445df9b0e30SDavid Ahern 		goto nla_put_failure;
2446df9b0e30SDavid Ahern 
2447053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2448053c095aSJohannes Berg 	return 0;
24498b8aec50SThomas Graf 
24508b8aec50SThomas Graf nla_put_failure:
245126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
245226932566SPatrick McHardy 	return -EMSGSIZE;
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds 
245584920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
245684920c14STony Zelenoff 			    u32 pid, u32 seq, int type, unsigned int flags,
245784920c14STony Zelenoff 			    struct neigh_table *tbl)
245884920c14STony Zelenoff {
245984920c14STony Zelenoff 	struct nlmsghdr *nlh;
246084920c14STony Zelenoff 	struct ndmsg *ndm;
246184920c14STony Zelenoff 
246284920c14STony Zelenoff 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
246384920c14STony Zelenoff 	if (nlh == NULL)
246484920c14STony Zelenoff 		return -EMSGSIZE;
246584920c14STony Zelenoff 
246684920c14STony Zelenoff 	ndm = nlmsg_data(nlh);
246784920c14STony Zelenoff 	ndm->ndm_family	 = tbl->family;
246884920c14STony Zelenoff 	ndm->ndm_pad1    = 0;
246984920c14STony Zelenoff 	ndm->ndm_pad2    = 0;
247084920c14STony Zelenoff 	ndm->ndm_flags	 = pn->flags | NTF_PROXY;
2471545469f7SJun Zhao 	ndm->ndm_type	 = RTN_UNICAST;
24726adc5fd6SKonstantin Khlebnikov 	ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
247384920c14STony Zelenoff 	ndm->ndm_state	 = NUD_NONE;
247484920c14STony Zelenoff 
24759a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
24769a6308d7SDavid S. Miller 		goto nla_put_failure;
247784920c14STony Zelenoff 
2478df9b0e30SDavid Ahern 	if (pn->protocol && nla_put_u8(skb, NDA_PROTOCOL, pn->protocol))
2479df9b0e30SDavid Ahern 		goto nla_put_failure;
2480df9b0e30SDavid Ahern 
2481053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2482053c095aSJohannes Berg 	return 0;
248384920c14STony Zelenoff 
248484920c14STony Zelenoff nla_put_failure:
248584920c14STony Zelenoff 	nlmsg_cancel(skb, nlh);
248684920c14STony Zelenoff 	return -EMSGSIZE;
248784920c14STony Zelenoff }
248884920c14STony Zelenoff 
24897b8f7a40SRoopa Prabhu static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
2490d961db35SThomas Graf {
2491d961db35SThomas Graf 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
24927b8f7a40SRoopa Prabhu 	__neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
2493d961db35SThomas Graf }
24941da177e4SLinus Torvalds 
249521fdd092SDavid Ahern static bool neigh_master_filtered(struct net_device *dev, int master_idx)
249621fdd092SDavid Ahern {
249721fdd092SDavid Ahern 	struct net_device *master;
249821fdd092SDavid Ahern 
249921fdd092SDavid Ahern 	if (!master_idx)
250021fdd092SDavid Ahern 		return false;
250121fdd092SDavid Ahern 
2502aab456dfSEric Dumazet 	master = dev ? netdev_master_upper_dev_get(dev) : NULL;
250321fdd092SDavid Ahern 	if (!master || master->ifindex != master_idx)
250421fdd092SDavid Ahern 		return true;
250521fdd092SDavid Ahern 
250621fdd092SDavid Ahern 	return false;
250721fdd092SDavid Ahern }
250821fdd092SDavid Ahern 
250916660f0bSDavid Ahern static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
251016660f0bSDavid Ahern {
2511aab456dfSEric Dumazet 	if (filter_idx && (!dev || dev->ifindex != filter_idx))
251216660f0bSDavid Ahern 		return true;
251316660f0bSDavid Ahern 
251416660f0bSDavid Ahern 	return false;
251516660f0bSDavid Ahern }
251616660f0bSDavid Ahern 
25176f52f80eSDavid Ahern struct neigh_dump_filter {
25186f52f80eSDavid Ahern 	int master_idx;
25196f52f80eSDavid Ahern 	int dev_idx;
25206f52f80eSDavid Ahern };
25216f52f80eSDavid Ahern 
25221da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
25236f52f80eSDavid Ahern 			    struct netlink_callback *cb,
25246f52f80eSDavid Ahern 			    struct neigh_dump_filter *filter)
25251da177e4SLinus Torvalds {
25263b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
25271da177e4SLinus Torvalds 	struct neighbour *n;
25281da177e4SLinus Torvalds 	int rc, h, s_h = cb->args[1];
25291da177e4SLinus Torvalds 	int idx, s_idx = idx = cb->args[2];
2530d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
253121fdd092SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
253221fdd092SDavid Ahern 
25336f52f80eSDavid Ahern 	if (filter->dev_idx || filter->master_idx)
253421fdd092SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
25351da177e4SLinus Torvalds 
2536d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2537d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2538d6bf7817SEric Dumazet 
25394bd6683bSEric Dumazet 	for (h = s_h; h < (1 << nht->hash_shift); h++) {
25401da177e4SLinus Torvalds 		if (h > s_h)
25411da177e4SLinus Torvalds 			s_idx = 0;
2542767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2543767e97e1SEric Dumazet 		     n != NULL;
2544767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next)) {
254518502acdSZhang Shengju 			if (idx < s_idx || !net_eq(dev_net(n->dev), net))
254618502acdSZhang Shengju 				goto next;
25476f52f80eSDavid Ahern 			if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
25486f52f80eSDavid Ahern 			    neigh_master_filtered(n->dev, filter->master_idx))
2549efc683fcSGautam Kachroo 				goto next;
255015e47304SEric W. Biederman 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
25511da177e4SLinus Torvalds 					    cb->nlh->nlmsg_seq,
2552b6544c0bSJamal Hadi Salim 					    RTM_NEWNEIGH,
255321fdd092SDavid Ahern 					    flags) < 0) {
25541da177e4SLinus Torvalds 				rc = -1;
25551da177e4SLinus Torvalds 				goto out;
25561da177e4SLinus Torvalds 			}
2557efc683fcSGautam Kachroo next:
2558efc683fcSGautam Kachroo 			idx++;
25591da177e4SLinus Torvalds 		}
25601da177e4SLinus Torvalds 	}
25611da177e4SLinus Torvalds 	rc = skb->len;
25621da177e4SLinus Torvalds out:
2563d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
25641da177e4SLinus Torvalds 	cb->args[1] = h;
25651da177e4SLinus Torvalds 	cb->args[2] = idx;
25661da177e4SLinus Torvalds 	return rc;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
256984920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
25706f52f80eSDavid Ahern 			     struct netlink_callback *cb,
25716f52f80eSDavid Ahern 			     struct neigh_dump_filter *filter)
257284920c14STony Zelenoff {
257384920c14STony Zelenoff 	struct pneigh_entry *n;
257484920c14STony Zelenoff 	struct net *net = sock_net(skb->sk);
257584920c14STony Zelenoff 	int rc, h, s_h = cb->args[3];
257684920c14STony Zelenoff 	int idx, s_idx = idx = cb->args[4];
25776f52f80eSDavid Ahern 	unsigned int flags = NLM_F_MULTI;
25786f52f80eSDavid Ahern 
25796f52f80eSDavid Ahern 	if (filter->dev_idx || filter->master_idx)
25806f52f80eSDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
258184920c14STony Zelenoff 
258284920c14STony Zelenoff 	read_lock_bh(&tbl->lock);
258384920c14STony Zelenoff 
25844bd6683bSEric Dumazet 	for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
258584920c14STony Zelenoff 		if (h > s_h)
258684920c14STony Zelenoff 			s_idx = 0;
258784920c14STony Zelenoff 		for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
258818502acdSZhang Shengju 			if (idx < s_idx || pneigh_net(n) != net)
258984920c14STony Zelenoff 				goto next;
25906f52f80eSDavid Ahern 			if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
25916f52f80eSDavid Ahern 			    neigh_master_filtered(n->dev, filter->master_idx))
25926f52f80eSDavid Ahern 				goto next;
259315e47304SEric W. Biederman 			if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
259484920c14STony Zelenoff 					    cb->nlh->nlmsg_seq,
25956f52f80eSDavid Ahern 					    RTM_NEWNEIGH, flags, tbl) < 0) {
259684920c14STony Zelenoff 				read_unlock_bh(&tbl->lock);
259784920c14STony Zelenoff 				rc = -1;
259884920c14STony Zelenoff 				goto out;
259984920c14STony Zelenoff 			}
260084920c14STony Zelenoff 		next:
260184920c14STony Zelenoff 			idx++;
260284920c14STony Zelenoff 		}
260384920c14STony Zelenoff 	}
260484920c14STony Zelenoff 
260584920c14STony Zelenoff 	read_unlock_bh(&tbl->lock);
260684920c14STony Zelenoff 	rc = skb->len;
260784920c14STony Zelenoff out:
260884920c14STony Zelenoff 	cb->args[3] = h;
260984920c14STony Zelenoff 	cb->args[4] = idx;
261084920c14STony Zelenoff 	return rc;
261184920c14STony Zelenoff 
261284920c14STony Zelenoff }
261384920c14STony Zelenoff 
261451183d23SDavid Ahern static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
261551183d23SDavid Ahern 				bool strict_check,
261651183d23SDavid Ahern 				struct neigh_dump_filter *filter,
261751183d23SDavid Ahern 				struct netlink_ext_ack *extack)
261851183d23SDavid Ahern {
261951183d23SDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
262051183d23SDavid Ahern 	int err, i;
262151183d23SDavid Ahern 
262251183d23SDavid Ahern 	if (strict_check) {
262351183d23SDavid Ahern 		struct ndmsg *ndm;
262451183d23SDavid Ahern 
262551183d23SDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
262651183d23SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
262751183d23SDavid Ahern 			return -EINVAL;
262851183d23SDavid Ahern 		}
262951183d23SDavid Ahern 
263051183d23SDavid Ahern 		ndm = nlmsg_data(nlh);
263151183d23SDavid Ahern 		if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_ifindex ||
263251183d23SDavid Ahern 		    ndm->ndm_state || ndm->ndm_flags || ndm->ndm_type) {
263351183d23SDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
263451183d23SDavid Ahern 			return -EINVAL;
263551183d23SDavid Ahern 		}
263651183d23SDavid Ahern 
263751183d23SDavid Ahern 		err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
2638a9cd3439SDavid Ahern 					 nda_policy, extack);
263951183d23SDavid Ahern 	} else {
264051183d23SDavid Ahern 		err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
2641a9cd3439SDavid Ahern 				  nda_policy, extack);
264251183d23SDavid Ahern 	}
264351183d23SDavid Ahern 	if (err < 0)
264451183d23SDavid Ahern 		return err;
264551183d23SDavid Ahern 
264651183d23SDavid Ahern 	for (i = 0; i <= NDA_MAX; ++i) {
264751183d23SDavid Ahern 		if (!tb[i])
264851183d23SDavid Ahern 			continue;
264951183d23SDavid Ahern 
265051183d23SDavid Ahern 		/* all new attributes should require strict_check */
265151183d23SDavid Ahern 		switch (i) {
265251183d23SDavid Ahern 		case NDA_IFINDEX:
265351183d23SDavid Ahern 			filter->dev_idx = nla_get_u32(tb[i]);
265451183d23SDavid Ahern 			break;
265551183d23SDavid Ahern 		case NDA_MASTER:
265651183d23SDavid Ahern 			filter->master_idx = nla_get_u32(tb[i]);
265751183d23SDavid Ahern 			break;
265851183d23SDavid Ahern 		default:
265951183d23SDavid Ahern 			if (strict_check) {
266051183d23SDavid Ahern 				NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
266151183d23SDavid Ahern 				return -EINVAL;
266251183d23SDavid Ahern 			}
266351183d23SDavid Ahern 		}
266451183d23SDavid Ahern 	}
266551183d23SDavid Ahern 
266651183d23SDavid Ahern 	return 0;
266751183d23SDavid Ahern }
266851183d23SDavid Ahern 
2669c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
26701da177e4SLinus Torvalds {
26716f52f80eSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
26726f52f80eSDavid Ahern 	struct neigh_dump_filter filter = {};
26731da177e4SLinus Torvalds 	struct neigh_table *tbl;
26741da177e4SLinus Torvalds 	int t, family, s_t;
267584920c14STony Zelenoff 	int proxy = 0;
26764bd6683bSEric Dumazet 	int err;
26771da177e4SLinus Torvalds 
26786f52f80eSDavid Ahern 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
267984920c14STony Zelenoff 
268084920c14STony Zelenoff 	/* check for full ndmsg structure presence, family member is
268184920c14STony Zelenoff 	 * the same for both structures
268284920c14STony Zelenoff 	 */
26836f52f80eSDavid Ahern 	if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
26846f52f80eSDavid Ahern 	    ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
268584920c14STony Zelenoff 		proxy = 1;
268684920c14STony Zelenoff 
268751183d23SDavid Ahern 	err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
268851183d23SDavid Ahern 	if (err < 0 && cb->strict_check)
268951183d23SDavid Ahern 		return err;
269051183d23SDavid Ahern 
26911da177e4SLinus Torvalds 	s_t = cb->args[0];
26921da177e4SLinus Torvalds 
2693d7480fd3SWANG Cong 	for (t = 0; t < NEIGH_NR_TABLES; t++) {
2694d7480fd3SWANG Cong 		tbl = neigh_tables[t];
2695d7480fd3SWANG Cong 
2696d7480fd3SWANG Cong 		if (!tbl)
2697d7480fd3SWANG Cong 			continue;
26981da177e4SLinus Torvalds 		if (t < s_t || (family && tbl->family != family))
26991da177e4SLinus Torvalds 			continue;
27001da177e4SLinus Torvalds 		if (t > s_t)
27011da177e4SLinus Torvalds 			memset(&cb->args[1], 0, sizeof(cb->args) -
27021da177e4SLinus Torvalds 						sizeof(cb->args[0]));
270384920c14STony Zelenoff 		if (proxy)
27046f52f80eSDavid Ahern 			err = pneigh_dump_table(tbl, skb, cb, &filter);
270584920c14STony Zelenoff 		else
27066f52f80eSDavid Ahern 			err = neigh_dump_table(tbl, skb, cb, &filter);
27074bd6683bSEric Dumazet 		if (err < 0)
27084bd6683bSEric Dumazet 			break;
27091da177e4SLinus Torvalds 	}
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	cb->args[0] = t;
27121da177e4SLinus Torvalds 	return skb->len;
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds 
271582cbb5c6SRoopa Prabhu static int neigh_valid_get_req(const struct nlmsghdr *nlh,
271682cbb5c6SRoopa Prabhu 			       struct neigh_table **tbl,
271782cbb5c6SRoopa Prabhu 			       void **dst, int *dev_idx, u8 *ndm_flags,
271882cbb5c6SRoopa Prabhu 			       struct netlink_ext_ack *extack)
271982cbb5c6SRoopa Prabhu {
272082cbb5c6SRoopa Prabhu 	struct nlattr *tb[NDA_MAX + 1];
272182cbb5c6SRoopa Prabhu 	struct ndmsg *ndm;
272282cbb5c6SRoopa Prabhu 	int err, i;
272382cbb5c6SRoopa Prabhu 
272482cbb5c6SRoopa Prabhu 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
272582cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid header for neighbor get request");
272682cbb5c6SRoopa Prabhu 		return -EINVAL;
272782cbb5c6SRoopa Prabhu 	}
272882cbb5c6SRoopa Prabhu 
272982cbb5c6SRoopa Prabhu 	ndm = nlmsg_data(nlh);
273082cbb5c6SRoopa Prabhu 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
273182cbb5c6SRoopa Prabhu 	    ndm->ndm_type) {
273282cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor get request");
273382cbb5c6SRoopa Prabhu 		return -EINVAL;
273482cbb5c6SRoopa Prabhu 	}
273582cbb5c6SRoopa Prabhu 
273682cbb5c6SRoopa Prabhu 	if (ndm->ndm_flags & ~NTF_PROXY) {
273782cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor get request");
273882cbb5c6SRoopa Prabhu 		return -EINVAL;
273982cbb5c6SRoopa Prabhu 	}
274082cbb5c6SRoopa Prabhu 
274182cbb5c6SRoopa Prabhu 	err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
274282cbb5c6SRoopa Prabhu 				 nda_policy, extack);
274382cbb5c6SRoopa Prabhu 	if (err < 0)
274482cbb5c6SRoopa Prabhu 		return err;
274582cbb5c6SRoopa Prabhu 
274682cbb5c6SRoopa Prabhu 	*ndm_flags = ndm->ndm_flags;
274782cbb5c6SRoopa Prabhu 	*dev_idx = ndm->ndm_ifindex;
274882cbb5c6SRoopa Prabhu 	*tbl = neigh_find_table(ndm->ndm_family);
274982cbb5c6SRoopa Prabhu 	if (*tbl == NULL) {
275082cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Unsupported family in header for neighbor get request");
275182cbb5c6SRoopa Prabhu 		return -EAFNOSUPPORT;
275282cbb5c6SRoopa Prabhu 	}
275382cbb5c6SRoopa Prabhu 
275482cbb5c6SRoopa Prabhu 	for (i = 0; i <= NDA_MAX; ++i) {
275582cbb5c6SRoopa Prabhu 		if (!tb[i])
275682cbb5c6SRoopa Prabhu 			continue;
275782cbb5c6SRoopa Prabhu 
275882cbb5c6SRoopa Prabhu 		switch (i) {
275982cbb5c6SRoopa Prabhu 		case NDA_DST:
276082cbb5c6SRoopa Prabhu 			if (nla_len(tb[i]) != (int)(*tbl)->key_len) {
276182cbb5c6SRoopa Prabhu 				NL_SET_ERR_MSG(extack, "Invalid network address in neighbor get request");
276282cbb5c6SRoopa Prabhu 				return -EINVAL;
276382cbb5c6SRoopa Prabhu 			}
276482cbb5c6SRoopa Prabhu 			*dst = nla_data(tb[i]);
276582cbb5c6SRoopa Prabhu 			break;
276682cbb5c6SRoopa Prabhu 		default:
276782cbb5c6SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor get request");
276882cbb5c6SRoopa Prabhu 			return -EINVAL;
276982cbb5c6SRoopa Prabhu 		}
277082cbb5c6SRoopa Prabhu 	}
277182cbb5c6SRoopa Prabhu 
277282cbb5c6SRoopa Prabhu 	return 0;
277382cbb5c6SRoopa Prabhu }
277482cbb5c6SRoopa Prabhu 
277582cbb5c6SRoopa Prabhu static inline size_t neigh_nlmsg_size(void)
277682cbb5c6SRoopa Prabhu {
277782cbb5c6SRoopa Prabhu 	return NLMSG_ALIGN(sizeof(struct ndmsg))
277882cbb5c6SRoopa Prabhu 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
277982cbb5c6SRoopa Prabhu 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
278082cbb5c6SRoopa Prabhu 	       + nla_total_size(sizeof(struct nda_cacheinfo))
278182cbb5c6SRoopa Prabhu 	       + nla_total_size(4)  /* NDA_PROBES */
278282cbb5c6SRoopa Prabhu 	       + nla_total_size(1); /* NDA_PROTOCOL */
278382cbb5c6SRoopa Prabhu }
278482cbb5c6SRoopa Prabhu 
278582cbb5c6SRoopa Prabhu static int neigh_get_reply(struct net *net, struct neighbour *neigh,
278682cbb5c6SRoopa Prabhu 			   u32 pid, u32 seq)
278782cbb5c6SRoopa Prabhu {
278882cbb5c6SRoopa Prabhu 	struct sk_buff *skb;
278982cbb5c6SRoopa Prabhu 	int err = 0;
279082cbb5c6SRoopa Prabhu 
279182cbb5c6SRoopa Prabhu 	skb = nlmsg_new(neigh_nlmsg_size(), GFP_KERNEL);
279282cbb5c6SRoopa Prabhu 	if (!skb)
279382cbb5c6SRoopa Prabhu 		return -ENOBUFS;
279482cbb5c6SRoopa Prabhu 
279582cbb5c6SRoopa Prabhu 	err = neigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0);
279682cbb5c6SRoopa Prabhu 	if (err) {
279782cbb5c6SRoopa Prabhu 		kfree_skb(skb);
279882cbb5c6SRoopa Prabhu 		goto errout;
279982cbb5c6SRoopa Prabhu 	}
280082cbb5c6SRoopa Prabhu 
280182cbb5c6SRoopa Prabhu 	err = rtnl_unicast(skb, net, pid);
280282cbb5c6SRoopa Prabhu errout:
280382cbb5c6SRoopa Prabhu 	return err;
280482cbb5c6SRoopa Prabhu }
280582cbb5c6SRoopa Prabhu 
280682cbb5c6SRoopa Prabhu static inline size_t pneigh_nlmsg_size(void)
280782cbb5c6SRoopa Prabhu {
280882cbb5c6SRoopa Prabhu 	return NLMSG_ALIGN(sizeof(struct ndmsg))
280982cbb5c6SRoopa Prabhu 	       + nla_total_size(MAX_ADDR_LEN); /* NDA_DST */
281082cbb5c6SRoopa Prabhu 	       + nla_total_size(1); /* NDA_PROTOCOL */
281182cbb5c6SRoopa Prabhu }
281282cbb5c6SRoopa Prabhu 
281382cbb5c6SRoopa Prabhu static int pneigh_get_reply(struct net *net, struct pneigh_entry *neigh,
281482cbb5c6SRoopa Prabhu 			    u32 pid, u32 seq, struct neigh_table *tbl)
281582cbb5c6SRoopa Prabhu {
281682cbb5c6SRoopa Prabhu 	struct sk_buff *skb;
281782cbb5c6SRoopa Prabhu 	int err = 0;
281882cbb5c6SRoopa Prabhu 
281982cbb5c6SRoopa Prabhu 	skb = nlmsg_new(pneigh_nlmsg_size(), GFP_KERNEL);
282082cbb5c6SRoopa Prabhu 	if (!skb)
282182cbb5c6SRoopa Prabhu 		return -ENOBUFS;
282282cbb5c6SRoopa Prabhu 
282382cbb5c6SRoopa Prabhu 	err = pneigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0, tbl);
282482cbb5c6SRoopa Prabhu 	if (err) {
282582cbb5c6SRoopa Prabhu 		kfree_skb(skb);
282682cbb5c6SRoopa Prabhu 		goto errout;
282782cbb5c6SRoopa Prabhu 	}
282882cbb5c6SRoopa Prabhu 
282982cbb5c6SRoopa Prabhu 	err = rtnl_unicast(skb, net, pid);
283082cbb5c6SRoopa Prabhu errout:
283182cbb5c6SRoopa Prabhu 	return err;
283282cbb5c6SRoopa Prabhu }
283382cbb5c6SRoopa Prabhu 
283482cbb5c6SRoopa Prabhu static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
283582cbb5c6SRoopa Prabhu 		     struct netlink_ext_ack *extack)
283682cbb5c6SRoopa Prabhu {
283782cbb5c6SRoopa Prabhu 	struct net *net = sock_net(in_skb->sk);
283882cbb5c6SRoopa Prabhu 	struct net_device *dev = NULL;
283982cbb5c6SRoopa Prabhu 	struct neigh_table *tbl = NULL;
284082cbb5c6SRoopa Prabhu 	struct neighbour *neigh;
284182cbb5c6SRoopa Prabhu 	void *dst = NULL;
284282cbb5c6SRoopa Prabhu 	u8 ndm_flags = 0;
284382cbb5c6SRoopa Prabhu 	int dev_idx = 0;
284482cbb5c6SRoopa Prabhu 	int err;
284582cbb5c6SRoopa Prabhu 
284682cbb5c6SRoopa Prabhu 	err = neigh_valid_get_req(nlh, &tbl, &dst, &dev_idx, &ndm_flags,
284782cbb5c6SRoopa Prabhu 				  extack);
284882cbb5c6SRoopa Prabhu 	if (err < 0)
284982cbb5c6SRoopa Prabhu 		return err;
285082cbb5c6SRoopa Prabhu 
285182cbb5c6SRoopa Prabhu 	if (dev_idx) {
285282cbb5c6SRoopa Prabhu 		dev = __dev_get_by_index(net, dev_idx);
285382cbb5c6SRoopa Prabhu 		if (!dev) {
285482cbb5c6SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
285582cbb5c6SRoopa Prabhu 			return -ENODEV;
285682cbb5c6SRoopa Prabhu 		}
285782cbb5c6SRoopa Prabhu 	}
285882cbb5c6SRoopa Prabhu 
285982cbb5c6SRoopa Prabhu 	if (!dst) {
286082cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Network address not specified");
286182cbb5c6SRoopa Prabhu 		return -EINVAL;
286282cbb5c6SRoopa Prabhu 	}
286382cbb5c6SRoopa Prabhu 
286482cbb5c6SRoopa Prabhu 	if (ndm_flags & NTF_PROXY) {
286582cbb5c6SRoopa Prabhu 		struct pneigh_entry *pn;
286682cbb5c6SRoopa Prabhu 
286782cbb5c6SRoopa Prabhu 		pn = pneigh_lookup(tbl, net, dst, dev, 0);
286882cbb5c6SRoopa Prabhu 		if (!pn) {
286982cbb5c6SRoopa Prabhu 			NL_SET_ERR_MSG(extack, "Proxy neighbour entry not found");
287082cbb5c6SRoopa Prabhu 			return -ENOENT;
287182cbb5c6SRoopa Prabhu 		}
287282cbb5c6SRoopa Prabhu 		return pneigh_get_reply(net, pn, NETLINK_CB(in_skb).portid,
287382cbb5c6SRoopa Prabhu 					nlh->nlmsg_seq, tbl);
287482cbb5c6SRoopa Prabhu 	}
287582cbb5c6SRoopa Prabhu 
287682cbb5c6SRoopa Prabhu 	if (!dev) {
287782cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "No device specified");
287882cbb5c6SRoopa Prabhu 		return -EINVAL;
287982cbb5c6SRoopa Prabhu 	}
288082cbb5c6SRoopa Prabhu 
288182cbb5c6SRoopa Prabhu 	neigh = neigh_lookup(tbl, dst, dev);
288282cbb5c6SRoopa Prabhu 	if (!neigh) {
288382cbb5c6SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Neighbour entry not found");
288482cbb5c6SRoopa Prabhu 		return -ENOENT;
288582cbb5c6SRoopa Prabhu 	}
288682cbb5c6SRoopa Prabhu 
288782cbb5c6SRoopa Prabhu 	err = neigh_get_reply(net, neigh, NETLINK_CB(in_skb).portid,
288882cbb5c6SRoopa Prabhu 			      nlh->nlmsg_seq);
288982cbb5c6SRoopa Prabhu 
289082cbb5c6SRoopa Prabhu 	neigh_release(neigh);
289182cbb5c6SRoopa Prabhu 
289282cbb5c6SRoopa Prabhu 	return err;
289382cbb5c6SRoopa Prabhu }
289482cbb5c6SRoopa Prabhu 
28951da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
28961da177e4SLinus Torvalds {
28971da177e4SLinus Torvalds 	int chain;
2898d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
28991da177e4SLinus Torvalds 
2900d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2901d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2902d6bf7817SEric Dumazet 
2903767e97e1SEric Dumazet 	read_lock(&tbl->lock); /* avoid resizes */
2904cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
29051da177e4SLinus Torvalds 		struct neighbour *n;
29061da177e4SLinus Torvalds 
2907767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2908767e97e1SEric Dumazet 		     n != NULL;
2909767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next))
29101da177e4SLinus Torvalds 			cb(n, cookie);
29111da177e4SLinus Torvalds 	}
2912d6bf7817SEric Dumazet 	read_unlock(&tbl->lock);
2913d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
29141da177e4SLinus Torvalds }
29151da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each);
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */
29181da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl,
29191da177e4SLinus Torvalds 			      int (*cb)(struct neighbour *))
29201da177e4SLinus Torvalds {
29211da177e4SLinus Torvalds 	int chain;
2922d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
29231da177e4SLinus Torvalds 
2924d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
2925d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
2926cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2927767e97e1SEric Dumazet 		struct neighbour *n;
2928767e97e1SEric Dumazet 		struct neighbour __rcu **np;
29291da177e4SLinus Torvalds 
2930d6bf7817SEric Dumazet 		np = &nht->hash_buckets[chain];
2931767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
2932767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
29331da177e4SLinus Torvalds 			int release;
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 			write_lock(&n->lock);
29361da177e4SLinus Torvalds 			release = cb(n);
29371da177e4SLinus Torvalds 			if (release) {
2938767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
2939767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
2940767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
294158956317SDavid Ahern 				neigh_mark_dead(n);
29421da177e4SLinus Torvalds 			} else
29431da177e4SLinus Torvalds 				np = &n->next;
29441da177e4SLinus Torvalds 			write_unlock(&n->lock);
29454f494554SThomas Graf 			if (release)
29464f494554SThomas Graf 				neigh_cleanup_and_release(n);
29471da177e4SLinus Torvalds 		}
29481da177e4SLinus Torvalds 	}
2949ecbb4169SAlexey Kuznetsov }
29501da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release);
29511da177e4SLinus Torvalds 
2952b79bda3dSEric W. Biederman int neigh_xmit(int index, struct net_device *dev,
29534fd3d7d9SEric W. Biederman 	       const void *addr, struct sk_buff *skb)
29544fd3d7d9SEric W. Biederman {
2955b79bda3dSEric W. Biederman 	int err = -EAFNOSUPPORT;
2956b79bda3dSEric W. Biederman 	if (likely(index < NEIGH_NR_TABLES)) {
29574fd3d7d9SEric W. Biederman 		struct neigh_table *tbl;
29584fd3d7d9SEric W. Biederman 		struct neighbour *neigh;
29594fd3d7d9SEric W. Biederman 
2960b79bda3dSEric W. Biederman 		tbl = neigh_tables[index];
29614fd3d7d9SEric W. Biederman 		if (!tbl)
29624fd3d7d9SEric W. Biederman 			goto out;
2963b560f03dSDavid Barroso 		rcu_read_lock_bh();
29644fd3d7d9SEric W. Biederman 		neigh = __neigh_lookup_noref(tbl, addr, dev);
29654fd3d7d9SEric W. Biederman 		if (!neigh)
29664fd3d7d9SEric W. Biederman 			neigh = __neigh_create(tbl, addr, dev, false);
29674fd3d7d9SEric W. Biederman 		err = PTR_ERR(neigh);
2968b560f03dSDavid Barroso 		if (IS_ERR(neigh)) {
2969b560f03dSDavid Barroso 			rcu_read_unlock_bh();
29704fd3d7d9SEric W. Biederman 			goto out_kfree_skb;
2971b560f03dSDavid Barroso 		}
29724fd3d7d9SEric W. Biederman 		err = neigh->output(neigh, skb);
2973b560f03dSDavid Barroso 		rcu_read_unlock_bh();
29744fd3d7d9SEric W. Biederman 	}
2975b79bda3dSEric W. Biederman 	else if (index == NEIGH_LINK_TABLE) {
2976b79bda3dSEric W. Biederman 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2977b79bda3dSEric W. Biederman 				      addr, NULL, skb->len);
2978b79bda3dSEric W. Biederman 		if (err < 0)
2979b79bda3dSEric W. Biederman 			goto out_kfree_skb;
2980b79bda3dSEric W. Biederman 		err = dev_queue_xmit(skb);
2981b79bda3dSEric W. Biederman 	}
29824fd3d7d9SEric W. Biederman out:
29834fd3d7d9SEric W. Biederman 	return err;
29844fd3d7d9SEric W. Biederman out_kfree_skb:
29854fd3d7d9SEric W. Biederman 	kfree_skb(skb);
29864fd3d7d9SEric W. Biederman 	goto out;
29874fd3d7d9SEric W. Biederman }
29884fd3d7d9SEric W. Biederman EXPORT_SYMBOL(neigh_xmit);
29894fd3d7d9SEric W. Biederman 
29901da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq)
29931da177e4SLinus Torvalds {
29941da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
29951218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2996d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
29971da177e4SLinus Torvalds 	struct neighbour *n = NULL;
29981da177e4SLinus Torvalds 	int bucket = state->bucket;
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
3001cd089336SDavid S. Miller 	for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
3002767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[bucket]);
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 		while (n) {
3005878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
3006426b5303SEric W. Biederman 				goto next;
30071da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
30081da177e4SLinus Torvalds 				loff_t fakep = 0;
30091da177e4SLinus Torvalds 				void *v;
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds 				v = state->neigh_sub_iter(state, n, &fakep);
30121da177e4SLinus Torvalds 				if (!v)
30131da177e4SLinus Torvalds 					goto next;
30141da177e4SLinus Torvalds 			}
30151da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
30161da177e4SLinus Torvalds 				break;
30171da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
30181da177e4SLinus Torvalds 				break;
30191da177e4SLinus Torvalds next:
3020767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
30211da177e4SLinus Torvalds 		}
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 		if (n)
30241da177e4SLinus Torvalds 			break;
30251da177e4SLinus Torvalds 	}
30261da177e4SLinus Torvalds 	state->bucket = bucket;
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds 	return n;
30291da177e4SLinus Torvalds }
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq,
30321da177e4SLinus Torvalds 					struct neighbour *n,
30331da177e4SLinus Torvalds 					loff_t *pos)
30341da177e4SLinus Torvalds {
30351da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
30361218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
3037d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	if (state->neigh_sub_iter) {
30401da177e4SLinus Torvalds 		void *v = state->neigh_sub_iter(state, n, pos);
30411da177e4SLinus Torvalds 		if (v)
30421da177e4SLinus Torvalds 			return n;
30431da177e4SLinus Torvalds 	}
3044767e97e1SEric Dumazet 	n = rcu_dereference_bh(n->next);
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds 	while (1) {
30471da177e4SLinus Torvalds 		while (n) {
3048878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
3049426b5303SEric W. Biederman 				goto next;
30501da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
30511da177e4SLinus Torvalds 				void *v = state->neigh_sub_iter(state, n, pos);
30521da177e4SLinus Torvalds 				if (v)
30531da177e4SLinus Torvalds 					return n;
30541da177e4SLinus Torvalds 				goto next;
30551da177e4SLinus Torvalds 			}
30561da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
30571da177e4SLinus Torvalds 				break;
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
30601da177e4SLinus Torvalds 				break;
30611da177e4SLinus Torvalds next:
3062767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
30631da177e4SLinus Torvalds 		}
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 		if (n)
30661da177e4SLinus Torvalds 			break;
30671da177e4SLinus Torvalds 
3068cd089336SDavid S. Miller 		if (++state->bucket >= (1 << nht->hash_shift))
30691da177e4SLinus Torvalds 			break;
30701da177e4SLinus Torvalds 
3071767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
30721da177e4SLinus Torvalds 	}
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 	if (n && pos)
30751da177e4SLinus Torvalds 		--(*pos);
30761da177e4SLinus Torvalds 	return n;
30771da177e4SLinus Torvalds }
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
30801da177e4SLinus Torvalds {
30811da177e4SLinus Torvalds 	struct neighbour *n = neigh_get_first(seq);
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds 	if (n) {
3084745e2031SChris Larson 		--(*pos);
30851da177e4SLinus Torvalds 		while (*pos) {
30861da177e4SLinus Torvalds 			n = neigh_get_next(seq, n, pos);
30871da177e4SLinus Torvalds 			if (!n)
30881da177e4SLinus Torvalds 				break;
30891da177e4SLinus Torvalds 		}
30901da177e4SLinus Torvalds 	}
30911da177e4SLinus Torvalds 	return *pos ? NULL : n;
30921da177e4SLinus Torvalds }
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
30951da177e4SLinus Torvalds {
30961da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
30971218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
30981da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
30991da177e4SLinus Torvalds 	struct pneigh_entry *pn = NULL;
31001da177e4SLinus Torvalds 	int bucket = state->bucket;
31011da177e4SLinus Torvalds 
31021da177e4SLinus Torvalds 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
31031da177e4SLinus Torvalds 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
31041da177e4SLinus Torvalds 		pn = tbl->phash_buckets[bucket];
3105878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
3106426b5303SEric W. Biederman 			pn = pn->next;
31071da177e4SLinus Torvalds 		if (pn)
31081da177e4SLinus Torvalds 			break;
31091da177e4SLinus Torvalds 	}
31101da177e4SLinus Torvalds 	state->bucket = bucket;
31111da177e4SLinus Torvalds 
31121da177e4SLinus Torvalds 	return pn;
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
31161da177e4SLinus Torvalds 					    struct pneigh_entry *pn,
31171da177e4SLinus Torvalds 					    loff_t *pos)
31181da177e4SLinus Torvalds {
31191da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
31201218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
31211da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
31221da177e4SLinus Torvalds 
3123df07a94cSJorge Boncompte [DTI2] 	do {
31241da177e4SLinus Torvalds 		pn = pn->next;
3125df07a94cSJorge Boncompte [DTI2] 	} while (pn && !net_eq(pneigh_net(pn), net));
3126df07a94cSJorge Boncompte [DTI2] 
31271da177e4SLinus Torvalds 	while (!pn) {
31281da177e4SLinus Torvalds 		if (++state->bucket > PNEIGH_HASHMASK)
31291da177e4SLinus Torvalds 			break;
31301da177e4SLinus Torvalds 		pn = tbl->phash_buckets[state->bucket];
3131878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
3132426b5303SEric W. Biederman 			pn = pn->next;
31331da177e4SLinus Torvalds 		if (pn)
31341da177e4SLinus Torvalds 			break;
31351da177e4SLinus Torvalds 	}
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds 	if (pn && pos)
31381da177e4SLinus Torvalds 		--(*pos);
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 	return pn;
31411da177e4SLinus Torvalds }
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
31441da177e4SLinus Torvalds {
31451da177e4SLinus Torvalds 	struct pneigh_entry *pn = pneigh_get_first(seq);
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	if (pn) {
3148745e2031SChris Larson 		--(*pos);
31491da177e4SLinus Torvalds 		while (*pos) {
31501da177e4SLinus Torvalds 			pn = pneigh_get_next(seq, pn, pos);
31511da177e4SLinus Torvalds 			if (!pn)
31521da177e4SLinus Torvalds 				break;
31531da177e4SLinus Torvalds 		}
31541da177e4SLinus Torvalds 	}
31551da177e4SLinus Torvalds 	return *pos ? NULL : pn;
31561da177e4SLinus Torvalds }
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
31591da177e4SLinus Torvalds {
31601da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
31611da177e4SLinus Torvalds 	void *rc;
3162745e2031SChris Larson 	loff_t idxpos = *pos;
31631da177e4SLinus Torvalds 
3164745e2031SChris Larson 	rc = neigh_get_idx(seq, &idxpos);
31651da177e4SLinus Torvalds 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3166745e2031SChris Larson 		rc = pneigh_get_idx(seq, &idxpos);
31671da177e4SLinus Torvalds 
31681da177e4SLinus Torvalds 	return rc;
31691da177e4SLinus Torvalds }
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
3172d6bf7817SEric Dumazet 	__acquires(rcu_bh)
31731da177e4SLinus Torvalds {
31741da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds 	state->tbl = tbl;
31771da177e4SLinus Torvalds 	state->bucket = 0;
31781da177e4SLinus Torvalds 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
31791da177e4SLinus Torvalds 
3180d6bf7817SEric Dumazet 	rcu_read_lock_bh();
3181d6bf7817SEric Dumazet 	state->nht = rcu_dereference_bh(tbl->nht);
3182767e97e1SEric Dumazet 
3183745e2031SChris Larson 	return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
31841da177e4SLinus Torvalds }
31851da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start);
31861da177e4SLinus Torvalds 
31871da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
31881da177e4SLinus Torvalds {
31891da177e4SLinus Torvalds 	struct neigh_seq_state *state;
31901da177e4SLinus Torvalds 	void *rc;
31911da177e4SLinus Torvalds 
31921da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
3193bff69732SChris Larson 		rc = neigh_get_first(seq);
31941da177e4SLinus Torvalds 		goto out;
31951da177e4SLinus Torvalds 	}
31961da177e4SLinus Torvalds 
31971da177e4SLinus Torvalds 	state = seq->private;
31981da177e4SLinus Torvalds 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
31991da177e4SLinus Torvalds 		rc = neigh_get_next(seq, v, NULL);
32001da177e4SLinus Torvalds 		if (rc)
32011da177e4SLinus Torvalds 			goto out;
32021da177e4SLinus Torvalds 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
32031da177e4SLinus Torvalds 			rc = pneigh_get_first(seq);
32041da177e4SLinus Torvalds 	} else {
32051da177e4SLinus Torvalds 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
32061da177e4SLinus Torvalds 		rc = pneigh_get_next(seq, v, NULL);
32071da177e4SLinus Torvalds 	}
32081da177e4SLinus Torvalds out:
32091da177e4SLinus Torvalds 	++(*pos);
32101da177e4SLinus Torvalds 	return rc;
32111da177e4SLinus Torvalds }
32121da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next);
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v)
3215d6bf7817SEric Dumazet 	__releases(rcu_bh)
32161da177e4SLinus Torvalds {
3217d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
32181da177e4SLinus Torvalds }
32191da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop);
32201da177e4SLinus Torvalds 
32211da177e4SLinus Torvalds /* statistics via seq_file */
32221da177e4SLinus Torvalds 
32231da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
32241da177e4SLinus Torvalds {
322571a5053aSChristoph Hellwig 	struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
32261da177e4SLinus Torvalds 	int cpu;
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 	if (*pos == 0)
32291da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
32301da177e4SLinus Torvalds 
32310f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
32321da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
32331da177e4SLinus Torvalds 			continue;
32341da177e4SLinus Torvalds 		*pos = cpu+1;
32351da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
32361da177e4SLinus Torvalds 	}
32371da177e4SLinus Torvalds 	return NULL;
32381da177e4SLinus Torvalds }
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
32411da177e4SLinus Torvalds {
324271a5053aSChristoph Hellwig 	struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
32431da177e4SLinus Torvalds 	int cpu;
32441da177e4SLinus Torvalds 
32450f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
32461da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
32471da177e4SLinus Torvalds 			continue;
32481da177e4SLinus Torvalds 		*pos = cpu+1;
32491da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
32501da177e4SLinus Torvalds 	}
32511da177e4SLinus Torvalds 	return NULL;
32521da177e4SLinus Torvalds }
32531da177e4SLinus Torvalds 
32541da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
32551da177e4SLinus Torvalds {
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds }
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v)
32601da177e4SLinus Torvalds {
326171a5053aSChristoph Hellwig 	struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
32621da177e4SLinus Torvalds 	struct neigh_statistics *st = v;
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
3265fb811395SRick 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");
32661da177e4SLinus Torvalds 		return 0;
32671da177e4SLinus Torvalds 	}
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
3270fb811395SRick Jones 			"%08lx %08lx  %08lx %08lx %08lx %08lx\n",
32711da177e4SLinus Torvalds 		   atomic_read(&tbl->entries),
32721da177e4SLinus Torvalds 
32731da177e4SLinus Torvalds 		   st->allocs,
32741da177e4SLinus Torvalds 		   st->destroys,
32751da177e4SLinus Torvalds 		   st->hash_grows,
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds 		   st->lookups,
32781da177e4SLinus Torvalds 		   st->hits,
32791da177e4SLinus Torvalds 
32801da177e4SLinus Torvalds 		   st->res_failed,
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 		   st->rcv_probes_mcast,
32831da177e4SLinus Torvalds 		   st->rcv_probes_ucast,
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds 		   st->periodic_gc_runs,
32869a6d276eSNeil Horman 		   st->forced_gc_runs,
3287fb811395SRick Jones 		   st->unres_discards,
3288fb811395SRick Jones 		   st->table_fulls
32891da177e4SLinus Torvalds 		   );
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 	return 0;
32921da177e4SLinus Torvalds }
32931da177e4SLinus Torvalds 
3294f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = {
32951da177e4SLinus Torvalds 	.start	= neigh_stat_seq_start,
32961da177e4SLinus Torvalds 	.next	= neigh_stat_seq_next,
32971da177e4SLinus Torvalds 	.stop	= neigh_stat_seq_stop,
32981da177e4SLinus Torvalds 	.show	= neigh_stat_seq_show,
32991da177e4SLinus Torvalds };
33001da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
33011da177e4SLinus Torvalds 
33027b8f7a40SRoopa Prabhu static void __neigh_notify(struct neighbour *n, int type, int flags,
33037b8f7a40SRoopa Prabhu 			   u32 pid)
33041da177e4SLinus Torvalds {
3305c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(n->dev);
33068b8aec50SThomas Graf 	struct sk_buff *skb;
3307b8673311SThomas Graf 	int err = -ENOBUFS;
33081da177e4SLinus Torvalds 
3309339bf98fSThomas Graf 	skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
33108b8aec50SThomas Graf 	if (skb == NULL)
3311b8673311SThomas Graf 		goto errout;
33121da177e4SLinus Torvalds 
33137b8f7a40SRoopa Prabhu 	err = neigh_fill_info(skb, n, pid, 0, type, flags);
331426932566SPatrick McHardy 	if (err < 0) {
331526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
331626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
331726932566SPatrick McHardy 		kfree_skb(skb);
331826932566SPatrick McHardy 		goto errout;
331926932566SPatrick McHardy 	}
33201ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
33211ce85fe4SPablo Neira Ayuso 	return;
3322b8673311SThomas Graf errout:
3323b8673311SThomas Graf 	if (err < 0)
3324426b5303SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3325b8673311SThomas Graf }
3326b8673311SThomas Graf 
3327b8673311SThomas Graf void neigh_app_ns(struct neighbour *n)
3328b8673311SThomas Graf {
33297b8f7a40SRoopa Prabhu 	__neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
33308b8aec50SThomas Graf }
33310a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns);
33321da177e4SLinus Torvalds 
33331da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
3334b93196dcSCong Wang static int zero;
3335555445cdSFrancesco Fusco static int int_max = INT_MAX;
3336b93196dcSCong Wang static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
33371da177e4SLinus Torvalds 
3338fe2c6338SJoe Perches static int proc_unres_qlen(struct ctl_table *ctl, int write,
3339fe2c6338SJoe Perches 			   void __user *buffer, size_t *lenp, loff_t *ppos)
33408b5c171bSEric Dumazet {
33418b5c171bSEric Dumazet 	int size, ret;
3342fe2c6338SJoe Perches 	struct ctl_table tmp = *ctl;
33438b5c171bSEric Dumazet 
3344ce46cc64SShan Wei 	tmp.extra1 = &zero;
3345ce46cc64SShan Wei 	tmp.extra2 = &unres_qlen_max;
33468b5c171bSEric Dumazet 	tmp.data = &size;
3347ce46cc64SShan Wei 
3348ce46cc64SShan Wei 	size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
3349ce46cc64SShan Wei 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3350ce46cc64SShan Wei 
33518b5c171bSEric Dumazet 	if (write && !ret)
33528b5c171bSEric Dumazet 		*(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
33538b5c171bSEric Dumazet 	return ret;
33548b5c171bSEric Dumazet }
33558b5c171bSEric Dumazet 
33561d4c8c29SJiri Pirko static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
33571d4c8c29SJiri Pirko 						   int family)
33581d4c8c29SJiri Pirko {
3359bba24896SJiri Pirko 	switch (family) {
3360bba24896SJiri Pirko 	case AF_INET:
33611d4c8c29SJiri Pirko 		return __in_dev_arp_parms_get_rcu(dev);
3362bba24896SJiri Pirko 	case AF_INET6:
3363bba24896SJiri Pirko 		return __in6_dev_nd_parms_get_rcu(dev);
3364bba24896SJiri Pirko 	}
33651d4c8c29SJiri Pirko 	return NULL;
33661d4c8c29SJiri Pirko }
33671d4c8c29SJiri Pirko 
33681d4c8c29SJiri Pirko static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
33691d4c8c29SJiri Pirko 				  int index)
33701d4c8c29SJiri Pirko {
33711d4c8c29SJiri Pirko 	struct net_device *dev;
33721d4c8c29SJiri Pirko 	int family = neigh_parms_family(p);
33731d4c8c29SJiri Pirko 
33741d4c8c29SJiri Pirko 	rcu_read_lock();
33751d4c8c29SJiri Pirko 	for_each_netdev_rcu(net, dev) {
33761d4c8c29SJiri Pirko 		struct neigh_parms *dst_p =
33771d4c8c29SJiri Pirko 				neigh_get_dev_parms_rcu(dev, family);
33781d4c8c29SJiri Pirko 
33791d4c8c29SJiri Pirko 		if (dst_p && !test_bit(index, dst_p->data_state))
33801d4c8c29SJiri Pirko 			dst_p->data[index] = p->data[index];
33811d4c8c29SJiri Pirko 	}
33821d4c8c29SJiri Pirko 	rcu_read_unlock();
33831d4c8c29SJiri Pirko }
33841d4c8c29SJiri Pirko 
33851d4c8c29SJiri Pirko static void neigh_proc_update(struct ctl_table *ctl, int write)
33861d4c8c29SJiri Pirko {
33871d4c8c29SJiri Pirko 	struct net_device *dev = ctl->extra1;
33881d4c8c29SJiri Pirko 	struct neigh_parms *p = ctl->extra2;
338977d47afbSJiri Pirko 	struct net *net = neigh_parms_net(p);
33901d4c8c29SJiri Pirko 	int index = (int *) ctl->data - p->data;
33911d4c8c29SJiri Pirko 
33921d4c8c29SJiri Pirko 	if (!write)
33931d4c8c29SJiri Pirko 		return;
33941d4c8c29SJiri Pirko 
33951d4c8c29SJiri Pirko 	set_bit(index, p->data_state);
33967627ae60SMarcus Huewe 	if (index == NEIGH_VAR_DELAY_PROBE_TIME)
33972a4501aeSIdo Schimmel 		call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
33981d4c8c29SJiri Pirko 	if (!dev) /* NULL dev means this is default value */
33991d4c8c29SJiri Pirko 		neigh_copy_dflt_parms(net, p, index);
34001d4c8c29SJiri Pirko }
34011d4c8c29SJiri Pirko 
34021f9248e5SJiri Pirko static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
34031f9248e5SJiri Pirko 					   void __user *buffer,
34041f9248e5SJiri Pirko 					   size_t *lenp, loff_t *ppos)
34051f9248e5SJiri Pirko {
34061f9248e5SJiri Pirko 	struct ctl_table tmp = *ctl;
34071d4c8c29SJiri Pirko 	int ret;
34081f9248e5SJiri Pirko 
34091f9248e5SJiri Pirko 	tmp.extra1 = &zero;
34101f9248e5SJiri Pirko 	tmp.extra2 = &int_max;
34111f9248e5SJiri Pirko 
34121d4c8c29SJiri Pirko 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
34131d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
34141d4c8c29SJiri Pirko 	return ret;
34151f9248e5SJiri Pirko }
34161f9248e5SJiri Pirko 
3417cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write,
3418cb5b09c1SJiri Pirko 			void __user *buffer, size_t *lenp, loff_t *ppos)
3419cb5b09c1SJiri Pirko {
34201d4c8c29SJiri Pirko 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
34211d4c8c29SJiri Pirko 
34221d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
34231d4c8c29SJiri Pirko 	return ret;
3424cb5b09c1SJiri Pirko }
3425cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec);
3426cb5b09c1SJiri Pirko 
3427cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3428cb5b09c1SJiri Pirko 				void __user *buffer,
3429cb5b09c1SJiri Pirko 				size_t *lenp, loff_t *ppos)
3430cb5b09c1SJiri Pirko {
34311d4c8c29SJiri Pirko 	int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
34321d4c8c29SJiri Pirko 
34331d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
34341d4c8c29SJiri Pirko 	return ret;
3435cb5b09c1SJiri Pirko }
3436cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3437cb5b09c1SJiri Pirko 
3438cb5b09c1SJiri Pirko static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3439cb5b09c1SJiri Pirko 					      void __user *buffer,
3440cb5b09c1SJiri Pirko 					      size_t *lenp, loff_t *ppos)
3441cb5b09c1SJiri Pirko {
34421d4c8c29SJiri Pirko 	int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
34431d4c8c29SJiri Pirko 
34441d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
34451d4c8c29SJiri Pirko 	return ret;
3446cb5b09c1SJiri Pirko }
3447cb5b09c1SJiri Pirko 
3448cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3449cb5b09c1SJiri Pirko 				   void __user *buffer,
3450cb5b09c1SJiri Pirko 				   size_t *lenp, loff_t *ppos)
3451cb5b09c1SJiri Pirko {
34521d4c8c29SJiri Pirko 	int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
34531d4c8c29SJiri Pirko 
34541d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
34551d4c8c29SJiri Pirko 	return ret;
3456cb5b09c1SJiri Pirko }
3457cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3458cb5b09c1SJiri Pirko 
3459cb5b09c1SJiri Pirko static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3460cb5b09c1SJiri Pirko 					  void __user *buffer,
3461cb5b09c1SJiri Pirko 					  size_t *lenp, loff_t *ppos)
3462cb5b09c1SJiri Pirko {
34631d4c8c29SJiri Pirko 	int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
34641d4c8c29SJiri Pirko 
34651d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
34661d4c8c29SJiri Pirko 	return ret;
3467cb5b09c1SJiri Pirko }
3468cb5b09c1SJiri Pirko 
34694bf6980dSJean-Francois Remy static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
34704bf6980dSJean-Francois Remy 					  void __user *buffer,
34714bf6980dSJean-Francois Remy 					  size_t *lenp, loff_t *ppos)
34724bf6980dSJean-Francois Remy {
34734bf6980dSJean-Francois Remy 	struct neigh_parms *p = ctl->extra2;
34744bf6980dSJean-Francois Remy 	int ret;
34754bf6980dSJean-Francois Remy 
34764bf6980dSJean-Francois Remy 	if (strcmp(ctl->procname, "base_reachable_time") == 0)
34774bf6980dSJean-Francois Remy 		ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
34784bf6980dSJean-Francois Remy 	else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
34794bf6980dSJean-Francois Remy 		ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
34804bf6980dSJean-Francois Remy 	else
34814bf6980dSJean-Francois Remy 		ret = -1;
34824bf6980dSJean-Francois Remy 
34834bf6980dSJean-Francois Remy 	if (write && ret == 0) {
34844bf6980dSJean-Francois Remy 		/* update reachable_time as well, otherwise, the change will
34854bf6980dSJean-Francois Remy 		 * only be effective after the next time neigh_periodic_work
34864bf6980dSJean-Francois Remy 		 * decides to recompute it
34874bf6980dSJean-Francois Remy 		 */
34884bf6980dSJean-Francois Remy 		p->reachable_time =
34894bf6980dSJean-Francois Remy 			neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
34904bf6980dSJean-Francois Remy 	}
34914bf6980dSJean-Francois Remy 	return ret;
34924bf6980dSJean-Francois Remy }
34934bf6980dSJean-Francois Remy 
34941f9248e5SJiri Pirko #define NEIGH_PARMS_DATA_OFFSET(index)	\
34951f9248e5SJiri Pirko 	(&((struct neigh_parms *) 0)->data[index])
34961f9248e5SJiri Pirko 
34971f9248e5SJiri Pirko #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
34981f9248e5SJiri Pirko 	[NEIGH_VAR_ ## attr] = { \
34991f9248e5SJiri Pirko 		.procname	= name, \
35001f9248e5SJiri Pirko 		.data		= NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
35011f9248e5SJiri Pirko 		.maxlen		= sizeof(int), \
35021f9248e5SJiri Pirko 		.mode		= mval, \
35031f9248e5SJiri Pirko 		.proc_handler	= proc, \
35041f9248e5SJiri Pirko 	}
35051f9248e5SJiri Pirko 
35061f9248e5SJiri Pirko #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
35071f9248e5SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
35081f9248e5SJiri Pirko 
35091f9248e5SJiri Pirko #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3510cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
35111f9248e5SJiri Pirko 
35121f9248e5SJiri Pirko #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3513cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
35141f9248e5SJiri Pirko 
35151f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
3516cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
35171f9248e5SJiri Pirko 
35181f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3519cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
35201f9248e5SJiri Pirko 
35211f9248e5SJiri Pirko #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3522cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
352354716e3bSEric W. Biederman 
35241da177e4SLinus Torvalds static struct neigh_sysctl_table {
35251da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
35268b5c171bSEric Dumazet 	struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3527ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = {
35281da177e4SLinus Torvalds 	.neigh_vars = {
35291f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
35301f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
35311f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
35328da86466SYOSHIFUJI Hideaki/吉藤英明 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
35331f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
35341f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
35351f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
35361f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
35371f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
35381f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
35391f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
35401f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
35411f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
35421f9248e5SJiri Pirko 		NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
35431f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
35441f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
35458b5c171bSEric Dumazet 		[NEIGH_VAR_GC_INTERVAL] = {
35461da177e4SLinus Torvalds 			.procname	= "gc_interval",
35471da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
35481da177e4SLinus Torvalds 			.mode		= 0644,
35496d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
35501da177e4SLinus Torvalds 		},
35518b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH1] = {
35521da177e4SLinus Torvalds 			.procname	= "gc_thresh1",
35531da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
35541da177e4SLinus Torvalds 			.mode		= 0644,
3555555445cdSFrancesco Fusco 			.extra1 	= &zero,
3556555445cdSFrancesco Fusco 			.extra2		= &int_max,
3557555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
35581da177e4SLinus Torvalds 		},
35598b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH2] = {
35601da177e4SLinus Torvalds 			.procname	= "gc_thresh2",
35611da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
35621da177e4SLinus Torvalds 			.mode		= 0644,
3563555445cdSFrancesco Fusco 			.extra1 	= &zero,
3564555445cdSFrancesco Fusco 			.extra2		= &int_max,
3565555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
35661da177e4SLinus Torvalds 		},
35678b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH3] = {
35681da177e4SLinus Torvalds 			.procname	= "gc_thresh3",
35691da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
35701da177e4SLinus Torvalds 			.mode		= 0644,
3571555445cdSFrancesco Fusco 			.extra1 	= &zero,
3572555445cdSFrancesco Fusco 			.extra2		= &int_max,
3573555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
35741da177e4SLinus Torvalds 		},
3575c3bac5a7SPavel Emelyanov 		{},
35761da177e4SLinus Torvalds 	},
35771da177e4SLinus Torvalds };
35781da177e4SLinus Torvalds 
35791da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
358073af614aSJiri Pirko 			  proc_handler *handler)
35811da177e4SLinus Torvalds {
35821f9248e5SJiri Pirko 	int i;
35833c607bbbSPavel Emelyanov 	struct neigh_sysctl_table *t;
35841f9248e5SJiri Pirko 	const char *dev_name_source;
35858f40a1f9SEric W. Biederman 	char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
358673af614aSJiri Pirko 	char *p_name;
35871da177e4SLinus Torvalds 
35883c607bbbSPavel Emelyanov 	t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
35891da177e4SLinus Torvalds 	if (!t)
35903c607bbbSPavel Emelyanov 		goto err;
35913c607bbbSPavel Emelyanov 
3592b194c1f1SJiri Pirko 	for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
35931f9248e5SJiri Pirko 		t->neigh_vars[i].data += (long) p;
3594cb5b09c1SJiri Pirko 		t->neigh_vars[i].extra1 = dev;
35951d4c8c29SJiri Pirko 		t->neigh_vars[i].extra2 = p;
3596cb5b09c1SJiri Pirko 	}
35971da177e4SLinus Torvalds 
35981da177e4SLinus Torvalds 	if (dev) {
35991da177e4SLinus Torvalds 		dev_name_source = dev->name;
3600d12af679SEric W. Biederman 		/* Terminate the table early */
36018b5c171bSEric Dumazet 		memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
36028b5c171bSEric Dumazet 		       sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
36031da177e4SLinus Torvalds 	} else {
36049ecf07a1SMathias Krause 		struct neigh_table *tbl = p->tbl;
36058f40a1f9SEric W. Biederman 		dev_name_source = "default";
36069ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
36079ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
36089ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
36099ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
36101da177e4SLinus Torvalds 	}
36111da177e4SLinus Torvalds 
3612f8572d8fSEric W. Biederman 	if (handler) {
36131da177e4SLinus Torvalds 		/* RetransTime */
36148b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
36151da177e4SLinus Torvalds 		/* ReachableTime */
36168b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
36171da177e4SLinus Torvalds 		/* RetransTime (in milliseconds)*/
36188b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
36191da177e4SLinus Torvalds 		/* ReachableTime (in milliseconds) */
36208b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
36214bf6980dSJean-Francois Remy 	} else {
36224bf6980dSJean-Francois Remy 		/* Those handlers will update p->reachable_time after
36234bf6980dSJean-Francois Remy 		 * base_reachable_time(_ms) is set to ensure the new timer starts being
36244bf6980dSJean-Francois Remy 		 * applied after the next neighbour update instead of waiting for
36254bf6980dSJean-Francois Remy 		 * neigh_periodic_work to update its value (can be multiple minutes)
36264bf6980dSJean-Francois Remy 		 * So any handler that replaces them should do this as well
36274bf6980dSJean-Francois Remy 		 */
36284bf6980dSJean-Francois Remy 		/* ReachableTime */
36294bf6980dSJean-Francois Remy 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
36304bf6980dSJean-Francois Remy 			neigh_proc_base_reachable_time;
36314bf6980dSJean-Francois Remy 		/* ReachableTime (in milliseconds) */
36324bf6980dSJean-Francois Remy 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
36334bf6980dSJean-Francois Remy 			neigh_proc_base_reachable_time;
36341da177e4SLinus Torvalds 	}
36351da177e4SLinus Torvalds 
3636464dc801SEric W. Biederman 	/* Don't export sysctls to unprivileged users */
3637464dc801SEric W. Biederman 	if (neigh_parms_net(p)->user_ns != &init_user_ns)
3638464dc801SEric W. Biederman 		t->neigh_vars[0].procname = NULL;
3639464dc801SEric W. Biederman 
364073af614aSJiri Pirko 	switch (neigh_parms_family(p)) {
364173af614aSJiri Pirko 	case AF_INET:
364273af614aSJiri Pirko 	      p_name = "ipv4";
364373af614aSJiri Pirko 	      break;
364473af614aSJiri Pirko 	case AF_INET6:
364573af614aSJiri Pirko 	      p_name = "ipv6";
364673af614aSJiri Pirko 	      break;
364773af614aSJiri Pirko 	default:
364873af614aSJiri Pirko 	      BUG();
364973af614aSJiri Pirko 	}
365073af614aSJiri Pirko 
36518f40a1f9SEric W. Biederman 	snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
36528f40a1f9SEric W. Biederman 		p_name, dev_name_source);
36534ab438fcSDenis V. Lunev 	t->sysctl_header =
36548f40a1f9SEric W. Biederman 		register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
36553c607bbbSPavel Emelyanov 	if (!t->sysctl_header)
36568f40a1f9SEric W. Biederman 		goto free;
36573c607bbbSPavel Emelyanov 
36581da177e4SLinus Torvalds 	p->sysctl_table = t;
36591da177e4SLinus Torvalds 	return 0;
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds free:
36621da177e4SLinus Torvalds 	kfree(t);
36633c607bbbSPavel Emelyanov err:
36643c607bbbSPavel Emelyanov 	return -ENOBUFS;
36651da177e4SLinus Torvalds }
36660a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register);
36671da177e4SLinus Torvalds 
36681da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p)
36691da177e4SLinus Torvalds {
36701da177e4SLinus Torvalds 	if (p->sysctl_table) {
36711da177e4SLinus Torvalds 		struct neigh_sysctl_table *t = p->sysctl_table;
36721da177e4SLinus Torvalds 		p->sysctl_table = NULL;
36735dd3df10SEric W. Biederman 		unregister_net_sysctl_table(t->sysctl_header);
36741da177e4SLinus Torvalds 		kfree(t);
36751da177e4SLinus Torvalds 	}
36761da177e4SLinus Torvalds }
36770a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister);
36781da177e4SLinus Torvalds 
36791da177e4SLinus Torvalds #endif	/* CONFIG_SYSCTL */
36801da177e4SLinus Torvalds 
3681c8822a4eSThomas Graf static int __init neigh_init(void)
3682c8822a4eSThomas Graf {
3683b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3684b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
368582cbb5c6SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETNEIGH, neigh_get, neigh_dump_info, 0);
3686c8822a4eSThomas Graf 
3687c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3688b97bac64SFlorian Westphal 		      0);
3689b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
3690c8822a4eSThomas Graf 
3691c8822a4eSThomas Graf 	return 0;
3692c8822a4eSThomas Graf }
3693c8822a4eSThomas Graf 
3694c8822a4eSThomas Graf subsys_initcall(neigh_init);
3695