xref: /openbmc/linux/net/core/neighbour.c (revision b3d0f789)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Generic address resolution entity
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Authors:
51da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
61da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *	Fixes:
141da177e4SLinus Torvalds  *	Vitaly E. Lavrov	releasing NULL neighbor in neigh_add.
151da177e4SLinus Torvalds  *	Harald Welte		Add neighbour cache statistics like rtstat
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19e005d193SJoe Perches 
205a0e3ad6STejun Heo #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/socket.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
261da177e4SLinus Torvalds #include <linux/proc_fs.h>
271da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
281da177e4SLinus Torvalds #include <linux/sysctl.h>
291da177e4SLinus Torvalds #endif
301da177e4SLinus Torvalds #include <linux/times.h>
31457c4cbcSEric W. Biederman #include <net/net_namespace.h>
321da177e4SLinus Torvalds #include <net/neighbour.h>
331da177e4SLinus Torvalds #include <net/dst.h>
341da177e4SLinus Torvalds #include <net/sock.h>
358d71740cSTom Tucker #include <net/netevent.h>
36a14a49d2SThomas Graf #include <net/netlink.h>
371da177e4SLinus Torvalds #include <linux/rtnetlink.h>
381da177e4SLinus Torvalds #include <linux/random.h>
39543537bdSPaulo Marques #include <linux/string.h>
40c3609d51Svignesh babu #include <linux/log2.h>
411d4c8c29SJiri Pirko #include <linux/inetdevice.h>
42bba24896SJiri Pirko #include <net/addrconf.h>
431da177e4SLinus Torvalds 
44d5d427cdSJoe Perches #define DEBUG
451da177e4SLinus Torvalds #define NEIGH_DEBUG 1
46d5d427cdSJoe Perches #define neigh_dbg(level, fmt, ...)		\
47d5d427cdSJoe Perches do {						\
48d5d427cdSJoe Perches 	if (level <= NEIGH_DEBUG)		\
49d5d427cdSJoe Perches 		pr_debug(fmt, ##__VA_ARGS__);	\
50d5d427cdSJoe Perches } while (0)
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define PNEIGH_HASHMASK		0xF
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg);
557b8f7a40SRoopa Prabhu static void __neigh_notify(struct neighbour *n, int type, int flags,
567b8f7a40SRoopa Prabhu 			   u32 pid);
577b8f7a40SRoopa Prabhu static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid);
581da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
591da177e4SLinus Torvalds 
6045fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS
619a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops;
6245fc3b11SAmos Waterland #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds    Neighbour hash table buckets are protected with rwlock tbl->lock.
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds    - All the scans/updates to hash buckets MUST be made under this lock.
681da177e4SLinus Torvalds    - NOTHING clever should be made under this lock: no callbacks
691da177e4SLinus Torvalds      to protocol backends, no attempts to send something to network.
701da177e4SLinus Torvalds      It will result in deadlocks, if backend/driver wants to use neighbour
711da177e4SLinus Torvalds      cache.
721da177e4SLinus Torvalds    - If the entry requires some non-trivial actions, increase
731da177e4SLinus Torvalds      its reference count and release table lock.
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds    Neighbour entries are protected:
761da177e4SLinus Torvalds    - with reference count.
771da177e4SLinus Torvalds    - with rwlock neigh->lock
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds    Reference count prevents destruction.
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds    neigh->lock mainly serializes ll address data and its validity state.
821da177e4SLinus Torvalds    However, the same lock is used to protect another entry fields:
831da177e4SLinus Torvalds     - timer
841da177e4SLinus Torvalds     - resolution queue
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds    Again, nothing clever shall be made under neigh->lock,
871da177e4SLinus Torvalds    the most complicated procedure, which we allow is dev->hard_header.
881da177e4SLinus Torvalds    It is supposed, that dev->hard_header is simplistic and does
891da177e4SLinus Torvalds    not make callbacks to neighbour tables.
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds 
928f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	kfree_skb(skb);
951da177e4SLinus Torvalds 	return -ENETDOWN;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
984f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh)
994f494554SThomas Graf {
1004f494554SThomas Graf 	if (neigh->parms->neigh_cleanup)
1014f494554SThomas Graf 		neigh->parms->neigh_cleanup(neigh);
1024f494554SThomas Graf 
1037b8f7a40SRoopa Prabhu 	__neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
10453f800e3SIdo Schimmel 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
1054f494554SThomas Graf 	neigh_release(neigh);
1064f494554SThomas Graf }
1074f494554SThomas Graf 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * It is random distribution in the interval (1/2)*base...(3/2)*base.
1101da177e4SLinus Torvalds  * It corresponds to default IPv6 settings and is not overridable,
1111da177e4SLinus Torvalds  * because it is really reasonable choice.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base)
1151da177e4SLinus Torvalds {
11663862b5bSAruna-Hewapathirane 	return base ? (prandom_u32() % base) + (base >> 1) : 0;
1171da177e4SLinus Torvalds }
1180a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	int shrunk = 0;
1241da177e4SLinus Torvalds 	int i;
125d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
130d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
131d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
132cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
133767e97e1SEric Dumazet 		struct neighbour *n;
134767e97e1SEric Dumazet 		struct neighbour __rcu **np;
1351da177e4SLinus Torvalds 
136d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
137767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
138767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
1391da177e4SLinus Torvalds 			/* Neighbour record may be discarded if:
1401da177e4SLinus Torvalds 			 * - nobody refers to it.
1411da177e4SLinus Torvalds 			 * - it is not permanent
1421da177e4SLinus Torvalds 			 */
1431da177e4SLinus Torvalds 			write_lock(&n->lock);
1441da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
1451da177e4SLinus Torvalds 			    !(n->nud_state & NUD_PERMANENT)) {
146767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
147767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
148767e97e1SEric Dumazet 						  lockdep_is_held(&tbl->lock)));
1491da177e4SLinus Torvalds 				n->dead = 1;
1501da177e4SLinus Torvalds 				shrunk	= 1;
1511da177e4SLinus Torvalds 				write_unlock(&n->lock);
1524f494554SThomas Graf 				neigh_cleanup_and_release(n);
1531da177e4SLinus Torvalds 				continue;
1541da177e4SLinus Torvalds 			}
1551da177e4SLinus Torvalds 			write_unlock(&n->lock);
1561da177e4SLinus Torvalds 			np = &n->next;
1571da177e4SLinus Torvalds 		}
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	tbl->last_flush = jiffies;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return shrunk;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
167a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when)
168a43d8994SPavel Emelyanov {
169a43d8994SPavel Emelyanov 	neigh_hold(n);
170a43d8994SPavel Emelyanov 	if (unlikely(mod_timer(&n->timer, when))) {
171a43d8994SPavel Emelyanov 		printk("NEIGH: BUG, double timer add, state is %x\n",
172a43d8994SPavel Emelyanov 		       n->nud_state);
173a43d8994SPavel Emelyanov 		dump_stack();
174a43d8994SPavel Emelyanov 	}
175a43d8994SPavel Emelyanov }
176a43d8994SPavel Emelyanov 
1771da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	if ((n->nud_state & NUD_IN_TIMER) &&
1801da177e4SLinus Torvalds 	    del_timer(&n->timer)) {
1811da177e4SLinus Torvalds 		neigh_release(n);
1821da177e4SLinus Torvalds 		return 1;
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	return 0;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct sk_buff *skb;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL) {
1921da177e4SLinus Torvalds 		dev_put(skb->dev);
1931da177e4SLinus Torvalds 		kfree_skb(skb);
1941da177e4SLinus Torvalds 	}
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
19749636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	int i;
200d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
2011da177e4SLinus Torvalds 
202d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
203d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
204d6bf7817SEric Dumazet 
205cd089336SDavid S. Miller 	for (i = 0; i < (1 << nht->hash_shift); i++) {
206767e97e1SEric Dumazet 		struct neighbour *n;
207767e97e1SEric Dumazet 		struct neighbour __rcu **np = &nht->hash_buckets[i];
2081da177e4SLinus Torvalds 
209767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
210767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
2111da177e4SLinus Torvalds 			if (dev && n->dev != dev) {
2121da177e4SLinus Torvalds 				np = &n->next;
2131da177e4SLinus Torvalds 				continue;
2141da177e4SLinus Torvalds 			}
215767e97e1SEric Dumazet 			rcu_assign_pointer(*np,
216767e97e1SEric Dumazet 				   rcu_dereference_protected(n->next,
217767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
2181da177e4SLinus Torvalds 			write_lock(&n->lock);
2191da177e4SLinus Torvalds 			neigh_del_timer(n);
2201da177e4SLinus Torvalds 			n->dead = 1;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) != 1) {
2231da177e4SLinus Torvalds 				/* The most unpleasant situation.
2241da177e4SLinus Torvalds 				   We must destroy neighbour entry,
2251da177e4SLinus Torvalds 				   but someone still uses it.
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 				   The destroy will be delayed until
2281da177e4SLinus Torvalds 				   the last user releases us, but
2291da177e4SLinus Torvalds 				   we must kill timers etc. and move
2301da177e4SLinus Torvalds 				   it to safe state.
2311da177e4SLinus Torvalds 				 */
232c9ab4d85SEric Dumazet 				__skb_queue_purge(&n->arp_queue);
2338b5c171bSEric Dumazet 				n->arp_queue_len_bytes = 0;
2341da177e4SLinus Torvalds 				n->output = neigh_blackhole;
2351da177e4SLinus Torvalds 				if (n->nud_state & NUD_VALID)
2361da177e4SLinus Torvalds 					n->nud_state = NUD_NOARP;
2371da177e4SLinus Torvalds 				else
2381da177e4SLinus Torvalds 					n->nud_state = NUD_NONE;
239d5d427cdSJoe Perches 				neigh_dbg(2, "neigh %p is stray\n", n);
2401da177e4SLinus Torvalds 			}
2411da177e4SLinus Torvalds 			write_unlock(&n->lock);
2424f494554SThomas Graf 			neigh_cleanup_and_release(n);
2431da177e4SLinus Torvalds 		}
2441da177e4SLinus Torvalds 	}
24549636bb1SHerbert Xu }
2461da177e4SLinus Torvalds 
24749636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
24849636bb1SHerbert Xu {
24949636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
25049636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
25149636bb1SHerbert Xu 	write_unlock_bh(&tbl->lock);
25249636bb1SHerbert Xu }
2530a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr);
25449636bb1SHerbert Xu 
25549636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
25649636bb1SHerbert Xu {
25749636bb1SHerbert Xu 	write_lock_bh(&tbl->lock);
25849636bb1SHerbert Xu 	neigh_flush_dev(tbl, dev);
2591da177e4SLinus Torvalds 	pneigh_ifdown(tbl, dev);
2601da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
2631da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
2641da177e4SLinus Torvalds 	return 0;
2651da177e4SLinus Torvalds }
2660a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown);
2671da177e4SLinus Torvalds 
268596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct neighbour *n = NULL;
2711da177e4SLinus Torvalds 	unsigned long now = jiffies;
2721da177e4SLinus Torvalds 	int entries;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	entries = atomic_inc_return(&tbl->entries) - 1;
2751da177e4SLinus Torvalds 	if (entries >= tbl->gc_thresh3 ||
2761da177e4SLinus Torvalds 	    (entries >= tbl->gc_thresh2 &&
2771da177e4SLinus Torvalds 	     time_after(now, tbl->last_flush + 5 * HZ))) {
2781da177e4SLinus Torvalds 		if (!neigh_forced_gc(tbl) &&
279fb811395SRick Jones 		    entries >= tbl->gc_thresh3) {
280fb811395SRick Jones 			net_info_ratelimited("%s: neighbor table overflow!\n",
281fb811395SRick Jones 					     tbl->id);
282fb811395SRick Jones 			NEIGH_CACHE_STAT_INC(tbl, table_fulls);
2831da177e4SLinus Torvalds 			goto out_entries;
2841da177e4SLinus Torvalds 		}
285fb811395SRick Jones 	}
2861da177e4SLinus Torvalds 
28708433effSYOSHIFUJI Hideaki / 吉藤英明 	n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
2881da177e4SLinus Torvalds 	if (!n)
2891da177e4SLinus Torvalds 		goto out_entries;
2901da177e4SLinus Torvalds 
291c9ab4d85SEric Dumazet 	__skb_queue_head_init(&n->arp_queue);
2921da177e4SLinus Torvalds 	rwlock_init(&n->lock);
2930ed8ddf4SEric Dumazet 	seqlock_init(&n->ha_lock);
2941da177e4SLinus Torvalds 	n->updated	  = n->used = now;
2951da177e4SLinus Torvalds 	n->nud_state	  = NUD_NONE;
2961da177e4SLinus Torvalds 	n->output	  = neigh_blackhole;
297f6b72b62SDavid S. Miller 	seqlock_init(&n->hh.hh_lock);
2981da177e4SLinus Torvalds 	n->parms	  = neigh_parms_clone(&tbl->parms);
299b24b8a24SPavel Emelyanov 	setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, allocs);
3021da177e4SLinus Torvalds 	n->tbl		  = tbl;
3031da177e4SLinus Torvalds 	atomic_set(&n->refcnt, 1);
3041da177e4SLinus Torvalds 	n->dead		  = 1;
3051da177e4SLinus Torvalds out:
3061da177e4SLinus Torvalds 	return n;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds out_entries:
3091da177e4SLinus Torvalds 	atomic_dec(&tbl->entries);
3101da177e4SLinus Torvalds 	goto out;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3132c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x)
3142c2aba6cSDavid S. Miller {
315b3d0f789SJason A. Donenfeld 	*x = get_random_u32() | 1;
3162c2aba6cSDavid S. Miller }
3172c2aba6cSDavid S. Miller 
318cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
3191da177e4SLinus Torvalds {
320cd089336SDavid S. Miller 	size_t size = (1 << shift) * sizeof(struct neighbour *);
321d6bf7817SEric Dumazet 	struct neigh_hash_table *ret;
3226193d2beSEric Dumazet 	struct neighbour __rcu **buckets;
3232c2aba6cSDavid S. Miller 	int i;
3241da177e4SLinus Torvalds 
325d6bf7817SEric Dumazet 	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
326d6bf7817SEric Dumazet 	if (!ret)
327d6bf7817SEric Dumazet 		return NULL;
328d6bf7817SEric Dumazet 	if (size <= PAGE_SIZE)
329d6bf7817SEric Dumazet 		buckets = kzalloc(size, GFP_ATOMIC);
330d6bf7817SEric Dumazet 	else
3316193d2beSEric Dumazet 		buckets = (struct neighbour __rcu **)
332d6bf7817SEric Dumazet 			  __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
333d6bf7817SEric Dumazet 					   get_order(size));
334d6bf7817SEric Dumazet 	if (!buckets) {
335d6bf7817SEric Dumazet 		kfree(ret);
336d6bf7817SEric Dumazet 		return NULL;
3371da177e4SLinus Torvalds 	}
3386193d2beSEric Dumazet 	ret->hash_buckets = buckets;
339cd089336SDavid S. Miller 	ret->hash_shift = shift;
3402c2aba6cSDavid S. Miller 	for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
3412c2aba6cSDavid S. Miller 		neigh_get_hash_rnd(&ret->hash_rnd[i]);
3421da177e4SLinus Torvalds 	return ret;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
345d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head)
3461da177e4SLinus Torvalds {
347d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = container_of(head,
348d6bf7817SEric Dumazet 						    struct neigh_hash_table,
349d6bf7817SEric Dumazet 						    rcu);
350cd089336SDavid S. Miller 	size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
3516193d2beSEric Dumazet 	struct neighbour __rcu **buckets = nht->hash_buckets;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	if (size <= PAGE_SIZE)
354d6bf7817SEric Dumazet 		kfree(buckets);
3551da177e4SLinus Torvalds 	else
356d6bf7817SEric Dumazet 		free_pages((unsigned long)buckets, get_order(size));
357d6bf7817SEric Dumazet 	kfree(nht);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
360d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
361cd089336SDavid S. Miller 						unsigned long new_shift)
3621da177e4SLinus Torvalds {
363d6bf7817SEric Dumazet 	unsigned int i, hash;
364d6bf7817SEric Dumazet 	struct neigh_hash_table *new_nht, *old_nht;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
3671da177e4SLinus Torvalds 
368d6bf7817SEric Dumazet 	old_nht = rcu_dereference_protected(tbl->nht,
369d6bf7817SEric Dumazet 					    lockdep_is_held(&tbl->lock));
370cd089336SDavid S. Miller 	new_nht = neigh_hash_alloc(new_shift);
371d6bf7817SEric Dumazet 	if (!new_nht)
372d6bf7817SEric Dumazet 		return old_nht;
3731da177e4SLinus Torvalds 
374cd089336SDavid S. Miller 	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
3751da177e4SLinus Torvalds 		struct neighbour *n, *next;
3761da177e4SLinus Torvalds 
377767e97e1SEric Dumazet 		for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
378767e97e1SEric Dumazet 						   lockdep_is_held(&tbl->lock));
379d6bf7817SEric Dumazet 		     n != NULL;
380d6bf7817SEric Dumazet 		     n = next) {
381d6bf7817SEric Dumazet 			hash = tbl->hash(n->primary_key, n->dev,
382d6bf7817SEric Dumazet 					 new_nht->hash_rnd);
3831da177e4SLinus Torvalds 
384cd089336SDavid S. Miller 			hash >>= (32 - new_nht->hash_shift);
385767e97e1SEric Dumazet 			next = rcu_dereference_protected(n->next,
386767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock));
3871da177e4SLinus Torvalds 
388767e97e1SEric Dumazet 			rcu_assign_pointer(n->next,
389767e97e1SEric Dumazet 					   rcu_dereference_protected(
390767e97e1SEric Dumazet 						new_nht->hash_buckets[hash],
391767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
392767e97e1SEric Dumazet 			rcu_assign_pointer(new_nht->hash_buckets[hash], n);
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
396d6bf7817SEric Dumazet 	rcu_assign_pointer(tbl->nht, new_nht);
397d6bf7817SEric Dumazet 	call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
398d6bf7817SEric Dumazet 	return new_nht;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
4021da177e4SLinus Torvalds 			       struct net_device *dev)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct neighbour *n;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4071da177e4SLinus Torvalds 
408d6bf7817SEric Dumazet 	rcu_read_lock_bh();
40960395a20SEric W. Biederman 	n = __neigh_lookup_noref(tbl, pkey, dev);
41060395a20SEric W. Biederman 	if (n) {
411767e97e1SEric Dumazet 		if (!atomic_inc_not_zero(&n->refcnt))
412767e97e1SEric Dumazet 			n = NULL;
4131da177e4SLinus Torvalds 		NEIGH_CACHE_STAT_INC(tbl, hits);
4141da177e4SLinus Torvalds 	}
415767e97e1SEric Dumazet 
416d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4171da177e4SLinus Torvalds 	return n;
4181da177e4SLinus Torvalds }
4190a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup);
4201da177e4SLinus Torvalds 
421426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
422426b5303SEric W. Biederman 				     const void *pkey)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	struct neighbour *n;
4251da177e4SLinus Torvalds 	int key_len = tbl->key_len;
426bc4bf5f3SPavel Emelyanov 	u32 hash_val;
427d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, lookups);
4301da177e4SLinus Torvalds 
431d6bf7817SEric Dumazet 	rcu_read_lock_bh();
432d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
433cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
434767e97e1SEric Dumazet 
435767e97e1SEric Dumazet 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
436767e97e1SEric Dumazet 	     n != NULL;
437767e97e1SEric Dumazet 	     n = rcu_dereference_bh(n->next)) {
438426b5303SEric W. Biederman 		if (!memcmp(n->primary_key, pkey, key_len) &&
439878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(n->dev), net)) {
440767e97e1SEric Dumazet 			if (!atomic_inc_not_zero(&n->refcnt))
441767e97e1SEric Dumazet 				n = NULL;
4421da177e4SLinus Torvalds 			NEIGH_CACHE_STAT_INC(tbl, hits);
4431da177e4SLinus Torvalds 			break;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 	}
446767e97e1SEric Dumazet 
447d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
4481da177e4SLinus Torvalds 	return n;
4491da177e4SLinus Torvalds }
4500a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev);
4511da177e4SLinus Torvalds 
452a263b309SDavid S. Miller struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
453a263b309SDavid S. Miller 				 struct net_device *dev, bool want_ref)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	u32 hash_val;
4561da177e4SLinus Torvalds 	int key_len = tbl->key_len;
4571da177e4SLinus Torvalds 	int error;
458596b9b68SDavid Miller 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
459d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	if (!n) {
4621da177e4SLinus Torvalds 		rc = ERR_PTR(-ENOBUFS);
4631da177e4SLinus Torvalds 		goto out;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	memcpy(n->primary_key, pkey, key_len);
4671da177e4SLinus Torvalds 	n->dev = dev;
4681da177e4SLinus Torvalds 	dev_hold(dev);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/* Protocol specific setup. */
4711da177e4SLinus Torvalds 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
4721da177e4SLinus Torvalds 		rc = ERR_PTR(error);
4731da177e4SLinus Torvalds 		goto out_neigh_release;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
476da6a8fa0SDavid Miller 	if (dev->netdev_ops->ndo_neigh_construct) {
477503eebc2SJiri Pirko 		error = dev->netdev_ops->ndo_neigh_construct(dev, n);
478da6a8fa0SDavid Miller 		if (error < 0) {
479da6a8fa0SDavid Miller 			rc = ERR_PTR(error);
480da6a8fa0SDavid Miller 			goto out_neigh_release;
481da6a8fa0SDavid Miller 		}
482da6a8fa0SDavid Miller 	}
483da6a8fa0SDavid Miller 
484447f2191SDavid S. Miller 	/* Device specific setup. */
485447f2191SDavid S. Miller 	if (n->parms->neigh_setup &&
486447f2191SDavid S. Miller 	    (error = n->parms->neigh_setup(n)) < 0) {
487447f2191SDavid S. Miller 		rc = ERR_PTR(error);
488447f2191SDavid S. Miller 		goto out_neigh_release;
489447f2191SDavid S. Miller 	}
490447f2191SDavid S. Miller 
4911f9248e5SJiri Pirko 	n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
494d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
495d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
4961da177e4SLinus Torvalds 
497cd089336SDavid S. Miller 	if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
498cd089336SDavid S. Miller 		nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
4991da177e4SLinus Torvalds 
500cd089336SDavid S. Miller 	hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	if (n->parms->dead) {
5031da177e4SLinus Torvalds 		rc = ERR_PTR(-EINVAL);
5041da177e4SLinus Torvalds 		goto out_tbl_unlock;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
507767e97e1SEric Dumazet 	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
508767e97e1SEric Dumazet 					    lockdep_is_held(&tbl->lock));
509767e97e1SEric Dumazet 	     n1 != NULL;
510767e97e1SEric Dumazet 	     n1 = rcu_dereference_protected(n1->next,
511767e97e1SEric Dumazet 			lockdep_is_held(&tbl->lock))) {
5121da177e4SLinus Torvalds 		if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
513a263b309SDavid S. Miller 			if (want_ref)
5141da177e4SLinus Torvalds 				neigh_hold(n1);
5151da177e4SLinus Torvalds 			rc = n1;
5161da177e4SLinus Torvalds 			goto out_tbl_unlock;
5171da177e4SLinus Torvalds 		}
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	n->dead = 0;
521a263b309SDavid S. Miller 	if (want_ref)
5221da177e4SLinus Torvalds 		neigh_hold(n);
523767e97e1SEric Dumazet 	rcu_assign_pointer(n->next,
524767e97e1SEric Dumazet 			   rcu_dereference_protected(nht->hash_buckets[hash_val],
525767e97e1SEric Dumazet 						     lockdep_is_held(&tbl->lock)));
526767e97e1SEric Dumazet 	rcu_assign_pointer(nht->hash_buckets[hash_val], n);
5271da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
528d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is created\n", n);
5291da177e4SLinus Torvalds 	rc = n;
5301da177e4SLinus Torvalds out:
5311da177e4SLinus Torvalds 	return rc;
5321da177e4SLinus Torvalds out_tbl_unlock:
5331da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
5341da177e4SLinus Torvalds out_neigh_release:
5351da177e4SLinus Torvalds 	neigh_release(n);
5361da177e4SLinus Torvalds 	goto out;
5371da177e4SLinus Torvalds }
538a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create);
5391da177e4SLinus Torvalds 
540be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len)
541fa86d322SPavel Emelyanov {
542fa86d322SPavel Emelyanov 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
543fa86d322SPavel Emelyanov 	hash_val ^= (hash_val >> 16);
544fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 8;
545fa86d322SPavel Emelyanov 	hash_val ^= hash_val >> 4;
546fa86d322SPavel Emelyanov 	hash_val &= PNEIGH_HASHMASK;
547be01d655SYOSHIFUJI Hideaki 	return hash_val;
548fa86d322SPavel Emelyanov }
549fa86d322SPavel Emelyanov 
550be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
551be01d655SYOSHIFUJI Hideaki 					      struct net *net,
552be01d655SYOSHIFUJI Hideaki 					      const void *pkey,
553be01d655SYOSHIFUJI Hideaki 					      int key_len,
554be01d655SYOSHIFUJI Hideaki 					      struct net_device *dev)
555be01d655SYOSHIFUJI Hideaki {
556be01d655SYOSHIFUJI Hideaki 	while (n) {
557be01d655SYOSHIFUJI Hideaki 		if (!memcmp(n->key, pkey, key_len) &&
558be01d655SYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net) &&
559be01d655SYOSHIFUJI Hideaki 		    (n->dev == dev || !n->dev))
560fa86d322SPavel Emelyanov 			return n;
561be01d655SYOSHIFUJI Hideaki 		n = n->next;
562be01d655SYOSHIFUJI Hideaki 	}
563be01d655SYOSHIFUJI Hideaki 	return NULL;
564be01d655SYOSHIFUJI Hideaki }
565be01d655SYOSHIFUJI Hideaki 
566be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
567be01d655SYOSHIFUJI Hideaki 		struct net *net, const void *pkey, struct net_device *dev)
568be01d655SYOSHIFUJI Hideaki {
569be01d655SYOSHIFUJI Hideaki 	int key_len = tbl->key_len;
570be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
571be01d655SYOSHIFUJI Hideaki 
572be01d655SYOSHIFUJI Hideaki 	return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
573be01d655SYOSHIFUJI Hideaki 				 net, pkey, key_len, dev);
574fa86d322SPavel Emelyanov }
5750a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup);
576fa86d322SPavel Emelyanov 
577426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
578426b5303SEric W. Biederman 				    struct net *net, const void *pkey,
5791da177e4SLinus Torvalds 				    struct net_device *dev, int creat)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	struct pneigh_entry *n;
5821da177e4SLinus Torvalds 	int key_len = tbl->key_len;
583be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	read_lock_bh(&tbl->lock);
586be01d655SYOSHIFUJI Hideaki 	n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
587be01d655SYOSHIFUJI Hideaki 			      net, pkey, key_len, dev);
588be01d655SYOSHIFUJI Hideaki 	read_unlock_bh(&tbl->lock);
5891da177e4SLinus Torvalds 
590be01d655SYOSHIFUJI Hideaki 	if (n || !creat)
5911da177e4SLinus Torvalds 		goto out;
5921da177e4SLinus Torvalds 
5934ae28944SPavel Emelyanov 	ASSERT_RTNL();
5944ae28944SPavel Emelyanov 
5951da177e4SLinus Torvalds 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
5961da177e4SLinus Torvalds 	if (!n)
5971da177e4SLinus Torvalds 		goto out;
5981da177e4SLinus Torvalds 
599efd7ef1cSEric W. Biederman 	write_pnet(&n->net, net);
6001da177e4SLinus Torvalds 	memcpy(n->key, pkey, key_len);
6011da177e4SLinus Torvalds 	n->dev = dev;
6021da177e4SLinus Torvalds 	if (dev)
6031da177e4SLinus Torvalds 		dev_hold(dev);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	if (tbl->pconstructor && tbl->pconstructor(n)) {
6061da177e4SLinus Torvalds 		if (dev)
6071da177e4SLinus Torvalds 			dev_put(dev);
6081da177e4SLinus Torvalds 		kfree(n);
6091da177e4SLinus Torvalds 		n = NULL;
6101da177e4SLinus Torvalds 		goto out;
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6141da177e4SLinus Torvalds 	n->next = tbl->phash_buckets[hash_val];
6151da177e4SLinus Torvalds 	tbl->phash_buckets[hash_val] = n;
6161da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6171da177e4SLinus Torvalds out:
6181da177e4SLinus Torvalds 	return n;
6191da177e4SLinus Torvalds }
6200a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 
623426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
6241da177e4SLinus Torvalds 		  struct net_device *dev)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6271da177e4SLinus Torvalds 	int key_len = tbl->key_len;
628be01d655SYOSHIFUJI Hideaki 	u32 hash_val = pneigh_hash(pkey, key_len);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
6311da177e4SLinus Torvalds 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
6321da177e4SLinus Torvalds 	     np = &n->next) {
633426b5303SEric W. Biederman 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
634878628fbSYOSHIFUJI Hideaki 		    net_eq(pneigh_net(n), net)) {
6351da177e4SLinus Torvalds 			*np = n->next;
6361da177e4SLinus Torvalds 			write_unlock_bh(&tbl->lock);
6371da177e4SLinus Torvalds 			if (tbl->pdestructor)
6381da177e4SLinus Torvalds 				tbl->pdestructor(n);
6391da177e4SLinus Torvalds 			if (n->dev)
6401da177e4SLinus Torvalds 				dev_put(n->dev);
6411da177e4SLinus Torvalds 			kfree(n);
6421da177e4SLinus Torvalds 			return 0;
6431da177e4SLinus Torvalds 		}
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
6461da177e4SLinus Torvalds 	return -ENOENT;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
6501da177e4SLinus Torvalds {
6511da177e4SLinus Torvalds 	struct pneigh_entry *n, **np;
6521da177e4SLinus Torvalds 	u32 h;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
6551da177e4SLinus Torvalds 		np = &tbl->phash_buckets[h];
6561da177e4SLinus Torvalds 		while ((n = *np) != NULL) {
6571da177e4SLinus Torvalds 			if (!dev || n->dev == dev) {
6581da177e4SLinus Torvalds 				*np = n->next;
6591da177e4SLinus Torvalds 				if (tbl->pdestructor)
6601da177e4SLinus Torvalds 					tbl->pdestructor(n);
6611da177e4SLinus Torvalds 				if (n->dev)
6621da177e4SLinus Torvalds 					dev_put(n->dev);
6631da177e4SLinus Torvalds 				kfree(n);
6641da177e4SLinus Torvalds 				continue;
6651da177e4SLinus Torvalds 			}
6661da177e4SLinus Torvalds 			np = &n->next;
6671da177e4SLinus Torvalds 		}
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 	return -ENOENT;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
67206f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms);
67306f0511dSDenis V. Lunev 
67406f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms)
67506f0511dSDenis V. Lunev {
67606f0511dSDenis V. Lunev 	if (atomic_dec_and_test(&parms->refcnt))
67706f0511dSDenis V. Lunev 		neigh_parms_destroy(parms);
67806f0511dSDenis V. Lunev }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /*
6811da177e4SLinus Torvalds  *	neighbour must already be out of the table;
6821da177e4SLinus Torvalds  *
6831da177e4SLinus Torvalds  */
6841da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh)
6851da177e4SLinus Torvalds {
686da6a8fa0SDavid Miller 	struct net_device *dev = neigh->dev;
687da6a8fa0SDavid Miller 
6881da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	if (!neigh->dead) {
691e005d193SJoe Perches 		pr_warn("Destroying alive neighbour %p\n", neigh);
6921da177e4SLinus Torvalds 		dump_stack();
6931da177e4SLinus Torvalds 		return;
6941da177e4SLinus Torvalds 	}
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	if (neigh_del_timer(neigh))
697e005d193SJoe Perches 		pr_warn("Impossible event\n");
6981da177e4SLinus Torvalds 
699c9ab4d85SEric Dumazet 	write_lock_bh(&neigh->lock);
700c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
701c9ab4d85SEric Dumazet 	write_unlock_bh(&neigh->lock);
7028b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
7031da177e4SLinus Torvalds 
704447f2191SDavid S. Miller 	if (dev->netdev_ops->ndo_neigh_destroy)
705503eebc2SJiri Pirko 		dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
706447f2191SDavid S. Miller 
707da6a8fa0SDavid Miller 	dev_put(dev);
7081da177e4SLinus Torvalds 	neigh_parms_put(neigh->parms);
7091da177e4SLinus Torvalds 
710d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is destroyed\n", neigh);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	atomic_dec(&neigh->tbl->entries);
7135b8b0060SDavid Miller 	kfree_rcu(neigh, rcu);
7141da177e4SLinus Torvalds }
7150a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /* Neighbour state is suspicious;
7181da177e4SLinus Torvalds    disable fast path.
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds    Called with write_locked neigh.
7211da177e4SLinus Torvalds  */
7221da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh)
7231da177e4SLinus Torvalds {
724d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is suspected\n", neigh);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	neigh->output = neigh->ops->output;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds /* Neighbour state is OK;
7301da177e4SLinus Torvalds    enable fast path.
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds    Called with write_locked neigh.
7331da177e4SLinus Torvalds  */
7341da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh)
7351da177e4SLinus Torvalds {
736d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is connected\n", neigh);
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	neigh->output = neigh->ops->connected_output;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
741e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work)
7421da177e4SLinus Torvalds {
743e4c4e448SEric Dumazet 	struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
744767e97e1SEric Dumazet 	struct neighbour *n;
745767e97e1SEric Dumazet 	struct neighbour __rcu **np;
746e4c4e448SEric Dumazet 	unsigned int i;
747d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
7501da177e4SLinus Torvalds 
751e4c4e448SEric Dumazet 	write_lock_bh(&tbl->lock);
752d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
753d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/*
7561da177e4SLinus Torvalds 	 *	periodically recompute ReachableTime from random function
7571da177e4SLinus Torvalds 	 */
7581da177e4SLinus Torvalds 
759e4c4e448SEric Dumazet 	if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
7601da177e4SLinus Torvalds 		struct neigh_parms *p;
761e4c4e448SEric Dumazet 		tbl->last_rand = jiffies;
76275fbfd33SNicolas Dichtel 		list_for_each_entry(p, &tbl->parms_list, list)
7631da177e4SLinus Torvalds 			p->reachable_time =
7641f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
767feff9ab2SDuan Jiong 	if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
768feff9ab2SDuan Jiong 		goto out;
769feff9ab2SDuan Jiong 
770cd089336SDavid S. Miller 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
771d6bf7817SEric Dumazet 		np = &nht->hash_buckets[i];
7721da177e4SLinus Torvalds 
773767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
774767e97e1SEric Dumazet 				lockdep_is_held(&tbl->lock))) != NULL) {
7751da177e4SLinus Torvalds 			unsigned int state;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 			write_lock(&n->lock);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 			state = n->nud_state;
7801da177e4SLinus Torvalds 			if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
7811da177e4SLinus Torvalds 				write_unlock(&n->lock);
7821da177e4SLinus Torvalds 				goto next_elt;
7831da177e4SLinus Torvalds 			}
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 			if (time_before(n->used, n->confirmed))
7861da177e4SLinus Torvalds 				n->used = n->confirmed;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 			if (atomic_read(&n->refcnt) == 1 &&
7891da177e4SLinus Torvalds 			    (state == NUD_FAILED ||
7901f9248e5SJiri Pirko 			     time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
7911da177e4SLinus Torvalds 				*np = n->next;
7921da177e4SLinus Torvalds 				n->dead = 1;
7931da177e4SLinus Torvalds 				write_unlock(&n->lock);
7944f494554SThomas Graf 				neigh_cleanup_and_release(n);
7951da177e4SLinus Torvalds 				continue;
7961da177e4SLinus Torvalds 			}
7971da177e4SLinus Torvalds 			write_unlock(&n->lock);
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds next_elt:
8001da177e4SLinus Torvalds 			np = &n->next;
8011da177e4SLinus Torvalds 		}
802e4c4e448SEric Dumazet 		/*
803e4c4e448SEric Dumazet 		 * It's fine to release lock here, even if hash table
804e4c4e448SEric Dumazet 		 * grows while we are preempted.
805e4c4e448SEric Dumazet 		 */
806e4c4e448SEric Dumazet 		write_unlock_bh(&tbl->lock);
807e4c4e448SEric Dumazet 		cond_resched();
808e4c4e448SEric Dumazet 		write_lock_bh(&tbl->lock);
80984338a6cSMichel Machado 		nht = rcu_dereference_protected(tbl->nht,
81084338a6cSMichel Machado 						lockdep_is_held(&tbl->lock));
811e4c4e448SEric Dumazet 	}
8122724680bSYOSHIFUJI Hideaki / 吉藤英明 out:
8131f9248e5SJiri Pirko 	/* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
8141f9248e5SJiri Pirko 	 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
8151f9248e5SJiri Pirko 	 * BASE_REACHABLE_TIME.
8161da177e4SLinus Torvalds 	 */
817f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
8181f9248e5SJiri Pirko 			      NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
819e4c4e448SEric Dumazet 	write_unlock_bh(&tbl->lock);
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct neigh_parms *p = n->parms;
8258da86466SYOSHIFUJI Hideaki/吉藤英明 	return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
8268da86466SYOSHIFUJI Hideaki/吉藤英明 	       (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
8278da86466SYOSHIFUJI Hideaki/吉藤英明 	        NEIGH_VAR(p, MCAST_PROBES));
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8305ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh)
8310a141509SEric Dumazet 	__releases(neigh->lock)
8320a141509SEric Dumazet 	__acquires(neigh->lock)
8335ef12d98STimo Teras {
8345ef12d98STimo Teras 	struct sk_buff *skb;
8355ef12d98STimo Teras 
8365ef12d98STimo Teras 	NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
837d5d427cdSJoe Perches 	neigh_dbg(2, "neigh %p is failed\n", neigh);
8385ef12d98STimo Teras 	neigh->updated = jiffies;
8395ef12d98STimo Teras 
8405ef12d98STimo Teras 	/* It is very thin place. report_unreachable is very complicated
8415ef12d98STimo Teras 	   routine. Particularly, it can hit the same neighbour entry!
8425ef12d98STimo Teras 
8435ef12d98STimo Teras 	   So that, we try to be accurate and avoid dead loop. --ANK
8445ef12d98STimo Teras 	 */
8455ef12d98STimo Teras 	while (neigh->nud_state == NUD_FAILED &&
8465ef12d98STimo Teras 	       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
8475ef12d98STimo Teras 		write_unlock(&neigh->lock);
8485ef12d98STimo Teras 		neigh->ops->error_report(neigh, skb);
8495ef12d98STimo Teras 		write_lock(&neigh->lock);
8505ef12d98STimo Teras 	}
851c9ab4d85SEric Dumazet 	__skb_queue_purge(&neigh->arp_queue);
8528b5c171bSEric Dumazet 	neigh->arp_queue_len_bytes = 0;
8535ef12d98STimo Teras }
8545ef12d98STimo Teras 
855cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh)
856cd28ca0aSEric Dumazet 	__releases(neigh->lock)
857cd28ca0aSEric Dumazet {
8584ed377e3SHannes Frederic Sowa 	struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
859cd28ca0aSEric Dumazet 	/* keep skb alive even if arp_queue overflows */
860cd28ca0aSEric Dumazet 	if (skb)
86119125c1aSMartin Zhang 		skb = skb_clone(skb, GFP_ATOMIC);
862cd28ca0aSEric Dumazet 	write_unlock(&neigh->lock);
86348481c8fSEric Dumazet 	if (neigh->ops->solicit)
864cd28ca0aSEric Dumazet 		neigh->ops->solicit(neigh, skb);
865cd28ca0aSEric Dumazet 	atomic_inc(&neigh->probes);
866cd28ca0aSEric Dumazet 	kfree_skb(skb);
867cd28ca0aSEric Dumazet }
868cd28ca0aSEric Dumazet 
8691da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg)
8721da177e4SLinus Torvalds {
8731da177e4SLinus Torvalds 	unsigned long now, next;
8741da177e4SLinus Torvalds 	struct neighbour *neigh = (struct neighbour *)arg;
87595c96174SEric Dumazet 	unsigned int state;
8761da177e4SLinus Torvalds 	int notify = 0;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	write_lock(&neigh->lock);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	state = neigh->nud_state;
8811da177e4SLinus Torvalds 	now = jiffies;
8821da177e4SLinus Torvalds 	next = now + HZ;
8831da177e4SLinus Torvalds 
884045f7b3bSDavid S. Miller 	if (!(state & NUD_IN_TIMER))
8851da177e4SLinus Torvalds 		goto out;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if (state & NUD_REACHABLE) {
8881da177e4SLinus Torvalds 		if (time_before_eq(now,
8891da177e4SLinus Torvalds 				   neigh->confirmed + neigh->parms->reachable_time)) {
890d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is still alive\n", neigh);
8911da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
8921da177e4SLinus Torvalds 		} else if (time_before_eq(now,
8931f9248e5SJiri Pirko 					  neigh->used +
8941f9248e5SJiri Pirko 					  NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
895d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is delayed\n", neigh);
8961da177e4SLinus Torvalds 			neigh->nud_state = NUD_DELAY;
897955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
8981da177e4SLinus Torvalds 			neigh_suspect(neigh);
8991f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
9001da177e4SLinus Torvalds 		} else {
901d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is suspected\n", neigh);
9021da177e4SLinus Torvalds 			neigh->nud_state = NUD_STALE;
903955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9041da177e4SLinus Torvalds 			neigh_suspect(neigh);
9058d71740cSTom Tucker 			notify = 1;
9061da177e4SLinus Torvalds 		}
9071da177e4SLinus Torvalds 	} else if (state & NUD_DELAY) {
9081da177e4SLinus Torvalds 		if (time_before_eq(now,
9091f9248e5SJiri Pirko 				   neigh->confirmed +
9101f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
911d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is now reachable\n", neigh);
9121da177e4SLinus Torvalds 			neigh->nud_state = NUD_REACHABLE;
913955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9141da177e4SLinus Torvalds 			neigh_connect(neigh);
9158d71740cSTom Tucker 			notify = 1;
9161da177e4SLinus Torvalds 			next = neigh->confirmed + neigh->parms->reachable_time;
9171da177e4SLinus Torvalds 		} else {
918d5d427cdSJoe Perches 			neigh_dbg(2, "neigh %p is probed\n", neigh);
9191da177e4SLinus Torvalds 			neigh->nud_state = NUD_PROBE;
920955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9211da177e4SLinus Torvalds 			atomic_set(&neigh->probes, 0);
922765c9c63SErik Kline 			notify = 1;
9231f9248e5SJiri Pirko 			next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
9241da177e4SLinus Torvalds 		}
9251da177e4SLinus Torvalds 	} else {
9261da177e4SLinus Torvalds 		/* NUD_PROBE|NUD_INCOMPLETE */
9271f9248e5SJiri Pirko 		next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
9311da177e4SLinus Torvalds 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
9321da177e4SLinus Torvalds 		neigh->nud_state = NUD_FAILED;
9331da177e4SLinus Torvalds 		notify = 1;
9345ef12d98STimo Teras 		neigh_invalidate(neigh);
9355e2c21dcSDuan Jiong 		goto out;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	if (neigh->nud_state & NUD_IN_TIMER) {
9391da177e4SLinus Torvalds 		if (time_before(next, jiffies + HZ/2))
9401da177e4SLinus Torvalds 			next = jiffies + HZ/2;
9416fb9974fSHerbert Xu 		if (!mod_timer(&neigh->timer, next))
9426fb9974fSHerbert Xu 			neigh_hold(neigh);
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
945cd28ca0aSEric Dumazet 		neigh_probe(neigh);
9469ff56607SDavid S. Miller 	} else {
9471da177e4SLinus Torvalds out:
9481da177e4SLinus Torvalds 		write_unlock(&neigh->lock);
9499ff56607SDavid S. Miller 	}
9501da177e4SLinus Torvalds 
951d961db35SThomas Graf 	if (notify)
9527b8f7a40SRoopa Prabhu 		neigh_update_notify(neigh, 0);
953d961db35SThomas Graf 
9541da177e4SLinus Torvalds 	neigh_release(neigh);
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	int rc;
960cd28ca0aSEric Dumazet 	bool immediate_probe = false;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	rc = 0;
9651da177e4SLinus Torvalds 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
9661da177e4SLinus Torvalds 		goto out_unlock_bh;
9672c51a97fSJulian Anastasov 	if (neigh->dead)
9682c51a97fSJulian Anastasov 		goto out_dead;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
9711f9248e5SJiri Pirko 		if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
9721f9248e5SJiri Pirko 		    NEIGH_VAR(neigh->parms, APP_PROBES)) {
973cd28ca0aSEric Dumazet 			unsigned long next, now = jiffies;
974cd28ca0aSEric Dumazet 
9751f9248e5SJiri Pirko 			atomic_set(&neigh->probes,
9761f9248e5SJiri Pirko 				   NEIGH_VAR(neigh->parms, UCAST_PROBES));
9771da177e4SLinus Torvalds 			neigh->nud_state     = NUD_INCOMPLETE;
978cd28ca0aSEric Dumazet 			neigh->updated = now;
9791f9248e5SJiri Pirko 			next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
9801f9248e5SJiri Pirko 					 HZ/2);
981cd28ca0aSEric Dumazet 			neigh_add_timer(neigh, next);
982cd28ca0aSEric Dumazet 			immediate_probe = true;
9831da177e4SLinus Torvalds 		} else {
9841da177e4SLinus Torvalds 			neigh->nud_state = NUD_FAILED;
985955aaa2fSYOSHIFUJI Hideaki 			neigh->updated = jiffies;
9861da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 			kfree_skb(skb);
9891da177e4SLinus Torvalds 			return 1;
9901da177e4SLinus Torvalds 		}
9911da177e4SLinus Torvalds 	} else if (neigh->nud_state & NUD_STALE) {
992d5d427cdSJoe Perches 		neigh_dbg(2, "neigh %p is delayed\n", neigh);
9931da177e4SLinus Torvalds 		neigh->nud_state = NUD_DELAY;
994955aaa2fSYOSHIFUJI Hideaki 		neigh->updated = jiffies;
9951f9248e5SJiri Pirko 		neigh_add_timer(neigh, jiffies +
9961f9248e5SJiri Pirko 				NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	if (neigh->nud_state == NUD_INCOMPLETE) {
10001da177e4SLinus Torvalds 		if (skb) {
10018b5c171bSEric Dumazet 			while (neigh->arp_queue_len_bytes + skb->truesize >
10021f9248e5SJiri Pirko 			       NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
10031da177e4SLinus Torvalds 				struct sk_buff *buff;
10048b5c171bSEric Dumazet 
1005f72051b0SDavid S. Miller 				buff = __skb_dequeue(&neigh->arp_queue);
10068b5c171bSEric Dumazet 				if (!buff)
10078b5c171bSEric Dumazet 					break;
10088b5c171bSEric Dumazet 				neigh->arp_queue_len_bytes -= buff->truesize;
10091da177e4SLinus Torvalds 				kfree_skb(buff);
10109a6d276eSNeil Horman 				NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
10111da177e4SLinus Torvalds 			}
1012a4731138SEric Dumazet 			skb_dst_force(skb);
10131da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->arp_queue, skb);
10148b5c171bSEric Dumazet 			neigh->arp_queue_len_bytes += skb->truesize;
10151da177e4SLinus Torvalds 		}
10161da177e4SLinus Torvalds 		rc = 1;
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds out_unlock_bh:
1019cd28ca0aSEric Dumazet 	if (immediate_probe)
1020cd28ca0aSEric Dumazet 		neigh_probe(neigh);
1021cd28ca0aSEric Dumazet 	else
1022cd28ca0aSEric Dumazet 		write_unlock(&neigh->lock);
1023cd28ca0aSEric Dumazet 	local_bh_enable();
10241da177e4SLinus Torvalds 	return rc;
10252c51a97fSJulian Anastasov 
10262c51a97fSJulian Anastasov out_dead:
10272c51a97fSJulian Anastasov 	if (neigh->nud_state & NUD_STALE)
10282c51a97fSJulian Anastasov 		goto out_unlock_bh;
10292c51a97fSJulian Anastasov 	write_unlock_bh(&neigh->lock);
10302c51a97fSJulian Anastasov 	kfree_skb(skb);
10312c51a97fSJulian Anastasov 	return 1;
10321da177e4SLinus Torvalds }
10330a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send);
10341da177e4SLinus Torvalds 
1035f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct hh_cache *hh;
10383b04dddeSStephen Hemminger 	void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
103991a72a70SDoug Kehn 		= NULL;
104091a72a70SDoug Kehn 
104191a72a70SDoug Kehn 	if (neigh->dev->header_ops)
104291a72a70SDoug Kehn 		update = neigh->dev->header_ops->cache_update;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	if (update) {
1045f6b72b62SDavid S. Miller 		hh = &neigh->hh;
1046f6b72b62SDavid S. Miller 		if (hh->hh_len) {
10473644f0ceSStephen Hemminger 			write_seqlock_bh(&hh->hh_lock);
10481da177e4SLinus Torvalds 			update(hh, neigh->dev, neigh->ha);
10493644f0ceSStephen Hemminger 			write_sequnlock_bh(&hh->hh_lock);
10501da177e4SLinus Torvalds 		}
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds /* Generic update routine.
10571da177e4SLinus Torvalds    -- lladdr is new lladdr or NULL, if it is not supplied.
10581da177e4SLinus Torvalds    -- new    is new state.
10591da177e4SLinus Torvalds    -- flags
10601da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
10611da177e4SLinus Torvalds 				if it is different.
10621da177e4SLinus Torvalds 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
10631da177e4SLinus Torvalds 				lladdr instead of overriding it
10641da177e4SLinus Torvalds 				if it is different.
10651da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
10681da177e4SLinus Torvalds 				NTF_ROUTER flag.
10691da177e4SLinus Torvalds 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
10701da177e4SLinus Torvalds 				a router.
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds    Caller MUST hold reference count on the entry.
10731da177e4SLinus Torvalds  */
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
10767b8f7a40SRoopa Prabhu 		 u32 flags, u32 nlmsg_pid)
10771da177e4SLinus Torvalds {
10781da177e4SLinus Torvalds 	u8 old;
10791da177e4SLinus Torvalds 	int err;
10801da177e4SLinus Torvalds 	int notify = 0;
10811da177e4SLinus Torvalds 	struct net_device *dev;
10821da177e4SLinus Torvalds 	int update_isrouter = 0;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	write_lock_bh(&neigh->lock);
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	dev    = neigh->dev;
10871da177e4SLinus Torvalds 	old    = neigh->nud_state;
10881da177e4SLinus Torvalds 	err    = -EPERM;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
10911da177e4SLinus Torvalds 	    (old & (NUD_NOARP | NUD_PERMANENT)))
10921da177e4SLinus Torvalds 		goto out;
10932c51a97fSJulian Anastasov 	if (neigh->dead)
10942c51a97fSJulian Anastasov 		goto out;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (!(new & NUD_VALID)) {
10971da177e4SLinus Torvalds 		neigh_del_timer(neigh);
10981da177e4SLinus Torvalds 		if (old & NUD_CONNECTED)
10991da177e4SLinus Torvalds 			neigh_suspect(neigh);
11001da177e4SLinus Torvalds 		neigh->nud_state = new;
11011da177e4SLinus Torvalds 		err = 0;
11021da177e4SLinus Torvalds 		notify = old & NUD_VALID;
11035ef12d98STimo Teras 		if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
11045ef12d98STimo Teras 		    (new & NUD_FAILED)) {
11055ef12d98STimo Teras 			neigh_invalidate(neigh);
11065ef12d98STimo Teras 			notify = 1;
11075ef12d98STimo Teras 		}
11081da177e4SLinus Torvalds 		goto out;
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	/* Compare new lladdr with cached one */
11121da177e4SLinus Torvalds 	if (!dev->addr_len) {
11131da177e4SLinus Torvalds 		/* First case: device needs no address. */
11141da177e4SLinus Torvalds 		lladdr = neigh->ha;
11151da177e4SLinus Torvalds 	} else if (lladdr) {
11161da177e4SLinus Torvalds 		/* The second case: if something is already cached
11171da177e4SLinus Torvalds 		   and a new address is proposed:
11181da177e4SLinus Torvalds 		   - compare new & old
11191da177e4SLinus Torvalds 		   - if they are different, check override flag
11201da177e4SLinus Torvalds 		 */
11211da177e4SLinus Torvalds 		if ((old & NUD_VALID) &&
11221da177e4SLinus Torvalds 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
11231da177e4SLinus Torvalds 			lladdr = neigh->ha;
11241da177e4SLinus Torvalds 	} else {
11251da177e4SLinus Torvalds 		/* No address is supplied; if we know something,
11261da177e4SLinus Torvalds 		   use it, otherwise discard the request.
11271da177e4SLinus Torvalds 		 */
11281da177e4SLinus Torvalds 		err = -EINVAL;
11291da177e4SLinus Torvalds 		if (!(old & NUD_VALID))
11301da177e4SLinus Torvalds 			goto out;
11311da177e4SLinus Torvalds 		lladdr = neigh->ha;
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/* If entry was valid and address is not changed,
11351da177e4SLinus Torvalds 	   do not change entry state, if new one is STALE.
11361da177e4SLinus Torvalds 	 */
11371da177e4SLinus Torvalds 	err = 0;
11381da177e4SLinus Torvalds 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
11391da177e4SLinus Torvalds 	if (old & NUD_VALID) {
11401da177e4SLinus Torvalds 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
11411da177e4SLinus Torvalds 			update_isrouter = 0;
11421da177e4SLinus Torvalds 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
11431da177e4SLinus Torvalds 			    (old & NUD_CONNECTED)) {
11441da177e4SLinus Torvalds 				lladdr = neigh->ha;
11451da177e4SLinus Torvalds 				new = NUD_STALE;
11461da177e4SLinus Torvalds 			} else
11471da177e4SLinus Torvalds 				goto out;
11481da177e4SLinus Torvalds 		} else {
11490e7bbcc1SJulian Anastasov 			if (lladdr == neigh->ha && new == NUD_STALE &&
11500e7bbcc1SJulian Anastasov 			    !(flags & NEIGH_UPDATE_F_ADMIN))
11511da177e4SLinus Torvalds 				new = old;
11521da177e4SLinus Torvalds 		}
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
115577d71233SIhar Hrachyshka 	/* Update timestamps only once we know we will make a change to the
115677d71233SIhar Hrachyshka 	 * neighbour entry. Otherwise we risk to move the locktime window with
115777d71233SIhar Hrachyshka 	 * noop updates and ignore relevant ARP updates.
115877d71233SIhar Hrachyshka 	 */
115977d71233SIhar Hrachyshka 	if (new != old || lladdr != neigh->ha) {
116077d71233SIhar Hrachyshka 		if (new & NUD_CONNECTED)
116177d71233SIhar Hrachyshka 			neigh->confirmed = jiffies;
116277d71233SIhar Hrachyshka 		neigh->updated = jiffies;
116377d71233SIhar Hrachyshka 	}
116477d71233SIhar Hrachyshka 
11651da177e4SLinus Torvalds 	if (new != old) {
11661da177e4SLinus Torvalds 		neigh_del_timer(neigh);
1167765c9c63SErik Kline 		if (new & NUD_PROBE)
1168765c9c63SErik Kline 			atomic_set(&neigh->probes, 0);
1169a43d8994SPavel Emelyanov 		if (new & NUD_IN_TIMER)
1170667347f1SDavid S. Miller 			neigh_add_timer(neigh, (jiffies +
11711da177e4SLinus Torvalds 						((new & NUD_REACHABLE) ?
1172667347f1SDavid S. Miller 						 neigh->parms->reachable_time :
1173667347f1SDavid S. Miller 						 0)));
11741da177e4SLinus Torvalds 		neigh->nud_state = new;
117553385d2dSBob Gilligan 		notify = 1;
11761da177e4SLinus Torvalds 	}
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	if (lladdr != neigh->ha) {
11790ed8ddf4SEric Dumazet 		write_seqlock(&neigh->ha_lock);
11801da177e4SLinus Torvalds 		memcpy(&neigh->ha, lladdr, dev->addr_len);
11810ed8ddf4SEric Dumazet 		write_sequnlock(&neigh->ha_lock);
11821da177e4SLinus Torvalds 		neigh_update_hhs(neigh);
11831da177e4SLinus Torvalds 		if (!(new & NUD_CONNECTED))
11841da177e4SLinus Torvalds 			neigh->confirmed = jiffies -
11851f9248e5SJiri Pirko 				      (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
11861da177e4SLinus Torvalds 		notify = 1;
11871da177e4SLinus Torvalds 	}
11881da177e4SLinus Torvalds 	if (new == old)
11891da177e4SLinus Torvalds 		goto out;
11901da177e4SLinus Torvalds 	if (new & NUD_CONNECTED)
11911da177e4SLinus Torvalds 		neigh_connect(neigh);
11921da177e4SLinus Torvalds 	else
11931da177e4SLinus Torvalds 		neigh_suspect(neigh);
11941da177e4SLinus Torvalds 	if (!(old & NUD_VALID)) {
11951da177e4SLinus Torvalds 		struct sk_buff *skb;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		/* Again: avoid dead loop if something went wrong */
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		while (neigh->nud_state & NUD_VALID &&
12001da177e4SLinus Torvalds 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
120169cce1d1SDavid S. Miller 			struct dst_entry *dst = skb_dst(skb);
120269cce1d1SDavid S. Miller 			struct neighbour *n2, *n1 = neigh;
12031da177e4SLinus Torvalds 			write_unlock_bh(&neigh->lock);
1204e049f288Sroy.qing.li@gmail.com 
1205e049f288Sroy.qing.li@gmail.com 			rcu_read_lock();
120613a43d94SDavid S. Miller 
120713a43d94SDavid S. Miller 			/* Why not just use 'neigh' as-is?  The problem is that
120813a43d94SDavid S. Miller 			 * things such as shaper, eql, and sch_teql can end up
120913a43d94SDavid S. Miller 			 * using alternative, different, neigh objects to output
121013a43d94SDavid S. Miller 			 * the packet in the output path.  So what we need to do
121113a43d94SDavid S. Miller 			 * here is re-lookup the top-level neigh in the path so
121213a43d94SDavid S. Miller 			 * we can reinject the packet there.
121313a43d94SDavid S. Miller 			 */
121413a43d94SDavid S. Miller 			n2 = NULL;
121513a43d94SDavid S. Miller 			if (dst) {
121613a43d94SDavid S. Miller 				n2 = dst_neigh_lookup_skb(dst, skb);
121713a43d94SDavid S. Miller 				if (n2)
121869cce1d1SDavid S. Miller 					n1 = n2;
121913a43d94SDavid S. Miller 			}
12208f40b161SDavid S. Miller 			n1->output(n1, skb);
122113a43d94SDavid S. Miller 			if (n2)
122213a43d94SDavid S. Miller 				neigh_release(n2);
1223e049f288Sroy.qing.li@gmail.com 			rcu_read_unlock();
1224e049f288Sroy.qing.li@gmail.com 
12251da177e4SLinus Torvalds 			write_lock_bh(&neigh->lock);
12261da177e4SLinus Torvalds 		}
1227c9ab4d85SEric Dumazet 		__skb_queue_purge(&neigh->arp_queue);
12288b5c171bSEric Dumazet 		neigh->arp_queue_len_bytes = 0;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds out:
12311da177e4SLinus Torvalds 	if (update_isrouter) {
12321da177e4SLinus Torvalds 		neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
12331da177e4SLinus Torvalds 			(neigh->flags | NTF_ROUTER) :
12341da177e4SLinus Torvalds 			(neigh->flags & ~NTF_ROUTER);
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds 	write_unlock_bh(&neigh->lock);
12378d71740cSTom Tucker 
12388d71740cSTom Tucker 	if (notify)
12397b8f7a40SRoopa Prabhu 		neigh_update_notify(neigh, nlmsg_pid);
1240d961db35SThomas Graf 
12411da177e4SLinus Torvalds 	return err;
12421da177e4SLinus Torvalds }
12430a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update);
12441da177e4SLinus Torvalds 
12457e980569SJiri Benc /* Update the neigh to listen temporarily for probe responses, even if it is
12467e980569SJiri Benc  * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
12477e980569SJiri Benc  */
12487e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh)
12497e980569SJiri Benc {
12502c51a97fSJulian Anastasov 	if (neigh->dead)
12512c51a97fSJulian Anastasov 		return;
12527e980569SJiri Benc 	neigh->updated = jiffies;
12537e980569SJiri Benc 	if (!(neigh->nud_state & NUD_FAILED))
12547e980569SJiri Benc 		return;
12552176d5d4SDuan Jiong 	neigh->nud_state = NUD_INCOMPLETE;
12562176d5d4SDuan Jiong 	atomic_set(&neigh->probes, neigh_max_probes(neigh));
12577e980569SJiri Benc 	neigh_add_timer(neigh,
12587e980569SJiri Benc 			jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
12597e980569SJiri Benc }
12607e980569SJiri Benc EXPORT_SYMBOL(__neigh_set_probe_once);
12617e980569SJiri Benc 
12621da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl,
12631da177e4SLinus Torvalds 				 u8 *lladdr, void *saddr,
12641da177e4SLinus Torvalds 				 struct net_device *dev)
12651da177e4SLinus Torvalds {
12661da177e4SLinus Torvalds 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
12671da177e4SLinus Torvalds 						 lladdr || !dev->addr_len);
12681da177e4SLinus Torvalds 	if (neigh)
12691da177e4SLinus Torvalds 		neigh_update(neigh, lladdr, NUD_STALE,
12707b8f7a40SRoopa Prabhu 			     NEIGH_UPDATE_F_OVERRIDE, 0);
12711da177e4SLinus Torvalds 	return neigh;
12721da177e4SLinus Torvalds }
12730a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns);
12741da177e4SLinus Torvalds 
127534d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */
1276bdf53c58SEric W. Biederman static void neigh_hh_init(struct neighbour *n)
12771da177e4SLinus Torvalds {
1278bdf53c58SEric W. Biederman 	struct net_device *dev = n->dev;
1279bdf53c58SEric W. Biederman 	__be16 prot = n->tbl->protocol;
1280f6b72b62SDavid S. Miller 	struct hh_cache	*hh = &n->hh;
12810ed8ddf4SEric Dumazet 
12820ed8ddf4SEric Dumazet 	write_lock_bh(&n->lock);
128334d101ddSEric Dumazet 
1284f6b72b62SDavid S. Miller 	/* Only one thread can come in here and initialize the
1285f6b72b62SDavid S. Miller 	 * hh_cache entry.
1286f6b72b62SDavid S. Miller 	 */
1287b23b5455SDavid S. Miller 	if (!hh->hh_len)
1288b23b5455SDavid S. Miller 		dev->header_ops->cache(n, hh, prot);
1289f6b72b62SDavid S. Miller 
12900ed8ddf4SEric Dumazet 	write_unlock_bh(&n->lock);
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds /* Slow and careful. */
12941da177e4SLinus Torvalds 
12958f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
12961da177e4SLinus Torvalds {
12971da177e4SLinus Torvalds 	int rc = 0;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	if (!neigh_event_send(neigh, skb)) {
13001da177e4SLinus Torvalds 		int err;
13011da177e4SLinus Torvalds 		struct net_device *dev = neigh->dev;
13020ed8ddf4SEric Dumazet 		unsigned int seq;
130334d101ddSEric Dumazet 
1304f6b72b62SDavid S. Miller 		if (dev->header_ops->cache && !neigh->hh.hh_len)
1305bdf53c58SEric W. Biederman 			neigh_hh_init(neigh);
130634d101ddSEric Dumazet 
13070ed8ddf4SEric Dumazet 		do {
1308e1f16503Sramesh.nagappa@gmail.com 			__skb_pull(skb, skb_network_offset(skb));
13090ed8ddf4SEric Dumazet 			seq = read_seqbegin(&neigh->ha_lock);
13100c4e8581SStephen Hemminger 			err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13111da177e4SLinus Torvalds 					      neigh->ha, NULL, skb->len);
13120ed8ddf4SEric Dumazet 		} while (read_seqretry(&neigh->ha_lock, seq));
131334d101ddSEric Dumazet 
13141da177e4SLinus Torvalds 		if (err >= 0)
1315542d4d68SDavid S. Miller 			rc = dev_queue_xmit(skb);
13161da177e4SLinus Torvalds 		else
13171da177e4SLinus Torvalds 			goto out_kfree_skb;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds out:
13201da177e4SLinus Torvalds 	return rc;
13211da177e4SLinus Torvalds out_kfree_skb:
13221da177e4SLinus Torvalds 	rc = -EINVAL;
13231da177e4SLinus Torvalds 	kfree_skb(skb);
13241da177e4SLinus Torvalds 	goto out;
13251da177e4SLinus Torvalds }
13260a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output);
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds /* As fast as possible without hh cache */
13291da177e4SLinus Torvalds 
13308f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
13311da177e4SLinus Torvalds {
13321da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
13330ed8ddf4SEric Dumazet 	unsigned int seq;
13348f40b161SDavid S. Miller 	int err;
13351da177e4SLinus Torvalds 
13360ed8ddf4SEric Dumazet 	do {
1337e1f16503Sramesh.nagappa@gmail.com 		__skb_pull(skb, skb_network_offset(skb));
13380ed8ddf4SEric Dumazet 		seq = read_seqbegin(&neigh->ha_lock);
13390c4e8581SStephen Hemminger 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
13401da177e4SLinus Torvalds 				      neigh->ha, NULL, skb->len);
13410ed8ddf4SEric Dumazet 	} while (read_seqretry(&neigh->ha_lock, seq));
13420ed8ddf4SEric Dumazet 
13431da177e4SLinus Torvalds 	if (err >= 0)
1344542d4d68SDavid S. Miller 		err = dev_queue_xmit(skb);
13451da177e4SLinus Torvalds 	else {
13461da177e4SLinus Torvalds 		err = -EINVAL;
13471da177e4SLinus Torvalds 		kfree_skb(skb);
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 	return err;
13501da177e4SLinus Torvalds }
13510a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output);
13521da177e4SLinus Torvalds 
13538f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
13548f40b161SDavid S. Miller {
13558f40b161SDavid S. Miller 	return dev_queue_xmit(skb);
13568f40b161SDavid S. Miller }
13578f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output);
13588f40b161SDavid S. Miller 
13591da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	struct neigh_table *tbl = (struct neigh_table *)arg;
13621da177e4SLinus Torvalds 	long sched_next = 0;
13631da177e4SLinus Torvalds 	unsigned long now = jiffies;
1364f72051b0SDavid S. Miller 	struct sk_buff *skb, *n;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
13671da177e4SLinus Torvalds 
1368f72051b0SDavid S. Miller 	skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1369f72051b0SDavid S. Miller 		long tdif = NEIGH_CB(skb)->sched_next - now;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 		if (tdif <= 0) {
1372f72051b0SDavid S. Miller 			struct net_device *dev = skb->dev;
137320e6074eSEric Dumazet 
1374f72051b0SDavid S. Miller 			__skb_unlink(skb, &tbl->proxy_queue);
137520e6074eSEric Dumazet 			if (tbl->proxy_redo && netif_running(dev)) {
137620e6074eSEric Dumazet 				rcu_read_lock();
1377f72051b0SDavid S. Miller 				tbl->proxy_redo(skb);
137820e6074eSEric Dumazet 				rcu_read_unlock();
137920e6074eSEric Dumazet 			} else {
1380f72051b0SDavid S. Miller 				kfree_skb(skb);
138120e6074eSEric Dumazet 			}
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 			dev_put(dev);
13841da177e4SLinus Torvalds 		} else if (!sched_next || tdif < sched_next)
13851da177e4SLinus Torvalds 			sched_next = tdif;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 	del_timer(&tbl->proxy_timer);
13881da177e4SLinus Torvalds 	if (sched_next)
13891da177e4SLinus Torvalds 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
13901da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
13941da177e4SLinus Torvalds 		    struct sk_buff *skb)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	unsigned long now = jiffies;
139763862b5bSAruna-Hewapathirane 
139863862b5bSAruna-Hewapathirane 	unsigned long sched_next = now + (prandom_u32() %
13991f9248e5SJiri Pirko 					  NEIGH_VAR(p, PROXY_DELAY));
14001da177e4SLinus Torvalds 
14011f9248e5SJiri Pirko 	if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
14021da177e4SLinus Torvalds 		kfree_skb(skb);
14031da177e4SLinus Torvalds 		return;
14041da177e4SLinus Torvalds 	}
1405a61bbcf2SPatrick McHardy 
1406a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->sched_next = sched_next;
1407a61bbcf2SPatrick McHardy 	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	spin_lock(&tbl->proxy_queue.lock);
14101da177e4SLinus Torvalds 	if (del_timer(&tbl->proxy_timer)) {
14111da177e4SLinus Torvalds 		if (time_before(tbl->proxy_timer.expires, sched_next))
14121da177e4SLinus Torvalds 			sched_next = tbl->proxy_timer.expires;
14131da177e4SLinus Torvalds 	}
1414adf30907SEric Dumazet 	skb_dst_drop(skb);
14151da177e4SLinus Torvalds 	dev_hold(skb->dev);
14161da177e4SLinus Torvalds 	__skb_queue_tail(&tbl->proxy_queue, skb);
14171da177e4SLinus Torvalds 	mod_timer(&tbl->proxy_timer, sched_next);
14181da177e4SLinus Torvalds 	spin_unlock(&tbl->proxy_queue.lock);
14191da177e4SLinus Torvalds }
14200a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue);
14211da177e4SLinus Torvalds 
142297fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1423426b5303SEric W. Biederman 						      struct net *net, int ifindex)
1424426b5303SEric W. Biederman {
1425426b5303SEric W. Biederman 	struct neigh_parms *p;
1426426b5303SEric W. Biederman 
142775fbfd33SNicolas Dichtel 	list_for_each_entry(p, &tbl->parms_list, list) {
1428878628fbSYOSHIFUJI Hideaki 		if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1429170d6f99SGao feng 		    (!p->dev && !ifindex && net_eq(net, &init_net)))
1430426b5303SEric W. Biederman 			return p;
1431426b5303SEric W. Biederman 	}
1432426b5303SEric W. Biederman 
1433426b5303SEric W. Biederman 	return NULL;
1434426b5303SEric W. Biederman }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
14371da177e4SLinus Torvalds 				      struct neigh_table *tbl)
14381da177e4SLinus Torvalds {
1439cf89d6b2SGao feng 	struct neigh_parms *p;
144000829823SStephen Hemminger 	struct net *net = dev_net(dev);
144100829823SStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14421da177e4SLinus Torvalds 
1443cf89d6b2SGao feng 	p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
14441da177e4SLinus Torvalds 	if (p) {
14451da177e4SLinus Torvalds 		p->tbl		  = tbl;
14461da177e4SLinus Torvalds 		atomic_set(&p->refcnt, 1);
14471da177e4SLinus Torvalds 		p->reachable_time =
14481f9248e5SJiri Pirko 				neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1449c7fb64dbSThomas Graf 		dev_hold(dev);
1450c7fb64dbSThomas Graf 		p->dev = dev;
1451efd7ef1cSEric W. Biederman 		write_pnet(&p->net, net);
14521da177e4SLinus Torvalds 		p->sysctl_table = NULL;
145363134803SVeaceslav Falico 
145463134803SVeaceslav Falico 		if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
145563134803SVeaceslav Falico 			dev_put(dev);
145663134803SVeaceslav Falico 			kfree(p);
145763134803SVeaceslav Falico 			return NULL;
145863134803SVeaceslav Falico 		}
145963134803SVeaceslav Falico 
14601da177e4SLinus Torvalds 		write_lock_bh(&tbl->lock);
146175fbfd33SNicolas Dichtel 		list_add(&p->list, &tbl->parms.list);
14621da177e4SLinus Torvalds 		write_unlock_bh(&tbl->lock);
14631d4c8c29SJiri Pirko 
14641d4c8c29SJiri Pirko 		neigh_parms_data_state_cleanall(p);
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 	return p;
14671da177e4SLinus Torvalds }
14680a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc);
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	struct neigh_parms *parms =
14731da177e4SLinus Torvalds 		container_of(head, struct neigh_parms, rcu_head);
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	neigh_parms_put(parms);
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	if (!parms || parms == &tbl->parms)
14811da177e4SLinus Torvalds 		return;
14821da177e4SLinus Torvalds 	write_lock_bh(&tbl->lock);
148375fbfd33SNicolas Dichtel 	list_del(&parms->list);
14841da177e4SLinus Torvalds 	parms->dead = 1;
14851da177e4SLinus Torvalds 	write_unlock_bh(&tbl->lock);
1486cecbb639SDavid S. Miller 	if (parms->dev)
1487cecbb639SDavid S. Miller 		dev_put(parms->dev);
14881da177e4SLinus Torvalds 	call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
14891da177e4SLinus Torvalds }
14900a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release);
14911da177e4SLinus Torvalds 
149206f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	kfree(parms);
14951da177e4SLinus Torvalds }
14961da177e4SLinus Torvalds 
1497c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class;
1498c2ecba71SPavel Emelianov 
1499d7480fd3SWANG Cong static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1500d7480fd3SWANG Cong 
1501d7480fd3SWANG Cong void neigh_table_init(int index, struct neigh_table *tbl)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	unsigned long now = jiffies;
15041da177e4SLinus Torvalds 	unsigned long phsize;
15051da177e4SLinus Torvalds 
150675fbfd33SNicolas Dichtel 	INIT_LIST_HEAD(&tbl->parms_list);
150775fbfd33SNicolas Dichtel 	list_add(&tbl->parms.list, &tbl->parms_list);
1508e42ea986SEric Dumazet 	write_pnet(&tbl->parms.net, &init_net);
15091da177e4SLinus Torvalds 	atomic_set(&tbl->parms.refcnt, 1);
15101da177e4SLinus Torvalds 	tbl->parms.reachable_time =
15111f9248e5SJiri Pirko 			  neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	tbl->stats = alloc_percpu(struct neigh_statistics);
15141da177e4SLinus Torvalds 	if (!tbl->stats)
15151da177e4SLinus Torvalds 		panic("cannot create neighbour cache statistics");
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
15189b739ba5SAlexey Dobriyan 	if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
15199b739ba5SAlexey Dobriyan 			      &neigh_stat_seq_fops, tbl))
15201da177e4SLinus Torvalds 		panic("cannot create neighbour proc dir entry");
15211da177e4SLinus Torvalds #endif
15221da177e4SLinus Torvalds 
1523cd089336SDavid S. Miller 	RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
152677d04bd9SAndrew Morton 	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
15271da177e4SLinus Torvalds 
1528d6bf7817SEric Dumazet 	if (!tbl->nht || !tbl->phash_buckets)
15291da177e4SLinus Torvalds 		panic("cannot allocate neighbour cache hashes");
15301da177e4SLinus Torvalds 
153108433effSYOSHIFUJI Hideaki / 吉藤英明 	if (!tbl->entry_size)
153208433effSYOSHIFUJI Hideaki / 吉藤英明 		tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
153308433effSYOSHIFUJI Hideaki / 吉藤英明 					tbl->key_len, NEIGH_PRIV_ALIGN);
153408433effSYOSHIFUJI Hideaki / 吉藤英明 	else
153508433effSYOSHIFUJI Hideaki / 吉藤英明 		WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
153608433effSYOSHIFUJI Hideaki / 吉藤英明 
15371da177e4SLinus Torvalds 	rwlock_init(&tbl->lock);
1538203b42f7STejun Heo 	INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1539f618002bSviresh kumar 	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1540f618002bSviresh kumar 			tbl->parms.reachable_time);
1541b24b8a24SPavel Emelyanov 	setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
1542c2ecba71SPavel Emelianov 	skb_queue_head_init_class(&tbl->proxy_queue,
1543c2ecba71SPavel Emelianov 			&neigh_table_proxy_queue_class);
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	tbl->last_flush = now;
15461da177e4SLinus Torvalds 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1547bd89efc5SSimon Kelley 
1548d7480fd3SWANG Cong 	neigh_tables[index] = tbl;
15491da177e4SLinus Torvalds }
15500a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init);
15511da177e4SLinus Torvalds 
1552d7480fd3SWANG Cong int neigh_table_clear(int index, struct neigh_table *tbl)
15531da177e4SLinus Torvalds {
1554d7480fd3SWANG Cong 	neigh_tables[index] = NULL;
15551da177e4SLinus Torvalds 	/* It is not clean... Fix it to unload IPv6 module safely */
1556a5c30b34STejun Heo 	cancel_delayed_work_sync(&tbl->gc_work);
15571da177e4SLinus Torvalds 	del_timer_sync(&tbl->proxy_timer);
15581da177e4SLinus Torvalds 	pneigh_queue_purge(&tbl->proxy_queue);
15591da177e4SLinus Torvalds 	neigh_ifdown(tbl, NULL);
15601da177e4SLinus Torvalds 	if (atomic_read(&tbl->entries))
1561e005d193SJoe Perches 		pr_crit("neighbour leakage\n");
15621da177e4SLinus Torvalds 
15636193d2beSEric Dumazet 	call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
15646193d2beSEric Dumazet 		 neigh_hash_free_rcu);
1565d6bf7817SEric Dumazet 	tbl->nht = NULL;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	kfree(tbl->phash_buckets);
15681da177e4SLinus Torvalds 	tbl->phash_buckets = NULL;
15691da177e4SLinus Torvalds 
15703f192b5cSAlexey Dobriyan 	remove_proc_entry(tbl->id, init_net.proc_net_stat);
15713f192b5cSAlexey Dobriyan 
15723fcde74bSKirill Korotaev 	free_percpu(tbl->stats);
15733fcde74bSKirill Korotaev 	tbl->stats = NULL;
15743fcde74bSKirill Korotaev 
15751da177e4SLinus Torvalds 	return 0;
15761da177e4SLinus Torvalds }
15770a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear);
15781da177e4SLinus Torvalds 
1579d7480fd3SWANG Cong static struct neigh_table *neigh_find_table(int family)
1580d7480fd3SWANG Cong {
1581d7480fd3SWANG Cong 	struct neigh_table *tbl = NULL;
1582d7480fd3SWANG Cong 
1583d7480fd3SWANG Cong 	switch (family) {
1584d7480fd3SWANG Cong 	case AF_INET:
1585d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_ARP_TABLE];
1586d7480fd3SWANG Cong 		break;
1587d7480fd3SWANG Cong 	case AF_INET6:
1588d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_ND_TABLE];
1589d7480fd3SWANG Cong 		break;
1590d7480fd3SWANG Cong 	case AF_DECnet:
1591d7480fd3SWANG Cong 		tbl = neigh_tables[NEIGH_DN_TABLE];
1592d7480fd3SWANG Cong 		break;
1593d7480fd3SWANG Cong 	}
1594d7480fd3SWANG Cong 
1595d7480fd3SWANG Cong 	return tbl;
1596d7480fd3SWANG Cong }
1597d7480fd3SWANG Cong 
1598c21ef3e3SDavid Ahern static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1599c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
16001da177e4SLinus Torvalds {
16013b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1602a14a49d2SThomas Graf 	struct ndmsg *ndm;
1603a14a49d2SThomas Graf 	struct nlattr *dst_attr;
16041da177e4SLinus Torvalds 	struct neigh_table *tbl;
1605d7480fd3SWANG Cong 	struct neighbour *neigh;
16061da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1607a14a49d2SThomas Graf 	int err = -EINVAL;
16081da177e4SLinus Torvalds 
1609110b2499SEric Dumazet 	ASSERT_RTNL();
1610a14a49d2SThomas Graf 	if (nlmsg_len(nlh) < sizeof(*ndm))
16111da177e4SLinus Torvalds 		goto out;
16121da177e4SLinus Torvalds 
1613a14a49d2SThomas Graf 	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1614a14a49d2SThomas Graf 	if (dst_attr == NULL)
1615a14a49d2SThomas Graf 		goto out;
1616a14a49d2SThomas Graf 
1617a14a49d2SThomas Graf 	ndm = nlmsg_data(nlh);
1618a14a49d2SThomas Graf 	if (ndm->ndm_ifindex) {
1619110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1620a14a49d2SThomas Graf 		if (dev == NULL) {
1621a14a49d2SThomas Graf 			err = -ENODEV;
1622a14a49d2SThomas Graf 			goto out;
1623a14a49d2SThomas Graf 		}
1624a14a49d2SThomas Graf 	}
1625a14a49d2SThomas Graf 
1626d7480fd3SWANG Cong 	tbl = neigh_find_table(ndm->ndm_family);
1627d7480fd3SWANG Cong 	if (tbl == NULL)
1628d7480fd3SWANG Cong 		return -EAFNOSUPPORT;
16291da177e4SLinus Torvalds 
1630a14a49d2SThomas Graf 	if (nla_len(dst_attr) < tbl->key_len)
1631110b2499SEric Dumazet 		goto out;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	if (ndm->ndm_flags & NTF_PROXY) {
1634426b5303SEric W. Biederman 		err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1635110b2499SEric Dumazet 		goto out;
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds 
1638a14a49d2SThomas Graf 	if (dev == NULL)
1639110b2499SEric Dumazet 		goto out;
16401da177e4SLinus Torvalds 
1641a14a49d2SThomas Graf 	neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1642a14a49d2SThomas Graf 	if (neigh == NULL) {
1643a14a49d2SThomas Graf 		err = -ENOENT;
1644110b2499SEric Dumazet 		goto out;
1645a14a49d2SThomas Graf 	}
1646a14a49d2SThomas Graf 
1647a14a49d2SThomas Graf 	err = neigh_update(neigh, NULL, NUD_FAILED,
16481da177e4SLinus Torvalds 			   NEIGH_UPDATE_F_OVERRIDE |
16497b8f7a40SRoopa Prabhu 			   NEIGH_UPDATE_F_ADMIN,
16507b8f7a40SRoopa Prabhu 			   NETLINK_CB(skb).portid);
1651a14a49d2SThomas Graf 	neigh_release(neigh);
1652a14a49d2SThomas Graf 
16531da177e4SLinus Torvalds out:
16541da177e4SLinus Torvalds 	return err;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
1657c21ef3e3SDavid Ahern static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1658c21ef3e3SDavid Ahern 		     struct netlink_ext_ack *extack)
16591da177e4SLinus Torvalds {
1660d7480fd3SWANG Cong 	int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
16613b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
16625208debdSThomas Graf 	struct ndmsg *ndm;
16635208debdSThomas Graf 	struct nlattr *tb[NDA_MAX+1];
16641da177e4SLinus Torvalds 	struct neigh_table *tbl;
16651da177e4SLinus Torvalds 	struct net_device *dev = NULL;
1666d7480fd3SWANG Cong 	struct neighbour *neigh;
1667d7480fd3SWANG Cong 	void *dst, *lladdr;
16685208debdSThomas Graf 	int err;
16691da177e4SLinus Torvalds 
1670110b2499SEric Dumazet 	ASSERT_RTNL();
1671c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
16725208debdSThomas Graf 	if (err < 0)
16731da177e4SLinus Torvalds 		goto out;
16741da177e4SLinus Torvalds 
16755208debdSThomas Graf 	err = -EINVAL;
16765208debdSThomas Graf 	if (tb[NDA_DST] == NULL)
16775208debdSThomas Graf 		goto out;
16785208debdSThomas Graf 
16795208debdSThomas Graf 	ndm = nlmsg_data(nlh);
16805208debdSThomas Graf 	if (ndm->ndm_ifindex) {
1681110b2499SEric Dumazet 		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
16825208debdSThomas Graf 		if (dev == NULL) {
16835208debdSThomas Graf 			err = -ENODEV;
16845208debdSThomas Graf 			goto out;
16855208debdSThomas Graf 		}
16865208debdSThomas Graf 
16875208debdSThomas Graf 		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1688110b2499SEric Dumazet 			goto out;
16895208debdSThomas Graf 	}
16905208debdSThomas Graf 
1691d7480fd3SWANG Cong 	tbl = neigh_find_table(ndm->ndm_family);
1692d7480fd3SWANG Cong 	if (tbl == NULL)
1693d7480fd3SWANG Cong 		return -EAFNOSUPPORT;
16941da177e4SLinus Torvalds 
16955208debdSThomas Graf 	if (nla_len(tb[NDA_DST]) < tbl->key_len)
1696110b2499SEric Dumazet 		goto out;
16975208debdSThomas Graf 	dst = nla_data(tb[NDA_DST]);
16985208debdSThomas Graf 	lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	if (ndm->ndm_flags & NTF_PROXY) {
170162dd9318SVille Nuorvala 		struct pneigh_entry *pn;
170262dd9318SVille Nuorvala 
17035208debdSThomas Graf 		err = -ENOBUFS;
1704426b5303SEric W. Biederman 		pn = pneigh_lookup(tbl, net, dst, dev, 1);
170562dd9318SVille Nuorvala 		if (pn) {
170662dd9318SVille Nuorvala 			pn->flags = ndm->ndm_flags;
170762dd9318SVille Nuorvala 			err = 0;
170862dd9318SVille Nuorvala 		}
1709110b2499SEric Dumazet 		goto out;
17101da177e4SLinus Torvalds 	}
17111da177e4SLinus Torvalds 
17125208debdSThomas Graf 	if (dev == NULL)
1713110b2499SEric Dumazet 		goto out;
17141da177e4SLinus Torvalds 
17155208debdSThomas Graf 	neigh = neigh_lookup(tbl, dst, dev);
17165208debdSThomas Graf 	if (neigh == NULL) {
17175208debdSThomas Graf 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
17181da177e4SLinus Torvalds 			err = -ENOENT;
1719110b2499SEric Dumazet 			goto out;
17205208debdSThomas Graf 		}
17215208debdSThomas Graf 
17225208debdSThomas Graf 		neigh = __neigh_lookup_errno(tbl, dst, dev);
17235208debdSThomas Graf 		if (IS_ERR(neigh)) {
17245208debdSThomas Graf 			err = PTR_ERR(neigh);
1725110b2499SEric Dumazet 			goto out;
17261da177e4SLinus Torvalds 		}
17275208debdSThomas Graf 	} else {
17285208debdSThomas Graf 		if (nlh->nlmsg_flags & NLM_F_EXCL) {
17295208debdSThomas Graf 			err = -EEXIST;
17305208debdSThomas Graf 			neigh_release(neigh);
1731110b2499SEric Dumazet 			goto out;
17321da177e4SLinus Torvalds 		}
17331da177e4SLinus Torvalds 
17345208debdSThomas Graf 		if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
17355208debdSThomas Graf 			flags &= ~NEIGH_UPDATE_F_OVERRIDE;
17365208debdSThomas Graf 	}
17371da177e4SLinus Torvalds 
17380c5c2d30SEric Biederman 	if (ndm->ndm_flags & NTF_USE) {
17390c5c2d30SEric Biederman 		neigh_event_send(neigh, NULL);
17400c5c2d30SEric Biederman 		err = 0;
17410c5c2d30SEric Biederman 	} else
17427b8f7a40SRoopa Prabhu 		err = neigh_update(neigh, lladdr, ndm->ndm_state, flags,
17437b8f7a40SRoopa Prabhu 				   NETLINK_CB(skb).portid);
17445208debdSThomas Graf 	neigh_release(neigh);
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds out:
17471da177e4SLinus Torvalds 	return err;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
1750c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1751c7fb64dbSThomas Graf {
1752ca860fb3SThomas Graf 	struct nlattr *nest;
1753e386c6ebSThomas Graf 
1754ca860fb3SThomas Graf 	nest = nla_nest_start(skb, NDTA_PARMS);
1755ca860fb3SThomas Graf 	if (nest == NULL)
1756ca860fb3SThomas Graf 		return -ENOBUFS;
1757c7fb64dbSThomas Graf 
17589a6308d7SDavid S. Miller 	if ((parms->dev &&
17599a6308d7SDavid S. Miller 	     nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
17609a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) ||
17611f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
17621f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
17638b5c171bSEric Dumazet 	    /* approximative value for deprecated QUEUE_LEN (in packets) */
17649a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTPA_QUEUE_LEN,
17651f9248e5SJiri Pirko 			NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
17661f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
17671f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
17681f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_UCAST_PROBES,
17691f9248e5SJiri Pirko 			NEIGH_VAR(parms, UCAST_PROBES)) ||
17701f9248e5SJiri Pirko 	    nla_put_u32(skb, NDTPA_MCAST_PROBES,
17711f9248e5SJiri Pirko 			NEIGH_VAR(parms, MCAST_PROBES)) ||
17728da86466SYOSHIFUJI Hideaki/吉藤英明 	    nla_put_u32(skb, NDTPA_MCAST_REPROBES,
17738da86466SYOSHIFUJI Hideaki/吉藤英明 			NEIGH_VAR(parms, MCAST_REPROBES)) ||
17742175d87cSNicolas Dichtel 	    nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
17752175d87cSNicolas Dichtel 			  NDTPA_PAD) ||
17769a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
17772175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
17781f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_GC_STALETIME,
17792175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
17809a6308d7SDavid S. Miller 	    nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
17812175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
17821f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_RETRANS_TIME,
17832175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
17841f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
17852175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
17861f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_PROXY_DELAY,
17872175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
17881f9248e5SJiri Pirko 	    nla_put_msecs(skb, NDTPA_LOCKTIME,
17892175d87cSNicolas Dichtel 			  NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
17909a6308d7SDavid S. Miller 		goto nla_put_failure;
1791ca860fb3SThomas Graf 	return nla_nest_end(skb, nest);
1792c7fb64dbSThomas Graf 
1793ca860fb3SThomas Graf nla_put_failure:
1794bc3ed28cSThomas Graf 	nla_nest_cancel(skb, nest);
1795bc3ed28cSThomas Graf 	return -EMSGSIZE;
1796c7fb64dbSThomas Graf }
1797c7fb64dbSThomas Graf 
1798ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1799ca860fb3SThomas Graf 			      u32 pid, u32 seq, int type, int flags)
1800c7fb64dbSThomas Graf {
1801c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1802c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1803c7fb64dbSThomas Graf 
1804ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1805ca860fb3SThomas Graf 	if (nlh == NULL)
180626932566SPatrick McHardy 		return -EMSGSIZE;
1807c7fb64dbSThomas Graf 
1808ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1809c7fb64dbSThomas Graf 
1810c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1811c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
18129ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
18139ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1814c7fb64dbSThomas Graf 
18159a6308d7SDavid S. Miller 	if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
18162175d87cSNicolas Dichtel 	    nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
18179a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
18189a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
18199a6308d7SDavid S. Miller 	    nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
18209a6308d7SDavid S. Miller 		goto nla_put_failure;
1821c7fb64dbSThomas Graf 	{
1822c7fb64dbSThomas Graf 		unsigned long now = jiffies;
1823c7fb64dbSThomas Graf 		unsigned int flush_delta = now - tbl->last_flush;
1824c7fb64dbSThomas Graf 		unsigned int rand_delta = now - tbl->last_rand;
1825d6bf7817SEric Dumazet 		struct neigh_hash_table *nht;
1826c7fb64dbSThomas Graf 		struct ndt_config ndc = {
1827c7fb64dbSThomas Graf 			.ndtc_key_len		= tbl->key_len,
1828c7fb64dbSThomas Graf 			.ndtc_entry_size	= tbl->entry_size,
1829c7fb64dbSThomas Graf 			.ndtc_entries		= atomic_read(&tbl->entries),
1830c7fb64dbSThomas Graf 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
1831c7fb64dbSThomas Graf 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
1832c7fb64dbSThomas Graf 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
1833c7fb64dbSThomas Graf 		};
1834c7fb64dbSThomas Graf 
1835d6bf7817SEric Dumazet 		rcu_read_lock_bh();
1836d6bf7817SEric Dumazet 		nht = rcu_dereference_bh(tbl->nht);
18372c2aba6cSDavid S. Miller 		ndc.ndtc_hash_rnd = nht->hash_rnd[0];
1838cd089336SDavid S. Miller 		ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
1839d6bf7817SEric Dumazet 		rcu_read_unlock_bh();
1840d6bf7817SEric Dumazet 
18419a6308d7SDavid S. Miller 		if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
18429a6308d7SDavid S. Miller 			goto nla_put_failure;
1843c7fb64dbSThomas Graf 	}
1844c7fb64dbSThomas Graf 
1845c7fb64dbSThomas Graf 	{
1846c7fb64dbSThomas Graf 		int cpu;
1847c7fb64dbSThomas Graf 		struct ndt_stats ndst;
1848c7fb64dbSThomas Graf 
1849c7fb64dbSThomas Graf 		memset(&ndst, 0, sizeof(ndst));
1850c7fb64dbSThomas Graf 
18516f912042SKAMEZAWA Hiroyuki 		for_each_possible_cpu(cpu) {
1852c7fb64dbSThomas Graf 			struct neigh_statistics	*st;
1853c7fb64dbSThomas Graf 
1854c7fb64dbSThomas Graf 			st = per_cpu_ptr(tbl->stats, cpu);
1855c7fb64dbSThomas Graf 			ndst.ndts_allocs		+= st->allocs;
1856c7fb64dbSThomas Graf 			ndst.ndts_destroys		+= st->destroys;
1857c7fb64dbSThomas Graf 			ndst.ndts_hash_grows		+= st->hash_grows;
1858c7fb64dbSThomas Graf 			ndst.ndts_res_failed		+= st->res_failed;
1859c7fb64dbSThomas Graf 			ndst.ndts_lookups		+= st->lookups;
1860c7fb64dbSThomas Graf 			ndst.ndts_hits			+= st->hits;
1861c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
1862c7fb64dbSThomas Graf 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
1863c7fb64dbSThomas Graf 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
1864c7fb64dbSThomas Graf 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
1865fb811395SRick Jones 			ndst.ndts_table_fulls		+= st->table_fulls;
1866c7fb64dbSThomas Graf 		}
1867c7fb64dbSThomas Graf 
1868b676338fSNicolas Dichtel 		if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
1869b676338fSNicolas Dichtel 				  NDTA_PAD))
18709a6308d7SDavid S. Miller 			goto nla_put_failure;
1871c7fb64dbSThomas Graf 	}
1872c7fb64dbSThomas Graf 
1873c7fb64dbSThomas Graf 	BUG_ON(tbl->parms.dev);
1874c7fb64dbSThomas Graf 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1875ca860fb3SThomas Graf 		goto nla_put_failure;
1876c7fb64dbSThomas Graf 
1877c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1878053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1879053c095aSJohannes Berg 	return 0;
1880c7fb64dbSThomas Graf 
1881ca860fb3SThomas Graf nla_put_failure:
1882c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
188326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
188426932566SPatrick McHardy 	return -EMSGSIZE;
1885c7fb64dbSThomas Graf }
1886c7fb64dbSThomas Graf 
1887ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb,
1888ca860fb3SThomas Graf 				    struct neigh_table *tbl,
1889c7fb64dbSThomas Graf 				    struct neigh_parms *parms,
1890ca860fb3SThomas Graf 				    u32 pid, u32 seq, int type,
1891ca860fb3SThomas Graf 				    unsigned int flags)
1892c7fb64dbSThomas Graf {
1893c7fb64dbSThomas Graf 	struct ndtmsg *ndtmsg;
1894c7fb64dbSThomas Graf 	struct nlmsghdr *nlh;
1895c7fb64dbSThomas Graf 
1896ca860fb3SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1897ca860fb3SThomas Graf 	if (nlh == NULL)
189826932566SPatrick McHardy 		return -EMSGSIZE;
1899c7fb64dbSThomas Graf 
1900ca860fb3SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1901c7fb64dbSThomas Graf 
1902c7fb64dbSThomas Graf 	read_lock_bh(&tbl->lock);
1903c7fb64dbSThomas Graf 	ndtmsg->ndtm_family = tbl->family;
19049ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad1   = 0;
19059ef1d4c7SPatrick McHardy 	ndtmsg->ndtm_pad2   = 0;
1906c7fb64dbSThomas Graf 
1907ca860fb3SThomas Graf 	if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1908ca860fb3SThomas Graf 	    neightbl_fill_parms(skb, parms) < 0)
1909ca860fb3SThomas Graf 		goto errout;
1910c7fb64dbSThomas Graf 
1911c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
1912053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1913053c095aSJohannes Berg 	return 0;
1914ca860fb3SThomas Graf errout:
1915c7fb64dbSThomas Graf 	read_unlock_bh(&tbl->lock);
191626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
191726932566SPatrick McHardy 	return -EMSGSIZE;
1918c7fb64dbSThomas Graf }
1919c7fb64dbSThomas Graf 
1920ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
19216b3f8674SThomas Graf 	[NDTA_NAME]		= { .type = NLA_STRING },
19226b3f8674SThomas Graf 	[NDTA_THRESH1]		= { .type = NLA_U32 },
19236b3f8674SThomas Graf 	[NDTA_THRESH2]		= { .type = NLA_U32 },
19246b3f8674SThomas Graf 	[NDTA_THRESH3]		= { .type = NLA_U32 },
19256b3f8674SThomas Graf 	[NDTA_GC_INTERVAL]	= { .type = NLA_U64 },
19266b3f8674SThomas Graf 	[NDTA_PARMS]		= { .type = NLA_NESTED },
19276b3f8674SThomas Graf };
19286b3f8674SThomas Graf 
1929ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
19306b3f8674SThomas Graf 	[NDTPA_IFINDEX]			= { .type = NLA_U32 },
19316b3f8674SThomas Graf 	[NDTPA_QUEUE_LEN]		= { .type = NLA_U32 },
19326b3f8674SThomas Graf 	[NDTPA_PROXY_QLEN]		= { .type = NLA_U32 },
19336b3f8674SThomas Graf 	[NDTPA_APP_PROBES]		= { .type = NLA_U32 },
19346b3f8674SThomas Graf 	[NDTPA_UCAST_PROBES]		= { .type = NLA_U32 },
19356b3f8674SThomas Graf 	[NDTPA_MCAST_PROBES]		= { .type = NLA_U32 },
19368da86466SYOSHIFUJI Hideaki/吉藤英明 	[NDTPA_MCAST_REPROBES]		= { .type = NLA_U32 },
19376b3f8674SThomas Graf 	[NDTPA_BASE_REACHABLE_TIME]	= { .type = NLA_U64 },
19386b3f8674SThomas Graf 	[NDTPA_GC_STALETIME]		= { .type = NLA_U64 },
19396b3f8674SThomas Graf 	[NDTPA_DELAY_PROBE_TIME]	= { .type = NLA_U64 },
19406b3f8674SThomas Graf 	[NDTPA_RETRANS_TIME]		= { .type = NLA_U64 },
19416b3f8674SThomas Graf 	[NDTPA_ANYCAST_DELAY]		= { .type = NLA_U64 },
19426b3f8674SThomas Graf 	[NDTPA_PROXY_DELAY]		= { .type = NLA_U64 },
19436b3f8674SThomas Graf 	[NDTPA_LOCKTIME]		= { .type = NLA_U64 },
19446b3f8674SThomas Graf };
19456b3f8674SThomas Graf 
1946c21ef3e3SDavid Ahern static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
1947c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
1948c7fb64dbSThomas Graf {
19493b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1950c7fb64dbSThomas Graf 	struct neigh_table *tbl;
19516b3f8674SThomas Graf 	struct ndtmsg *ndtmsg;
19526b3f8674SThomas Graf 	struct nlattr *tb[NDTA_MAX+1];
1953d7480fd3SWANG Cong 	bool found = false;
1954d7480fd3SWANG Cong 	int err, tidx;
1955c7fb64dbSThomas Graf 
19566b3f8674SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
1957c21ef3e3SDavid Ahern 			  nl_neightbl_policy, extack);
19586b3f8674SThomas Graf 	if (err < 0)
19596b3f8674SThomas Graf 		goto errout;
1960c7fb64dbSThomas Graf 
19616b3f8674SThomas Graf 	if (tb[NDTA_NAME] == NULL) {
19626b3f8674SThomas Graf 		err = -EINVAL;
19636b3f8674SThomas Graf 		goto errout;
19646b3f8674SThomas Graf 	}
19656b3f8674SThomas Graf 
19666b3f8674SThomas Graf 	ndtmsg = nlmsg_data(nlh);
1967d7480fd3SWANG Cong 
1968d7480fd3SWANG Cong 	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
1969d7480fd3SWANG Cong 		tbl = neigh_tables[tidx];
1970d7480fd3SWANG Cong 		if (!tbl)
1971d7480fd3SWANG Cong 			continue;
1972c7fb64dbSThomas Graf 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1973c7fb64dbSThomas Graf 			continue;
1974d7480fd3SWANG Cong 		if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
1975d7480fd3SWANG Cong 			found = true;
1976c7fb64dbSThomas Graf 			break;
1977c7fb64dbSThomas Graf 		}
1978c7fb64dbSThomas Graf 	}
1979c7fb64dbSThomas Graf 
1980d7480fd3SWANG Cong 	if (!found)
1981d7480fd3SWANG Cong 		return -ENOENT;
1982d7480fd3SWANG Cong 
1983c7fb64dbSThomas Graf 	/*
1984c7fb64dbSThomas Graf 	 * We acquire tbl->lock to be nice to the periodic timers and
1985c7fb64dbSThomas Graf 	 * make sure they always see a consistent set of values.
1986c7fb64dbSThomas Graf 	 */
1987c7fb64dbSThomas Graf 	write_lock_bh(&tbl->lock);
1988c7fb64dbSThomas Graf 
19896b3f8674SThomas Graf 	if (tb[NDTA_PARMS]) {
19906b3f8674SThomas Graf 		struct nlattr *tbp[NDTPA_MAX+1];
1991c7fb64dbSThomas Graf 		struct neigh_parms *p;
19926b3f8674SThomas Graf 		int i, ifindex = 0;
1993c7fb64dbSThomas Graf 
19946b3f8674SThomas Graf 		err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
1995c21ef3e3SDavid Ahern 				       nl_ntbl_parm_policy, extack);
19966b3f8674SThomas Graf 		if (err < 0)
19976b3f8674SThomas Graf 			goto errout_tbl_lock;
1998c7fb64dbSThomas Graf 
19996b3f8674SThomas Graf 		if (tbp[NDTPA_IFINDEX])
20006b3f8674SThomas Graf 			ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2001c7fb64dbSThomas Graf 
200297fd5bc7STobias Klauser 		p = lookup_neigh_parms(tbl, net, ifindex);
2003c7fb64dbSThomas Graf 		if (p == NULL) {
2004c7fb64dbSThomas Graf 			err = -ENOENT;
20056b3f8674SThomas Graf 			goto errout_tbl_lock;
2006c7fb64dbSThomas Graf 		}
2007c7fb64dbSThomas Graf 
20086b3f8674SThomas Graf 		for (i = 1; i <= NDTPA_MAX; i++) {
20096b3f8674SThomas Graf 			if (tbp[i] == NULL)
20106b3f8674SThomas Graf 				continue;
2011c7fb64dbSThomas Graf 
20126b3f8674SThomas Graf 			switch (i) {
20136b3f8674SThomas Graf 			case NDTPA_QUEUE_LEN:
20141f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
20151f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]) *
20161f9248e5SJiri Pirko 					      SKB_TRUESIZE(ETH_FRAME_LEN));
20178b5c171bSEric Dumazet 				break;
20188b5c171bSEric Dumazet 			case NDTPA_QUEUE_LENBYTES:
20191f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
20201f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20216b3f8674SThomas Graf 				break;
20226b3f8674SThomas Graf 			case NDTPA_PROXY_QLEN:
20231f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, PROXY_QLEN,
20241f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20256b3f8674SThomas Graf 				break;
20266b3f8674SThomas Graf 			case NDTPA_APP_PROBES:
20271f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, APP_PROBES,
20281f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20296b3f8674SThomas Graf 				break;
20306b3f8674SThomas Graf 			case NDTPA_UCAST_PROBES:
20311f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, UCAST_PROBES,
20321f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20336b3f8674SThomas Graf 				break;
20346b3f8674SThomas Graf 			case NDTPA_MCAST_PROBES:
20351f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, MCAST_PROBES,
20361f9248e5SJiri Pirko 					      nla_get_u32(tbp[i]));
20376b3f8674SThomas Graf 				break;
20388da86466SYOSHIFUJI Hideaki/吉藤英明 			case NDTPA_MCAST_REPROBES:
20398da86466SYOSHIFUJI Hideaki/吉藤英明 				NEIGH_VAR_SET(p, MCAST_REPROBES,
20408da86466SYOSHIFUJI Hideaki/吉藤英明 					      nla_get_u32(tbp[i]));
20418da86466SYOSHIFUJI Hideaki/吉藤英明 				break;
20426b3f8674SThomas Graf 			case NDTPA_BASE_REACHABLE_TIME:
20431f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
20441f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20454bf6980dSJean-Francois Remy 				/* update reachable_time as well, otherwise, the change will
20464bf6980dSJean-Francois Remy 				 * only be effective after the next time neigh_periodic_work
20474bf6980dSJean-Francois Remy 				 * decides to recompute it (can be multiple minutes)
20484bf6980dSJean-Francois Remy 				 */
20494bf6980dSJean-Francois Remy 				p->reachable_time =
20504bf6980dSJean-Francois Remy 					neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
20516b3f8674SThomas Graf 				break;
20526b3f8674SThomas Graf 			case NDTPA_GC_STALETIME:
20531f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, GC_STALETIME,
20541f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20556b3f8674SThomas Graf 				break;
20566b3f8674SThomas Graf 			case NDTPA_DELAY_PROBE_TIME:
20571f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
20581f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20592a4501aeSIdo Schimmel 				call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
20606b3f8674SThomas Graf 				break;
20616b3f8674SThomas Graf 			case NDTPA_RETRANS_TIME:
20621f9248e5SJiri Pirko 				NEIGH_VAR_SET(p, RETRANS_TIME,
20631f9248e5SJiri Pirko 					      nla_get_msecs(tbp[i]));
20646b3f8674SThomas Graf 				break;
20656b3f8674SThomas Graf 			case NDTPA_ANYCAST_DELAY:
20663977458cSJiri Pirko 				NEIGH_VAR_SET(p, ANYCAST_DELAY,
20673977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
20686b3f8674SThomas Graf 				break;
20696b3f8674SThomas Graf 			case NDTPA_PROXY_DELAY:
20703977458cSJiri Pirko 				NEIGH_VAR_SET(p, PROXY_DELAY,
20713977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
20726b3f8674SThomas Graf 				break;
20736b3f8674SThomas Graf 			case NDTPA_LOCKTIME:
20743977458cSJiri Pirko 				NEIGH_VAR_SET(p, LOCKTIME,
20753977458cSJiri Pirko 					      nla_get_msecs(tbp[i]));
20766b3f8674SThomas Graf 				break;
2077c7fb64dbSThomas Graf 			}
20786b3f8674SThomas Graf 		}
20796b3f8674SThomas Graf 	}
20806b3f8674SThomas Graf 
2081dc25c676SGao feng 	err = -ENOENT;
2082dc25c676SGao feng 	if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2083dc25c676SGao feng 	     tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2084dc25c676SGao feng 	    !net_eq(net, &init_net))
2085dc25c676SGao feng 		goto errout_tbl_lock;
2086dc25c676SGao feng 
20876b3f8674SThomas Graf 	if (tb[NDTA_THRESH1])
20886b3f8674SThomas Graf 		tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
20896b3f8674SThomas Graf 
20906b3f8674SThomas Graf 	if (tb[NDTA_THRESH2])
20916b3f8674SThomas Graf 		tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
20926b3f8674SThomas Graf 
20936b3f8674SThomas Graf 	if (tb[NDTA_THRESH3])
20946b3f8674SThomas Graf 		tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
20956b3f8674SThomas Graf 
20966b3f8674SThomas Graf 	if (tb[NDTA_GC_INTERVAL])
20976b3f8674SThomas Graf 		tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2098c7fb64dbSThomas Graf 
2099c7fb64dbSThomas Graf 	err = 0;
2100c7fb64dbSThomas Graf 
21016b3f8674SThomas Graf errout_tbl_lock:
2102c7fb64dbSThomas Graf 	write_unlock_bh(&tbl->lock);
21036b3f8674SThomas Graf errout:
2104c7fb64dbSThomas Graf 	return err;
2105c7fb64dbSThomas Graf }
2106c7fb64dbSThomas Graf 
2107c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2108c7fb64dbSThomas Graf {
21093b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2110ca860fb3SThomas Graf 	int family, tidx, nidx = 0;
2111ca860fb3SThomas Graf 	int tbl_skip = cb->args[0];
2112ca860fb3SThomas Graf 	int neigh_skip = cb->args[1];
2113c7fb64dbSThomas Graf 	struct neigh_table *tbl;
2114c7fb64dbSThomas Graf 
2115ca860fb3SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2116c7fb64dbSThomas Graf 
2117d7480fd3SWANG Cong 	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2118c7fb64dbSThomas Graf 		struct neigh_parms *p;
2119c7fb64dbSThomas Graf 
2120d7480fd3SWANG Cong 		tbl = neigh_tables[tidx];
2121d7480fd3SWANG Cong 		if (!tbl)
2122d7480fd3SWANG Cong 			continue;
2123d7480fd3SWANG Cong 
2124ca860fb3SThomas Graf 		if (tidx < tbl_skip || (family && tbl->family != family))
2125c7fb64dbSThomas Graf 			continue;
2126c7fb64dbSThomas Graf 
212715e47304SEric W. Biederman 		if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2128ca860fb3SThomas Graf 				       cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
21297b46a644SDavid S. Miller 				       NLM_F_MULTI) < 0)
2130c7fb64dbSThomas Graf 			break;
2131c7fb64dbSThomas Graf 
213275fbfd33SNicolas Dichtel 		nidx = 0;
213375fbfd33SNicolas Dichtel 		p = list_next_entry(&tbl->parms, list);
213475fbfd33SNicolas Dichtel 		list_for_each_entry_from(p, &tbl->parms_list, list) {
2135878628fbSYOSHIFUJI Hideaki 			if (!net_eq(neigh_parms_net(p), net))
2136426b5303SEric W. Biederman 				continue;
2137426b5303SEric W. Biederman 
2138efc683fcSGautam Kachroo 			if (nidx < neigh_skip)
2139efc683fcSGautam Kachroo 				goto next;
2140c7fb64dbSThomas Graf 
2141ca860fb3SThomas Graf 			if (neightbl_fill_param_info(skb, tbl, p,
214215e47304SEric W. Biederman 						     NETLINK_CB(cb->skb).portid,
2143ca860fb3SThomas Graf 						     cb->nlh->nlmsg_seq,
2144ca860fb3SThomas Graf 						     RTM_NEWNEIGHTBL,
21457b46a644SDavid S. Miller 						     NLM_F_MULTI) < 0)
2146c7fb64dbSThomas Graf 				goto out;
2147efc683fcSGautam Kachroo 		next:
2148efc683fcSGautam Kachroo 			nidx++;
2149c7fb64dbSThomas Graf 		}
2150c7fb64dbSThomas Graf 
2151ca860fb3SThomas Graf 		neigh_skip = 0;
2152c7fb64dbSThomas Graf 	}
2153c7fb64dbSThomas Graf out:
2154ca860fb3SThomas Graf 	cb->args[0] = tidx;
2155ca860fb3SThomas Graf 	cb->args[1] = nidx;
2156c7fb64dbSThomas Graf 
2157c7fb64dbSThomas Graf 	return skb->len;
2158c7fb64dbSThomas Graf }
21591da177e4SLinus Torvalds 
21608b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
21618b8aec50SThomas Graf 			   u32 pid, u32 seq, int type, unsigned int flags)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	unsigned long now = jiffies;
21641da177e4SLinus Torvalds 	struct nda_cacheinfo ci;
21658b8aec50SThomas Graf 	struct nlmsghdr *nlh;
21668b8aec50SThomas Graf 	struct ndmsg *ndm;
21671da177e4SLinus Torvalds 
21688b8aec50SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
21698b8aec50SThomas Graf 	if (nlh == NULL)
217026932566SPatrick McHardy 		return -EMSGSIZE;
21718b8aec50SThomas Graf 
21728b8aec50SThomas Graf 	ndm = nlmsg_data(nlh);
21738b8aec50SThomas Graf 	ndm->ndm_family	 = neigh->ops->family;
21749ef1d4c7SPatrick McHardy 	ndm->ndm_pad1    = 0;
21759ef1d4c7SPatrick McHardy 	ndm->ndm_pad2    = 0;
21768b8aec50SThomas Graf 	ndm->ndm_flags	 = neigh->flags;
21778b8aec50SThomas Graf 	ndm->ndm_type	 = neigh->type;
21788b8aec50SThomas Graf 	ndm->ndm_ifindex = neigh->dev->ifindex;
21791da177e4SLinus Torvalds 
21809a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
21819a6308d7SDavid S. Miller 		goto nla_put_failure;
21828b8aec50SThomas Graf 
21838b8aec50SThomas Graf 	read_lock_bh(&neigh->lock);
21848b8aec50SThomas Graf 	ndm->ndm_state	 = neigh->nud_state;
21850ed8ddf4SEric Dumazet 	if (neigh->nud_state & NUD_VALID) {
21860ed8ddf4SEric Dumazet 		char haddr[MAX_ADDR_LEN];
21870ed8ddf4SEric Dumazet 
21880ed8ddf4SEric Dumazet 		neigh_ha_snapshot(haddr, neigh, neigh->dev);
21890ed8ddf4SEric Dumazet 		if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
21908b8aec50SThomas Graf 			read_unlock_bh(&neigh->lock);
21918b8aec50SThomas Graf 			goto nla_put_failure;
21928b8aec50SThomas Graf 		}
21930ed8ddf4SEric Dumazet 	}
21948b8aec50SThomas Graf 
2195b9f5f52cSStephen Hemminger 	ci.ndm_used	 = jiffies_to_clock_t(now - neigh->used);
2196b9f5f52cSStephen Hemminger 	ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2197b9f5f52cSStephen Hemminger 	ci.ndm_updated	 = jiffies_to_clock_t(now - neigh->updated);
21988b8aec50SThomas Graf 	ci.ndm_refcnt	 = atomic_read(&neigh->refcnt) - 1;
21998b8aec50SThomas Graf 	read_unlock_bh(&neigh->lock);
22008b8aec50SThomas Graf 
22019a6308d7SDavid S. Miller 	if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
22029a6308d7SDavid S. Miller 	    nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
22039a6308d7SDavid S. Miller 		goto nla_put_failure;
22048b8aec50SThomas Graf 
2205053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2206053c095aSJohannes Berg 	return 0;
22078b8aec50SThomas Graf 
22088b8aec50SThomas Graf nla_put_failure:
220926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
221026932566SPatrick McHardy 	return -EMSGSIZE;
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds 
221384920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
221484920c14STony Zelenoff 			    u32 pid, u32 seq, int type, unsigned int flags,
221584920c14STony Zelenoff 			    struct neigh_table *tbl)
221684920c14STony Zelenoff {
221784920c14STony Zelenoff 	struct nlmsghdr *nlh;
221884920c14STony Zelenoff 	struct ndmsg *ndm;
221984920c14STony Zelenoff 
222084920c14STony Zelenoff 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
222184920c14STony Zelenoff 	if (nlh == NULL)
222284920c14STony Zelenoff 		return -EMSGSIZE;
222384920c14STony Zelenoff 
222484920c14STony Zelenoff 	ndm = nlmsg_data(nlh);
222584920c14STony Zelenoff 	ndm->ndm_family	 = tbl->family;
222684920c14STony Zelenoff 	ndm->ndm_pad1    = 0;
222784920c14STony Zelenoff 	ndm->ndm_pad2    = 0;
222884920c14STony Zelenoff 	ndm->ndm_flags	 = pn->flags | NTF_PROXY;
2229545469f7SJun Zhao 	ndm->ndm_type	 = RTN_UNICAST;
22306adc5fd6SKonstantin Khlebnikov 	ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
223184920c14STony Zelenoff 	ndm->ndm_state	 = NUD_NONE;
223284920c14STony Zelenoff 
22339a6308d7SDavid S. Miller 	if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
22349a6308d7SDavid S. Miller 		goto nla_put_failure;
223584920c14STony Zelenoff 
2236053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2237053c095aSJohannes Berg 	return 0;
223884920c14STony Zelenoff 
223984920c14STony Zelenoff nla_put_failure:
224084920c14STony Zelenoff 	nlmsg_cancel(skb, nlh);
224184920c14STony Zelenoff 	return -EMSGSIZE;
224284920c14STony Zelenoff }
224384920c14STony Zelenoff 
22447b8f7a40SRoopa Prabhu static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
2245d961db35SThomas Graf {
2246d961db35SThomas Graf 	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
22477b8f7a40SRoopa Prabhu 	__neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
2248d961db35SThomas Graf }
22491da177e4SLinus Torvalds 
225021fdd092SDavid Ahern static bool neigh_master_filtered(struct net_device *dev, int master_idx)
225121fdd092SDavid Ahern {
225221fdd092SDavid Ahern 	struct net_device *master;
225321fdd092SDavid Ahern 
225421fdd092SDavid Ahern 	if (!master_idx)
225521fdd092SDavid Ahern 		return false;
225621fdd092SDavid Ahern 
225721fdd092SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
225821fdd092SDavid Ahern 	if (!master || master->ifindex != master_idx)
225921fdd092SDavid Ahern 		return true;
226021fdd092SDavid Ahern 
226121fdd092SDavid Ahern 	return false;
226221fdd092SDavid Ahern }
226321fdd092SDavid Ahern 
226416660f0bSDavid Ahern static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
226516660f0bSDavid Ahern {
226616660f0bSDavid Ahern 	if (filter_idx && dev->ifindex != filter_idx)
226716660f0bSDavid Ahern 		return true;
226816660f0bSDavid Ahern 
226916660f0bSDavid Ahern 	return false;
227016660f0bSDavid Ahern }
227116660f0bSDavid Ahern 
22721da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
22731da177e4SLinus Torvalds 			    struct netlink_callback *cb)
22741da177e4SLinus Torvalds {
22753b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
227621fdd092SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
227721fdd092SDavid Ahern 	struct nlattr *tb[NDA_MAX + 1];
22781da177e4SLinus Torvalds 	struct neighbour *n;
22791da177e4SLinus Torvalds 	int rc, h, s_h = cb->args[1];
22801da177e4SLinus Torvalds 	int idx, s_idx = idx = cb->args[2];
2281d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
228216660f0bSDavid Ahern 	int filter_master_idx = 0, filter_idx = 0;
228321fdd092SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
228421fdd092SDavid Ahern 	int err;
228521fdd092SDavid Ahern 
2286fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL, NULL);
228721fdd092SDavid Ahern 	if (!err) {
228816660f0bSDavid Ahern 		if (tb[NDA_IFINDEX])
228916660f0bSDavid Ahern 			filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
229016660f0bSDavid Ahern 
229121fdd092SDavid Ahern 		if (tb[NDA_MASTER])
229221fdd092SDavid Ahern 			filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
229321fdd092SDavid Ahern 
229416660f0bSDavid Ahern 		if (filter_idx || filter_master_idx)
229521fdd092SDavid Ahern 			flags |= NLM_F_DUMP_FILTERED;
229621fdd092SDavid Ahern 	}
22971da177e4SLinus Torvalds 
2298d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2299d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2300d6bf7817SEric Dumazet 
23014bd6683bSEric Dumazet 	for (h = s_h; h < (1 << nht->hash_shift); h++) {
23021da177e4SLinus Torvalds 		if (h > s_h)
23031da177e4SLinus Torvalds 			s_idx = 0;
2304767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2305767e97e1SEric Dumazet 		     n != NULL;
2306767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next)) {
230718502acdSZhang Shengju 			if (idx < s_idx || !net_eq(dev_net(n->dev), net))
230818502acdSZhang Shengju 				goto next;
230918502acdSZhang Shengju 			if (neigh_ifindex_filtered(n->dev, filter_idx) ||
231018502acdSZhang Shengju 			    neigh_master_filtered(n->dev, filter_master_idx))
2311efc683fcSGautam Kachroo 				goto next;
231215e47304SEric W. Biederman 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
23131da177e4SLinus Torvalds 					    cb->nlh->nlmsg_seq,
2314b6544c0bSJamal Hadi Salim 					    RTM_NEWNEIGH,
231521fdd092SDavid Ahern 					    flags) < 0) {
23161da177e4SLinus Torvalds 				rc = -1;
23171da177e4SLinus Torvalds 				goto out;
23181da177e4SLinus Torvalds 			}
2319efc683fcSGautam Kachroo next:
2320efc683fcSGautam Kachroo 			idx++;
23211da177e4SLinus Torvalds 		}
23221da177e4SLinus Torvalds 	}
23231da177e4SLinus Torvalds 	rc = skb->len;
23241da177e4SLinus Torvalds out:
2325d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
23261da177e4SLinus Torvalds 	cb->args[1] = h;
23271da177e4SLinus Torvalds 	cb->args[2] = idx;
23281da177e4SLinus Torvalds 	return rc;
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
233184920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
233284920c14STony Zelenoff 			     struct netlink_callback *cb)
233384920c14STony Zelenoff {
233484920c14STony Zelenoff 	struct pneigh_entry *n;
233584920c14STony Zelenoff 	struct net *net = sock_net(skb->sk);
233684920c14STony Zelenoff 	int rc, h, s_h = cb->args[3];
233784920c14STony Zelenoff 	int idx, s_idx = idx = cb->args[4];
233884920c14STony Zelenoff 
233984920c14STony Zelenoff 	read_lock_bh(&tbl->lock);
234084920c14STony Zelenoff 
23414bd6683bSEric Dumazet 	for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
234284920c14STony Zelenoff 		if (h > s_h)
234384920c14STony Zelenoff 			s_idx = 0;
234484920c14STony Zelenoff 		for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
234518502acdSZhang Shengju 			if (idx < s_idx || pneigh_net(n) != net)
234684920c14STony Zelenoff 				goto next;
234715e47304SEric W. Biederman 			if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
234884920c14STony Zelenoff 					    cb->nlh->nlmsg_seq,
234984920c14STony Zelenoff 					    RTM_NEWNEIGH,
23507b46a644SDavid S. Miller 					    NLM_F_MULTI, tbl) < 0) {
235184920c14STony Zelenoff 				read_unlock_bh(&tbl->lock);
235284920c14STony Zelenoff 				rc = -1;
235384920c14STony Zelenoff 				goto out;
235484920c14STony Zelenoff 			}
235584920c14STony Zelenoff 		next:
235684920c14STony Zelenoff 			idx++;
235784920c14STony Zelenoff 		}
235884920c14STony Zelenoff 	}
235984920c14STony Zelenoff 
236084920c14STony Zelenoff 	read_unlock_bh(&tbl->lock);
236184920c14STony Zelenoff 	rc = skb->len;
236284920c14STony Zelenoff out:
236384920c14STony Zelenoff 	cb->args[3] = h;
236484920c14STony Zelenoff 	cb->args[4] = idx;
236584920c14STony Zelenoff 	return rc;
236684920c14STony Zelenoff 
236784920c14STony Zelenoff }
236884920c14STony Zelenoff 
2369c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
23701da177e4SLinus Torvalds {
23711da177e4SLinus Torvalds 	struct neigh_table *tbl;
23721da177e4SLinus Torvalds 	int t, family, s_t;
237384920c14STony Zelenoff 	int proxy = 0;
23744bd6683bSEric Dumazet 	int err;
23751da177e4SLinus Torvalds 
23768b8aec50SThomas Graf 	family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
237784920c14STony Zelenoff 
237884920c14STony Zelenoff 	/* check for full ndmsg structure presence, family member is
237984920c14STony Zelenoff 	 * the same for both structures
238084920c14STony Zelenoff 	 */
238184920c14STony Zelenoff 	if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
238284920c14STony Zelenoff 	    ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
238384920c14STony Zelenoff 		proxy = 1;
238484920c14STony Zelenoff 
23851da177e4SLinus Torvalds 	s_t = cb->args[0];
23861da177e4SLinus Torvalds 
2387d7480fd3SWANG Cong 	for (t = 0; t < NEIGH_NR_TABLES; t++) {
2388d7480fd3SWANG Cong 		tbl = neigh_tables[t];
2389d7480fd3SWANG Cong 
2390d7480fd3SWANG Cong 		if (!tbl)
2391d7480fd3SWANG Cong 			continue;
23921da177e4SLinus Torvalds 		if (t < s_t || (family && tbl->family != family))
23931da177e4SLinus Torvalds 			continue;
23941da177e4SLinus Torvalds 		if (t > s_t)
23951da177e4SLinus Torvalds 			memset(&cb->args[1], 0, sizeof(cb->args) -
23961da177e4SLinus Torvalds 						sizeof(cb->args[0]));
239784920c14STony Zelenoff 		if (proxy)
239884920c14STony Zelenoff 			err = pneigh_dump_table(tbl, skb, cb);
239984920c14STony Zelenoff 		else
240084920c14STony Zelenoff 			err = neigh_dump_table(tbl, skb, cb);
24014bd6683bSEric Dumazet 		if (err < 0)
24024bd6683bSEric Dumazet 			break;
24031da177e4SLinus Torvalds 	}
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	cb->args[0] = t;
24061da177e4SLinus Torvalds 	return skb->len;
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
24101da177e4SLinus Torvalds {
24111da177e4SLinus Torvalds 	int chain;
2412d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
24131da177e4SLinus Torvalds 
2414d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2415d6bf7817SEric Dumazet 	nht = rcu_dereference_bh(tbl->nht);
2416d6bf7817SEric Dumazet 
2417767e97e1SEric Dumazet 	read_lock(&tbl->lock); /* avoid resizes */
2418cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
24191da177e4SLinus Torvalds 		struct neighbour *n;
24201da177e4SLinus Torvalds 
2421767e97e1SEric Dumazet 		for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2422767e97e1SEric Dumazet 		     n != NULL;
2423767e97e1SEric Dumazet 		     n = rcu_dereference_bh(n->next))
24241da177e4SLinus Torvalds 			cb(n, cookie);
24251da177e4SLinus Torvalds 	}
2426d6bf7817SEric Dumazet 	read_unlock(&tbl->lock);
2427d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each);
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */
24321da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl,
24331da177e4SLinus Torvalds 			      int (*cb)(struct neighbour *))
24341da177e4SLinus Torvalds {
24351da177e4SLinus Torvalds 	int chain;
2436d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
24371da177e4SLinus Torvalds 
2438d6bf7817SEric Dumazet 	nht = rcu_dereference_protected(tbl->nht,
2439d6bf7817SEric Dumazet 					lockdep_is_held(&tbl->lock));
2440cd089336SDavid S. Miller 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2441767e97e1SEric Dumazet 		struct neighbour *n;
2442767e97e1SEric Dumazet 		struct neighbour __rcu **np;
24431da177e4SLinus Torvalds 
2444d6bf7817SEric Dumazet 		np = &nht->hash_buckets[chain];
2445767e97e1SEric Dumazet 		while ((n = rcu_dereference_protected(*np,
2446767e97e1SEric Dumazet 					lockdep_is_held(&tbl->lock))) != NULL) {
24471da177e4SLinus Torvalds 			int release;
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 			write_lock(&n->lock);
24501da177e4SLinus Torvalds 			release = cb(n);
24511da177e4SLinus Torvalds 			if (release) {
2452767e97e1SEric Dumazet 				rcu_assign_pointer(*np,
2453767e97e1SEric Dumazet 					rcu_dereference_protected(n->next,
2454767e97e1SEric Dumazet 						lockdep_is_held(&tbl->lock)));
24551da177e4SLinus Torvalds 				n->dead = 1;
24561da177e4SLinus Torvalds 			} else
24571da177e4SLinus Torvalds 				np = &n->next;
24581da177e4SLinus Torvalds 			write_unlock(&n->lock);
24594f494554SThomas Graf 			if (release)
24604f494554SThomas Graf 				neigh_cleanup_and_release(n);
24611da177e4SLinus Torvalds 		}
24621da177e4SLinus Torvalds 	}
2463ecbb4169SAlexey Kuznetsov }
24641da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release);
24651da177e4SLinus Torvalds 
2466b79bda3dSEric W. Biederman int neigh_xmit(int index, struct net_device *dev,
24674fd3d7d9SEric W. Biederman 	       const void *addr, struct sk_buff *skb)
24684fd3d7d9SEric W. Biederman {
2469b79bda3dSEric W. Biederman 	int err = -EAFNOSUPPORT;
2470b79bda3dSEric W. Biederman 	if (likely(index < NEIGH_NR_TABLES)) {
24714fd3d7d9SEric W. Biederman 		struct neigh_table *tbl;
24724fd3d7d9SEric W. Biederman 		struct neighbour *neigh;
24734fd3d7d9SEric W. Biederman 
2474b79bda3dSEric W. Biederman 		tbl = neigh_tables[index];
24754fd3d7d9SEric W. Biederman 		if (!tbl)
24764fd3d7d9SEric W. Biederman 			goto out;
2477b560f03dSDavid Barroso 		rcu_read_lock_bh();
24784fd3d7d9SEric W. Biederman 		neigh = __neigh_lookup_noref(tbl, addr, dev);
24794fd3d7d9SEric W. Biederman 		if (!neigh)
24804fd3d7d9SEric W. Biederman 			neigh = __neigh_create(tbl, addr, dev, false);
24814fd3d7d9SEric W. Biederman 		err = PTR_ERR(neigh);
2482b560f03dSDavid Barroso 		if (IS_ERR(neigh)) {
2483b560f03dSDavid Barroso 			rcu_read_unlock_bh();
24844fd3d7d9SEric W. Biederman 			goto out_kfree_skb;
2485b560f03dSDavid Barroso 		}
24864fd3d7d9SEric W. Biederman 		err = neigh->output(neigh, skb);
2487b560f03dSDavid Barroso 		rcu_read_unlock_bh();
24884fd3d7d9SEric W. Biederman 	}
2489b79bda3dSEric W. Biederman 	else if (index == NEIGH_LINK_TABLE) {
2490b79bda3dSEric W. Biederman 		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2491b79bda3dSEric W. Biederman 				      addr, NULL, skb->len);
2492b79bda3dSEric W. Biederman 		if (err < 0)
2493b79bda3dSEric W. Biederman 			goto out_kfree_skb;
2494b79bda3dSEric W. Biederman 		err = dev_queue_xmit(skb);
2495b79bda3dSEric W. Biederman 	}
24964fd3d7d9SEric W. Biederman out:
24974fd3d7d9SEric W. Biederman 	return err;
24984fd3d7d9SEric W. Biederman out_kfree_skb:
24994fd3d7d9SEric W. Biederman 	kfree_skb(skb);
25004fd3d7d9SEric W. Biederman 	goto out;
25014fd3d7d9SEric W. Biederman }
25024fd3d7d9SEric W. Biederman EXPORT_SYMBOL(neigh_xmit);
25034fd3d7d9SEric W. Biederman 
25041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq)
25071da177e4SLinus Torvalds {
25081da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25091218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2510d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
25111da177e4SLinus Torvalds 	struct neighbour *n = NULL;
25121da177e4SLinus Torvalds 	int bucket = state->bucket;
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2515cd089336SDavid S. Miller 	for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
2516767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[bucket]);
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 		while (n) {
2519878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2520426b5303SEric W. Biederman 				goto next;
25211da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
25221da177e4SLinus Torvalds 				loff_t fakep = 0;
25231da177e4SLinus Torvalds 				void *v;
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds 				v = state->neigh_sub_iter(state, n, &fakep);
25261da177e4SLinus Torvalds 				if (!v)
25271da177e4SLinus Torvalds 					goto next;
25281da177e4SLinus Torvalds 			}
25291da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
25301da177e4SLinus Torvalds 				break;
25311da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
25321da177e4SLinus Torvalds 				break;
25331da177e4SLinus Torvalds next:
2534767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
25351da177e4SLinus Torvalds 		}
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 		if (n)
25381da177e4SLinus Torvalds 			break;
25391da177e4SLinus Torvalds 	}
25401da177e4SLinus Torvalds 	state->bucket = bucket;
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	return n;
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq,
25461da177e4SLinus Torvalds 					struct neighbour *n,
25471da177e4SLinus Torvalds 					loff_t *pos)
25481da177e4SLinus Torvalds {
25491da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
25501218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
2551d6bf7817SEric Dumazet 	struct neigh_hash_table *nht = state->nht;
25521da177e4SLinus Torvalds 
25531da177e4SLinus Torvalds 	if (state->neigh_sub_iter) {
25541da177e4SLinus Torvalds 		void *v = state->neigh_sub_iter(state, n, pos);
25551da177e4SLinus Torvalds 		if (v)
25561da177e4SLinus Torvalds 			return n;
25571da177e4SLinus Torvalds 	}
2558767e97e1SEric Dumazet 	n = rcu_dereference_bh(n->next);
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	while (1) {
25611da177e4SLinus Torvalds 		while (n) {
2562878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(n->dev), net))
2563426b5303SEric W. Biederman 				goto next;
25641da177e4SLinus Torvalds 			if (state->neigh_sub_iter) {
25651da177e4SLinus Torvalds 				void *v = state->neigh_sub_iter(state, n, pos);
25661da177e4SLinus Torvalds 				if (v)
25671da177e4SLinus Torvalds 					return n;
25681da177e4SLinus Torvalds 				goto next;
25691da177e4SLinus Torvalds 			}
25701da177e4SLinus Torvalds 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
25711da177e4SLinus Torvalds 				break;
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 			if (n->nud_state & ~NUD_NOARP)
25741da177e4SLinus Torvalds 				break;
25751da177e4SLinus Torvalds next:
2576767e97e1SEric Dumazet 			n = rcu_dereference_bh(n->next);
25771da177e4SLinus Torvalds 		}
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 		if (n)
25801da177e4SLinus Torvalds 			break;
25811da177e4SLinus Torvalds 
2582cd089336SDavid S. Miller 		if (++state->bucket >= (1 << nht->hash_shift))
25831da177e4SLinus Torvalds 			break;
25841da177e4SLinus Torvalds 
2585767e97e1SEric Dumazet 		n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
25861da177e4SLinus Torvalds 	}
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 	if (n && pos)
25891da177e4SLinus Torvalds 		--(*pos);
25901da177e4SLinus Torvalds 	return n;
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	struct neighbour *n = neigh_get_first(seq);
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	if (n) {
2598745e2031SChris Larson 		--(*pos);
25991da177e4SLinus Torvalds 		while (*pos) {
26001da177e4SLinus Torvalds 			n = neigh_get_next(seq, n, pos);
26011da177e4SLinus Torvalds 			if (!n)
26021da177e4SLinus Torvalds 				break;
26031da177e4SLinus Torvalds 		}
26041da177e4SLinus Torvalds 	}
26051da177e4SLinus Torvalds 	return *pos ? NULL : n;
26061da177e4SLinus Torvalds }
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
26091da177e4SLinus Torvalds {
26101da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26111218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
26121da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
26131da177e4SLinus Torvalds 	struct pneigh_entry *pn = NULL;
26141da177e4SLinus Torvalds 	int bucket = state->bucket;
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
26171da177e4SLinus Torvalds 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
26181da177e4SLinus Torvalds 		pn = tbl->phash_buckets[bucket];
2619878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2620426b5303SEric W. Biederman 			pn = pn->next;
26211da177e4SLinus Torvalds 		if (pn)
26221da177e4SLinus Torvalds 			break;
26231da177e4SLinus Torvalds 	}
26241da177e4SLinus Torvalds 	state->bucket = bucket;
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	return pn;
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
26301da177e4SLinus Torvalds 					    struct pneigh_entry *pn,
26311da177e4SLinus Torvalds 					    loff_t *pos)
26321da177e4SLinus Torvalds {
26331da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26341218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
26351da177e4SLinus Torvalds 	struct neigh_table *tbl = state->tbl;
26361da177e4SLinus Torvalds 
2637df07a94cSJorge Boncompte [DTI2] 	do {
26381da177e4SLinus Torvalds 		pn = pn->next;
2639df07a94cSJorge Boncompte [DTI2] 	} while (pn && !net_eq(pneigh_net(pn), net));
2640df07a94cSJorge Boncompte [DTI2] 
26411da177e4SLinus Torvalds 	while (!pn) {
26421da177e4SLinus Torvalds 		if (++state->bucket > PNEIGH_HASHMASK)
26431da177e4SLinus Torvalds 			break;
26441da177e4SLinus Torvalds 		pn = tbl->phash_buckets[state->bucket];
2645878628fbSYOSHIFUJI Hideaki 		while (pn && !net_eq(pneigh_net(pn), net))
2646426b5303SEric W. Biederman 			pn = pn->next;
26471da177e4SLinus Torvalds 		if (pn)
26481da177e4SLinus Torvalds 			break;
26491da177e4SLinus Torvalds 	}
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	if (pn && pos)
26521da177e4SLinus Torvalds 		--(*pos);
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	return pn;
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
26581da177e4SLinus Torvalds {
26591da177e4SLinus Torvalds 	struct pneigh_entry *pn = pneigh_get_first(seq);
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds 	if (pn) {
2662745e2031SChris Larson 		--(*pos);
26631da177e4SLinus Torvalds 		while (*pos) {
26641da177e4SLinus Torvalds 			pn = pneigh_get_next(seq, pn, pos);
26651da177e4SLinus Torvalds 			if (!pn)
26661da177e4SLinus Torvalds 				break;
26671da177e4SLinus Torvalds 		}
26681da177e4SLinus Torvalds 	}
26691da177e4SLinus Torvalds 	return *pos ? NULL : pn;
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
26731da177e4SLinus Torvalds {
26741da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26751da177e4SLinus Torvalds 	void *rc;
2676745e2031SChris Larson 	loff_t idxpos = *pos;
26771da177e4SLinus Torvalds 
2678745e2031SChris Larson 	rc = neigh_get_idx(seq, &idxpos);
26791da177e4SLinus Torvalds 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2680745e2031SChris Larson 		rc = pneigh_get_idx(seq, &idxpos);
26811da177e4SLinus Torvalds 
26821da177e4SLinus Torvalds 	return rc;
26831da177e4SLinus Torvalds }
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2686d6bf7817SEric Dumazet 	__acquires(rcu_bh)
26871da177e4SLinus Torvalds {
26881da177e4SLinus Torvalds 	struct neigh_seq_state *state = seq->private;
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds 	state->tbl = tbl;
26911da177e4SLinus Torvalds 	state->bucket = 0;
26921da177e4SLinus Torvalds 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
26931da177e4SLinus Torvalds 
2694d6bf7817SEric Dumazet 	rcu_read_lock_bh();
2695d6bf7817SEric Dumazet 	state->nht = rcu_dereference_bh(tbl->nht);
2696767e97e1SEric Dumazet 
2697745e2031SChris Larson 	return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start);
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
27021da177e4SLinus Torvalds {
27031da177e4SLinus Torvalds 	struct neigh_seq_state *state;
27041da177e4SLinus Torvalds 	void *rc;
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2707bff69732SChris Larson 		rc = neigh_get_first(seq);
27081da177e4SLinus Torvalds 		goto out;
27091da177e4SLinus Torvalds 	}
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	state = seq->private;
27121da177e4SLinus Torvalds 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
27131da177e4SLinus Torvalds 		rc = neigh_get_next(seq, v, NULL);
27141da177e4SLinus Torvalds 		if (rc)
27151da177e4SLinus Torvalds 			goto out;
27161da177e4SLinus Torvalds 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
27171da177e4SLinus Torvalds 			rc = pneigh_get_first(seq);
27181da177e4SLinus Torvalds 	} else {
27191da177e4SLinus Torvalds 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
27201da177e4SLinus Torvalds 		rc = pneigh_get_next(seq, v, NULL);
27211da177e4SLinus Torvalds 	}
27221da177e4SLinus Torvalds out:
27231da177e4SLinus Torvalds 	++(*pos);
27241da177e4SLinus Torvalds 	return rc;
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next);
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v)
2729d6bf7817SEric Dumazet 	__releases(rcu_bh)
27301da177e4SLinus Torvalds {
2731d6bf7817SEric Dumazet 	rcu_read_unlock_bh();
27321da177e4SLinus Torvalds }
27331da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop);
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds /* statistics via seq_file */
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
27381da177e4SLinus Torvalds {
273981c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27401da177e4SLinus Torvalds 	int cpu;
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 	if (*pos == 0)
27431da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
27441da177e4SLinus Torvalds 
27450f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
27461da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
27471da177e4SLinus Torvalds 			continue;
27481da177e4SLinus Torvalds 		*pos = cpu+1;
27491da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
27501da177e4SLinus Torvalds 	}
27511da177e4SLinus Torvalds 	return NULL;
27521da177e4SLinus Torvalds }
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
27551da177e4SLinus Torvalds {
275681c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27571da177e4SLinus Torvalds 	int cpu;
27581da177e4SLinus Torvalds 
27590f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
27601da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
27611da177e4SLinus Torvalds 			continue;
27621da177e4SLinus Torvalds 		*pos = cpu+1;
27631da177e4SLinus Torvalds 		return per_cpu_ptr(tbl->stats, cpu);
27641da177e4SLinus Torvalds 	}
27651da177e4SLinus Torvalds 	return NULL;
27661da177e4SLinus Torvalds }
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
27691da177e4SLinus Torvalds {
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds }
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v)
27741da177e4SLinus Torvalds {
277581c1ebfcSAlexey Dobriyan 	struct neigh_table *tbl = seq->private;
27761da177e4SLinus Torvalds 	struct neigh_statistics *st = v;
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2779fb811395SRick 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");
27801da177e4SLinus Torvalds 		return 0;
27811da177e4SLinus Torvalds 	}
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
2784fb811395SRick Jones 			"%08lx %08lx  %08lx %08lx %08lx %08lx\n",
27851da177e4SLinus Torvalds 		   atomic_read(&tbl->entries),
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 		   st->allocs,
27881da177e4SLinus Torvalds 		   st->destroys,
27891da177e4SLinus Torvalds 		   st->hash_grows,
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 		   st->lookups,
27921da177e4SLinus Torvalds 		   st->hits,
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds 		   st->res_failed,
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 		   st->rcv_probes_mcast,
27971da177e4SLinus Torvalds 		   st->rcv_probes_ucast,
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 		   st->periodic_gc_runs,
28009a6d276eSNeil Horman 		   st->forced_gc_runs,
2801fb811395SRick Jones 		   st->unres_discards,
2802fb811395SRick Jones 		   st->table_fulls
28031da177e4SLinus Torvalds 		   );
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds 	return 0;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
2808f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = {
28091da177e4SLinus Torvalds 	.start	= neigh_stat_seq_start,
28101da177e4SLinus Torvalds 	.next	= neigh_stat_seq_next,
28111da177e4SLinus Torvalds 	.stop	= neigh_stat_seq_stop,
28121da177e4SLinus Torvalds 	.show	= neigh_stat_seq_show,
28131da177e4SLinus Torvalds };
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file)
28161da177e4SLinus Torvalds {
28171da177e4SLinus Torvalds 	int ret = seq_open(file, &neigh_stat_seq_ops);
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 	if (!ret) {
28201da177e4SLinus Torvalds 		struct seq_file *sf = file->private_data;
2821d9dda78bSAl Viro 		sf->private = PDE_DATA(inode);
28221da177e4SLinus Torvalds 	}
28231da177e4SLinus Torvalds 	return ret;
28241da177e4SLinus Torvalds };
28251da177e4SLinus Torvalds 
28269a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = {
28271da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28281da177e4SLinus Torvalds 	.open 	 = neigh_stat_seq_open,
28291da177e4SLinus Torvalds 	.read	 = seq_read,
28301da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
28311da177e4SLinus Torvalds 	.release = seq_release,
28321da177e4SLinus Torvalds };
28331da177e4SLinus Torvalds 
28341da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
28351da177e4SLinus Torvalds 
2836339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void)
2837339bf98fSThomas Graf {
2838339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ndmsg))
2839339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2840339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2841339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct nda_cacheinfo))
2842339bf98fSThomas Graf 	       + nla_total_size(4); /* NDA_PROBES */
2843339bf98fSThomas Graf }
2844339bf98fSThomas Graf 
28457b8f7a40SRoopa Prabhu static void __neigh_notify(struct neighbour *n, int type, int flags,
28467b8f7a40SRoopa Prabhu 			   u32 pid)
28471da177e4SLinus Torvalds {
2848c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(n->dev);
28498b8aec50SThomas Graf 	struct sk_buff *skb;
2850b8673311SThomas Graf 	int err = -ENOBUFS;
28511da177e4SLinus Torvalds 
2852339bf98fSThomas Graf 	skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
28538b8aec50SThomas Graf 	if (skb == NULL)
2854b8673311SThomas Graf 		goto errout;
28551da177e4SLinus Torvalds 
28567b8f7a40SRoopa Prabhu 	err = neigh_fill_info(skb, n, pid, 0, type, flags);
285726932566SPatrick McHardy 	if (err < 0) {
285826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
285926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
286026932566SPatrick McHardy 		kfree_skb(skb);
286126932566SPatrick McHardy 		goto errout;
286226932566SPatrick McHardy 	}
28631ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
28641ce85fe4SPablo Neira Ayuso 	return;
2865b8673311SThomas Graf errout:
2866b8673311SThomas Graf 	if (err < 0)
2867426b5303SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2868b8673311SThomas Graf }
2869b8673311SThomas Graf 
2870b8673311SThomas Graf void neigh_app_ns(struct neighbour *n)
2871b8673311SThomas Graf {
28727b8f7a40SRoopa Prabhu 	__neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
28738b8aec50SThomas Graf }
28740a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns);
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
2877b93196dcSCong Wang static int zero;
2878555445cdSFrancesco Fusco static int int_max = INT_MAX;
2879b93196dcSCong Wang static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
28801da177e4SLinus Torvalds 
2881fe2c6338SJoe Perches static int proc_unres_qlen(struct ctl_table *ctl, int write,
2882fe2c6338SJoe Perches 			   void __user *buffer, size_t *lenp, loff_t *ppos)
28838b5c171bSEric Dumazet {
28848b5c171bSEric Dumazet 	int size, ret;
2885fe2c6338SJoe Perches 	struct ctl_table tmp = *ctl;
28868b5c171bSEric Dumazet 
2887ce46cc64SShan Wei 	tmp.extra1 = &zero;
2888ce46cc64SShan Wei 	tmp.extra2 = &unres_qlen_max;
28898b5c171bSEric Dumazet 	tmp.data = &size;
2890ce46cc64SShan Wei 
2891ce46cc64SShan Wei 	size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
2892ce46cc64SShan Wei 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2893ce46cc64SShan Wei 
28948b5c171bSEric Dumazet 	if (write && !ret)
28958b5c171bSEric Dumazet 		*(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
28968b5c171bSEric Dumazet 	return ret;
28978b5c171bSEric Dumazet }
28988b5c171bSEric Dumazet 
28991d4c8c29SJiri Pirko static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
29001d4c8c29SJiri Pirko 						   int family)
29011d4c8c29SJiri Pirko {
2902bba24896SJiri Pirko 	switch (family) {
2903bba24896SJiri Pirko 	case AF_INET:
29041d4c8c29SJiri Pirko 		return __in_dev_arp_parms_get_rcu(dev);
2905bba24896SJiri Pirko 	case AF_INET6:
2906bba24896SJiri Pirko 		return __in6_dev_nd_parms_get_rcu(dev);
2907bba24896SJiri Pirko 	}
29081d4c8c29SJiri Pirko 	return NULL;
29091d4c8c29SJiri Pirko }
29101d4c8c29SJiri Pirko 
29111d4c8c29SJiri Pirko static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
29121d4c8c29SJiri Pirko 				  int index)
29131d4c8c29SJiri Pirko {
29141d4c8c29SJiri Pirko 	struct net_device *dev;
29151d4c8c29SJiri Pirko 	int family = neigh_parms_family(p);
29161d4c8c29SJiri Pirko 
29171d4c8c29SJiri Pirko 	rcu_read_lock();
29181d4c8c29SJiri Pirko 	for_each_netdev_rcu(net, dev) {
29191d4c8c29SJiri Pirko 		struct neigh_parms *dst_p =
29201d4c8c29SJiri Pirko 				neigh_get_dev_parms_rcu(dev, family);
29211d4c8c29SJiri Pirko 
29221d4c8c29SJiri Pirko 		if (dst_p && !test_bit(index, dst_p->data_state))
29231d4c8c29SJiri Pirko 			dst_p->data[index] = p->data[index];
29241d4c8c29SJiri Pirko 	}
29251d4c8c29SJiri Pirko 	rcu_read_unlock();
29261d4c8c29SJiri Pirko }
29271d4c8c29SJiri Pirko 
29281d4c8c29SJiri Pirko static void neigh_proc_update(struct ctl_table *ctl, int write)
29291d4c8c29SJiri Pirko {
29301d4c8c29SJiri Pirko 	struct net_device *dev = ctl->extra1;
29311d4c8c29SJiri Pirko 	struct neigh_parms *p = ctl->extra2;
293277d47afbSJiri Pirko 	struct net *net = neigh_parms_net(p);
29331d4c8c29SJiri Pirko 	int index = (int *) ctl->data - p->data;
29341d4c8c29SJiri Pirko 
29351d4c8c29SJiri Pirko 	if (!write)
29361d4c8c29SJiri Pirko 		return;
29371d4c8c29SJiri Pirko 
29381d4c8c29SJiri Pirko 	set_bit(index, p->data_state);
29397627ae60SMarcus Huewe 	if (index == NEIGH_VAR_DELAY_PROBE_TIME)
29402a4501aeSIdo Schimmel 		call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
29411d4c8c29SJiri Pirko 	if (!dev) /* NULL dev means this is default value */
29421d4c8c29SJiri Pirko 		neigh_copy_dflt_parms(net, p, index);
29431d4c8c29SJiri Pirko }
29441d4c8c29SJiri Pirko 
29451f9248e5SJiri Pirko static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
29461f9248e5SJiri Pirko 					   void __user *buffer,
29471f9248e5SJiri Pirko 					   size_t *lenp, loff_t *ppos)
29481f9248e5SJiri Pirko {
29491f9248e5SJiri Pirko 	struct ctl_table tmp = *ctl;
29501d4c8c29SJiri Pirko 	int ret;
29511f9248e5SJiri Pirko 
29521f9248e5SJiri Pirko 	tmp.extra1 = &zero;
29531f9248e5SJiri Pirko 	tmp.extra2 = &int_max;
29541f9248e5SJiri Pirko 
29551d4c8c29SJiri Pirko 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
29561d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29571d4c8c29SJiri Pirko 	return ret;
29581f9248e5SJiri Pirko }
29591f9248e5SJiri Pirko 
2960cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write,
2961cb5b09c1SJiri Pirko 			void __user *buffer, size_t *lenp, loff_t *ppos)
2962cb5b09c1SJiri Pirko {
29631d4c8c29SJiri Pirko 	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
29641d4c8c29SJiri Pirko 
29651d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29661d4c8c29SJiri Pirko 	return ret;
2967cb5b09c1SJiri Pirko }
2968cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec);
2969cb5b09c1SJiri Pirko 
2970cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
2971cb5b09c1SJiri Pirko 				void __user *buffer,
2972cb5b09c1SJiri Pirko 				size_t *lenp, loff_t *ppos)
2973cb5b09c1SJiri Pirko {
29741d4c8c29SJiri Pirko 	int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
29751d4c8c29SJiri Pirko 
29761d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29771d4c8c29SJiri Pirko 	return ret;
2978cb5b09c1SJiri Pirko }
2979cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
2980cb5b09c1SJiri Pirko 
2981cb5b09c1SJiri Pirko static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
2982cb5b09c1SJiri Pirko 					      void __user *buffer,
2983cb5b09c1SJiri Pirko 					      size_t *lenp, loff_t *ppos)
2984cb5b09c1SJiri Pirko {
29851d4c8c29SJiri Pirko 	int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
29861d4c8c29SJiri Pirko 
29871d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29881d4c8c29SJiri Pirko 	return ret;
2989cb5b09c1SJiri Pirko }
2990cb5b09c1SJiri Pirko 
2991cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
2992cb5b09c1SJiri Pirko 				   void __user *buffer,
2993cb5b09c1SJiri Pirko 				   size_t *lenp, loff_t *ppos)
2994cb5b09c1SJiri Pirko {
29951d4c8c29SJiri Pirko 	int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
29961d4c8c29SJiri Pirko 
29971d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
29981d4c8c29SJiri Pirko 	return ret;
2999cb5b09c1SJiri Pirko }
3000cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3001cb5b09c1SJiri Pirko 
3002cb5b09c1SJiri Pirko static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3003cb5b09c1SJiri Pirko 					  void __user *buffer,
3004cb5b09c1SJiri Pirko 					  size_t *lenp, loff_t *ppos)
3005cb5b09c1SJiri Pirko {
30061d4c8c29SJiri Pirko 	int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
30071d4c8c29SJiri Pirko 
30081d4c8c29SJiri Pirko 	neigh_proc_update(ctl, write);
30091d4c8c29SJiri Pirko 	return ret;
3010cb5b09c1SJiri Pirko }
3011cb5b09c1SJiri Pirko 
30124bf6980dSJean-Francois Remy static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
30134bf6980dSJean-Francois Remy 					  void __user *buffer,
30144bf6980dSJean-Francois Remy 					  size_t *lenp, loff_t *ppos)
30154bf6980dSJean-Francois Remy {
30164bf6980dSJean-Francois Remy 	struct neigh_parms *p = ctl->extra2;
30174bf6980dSJean-Francois Remy 	int ret;
30184bf6980dSJean-Francois Remy 
30194bf6980dSJean-Francois Remy 	if (strcmp(ctl->procname, "base_reachable_time") == 0)
30204bf6980dSJean-Francois Remy 		ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
30214bf6980dSJean-Francois Remy 	else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
30224bf6980dSJean-Francois Remy 		ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
30234bf6980dSJean-Francois Remy 	else
30244bf6980dSJean-Francois Remy 		ret = -1;
30254bf6980dSJean-Francois Remy 
30264bf6980dSJean-Francois Remy 	if (write && ret == 0) {
30274bf6980dSJean-Francois Remy 		/* update reachable_time as well, otherwise, the change will
30284bf6980dSJean-Francois Remy 		 * only be effective after the next time neigh_periodic_work
30294bf6980dSJean-Francois Remy 		 * decides to recompute it
30304bf6980dSJean-Francois Remy 		 */
30314bf6980dSJean-Francois Remy 		p->reachable_time =
30324bf6980dSJean-Francois Remy 			neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
30334bf6980dSJean-Francois Remy 	}
30344bf6980dSJean-Francois Remy 	return ret;
30354bf6980dSJean-Francois Remy }
30364bf6980dSJean-Francois Remy 
30371f9248e5SJiri Pirko #define NEIGH_PARMS_DATA_OFFSET(index)	\
30381f9248e5SJiri Pirko 	(&((struct neigh_parms *) 0)->data[index])
30391f9248e5SJiri Pirko 
30401f9248e5SJiri Pirko #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
30411f9248e5SJiri Pirko 	[NEIGH_VAR_ ## attr] = { \
30421f9248e5SJiri Pirko 		.procname	= name, \
30431f9248e5SJiri Pirko 		.data		= NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
30441f9248e5SJiri Pirko 		.maxlen		= sizeof(int), \
30451f9248e5SJiri Pirko 		.mode		= mval, \
30461f9248e5SJiri Pirko 		.proc_handler	= proc, \
30471f9248e5SJiri Pirko 	}
30481f9248e5SJiri Pirko 
30491f9248e5SJiri Pirko #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
30501f9248e5SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
30511f9248e5SJiri Pirko 
30521f9248e5SJiri Pirko #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3053cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
30541f9248e5SJiri Pirko 
30551f9248e5SJiri Pirko #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3056cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
30571f9248e5SJiri Pirko 
30581f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
3059cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
30601f9248e5SJiri Pirko 
30611f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3062cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
30631f9248e5SJiri Pirko 
30641f9248e5SJiri Pirko #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3065cb5b09c1SJiri Pirko 	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
306654716e3bSEric W. Biederman 
30671da177e4SLinus Torvalds static struct neigh_sysctl_table {
30681da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
30698b5c171bSEric Dumazet 	struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3070ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = {
30711da177e4SLinus Torvalds 	.neigh_vars = {
30721f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
30731f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
30741f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
30758da86466SYOSHIFUJI Hideaki/吉藤英明 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
30761f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
30771f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
30781f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
30791f9248e5SJiri Pirko 		NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
30801f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
30811f9248e5SJiri Pirko 		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
30821f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
30831f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
30841f9248e5SJiri Pirko 		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
30851f9248e5SJiri Pirko 		NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
30861f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
30871f9248e5SJiri Pirko 		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
30888b5c171bSEric Dumazet 		[NEIGH_VAR_GC_INTERVAL] = {
30891da177e4SLinus Torvalds 			.procname	= "gc_interval",
30901da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
30911da177e4SLinus Torvalds 			.mode		= 0644,
30926d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
30931da177e4SLinus Torvalds 		},
30948b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH1] = {
30951da177e4SLinus Torvalds 			.procname	= "gc_thresh1",
30961da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
30971da177e4SLinus Torvalds 			.mode		= 0644,
3098555445cdSFrancesco Fusco 			.extra1 	= &zero,
3099555445cdSFrancesco Fusco 			.extra2		= &int_max,
3100555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
31011da177e4SLinus Torvalds 		},
31028b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH2] = {
31031da177e4SLinus Torvalds 			.procname	= "gc_thresh2",
31041da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
31051da177e4SLinus Torvalds 			.mode		= 0644,
3106555445cdSFrancesco Fusco 			.extra1 	= &zero,
3107555445cdSFrancesco Fusco 			.extra2		= &int_max,
3108555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
31091da177e4SLinus Torvalds 		},
31108b5c171bSEric Dumazet 		[NEIGH_VAR_GC_THRESH3] = {
31111da177e4SLinus Torvalds 			.procname	= "gc_thresh3",
31121da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
31131da177e4SLinus Torvalds 			.mode		= 0644,
3114555445cdSFrancesco Fusco 			.extra1 	= &zero,
3115555445cdSFrancesco Fusco 			.extra2		= &int_max,
3116555445cdSFrancesco Fusco 			.proc_handler	= proc_dointvec_minmax,
31171da177e4SLinus Torvalds 		},
3118c3bac5a7SPavel Emelyanov 		{},
31191da177e4SLinus Torvalds 	},
31201da177e4SLinus Torvalds };
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
312373af614aSJiri Pirko 			  proc_handler *handler)
31241da177e4SLinus Torvalds {
31251f9248e5SJiri Pirko 	int i;
31263c607bbbSPavel Emelyanov 	struct neigh_sysctl_table *t;
31271f9248e5SJiri Pirko 	const char *dev_name_source;
31288f40a1f9SEric W. Biederman 	char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
312973af614aSJiri Pirko 	char *p_name;
31301da177e4SLinus Torvalds 
31313c607bbbSPavel Emelyanov 	t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
31321da177e4SLinus Torvalds 	if (!t)
31333c607bbbSPavel Emelyanov 		goto err;
31343c607bbbSPavel Emelyanov 
3135b194c1f1SJiri Pirko 	for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
31361f9248e5SJiri Pirko 		t->neigh_vars[i].data += (long) p;
3137cb5b09c1SJiri Pirko 		t->neigh_vars[i].extra1 = dev;
31381d4c8c29SJiri Pirko 		t->neigh_vars[i].extra2 = p;
3139cb5b09c1SJiri Pirko 	}
31401da177e4SLinus Torvalds 
31411da177e4SLinus Torvalds 	if (dev) {
31421da177e4SLinus Torvalds 		dev_name_source = dev->name;
3143d12af679SEric W. Biederman 		/* Terminate the table early */
31448b5c171bSEric Dumazet 		memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
31458b5c171bSEric Dumazet 		       sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
31461da177e4SLinus Torvalds 	} else {
31479ecf07a1SMathias Krause 		struct neigh_table *tbl = p->tbl;
31488f40a1f9SEric W. Biederman 		dev_name_source = "default";
31499ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
31509ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
31519ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
31529ecf07a1SMathias Krause 		t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
31531da177e4SLinus Torvalds 	}
31541da177e4SLinus Torvalds 
3155f8572d8fSEric W. Biederman 	if (handler) {
31561da177e4SLinus Torvalds 		/* RetransTime */
31578b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
31581da177e4SLinus Torvalds 		/* ReachableTime */
31598b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
31601da177e4SLinus Torvalds 		/* RetransTime (in milliseconds)*/
31618b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
31621da177e4SLinus Torvalds 		/* ReachableTime (in milliseconds) */
31638b5c171bSEric Dumazet 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
31644bf6980dSJean-Francois Remy 	} else {
31654bf6980dSJean-Francois Remy 		/* Those handlers will update p->reachable_time after
31664bf6980dSJean-Francois Remy 		 * base_reachable_time(_ms) is set to ensure the new timer starts being
31674bf6980dSJean-Francois Remy 		 * applied after the next neighbour update instead of waiting for
31684bf6980dSJean-Francois Remy 		 * neigh_periodic_work to update its value (can be multiple minutes)
31694bf6980dSJean-Francois Remy 		 * So any handler that replaces them should do this as well
31704bf6980dSJean-Francois Remy 		 */
31714bf6980dSJean-Francois Remy 		/* ReachableTime */
31724bf6980dSJean-Francois Remy 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
31734bf6980dSJean-Francois Remy 			neigh_proc_base_reachable_time;
31744bf6980dSJean-Francois Remy 		/* ReachableTime (in milliseconds) */
31754bf6980dSJean-Francois Remy 		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
31764bf6980dSJean-Francois Remy 			neigh_proc_base_reachable_time;
31771da177e4SLinus Torvalds 	}
31781da177e4SLinus Torvalds 
3179464dc801SEric W. Biederman 	/* Don't export sysctls to unprivileged users */
3180464dc801SEric W. Biederman 	if (neigh_parms_net(p)->user_ns != &init_user_ns)
3181464dc801SEric W. Biederman 		t->neigh_vars[0].procname = NULL;
3182464dc801SEric W. Biederman 
318373af614aSJiri Pirko 	switch (neigh_parms_family(p)) {
318473af614aSJiri Pirko 	case AF_INET:
318573af614aSJiri Pirko 	      p_name = "ipv4";
318673af614aSJiri Pirko 	      break;
318773af614aSJiri Pirko 	case AF_INET6:
318873af614aSJiri Pirko 	      p_name = "ipv6";
318973af614aSJiri Pirko 	      break;
319073af614aSJiri Pirko 	default:
319173af614aSJiri Pirko 	      BUG();
319273af614aSJiri Pirko 	}
319373af614aSJiri Pirko 
31948f40a1f9SEric W. Biederman 	snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
31958f40a1f9SEric W. Biederman 		p_name, dev_name_source);
31964ab438fcSDenis V. Lunev 	t->sysctl_header =
31978f40a1f9SEric W. Biederman 		register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
31983c607bbbSPavel Emelyanov 	if (!t->sysctl_header)
31998f40a1f9SEric W. Biederman 		goto free;
32003c607bbbSPavel Emelyanov 
32011da177e4SLinus Torvalds 	p->sysctl_table = t;
32021da177e4SLinus Torvalds 	return 0;
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds free:
32051da177e4SLinus Torvalds 	kfree(t);
32063c607bbbSPavel Emelyanov err:
32073c607bbbSPavel Emelyanov 	return -ENOBUFS;
32081da177e4SLinus Torvalds }
32090a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register);
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p)
32121da177e4SLinus Torvalds {
32131da177e4SLinus Torvalds 	if (p->sysctl_table) {
32141da177e4SLinus Torvalds 		struct neigh_sysctl_table *t = p->sysctl_table;
32151da177e4SLinus Torvalds 		p->sysctl_table = NULL;
32165dd3df10SEric W. Biederman 		unregister_net_sysctl_table(t->sysctl_header);
32171da177e4SLinus Torvalds 		kfree(t);
32181da177e4SLinus Torvalds 	}
32191da177e4SLinus Torvalds }
32200a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister);
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds #endif	/* CONFIG_SYSCTL */
32231da177e4SLinus Torvalds 
3224c8822a4eSThomas Graf static int __init neigh_init(void)
3225c8822a4eSThomas Graf {
3226c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
3227c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
3228c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
3229c8822a4eSThomas Graf 
3230c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3231c7ac8679SGreg Rose 		      NULL);
3232c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
3233c8822a4eSThomas Graf 
3234c8822a4eSThomas Graf 	return 0;
3235c8822a4eSThomas Graf }
3236c8822a4eSThomas Graf 
3237c8822a4eSThomas Graf subsys_initcall(neigh_init);
3238c8822a4eSThomas Graf 
3239