File Index
Files and directories
Sign in
FILE

neighbour.c

File preview with syntax highlighting.

linux-6.19.14/net/core/neighbour.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *	Generic address resolution entity
   4 *
   5 *	Authors:
   6 *	Pedro Roque		<roque@di.fc.ul.pt>
   7 *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
   8 *
   9 *	Fixes:
  10 *	Vitaly E. Lavrov	releasing NULL neighbor in neigh_add.
  11 *	Harald Welte		Add neighbour cache statistics like rtstat
  12 */
  13
  14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15
  16#include <linux/slab.h>
  17#include <linux/types.h>
  18#include <linux/kernel.h>
  19#include <linux/module.h>
  20#include <linux/socket.h>
  21#include <linux/netdevice.h>
  22#include <linux/proc_fs.h>
  23#ifdef CONFIG_SYSCTL
  24#include <linux/sysctl.h>
  25#endif
  26#include <linux/times.h>
  27#include <net/net_namespace.h>
  28#include <net/neighbour.h>
  29#include <net/arp.h>
  30#include <net/dst.h>
  31#include <net/ip.h>
  32#include <net/sock.h>
  33#include <net/netevent.h>
  34#include <net/netlink.h>
  35#include <linux/rtnetlink.h>
  36#include <linux/random.h>
  37#include <linux/string.h>
  38#include <linux/log2.h>
  39#include <linux/inetdevice.h>
  40#include <net/addrconf.h>
  41
  42#include <trace/events/neigh.h>
  43
  44#define NEIGH_DEBUG 1
  45#define neigh_dbg(level, fmt, ...)		\
  46do {						\
  47	if (level <= NEIGH_DEBUG)		\
  48		pr_debug(fmt, ##__VA_ARGS__);	\
  49} while (0)
  50
  51#define PNEIGH_HASHMASK		0xF
  52
  53static void neigh_timer_handler(struct timer_list *t);
  54static void __neigh_notify(struct neighbour *n, int type, int flags,
  55			   u32 pid);
  56static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid);
  57static void pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
  58			  bool skip_perm);
  59
  60#ifdef CONFIG_PROC_FS
  61static const struct seq_operations neigh_stat_seq_ops;
  62#endif
  63
  64static struct hlist_head *neigh_get_dev_table(struct net_device *dev, int family)
  65{
  66	int i;
  67
  68	switch (family) {
  69	default:
  70		DEBUG_NET_WARN_ON_ONCE(1);
  71		fallthrough; /* to avoid panic by null-ptr-deref */
  72	case AF_INET:
  73		i = NEIGH_ARP_TABLE;
  74		break;
  75	case AF_INET6:
  76		i = NEIGH_ND_TABLE;
  77		break;
  78	}
  79
  80	return &dev->neighbours[i];
  81}
  82
  83/*
  84   Neighbour hash table buckets are protected with tbl->lock.
  85
  86   - All the scans/updates to hash buckets MUST be made under this lock.
  87   - NOTHING clever should be made under this lock: no callbacks
  88     to protocol backends, no attempts to send something to network.
  89     It will result in deadlocks, if backend/driver wants to use neighbour
  90     cache.
  91   - If the entry requires some non-trivial actions, increase
  92     its reference count and release table lock.
  93
  94   Neighbour entries are protected:
  95   - with reference count.
  96   - with rwlock neigh->lock
  97
  98   Reference count prevents destruction.
  99
 100   neigh->lock mainly serializes ll address data and its validity state.
 101   However, the same lock is used to protect another entry fields:
 102    - timer
 103    - resolution queue
 104
 105   Again, nothing clever shall be made under neigh->lock,
 106   the most complicated procedure, which we allow is dev->hard_header.
 107   It is supposed, that dev->hard_header is simplistic and does
 108   not make callbacks to neighbour tables.
 109 */
 110
 111static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
 112{
 113	kfree_skb(skb);
 114	return -ENETDOWN;
 115}
 116
 117static void neigh_cleanup_and_release(struct neighbour *neigh)
 118{
 119	trace_neigh_cleanup_and_release(neigh, 0);
 120	__neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
 121	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
 122	neigh_release(neigh);
 123}
 124
 125/*
 126 * It is random distribution in the interval (1/2)*base...(3/2)*base.
 127 * It corresponds to default IPv6 settings and is not overridable,
 128 * because it is really reasonable choice.
 129 */
 130
 131unsigned long neigh_rand_reach_time(unsigned long base)
 132{
 133	return base ? get_random_u32_below(base) + (base >> 1) : 0;
 134}
 135EXPORT_SYMBOL(neigh_rand_reach_time);
 136
 137static void neigh_mark_dead(struct neighbour *n)
 138{
 139	n->dead = 1;
 140	if (!list_empty(&n->gc_list)) {
 141		list_del_init(&n->gc_list);
 142		atomic_dec(&n->tbl->gc_entries);
 143	}
 144	if (!list_empty(&n->managed_list))
 145		list_del_init(&n->managed_list);
 146}
 147
 148static void neigh_update_gc_list(struct neighbour *n)
 149{
 150	bool on_gc_list, exempt_from_gc;
 151
 152	spin_lock_bh(&n->tbl->lock);
 153	write_lock(&n->lock);
 154	if (n->dead)
 155		goto out;
 156
 157	/* remove from the gc list if new state is permanent or if neighbor is
 158	 * externally learned / validated; otherwise entry should be on the gc
 159	 * list
 160	 */
 161	exempt_from_gc = n->nud_state & NUD_PERMANENT ||
 162			 n->flags & (NTF_EXT_LEARNED | NTF_EXT_VALIDATED);
 163	on_gc_list = !list_empty(&n->gc_list);
 164
 165	if (exempt_from_gc && on_gc_list) {
 166		list_del_init(&n->gc_list);
 167		atomic_dec(&n->tbl->gc_entries);
 168	} else if (!exempt_from_gc && !on_gc_list) {
 169		/* add entries to the tail; cleaning removes from the front */
 170		list_add_tail(&n->gc_list, &n->tbl->gc_list);
 171		atomic_inc(&n->tbl->gc_entries);
 172	}
 173out:
 174	write_unlock(&n->lock);
 175	spin_unlock_bh(&n->tbl->lock);
 176}
 177
 178static void neigh_update_managed_list(struct neighbour *n)
 179{
 180	bool on_managed_list, add_to_managed;
 181
 182	spin_lock_bh(&n->tbl->lock);
 183	write_lock(&n->lock);
 184	if (n->dead)
 185		goto out;
 186
 187	add_to_managed = n->flags & NTF_MANAGED;
 188	on_managed_list = !list_empty(&n->managed_list);
 189
 190	if (!add_to_managed && on_managed_list)
 191		list_del_init(&n->managed_list);
 192	else if (add_to_managed && !on_managed_list)
 193		list_add_tail(&n->managed_list, &n->tbl->managed_list);
 194out:
 195	write_unlock(&n->lock);
 196	spin_unlock_bh(&n->tbl->lock);
 197}
 198
 199static void neigh_update_flags(struct neighbour *neigh, u32 flags, int *notify,
 200			       bool *gc_update, bool *managed_update)
 201{
 202	u32 ndm_flags, old_flags = neigh->flags;
 203
 204	if (!(flags & NEIGH_UPDATE_F_ADMIN))
 205		return;
 206
 207	ndm_flags  = (flags & NEIGH_UPDATE_F_EXT_LEARNED) ? NTF_EXT_LEARNED : 0;
 208	ndm_flags |= (flags & NEIGH_UPDATE_F_MANAGED) ? NTF_MANAGED : 0;
 209	ndm_flags |= (flags & NEIGH_UPDATE_F_EXT_VALIDATED) ? NTF_EXT_VALIDATED : 0;
 210
 211	if ((old_flags ^ ndm_flags) & NTF_EXT_LEARNED) {
 212		if (ndm_flags & NTF_EXT_LEARNED)
 213			neigh->flags |= NTF_EXT_LEARNED;
 214		else
 215			neigh->flags &= ~NTF_EXT_LEARNED;
 216		*notify = 1;
 217		*gc_update = true;
 218	}
 219	if ((old_flags ^ ndm_flags) & NTF_MANAGED) {
 220		if (ndm_flags & NTF_MANAGED)
 221			neigh->flags |= NTF_MANAGED;
 222		else
 223			neigh->flags &= ~NTF_MANAGED;
 224		*notify = 1;
 225		*managed_update = true;
 226	}
 227	if ((old_flags ^ ndm_flags) & NTF_EXT_VALIDATED) {
 228		if (ndm_flags & NTF_EXT_VALIDATED)
 229			neigh->flags |= NTF_EXT_VALIDATED;
 230		else
 231			neigh->flags &= ~NTF_EXT_VALIDATED;
 232		*notify = 1;
 233		*gc_update = true;
 234	}
 235}
 236
 237bool neigh_remove_one(struct neighbour *n)
 238{
 239	bool retval = false;
 240
 241	write_lock(&n->lock);
 242	if (refcount_read(&n->refcnt) == 1) {
 243		hlist_del_rcu(&n->hash);
 244		hlist_del_rcu(&n->dev_list);
 245		neigh_mark_dead(n);
 246		retval = true;
 247	}
 248	write_unlock(&n->lock);
 249	if (retval)
 250		neigh_cleanup_and_release(n);
 251	return retval;
 252}
 253
 254static int neigh_forced_gc(struct neigh_table *tbl)
 255{
 256	int max_clean = atomic_read(&tbl->gc_entries) -
 257			READ_ONCE(tbl->gc_thresh2);
 258	u64 tmax = ktime_get_ns() + NSEC_PER_MSEC;
 259	unsigned long tref = jiffies - 5 * HZ;
 260	struct neighbour *n, *tmp;
 261	int shrunk = 0;
 262	int loop = 0;
 263
 264	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
 265
 266	spin_lock_bh(&tbl->lock);
 267
 268	list_for_each_entry_safe(n, tmp, &tbl->gc_list, gc_list) {
 269		if (refcount_read(&n->refcnt) == 1) {
 270			bool remove = false;
 271
 272			write_lock(&n->lock);
 273			if ((n->nud_state == NUD_FAILED) ||
 274			    (n->nud_state == NUD_NOARP) ||
 275			    (tbl->is_multicast &&
 276			     tbl->is_multicast(n->primary_key)) ||
 277			    !time_in_range(n->updated, tref, jiffies))
 278				remove = true;
 279			write_unlock(&n->lock);
 280
 281			if (remove && neigh_remove_one(n))
 282				shrunk++;
 283			if (shrunk >= max_clean)
 284				break;
 285			if (++loop == 16) {
 286				if (ktime_get_ns() > tmax)
 287					goto unlock;
 288				loop = 0;
 289			}
 290		}
 291	}
 292
 293	WRITE_ONCE(tbl->last_flush, jiffies);
 294unlock:
 295	spin_unlock_bh(&tbl->lock);
 296
 297	return shrunk;
 298}
 299
 300static void neigh_add_timer(struct neighbour *n, unsigned long when)
 301{
 302	/* Use safe distance from the jiffies - LONG_MAX point while timer
 303	 * is running in DELAY/PROBE state but still show to user space
 304	 * large times in the past.
 305	 */
 306	unsigned long mint = jiffies - (LONG_MAX - 86400 * HZ);
 307
 308	neigh_hold(n);
 309	if (!time_in_range(n->confirmed, mint, jiffies))
 310		n->confirmed = mint;
 311	if (time_before(n->used, n->confirmed))
 312		n->used = n->confirmed;
 313	if (unlikely(mod_timer(&n->timer, when))) {
 314		printk("NEIGH: BUG, double timer add, state is %x\n",
 315		       n->nud_state);
 316		dump_stack();
 317	}
 318}
 319
 320static int neigh_del_timer(struct neighbour *n)
 321{
 322	if ((n->nud_state & NUD_IN_TIMER) &&
 323	    timer_delete(&n->timer)) {
 324		neigh_release(n);
 325		return 1;
 326	}
 327	return 0;
 328}
 329
 330static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
 331						   int family)
 332{
 333	switch (family) {
 334	case AF_INET:
 335		return __in_dev_arp_parms_get_rcu(dev);
 336	case AF_INET6:
 337		return __in6_dev_nd_parms_get_rcu(dev);
 338	}
 339	return NULL;
 340}
 341
 342static void neigh_parms_qlen_dec(struct net_device *dev, int family)
 343{
 344	struct neigh_parms *p;
 345
 346	rcu_read_lock();
 347	p = neigh_get_dev_parms_rcu(dev, family);
 348	if (p)
 349		p->qlen--;
 350	rcu_read_unlock();
 351}
 352
 353static void pneigh_queue_purge(struct sk_buff_head *list, struct net *net,
 354			       int family)
 355{
 356	struct sk_buff_head tmp;
 357	unsigned long flags;
 358	struct sk_buff *skb;
 359
 360	skb_queue_head_init(&tmp);
 361	spin_lock_irqsave(&list->lock, flags);
 362	skb = skb_peek(list);
 363	while (skb != NULL) {
 364		struct sk_buff *skb_next = skb_peek_next(skb, list);
 365		struct net_device *dev = skb->dev;
 366
 367		if (net == NULL || net_eq(dev_net(dev), net)) {
 368			neigh_parms_qlen_dec(dev, family);
 369			__skb_unlink(skb, list);
 370			__skb_queue_tail(&tmp, skb);
 371		}
 372		skb = skb_next;
 373	}
 374	spin_unlock_irqrestore(&list->lock, flags);
 375
 376	while ((skb = __skb_dequeue(&tmp))) {
 377		dev_put(skb->dev);
 378		kfree_skb(skb);
 379	}
 380}
 381
 382static void neigh_flush_one(struct neighbour *n)
 383{
 384	hlist_del_rcu(&n->hash);
 385	hlist_del_rcu(&n->dev_list);
 386
 387	write_lock(&n->lock);
 388
 389	neigh_del_timer(n);
 390	neigh_mark_dead(n);
 391
 392	if (refcount_read(&n->refcnt) != 1) {
 393		/* The most unpleasant situation.
 394		 * We must destroy neighbour entry,
 395		 * but someone still uses it.
 396		 *
 397		 * The destroy will be delayed until
 398		 * the last user releases us, but
 399		 * we must kill timers etc. and move
 400		 * it to safe state.
 401		 */
 402		__skb_queue_purge(&n->arp_queue);
 403		n->arp_queue_len_bytes = 0;
 404		WRITE_ONCE(n->output, neigh_blackhole);
 405
 406		if (n->nud_state & NUD_VALID)
 407			n->nud_state = NUD_NOARP;
 408		else
 409			n->nud_state = NUD_NONE;
 410
 411		neigh_dbg(2, "neigh %p is stray\n", n);
 412	}
 413
 414	write_unlock(&n->lock);
 415
 416	neigh_cleanup_and_release(n);
 417}
 418
 419static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
 420			    bool skip_perm)
 421{
 422	struct hlist_head *dev_head;
 423	struct hlist_node *tmp;
 424	struct neighbour *n;
 425
 426	dev_head = neigh_get_dev_table(dev, tbl->family);
 427
 428	hlist_for_each_entry_safe(n, tmp, dev_head, dev_list) {
 429		if (skip_perm &&
 430		    (n->nud_state & NUD_PERMANENT ||
 431		     n->flags & NTF_EXT_VALIDATED))
 432			continue;
 433
 434		neigh_flush_one(n);
 435	}
 436}
 437
 438static void neigh_flush_table(struct neigh_table *tbl)
 439{
 440	struct neigh_hash_table *nht;
 441	int i;
 442
 443	nht = rcu_dereference_protected(tbl->nht,
 444					lockdep_is_held(&tbl->lock));
 445
 446	for (i = 0; i < (1 << nht->hash_shift); i++) {
 447		struct hlist_node *tmp;
 448		struct neighbour *n;
 449
 450		neigh_for_each_in_bucket_safe(n, tmp, &nht->hash_heads[i])
 451			neigh_flush_one(n);
 452	}
 453}
 454
 455void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
 456{
 457	spin_lock_bh(&tbl->lock);
 458	neigh_flush_dev(tbl, dev, false);
 459	spin_unlock_bh(&tbl->lock);
 460}
 461EXPORT_SYMBOL(neigh_changeaddr);
 462
 463static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
 464			  bool skip_perm)
 465{
 466	spin_lock_bh(&tbl->lock);
 467	if (likely(dev)) {
 468		neigh_flush_dev(tbl, dev, skip_perm);
 469	} else {
 470		DEBUG_NET_WARN_ON_ONCE(skip_perm);
 471		neigh_flush_table(tbl);
 472	}
 473	spin_unlock_bh(&tbl->lock);
 474
 475	pneigh_ifdown(tbl, dev, skip_perm);
 476	pneigh_queue_purge(&tbl->proxy_queue, dev ? dev_net(dev) : NULL,
 477			   tbl->family);
 478	if (skb_queue_empty_lockless(&tbl->proxy_queue))
 479		timer_delete_sync(&tbl->proxy_timer);
 480	return 0;
 481}
 482
 483int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
 484{
 485	__neigh_ifdown(tbl, dev, true);
 486	return 0;
 487}
 488EXPORT_SYMBOL(neigh_carrier_down);
 489
 490int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
 491{
 492	__neigh_ifdown(tbl, dev, false);
 493	return 0;
 494}
 495EXPORT_SYMBOL(neigh_ifdown);
 496
 497static struct neighbour *neigh_alloc(struct neigh_table *tbl,
 498				     struct net_device *dev,
 499				     u32 flags, bool exempt_from_gc)
 500{
 501	struct neighbour *n = NULL;
 502	unsigned long now = jiffies;
 503	int entries, gc_thresh3;
 504
 505	if (exempt_from_gc)
 506		goto do_alloc;
 507
 508	entries = atomic_inc_return(&tbl->gc_entries) - 1;
 509	gc_thresh3 = READ_ONCE(tbl->gc_thresh3);
 510	if (entries >= gc_thresh3 ||
 511	    (entries >= READ_ONCE(tbl->gc_thresh2) &&
 512	     time_after(now, READ_ONCE(tbl->last_flush) + 5 * HZ))) {
 513		if (!neigh_forced_gc(tbl) && entries >= gc_thresh3) {
 514			net_info_ratelimited("%s: neighbor table overflow!\n",
 515					     tbl->id);
 516			NEIGH_CACHE_STAT_INC(tbl, table_fulls);
 517			goto out_entries;
 518		}
 519	}
 520
 521do_alloc:
 522	n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
 523	if (!n)
 524		goto out_entries;
 525
 526	__skb_queue_head_init(&n->arp_queue);
 527	rwlock_init(&n->lock);
 528	seqlock_init(&n->ha_lock);
 529	n->updated	  = n->used = now;
 530	n->nud_state	  = NUD_NONE;
 531	n->output	  = neigh_blackhole;
 532	n->flags	  = flags;
 533	seqlock_init(&n->hh.hh_lock);
 534	n->parms	  = neigh_parms_clone(&tbl->parms);
 535	timer_setup(&n->timer, neigh_timer_handler, 0);
 536
 537	NEIGH_CACHE_STAT_INC(tbl, allocs);
 538	n->tbl		  = tbl;
 539	refcount_set(&n->refcnt, 1);
 540	n->dead		  = 1;
 541	INIT_LIST_HEAD(&n->gc_list);
 542	INIT_LIST_HEAD(&n->managed_list);
 543
 544	atomic_inc(&tbl->entries);
 545out:
 546	return n;
 547
 548out_entries:
 549	if (!exempt_from_gc)
 550		atomic_dec(&tbl->gc_entries);
 551	goto out;
 552}
 553
 554static void neigh_get_hash_rnd(u32 *x)
 555{
 556	*x = get_random_u32() | 1;
 557}
 558
 559static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
 560{
 561	size_t size = (1 << shift) * sizeof(struct hlist_head);
 562	struct hlist_head *hash_heads;
 563	struct neigh_hash_table *ret;
 564	int i;
 565
 566	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
 567	if (!ret)
 568		return NULL;
 569
 570	hash_heads = kzalloc(size, GFP_ATOMIC);
 571	if (!hash_heads) {
 572		kfree(ret);
 573		return NULL;
 574	}
 575	ret->hash_heads = hash_heads;
 576	ret->hash_shift = shift;
 577	for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
 578		neigh_get_hash_rnd(&ret->hash_rnd[i]);
 579	return ret;
 580}
 581
 582static void neigh_hash_free_rcu(struct rcu_head *head)
 583{
 584	struct neigh_hash_table *nht = container_of(head,
 585						    struct neigh_hash_table,
 586						    rcu);
 587
 588	kfree(nht->hash_heads);
 589	kfree(nht);
 590}
 591
 592static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
 593						unsigned long new_shift)
 594{
 595	unsigned int i, hash;
 596	struct neigh_hash_table *new_nht, *old_nht;
 597
 598	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
 599
 600	old_nht = rcu_dereference_protected(tbl->nht,
 601					    lockdep_is_held(&tbl->lock));
 602	new_nht = neigh_hash_alloc(new_shift);
 603	if (!new_nht)
 604		return old_nht;
 605
 606	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
 607		struct hlist_node *tmp;
 608		struct neighbour *n;
 609
 610		neigh_for_each_in_bucket_safe(n, tmp, &old_nht->hash_heads[i]) {
 611			hash = tbl->hash(n->primary_key, n->dev,
 612					 new_nht->hash_rnd);
 613
 614			hash >>= (32 - new_nht->hash_shift);
 615
 616			hlist_del_rcu(&n->hash);
 617			hlist_add_head_rcu(&n->hash, &new_nht->hash_heads[hash]);
 618		}
 619	}
 620
 621	rcu_assign_pointer(tbl->nht, new_nht);
 622	call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
 623	return new_nht;
 624}
 625
 626struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
 627			       struct net_device *dev)
 628{
 629	struct neighbour *n;
 630
 631	NEIGH_CACHE_STAT_INC(tbl, lookups);
 632
 633	rcu_read_lock();
 634	n = __neigh_lookup_noref(tbl, pkey, dev);
 635	if (n) {
 636		if (!refcount_inc_not_zero(&n->refcnt))
 637			n = NULL;
 638		NEIGH_CACHE_STAT_INC(tbl, hits);
 639	}
 640
 641	rcu_read_unlock();
 642	return n;
 643}
 644EXPORT_SYMBOL(neigh_lookup);
 645
 646static struct neighbour *
 647___neigh_create(struct neigh_table *tbl, const void *pkey,
 648		struct net_device *dev, u32 flags,
 649		bool exempt_from_gc, bool want_ref)
 650{
 651	u32 hash_val, key_len = tbl->key_len;
 652	struct neighbour *n1, *rc, *n;
 653	struct neigh_hash_table *nht;
 654	int error;
 655
 656	n = neigh_alloc(tbl, dev, flags, exempt_from_gc);
 657	trace_neigh_create(tbl, dev, pkey, n, exempt_from_gc);
 658	if (!n) {
 659		rc = ERR_PTR(-ENOBUFS);
 660		goto out;
 661	}
 662
 663	memcpy(n->primary_key, pkey, key_len);
 664	n->dev = dev;
 665	netdev_hold(dev, &n->dev_tracker, GFP_ATOMIC);
 666
 667	/* Protocol specific setup. */
 668	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
 669		rc = ERR_PTR(error);
 670		goto out_neigh_release;
 671	}
 672
 673	if (dev->netdev_ops->ndo_neigh_construct) {
 674		error = dev->netdev_ops->ndo_neigh_construct(dev, n);
 675		if (error < 0) {
 676			rc = ERR_PTR(error);
 677			goto out_neigh_release;
 678		}
 679	}
 680
 681	/* Device specific setup. */
 682	if (n->parms->neigh_setup &&
 683	    (error = n->parms->neigh_setup(n)) < 0) {
 684		rc = ERR_PTR(error);
 685		goto out_neigh_release;
 686	}
 687
 688	n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
 689
 690	spin_lock_bh(&tbl->lock);
 691	nht = rcu_dereference_protected(tbl->nht,
 692					lockdep_is_held(&tbl->lock));
 693
 694	if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
 695		nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
 696
 697	hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
 698
 699	if (n->parms->dead) {
 700		rc = ERR_PTR(-EINVAL);
 701		goto out_tbl_unlock;
 702	}
 703
 704	neigh_for_each_in_bucket(n1, &nht->hash_heads[hash_val]) {
 705		if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
 706			if (want_ref)
 707				neigh_hold(n1);
 708			rc = n1;
 709			goto out_tbl_unlock;
 710		}
 711	}
 712
 713	n->dead = 0;
 714	if (!exempt_from_gc)
 715		list_add_tail(&n->gc_list, &n->tbl->gc_list);
 716	if (n->flags & NTF_MANAGED)
 717		list_add_tail(&n->managed_list, &n->tbl->managed_list);
 718	if (want_ref)
 719		neigh_hold(n);
 720	hlist_add_head_rcu(&n->hash, &nht->hash_heads[hash_val]);
 721
 722	hlist_add_head_rcu(&n->dev_list,
 723			   neigh_get_dev_table(dev, tbl->family));
 724
 725	spin_unlock_bh(&tbl->lock);
 726	neigh_dbg(2, "neigh %p is created\n", n);
 727	rc = n;
 728out:
 729	return rc;
 730out_tbl_unlock:
 731	spin_unlock_bh(&tbl->lock);
 732out_neigh_release:
 733	if (!exempt_from_gc)
 734		atomic_dec(&tbl->gc_entries);
 735	neigh_release(n);
 736	goto out;
 737}
 738
 739struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
 740				 struct net_device *dev, bool want_ref)
 741{
 742	bool exempt_from_gc = !!(dev->flags & IFF_LOOPBACK);
 743
 744	return ___neigh_create(tbl, pkey, dev, 0, exempt_from_gc, want_ref);
 745}
 746EXPORT_SYMBOL(__neigh_create);
 747
 748static u32 pneigh_hash(const void *pkey, unsigned int key_len)
 749{
 750	u32 hash_val = *(u32 *)(pkey + key_len - 4);
 751	hash_val ^= (hash_val >> 16);
 752	hash_val ^= hash_val >> 8;
 753	hash_val ^= hash_val >> 4;
 754	hash_val &= PNEIGH_HASHMASK;
 755	return hash_val;
 756}
 757
 758struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl,
 759				   struct net *net, const void *pkey,
 760				   struct net_device *dev)
 761{
 762	struct pneigh_entry *n;
 763	unsigned int key_len;
 764	u32 hash_val;
 765
 766	key_len = tbl->key_len;
 767	hash_val = pneigh_hash(pkey, key_len);
 768	n = rcu_dereference_check(tbl->phash_buckets[hash_val],
 769				  lockdep_is_held(&tbl->phash_lock));
 770
 771	while (n) {
 772		if (!memcmp(n->key, pkey, key_len) &&
 773		    net_eq(pneigh_net(n), net) &&
 774		    (n->dev == dev || !n->dev))
 775			return n;
 776
 777		n = rcu_dereference_check(n->next, lockdep_is_held(&tbl->phash_lock));
 778	}
 779
 780	return NULL;
 781}
 782EXPORT_IPV6_MOD(pneigh_lookup);
 783
 784int pneigh_create(struct neigh_table *tbl, struct net *net,
 785		  const void *pkey, struct net_device *dev,
 786		  u32 flags, u8 protocol, bool permanent)
 787{
 788	struct pneigh_entry *n;
 789	unsigned int key_len;
 790	u32 hash_val;
 791	int err = 0;
 792
 793	mutex_lock(&tbl->phash_lock);
 794
 795	n = pneigh_lookup(tbl, net, pkey, dev);
 796	if (n)
 797		goto update;
 798
 799	key_len = tbl->key_len;
 800	n = kzalloc(sizeof(*n) + key_len, GFP_KERNEL);
 801	if (!n) {
 802		err = -ENOBUFS;
 803		goto out;
 804	}
 805
 806	write_pnet(&n->net, net);
 807	memcpy(n->key, pkey, key_len);
 808	n->dev = dev;
 809	netdev_hold(dev, &n->dev_tracker, GFP_KERNEL);
 810
 811	if (tbl->pconstructor && tbl->pconstructor(n)) {
 812		netdev_put(dev, &n->dev_tracker);
 813		kfree(n);
 814		err = -ENOBUFS;
 815		goto out;
 816	}
 817
 818	hash_val = pneigh_hash(pkey, key_len);
 819	n->next = tbl->phash_buckets[hash_val];
 820	rcu_assign_pointer(tbl->phash_buckets[hash_val], n);
 821update:
 822	WRITE_ONCE(n->flags, flags);
 823	n->permanent = permanent;
 824	if (protocol)
 825		WRITE_ONCE(n->protocol, protocol);
 826out:
 827	mutex_unlock(&tbl->phash_lock);
 828	return err;
 829}
 830
 831static void pneigh_destroy(struct rcu_head *rcu)
 832{
 833	struct pneigh_entry *n = container_of(rcu, struct pneigh_entry, rcu);
 834
 835	netdev_put(n->dev, &n->dev_tracker);
 836	kfree(n);
 837}
 838
 839int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
 840		  struct net_device *dev)
 841{
 842	struct pneigh_entry *n, __rcu **np;
 843	unsigned int key_len;
 844	u32 hash_val;
 845
 846	key_len = tbl->key_len;
 847	hash_val = pneigh_hash(pkey, key_len);
 848
 849	mutex_lock(&tbl->phash_lock);
 850
 851	for (np = &tbl->phash_buckets[hash_val];
 852	     (n = rcu_dereference_protected(*np, 1)) != NULL;
 853	     np = &n->next) {
 854		if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
 855		    net_eq(pneigh_net(n), net)) {
 856			rcu_assign_pointer(*np, n->next);
 857
 858			mutex_unlock(&tbl->phash_lock);
 859
 860			if (tbl->pdestructor)
 861				tbl->pdestructor(n);
 862
 863			call_rcu(&n->rcu, pneigh_destroy);
 864			return 0;
 865		}
 866	}
 867
 868	mutex_unlock(&tbl->phash_lock);
 869	return -ENOENT;
 870}
 871
 872static void pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
 873			  bool skip_perm)
 874{
 875	struct pneigh_entry *n, __rcu **np;
 876	LIST_HEAD(head);
 877	u32 h;
 878
 879	mutex_lock(&tbl->phash_lock);
 880
 881	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
 882		np = &tbl->phash_buckets[h];
 883		while ((n = rcu_dereference_protected(*np, 1)) != NULL) {
 884			if (skip_perm && n->permanent)
 885				goto skip;
 886			if (!dev || n->dev == dev) {
 887				rcu_assign_pointer(*np, n->next);
 888				list_add(&n->free_node, &head);
 889				continue;
 890			}
 891skip:
 892			np = &n->next;
 893		}
 894	}
 895
 896	mutex_unlock(&tbl->phash_lock);
 897
 898	while (!list_empty(&head)) {
 899		n = list_first_entry(&head, typeof(*n), free_node);
 900		list_del(&n->free_node);
 901
 902		if (tbl->pdestructor)
 903			tbl->pdestructor(n);
 904
 905		call_rcu(&n->rcu, pneigh_destroy);
 906	}
 907}
 908
 909static inline void neigh_parms_put(struct neigh_parms *parms)
 910{
 911	if (refcount_dec_and_test(&parms->refcnt))
 912		kfree(parms);
 913}
 914
 915/*
 916 *	neighbour must already be out of the table;
 917 *
 918 */
 919void neigh_destroy(struct neighbour *neigh)
 920{
 921	struct net_device *dev = neigh->dev;
 922
 923	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
 924
 925	if (!neigh->dead) {
 926		pr_warn("Destroying alive neighbour %p\n", neigh);
 927		dump_stack();
 928		return;
 929	}
 930
 931	if (neigh_del_timer(neigh))
 932		pr_warn("Impossible event\n");
 933
 934	write_lock_bh(&neigh->lock);
 935	__skb_queue_purge(&neigh->arp_queue);
 936	write_unlock_bh(&neigh->lock);
 937	neigh->arp_queue_len_bytes = 0;
 938
 939	if (dev->netdev_ops->ndo_neigh_destroy)
 940		dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
 941
 942	netdev_put(dev, &neigh->dev_tracker);
 943	neigh_parms_put(neigh->parms);
 944
 945	neigh_dbg(2, "neigh %p is destroyed\n", neigh);
 946
 947	atomic_dec(&neigh->tbl->entries);
 948	kfree_rcu(neigh, rcu);
 949}
 950EXPORT_SYMBOL(neigh_destroy);
 951
 952/* Neighbour state is suspicious;
 953   disable fast path.
 954
 955   Called with write_locked neigh.
 956 */
 957static void neigh_suspect(struct neighbour *neigh)
 958{
 959	neigh_dbg(2, "neigh %p is suspected\n", neigh);
 960
 961	WRITE_ONCE(neigh->output, neigh->ops->output);
 962}
 963
 964/* Neighbour state is OK;
 965   enable fast path.
 966
 967   Called with write_locked neigh.
 968 */
 969static void neigh_connect(struct neighbour *neigh)
 970{
 971	neigh_dbg(2, "neigh %p is connected\n", neigh);
 972
 973	WRITE_ONCE(neigh->output, neigh->ops->connected_output);
 974}
 975
 976static void neigh_periodic_work(struct work_struct *work)
 977{
 978	struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
 979	struct neigh_hash_table *nht;
 980	struct hlist_node *tmp;
 981	struct neighbour *n;
 982	unsigned int i;
 983
 984	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
 985
 986	spin_lock_bh(&tbl->lock);
 987	nht = rcu_dereference_protected(tbl->nht,
 988					lockdep_is_held(&tbl->lock));
 989
 990	/*
 991	 *	periodically recompute ReachableTime from random function
 992	 */
 993
 994	if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
 995		struct neigh_parms *p;
 996
 997		WRITE_ONCE(tbl->last_rand, jiffies);
 998		list_for_each_entry(p, &tbl->parms_list, list)
 999			neigh_set_reach_time(p);
1000	}
1001
1002	if (atomic_read(&tbl->entries) < READ_ONCE(tbl->gc_thresh1))
1003		goto out;
1004
1005	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
1006		neigh_for_each_in_bucket_safe(n, tmp, &nht->hash_heads[i]) {
1007			unsigned int state;
1008
1009			write_lock(&n->lock);
1010
1011			state = n->nud_state;
1012			if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
1013			    (n->flags &
1014			     (NTF_EXT_LEARNED | NTF_EXT_VALIDATED))) {
1015				write_unlock(&n->lock);
1016				continue;
1017			}
1018
1019			if (time_before(n->used, n->confirmed) &&
1020			    time_is_before_eq_jiffies(n->confirmed))
1021				n->used = n->confirmed;
1022
1023			if (refcount_read(&n->refcnt) == 1 &&
1024			    (state == NUD_FAILED ||
1025			     !time_in_range_open(jiffies, n->used,
1026						 n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
1027				hlist_del_rcu(&n->hash);
1028				hlist_del_rcu(&n->dev_list);
1029				neigh_mark_dead(n);
1030				write_unlock(&n->lock);
1031				neigh_cleanup_and_release(n);
1032				continue;
1033			}
1034			write_unlock(&n->lock);
1035		}
1036		/*
1037		 * It's fine to release lock here, even if hash table
1038		 * grows while we are preempted.
1039		 */
1040		spin_unlock_bh(&tbl->lock);
1041		cond_resched();
1042		spin_lock_bh(&tbl->lock);
1043		nht = rcu_dereference_protected(tbl->nht,
1044						lockdep_is_held(&tbl->lock));
1045	}
1046out:
1047	/* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
1048	 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
1049	 * BASE_REACHABLE_TIME.
1050	 */
1051	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1052			      NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
1053	spin_unlock_bh(&tbl->lock);
1054}
1055
1056static __inline__ int neigh_max_probes(struct neighbour *n)
1057{
1058	struct neigh_parms *p = n->parms;
1059	return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
1060	       (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
1061	        NEIGH_VAR(p, MCAST_PROBES));
1062}
1063
1064static void neigh_invalidate(struct neighbour *neigh)
1065	__releases(neigh->lock)
1066	__acquires(neigh->lock)
1067{
1068	struct sk_buff *skb;
1069
1070	NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
1071	neigh_dbg(2, "neigh %p is failed\n", neigh);
1072	neigh->updated = jiffies;
1073
1074	/* It is very thin place. report_unreachable is very complicated
1075	   routine. Particularly, it can hit the same neighbour entry!
1076
1077	   So that, we try to be accurate and avoid dead loop. --ANK
1078	 */
1079	while (neigh->nud_state == NUD_FAILED &&
1080	       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1081		write_unlock(&neigh->lock);
1082		neigh->ops->error_report(neigh, skb);
1083		write_lock(&neigh->lock);
1084	}
1085	__skb_queue_purge(&neigh->arp_queue);
1086	neigh->arp_queue_len_bytes = 0;
1087}
1088
1089static void neigh_probe(struct neighbour *neigh)
1090	__releases(neigh->lock)
1091{
1092	struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
1093	/* keep skb alive even if arp_queue overflows */
1094	if (skb)
1095		skb = skb_clone(skb, GFP_ATOMIC);
1096	write_unlock(&neigh->lock);
1097	if (neigh->ops->solicit)
1098		neigh->ops->solicit(neigh, skb);
1099	atomic_inc(&neigh->probes);
1100	consume_skb(skb);
1101}
1102
1103/* Called when a timer expires for a neighbour entry. */
1104
1105static void neigh_timer_handler(struct timer_list *t)
1106{
1107	unsigned long now, next;
1108	struct neighbour *neigh = timer_container_of(neigh, t, timer);
1109	unsigned int state;
1110	int notify = 0;
1111
1112	write_lock(&neigh->lock);
1113
1114	state = neigh->nud_state;
1115	now = jiffies;
1116	next = now + HZ;
1117
1118	if (!(state & NUD_IN_TIMER))
1119		goto out;
1120
1121	if (state & NUD_REACHABLE) {
1122		if (time_before_eq(now,
1123				   neigh->confirmed + neigh->parms->reachable_time)) {
1124			neigh_dbg(2, "neigh %p is still alive\n", neigh);
1125			next = neigh->confirmed + neigh->parms->reachable_time;
1126		} else if (time_before_eq(now,
1127					  neigh->used +
1128					  NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
1129			neigh_dbg(2, "neigh %p is delayed\n", neigh);
1130			WRITE_ONCE(neigh->nud_state, NUD_DELAY);
1131			neigh->updated = jiffies;
1132			neigh_suspect(neigh);
1133			next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
1134		} else {
1135			neigh_dbg(2, "neigh %p is suspected\n", neigh);
1136			WRITE_ONCE(neigh->nud_state, NUD_STALE);
1137			neigh->updated = jiffies;
1138			neigh_suspect(neigh);
1139			notify = 1;
1140		}
1141	} else if (state & NUD_DELAY) {
1142		if (time_before_eq(now,
1143				   neigh->confirmed +
1144				   NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
1145			neigh_dbg(2, "neigh %p is now reachable\n", neigh);
1146			WRITE_ONCE(neigh->nud_state, NUD_REACHABLE);
1147			neigh->updated = jiffies;
1148			neigh_connect(neigh);
1149			notify = 1;
1150			next = neigh->confirmed + neigh->parms->reachable_time;
1151		} else {
1152			neigh_dbg(2, "neigh %p is probed\n", neigh);
1153			WRITE_ONCE(neigh->nud_state, NUD_PROBE);
1154			neigh->updated = jiffies;
1155			atomic_set(&neigh->probes, 0);
1156			notify = 1;
1157			next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1158					 HZ/100);
1159		}
1160	} else {
1161		/* NUD_PROBE|NUD_INCOMPLETE */
1162		next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), HZ/100);
1163	}
1164
1165	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
1166	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
1167		if (neigh->nud_state == NUD_PROBE &&
1168		    neigh->flags & NTF_EXT_VALIDATED) {
1169			WRITE_ONCE(neigh->nud_state, NUD_STALE);
1170			neigh->updated = jiffies;
1171		} else {
1172			WRITE_ONCE(neigh->nud_state, NUD_FAILED);
1173			neigh_invalidate(neigh);
1174		}
1175		notify = 1;
1176		goto out;
1177	}
1178
1179	if (neigh->nud_state & NUD_IN_TIMER) {
1180		if (time_before(next, jiffies + HZ/100))
1181			next = jiffies + HZ/100;
1182		if (!mod_timer(&neigh->timer, next))
1183			neigh_hold(neigh);
1184	}
1185	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
1186		neigh_probe(neigh);
1187	} else {
1188out:
1189		write_unlock(&neigh->lock);
1190	}
1191
1192	if (notify)
1193		neigh_update_notify(neigh, 0);
1194
1195	trace_neigh_timer_handler(neigh, 0);
1196
1197	neigh_release(neigh);
1198}
1199
1200int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb,
1201		       const bool immediate_ok)
1202{
1203	int rc;
1204	bool immediate_probe = false;
1205
1206	write_lock_bh(&neigh->lock);
1207
1208	rc = 0;
1209	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
1210		goto out_unlock_bh;
1211	if (neigh->dead)
1212		goto out_dead;
1213
1214	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
1215		if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
1216		    NEIGH_VAR(neigh->parms, APP_PROBES)) {
1217			unsigned long next, now = jiffies;
1218
1219			atomic_set(&neigh->probes,
1220				   NEIGH_VAR(neigh->parms, UCAST_PROBES));
1221			neigh_del_timer(neigh);
1222			WRITE_ONCE(neigh->nud_state, NUD_INCOMPLETE);
1223			neigh->updated = now;
1224			if (!immediate_ok) {
1225				next = now + 1;
1226			} else {
1227				immediate_probe = true;
1228				next = now + max(NEIGH_VAR(neigh->parms,
1229							   RETRANS_TIME),
1230						 HZ / 100);
1231			}
1232			neigh_add_timer(neigh, next);
1233		} else {
1234			WRITE_ONCE(neigh->nud_state, NUD_FAILED);
1235			neigh->updated = jiffies;
1236			write_unlock_bh(&neigh->lock);
1237
1238			kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_FAILED);
1239			return 1;
1240		}
1241	} else if (neigh->nud_state & NUD_STALE) {
1242		neigh_dbg(2, "neigh %p is delayed\n", neigh);
1243		neigh_del_timer(neigh);
1244		WRITE_ONCE(neigh->nud_state, NUD_DELAY);
1245		neigh->updated = jiffies;
1246		neigh_add_timer(neigh, jiffies +
1247				NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
1248	}
1249
1250	if (neigh->nud_state == NUD_INCOMPLETE) {
1251		if (skb) {
1252			while (neigh->arp_queue_len_bytes + skb->truesize >
1253			       NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
1254				struct sk_buff *buff;
1255
1256				buff = __skb_dequeue(&neigh->arp_queue);
1257				if (!buff)
1258					break;
1259				neigh->arp_queue_len_bytes -= buff->truesize;
1260				kfree_skb_reason(buff, SKB_DROP_REASON_NEIGH_QUEUEFULL);
1261				NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
1262			}
1263			skb_dst_force(skb);
1264			__skb_queue_tail(&neigh->arp_queue, skb);
1265			neigh->arp_queue_len_bytes += skb->truesize;
1266		}
1267		rc = 1;
1268	}
1269out_unlock_bh:
1270	if (immediate_probe)
1271		neigh_probe(neigh);
1272	else
1273		write_unlock(&neigh->lock);
1274	local_bh_enable();
1275	trace_neigh_event_send_done(neigh, rc);
1276	return rc;
1277
1278out_dead:
1279	if (neigh->nud_state & NUD_STALE)
1280		goto out_unlock_bh;
1281	write_unlock_bh(&neigh->lock);
1282	kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_DEAD);
1283	trace_neigh_event_send_dead(neigh, 1);
1284	return 1;
1285}
1286EXPORT_SYMBOL(__neigh_event_send);
1287
1288static void neigh_update_hhs(struct neighbour *neigh)
1289{
1290	struct hh_cache *hh;
1291	void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
1292		= NULL;
1293
1294	if (neigh->dev->header_ops)
1295		update = neigh->dev->header_ops->cache_update;
1296
1297	if (update) {
1298		hh = &neigh->hh;
1299		if (READ_ONCE(hh->hh_len)) {
1300			write_seqlock_bh(&hh->hh_lock);
1301			update(hh, neigh->dev, neigh->ha);
1302			write_sequnlock_bh(&hh->hh_lock);
1303		}
1304	}
1305}
1306
1307/* Generic update routine.
1308   -- lladdr is new lladdr or NULL, if it is not supplied.
1309   -- new    is new state.
1310   -- flags
1311	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1312				if it is different.
1313	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
1314				lladdr instead of overriding it
1315				if it is different.
1316	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
1317	NEIGH_UPDATE_F_USE	means that the entry is user triggered.
1318	NEIGH_UPDATE_F_MANAGED	means that the entry will be auto-refreshed.
1319	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
1320				NTF_ROUTER flag.
1321	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
1322				a router.
1323	NEIGH_UPDATE_F_EXT_VALIDATED means that the entry will not be removed
1324				or invalidated.
1325
1326   Caller MUST hold reference count on the entry.
1327 */
1328static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
1329			  u8 new, u32 flags, u32 nlmsg_pid,
1330			  struct netlink_ext_ack *extack)
1331{
1332	bool gc_update = false, managed_update = false;
1333	int update_isrouter = 0;
1334	struct net_device *dev;
1335	int err, notify = 0;
1336	u8 old;
1337
1338	trace_neigh_update(neigh, lladdr, new, flags, nlmsg_pid);
1339
1340	write_lock_bh(&neigh->lock);
1341
1342	dev    = neigh->dev;
1343	old    = neigh->nud_state;
1344	err    = -EPERM;
1345
1346	if (neigh->dead) {
1347		NL_SET_ERR_MSG(extack, "Neighbor entry is now dead");
1348		new = old;
1349		goto out;
1350	}
1351	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
1352	    (old & (NUD_NOARP | NUD_PERMANENT)))
1353		goto out;
1354
1355	neigh_update_flags(neigh, flags, &notify, &gc_update, &managed_update);
1356	if (flags & (NEIGH_UPDATE_F_USE | NEIGH_UPDATE_F_MANAGED)) {
1357		new = old & ~NUD_PERMANENT;
1358		WRITE_ONCE(neigh->nud_state, new);
1359		err = 0;
1360		goto out;
1361	}
1362
1363	if (!(new & NUD_VALID)) {
1364		neigh_del_timer(neigh);
1365		if (old & NUD_CONNECTED)
1366			neigh_suspect(neigh);
1367		WRITE_ONCE(neigh->nud_state, new);
1368		err = 0;
1369		notify = old & NUD_VALID;
1370		if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
1371		    (new & NUD_FAILED)) {
1372			neigh_invalidate(neigh);
1373			notify = 1;
1374		}
1375		goto out;
1376	}
1377
1378	/* Compare new lladdr with cached one */
1379	if (!dev->addr_len) {
1380		/* First case: device needs no address. */
1381		lladdr = neigh->ha;
1382	} else if (lladdr) {
1383		/* The second case: if something is already cached
1384		   and a new address is proposed:
1385		   - compare new & old
1386		   - if they are different, check override flag
1387		 */
1388		if ((old & NUD_VALID) &&
1389		    !memcmp(lladdr, neigh->ha, dev->addr_len))
1390			lladdr = neigh->ha;
1391	} else {
1392		/* No address is supplied; if we know something,
1393		   use it, otherwise discard the request.
1394		 */
1395		err = -EINVAL;
1396		if (!(old & NUD_VALID)) {
1397			NL_SET_ERR_MSG(extack, "No link layer address given");
1398			goto out;
1399		}
1400		lladdr = neigh->ha;
1401	}
1402
1403	/* Update confirmed timestamp for neighbour entry after we
1404	 * received ARP packet even if it doesn't change IP to MAC binding.
1405	 */
1406	if (new & NUD_CONNECTED)
1407		neigh->confirmed = jiffies;
1408
1409	/* If entry was valid and address is not changed,
1410	   do not change entry state, if new one is STALE.
1411	 */
1412	err = 0;
1413	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1414	if (old & NUD_VALID) {
1415		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1416			update_isrouter = 0;
1417			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1418			    (old & NUD_CONNECTED)) {
1419				lladdr = neigh->ha;
1420				new = NUD_STALE;
1421			} else
1422				goto out;
1423		} else {
1424			if (lladdr == neigh->ha && new == NUD_STALE &&
1425			    !(flags & NEIGH_UPDATE_F_ADMIN))
1426				new = old;
1427		}
1428	}
1429
1430	/* Update timestamp only once we know we will make a change to the
1431	 * neighbour entry. Otherwise we risk to move the locktime window with
1432	 * noop updates and ignore relevant ARP updates.
1433	 */
1434	if (new != old || lladdr != neigh->ha)
1435		neigh->updated = jiffies;
1436
1437	if (new != old) {
1438		neigh_del_timer(neigh);
1439		if (new & NUD_PROBE)
1440			atomic_set(&neigh->probes, 0);
1441		if (new & NUD_IN_TIMER)
1442			neigh_add_timer(neigh, (jiffies +
1443						((new & NUD_REACHABLE) ?
1444						 neigh->parms->reachable_time :
1445						 0)));
1446		WRITE_ONCE(neigh->nud_state, new);
1447		notify = 1;
1448	}
1449
1450	if (lladdr != neigh->ha) {
1451		write_seqlock(&neigh->ha_lock);
1452		memcpy(&neigh->ha, lladdr, dev->addr_len);
1453		write_sequnlock(&neigh->ha_lock);
1454		neigh_update_hhs(neigh);
1455		if (!(new & NUD_CONNECTED))
1456			neigh->confirmed = jiffies -
1457				      (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
1458		notify = 1;
1459	}
1460	if (new == old)
1461		goto out;
1462	if (new & NUD_CONNECTED)
1463		neigh_connect(neigh);
1464	else
1465		neigh_suspect(neigh);
1466	if (!(old & NUD_VALID)) {
1467		struct sk_buff *skb;
1468
1469		/* Again: avoid dead loop if something went wrong */
1470
1471		while (neigh->nud_state & NUD_VALID &&
1472		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1473			struct dst_entry *dst = skb_dst(skb);
1474			struct neighbour *n2, *n1 = neigh;
1475			write_unlock_bh(&neigh->lock);
1476
1477			rcu_read_lock();
1478
1479			/* Why not just use 'neigh' as-is?  The problem is that
1480			 * things such as shaper, eql, and sch_teql can end up
1481			 * using alternative, different, neigh objects to output
1482			 * the packet in the output path.  So what we need to do
1483			 * here is re-lookup the top-level neigh in the path so
1484			 * we can reinject the packet there.
1485			 */
1486			n2 = NULL;
1487			if (dst &&
1488			    READ_ONCE(dst->obsolete) != DST_OBSOLETE_DEAD) {
1489				n2 = dst_neigh_lookup_skb(dst, skb);
1490				if (n2)
1491					n1 = n2;
1492			}
1493			READ_ONCE(n1->output)(n1, skb);
1494			if (n2)
1495				neigh_release(n2);
1496			rcu_read_unlock();
1497
1498			write_lock_bh(&neigh->lock);
1499		}
1500		__skb_queue_purge(&neigh->arp_queue);
1501		neigh->arp_queue_len_bytes = 0;
1502	}
1503out:
1504	if (update_isrouter)
1505		neigh_update_is_router(neigh, flags, &notify);
1506	write_unlock_bh(&neigh->lock);
1507	if (((new ^ old) & NUD_PERMANENT) || gc_update)
1508		neigh_update_gc_list(neigh);
1509	if (managed_update)
1510		neigh_update_managed_list(neigh);
1511	if (notify)
1512		neigh_update_notify(neigh, nlmsg_pid);
1513	trace_neigh_update_done(neigh, err);
1514	return err;
1515}
1516
1517int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1518		 u32 flags, u32 nlmsg_pid)
1519{
1520	return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL);
1521}
1522EXPORT_SYMBOL(neigh_update);
1523
1524/* Update the neigh to listen temporarily for probe responses, even if it is
1525 * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
1526 */
1527void __neigh_set_probe_once(struct neighbour *neigh)
1528{
1529	if (neigh->dead)
1530		return;
1531	neigh->updated = jiffies;
1532	if (!(neigh->nud_state & NUD_FAILED))
1533		return;
1534	WRITE_ONCE(neigh->nud_state, NUD_INCOMPLETE);
1535	atomic_set(&neigh->probes, neigh_max_probes(neigh));
1536	neigh_add_timer(neigh,
1537			jiffies + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1538				      HZ/100));
1539}
1540EXPORT_SYMBOL(__neigh_set_probe_once);
1541
1542struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1543				 u8 *lladdr, void *saddr,
1544				 struct net_device *dev)
1545{
1546	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1547						 lladdr || !dev->addr_len);
1548	if (neigh)
1549		neigh_update(neigh, lladdr, NUD_STALE,
1550			     NEIGH_UPDATE_F_OVERRIDE, 0);
1551	return neigh;
1552}
1553EXPORT_SYMBOL(neigh_event_ns);
1554
1555/* called with read_lock_bh(&n->lock); */
1556static void neigh_hh_init(struct neighbour *n)
1557{
1558	struct net_device *dev = n->dev;
1559	__be16 prot = n->tbl->protocol;
1560	struct hh_cache	*hh = &n->hh;
1561
1562	write_lock_bh(&n->lock);
1563
1564	/* Only one thread can come in here and initialize the
1565	 * hh_cache entry.
1566	 */
1567	if (!hh->hh_len)
1568		dev->header_ops->cache(n, hh, prot);
1569
1570	write_unlock_bh(&n->lock);
1571}
1572
1573/* Slow and careful. */
1574
1575int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1576{
1577	int rc = 0;
1578
1579	if (!neigh_event_send(neigh, skb)) {
1580		int err;
1581		struct net_device *dev = neigh->dev;
1582		unsigned int seq;
1583
1584		if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len))
1585			neigh_hh_init(neigh);
1586
1587		do {
1588			__skb_pull(skb, skb_network_offset(skb));
1589			seq = read_seqbegin(&neigh->ha_lock);
1590			err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1591					      neigh->ha, NULL, skb->len);
1592		} while (read_seqretry(&neigh->ha_lock, seq));
1593
1594		if (err >= 0)
1595			rc = dev_queue_xmit(skb);
1596		else
1597			goto out_kfree_skb;
1598	}
1599out:
1600	return rc;
1601out_kfree_skb:
1602	rc = -EINVAL;
1603	kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_HH_FILLFAIL);
1604	goto out;
1605}
1606EXPORT_SYMBOL(neigh_resolve_output);
1607
1608/* As fast as possible without hh cache */
1609
1610int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
1611{
1612	struct net_device *dev = neigh->dev;
1613	unsigned int seq;
1614	int err;
1615
1616	do {
1617		__skb_pull(skb, skb_network_offset(skb));
1618		seq = read_seqbegin(&neigh->ha_lock);
1619		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1620				      neigh->ha, NULL, skb->len);
1621	} while (read_seqretry(&neigh->ha_lock, seq));
1622
1623	if (err >= 0)
1624		err = dev_queue_xmit(skb);
1625	else {
1626		err = -EINVAL;
1627		kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_HH_FILLFAIL);
1628	}
1629	return err;
1630}
1631EXPORT_SYMBOL(neigh_connected_output);
1632
1633int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1634{
1635	return dev_queue_xmit(skb);
1636}
1637EXPORT_SYMBOL(neigh_direct_output);
1638
1639static void neigh_managed_work(struct work_struct *work)
1640{
1641	struct neigh_table *tbl = container_of(work, struct neigh_table,
1642					       managed_work.work);
1643	struct neighbour *neigh;
1644
1645	spin_lock_bh(&tbl->lock);
1646	list_for_each_entry(neigh, &tbl->managed_list, managed_list)
1647		neigh_event_send_probe(neigh, NULL, false);
1648	queue_delayed_work(system_power_efficient_wq, &tbl->managed_work,
1649			   NEIGH_VAR(&tbl->parms, INTERVAL_PROBE_TIME_MS));
1650	spin_unlock_bh(&tbl->lock);
1651}
1652
1653static void neigh_proxy_process(struct timer_list *t)
1654{
1655	struct neigh_table *tbl = timer_container_of(tbl, t, proxy_timer);
1656	long sched_next = 0;
1657	unsigned long now = jiffies;
1658	struct sk_buff *skb, *n;
1659
1660	spin_lock(&tbl->proxy_queue.lock);
1661
1662	skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1663		long tdif = NEIGH_CB(skb)->sched_next - now;
1664
1665		if (tdif <= 0) {
1666			struct net_device *dev = skb->dev;
1667
1668			neigh_parms_qlen_dec(dev, tbl->family);
1669			__skb_unlink(skb, &tbl->proxy_queue);
1670
1671			if (tbl->proxy_redo && netif_running(dev)) {
1672				rcu_read_lock();
1673				tbl->proxy_redo(skb);
1674				rcu_read_unlock();
1675			} else {
1676				kfree_skb(skb);
1677			}
1678
1679			dev_put(dev);
1680		} else if (!sched_next || tdif < sched_next)
1681			sched_next = tdif;
1682	}
1683	timer_delete(&tbl->proxy_timer);
1684	if (sched_next)
1685		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1686	spin_unlock(&tbl->proxy_queue.lock);
1687}
1688
1689static unsigned long neigh_proxy_delay(struct neigh_parms *p)
1690{
1691	/* If proxy_delay is zero, do not call get_random_u32_below()
1692	 * as it is undefined behavior.
1693	 */
1694	unsigned long proxy_delay = NEIGH_VAR(p, PROXY_DELAY);
1695
1696	return proxy_delay ?
1697	       jiffies + get_random_u32_below(proxy_delay) : jiffies;
1698}
1699
1700void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1701		    struct sk_buff *skb)
1702{
1703	unsigned long sched_next = neigh_proxy_delay(p);
1704
1705	if (p->qlen > NEIGH_VAR(p, PROXY_QLEN)) {
1706		kfree_skb(skb);
1707		return;
1708	}
1709
1710	NEIGH_CB(skb)->sched_next = sched_next;
1711	NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1712
1713	spin_lock(&tbl->proxy_queue.lock);
1714	if (timer_delete(&tbl->proxy_timer)) {
1715		if (time_before(tbl->proxy_timer.expires, sched_next))
1716			sched_next = tbl->proxy_timer.expires;
1717	}
1718	skb_dst_drop(skb);
1719	dev_hold(skb->dev);
1720	__skb_queue_tail(&tbl->proxy_queue, skb);
1721	p->qlen++;
1722	mod_timer(&tbl->proxy_timer, sched_next);
1723	spin_unlock(&tbl->proxy_queue.lock);
1724}
1725EXPORT_SYMBOL(pneigh_enqueue);
1726
1727static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1728						      struct net *net, int ifindex)
1729{
1730	struct neigh_parms *p;
1731
1732	list_for_each_entry(p, &tbl->parms_list, list) {
1733		if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1734		    (!p->dev && !ifindex && net_eq(net, &init_net)))
1735			return p;
1736	}
1737
1738	return NULL;
1739}
1740
1741struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1742				      struct neigh_table *tbl)
1743{
1744	struct neigh_parms *p;
1745	struct net *net = dev_net(dev);
1746	const struct net_device_ops *ops = dev->netdev_ops;
1747
1748	p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
1749	if (p) {
1750		p->tbl		  = tbl;
1751		refcount_set(&p->refcnt, 1);
1752		neigh_set_reach_time(p);
1753		p->qlen = 0;
1754		netdev_hold(dev, &p->dev_tracker, GFP_KERNEL);
1755		p->dev = dev;
1756		write_pnet(&p->net, net);
1757		p->sysctl_table = NULL;
1758
1759		if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
1760			netdev_put(dev, &p->dev_tracker);
1761			kfree(p);
1762			return NULL;
1763		}
1764
1765		spin_lock_bh(&tbl->lock);
1766		list_add_rcu(&p->list, &tbl->parms.list);
1767		spin_unlock_bh(&tbl->lock);
1768
1769		neigh_parms_data_state_cleanall(p);
1770	}
1771	return p;
1772}
1773EXPORT_SYMBOL(neigh_parms_alloc);
1774
1775static void neigh_rcu_free_parms(struct rcu_head *head)
1776{
1777	struct neigh_parms *parms =
1778		container_of(head, struct neigh_parms, rcu_head);
1779
1780	neigh_parms_put(parms);
1781}
1782
1783void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1784{
1785	if (!parms || parms == &tbl->parms)
1786		return;
1787
1788	spin_lock_bh(&tbl->lock);
1789	list_del_rcu(&parms->list);
1790	parms->dead = 1;
1791	spin_unlock_bh(&tbl->lock);
1792
1793	netdev_put(parms->dev, &parms->dev_tracker);
1794	call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1795}
1796EXPORT_SYMBOL(neigh_parms_release);
1797
1798static struct lock_class_key neigh_table_proxy_queue_class;
1799
1800static struct neigh_table __rcu *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1801
1802void neigh_table_init(int index, struct neigh_table *tbl)
1803{
1804	unsigned long now = jiffies;
1805	unsigned long phsize;
1806
1807	INIT_LIST_HEAD(&tbl->parms_list);
1808	INIT_LIST_HEAD(&tbl->gc_list);
1809	INIT_LIST_HEAD(&tbl->managed_list);
1810
1811	list_add(&tbl->parms.list, &tbl->parms_list);
1812	write_pnet(&tbl->parms.net, &init_net);
1813	refcount_set(&tbl->parms.refcnt, 1);
1814	neigh_set_reach_time(&tbl->parms);
1815	tbl->parms.qlen = 0;
1816
1817	tbl->stats = alloc_percpu(struct neigh_statistics);
1818	if (!tbl->stats)
1819		panic("cannot create neighbour cache statistics");
1820
1821#ifdef CONFIG_PROC_FS
1822	if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
1823			      &neigh_stat_seq_ops, tbl))
1824		panic("cannot create neighbour proc dir entry");
1825#endif
1826
1827	RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
1828
1829	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1830	tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
1831
1832	if (!tbl->nht || !tbl->phash_buckets)
1833		panic("cannot allocate neighbour cache hashes");
1834
1835	if (!tbl->entry_size)
1836		tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
1837					tbl->key_len, NEIGH_PRIV_ALIGN);
1838	else
1839		WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
1840
1841	spin_lock_init(&tbl->lock);
1842	mutex_init(&tbl->phash_lock);
1843
1844	INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1845	queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1846			tbl->parms.reachable_time);
1847	INIT_DEFERRABLE_WORK(&tbl->managed_work, neigh_managed_work);
1848	queue_delayed_work(system_power_efficient_wq, &tbl->managed_work, 0);
1849
1850	timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
1851	skb_queue_head_init_class(&tbl->proxy_queue,
1852			&neigh_table_proxy_queue_class);
1853
1854	tbl->last_flush = now;
1855	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1856
1857	rcu_assign_pointer(neigh_tables[index], tbl);
1858}
1859EXPORT_SYMBOL(neigh_table_init);
1860
1861/*
1862 * Only called from ndisc_cleanup(), which means this is dead code
1863 * because we no longer can unload IPv6 module.
1864 */
1865int neigh_table_clear(int index, struct neigh_table *tbl)
1866{
1867	RCU_INIT_POINTER(neigh_tables[index], NULL);
1868	synchronize_rcu();
1869
1870	/* It is not clean... Fix it to unload IPv6 module safely */
1871	cancel_delayed_work_sync(&tbl->managed_work);
1872	cancel_delayed_work_sync(&tbl->gc_work);
1873	timer_delete_sync(&tbl->proxy_timer);
1874	pneigh_queue_purge(&tbl->proxy_queue, NULL, tbl->family);
1875	neigh_ifdown(tbl, NULL);
1876	if (atomic_read(&tbl->entries))
1877		pr_crit("neighbour leakage\n");
1878
1879	call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1880		 neigh_hash_free_rcu);
1881	tbl->nht = NULL;
1882
1883	kfree(tbl->phash_buckets);
1884	tbl->phash_buckets = NULL;
1885
1886	remove_proc_entry(tbl->id, init_net.proc_net_stat);
1887
1888	free_percpu(tbl->stats);
1889	tbl->stats = NULL;
1890
1891	return 0;
1892}
1893EXPORT_SYMBOL(neigh_table_clear);
1894
1895static struct neigh_table *neigh_find_table(int family)
1896{
1897	struct neigh_table *tbl = NULL;
1898
1899	switch (family) {
1900	case AF_INET:
1901		tbl = rcu_dereference_rtnl(neigh_tables[NEIGH_ARP_TABLE]);
1902		break;
1903	case AF_INET6:
1904		tbl = rcu_dereference_rtnl(neigh_tables[NEIGH_ND_TABLE]);
1905		break;
1906	}
1907
1908	return tbl;
1909}
1910
1911const struct nla_policy nda_policy[NDA_MAX+1] = {
1912	[NDA_UNSPEC]		= { .strict_start_type = NDA_NH_ID },
1913	[NDA_DST]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1914	[NDA_LLADDR]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1915	[NDA_CACHEINFO]		= { .len = sizeof(struct nda_cacheinfo) },
1916	[NDA_PROBES]		= { .type = NLA_U32 },
1917	[NDA_VLAN]		= { .type = NLA_U16 },
1918	[NDA_PORT]		= { .type = NLA_U16 },
1919	[NDA_VNI]		= { .type = NLA_U32 },
1920	[NDA_IFINDEX]		= { .type = NLA_U32 },
1921	[NDA_MASTER]		= { .type = NLA_U32 },
1922	[NDA_PROTOCOL]		= { .type = NLA_U8 },
1923	[NDA_NH_ID]		= { .type = NLA_U32 },
1924	[NDA_FLAGS_EXT]		= NLA_POLICY_MASK(NLA_U32, NTF_EXT_MASK),
1925	[NDA_FDB_EXT_ATTRS]	= { .type = NLA_NESTED },
1926};
1927
1928static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1929			struct netlink_ext_ack *extack)
1930{
1931	struct net *net = sock_net(skb->sk);
1932	struct ndmsg *ndm;
1933	struct nlattr *dst_attr;
1934	struct neigh_table *tbl;
1935	struct neighbour *neigh;
1936	struct net_device *dev = NULL;
1937	int err = -EINVAL;
1938
1939	ASSERT_RTNL();
1940	if (nlmsg_len(nlh) < sizeof(*ndm))
1941		goto out;
1942
1943	dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1944	if (!dst_attr) {
1945		NL_SET_ERR_MSG(extack, "Network address not specified");
1946		goto out;
1947	}
1948
1949	ndm = nlmsg_data(nlh);
1950	if (ndm->ndm_ifindex) {
1951		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1952		if (dev == NULL) {
1953			err = -ENODEV;
1954			goto out;
1955		}
1956	}
1957
1958	tbl = neigh_find_table(ndm->ndm_family);
1959	if (tbl == NULL)
1960		return -EAFNOSUPPORT;
1961
1962	if (nla_len(dst_attr) < (int)tbl->key_len) {
1963		NL_SET_ERR_MSG(extack, "Invalid network address");
1964		goto out;
1965	}
1966
1967	if (ndm->ndm_flags & NTF_PROXY) {
1968		err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1969		goto out;
1970	}
1971
1972	if (dev == NULL)
1973		goto out;
1974
1975	neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1976	if (neigh == NULL) {
1977		err = -ENOENT;
1978		goto out;
1979	}
1980
1981	err = __neigh_update(neigh, NULL, NUD_FAILED,
1982			     NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN,
1983			     NETLINK_CB(skb).portid, extack);
1984	spin_lock_bh(&tbl->lock);
1985	neigh_release(neigh);
1986	neigh_remove_one(neigh);
1987	spin_unlock_bh(&tbl->lock);
1988
1989out:
1990	return err;
1991}
1992
1993static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1994		     struct netlink_ext_ack *extack)
1995{
1996	int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
1997		    NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1998	struct net *net = sock_net(skb->sk);
1999	struct ndmsg *ndm;
2000	struct nlattr *tb[NDA_MAX+1];
2001	struct neigh_table *tbl;
2002	struct net_device *dev = NULL;
2003	struct neighbour *neigh;
2004	void *dst, *lladdr;
2005	u8 protocol = 0;
2006	u32 ndm_flags;
2007	int err;
2008
2009	ASSERT_RTNL();
2010	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
2011				     nda_policy, extack);
2012	if (err < 0)
2013		goto out;
2014
2015	err = -EINVAL;
2016	if (!tb[NDA_DST]) {
2017		NL_SET_ERR_MSG(extack, "Network address not specified");
2018		goto out;
2019	}
2020
2021	ndm = nlmsg_data(nlh);
2022	ndm_flags = ndm->ndm_flags;
2023	if (tb[NDA_FLAGS_EXT]) {
2024		u32 ext = nla_get_u32(tb[NDA_FLAGS_EXT]);
2025
2026		BUILD_BUG_ON(sizeof(neigh->flags) * BITS_PER_BYTE <
2027			     (sizeof(ndm->ndm_flags) * BITS_PER_BYTE +
2028			      hweight32(NTF_EXT_MASK)));
2029		ndm_flags |= (ext << NTF_EXT_SHIFT);
2030	}
2031	if (ndm->ndm_ifindex) {
2032		dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2033		if (dev == NULL) {
2034			err = -ENODEV;
2035			goto out;
2036		}
2037
2038		if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) {
2039			NL_SET_ERR_MSG(extack, "Invalid link address");
2040			goto out;
2041		}
2042	}
2043
2044	tbl = neigh_find_table(ndm->ndm_family);
2045	if (tbl == NULL)
2046		return -EAFNOSUPPORT;
2047
2048	if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) {
2049		NL_SET_ERR_MSG(extack, "Invalid network address");
2050		goto out;
2051	}
2052
2053	dst = nla_data(tb[NDA_DST]);
2054	lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
2055
2056	if (tb[NDA_PROTOCOL])
2057		protocol = nla_get_u8(tb[NDA_PROTOCOL]);
2058	if (ndm_flags & NTF_PROXY) {
2059		if (ndm_flags & (NTF_MANAGED | NTF_EXT_VALIDATED)) {
2060			NL_SET_ERR_MSG(extack, "Invalid NTF_* flag combination");
2061			goto out;
2062		}
2063
2064		err = pneigh_create(tbl, net, dst, dev, ndm_flags, protocol,
2065				    !!(ndm->ndm_state & NUD_PERMANENT));
2066		goto out;
2067	}
2068
2069	if (!dev) {
2070		NL_SET_ERR_MSG(extack, "Device not specified");
2071		goto out;
2072	}
2073
2074	if (tbl->allow_add && !tbl->allow_add(dev, extack)) {
2075		err = -EINVAL;
2076		goto out;
2077	}
2078
2079	neigh = neigh_lookup(tbl, dst, dev);
2080	if (neigh == NULL) {
2081		bool ndm_permanent  = ndm->ndm_state & NUD_PERMANENT;
2082		bool exempt_from_gc = ndm_permanent ||
2083				      ndm_flags & (NTF_EXT_LEARNED |
2084						   NTF_EXT_VALIDATED);
2085
2086		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2087			err = -ENOENT;
2088			goto out;
2089		}
2090		if (ndm_permanent && (ndm_flags & NTF_MANAGED)) {
2091			NL_SET_ERR_MSG(extack, "Invalid NTF_* flag for permanent entry");
2092			err = -EINVAL;
2093			goto out;
2094		}
2095		if (ndm_flags & NTF_EXT_VALIDATED) {
2096			u8 state = ndm->ndm_state;
2097
2098			/* NTF_USE and NTF_MANAGED will result in the neighbor
2099			 * being created with an invalid state (NUD_NONE).
2100			 */
2101			if (ndm_flags & (NTF_USE | NTF_MANAGED))
2102				state = NUD_NONE;
2103
2104			if (!(state & NUD_VALID)) {
2105				NL_SET_ERR_MSG(extack,
2106					       "Cannot create externally validated neighbor with an invalid state");
2107				err = -EINVAL;
2108				goto out;
2109			}
2110		}
2111
2112		neigh = ___neigh_create(tbl, dst, dev,
2113					ndm_flags &
2114					(NTF_EXT_LEARNED | NTF_MANAGED |
2115					 NTF_EXT_VALIDATED),
2116					exempt_from_gc, true);
2117		if (IS_ERR(neigh)) {
2118			err = PTR_ERR(neigh);
2119			goto out;
2120		}
2121	} else {
2122		if (nlh->nlmsg_flags & NLM_F_EXCL) {
2123			err = -EEXIST;
2124			neigh_release(neigh);
2125			goto out;
2126		}
2127		if (ndm_flags & NTF_EXT_VALIDATED) {
2128			u8 state = ndm->ndm_state;
2129
2130			/* NTF_USE and NTF_MANAGED do not update the existing
2131			 * state other than clearing it if it was
2132			 * NUD_PERMANENT.
2133			 */
2134			if (ndm_flags & (NTF_USE | NTF_MANAGED))
2135				state = READ_ONCE(neigh->nud_state) & ~NUD_PERMANENT;
2136
2137			if (!(state & NUD_VALID)) {
2138				NL_SET_ERR_MSG(extack,
2139					       "Cannot mark neighbor as externally validated with an invalid state");
2140				err = -EINVAL;
2141				neigh_release(neigh);
2142				goto out;
2143			}
2144		}
2145
2146		if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
2147			flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
2148				   NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
2149	}
2150
2151	if (protocol)
2152		neigh->protocol = protocol;
2153	if (ndm_flags & NTF_EXT_LEARNED)
2154		flags |= NEIGH_UPDATE_F_EXT_LEARNED;
2155	if (ndm_flags & NTF_ROUTER)
2156		flags |= NEIGH_UPDATE_F_ISROUTER;
2157	if (ndm_flags & NTF_MANAGED)
2158		flags |= NEIGH_UPDATE_F_MANAGED;
2159	if (ndm_flags & NTF_USE)
2160		flags |= NEIGH_UPDATE_F_USE;
2161	if (ndm_flags & NTF_EXT_VALIDATED)
2162		flags |= NEIGH_UPDATE_F_EXT_VALIDATED;
2163
2164	err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags,
2165			     NETLINK_CB(skb).portid, extack);
2166	if (!err && ndm_flags & (NTF_USE | NTF_MANAGED))
2167		neigh_event_send(neigh, NULL);
2168	neigh_release(neigh);
2169out:
2170	return err;
2171}
2172
2173static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
2174{
2175	struct nlattr *nest;
2176
2177	nest = nla_nest_start_noflag(skb, NDTA_PARMS);
2178	if (nest == NULL)
2179		return -ENOBUFS;
2180
2181	if ((parms->dev &&
2182	     nla_put_u32(skb, NDTPA_IFINDEX, READ_ONCE(parms->dev->ifindex))) ||
2183	    nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
2184	    nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
2185			NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
2186	    /* approximative value for deprecated QUEUE_LEN (in packets) */
2187	    nla_put_u32(skb, NDTPA_QUEUE_LEN,
2188			NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
2189	    nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
2190	    nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
2191	    nla_put_u32(skb, NDTPA_UCAST_PROBES,
2192			NEIGH_VAR(parms, UCAST_PROBES)) ||
2193	    nla_put_u32(skb, NDTPA_MCAST_PROBES,
2194			NEIGH_VAR(parms, MCAST_PROBES)) ||
2195	    nla_put_u32(skb, NDTPA_MCAST_REPROBES,
2196			NEIGH_VAR(parms, MCAST_REPROBES)) ||
2197	    nla_put_msecs(skb, NDTPA_REACHABLE_TIME, READ_ONCE(parms->reachable_time),
2198			  NDTPA_PAD) ||
2199	    nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
2200			  NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
2201	    nla_put_msecs(skb, NDTPA_GC_STALETIME,
2202			  NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
2203	    nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
2204			  NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
2205	    nla_put_msecs(skb, NDTPA_RETRANS_TIME,
2206			  NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
2207	    nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
2208			  NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
2209	    nla_put_msecs(skb, NDTPA_PROXY_DELAY,
2210			  NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
2211	    nla_put_msecs(skb, NDTPA_LOCKTIME,
2212			  NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD) ||
2213	    nla_put_msecs(skb, NDTPA_INTERVAL_PROBE_TIME_MS,
2214			  NEIGH_VAR(parms, INTERVAL_PROBE_TIME_MS), NDTPA_PAD))
2215		goto nla_put_failure;
2216	return nla_nest_end(skb, nest);
2217
2218nla_put_failure:
2219	nla_nest_cancel(skb, nest);
2220	return -EMSGSIZE;
2221}
2222
2223static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
2224			      u32 pid, u32 seq, int type, int flags)
2225{
2226	struct nlmsghdr *nlh;
2227	struct ndtmsg *ndtmsg;
2228
2229	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2230	if (nlh == NULL)
2231		return -EMSGSIZE;
2232
2233	ndtmsg = nlmsg_data(nlh);
2234	ndtmsg->ndtm_family = tbl->family;
2235	ndtmsg->ndtm_pad1   = 0;
2236	ndtmsg->ndtm_pad2   = 0;
2237
2238	if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
2239	    nla_put_msecs(skb, NDTA_GC_INTERVAL, READ_ONCE(tbl->gc_interval),
2240			  NDTA_PAD) ||
2241	    nla_put_u32(skb, NDTA_THRESH1, READ_ONCE(tbl->gc_thresh1)) ||
2242	    nla_put_u32(skb, NDTA_THRESH2, READ_ONCE(tbl->gc_thresh2)) ||
2243	    nla_put_u32(skb, NDTA_THRESH3, READ_ONCE(tbl->gc_thresh3)))
2244		goto nla_put_failure;
2245	{
2246		unsigned long now = jiffies;
2247		long flush_delta = now - READ_ONCE(tbl->last_flush);
2248		long rand_delta = now - READ_ONCE(tbl->last_rand);
2249		struct neigh_hash_table *nht;
2250		struct ndt_config ndc = {
2251			.ndtc_key_len		= tbl->key_len,
2252			.ndtc_entry_size	= tbl->entry_size,
2253			.ndtc_entries		= atomic_read(&tbl->entries),
2254			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
2255			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
2256			.ndtc_proxy_qlen	= READ_ONCE(tbl->proxy_queue.qlen),
2257		};
2258
2259		nht = rcu_dereference(tbl->nht);
2260		ndc.ndtc_hash_rnd = nht->hash_rnd[0];
2261		ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
2262
2263		if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
2264			goto nla_put_failure;
2265	}
2266
2267	{
2268		int cpu;
2269		struct ndt_stats ndst;
2270
2271		memset(&ndst, 0, sizeof(ndst));
2272
2273		for_each_possible_cpu(cpu) {
2274			struct neigh_statistics	*st;
2275
2276			st = per_cpu_ptr(tbl->stats, cpu);
2277			ndst.ndts_allocs		+= READ_ONCE(st->allocs);
2278			ndst.ndts_destroys		+= READ_ONCE(st->destroys);
2279			ndst.ndts_hash_grows		+= READ_ONCE(st->hash_grows);
2280			ndst.ndts_res_failed		+= READ_ONCE(st->res_failed);
2281			ndst.ndts_lookups		+= READ_ONCE(st->lookups);
2282			ndst.ndts_hits			+= READ_ONCE(st->hits);
2283			ndst.ndts_rcv_probes_mcast	+= READ_ONCE(st->rcv_probes_mcast);
2284			ndst.ndts_rcv_probes_ucast	+= READ_ONCE(st->rcv_probes_ucast);
2285			ndst.ndts_periodic_gc_runs	+= READ_ONCE(st->periodic_gc_runs);
2286			ndst.ndts_forced_gc_runs	+= READ_ONCE(st->forced_gc_runs);
2287			ndst.ndts_table_fulls		+= READ_ONCE(st->table_fulls);
2288		}
2289
2290		if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
2291				  NDTA_PAD))
2292			goto nla_put_failure;
2293	}
2294
2295	BUG_ON(tbl->parms.dev);
2296	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
2297		goto nla_put_failure;
2298
2299	nlmsg_end(skb, nlh);
2300	return 0;
2301
2302nla_put_failure:
2303	nlmsg_cancel(skb, nlh);
2304	return -EMSGSIZE;
2305}
2306
2307static int neightbl_fill_param_info(struct sk_buff *skb,
2308				    struct neigh_table *tbl,
2309				    struct neigh_parms *parms,
2310				    u32 pid, u32 seq, int type,
2311				    unsigned int flags)
2312{
2313	struct ndtmsg *ndtmsg;
2314	struct nlmsghdr *nlh;
2315
2316	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2317	if (nlh == NULL)
2318		return -EMSGSIZE;
2319
2320	ndtmsg = nlmsg_data(nlh);
2321	ndtmsg->ndtm_family = tbl->family;
2322	ndtmsg->ndtm_pad1   = 0;
2323	ndtmsg->ndtm_pad2   = 0;
2324
2325	if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
2326	    neightbl_fill_parms(skb, parms) < 0)
2327		goto errout;
2328
2329	nlmsg_end(skb, nlh);
2330	return 0;
2331errout:
2332	nlmsg_cancel(skb, nlh);
2333	return -EMSGSIZE;
2334}
2335
2336static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
2337	[NDTA_NAME]		= { .type = NLA_STRING },
2338	[NDTA_THRESH1]		= { .type = NLA_U32 },
2339	[NDTA_THRESH2]		= { .type = NLA_U32 },
2340	[NDTA_THRESH3]		= { .type = NLA_U32 },
2341	[NDTA_GC_INTERVAL]	= { .type = NLA_U64 },
2342	[NDTA_PARMS]		= { .type = NLA_NESTED },
2343};
2344
2345static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
2346	[NDTPA_IFINDEX]			= { .type = NLA_U32 },
2347	[NDTPA_QUEUE_LEN]		= { .type = NLA_U32 },
2348	[NDTPA_QUEUE_LENBYTES]		= { .type = NLA_U32 },
2349	[NDTPA_PROXY_QLEN]		= { .type = NLA_U32 },
2350	[NDTPA_APP_PROBES]		= { .type = NLA_U32 },
2351	[NDTPA_UCAST_PROBES]		= { .type = NLA_U32 },
2352	[NDTPA_MCAST_PROBES]		= { .type = NLA_U32 },
2353	[NDTPA_MCAST_REPROBES]		= { .type = NLA_U32 },
2354	[NDTPA_BASE_REACHABLE_TIME]	= { .type = NLA_U64 },
2355	[NDTPA_GC_STALETIME]		= { .type = NLA_U64 },
2356	[NDTPA_DELAY_PROBE_TIME]	= { .type = NLA_U64 },
2357	[NDTPA_RETRANS_TIME]		= { .type = NLA_U64 },
2358	[NDTPA_ANYCAST_DELAY]		= { .type = NLA_U64 },
2359	[NDTPA_PROXY_DELAY]		= { .type = NLA_U64 },
2360	[NDTPA_LOCKTIME]		= { .type = NLA_U64 },
2361	[NDTPA_INTERVAL_PROBE_TIME_MS]	= { .type = NLA_U64, .min = 1 },
2362};
2363
2364static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
2365			struct netlink_ext_ack *extack)
2366{
2367	struct net *net = sock_net(skb->sk);
2368	struct nlattr *tb[NDTA_MAX + 1];
2369	struct neigh_table *tbl;
2370	struct ndtmsg *ndtmsg;
2371	bool found = false;
2372	int err, tidx;
2373
2374	err = nlmsg_parse_deprecated(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
2375				     nl_neightbl_policy, extack);
2376	if (err < 0)
2377		goto errout;
2378
2379	if (tb[NDTA_NAME] == NULL) {
2380		err = -EINVAL;
2381		goto errout;
2382	}
2383
2384	ndtmsg = nlmsg_data(nlh);
2385
2386	rcu_read_lock();
2387
2388	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2389		tbl = rcu_dereference(neigh_tables[tidx]);
2390		if (!tbl)
2391			continue;
2392
2393		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2394			continue;
2395
2396		if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2397			found = true;
2398			break;
2399		}
2400	}
2401
2402	if (!found) {
2403		rcu_read_unlock();
2404		err = -ENOENT;
2405		goto errout;
2406	}
2407
2408	/*
2409	 * We acquire tbl->lock to be nice to the periodic timers and
2410	 * make sure they always see a consistent set of values.
2411	 */
2412	spin_lock_bh(&tbl->lock);
2413
2414	if (tb[NDTA_PARMS]) {
2415		struct nlattr *tbp[NDTPA_MAX+1];
2416		struct neigh_parms *p;
2417		int i, ifindex = 0;
2418
2419		err = nla_parse_nested_deprecated(tbp, NDTPA_MAX,
2420						  tb[NDTA_PARMS],
2421						  nl_ntbl_parm_policy, extack);
2422		if (err < 0)
2423			goto errout_tbl_lock;
2424
2425		if (tbp[NDTPA_IFINDEX])
2426			ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2427
2428		p = lookup_neigh_parms(tbl, net, ifindex);
2429		if (p == NULL) {
2430			err = -ENOENT;
2431			goto errout_tbl_lock;
2432		}
2433
2434		for (i = 1; i <= NDTPA_MAX; i++) {
2435			if (tbp[i] == NULL)
2436				continue;
2437
2438			switch (i) {
2439			case NDTPA_QUEUE_LEN:
2440				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2441					      nla_get_u32(tbp[i]) *
2442					      SKB_TRUESIZE(ETH_FRAME_LEN));
2443				break;
2444			case NDTPA_QUEUE_LENBYTES:
2445				NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2446					      nla_get_u32(tbp[i]));
2447				break;
2448			case NDTPA_PROXY_QLEN:
2449				NEIGH_VAR_SET(p, PROXY_QLEN,
2450					      nla_get_u32(tbp[i]));
2451				break;
2452			case NDTPA_APP_PROBES:
2453				NEIGH_VAR_SET(p, APP_PROBES,
2454					      nla_get_u32(tbp[i]));
2455				break;
2456			case NDTPA_UCAST_PROBES:
2457				NEIGH_VAR_SET(p, UCAST_PROBES,
2458					      nla_get_u32(tbp[i]));
2459				break;
2460			case NDTPA_MCAST_PROBES:
2461				NEIGH_VAR_SET(p, MCAST_PROBES,
2462					      nla_get_u32(tbp[i]));
2463				break;
2464			case NDTPA_MCAST_REPROBES:
2465				NEIGH_VAR_SET(p, MCAST_REPROBES,
2466					      nla_get_u32(tbp[i]));
2467				break;
2468			case NDTPA_BASE_REACHABLE_TIME:
2469				NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2470					      nla_get_msecs(tbp[i]));
2471				/* update reachable_time as well, otherwise, the change will
2472				 * only be effective after the next time neigh_periodic_work
2473				 * decides to recompute it (can be multiple minutes)
2474				 */
2475				neigh_set_reach_time(p);
2476				break;
2477			case NDTPA_GC_STALETIME:
2478				NEIGH_VAR_SET(p, GC_STALETIME,
2479					      nla_get_msecs(tbp[i]));
2480				break;
2481			case NDTPA_DELAY_PROBE_TIME:
2482				NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
2483					      nla_get_msecs(tbp[i]));
2484				call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
2485				break;
2486			case NDTPA_INTERVAL_PROBE_TIME_MS:
2487				NEIGH_VAR_SET(p, INTERVAL_PROBE_TIME_MS,
2488					      nla_get_msecs(tbp[i]));
2489				break;
2490			case NDTPA_RETRANS_TIME:
2491				NEIGH_VAR_SET(p, RETRANS_TIME,
2492					      nla_get_msecs(tbp[i]));
2493				break;
2494			case NDTPA_ANYCAST_DELAY:
2495				NEIGH_VAR_SET(p, ANYCAST_DELAY,
2496					      nla_get_msecs(tbp[i]));
2497				break;
2498			case NDTPA_PROXY_DELAY:
2499				NEIGH_VAR_SET(p, PROXY_DELAY,
2500					      nla_get_msecs(tbp[i]));
2501				break;
2502			case NDTPA_LOCKTIME:
2503				NEIGH_VAR_SET(p, LOCKTIME,
2504					      nla_get_msecs(tbp[i]));
2505				break;
2506			}
2507		}
2508	}
2509
2510	err = -ENOENT;
2511	if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2512	     tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2513	    !net_eq(net, &init_net))
2514		goto errout_tbl_lock;
2515
2516	if (tb[NDTA_THRESH1])
2517		WRITE_ONCE(tbl->gc_thresh1, nla_get_u32(tb[NDTA_THRESH1]));
2518
2519	if (tb[NDTA_THRESH2])
2520		WRITE_ONCE(tbl->gc_thresh2, nla_get_u32(tb[NDTA_THRESH2]));
2521
2522	if (tb[NDTA_THRESH3])
2523		WRITE_ONCE(tbl->gc_thresh3, nla_get_u32(tb[NDTA_THRESH3]));
2524
2525	if (tb[NDTA_GC_INTERVAL])
2526		WRITE_ONCE(tbl->gc_interval, nla_get_msecs(tb[NDTA_GC_INTERVAL]));
2527
2528	err = 0;
2529
2530errout_tbl_lock:
2531	spin_unlock_bh(&tbl->lock);
2532	rcu_read_unlock();
2533errout:
2534	return err;
2535}
2536
2537static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
2538				    struct netlink_ext_ack *extack)
2539{
2540	struct ndtmsg *ndtm;
2541
2542	ndtm = nlmsg_payload(nlh, sizeof(*ndtm));
2543	if (!ndtm) {
2544		NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
2545		return -EINVAL;
2546	}
2547
2548	if (ndtm->ndtm_pad1  || ndtm->ndtm_pad2) {
2549		NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
2550		return -EINVAL;
2551	}
2552
2553	if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
2554		NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
2555		return -EINVAL;
2556	}
2557
2558	return 0;
2559}
2560
2561static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2562{
2563	const struct nlmsghdr *nlh = cb->nlh;
2564	struct net *net = sock_net(skb->sk);
2565	int family, tidx, nidx = 0;
2566	int tbl_skip = cb->args[0];
2567	int neigh_skip = cb->args[1];
2568	struct neigh_table *tbl;
2569
2570	if (cb->strict_check) {
2571		int err = neightbl_valid_dump_info(nlh, cb->extack);
2572
2573		if (err < 0)
2574			return err;
2575	}
2576
2577	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
2578
2579	rcu_read_lock();
2580
2581	for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2582		struct neigh_parms *p;
2583
2584		tbl = rcu_dereference(neigh_tables[tidx]);
2585		if (!tbl)
2586			continue;
2587
2588		if (tidx < tbl_skip || (family && tbl->family != family))
2589			continue;
2590
2591		if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2592				       nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2593				       NLM_F_MULTI) < 0)
2594			break;
2595
2596		nidx = 0;
2597		p = list_next_entry(&tbl->parms, list);
2598		list_for_each_entry_from_rcu(p, &tbl->parms_list, list) {
2599			if (!net_eq(neigh_parms_net(p), net))
2600				continue;
2601
2602			if (nidx < neigh_skip)
2603				goto next;
2604
2605			if (neightbl_fill_param_info(skb, tbl, p,
2606						     NETLINK_CB(cb->skb).portid,
2607						     nlh->nlmsg_seq,
2608						     RTM_NEWNEIGHTBL,
2609						     NLM_F_MULTI) < 0)
2610				goto out;
2611		next:
2612			nidx++;
2613		}
2614
2615		neigh_skip = 0;
2616	}
2617out:
2618	rcu_read_unlock();
2619
2620	cb->args[0] = tidx;
2621	cb->args[1] = nidx;
2622
2623	return skb->len;
2624}
2625
2626static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2627			   u32 pid, u32 seq, int type, unsigned int flags)
2628{
2629	u32 neigh_flags, neigh_flags_ext;
2630	unsigned long now = jiffies;
2631	struct nda_cacheinfo ci;
2632	struct nlmsghdr *nlh;
2633	struct ndmsg *ndm;
2634
2635	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2636	if (nlh == NULL)
2637		return -EMSGSIZE;
2638
2639	neigh_flags_ext = neigh->flags >> NTF_EXT_SHIFT;
2640	neigh_flags     = neigh->flags & NTF_OLD_MASK;
2641
2642	ndm = nlmsg_data(nlh);
2643	ndm->ndm_family	 = neigh->ops->family;
2644	ndm->ndm_pad1    = 0;
2645	ndm->ndm_pad2    = 0;
2646	ndm->ndm_flags	 = neigh_flags;
2647	ndm->ndm_type	 = neigh->type;
2648	ndm->ndm_ifindex = neigh->dev->ifindex;
2649
2650	if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
2651		goto nla_put_failure;
2652
2653	read_lock_bh(&neigh->lock);
2654	ndm->ndm_state	 = neigh->nud_state;
2655	if (neigh->nud_state & NUD_VALID) {
2656		char haddr[MAX_ADDR_LEN];
2657
2658		neigh_ha_snapshot(haddr, neigh, neigh->dev);
2659		if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2660			read_unlock_bh(&neigh->lock);
2661			goto nla_put_failure;
2662		}
2663	}
2664
2665	ci.ndm_used	 = jiffies_to_clock_t(now - neigh->used);
2666	ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2667	ci.ndm_updated	 = jiffies_to_clock_t(now - neigh->updated);
2668	ci.ndm_refcnt	 = refcount_read(&neigh->refcnt) - 1;
2669	read_unlock_bh(&neigh->lock);
2670
2671	if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
2672	    nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
2673		goto nla_put_failure;
2674
2675	if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol))
2676		goto nla_put_failure;
2677	if (neigh_flags_ext && nla_put_u32(skb, NDA_FLAGS_EXT, neigh_flags_ext))
2678		goto nla_put_failure;
2679
2680	nlmsg_end(skb, nlh);
2681	return 0;
2682
2683nla_put_failure:
2684	nlmsg_cancel(skb, nlh);
2685	return -EMSGSIZE;
2686}
2687
2688static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
2689			    u32 pid, u32 seq, int type, unsigned int flags,
2690			    struct neigh_table *tbl)
2691{
2692	u32 neigh_flags, neigh_flags_ext;
2693	struct nlmsghdr *nlh;
2694	struct ndmsg *ndm;
2695	u8 protocol;
2696
2697	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2698	if (nlh == NULL)
2699		return -EMSGSIZE;
2700
2701	neigh_flags = READ_ONCE(pn->flags);
2702	neigh_flags_ext = neigh_flags >> NTF_EXT_SHIFT;
2703	neigh_flags &= NTF_OLD_MASK;
2704
2705	ndm = nlmsg_data(nlh);
2706	ndm->ndm_family	 = tbl->family;
2707	ndm->ndm_pad1    = 0;
2708	ndm->ndm_pad2    = 0;
2709	ndm->ndm_flags	 = neigh_flags | NTF_PROXY;
2710	ndm->ndm_type	 = RTN_UNICAST;
2711	ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
2712	ndm->ndm_state	 = NUD_NONE;
2713
2714	if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
2715		goto nla_put_failure;
2716
2717	protocol = READ_ONCE(pn->protocol);
2718	if (protocol && nla_put_u8(skb, NDA_PROTOCOL, protocol))
2719		goto nla_put_failure;
2720	if (neigh_flags_ext && nla_put_u32(skb, NDA_FLAGS_EXT, neigh_flags_ext))
2721		goto nla_put_failure;
2722
2723	nlmsg_end(skb, nlh);
2724	return 0;
2725
2726nla_put_failure:
2727	nlmsg_cancel(skb, nlh);
2728	return -EMSGSIZE;
2729}
2730
2731static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
2732{
2733	call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2734	__neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
2735}
2736
2737static bool neigh_master_filtered(struct net_device *dev, int master_idx)
2738{
2739	struct net_device *master;
2740
2741	if (!master_idx)
2742		return false;
2743
2744	master = dev ? netdev_master_upper_dev_get_rcu(dev) : NULL;
2745
2746	/* 0 is already used to denote NDA_MASTER wasn't passed, therefore need another
2747	 * invalid value for ifindex to denote "no master".
2748	 */
2749	if (master_idx == -1)
2750		return !!master;
2751
2752	if (!master || master->ifindex != master_idx)
2753		return true;
2754
2755	return false;
2756}
2757
2758static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
2759{
2760	if (filter_idx && (!dev || dev->ifindex != filter_idx))
2761		return true;
2762
2763	return false;
2764}
2765
2766struct neigh_dump_filter {
2767	int master_idx;
2768	int dev_idx;
2769};
2770
2771static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2772			    struct netlink_callback *cb,
2773			    struct neigh_dump_filter *filter)
2774{
2775	struct net *net = sock_net(skb->sk);
2776	struct neighbour *n;
2777	int err = 0, h, s_h = cb->args[1];
2778	int idx, s_idx = idx = cb->args[2];
2779	struct neigh_hash_table *nht;
2780	unsigned int flags = NLM_F_MULTI;
2781
2782	if (filter->dev_idx || filter->master_idx)
2783		flags |= NLM_F_DUMP_FILTERED;
2784
2785	nht = rcu_dereference(tbl->nht);
2786
2787	for (h = s_h; h < (1 << nht->hash_shift); h++) {
2788		if (h > s_h)
2789			s_idx = 0;
2790		idx = 0;
2791		neigh_for_each_in_bucket_rcu(n, &nht->hash_heads[h]) {
2792			if (idx < s_idx || !net_eq(dev_net(n->dev), net))
2793				goto next;
2794			if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2795			    neigh_master_filtered(n->dev, filter->master_idx))
2796				goto next;
2797			err = neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2798					      cb->nlh->nlmsg_seq,
2799					      RTM_NEWNEIGH, flags);
2800			if (err < 0)
2801				goto out;
2802next:
2803			idx++;
2804		}
2805	}
2806out:
2807	cb->args[1] = h;
2808	cb->args[2] = idx;
2809	return err;
2810}
2811
2812static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2813			     struct netlink_callback *cb,
2814			     struct neigh_dump_filter *filter)
2815{
2816	struct pneigh_entry *n;
2817	struct net *net = sock_net(skb->sk);
2818	int err = 0, h, s_h = cb->args[3];
2819	int idx, s_idx = idx = cb->args[4];
2820	unsigned int flags = NLM_F_MULTI;
2821
2822	if (filter->dev_idx || filter->master_idx)
2823		flags |= NLM_F_DUMP_FILTERED;
2824
2825	for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
2826		if (h > s_h)
2827			s_idx = 0;
2828		for (n = rcu_dereference(tbl->phash_buckets[h]), idx = 0;
2829		     n;
2830		     n = rcu_dereference(n->next)) {
2831			if (idx < s_idx || pneigh_net(n) != net)
2832				goto next;
2833			if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2834			    neigh_master_filtered(n->dev, filter->master_idx))
2835				goto next;
2836			err = pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2837					       cb->nlh->nlmsg_seq,
2838					       RTM_NEWNEIGH, flags, tbl);
2839			if (err < 0)
2840				goto out;
2841		next:
2842			idx++;
2843		}
2844	}
2845
2846out:
2847	cb->args[3] = h;
2848	cb->args[4] = idx;
2849	return err;
2850}
2851
2852static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
2853				bool strict_check,
2854				struct neigh_dump_filter *filter,
2855				struct netlink_ext_ack *extack)
2856{
2857	struct nlattr *tb[NDA_MAX + 1];
2858	int err, i;
2859
2860	if (strict_check) {
2861		struct ndmsg *ndm;
2862
2863		ndm = nlmsg_payload(nlh, sizeof(*ndm));
2864		if (!ndm) {
2865			NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
2866			return -EINVAL;
2867		}
2868
2869		if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_ifindex ||
2870		    ndm->ndm_state || ndm->ndm_type) {
2871			NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
2872			return -EINVAL;
2873		}
2874
2875		if (ndm->ndm_flags & ~NTF_PROXY) {
2876			NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor dump request");
2877			return -EINVAL;
2878		}
2879
2880		err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg),
2881						    tb, NDA_MAX, nda_policy,
2882						    extack);
2883	} else {
2884		err = nlmsg_parse_deprecated(nlh, sizeof(struct ndmsg), tb,
2885					     NDA_MAX, nda_policy, extack);
2886	}
2887	if (err < 0)
2888		return err;
2889
2890	for (i = 0; i <= NDA_MAX; ++i) {
2891		if (!tb[i])
2892			continue;
2893
2894		/* all new attributes should require strict_check */
2895		switch (i) {
2896		case NDA_IFINDEX:
2897			filter->dev_idx = nla_get_u32(tb[i]);
2898			break;
2899		case NDA_MASTER:
2900			filter->master_idx = nla_get_u32(tb[i]);
2901			break;
2902		default:
2903			if (strict_check) {
2904				NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
2905				return -EINVAL;
2906			}
2907		}
2908	}
2909
2910	return 0;
2911}
2912
2913static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2914{
2915	const struct nlmsghdr *nlh = cb->nlh;
2916	struct neigh_dump_filter filter = {};
2917	struct neigh_table *tbl;
2918	int t, family, s_t;
2919	int proxy = 0;
2920	int err;
2921
2922	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
2923
2924	/* check for full ndmsg structure presence, family member is
2925	 * the same for both structures
2926	 */
2927	if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
2928	    ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
2929		proxy = 1;
2930
2931	err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
2932	if (err < 0 && cb->strict_check)
2933		return err;
2934	err = 0;
2935
2936	s_t = cb->args[0];
2937
2938	rcu_read_lock();
2939	for (t = 0; t < NEIGH_NR_TABLES; t++) {
2940		tbl = rcu_dereference(neigh_tables[t]);
2941
2942		if (!tbl)
2943			continue;
2944		if (t < s_t || (family && tbl->family != family))
2945			continue;
2946		if (t > s_t)
2947			memset(&cb->args[1], 0, sizeof(cb->args) -
2948						sizeof(cb->args[0]));
2949		if (proxy)
2950			err = pneigh_dump_table(tbl, skb, cb, &filter);
2951		else
2952			err = neigh_dump_table(tbl, skb, cb, &filter);
2953		if (err < 0)
2954			break;
2955	}
2956	rcu_read_unlock();
2957
2958	cb->args[0] = t;
2959	return err;
2960}
2961
2962static struct ndmsg *neigh_valid_get_req(const struct nlmsghdr *nlh,
2963					 struct nlattr **tb,
2964					 struct netlink_ext_ack *extack)
2965{
2966	struct ndmsg *ndm;
2967	int err, i;
2968
2969	ndm = nlmsg_payload(nlh, sizeof(*ndm));
2970	if (!ndm) {
2971		NL_SET_ERR_MSG(extack, "Invalid header for neighbor get request");
2972		return ERR_PTR(-EINVAL);
2973	}
2974
2975	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
2976	    ndm->ndm_type) {
2977		NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor get request");
2978		return ERR_PTR(-EINVAL);
2979	}
2980
2981	if (ndm->ndm_flags & ~NTF_PROXY) {
2982		NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor get request");
2983		return ERR_PTR(-EINVAL);
2984	}
2985
2986	if (!(ndm->ndm_flags & NTF_PROXY) && !ndm->ndm_ifindex) {
2987		NL_SET_ERR_MSG(extack, "No device specified");
2988		return ERR_PTR(-EINVAL);
2989	}
2990
2991	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
2992					    NDA_MAX, nda_policy, extack);
2993	if (err < 0)
2994		return ERR_PTR(err);
2995
2996	for (i = 0; i <= NDA_MAX; ++i) {
2997		switch (i) {
2998		case NDA_DST:
2999			if (!tb[i]) {
3000				NL_SET_ERR_ATTR_MISS(extack, NULL, NDA_DST);
3001				return ERR_PTR(-EINVAL);
3002			}
3003			break;
3004		default:
3005			if (!tb[i])
3006				continue;
3007
3008			NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor get request");
3009			return ERR_PTR(-EINVAL);
3010		}
3011	}
3012
3013	return ndm;
3014}
3015
3016static inline size_t neigh_nlmsg_size(void)
3017{
3018	return NLMSG_ALIGN(sizeof(struct ndmsg))
3019	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
3020	       + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
3021	       + nla_total_size(sizeof(struct nda_cacheinfo))
3022	       + nla_total_size(4)  /* NDA_PROBES */
3023	       + nla_total_size(4)  /* NDA_FLAGS_EXT */
3024	       + nla_total_size(1); /* NDA_PROTOCOL */
3025}
3026
3027static inline size_t pneigh_nlmsg_size(void)
3028{
3029	return NLMSG_ALIGN(sizeof(struct ndmsg))
3030	       + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
3031	       + nla_total_size(4)  /* NDA_FLAGS_EXT */
3032	       + nla_total_size(1); /* NDA_PROTOCOL */
3033}
3034
3035static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
3036		     struct netlink_ext_ack *extack)
3037{
3038	struct net *net = sock_net(in_skb->sk);
3039	u32 pid = NETLINK_CB(in_skb).portid;
3040	struct nlattr *tb[NDA_MAX + 1];
3041	struct net_device *dev = NULL;
3042	u32 seq = nlh->nlmsg_seq;
3043	struct neigh_table *tbl;
3044	struct neighbour *neigh;
3045	struct sk_buff *skb;
3046	struct ndmsg *ndm;
3047	void *dst;
3048	int err;
3049
3050	ndm = neigh_valid_get_req(nlh, tb, extack);
3051	if (IS_ERR(ndm))
3052		return PTR_ERR(ndm);
3053
3054	if (ndm->ndm_flags & NTF_PROXY)
3055		skb = nlmsg_new(neigh_nlmsg_size(), GFP_KERNEL);
3056	else
3057		skb = nlmsg_new(pneigh_nlmsg_size(), GFP_KERNEL);
3058	if (!skb)
3059		return -ENOBUFS;
3060
3061	rcu_read_lock();
3062
3063	tbl = neigh_find_table(ndm->ndm_family);
3064	if (!tbl) {
3065		NL_SET_ERR_MSG(extack, "Unsupported family in header for neighbor get request");
3066		err = -EAFNOSUPPORT;
3067		goto err_unlock;
3068	}
3069
3070	if (nla_len(tb[NDA_DST]) != (int)tbl->key_len) {
3071		NL_SET_ERR_MSG(extack, "Invalid network address in neighbor get request");
3072		err = -EINVAL;
3073		goto err_unlock;
3074	}
3075
3076	dst = nla_data(tb[NDA_DST]);
3077
3078	if (ndm->ndm_ifindex) {
3079		dev = dev_get_by_index_rcu(net, ndm->ndm_ifindex);
3080		if (!dev) {
3081			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
3082			err = -ENODEV;
3083			goto err_unlock;
3084		}
3085	}
3086
3087	if (ndm->ndm_flags & NTF_PROXY) {
3088		struct pneigh_entry *pn;
3089
3090		pn = pneigh_lookup(tbl, net, dst, dev);
3091		if (!pn) {
3092			NL_SET_ERR_MSG(extack, "Proxy neighbour entry not found");
3093			err = -ENOENT;
3094			goto err_unlock;
3095		}
3096
3097		err = pneigh_fill_info(skb, pn, pid, seq, RTM_NEWNEIGH, 0, tbl);
3098		if (err)
3099			goto err_unlock;
3100	} else {
3101		neigh = neigh_lookup(tbl, dst, dev);
3102		if (!neigh) {
3103			NL_SET_ERR_MSG(extack, "Neighbour entry not found");
3104			err = -ENOENT;
3105			goto err_unlock;
3106		}
3107
3108		err = neigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0);
3109		neigh_release(neigh);
3110		if (err)
3111			goto err_unlock;
3112	}
3113
3114	rcu_read_unlock();
3115
3116	return rtnl_unicast(skb, net, pid);
3117err_unlock:
3118	rcu_read_unlock();
3119	kfree_skb(skb);
3120	return err;
3121}
3122
3123void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
3124{
3125	int chain;
3126	struct neigh_hash_table *nht;
3127
3128	rcu_read_lock();
3129	nht = rcu_dereference(tbl->nht);
3130
3131	spin_lock_bh(&tbl->lock); /* avoid resizes */
3132	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
3133		struct neighbour *n;
3134
3135		neigh_for_each_in_bucket(n, &nht->hash_heads[chain])
3136			cb(n, cookie);
3137	}
3138	spin_unlock_bh(&tbl->lock);
3139	rcu_read_unlock();
3140}
3141EXPORT_SYMBOL(neigh_for_each);
3142
3143/* The tbl->lock must be held as a writer and BH disabled. */
3144void __neigh_for_each_release(struct neigh_table *tbl,
3145			      int (*cb)(struct neighbour *))
3146{
3147	struct neigh_hash_table *nht;
3148	int chain;
3149
3150	nht = rcu_dereference_protected(tbl->nht,
3151					lockdep_is_held(&tbl->lock));
3152	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
3153		struct hlist_node *tmp;
3154		struct neighbour *n;
3155
3156		neigh_for_each_in_bucket_safe(n, tmp, &nht->hash_heads[chain]) {
3157			int release;
3158
3159			write_lock(&n->lock);
3160			release = cb(n);
3161			if (release) {
3162				hlist_del_rcu(&n->hash);
3163				hlist_del_rcu(&n->dev_list);
3164				neigh_mark_dead(n);
3165			}
3166			write_unlock(&n->lock);
3167			if (release)
3168				neigh_cleanup_and_release(n);
3169		}
3170	}
3171}
3172EXPORT_SYMBOL(__neigh_for_each_release);
3173
3174int neigh_xmit(int index, struct net_device *dev,
3175	       const void *addr, struct sk_buff *skb)
3176{
3177	int err = -EAFNOSUPPORT;
3178
3179	if (likely(index < NEIGH_NR_TABLES)) {
3180		struct neigh_table *tbl;
3181		struct neighbour *neigh;
3182
3183		rcu_read_lock();
3184		tbl = rcu_dereference(neigh_tables[index]);
3185		if (!tbl)
3186			goto out_unlock;
3187		if (index == NEIGH_ARP_TABLE) {
3188			u32 key = *((u32 *)addr);
3189
3190			neigh = __ipv4_neigh_lookup_noref(dev, key);
3191		} else {
3192			neigh = __neigh_lookup_noref(tbl, addr, dev);
3193		}
3194		if (!neigh)
3195			neigh = __neigh_create(tbl, addr, dev, false);
3196		err = PTR_ERR(neigh);
3197		if (IS_ERR(neigh)) {
3198			rcu_read_unlock();
3199			goto out_kfree_skb;
3200		}
3201		err = READ_ONCE(neigh->output)(neigh, skb);
3202out_unlock:
3203		rcu_read_unlock();
3204	}
3205	else if (index == NEIGH_LINK_TABLE) {
3206		err = dev_hard_header(skb, dev, ntohs(skb->protocol),
3207				      addr, NULL, skb->len);
3208		if (err < 0)
3209			goto out_kfree_skb;
3210		err = dev_queue_xmit(skb);
3211	}
3212out:
3213	return err;
3214out_kfree_skb:
3215	kfree_skb(skb);
3216	goto out;
3217}
3218EXPORT_SYMBOL(neigh_xmit);
3219
3220#ifdef CONFIG_PROC_FS
3221
3222static struct neighbour *neigh_get_valid(struct seq_file *seq,
3223					 struct neighbour *n,
3224					 loff_t *pos)
3225{
3226	struct neigh_seq_state *state = seq->private;
3227	struct net *net = seq_file_net(seq);
3228
3229	if (!net_eq(dev_net(n->dev), net))
3230		return NULL;
3231
3232	if (state->neigh_sub_iter) {
3233		loff_t fakep = 0;
3234		void *v;
3235
3236		v = state->neigh_sub_iter(state, n, pos ? pos : &fakep);
3237		if (!v)
3238			return NULL;
3239		if (pos)
3240			return v;
3241	}
3242
3243	if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
3244		return n;
3245
3246	if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
3247		return n;
3248
3249	return NULL;
3250}
3251
3252static struct neighbour *neigh_get_first(struct seq_file *seq)
3253{
3254	struct neigh_seq_state *state = seq->private;
3255	struct neigh_hash_table *nht = state->nht;
3256	struct neighbour *n, *tmp;
3257
3258	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
3259
3260	while (++state->bucket < (1 << nht->hash_shift)) {
3261		neigh_for_each_in_bucket(n, &nht->hash_heads[state->bucket]) {
3262			tmp = neigh_get_valid(seq, n, NULL);
3263			if (tmp)
3264				return tmp;
3265		}
3266	}
3267
3268	return NULL;
3269}
3270
3271static struct neighbour *neigh_get_next(struct seq_file *seq,
3272					struct neighbour *n,
3273					loff_t *pos)
3274{
3275	struct neigh_seq_state *state = seq->private;
3276	struct neighbour *tmp;
3277
3278	if (state->neigh_sub_iter) {
3279		void *v = state->neigh_sub_iter(state, n, pos);
3280
3281		if (v)
3282			return n;
3283	}
3284
3285	hlist_for_each_entry_continue(n, hash) {
3286		tmp = neigh_get_valid(seq, n, pos);
3287		if (tmp) {
3288			n = tmp;
3289			goto out;
3290		}
3291	}
3292
3293	n = neigh_get_first(seq);
3294out:
3295	if (n && pos)
3296		--(*pos);
3297
3298	return n;
3299}
3300
3301static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
3302{
3303	struct neighbour *n = neigh_get_first(seq);
3304
3305	if (n) {
3306		--(*pos);
3307		while (*pos) {
3308			n = neigh_get_next(seq, n, pos);
3309			if (!n)
3310				break;
3311		}
3312	}
3313	return *pos ? NULL : n;
3314}
3315
3316static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
3317{
3318	struct neigh_seq_state *state = seq->private;
3319	struct net *net = seq_file_net(seq);
3320	struct neigh_table *tbl = state->tbl;
3321	struct pneigh_entry *pn = NULL;
3322	int bucket;
3323
3324	state->flags |= NEIGH_SEQ_IS_PNEIGH;
3325	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
3326		pn = rcu_dereference(tbl->phash_buckets[bucket]);
3327
3328		while (pn && !net_eq(pneigh_net(pn), net))
3329			pn = rcu_dereference(pn->next);
3330		if (pn)
3331			break;
3332	}
3333	state->bucket = bucket;
3334
3335	return pn;
3336}
3337
3338static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
3339					    struct pneigh_entry *pn,
3340					    loff_t *pos)
3341{
3342	struct neigh_seq_state *state = seq->private;
3343	struct net *net = seq_file_net(seq);
3344	struct neigh_table *tbl = state->tbl;
3345
3346	do {
3347		pn = rcu_dereference(pn->next);
3348	} while (pn && !net_eq(pneigh_net(pn), net));
3349
3350	while (!pn) {
3351		if (++state->bucket > PNEIGH_HASHMASK)
3352			break;
3353
3354		pn = rcu_dereference(tbl->phash_buckets[state->bucket]);
3355
3356		while (pn && !net_eq(pneigh_net(pn), net))
3357			pn = rcu_dereference(pn->next);
3358		if (pn)
3359			break;
3360	}
3361
3362	if (pn && pos)
3363		--(*pos);
3364
3365	return pn;
3366}
3367
3368static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
3369{
3370	struct pneigh_entry *pn = pneigh_get_first(seq);
3371
3372	if (pn) {
3373		--(*pos);
3374		while (*pos) {
3375			pn = pneigh_get_next(seq, pn, pos);
3376			if (!pn)
3377				break;
3378		}
3379	}
3380	return *pos ? NULL : pn;
3381}
3382
3383static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
3384{
3385	struct neigh_seq_state *state = seq->private;
3386	void *rc;
3387	loff_t idxpos = *pos;
3388
3389	rc = neigh_get_idx(seq, &idxpos);
3390	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3391		rc = pneigh_get_idx(seq, &idxpos);
3392
3393	return rc;
3394}
3395
3396void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
3397	__acquires(tbl->lock)
3398	__acquires(rcu)
3399{
3400	struct neigh_seq_state *state = seq->private;
3401
3402	state->tbl = tbl;
3403	state->bucket = -1;
3404	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
3405
3406	rcu_read_lock();
3407	state->nht = rcu_dereference(tbl->nht);
3408	spin_lock_bh(&tbl->lock);
3409
3410	return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
3411}
3412EXPORT_SYMBOL(neigh_seq_start);
3413
3414void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3415{
3416	struct neigh_seq_state *state;
3417	void *rc;
3418
3419	if (v == SEQ_START_TOKEN) {
3420		rc = neigh_get_first(seq);
3421		goto out;
3422	}
3423
3424	state = seq->private;
3425	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
3426		rc = neigh_get_next(seq, v, NULL);
3427		if (rc)
3428			goto out;
3429		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3430			rc = pneigh_get_first(seq);
3431	} else {
3432		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
3433		rc = pneigh_get_next(seq, v, NULL);
3434	}
3435out:
3436	++(*pos);
3437	return rc;
3438}
3439EXPORT_SYMBOL(neigh_seq_next);
3440
3441void neigh_seq_stop(struct seq_file *seq, void *v)
3442	__releases(tbl->lock)
3443	__releases(rcu)
3444{
3445	struct neigh_seq_state *state = seq->private;
3446	struct neigh_table *tbl = state->tbl;
3447
3448	spin_unlock_bh(&tbl->lock);
3449	rcu_read_unlock();
3450}
3451EXPORT_SYMBOL(neigh_seq_stop);
3452
3453/* statistics via seq_file */
3454
3455static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
3456{
3457	struct neigh_table *tbl = pde_data(file_inode(seq->file));
3458	int cpu;
3459
3460	if (*pos == 0)
3461		return SEQ_START_TOKEN;
3462
3463	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
3464		if (!cpu_possible(cpu))
3465			continue;
3466		*pos = cpu+1;
3467		return per_cpu_ptr(tbl->stats, cpu);
3468	}
3469	return NULL;
3470}
3471
3472static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3473{
3474	struct neigh_table *tbl = pde_data(file_inode(seq->file));
3475	int cpu;
3476
3477	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
3478		if (!cpu_possible(cpu))
3479			continue;
3480		*pos = cpu+1;
3481		return per_cpu_ptr(tbl->stats, cpu);
3482	}
3483	(*pos)++;
3484	return NULL;
3485}
3486
3487static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
3488{
3489
3490}
3491
3492static int neigh_stat_seq_show(struct seq_file *seq, void *v)
3493{
3494	struct neigh_table *tbl = pde_data(file_inode(seq->file));
3495	struct neigh_statistics *st = v;
3496
3497	if (v == SEQ_START_TOKEN) {
3498		seq_puts(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");
3499		return 0;
3500	}
3501
3502	seq_printf(seq, "%08x %08lx %08lx %08lx   %08lx %08lx %08lx   "
3503			"%08lx         %08lx         %08lx         "
3504			"%08lx       %08lx            %08lx\n",
3505		   atomic_read(&tbl->entries),
3506
3507		   st->allocs,
3508		   st->destroys,
3509		   st->hash_grows,
3510
3511		   st->lookups,
3512		   st->hits,
3513
3514		   st->res_failed,
3515
3516		   st->rcv_probes_mcast,
3517		   st->rcv_probes_ucast,
3518
3519		   st->periodic_gc_runs,
3520		   st->forced_gc_runs,
3521		   st->unres_discards,
3522		   st->table_fulls
3523		   );
3524
3525	return 0;
3526}
3527
3528static const struct seq_operations neigh_stat_seq_ops = {
3529	.start	= neigh_stat_seq_start,
3530	.next	= neigh_stat_seq_next,
3531	.stop	= neigh_stat_seq_stop,
3532	.show	= neigh_stat_seq_show,
3533};
3534#endif /* CONFIG_PROC_FS */
3535
3536static void __neigh_notify(struct neighbour *n, int type, int flags,
3537			   u32 pid)
3538{
3539	struct sk_buff *skb;
3540	int err = -ENOBUFS;
3541	struct net *net;
3542
3543	rcu_read_lock();
3544	net = dev_net_rcu(n->dev);
3545	skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
3546	if (skb == NULL)
3547		goto errout;
3548
3549	err = neigh_fill_info(skb, n, pid, 0, type, flags);
3550	if (err < 0) {
3551		/* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
3552		WARN_ON(err == -EMSGSIZE);
3553		kfree_skb(skb);
3554		goto errout;
3555	}
3556	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3557	goto out;
3558errout:
3559	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3560out:
3561	rcu_read_unlock();
3562}
3563
3564void neigh_app_ns(struct neighbour *n)
3565{
3566	__neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
3567}
3568EXPORT_SYMBOL(neigh_app_ns);
3569
3570#ifdef CONFIG_SYSCTL
3571static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
3572
3573static int proc_unres_qlen(const struct ctl_table *ctl, int write,
3574			   void *buffer, size_t *lenp, loff_t *ppos)
3575{
3576	int size, ret;
3577	struct ctl_table tmp = *ctl;
3578
3579	tmp.extra1 = SYSCTL_ZERO;
3580	tmp.extra2 = &unres_qlen_max;
3581	tmp.data = &size;
3582
3583	size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
3584	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3585
3586	if (write && !ret)
3587		*(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
3588	return ret;
3589}
3590
3591static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
3592				  int index)
3593{
3594	struct net_device *dev;
3595	int family = neigh_parms_family(p);
3596
3597	rcu_read_lock();
3598	for_each_netdev_rcu(net, dev) {
3599		struct neigh_parms *dst_p =
3600				neigh_get_dev_parms_rcu(dev, family);
3601
3602		if (dst_p && !test_bit(index, dst_p->data_state))
3603			dst_p->data[index] = p->data[index];
3604	}
3605	rcu_read_unlock();
3606}
3607
3608static void neigh_proc_update(const struct ctl_table *ctl, int write)
3609{
3610	struct net_device *dev = ctl->extra1;
3611	struct neigh_parms *p = ctl->extra2;
3612	struct net *net = neigh_parms_net(p);
3613	int index = (int *) ctl->data - p->data;
3614
3615	if (!write)
3616		return;
3617
3618	set_bit(index, p->data_state);
3619	if (index == NEIGH_VAR_DELAY_PROBE_TIME)
3620		call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
3621	if (!dev) /* NULL dev means this is default value */
3622		neigh_copy_dflt_parms(net, p, index);
3623}
3624
3625static int neigh_proc_dointvec_zero_intmax(const struct ctl_table *ctl, int write,
3626					   void *buffer, size_t *lenp,
3627					   loff_t *ppos)
3628{
3629	struct ctl_table tmp = *ctl;
3630	int ret;
3631
3632	tmp.extra1 = SYSCTL_ZERO;
3633	tmp.extra2 = SYSCTL_INT_MAX;
3634
3635	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3636	neigh_proc_update(ctl, write);
3637	return ret;
3638}
3639
3640static int neigh_proc_dointvec_ms_jiffies_positive(const struct ctl_table *ctl, int write,
3641						   void *buffer, size_t *lenp, loff_t *ppos)
3642{
3643	struct ctl_table tmp = *ctl;
3644	int ret;
3645
3646	int min = msecs_to_jiffies(1);
3647
3648	tmp.extra1 = &min;
3649	tmp.extra2 = NULL;
3650
3651	ret = proc_dointvec_ms_jiffies_minmax(&tmp, write, buffer, lenp, ppos);
3652	neigh_proc_update(ctl, write);
3653	return ret;
3654}
3655
3656int neigh_proc_dointvec(const struct ctl_table *ctl, int write, void *buffer,
3657			size_t *lenp, loff_t *ppos)
3658{
3659	int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3660
3661	neigh_proc_update(ctl, write);
3662	return ret;
3663}
3664EXPORT_SYMBOL(neigh_proc_dointvec);
3665
3666int neigh_proc_dointvec_jiffies(const struct ctl_table *ctl, int write, void *buffer,
3667				size_t *lenp, loff_t *ppos)
3668{
3669	int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3670
3671	neigh_proc_update(ctl, write);
3672	return ret;
3673}
3674EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3675
3676static int neigh_proc_dointvec_userhz_jiffies(const struct ctl_table *ctl, int write,
3677					      void *buffer, size_t *lenp,
3678					      loff_t *ppos)
3679{
3680	int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
3681
3682	neigh_proc_update(ctl, write);
3683	return ret;
3684}
3685
3686int neigh_proc_dointvec_ms_jiffies(const struct ctl_table *ctl, int write,
3687				   void *buffer, size_t *lenp, loff_t *ppos)
3688{
3689	int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3690
3691	neigh_proc_update(ctl, write);
3692	return ret;
3693}
3694EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3695
3696static int neigh_proc_dointvec_unres_qlen(const struct ctl_table *ctl, int write,
3697					  void *buffer, size_t *lenp,
3698					  loff_t *ppos)
3699{
3700	int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
3701
3702	neigh_proc_update(ctl, write);
3703	return ret;
3704}
3705
3706static int neigh_proc_base_reachable_time(const struct ctl_table *ctl, int write,
3707					  void *buffer, size_t *lenp,
3708					  loff_t *ppos)
3709{
3710	struct neigh_parms *p = ctl->extra2;
3711	int ret;
3712
3713	if (strcmp(ctl->procname, "base_reachable_time") == 0)
3714		ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3715	else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
3716		ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3717	else
3718		ret = -1;
3719
3720	if (write && ret == 0) {
3721		/* update reachable_time as well, otherwise, the change will
3722		 * only be effective after the next time neigh_periodic_work
3723		 * decides to recompute it
3724		 */
3725		neigh_set_reach_time(p);
3726	}
3727	return ret;
3728}
3729
3730#define NEIGH_PARMS_DATA_OFFSET(index)	\
3731	(&((struct neigh_parms *) 0)->data[index])
3732
3733#define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
3734	[NEIGH_VAR_ ## attr] = { \
3735		.procname	= name, \
3736		.data		= NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
3737		.maxlen		= sizeof(int), \
3738		.mode		= mval, \
3739		.proc_handler	= proc, \
3740	}
3741
3742#define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
3743	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
3744
3745#define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3746	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
3747
3748#define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3749	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
3750
3751#define NEIGH_SYSCTL_MS_JIFFIES_POSITIVE_ENTRY(attr, name) \
3752	NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies_positive)
3753
3754#define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3755	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
3756
3757#define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3758	NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
3759
3760static struct neigh_sysctl_table {
3761	struct ctl_table_header *sysctl_header;
3762	struct ctl_table neigh_vars[NEIGH_VAR_MAX];
3763} neigh_sysctl_template __read_mostly = {
3764	.neigh_vars = {
3765		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
3766		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
3767		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
3768		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
3769		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
3770		NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
3771		NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
3772		NEIGH_SYSCTL_MS_JIFFIES_POSITIVE_ENTRY(INTERVAL_PROBE_TIME_MS,
3773						       "interval_probe_time_ms"),
3774		NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
3775		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
3776		NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
3777		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
3778		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
3779		NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
3780		NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
3781		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
3782		NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
3783		[NEIGH_VAR_GC_INTERVAL] = {
3784			.procname	= "gc_interval",
3785			.maxlen		= sizeof(int),
3786			.mode		= 0644,
3787			.proc_handler	= proc_dointvec_jiffies,
3788		},
3789		[NEIGH_VAR_GC_THRESH1] = {
3790			.procname	= "gc_thresh1",
3791			.maxlen		= sizeof(int),
3792			.mode		= 0644,
3793			.extra1		= SYSCTL_ZERO,
3794			.extra2		= SYSCTL_INT_MAX,
3795			.proc_handler	= proc_dointvec_minmax,
3796		},
3797		[NEIGH_VAR_GC_THRESH2] = {
3798			.procname	= "gc_thresh2",
3799			.maxlen		= sizeof(int),
3800			.mode		= 0644,
3801			.extra1		= SYSCTL_ZERO,
3802			.extra2		= SYSCTL_INT_MAX,
3803			.proc_handler	= proc_dointvec_minmax,
3804		},
3805		[NEIGH_VAR_GC_THRESH3] = {
3806			.procname	= "gc_thresh3",
3807			.maxlen		= sizeof(int),
3808			.mode		= 0644,
3809			.extra1		= SYSCTL_ZERO,
3810			.extra2		= SYSCTL_INT_MAX,
3811			.proc_handler	= proc_dointvec_minmax,
3812		},
3813	},
3814};
3815
3816int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
3817			  proc_handler *handler)
3818{
3819	int i;
3820	struct neigh_sysctl_table *t;
3821	const char *dev_name_source;
3822	char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
3823	char *p_name;
3824	size_t neigh_vars_size;
3825
3826	t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);
3827	if (!t)
3828		goto err;
3829
3830	for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
3831		t->neigh_vars[i].data += (long) p;
3832		t->neigh_vars[i].extra1 = dev;
3833		t->neigh_vars[i].extra2 = p;
3834	}
3835
3836	neigh_vars_size = ARRAY_SIZE(t->neigh_vars);
3837	if (dev) {
3838		dev_name_source = dev->name;
3839		/* Terminate the table early */
3840		neigh_vars_size = NEIGH_VAR_BASE_REACHABLE_TIME_MS + 1;
3841	} else {
3842		struct neigh_table *tbl = p->tbl;
3843		dev_name_source = "default";
3844		t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
3845		t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
3846		t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
3847		t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
3848	}
3849
3850	if (handler) {
3851		/* RetransTime */
3852		t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
3853		/* ReachableTime */
3854		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
3855		/* RetransTime (in milliseconds)*/
3856		t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
3857		/* ReachableTime (in milliseconds) */
3858		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
3859	} else {
3860		/* Those handlers will update p->reachable_time after
3861		 * base_reachable_time(_ms) is set to ensure the new timer starts being
3862		 * applied after the next neighbour update instead of waiting for
3863		 * neigh_periodic_work to update its value (can be multiple minutes)
3864		 * So any handler that replaces them should do this as well
3865		 */
3866		/* ReachableTime */
3867		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
3868			neigh_proc_base_reachable_time;
3869		/* ReachableTime (in milliseconds) */
3870		t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
3871			neigh_proc_base_reachable_time;
3872	}
3873
3874	switch (neigh_parms_family(p)) {
3875	case AF_INET:
3876	      p_name = "ipv4";
3877	      break;
3878	case AF_INET6:
3879	      p_name = "ipv6";
3880	      break;
3881	default:
3882	      BUG();
3883	}
3884
3885	snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
3886		p_name, dev_name_source);
3887	t->sysctl_header = register_net_sysctl_sz(neigh_parms_net(p),
3888						  neigh_path, t->neigh_vars,
3889						  neigh_vars_size);
3890	if (!t->sysctl_header)
3891		goto free;
3892
3893	p->sysctl_table = t;
3894	return 0;
3895
3896free:
3897	kfree(t);
3898err:
3899	return -ENOBUFS;
3900}
3901EXPORT_SYMBOL(neigh_sysctl_register);
3902
3903void neigh_sysctl_unregister(struct neigh_parms *p)
3904{
3905	if (p->sysctl_table) {
3906		struct neigh_sysctl_table *t = p->sysctl_table;
3907		p->sysctl_table = NULL;
3908		unregister_net_sysctl_table(t->sysctl_header);
3909		kfree(t);
3910	}
3911}
3912EXPORT_SYMBOL(neigh_sysctl_unregister);
3913
3914#endif	/* CONFIG_SYSCTL */
3915
3916static const struct rtnl_msg_handler neigh_rtnl_msg_handlers[] __initconst = {
3917	{.msgtype = RTM_NEWNEIGH, .doit = neigh_add},
3918	{.msgtype = RTM_DELNEIGH, .doit = neigh_delete},
3919	{.msgtype = RTM_GETNEIGH, .doit = neigh_get, .dumpit = neigh_dump_info,
3920	 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
3921	{.msgtype = RTM_GETNEIGHTBL, .dumpit = neightbl_dump_info,
3922	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
3923	{.msgtype = RTM_SETNEIGHTBL, .doit = neightbl_set,
3924	 .flags = RTNL_FLAG_DOIT_UNLOCKED},
3925};
3926
3927static int __init neigh_init(void)
3928{
3929	rtnl_register_many(neigh_rtnl_msg_handlers);
3930	return 0;
3931}
3932
3933subsys_initcall(neigh_init);
~/ Read-only file preview.