2 * Generic address resolution entity
5 * Pedro Roque <roque@di.fc.ul.pt>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
15 * Harald Welte Add neighbour cache statistics like rtstat
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/slab.h>
21 #include <linux/kmemleak.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/socket.h>
26 #include <linux/netdevice.h>
27 #include <linux/proc_fs.h>
29 #include <linux/sysctl.h>
31 #include <linux/times.h>
32 #include <net/net_namespace.h>
33 #include <net/neighbour.h>
37 #include <net/netevent.h>
38 #include <net/netlink.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/random.h>
41 #include <linux/string.h>
42 #include <linux/log2.h>
43 #include <linux/inetdevice.h>
44 #include <net/addrconf.h>
48 #define neigh_dbg(level, fmt, ...) \
50 if (level <= NEIGH_DEBUG) \
51 pr_debug(fmt, ##__VA_ARGS__); \
54 #define PNEIGH_HASHMASK 0xF
56 static void neigh_timer_handler(struct timer_list *t);
57 static void __neigh_notify(struct neighbour *n, int type, int flags,
59 static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid);
60 static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
61 struct net_device *dev);
64 static const struct seq_operations neigh_stat_seq_ops;
68 Neighbour hash table buckets are protected with rwlock tbl->lock.
70 - All the scans/updates to hash buckets MUST be made under this lock.
71 - NOTHING clever should be made under this lock: no callbacks
72 to protocol backends, no attempts to send something to network.
73 It will result in deadlocks, if backend/driver wants to use neighbour
75 - If the entry requires some non-trivial actions, increase
76 its reference count and release table lock.
78 Neighbour entries are protected:
79 - with reference count.
80 - with rwlock neigh->lock
82 Reference count prevents destruction.
84 neigh->lock mainly serializes ll address data and its validity state.
85 However, the same lock is used to protect another entry fields:
89 Again, nothing clever shall be made under neigh->lock,
90 the most complicated procedure, which we allow is dev->hard_header.
91 It is supposed, that dev->hard_header is simplistic and does
92 not make callbacks to neighbour tables.
95 static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
101 static void neigh_cleanup_and_release(struct neighbour *neigh)
103 if (neigh->parms->neigh_cleanup)
104 neigh->parms->neigh_cleanup(neigh);
106 __neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
107 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
108 neigh_release(neigh);
112 * It is random distribution in the interval (1/2)*base...(3/2)*base.
113 * It corresponds to default IPv6 settings and is not overridable,
114 * because it is really reasonable choice.
117 unsigned long neigh_rand_reach_time(unsigned long base)
119 return base ? (prandom_u32() % base) + (base >> 1) : 0;
121 EXPORT_SYMBOL(neigh_rand_reach_time);
124 static bool neigh_del(struct neighbour *n, __u8 state, __u8 flags,
125 struct neighbour __rcu **np, struct neigh_table *tbl)
129 write_lock(&n->lock);
130 if (refcount_read(&n->refcnt) == 1 && !(n->nud_state & state) &&
131 !(n->flags & flags)) {
132 struct neighbour *neigh;
134 neigh = rcu_dereference_protected(n->next,
135 lockdep_is_held(&tbl->lock));
136 rcu_assign_pointer(*np, neigh);
140 write_unlock(&n->lock);
142 neigh_cleanup_and_release(n);
146 bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
148 struct neigh_hash_table *nht;
149 void *pkey = ndel->primary_key;
152 struct neighbour __rcu **np;
154 nht = rcu_dereference_protected(tbl->nht,
155 lockdep_is_held(&tbl->lock));
156 hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
157 hash_val = hash_val >> (32 - nht->hash_shift);
159 np = &nht->hash_buckets[hash_val];
160 while ((n = rcu_dereference_protected(*np,
161 lockdep_is_held(&tbl->lock)))) {
163 return neigh_del(n, 0, 0, np, tbl);
169 static int neigh_forced_gc(struct neigh_table *tbl)
173 struct neigh_hash_table *nht;
175 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
177 write_lock_bh(&tbl->lock);
178 nht = rcu_dereference_protected(tbl->nht,
179 lockdep_is_held(&tbl->lock));
180 for (i = 0; i < (1 << nht->hash_shift); i++) {
182 struct neighbour __rcu **np;
184 np = &nht->hash_buckets[i];
185 while ((n = rcu_dereference_protected(*np,
186 lockdep_is_held(&tbl->lock))) != NULL) {
187 /* Neighbour record may be discarded if:
188 * - nobody refers to it.
189 * - it is not permanent
191 if (neigh_del(n, NUD_PERMANENT, NTF_EXT_LEARNED, np,
200 tbl->last_flush = jiffies;
202 write_unlock_bh(&tbl->lock);
207 static void neigh_add_timer(struct neighbour *n, unsigned long when)
210 if (unlikely(mod_timer(&n->timer, when))) {
211 printk("NEIGH: BUG, double timer add, state is %x\n",
217 static int neigh_del_timer(struct neighbour *n)
219 if ((n->nud_state & NUD_IN_TIMER) &&
220 del_timer(&n->timer)) {
227 static void pneigh_queue_purge(struct sk_buff_head *list, struct net *net)
229 struct sk_buff_head tmp;
233 skb_queue_head_init(&tmp);
234 spin_lock_irqsave(&list->lock, flags);
235 skb = skb_peek(list);
236 while (skb != NULL) {
237 struct sk_buff *skb_next = skb_peek_next(skb, list);
238 if (net == NULL || net_eq(dev_net(skb->dev), net)) {
239 __skb_unlink(skb, list);
240 __skb_queue_tail(&tmp, skb);
244 spin_unlock_irqrestore(&list->lock, flags);
246 while ((skb = __skb_dequeue(&tmp))) {
252 static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
255 struct neigh_hash_table *nht;
257 nht = rcu_dereference_protected(tbl->nht,
258 lockdep_is_held(&tbl->lock));
260 for (i = 0; i < (1 << nht->hash_shift); i++) {
262 struct neighbour __rcu **np = &nht->hash_buckets[i];
264 while ((n = rcu_dereference_protected(*np,
265 lockdep_is_held(&tbl->lock))) != NULL) {
266 if (dev && n->dev != dev) {
270 rcu_assign_pointer(*np,
271 rcu_dereference_protected(n->next,
272 lockdep_is_held(&tbl->lock)));
273 write_lock(&n->lock);
277 if (refcount_read(&n->refcnt) != 1) {
278 /* The most unpleasant situation.
279 We must destroy neighbour entry,
280 but someone still uses it.
282 The destroy will be delayed until
283 the last user releases us, but
284 we must kill timers etc. and move
287 __skb_queue_purge(&n->arp_queue);
288 n->arp_queue_len_bytes = 0;
289 n->output = neigh_blackhole;
290 if (n->nud_state & NUD_VALID)
291 n->nud_state = NUD_NOARP;
293 n->nud_state = NUD_NONE;
294 neigh_dbg(2, "neigh %p is stray\n", n);
296 write_unlock(&n->lock);
297 neigh_cleanup_and_release(n);
302 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
304 write_lock_bh(&tbl->lock);
305 neigh_flush_dev(tbl, dev);
306 write_unlock_bh(&tbl->lock);
308 EXPORT_SYMBOL(neigh_changeaddr);
310 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
312 write_lock_bh(&tbl->lock);
313 neigh_flush_dev(tbl, dev);
314 pneigh_ifdown_and_unlock(tbl, dev);
315 pneigh_queue_purge(&tbl->proxy_queue, dev ? dev_net(dev) : NULL);
316 if (skb_queue_empty_lockless(&tbl->proxy_queue))
317 del_timer_sync(&tbl->proxy_timer);
320 EXPORT_SYMBOL(neigh_ifdown);
322 static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
324 struct neighbour *n = NULL;
325 unsigned long now = jiffies;
328 entries = atomic_inc_return(&tbl->entries) - 1;
329 if (entries >= tbl->gc_thresh3 ||
330 (entries >= tbl->gc_thresh2 &&
331 time_after(now, tbl->last_flush + 5 * HZ))) {
332 if (!neigh_forced_gc(tbl) &&
333 entries >= tbl->gc_thresh3) {
334 net_info_ratelimited("%s: neighbor table overflow!\n",
336 NEIGH_CACHE_STAT_INC(tbl, table_fulls);
341 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
345 __skb_queue_head_init(&n->arp_queue);
346 rwlock_init(&n->lock);
347 seqlock_init(&n->ha_lock);
348 n->updated = n->used = now;
349 n->nud_state = NUD_NONE;
350 n->output = neigh_blackhole;
351 seqlock_init(&n->hh.hh_lock);
352 n->parms = neigh_parms_clone(&tbl->parms);
353 timer_setup(&n->timer, neigh_timer_handler, 0);
355 NEIGH_CACHE_STAT_INC(tbl, allocs);
357 refcount_set(&n->refcnt, 1);
363 atomic_dec(&tbl->entries);
367 static void neigh_get_hash_rnd(u32 *x)
369 *x = get_random_u32() | 1;
372 static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
374 size_t size = (1 << shift) * sizeof(struct neighbour *);
375 struct neigh_hash_table *ret;
376 struct neighbour __rcu **buckets;
379 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
382 if (size <= PAGE_SIZE) {
383 buckets = kzalloc(size, GFP_ATOMIC);
385 buckets = (struct neighbour __rcu **)
386 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
388 kmemleak_alloc(buckets, size, 1, GFP_ATOMIC);
394 ret->hash_buckets = buckets;
395 ret->hash_shift = shift;
396 for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
397 neigh_get_hash_rnd(&ret->hash_rnd[i]);
401 static void neigh_hash_free_rcu(struct rcu_head *head)
403 struct neigh_hash_table *nht = container_of(head,
404 struct neigh_hash_table,
406 size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
407 struct neighbour __rcu **buckets = nht->hash_buckets;
409 if (size <= PAGE_SIZE) {
412 kmemleak_free(buckets);
413 free_pages((unsigned long)buckets, get_order(size));
418 static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
419 unsigned long new_shift)
421 unsigned int i, hash;
422 struct neigh_hash_table *new_nht, *old_nht;
424 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
426 old_nht = rcu_dereference_protected(tbl->nht,
427 lockdep_is_held(&tbl->lock));
428 new_nht = neigh_hash_alloc(new_shift);
432 for (i = 0; i < (1 << old_nht->hash_shift); i++) {
433 struct neighbour *n, *next;
435 for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
436 lockdep_is_held(&tbl->lock));
439 hash = tbl->hash(n->primary_key, n->dev,
442 hash >>= (32 - new_nht->hash_shift);
443 next = rcu_dereference_protected(n->next,
444 lockdep_is_held(&tbl->lock));
446 rcu_assign_pointer(n->next,
447 rcu_dereference_protected(
448 new_nht->hash_buckets[hash],
449 lockdep_is_held(&tbl->lock)));
450 rcu_assign_pointer(new_nht->hash_buckets[hash], n);
454 rcu_assign_pointer(tbl->nht, new_nht);
455 call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
459 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
460 struct net_device *dev)
464 NEIGH_CACHE_STAT_INC(tbl, lookups);
467 n = __neigh_lookup_noref(tbl, pkey, dev);
469 if (!refcount_inc_not_zero(&n->refcnt))
471 NEIGH_CACHE_STAT_INC(tbl, hits);
474 rcu_read_unlock_bh();
477 EXPORT_SYMBOL(neigh_lookup);
479 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
483 unsigned int key_len = tbl->key_len;
485 struct neigh_hash_table *nht;
487 NEIGH_CACHE_STAT_INC(tbl, lookups);
490 nht = rcu_dereference_bh(tbl->nht);
491 hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
493 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
495 n = rcu_dereference_bh(n->next)) {
496 if (!memcmp(n->primary_key, pkey, key_len) &&
497 net_eq(dev_net(n->dev), net)) {
498 if (!refcount_inc_not_zero(&n->refcnt))
500 NEIGH_CACHE_STAT_INC(tbl, hits);
505 rcu_read_unlock_bh();
508 EXPORT_SYMBOL(neigh_lookup_nodev);
510 struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
511 struct net_device *dev, bool want_ref)
514 unsigned int key_len = tbl->key_len;
516 struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
517 struct neigh_hash_table *nht;
520 rc = ERR_PTR(-ENOBUFS);
524 memcpy(n->primary_key, pkey, key_len);
528 /* Protocol specific setup. */
529 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
531 goto out_neigh_release;
534 if (dev->netdev_ops->ndo_neigh_construct) {
535 error = dev->netdev_ops->ndo_neigh_construct(dev, n);
538 goto out_neigh_release;
542 /* Device specific setup. */
543 if (n->parms->neigh_setup &&
544 (error = n->parms->neigh_setup(n)) < 0) {
546 goto out_neigh_release;
549 n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
551 write_lock_bh(&tbl->lock);
552 nht = rcu_dereference_protected(tbl->nht,
553 lockdep_is_held(&tbl->lock));
555 if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
556 nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
558 hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
560 if (n->parms->dead) {
561 rc = ERR_PTR(-EINVAL);
565 for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
566 lockdep_is_held(&tbl->lock));
568 n1 = rcu_dereference_protected(n1->next,
569 lockdep_is_held(&tbl->lock))) {
570 if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
581 rcu_assign_pointer(n->next,
582 rcu_dereference_protected(nht->hash_buckets[hash_val],
583 lockdep_is_held(&tbl->lock)));
584 rcu_assign_pointer(nht->hash_buckets[hash_val], n);
585 write_unlock_bh(&tbl->lock);
586 neigh_dbg(2, "neigh %p is created\n", n);
591 write_unlock_bh(&tbl->lock);
596 EXPORT_SYMBOL(__neigh_create);
598 static u32 pneigh_hash(const void *pkey, unsigned int key_len)
600 u32 hash_val = *(u32 *)(pkey + key_len - 4);
601 hash_val ^= (hash_val >> 16);
602 hash_val ^= hash_val >> 8;
603 hash_val ^= hash_val >> 4;
604 hash_val &= PNEIGH_HASHMASK;
608 static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
611 unsigned int key_len,
612 struct net_device *dev)
615 if (!memcmp(n->key, pkey, key_len) &&
616 net_eq(pneigh_net(n), net) &&
617 (n->dev == dev || !n->dev))
624 struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
625 struct net *net, const void *pkey, struct net_device *dev)
627 unsigned int key_len = tbl->key_len;
628 u32 hash_val = pneigh_hash(pkey, key_len);
630 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
631 net, pkey, key_len, dev);
633 EXPORT_SYMBOL_GPL(__pneigh_lookup);
635 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
636 struct net *net, const void *pkey,
637 struct net_device *dev, int creat)
639 struct pneigh_entry *n;
640 unsigned int key_len = tbl->key_len;
641 u32 hash_val = pneigh_hash(pkey, key_len);
643 read_lock_bh(&tbl->lock);
644 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
645 net, pkey, key_len, dev);
646 read_unlock_bh(&tbl->lock);
653 n = kzalloc(sizeof(*n) + key_len, GFP_KERNEL);
657 write_pnet(&n->net, net);
658 memcpy(n->key, pkey, key_len);
663 if (tbl->pconstructor && tbl->pconstructor(n)) {
671 write_lock_bh(&tbl->lock);
672 n->next = tbl->phash_buckets[hash_val];
673 tbl->phash_buckets[hash_val] = n;
674 write_unlock_bh(&tbl->lock);
678 EXPORT_SYMBOL(pneigh_lookup);
681 int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
682 struct net_device *dev)
684 struct pneigh_entry *n, **np;
685 unsigned int key_len = tbl->key_len;
686 u32 hash_val = pneigh_hash(pkey, key_len);
688 write_lock_bh(&tbl->lock);
689 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
691 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
692 net_eq(pneigh_net(n), net)) {
694 write_unlock_bh(&tbl->lock);
695 if (tbl->pdestructor)
703 write_unlock_bh(&tbl->lock);
707 static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
708 struct net_device *dev)
710 struct pneigh_entry *n, **np, *freelist = NULL;
713 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
714 np = &tbl->phash_buckets[h];
715 while ((n = *np) != NULL) {
716 if (!dev || n->dev == dev) {
725 write_unlock_bh(&tbl->lock);
726 while ((n = freelist)) {
729 if (tbl->pdestructor)
738 static void neigh_parms_destroy(struct neigh_parms *parms);
740 static inline void neigh_parms_put(struct neigh_parms *parms)
742 if (refcount_dec_and_test(&parms->refcnt))
743 neigh_parms_destroy(parms);
747 * neighbour must already be out of the table;
750 void neigh_destroy(struct neighbour *neigh)
752 struct net_device *dev = neigh->dev;
754 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
757 pr_warn("Destroying alive neighbour %p\n", neigh);
762 if (neigh_del_timer(neigh))
763 pr_warn("Impossible event\n");
765 write_lock_bh(&neigh->lock);
766 __skb_queue_purge(&neigh->arp_queue);
767 write_unlock_bh(&neigh->lock);
768 neigh->arp_queue_len_bytes = 0;
770 if (dev->netdev_ops->ndo_neigh_destroy)
771 dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
774 neigh_parms_put(neigh->parms);
776 neigh_dbg(2, "neigh %p is destroyed\n", neigh);
778 atomic_dec(&neigh->tbl->entries);
779 kfree_rcu(neigh, rcu);
781 EXPORT_SYMBOL(neigh_destroy);
783 /* Neighbour state is suspicious;
786 Called with write_locked neigh.
788 static void neigh_suspect(struct neighbour *neigh)
790 neigh_dbg(2, "neigh %p is suspected\n", neigh);
792 neigh->output = neigh->ops->output;
795 /* Neighbour state is OK;
798 Called with write_locked neigh.
800 static void neigh_connect(struct neighbour *neigh)
802 neigh_dbg(2, "neigh %p is connected\n", neigh);
804 neigh->output = neigh->ops->connected_output;
807 static void neigh_periodic_work(struct work_struct *work)
809 struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
811 struct neighbour __rcu **np;
813 struct neigh_hash_table *nht;
815 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
817 write_lock_bh(&tbl->lock);
818 nht = rcu_dereference_protected(tbl->nht,
819 lockdep_is_held(&tbl->lock));
822 * periodically recompute ReachableTime from random function
825 if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
826 struct neigh_parms *p;
827 tbl->last_rand = jiffies;
828 list_for_each_entry(p, &tbl->parms_list, list)
830 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
833 if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
836 for (i = 0 ; i < (1 << nht->hash_shift); i++) {
837 np = &nht->hash_buckets[i];
839 while ((n = rcu_dereference_protected(*np,
840 lockdep_is_held(&tbl->lock))) != NULL) {
843 write_lock(&n->lock);
845 state = n->nud_state;
846 if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
847 (n->flags & NTF_EXT_LEARNED)) {
848 write_unlock(&n->lock);
852 if (time_before(n->used, n->confirmed))
853 n->used = n->confirmed;
855 if (refcount_read(&n->refcnt) == 1 &&
856 (state == NUD_FAILED ||
857 time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
860 write_unlock(&n->lock);
861 neigh_cleanup_and_release(n);
864 write_unlock(&n->lock);
870 * It's fine to release lock here, even if hash table
871 * grows while we are preempted.
873 write_unlock_bh(&tbl->lock);
875 write_lock_bh(&tbl->lock);
876 nht = rcu_dereference_protected(tbl->nht,
877 lockdep_is_held(&tbl->lock));
880 /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
881 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
882 * BASE_REACHABLE_TIME.
884 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
885 NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
886 write_unlock_bh(&tbl->lock);
889 static __inline__ int neigh_max_probes(struct neighbour *n)
891 struct neigh_parms *p = n->parms;
892 return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
893 (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
894 NEIGH_VAR(p, MCAST_PROBES));
897 static void neigh_invalidate(struct neighbour *neigh)
898 __releases(neigh->lock)
899 __acquires(neigh->lock)
903 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
904 neigh_dbg(2, "neigh %p is failed\n", neigh);
905 neigh->updated = jiffies;
907 /* It is very thin place. report_unreachable is very complicated
908 routine. Particularly, it can hit the same neighbour entry!
910 So that, we try to be accurate and avoid dead loop. --ANK
912 while (neigh->nud_state == NUD_FAILED &&
913 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
914 write_unlock(&neigh->lock);
915 neigh->ops->error_report(neigh, skb);
916 write_lock(&neigh->lock);
918 __skb_queue_purge(&neigh->arp_queue);
919 neigh->arp_queue_len_bytes = 0;
922 static void neigh_probe(struct neighbour *neigh)
923 __releases(neigh->lock)
925 struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
926 /* keep skb alive even if arp_queue overflows */
928 skb = skb_clone(skb, GFP_ATOMIC);
929 write_unlock(&neigh->lock);
930 if (neigh->ops->solicit)
931 neigh->ops->solicit(neigh, skb);
932 atomic_inc(&neigh->probes);
936 /* Called when a timer expires for a neighbour entry. */
938 static void neigh_timer_handler(struct timer_list *t)
940 unsigned long now, next;
941 struct neighbour *neigh = from_timer(neigh, t, timer);
945 write_lock(&neigh->lock);
947 state = neigh->nud_state;
951 if (!(state & NUD_IN_TIMER))
954 if (state & NUD_REACHABLE) {
955 if (time_before_eq(now,
956 neigh->confirmed + neigh->parms->reachable_time)) {
957 neigh_dbg(2, "neigh %p is still alive\n", neigh);
958 next = neigh->confirmed + neigh->parms->reachable_time;
959 } else if (time_before_eq(now,
961 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
962 neigh_dbg(2, "neigh %p is delayed\n", neigh);
963 neigh->nud_state = NUD_DELAY;
964 neigh->updated = jiffies;
965 neigh_suspect(neigh);
966 next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
968 neigh_dbg(2, "neigh %p is suspected\n", neigh);
969 neigh->nud_state = NUD_STALE;
970 neigh->updated = jiffies;
971 neigh_suspect(neigh);
974 } else if (state & NUD_DELAY) {
975 if (time_before_eq(now,
977 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
978 neigh_dbg(2, "neigh %p is now reachable\n", neigh);
979 neigh->nud_state = NUD_REACHABLE;
980 neigh->updated = jiffies;
981 neigh_connect(neigh);
983 next = neigh->confirmed + neigh->parms->reachable_time;
985 neigh_dbg(2, "neigh %p is probed\n", neigh);
986 neigh->nud_state = NUD_PROBE;
987 neigh->updated = jiffies;
988 atomic_set(&neigh->probes, 0);
990 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
993 /* NUD_PROBE|NUD_INCOMPLETE */
994 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
997 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
998 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
999 neigh->nud_state = NUD_FAILED;
1001 neigh_invalidate(neigh);
1005 if (neigh->nud_state & NUD_IN_TIMER) {
1006 if (time_before(next, jiffies + HZ/2))
1007 next = jiffies + HZ/2;
1008 if (!mod_timer(&neigh->timer, next))
1011 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
1015 write_unlock(&neigh->lock);
1019 neigh_update_notify(neigh, 0);
1021 neigh_release(neigh);
1024 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
1027 bool immediate_probe = false;
1029 write_lock_bh(&neigh->lock);
1032 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
1037 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
1038 if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
1039 NEIGH_VAR(neigh->parms, APP_PROBES)) {
1040 unsigned long next, now = jiffies;
1042 atomic_set(&neigh->probes,
1043 NEIGH_VAR(neigh->parms, UCAST_PROBES));
1044 neigh_del_timer(neigh);
1045 neigh->nud_state = NUD_INCOMPLETE;
1046 neigh->updated = now;
1047 next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
1049 neigh_add_timer(neigh, next);
1050 immediate_probe = true;
1052 neigh->nud_state = NUD_FAILED;
1053 neigh->updated = jiffies;
1054 write_unlock_bh(&neigh->lock);
1059 } else if (neigh->nud_state & NUD_STALE) {
1060 neigh_dbg(2, "neigh %p is delayed\n", neigh);
1061 neigh_del_timer(neigh);
1062 neigh->nud_state = NUD_DELAY;
1063 neigh->updated = jiffies;
1064 neigh_add_timer(neigh, jiffies +
1065 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
1068 if (neigh->nud_state == NUD_INCOMPLETE) {
1070 while (neigh->arp_queue_len_bytes + skb->truesize >
1071 NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
1072 struct sk_buff *buff;
1074 buff = __skb_dequeue(&neigh->arp_queue);
1077 neigh->arp_queue_len_bytes -= buff->truesize;
1079 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
1082 __skb_queue_tail(&neigh->arp_queue, skb);
1083 neigh->arp_queue_len_bytes += skb->truesize;
1088 if (immediate_probe)
1091 write_unlock(&neigh->lock);
1096 if (neigh->nud_state & NUD_STALE)
1098 write_unlock_bh(&neigh->lock);
1102 EXPORT_SYMBOL(__neigh_event_send);
1104 static void neigh_update_hhs(struct neighbour *neigh)
1106 struct hh_cache *hh;
1107 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
1110 if (neigh->dev->header_ops)
1111 update = neigh->dev->header_ops->cache_update;
1115 if (READ_ONCE(hh->hh_len)) {
1116 write_seqlock_bh(&hh->hh_lock);
1117 update(hh, neigh->dev, neigh->ha);
1118 write_sequnlock_bh(&hh->hh_lock);
1125 /* Generic update routine.
1126 -- lladdr is new lladdr or NULL, if it is not supplied.
1127 -- new is new state.
1129 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1131 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
1132 lladdr instead of overriding it
1134 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
1136 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
1138 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
1141 Caller MUST hold reference count on the entry.
1144 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1145 u32 flags, u32 nlmsg_pid)
1150 struct net_device *dev;
1151 int update_isrouter = 0;
1153 write_lock_bh(&neigh->lock);
1156 old = neigh->nud_state;
1159 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
1160 (old & (NUD_NOARP | NUD_PERMANENT)))
1165 neigh_update_ext_learned(neigh, flags, ¬ify);
1167 if (!(new & NUD_VALID)) {
1168 neigh_del_timer(neigh);
1169 if (old & NUD_CONNECTED)
1170 neigh_suspect(neigh);
1171 neigh->nud_state = new;
1173 notify = old & NUD_VALID;
1174 if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
1175 (new & NUD_FAILED)) {
1176 neigh_invalidate(neigh);
1182 /* Compare new lladdr with cached one */
1183 if (!dev->addr_len) {
1184 /* First case: device needs no address. */
1186 } else if (lladdr) {
1187 /* The second case: if something is already cached
1188 and a new address is proposed:
1190 - if they are different, check override flag
1192 if ((old & NUD_VALID) &&
1193 !memcmp(lladdr, neigh->ha, dev->addr_len))
1196 /* No address is supplied; if we know something,
1197 use it, otherwise discard the request.
1200 if (!(old & NUD_VALID))
1205 /* Update confirmed timestamp for neighbour entry after we
1206 * received ARP packet even if it doesn't change IP to MAC binding.
1208 if (new & NUD_CONNECTED)
1209 neigh->confirmed = jiffies;
1211 /* If entry was valid and address is not changed,
1212 do not change entry state, if new one is STALE.
1215 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1216 if (old & NUD_VALID) {
1217 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1218 update_isrouter = 0;
1219 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1220 (old & NUD_CONNECTED)) {
1226 if (lladdr == neigh->ha && new == NUD_STALE &&
1227 !(flags & NEIGH_UPDATE_F_ADMIN))
1232 /* Update timestamp only once we know we will make a change to the
1233 * neighbour entry. Otherwise we risk to move the locktime window with
1234 * noop updates and ignore relevant ARP updates.
1236 if (new != old || lladdr != neigh->ha)
1237 neigh->updated = jiffies;
1240 neigh_del_timer(neigh);
1241 if (new & NUD_PROBE)
1242 atomic_set(&neigh->probes, 0);
1243 if (new & NUD_IN_TIMER)
1244 neigh_add_timer(neigh, (jiffies +
1245 ((new & NUD_REACHABLE) ?
1246 neigh->parms->reachable_time :
1248 neigh->nud_state = new;
1252 if (lladdr != neigh->ha) {
1253 write_seqlock(&neigh->ha_lock);
1254 memcpy(&neigh->ha, lladdr, dev->addr_len);
1255 write_sequnlock(&neigh->ha_lock);
1256 neigh_update_hhs(neigh);
1257 if (!(new & NUD_CONNECTED))
1258 neigh->confirmed = jiffies -
1259 (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
1264 if (new & NUD_CONNECTED)
1265 neigh_connect(neigh);
1267 neigh_suspect(neigh);
1268 if (!(old & NUD_VALID)) {
1269 struct sk_buff *skb;
1271 /* Again: avoid dead loop if something went wrong */
1273 while (neigh->nud_state & NUD_VALID &&
1274 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1275 struct dst_entry *dst = skb_dst(skb);
1276 struct neighbour *n2, *n1 = neigh;
1277 write_unlock_bh(&neigh->lock);
1281 /* Why not just use 'neigh' as-is? The problem is that
1282 * things such as shaper, eql, and sch_teql can end up
1283 * using alternative, different, neigh objects to output
1284 * the packet in the output path. So what we need to do
1285 * here is re-lookup the top-level neigh in the path so
1286 * we can reinject the packet there.
1289 if (dst && dst->obsolete != DST_OBSOLETE_DEAD) {
1290 n2 = dst_neigh_lookup_skb(dst, skb);
1294 n1->output(n1, skb);
1299 write_lock_bh(&neigh->lock);
1301 __skb_queue_purge(&neigh->arp_queue);
1302 neigh->arp_queue_len_bytes = 0;
1305 if (update_isrouter) {
1306 neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
1307 (neigh->flags | NTF_ROUTER) :
1308 (neigh->flags & ~NTF_ROUTER);
1310 write_unlock_bh(&neigh->lock);
1313 neigh_update_notify(neigh, nlmsg_pid);
1317 EXPORT_SYMBOL(neigh_update);
1319 /* Update the neigh to listen temporarily for probe responses, even if it is
1320 * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
1322 void __neigh_set_probe_once(struct neighbour *neigh)
1326 neigh->updated = jiffies;
1327 if (!(neigh->nud_state & NUD_FAILED))
1329 neigh->nud_state = NUD_INCOMPLETE;
1330 atomic_set(&neigh->probes, neigh_max_probes(neigh));
1331 neigh_add_timer(neigh,
1332 jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
1334 EXPORT_SYMBOL(__neigh_set_probe_once);
1336 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1337 u8 *lladdr, void *saddr,
1338 struct net_device *dev)
1340 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1341 lladdr || !dev->addr_len);
1343 neigh_update(neigh, lladdr, NUD_STALE,
1344 NEIGH_UPDATE_F_OVERRIDE, 0);
1347 EXPORT_SYMBOL(neigh_event_ns);
1349 /* called with read_lock_bh(&n->lock); */
1350 static void neigh_hh_init(struct neighbour *n)
1352 struct net_device *dev = n->dev;
1353 __be16 prot = n->tbl->protocol;
1354 struct hh_cache *hh = &n->hh;
1356 write_lock_bh(&n->lock);
1358 /* Only one thread can come in here and initialize the
1362 dev->header_ops->cache(n, hh, prot);
1364 write_unlock_bh(&n->lock);
1367 /* Slow and careful. */
1369 int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1373 if (!neigh_event_send(neigh, skb)) {
1375 struct net_device *dev = neigh->dev;
1378 if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len))
1379 neigh_hh_init(neigh);
1382 __skb_pull(skb, skb_network_offset(skb));
1383 seq = read_seqbegin(&neigh->ha_lock);
1384 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1385 neigh->ha, NULL, skb->len);
1386 } while (read_seqretry(&neigh->ha_lock, seq));
1389 rc = dev_queue_xmit(skb);
1400 EXPORT_SYMBOL(neigh_resolve_output);
1402 /* As fast as possible without hh cache */
1404 int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
1406 struct net_device *dev = neigh->dev;
1411 __skb_pull(skb, skb_network_offset(skb));
1412 seq = read_seqbegin(&neigh->ha_lock);
1413 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1414 neigh->ha, NULL, skb->len);
1415 } while (read_seqretry(&neigh->ha_lock, seq));
1418 err = dev_queue_xmit(skb);
1425 EXPORT_SYMBOL(neigh_connected_output);
1427 int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1429 return dev_queue_xmit(skb);
1431 EXPORT_SYMBOL(neigh_direct_output);
1433 static void neigh_proxy_process(struct timer_list *t)
1435 struct neigh_table *tbl = from_timer(tbl, t, proxy_timer);
1436 long sched_next = 0;
1437 unsigned long now = jiffies;
1438 struct sk_buff *skb, *n;
1440 spin_lock(&tbl->proxy_queue.lock);
1442 skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1443 long tdif = NEIGH_CB(skb)->sched_next - now;
1446 struct net_device *dev = skb->dev;
1448 __skb_unlink(skb, &tbl->proxy_queue);
1449 if (tbl->proxy_redo && netif_running(dev)) {
1451 tbl->proxy_redo(skb);
1458 } else if (!sched_next || tdif < sched_next)
1461 del_timer(&tbl->proxy_timer);
1463 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1464 spin_unlock(&tbl->proxy_queue.lock);
1467 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1468 struct sk_buff *skb)
1470 unsigned long now = jiffies;
1472 unsigned long sched_next = now + (prandom_u32() %
1473 NEIGH_VAR(p, PROXY_DELAY));
1475 if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
1480 NEIGH_CB(skb)->sched_next = sched_next;
1481 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1483 spin_lock(&tbl->proxy_queue.lock);
1484 if (del_timer(&tbl->proxy_timer)) {
1485 if (time_before(tbl->proxy_timer.expires, sched_next))
1486 sched_next = tbl->proxy_timer.expires;
1490 __skb_queue_tail(&tbl->proxy_queue, skb);
1491 mod_timer(&tbl->proxy_timer, sched_next);
1492 spin_unlock(&tbl->proxy_queue.lock);
1494 EXPORT_SYMBOL(pneigh_enqueue);
1496 static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1497 struct net *net, int ifindex)
1499 struct neigh_parms *p;
1501 list_for_each_entry(p, &tbl->parms_list, list) {
1502 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1503 (!p->dev && !ifindex && net_eq(net, &init_net)))
1510 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1511 struct neigh_table *tbl)
1513 struct neigh_parms *p;
1514 struct net *net = dev_net(dev);
1515 const struct net_device_ops *ops = dev->netdev_ops;
1517 p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
1520 refcount_set(&p->refcnt, 1);
1522 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1525 write_pnet(&p->net, net);
1526 p->sysctl_table = NULL;
1528 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
1534 write_lock_bh(&tbl->lock);
1535 list_add(&p->list, &tbl->parms.list);
1536 write_unlock_bh(&tbl->lock);
1538 neigh_parms_data_state_cleanall(p);
1542 EXPORT_SYMBOL(neigh_parms_alloc);
1544 static void neigh_rcu_free_parms(struct rcu_head *head)
1546 struct neigh_parms *parms =
1547 container_of(head, struct neigh_parms, rcu_head);
1549 neigh_parms_put(parms);
1552 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1554 if (!parms || parms == &tbl->parms)
1556 write_lock_bh(&tbl->lock);
1557 list_del(&parms->list);
1559 write_unlock_bh(&tbl->lock);
1561 dev_put(parms->dev);
1562 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1564 EXPORT_SYMBOL(neigh_parms_release);
1566 static void neigh_parms_destroy(struct neigh_parms *parms)
1571 static struct lock_class_key neigh_table_proxy_queue_class;
1573 static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1575 void neigh_table_init(int index, struct neigh_table *tbl)
1577 unsigned long now = jiffies;
1578 unsigned long phsize;
1580 INIT_LIST_HEAD(&tbl->parms_list);
1581 list_add(&tbl->parms.list, &tbl->parms_list);
1582 write_pnet(&tbl->parms.net, &init_net);
1583 refcount_set(&tbl->parms.refcnt, 1);
1584 tbl->parms.reachable_time =
1585 neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
1587 tbl->stats = alloc_percpu(struct neigh_statistics);
1589 panic("cannot create neighbour cache statistics");
1591 #ifdef CONFIG_PROC_FS
1592 if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
1593 &neigh_stat_seq_ops, tbl))
1594 panic("cannot create neighbour proc dir entry");
1597 RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
1599 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1600 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
1602 if (!tbl->nht || !tbl->phash_buckets)
1603 panic("cannot allocate neighbour cache hashes");
1605 if (!tbl->entry_size)
1606 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
1607 tbl->key_len, NEIGH_PRIV_ALIGN);
1609 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
1611 rwlock_init(&tbl->lock);
1612 INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1613 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1614 tbl->parms.reachable_time);
1615 timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
1616 skb_queue_head_init_class(&tbl->proxy_queue,
1617 &neigh_table_proxy_queue_class);
1619 tbl->last_flush = now;
1620 tbl->last_rand = now + tbl->parms.reachable_time * 20;
1622 neigh_tables[index] = tbl;
1624 EXPORT_SYMBOL(neigh_table_init);
1626 int neigh_table_clear(int index, struct neigh_table *tbl)
1628 neigh_tables[index] = NULL;
1629 /* It is not clean... Fix it to unload IPv6 module safely */
1630 cancel_delayed_work_sync(&tbl->gc_work);
1631 del_timer_sync(&tbl->proxy_timer);
1632 pneigh_queue_purge(&tbl->proxy_queue, NULL);
1633 neigh_ifdown(tbl, NULL);
1634 if (atomic_read(&tbl->entries))
1635 pr_crit("neighbour leakage\n");
1637 call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1638 neigh_hash_free_rcu);
1641 kfree(tbl->phash_buckets);
1642 tbl->phash_buckets = NULL;
1644 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1646 free_percpu(tbl->stats);
1651 EXPORT_SYMBOL(neigh_table_clear);
1653 static struct neigh_table *neigh_find_table(int family)
1655 struct neigh_table *tbl = NULL;
1659 tbl = neigh_tables[NEIGH_ARP_TABLE];
1662 tbl = neigh_tables[NEIGH_ND_TABLE];
1665 tbl = neigh_tables[NEIGH_DN_TABLE];
1672 static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
1673 struct netlink_ext_ack *extack)
1675 struct net *net = sock_net(skb->sk);
1677 struct nlattr *dst_attr;
1678 struct neigh_table *tbl;
1679 struct neighbour *neigh;
1680 struct net_device *dev = NULL;
1684 if (nlmsg_len(nlh) < sizeof(*ndm))
1687 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1688 if (dst_attr == NULL)
1691 ndm = nlmsg_data(nlh);
1692 if (ndm->ndm_ifindex) {
1693 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1700 tbl = neigh_find_table(ndm->ndm_family);
1702 return -EAFNOSUPPORT;
1704 if (nla_len(dst_attr) < (int)tbl->key_len)
1707 if (ndm->ndm_flags & NTF_PROXY) {
1708 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1715 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1716 if (neigh == NULL) {
1721 err = neigh_update(neigh, NULL, NUD_FAILED,
1722 NEIGH_UPDATE_F_OVERRIDE |
1723 NEIGH_UPDATE_F_ADMIN,
1724 NETLINK_CB(skb).portid);
1725 write_lock_bh(&tbl->lock);
1726 neigh_release(neigh);
1727 neigh_remove_one(neigh, tbl);
1728 write_unlock_bh(&tbl->lock);
1734 static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1735 struct netlink_ext_ack *extack)
1737 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
1738 struct net *net = sock_net(skb->sk);
1740 struct nlattr *tb[NDA_MAX+1];
1741 struct neigh_table *tbl;
1742 struct net_device *dev = NULL;
1743 struct neighbour *neigh;
1748 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
1753 if (tb[NDA_DST] == NULL)
1756 ndm = nlmsg_data(nlh);
1757 if (ndm->ndm_ifindex) {
1758 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1764 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1768 tbl = neigh_find_table(ndm->ndm_family);
1770 return -EAFNOSUPPORT;
1772 if (nla_len(tb[NDA_DST]) < (int)tbl->key_len)
1774 dst = nla_data(tb[NDA_DST]);
1775 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
1777 if (ndm->ndm_flags & NTF_PROXY) {
1778 struct pneigh_entry *pn;
1781 pn = pneigh_lookup(tbl, net, dst, dev, 1);
1783 pn->flags = ndm->ndm_flags;
1792 neigh = neigh_lookup(tbl, dst, dev);
1793 if (neigh == NULL) {
1794 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1799 neigh = __neigh_lookup_errno(tbl, dst, dev);
1800 if (IS_ERR(neigh)) {
1801 err = PTR_ERR(neigh);
1805 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1807 neigh_release(neigh);
1811 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
1812 flags &= ~NEIGH_UPDATE_F_OVERRIDE;
1815 if (ndm->ndm_flags & NTF_EXT_LEARNED)
1816 flags |= NEIGH_UPDATE_F_EXT_LEARNED;
1818 if (ndm->ndm_flags & NTF_USE) {
1819 neigh_event_send(neigh, NULL);
1822 err = neigh_update(neigh, lladdr, ndm->ndm_state, flags,
1823 NETLINK_CB(skb).portid);
1824 neigh_release(neigh);
1830 static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1832 struct nlattr *nest;
1834 nest = nla_nest_start(skb, NDTA_PARMS);
1839 nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
1840 nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
1841 nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
1842 NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
1843 /* approximative value for deprecated QUEUE_LEN (in packets) */
1844 nla_put_u32(skb, NDTPA_QUEUE_LEN,
1845 NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
1846 nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
1847 nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
1848 nla_put_u32(skb, NDTPA_UCAST_PROBES,
1849 NEIGH_VAR(parms, UCAST_PROBES)) ||
1850 nla_put_u32(skb, NDTPA_MCAST_PROBES,
1851 NEIGH_VAR(parms, MCAST_PROBES)) ||
1852 nla_put_u32(skb, NDTPA_MCAST_REPROBES,
1853 NEIGH_VAR(parms, MCAST_REPROBES)) ||
1854 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
1856 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
1857 NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
1858 nla_put_msecs(skb, NDTPA_GC_STALETIME,
1859 NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
1860 nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
1861 NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
1862 nla_put_msecs(skb, NDTPA_RETRANS_TIME,
1863 NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
1864 nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
1865 NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
1866 nla_put_msecs(skb, NDTPA_PROXY_DELAY,
1867 NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
1868 nla_put_msecs(skb, NDTPA_LOCKTIME,
1869 NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
1870 goto nla_put_failure;
1871 return nla_nest_end(skb, nest);
1874 nla_nest_cancel(skb, nest);
1878 static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1879 u32 pid, u32 seq, int type, int flags)
1881 struct nlmsghdr *nlh;
1882 struct ndtmsg *ndtmsg;
1884 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1888 ndtmsg = nlmsg_data(nlh);
1890 read_lock_bh(&tbl->lock);
1891 ndtmsg->ndtm_family = tbl->family;
1892 ndtmsg->ndtm_pad1 = 0;
1893 ndtmsg->ndtm_pad2 = 0;
1895 if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
1896 nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
1897 nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
1898 nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
1899 nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
1900 goto nla_put_failure;
1902 unsigned long now = jiffies;
1903 long flush_delta = now - tbl->last_flush;
1904 long rand_delta = now - tbl->last_rand;
1905 struct neigh_hash_table *nht;
1906 struct ndt_config ndc = {
1907 .ndtc_key_len = tbl->key_len,
1908 .ndtc_entry_size = tbl->entry_size,
1909 .ndtc_entries = atomic_read(&tbl->entries),
1910 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
1911 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
1912 .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
1916 nht = rcu_dereference_bh(tbl->nht);
1917 ndc.ndtc_hash_rnd = nht->hash_rnd[0];
1918 ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
1919 rcu_read_unlock_bh();
1921 if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
1922 goto nla_put_failure;
1927 struct ndt_stats ndst;
1929 memset(&ndst, 0, sizeof(ndst));
1931 for_each_possible_cpu(cpu) {
1932 struct neigh_statistics *st;
1934 st = per_cpu_ptr(tbl->stats, cpu);
1935 ndst.ndts_allocs += st->allocs;
1936 ndst.ndts_destroys += st->destroys;
1937 ndst.ndts_hash_grows += st->hash_grows;
1938 ndst.ndts_res_failed += st->res_failed;
1939 ndst.ndts_lookups += st->lookups;
1940 ndst.ndts_hits += st->hits;
1941 ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
1942 ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
1943 ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
1944 ndst.ndts_forced_gc_runs += st->forced_gc_runs;
1945 ndst.ndts_table_fulls += st->table_fulls;
1948 if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
1950 goto nla_put_failure;
1953 BUG_ON(tbl->parms.dev);
1954 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1955 goto nla_put_failure;
1957 read_unlock_bh(&tbl->lock);
1958 nlmsg_end(skb, nlh);
1962 read_unlock_bh(&tbl->lock);
1963 nlmsg_cancel(skb, nlh);
1967 static int neightbl_fill_param_info(struct sk_buff *skb,
1968 struct neigh_table *tbl,
1969 struct neigh_parms *parms,
1970 u32 pid, u32 seq, int type,
1973 struct ndtmsg *ndtmsg;
1974 struct nlmsghdr *nlh;
1976 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1980 ndtmsg = nlmsg_data(nlh);
1982 read_lock_bh(&tbl->lock);
1983 ndtmsg->ndtm_family = tbl->family;
1984 ndtmsg->ndtm_pad1 = 0;
1985 ndtmsg->ndtm_pad2 = 0;
1987 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1988 neightbl_fill_parms(skb, parms) < 0)
1991 read_unlock_bh(&tbl->lock);
1992 nlmsg_end(skb, nlh);
1995 read_unlock_bh(&tbl->lock);
1996 nlmsg_cancel(skb, nlh);
2000 static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
2001 [NDTA_NAME] = { .type = NLA_STRING },
2002 [NDTA_THRESH1] = { .type = NLA_U32 },
2003 [NDTA_THRESH2] = { .type = NLA_U32 },
2004 [NDTA_THRESH3] = { .type = NLA_U32 },
2005 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
2006 [NDTA_PARMS] = { .type = NLA_NESTED },
2009 static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
2010 [NDTPA_IFINDEX] = { .type = NLA_U32 },
2011 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
2012 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
2013 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
2014 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
2015 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
2016 [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
2017 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
2018 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
2019 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
2020 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
2021 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
2022 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
2023 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
2026 static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
2027 struct netlink_ext_ack *extack)
2029 struct net *net = sock_net(skb->sk);
2030 struct neigh_table *tbl;
2031 struct ndtmsg *ndtmsg;
2032 struct nlattr *tb[NDTA_MAX+1];
2036 err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
2037 nl_neightbl_policy, extack);
2041 if (tb[NDTA_NAME] == NULL) {
2046 ndtmsg = nlmsg_data(nlh);
2048 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2049 tbl = neigh_tables[tidx];
2052 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
2054 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
2064 * We acquire tbl->lock to be nice to the periodic timers and
2065 * make sure they always see a consistent set of values.
2067 write_lock_bh(&tbl->lock);
2069 if (tb[NDTA_PARMS]) {
2070 struct nlattr *tbp[NDTPA_MAX+1];
2071 struct neigh_parms *p;
2074 err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
2075 nl_ntbl_parm_policy, extack);
2077 goto errout_tbl_lock;
2079 if (tbp[NDTPA_IFINDEX])
2080 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2082 p = lookup_neigh_parms(tbl, net, ifindex);
2085 goto errout_tbl_lock;
2088 for (i = 1; i <= NDTPA_MAX; i++) {
2093 case NDTPA_QUEUE_LEN:
2094 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2095 nla_get_u32(tbp[i]) *
2096 SKB_TRUESIZE(ETH_FRAME_LEN));
2098 case NDTPA_QUEUE_LENBYTES:
2099 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2100 nla_get_u32(tbp[i]));
2102 case NDTPA_PROXY_QLEN:
2103 NEIGH_VAR_SET(p, PROXY_QLEN,
2104 nla_get_u32(tbp[i]));
2106 case NDTPA_APP_PROBES:
2107 NEIGH_VAR_SET(p, APP_PROBES,
2108 nla_get_u32(tbp[i]));
2110 case NDTPA_UCAST_PROBES:
2111 NEIGH_VAR_SET(p, UCAST_PROBES,
2112 nla_get_u32(tbp[i]));
2114 case NDTPA_MCAST_PROBES:
2115 NEIGH_VAR_SET(p, MCAST_PROBES,
2116 nla_get_u32(tbp[i]));
2118 case NDTPA_MCAST_REPROBES:
2119 NEIGH_VAR_SET(p, MCAST_REPROBES,
2120 nla_get_u32(tbp[i]));
2122 case NDTPA_BASE_REACHABLE_TIME:
2123 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2124 nla_get_msecs(tbp[i]));
2125 /* update reachable_time as well, otherwise, the change will
2126 * only be effective after the next time neigh_periodic_work
2127 * decides to recompute it (can be multiple minutes)
2130 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
2132 case NDTPA_GC_STALETIME:
2133 NEIGH_VAR_SET(p, GC_STALETIME,
2134 nla_get_msecs(tbp[i]));
2136 case NDTPA_DELAY_PROBE_TIME:
2137 NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
2138 nla_get_msecs(tbp[i]));
2139 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
2141 case NDTPA_RETRANS_TIME:
2142 NEIGH_VAR_SET(p, RETRANS_TIME,
2143 nla_get_msecs(tbp[i]));
2145 case NDTPA_ANYCAST_DELAY:
2146 NEIGH_VAR_SET(p, ANYCAST_DELAY,
2147 nla_get_msecs(tbp[i]));
2149 case NDTPA_PROXY_DELAY:
2150 NEIGH_VAR_SET(p, PROXY_DELAY,
2151 nla_get_msecs(tbp[i]));
2153 case NDTPA_LOCKTIME:
2154 NEIGH_VAR_SET(p, LOCKTIME,
2155 nla_get_msecs(tbp[i]));
2162 if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2163 tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2164 !net_eq(net, &init_net))
2165 goto errout_tbl_lock;
2167 if (tb[NDTA_THRESH1])
2168 tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
2170 if (tb[NDTA_THRESH2])
2171 tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
2173 if (tb[NDTA_THRESH3])
2174 tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
2176 if (tb[NDTA_GC_INTERVAL])
2177 tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2182 write_unlock_bh(&tbl->lock);
2187 static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2189 struct net *net = sock_net(skb->sk);
2190 int family, tidx, nidx = 0;
2191 int tbl_skip = cb->args[0];
2192 int neigh_skip = cb->args[1];
2193 struct neigh_table *tbl;
2195 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2197 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2198 struct neigh_parms *p;
2200 tbl = neigh_tables[tidx];
2204 if (tidx < tbl_skip || (family && tbl->family != family))
2207 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2208 cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2213 p = list_next_entry(&tbl->parms, list);
2214 list_for_each_entry_from(p, &tbl->parms_list, list) {
2215 if (!net_eq(neigh_parms_net(p), net))
2218 if (nidx < neigh_skip)
2221 if (neightbl_fill_param_info(skb, tbl, p,
2222 NETLINK_CB(cb->skb).portid,
2240 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2241 u32 pid, u32 seq, int type, unsigned int flags)
2243 unsigned long now = jiffies;
2244 struct nda_cacheinfo ci;
2245 struct nlmsghdr *nlh;
2248 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2252 ndm = nlmsg_data(nlh);
2253 ndm->ndm_family = neigh->ops->family;
2256 ndm->ndm_flags = neigh->flags;
2257 ndm->ndm_type = neigh->type;
2258 ndm->ndm_ifindex = neigh->dev->ifindex;
2260 if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
2261 goto nla_put_failure;
2263 read_lock_bh(&neigh->lock);
2264 ndm->ndm_state = neigh->nud_state;
2265 if (neigh->nud_state & NUD_VALID) {
2266 char haddr[MAX_ADDR_LEN];
2268 neigh_ha_snapshot(haddr, neigh, neigh->dev);
2269 if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2270 read_unlock_bh(&neigh->lock);
2271 goto nla_put_failure;
2275 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2276 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2277 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
2278 ci.ndm_refcnt = refcount_read(&neigh->refcnt) - 1;
2279 read_unlock_bh(&neigh->lock);
2281 if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
2282 nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
2283 goto nla_put_failure;
2285 nlmsg_end(skb, nlh);
2289 nlmsg_cancel(skb, nlh);
2293 static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
2294 u32 pid, u32 seq, int type, unsigned int flags,
2295 struct neigh_table *tbl)
2297 struct nlmsghdr *nlh;
2300 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2304 ndm = nlmsg_data(nlh);
2305 ndm->ndm_family = tbl->family;
2308 ndm->ndm_flags = pn->flags | NTF_PROXY;
2309 ndm->ndm_type = RTN_UNICAST;
2310 ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
2311 ndm->ndm_state = NUD_NONE;
2313 if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
2314 goto nla_put_failure;
2316 nlmsg_end(skb, nlh);
2320 nlmsg_cancel(skb, nlh);
2324 static void neigh_update_notify(struct neighbour *neigh, u32 nlmsg_pid)
2326 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2327 __neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
2330 static bool neigh_master_filtered(struct net_device *dev, int master_idx)
2332 struct net_device *master;
2337 master = netdev_master_upper_dev_get(dev);
2338 if (!master || master->ifindex != master_idx)
2344 static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
2346 if (filter_idx && dev->ifindex != filter_idx)
2352 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2353 struct netlink_callback *cb)
2355 struct net *net = sock_net(skb->sk);
2356 const struct nlmsghdr *nlh = cb->nlh;
2357 struct nlattr *tb[NDA_MAX + 1];
2358 struct neighbour *n;
2359 int rc, h, s_h = cb->args[1];
2360 int idx, s_idx = idx = cb->args[2];
2361 struct neigh_hash_table *nht;
2362 int filter_master_idx = 0, filter_idx = 0;
2363 unsigned int flags = NLM_F_MULTI;
2366 err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL, NULL);
2368 if (tb[NDA_IFINDEX]) {
2369 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
2371 filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
2373 if (tb[NDA_MASTER]) {
2374 if (nla_len(tb[NDA_MASTER]) != sizeof(u32))
2376 filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
2378 if (filter_idx || filter_master_idx)
2379 flags |= NLM_F_DUMP_FILTERED;
2383 nht = rcu_dereference_bh(tbl->nht);
2385 for (h = s_h; h < (1 << nht->hash_shift); h++) {
2388 for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2390 n = rcu_dereference_bh(n->next)) {
2391 if (idx < s_idx || !net_eq(dev_net(n->dev), net))
2393 if (neigh_ifindex_filtered(n->dev, filter_idx) ||
2394 neigh_master_filtered(n->dev, filter_master_idx))
2396 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2409 rcu_read_unlock_bh();
2415 static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2416 struct netlink_callback *cb)
2418 struct pneigh_entry *n;
2419 struct net *net = sock_net(skb->sk);
2420 int rc, h, s_h = cb->args[3];
2421 int idx, s_idx = idx = cb->args[4];
2423 read_lock_bh(&tbl->lock);
2425 for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
2428 for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
2429 if (idx < s_idx || pneigh_net(n) != net)
2431 if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2434 NLM_F_MULTI, tbl) < 0) {
2435 read_unlock_bh(&tbl->lock);
2444 read_unlock_bh(&tbl->lock);
2453 static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2455 struct neigh_table *tbl;
2460 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2462 /* check for full ndmsg structure presence, family member is
2463 * the same for both structures
2465 if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
2466 ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
2471 for (t = 0; t < NEIGH_NR_TABLES; t++) {
2472 tbl = neigh_tables[t];
2476 if (t < s_t || (family && tbl->family != family))
2479 memset(&cb->args[1], 0, sizeof(cb->args) -
2480 sizeof(cb->args[0]));
2482 err = pneigh_dump_table(tbl, skb, cb);
2484 err = neigh_dump_table(tbl, skb, cb);
2493 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
2496 struct neigh_hash_table *nht;
2499 nht = rcu_dereference_bh(tbl->nht);
2501 read_lock(&tbl->lock); /* avoid resizes */
2502 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2503 struct neighbour *n;
2505 for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2507 n = rcu_dereference_bh(n->next))
2510 read_unlock(&tbl->lock);
2511 rcu_read_unlock_bh();
2513 EXPORT_SYMBOL(neigh_for_each);
2515 /* The tbl->lock must be held as a writer and BH disabled. */
2516 void __neigh_for_each_release(struct neigh_table *tbl,
2517 int (*cb)(struct neighbour *))
2520 struct neigh_hash_table *nht;
2522 nht = rcu_dereference_protected(tbl->nht,
2523 lockdep_is_held(&tbl->lock));
2524 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2525 struct neighbour *n;
2526 struct neighbour __rcu **np;
2528 np = &nht->hash_buckets[chain];
2529 while ((n = rcu_dereference_protected(*np,
2530 lockdep_is_held(&tbl->lock))) != NULL) {
2533 write_lock(&n->lock);
2536 rcu_assign_pointer(*np,
2537 rcu_dereference_protected(n->next,
2538 lockdep_is_held(&tbl->lock)));
2542 write_unlock(&n->lock);
2544 neigh_cleanup_and_release(n);
2548 EXPORT_SYMBOL(__neigh_for_each_release);
2550 int neigh_xmit(int index, struct net_device *dev,
2551 const void *addr, struct sk_buff *skb)
2553 int err = -EAFNOSUPPORT;
2554 if (likely(index < NEIGH_NR_TABLES)) {
2555 struct neigh_table *tbl;
2556 struct neighbour *neigh;
2558 tbl = neigh_tables[index];
2562 if (index == NEIGH_ARP_TABLE) {
2563 u32 key = *((u32 *)addr);
2565 neigh = __ipv4_neigh_lookup_noref(dev, key);
2567 neigh = __neigh_lookup_noref(tbl, addr, dev);
2570 neigh = __neigh_create(tbl, addr, dev, false);
2571 err = PTR_ERR(neigh);
2572 if (IS_ERR(neigh)) {
2573 rcu_read_unlock_bh();
2576 err = neigh->output(neigh, skb);
2577 rcu_read_unlock_bh();
2579 else if (index == NEIGH_LINK_TABLE) {
2580 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2581 addr, NULL, skb->len);
2584 err = dev_queue_xmit(skb);
2592 EXPORT_SYMBOL(neigh_xmit);
2594 #ifdef CONFIG_PROC_FS
2596 static struct neighbour *neigh_get_first(struct seq_file *seq)
2598 struct neigh_seq_state *state = seq->private;
2599 struct net *net = seq_file_net(seq);
2600 struct neigh_hash_table *nht = state->nht;
2601 struct neighbour *n = NULL;
2602 int bucket = state->bucket;
2604 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2605 for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
2606 n = rcu_dereference_bh(nht->hash_buckets[bucket]);
2609 if (!net_eq(dev_net(n->dev), net))
2611 if (state->neigh_sub_iter) {
2615 v = state->neigh_sub_iter(state, n, &fakep);
2619 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2621 if (n->nud_state & ~NUD_NOARP)
2624 n = rcu_dereference_bh(n->next);
2630 state->bucket = bucket;
2635 static struct neighbour *neigh_get_next(struct seq_file *seq,
2636 struct neighbour *n,
2639 struct neigh_seq_state *state = seq->private;
2640 struct net *net = seq_file_net(seq);
2641 struct neigh_hash_table *nht = state->nht;
2643 if (state->neigh_sub_iter) {
2644 void *v = state->neigh_sub_iter(state, n, pos);
2648 n = rcu_dereference_bh(n->next);
2652 if (!net_eq(dev_net(n->dev), net))
2654 if (state->neigh_sub_iter) {
2655 void *v = state->neigh_sub_iter(state, n, pos);
2660 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2663 if (n->nud_state & ~NUD_NOARP)
2666 n = rcu_dereference_bh(n->next);
2672 if (++state->bucket >= (1 << nht->hash_shift))
2675 n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
2683 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2685 struct neighbour *n = neigh_get_first(seq);
2690 n = neigh_get_next(seq, n, pos);
2695 return *pos ? NULL : n;
2698 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2700 struct neigh_seq_state *state = seq->private;
2701 struct net *net = seq_file_net(seq);
2702 struct neigh_table *tbl = state->tbl;
2703 struct pneigh_entry *pn = NULL;
2704 int bucket = state->bucket;
2706 state->flags |= NEIGH_SEQ_IS_PNEIGH;
2707 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2708 pn = tbl->phash_buckets[bucket];
2709 while (pn && !net_eq(pneigh_net(pn), net))
2714 state->bucket = bucket;
2719 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2720 struct pneigh_entry *pn,
2723 struct neigh_seq_state *state = seq->private;
2724 struct net *net = seq_file_net(seq);
2725 struct neigh_table *tbl = state->tbl;
2729 } while (pn && !net_eq(pneigh_net(pn), net));
2732 if (++state->bucket > PNEIGH_HASHMASK)
2734 pn = tbl->phash_buckets[state->bucket];
2735 while (pn && !net_eq(pneigh_net(pn), net))
2747 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2749 struct pneigh_entry *pn = pneigh_get_first(seq);
2754 pn = pneigh_get_next(seq, pn, pos);
2759 return *pos ? NULL : pn;
2762 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2764 struct neigh_seq_state *state = seq->private;
2766 loff_t idxpos = *pos;
2768 rc = neigh_get_idx(seq, &idxpos);
2769 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2770 rc = pneigh_get_idx(seq, &idxpos);
2775 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2776 __acquires(tbl->lock)
2779 struct neigh_seq_state *state = seq->private;
2783 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2786 state->nht = rcu_dereference_bh(tbl->nht);
2787 read_lock(&tbl->lock);
2789 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
2791 EXPORT_SYMBOL(neigh_seq_start);
2793 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2795 struct neigh_seq_state *state;
2798 if (v == SEQ_START_TOKEN) {
2799 rc = neigh_get_first(seq);
2803 state = seq->private;
2804 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
2805 rc = neigh_get_next(seq, v, NULL);
2808 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2809 rc = pneigh_get_first(seq);
2811 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
2812 rc = pneigh_get_next(seq, v, NULL);
2818 EXPORT_SYMBOL(neigh_seq_next);
2820 void neigh_seq_stop(struct seq_file *seq, void *v)
2821 __releases(tbl->lock)
2824 struct neigh_seq_state *state = seq->private;
2825 struct neigh_table *tbl = state->tbl;
2827 read_unlock(&tbl->lock);
2828 rcu_read_unlock_bh();
2830 EXPORT_SYMBOL(neigh_seq_stop);
2832 /* statistics via seq_file */
2834 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
2836 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
2840 return SEQ_START_TOKEN;
2842 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
2843 if (!cpu_possible(cpu))
2846 return per_cpu_ptr(tbl->stats, cpu);
2851 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2853 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
2856 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
2857 if (!cpu_possible(cpu))
2860 return per_cpu_ptr(tbl->stats, cpu);
2866 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
2871 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
2873 struct neigh_table *tbl = PDE_DATA(file_inode(seq->file));
2874 struct neigh_statistics *st = v;
2876 if (v == SEQ_START_TOKEN) {
2877 seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards table_fulls\n");
2881 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
2882 "%08lx %08lx %08lx %08lx %08lx %08lx\n",
2883 atomic_read(&tbl->entries),
2894 st->rcv_probes_mcast,
2895 st->rcv_probes_ucast,
2897 st->periodic_gc_runs,
2906 static const struct seq_operations neigh_stat_seq_ops = {
2907 .start = neigh_stat_seq_start,
2908 .next = neigh_stat_seq_next,
2909 .stop = neigh_stat_seq_stop,
2910 .show = neigh_stat_seq_show,
2912 #endif /* CONFIG_PROC_FS */
2914 static inline size_t neigh_nlmsg_size(void)
2916 return NLMSG_ALIGN(sizeof(struct ndmsg))
2917 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2918 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2919 + nla_total_size(sizeof(struct nda_cacheinfo))
2920 + nla_total_size(4); /* NDA_PROBES */
2923 static void __neigh_notify(struct neighbour *n, int type, int flags,
2926 struct net *net = dev_net(n->dev);
2927 struct sk_buff *skb;
2930 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
2934 err = neigh_fill_info(skb, n, pid, 0, type, flags);
2936 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
2937 WARN_ON(err == -EMSGSIZE);
2941 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2945 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2948 void neigh_app_ns(struct neighbour *n)
2950 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
2952 EXPORT_SYMBOL(neigh_app_ns);
2954 #ifdef CONFIG_SYSCTL
2956 static int int_max = INT_MAX;
2957 static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
2959 static int proc_unres_qlen(struct ctl_table *ctl, int write,
2960 void __user *buffer, size_t *lenp, loff_t *ppos)
2963 struct ctl_table tmp = *ctl;
2966 tmp.extra2 = &unres_qlen_max;
2969 size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
2970 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2973 *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
2977 static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
2982 return __in_dev_arp_parms_get_rcu(dev);
2984 return __in6_dev_nd_parms_get_rcu(dev);
2989 static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
2992 struct net_device *dev;
2993 int family = neigh_parms_family(p);
2996 for_each_netdev_rcu(net, dev) {
2997 struct neigh_parms *dst_p =
2998 neigh_get_dev_parms_rcu(dev, family);
3000 if (dst_p && !test_bit(index, dst_p->data_state))
3001 dst_p->data[index] = p->data[index];
3006 static void neigh_proc_update(struct ctl_table *ctl, int write)
3008 struct net_device *dev = ctl->extra1;
3009 struct neigh_parms *p = ctl->extra2;
3010 struct net *net = neigh_parms_net(p);
3011 int index = (int *) ctl->data - p->data;
3016 set_bit(index, p->data_state);
3017 if (index == NEIGH_VAR_DELAY_PROBE_TIME)
3018 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
3019 if (!dev) /* NULL dev means this is default value */
3020 neigh_copy_dflt_parms(net, p, index);
3023 static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
3024 void __user *buffer,
3025 size_t *lenp, loff_t *ppos)
3027 struct ctl_table tmp = *ctl;
3031 tmp.extra2 = &int_max;
3033 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
3034 neigh_proc_update(ctl, write);
3038 int neigh_proc_dointvec(struct ctl_table *ctl, int write,
3039 void __user *buffer, size_t *lenp, loff_t *ppos)
3041 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3043 neigh_proc_update(ctl, write);
3046 EXPORT_SYMBOL(neigh_proc_dointvec);
3048 int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3049 void __user *buffer,
3050 size_t *lenp, loff_t *ppos)
3052 int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3054 neigh_proc_update(ctl, write);
3057 EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3059 static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3060 void __user *buffer,
3061 size_t *lenp, loff_t *ppos)
3063 int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
3065 neigh_proc_update(ctl, write);
3069 int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3070 void __user *buffer,
3071 size_t *lenp, loff_t *ppos)
3073 int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3075 neigh_proc_update(ctl, write);
3078 EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3080 static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3081 void __user *buffer,
3082 size_t *lenp, loff_t *ppos)
3084 int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
3086 neigh_proc_update(ctl, write);
3090 static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
3091 void __user *buffer,
3092 size_t *lenp, loff_t *ppos)
3094 struct neigh_parms *p = ctl->extra2;
3097 if (strcmp(ctl->procname, "base_reachable_time") == 0)
3098 ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3099 else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
3100 ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3104 if (write && ret == 0) {
3105 /* update reachable_time as well, otherwise, the change will
3106 * only be effective after the next time neigh_periodic_work
3107 * decides to recompute it
3110 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
3115 #define NEIGH_PARMS_DATA_OFFSET(index) \
3116 (&((struct neigh_parms *) 0)->data[index])
3118 #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
3119 [NEIGH_VAR_ ## attr] = { \
3121 .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
3122 .maxlen = sizeof(int), \
3124 .proc_handler = proc, \
3127 #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
3128 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
3130 #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3131 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
3133 #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3134 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
3136 #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
3137 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
3139 #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3140 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
3142 #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3143 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
3145 static struct neigh_sysctl_table {
3146 struct ctl_table_header *sysctl_header;
3147 struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3148 } neigh_sysctl_template __read_mostly = {
3150 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
3151 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
3152 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
3153 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
3154 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
3155 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
3156 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
3157 NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
3158 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
3159 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
3160 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
3161 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
3162 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
3163 NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
3164 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
3165 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
3166 [NEIGH_VAR_GC_INTERVAL] = {
3167 .procname = "gc_interval",
3168 .maxlen = sizeof(int),
3170 .proc_handler = proc_dointvec_jiffies,
3172 [NEIGH_VAR_GC_THRESH1] = {
3173 .procname = "gc_thresh1",
3174 .maxlen = sizeof(int),
3178 .proc_handler = proc_dointvec_minmax,
3180 [NEIGH_VAR_GC_THRESH2] = {
3181 .procname = "gc_thresh2",
3182 .maxlen = sizeof(int),
3186 .proc_handler = proc_dointvec_minmax,
3188 [NEIGH_VAR_GC_THRESH3] = {
3189 .procname = "gc_thresh3",
3190 .maxlen = sizeof(int),
3194 .proc_handler = proc_dointvec_minmax,
3200 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
3201 proc_handler *handler)
3204 struct neigh_sysctl_table *t;
3205 const char *dev_name_source;
3206 char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
3209 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
3213 for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
3214 t->neigh_vars[i].data += (long) p;
3215 t->neigh_vars[i].extra1 = dev;
3216 t->neigh_vars[i].extra2 = p;
3220 dev_name_source = dev->name;
3221 /* Terminate the table early */
3222 memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
3223 sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
3225 struct neigh_table *tbl = p->tbl;
3226 dev_name_source = "default";
3227 t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
3228 t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
3229 t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
3230 t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
3235 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
3237 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
3238 /* RetransTime (in milliseconds)*/
3239 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
3240 /* ReachableTime (in milliseconds) */
3241 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
3243 /* Those handlers will update p->reachable_time after
3244 * base_reachable_time(_ms) is set to ensure the new timer starts being
3245 * applied after the next neighbour update instead of waiting for
3246 * neigh_periodic_work to update its value (can be multiple minutes)
3247 * So any handler that replaces them should do this as well
3250 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
3251 neigh_proc_base_reachable_time;
3252 /* ReachableTime (in milliseconds) */
3253 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
3254 neigh_proc_base_reachable_time;
3257 /* Don't export sysctls to unprivileged users */
3258 if (neigh_parms_net(p)->user_ns != &init_user_ns)
3259 t->neigh_vars[0].procname = NULL;
3261 switch (neigh_parms_family(p)) {
3272 snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
3273 p_name, dev_name_source);
3275 register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
3276 if (!t->sysctl_header)
3279 p->sysctl_table = t;
3287 EXPORT_SYMBOL(neigh_sysctl_register);
3289 void neigh_sysctl_unregister(struct neigh_parms *p)
3291 if (p->sysctl_table) {
3292 struct neigh_sysctl_table *t = p->sysctl_table;
3293 p->sysctl_table = NULL;
3294 unregister_net_sysctl_table(t->sysctl_header);
3298 EXPORT_SYMBOL(neigh_sysctl_unregister);
3300 #endif /* CONFIG_SYSCTL */
3302 static int __init neigh_init(void)
3304 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, 0);
3305 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, 0);
3306 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, 0);
3308 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3310 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, 0);
3315 subsys_initcall(neigh_init);