mm owner: fix race between swapoff and exit
[linux-2.6/next.git] / net / core / neighbour.c
blob9d92e41826e7d02ef7d8836825ef27ebb8423f68
1 /*
2 * Generic address resolution entity
4 * Authors:
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.
13 * Fixes:
14 * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
15 * Harald Welte Add neighbour cache statistics like rtstat
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/socket.h>
22 #include <linux/netdevice.h>
23 #include <linux/proc_fs.h>
24 #ifdef CONFIG_SYSCTL
25 #include <linux/sysctl.h>
26 #endif
27 #include <linux/times.h>
28 #include <net/net_namespace.h>
29 #include <net/neighbour.h>
30 #include <net/dst.h>
31 #include <net/sock.h>
32 #include <net/netevent.h>
33 #include <net/netlink.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/random.h>
36 #include <linux/string.h>
37 #include <linux/log2.h>
39 #define NEIGH_DEBUG 1
41 #define NEIGH_PRINTK(x...) printk(x)
42 #define NEIGH_NOPRINTK(x...) do { ; } while(0)
43 #define NEIGH_PRINTK0 NEIGH_PRINTK
44 #define NEIGH_PRINTK1 NEIGH_NOPRINTK
45 #define NEIGH_PRINTK2 NEIGH_NOPRINTK
47 #if NEIGH_DEBUG >= 1
48 #undef NEIGH_PRINTK1
49 #define NEIGH_PRINTK1 NEIGH_PRINTK
50 #endif
51 #if NEIGH_DEBUG >= 2
52 #undef NEIGH_PRINTK2
53 #define NEIGH_PRINTK2 NEIGH_PRINTK
54 #endif
56 #define PNEIGH_HASHMASK 0xF
58 static void neigh_timer_handler(unsigned long arg);
59 static void __neigh_notify(struct neighbour *n, int type, int flags);
60 static void neigh_update_notify(struct neighbour *neigh);
61 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
63 static struct neigh_table *neigh_tables;
64 #ifdef CONFIG_PROC_FS
65 static const struct file_operations neigh_stat_seq_fops;
66 #endif
69 Neighbour hash table buckets are protected with rwlock tbl->lock.
71 - All the scans/updates to hash buckets MUST be made under this lock.
72 - NOTHING clever should be made under this lock: no callbacks
73 to protocol backends, no attempts to send something to network.
74 It will result in deadlocks, if backend/driver wants to use neighbour
75 cache.
76 - If the entry requires some non-trivial actions, increase
77 its reference count and release table lock.
79 Neighbour entries are protected:
80 - with reference count.
81 - with rwlock neigh->lock
83 Reference count prevents destruction.
85 neigh->lock mainly serializes ll address data and its validity state.
86 However, the same lock is used to protect another entry fields:
87 - timer
88 - resolution queue
90 Again, nothing clever shall be made under neigh->lock,
91 the most complicated procedure, which we allow is dev->hard_header.
92 It is supposed, that dev->hard_header is simplistic and does
93 not make callbacks to neighbour tables.
95 The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
96 list of neighbour tables. This list is used only in process context,
99 static DEFINE_RWLOCK(neigh_tbl_lock);
101 static int neigh_blackhole(struct sk_buff *skb)
103 kfree_skb(skb);
104 return -ENETDOWN;
107 static void neigh_cleanup_and_release(struct neighbour *neigh)
109 if (neigh->parms->neigh_cleanup)
110 neigh->parms->neigh_cleanup(neigh);
112 __neigh_notify(neigh, RTM_DELNEIGH, 0);
113 neigh_release(neigh);
117 * It is random distribution in the interval (1/2)*base...(3/2)*base.
118 * It corresponds to default IPv6 settings and is not overridable,
119 * because it is really reasonable choice.
122 unsigned long neigh_rand_reach_time(unsigned long base)
124 return (base ? (net_random() % base) + (base >> 1) : 0);
126 EXPORT_SYMBOL(neigh_rand_reach_time);
129 static int neigh_forced_gc(struct neigh_table *tbl)
131 int shrunk = 0;
132 int i;
134 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
136 write_lock_bh(&tbl->lock);
137 for (i = 0; i <= tbl->hash_mask; i++) {
138 struct neighbour *n, **np;
140 np = &tbl->hash_buckets[i];
141 while ((n = *np) != NULL) {
142 /* Neighbour record may be discarded if:
143 * - nobody refers to it.
144 * - it is not permanent
146 write_lock(&n->lock);
147 if (atomic_read(&n->refcnt) == 1 &&
148 !(n->nud_state & NUD_PERMANENT)) {
149 *np = n->next;
150 n->dead = 1;
151 shrunk = 1;
152 write_unlock(&n->lock);
153 neigh_cleanup_and_release(n);
154 continue;
156 write_unlock(&n->lock);
157 np = &n->next;
161 tbl->last_flush = jiffies;
163 write_unlock_bh(&tbl->lock);
165 return shrunk;
168 static void neigh_add_timer(struct neighbour *n, unsigned long when)
170 neigh_hold(n);
171 if (unlikely(mod_timer(&n->timer, when))) {
172 printk("NEIGH: BUG, double timer add, state is %x\n",
173 n->nud_state);
174 dump_stack();
178 static int neigh_del_timer(struct neighbour *n)
180 if ((n->nud_state & NUD_IN_TIMER) &&
181 del_timer(&n->timer)) {
182 neigh_release(n);
183 return 1;
185 return 0;
188 static void pneigh_queue_purge(struct sk_buff_head *list)
190 struct sk_buff *skb;
192 while ((skb = skb_dequeue(list)) != NULL) {
193 dev_put(skb->dev);
194 kfree_skb(skb);
198 static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
200 int i;
202 for (i = 0; i <= tbl->hash_mask; i++) {
203 struct neighbour *n, **np = &tbl->hash_buckets[i];
205 while ((n = *np) != NULL) {
206 if (dev && n->dev != dev) {
207 np = &n->next;
208 continue;
210 *np = n->next;
211 write_lock(&n->lock);
212 neigh_del_timer(n);
213 n->dead = 1;
215 if (atomic_read(&n->refcnt) != 1) {
216 /* The most unpleasant situation.
217 We must destroy neighbour entry,
218 but someone still uses it.
220 The destroy will be delayed until
221 the last user releases us, but
222 we must kill timers etc. and move
223 it to safe state.
225 skb_queue_purge(&n->arp_queue);
226 n->output = neigh_blackhole;
227 if (n->nud_state & NUD_VALID)
228 n->nud_state = NUD_NOARP;
229 else
230 n->nud_state = NUD_NONE;
231 NEIGH_PRINTK2("neigh %p is stray.\n", n);
233 write_unlock(&n->lock);
234 neigh_cleanup_and_release(n);
239 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
241 write_lock_bh(&tbl->lock);
242 neigh_flush_dev(tbl, dev);
243 write_unlock_bh(&tbl->lock);
245 EXPORT_SYMBOL(neigh_changeaddr);
247 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
249 write_lock_bh(&tbl->lock);
250 neigh_flush_dev(tbl, dev);
251 pneigh_ifdown(tbl, dev);
252 write_unlock_bh(&tbl->lock);
254 del_timer_sync(&tbl->proxy_timer);
255 pneigh_queue_purge(&tbl->proxy_queue);
256 return 0;
258 EXPORT_SYMBOL(neigh_ifdown);
260 static struct neighbour *neigh_alloc(struct neigh_table *tbl)
262 struct neighbour *n = NULL;
263 unsigned long now = jiffies;
264 int entries;
266 entries = atomic_inc_return(&tbl->entries) - 1;
267 if (entries >= tbl->gc_thresh3 ||
268 (entries >= tbl->gc_thresh2 &&
269 time_after(now, tbl->last_flush + 5 * HZ))) {
270 if (!neigh_forced_gc(tbl) &&
271 entries >= tbl->gc_thresh3)
272 goto out_entries;
275 n = kmem_cache_zalloc(tbl->kmem_cachep, GFP_ATOMIC);
276 if (!n)
277 goto out_entries;
279 skb_queue_head_init(&n->arp_queue);
280 rwlock_init(&n->lock);
281 n->updated = n->used = now;
282 n->nud_state = NUD_NONE;
283 n->output = neigh_blackhole;
284 n->parms = neigh_parms_clone(&tbl->parms);
285 setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
287 NEIGH_CACHE_STAT_INC(tbl, allocs);
288 n->tbl = tbl;
289 atomic_set(&n->refcnt, 1);
290 n->dead = 1;
291 out:
292 return n;
294 out_entries:
295 atomic_dec(&tbl->entries);
296 goto out;
299 static struct neighbour **neigh_hash_alloc(unsigned int entries)
301 unsigned long size = entries * sizeof(struct neighbour *);
302 struct neighbour **ret;
304 if (size <= PAGE_SIZE) {
305 ret = kzalloc(size, GFP_ATOMIC);
306 } else {
307 ret = (struct neighbour **)
308 __get_free_pages(GFP_ATOMIC|__GFP_ZERO, get_order(size));
310 return ret;
313 static void neigh_hash_free(struct neighbour **hash, unsigned int entries)
315 unsigned long size = entries * sizeof(struct neighbour *);
317 if (size <= PAGE_SIZE)
318 kfree(hash);
319 else
320 free_pages((unsigned long)hash, get_order(size));
323 static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries)
325 struct neighbour **new_hash, **old_hash;
326 unsigned int i, new_hash_mask, old_entries;
328 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
330 BUG_ON(!is_power_of_2(new_entries));
331 new_hash = neigh_hash_alloc(new_entries);
332 if (!new_hash)
333 return;
335 old_entries = tbl->hash_mask + 1;
336 new_hash_mask = new_entries - 1;
337 old_hash = tbl->hash_buckets;
339 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
340 for (i = 0; i < old_entries; i++) {
341 struct neighbour *n, *next;
343 for (n = old_hash[i]; n; n = next) {
344 unsigned int hash_val = tbl->hash(n->primary_key, n->dev);
346 hash_val &= new_hash_mask;
347 next = n->next;
349 n->next = new_hash[hash_val];
350 new_hash[hash_val] = n;
353 tbl->hash_buckets = new_hash;
354 tbl->hash_mask = new_hash_mask;
356 neigh_hash_free(old_hash, old_entries);
359 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
360 struct net_device *dev)
362 struct neighbour *n;
363 int key_len = tbl->key_len;
364 u32 hash_val;
366 NEIGH_CACHE_STAT_INC(tbl, lookups);
368 read_lock_bh(&tbl->lock);
369 hash_val = tbl->hash(pkey, dev);
370 for (n = tbl->hash_buckets[hash_val & tbl->hash_mask]; n; n = n->next) {
371 if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
372 neigh_hold(n);
373 NEIGH_CACHE_STAT_INC(tbl, hits);
374 break;
377 read_unlock_bh(&tbl->lock);
378 return n;
380 EXPORT_SYMBOL(neigh_lookup);
382 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
383 const void *pkey)
385 struct neighbour *n;
386 int key_len = tbl->key_len;
387 u32 hash_val;
389 NEIGH_CACHE_STAT_INC(tbl, lookups);
391 read_lock_bh(&tbl->lock);
392 hash_val = tbl->hash(pkey, NULL);
393 for (n = tbl->hash_buckets[hash_val & tbl->hash_mask]; n; n = n->next) {
394 if (!memcmp(n->primary_key, pkey, key_len) &&
395 net_eq(dev_net(n->dev), net)) {
396 neigh_hold(n);
397 NEIGH_CACHE_STAT_INC(tbl, hits);
398 break;
401 read_unlock_bh(&tbl->lock);
402 return n;
404 EXPORT_SYMBOL(neigh_lookup_nodev);
406 struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
407 struct net_device *dev)
409 u32 hash_val;
410 int key_len = tbl->key_len;
411 int error;
412 struct neighbour *n1, *rc, *n = neigh_alloc(tbl);
414 if (!n) {
415 rc = ERR_PTR(-ENOBUFS);
416 goto out;
419 memcpy(n->primary_key, pkey, key_len);
420 n->dev = dev;
421 dev_hold(dev);
423 /* Protocol specific setup. */
424 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
425 rc = ERR_PTR(error);
426 goto out_neigh_release;
429 /* Device specific setup. */
430 if (n->parms->neigh_setup &&
431 (error = n->parms->neigh_setup(n)) < 0) {
432 rc = ERR_PTR(error);
433 goto out_neigh_release;
436 n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
438 write_lock_bh(&tbl->lock);
440 if (atomic_read(&tbl->entries) > (tbl->hash_mask + 1))
441 neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1);
443 hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
445 if (n->parms->dead) {
446 rc = ERR_PTR(-EINVAL);
447 goto out_tbl_unlock;
450 for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) {
451 if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
452 neigh_hold(n1);
453 rc = n1;
454 goto out_tbl_unlock;
458 n->next = tbl->hash_buckets[hash_val];
459 tbl->hash_buckets[hash_val] = n;
460 n->dead = 0;
461 neigh_hold(n);
462 write_unlock_bh(&tbl->lock);
463 NEIGH_PRINTK2("neigh %p is created.\n", n);
464 rc = n;
465 out:
466 return rc;
467 out_tbl_unlock:
468 write_unlock_bh(&tbl->lock);
469 out_neigh_release:
470 neigh_release(n);
471 goto out;
473 EXPORT_SYMBOL(neigh_create);
475 static u32 pneigh_hash(const void *pkey, int key_len)
477 u32 hash_val = *(u32 *)(pkey + key_len - 4);
478 hash_val ^= (hash_val >> 16);
479 hash_val ^= hash_val >> 8;
480 hash_val ^= hash_val >> 4;
481 hash_val &= PNEIGH_HASHMASK;
482 return hash_val;
485 static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
486 struct net *net,
487 const void *pkey,
488 int key_len,
489 struct net_device *dev)
491 while (n) {
492 if (!memcmp(n->key, pkey, key_len) &&
493 net_eq(pneigh_net(n), net) &&
494 (n->dev == dev || !n->dev))
495 return n;
496 n = n->next;
498 return NULL;
501 struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
502 struct net *net, const void *pkey, struct net_device *dev)
504 int key_len = tbl->key_len;
505 u32 hash_val = pneigh_hash(pkey, key_len);
507 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
508 net, pkey, key_len, dev);
510 EXPORT_SYMBOL_GPL(__pneigh_lookup);
512 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
513 struct net *net, const void *pkey,
514 struct net_device *dev, int creat)
516 struct pneigh_entry *n;
517 int key_len = tbl->key_len;
518 u32 hash_val = pneigh_hash(pkey, key_len);
520 read_lock_bh(&tbl->lock);
521 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
522 net, pkey, key_len, dev);
523 read_unlock_bh(&tbl->lock);
525 if (n || !creat)
526 goto out;
528 ASSERT_RTNL();
530 n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
531 if (!n)
532 goto out;
534 #ifdef CONFIG_NET_NS
535 n->net = hold_net(net);
536 #endif
537 memcpy(n->key, pkey, key_len);
538 n->dev = dev;
539 if (dev)
540 dev_hold(dev);
542 if (tbl->pconstructor && tbl->pconstructor(n)) {
543 if (dev)
544 dev_put(dev);
545 release_net(net);
546 kfree(n);
547 n = NULL;
548 goto out;
551 write_lock_bh(&tbl->lock);
552 n->next = tbl->phash_buckets[hash_val];
553 tbl->phash_buckets[hash_val] = n;
554 write_unlock_bh(&tbl->lock);
555 out:
556 return n;
558 EXPORT_SYMBOL(pneigh_lookup);
561 int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
562 struct net_device *dev)
564 struct pneigh_entry *n, **np;
565 int key_len = tbl->key_len;
566 u32 hash_val = pneigh_hash(pkey, key_len);
568 write_lock_bh(&tbl->lock);
569 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
570 np = &n->next) {
571 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
572 net_eq(pneigh_net(n), net)) {
573 *np = n->next;
574 write_unlock_bh(&tbl->lock);
575 if (tbl->pdestructor)
576 tbl->pdestructor(n);
577 if (n->dev)
578 dev_put(n->dev);
579 release_net(pneigh_net(n));
580 kfree(n);
581 return 0;
584 write_unlock_bh(&tbl->lock);
585 return -ENOENT;
588 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
590 struct pneigh_entry *n, **np;
591 u32 h;
593 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
594 np = &tbl->phash_buckets[h];
595 while ((n = *np) != NULL) {
596 if (!dev || n->dev == dev) {
597 *np = n->next;
598 if (tbl->pdestructor)
599 tbl->pdestructor(n);
600 if (n->dev)
601 dev_put(n->dev);
602 release_net(pneigh_net(n));
603 kfree(n);
604 continue;
606 np = &n->next;
609 return -ENOENT;
612 static void neigh_parms_destroy(struct neigh_parms *parms);
614 static inline void neigh_parms_put(struct neigh_parms *parms)
616 if (atomic_dec_and_test(&parms->refcnt))
617 neigh_parms_destroy(parms);
621 * neighbour must already be out of the table;
624 void neigh_destroy(struct neighbour *neigh)
626 struct hh_cache *hh;
628 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
630 if (!neigh->dead) {
631 printk(KERN_WARNING
632 "Destroying alive neighbour %p\n", neigh);
633 dump_stack();
634 return;
637 if (neigh_del_timer(neigh))
638 printk(KERN_WARNING "Impossible event.\n");
640 while ((hh = neigh->hh) != NULL) {
641 neigh->hh = hh->hh_next;
642 hh->hh_next = NULL;
644 write_seqlock_bh(&hh->hh_lock);
645 hh->hh_output = neigh_blackhole;
646 write_sequnlock_bh(&hh->hh_lock);
647 if (atomic_dec_and_test(&hh->hh_refcnt))
648 kfree(hh);
651 skb_queue_purge(&neigh->arp_queue);
653 dev_put(neigh->dev);
654 neigh_parms_put(neigh->parms);
656 NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
658 atomic_dec(&neigh->tbl->entries);
659 kmem_cache_free(neigh->tbl->kmem_cachep, neigh);
661 EXPORT_SYMBOL(neigh_destroy);
663 /* Neighbour state is suspicious;
664 disable fast path.
666 Called with write_locked neigh.
668 static void neigh_suspect(struct neighbour *neigh)
670 struct hh_cache *hh;
672 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
674 neigh->output = neigh->ops->output;
676 for (hh = neigh->hh; hh; hh = hh->hh_next)
677 hh->hh_output = neigh->ops->output;
680 /* Neighbour state is OK;
681 enable fast path.
683 Called with write_locked neigh.
685 static void neigh_connect(struct neighbour *neigh)
687 struct hh_cache *hh;
689 NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
691 neigh->output = neigh->ops->connected_output;
693 for (hh = neigh->hh; hh; hh = hh->hh_next)
694 hh->hh_output = neigh->ops->hh_output;
697 static void neigh_periodic_timer(unsigned long arg)
699 struct neigh_table *tbl = (struct neigh_table *)arg;
700 struct neighbour *n, **np;
701 unsigned long expire, now = jiffies;
703 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
705 write_lock(&tbl->lock);
708 * periodically recompute ReachableTime from random function
711 if (time_after(now, tbl->last_rand + 300 * HZ)) {
712 struct neigh_parms *p;
713 tbl->last_rand = now;
714 for (p = &tbl->parms; p; p = p->next)
715 p->reachable_time =
716 neigh_rand_reach_time(p->base_reachable_time);
719 np = &tbl->hash_buckets[tbl->hash_chain_gc];
720 tbl->hash_chain_gc = ((tbl->hash_chain_gc + 1) & tbl->hash_mask);
722 while ((n = *np) != NULL) {
723 unsigned int state;
725 write_lock(&n->lock);
727 state = n->nud_state;
728 if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
729 write_unlock(&n->lock);
730 goto next_elt;
733 if (time_before(n->used, n->confirmed))
734 n->used = n->confirmed;
736 if (atomic_read(&n->refcnt) == 1 &&
737 (state == NUD_FAILED ||
738 time_after(now, n->used + n->parms->gc_staletime))) {
739 *np = n->next;
740 n->dead = 1;
741 write_unlock(&n->lock);
742 neigh_cleanup_and_release(n);
743 continue;
745 write_unlock(&n->lock);
747 next_elt:
748 np = &n->next;
751 /* Cycle through all hash buckets every base_reachable_time/2 ticks.
752 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
753 * base_reachable_time.
755 expire = tbl->parms.base_reachable_time >> 1;
756 expire /= (tbl->hash_mask + 1);
757 if (!expire)
758 expire = 1;
760 if (expire>HZ)
761 mod_timer(&tbl->gc_timer, round_jiffies(now + expire));
762 else
763 mod_timer(&tbl->gc_timer, now + expire);
765 write_unlock(&tbl->lock);
768 static __inline__ int neigh_max_probes(struct neighbour *n)
770 struct neigh_parms *p = n->parms;
771 return (n->nud_state & NUD_PROBE ?
772 p->ucast_probes :
773 p->ucast_probes + p->app_probes + p->mcast_probes);
776 /* Called when a timer expires for a neighbour entry. */
778 static void neigh_timer_handler(unsigned long arg)
780 unsigned long now, next;
781 struct neighbour *neigh = (struct neighbour *)arg;
782 unsigned state;
783 int notify = 0;
785 write_lock(&neigh->lock);
787 state = neigh->nud_state;
788 now = jiffies;
789 next = now + HZ;
791 if (!(state & NUD_IN_TIMER)) {
792 #ifndef CONFIG_SMP
793 printk(KERN_WARNING "neigh: timer & !nud_in_timer\n");
794 #endif
795 goto out;
798 if (state & NUD_REACHABLE) {
799 if (time_before_eq(now,
800 neigh->confirmed + neigh->parms->reachable_time)) {
801 NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
802 next = neigh->confirmed + neigh->parms->reachable_time;
803 } else if (time_before_eq(now,
804 neigh->used + neigh->parms->delay_probe_time)) {
805 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
806 neigh->nud_state = NUD_DELAY;
807 neigh->updated = jiffies;
808 neigh_suspect(neigh);
809 next = now + neigh->parms->delay_probe_time;
810 } else {
811 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
812 neigh->nud_state = NUD_STALE;
813 neigh->updated = jiffies;
814 neigh_suspect(neigh);
815 notify = 1;
817 } else if (state & NUD_DELAY) {
818 if (time_before_eq(now,
819 neigh->confirmed + neigh->parms->delay_probe_time)) {
820 NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh);
821 neigh->nud_state = NUD_REACHABLE;
822 neigh->updated = jiffies;
823 neigh_connect(neigh);
824 notify = 1;
825 next = neigh->confirmed + neigh->parms->reachable_time;
826 } else {
827 NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
828 neigh->nud_state = NUD_PROBE;
829 neigh->updated = jiffies;
830 atomic_set(&neigh->probes, 0);
831 next = now + neigh->parms->retrans_time;
833 } else {
834 /* NUD_PROBE|NUD_INCOMPLETE */
835 next = now + neigh->parms->retrans_time;
838 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
839 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
840 struct sk_buff *skb;
842 neigh->nud_state = NUD_FAILED;
843 neigh->updated = jiffies;
844 notify = 1;
845 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
846 NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
848 /* It is very thin place. report_unreachable is very complicated
849 routine. Particularly, it can hit the same neighbour entry!
851 So that, we try to be accurate and avoid dead loop. --ANK
853 while (neigh->nud_state == NUD_FAILED &&
854 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
855 write_unlock(&neigh->lock);
856 neigh->ops->error_report(neigh, skb);
857 write_lock(&neigh->lock);
859 skb_queue_purge(&neigh->arp_queue);
862 if (neigh->nud_state & NUD_IN_TIMER) {
863 if (time_before(next, jiffies + HZ/2))
864 next = jiffies + HZ/2;
865 if (!mod_timer(&neigh->timer, next))
866 neigh_hold(neigh);
868 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
869 struct sk_buff *skb = skb_peek(&neigh->arp_queue);
870 /* keep skb alive even if arp_queue overflows */
871 if (skb)
872 skb = skb_copy(skb, GFP_ATOMIC);
873 write_unlock(&neigh->lock);
874 neigh->ops->solicit(neigh, skb);
875 atomic_inc(&neigh->probes);
876 if (skb)
877 kfree_skb(skb);
878 } else {
879 out:
880 write_unlock(&neigh->lock);
883 if (notify)
884 neigh_update_notify(neigh);
886 neigh_release(neigh);
889 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
891 int rc;
892 unsigned long now;
894 write_lock_bh(&neigh->lock);
896 rc = 0;
897 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
898 goto out_unlock_bh;
900 now = jiffies;
902 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
903 if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
904 atomic_set(&neigh->probes, neigh->parms->ucast_probes);
905 neigh->nud_state = NUD_INCOMPLETE;
906 neigh->updated = jiffies;
907 neigh_add_timer(neigh, now + 1);
908 } else {
909 neigh->nud_state = NUD_FAILED;
910 neigh->updated = jiffies;
911 write_unlock_bh(&neigh->lock);
913 if (skb)
914 kfree_skb(skb);
915 return 1;
917 } else if (neigh->nud_state & NUD_STALE) {
918 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
919 neigh->nud_state = NUD_DELAY;
920 neigh->updated = jiffies;
921 neigh_add_timer(neigh,
922 jiffies + neigh->parms->delay_probe_time);
925 if (neigh->nud_state == NUD_INCOMPLETE) {
926 if (skb) {
927 if (skb_queue_len(&neigh->arp_queue) >=
928 neigh->parms->queue_len) {
929 struct sk_buff *buff;
930 buff = neigh->arp_queue.next;
931 __skb_unlink(buff, &neigh->arp_queue);
932 kfree_skb(buff);
933 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
935 __skb_queue_tail(&neigh->arp_queue, skb);
937 rc = 1;
939 out_unlock_bh:
940 write_unlock_bh(&neigh->lock);
941 return rc;
943 EXPORT_SYMBOL(__neigh_event_send);
945 static void neigh_update_hhs(struct neighbour *neigh)
947 struct hh_cache *hh;
948 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
949 = neigh->dev->header_ops->cache_update;
951 if (update) {
952 for (hh = neigh->hh; hh; hh = hh->hh_next) {
953 write_seqlock_bh(&hh->hh_lock);
954 update(hh, neigh->dev, neigh->ha);
955 write_sequnlock_bh(&hh->hh_lock);
962 /* Generic update routine.
963 -- lladdr is new lladdr or NULL, if it is not supplied.
964 -- new is new state.
965 -- flags
966 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
967 if it is different.
968 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
969 lladdr instead of overriding it
970 if it is different.
971 It also allows to retain current state
972 if lladdr is unchanged.
973 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
975 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
976 NTF_ROUTER flag.
977 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
978 a router.
980 Caller MUST hold reference count on the entry.
983 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
984 u32 flags)
986 u8 old;
987 int err;
988 int notify = 0;
989 struct net_device *dev;
990 int update_isrouter = 0;
992 write_lock_bh(&neigh->lock);
994 dev = neigh->dev;
995 old = neigh->nud_state;
996 err = -EPERM;
998 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
999 (old & (NUD_NOARP | NUD_PERMANENT)))
1000 goto out;
1002 if (!(new & NUD_VALID)) {
1003 neigh_del_timer(neigh);
1004 if (old & NUD_CONNECTED)
1005 neigh_suspect(neigh);
1006 neigh->nud_state = new;
1007 err = 0;
1008 notify = old & NUD_VALID;
1009 goto out;
1012 /* Compare new lladdr with cached one */
1013 if (!dev->addr_len) {
1014 /* First case: device needs no address. */
1015 lladdr = neigh->ha;
1016 } else if (lladdr) {
1017 /* The second case: if something is already cached
1018 and a new address is proposed:
1019 - compare new & old
1020 - if they are different, check override flag
1022 if ((old & NUD_VALID) &&
1023 !memcmp(lladdr, neigh->ha, dev->addr_len))
1024 lladdr = neigh->ha;
1025 } else {
1026 /* No address is supplied; if we know something,
1027 use it, otherwise discard the request.
1029 err = -EINVAL;
1030 if (!(old & NUD_VALID))
1031 goto out;
1032 lladdr = neigh->ha;
1035 if (new & NUD_CONNECTED)
1036 neigh->confirmed = jiffies;
1037 neigh->updated = jiffies;
1039 /* If entry was valid and address is not changed,
1040 do not change entry state, if new one is STALE.
1042 err = 0;
1043 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1044 if (old & NUD_VALID) {
1045 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1046 update_isrouter = 0;
1047 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1048 (old & NUD_CONNECTED)) {
1049 lladdr = neigh->ha;
1050 new = NUD_STALE;
1051 } else
1052 goto out;
1053 } else {
1054 if (lladdr == neigh->ha && new == NUD_STALE &&
1055 ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
1056 (old & NUD_CONNECTED))
1058 new = old;
1062 if (new != old) {
1063 neigh_del_timer(neigh);
1064 if (new & NUD_IN_TIMER)
1065 neigh_add_timer(neigh, (jiffies +
1066 ((new & NUD_REACHABLE) ?
1067 neigh->parms->reachable_time :
1068 0)));
1069 neigh->nud_state = new;
1072 if (lladdr != neigh->ha) {
1073 memcpy(&neigh->ha, lladdr, dev->addr_len);
1074 neigh_update_hhs(neigh);
1075 if (!(new & NUD_CONNECTED))
1076 neigh->confirmed = jiffies -
1077 (neigh->parms->base_reachable_time << 1);
1078 notify = 1;
1080 if (new == old)
1081 goto out;
1082 if (new & NUD_CONNECTED)
1083 neigh_connect(neigh);
1084 else
1085 neigh_suspect(neigh);
1086 if (!(old & NUD_VALID)) {
1087 struct sk_buff *skb;
1089 /* Again: avoid dead loop if something went wrong */
1091 while (neigh->nud_state & NUD_VALID &&
1092 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1093 struct neighbour *n1 = neigh;
1094 write_unlock_bh(&neigh->lock);
1095 /* On shaper/eql skb->dst->neighbour != neigh :( */
1096 if (skb->dst && skb->dst->neighbour)
1097 n1 = skb->dst->neighbour;
1098 n1->output(skb);
1099 write_lock_bh(&neigh->lock);
1101 skb_queue_purge(&neigh->arp_queue);
1103 out:
1104 if (update_isrouter) {
1105 neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
1106 (neigh->flags | NTF_ROUTER) :
1107 (neigh->flags & ~NTF_ROUTER);
1109 write_unlock_bh(&neigh->lock);
1111 if (notify)
1112 neigh_update_notify(neigh);
1114 return err;
1116 EXPORT_SYMBOL(neigh_update);
1118 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1119 u8 *lladdr, void *saddr,
1120 struct net_device *dev)
1122 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1123 lladdr || !dev->addr_len);
1124 if (neigh)
1125 neigh_update(neigh, lladdr, NUD_STALE,
1126 NEIGH_UPDATE_F_OVERRIDE);
1127 return neigh;
1129 EXPORT_SYMBOL(neigh_event_ns);
1131 static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst,
1132 __be16 protocol)
1134 struct hh_cache *hh;
1135 struct net_device *dev = dst->dev;
1137 for (hh = n->hh; hh; hh = hh->hh_next)
1138 if (hh->hh_type == protocol)
1139 break;
1141 if (!hh && (hh = kzalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) {
1142 seqlock_init(&hh->hh_lock);
1143 hh->hh_type = protocol;
1144 atomic_set(&hh->hh_refcnt, 0);
1145 hh->hh_next = NULL;
1147 if (dev->header_ops->cache(n, hh)) {
1148 kfree(hh);
1149 hh = NULL;
1150 } else {
1151 atomic_inc(&hh->hh_refcnt);
1152 hh->hh_next = n->hh;
1153 n->hh = hh;
1154 if (n->nud_state & NUD_CONNECTED)
1155 hh->hh_output = n->ops->hh_output;
1156 else
1157 hh->hh_output = n->ops->output;
1160 if (hh) {
1161 atomic_inc(&hh->hh_refcnt);
1162 dst->hh = hh;
1166 /* This function can be used in contexts, where only old dev_queue_xmit
1167 worked, f.e. if you want to override normal output path (eql, shaper),
1168 but resolution is not made yet.
1171 int neigh_compat_output(struct sk_buff *skb)
1173 struct net_device *dev = skb->dev;
1175 __skb_pull(skb, skb_network_offset(skb));
1177 if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
1178 skb->len) < 0 &&
1179 dev->header_ops->rebuild(skb))
1180 return 0;
1182 return dev_queue_xmit(skb);
1184 EXPORT_SYMBOL(neigh_compat_output);
1186 /* Slow and careful. */
1188 int neigh_resolve_output(struct sk_buff *skb)
1190 struct dst_entry *dst = skb->dst;
1191 struct neighbour *neigh;
1192 int rc = 0;
1194 if (!dst || !(neigh = dst->neighbour))
1195 goto discard;
1197 __skb_pull(skb, skb_network_offset(skb));
1199 if (!neigh_event_send(neigh, skb)) {
1200 int err;
1201 struct net_device *dev = neigh->dev;
1202 if (dev->header_ops->cache && !dst->hh) {
1203 write_lock_bh(&neigh->lock);
1204 if (!dst->hh)
1205 neigh_hh_init(neigh, dst, dst->ops->protocol);
1206 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1207 neigh->ha, NULL, skb->len);
1208 write_unlock_bh(&neigh->lock);
1209 } else {
1210 read_lock_bh(&neigh->lock);
1211 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1212 neigh->ha, NULL, skb->len);
1213 read_unlock_bh(&neigh->lock);
1215 if (err >= 0)
1216 rc = neigh->ops->queue_xmit(skb);
1217 else
1218 goto out_kfree_skb;
1220 out:
1221 return rc;
1222 discard:
1223 NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
1224 dst, dst ? dst->neighbour : NULL);
1225 out_kfree_skb:
1226 rc = -EINVAL;
1227 kfree_skb(skb);
1228 goto out;
1230 EXPORT_SYMBOL(neigh_resolve_output);
1232 /* As fast as possible without hh cache */
1234 int neigh_connected_output(struct sk_buff *skb)
1236 int err;
1237 struct dst_entry *dst = skb->dst;
1238 struct neighbour *neigh = dst->neighbour;
1239 struct net_device *dev = neigh->dev;
1241 __skb_pull(skb, skb_network_offset(skb));
1243 read_lock_bh(&neigh->lock);
1244 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1245 neigh->ha, NULL, skb->len);
1246 read_unlock_bh(&neigh->lock);
1247 if (err >= 0)
1248 err = neigh->ops->queue_xmit(skb);
1249 else {
1250 err = -EINVAL;
1251 kfree_skb(skb);
1253 return err;
1255 EXPORT_SYMBOL(neigh_connected_output);
1257 static void neigh_proxy_process(unsigned long arg)
1259 struct neigh_table *tbl = (struct neigh_table *)arg;
1260 long sched_next = 0;
1261 unsigned long now = jiffies;
1262 struct sk_buff *skb;
1264 spin_lock(&tbl->proxy_queue.lock);
1266 skb = tbl->proxy_queue.next;
1268 while (skb != (struct sk_buff *)&tbl->proxy_queue) {
1269 struct sk_buff *back = skb;
1270 long tdif = NEIGH_CB(back)->sched_next - now;
1272 skb = skb->next;
1273 if (tdif <= 0) {
1274 struct net_device *dev = back->dev;
1275 __skb_unlink(back, &tbl->proxy_queue);
1276 if (tbl->proxy_redo && netif_running(dev))
1277 tbl->proxy_redo(back);
1278 else
1279 kfree_skb(back);
1281 dev_put(dev);
1282 } else if (!sched_next || tdif < sched_next)
1283 sched_next = tdif;
1285 del_timer(&tbl->proxy_timer);
1286 if (sched_next)
1287 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1288 spin_unlock(&tbl->proxy_queue.lock);
1291 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1292 struct sk_buff *skb)
1294 unsigned long now = jiffies;
1295 unsigned long sched_next = now + (net_random() % p->proxy_delay);
1297 if (tbl->proxy_queue.qlen > p->proxy_qlen) {
1298 kfree_skb(skb);
1299 return;
1302 NEIGH_CB(skb)->sched_next = sched_next;
1303 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1305 spin_lock(&tbl->proxy_queue.lock);
1306 if (del_timer(&tbl->proxy_timer)) {
1307 if (time_before(tbl->proxy_timer.expires, sched_next))
1308 sched_next = tbl->proxy_timer.expires;
1310 dst_release(skb->dst);
1311 skb->dst = NULL;
1312 dev_hold(skb->dev);
1313 __skb_queue_tail(&tbl->proxy_queue, skb);
1314 mod_timer(&tbl->proxy_timer, sched_next);
1315 spin_unlock(&tbl->proxy_queue.lock);
1317 EXPORT_SYMBOL(pneigh_enqueue);
1319 static inline struct neigh_parms *lookup_neigh_params(struct neigh_table *tbl,
1320 struct net *net, int ifindex)
1322 struct neigh_parms *p;
1324 for (p = &tbl->parms; p; p = p->next) {
1325 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
1326 (!p->dev && !ifindex))
1327 return p;
1330 return NULL;
1333 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1334 struct neigh_table *tbl)
1336 struct neigh_parms *p, *ref;
1337 struct net *net;
1339 net = dev_net(dev);
1340 ref = lookup_neigh_params(tbl, net, 0);
1341 if (!ref)
1342 return NULL;
1344 p = kmemdup(ref, sizeof(*p), GFP_KERNEL);
1345 if (p) {
1346 p->tbl = tbl;
1347 atomic_set(&p->refcnt, 1);
1348 INIT_RCU_HEAD(&p->rcu_head);
1349 p->reachable_time =
1350 neigh_rand_reach_time(p->base_reachable_time);
1352 if (dev->neigh_setup && dev->neigh_setup(dev, p)) {
1353 kfree(p);
1354 return NULL;
1357 dev_hold(dev);
1358 p->dev = dev;
1359 #ifdef CONFIG_NET_NS
1360 p->net = hold_net(net);
1361 #endif
1362 p->sysctl_table = NULL;
1363 write_lock_bh(&tbl->lock);
1364 p->next = tbl->parms.next;
1365 tbl->parms.next = p;
1366 write_unlock_bh(&tbl->lock);
1368 return p;
1370 EXPORT_SYMBOL(neigh_parms_alloc);
1372 static void neigh_rcu_free_parms(struct rcu_head *head)
1374 struct neigh_parms *parms =
1375 container_of(head, struct neigh_parms, rcu_head);
1377 neigh_parms_put(parms);
1380 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1382 struct neigh_parms **p;
1384 if (!parms || parms == &tbl->parms)
1385 return;
1386 write_lock_bh(&tbl->lock);
1387 for (p = &tbl->parms.next; *p; p = &(*p)->next) {
1388 if (*p == parms) {
1389 *p = parms->next;
1390 parms->dead = 1;
1391 write_unlock_bh(&tbl->lock);
1392 if (parms->dev)
1393 dev_put(parms->dev);
1394 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1395 return;
1398 write_unlock_bh(&tbl->lock);
1399 NEIGH_PRINTK1("neigh_parms_release: not found\n");
1401 EXPORT_SYMBOL(neigh_parms_release);
1403 static void neigh_parms_destroy(struct neigh_parms *parms)
1405 release_net(neigh_parms_net(parms));
1406 kfree(parms);
1409 static struct lock_class_key neigh_table_proxy_queue_class;
1411 void neigh_table_init_no_netlink(struct neigh_table *tbl)
1413 unsigned long now = jiffies;
1414 unsigned long phsize;
1416 #ifdef CONFIG_NET_NS
1417 tbl->parms.net = &init_net;
1418 #endif
1419 atomic_set(&tbl->parms.refcnt, 1);
1420 INIT_RCU_HEAD(&tbl->parms.rcu_head);
1421 tbl->parms.reachable_time =
1422 neigh_rand_reach_time(tbl->parms.base_reachable_time);
1424 if (!tbl->kmem_cachep)
1425 tbl->kmem_cachep =
1426 kmem_cache_create(tbl->id, tbl->entry_size, 0,
1427 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1428 NULL);
1429 tbl->stats = alloc_percpu(struct neigh_statistics);
1430 if (!tbl->stats)
1431 panic("cannot create neighbour cache statistics");
1433 #ifdef CONFIG_PROC_FS
1434 tbl->pde = proc_create_data(tbl->id, 0, init_net.proc_net_stat,
1435 &neigh_stat_seq_fops, tbl);
1436 if (!tbl->pde)
1437 panic("cannot create neighbour proc dir entry");
1438 #endif
1440 tbl->hash_mask = 1;
1441 tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1);
1443 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1444 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
1446 if (!tbl->hash_buckets || !tbl->phash_buckets)
1447 panic("cannot allocate neighbour cache hashes");
1449 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1451 rwlock_init(&tbl->lock);
1452 setup_timer(&tbl->gc_timer, neigh_periodic_timer, (unsigned long)tbl);
1453 tbl->gc_timer.expires = now + 1;
1454 add_timer(&tbl->gc_timer);
1456 setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
1457 skb_queue_head_init_class(&tbl->proxy_queue,
1458 &neigh_table_proxy_queue_class);
1460 tbl->last_flush = now;
1461 tbl->last_rand = now + tbl->parms.reachable_time * 20;
1463 EXPORT_SYMBOL(neigh_table_init_no_netlink);
1465 void neigh_table_init(struct neigh_table *tbl)
1467 struct neigh_table *tmp;
1469 neigh_table_init_no_netlink(tbl);
1470 write_lock(&neigh_tbl_lock);
1471 for (tmp = neigh_tables; tmp; tmp = tmp->next) {
1472 if (tmp->family == tbl->family)
1473 break;
1475 tbl->next = neigh_tables;
1476 neigh_tables = tbl;
1477 write_unlock(&neigh_tbl_lock);
1479 if (unlikely(tmp)) {
1480 printk(KERN_ERR "NEIGH: Registering multiple tables for "
1481 "family %d\n", tbl->family);
1482 dump_stack();
1485 EXPORT_SYMBOL(neigh_table_init);
1487 int neigh_table_clear(struct neigh_table *tbl)
1489 struct neigh_table **tp;
1491 /* It is not clean... Fix it to unload IPv6 module safely */
1492 del_timer_sync(&tbl->gc_timer);
1493 del_timer_sync(&tbl->proxy_timer);
1494 pneigh_queue_purge(&tbl->proxy_queue);
1495 neigh_ifdown(tbl, NULL);
1496 if (atomic_read(&tbl->entries))
1497 printk(KERN_CRIT "neighbour leakage\n");
1498 write_lock(&neigh_tbl_lock);
1499 for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
1500 if (*tp == tbl) {
1501 *tp = tbl->next;
1502 break;
1505 write_unlock(&neigh_tbl_lock);
1507 neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1);
1508 tbl->hash_buckets = NULL;
1510 kfree(tbl->phash_buckets);
1511 tbl->phash_buckets = NULL;
1513 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1515 free_percpu(tbl->stats);
1516 tbl->stats = NULL;
1518 kmem_cache_destroy(tbl->kmem_cachep);
1519 tbl->kmem_cachep = NULL;
1521 return 0;
1523 EXPORT_SYMBOL(neigh_table_clear);
1525 static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1527 struct net *net = sock_net(skb->sk);
1528 struct ndmsg *ndm;
1529 struct nlattr *dst_attr;
1530 struct neigh_table *tbl;
1531 struct net_device *dev = NULL;
1532 int err = -EINVAL;
1534 if (nlmsg_len(nlh) < sizeof(*ndm))
1535 goto out;
1537 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1538 if (dst_attr == NULL)
1539 goto out;
1541 ndm = nlmsg_data(nlh);
1542 if (ndm->ndm_ifindex) {
1543 dev = dev_get_by_index(net, ndm->ndm_ifindex);
1544 if (dev == NULL) {
1545 err = -ENODEV;
1546 goto out;
1550 read_lock(&neigh_tbl_lock);
1551 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1552 struct neighbour *neigh;
1554 if (tbl->family != ndm->ndm_family)
1555 continue;
1556 read_unlock(&neigh_tbl_lock);
1558 if (nla_len(dst_attr) < tbl->key_len)
1559 goto out_dev_put;
1561 if (ndm->ndm_flags & NTF_PROXY) {
1562 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
1563 goto out_dev_put;
1566 if (dev == NULL)
1567 goto out_dev_put;
1569 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1570 if (neigh == NULL) {
1571 err = -ENOENT;
1572 goto out_dev_put;
1575 err = neigh_update(neigh, NULL, NUD_FAILED,
1576 NEIGH_UPDATE_F_OVERRIDE |
1577 NEIGH_UPDATE_F_ADMIN);
1578 neigh_release(neigh);
1579 goto out_dev_put;
1581 read_unlock(&neigh_tbl_lock);
1582 err = -EAFNOSUPPORT;
1584 out_dev_put:
1585 if (dev)
1586 dev_put(dev);
1587 out:
1588 return err;
1591 static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1593 struct net *net = sock_net(skb->sk);
1594 struct ndmsg *ndm;
1595 struct nlattr *tb[NDA_MAX+1];
1596 struct neigh_table *tbl;
1597 struct net_device *dev = NULL;
1598 int err;
1600 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
1601 if (err < 0)
1602 goto out;
1604 err = -EINVAL;
1605 if (tb[NDA_DST] == NULL)
1606 goto out;
1608 ndm = nlmsg_data(nlh);
1609 if (ndm->ndm_ifindex) {
1610 dev = dev_get_by_index(net, ndm->ndm_ifindex);
1611 if (dev == NULL) {
1612 err = -ENODEV;
1613 goto out;
1616 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
1617 goto out_dev_put;
1620 read_lock(&neigh_tbl_lock);
1621 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1622 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
1623 struct neighbour *neigh;
1624 void *dst, *lladdr;
1626 if (tbl->family != ndm->ndm_family)
1627 continue;
1628 read_unlock(&neigh_tbl_lock);
1630 if (nla_len(tb[NDA_DST]) < tbl->key_len)
1631 goto out_dev_put;
1632 dst = nla_data(tb[NDA_DST]);
1633 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
1635 if (ndm->ndm_flags & NTF_PROXY) {
1636 struct pneigh_entry *pn;
1638 err = -ENOBUFS;
1639 pn = pneigh_lookup(tbl, net, dst, dev, 1);
1640 if (pn) {
1641 pn->flags = ndm->ndm_flags;
1642 err = 0;
1644 goto out_dev_put;
1647 if (dev == NULL)
1648 goto out_dev_put;
1650 neigh = neigh_lookup(tbl, dst, dev);
1651 if (neigh == NULL) {
1652 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1653 err = -ENOENT;
1654 goto out_dev_put;
1657 neigh = __neigh_lookup_errno(tbl, dst, dev);
1658 if (IS_ERR(neigh)) {
1659 err = PTR_ERR(neigh);
1660 goto out_dev_put;
1662 } else {
1663 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1664 err = -EEXIST;
1665 neigh_release(neigh);
1666 goto out_dev_put;
1669 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
1670 flags &= ~NEIGH_UPDATE_F_OVERRIDE;
1673 err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
1674 neigh_release(neigh);
1675 goto out_dev_put;
1678 read_unlock(&neigh_tbl_lock);
1679 err = -EAFNOSUPPORT;
1681 out_dev_put:
1682 if (dev)
1683 dev_put(dev);
1684 out:
1685 return err;
1688 static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1690 struct nlattr *nest;
1692 nest = nla_nest_start(skb, NDTA_PARMS);
1693 if (nest == NULL)
1694 return -ENOBUFS;
1696 if (parms->dev)
1697 NLA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex);
1699 NLA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt));
1700 NLA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len);
1701 NLA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen);
1702 NLA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes);
1703 NLA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes);
1704 NLA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes);
1705 NLA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time);
1706 NLA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME,
1707 parms->base_reachable_time);
1708 NLA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime);
1709 NLA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time);
1710 NLA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time);
1711 NLA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay);
1712 NLA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay);
1713 NLA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime);
1715 return nla_nest_end(skb, nest);
1717 nla_put_failure:
1718 nla_nest_cancel(skb, nest);
1719 return -EMSGSIZE;
1722 static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1723 u32 pid, u32 seq, int type, int flags)
1725 struct nlmsghdr *nlh;
1726 struct ndtmsg *ndtmsg;
1728 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1729 if (nlh == NULL)
1730 return -EMSGSIZE;
1732 ndtmsg = nlmsg_data(nlh);
1734 read_lock_bh(&tbl->lock);
1735 ndtmsg->ndtm_family = tbl->family;
1736 ndtmsg->ndtm_pad1 = 0;
1737 ndtmsg->ndtm_pad2 = 0;
1739 NLA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1740 NLA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval);
1741 NLA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1);
1742 NLA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2);
1743 NLA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3);
1746 unsigned long now = jiffies;
1747 unsigned int flush_delta = now - tbl->last_flush;
1748 unsigned int rand_delta = now - tbl->last_rand;
1750 struct ndt_config ndc = {
1751 .ndtc_key_len = tbl->key_len,
1752 .ndtc_entry_size = tbl->entry_size,
1753 .ndtc_entries = atomic_read(&tbl->entries),
1754 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
1755 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
1756 .ndtc_hash_rnd = tbl->hash_rnd,
1757 .ndtc_hash_mask = tbl->hash_mask,
1758 .ndtc_hash_chain_gc = tbl->hash_chain_gc,
1759 .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
1762 NLA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc);
1766 int cpu;
1767 struct ndt_stats ndst;
1769 memset(&ndst, 0, sizeof(ndst));
1771 for_each_possible_cpu(cpu) {
1772 struct neigh_statistics *st;
1774 st = per_cpu_ptr(tbl->stats, cpu);
1775 ndst.ndts_allocs += st->allocs;
1776 ndst.ndts_destroys += st->destroys;
1777 ndst.ndts_hash_grows += st->hash_grows;
1778 ndst.ndts_res_failed += st->res_failed;
1779 ndst.ndts_lookups += st->lookups;
1780 ndst.ndts_hits += st->hits;
1781 ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
1782 ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
1783 ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
1784 ndst.ndts_forced_gc_runs += st->forced_gc_runs;
1787 NLA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst);
1790 BUG_ON(tbl->parms.dev);
1791 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1792 goto nla_put_failure;
1794 read_unlock_bh(&tbl->lock);
1795 return nlmsg_end(skb, nlh);
1797 nla_put_failure:
1798 read_unlock_bh(&tbl->lock);
1799 nlmsg_cancel(skb, nlh);
1800 return -EMSGSIZE;
1803 static int neightbl_fill_param_info(struct sk_buff *skb,
1804 struct neigh_table *tbl,
1805 struct neigh_parms *parms,
1806 u32 pid, u32 seq, int type,
1807 unsigned int flags)
1809 struct ndtmsg *ndtmsg;
1810 struct nlmsghdr *nlh;
1812 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1813 if (nlh == NULL)
1814 return -EMSGSIZE;
1816 ndtmsg = nlmsg_data(nlh);
1818 read_lock_bh(&tbl->lock);
1819 ndtmsg->ndtm_family = tbl->family;
1820 ndtmsg->ndtm_pad1 = 0;
1821 ndtmsg->ndtm_pad2 = 0;
1823 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1824 neightbl_fill_parms(skb, parms) < 0)
1825 goto errout;
1827 read_unlock_bh(&tbl->lock);
1828 return nlmsg_end(skb, nlh);
1829 errout:
1830 read_unlock_bh(&tbl->lock);
1831 nlmsg_cancel(skb, nlh);
1832 return -EMSGSIZE;
1835 static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
1836 [NDTA_NAME] = { .type = NLA_STRING },
1837 [NDTA_THRESH1] = { .type = NLA_U32 },
1838 [NDTA_THRESH2] = { .type = NLA_U32 },
1839 [NDTA_THRESH3] = { .type = NLA_U32 },
1840 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
1841 [NDTA_PARMS] = { .type = NLA_NESTED },
1844 static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
1845 [NDTPA_IFINDEX] = { .type = NLA_U32 },
1846 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
1847 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
1848 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
1849 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
1850 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
1851 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
1852 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
1853 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
1854 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
1855 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
1856 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
1857 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
1860 static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1862 struct net *net = sock_net(skb->sk);
1863 struct neigh_table *tbl;
1864 struct ndtmsg *ndtmsg;
1865 struct nlattr *tb[NDTA_MAX+1];
1866 int err;
1868 err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
1869 nl_neightbl_policy);
1870 if (err < 0)
1871 goto errout;
1873 if (tb[NDTA_NAME] == NULL) {
1874 err = -EINVAL;
1875 goto errout;
1878 ndtmsg = nlmsg_data(nlh);
1879 read_lock(&neigh_tbl_lock);
1880 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1881 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1882 continue;
1884 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0)
1885 break;
1888 if (tbl == NULL) {
1889 err = -ENOENT;
1890 goto errout_locked;
1894 * We acquire tbl->lock to be nice to the periodic timers and
1895 * make sure they always see a consistent set of values.
1897 write_lock_bh(&tbl->lock);
1899 if (tb[NDTA_PARMS]) {
1900 struct nlattr *tbp[NDTPA_MAX+1];
1901 struct neigh_parms *p;
1902 int i, ifindex = 0;
1904 err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
1905 nl_ntbl_parm_policy);
1906 if (err < 0)
1907 goto errout_tbl_lock;
1909 if (tbp[NDTPA_IFINDEX])
1910 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
1912 p = lookup_neigh_params(tbl, net, ifindex);
1913 if (p == NULL) {
1914 err = -ENOENT;
1915 goto errout_tbl_lock;
1918 for (i = 1; i <= NDTPA_MAX; i++) {
1919 if (tbp[i] == NULL)
1920 continue;
1922 switch (i) {
1923 case NDTPA_QUEUE_LEN:
1924 p->queue_len = nla_get_u32(tbp[i]);
1925 break;
1926 case NDTPA_PROXY_QLEN:
1927 p->proxy_qlen = nla_get_u32(tbp[i]);
1928 break;
1929 case NDTPA_APP_PROBES:
1930 p->app_probes = nla_get_u32(tbp[i]);
1931 break;
1932 case NDTPA_UCAST_PROBES:
1933 p->ucast_probes = nla_get_u32(tbp[i]);
1934 break;
1935 case NDTPA_MCAST_PROBES:
1936 p->mcast_probes = nla_get_u32(tbp[i]);
1937 break;
1938 case NDTPA_BASE_REACHABLE_TIME:
1939 p->base_reachable_time = nla_get_msecs(tbp[i]);
1940 break;
1941 case NDTPA_GC_STALETIME:
1942 p->gc_staletime = nla_get_msecs(tbp[i]);
1943 break;
1944 case NDTPA_DELAY_PROBE_TIME:
1945 p->delay_probe_time = nla_get_msecs(tbp[i]);
1946 break;
1947 case NDTPA_RETRANS_TIME:
1948 p->retrans_time = nla_get_msecs(tbp[i]);
1949 break;
1950 case NDTPA_ANYCAST_DELAY:
1951 p->anycast_delay = nla_get_msecs(tbp[i]);
1952 break;
1953 case NDTPA_PROXY_DELAY:
1954 p->proxy_delay = nla_get_msecs(tbp[i]);
1955 break;
1956 case NDTPA_LOCKTIME:
1957 p->locktime = nla_get_msecs(tbp[i]);
1958 break;
1963 if (tb[NDTA_THRESH1])
1964 tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
1966 if (tb[NDTA_THRESH2])
1967 tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
1969 if (tb[NDTA_THRESH3])
1970 tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
1972 if (tb[NDTA_GC_INTERVAL])
1973 tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
1975 err = 0;
1977 errout_tbl_lock:
1978 write_unlock_bh(&tbl->lock);
1979 errout_locked:
1980 read_unlock(&neigh_tbl_lock);
1981 errout:
1982 return err;
1985 static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1987 struct net *net = sock_net(skb->sk);
1988 int family, tidx, nidx = 0;
1989 int tbl_skip = cb->args[0];
1990 int neigh_skip = cb->args[1];
1991 struct neigh_table *tbl;
1993 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
1995 read_lock(&neigh_tbl_lock);
1996 for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) {
1997 struct neigh_parms *p;
1999 if (tidx < tbl_skip || (family && tbl->family != family))
2000 continue;
2002 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid,
2003 cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2004 NLM_F_MULTI) <= 0)
2005 break;
2007 for (nidx = 0, p = tbl->parms.next; p; p = p->next) {
2008 if (!net_eq(neigh_parms_net(p), net))
2009 continue;
2011 if (nidx++ < neigh_skip)
2012 continue;
2014 if (neightbl_fill_param_info(skb, tbl, p,
2015 NETLINK_CB(cb->skb).pid,
2016 cb->nlh->nlmsg_seq,
2017 RTM_NEWNEIGHTBL,
2018 NLM_F_MULTI) <= 0)
2019 goto out;
2022 neigh_skip = 0;
2024 out:
2025 read_unlock(&neigh_tbl_lock);
2026 cb->args[0] = tidx;
2027 cb->args[1] = nidx;
2029 return skb->len;
2032 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2033 u32 pid, u32 seq, int type, unsigned int flags)
2035 unsigned long now = jiffies;
2036 struct nda_cacheinfo ci;
2037 struct nlmsghdr *nlh;
2038 struct ndmsg *ndm;
2040 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2041 if (nlh == NULL)
2042 return -EMSGSIZE;
2044 ndm = nlmsg_data(nlh);
2045 ndm->ndm_family = neigh->ops->family;
2046 ndm->ndm_pad1 = 0;
2047 ndm->ndm_pad2 = 0;
2048 ndm->ndm_flags = neigh->flags;
2049 ndm->ndm_type = neigh->type;
2050 ndm->ndm_ifindex = neigh->dev->ifindex;
2052 NLA_PUT(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key);
2054 read_lock_bh(&neigh->lock);
2055 ndm->ndm_state = neigh->nud_state;
2056 if ((neigh->nud_state & NUD_VALID) &&
2057 nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, neigh->ha) < 0) {
2058 read_unlock_bh(&neigh->lock);
2059 goto nla_put_failure;
2062 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2063 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2064 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
2065 ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1;
2066 read_unlock_bh(&neigh->lock);
2068 NLA_PUT_U32(skb, NDA_PROBES, atomic_read(&neigh->probes));
2069 NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
2071 return nlmsg_end(skb, nlh);
2073 nla_put_failure:
2074 nlmsg_cancel(skb, nlh);
2075 return -EMSGSIZE;
2078 static void neigh_update_notify(struct neighbour *neigh)
2080 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2081 __neigh_notify(neigh, RTM_NEWNEIGH, 0);
2084 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2085 struct netlink_callback *cb)
2087 struct net * net = sock_net(skb->sk);
2088 struct neighbour *n;
2089 int rc, h, s_h = cb->args[1];
2090 int idx, s_idx = idx = cb->args[2];
2092 read_lock_bh(&tbl->lock);
2093 for (h = 0; h <= tbl->hash_mask; h++) {
2094 if (h < s_h)
2095 continue;
2096 if (h > s_h)
2097 s_idx = 0;
2098 for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next) {
2099 int lidx;
2100 if (dev_net(n->dev) != net)
2101 continue;
2102 lidx = idx++;
2103 if (lidx < s_idx)
2104 continue;
2105 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
2106 cb->nlh->nlmsg_seq,
2107 RTM_NEWNEIGH,
2108 NLM_F_MULTI) <= 0) {
2109 read_unlock_bh(&tbl->lock);
2110 rc = -1;
2111 goto out;
2115 read_unlock_bh(&tbl->lock);
2116 rc = skb->len;
2117 out:
2118 cb->args[1] = h;
2119 cb->args[2] = idx;
2120 return rc;
2123 static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
2125 struct neigh_table *tbl;
2126 int t, family, s_t;
2128 read_lock(&neigh_tbl_lock);
2129 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
2130 s_t = cb->args[0];
2132 for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) {
2133 if (t < s_t || (family && tbl->family != family))
2134 continue;
2135 if (t > s_t)
2136 memset(&cb->args[1], 0, sizeof(cb->args) -
2137 sizeof(cb->args[0]));
2138 if (neigh_dump_table(tbl, skb, cb) < 0)
2139 break;
2141 read_unlock(&neigh_tbl_lock);
2143 cb->args[0] = t;
2144 return skb->len;
2147 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
2149 int chain;
2151 read_lock_bh(&tbl->lock);
2152 for (chain = 0; chain <= tbl->hash_mask; chain++) {
2153 struct neighbour *n;
2155 for (n = tbl->hash_buckets[chain]; n; n = n->next)
2156 cb(n, cookie);
2158 read_unlock_bh(&tbl->lock);
2160 EXPORT_SYMBOL(neigh_for_each);
2162 /* The tbl->lock must be held as a writer and BH disabled. */
2163 void __neigh_for_each_release(struct neigh_table *tbl,
2164 int (*cb)(struct neighbour *))
2166 int chain;
2168 for (chain = 0; chain <= tbl->hash_mask; chain++) {
2169 struct neighbour *n, **np;
2171 np = &tbl->hash_buckets[chain];
2172 while ((n = *np) != NULL) {
2173 int release;
2175 write_lock(&n->lock);
2176 release = cb(n);
2177 if (release) {
2178 *np = n->next;
2179 n->dead = 1;
2180 } else
2181 np = &n->next;
2182 write_unlock(&n->lock);
2183 if (release)
2184 neigh_cleanup_and_release(n);
2188 EXPORT_SYMBOL(__neigh_for_each_release);
2190 #ifdef CONFIG_PROC_FS
2192 static struct neighbour *neigh_get_first(struct seq_file *seq)
2194 struct neigh_seq_state *state = seq->private;
2195 struct net *net = seq_file_net(seq);
2196 struct neigh_table *tbl = state->tbl;
2197 struct neighbour *n = NULL;
2198 int bucket = state->bucket;
2200 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2201 for (bucket = 0; bucket <= tbl->hash_mask; bucket++) {
2202 n = tbl->hash_buckets[bucket];
2204 while (n) {
2205 if (!net_eq(dev_net(n->dev), net))
2206 goto next;
2207 if (state->neigh_sub_iter) {
2208 loff_t fakep = 0;
2209 void *v;
2211 v = state->neigh_sub_iter(state, n, &fakep);
2212 if (!v)
2213 goto next;
2215 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2216 break;
2217 if (n->nud_state & ~NUD_NOARP)
2218 break;
2219 next:
2220 n = n->next;
2223 if (n)
2224 break;
2226 state->bucket = bucket;
2228 return n;
2231 static struct neighbour *neigh_get_next(struct seq_file *seq,
2232 struct neighbour *n,
2233 loff_t *pos)
2235 struct neigh_seq_state *state = seq->private;
2236 struct net *net = seq_file_net(seq);
2237 struct neigh_table *tbl = state->tbl;
2239 if (state->neigh_sub_iter) {
2240 void *v = state->neigh_sub_iter(state, n, pos);
2241 if (v)
2242 return n;
2244 n = n->next;
2246 while (1) {
2247 while (n) {
2248 if (!net_eq(dev_net(n->dev), net))
2249 goto next;
2250 if (state->neigh_sub_iter) {
2251 void *v = state->neigh_sub_iter(state, n, pos);
2252 if (v)
2253 return n;
2254 goto next;
2256 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2257 break;
2259 if (n->nud_state & ~NUD_NOARP)
2260 break;
2261 next:
2262 n = n->next;
2265 if (n)
2266 break;
2268 if (++state->bucket > tbl->hash_mask)
2269 break;
2271 n = tbl->hash_buckets[state->bucket];
2274 if (n && pos)
2275 --(*pos);
2276 return n;
2279 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2281 struct neighbour *n = neigh_get_first(seq);
2283 if (n) {
2284 --(*pos);
2285 while (*pos) {
2286 n = neigh_get_next(seq, n, pos);
2287 if (!n)
2288 break;
2291 return *pos ? NULL : n;
2294 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2296 struct neigh_seq_state *state = seq->private;
2297 struct net *net = seq_file_net(seq);
2298 struct neigh_table *tbl = state->tbl;
2299 struct pneigh_entry *pn = NULL;
2300 int bucket = state->bucket;
2302 state->flags |= NEIGH_SEQ_IS_PNEIGH;
2303 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2304 pn = tbl->phash_buckets[bucket];
2305 while (pn && !net_eq(pneigh_net(pn), net))
2306 pn = pn->next;
2307 if (pn)
2308 break;
2310 state->bucket = bucket;
2312 return pn;
2315 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2316 struct pneigh_entry *pn,
2317 loff_t *pos)
2319 struct neigh_seq_state *state = seq->private;
2320 struct net *net = seq_file_net(seq);
2321 struct neigh_table *tbl = state->tbl;
2323 pn = pn->next;
2324 while (!pn) {
2325 if (++state->bucket > PNEIGH_HASHMASK)
2326 break;
2327 pn = tbl->phash_buckets[state->bucket];
2328 while (pn && !net_eq(pneigh_net(pn), net))
2329 pn = pn->next;
2330 if (pn)
2331 break;
2334 if (pn && pos)
2335 --(*pos);
2337 return pn;
2340 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2342 struct pneigh_entry *pn = pneigh_get_first(seq);
2344 if (pn) {
2345 --(*pos);
2346 while (*pos) {
2347 pn = pneigh_get_next(seq, pn, pos);
2348 if (!pn)
2349 break;
2352 return *pos ? NULL : pn;
2355 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2357 struct neigh_seq_state *state = seq->private;
2358 void *rc;
2359 loff_t idxpos = *pos;
2361 rc = neigh_get_idx(seq, &idxpos);
2362 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2363 rc = pneigh_get_idx(seq, &idxpos);
2365 return rc;
2368 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2369 __acquires(tbl->lock)
2371 struct neigh_seq_state *state = seq->private;
2373 state->tbl = tbl;
2374 state->bucket = 0;
2375 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2377 read_lock_bh(&tbl->lock);
2379 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
2381 EXPORT_SYMBOL(neigh_seq_start);
2383 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2385 struct neigh_seq_state *state;
2386 void *rc;
2388 if (v == SEQ_START_TOKEN) {
2389 rc = neigh_get_first(seq);
2390 goto out;
2393 state = seq->private;
2394 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
2395 rc = neigh_get_next(seq, v, NULL);
2396 if (rc)
2397 goto out;
2398 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2399 rc = pneigh_get_first(seq);
2400 } else {
2401 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
2402 rc = pneigh_get_next(seq, v, NULL);
2404 out:
2405 ++(*pos);
2406 return rc;
2408 EXPORT_SYMBOL(neigh_seq_next);
2410 void neigh_seq_stop(struct seq_file *seq, void *v)
2411 __releases(tbl->lock)
2413 struct neigh_seq_state *state = seq->private;
2414 struct neigh_table *tbl = state->tbl;
2416 read_unlock_bh(&tbl->lock);
2418 EXPORT_SYMBOL(neigh_seq_stop);
2420 /* statistics via seq_file */
2422 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
2424 struct proc_dir_entry *pde = seq->private;
2425 struct neigh_table *tbl = pde->data;
2426 int cpu;
2428 if (*pos == 0)
2429 return SEQ_START_TOKEN;
2431 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
2432 if (!cpu_possible(cpu))
2433 continue;
2434 *pos = cpu+1;
2435 return per_cpu_ptr(tbl->stats, cpu);
2437 return NULL;
2440 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2442 struct proc_dir_entry *pde = seq->private;
2443 struct neigh_table *tbl = pde->data;
2444 int cpu;
2446 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
2447 if (!cpu_possible(cpu))
2448 continue;
2449 *pos = cpu+1;
2450 return per_cpu_ptr(tbl->stats, cpu);
2452 return NULL;
2455 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
2460 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
2462 struct proc_dir_entry *pde = seq->private;
2463 struct neigh_table *tbl = pde->data;
2464 struct neigh_statistics *st = v;
2466 if (v == SEQ_START_TOKEN) {
2467 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\n");
2468 return 0;
2471 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
2472 "%08lx %08lx %08lx %08lx %08lx\n",
2473 atomic_read(&tbl->entries),
2475 st->allocs,
2476 st->destroys,
2477 st->hash_grows,
2479 st->lookups,
2480 st->hits,
2482 st->res_failed,
2484 st->rcv_probes_mcast,
2485 st->rcv_probes_ucast,
2487 st->periodic_gc_runs,
2488 st->forced_gc_runs,
2489 st->unres_discards
2492 return 0;
2495 static const struct seq_operations neigh_stat_seq_ops = {
2496 .start = neigh_stat_seq_start,
2497 .next = neigh_stat_seq_next,
2498 .stop = neigh_stat_seq_stop,
2499 .show = neigh_stat_seq_show,
2502 static int neigh_stat_seq_open(struct inode *inode, struct file *file)
2504 int ret = seq_open(file, &neigh_stat_seq_ops);
2506 if (!ret) {
2507 struct seq_file *sf = file->private_data;
2508 sf->private = PDE(inode);
2510 return ret;
2513 static const struct file_operations neigh_stat_seq_fops = {
2514 .owner = THIS_MODULE,
2515 .open = neigh_stat_seq_open,
2516 .read = seq_read,
2517 .llseek = seq_lseek,
2518 .release = seq_release,
2521 #endif /* CONFIG_PROC_FS */
2523 static inline size_t neigh_nlmsg_size(void)
2525 return NLMSG_ALIGN(sizeof(struct ndmsg))
2526 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2527 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2528 + nla_total_size(sizeof(struct nda_cacheinfo))
2529 + nla_total_size(4); /* NDA_PROBES */
2532 static void __neigh_notify(struct neighbour *n, int type, int flags)
2534 struct net *net = dev_net(n->dev);
2535 struct sk_buff *skb;
2536 int err = -ENOBUFS;
2538 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
2539 if (skb == NULL)
2540 goto errout;
2542 err = neigh_fill_info(skb, n, 0, 0, type, flags);
2543 if (err < 0) {
2544 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
2545 WARN_ON(err == -EMSGSIZE);
2546 kfree_skb(skb);
2547 goto errout;
2549 err = rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2550 errout:
2551 if (err < 0)
2552 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2555 #ifdef CONFIG_ARPD
2556 void neigh_app_ns(struct neighbour *n)
2558 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
2560 EXPORT_SYMBOL(neigh_app_ns);
2561 #endif /* CONFIG_ARPD */
2563 #ifdef CONFIG_SYSCTL
2565 static struct neigh_sysctl_table {
2566 struct ctl_table_header *sysctl_header;
2567 struct ctl_table neigh_vars[__NET_NEIGH_MAX];
2568 char *dev_name;
2569 } neigh_sysctl_template __read_mostly = {
2570 .neigh_vars = {
2572 .ctl_name = NET_NEIGH_MCAST_SOLICIT,
2573 .procname = "mcast_solicit",
2574 .maxlen = sizeof(int),
2575 .mode = 0644,
2576 .proc_handler = &proc_dointvec,
2579 .ctl_name = NET_NEIGH_UCAST_SOLICIT,
2580 .procname = "ucast_solicit",
2581 .maxlen = sizeof(int),
2582 .mode = 0644,
2583 .proc_handler = &proc_dointvec,
2586 .ctl_name = NET_NEIGH_APP_SOLICIT,
2587 .procname = "app_solicit",
2588 .maxlen = sizeof(int),
2589 .mode = 0644,
2590 .proc_handler = &proc_dointvec,
2593 .procname = "retrans_time",
2594 .maxlen = sizeof(int),
2595 .mode = 0644,
2596 .proc_handler = &proc_dointvec_userhz_jiffies,
2599 .ctl_name = NET_NEIGH_REACHABLE_TIME,
2600 .procname = "base_reachable_time",
2601 .maxlen = sizeof(int),
2602 .mode = 0644,
2603 .proc_handler = &proc_dointvec_jiffies,
2604 .strategy = &sysctl_jiffies,
2607 .ctl_name = NET_NEIGH_DELAY_PROBE_TIME,
2608 .procname = "delay_first_probe_time",
2609 .maxlen = sizeof(int),
2610 .mode = 0644,
2611 .proc_handler = &proc_dointvec_jiffies,
2612 .strategy = &sysctl_jiffies,
2615 .ctl_name = NET_NEIGH_GC_STALE_TIME,
2616 .procname = "gc_stale_time",
2617 .maxlen = sizeof(int),
2618 .mode = 0644,
2619 .proc_handler = &proc_dointvec_jiffies,
2620 .strategy = &sysctl_jiffies,
2623 .ctl_name = NET_NEIGH_UNRES_QLEN,
2624 .procname = "unres_qlen",
2625 .maxlen = sizeof(int),
2626 .mode = 0644,
2627 .proc_handler = &proc_dointvec,
2630 .ctl_name = NET_NEIGH_PROXY_QLEN,
2631 .procname = "proxy_qlen",
2632 .maxlen = sizeof(int),
2633 .mode = 0644,
2634 .proc_handler = &proc_dointvec,
2637 .procname = "anycast_delay",
2638 .maxlen = sizeof(int),
2639 .mode = 0644,
2640 .proc_handler = &proc_dointvec_userhz_jiffies,
2643 .procname = "proxy_delay",
2644 .maxlen = sizeof(int),
2645 .mode = 0644,
2646 .proc_handler = &proc_dointvec_userhz_jiffies,
2649 .procname = "locktime",
2650 .maxlen = sizeof(int),
2651 .mode = 0644,
2652 .proc_handler = &proc_dointvec_userhz_jiffies,
2655 .ctl_name = NET_NEIGH_RETRANS_TIME_MS,
2656 .procname = "retrans_time_ms",
2657 .maxlen = sizeof(int),
2658 .mode = 0644,
2659 .proc_handler = &proc_dointvec_ms_jiffies,
2660 .strategy = &sysctl_ms_jiffies,
2663 .ctl_name = NET_NEIGH_REACHABLE_TIME_MS,
2664 .procname = "base_reachable_time_ms",
2665 .maxlen = sizeof(int),
2666 .mode = 0644,
2667 .proc_handler = &proc_dointvec_ms_jiffies,
2668 .strategy = &sysctl_ms_jiffies,
2671 .ctl_name = NET_NEIGH_GC_INTERVAL,
2672 .procname = "gc_interval",
2673 .maxlen = sizeof(int),
2674 .mode = 0644,
2675 .proc_handler = &proc_dointvec_jiffies,
2676 .strategy = &sysctl_jiffies,
2679 .ctl_name = NET_NEIGH_GC_THRESH1,
2680 .procname = "gc_thresh1",
2681 .maxlen = sizeof(int),
2682 .mode = 0644,
2683 .proc_handler = &proc_dointvec,
2686 .ctl_name = NET_NEIGH_GC_THRESH2,
2687 .procname = "gc_thresh2",
2688 .maxlen = sizeof(int),
2689 .mode = 0644,
2690 .proc_handler = &proc_dointvec,
2693 .ctl_name = NET_NEIGH_GC_THRESH3,
2694 .procname = "gc_thresh3",
2695 .maxlen = sizeof(int),
2696 .mode = 0644,
2697 .proc_handler = &proc_dointvec,
2703 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
2704 int p_id, int pdev_id, char *p_name,
2705 proc_handler *handler, ctl_handler *strategy)
2707 struct neigh_sysctl_table *t;
2708 const char *dev_name_source = NULL;
2710 #define NEIGH_CTL_PATH_ROOT 0
2711 #define NEIGH_CTL_PATH_PROTO 1
2712 #define NEIGH_CTL_PATH_NEIGH 2
2713 #define NEIGH_CTL_PATH_DEV 3
2715 struct ctl_path neigh_path[] = {
2716 { .procname = "net", .ctl_name = CTL_NET, },
2717 { .procname = "proto", .ctl_name = 0, },
2718 { .procname = "neigh", .ctl_name = 0, },
2719 { .procname = "default", .ctl_name = NET_PROTO_CONF_DEFAULT, },
2720 { },
2723 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
2724 if (!t)
2725 goto err;
2727 t->neigh_vars[0].data = &p->mcast_probes;
2728 t->neigh_vars[1].data = &p->ucast_probes;
2729 t->neigh_vars[2].data = &p->app_probes;
2730 t->neigh_vars[3].data = &p->retrans_time;
2731 t->neigh_vars[4].data = &p->base_reachable_time;
2732 t->neigh_vars[5].data = &p->delay_probe_time;
2733 t->neigh_vars[6].data = &p->gc_staletime;
2734 t->neigh_vars[7].data = &p->queue_len;
2735 t->neigh_vars[8].data = &p->proxy_qlen;
2736 t->neigh_vars[9].data = &p->anycast_delay;
2737 t->neigh_vars[10].data = &p->proxy_delay;
2738 t->neigh_vars[11].data = &p->locktime;
2739 t->neigh_vars[12].data = &p->retrans_time;
2740 t->neigh_vars[13].data = &p->base_reachable_time;
2742 if (dev) {
2743 dev_name_source = dev->name;
2744 neigh_path[NEIGH_CTL_PATH_DEV].ctl_name = dev->ifindex;
2745 /* Terminate the table early */
2746 memset(&t->neigh_vars[14], 0, sizeof(t->neigh_vars[14]));
2747 } else {
2748 dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname;
2749 t->neigh_vars[14].data = (int *)(p + 1);
2750 t->neigh_vars[15].data = (int *)(p + 1) + 1;
2751 t->neigh_vars[16].data = (int *)(p + 1) + 2;
2752 t->neigh_vars[17].data = (int *)(p + 1) + 3;
2756 if (handler || strategy) {
2757 /* RetransTime */
2758 t->neigh_vars[3].proc_handler = handler;
2759 t->neigh_vars[3].strategy = strategy;
2760 t->neigh_vars[3].extra1 = dev;
2761 if (!strategy)
2762 t->neigh_vars[3].ctl_name = CTL_UNNUMBERED;
2763 /* ReachableTime */
2764 t->neigh_vars[4].proc_handler = handler;
2765 t->neigh_vars[4].strategy = strategy;
2766 t->neigh_vars[4].extra1 = dev;
2767 if (!strategy)
2768 t->neigh_vars[4].ctl_name = CTL_UNNUMBERED;
2769 /* RetransTime (in milliseconds)*/
2770 t->neigh_vars[12].proc_handler = handler;
2771 t->neigh_vars[12].strategy = strategy;
2772 t->neigh_vars[12].extra1 = dev;
2773 if (!strategy)
2774 t->neigh_vars[12].ctl_name = CTL_UNNUMBERED;
2775 /* ReachableTime (in milliseconds) */
2776 t->neigh_vars[13].proc_handler = handler;
2777 t->neigh_vars[13].strategy = strategy;
2778 t->neigh_vars[13].extra1 = dev;
2779 if (!strategy)
2780 t->neigh_vars[13].ctl_name = CTL_UNNUMBERED;
2783 t->dev_name = kstrdup(dev_name_source, GFP_KERNEL);
2784 if (!t->dev_name)
2785 goto free;
2787 neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name;
2788 neigh_path[NEIGH_CTL_PATH_NEIGH].ctl_name = pdev_id;
2789 neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name;
2790 neigh_path[NEIGH_CTL_PATH_PROTO].ctl_name = p_id;
2792 t->sysctl_header =
2793 register_net_sysctl_table(neigh_parms_net(p), neigh_path, t->neigh_vars);
2794 if (!t->sysctl_header)
2795 goto free_procname;
2797 p->sysctl_table = t;
2798 return 0;
2800 free_procname:
2801 kfree(t->dev_name);
2802 free:
2803 kfree(t);
2804 err:
2805 return -ENOBUFS;
2807 EXPORT_SYMBOL(neigh_sysctl_register);
2809 void neigh_sysctl_unregister(struct neigh_parms *p)
2811 if (p->sysctl_table) {
2812 struct neigh_sysctl_table *t = p->sysctl_table;
2813 p->sysctl_table = NULL;
2814 unregister_sysctl_table(t->sysctl_header);
2815 kfree(t->dev_name);
2816 kfree(t);
2819 EXPORT_SYMBOL(neigh_sysctl_unregister);
2821 #endif /* CONFIG_SYSCTL */
2823 static int __init neigh_init(void)
2825 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL);
2826 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL);
2827 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info);
2829 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info);
2830 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL);
2832 return 0;
2835 subsys_initcall(neigh_init);