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(unsigned long arg);
57 static void __neigh_notify(struct neighbour *n, int type, int flags);
58 static void neigh_update_notify(struct neighbour *neigh);
59 static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
60 struct net_device *dev);
63 static const struct file_operations neigh_stat_seq_fops;
67 Neighbour hash table buckets are protected with rwlock tbl->lock.
69 - All the scans/updates to hash buckets MUST be made under this lock.
70 - NOTHING clever should be made under this lock: no callbacks
71 to protocol backends, no attempts to send something to network.
72 It will result in deadlocks, if backend/driver wants to use neighbour
74 - If the entry requires some non-trivial actions, increase
75 its reference count and release table lock.
77 Neighbour entries are protected:
78 - with reference count.
79 - with rwlock neigh->lock
81 Reference count prevents destruction.
83 neigh->lock mainly serializes ll address data and its validity state.
84 However, the same lock is used to protect another entry fields:
88 Again, nothing clever shall be made under neigh->lock,
89 the most complicated procedure, which we allow is dev->hard_header.
90 It is supposed, that dev->hard_header is simplistic and does
91 not make callbacks to neighbour tables.
94 static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
100 static void neigh_cleanup_and_release(struct neighbour *neigh)
102 if (neigh->parms->neigh_cleanup)
103 neigh->parms->neigh_cleanup(neigh);
105 __neigh_notify(neigh, RTM_DELNEIGH, 0);
106 neigh_release(neigh);
110 * It is random distribution in the interval (1/2)*base...(3/2)*base.
111 * It corresponds to default IPv6 settings and is not overridable,
112 * because it is really reasonable choice.
115 unsigned long neigh_rand_reach_time(unsigned long base)
117 return base ? (prandom_u32() % base) + (base >> 1) : 0;
119 EXPORT_SYMBOL(neigh_rand_reach_time);
122 static int neigh_forced_gc(struct neigh_table *tbl)
126 struct neigh_hash_table *nht;
128 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
130 write_lock_bh(&tbl->lock);
131 nht = rcu_dereference_protected(tbl->nht,
132 lockdep_is_held(&tbl->lock));
133 for (i = 0; i < (1 << nht->hash_shift); i++) {
135 struct neighbour __rcu **np;
137 np = &nht->hash_buckets[i];
138 while ((n = rcu_dereference_protected(*np,
139 lockdep_is_held(&tbl->lock))) != NULL) {
140 /* Neighbour record may be discarded if:
141 * - nobody refers to it.
142 * - it is not permanent
144 write_lock(&n->lock);
145 if (atomic_read(&n->refcnt) == 1 &&
146 !(n->nud_state & NUD_PERMANENT)) {
147 rcu_assign_pointer(*np,
148 rcu_dereference_protected(n->next,
149 lockdep_is_held(&tbl->lock)));
152 write_unlock(&n->lock);
153 neigh_cleanup_and_release(n);
156 write_unlock(&n->lock);
161 tbl->last_flush = jiffies;
163 write_unlock_bh(&tbl->lock);
168 static void neigh_add_timer(struct neighbour *n, unsigned long when)
171 if (unlikely(mod_timer(&n->timer, when))) {
172 printk("NEIGH: BUG, double timer add, state is %x\n",
178 static int neigh_del_timer(struct neighbour *n)
180 if ((n->nud_state & NUD_IN_TIMER) &&
181 del_timer(&n->timer)) {
188 static void pneigh_queue_purge(struct sk_buff_head *list)
192 while ((skb = skb_dequeue(list)) != NULL) {
198 static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
201 struct neigh_hash_table *nht;
203 nht = rcu_dereference_protected(tbl->nht,
204 lockdep_is_held(&tbl->lock));
206 for (i = 0; i < (1 << nht->hash_shift); i++) {
208 struct neighbour __rcu **np = &nht->hash_buckets[i];
210 while ((n = rcu_dereference_protected(*np,
211 lockdep_is_held(&tbl->lock))) != NULL) {
212 if (dev && n->dev != dev) {
216 rcu_assign_pointer(*np,
217 rcu_dereference_protected(n->next,
218 lockdep_is_held(&tbl->lock)));
219 write_lock(&n->lock);
223 if (atomic_read(&n->refcnt) != 1) {
224 /* The most unpleasant situation.
225 We must destroy neighbour entry,
226 but someone still uses it.
228 The destroy will be delayed until
229 the last user releases us, but
230 we must kill timers etc. and move
233 __skb_queue_purge(&n->arp_queue);
234 n->arp_queue_len_bytes = 0;
235 n->output = neigh_blackhole;
236 if (n->nud_state & NUD_VALID)
237 n->nud_state = NUD_NOARP;
239 n->nud_state = NUD_NONE;
240 neigh_dbg(2, "neigh %p is stray\n", n);
242 write_unlock(&n->lock);
243 neigh_cleanup_and_release(n);
248 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
250 write_lock_bh(&tbl->lock);
251 neigh_flush_dev(tbl, dev);
252 write_unlock_bh(&tbl->lock);
254 EXPORT_SYMBOL(neigh_changeaddr);
256 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
258 write_lock_bh(&tbl->lock);
259 neigh_flush_dev(tbl, dev);
260 pneigh_ifdown_and_unlock(tbl, dev);
262 del_timer_sync(&tbl->proxy_timer);
263 pneigh_queue_purge(&tbl->proxy_queue);
266 EXPORT_SYMBOL(neigh_ifdown);
268 static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
270 struct neighbour *n = NULL;
271 unsigned long now = jiffies;
274 entries = atomic_inc_return(&tbl->entries) - 1;
275 if (entries >= tbl->gc_thresh3 ||
276 (entries >= tbl->gc_thresh2 &&
277 time_after(now, tbl->last_flush + 5 * HZ))) {
278 if (!neigh_forced_gc(tbl) &&
279 entries >= tbl->gc_thresh3) {
280 net_info_ratelimited("%s: neighbor table overflow!\n",
282 NEIGH_CACHE_STAT_INC(tbl, table_fulls);
287 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
291 __skb_queue_head_init(&n->arp_queue);
292 rwlock_init(&n->lock);
293 seqlock_init(&n->ha_lock);
294 n->updated = n->used = now;
295 n->nud_state = NUD_NONE;
296 n->output = neigh_blackhole;
297 seqlock_init(&n->hh.hh_lock);
298 n->parms = neigh_parms_clone(&tbl->parms);
299 setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
301 NEIGH_CACHE_STAT_INC(tbl, allocs);
303 atomic_set(&n->refcnt, 1);
309 atomic_dec(&tbl->entries);
313 static void neigh_get_hash_rnd(u32 *x)
315 get_random_bytes(x, sizeof(*x));
319 static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
321 size_t size = (1 << shift) * sizeof(struct neighbour *);
322 struct neigh_hash_table *ret;
323 struct neighbour __rcu **buckets;
326 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
329 if (size <= PAGE_SIZE) {
330 buckets = kzalloc(size, GFP_ATOMIC);
332 buckets = (struct neighbour __rcu **)
333 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
335 kmemleak_alloc(buckets, size, 1, GFP_ATOMIC);
341 ret->hash_buckets = buckets;
342 ret->hash_shift = shift;
343 for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
344 neigh_get_hash_rnd(&ret->hash_rnd[i]);
348 static void neigh_hash_free_rcu(struct rcu_head *head)
350 struct neigh_hash_table *nht = container_of(head,
351 struct neigh_hash_table,
353 size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
354 struct neighbour __rcu **buckets = nht->hash_buckets;
356 if (size <= PAGE_SIZE) {
359 kmemleak_free(buckets);
360 free_pages((unsigned long)buckets, get_order(size));
365 static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
366 unsigned long new_shift)
368 unsigned int i, hash;
369 struct neigh_hash_table *new_nht, *old_nht;
371 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
373 old_nht = rcu_dereference_protected(tbl->nht,
374 lockdep_is_held(&tbl->lock));
375 new_nht = neigh_hash_alloc(new_shift);
379 for (i = 0; i < (1 << old_nht->hash_shift); i++) {
380 struct neighbour *n, *next;
382 for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
383 lockdep_is_held(&tbl->lock));
386 hash = tbl->hash(n->primary_key, n->dev,
389 hash >>= (32 - new_nht->hash_shift);
390 next = rcu_dereference_protected(n->next,
391 lockdep_is_held(&tbl->lock));
393 rcu_assign_pointer(n->next,
394 rcu_dereference_protected(
395 new_nht->hash_buckets[hash],
396 lockdep_is_held(&tbl->lock)));
397 rcu_assign_pointer(new_nht->hash_buckets[hash], n);
401 rcu_assign_pointer(tbl->nht, new_nht);
402 call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
406 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
407 struct net_device *dev)
411 NEIGH_CACHE_STAT_INC(tbl, lookups);
414 n = __neigh_lookup_noref(tbl, pkey, dev);
416 if (!atomic_inc_not_zero(&n->refcnt))
418 NEIGH_CACHE_STAT_INC(tbl, hits);
421 rcu_read_unlock_bh();
424 EXPORT_SYMBOL(neigh_lookup);
426 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
430 int key_len = tbl->key_len;
432 struct neigh_hash_table *nht;
434 NEIGH_CACHE_STAT_INC(tbl, lookups);
437 nht = rcu_dereference_bh(tbl->nht);
438 hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
440 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
442 n = rcu_dereference_bh(n->next)) {
443 if (!memcmp(n->primary_key, pkey, key_len) &&
444 net_eq(dev_net(n->dev), net)) {
445 if (!atomic_inc_not_zero(&n->refcnt))
447 NEIGH_CACHE_STAT_INC(tbl, hits);
452 rcu_read_unlock_bh();
455 EXPORT_SYMBOL(neigh_lookup_nodev);
457 struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
458 struct net_device *dev, bool want_ref)
461 int key_len = tbl->key_len;
463 struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
464 struct neigh_hash_table *nht;
467 rc = ERR_PTR(-ENOBUFS);
471 memcpy(n->primary_key, pkey, key_len);
475 /* Protocol specific setup. */
476 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
478 goto out_neigh_release;
481 if (dev->netdev_ops->ndo_neigh_construct) {
482 error = dev->netdev_ops->ndo_neigh_construct(dev, n);
485 goto out_neigh_release;
489 /* Device specific setup. */
490 if (n->parms->neigh_setup &&
491 (error = n->parms->neigh_setup(n)) < 0) {
493 goto out_neigh_release;
496 n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
498 write_lock_bh(&tbl->lock);
499 nht = rcu_dereference_protected(tbl->nht,
500 lockdep_is_held(&tbl->lock));
502 if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
503 nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
505 hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
507 if (n->parms->dead) {
508 rc = ERR_PTR(-EINVAL);
512 for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
513 lockdep_is_held(&tbl->lock));
515 n1 = rcu_dereference_protected(n1->next,
516 lockdep_is_held(&tbl->lock))) {
517 if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
528 rcu_assign_pointer(n->next,
529 rcu_dereference_protected(nht->hash_buckets[hash_val],
530 lockdep_is_held(&tbl->lock)));
531 rcu_assign_pointer(nht->hash_buckets[hash_val], n);
532 write_unlock_bh(&tbl->lock);
533 neigh_dbg(2, "neigh %p is created\n", n);
538 write_unlock_bh(&tbl->lock);
543 EXPORT_SYMBOL(__neigh_create);
545 static u32 pneigh_hash(const void *pkey, int key_len)
547 u32 hash_val = *(u32 *)(pkey + key_len - 4);
548 hash_val ^= (hash_val >> 16);
549 hash_val ^= hash_val >> 8;
550 hash_val ^= hash_val >> 4;
551 hash_val &= PNEIGH_HASHMASK;
555 static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
559 struct net_device *dev)
562 if (!memcmp(n->key, pkey, key_len) &&
563 net_eq(pneigh_net(n), net) &&
564 (n->dev == dev || !n->dev))
571 struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
572 struct net *net, const void *pkey, struct net_device *dev)
574 int key_len = tbl->key_len;
575 u32 hash_val = pneigh_hash(pkey, key_len);
577 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
578 net, pkey, key_len, dev);
580 EXPORT_SYMBOL_GPL(__pneigh_lookup);
582 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
583 struct net *net, const void *pkey,
584 struct net_device *dev, int creat)
586 struct pneigh_entry *n;
587 int key_len = tbl->key_len;
588 u32 hash_val = pneigh_hash(pkey, key_len);
590 read_lock_bh(&tbl->lock);
591 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
592 net, pkey, key_len, dev);
593 read_unlock_bh(&tbl->lock);
600 n = kzalloc(sizeof(*n) + key_len, GFP_KERNEL);
604 write_pnet(&n->net, net);
605 memcpy(n->key, pkey, key_len);
610 if (tbl->pconstructor && tbl->pconstructor(n)) {
618 write_lock_bh(&tbl->lock);
619 n->next = tbl->phash_buckets[hash_val];
620 tbl->phash_buckets[hash_val] = n;
621 write_unlock_bh(&tbl->lock);
625 EXPORT_SYMBOL(pneigh_lookup);
628 int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
629 struct net_device *dev)
631 struct pneigh_entry *n, **np;
632 int key_len = tbl->key_len;
633 u32 hash_val = pneigh_hash(pkey, key_len);
635 write_lock_bh(&tbl->lock);
636 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
638 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
639 net_eq(pneigh_net(n), net)) {
641 write_unlock_bh(&tbl->lock);
642 if (tbl->pdestructor)
650 write_unlock_bh(&tbl->lock);
654 static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
655 struct net_device *dev)
657 struct pneigh_entry *n, **np, *freelist = NULL;
660 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
661 np = &tbl->phash_buckets[h];
662 while ((n = *np) != NULL) {
663 if (!dev || n->dev == dev) {
672 write_unlock_bh(&tbl->lock);
673 while ((n = freelist)) {
676 if (tbl->pdestructor)
685 static void neigh_parms_destroy(struct neigh_parms *parms);
687 static inline void neigh_parms_put(struct neigh_parms *parms)
689 if (atomic_dec_and_test(&parms->refcnt))
690 neigh_parms_destroy(parms);
694 * neighbour must already be out of the table;
697 void neigh_destroy(struct neighbour *neigh)
699 struct net_device *dev = neigh->dev;
701 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
704 pr_warn("Destroying alive neighbour %p\n", neigh);
709 if (neigh_del_timer(neigh))
710 pr_warn("Impossible event\n");
712 write_lock_bh(&neigh->lock);
713 __skb_queue_purge(&neigh->arp_queue);
714 write_unlock_bh(&neigh->lock);
715 neigh->arp_queue_len_bytes = 0;
717 if (dev->netdev_ops->ndo_neigh_destroy)
718 dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
721 neigh_parms_put(neigh->parms);
723 neigh_dbg(2, "neigh %p is destroyed\n", neigh);
725 atomic_dec(&neigh->tbl->entries);
726 kfree_rcu(neigh, rcu);
728 EXPORT_SYMBOL(neigh_destroy);
730 /* Neighbour state is suspicious;
733 Called with write_locked neigh.
735 static void neigh_suspect(struct neighbour *neigh)
737 neigh_dbg(2, "neigh %p is suspected\n", neigh);
739 neigh->output = neigh->ops->output;
742 /* Neighbour state is OK;
745 Called with write_locked neigh.
747 static void neigh_connect(struct neighbour *neigh)
749 neigh_dbg(2, "neigh %p is connected\n", neigh);
751 neigh->output = neigh->ops->connected_output;
754 static void neigh_periodic_work(struct work_struct *work)
756 struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
758 struct neighbour __rcu **np;
760 struct neigh_hash_table *nht;
762 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
764 write_lock_bh(&tbl->lock);
765 nht = rcu_dereference_protected(tbl->nht,
766 lockdep_is_held(&tbl->lock));
769 * periodically recompute ReachableTime from random function
772 if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
773 struct neigh_parms *p;
774 tbl->last_rand = jiffies;
775 list_for_each_entry(p, &tbl->parms_list, list)
777 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
780 if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
783 for (i = 0 ; i < (1 << nht->hash_shift); i++) {
784 np = &nht->hash_buckets[i];
786 while ((n = rcu_dereference_protected(*np,
787 lockdep_is_held(&tbl->lock))) != NULL) {
790 write_lock(&n->lock);
792 state = n->nud_state;
793 if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
794 write_unlock(&n->lock);
798 if (time_before(n->used, n->confirmed))
799 n->used = n->confirmed;
801 if (atomic_read(&n->refcnt) == 1 &&
802 (state == NUD_FAILED ||
803 time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
806 write_unlock(&n->lock);
807 neigh_cleanup_and_release(n);
810 write_unlock(&n->lock);
816 * It's fine to release lock here, even if hash table
817 * grows while we are preempted.
819 write_unlock_bh(&tbl->lock);
821 write_lock_bh(&tbl->lock);
822 nht = rcu_dereference_protected(tbl->nht,
823 lockdep_is_held(&tbl->lock));
826 /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
827 * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
828 * BASE_REACHABLE_TIME.
830 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
831 NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
832 write_unlock_bh(&tbl->lock);
835 static __inline__ int neigh_max_probes(struct neighbour *n)
837 struct neigh_parms *p = n->parms;
838 return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
839 (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
840 NEIGH_VAR(p, MCAST_PROBES));
843 static void neigh_invalidate(struct neighbour *neigh)
844 __releases(neigh->lock)
845 __acquires(neigh->lock)
849 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
850 neigh_dbg(2, "neigh %p is failed\n", neigh);
851 neigh->updated = jiffies;
853 /* It is very thin place. report_unreachable is very complicated
854 routine. Particularly, it can hit the same neighbour entry!
856 So that, we try to be accurate and avoid dead loop. --ANK
858 while (neigh->nud_state == NUD_FAILED &&
859 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
860 write_unlock(&neigh->lock);
861 neigh->ops->error_report(neigh, skb);
862 write_lock(&neigh->lock);
864 __skb_queue_purge(&neigh->arp_queue);
865 neigh->arp_queue_len_bytes = 0;
868 static void neigh_probe(struct neighbour *neigh)
869 __releases(neigh->lock)
871 struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
872 /* keep skb alive even if arp_queue overflows */
874 skb = skb_clone(skb, GFP_ATOMIC);
875 write_unlock(&neigh->lock);
876 if (neigh->ops->solicit)
877 neigh->ops->solicit(neigh, skb);
878 atomic_inc(&neigh->probes);
882 /* Called when a timer expires for a neighbour entry. */
884 static void neigh_timer_handler(unsigned long arg)
886 unsigned long now, next;
887 struct neighbour *neigh = (struct neighbour *)arg;
891 write_lock(&neigh->lock);
893 state = neigh->nud_state;
897 if (!(state & NUD_IN_TIMER))
900 if (state & NUD_REACHABLE) {
901 if (time_before_eq(now,
902 neigh->confirmed + neigh->parms->reachable_time)) {
903 neigh_dbg(2, "neigh %p is still alive\n", neigh);
904 next = neigh->confirmed + neigh->parms->reachable_time;
905 } else if (time_before_eq(now,
907 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
908 neigh_dbg(2, "neigh %p is delayed\n", neigh);
909 neigh->nud_state = NUD_DELAY;
910 neigh->updated = jiffies;
911 neigh_suspect(neigh);
912 next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
914 neigh_dbg(2, "neigh %p is suspected\n", neigh);
915 neigh->nud_state = NUD_STALE;
916 neigh->updated = jiffies;
917 neigh_suspect(neigh);
920 } else if (state & NUD_DELAY) {
921 if (time_before_eq(now,
923 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
924 neigh_dbg(2, "neigh %p is now reachable\n", neigh);
925 neigh->nud_state = NUD_REACHABLE;
926 neigh->updated = jiffies;
927 neigh_connect(neigh);
929 next = neigh->confirmed + neigh->parms->reachable_time;
931 neigh_dbg(2, "neigh %p is probed\n", neigh);
932 neigh->nud_state = NUD_PROBE;
933 neigh->updated = jiffies;
934 atomic_set(&neigh->probes, 0);
936 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
939 /* NUD_PROBE|NUD_INCOMPLETE */
940 next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
943 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
944 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
945 neigh->nud_state = NUD_FAILED;
947 neigh_invalidate(neigh);
951 if (neigh->nud_state & NUD_IN_TIMER) {
952 if (time_before(next, jiffies + HZ/2))
953 next = jiffies + HZ/2;
954 if (!mod_timer(&neigh->timer, next))
957 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
961 write_unlock(&neigh->lock);
965 neigh_update_notify(neigh);
967 neigh_release(neigh);
970 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
973 bool immediate_probe = false;
975 write_lock_bh(&neigh->lock);
978 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
983 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
984 if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
985 NEIGH_VAR(neigh->parms, APP_PROBES)) {
986 unsigned long next, now = jiffies;
988 atomic_set(&neigh->probes,
989 NEIGH_VAR(neigh->parms, UCAST_PROBES));
990 neigh_del_timer(neigh);
991 neigh->nud_state = NUD_INCOMPLETE;
992 neigh->updated = now;
993 next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
995 neigh_add_timer(neigh, next);
996 immediate_probe = true;
998 neigh->nud_state = NUD_FAILED;
999 neigh->updated = jiffies;
1000 write_unlock_bh(&neigh->lock);
1005 } else if (neigh->nud_state & NUD_STALE) {
1006 neigh_dbg(2, "neigh %p is delayed\n", neigh);
1007 neigh_del_timer(neigh);
1008 neigh->nud_state = NUD_DELAY;
1009 neigh->updated = jiffies;
1010 neigh_add_timer(neigh, jiffies +
1011 NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
1014 if (neigh->nud_state == NUD_INCOMPLETE) {
1016 while (neigh->arp_queue_len_bytes + skb->truesize >
1017 NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
1018 struct sk_buff *buff;
1020 buff = __skb_dequeue(&neigh->arp_queue);
1023 neigh->arp_queue_len_bytes -= buff->truesize;
1025 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
1028 __skb_queue_tail(&neigh->arp_queue, skb);
1029 neigh->arp_queue_len_bytes += skb->truesize;
1034 if (immediate_probe)
1037 write_unlock(&neigh->lock);
1042 if (neigh->nud_state & NUD_STALE)
1044 write_unlock_bh(&neigh->lock);
1048 EXPORT_SYMBOL(__neigh_event_send);
1050 static void neigh_update_hhs(struct neighbour *neigh)
1052 struct hh_cache *hh;
1053 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
1056 if (neigh->dev->header_ops)
1057 update = neigh->dev->header_ops->cache_update;
1061 if (READ_ONCE(hh->hh_len)) {
1062 write_seqlock_bh(&hh->hh_lock);
1063 update(hh, neigh->dev, neigh->ha);
1064 write_sequnlock_bh(&hh->hh_lock);
1071 /* Generic update routine.
1072 -- lladdr is new lladdr or NULL, if it is not supplied.
1073 -- new is new state.
1075 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1077 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
1078 lladdr instead of overriding it
1080 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
1082 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
1084 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
1087 Caller MUST hold reference count on the entry.
1090 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1096 struct net_device *dev;
1097 int update_isrouter = 0;
1099 write_lock_bh(&neigh->lock);
1102 old = neigh->nud_state;
1105 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
1106 (old & (NUD_NOARP | NUD_PERMANENT)))
1111 if (!(new & NUD_VALID)) {
1112 neigh_del_timer(neigh);
1113 if (old & NUD_CONNECTED)
1114 neigh_suspect(neigh);
1115 neigh->nud_state = new;
1117 notify = old & NUD_VALID;
1118 if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
1119 (new & NUD_FAILED)) {
1120 neigh_invalidate(neigh);
1126 /* Compare new lladdr with cached one */
1127 if (!dev->addr_len) {
1128 /* First case: device needs no address. */
1130 } else if (lladdr) {
1131 /* The second case: if something is already cached
1132 and a new address is proposed:
1134 - if they are different, check override flag
1136 if ((old & NUD_VALID) &&
1137 !memcmp(lladdr, neigh->ha, dev->addr_len))
1140 /* No address is supplied; if we know something,
1141 use it, otherwise discard the request.
1144 if (!(old & NUD_VALID))
1149 /* Update confirmed timestamp for neighbour entry after we
1150 * received ARP packet even if it doesn't change IP to MAC binding.
1152 if (new & NUD_CONNECTED)
1153 neigh->confirmed = jiffies;
1155 /* If entry was valid and address is not changed,
1156 do not change entry state, if new one is STALE.
1159 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1160 if (old & NUD_VALID) {
1161 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1162 update_isrouter = 0;
1163 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1164 (old & NUD_CONNECTED)) {
1170 if (lladdr == neigh->ha && new == NUD_STALE &&
1171 !(flags & NEIGH_UPDATE_F_ADMIN))
1176 /* Update timestamp only once we know we will make a change to the
1177 * neighbour entry. Otherwise we risk to move the locktime window with
1178 * noop updates and ignore relevant ARP updates.
1180 if (new != old || lladdr != neigh->ha)
1181 neigh->updated = jiffies;
1184 neigh_del_timer(neigh);
1185 if (new & NUD_PROBE)
1186 atomic_set(&neigh->probes, 0);
1187 if (new & NUD_IN_TIMER)
1188 neigh_add_timer(neigh, (jiffies +
1189 ((new & NUD_REACHABLE) ?
1190 neigh->parms->reachable_time :
1192 neigh->nud_state = new;
1196 if (lladdr != neigh->ha) {
1197 write_seqlock(&neigh->ha_lock);
1198 memcpy(&neigh->ha, lladdr, dev->addr_len);
1199 write_sequnlock(&neigh->ha_lock);
1200 neigh_update_hhs(neigh);
1201 if (!(new & NUD_CONNECTED))
1202 neigh->confirmed = jiffies -
1203 (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
1208 if (new & NUD_CONNECTED)
1209 neigh_connect(neigh);
1211 neigh_suspect(neigh);
1212 if (!(old & NUD_VALID)) {
1213 struct sk_buff *skb;
1215 /* Again: avoid dead loop if something went wrong */
1217 while (neigh->nud_state & NUD_VALID &&
1218 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1219 struct dst_entry *dst = skb_dst(skb);
1220 struct neighbour *n2, *n1 = neigh;
1221 write_unlock_bh(&neigh->lock);
1225 /* Why not just use 'neigh' as-is? The problem is that
1226 * things such as shaper, eql, and sch_teql can end up
1227 * using alternative, different, neigh objects to output
1228 * the packet in the output path. So what we need to do
1229 * here is re-lookup the top-level neigh in the path so
1230 * we can reinject the packet there.
1233 if (dst && dst->obsolete != DST_OBSOLETE_DEAD) {
1234 n2 = dst_neigh_lookup_skb(dst, skb);
1238 n1->output(n1, skb);
1243 write_lock_bh(&neigh->lock);
1245 __skb_queue_purge(&neigh->arp_queue);
1246 neigh->arp_queue_len_bytes = 0;
1249 if (update_isrouter) {
1250 neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
1251 (neigh->flags | NTF_ROUTER) :
1252 (neigh->flags & ~NTF_ROUTER);
1254 write_unlock_bh(&neigh->lock);
1257 neigh_update_notify(neigh);
1261 EXPORT_SYMBOL(neigh_update);
1263 /* Update the neigh to listen temporarily for probe responses, even if it is
1264 * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
1266 void __neigh_set_probe_once(struct neighbour *neigh)
1270 neigh->updated = jiffies;
1271 if (!(neigh->nud_state & NUD_FAILED))
1273 neigh->nud_state = NUD_INCOMPLETE;
1274 atomic_set(&neigh->probes, neigh_max_probes(neigh));
1275 neigh_add_timer(neigh,
1276 jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
1278 EXPORT_SYMBOL(__neigh_set_probe_once);
1280 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1281 u8 *lladdr, void *saddr,
1282 struct net_device *dev)
1284 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1285 lladdr || !dev->addr_len);
1287 neigh_update(neigh, lladdr, NUD_STALE,
1288 NEIGH_UPDATE_F_OVERRIDE);
1291 EXPORT_SYMBOL(neigh_event_ns);
1293 /* called with read_lock_bh(&n->lock); */
1294 static void neigh_hh_init(struct neighbour *n)
1296 struct net_device *dev = n->dev;
1297 __be16 prot = n->tbl->protocol;
1298 struct hh_cache *hh = &n->hh;
1300 write_lock_bh(&n->lock);
1302 /* Only one thread can come in here and initialize the
1306 dev->header_ops->cache(n, hh, prot);
1308 write_unlock_bh(&n->lock);
1311 /* Slow and careful. */
1313 int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1317 if (!neigh_event_send(neigh, skb)) {
1319 struct net_device *dev = neigh->dev;
1322 if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len))
1323 neigh_hh_init(neigh);
1326 __skb_pull(skb, skb_network_offset(skb));
1327 seq = read_seqbegin(&neigh->ha_lock);
1328 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1329 neigh->ha, NULL, skb->len);
1330 } while (read_seqretry(&neigh->ha_lock, seq));
1333 rc = dev_queue_xmit(skb);
1344 EXPORT_SYMBOL(neigh_resolve_output);
1346 /* As fast as possible without hh cache */
1348 int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
1350 struct net_device *dev = neigh->dev;
1355 __skb_pull(skb, skb_network_offset(skb));
1356 seq = read_seqbegin(&neigh->ha_lock);
1357 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1358 neigh->ha, NULL, skb->len);
1359 } while (read_seqretry(&neigh->ha_lock, seq));
1362 err = dev_queue_xmit(skb);
1369 EXPORT_SYMBOL(neigh_connected_output);
1371 int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1373 return dev_queue_xmit(skb);
1375 EXPORT_SYMBOL(neigh_direct_output);
1377 static void neigh_proxy_process(unsigned long arg)
1379 struct neigh_table *tbl = (struct neigh_table *)arg;
1380 long sched_next = 0;
1381 unsigned long now = jiffies;
1382 struct sk_buff *skb, *n;
1384 spin_lock(&tbl->proxy_queue.lock);
1386 skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1387 long tdif = NEIGH_CB(skb)->sched_next - now;
1390 struct net_device *dev = skb->dev;
1392 __skb_unlink(skb, &tbl->proxy_queue);
1393 if (tbl->proxy_redo && netif_running(dev)) {
1395 tbl->proxy_redo(skb);
1402 } else if (!sched_next || tdif < sched_next)
1405 del_timer(&tbl->proxy_timer);
1407 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1408 spin_unlock(&tbl->proxy_queue.lock);
1411 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1412 struct sk_buff *skb)
1414 unsigned long now = jiffies;
1416 unsigned long sched_next = now + (prandom_u32() %
1417 NEIGH_VAR(p, PROXY_DELAY));
1419 if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
1424 NEIGH_CB(skb)->sched_next = sched_next;
1425 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1427 spin_lock(&tbl->proxy_queue.lock);
1428 if (del_timer(&tbl->proxy_timer)) {
1429 if (time_before(tbl->proxy_timer.expires, sched_next))
1430 sched_next = tbl->proxy_timer.expires;
1434 __skb_queue_tail(&tbl->proxy_queue, skb);
1435 mod_timer(&tbl->proxy_timer, sched_next);
1436 spin_unlock(&tbl->proxy_queue.lock);
1438 EXPORT_SYMBOL(pneigh_enqueue);
1440 static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
1441 struct net *net, int ifindex)
1443 struct neigh_parms *p;
1445 list_for_each_entry(p, &tbl->parms_list, list) {
1446 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1447 (!p->dev && !ifindex && net_eq(net, &init_net)))
1454 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1455 struct neigh_table *tbl)
1457 struct neigh_parms *p;
1458 struct net *net = dev_net(dev);
1459 const struct net_device_ops *ops = dev->netdev_ops;
1461 p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
1464 atomic_set(&p->refcnt, 1);
1466 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1469 write_pnet(&p->net, net);
1470 p->sysctl_table = NULL;
1472 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
1478 write_lock_bh(&tbl->lock);
1479 list_add(&p->list, &tbl->parms.list);
1480 write_unlock_bh(&tbl->lock);
1482 neigh_parms_data_state_cleanall(p);
1486 EXPORT_SYMBOL(neigh_parms_alloc);
1488 static void neigh_rcu_free_parms(struct rcu_head *head)
1490 struct neigh_parms *parms =
1491 container_of(head, struct neigh_parms, rcu_head);
1493 neigh_parms_put(parms);
1496 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1498 if (!parms || parms == &tbl->parms)
1500 write_lock_bh(&tbl->lock);
1501 list_del(&parms->list);
1503 write_unlock_bh(&tbl->lock);
1505 dev_put(parms->dev);
1506 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1508 EXPORT_SYMBOL(neigh_parms_release);
1510 static void neigh_parms_destroy(struct neigh_parms *parms)
1515 static struct lock_class_key neigh_table_proxy_queue_class;
1517 static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
1519 void neigh_table_init(int index, struct neigh_table *tbl)
1521 unsigned long now = jiffies;
1522 unsigned long phsize;
1524 INIT_LIST_HEAD(&tbl->parms_list);
1525 list_add(&tbl->parms.list, &tbl->parms_list);
1526 write_pnet(&tbl->parms.net, &init_net);
1527 atomic_set(&tbl->parms.refcnt, 1);
1528 tbl->parms.reachable_time =
1529 neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
1531 tbl->stats = alloc_percpu(struct neigh_statistics);
1533 panic("cannot create neighbour cache statistics");
1535 #ifdef CONFIG_PROC_FS
1536 if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
1537 &neigh_stat_seq_fops, tbl))
1538 panic("cannot create neighbour proc dir entry");
1541 RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
1543 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1544 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
1546 if (!tbl->nht || !tbl->phash_buckets)
1547 panic("cannot allocate neighbour cache hashes");
1549 if (!tbl->entry_size)
1550 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
1551 tbl->key_len, NEIGH_PRIV_ALIGN);
1553 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
1555 rwlock_init(&tbl->lock);
1556 INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
1557 queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
1558 tbl->parms.reachable_time);
1559 setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
1560 skb_queue_head_init_class(&tbl->proxy_queue,
1561 &neigh_table_proxy_queue_class);
1563 tbl->last_flush = now;
1564 tbl->last_rand = now + tbl->parms.reachable_time * 20;
1566 neigh_tables[index] = tbl;
1568 EXPORT_SYMBOL(neigh_table_init);
1570 int neigh_table_clear(int index, struct neigh_table *tbl)
1572 neigh_tables[index] = NULL;
1573 /* It is not clean... Fix it to unload IPv6 module safely */
1574 cancel_delayed_work_sync(&tbl->gc_work);
1575 del_timer_sync(&tbl->proxy_timer);
1576 pneigh_queue_purge(&tbl->proxy_queue);
1577 neigh_ifdown(tbl, NULL);
1578 if (atomic_read(&tbl->entries))
1579 pr_crit("neighbour leakage\n");
1581 call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1582 neigh_hash_free_rcu);
1585 kfree(tbl->phash_buckets);
1586 tbl->phash_buckets = NULL;
1588 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1590 free_percpu(tbl->stats);
1595 EXPORT_SYMBOL(neigh_table_clear);
1597 static struct neigh_table *neigh_find_table(int family)
1599 struct neigh_table *tbl = NULL;
1603 tbl = neigh_tables[NEIGH_ARP_TABLE];
1606 tbl = neigh_tables[NEIGH_ND_TABLE];
1609 tbl = neigh_tables[NEIGH_DN_TABLE];
1616 static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh)
1618 struct net *net = sock_net(skb->sk);
1620 struct nlattr *dst_attr;
1621 struct neigh_table *tbl;
1622 struct neighbour *neigh;
1623 struct net_device *dev = NULL;
1627 if (nlmsg_len(nlh) < sizeof(*ndm))
1630 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1631 if (dst_attr == NULL)
1634 ndm = nlmsg_data(nlh);
1635 if (ndm->ndm_ifindex) {
1636 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1643 tbl = neigh_find_table(ndm->ndm_family);
1645 return -EAFNOSUPPORT;
1647 if (nla_len(dst_attr) < tbl->key_len)
1650 if (ndm->ndm_flags & NTF_PROXY) {
1651 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1658 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1659 if (neigh == NULL) {
1664 err = neigh_update(neigh, NULL, NUD_FAILED,
1665 NEIGH_UPDATE_F_OVERRIDE |
1666 NEIGH_UPDATE_F_ADMIN);
1667 neigh_release(neigh);
1673 static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh)
1675 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
1676 struct net *net = sock_net(skb->sk);
1678 struct nlattr *tb[NDA_MAX+1];
1679 struct neigh_table *tbl;
1680 struct net_device *dev = NULL;
1681 struct neighbour *neigh;
1686 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
1691 if (tb[NDA_DST] == NULL)
1694 ndm = nlmsg_data(nlh);
1695 if (ndm->ndm_ifindex) {
1696 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
1702 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1706 tbl = neigh_find_table(ndm->ndm_family);
1708 return -EAFNOSUPPORT;
1710 if (nla_len(tb[NDA_DST]) < tbl->key_len)
1712 dst = nla_data(tb[NDA_DST]);
1713 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
1715 if (ndm->ndm_flags & NTF_PROXY) {
1716 struct pneigh_entry *pn;
1719 pn = pneigh_lookup(tbl, net, dst, dev, 1);
1721 pn->flags = ndm->ndm_flags;
1730 neigh = neigh_lookup(tbl, dst, dev);
1731 if (neigh == NULL) {
1732 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1737 neigh = __neigh_lookup_errno(tbl, dst, dev);
1738 if (IS_ERR(neigh)) {
1739 err = PTR_ERR(neigh);
1743 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1745 neigh_release(neigh);
1749 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
1750 flags &= ~NEIGH_UPDATE_F_OVERRIDE;
1753 if (ndm->ndm_flags & NTF_USE) {
1754 neigh_event_send(neigh, NULL);
1757 err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
1758 neigh_release(neigh);
1764 static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1766 struct nlattr *nest;
1768 nest = nla_nest_start(skb, NDTA_PARMS);
1773 nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
1774 nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) ||
1775 nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
1776 NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
1777 /* approximative value for deprecated QUEUE_LEN (in packets) */
1778 nla_put_u32(skb, NDTPA_QUEUE_LEN,
1779 NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
1780 nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
1781 nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
1782 nla_put_u32(skb, NDTPA_UCAST_PROBES,
1783 NEIGH_VAR(parms, UCAST_PROBES)) ||
1784 nla_put_u32(skb, NDTPA_MCAST_PROBES,
1785 NEIGH_VAR(parms, MCAST_PROBES)) ||
1786 nla_put_u32(skb, NDTPA_MCAST_REPROBES,
1787 NEIGH_VAR(parms, MCAST_REPROBES)) ||
1788 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
1790 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
1791 NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
1792 nla_put_msecs(skb, NDTPA_GC_STALETIME,
1793 NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
1794 nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
1795 NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
1796 nla_put_msecs(skb, NDTPA_RETRANS_TIME,
1797 NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
1798 nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
1799 NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
1800 nla_put_msecs(skb, NDTPA_PROXY_DELAY,
1801 NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
1802 nla_put_msecs(skb, NDTPA_LOCKTIME,
1803 NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
1804 goto nla_put_failure;
1805 return nla_nest_end(skb, nest);
1808 nla_nest_cancel(skb, nest);
1812 static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1813 u32 pid, u32 seq, int type, int flags)
1815 struct nlmsghdr *nlh;
1816 struct ndtmsg *ndtmsg;
1818 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1822 ndtmsg = nlmsg_data(nlh);
1824 read_lock_bh(&tbl->lock);
1825 ndtmsg->ndtm_family = tbl->family;
1826 ndtmsg->ndtm_pad1 = 0;
1827 ndtmsg->ndtm_pad2 = 0;
1829 if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
1830 nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
1831 nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
1832 nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
1833 nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
1834 goto nla_put_failure;
1836 unsigned long now = jiffies;
1837 long flush_delta = now - tbl->last_flush;
1838 long rand_delta = now - tbl->last_rand;
1839 struct neigh_hash_table *nht;
1840 struct ndt_config ndc = {
1841 .ndtc_key_len = tbl->key_len,
1842 .ndtc_entry_size = tbl->entry_size,
1843 .ndtc_entries = atomic_read(&tbl->entries),
1844 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
1845 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
1846 .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
1850 nht = rcu_dereference_bh(tbl->nht);
1851 ndc.ndtc_hash_rnd = nht->hash_rnd[0];
1852 ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
1853 rcu_read_unlock_bh();
1855 if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
1856 goto nla_put_failure;
1861 struct ndt_stats ndst;
1863 memset(&ndst, 0, sizeof(ndst));
1865 for_each_possible_cpu(cpu) {
1866 struct neigh_statistics *st;
1868 st = per_cpu_ptr(tbl->stats, cpu);
1869 ndst.ndts_allocs += st->allocs;
1870 ndst.ndts_destroys += st->destroys;
1871 ndst.ndts_hash_grows += st->hash_grows;
1872 ndst.ndts_res_failed += st->res_failed;
1873 ndst.ndts_lookups += st->lookups;
1874 ndst.ndts_hits += st->hits;
1875 ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
1876 ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
1877 ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
1878 ndst.ndts_forced_gc_runs += st->forced_gc_runs;
1879 ndst.ndts_table_fulls += st->table_fulls;
1882 if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
1884 goto nla_put_failure;
1887 BUG_ON(tbl->parms.dev);
1888 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1889 goto nla_put_failure;
1891 read_unlock_bh(&tbl->lock);
1892 nlmsg_end(skb, nlh);
1896 read_unlock_bh(&tbl->lock);
1897 nlmsg_cancel(skb, nlh);
1901 static int neightbl_fill_param_info(struct sk_buff *skb,
1902 struct neigh_table *tbl,
1903 struct neigh_parms *parms,
1904 u32 pid, u32 seq, int type,
1907 struct ndtmsg *ndtmsg;
1908 struct nlmsghdr *nlh;
1910 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1914 ndtmsg = nlmsg_data(nlh);
1916 read_lock_bh(&tbl->lock);
1917 ndtmsg->ndtm_family = tbl->family;
1918 ndtmsg->ndtm_pad1 = 0;
1919 ndtmsg->ndtm_pad2 = 0;
1921 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1922 neightbl_fill_parms(skb, parms) < 0)
1925 read_unlock_bh(&tbl->lock);
1926 nlmsg_end(skb, nlh);
1929 read_unlock_bh(&tbl->lock);
1930 nlmsg_cancel(skb, nlh);
1934 static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
1935 [NDTA_NAME] = { .type = NLA_STRING },
1936 [NDTA_THRESH1] = { .type = NLA_U32 },
1937 [NDTA_THRESH2] = { .type = NLA_U32 },
1938 [NDTA_THRESH3] = { .type = NLA_U32 },
1939 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
1940 [NDTA_PARMS] = { .type = NLA_NESTED },
1943 static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
1944 [NDTPA_IFINDEX] = { .type = NLA_U32 },
1945 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
1946 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
1947 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
1948 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
1949 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
1950 [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
1951 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
1952 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
1953 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
1954 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
1955 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
1956 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
1957 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
1960 static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh)
1962 struct net *net = sock_net(skb->sk);
1963 struct neigh_table *tbl;
1964 struct ndtmsg *ndtmsg;
1965 struct nlattr *tb[NDTA_MAX+1];
1969 err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
1970 nl_neightbl_policy);
1974 if (tb[NDTA_NAME] == NULL) {
1979 ndtmsg = nlmsg_data(nlh);
1981 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
1982 tbl = neigh_tables[tidx];
1985 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1987 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
1997 * We acquire tbl->lock to be nice to the periodic timers and
1998 * make sure they always see a consistent set of values.
2000 write_lock_bh(&tbl->lock);
2002 if (tb[NDTA_PARMS]) {
2003 struct nlattr *tbp[NDTPA_MAX+1];
2004 struct neigh_parms *p;
2007 err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
2008 nl_ntbl_parm_policy);
2010 goto errout_tbl_lock;
2012 if (tbp[NDTPA_IFINDEX])
2013 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
2015 p = lookup_neigh_parms(tbl, net, ifindex);
2018 goto errout_tbl_lock;
2021 for (i = 1; i <= NDTPA_MAX; i++) {
2026 case NDTPA_QUEUE_LEN:
2027 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2028 nla_get_u32(tbp[i]) *
2029 SKB_TRUESIZE(ETH_FRAME_LEN));
2031 case NDTPA_QUEUE_LENBYTES:
2032 NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
2033 nla_get_u32(tbp[i]));
2035 case NDTPA_PROXY_QLEN:
2036 NEIGH_VAR_SET(p, PROXY_QLEN,
2037 nla_get_u32(tbp[i]));
2039 case NDTPA_APP_PROBES:
2040 NEIGH_VAR_SET(p, APP_PROBES,
2041 nla_get_u32(tbp[i]));
2043 case NDTPA_UCAST_PROBES:
2044 NEIGH_VAR_SET(p, UCAST_PROBES,
2045 nla_get_u32(tbp[i]));
2047 case NDTPA_MCAST_PROBES:
2048 NEIGH_VAR_SET(p, MCAST_PROBES,
2049 nla_get_u32(tbp[i]));
2051 case NDTPA_MCAST_REPROBES:
2052 NEIGH_VAR_SET(p, MCAST_REPROBES,
2053 nla_get_u32(tbp[i]));
2055 case NDTPA_BASE_REACHABLE_TIME:
2056 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2057 nla_get_msecs(tbp[i]));
2058 /* update reachable_time as well, otherwise, the change will
2059 * only be effective after the next time neigh_periodic_work
2060 * decides to recompute it (can be multiple minutes)
2063 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
2065 case NDTPA_GC_STALETIME:
2066 NEIGH_VAR_SET(p, GC_STALETIME,
2067 nla_get_msecs(tbp[i]));
2069 case NDTPA_DELAY_PROBE_TIME:
2070 NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
2071 nla_get_msecs(tbp[i]));
2072 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
2074 case NDTPA_RETRANS_TIME:
2075 NEIGH_VAR_SET(p, RETRANS_TIME,
2076 nla_get_msecs(tbp[i]));
2078 case NDTPA_ANYCAST_DELAY:
2079 NEIGH_VAR_SET(p, ANYCAST_DELAY,
2080 nla_get_msecs(tbp[i]));
2082 case NDTPA_PROXY_DELAY:
2083 NEIGH_VAR_SET(p, PROXY_DELAY,
2084 nla_get_msecs(tbp[i]));
2086 case NDTPA_LOCKTIME:
2087 NEIGH_VAR_SET(p, LOCKTIME,
2088 nla_get_msecs(tbp[i]));
2095 if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
2096 tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
2097 !net_eq(net, &init_net))
2098 goto errout_tbl_lock;
2100 if (tb[NDTA_THRESH1])
2101 tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
2103 if (tb[NDTA_THRESH2])
2104 tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
2106 if (tb[NDTA_THRESH3])
2107 tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
2109 if (tb[NDTA_GC_INTERVAL])
2110 tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
2115 write_unlock_bh(&tbl->lock);
2120 static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2122 struct net *net = sock_net(skb->sk);
2123 int family, tidx, nidx = 0;
2124 int tbl_skip = cb->args[0];
2125 int neigh_skip = cb->args[1];
2126 struct neigh_table *tbl;
2128 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2130 for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
2131 struct neigh_parms *p;
2133 tbl = neigh_tables[tidx];
2137 if (tidx < tbl_skip || (family && tbl->family != family))
2140 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
2141 cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2146 p = list_next_entry(&tbl->parms, list);
2147 list_for_each_entry_from(p, &tbl->parms_list, list) {
2148 if (!net_eq(neigh_parms_net(p), net))
2151 if (nidx < neigh_skip)
2154 if (neightbl_fill_param_info(skb, tbl, p,
2155 NETLINK_CB(cb->skb).portid,
2173 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2174 u32 pid, u32 seq, int type, unsigned int flags)
2176 unsigned long now = jiffies;
2177 struct nda_cacheinfo ci;
2178 struct nlmsghdr *nlh;
2181 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2185 ndm = nlmsg_data(nlh);
2186 ndm->ndm_family = neigh->ops->family;
2189 ndm->ndm_flags = neigh->flags;
2190 ndm->ndm_type = neigh->type;
2191 ndm->ndm_ifindex = neigh->dev->ifindex;
2193 if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
2194 goto nla_put_failure;
2196 read_lock_bh(&neigh->lock);
2197 ndm->ndm_state = neigh->nud_state;
2198 if (neigh->nud_state & NUD_VALID) {
2199 char haddr[MAX_ADDR_LEN];
2201 neigh_ha_snapshot(haddr, neigh, neigh->dev);
2202 if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2203 read_unlock_bh(&neigh->lock);
2204 goto nla_put_failure;
2208 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2209 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2210 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
2211 ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1;
2212 read_unlock_bh(&neigh->lock);
2214 if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
2215 nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
2216 goto nla_put_failure;
2218 nlmsg_end(skb, nlh);
2222 nlmsg_cancel(skb, nlh);
2226 static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
2227 u32 pid, u32 seq, int type, unsigned int flags,
2228 struct neigh_table *tbl)
2230 struct nlmsghdr *nlh;
2233 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2237 ndm = nlmsg_data(nlh);
2238 ndm->ndm_family = tbl->family;
2241 ndm->ndm_flags = pn->flags | NTF_PROXY;
2242 ndm->ndm_type = RTN_UNICAST;
2243 ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
2244 ndm->ndm_state = NUD_NONE;
2246 if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
2247 goto nla_put_failure;
2249 nlmsg_end(skb, nlh);
2253 nlmsg_cancel(skb, nlh);
2257 static void neigh_update_notify(struct neighbour *neigh)
2259 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2260 __neigh_notify(neigh, RTM_NEWNEIGH, 0);
2263 static bool neigh_master_filtered(struct net_device *dev, int master_idx)
2265 struct net_device *master;
2270 master = netdev_master_upper_dev_get(dev);
2271 if (!master || master->ifindex != master_idx)
2277 static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
2279 if (filter_idx && dev->ifindex != filter_idx)
2285 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2286 struct netlink_callback *cb)
2288 struct net *net = sock_net(skb->sk);
2289 const struct nlmsghdr *nlh = cb->nlh;
2290 struct nlattr *tb[NDA_MAX + 1];
2291 struct neighbour *n;
2292 int rc, h, s_h = cb->args[1];
2293 int idx, s_idx = idx = cb->args[2];
2294 struct neigh_hash_table *nht;
2295 int filter_master_idx = 0, filter_idx = 0;
2296 unsigned int flags = NLM_F_MULTI;
2299 err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL);
2301 if (tb[NDA_IFINDEX]) {
2302 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
2304 filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
2306 if (tb[NDA_MASTER]) {
2307 if (nla_len(tb[NDA_MASTER]) != sizeof(u32))
2309 filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
2311 if (filter_idx || filter_master_idx)
2312 flags |= NLM_F_DUMP_FILTERED;
2316 nht = rcu_dereference_bh(tbl->nht);
2318 for (h = s_h; h < (1 << nht->hash_shift); h++) {
2321 for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2323 n = rcu_dereference_bh(n->next)) {
2324 if (!net_eq(dev_net(n->dev), net))
2326 if (neigh_ifindex_filtered(n->dev, filter_idx))
2328 if (neigh_master_filtered(n->dev, filter_master_idx))
2332 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2345 rcu_read_unlock_bh();
2351 static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2352 struct netlink_callback *cb)
2354 struct pneigh_entry *n;
2355 struct net *net = sock_net(skb->sk);
2356 int rc, h, s_h = cb->args[3];
2357 int idx, s_idx = idx = cb->args[4];
2359 read_lock_bh(&tbl->lock);
2361 for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
2364 for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
2365 if (pneigh_net(n) != net)
2369 if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
2372 NLM_F_MULTI, tbl) < 0) {
2373 read_unlock_bh(&tbl->lock);
2382 read_unlock_bh(&tbl->lock);
2391 static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2393 struct neigh_table *tbl;
2398 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2400 /* check for full ndmsg structure presence, family member is
2401 * the same for both structures
2403 if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
2404 ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
2409 for (t = 0; t < NEIGH_NR_TABLES; t++) {
2410 tbl = neigh_tables[t];
2414 if (t < s_t || (family && tbl->family != family))
2417 memset(&cb->args[1], 0, sizeof(cb->args) -
2418 sizeof(cb->args[0]));
2420 err = pneigh_dump_table(tbl, skb, cb);
2422 err = neigh_dump_table(tbl, skb, cb);
2431 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
2434 struct neigh_hash_table *nht;
2437 nht = rcu_dereference_bh(tbl->nht);
2439 read_lock(&tbl->lock); /* avoid resizes */
2440 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2441 struct neighbour *n;
2443 for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2445 n = rcu_dereference_bh(n->next))
2448 read_unlock(&tbl->lock);
2449 rcu_read_unlock_bh();
2451 EXPORT_SYMBOL(neigh_for_each);
2453 /* The tbl->lock must be held as a writer and BH disabled. */
2454 void __neigh_for_each_release(struct neigh_table *tbl,
2455 int (*cb)(struct neighbour *))
2458 struct neigh_hash_table *nht;
2460 nht = rcu_dereference_protected(tbl->nht,
2461 lockdep_is_held(&tbl->lock));
2462 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
2463 struct neighbour *n;
2464 struct neighbour __rcu **np;
2466 np = &nht->hash_buckets[chain];
2467 while ((n = rcu_dereference_protected(*np,
2468 lockdep_is_held(&tbl->lock))) != NULL) {
2471 write_lock(&n->lock);
2474 rcu_assign_pointer(*np,
2475 rcu_dereference_protected(n->next,
2476 lockdep_is_held(&tbl->lock)));
2480 write_unlock(&n->lock);
2482 neigh_cleanup_and_release(n);
2486 EXPORT_SYMBOL(__neigh_for_each_release);
2488 int neigh_xmit(int index, struct net_device *dev,
2489 const void *addr, struct sk_buff *skb)
2491 int err = -EAFNOSUPPORT;
2492 if (likely(index < NEIGH_NR_TABLES)) {
2493 struct neigh_table *tbl;
2494 struct neighbour *neigh;
2496 tbl = neigh_tables[index];
2500 if (index == NEIGH_ARP_TABLE) {
2501 u32 key = *((u32 *)addr);
2503 neigh = __ipv4_neigh_lookup_noref(dev, key);
2505 neigh = __neigh_lookup_noref(tbl, addr, dev);
2508 neigh = __neigh_create(tbl, addr, dev, false);
2509 err = PTR_ERR(neigh);
2510 if (IS_ERR(neigh)) {
2511 rcu_read_unlock_bh();
2514 err = neigh->output(neigh, skb);
2515 rcu_read_unlock_bh();
2517 else if (index == NEIGH_LINK_TABLE) {
2518 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2519 addr, NULL, skb->len);
2522 err = dev_queue_xmit(skb);
2530 EXPORT_SYMBOL(neigh_xmit);
2532 #ifdef CONFIG_PROC_FS
2534 static struct neighbour *neigh_get_first(struct seq_file *seq)
2536 struct neigh_seq_state *state = seq->private;
2537 struct net *net = seq_file_net(seq);
2538 struct neigh_hash_table *nht = state->nht;
2539 struct neighbour *n = NULL;
2540 int bucket = state->bucket;
2542 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2543 for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
2544 n = rcu_dereference_bh(nht->hash_buckets[bucket]);
2547 if (!net_eq(dev_net(n->dev), net))
2549 if (state->neigh_sub_iter) {
2553 v = state->neigh_sub_iter(state, n, &fakep);
2557 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2559 if (n->nud_state & ~NUD_NOARP)
2562 n = rcu_dereference_bh(n->next);
2568 state->bucket = bucket;
2573 static struct neighbour *neigh_get_next(struct seq_file *seq,
2574 struct neighbour *n,
2577 struct neigh_seq_state *state = seq->private;
2578 struct net *net = seq_file_net(seq);
2579 struct neigh_hash_table *nht = state->nht;
2581 if (state->neigh_sub_iter) {
2582 void *v = state->neigh_sub_iter(state, n, pos);
2586 n = rcu_dereference_bh(n->next);
2590 if (!net_eq(dev_net(n->dev), net))
2592 if (state->neigh_sub_iter) {
2593 void *v = state->neigh_sub_iter(state, n, pos);
2598 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2601 if (n->nud_state & ~NUD_NOARP)
2604 n = rcu_dereference_bh(n->next);
2610 if (++state->bucket >= (1 << nht->hash_shift))
2613 n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
2621 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2623 struct neighbour *n = neigh_get_first(seq);
2628 n = neigh_get_next(seq, n, pos);
2633 return *pos ? NULL : n;
2636 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2638 struct neigh_seq_state *state = seq->private;
2639 struct net *net = seq_file_net(seq);
2640 struct neigh_table *tbl = state->tbl;
2641 struct pneigh_entry *pn = NULL;
2642 int bucket = state->bucket;
2644 state->flags |= NEIGH_SEQ_IS_PNEIGH;
2645 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2646 pn = tbl->phash_buckets[bucket];
2647 while (pn && !net_eq(pneigh_net(pn), net))
2652 state->bucket = bucket;
2657 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2658 struct pneigh_entry *pn,
2661 struct neigh_seq_state *state = seq->private;
2662 struct net *net = seq_file_net(seq);
2663 struct neigh_table *tbl = state->tbl;
2667 } while (pn && !net_eq(pneigh_net(pn), net));
2670 if (++state->bucket > PNEIGH_HASHMASK)
2672 pn = tbl->phash_buckets[state->bucket];
2673 while (pn && !net_eq(pneigh_net(pn), net))
2685 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2687 struct pneigh_entry *pn = pneigh_get_first(seq);
2692 pn = pneigh_get_next(seq, pn, pos);
2697 return *pos ? NULL : pn;
2700 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2702 struct neigh_seq_state *state = seq->private;
2704 loff_t idxpos = *pos;
2706 rc = neigh_get_idx(seq, &idxpos);
2707 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2708 rc = pneigh_get_idx(seq, &idxpos);
2713 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2714 __acquires(tbl->lock)
2717 struct neigh_seq_state *state = seq->private;
2721 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2724 state->nht = rcu_dereference_bh(tbl->nht);
2725 read_lock(&tbl->lock);
2727 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
2729 EXPORT_SYMBOL(neigh_seq_start);
2731 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2733 struct neigh_seq_state *state;
2736 if (v == SEQ_START_TOKEN) {
2737 rc = neigh_get_first(seq);
2741 state = seq->private;
2742 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
2743 rc = neigh_get_next(seq, v, NULL);
2746 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2747 rc = pneigh_get_first(seq);
2749 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
2750 rc = pneigh_get_next(seq, v, NULL);
2756 EXPORT_SYMBOL(neigh_seq_next);
2758 void neigh_seq_stop(struct seq_file *seq, void *v)
2759 __releases(tbl->lock)
2762 struct neigh_seq_state *state = seq->private;
2763 struct neigh_table *tbl = state->tbl;
2765 read_unlock(&tbl->lock);
2766 rcu_read_unlock_bh();
2768 EXPORT_SYMBOL(neigh_seq_stop);
2770 /* statistics via seq_file */
2772 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
2774 struct neigh_table *tbl = seq->private;
2778 return SEQ_START_TOKEN;
2780 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
2781 if (!cpu_possible(cpu))
2784 return per_cpu_ptr(tbl->stats, cpu);
2789 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2791 struct neigh_table *tbl = seq->private;
2794 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
2795 if (!cpu_possible(cpu))
2798 return per_cpu_ptr(tbl->stats, cpu);
2804 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
2809 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
2811 struct neigh_table *tbl = seq->private;
2812 struct neigh_statistics *st = v;
2814 if (v == SEQ_START_TOKEN) {
2815 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");
2819 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
2820 "%08lx %08lx %08lx %08lx %08lx %08lx\n",
2821 atomic_read(&tbl->entries),
2832 st->rcv_probes_mcast,
2833 st->rcv_probes_ucast,
2835 st->periodic_gc_runs,
2844 static const struct seq_operations neigh_stat_seq_ops = {
2845 .start = neigh_stat_seq_start,
2846 .next = neigh_stat_seq_next,
2847 .stop = neigh_stat_seq_stop,
2848 .show = neigh_stat_seq_show,
2851 static int neigh_stat_seq_open(struct inode *inode, struct file *file)
2853 int ret = seq_open(file, &neigh_stat_seq_ops);
2856 struct seq_file *sf = file->private_data;
2857 sf->private = PDE_DATA(inode);
2862 static const struct file_operations neigh_stat_seq_fops = {
2863 .owner = THIS_MODULE,
2864 .open = neigh_stat_seq_open,
2866 .llseek = seq_lseek,
2867 .release = seq_release,
2870 #endif /* CONFIG_PROC_FS */
2872 static inline size_t neigh_nlmsg_size(void)
2874 return NLMSG_ALIGN(sizeof(struct ndmsg))
2875 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2876 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2877 + nla_total_size(sizeof(struct nda_cacheinfo))
2878 + nla_total_size(4); /* NDA_PROBES */
2881 static void __neigh_notify(struct neighbour *n, int type, int flags)
2883 struct net *net = dev_net(n->dev);
2884 struct sk_buff *skb;
2887 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
2891 err = neigh_fill_info(skb, n, 0, 0, type, flags);
2893 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
2894 WARN_ON(err == -EMSGSIZE);
2898 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2902 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2905 void neigh_app_ns(struct neighbour *n)
2907 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
2909 EXPORT_SYMBOL(neigh_app_ns);
2911 #ifdef CONFIG_SYSCTL
2913 static int int_max = INT_MAX;
2914 static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
2916 static int proc_unres_qlen(struct ctl_table *ctl, int write,
2917 void __user *buffer, size_t *lenp, loff_t *ppos)
2920 struct ctl_table tmp = *ctl;
2923 tmp.extra2 = &unres_qlen_max;
2926 size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
2927 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2930 *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
2934 static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
2939 return __in_dev_arp_parms_get_rcu(dev);
2941 return __in6_dev_nd_parms_get_rcu(dev);
2946 static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
2949 struct net_device *dev;
2950 int family = neigh_parms_family(p);
2953 for_each_netdev_rcu(net, dev) {
2954 struct neigh_parms *dst_p =
2955 neigh_get_dev_parms_rcu(dev, family);
2957 if (dst_p && !test_bit(index, dst_p->data_state))
2958 dst_p->data[index] = p->data[index];
2963 static void neigh_proc_update(struct ctl_table *ctl, int write)
2965 struct net_device *dev = ctl->extra1;
2966 struct neigh_parms *p = ctl->extra2;
2967 struct net *net = neigh_parms_net(p);
2968 int index = (int *) ctl->data - p->data;
2973 set_bit(index, p->data_state);
2974 if (index == NEIGH_VAR_DELAY_PROBE_TIME)
2975 call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
2976 if (!dev) /* NULL dev means this is default value */
2977 neigh_copy_dflt_parms(net, p, index);
2980 static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
2981 void __user *buffer,
2982 size_t *lenp, loff_t *ppos)
2984 struct ctl_table tmp = *ctl;
2988 tmp.extra2 = &int_max;
2990 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
2991 neigh_proc_update(ctl, write);
2995 int neigh_proc_dointvec(struct ctl_table *ctl, int write,
2996 void __user *buffer, size_t *lenp, loff_t *ppos)
2998 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3000 neigh_proc_update(ctl, write);
3003 EXPORT_SYMBOL(neigh_proc_dointvec);
3005 int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3006 void __user *buffer,
3007 size_t *lenp, loff_t *ppos)
3009 int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3011 neigh_proc_update(ctl, write);
3014 EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
3016 static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
3017 void __user *buffer,
3018 size_t *lenp, loff_t *ppos)
3020 int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
3022 neigh_proc_update(ctl, write);
3026 int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3027 void __user *buffer,
3028 size_t *lenp, loff_t *ppos)
3030 int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3032 neigh_proc_update(ctl, write);
3035 EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
3037 static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
3038 void __user *buffer,
3039 size_t *lenp, loff_t *ppos)
3041 int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
3043 neigh_proc_update(ctl, write);
3047 static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
3048 void __user *buffer,
3049 size_t *lenp, loff_t *ppos)
3051 struct neigh_parms *p = ctl->extra2;
3054 if (strcmp(ctl->procname, "base_reachable_time") == 0)
3055 ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
3056 else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
3057 ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
3061 if (write && ret == 0) {
3062 /* update reachable_time as well, otherwise, the change will
3063 * only be effective after the next time neigh_periodic_work
3064 * decides to recompute it
3067 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
3072 #define NEIGH_PARMS_DATA_OFFSET(index) \
3073 (&((struct neigh_parms *) 0)->data[index])
3075 #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
3076 [NEIGH_VAR_ ## attr] = { \
3078 .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
3079 .maxlen = sizeof(int), \
3081 .proc_handler = proc, \
3084 #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
3085 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
3087 #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
3088 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
3090 #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
3091 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
3093 #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
3094 NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
3096 #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
3097 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
3099 #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
3100 NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
3102 static struct neigh_sysctl_table {
3103 struct ctl_table_header *sysctl_header;
3104 struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
3105 } neigh_sysctl_template __read_mostly = {
3107 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
3108 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
3109 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
3110 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
3111 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
3112 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
3113 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
3114 NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
3115 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
3116 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
3117 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
3118 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
3119 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
3120 NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
3121 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
3122 NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
3123 [NEIGH_VAR_GC_INTERVAL] = {
3124 .procname = "gc_interval",
3125 .maxlen = sizeof(int),
3127 .proc_handler = proc_dointvec_jiffies,
3129 [NEIGH_VAR_GC_THRESH1] = {
3130 .procname = "gc_thresh1",
3131 .maxlen = sizeof(int),
3135 .proc_handler = proc_dointvec_minmax,
3137 [NEIGH_VAR_GC_THRESH2] = {
3138 .procname = "gc_thresh2",
3139 .maxlen = sizeof(int),
3143 .proc_handler = proc_dointvec_minmax,
3145 [NEIGH_VAR_GC_THRESH3] = {
3146 .procname = "gc_thresh3",
3147 .maxlen = sizeof(int),
3151 .proc_handler = proc_dointvec_minmax,
3157 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
3158 proc_handler *handler)
3161 struct neigh_sysctl_table *t;
3162 const char *dev_name_source;
3163 char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
3166 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
3170 for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
3171 t->neigh_vars[i].data += (long) p;
3172 t->neigh_vars[i].extra1 = dev;
3173 t->neigh_vars[i].extra2 = p;
3177 dev_name_source = dev->name;
3178 /* Terminate the table early */
3179 memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
3180 sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
3182 struct neigh_table *tbl = p->tbl;
3183 dev_name_source = "default";
3184 t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
3185 t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
3186 t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
3187 t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
3192 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
3194 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
3195 /* RetransTime (in milliseconds)*/
3196 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
3197 /* ReachableTime (in milliseconds) */
3198 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
3200 /* Those handlers will update p->reachable_time after
3201 * base_reachable_time(_ms) is set to ensure the new timer starts being
3202 * applied after the next neighbour update instead of waiting for
3203 * neigh_periodic_work to update its value (can be multiple minutes)
3204 * So any handler that replaces them should do this as well
3207 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
3208 neigh_proc_base_reachable_time;
3209 /* ReachableTime (in milliseconds) */
3210 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
3211 neigh_proc_base_reachable_time;
3214 /* Don't export sysctls to unprivileged users */
3215 if (neigh_parms_net(p)->user_ns != &init_user_ns)
3216 t->neigh_vars[0].procname = NULL;
3218 switch (neigh_parms_family(p)) {
3229 snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
3230 p_name, dev_name_source);
3232 register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
3233 if (!t->sysctl_header)
3236 p->sysctl_table = t;
3244 EXPORT_SYMBOL(neigh_sysctl_register);
3246 void neigh_sysctl_unregister(struct neigh_parms *p)
3248 if (p->sysctl_table) {
3249 struct neigh_sysctl_table *t = p->sysctl_table;
3250 p->sysctl_table = NULL;
3251 unregister_net_sysctl_table(t->sysctl_header);
3255 EXPORT_SYMBOL(neigh_sysctl_unregister);
3257 #endif /* CONFIG_SYSCTL */
3259 static int __init neigh_init(void)
3261 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
3262 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
3263 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
3265 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
3267 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
3272 subsys_initcall(neigh_init);