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