1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic address resolution entity
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
10 * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
11 * Harald Welte Add neighbour cache statistics like rtstat
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 #include <linux/sysctl.h>
27 #include <linux/times.h>
28 #include <net/net_namespace.h>
29 #include <net/neighbour.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>
42 #include <trace/events/neigh.h>
45 #define neigh_dbg(level, fmt, ...) \
47 if (level <= NEIGH_DEBUG) \
48 pr_debug(fmt, ##__VA_ARGS__); \
51 #define PNEIGH_HASHMASK 0xF
53 static void neigh_timer_handler(struct timer_list *t);
54 static void __neigh_notify(struct neighbour *n, int type, int flags,
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);
61 static const struct seq_operations neigh_stat_seq_ops;
65 Neighbour hash table buckets are protected with rwlock tbl->lock.
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
72 - If the entry requires some non-trivial actions, increase
73 its reference count and release table lock.
75 Neighbour entries are protected:
76 - with reference count.
77 - with rwlock neigh->lock
79 Reference count prevents destruction.
81 neigh->lock mainly serializes ll address data and its validity state.
82 However, the same lock is used to protect another entry fields:
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.
92 static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
98 static void neigh_cleanup_and_release(struct neighbour *neigh)
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);
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.
112 unsigned long neigh_rand_reach_time(unsigned long base)
114 return base ? get_random_u32_below(base) + (base >> 1) : 0;
116 EXPORT_SYMBOL(neigh_rand_reach_time);
118 static void neigh_mark_dead(struct neighbour *n)
121 if (!list_empty(&n->gc_list)) {
122 list_del_init(&n->gc_list);
123 atomic_dec(&n->tbl->gc_entries);
125 if (!list_empty(&n->managed_list))
126 list_del_init(&n->managed_list);
129 static void neigh_update_gc_list(struct neighbour *n)
131 bool on_gc_list, exempt_from_gc;
133 write_lock_bh(&n->tbl->lock);
134 write_lock(&n->lock);
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
141 exempt_from_gc = n->nud_state & NUD_PERMANENT ||
142 n->flags & NTF_EXT_LEARNED;
143 on_gc_list = !list_empty(&n->gc_list);
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);
154 write_unlock(&n->lock);
155 write_unlock_bh(&n->tbl->lock);
158 static void neigh_update_managed_list(struct neighbour *n)
160 bool on_managed_list, add_to_managed;
162 write_lock_bh(&n->tbl->lock);
163 write_lock(&n->lock);
167 add_to_managed = n->flags & NTF_MANAGED;
168 on_managed_list = !list_empty(&n->managed_list);
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);
175 write_unlock(&n->lock);
176 write_unlock_bh(&n->tbl->lock);
179 static void neigh_update_flags(struct neighbour *neigh, u32 flags, int *notify,
180 bool *gc_update, bool *managed_update)
182 u32 ndm_flags, old_flags = neigh->flags;
184 if (!(flags & NEIGH_UPDATE_F_ADMIN))
187 ndm_flags = (flags & NEIGH_UPDATE_F_EXT_LEARNED) ? NTF_EXT_LEARNED : 0;
188 ndm_flags |= (flags & NEIGH_UPDATE_F_MANAGED) ? NTF_MANAGED : 0;
190 if ((old_flags ^ ndm_flags) & NTF_EXT_LEARNED) {
191 if (ndm_flags & NTF_EXT_LEARNED)
192 neigh->flags |= NTF_EXT_LEARNED;
194 neigh->flags &= ~NTF_EXT_LEARNED;
198 if ((old_flags ^ ndm_flags) & NTF_MANAGED) {
199 if (ndm_flags & NTF_MANAGED)
200 neigh->flags |= NTF_MANAGED;
202 neigh->flags &= ~NTF_MANAGED;
204 *managed_update = true;
208 static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
209 struct neigh_table *tbl)
213 write_lock(&n->lock);
214 if (refcount_read(&n->refcnt) == 1) {
215 struct neighbour *neigh;
217 neigh = rcu_dereference_protected(n->next,
218 lockdep_is_held(&tbl->lock));
219 rcu_assign_pointer(*np, neigh);
223 write_unlock(&n->lock);
225 neigh_cleanup_and_release(n);
229 bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
231 struct neigh_hash_table *nht;
232 void *pkey = ndel->primary_key;
235 struct neighbour __rcu **np;
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);
242 np = &nht->hash_buckets[hash_val];
243 while ((n = rcu_dereference_protected(*np,
244 lockdep_is_held(&tbl->lock)))) {
246 return neigh_del(n, np, tbl);
252 static int neigh_forced_gc(struct neigh_table *tbl)
254 int max_clean = atomic_read(&tbl->gc_entries) -
255 READ_ONCE(tbl->gc_thresh2);
256 unsigned long tref = jiffies - 5 * HZ;
257 struct neighbour *n, *tmp;
260 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
262 write_lock_bh(&tbl->lock);
264 list_for_each_entry_safe(n, tmp, &tbl->gc_list, gc_list) {
265 if (refcount_read(&n->refcnt) == 1) {
268 write_lock(&n->lock);
269 if ((n->nud_state == NUD_FAILED) ||
270 (n->nud_state == NUD_NOARP) ||
271 (tbl->is_multicast &&
272 tbl->is_multicast(n->primary_key)) ||
273 !time_in_range(n->updated, tref, jiffies))
275 write_unlock(&n->lock);
277 if (remove && neigh_remove_one(n, tbl))
279 if (shrunk >= max_clean)
284 WRITE_ONCE(tbl->last_flush, jiffies);
286 write_unlock_bh(&tbl->lock);
291 static void neigh_add_timer(struct neighbour *n, unsigned long when)
293 /* Use safe distance from the jiffies - LONG_MAX point while timer
294 * is running in DELAY/PROBE state but still show to user space
295 * large times in the past.
297 unsigned long mint = jiffies - (LONG_MAX - 86400 * HZ);
300 if (!time_in_range(n->confirmed, mint, jiffies))
302 if (time_before(n->used, n->confirmed))
303 n->used = n->confirmed;
304 if (unlikely(mod_timer(&n->timer, when))) {
305 printk("NEIGH: BUG, double timer add, state is %x\n",
311 static int neigh_del_timer(struct neighbour *n)
313 if ((n->nud_state & NUD_IN_TIMER) &&
314 del_timer(&n->timer)) {
321 static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
326 return __in_dev_arp_parms_get_rcu(dev);
328 return __in6_dev_nd_parms_get_rcu(dev);
333 static void neigh_parms_qlen_dec(struct net_device *dev, int family)
335 struct neigh_parms *p;
338 p = neigh_get_dev_parms_rcu(dev, family);
344 static void pneigh_queue_purge(struct sk_buff_head *list, struct net *net,
347 struct sk_buff_head tmp;
351 skb_queue_head_init(&tmp);
352 spin_lock_irqsave(&list->lock, flags);
353 skb = skb_peek(list);
354 while (skb != NULL) {
355 struct sk_buff *skb_next = skb_peek_next(skb, list);
356 struct net_device *dev = skb->dev;
358 if (net == NULL || net_eq(dev_net(dev), net)) {
359 neigh_parms_qlen_dec(dev, family);
360 __skb_unlink(skb, list);
361 __skb_queue_tail(&tmp, skb);
365 spin_unlock_irqrestore(&list->lock, flags);
367 while ((skb = __skb_dequeue(&tmp))) {
373 static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
377 struct neigh_hash_table *nht;
379 nht = rcu_dereference_protected(tbl->nht,
380 lockdep_is_held(&tbl->lock));
382 for (i = 0; i < (1 << nht->hash_shift); i++) {
384 struct neighbour __rcu **np = &nht->hash_buckets[i];
386 while ((n = rcu_dereference_protected(*np,
387 lockdep_is_held(&tbl->lock))) != NULL) {
388 if (dev && n->dev != dev) {
392 if (skip_perm && n->nud_state & NUD_PERMANENT) {
396 rcu_assign_pointer(*np,
397 rcu_dereference_protected(n->next,
398 lockdep_is_held(&tbl->lock)));
399 write_lock(&n->lock);
402 if (refcount_read(&n->refcnt) != 1) {
403 /* The most unpleasant situation.
404 We must destroy neighbour entry,
405 but someone still uses it.
407 The destroy will be delayed until
408 the last user releases us, but
409 we must kill timers etc. and move
412 __skb_queue_purge(&n->arp_queue);
413 n->arp_queue_len_bytes = 0;
414 WRITE_ONCE(n->output, neigh_blackhole);
415 if (n->nud_state & NUD_VALID)
416 n->nud_state = NUD_NOARP;
418 n->nud_state = NUD_NONE;
419 neigh_dbg(2, "neigh %p is stray\n", n);
421 write_unlock(&n->lock);
422 neigh_cleanup_and_release(n);
427 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
429 write_lock_bh(&tbl->lock);
430 neigh_flush_dev(tbl, dev, false);
431 write_unlock_bh(&tbl->lock);
433 EXPORT_SYMBOL(neigh_changeaddr);
435 static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
438 write_lock_bh(&tbl->lock);
439 neigh_flush_dev(tbl, dev, skip_perm);
440 pneigh_ifdown_and_unlock(tbl, dev);
441 pneigh_queue_purge(&tbl->proxy_queue, dev ? dev_net(dev) : NULL,
443 if (skb_queue_empty_lockless(&tbl->proxy_queue))
444 del_timer_sync(&tbl->proxy_timer);
448 int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
450 __neigh_ifdown(tbl, dev, true);
453 EXPORT_SYMBOL(neigh_carrier_down);
455 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
457 __neigh_ifdown(tbl, dev, false);
460 EXPORT_SYMBOL(neigh_ifdown);
462 static struct neighbour *neigh_alloc(struct neigh_table *tbl,
463 struct net_device *dev,
464 u32 flags, bool exempt_from_gc)
466 struct neighbour *n = NULL;
467 unsigned long now = jiffies;
468 int entries, gc_thresh3;
473 entries = atomic_inc_return(&tbl->gc_entries) - 1;
474 gc_thresh3 = READ_ONCE(tbl->gc_thresh3);
475 if (entries >= gc_thresh3 ||
476 (entries >= READ_ONCE(tbl->gc_thresh2) &&
477 time_after(now, READ_ONCE(tbl->last_flush) + 5 * HZ))) {
478 if (!neigh_forced_gc(tbl) && entries >= gc_thresh3) {
479 net_info_ratelimited("%s: neighbor table overflow!\n",
481 NEIGH_CACHE_STAT_INC(tbl, table_fulls);
487 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
491 __skb_queue_head_init(&n->arp_queue);
492 rwlock_init(&n->lock);
493 seqlock_init(&n->ha_lock);
494 n->updated = n->used = now;
495 n->nud_state = NUD_NONE;
496 n->output = neigh_blackhole;
498 seqlock_init(&n->hh.hh_lock);
499 n->parms = neigh_parms_clone(&tbl->parms);
500 timer_setup(&n->timer, neigh_timer_handler, 0);
502 NEIGH_CACHE_STAT_INC(tbl, allocs);
504 refcount_set(&n->refcnt, 1);
506 INIT_LIST_HEAD(&n->gc_list);
507 INIT_LIST_HEAD(&n->managed_list);
509 atomic_inc(&tbl->entries);
515 atomic_dec(&tbl->gc_entries);
519 static void neigh_get_hash_rnd(u32 *x)
521 *x = get_random_u32() | 1;
524 static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
526 size_t size = (1 << shift) * sizeof(struct neighbour *);
527 struct neigh_hash_table *ret;
528 struct neighbour __rcu **buckets;
531 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
534 if (size <= PAGE_SIZE) {
535 buckets = kzalloc(size, GFP_ATOMIC);
537 buckets = (struct neighbour __rcu **)
538 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
540 kmemleak_alloc(buckets, size, 1, GFP_ATOMIC);
546 ret->hash_buckets = buckets;
547 ret->hash_shift = shift;
548 for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
549 neigh_get_hash_rnd(&ret->hash_rnd[i]);
553 static void neigh_hash_free_rcu(struct rcu_head *head)
555 struct neigh_hash_table *nht = container_of(head,
556 struct neigh_hash_table,
558 size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
559 struct neighbour __rcu **buckets = nht->hash_buckets;
561 if (size <= PAGE_SIZE) {
564 kmemleak_free(buckets);
565 free_pages((unsigned long)buckets, get_order(size));
570 static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
571 unsigned long new_shift)
573 unsigned int i, hash;
574 struct neigh_hash_table *new_nht, *old_nht;
576 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
578 old_nht = rcu_dereference_protected(tbl->nht,
579 lockdep_is_held(&tbl->lock));
580 new_nht = neigh_hash_alloc(new_shift);
584 for (i = 0; i < (1 << old_nht->hash_shift); i++) {
585 struct neighbour *n, *next;
587 for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
588 lockdep_is_held(&tbl->lock));
591 hash = tbl->hash(n->primary_key, n->dev,
594 hash >>= (32 - new_nht->hash_shift);
595 next = rcu_dereference_protected(n->next,
596 lockdep_is_held(&tbl->lock));
598 rcu_assign_pointer(n->next,
599 rcu_dereference_protected(
600 new_nht->hash_buckets[hash],
601 lockdep_is_held(&tbl->lock)));
602 rcu_assign_pointer(new_nht->hash_buckets[hash], n);
606 rcu_assign_pointer(tbl->nht, new_nht);
607 call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
611 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
612 struct net_device *dev)
616 NEIGH_CACHE_STAT_INC(tbl, lookups);
619 n = __neigh_lookup_noref(tbl, pkey, dev);
621 if (!refcount_inc_not_zero(&n->refcnt))
623 NEIGH_CACHE_STAT_INC(tbl, hits);
629 EXPORT_SYMBOL(neigh_lookup);
631 static struct neighbour *
632 ___neigh_create(struct neigh_table *tbl, const void *pkey,
633 struct net_device *dev, u32 flags,
634 bool exempt_from_gc, bool want_ref)
636 u32 hash_val, key_len = tbl->key_len;
637 struct neighbour *n1, *rc, *n;
638 struct neigh_hash_table *nht;
641 n = neigh_alloc(tbl, dev, flags, exempt_from_gc);
642 trace_neigh_create(tbl, dev, pkey, n, exempt_from_gc);
644 rc = ERR_PTR(-ENOBUFS);
648 memcpy(n->primary_key, pkey, key_len);
650 netdev_hold(dev, &n->dev_tracker, GFP_ATOMIC);
652 /* Protocol specific setup. */
653 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
655 goto out_neigh_release;
658 if (dev->netdev_ops->ndo_neigh_construct) {
659 error = dev->netdev_ops->ndo_neigh_construct(dev, n);
662 goto out_neigh_release;
666 /* Device specific setup. */
667 if (n->parms->neigh_setup &&
668 (error = n->parms->neigh_setup(n)) < 0) {
670 goto out_neigh_release;
673 n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
675 write_lock_bh(&tbl->lock);
676 nht = rcu_dereference_protected(tbl->nht,
677 lockdep_is_held(&tbl->lock));
679 if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
680 nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
682 hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
684 if (n->parms->dead) {
685 rc = ERR_PTR(-EINVAL);
689 for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
690 lockdep_is_held(&tbl->lock));
692 n1 = rcu_dereference_protected(n1->next,
693 lockdep_is_held(&tbl->lock))) {
694 if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
704 list_add_tail(&n->gc_list, &n->tbl->gc_list);
705 if (n->flags & NTF_MANAGED)
706 list_add_tail(&n->managed_list, &n->tbl->managed_list);
709 rcu_assign_pointer(n->next,
710 rcu_dereference_protected(nht->hash_buckets[hash_val],
711 lockdep_is_held(&tbl->lock)));
712 rcu_assign_pointer(nht->hash_buckets[hash_val], n);
713 write_unlock_bh(&tbl->lock);
714 neigh_dbg(2, "neigh %p is created\n", n);
719 write_unlock_bh(&tbl->lock);
722 atomic_dec(&tbl->gc_entries);
727 struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
728 struct net_device *dev, bool want_ref)
730 return ___neigh_create(tbl, pkey, dev, 0, false, want_ref);
732 EXPORT_SYMBOL(__neigh_create);
734 static u32 pneigh_hash(const void *pkey, unsigned int key_len)
736 u32 hash_val = *(u32 *)(pkey + key_len - 4);
737 hash_val ^= (hash_val >> 16);
738 hash_val ^= hash_val >> 8;
739 hash_val ^= hash_val >> 4;
740 hash_val &= PNEIGH_HASHMASK;
744 static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
747 unsigned int key_len,
748 struct net_device *dev)
751 if (!memcmp(n->key, pkey, key_len) &&
752 net_eq(pneigh_net(n), net) &&
753 (n->dev == dev || !n->dev))
760 struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
761 struct net *net, const void *pkey, struct net_device *dev)
763 unsigned int key_len = tbl->key_len;
764 u32 hash_val = pneigh_hash(pkey, key_len);
766 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
767 net, pkey, key_len, dev);
769 EXPORT_SYMBOL_GPL(__pneigh_lookup);
771 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
772 struct net *net, const void *pkey,
773 struct net_device *dev, int creat)
775 struct pneigh_entry *n;
776 unsigned int key_len = tbl->key_len;
777 u32 hash_val = pneigh_hash(pkey, key_len);
779 read_lock_bh(&tbl->lock);
780 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
781 net, pkey, key_len, dev);
782 read_unlock_bh(&tbl->lock);
789 n = kzalloc(sizeof(*n) + key_len, GFP_KERNEL);
793 write_pnet(&n->net, net);
794 memcpy(n->key, pkey, key_len);
796 netdev_hold(dev, &n->dev_tracker, GFP_KERNEL);
798 if (tbl->pconstructor && tbl->pconstructor(n)) {
799 netdev_put(dev, &n->dev_tracker);
805 write_lock_bh(&tbl->lock);
806 n->next = tbl->phash_buckets[hash_val];
807 tbl->phash_buckets[hash_val] = n;
808 write_unlock_bh(&tbl->lock);
812 EXPORT_SYMBOL(pneigh_lookup);
815 int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
816 struct net_device *dev)
818 struct pneigh_entry *n, **np;
819 unsigned int key_len = tbl->key_len;
820 u32 hash_val = pneigh_hash(pkey, key_len);
822 write_lock_bh(&tbl->lock);
823 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
825 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
826 net_eq(pneigh_net(n), net)) {
828 write_unlock_bh(&tbl->lock);
829 if (tbl->pdestructor)
831 netdev_put(n->dev, &n->dev_tracker);
836 write_unlock_bh(&tbl->lock);
840 static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
841 struct net_device *dev)
843 struct pneigh_entry *n, **np, *freelist = NULL;
846 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
847 np = &tbl->phash_buckets[h];
848 while ((n = *np) != NULL) {
849 if (!dev || n->dev == dev) {
858 write_unlock_bh(&tbl->lock);
859 while ((n = freelist)) {
862 if (tbl->pdestructor)
864 netdev_put(n->dev, &n->dev_tracker);
870 static void neigh_parms_destroy(struct neigh_parms *parms);
872 static inline void neigh_parms_put(struct neigh_parms *parms)
874 if (refcount_dec_and_test(&parms->refcnt))
875 neigh_parms_destroy(parms);
879 * neighbour must already be out of the table;
882 void neigh_destroy(struct neighbour *neigh)
884 struct net_device *dev = neigh->dev;
886 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
889 pr_warn("Destroying alive neighbour %p\n", neigh);
894 if (neigh_del_timer(neigh))
895 pr_warn("Impossible event\n");
897 write_lock_bh(&neigh->lock);
898 __skb_queue_purge(&neigh->arp_queue);
899 write_unlock_bh(&neigh->lock);
900 neigh->arp_queue_len_bytes = 0;
902 if (dev->netdev_ops->ndo_neigh_destroy)
903 dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
905 netdev_put(dev, &neigh->dev_tracker);
906 neigh_parms_put(neigh->parms);
908 neigh_dbg(2, "neigh %p is destroyed\n", neigh);
910 atomic_dec(&neigh->tbl->entries);
911 kfree_rcu(neigh, rcu);
913 EXPORT_SYMBOL(neigh_destroy);
915 /* Neighbour state is suspicious;
918 Called with write_locked neigh.
920 static void neigh_suspect(struct neighbour *neigh)
922 neigh_dbg(2, "neigh %p is suspected\n", neigh);
924 WRITE_ONCE(neigh->output, neigh->ops->output);
927 /* Neighbour state is OK;
930 Called with write_locked neigh.
932 static void neigh_connect(struct neighbour *neigh)
934 neigh_dbg(2, "neigh %p is connected\n", neigh);
936 WRITE_ONCE(neigh->output, neigh->ops->connected_output);
939 static void neigh_periodic_work(struct work_struct *work)
941 struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
943 struct neighbour __rcu **np;
945 struct neigh_hash_table *nht;
947 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
949 write_lock_bh(&tbl->lock);
950 nht = rcu_dereference_protected(tbl->nht,
951 lockdep_is_held(&tbl->lock));
954 * periodically recompute ReachableTime from random function
957 if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
958 struct neigh_parms *p;
960 WRITE_ONCE(tbl->last_rand, jiffies);
961 list_for_each_entry(p, &tbl->parms_list, list)
963 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
966 if (atomic_read(&tbl->entries) < READ_ONCE(tbl->gc_thresh1))
969 for (i = 0 ; i < (1 << nht->hash_shift); i++) {
970 np = &nht->hash_buckets[i];
972 while ((n = rcu_dereference_protected(*np,
973 lockdep_is_held(&tbl->lock))) != NULL) {
976 write_lock(&n->lock);
978 state = n->nud_state;
979 if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
980 (n->flags & NTF_EXT_LEARNED)) {
981 write_unlock(&n->lock);
985 if (time_before(n->used, n->confirmed) &&
986 time_is_before_eq_jiffies(n->confirmed))
987 n->used = n->confirmed;
989 if (refcount_read(&n->refcnt) == 1 &&
990 (state == NUD_FAILED ||
991 !time_in_range_open(jiffies, n->used,
992 n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
993 rcu_assign_pointer(*np,
994 rcu_dereference_protected(n->next,
995 lockdep_is_held(&tbl->lock)));
997 write_unlock(&n->lock);
998 neigh_cleanup_and_release(n);
1001 write_unlock(&n->lock);
1007 * It's fine to release lock here, even if hash table
1008 * grows while we are preempted.
1010 write_unlock_bh(&tbl->lock);
1012 write_lock_bh(&tbl->lock);
1013 nht = rcu_dereference_protected(tbl->nht,
1014 lockdep_is_held(&tbl->lock));
1017 /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
1018 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
1019 * BASE_REACHABLE_TIME.
1021 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1022 NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
1023 write_unlock_bh(&tbl->lock);
1026 static __inline__ int neigh_max_probes(struct neighbour *n)
1028 struct neigh_parms *p = n->parms;
1029 return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
1030 (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
1031 NEIGH_VAR(p, MCAST_PROBES));
1034 static void neigh_invalidate(struct neighbour *neigh)
1035 __releases(neigh->lock)
1036 __acquires(neigh->lock)
1038 struct sk_buff *skb;
1040 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
1041 neigh_dbg(2, "neigh %p is failed\n", neigh);
1042 neigh->updated = jiffies;
1044 /* It is very thin place. report_unreachable is very complicated
1045 routine. Particularly, it can hit the same neighbour entry!
1047 So that, we try to be accurate and avoid dead loop. --ANK
1049 while (neigh->nud_state == NUD_FAILED &&
1050 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1051 write_unlock(&neigh->lock);
1052 neigh->ops->error_report(neigh, skb);
1053 write_lock(&neigh->lock);
1055 __skb_queue_purge(&neigh->arp_queue);
1056 neigh->arp_queue_len_bytes = 0;
1059 static void neigh_probe(struct neighbour *neigh)
1060 __releases(neigh->lock)
1062 struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
1063 /* keep skb alive even if arp_queue overflows */
1065 skb = skb_clone(skb, GFP_ATOMIC);
1066 write_unlock(&neigh->lock);
1067 if (neigh->ops->solicit)
1068 neigh->ops->solicit(neigh, skb);
1069 atomic_inc(&neigh->probes);
1073 /* Called when a timer expires for a neighbour entry. */
1075 static void neigh_timer_handler(struct timer_list *t)
1077 unsigned long now, next;
1078 struct neighbour *neigh = from_timer(neigh, t, timer);
1082 write_lock(&neigh->lock);
1084 state = neigh->nud_state;
1088 if (!(state & NUD_IN_TIMER))
1091 if (state & NUD_REACHABLE) {
1092 if (time_before_eq(now,
1093 neigh->confirmed + neigh->parms->reachable_time)) {
1094 neigh_dbg(2, "neigh %p is still alive\n", neigh);
1095 next = neigh->confirmed + neigh->parms->reachable_time;
1096 } else if (time_before_eq(now,
1098 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
1099 neigh_dbg(2, "neigh %p is delayed\n", neigh);
1100 WRITE_ONCE(neigh->nud_state, NUD_DELAY);
1101 neigh->updated = jiffies;
1102 neigh_suspect(neigh);
1103 next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
1105 neigh_dbg(2, "neigh %p is suspected\n", neigh);
1106 WRITE_ONCE(neigh->nud_state, NUD_STALE);
1107 neigh->updated = jiffies;
1108 neigh_suspect(neigh);
1111 } else if (state & NUD_DELAY) {
1112 if (time_before_eq(now,
1114 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
1115 neigh_dbg(2, "neigh %p is now reachable\n", neigh);
1116 WRITE_ONCE(neigh->nud_state, NUD_REACHABLE);
1117 neigh->updated = jiffies;
1118 neigh_connect(neigh);
1120 next = neigh->confirmed + neigh->parms->reachable_time;
1122 neigh_dbg(2, "neigh %p is probed\n", neigh);
1123 WRITE_ONCE(neigh->nud_state, NUD_PROBE);
1124 neigh->updated = jiffies;
1125 atomic_set(&neigh->probes, 0);
1127 next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1131 /* NUD_PROBE|NUD_INCOMPLETE */
1132 next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), HZ/100);
1135 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
1136 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
1137 WRITE_ONCE(neigh->nud_state, NUD_FAILED);
1139 neigh_invalidate(neigh);
1143 if (neigh->nud_state & NUD_IN_TIMER) {
1144 if (time_before(next, jiffies + HZ/100))
1145 next = jiffies + HZ/100;
1146 if (!mod_timer(&neigh->timer, next))
1149 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
1153 write_unlock(&neigh->lock);
1157 neigh_update_notify(neigh, 0);
1159 trace_neigh_timer_handler(neigh, 0);
1161 neigh_release(neigh);
1164 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb,
1165 const bool immediate_ok)
1168 bool immediate_probe = false;
1170 write_lock_bh(&neigh->lock);
1173 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
1178 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
1179 if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
1180 NEIGH_VAR(neigh->parms, APP_PROBES)) {
1181 unsigned long next, now = jiffies;
1183 atomic_set(&neigh->probes,
1184 NEIGH_VAR(neigh->parms, UCAST_PROBES));
1185 neigh_del_timer(neigh);
1186 WRITE_ONCE(neigh->nud_state, NUD_INCOMPLETE);
1187 neigh->updated = now;
1188 if (!immediate_ok) {
1191 immediate_probe = true;
1192 next = now + max(NEIGH_VAR(neigh->parms,
1196 neigh_add_timer(neigh, next);
1198 WRITE_ONCE(neigh->nud_state, NUD_FAILED);
1199 neigh->updated = jiffies;
1200 write_unlock_bh(&neigh->lock);
1202 kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_FAILED);
1205 } else if (neigh->nud_state & NUD_STALE) {
1206 neigh_dbg(2, "neigh %p is delayed\n", neigh);
1207 neigh_del_timer(neigh);
1208 WRITE_ONCE(neigh->nud_state, NUD_DELAY);
1209 neigh->updated = jiffies;
1210 neigh_add_timer(neigh, jiffies +
1211 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
1214 if (neigh->nud_state == NUD_INCOMPLETE) {
1216 while (neigh->arp_queue_len_bytes + skb->truesize >
1217 NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
1218 struct sk_buff *buff;
1220 buff = __skb_dequeue(&neigh->arp_queue);
1223 neigh->arp_queue_len_bytes -= buff->truesize;
1224 kfree_skb_reason(buff, SKB_DROP_REASON_NEIGH_QUEUEFULL);
1225 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
1228 __skb_queue_tail(&neigh->arp_queue, skb);
1229 neigh->arp_queue_len_bytes += skb->truesize;
1234 if (immediate_probe)
1237 write_unlock(&neigh->lock);
1239 trace_neigh_event_send_done(neigh, rc);
1243 if (neigh->nud_state & NUD_STALE)
1245 write_unlock_bh(&neigh->lock);
1246 kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_DEAD);
1247 trace_neigh_event_send_dead(neigh, 1);
1250 EXPORT_SYMBOL(__neigh_event_send);
1252 static void neigh_update_hhs(struct neighbour *neigh)
1254 struct hh_cache *hh;
1255 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
1258 if (neigh->dev->header_ops)
1259 update = neigh->dev->header_ops->cache_update;
1263 if (READ_ONCE(hh->hh_len)) {
1264 write_seqlock_bh(&hh->hh_lock);
1265 update(hh, neigh->dev, neigh->ha);
1266 write_sequnlock_bh(&hh->hh_lock);
1271 /* Generic update routine.
1272 -- lladdr is new lladdr or NULL, if it is not supplied.
1273 -- new is new state.
1275 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1277 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
1278 lladdr instead of overriding it
1280 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
1281 NEIGH_UPDATE_F_USE means that the entry is user triggered.
1282 NEIGH_UPDATE_F_MANAGED means that the entry will be auto-refreshed.
1283 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
1285 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
1288 Caller MUST hold reference count on the entry.
1290 static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
1291 u8 new, u32 flags, u32 nlmsg_pid,
1292 struct netlink_ext_ack *extack)
1294 bool gc_update = false, managed_update = false;
1295 int update_isrouter = 0;
1296 struct net_device *dev;
1297 int err, notify = 0;
1300 trace_neigh_update(neigh, lladdr, new, flags, nlmsg_pid);
1302 write_lock_bh(&neigh->lock);
1305 old = neigh->nud_state;
1309 NL_SET_ERR_MSG(extack, "Neighbor entry is now dead");
1313 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
1314 (old & (NUD_NOARP | NUD_PERMANENT)))
1317 neigh_update_flags(neigh, flags, ¬ify, &gc_update, &managed_update);
1318 if (flags & (NEIGH_UPDATE_F_USE | NEIGH_UPDATE_F_MANAGED)) {
1319 new = old & ~NUD_PERMANENT;
1320 WRITE_ONCE(neigh->nud_state, new);
1325 if (!(new & NUD_VALID)) {
1326 neigh_del_timer(neigh);
1327 if (old & NUD_CONNECTED)
1328 neigh_suspect(neigh);
1329 WRITE_ONCE(neigh->nud_state, new);
1331 notify = old & NUD_VALID;
1332 if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
1333 (new & NUD_FAILED)) {
1334 neigh_invalidate(neigh);
1340 /* Compare new lladdr with cached one */
1341 if (!dev->addr_len) {
1342 /* First case: device needs no address. */
1344 } else if (lladdr) {
1345 /* The second case: if something is already cached
1346 and a new address is proposed:
1348 - if they are different, check override flag
1350 if ((old & NUD_VALID) &&
1351 !memcmp(lladdr, neigh->ha, dev->addr_len))
1354 /* No address is supplied; if we know something,
1355 use it, otherwise discard the request.
1358 if (!(old & NUD_VALID)) {
1359 NL_SET_ERR_MSG(extack, "No link layer address given");
1365 /* Update confirmed timestamp for neighbour entry after we
1366 * received ARP packet even if it doesn't change IP to MAC binding.
1368 if (new & NUD_CONNECTED)
1369 neigh->confirmed = jiffies;
1371 /* If entry was valid and address is not changed,
1372 do not change entry state, if new one is STALE.
1375 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1376 if (old & NUD_VALID) {
1377 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1378 update_isrouter = 0;
1379 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1380 (old & NUD_CONNECTED)) {
1386 if (lladdr == neigh->ha && new == NUD_STALE &&
1387 !(flags & NEIGH_UPDATE_F_ADMIN))
1392 /* Update timestamp only once we know we will make a change to the
1393 * neighbour entry. Otherwise we risk to move the locktime window with
1394 * noop updates and ignore relevant ARP updates.
1396 if (new != old || lladdr != neigh->ha)
1397 neigh->updated = jiffies;
1400 neigh_del_timer(neigh);
1401 if (new & NUD_PROBE)
1402 atomic_set(&neigh->probes, 0);
1403 if (new & NUD_IN_TIMER)
1404 neigh_add_timer(neigh, (jiffies +
1405 ((new & NUD_REACHABLE) ?
1406 neigh->parms->reachable_time :
1408 WRITE_ONCE(neigh->nud_state, new);
1412 if (lladdr != neigh->ha) {
1413 write_seqlock(&neigh->ha_lock);
1414 memcpy(&neigh->ha, lladdr, dev->addr_len);
1415 write_sequnlock(&neigh->ha_lock);
1416 neigh_update_hhs(neigh);
1417 if (!(new & NUD_CONNECTED))
1418 neigh->confirmed = jiffies -
1419 (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
1424 if (new & NUD_CONNECTED)
1425 neigh_connect(neigh);
1427 neigh_suspect(neigh);
1428 if (!(old & NUD_VALID)) {
1429 struct sk_buff *skb;
1431 /* Again: avoid dead loop if something went wrong */
1433 while (neigh->nud_state & NUD_VALID &&
1434 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1435 struct dst_entry *dst = skb_dst(skb);
1436 struct neighbour *n2, *n1 = neigh;
1437 write_unlock_bh(&neigh->lock);
1441 /* Why not just use 'neigh' as-is? The problem is that
1442 * things such as shaper, eql, and sch_teql can end up
1443 * using alternative, different, neigh objects to output
1444 * the packet in the output path. So what we need to do
1445 * here is re-lookup the top-level neigh in the path so
1446 * we can reinject the packet there.
1449 if (dst && dst->obsolete != DST_OBSOLETE_DEAD) {
1450 n2 = dst_neigh_lookup_skb(dst, skb);
1454 READ_ONCE(n1->output)(n1, skb);
1459 write_lock_bh(&neigh->lock);
1461 __skb_queue_purge(&neigh->arp_queue);
1462 neigh->arp_queue_len_bytes = 0;
1465 if (update_isrouter)
1466 neigh_update_is_router(neigh, flags, ¬ify);
1467 write_unlock_bh(&neigh->lock);
1468 if (((new ^ old) & NUD_PERMANENT) || gc_update)
1469 neigh_update_gc_list(neigh);
1471 neigh_update_managed_list(neigh);
1473 neigh_update_notify(neigh, nlmsg_pid);
1474 trace_neigh_update_done(neigh, err);
1478 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1479 u32 flags, u32 nlmsg_pid)
1481 return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL);
1483 EXPORT_SYMBOL(neigh_update);
1485 /* Update the neigh to listen temporarily for probe responses, even if it is
1486 * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
1488 void __neigh_set_probe_once(struct neighbour *neigh)
1492 neigh->updated = jiffies;
1493 if (!(neigh->nud_state & NUD_FAILED))
1495 WRITE_ONCE(neigh->nud_state, NUD_INCOMPLETE);
1496 atomic_set(&neigh->probes, neigh_max_probes(neigh));
1497 neigh_add_timer(neigh,
1498 jiffies + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1501 EXPORT_SYMBOL(__neigh_set_probe_once);
1503 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1504 u8 *lladdr, void *saddr,
1505 struct net_device *dev)
1507 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1508 lladdr || !dev->addr_len);
1510 neigh_update(neigh, lladdr, NUD_STALE,
1511 NEIGH_UPDATE_F_OVERRIDE, 0);
1514 EXPORT_SYMBOL(neigh_event_ns);
1516 /* called with read_lock_bh(&n->lock); */
1517 static void neigh_hh_init(struct neighbour *n)
1519 struct net_device *dev = n->dev;
1520 __be16 prot = n->tbl->protocol;
1521 struct hh_cache *hh = &n->hh;
1523 write_lock_bh(&n->lock);
1525 /* Only one thread can come in here and initialize the
1529 dev->header_ops->cache(n, hh, prot);
1531 write_unlock_bh(&n->lock);
1534 /* Slow and careful. */
1536 int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1540 if (!neigh_event_send(neigh, skb)) {
1542 struct net_device *dev = neigh->dev;
1545 if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len))
1546 neigh_hh_init(neigh);
1549 __skb_pull(skb, skb_network_offset(skb));
1550 seq = read_seqbegin(&neigh->ha_lock);
1551 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1552 neigh->ha, NULL, skb->len);
1553 } while (read_seqretry(&neigh->ha_lock, seq));
1556 rc = dev_queue_xmit(skb);
1567 EXPORT_SYMBOL(neigh_resolve_output);
1569 /* As fast as possible without hh cache */
1571 int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
1573 struct net_device *dev = neigh->dev;
1578 __skb_pull(skb, skb_network_offset(skb));
1579 seq = read_seqbegin(&neigh->ha_lock);
1580 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1581 neigh->ha, NULL, skb->len);
1582 } while (read_seqretry(&neigh->ha_lock, seq));
1585 err = dev_queue_xmit(skb);
1592 EXPORT_SYMBOL(neigh_connected_output);
1594 int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1596 return dev_queue_xmit(skb);
1598 EXPORT_SYMBOL(neigh_direct_output);
1600 static void neigh_managed_work(struct work_struct *work)
1602 struct neigh_table *tbl = container_of(work, struct neigh_table,
1604 struct neighbour *neigh;
1606 write_lock_bh(&tbl->lock);
1607 list_for_each_entry(neigh, &tbl->managed_list, managed_list)
1608 neigh_event_send_probe(neigh, NULL, false);
1609 queue_delayed_work(system_power_efficient_wq, &tbl->managed_work,
1610 NEIGH_VAR(&tbl->parms, INTERVAL_PROBE_TIME_MS));
1611 write_unlock_bh(&tbl->lock);
1614 static void neigh_proxy_process(struct timer_list *t)
1616 struct neigh_table *tbl = from_timer(tbl, t, proxy_timer);
1617 long sched_next = 0;
1618 unsigned long now = jiffies;
1619 struct sk_buff *skb, *n;
1621 spin_lock(&tbl->proxy_queue.lock);
1623 skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1624 long tdif = NEIGH_CB(skb)->sched_next - now;
1627 struct net_device *dev = skb->dev;
1629 neigh_parms_qlen_dec(dev, tbl->family);
1630 __skb_unlink(skb, &tbl->proxy_queue);
1632 if (tbl->proxy_redo && netif_running(dev)) {
1634 tbl->proxy_redo(skb);
1641 } else if (!sched_next || tdif < sched_next)
1644 del_timer(&tbl->proxy_timer);
1646 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1647 spin_unlock(&tbl->proxy_queue.lock);
1650 static unsigned long neigh_proxy_delay(struct neigh_parms *p)
1652 /* If proxy_delay is zero, do not call get_random_u32_below()
1653 * as it is undefined behavior.
1655 unsigned long proxy_delay = NEIGH_VAR(p, PROXY_DELAY);
1657 return proxy_delay ?
1658 jiffies + get_random_u32_below(proxy_delay) : jiffies;
1661 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1662 struct sk_buff *skb)
1664 unsigned long sched_next = neigh_proxy_delay(p);
1666 if (p->qlen > NEIGH_VAR(p, PROXY_QLEN)) {
1671 NEIGH_CB(skb)->sched_next = sched_next;
1672 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1674 spin_lock(&tbl->proxy_queue.lock);
1675 if (del_timer(&tbl->proxy_timer)) {
1676 if (time_before(tbl->proxy_timer.expires, sched_next))
1677 sched_next = tbl->proxy_timer.expires;
1681 __skb_queue_tail(&tbl->proxy_queue, skb);
1683 mod_timer(&tbl->proxy_timer, sched_next);
1684 spin_unlock(&tbl->proxy_queue.lock);
1686 EXPORT_SYMBOL(pneigh_enqueue);
1688 static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1689 struct net *net, int ifindex)
1691 struct neigh_parms *p;
1693 list_for_each_entry(p, &tbl->parms_list, list) {
1694 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1695 (!p->dev && !ifindex && net_eq(net, &init_net)))
1702 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1703 struct neigh_table *tbl)
1705 struct neigh_parms *p;
1706 struct net *net = dev_net(dev);
1707 const struct net_device_ops *ops = dev->netdev_ops;
1709 p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
1712 refcount_set(&p->refcnt, 1);
1714 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1716 netdev_hold(dev, &p->dev_tracker, GFP_KERNEL);
1718 write_pnet(&p->net, net);
1719 p->sysctl_table = NULL;
1721 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
1722 netdev_put(dev, &p->dev_tracker);
1727 write_lock_bh(&tbl->lock);
1728 list_add(&p->list, &tbl->parms.list);
1729 write_unlock_bh(&tbl->lock);
1731 neigh_parms_data_state_cleanall(p);
1735 EXPORT_SYMBOL(neigh_parms_alloc);
1737 static void neigh_rcu_free_parms(struct rcu_head *head)
1739 struct neigh_parms *parms =
1740 container_of(head, struct neigh_parms, rcu_head);
1742 neigh_parms_put(parms);
1745 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1747 if (!parms || parms == &tbl->parms)
1749 write_lock_bh(&tbl->lock);
1750 list_del(&parms->list);
1752 write_unlock_bh(&tbl->lock);
1753 netdev_put(parms->dev, &parms->dev_tracker);
1754 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1756 EXPORT_SYMBOL(neigh_parms_release);
1758 static void neigh_parms_destroy(struct neigh_parms *parms)
1763 static struct lock_class_key neigh_table_proxy_queue_class;
1765 static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1767 void neigh_table_init(int index, struct neigh_table *tbl)
1769 unsigned long now = jiffies;
1770 unsigned long phsize;
1772 INIT_LIST_HEAD(&tbl->parms_list);
1773 INIT_LIST_HEAD(&tbl->gc_list);
1774 INIT_LIST_HEAD(&tbl->managed_list);
1776 list_add(&tbl->parms.list, &tbl->parms_list);
1777 write_pnet(&tbl->parms.net, &init_net);
1778 refcount_set(&tbl->parms.refcnt, 1);
1779 tbl->parms.reachable_time =
1780 neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
1781 tbl->parms.qlen = 0;
1783 tbl->stats = alloc_percpu(struct neigh_statistics);
1785 panic("cannot create neighbour cache statistics");
1787 #ifdef CONFIG_PROC_FS
1788 if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
1789 &neigh_stat_seq_ops, tbl))
1790 panic("cannot create neighbour proc dir entry");
1793 RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
1795 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1796 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
1798 if (!tbl->nht || !tbl->phash_buckets)
1799 panic("cannot allocate neighbour cache hashes");
1801 if (!tbl->entry_size)
1802 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
1803 tbl->key_len, NEIGH_PRIV_ALIGN);
1805 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
1807 rwlock_init(&tbl->lock);
1809 INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1810 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1811 tbl->parms.reachable_time);
1812 INIT_DEFERRABLE_WORK(&tbl->managed_work, neigh_managed_work);
1813 queue_delayed_work(system_power_efficient_wq, &tbl->managed_work, 0);
1815 timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
1816 skb_queue_head_init_class(&tbl->proxy_queue,
1817 &neigh_table_proxy_queue_class);
1819 tbl->last_flush = now;
1820 tbl->last_rand = now + tbl->parms.reachable_time * 20;
1822 neigh_tables[index] = tbl;
1824 EXPORT_SYMBOL(neigh_table_init);
1826 int neigh_table_clear(int index, struct neigh_table *tbl)
1828 neigh_tables[index] = NULL;
1829 /* It is not clean... Fix it to unload IPv6 module safely */
1830 cancel_delayed_work_sync(&tbl->managed_work);
1831 cancel_delayed_work_sync(&tbl->gc_work);
1832 del_timer_sync(&tbl->proxy_timer);
1833 pneigh_queue_purge(&tbl->proxy_queue, NULL, tbl->family);
1834 neigh_ifdown(tbl, NULL);
1835 if (atomic_read(&tbl->entries))
1836 pr_crit("neighbour leakage\n");
1838 call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1839 neigh_hash_free_rcu);
1842 kfree(tbl->phash_buckets);
1843 tbl->phash_buckets = NULL;
1845 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1847 free_percpu(tbl->stats);
1852 EXPORT_SYMBOL(neigh_table_clear);
1854 static struct neigh_table *neigh_find_table(int family)
1856 struct neigh_table *tbl = NULL;
1860 tbl = neigh_tables[NEIGH_ARP_TABLE];
1863 tbl = neigh_tables[NEIGH_ND_TABLE];
1870 const struct nla_policy nda_policy[NDA_MAX+1] = {
1871 [NDA_UNSPEC] = { .strict_start_type = NDA_NH_ID },
1872 [NDA_DST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1873 [NDA_LLADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1874 [NDA_CACHEINFO] = { .len = sizeof(struct nda_cacheinfo) },
1875 [NDA_PROBES] = { .type = NLA_U32 },
1876 [NDA_VLAN] = { .type = NLA_U16 },
1877 [NDA_PORT] = { .type = NLA_U16 },
1878 [NDA_VNI] = { .type = NLA_U32 },
1879 [NDA_IFINDEX] = { .type = NLA_U32 },
1880 [NDA_MASTER] = { .type = NLA_U32 },
1881 [NDA_PROTOCOL] = { .type = NLA_U8 },
1882 [NDA_NH_ID] = { .type = NLA_U32 },
1883 [NDA_FLAGS_EXT] = NLA_POLICY_MASK(NLA_U32, NTF_EXT_MASK),
1884 [NDA_FDB_EXT_ATTRS] = { .type = NLA_NESTED },
1887 static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1888 struct netlink_ext_ack *extack)
1890 struct net *net = sock_net(skb->sk);
1892 struct nlattr *dst_attr;
1893 struct neigh_table *tbl;
1894 struct neighbour *neigh;
1895 struct net_device *dev = NULL;
1899 if (nlmsg_len(nlh) < sizeof(*ndm))
1902 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1904 NL_SET_ERR_MSG(extack, "Network address not specified");
1908 ndm = nlmsg_data(nlh);
1909 if (ndm->ndm_ifindex) {
1910 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1917 tbl = neigh_find_table(ndm->ndm_family);
1919 return -EAFNOSUPPORT;
1921 if (nla_len(dst_attr) < (int)tbl->key_len) {
1922 NL_SET_ERR_MSG(extack, "Invalid network address");
1926 if (ndm->ndm_flags & NTF_PROXY) {
1927 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1934 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1935 if (neigh == NULL) {
1940 err = __neigh_update(neigh, NULL, NUD_FAILED,
1941 NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN,
1942 NETLINK_CB(skb).portid, extack);
1943 write_lock_bh(&tbl->lock);
1944 neigh_release(neigh);
1945 neigh_remove_one(neigh, tbl);
1946 write_unlock_bh(&tbl->lock);
1952 static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1953 struct netlink_ext_ack *extack)
1955 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
1956 NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1957 struct net *net = sock_net(skb->sk);
1959 struct nlattr *tb[NDA_MAX+1];
1960 struct neigh_table *tbl;
1961 struct net_device *dev = NULL;
1962 struct neighbour *neigh;
1969 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
1970 nda_policy, extack);
1976 NL_SET_ERR_MSG(extack, "Network address not specified");
1980 ndm = nlmsg_data(nlh);
1981 ndm_flags = ndm->ndm_flags;
1982 if (tb[NDA_FLAGS_EXT]) {
1983 u32 ext = nla_get_u32(tb[NDA_FLAGS_EXT]);
1985 BUILD_BUG_ON(sizeof(neigh->flags) * BITS_PER_BYTE <
1986 (sizeof(ndm->ndm_flags) * BITS_PER_BYTE +
1987 hweight32(NTF_EXT_MASK)));
1988 ndm_flags |= (ext << NTF_EXT_SHIFT);
1990 if (ndm->ndm_ifindex) {
1991 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1997 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) {
1998 NL_SET_ERR_MSG(extack, "Invalid link address");
2003 tbl = neigh_find_table(ndm->ndm_family);
2005 return -EAFNOSUPPORT;
2007 if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) {
2008 NL_SET_ERR_MSG(extack, "Invalid network address");
2012 dst = nla_data(tb[NDA_DST]);
2013 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
2015 if (tb[NDA_PROTOCOL])
2016 protocol = nla_get_u8(tb[NDA_PROTOCOL]);
2017 if (ndm_flags & NTF_PROXY) {
2018 struct pneigh_entry *pn;
2020 if (ndm_flags & NTF_MANAGED) {
2021 NL_SET_ERR_MSG(extack, "Invalid NTF_* flag combination");
2026 pn = pneigh_lookup(tbl, net, dst, dev, 1);
2028 pn->flags = ndm_flags;
2030 pn->protocol = protocol;
2037 NL_SET_ERR_MSG(extack, "Device not specified");
2041 if (tbl->allow_add && !tbl->allow_add(dev, extack)) {
2046 neigh = neigh_lookup(tbl, dst, dev);
2047 if (neigh == NULL) {
2048 bool ndm_permanent = ndm->ndm_state & NUD_PERMANENT;
2049 bool exempt_from_gc = ndm_permanent ||
2050 ndm_flags & NTF_EXT_LEARNED;
2052 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2056 if (ndm_permanent && (ndm_flags & NTF_MANAGED)) {
2057 NL_SET_ERR_MSG(extack, "Invalid NTF_* flag for permanent entry");
2062 neigh = ___neigh_create(tbl, dst, dev,
2064 (NTF_EXT_LEARNED | NTF_MANAGED),
2065 exempt_from_gc, true);
2066 if (IS_ERR(neigh)) {
2067 err = PTR_ERR(neigh);
2071 if (nlh->nlmsg_flags & NLM_F_EXCL) {
2073 neigh_release(neigh);
2077 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
2078 flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
2079 NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
2083 neigh->protocol = protocol;
2084 if (ndm_flags & NTF_EXT_LEARNED)
2085 flags |= NEIGH_UPDATE_F_EXT_LEARNED;
2086 if (ndm_flags & NTF_ROUTER)
2087 flags |= NEIGH_UPDATE_F_ISROUTER;
2088 if (ndm_flags & NTF_MANAGED)
2089 flags |= NEIGH_UPDATE_F_MANAGED;
2090 if (ndm_flags & NTF_USE)
2091 flags |= NEIGH_UPDATE_F_USE;
2093 err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags,
2094 NETLINK_CB(skb).portid, extack);
2095 if (!err && ndm_flags & (NTF_USE | NTF_MANAGED)) {
2096 neigh_event_send(neigh, NULL);
2099 neigh_release(neigh);
2104 static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
2106 struct nlattr *nest;
2108 nest = nla_nest_start_noflag(skb, NDTA_PARMS);
2113 nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
2114 nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
2115 nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
2116 NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
2117 /* approximative value for deprecated QUEUE_LEN (in packets) */
2118 nla_put_u32(skb, NDTPA_QUEUE_LEN,
2119 NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
2120 nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
2121 nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
2122 nla_put_u32(skb, NDTPA_UCAST_PROBES,
2123 NEIGH_VAR(parms, UCAST_PROBES)) ||
2124 nla_put_u32(skb, NDTPA_MCAST_PROBES,
2125 NEIGH_VAR(parms, MCAST_PROBES)) ||
2126 nla_put_u32(skb, NDTPA_MCAST_REPROBES,
2127 NEIGH_VAR(parms, MCAST_REPROBES)) ||
2128 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
2130 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
2131 NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
2132 nla_put_msecs(skb, NDTPA_GC_STALETIME,
2133 NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
2134 nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
2135 NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
2136 nla_put_msecs(skb, NDTPA_RETRANS_TIME,
2137 NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
2138 nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
2139 NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
2140 nla_put_msecs(skb, NDTPA_PROXY_DELAY,
2141 NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
2142 nla_put_msecs(skb, NDTPA_LOCKTIME,
2143 NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD) ||
2144 nla_put_msecs(skb, NDTPA_INTERVAL_PROBE_TIME_MS,
2145 NEIGH_VAR(parms, INTERVAL_PROBE_TIME_MS), NDTPA_PAD))
2146 goto nla_put_failure;
2147 return nla_nest_end(skb, nest);
2150 nla_nest_cancel(skb, nest);
2154 static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
2155 u32 pid, u32 seq, int type, int flags)
2157 struct nlmsghdr *nlh;
2158 struct ndtmsg *ndtmsg;
2160 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2164 ndtmsg = nlmsg_data(nlh);
2166 read_lock_bh(&tbl->lock);
2167 ndtmsg->ndtm_family = tbl->family;
2168 ndtmsg->ndtm_pad1 = 0;
2169 ndtmsg->ndtm_pad2 = 0;
2171 if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
2172 nla_put_msecs(skb, NDTA_GC_INTERVAL, READ_ONCE(tbl->gc_interval),
2174 nla_put_u32(skb, NDTA_THRESH1, READ_ONCE(tbl->gc_thresh1)) ||
2175 nla_put_u32(skb, NDTA_THRESH2, READ_ONCE(tbl->gc_thresh2)) ||
2176 nla_put_u32(skb, NDTA_THRESH3, READ_ONCE(tbl->gc_thresh3)))
2177 goto nla_put_failure;
2179 unsigned long now = jiffies;
2180 long flush_delta = now - READ_ONCE(tbl->last_flush);
2181 long rand_delta = now - READ_ONCE(tbl->last_rand);
2182 struct neigh_hash_table *nht;
2183 struct ndt_config ndc = {
2184 .ndtc_key_len = tbl->key_len,
2185 .ndtc_entry_size = tbl->entry_size,
2186 .ndtc_entries = atomic_read(&tbl->entries),
2187 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
2188 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
2189 .ndtc_proxy_qlen = READ_ONCE(tbl->proxy_queue.qlen),
2193 nht = rcu_dereference(tbl->nht);
2194 ndc.ndtc_hash_rnd = nht->hash_rnd[0];
2195 ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
2198 if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
2199 goto nla_put_failure;
2204 struct ndt_stats ndst;
2206 memset(&ndst, 0, sizeof(ndst));
2208 for_each_possible_cpu(cpu) {
2209 struct neigh_statistics *st;
2211 st = per_cpu_ptr(tbl->stats, cpu);
2212 ndst.ndts_allocs += READ_ONCE(st->allocs);
2213 ndst.ndts_destroys += READ_ONCE(st->destroys);
2214 ndst.ndts_hash_grows += READ_ONCE(st->hash_grows);
2215 ndst.ndts_res_failed += READ_ONCE(st->res_failed);
2216 ndst.ndts_lookups += READ_ONCE(st->lookups);
2217 ndst.ndts_hits += READ_ONCE(st->hits);
2218 ndst.ndts_rcv_probes_mcast += READ_ONCE(st->rcv_probes_mcast);
2219 ndst.ndts_rcv_probes_ucast += READ_ONCE(st->rcv_probes_ucast);
2220 ndst.ndts_periodic_gc_runs += READ_ONCE(st->periodic_gc_runs);
2221 ndst.ndts_forced_gc_runs += READ_ONCE(st->forced_gc_runs);
2222 ndst.ndts_table_fulls += READ_ONCE(st->table_fulls);
2225 if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
2227 goto nla_put_failure;
2230 BUG_ON(tbl->parms.dev);
2231 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
2232 goto nla_put_failure;
2234 read_unlock_bh(&tbl->lock);
2235 nlmsg_end(skb, nlh);
2239 read_unlock_bh(&tbl->lock);
2240 nlmsg_cancel(skb, nlh);
2244 static int neightbl_fill_param_info(struct sk_buff *skb,
2245 struct neigh_table *tbl,
2246 struct neigh_parms *parms,
2247 u32 pid, u32 seq, int type,
2250 struct ndtmsg *ndtmsg;
2251 struct nlmsghdr *nlh;
2253 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
2257 ndtmsg = nlmsg_data(nlh);
2259 read_lock_bh(&tbl->lock);
2260 ndtmsg->ndtm_family = tbl->family;
2261 ndtmsg->ndtm_pad1 = 0;
2262 ndtmsg->ndtm_pad2 = 0;
2264 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
2265 neightbl_fill_parms(skb, parms) < 0)
2268 read_unlock_bh(&tbl->lock);
2269 nlmsg_end(skb, nlh);
2272 read_unlock_bh(&tbl->lock);
2273 nlmsg_cancel(skb, nlh);
2277 static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
2278 [NDTA_NAME] = { .type = NLA_STRING },
2279 [NDTA_THRESH1] = { .type = NLA_U32 },
2280 [NDTA_THRESH2] = { .type = NLA_U32 },
2281 [NDTA_THRESH3] = { .type = NLA_U32 },
2282 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
2283 [NDTA_PARMS] = { .type = NLA_NESTED },
2286 static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
2287 [NDTPA_IFINDEX] = { .type = NLA_U32 },
2288 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
2289 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
2290 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
2291 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
2292 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
2293 [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
2294 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
2295 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
2296 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
2297 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
2298 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
2299 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
2300 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
2301 [NDTPA_INTERVAL_PROBE_TIME_MS] = { .type = NLA_U64, .min = 1 },
2304 static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
2305 struct netlink_ext_ack *extack)
2307 struct net *net = sock_net(skb->sk);
2308 struct neigh_table *tbl;
2309 struct ndtmsg *ndtmsg;
2310 struct nlattr *tb[NDTA_MAX+1];
2314 err = nlmsg_parse_deprecated(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
2315 nl_neightbl_policy, extack);
2319 if (tb[NDTA_NAME] == NULL) {
2324 ndtmsg = nlmsg_data(nlh);
2326 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2327 tbl = neigh_tables[tidx];
2330 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2332 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2342 * We acquire tbl->lock to be nice to the periodic timers and
2343 * make sure they always see a consistent set of values.
2345 write_lock_bh(&tbl->lock);
2347 if (tb[NDTA_PARMS]) {
2348 struct nlattr *tbp[NDTPA_MAX+1];
2349 struct neigh_parms *p;
2352 err = nla_parse_nested_deprecated(tbp, NDTPA_MAX,
2354 nl_ntbl_parm_policy, extack);
2356 goto errout_tbl_lock;
2358 if (tbp[NDTPA_IFINDEX])
2359 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2361 p = lookup_neigh_parms(tbl, net, ifindex);
2364 goto errout_tbl_lock;
2367 for (i = 1; i <= NDTPA_MAX; i++) {
2372 case NDTPA_QUEUE_LEN:
2373 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2374 nla_get_u32(tbp[i]) *
2375 SKB_TRUESIZE(ETH_FRAME_LEN));
2377 case NDTPA_QUEUE_LENBYTES:
2378 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2379 nla_get_u32(tbp[i]));
2381 case NDTPA_PROXY_QLEN:
2382 NEIGH_VAR_SET(p, PROXY_QLEN,
2383 nla_get_u32(tbp[i]));
2385 case NDTPA_APP_PROBES:
2386 NEIGH_VAR_SET(p, APP_PROBES,
2387 nla_get_u32(tbp[i]));
2389 case NDTPA_UCAST_PROBES:
2390 NEIGH_VAR_SET(p, UCAST_PROBES,
2391 nla_get_u32(tbp[i]));
2393 case NDTPA_MCAST_PROBES:
2394 NEIGH_VAR_SET(p, MCAST_PROBES,
2395 nla_get_u32(tbp[i]));
2397 case NDTPA_MCAST_REPROBES:
2398 NEIGH_VAR_SET(p, MCAST_REPROBES,
2399 nla_get_u32(tbp[i]));
2401 case NDTPA_BASE_REACHABLE_TIME:
2402 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2403 nla_get_msecs(tbp[i]));
2404 /* update reachable_time as well, otherwise, the change will
2405 * only be effective after the next time neigh_periodic_work
2406 * decides to recompute it (can be multiple minutes)
2409 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
2411 case NDTPA_GC_STALETIME:
2412 NEIGH_VAR_SET(p, GC_STALETIME,
2413 nla_get_msecs(tbp[i]));
2415 case NDTPA_DELAY_PROBE_TIME:
2416 NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
2417 nla_get_msecs(tbp[i]));
2418 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
2420 case NDTPA_INTERVAL_PROBE_TIME_MS:
2421 NEIGH_VAR_SET(p, INTERVAL_PROBE_TIME_MS,
2422 nla_get_msecs(tbp[i]));
2424 case NDTPA_RETRANS_TIME:
2425 NEIGH_VAR_SET(p, RETRANS_TIME,
2426 nla_get_msecs(tbp[i]));
2428 case NDTPA_ANYCAST_DELAY:
2429 NEIGH_VAR_SET(p, ANYCAST_DELAY,
2430 nla_get_msecs(tbp[i]));
2432 case NDTPA_PROXY_DELAY:
2433 NEIGH_VAR_SET(p, PROXY_DELAY,
2434 nla_get_msecs(tbp[i]));
2436 case NDTPA_LOCKTIME:
2437 NEIGH_VAR_SET(p, LOCKTIME,
2438 nla_get_msecs(tbp[i]));
2445 if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2446 tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2447 !net_eq(net, &init_net))
2448 goto errout_tbl_lock;
2450 if (tb[NDTA_THRESH1])
2451 WRITE_ONCE(tbl->gc_thresh1, nla_get_u32(tb[NDTA_THRESH1]));
2453 if (tb[NDTA_THRESH2])
2454 WRITE_ONCE(tbl->gc_thresh2, nla_get_u32(tb[NDTA_THRESH2]));
2456 if (tb[NDTA_THRESH3])
2457 WRITE_ONCE(tbl->gc_thresh3, nla_get_u32(tb[NDTA_THRESH3]));
2459 if (tb[NDTA_GC_INTERVAL])
2460 WRITE_ONCE(tbl->gc_interval, nla_get_msecs(tb[NDTA_GC_INTERVAL]));
2465 write_unlock_bh(&tbl->lock);
2470 static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
2471 struct netlink_ext_ack *extack)
2473 struct ndtmsg *ndtm;
2475 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndtm))) {
2476 NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
2480 ndtm = nlmsg_data(nlh);
2481 if (ndtm->ndtm_pad1 || ndtm->ndtm_pad2) {
2482 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
2486 if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
2487 NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
2494 static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2496 const struct nlmsghdr *nlh = cb->nlh;
2497 struct net *net = sock_net(skb->sk);
2498 int family, tidx, nidx = 0;
2499 int tbl_skip = cb->args[0];
2500 int neigh_skip = cb->args[1];
2501 struct neigh_table *tbl;
2503 if (cb->strict_check) {
2504 int err = neightbl_valid_dump_info(nlh, cb->extack);
2510 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
2512 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2513 struct neigh_parms *p;
2515 tbl = neigh_tables[tidx];
2519 if (tidx < tbl_skip || (family && tbl->family != family))
2522 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2523 nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2528 p = list_next_entry(&tbl->parms, list);
2529 list_for_each_entry_from(p, &tbl->parms_list, list) {
2530 if (!net_eq(neigh_parms_net(p), net))
2533 if (nidx < neigh_skip)
2536 if (neightbl_fill_param_info(skb, tbl, p,
2537 NETLINK_CB(cb->skb).portid,
2555 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2556 u32 pid, u32 seq, int type, unsigned int flags)
2558 u32 neigh_flags, neigh_flags_ext;
2559 unsigned long now = jiffies;
2560 struct nda_cacheinfo ci;
2561 struct nlmsghdr *nlh;
2564 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2568 neigh_flags_ext = neigh->flags >> NTF_EXT_SHIFT;
2569 neigh_flags = neigh->flags & NTF_OLD_MASK;
2571 ndm = nlmsg_data(nlh);
2572 ndm->ndm_family = neigh->ops->family;
2575 ndm->ndm_flags = neigh_flags;
2576 ndm->ndm_type = neigh->type;
2577 ndm->ndm_ifindex = neigh->dev->ifindex;
2579 if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
2580 goto nla_put_failure;
2582 read_lock_bh(&neigh->lock);
2583 ndm->ndm_state = neigh->nud_state;
2584 if (neigh->nud_state & NUD_VALID) {
2585 char haddr[MAX_ADDR_LEN];
2587 neigh_ha_snapshot(haddr, neigh, neigh->dev);
2588 if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2589 read_unlock_bh(&neigh->lock);
2590 goto nla_put_failure;
2594 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2595 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2596 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
2597 ci.ndm_refcnt = refcount_read(&neigh->refcnt) - 1;
2598 read_unlock_bh(&neigh->lock);
2600 if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
2601 nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
2602 goto nla_put_failure;
2604 if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol))
2605 goto nla_put_failure;
2606 if (neigh_flags_ext && nla_put_u32(skb, NDA_FLAGS_EXT, neigh_flags_ext))
2607 goto nla_put_failure;
2609 nlmsg_end(skb, nlh);
2613 nlmsg_cancel(skb, nlh);
2617 static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
2618 u32 pid, u32 seq, int type, unsigned int flags,
2619 struct neigh_table *tbl)
2621 u32 neigh_flags, neigh_flags_ext;
2622 struct nlmsghdr *nlh;
2625 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2629 neigh_flags_ext = pn->flags >> NTF_EXT_SHIFT;
2630 neigh_flags = pn->flags & NTF_OLD_MASK;
2632 ndm = nlmsg_data(nlh);
2633 ndm->ndm_family = tbl->family;
2636 ndm->ndm_flags = neigh_flags | NTF_PROXY;
2637 ndm->ndm_type = RTN_UNICAST;
2638 ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
2639 ndm->ndm_state = NUD_NONE;
2641 if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
2642 goto nla_put_failure;
2644 if (pn->protocol && nla_put_u8(skb, NDA_PROTOCOL, pn->protocol))
2645 goto nla_put_failure;
2646 if (neigh_flags_ext && nla_put_u32(skb, NDA_FLAGS_EXT, neigh_flags_ext))
2647 goto nla_put_failure;
2649 nlmsg_end(skb, nlh);
2653 nlmsg_cancel(skb, nlh);
2657 static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
2659 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2660 __neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
2663 static bool neigh_master_filtered(struct net_device *dev, int master_idx)
2665 struct net_device *master;
2670 master = dev ? netdev_master_upper_dev_get(dev) : NULL;
2672 /* 0 is already used to denote NDA_MASTER wasn't passed, therefore need another
2673 * invalid value for ifindex to denote "no master".
2675 if (master_idx == -1)
2678 if (!master || master->ifindex != master_idx)
2684 static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
2686 if (filter_idx && (!dev || dev->ifindex != filter_idx))
2692 struct neigh_dump_filter {
2697 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2698 struct netlink_callback *cb,
2699 struct neigh_dump_filter *filter)
2701 struct net *net = sock_net(skb->sk);
2702 struct neighbour *n;
2703 int rc, h, s_h = cb->args[1];
2704 int idx, s_idx = idx = cb->args[2];
2705 struct neigh_hash_table *nht;
2706 unsigned int flags = NLM_F_MULTI;
2708 if (filter->dev_idx || filter->master_idx)
2709 flags |= NLM_F_DUMP_FILTERED;
2712 nht = rcu_dereference(tbl->nht);
2714 for (h = s_h; h < (1 << nht->hash_shift); h++) {
2717 for (n = rcu_dereference(nht->hash_buckets[h]), idx = 0;
2719 n = rcu_dereference(n->next)) {
2720 if (idx < s_idx || !net_eq(dev_net(n->dev), net))
2722 if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2723 neigh_master_filtered(n->dev, filter->master_idx))
2725 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2744 static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2745 struct netlink_callback *cb,
2746 struct neigh_dump_filter *filter)
2748 struct pneigh_entry *n;
2749 struct net *net = sock_net(skb->sk);
2750 int rc, h, s_h = cb->args[3];
2751 int idx, s_idx = idx = cb->args[4];
2752 unsigned int flags = NLM_F_MULTI;
2754 if (filter->dev_idx || filter->master_idx)
2755 flags |= NLM_F_DUMP_FILTERED;
2757 read_lock_bh(&tbl->lock);
2759 for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
2762 for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
2763 if (idx < s_idx || pneigh_net(n) != net)
2765 if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
2766 neigh_master_filtered(n->dev, filter->master_idx))
2768 if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2770 RTM_NEWNEIGH, flags, tbl) < 0) {
2771 read_unlock_bh(&tbl->lock);
2780 read_unlock_bh(&tbl->lock);
2789 static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
2791 struct neigh_dump_filter *filter,
2792 struct netlink_ext_ack *extack)
2794 struct nlattr *tb[NDA_MAX + 1];
2800 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
2801 NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
2805 ndm = nlmsg_data(nlh);
2806 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_ifindex ||
2807 ndm->ndm_state || ndm->ndm_type) {
2808 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
2812 if (ndm->ndm_flags & ~NTF_PROXY) {
2813 NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor dump request");
2817 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg),
2818 tb, NDA_MAX, nda_policy,
2821 err = nlmsg_parse_deprecated(nlh, sizeof(struct ndmsg), tb,
2822 NDA_MAX, nda_policy, extack);
2827 for (i = 0; i <= NDA_MAX; ++i) {
2831 /* all new attributes should require strict_check */
2834 filter->dev_idx = nla_get_u32(tb[i]);
2837 filter->master_idx = nla_get_u32(tb[i]);
2841 NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
2850 static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2852 const struct nlmsghdr *nlh = cb->nlh;
2853 struct neigh_dump_filter filter = {};
2854 struct neigh_table *tbl;
2859 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
2861 /* check for full ndmsg structure presence, family member is
2862 * the same for both structures
2864 if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
2865 ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
2868 err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
2869 if (err < 0 && cb->strict_check)
2874 for (t = 0; t < NEIGH_NR_TABLES; t++) {
2875 tbl = neigh_tables[t];
2879 if (t < s_t || (family && tbl->family != family))
2882 memset(&cb->args[1], 0, sizeof(cb->args) -
2883 sizeof(cb->args[0]));
2885 err = pneigh_dump_table(tbl, skb, cb, &filter);
2887 err = neigh_dump_table(tbl, skb, cb, &filter);
2896 static int neigh_valid_get_req(const struct nlmsghdr *nlh,
2897 struct neigh_table **tbl,
2898 void **dst, int *dev_idx, u8 *ndm_flags,
2899 struct netlink_ext_ack *extack)
2901 struct nlattr *tb[NDA_MAX + 1];
2905 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
2906 NL_SET_ERR_MSG(extack, "Invalid header for neighbor get request");
2910 ndm = nlmsg_data(nlh);
2911 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
2913 NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor get request");
2917 if (ndm->ndm_flags & ~NTF_PROXY) {
2918 NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor get request");
2922 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
2923 NDA_MAX, nda_policy, extack);
2927 *ndm_flags = ndm->ndm_flags;
2928 *dev_idx = ndm->ndm_ifindex;
2929 *tbl = neigh_find_table(ndm->ndm_family);
2931 NL_SET_ERR_MSG(extack, "Unsupported family in header for neighbor get request");
2932 return -EAFNOSUPPORT;
2935 for (i = 0; i <= NDA_MAX; ++i) {
2941 if (nla_len(tb[i]) != (int)(*tbl)->key_len) {
2942 NL_SET_ERR_MSG(extack, "Invalid network address in neighbor get request");
2945 *dst = nla_data(tb[i]);
2948 NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor get request");
2956 static inline size_t neigh_nlmsg_size(void)
2958 return NLMSG_ALIGN(sizeof(struct ndmsg))
2959 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2960 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2961 + nla_total_size(sizeof(struct nda_cacheinfo))
2962 + nla_total_size(4) /* NDA_PROBES */
2963 + nla_total_size(4) /* NDA_FLAGS_EXT */
2964 + nla_total_size(1); /* NDA_PROTOCOL */
2967 static int neigh_get_reply(struct net *net, struct neighbour *neigh,
2970 struct sk_buff *skb;
2973 skb = nlmsg_new(neigh_nlmsg_size(), GFP_KERNEL);
2977 err = neigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0);
2983 err = rtnl_unicast(skb, net, pid);
2988 static inline size_t pneigh_nlmsg_size(void)
2990 return NLMSG_ALIGN(sizeof(struct ndmsg))
2991 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2992 + nla_total_size(4) /* NDA_FLAGS_EXT */
2993 + nla_total_size(1); /* NDA_PROTOCOL */
2996 static int pneigh_get_reply(struct net *net, struct pneigh_entry *neigh,
2997 u32 pid, u32 seq, struct neigh_table *tbl)
2999 struct sk_buff *skb;
3002 skb = nlmsg_new(pneigh_nlmsg_size(), GFP_KERNEL);
3006 err = pneigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0, tbl);
3012 err = rtnl_unicast(skb, net, pid);
3017 static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
3018 struct netlink_ext_ack *extack)
3020 struct net *net = sock_net(in_skb->sk);
3021 struct net_device *dev = NULL;
3022 struct neigh_table *tbl = NULL;
3023 struct neighbour *neigh;
3029 err = neigh_valid_get_req(nlh, &tbl, &dst, &dev_idx, &ndm_flags,
3035 dev = __dev_get_by_index(net, dev_idx);
3037 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
3043 NL_SET_ERR_MSG(extack, "Network address not specified");
3047 if (ndm_flags & NTF_PROXY) {
3048 struct pneigh_entry *pn;
3050 pn = pneigh_lookup(tbl, net, dst, dev, 0);
3052 NL_SET_ERR_MSG(extack, "Proxy neighbour entry not found");
3055 return pneigh_get_reply(net, pn, NETLINK_CB(in_skb).portid,
3056 nlh->nlmsg_seq, tbl);
3060 NL_SET_ERR_MSG(extack, "No device specified");
3064 neigh = neigh_lookup(tbl, dst, dev);
3066 NL_SET_ERR_MSG(extack, "Neighbour entry not found");
3070 err = neigh_get_reply(net, neigh, NETLINK_CB(in_skb).portid,
3073 neigh_release(neigh);
3078 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
3081 struct neigh_hash_table *nht;
3084 nht = rcu_dereference(tbl->nht);
3086 read_lock_bh(&tbl->lock); /* avoid resizes */
3087 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
3088 struct neighbour *n;
3090 for (n = rcu_dereference(nht->hash_buckets[chain]);
3092 n = rcu_dereference(n->next))
3095 read_unlock_bh(&tbl->lock);
3098 EXPORT_SYMBOL(neigh_for_each);
3100 /* The tbl->lock must be held as a writer and BH disabled. */
3101 void __neigh_for_each_release(struct neigh_table *tbl,
3102 int (*cb)(struct neighbour *))
3105 struct neigh_hash_table *nht;
3107 nht = rcu_dereference_protected(tbl->nht,
3108 lockdep_is_held(&tbl->lock));
3109 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
3110 struct neighbour *n;
3111 struct neighbour __rcu **np;
3113 np = &nht->hash_buckets[chain];
3114 while ((n = rcu_dereference_protected(*np,
3115 lockdep_is_held(&tbl->lock))) != NULL) {
3118 write_lock(&n->lock);
3121 rcu_assign_pointer(*np,
3122 rcu_dereference_protected(n->next,
3123 lockdep_is_held(&tbl->lock)));
3127 write_unlock(&n->lock);
3129 neigh_cleanup_and_release(n);
3133 EXPORT_SYMBOL(__neigh_for_each_release);
3135 int neigh_xmit(int index, struct net_device *dev,
3136 const void *addr, struct sk_buff *skb)
3138 int err = -EAFNOSUPPORT;
3139 if (likely(index < NEIGH_NR_TABLES)) {
3140 struct neigh_table *tbl;
3141 struct neighbour *neigh;
3143 tbl = neigh_tables[index];
3147 if (index == NEIGH_ARP_TABLE) {
3148 u32 key = *((u32 *)addr);
3150 neigh = __ipv4_neigh_lookup_noref(dev, key);
3152 neigh = __neigh_lookup_noref(tbl, addr, dev);
3155 neigh = __neigh_create(tbl, addr, dev, false);
3156 err = PTR_ERR(neigh);
3157 if (IS_ERR(neigh)) {
3161 err = READ_ONCE(neigh->output)(neigh, skb);
3164 else if (index == NEIGH_LINK_TABLE) {
3165 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
3166 addr, NULL, skb->len);
3169 err = dev_queue_xmit(skb);
3177 EXPORT_SYMBOL(neigh_xmit);
3179 #ifdef CONFIG_PROC_FS
3181 static struct neighbour *neigh_get_first(struct seq_file *seq)
3183 struct neigh_seq_state *state = seq->private;
3184 struct net *net = seq_file_net(seq);
3185 struct neigh_hash_table *nht = state->nht;
3186 struct neighbour *n = NULL;
3189 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
3190 for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
3191 n = rcu_dereference(nht->hash_buckets[bucket]);
3194 if (!net_eq(dev_net(n->dev), net))
3196 if (state->neigh_sub_iter) {
3200 v = state->neigh_sub_iter(state, n, &fakep);
3204 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
3206 if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
3209 n = rcu_dereference(n->next);
3215 state->bucket = bucket;
3220 static struct neighbour *neigh_get_next(struct seq_file *seq,
3221 struct neighbour *n,
3224 struct neigh_seq_state *state = seq->private;
3225 struct net *net = seq_file_net(seq);
3226 struct neigh_hash_table *nht = state->nht;
3228 if (state->neigh_sub_iter) {
3229 void *v = state->neigh_sub_iter(state, n, pos);
3233 n = rcu_dereference(n->next);
3237 if (!net_eq(dev_net(n->dev), net))
3239 if (state->neigh_sub_iter) {
3240 void *v = state->neigh_sub_iter(state, n, pos);
3245 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
3248 if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
3251 n = rcu_dereference(n->next);
3257 if (++state->bucket >= (1 << nht->hash_shift))
3260 n = rcu_dereference(nht->hash_buckets[state->bucket]);
3268 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
3270 struct neighbour *n = neigh_get_first(seq);
3275 n = neigh_get_next(seq, n, pos);
3280 return *pos ? NULL : n;
3283 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
3285 struct neigh_seq_state *state = seq->private;
3286 struct net *net = seq_file_net(seq);
3287 struct neigh_table *tbl = state->tbl;
3288 struct pneigh_entry *pn = NULL;
3291 state->flags |= NEIGH_SEQ_IS_PNEIGH;
3292 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
3293 pn = tbl->phash_buckets[bucket];
3294 while (pn && !net_eq(pneigh_net(pn), net))
3299 state->bucket = bucket;
3304 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
3305 struct pneigh_entry *pn,
3308 struct neigh_seq_state *state = seq->private;
3309 struct net *net = seq_file_net(seq);
3310 struct neigh_table *tbl = state->tbl;
3314 } while (pn && !net_eq(pneigh_net(pn), net));
3317 if (++state->bucket > PNEIGH_HASHMASK)
3319 pn = tbl->phash_buckets[state->bucket];
3320 while (pn && !net_eq(pneigh_net(pn), net))
3332 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
3334 struct pneigh_entry *pn = pneigh_get_first(seq);
3339 pn = pneigh_get_next(seq, pn, pos);
3344 return *pos ? NULL : pn;
3347 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
3349 struct neigh_seq_state *state = seq->private;
3351 loff_t idxpos = *pos;
3353 rc = neigh_get_idx(seq, &idxpos);
3354 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3355 rc = pneigh_get_idx(seq, &idxpos);
3360 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
3361 __acquires(tbl->lock)
3364 struct neigh_seq_state *state = seq->private;
3368 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
3371 state->nht = rcu_dereference(tbl->nht);
3372 read_lock_bh(&tbl->lock);
3374 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
3376 EXPORT_SYMBOL(neigh_seq_start);
3378 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3380 struct neigh_seq_state *state;
3383 if (v == SEQ_START_TOKEN) {
3384 rc = neigh_get_first(seq);
3388 state = seq->private;
3389 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
3390 rc = neigh_get_next(seq, v, NULL);
3393 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
3394 rc = pneigh_get_first(seq);
3396 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
3397 rc = pneigh_get_next(seq, v, NULL);
3403 EXPORT_SYMBOL(neigh_seq_next);
3405 void neigh_seq_stop(struct seq_file *seq, void *v)
3406 __releases(tbl->lock)
3409 struct neigh_seq_state *state = seq->private;
3410 struct neigh_table *tbl = state->tbl;
3412 read_unlock_bh(&tbl->lock);
3415 EXPORT_SYMBOL(neigh_seq_stop);
3417 /* statistics via seq_file */
3419 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
3421 struct neigh_table *tbl = pde_data(file_inode(seq->file));
3425 return SEQ_START_TOKEN;
3427 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
3428 if (!cpu_possible(cpu))
3431 return per_cpu_ptr(tbl->stats, cpu);
3436 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3438 struct neigh_table *tbl = pde_data(file_inode(seq->file));
3441 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
3442 if (!cpu_possible(cpu))
3445 return per_cpu_ptr(tbl->stats, cpu);
3451 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
3456 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
3458 struct neigh_table *tbl = pde_data(file_inode(seq->file));
3459 struct neigh_statistics *st = v;
3461 if (v == SEQ_START_TOKEN) {
3462 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");
3466 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
3467 "%08lx %08lx %08lx "
3468 "%08lx %08lx %08lx\n",
3469 atomic_read(&tbl->entries),
3480 st->rcv_probes_mcast,
3481 st->rcv_probes_ucast,
3483 st->periodic_gc_runs,
3492 static const struct seq_operations neigh_stat_seq_ops = {
3493 .start = neigh_stat_seq_start,
3494 .next = neigh_stat_seq_next,
3495 .stop = neigh_stat_seq_stop,
3496 .show = neigh_stat_seq_show,
3498 #endif /* CONFIG_PROC_FS */
3500 static void __neigh_notify(struct neighbour *n, int type, int flags,
3503 struct net *net = dev_net(n->dev);
3504 struct sk_buff *skb;
3507 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
3511 err = neigh_fill_info(skb, n, pid, 0, type, flags);
3513 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
3514 WARN_ON(err == -EMSGSIZE);
3518 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3522 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3525 void neigh_app_ns(struct neighbour *n)
3527 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
3529 EXPORT_SYMBOL(neigh_app_ns);
3531 #ifdef CONFIG_SYSCTL
3532 static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
3534 static int proc_unres_qlen(struct ctl_table *ctl, int write,
3535 void *buffer, size_t *lenp, loff_t *ppos)
3538 struct ctl_table tmp = *ctl;
3540 tmp.extra1 = SYSCTL_ZERO;
3541 tmp.extra2 = &unres_qlen_max;
3544 size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
3545 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3548 *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
3552 static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
3555 struct net_device *dev;
3556 int family = neigh_parms_family(p);
3559 for_each_netdev_rcu(net, dev) {
3560 struct neigh_parms *dst_p =
3561 neigh_get_dev_parms_rcu(dev, family);
3563 if (dst_p && !test_bit(index, dst_p->data_state))
3564 dst_p->data[index] = p->data[index];
3569 static void neigh_proc_update(struct ctl_table *ctl, int write)
3571 struct net_device *dev = ctl->extra1;
3572 struct neigh_parms *p = ctl->extra2;
3573 struct net *net = neigh_parms_net(p);
3574 int index = (int *) ctl->data - p->data;
3579 set_bit(index, p->data_state);
3580 if (index == NEIGH_VAR_DELAY_PROBE_TIME)
3581 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
3582 if (!dev) /* NULL dev means this is default value */
3583 neigh_copy_dflt_parms(net, p, index);
3586 static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
3587 void *buffer, size_t *lenp,
3590 struct ctl_table tmp = *ctl;
3593 tmp.extra1 = SYSCTL_ZERO;
3594 tmp.extra2 = SYSCTL_INT_MAX;
3596 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3597 neigh_proc_update(ctl, write);
3601 static int neigh_proc_dointvec_ms_jiffies_positive(struct ctl_table *ctl, int write,
3602 void *buffer, size_t *lenp, loff_t *ppos)
3604 struct ctl_table tmp = *ctl;
3607 int min = msecs_to_jiffies(1);
3612 ret = proc_dointvec_ms_jiffies_minmax(&tmp, write, buffer, lenp, ppos);
3613 neigh_proc_update(ctl, write);
3617 int neigh_proc_dointvec(struct ctl_table *ctl, int write, void *buffer,
3618 size_t *lenp, loff_t *ppos)
3620 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3622 neigh_proc_update(ctl, write);
3625 EXPORT_SYMBOL(neigh_proc_dointvec);
3627 int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write, void *buffer,
3628 size_t *lenp, loff_t *ppos)
3630 int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3632 neigh_proc_update(ctl, write);
3635 EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3637 static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3638 void *buffer, size_t *lenp,
3641 int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
3643 neigh_proc_update(ctl, write);
3647 int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3648 void *buffer, size_t *lenp, loff_t *ppos)
3650 int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3652 neigh_proc_update(ctl, write);
3655 EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3657 static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3658 void *buffer, size_t *lenp,
3661 int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
3663 neigh_proc_update(ctl, write);
3667 static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
3668 void *buffer, size_t *lenp,
3671 struct neigh_parms *p = ctl->extra2;
3674 if (strcmp(ctl->procname, "base_reachable_time") == 0)
3675 ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3676 else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
3677 ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3681 if (write && ret == 0) {
3682 /* update reachable_time as well, otherwise, the change will
3683 * only be effective after the next time neigh_periodic_work
3684 * decides to recompute it
3687 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
3692 #define NEIGH_PARMS_DATA_OFFSET(index) \
3693 (&((struct neigh_parms *) 0)->data[index])
3695 #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
3696 [NEIGH_VAR_ ## attr] = { \
3698 .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
3699 .maxlen = sizeof(int), \
3701 .proc_handler = proc, \
3704 #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
3705 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
3707 #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3708 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
3710 #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3711 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
3713 #define NEIGH_SYSCTL_MS_JIFFIES_POSITIVE_ENTRY(attr, name) \
3714 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies_positive)
3716 #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3717 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
3719 #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3720 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
3722 static struct neigh_sysctl_table {
3723 struct ctl_table_header *sysctl_header;
3724 struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3725 } neigh_sysctl_template __read_mostly = {
3727 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
3728 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
3729 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
3730 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
3731 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
3732 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
3733 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
3734 NEIGH_SYSCTL_MS_JIFFIES_POSITIVE_ENTRY(INTERVAL_PROBE_TIME_MS,
3735 "interval_probe_time_ms"),
3736 NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
3737 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
3738 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
3739 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
3740 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
3741 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
3742 NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
3743 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
3744 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
3745 [NEIGH_VAR_GC_INTERVAL] = {
3746 .procname = "gc_interval",
3747 .maxlen = sizeof(int),
3749 .proc_handler = proc_dointvec_jiffies,
3751 [NEIGH_VAR_GC_THRESH1] = {
3752 .procname = "gc_thresh1",
3753 .maxlen = sizeof(int),
3755 .extra1 = SYSCTL_ZERO,
3756 .extra2 = SYSCTL_INT_MAX,
3757 .proc_handler = proc_dointvec_minmax,
3759 [NEIGH_VAR_GC_THRESH2] = {
3760 .procname = "gc_thresh2",
3761 .maxlen = sizeof(int),
3763 .extra1 = SYSCTL_ZERO,
3764 .extra2 = SYSCTL_INT_MAX,
3765 .proc_handler = proc_dointvec_minmax,
3767 [NEIGH_VAR_GC_THRESH3] = {
3768 .procname = "gc_thresh3",
3769 .maxlen = sizeof(int),
3771 .extra1 = SYSCTL_ZERO,
3772 .extra2 = SYSCTL_INT_MAX,
3773 .proc_handler = proc_dointvec_minmax,
3779 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
3780 proc_handler *handler)
3783 struct neigh_sysctl_table *t;
3784 const char *dev_name_source;
3785 char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
3787 size_t neigh_vars_size;
3789 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);
3793 for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
3794 t->neigh_vars[i].data += (long) p;
3795 t->neigh_vars[i].extra1 = dev;
3796 t->neigh_vars[i].extra2 = p;
3799 neigh_vars_size = ARRAY_SIZE(t->neigh_vars);
3801 dev_name_source = dev->name;
3802 /* Terminate the table early */
3803 memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
3804 sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
3805 neigh_vars_size = NEIGH_VAR_BASE_REACHABLE_TIME_MS + 1;
3807 struct neigh_table *tbl = p->tbl;
3808 dev_name_source = "default";
3809 t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
3810 t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
3811 t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
3812 t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
3817 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
3819 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
3820 /* RetransTime (in milliseconds)*/
3821 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
3822 /* ReachableTime (in milliseconds) */
3823 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
3825 /* Those handlers will update p->reachable_time after
3826 * base_reachable_time(_ms) is set to ensure the new timer starts being
3827 * applied after the next neighbour update instead of waiting for
3828 * neigh_periodic_work to update its value (can be multiple minutes)
3829 * So any handler that replaces them should do this as well
3832 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
3833 neigh_proc_base_reachable_time;
3834 /* ReachableTime (in milliseconds) */
3835 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
3836 neigh_proc_base_reachable_time;
3839 switch (neigh_parms_family(p)) {
3850 snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
3851 p_name, dev_name_source);
3852 t->sysctl_header = register_net_sysctl_sz(neigh_parms_net(p),
3853 neigh_path, t->neigh_vars,
3855 if (!t->sysctl_header)
3858 p->sysctl_table = t;
3866 EXPORT_SYMBOL(neigh_sysctl_register);
3868 void neigh_sysctl_unregister(struct neigh_parms *p)
3870 if (p->sysctl_table) {
3871 struct neigh_sysctl_table *t = p->sysctl_table;
3872 p->sysctl_table = NULL;
3873 unregister_net_sysctl_table(t->sysctl_header);
3877 EXPORT_SYMBOL(neigh_sysctl_unregister);
3879 #endif /* CONFIG_SYSCTL */
3881 static int __init neigh_init(void)
3883 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3884 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
3885 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, neigh_get, neigh_dump_info, 0);
3887 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3889 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
3894 subsys_initcall(neigh_init);