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 #include <linux/config.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/socket.h>
23 #include <linux/sched.h>
24 #include <linux/netdevice.h>
25 #include <linux/proc_fs.h>
27 #include <linux/sysctl.h>
29 #include <linux/times.h>
30 #include <net/neighbour.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/random.h>
38 #define NEIGH_PRINTK(x...) printk(x)
39 #define NEIGH_NOPRINTK(x...) do { ; } while(0)
40 #define NEIGH_PRINTK0 NEIGH_PRINTK
41 #define NEIGH_PRINTK1 NEIGH_NOPRINTK
42 #define NEIGH_PRINTK2 NEIGH_NOPRINTK
46 #define NEIGH_PRINTK1 NEIGH_PRINTK
50 #define NEIGH_PRINTK2 NEIGH_PRINTK
53 #define PNEIGH_HASHMASK 0xF
55 static void neigh_timer_handler(unsigned long arg);
57 static void neigh_app_notify(struct neighbour *n);
59 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
60 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
62 static int neigh_glbl_allocs;
63 static struct neigh_table *neigh_tables;
64 static 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.
93 The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
94 list of neighbour tables. This list is used only in process context,
97 static rwlock_t neigh_tbl_lock = RW_LOCK_UNLOCKED;
99 static int neigh_blackhole(struct sk_buff *skb)
106 * It is random distribution in the interval (1/2)*base...(3/2)*base.
107 * It corresponds to default IPv6 settings and is not overridable,
108 * because it is really reasonable choice.
111 unsigned long neigh_rand_reach_time(unsigned long base)
113 return (net_random() % base) + (base >> 1);
117 static int neigh_forced_gc(struct neigh_table *tbl)
122 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
124 write_lock_bh(&tbl->lock);
125 for (i = 0; i <= tbl->hash_mask; i++) {
126 struct neighbour *n, **np;
128 np = &tbl->hash_buckets[i];
129 while ((n = *np) != NULL) {
130 /* Neighbour record may be discarded if:
131 * - nobody refers to it.
132 * - it is not permanent
134 write_lock(&n->lock);
135 if (atomic_read(&n->refcnt) == 1 &&
136 !(n->nud_state & NUD_PERMANENT)) {
140 write_unlock(&n->lock);
144 write_unlock(&n->lock);
149 tbl->last_flush = jiffies;
151 write_unlock_bh(&tbl->lock);
156 static int neigh_del_timer(struct neighbour *n)
158 if ((n->nud_state & NUD_IN_TIMER) &&
159 del_timer(&n->timer)) {
166 static void pneigh_queue_purge(struct sk_buff_head *list)
170 while ((skb = skb_dequeue(list)) != NULL) {
176 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
180 write_lock_bh(&tbl->lock);
182 for (i=0; i <= tbl->hash_mask; i++) {
183 struct neighbour *n, **np;
185 np = &tbl->hash_buckets[i];
186 while ((n = *np) != NULL) {
187 if (dev && n->dev != dev) {
192 write_lock_bh(&n->lock);
195 write_unlock_bh(&n->lock);
200 write_unlock_bh(&tbl->lock);
203 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
207 write_lock_bh(&tbl->lock);
209 for (i = 0; i <= tbl->hash_mask; i++) {
210 struct neighbour *n, **np = &tbl->hash_buckets[i];
212 while ((n = *np) != NULL) {
213 if (dev && n->dev != dev) {
218 write_lock(&n->lock);
222 if (atomic_read(&n->refcnt) != 1) {
223 /* The most unpleasant situation.
224 We must destroy neighbour entry,
225 but someone still uses it.
227 The destroy will be delayed until
228 the last user releases us, but
229 we must kill timers etc. and move
232 skb_queue_purge(&n->arp_queue);
233 n->output = neigh_blackhole;
234 if (n->nud_state & NUD_VALID)
235 n->nud_state = NUD_NOARP;
237 n->nud_state = NUD_NONE;
238 NEIGH_PRINTK2("neigh %p is stray.\n", n);
240 write_unlock(&n->lock);
245 pneigh_ifdown(tbl, dev);
246 write_unlock_bh(&tbl->lock);
248 del_timer_sync(&tbl->proxy_timer);
249 pneigh_queue_purge(&tbl->proxy_queue);
253 static struct neighbour *neigh_alloc(struct neigh_table *tbl)
255 struct neighbour *n = NULL;
256 unsigned long now = jiffies;
258 if (tbl->entries > tbl->gc_thresh3 ||
259 (tbl->entries > tbl->gc_thresh2 &&
260 time_after(now, tbl->last_flush + 5 * HZ))) {
261 if (!neigh_forced_gc(tbl) &&
262 tbl->entries > tbl->gc_thresh3)
266 n = kmem_cache_alloc(tbl->kmem_cachep, SLAB_ATOMIC);
270 memset(n, 0, tbl->entry_size);
272 skb_queue_head_init(&n->arp_queue);
273 n->lock = RW_LOCK_UNLOCKED;
274 n->updated = n->used = now;
275 n->nud_state = NUD_NONE;
276 n->output = neigh_blackhole;
277 n->parms = neigh_parms_clone(&tbl->parms);
278 init_timer(&n->timer);
279 n->timer.function = neigh_timer_handler;
280 n->timer.data = (unsigned long)n;
282 NEIGH_CACHE_STAT_INC(tbl, allocs);
286 atomic_set(&n->refcnt, 1);
292 static struct neighbour **neigh_hash_alloc(unsigned int entries)
294 unsigned long size = entries * sizeof(struct neighbour *);
295 struct neighbour **ret;
297 if (size <= PAGE_SIZE) {
298 ret = kmalloc(size, GFP_ATOMIC);
300 ret = (struct neighbour **)
301 __get_free_pages(GFP_ATOMIC, get_order(size));
304 memset(ret, 0, size);
309 static void neigh_hash_free(struct neighbour **hash, unsigned int entries)
311 unsigned long size = entries * sizeof(struct neighbour *);
313 if (size <= PAGE_SIZE)
316 free_pages((unsigned long)hash, get_order(size));
319 static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries)
321 struct neighbour **new_hash, **old_hash;
322 unsigned int i, new_hash_mask, old_entries;
324 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
326 BUG_ON(new_entries & (new_entries - 1));
327 new_hash = neigh_hash_alloc(new_entries);
331 old_entries = tbl->hash_mask + 1;
332 new_hash_mask = new_entries - 1;
333 old_hash = tbl->hash_buckets;
335 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
336 for (i = 0; i < old_entries; i++) {
337 struct neighbour *n, *next;
339 for (n = old_hash[i]; n; n = next) {
340 unsigned int hash_val = tbl->hash(n->primary_key, n->dev);
342 hash_val &= new_hash_mask;
345 n->next = new_hash[hash_val];
346 new_hash[hash_val] = n;
349 tbl->hash_buckets = new_hash;
350 tbl->hash_mask = new_hash_mask;
352 neigh_hash_free(old_hash, old_entries);
355 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
356 struct net_device *dev)
359 int key_len = tbl->key_len;
360 u32 hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
362 NEIGH_CACHE_STAT_INC(tbl, lookups);
364 read_lock_bh(&tbl->lock);
365 for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
366 if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
368 NEIGH_CACHE_STAT_INC(tbl, hits);
372 read_unlock_bh(&tbl->lock);
376 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, const void *pkey)
379 int key_len = tbl->key_len;
380 u32 hash_val = tbl->hash(pkey, NULL) & tbl->hash_mask;
382 NEIGH_CACHE_STAT_INC(tbl, lookups);
384 read_lock_bh(&tbl->lock);
385 for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
386 if (!memcmp(n->primary_key, pkey, key_len)) {
388 NEIGH_CACHE_STAT_INC(tbl, hits);
392 read_unlock_bh(&tbl->lock);
396 struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
397 struct net_device *dev)
400 int key_len = tbl->key_len;
402 struct neighbour *n1, *rc, *n = neigh_alloc(tbl);
405 rc = ERR_PTR(-ENOBUFS);
409 memcpy(n->primary_key, pkey, key_len);
413 /* Protocol specific setup. */
414 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
416 goto out_neigh_release;
419 /* Device specific setup. */
420 if (n->parms->neigh_setup &&
421 (error = n->parms->neigh_setup(n)) < 0) {
423 goto out_neigh_release;
426 n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
428 write_lock_bh(&tbl->lock);
430 if (tbl->entries > (tbl->hash_mask + 1))
431 neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1);
433 hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
435 if (n->parms->dead) {
436 rc = ERR_PTR(-EINVAL);
440 for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) {
441 if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
448 n->next = tbl->hash_buckets[hash_val];
449 tbl->hash_buckets[hash_val] = n;
452 write_unlock_bh(&tbl->lock);
453 NEIGH_PRINTK2("neigh %p is created.\n", n);
458 write_unlock_bh(&tbl->lock);
464 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, const void *pkey,
465 struct net_device *dev, int creat)
467 struct pneigh_entry *n;
468 int key_len = tbl->key_len;
469 u32 hash_val = *(u32 *)(pkey + key_len - 4);
471 hash_val ^= (hash_val >> 16);
472 hash_val ^= hash_val >> 8;
473 hash_val ^= hash_val >> 4;
474 hash_val &= PNEIGH_HASHMASK;
476 read_lock_bh(&tbl->lock);
478 for (n = tbl->phash_buckets[hash_val]; n; n = n->next) {
479 if (!memcmp(n->key, pkey, key_len) &&
480 (n->dev == dev || !n->dev)) {
481 read_unlock_bh(&tbl->lock);
485 read_unlock_bh(&tbl->lock);
490 n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
494 memcpy(n->key, pkey, key_len);
499 if (tbl->pconstructor && tbl->pconstructor(n)) {
507 write_lock_bh(&tbl->lock);
508 n->next = tbl->phash_buckets[hash_val];
509 tbl->phash_buckets[hash_val] = n;
510 write_unlock_bh(&tbl->lock);
516 int pneigh_delete(struct neigh_table *tbl, const void *pkey,
517 struct net_device *dev)
519 struct pneigh_entry *n, **np;
520 int key_len = tbl->key_len;
521 u32 hash_val = *(u32 *)(pkey + key_len - 4);
523 hash_val ^= (hash_val >> 16);
524 hash_val ^= hash_val >> 8;
525 hash_val ^= hash_val >> 4;
526 hash_val &= PNEIGH_HASHMASK;
528 write_lock_bh(&tbl->lock);
529 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
531 if (!memcmp(n->key, pkey, key_len) && n->dev == dev) {
533 write_unlock_bh(&tbl->lock);
534 if (tbl->pdestructor)
542 write_unlock_bh(&tbl->lock);
546 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
548 struct pneigh_entry *n, **np;
551 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
552 np = &tbl->phash_buckets[h];
553 while ((n = *np) != NULL) {
554 if (!dev || n->dev == dev) {
556 if (tbl->pdestructor)
571 * neighbour must already be out of the table;
574 void neigh_destroy(struct neighbour *neigh)
578 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
582 "Destroying alive neighbour %p\n", neigh);
587 if (neigh_del_timer(neigh))
588 printk(KERN_WARNING "Impossible event.\n");
590 while ((hh = neigh->hh) != NULL) {
591 neigh->hh = hh->hh_next;
593 write_lock_bh(&hh->hh_lock);
594 hh->hh_output = neigh_blackhole;
595 write_unlock_bh(&hh->hh_lock);
596 if (atomic_dec_and_test(&hh->hh_refcnt))
600 if (neigh->ops && neigh->ops->destructor)
601 (neigh->ops->destructor)(neigh);
603 skb_queue_purge(&neigh->arp_queue);
606 neigh_parms_put(neigh->parms);
608 NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
611 neigh->tbl->entries--;
612 kmem_cache_free(neigh->tbl->kmem_cachep, neigh);
615 /* Neighbour state is suspicious;
618 Called with write_locked neigh.
620 static void neigh_suspect(struct neighbour *neigh)
624 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
626 neigh->output = neigh->ops->output;
628 for (hh = neigh->hh; hh; hh = hh->hh_next)
629 hh->hh_output = neigh->ops->output;
632 /* Neighbour state is OK;
635 Called with write_locked neigh.
637 static void neigh_connect(struct neighbour *neigh)
641 NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
643 neigh->output = neigh->ops->connected_output;
645 for (hh = neigh->hh; hh; hh = hh->hh_next)
646 hh->hh_output = neigh->ops->hh_output;
649 static void neigh_periodic_timer(unsigned long arg)
651 struct neigh_table *tbl = (struct neigh_table *)arg;
652 struct neighbour *n, **np;
653 unsigned long expire, now = jiffies;
655 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
657 write_lock(&tbl->lock);
660 * periodically recompute ReachableTime from random function
663 if (time_after(now, tbl->last_rand + 300 * HZ)) {
664 struct neigh_parms *p;
665 tbl->last_rand = now;
666 for (p = &tbl->parms; p; p = p->next)
668 neigh_rand_reach_time(p->base_reachable_time);
671 np = &tbl->hash_buckets[tbl->hash_chain_gc];
672 tbl->hash_chain_gc = ((tbl->hash_chain_gc + 1) & tbl->hash_mask);
674 while ((n = *np) != NULL) {
677 write_lock(&n->lock);
679 state = n->nud_state;
680 if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
681 write_unlock(&n->lock);
685 if (time_before(n->used, n->confirmed))
686 n->used = n->confirmed;
688 if (atomic_read(&n->refcnt) == 1 &&
689 (state == NUD_FAILED ||
690 time_after(now, n->used + n->parms->gc_staletime))) {
693 write_unlock(&n->lock);
697 write_unlock(&n->lock);
703 /* Cycle through all hash buckets every base_reachable_time/2 ticks.
704 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
705 * base_reachable_time.
707 expire = tbl->parms.base_reachable_time >> 1;
708 expire /= (tbl->hash_mask + 1);
712 mod_timer(&tbl->gc_timer, now + expire);
714 write_unlock(&tbl->lock);
717 static __inline__ int neigh_max_probes(struct neighbour *n)
719 struct neigh_parms *p = n->parms;
720 return (n->nud_state & NUD_PROBE ?
722 p->ucast_probes + p->app_probes + p->mcast_probes);
726 /* Called when a timer expires for a neighbour entry. */
728 static void neigh_timer_handler(unsigned long arg)
730 unsigned long now, next;
731 struct neighbour *neigh = (struct neighbour *)arg;
735 write_lock(&neigh->lock);
737 state = neigh->nud_state;
741 if (!(state & NUD_IN_TIMER)) {
743 printk(KERN_WARNING "neigh: timer & !nud_in_timer\n");
748 if (state & NUD_REACHABLE) {
749 if (time_before_eq(now,
750 neigh->confirmed + neigh->parms->reachable_time)) {
751 NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
752 next = neigh->confirmed + neigh->parms->reachable_time;
753 } else if (time_before_eq(now,
754 neigh->used + neigh->parms->delay_probe_time)) {
755 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
756 neigh->nud_state = NUD_DELAY;
757 neigh_suspect(neigh);
758 next = now + neigh->parms->delay_probe_time;
760 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
761 neigh->nud_state = NUD_STALE;
762 neigh_suspect(neigh);
764 } else if (state & NUD_DELAY) {
765 if (time_before_eq(now,
766 neigh->confirmed + neigh->parms->delay_probe_time)) {
767 NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh);
768 neigh->nud_state = NUD_REACHABLE;
769 neigh_connect(neigh);
770 next = neigh->confirmed + neigh->parms->reachable_time;
772 NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
773 neigh->nud_state = NUD_PROBE;
774 atomic_set(&neigh->probes, 0);
775 next = now + neigh->parms->retrans_time;
778 /* NUD_PROBE|NUD_INCOMPLETE */
779 next = now + neigh->parms->retrans_time;
782 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
783 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
786 neigh->nud_state = NUD_FAILED;
788 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
789 NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
791 /* It is very thin place. report_unreachable is very complicated
792 routine. Particularly, it can hit the same neighbour entry!
794 So that, we try to be accurate and avoid dead loop. --ANK
796 while (neigh->nud_state == NUD_FAILED &&
797 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
798 write_unlock(&neigh->lock);
799 neigh->ops->error_report(neigh, skb);
800 write_lock(&neigh->lock);
802 skb_queue_purge(&neigh->arp_queue);
805 if (neigh->nud_state & NUD_IN_TIMER) {
807 if (time_before(next, jiffies + HZ/2))
808 next = jiffies + HZ/2;
809 neigh->timer.expires = next;
810 add_timer(&neigh->timer);
812 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
813 struct sk_buff *skb = skb_peek(&neigh->arp_queue);
814 /* keep skb alive even if arp_queue overflows */
817 write_unlock(&neigh->lock);
818 neigh->ops->solicit(neigh, skb);
819 atomic_inc(&neigh->probes);
824 write_unlock(&neigh->lock);
828 if (notify && neigh->parms->app_probes)
829 neigh_app_notify(neigh);
831 neigh_release(neigh);
834 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
839 write_lock_bh(&neigh->lock);
842 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
847 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
848 if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
849 atomic_set(&neigh->probes, neigh->parms->ucast_probes);
850 neigh->nud_state = NUD_INCOMPLETE;
852 neigh->timer.expires = now + 1;
853 add_timer(&neigh->timer);
855 neigh->nud_state = NUD_FAILED;
856 write_unlock_bh(&neigh->lock);
862 } else if (neigh->nud_state & NUD_STALE) {
863 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
865 neigh->nud_state = NUD_DELAY;
866 neigh->timer.expires = jiffies + neigh->parms->delay_probe_time;
867 add_timer(&neigh->timer);
870 if (neigh->nud_state == NUD_INCOMPLETE) {
872 if (skb_queue_len(&neigh->arp_queue) >=
873 neigh->parms->queue_len) {
874 struct sk_buff *buff;
875 buff = neigh->arp_queue.next;
876 __skb_unlink(buff, &neigh->arp_queue);
879 __skb_queue_tail(&neigh->arp_queue, skb);
884 write_unlock_bh(&neigh->lock);
888 static __inline__ void neigh_update_hhs(struct neighbour *neigh)
891 void (*update)(struct hh_cache*, struct net_device*, unsigned char *) =
892 neigh->dev->header_cache_update;
895 for (hh = neigh->hh; hh; hh = hh->hh_next) {
896 write_lock_bh(&hh->hh_lock);
897 update(hh, neigh->dev, neigh->ha);
898 write_unlock_bh(&hh->hh_lock);
905 /* Generic update routine.
906 -- lladdr is new lladdr or NULL, if it is not supplied.
909 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
911 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
912 lladdr instead of overriding it
914 It also allows to retain current state
915 if lladdr is unchanged.
916 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
918 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
920 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
923 Caller MUST hold reference count on the entry.
926 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
934 struct net_device *dev;
935 int update_isrouter = 0;
937 write_lock_bh(&neigh->lock);
940 old = neigh->nud_state;
943 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
944 (old & (NUD_NOARP | NUD_PERMANENT)))
947 if (!(new & NUD_VALID)) {
948 neigh_del_timer(neigh);
949 if (old & NUD_CONNECTED)
950 neigh_suspect(neigh);
951 neigh->nud_state = new;
954 notify = old & NUD_VALID;
959 /* Compare new lladdr with cached one */
960 if (!dev->addr_len) {
961 /* First case: device needs no address. */
964 /* The second case: if something is already cached
965 and a new address is proposed:
967 - if they are different, check override flag
969 if ((old & NUD_VALID) &&
970 !memcmp(lladdr, neigh->ha, dev->addr_len))
973 /* No address is supplied; if we know something,
974 use it, otherwise discard the request.
977 if (!(old & NUD_VALID))
982 if (new & NUD_CONNECTED)
983 neigh->confirmed = jiffies;
984 neigh->updated = jiffies;
986 /* If entry was valid and address is not changed,
987 do not change entry state, if new one is STALE.
990 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
991 if (old & NUD_VALID) {
992 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
994 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
995 (old & NUD_CONNECTED)) {
1001 if (lladdr == neigh->ha && new == NUD_STALE &&
1002 ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
1003 (old & NUD_CONNECTED))
1010 neigh_del_timer(neigh);
1011 if (new & NUD_IN_TIMER) {
1013 neigh->timer.expires = jiffies +
1014 ((new & NUD_REACHABLE) ?
1015 neigh->parms->reachable_time : 0);
1016 add_timer(&neigh->timer);
1018 neigh->nud_state = new;
1021 if (lladdr != neigh->ha) {
1022 memcpy(&neigh->ha, lladdr, dev->addr_len);
1023 neigh_update_hhs(neigh);
1024 if (!(new & NUD_CONNECTED))
1025 neigh->confirmed = jiffies -
1026 (neigh->parms->base_reachable_time << 1);
1033 if (new & NUD_CONNECTED)
1034 neigh_connect(neigh);
1036 neigh_suspect(neigh);
1037 if (!(old & NUD_VALID)) {
1038 struct sk_buff *skb;
1040 /* Again: avoid dead loop if something went wrong */
1042 while (neigh->nud_state & NUD_VALID &&
1043 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1044 struct neighbour *n1 = neigh;
1045 write_unlock_bh(&neigh->lock);
1046 /* On shaper/eql skb->dst->neighbour != neigh :( */
1047 if (skb->dst && skb->dst->neighbour)
1048 n1 = skb->dst->neighbour;
1050 write_lock_bh(&neigh->lock);
1052 skb_queue_purge(&neigh->arp_queue);
1055 if (update_isrouter) {
1056 neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
1057 (neigh->flags | NTF_ROUTER) :
1058 (neigh->flags & ~NTF_ROUTER);
1060 write_unlock_bh(&neigh->lock);
1062 if (notify && neigh->parms->app_probes)
1063 neigh_app_notify(neigh);
1068 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1069 u8 *lladdr, void *saddr,
1070 struct net_device *dev)
1072 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1073 lladdr || !dev->addr_len);
1075 neigh_update(neigh, lladdr, NUD_STALE,
1076 NEIGH_UPDATE_F_OVERRIDE);
1080 static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst,
1083 struct hh_cache *hh;
1084 struct net_device *dev = dst->dev;
1086 for (hh = n->hh; hh; hh = hh->hh_next)
1087 if (hh->hh_type == protocol)
1090 if (!hh && (hh = kmalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) {
1091 memset(hh, 0, sizeof(struct hh_cache));
1092 hh->hh_lock = RW_LOCK_UNLOCKED;
1093 hh->hh_type = protocol;
1094 atomic_set(&hh->hh_refcnt, 0);
1096 if (dev->hard_header_cache(n, hh)) {
1100 atomic_inc(&hh->hh_refcnt);
1101 hh->hh_next = n->hh;
1103 if (n->nud_state & NUD_CONNECTED)
1104 hh->hh_output = n->ops->hh_output;
1106 hh->hh_output = n->ops->output;
1110 atomic_inc(&hh->hh_refcnt);
1115 /* This function can be used in contexts, where only old dev_queue_xmit
1116 worked, f.e. if you want to override normal output path (eql, shaper),
1117 but resolution is not made yet.
1120 int neigh_compat_output(struct sk_buff *skb)
1122 struct net_device *dev = skb->dev;
1124 __skb_pull(skb, skb->nh.raw - skb->data);
1126 if (dev->hard_header &&
1127 dev->hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
1129 dev->rebuild_header(skb))
1132 return dev_queue_xmit(skb);
1135 /* Slow and careful. */
1137 int neigh_resolve_output(struct sk_buff *skb)
1139 struct dst_entry *dst = skb->dst;
1140 struct neighbour *neigh;
1143 if (!dst || !(neigh = dst->neighbour))
1146 __skb_pull(skb, skb->nh.raw - skb->data);
1148 if (!neigh_event_send(neigh, skb)) {
1150 struct net_device *dev = neigh->dev;
1151 if (dev->hard_header_cache && !dst->hh) {
1152 write_lock_bh(&neigh->lock);
1154 neigh_hh_init(neigh, dst, dst->ops->protocol);
1155 err = dev->hard_header(skb, dev, ntohs(skb->protocol),
1156 neigh->ha, NULL, skb->len);
1157 write_unlock_bh(&neigh->lock);
1159 read_lock_bh(&neigh->lock);
1160 err = dev->hard_header(skb, dev, ntohs(skb->protocol),
1161 neigh->ha, NULL, skb->len);
1162 read_unlock_bh(&neigh->lock);
1165 rc = neigh->ops->queue_xmit(skb);
1172 NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
1173 dst, dst ? dst->neighbour : NULL);
1180 /* As fast as possible without hh cache */
1182 int neigh_connected_output(struct sk_buff *skb)
1185 struct dst_entry *dst = skb->dst;
1186 struct neighbour *neigh = dst->neighbour;
1187 struct net_device *dev = neigh->dev;
1189 __skb_pull(skb, skb->nh.raw - skb->data);
1191 read_lock_bh(&neigh->lock);
1192 err = dev->hard_header(skb, dev, ntohs(skb->protocol),
1193 neigh->ha, NULL, skb->len);
1194 read_unlock_bh(&neigh->lock);
1196 err = neigh->ops->queue_xmit(skb);
1204 static void neigh_proxy_process(unsigned long arg)
1206 struct neigh_table *tbl = (struct neigh_table *)arg;
1207 long sched_next = 0;
1208 unsigned long now = jiffies;
1209 struct sk_buff *skb;
1211 spin_lock(&tbl->proxy_queue.lock);
1213 skb = tbl->proxy_queue.next;
1215 while (skb != (struct sk_buff *)&tbl->proxy_queue) {
1216 struct sk_buff *back = skb;
1217 long tdif = back->stamp.tv_usec - now;
1221 struct net_device *dev = back->dev;
1222 __skb_unlink(back, &tbl->proxy_queue);
1223 if (tbl->proxy_redo && netif_running(dev))
1224 tbl->proxy_redo(back);
1229 } else if (!sched_next || tdif < sched_next)
1232 del_timer(&tbl->proxy_timer);
1234 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1235 spin_unlock(&tbl->proxy_queue.lock);
1238 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1239 struct sk_buff *skb)
1241 unsigned long now = jiffies;
1242 unsigned long sched_next = now + (net_random() % p->proxy_delay);
1244 if (tbl->proxy_queue.qlen > p->proxy_qlen) {
1248 skb->stamp.tv_sec = LOCALLY_ENQUEUED;
1249 skb->stamp.tv_usec = sched_next;
1251 spin_lock(&tbl->proxy_queue.lock);
1252 if (del_timer(&tbl->proxy_timer)) {
1253 if (time_before(tbl->proxy_timer.expires, sched_next))
1254 sched_next = tbl->proxy_timer.expires;
1256 dst_release(skb->dst);
1259 __skb_queue_tail(&tbl->proxy_queue, skb);
1260 mod_timer(&tbl->proxy_timer, sched_next);
1261 spin_unlock(&tbl->proxy_queue.lock);
1265 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1266 struct neigh_table *tbl)
1268 struct neigh_parms *p = kmalloc(sizeof(*p), GFP_KERNEL);
1271 memcpy(p, &tbl->parms, sizeof(*p));
1273 atomic_set(&p->refcnt, 1);
1274 INIT_RCU_HEAD(&p->rcu_head);
1276 neigh_rand_reach_time(p->base_reachable_time);
1277 if (dev && dev->neigh_setup && dev->neigh_setup(dev, p)) {
1281 p->sysctl_table = NULL;
1282 write_lock_bh(&tbl->lock);
1283 p->next = tbl->parms.next;
1284 tbl->parms.next = p;
1285 write_unlock_bh(&tbl->lock);
1290 static void neigh_rcu_free_parms(struct rcu_head *head)
1292 struct neigh_parms *parms =
1293 container_of(head, struct neigh_parms, rcu_head);
1295 neigh_parms_put(parms);
1298 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1300 struct neigh_parms **p;
1302 if (!parms || parms == &tbl->parms)
1304 write_lock_bh(&tbl->lock);
1305 for (p = &tbl->parms.next; *p; p = &(*p)->next) {
1309 write_unlock_bh(&tbl->lock);
1310 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1314 write_unlock_bh(&tbl->lock);
1315 NEIGH_PRINTK1("neigh_parms_release: not found\n");
1318 void neigh_parms_destroy(struct neigh_parms *parms)
1324 void neigh_table_init(struct neigh_table *tbl)
1326 unsigned long now = jiffies;
1327 unsigned long phsize;
1329 atomic_set(&tbl->parms.refcnt, 1);
1330 INIT_RCU_HEAD(&tbl->parms.rcu_head);
1331 tbl->parms.reachable_time =
1332 neigh_rand_reach_time(tbl->parms.base_reachable_time);
1334 if (!tbl->kmem_cachep)
1335 tbl->kmem_cachep = kmem_cache_create(tbl->id,
1337 0, SLAB_HWCACHE_ALIGN,
1340 if (!tbl->kmem_cachep)
1341 panic("cannot create neighbour cache");
1343 tbl->stats = alloc_percpu(struct neigh_statistics);
1345 panic("cannot create neighbour cache statistics");
1347 #ifdef CONFIG_PROC_FS
1348 tbl->pde = create_proc_entry(tbl->id, 0, proc_net_stat);
1350 panic("cannot create neighbour proc dir entry");
1351 tbl->pde->proc_fops = &neigh_stat_seq_fops;
1352 tbl->pde->data = tbl;
1356 tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1);
1358 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1359 tbl->phash_buckets = kmalloc(phsize, GFP_KERNEL);
1361 if (!tbl->hash_buckets || !tbl->phash_buckets)
1362 panic("cannot allocate neighbour cache hashes");
1364 memset(tbl->phash_buckets, 0, phsize);
1366 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1368 tbl->lock = RW_LOCK_UNLOCKED;
1369 init_timer(&tbl->gc_timer);
1370 tbl->gc_timer.data = (unsigned long)tbl;
1371 tbl->gc_timer.function = neigh_periodic_timer;
1372 tbl->gc_timer.expires = now + 1;
1373 add_timer(&tbl->gc_timer);
1375 init_timer(&tbl->proxy_timer);
1376 tbl->proxy_timer.data = (unsigned long)tbl;
1377 tbl->proxy_timer.function = neigh_proxy_process;
1378 skb_queue_head_init(&tbl->proxy_queue);
1380 tbl->last_flush = now;
1381 tbl->last_rand = now + tbl->parms.reachable_time * 20;
1382 write_lock(&neigh_tbl_lock);
1383 tbl->next = neigh_tables;
1385 write_unlock(&neigh_tbl_lock);
1388 int neigh_table_clear(struct neigh_table *tbl)
1390 struct neigh_table **tp;
1392 /* It is not clean... Fix it to unload IPv6 module safely */
1393 del_timer_sync(&tbl->gc_timer);
1394 del_timer_sync(&tbl->proxy_timer);
1395 pneigh_queue_purge(&tbl->proxy_queue);
1396 neigh_ifdown(tbl, NULL);
1398 printk(KERN_CRIT "neighbour leakage\n");
1399 write_lock(&neigh_tbl_lock);
1400 for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
1406 write_unlock(&neigh_tbl_lock);
1408 neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1);
1409 tbl->hash_buckets = NULL;
1411 kfree(tbl->phash_buckets);
1412 tbl->phash_buckets = NULL;
1417 int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1419 struct ndmsg *ndm = NLMSG_DATA(nlh);
1420 struct rtattr **nda = arg;
1421 struct neigh_table *tbl;
1422 struct net_device *dev = NULL;
1425 if (ndm->ndm_ifindex &&
1426 (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1429 read_lock(&neigh_tbl_lock);
1430 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1431 struct neighbour *n;
1433 if (tbl->family != ndm->ndm_family)
1435 read_unlock(&neigh_tbl_lock);
1438 if (!nda[NDA_DST - 1] ||
1439 nda[NDA_DST - 1]->rta_len != RTA_LENGTH(tbl->key_len))
1442 if (ndm->ndm_flags & NTF_PROXY) {
1443 err = pneigh_delete(tbl,
1444 RTA_DATA(nda[NDA_DST - 1]), dev);
1451 n = neigh_lookup(tbl, RTA_DATA(nda[NDA_DST - 1]), dev);
1453 err = neigh_update(n, NULL, NUD_FAILED,
1454 NEIGH_UPDATE_F_OVERRIDE|
1455 NEIGH_UPDATE_F_ADMIN);
1460 read_unlock(&neigh_tbl_lock);
1461 err = -EADDRNOTAVAIL;
1469 int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1471 struct ndmsg *ndm = NLMSG_DATA(nlh);
1472 struct rtattr **nda = arg;
1473 struct neigh_table *tbl;
1474 struct net_device *dev = NULL;
1477 if (ndm->ndm_ifindex &&
1478 (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1481 read_lock(&neigh_tbl_lock);
1482 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1484 struct neighbour *n;
1486 if (tbl->family != ndm->ndm_family)
1488 read_unlock(&neigh_tbl_lock);
1491 if (!nda[NDA_DST - 1] ||
1492 nda[NDA_DST - 1]->rta_len != RTA_LENGTH(tbl->key_len))
1494 if (ndm->ndm_flags & NTF_PROXY) {
1496 if (pneigh_lookup(tbl,
1497 RTA_DATA(nda[NDA_DST - 1]), dev, 1))
1504 if (nda[NDA_LLADDR - 1] &&
1505 nda[NDA_LLADDR - 1]->rta_len != RTA_LENGTH(dev->addr_len))
1508 n = neigh_lookup(tbl, RTA_DATA(nda[NDA_DST - 1]), dev);
1510 if (nlh->nlmsg_flags & NLM_F_EXCL)
1512 override = nlh->nlmsg_flags & NLM_F_REPLACE;
1513 } else if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1516 n = __neigh_lookup_errno(tbl, RTA_DATA(nda[NDA_DST - 1]),
1524 err = neigh_update(n, nda[NDA_LLADDR - 1] ?
1525 RTA_DATA(nda[NDA_LLADDR - 1]) :
1528 (override ? NEIGH_UPDATE_F_OVERRIDE : 0) |
1529 NEIGH_UPDATE_F_ADMIN);
1536 read_unlock(&neigh_tbl_lock);
1537 err = -EADDRNOTAVAIL;
1546 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *n,
1547 u32 pid, u32 seq, int event)
1549 unsigned long now = jiffies;
1550 unsigned char *b = skb->tail;
1551 struct nda_cacheinfo ci;
1553 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq, event,
1554 sizeof(struct ndmsg));
1555 struct ndmsg *ndm = NLMSG_DATA(nlh);
1557 ndm->ndm_family = n->ops->family;
1558 ndm->ndm_flags = n->flags;
1559 ndm->ndm_type = n->type;
1560 ndm->ndm_ifindex = n->dev->ifindex;
1561 RTA_PUT(skb, NDA_DST, n->tbl->key_len, n->primary_key);
1562 read_lock_bh(&n->lock);
1564 ndm->ndm_state = n->nud_state;
1565 if (n->nud_state & NUD_VALID)
1566 RTA_PUT(skb, NDA_LLADDR, n->dev->addr_len, n->ha);
1567 ci.ndm_used = now - n->used;
1568 ci.ndm_confirmed = now - n->confirmed;
1569 ci.ndm_updated = now - n->updated;
1570 ci.ndm_refcnt = atomic_read(&n->refcnt) - 1;
1571 read_unlock_bh(&n->lock);
1573 RTA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
1574 nlh->nlmsg_len = skb->tail - b;
1580 read_unlock_bh(&n->lock);
1581 skb_trim(skb, b - skb->data);
1586 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
1587 struct netlink_callback *cb)
1589 struct neighbour *n;
1590 int rc, h, s_h = cb->args[1];
1591 int idx, s_idx = idx = cb->args[2];
1593 for (h = 0; h <= tbl->hash_mask; h++) {
1598 read_lock_bh(&tbl->lock);
1599 for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next, idx++) {
1602 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
1604 RTM_NEWNEIGH) <= 0) {
1605 read_unlock_bh(&tbl->lock);
1610 read_unlock_bh(&tbl->lock);
1619 int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1621 struct neigh_table *tbl;
1624 read_lock(&neigh_tbl_lock);
1625 family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
1628 for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) {
1629 if (t < s_t || (family && tbl->family != family))
1632 memset(&cb->args[1], 0, sizeof(cb->args) -
1633 sizeof(cb->args[0]));
1634 if (neigh_dump_table(tbl, skb, cb) < 0)
1637 read_unlock(&neigh_tbl_lock);
1643 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
1647 read_lock_bh(&tbl->lock);
1648 for (chain = 0; chain <= tbl->hash_mask; chain++) {
1649 struct neighbour *n;
1651 for (n = tbl->hash_buckets[chain]; n; n = n->next)
1654 read_unlock_bh(&tbl->lock);
1656 EXPORT_SYMBOL(neigh_for_each);
1658 /* The tbl->lock must be held as a writer and BH disabled. */
1659 void __neigh_for_each_release(struct neigh_table *tbl,
1660 int (*cb)(struct neighbour *))
1664 for (chain = 0; chain <= tbl->hash_mask; chain++) {
1665 struct neighbour *n, **np;
1667 np = &tbl->hash_buckets[chain];
1668 while ((n = *np) != NULL) {
1671 write_lock(&n->lock);
1678 write_unlock(&n->lock);
1684 EXPORT_SYMBOL(__neigh_for_each_release);
1686 #ifdef CONFIG_PROC_FS
1688 static struct neighbour *neigh_get_first(struct seq_file *seq)
1690 struct neigh_seq_state *state = seq->private;
1691 struct neigh_table *tbl = state->tbl;
1692 struct neighbour *n = NULL;
1693 int bucket = state->bucket;
1695 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
1696 for (bucket = 0; bucket <= tbl->hash_mask; bucket++) {
1697 n = tbl->hash_buckets[bucket];
1700 if (state->neigh_sub_iter) {
1704 v = state->neigh_sub_iter(state, n, &fakep);
1708 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
1710 if (n->nud_state & ~NUD_NOARP)
1719 state->bucket = bucket;
1724 static struct neighbour *neigh_get_next(struct seq_file *seq,
1725 struct neighbour *n,
1728 struct neigh_seq_state *state = seq->private;
1729 struct neigh_table *tbl = state->tbl;
1731 if (state->neigh_sub_iter) {
1732 void *v = state->neigh_sub_iter(state, n, pos);
1740 if (state->neigh_sub_iter) {
1741 void *v = state->neigh_sub_iter(state, n, pos);
1746 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
1749 if (n->nud_state & ~NUD_NOARP)
1758 if (++state->bucket > tbl->hash_mask)
1761 n = tbl->hash_buckets[state->bucket];
1769 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
1771 struct neighbour *n = neigh_get_first(seq);
1775 n = neigh_get_next(seq, n, pos);
1780 return *pos ? NULL : n;
1783 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
1785 struct neigh_seq_state *state = seq->private;
1786 struct neigh_table *tbl = state->tbl;
1787 struct pneigh_entry *pn = NULL;
1788 int bucket = state->bucket;
1790 state->flags |= NEIGH_SEQ_IS_PNEIGH;
1791 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
1792 pn = tbl->phash_buckets[bucket];
1796 state->bucket = bucket;
1801 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
1802 struct pneigh_entry *pn,
1805 struct neigh_seq_state *state = seq->private;
1806 struct neigh_table *tbl = state->tbl;
1810 if (++state->bucket > PNEIGH_HASHMASK)
1812 pn = tbl->phash_buckets[state->bucket];
1823 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
1825 struct pneigh_entry *pn = pneigh_get_first(seq);
1829 pn = pneigh_get_next(seq, pn, pos);
1834 return *pos ? NULL : pn;
1837 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
1839 struct neigh_seq_state *state = seq->private;
1842 rc = neigh_get_idx(seq, pos);
1843 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
1844 rc = pneigh_get_idx(seq, pos);
1849 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
1851 struct neigh_seq_state *state = seq->private;
1852 loff_t pos_minus_one;
1856 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
1858 read_lock_bh(&tbl->lock);
1860 pos_minus_one = *pos - 1;
1861 return *pos ? neigh_get_idx_any(seq, &pos_minus_one) : SEQ_START_TOKEN;
1863 EXPORT_SYMBOL(neigh_seq_start);
1865 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1867 struct neigh_seq_state *state;
1870 if (v == SEQ_START_TOKEN) {
1871 rc = neigh_get_idx(seq, pos);
1875 state = seq->private;
1876 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
1877 rc = neigh_get_next(seq, v, NULL);
1880 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
1881 rc = pneigh_get_first(seq);
1883 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
1884 rc = pneigh_get_next(seq, v, NULL);
1890 EXPORT_SYMBOL(neigh_seq_next);
1892 void neigh_seq_stop(struct seq_file *seq, void *v)
1894 struct neigh_seq_state *state = seq->private;
1895 struct neigh_table *tbl = state->tbl;
1897 read_unlock_bh(&tbl->lock);
1899 EXPORT_SYMBOL(neigh_seq_stop);
1901 /* statistics via seq_file */
1903 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
1905 struct proc_dir_entry *pde = seq->private;
1906 struct neigh_table *tbl = pde->data;
1910 return SEQ_START_TOKEN;
1912 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
1913 if (!cpu_possible(cpu))
1916 return per_cpu_ptr(tbl->stats, cpu);
1921 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1923 struct proc_dir_entry *pde = seq->private;
1924 struct neigh_table *tbl = pde->data;
1927 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
1928 if (!cpu_possible(cpu))
1931 return per_cpu_ptr(tbl->stats, cpu);
1936 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
1941 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
1943 struct proc_dir_entry *pde = seq->private;
1944 struct neigh_table *tbl = pde->data;
1945 struct neigh_statistics *st = v;
1947 if (v == SEQ_START_TOKEN) {
1948 seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs forced_gc_goal_miss\n");
1952 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
1953 "%08lx %08lx %08lx %08lx\n",
1965 st->rcv_probes_mcast,
1966 st->rcv_probes_ucast,
1968 st->periodic_gc_runs,
1975 static struct seq_operations neigh_stat_seq_ops = {
1976 .start = neigh_stat_seq_start,
1977 .next = neigh_stat_seq_next,
1978 .stop = neigh_stat_seq_stop,
1979 .show = neigh_stat_seq_show,
1982 static int neigh_stat_seq_open(struct inode *inode, struct file *file)
1984 int ret = seq_open(file, &neigh_stat_seq_ops);
1987 struct seq_file *sf = file->private_data;
1988 sf->private = PDE(inode);
1993 static struct file_operations neigh_stat_seq_fops = {
1994 .owner = THIS_MODULE,
1995 .open = neigh_stat_seq_open,
1997 .llseek = seq_lseek,
1998 .release = seq_release,
2001 #endif /* CONFIG_PROC_FS */
2004 void neigh_app_ns(struct neighbour *n)
2006 struct nlmsghdr *nlh;
2007 int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
2008 struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
2013 if (neigh_fill_info(skb, n, 0, 0, RTM_GETNEIGH) < 0) {
2017 nlh = (struct nlmsghdr *)skb->data;
2018 nlh->nlmsg_flags = NLM_F_REQUEST;
2019 NETLINK_CB(skb).dst_groups = RTMGRP_NEIGH;
2020 netlink_broadcast(rtnl, skb, 0, RTMGRP_NEIGH, GFP_ATOMIC);
2023 static void neigh_app_notify(struct neighbour *n)
2025 struct nlmsghdr *nlh;
2026 int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
2027 struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
2032 if (neigh_fill_info(skb, n, 0, 0, RTM_NEWNEIGH) < 0) {
2036 nlh = (struct nlmsghdr *)skb->data;
2037 NETLINK_CB(skb).dst_groups = RTMGRP_NEIGH;
2038 netlink_broadcast(rtnl, skb, 0, RTMGRP_NEIGH, GFP_ATOMIC);
2041 #endif /* CONFIG_ARPD */
2043 #ifdef CONFIG_SYSCTL
2045 static struct neigh_sysctl_table {
2046 struct ctl_table_header *sysctl_header;
2047 ctl_table neigh_vars[17];
2048 ctl_table neigh_dev[2];
2049 ctl_table neigh_neigh_dir[2];
2050 ctl_table neigh_proto_dir[2];
2051 ctl_table neigh_root_dir[2];
2052 } neigh_sysctl_template = {
2055 .ctl_name = NET_NEIGH_MCAST_SOLICIT,
2056 .procname = "mcast_solicit",
2057 .maxlen = sizeof(int),
2059 .proc_handler = &proc_dointvec,
2062 .ctl_name = NET_NEIGH_UCAST_SOLICIT,
2063 .procname = "ucast_solicit",
2064 .maxlen = sizeof(int),
2066 .proc_handler = &proc_dointvec,
2069 .ctl_name = NET_NEIGH_APP_SOLICIT,
2070 .procname = "app_solicit",
2071 .maxlen = sizeof(int),
2073 .proc_handler = &proc_dointvec,
2076 .ctl_name = NET_NEIGH_RETRANS_TIME,
2077 .procname = "retrans_time",
2078 .maxlen = sizeof(int),
2080 .proc_handler = &proc_dointvec_userhz_jiffies,
2083 .ctl_name = NET_NEIGH_REACHABLE_TIME,
2084 .procname = "base_reachable_time",
2085 .maxlen = sizeof(int),
2087 .proc_handler = &proc_dointvec_jiffies,
2088 .strategy = &sysctl_jiffies,
2091 .ctl_name = NET_NEIGH_DELAY_PROBE_TIME,
2092 .procname = "delay_first_probe_time",
2093 .maxlen = sizeof(int),
2095 .proc_handler = &proc_dointvec_jiffies,
2096 .strategy = &sysctl_jiffies,
2099 .ctl_name = NET_NEIGH_GC_STALE_TIME,
2100 .procname = "gc_stale_time",
2101 .maxlen = sizeof(int),
2103 .proc_handler = &proc_dointvec_jiffies,
2104 .strategy = &sysctl_jiffies,
2107 .ctl_name = NET_NEIGH_UNRES_QLEN,
2108 .procname = "unres_qlen",
2109 .maxlen = sizeof(int),
2111 .proc_handler = &proc_dointvec,
2114 .ctl_name = NET_NEIGH_PROXY_QLEN,
2115 .procname = "proxy_qlen",
2116 .maxlen = sizeof(int),
2118 .proc_handler = &proc_dointvec,
2121 .ctl_name = NET_NEIGH_ANYCAST_DELAY,
2122 .procname = "anycast_delay",
2123 .maxlen = sizeof(int),
2125 .proc_handler = &proc_dointvec_userhz_jiffies,
2128 .ctl_name = NET_NEIGH_PROXY_DELAY,
2129 .procname = "proxy_delay",
2130 .maxlen = sizeof(int),
2132 .proc_handler = &proc_dointvec_userhz_jiffies,
2135 .ctl_name = NET_NEIGH_LOCKTIME,
2136 .procname = "locktime",
2137 .maxlen = sizeof(int),
2139 .proc_handler = &proc_dointvec_userhz_jiffies,
2142 .ctl_name = NET_NEIGH_GC_INTERVAL,
2143 .procname = "gc_interval",
2144 .maxlen = sizeof(int),
2146 .proc_handler = &proc_dointvec_jiffies,
2147 .strategy = &sysctl_jiffies,
2150 .ctl_name = NET_NEIGH_GC_THRESH1,
2151 .procname = "gc_thresh1",
2152 .maxlen = sizeof(int),
2154 .proc_handler = &proc_dointvec,
2157 .ctl_name = NET_NEIGH_GC_THRESH2,
2158 .procname = "gc_thresh2",
2159 .maxlen = sizeof(int),
2161 .proc_handler = &proc_dointvec,
2164 .ctl_name = NET_NEIGH_GC_THRESH3,
2165 .procname = "gc_thresh3",
2166 .maxlen = sizeof(int),
2168 .proc_handler = &proc_dointvec,
2173 .ctl_name = NET_PROTO_CONF_DEFAULT,
2174 .procname = "default",
2178 .neigh_neigh_dir = {
2180 .procname = "neigh",
2184 .neigh_proto_dir = {
2191 .ctl_name = CTL_NET,
2198 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
2199 int p_id, int pdev_id, char *p_name,
2200 proc_handler *handler)
2202 struct neigh_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
2203 const char *dev_name_source = NULL;
2204 char *dev_name = NULL;
2209 memcpy(t, &neigh_sysctl_template, sizeof(*t));
2210 t->neigh_vars[0].data = &p->mcast_probes;
2211 t->neigh_vars[1].data = &p->ucast_probes;
2212 t->neigh_vars[2].data = &p->app_probes;
2213 t->neigh_vars[3].data = &p->retrans_time;
2215 t->neigh_vars[3].proc_handler = handler;
2216 t->neigh_vars[3].extra1 = dev;
2218 t->neigh_vars[4].data = &p->base_reachable_time;
2219 t->neigh_vars[5].data = &p->delay_probe_time;
2220 t->neigh_vars[6].data = &p->gc_staletime;
2221 t->neigh_vars[7].data = &p->queue_len;
2222 t->neigh_vars[8].data = &p->proxy_qlen;
2223 t->neigh_vars[9].data = &p->anycast_delay;
2224 t->neigh_vars[10].data = &p->proxy_delay;
2225 t->neigh_vars[11].data = &p->locktime;
2227 dev_name_source = t->neigh_dev[0].procname;
2229 dev_name_source = dev->name;
2230 t->neigh_dev[0].ctl_name = dev->ifindex;
2231 memset(&t->neigh_vars[12], 0, sizeof(ctl_table));
2233 t->neigh_vars[12].data = (int *)(p + 1);
2234 t->neigh_vars[13].data = (int *)(p + 1) + 1;
2235 t->neigh_vars[14].data = (int *)(p + 1) + 2;
2236 t->neigh_vars[15].data = (int *)(p + 1) + 3;
2239 dev_name = net_sysctl_strdup(dev_name_source);
2245 t->neigh_dev[0].procname = dev_name;
2247 t->neigh_neigh_dir[0].ctl_name = pdev_id;
2249 t->neigh_proto_dir[0].procname = p_name;
2250 t->neigh_proto_dir[0].ctl_name = p_id;
2252 t->neigh_dev[0].child = t->neigh_vars;
2253 t->neigh_neigh_dir[0].child = t->neigh_dev;
2254 t->neigh_proto_dir[0].child = t->neigh_neigh_dir;
2255 t->neigh_root_dir[0].child = t->neigh_proto_dir;
2257 t->sysctl_header = register_sysctl_table(t->neigh_root_dir, 0);
2258 if (!t->sysctl_header) {
2262 p->sysctl_table = t;
2274 void neigh_sysctl_unregister(struct neigh_parms *p)
2276 if (p->sysctl_table) {
2277 struct neigh_sysctl_table *t = p->sysctl_table;
2278 p->sysctl_table = NULL;
2279 unregister_sysctl_table(t->sysctl_header);
2280 kfree(t->neigh_dev[0].procname);
2285 #endif /* CONFIG_SYSCTL */
2287 EXPORT_SYMBOL(__neigh_event_send);
2288 EXPORT_SYMBOL(neigh_add);
2289 EXPORT_SYMBOL(neigh_changeaddr);
2290 EXPORT_SYMBOL(neigh_compat_output);
2291 EXPORT_SYMBOL(neigh_connected_output);
2292 EXPORT_SYMBOL(neigh_create);
2293 EXPORT_SYMBOL(neigh_delete);
2294 EXPORT_SYMBOL(neigh_destroy);
2295 EXPORT_SYMBOL(neigh_dump_info);
2296 EXPORT_SYMBOL(neigh_event_ns);
2297 EXPORT_SYMBOL(neigh_ifdown);
2298 EXPORT_SYMBOL(neigh_lookup);
2299 EXPORT_SYMBOL(neigh_lookup_nodev);
2300 EXPORT_SYMBOL(neigh_parms_alloc);
2301 EXPORT_SYMBOL(neigh_parms_release);
2302 EXPORT_SYMBOL(neigh_rand_reach_time);
2303 EXPORT_SYMBOL(neigh_resolve_output);
2304 EXPORT_SYMBOL(neigh_table_clear);
2305 EXPORT_SYMBOL(neigh_table_init);
2306 EXPORT_SYMBOL(neigh_update);
2307 EXPORT_SYMBOL(neigh_update_hhs);
2308 EXPORT_SYMBOL(pneigh_enqueue);
2309 EXPORT_SYMBOL(pneigh_lookup);
2312 EXPORT_SYMBOL(neigh_app_ns);
2314 #ifdef CONFIG_SYSCTL
2315 EXPORT_SYMBOL(neigh_sysctl_register);
2316 EXPORT_SYMBOL(neigh_sysctl_unregister);