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 struct neigh_table
*neigh_tables
;
63 static struct file_operations neigh_stat_seq_fops
;
66 Neighbour hash table buckets are protected with rwlock tbl->lock.
68 - All the scans/updates to hash buckets MUST be made under this lock.
69 - NOTHING clever should be made under this lock: no callbacks
70 to protocol backends, no attempts to send something to network.
71 It will result in deadlocks, if backend/driver wants to use neighbour
73 - If the entry requires some non-trivial actions, increase
74 its reference count and release table lock.
76 Neighbour entries are protected:
77 - with reference count.
78 - with rwlock neigh->lock
80 Reference count prevents destruction.
82 neigh->lock mainly serializes ll address data and its validity state.
83 However, the same lock is used to protect another entry fields:
87 Again, nothing clever shall be made under neigh->lock,
88 the most complicated procedure, which we allow is dev->hard_header.
89 It is supposed, that dev->hard_header is simplistic and does
90 not make callbacks to neighbour tables.
92 The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
93 list of neighbour tables. This list is used only in process context,
96 static DEFINE_RWLOCK(neigh_tbl_lock
);
98 static int neigh_blackhole(struct sk_buff
*skb
)
105 * It is random distribution in the interval (1/2)*base...(3/2)*base.
106 * It corresponds to default IPv6 settings and is not overridable,
107 * because it is really reasonable choice.
110 unsigned long neigh_rand_reach_time(unsigned long base
)
112 return (base
? (net_random() % base
) + (base
>> 1) : 0);
116 static int neigh_forced_gc(struct neigh_table
*tbl
)
121 NEIGH_CACHE_STAT_INC(tbl
, forced_gc_runs
);
123 write_lock_bh(&tbl
->lock
);
124 for (i
= 0; i
<= tbl
->hash_mask
; i
++) {
125 struct neighbour
*n
, **np
;
127 np
= &tbl
->hash_buckets
[i
];
128 while ((n
= *np
) != NULL
) {
129 /* Neighbour record may be discarded if:
130 * - nobody refers to it.
131 * - it is not permanent
133 write_lock(&n
->lock
);
134 if (atomic_read(&n
->refcnt
) == 1 &&
135 !(n
->nud_state
& NUD_PERMANENT
)) {
139 write_unlock(&n
->lock
);
143 write_unlock(&n
->lock
);
148 tbl
->last_flush
= jiffies
;
150 write_unlock_bh(&tbl
->lock
);
155 static int neigh_del_timer(struct neighbour
*n
)
157 if ((n
->nud_state
& NUD_IN_TIMER
) &&
158 del_timer(&n
->timer
)) {
165 static void pneigh_queue_purge(struct sk_buff_head
*list
)
169 while ((skb
= skb_dequeue(list
)) != NULL
) {
175 void neigh_changeaddr(struct neigh_table
*tbl
, struct net_device
*dev
)
179 write_lock_bh(&tbl
->lock
);
181 for (i
=0; i
<= tbl
->hash_mask
; i
++) {
182 struct neighbour
*n
, **np
;
184 np
= &tbl
->hash_buckets
[i
];
185 while ((n
= *np
) != NULL
) {
186 if (dev
&& n
->dev
!= dev
) {
191 write_lock_bh(&n
->lock
);
194 write_unlock_bh(&n
->lock
);
199 write_unlock_bh(&tbl
->lock
);
202 int neigh_ifdown(struct neigh_table
*tbl
, struct net_device
*dev
)
206 write_lock_bh(&tbl
->lock
);
208 for (i
= 0; i
<= tbl
->hash_mask
; i
++) {
209 struct neighbour
*n
, **np
= &tbl
->hash_buckets
[i
];
211 while ((n
= *np
) != NULL
) {
212 if (dev
&& n
->dev
!= dev
) {
217 write_lock(&n
->lock
);
221 if (atomic_read(&n
->refcnt
) != 1) {
222 /* The most unpleasant situation.
223 We must destroy neighbour entry,
224 but someone still uses it.
226 The destroy will be delayed until
227 the last user releases us, but
228 we must kill timers etc. and move
231 skb_queue_purge(&n
->arp_queue
);
232 n
->output
= neigh_blackhole
;
233 if (n
->nud_state
& NUD_VALID
)
234 n
->nud_state
= NUD_NOARP
;
236 n
->nud_state
= NUD_NONE
;
237 NEIGH_PRINTK2("neigh %p is stray.\n", n
);
239 write_unlock(&n
->lock
);
244 pneigh_ifdown(tbl
, dev
);
245 write_unlock_bh(&tbl
->lock
);
247 del_timer_sync(&tbl
->proxy_timer
);
248 pneigh_queue_purge(&tbl
->proxy_queue
);
252 static struct neighbour
*neigh_alloc(struct neigh_table
*tbl
)
254 struct neighbour
*n
= NULL
;
255 unsigned long now
= jiffies
;
258 entries
= atomic_inc_return(&tbl
->entries
) - 1;
259 if (entries
>= tbl
->gc_thresh3
||
260 (entries
>= tbl
->gc_thresh2
&&
261 time_after(now
, tbl
->last_flush
+ 5 * HZ
))) {
262 if (!neigh_forced_gc(tbl
) &&
263 entries
>= tbl
->gc_thresh3
)
267 n
= kmem_cache_alloc(tbl
->kmem_cachep
, SLAB_ATOMIC
);
271 memset(n
, 0, tbl
->entry_size
);
273 skb_queue_head_init(&n
->arp_queue
);
274 rwlock_init(&n
->lock
);
275 n
->updated
= n
->used
= now
;
276 n
->nud_state
= NUD_NONE
;
277 n
->output
= neigh_blackhole
;
278 n
->parms
= neigh_parms_clone(&tbl
->parms
);
279 init_timer(&n
->timer
);
280 n
->timer
.function
= neigh_timer_handler
;
281 n
->timer
.data
= (unsigned long)n
;
283 NEIGH_CACHE_STAT_INC(tbl
, allocs
);
285 atomic_set(&n
->refcnt
, 1);
291 atomic_dec(&tbl
->entries
);
295 static struct neighbour
**neigh_hash_alloc(unsigned int entries
)
297 unsigned long size
= entries
* sizeof(struct neighbour
*);
298 struct neighbour
**ret
;
300 if (size
<= PAGE_SIZE
) {
301 ret
= kmalloc(size
, GFP_ATOMIC
);
303 ret
= (struct neighbour
**)
304 __get_free_pages(GFP_ATOMIC
, get_order(size
));
307 memset(ret
, 0, size
);
312 static void neigh_hash_free(struct neighbour
**hash
, unsigned int entries
)
314 unsigned long size
= entries
* sizeof(struct neighbour
*);
316 if (size
<= PAGE_SIZE
)
319 free_pages((unsigned long)hash
, get_order(size
));
322 static void neigh_hash_grow(struct neigh_table
*tbl
, unsigned long new_entries
)
324 struct neighbour
**new_hash
, **old_hash
;
325 unsigned int i
, new_hash_mask
, old_entries
;
327 NEIGH_CACHE_STAT_INC(tbl
, hash_grows
);
329 BUG_ON(new_entries
& (new_entries
- 1));
330 new_hash
= neigh_hash_alloc(new_entries
);
334 old_entries
= tbl
->hash_mask
+ 1;
335 new_hash_mask
= new_entries
- 1;
336 old_hash
= tbl
->hash_buckets
;
338 get_random_bytes(&tbl
->hash_rnd
, sizeof(tbl
->hash_rnd
));
339 for (i
= 0; i
< old_entries
; i
++) {
340 struct neighbour
*n
, *next
;
342 for (n
= old_hash
[i
]; n
; n
= next
) {
343 unsigned int hash_val
= tbl
->hash(n
->primary_key
, n
->dev
);
345 hash_val
&= new_hash_mask
;
348 n
->next
= new_hash
[hash_val
];
349 new_hash
[hash_val
] = n
;
352 tbl
->hash_buckets
= new_hash
;
353 tbl
->hash_mask
= new_hash_mask
;
355 neigh_hash_free(old_hash
, old_entries
);
358 struct neighbour
*neigh_lookup(struct neigh_table
*tbl
, const void *pkey
,
359 struct net_device
*dev
)
362 int key_len
= tbl
->key_len
;
363 u32 hash_val
= tbl
->hash(pkey
, dev
) & tbl
->hash_mask
;
365 NEIGH_CACHE_STAT_INC(tbl
, lookups
);
367 read_lock_bh(&tbl
->lock
);
368 for (n
= tbl
->hash_buckets
[hash_val
]; n
; n
= n
->next
) {
369 if (dev
== n
->dev
&& !memcmp(n
->primary_key
, pkey
, key_len
)) {
371 NEIGH_CACHE_STAT_INC(tbl
, hits
);
375 read_unlock_bh(&tbl
->lock
);
379 struct neighbour
*neigh_lookup_nodev(struct neigh_table
*tbl
, const void *pkey
)
382 int key_len
= tbl
->key_len
;
383 u32 hash_val
= tbl
->hash(pkey
, NULL
) & tbl
->hash_mask
;
385 NEIGH_CACHE_STAT_INC(tbl
, lookups
);
387 read_lock_bh(&tbl
->lock
);
388 for (n
= tbl
->hash_buckets
[hash_val
]; n
; n
= n
->next
) {
389 if (!memcmp(n
->primary_key
, pkey
, key_len
)) {
391 NEIGH_CACHE_STAT_INC(tbl
, hits
);
395 read_unlock_bh(&tbl
->lock
);
399 struct neighbour
*neigh_create(struct neigh_table
*tbl
, const void *pkey
,
400 struct net_device
*dev
)
403 int key_len
= tbl
->key_len
;
405 struct neighbour
*n1
, *rc
, *n
= neigh_alloc(tbl
);
408 rc
= ERR_PTR(-ENOBUFS
);
412 memcpy(n
->primary_key
, pkey
, key_len
);
416 /* Protocol specific setup. */
417 if (tbl
->constructor
&& (error
= tbl
->constructor(n
)) < 0) {
419 goto out_neigh_release
;
422 /* Device specific setup. */
423 if (n
->parms
->neigh_setup
&&
424 (error
= n
->parms
->neigh_setup(n
)) < 0) {
426 goto out_neigh_release
;
429 n
->confirmed
= jiffies
- (n
->parms
->base_reachable_time
<< 1);
431 write_lock_bh(&tbl
->lock
);
433 if (atomic_read(&tbl
->entries
) > (tbl
->hash_mask
+ 1))
434 neigh_hash_grow(tbl
, (tbl
->hash_mask
+ 1) << 1);
436 hash_val
= tbl
->hash(pkey
, dev
) & tbl
->hash_mask
;
438 if (n
->parms
->dead
) {
439 rc
= ERR_PTR(-EINVAL
);
443 for (n1
= tbl
->hash_buckets
[hash_val
]; n1
; n1
= n1
->next
) {
444 if (dev
== n1
->dev
&& !memcmp(n1
->primary_key
, pkey
, key_len
)) {
451 n
->next
= tbl
->hash_buckets
[hash_val
];
452 tbl
->hash_buckets
[hash_val
] = n
;
455 write_unlock_bh(&tbl
->lock
);
456 NEIGH_PRINTK2("neigh %p is created.\n", n
);
461 write_unlock_bh(&tbl
->lock
);
467 struct pneigh_entry
* pneigh_lookup(struct neigh_table
*tbl
, const void *pkey
,
468 struct net_device
*dev
, int creat
)
470 struct pneigh_entry
*n
;
471 int key_len
= tbl
->key_len
;
472 u32 hash_val
= *(u32
*)(pkey
+ key_len
- 4);
474 hash_val
^= (hash_val
>> 16);
475 hash_val
^= hash_val
>> 8;
476 hash_val
^= hash_val
>> 4;
477 hash_val
&= PNEIGH_HASHMASK
;
479 read_lock_bh(&tbl
->lock
);
481 for (n
= tbl
->phash_buckets
[hash_val
]; n
; n
= n
->next
) {
482 if (!memcmp(n
->key
, pkey
, key_len
) &&
483 (n
->dev
== dev
|| !n
->dev
)) {
484 read_unlock_bh(&tbl
->lock
);
488 read_unlock_bh(&tbl
->lock
);
493 n
= kmalloc(sizeof(*n
) + key_len
, GFP_KERNEL
);
497 memcpy(n
->key
, pkey
, key_len
);
502 if (tbl
->pconstructor
&& tbl
->pconstructor(n
)) {
510 write_lock_bh(&tbl
->lock
);
511 n
->next
= tbl
->phash_buckets
[hash_val
];
512 tbl
->phash_buckets
[hash_val
] = n
;
513 write_unlock_bh(&tbl
->lock
);
519 int pneigh_delete(struct neigh_table
*tbl
, const void *pkey
,
520 struct net_device
*dev
)
522 struct pneigh_entry
*n
, **np
;
523 int key_len
= tbl
->key_len
;
524 u32 hash_val
= *(u32
*)(pkey
+ key_len
- 4);
526 hash_val
^= (hash_val
>> 16);
527 hash_val
^= hash_val
>> 8;
528 hash_val
^= hash_val
>> 4;
529 hash_val
&= PNEIGH_HASHMASK
;
531 write_lock_bh(&tbl
->lock
);
532 for (np
= &tbl
->phash_buckets
[hash_val
]; (n
= *np
) != NULL
;
534 if (!memcmp(n
->key
, pkey
, key_len
) && n
->dev
== dev
) {
536 write_unlock_bh(&tbl
->lock
);
537 if (tbl
->pdestructor
)
545 write_unlock_bh(&tbl
->lock
);
549 static int pneigh_ifdown(struct neigh_table
*tbl
, struct net_device
*dev
)
551 struct pneigh_entry
*n
, **np
;
554 for (h
= 0; h
<= PNEIGH_HASHMASK
; h
++) {
555 np
= &tbl
->phash_buckets
[h
];
556 while ((n
= *np
) != NULL
) {
557 if (!dev
|| n
->dev
== dev
) {
559 if (tbl
->pdestructor
)
574 * neighbour must already be out of the table;
577 void neigh_destroy(struct neighbour
*neigh
)
581 NEIGH_CACHE_STAT_INC(neigh
->tbl
, destroys
);
585 "Destroying alive neighbour %p\n", neigh
);
590 if (neigh_del_timer(neigh
))
591 printk(KERN_WARNING
"Impossible event.\n");
593 while ((hh
= neigh
->hh
) != NULL
) {
594 neigh
->hh
= hh
->hh_next
;
596 write_lock_bh(&hh
->hh_lock
);
597 hh
->hh_output
= neigh_blackhole
;
598 write_unlock_bh(&hh
->hh_lock
);
599 if (atomic_dec_and_test(&hh
->hh_refcnt
))
603 if (neigh
->ops
&& neigh
->ops
->destructor
)
604 (neigh
->ops
->destructor
)(neigh
);
606 skb_queue_purge(&neigh
->arp_queue
);
609 neigh_parms_put(neigh
->parms
);
611 NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh
);
613 atomic_dec(&neigh
->tbl
->entries
);
614 kmem_cache_free(neigh
->tbl
->kmem_cachep
, neigh
);
617 /* Neighbour state is suspicious;
620 Called with write_locked neigh.
622 static void neigh_suspect(struct neighbour
*neigh
)
626 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh
);
628 neigh
->output
= neigh
->ops
->output
;
630 for (hh
= neigh
->hh
; hh
; hh
= hh
->hh_next
)
631 hh
->hh_output
= neigh
->ops
->output
;
634 /* Neighbour state is OK;
637 Called with write_locked neigh.
639 static void neigh_connect(struct neighbour
*neigh
)
643 NEIGH_PRINTK2("neigh %p is connected.\n", neigh
);
645 neigh
->output
= neigh
->ops
->connected_output
;
647 for (hh
= neigh
->hh
; hh
; hh
= hh
->hh_next
)
648 hh
->hh_output
= neigh
->ops
->hh_output
;
651 static void neigh_periodic_timer(unsigned long arg
)
653 struct neigh_table
*tbl
= (struct neigh_table
*)arg
;
654 struct neighbour
*n
, **np
;
655 unsigned long expire
, now
= jiffies
;
657 NEIGH_CACHE_STAT_INC(tbl
, periodic_gc_runs
);
659 write_lock(&tbl
->lock
);
662 * periodically recompute ReachableTime from random function
665 if (time_after(now
, tbl
->last_rand
+ 300 * HZ
)) {
666 struct neigh_parms
*p
;
667 tbl
->last_rand
= now
;
668 for (p
= &tbl
->parms
; p
; p
= p
->next
)
670 neigh_rand_reach_time(p
->base_reachable_time
);
673 np
= &tbl
->hash_buckets
[tbl
->hash_chain_gc
];
674 tbl
->hash_chain_gc
= ((tbl
->hash_chain_gc
+ 1) & tbl
->hash_mask
);
676 while ((n
= *np
) != NULL
) {
679 write_lock(&n
->lock
);
681 state
= n
->nud_state
;
682 if (state
& (NUD_PERMANENT
| NUD_IN_TIMER
)) {
683 write_unlock(&n
->lock
);
687 if (time_before(n
->used
, n
->confirmed
))
688 n
->used
= n
->confirmed
;
690 if (atomic_read(&n
->refcnt
) == 1 &&
691 (state
== NUD_FAILED
||
692 time_after(now
, n
->used
+ n
->parms
->gc_staletime
))) {
695 write_unlock(&n
->lock
);
699 write_unlock(&n
->lock
);
705 /* Cycle through all hash buckets every base_reachable_time/2 ticks.
706 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
707 * base_reachable_time.
709 expire
= tbl
->parms
.base_reachable_time
>> 1;
710 expire
/= (tbl
->hash_mask
+ 1);
714 mod_timer(&tbl
->gc_timer
, now
+ expire
);
716 write_unlock(&tbl
->lock
);
719 static __inline__
int neigh_max_probes(struct neighbour
*n
)
721 struct neigh_parms
*p
= n
->parms
;
722 return (n
->nud_state
& NUD_PROBE
?
724 p
->ucast_probes
+ p
->app_probes
+ p
->mcast_probes
);
728 /* Called when a timer expires for a neighbour entry. */
730 static void neigh_timer_handler(unsigned long arg
)
732 unsigned long now
, next
;
733 struct neighbour
*neigh
= (struct neighbour
*)arg
;
737 write_lock(&neigh
->lock
);
739 state
= neigh
->nud_state
;
743 if (!(state
& NUD_IN_TIMER
)) {
745 printk(KERN_WARNING
"neigh: timer & !nud_in_timer\n");
750 if (state
& NUD_REACHABLE
) {
751 if (time_before_eq(now
,
752 neigh
->confirmed
+ neigh
->parms
->reachable_time
)) {
753 NEIGH_PRINTK2("neigh %p is still alive.\n", neigh
);
754 next
= neigh
->confirmed
+ neigh
->parms
->reachable_time
;
755 } else if (time_before_eq(now
,
756 neigh
->used
+ neigh
->parms
->delay_probe_time
)) {
757 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh
);
758 neigh
->nud_state
= NUD_DELAY
;
759 neigh_suspect(neigh
);
760 next
= now
+ neigh
->parms
->delay_probe_time
;
762 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh
);
763 neigh
->nud_state
= NUD_STALE
;
764 neigh_suspect(neigh
);
766 } else if (state
& NUD_DELAY
) {
767 if (time_before_eq(now
,
768 neigh
->confirmed
+ neigh
->parms
->delay_probe_time
)) {
769 NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh
);
770 neigh
->nud_state
= NUD_REACHABLE
;
771 neigh_connect(neigh
);
772 next
= neigh
->confirmed
+ neigh
->parms
->reachable_time
;
774 NEIGH_PRINTK2("neigh %p is probed.\n", neigh
);
775 neigh
->nud_state
= NUD_PROBE
;
776 atomic_set(&neigh
->probes
, 0);
777 next
= now
+ neigh
->parms
->retrans_time
;
780 /* NUD_PROBE|NUD_INCOMPLETE */
781 next
= now
+ neigh
->parms
->retrans_time
;
784 if ((neigh
->nud_state
& (NUD_INCOMPLETE
| NUD_PROBE
)) &&
785 atomic_read(&neigh
->probes
) >= neigh_max_probes(neigh
)) {
788 neigh
->nud_state
= NUD_FAILED
;
790 NEIGH_CACHE_STAT_INC(neigh
->tbl
, res_failed
);
791 NEIGH_PRINTK2("neigh %p is failed.\n", neigh
);
793 /* It is very thin place. report_unreachable is very complicated
794 routine. Particularly, it can hit the same neighbour entry!
796 So that, we try to be accurate and avoid dead loop. --ANK
798 while (neigh
->nud_state
== NUD_FAILED
&&
799 (skb
= __skb_dequeue(&neigh
->arp_queue
)) != NULL
) {
800 write_unlock(&neigh
->lock
);
801 neigh
->ops
->error_report(neigh
, skb
);
802 write_lock(&neigh
->lock
);
804 skb_queue_purge(&neigh
->arp_queue
);
807 if (neigh
->nud_state
& NUD_IN_TIMER
) {
809 if (time_before(next
, jiffies
+ HZ
/2))
810 next
= jiffies
+ HZ
/2;
811 neigh
->timer
.expires
= next
;
812 add_timer(&neigh
->timer
);
814 if (neigh
->nud_state
& (NUD_INCOMPLETE
| NUD_PROBE
)) {
815 struct sk_buff
*skb
= skb_peek(&neigh
->arp_queue
);
816 /* keep skb alive even if arp_queue overflows */
819 write_unlock(&neigh
->lock
);
820 neigh
->ops
->solicit(neigh
, skb
);
821 atomic_inc(&neigh
->probes
);
826 write_unlock(&neigh
->lock
);
830 if (notify
&& neigh
->parms
->app_probes
)
831 neigh_app_notify(neigh
);
833 neigh_release(neigh
);
836 int __neigh_event_send(struct neighbour
*neigh
, struct sk_buff
*skb
)
841 write_lock_bh(&neigh
->lock
);
844 if (neigh
->nud_state
& (NUD_CONNECTED
| NUD_DELAY
| NUD_PROBE
))
849 if (!(neigh
->nud_state
& (NUD_STALE
| NUD_INCOMPLETE
))) {
850 if (neigh
->parms
->mcast_probes
+ neigh
->parms
->app_probes
) {
851 atomic_set(&neigh
->probes
, neigh
->parms
->ucast_probes
);
852 neigh
->nud_state
= NUD_INCOMPLETE
;
854 neigh
->timer
.expires
= now
+ 1;
855 add_timer(&neigh
->timer
);
857 neigh
->nud_state
= NUD_FAILED
;
858 write_unlock_bh(&neigh
->lock
);
864 } else if (neigh
->nud_state
& NUD_STALE
) {
865 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh
);
867 neigh
->nud_state
= NUD_DELAY
;
868 neigh
->timer
.expires
= jiffies
+ neigh
->parms
->delay_probe_time
;
869 add_timer(&neigh
->timer
);
872 if (neigh
->nud_state
== NUD_INCOMPLETE
) {
874 if (skb_queue_len(&neigh
->arp_queue
) >=
875 neigh
->parms
->queue_len
) {
876 struct sk_buff
*buff
;
877 buff
= neigh
->arp_queue
.next
;
878 __skb_unlink(buff
, &neigh
->arp_queue
);
881 __skb_queue_tail(&neigh
->arp_queue
, skb
);
886 write_unlock_bh(&neigh
->lock
);
890 static __inline__
void neigh_update_hhs(struct neighbour
*neigh
)
893 void (*update
)(struct hh_cache
*, struct net_device
*, unsigned char *) =
894 neigh
->dev
->header_cache_update
;
897 for (hh
= neigh
->hh
; hh
; hh
= hh
->hh_next
) {
898 write_lock_bh(&hh
->hh_lock
);
899 update(hh
, neigh
->dev
, neigh
->ha
);
900 write_unlock_bh(&hh
->hh_lock
);
907 /* Generic update routine.
908 -- lladdr is new lladdr or NULL, if it is not supplied.
911 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
913 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
914 lladdr instead of overriding it
916 It also allows to retain current state
917 if lladdr is unchanged.
918 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
920 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
922 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
925 Caller MUST hold reference count on the entry.
928 int neigh_update(struct neighbour
*neigh
, const u8
*lladdr
, u8
new,
936 struct net_device
*dev
;
937 int update_isrouter
= 0;
939 write_lock_bh(&neigh
->lock
);
942 old
= neigh
->nud_state
;
945 if (!(flags
& NEIGH_UPDATE_F_ADMIN
) &&
946 (old
& (NUD_NOARP
| NUD_PERMANENT
)))
949 if (!(new & NUD_VALID
)) {
950 neigh_del_timer(neigh
);
951 if (old
& NUD_CONNECTED
)
952 neigh_suspect(neigh
);
953 neigh
->nud_state
= new;
956 notify
= old
& NUD_VALID
;
961 /* Compare new lladdr with cached one */
962 if (!dev
->addr_len
) {
963 /* First case: device needs no address. */
966 /* The second case: if something is already cached
967 and a new address is proposed:
969 - if they are different, check override flag
971 if ((old
& NUD_VALID
) &&
972 !memcmp(lladdr
, neigh
->ha
, dev
->addr_len
))
975 /* No address is supplied; if we know something,
976 use it, otherwise discard the request.
979 if (!(old
& NUD_VALID
))
984 if (new & NUD_CONNECTED
)
985 neigh
->confirmed
= jiffies
;
986 neigh
->updated
= jiffies
;
988 /* If entry was valid and address is not changed,
989 do not change entry state, if new one is STALE.
992 update_isrouter
= flags
& NEIGH_UPDATE_F_OVERRIDE_ISROUTER
;
993 if (old
& NUD_VALID
) {
994 if (lladdr
!= neigh
->ha
&& !(flags
& NEIGH_UPDATE_F_OVERRIDE
)) {
996 if ((flags
& NEIGH_UPDATE_F_WEAK_OVERRIDE
) &&
997 (old
& NUD_CONNECTED
)) {
1003 if (lladdr
== neigh
->ha
&& new == NUD_STALE
&&
1004 ((flags
& NEIGH_UPDATE_F_WEAK_OVERRIDE
) ||
1005 (old
& NUD_CONNECTED
))
1012 neigh_del_timer(neigh
);
1013 if (new & NUD_IN_TIMER
) {
1015 neigh
->timer
.expires
= jiffies
+
1016 ((new & NUD_REACHABLE
) ?
1017 neigh
->parms
->reachable_time
: 0);
1018 add_timer(&neigh
->timer
);
1020 neigh
->nud_state
= new;
1023 if (lladdr
!= neigh
->ha
) {
1024 memcpy(&neigh
->ha
, lladdr
, dev
->addr_len
);
1025 neigh_update_hhs(neigh
);
1026 if (!(new & NUD_CONNECTED
))
1027 neigh
->confirmed
= jiffies
-
1028 (neigh
->parms
->base_reachable_time
<< 1);
1035 if (new & NUD_CONNECTED
)
1036 neigh_connect(neigh
);
1038 neigh_suspect(neigh
);
1039 if (!(old
& NUD_VALID
)) {
1040 struct sk_buff
*skb
;
1042 /* Again: avoid dead loop if something went wrong */
1044 while (neigh
->nud_state
& NUD_VALID
&&
1045 (skb
= __skb_dequeue(&neigh
->arp_queue
)) != NULL
) {
1046 struct neighbour
*n1
= neigh
;
1047 write_unlock_bh(&neigh
->lock
);
1048 /* On shaper/eql skb->dst->neighbour != neigh :( */
1049 if (skb
->dst
&& skb
->dst
->neighbour
)
1050 n1
= skb
->dst
->neighbour
;
1052 write_lock_bh(&neigh
->lock
);
1054 skb_queue_purge(&neigh
->arp_queue
);
1057 if (update_isrouter
) {
1058 neigh
->flags
= (flags
& NEIGH_UPDATE_F_ISROUTER
) ?
1059 (neigh
->flags
| NTF_ROUTER
) :
1060 (neigh
->flags
& ~NTF_ROUTER
);
1062 write_unlock_bh(&neigh
->lock
);
1064 if (notify
&& neigh
->parms
->app_probes
)
1065 neigh_app_notify(neigh
);
1070 struct neighbour
*neigh_event_ns(struct neigh_table
*tbl
,
1071 u8
*lladdr
, void *saddr
,
1072 struct net_device
*dev
)
1074 struct neighbour
*neigh
= __neigh_lookup(tbl
, saddr
, dev
,
1075 lladdr
|| !dev
->addr_len
);
1077 neigh_update(neigh
, lladdr
, NUD_STALE
,
1078 NEIGH_UPDATE_F_OVERRIDE
);
1082 static void neigh_hh_init(struct neighbour
*n
, struct dst_entry
*dst
,
1085 struct hh_cache
*hh
;
1086 struct net_device
*dev
= dst
->dev
;
1088 for (hh
= n
->hh
; hh
; hh
= hh
->hh_next
)
1089 if (hh
->hh_type
== protocol
)
1092 if (!hh
&& (hh
= kmalloc(sizeof(*hh
), GFP_ATOMIC
)) != NULL
) {
1093 memset(hh
, 0, sizeof(struct hh_cache
));
1094 rwlock_init(&hh
->hh_lock
);
1095 hh
->hh_type
= protocol
;
1096 atomic_set(&hh
->hh_refcnt
, 0);
1098 if (dev
->hard_header_cache(n
, hh
)) {
1102 atomic_inc(&hh
->hh_refcnt
);
1103 hh
->hh_next
= n
->hh
;
1105 if (n
->nud_state
& NUD_CONNECTED
)
1106 hh
->hh_output
= n
->ops
->hh_output
;
1108 hh
->hh_output
= n
->ops
->output
;
1112 atomic_inc(&hh
->hh_refcnt
);
1117 /* This function can be used in contexts, where only old dev_queue_xmit
1118 worked, f.e. if you want to override normal output path (eql, shaper),
1119 but resolution is not made yet.
1122 int neigh_compat_output(struct sk_buff
*skb
)
1124 struct net_device
*dev
= skb
->dev
;
1126 __skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
1128 if (dev
->hard_header
&&
1129 dev
->hard_header(skb
, dev
, ntohs(skb
->protocol
), NULL
, NULL
,
1131 dev
->rebuild_header(skb
))
1134 return dev_queue_xmit(skb
);
1137 /* Slow and careful. */
1139 int neigh_resolve_output(struct sk_buff
*skb
)
1141 struct dst_entry
*dst
= skb
->dst
;
1142 struct neighbour
*neigh
;
1145 if (!dst
|| !(neigh
= dst
->neighbour
))
1148 __skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
1150 if (!neigh_event_send(neigh
, skb
)) {
1152 struct net_device
*dev
= neigh
->dev
;
1153 if (dev
->hard_header_cache
&& !dst
->hh
) {
1154 write_lock_bh(&neigh
->lock
);
1156 neigh_hh_init(neigh
, dst
, dst
->ops
->protocol
);
1157 err
= dev
->hard_header(skb
, dev
, ntohs(skb
->protocol
),
1158 neigh
->ha
, NULL
, skb
->len
);
1159 write_unlock_bh(&neigh
->lock
);
1161 read_lock_bh(&neigh
->lock
);
1162 err
= dev
->hard_header(skb
, dev
, ntohs(skb
->protocol
),
1163 neigh
->ha
, NULL
, skb
->len
);
1164 read_unlock_bh(&neigh
->lock
);
1167 rc
= neigh
->ops
->queue_xmit(skb
);
1174 NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
1175 dst
, dst
? dst
->neighbour
: NULL
);
1182 /* As fast as possible without hh cache */
1184 int neigh_connected_output(struct sk_buff
*skb
)
1187 struct dst_entry
*dst
= skb
->dst
;
1188 struct neighbour
*neigh
= dst
->neighbour
;
1189 struct net_device
*dev
= neigh
->dev
;
1191 __skb_pull(skb
, skb
->nh
.raw
- skb
->data
);
1193 read_lock_bh(&neigh
->lock
);
1194 err
= dev
->hard_header(skb
, dev
, ntohs(skb
->protocol
),
1195 neigh
->ha
, NULL
, skb
->len
);
1196 read_unlock_bh(&neigh
->lock
);
1198 err
= neigh
->ops
->queue_xmit(skb
);
1206 static void neigh_proxy_process(unsigned long arg
)
1208 struct neigh_table
*tbl
= (struct neigh_table
*)arg
;
1209 long sched_next
= 0;
1210 unsigned long now
= jiffies
;
1211 struct sk_buff
*skb
;
1213 spin_lock(&tbl
->proxy_queue
.lock
);
1215 skb
= tbl
->proxy_queue
.next
;
1217 while (skb
!= (struct sk_buff
*)&tbl
->proxy_queue
) {
1218 struct sk_buff
*back
= skb
;
1219 long tdif
= back
->stamp
.tv_usec
- now
;
1223 struct net_device
*dev
= back
->dev
;
1224 __skb_unlink(back
, &tbl
->proxy_queue
);
1225 if (tbl
->proxy_redo
&& netif_running(dev
))
1226 tbl
->proxy_redo(back
);
1231 } else if (!sched_next
|| tdif
< sched_next
)
1234 del_timer(&tbl
->proxy_timer
);
1236 mod_timer(&tbl
->proxy_timer
, jiffies
+ sched_next
);
1237 spin_unlock(&tbl
->proxy_queue
.lock
);
1240 void pneigh_enqueue(struct neigh_table
*tbl
, struct neigh_parms
*p
,
1241 struct sk_buff
*skb
)
1243 unsigned long now
= jiffies
;
1244 unsigned long sched_next
= now
+ (net_random() % p
->proxy_delay
);
1246 if (tbl
->proxy_queue
.qlen
> p
->proxy_qlen
) {
1250 skb
->stamp
.tv_sec
= LOCALLY_ENQUEUED
;
1251 skb
->stamp
.tv_usec
= sched_next
;
1253 spin_lock(&tbl
->proxy_queue
.lock
);
1254 if (del_timer(&tbl
->proxy_timer
)) {
1255 if (time_before(tbl
->proxy_timer
.expires
, sched_next
))
1256 sched_next
= tbl
->proxy_timer
.expires
;
1258 dst_release(skb
->dst
);
1261 __skb_queue_tail(&tbl
->proxy_queue
, skb
);
1262 mod_timer(&tbl
->proxy_timer
, sched_next
);
1263 spin_unlock(&tbl
->proxy_queue
.lock
);
1267 struct neigh_parms
*neigh_parms_alloc(struct net_device
*dev
,
1268 struct neigh_table
*tbl
)
1270 struct neigh_parms
*p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
1273 memcpy(p
, &tbl
->parms
, sizeof(*p
));
1275 atomic_set(&p
->refcnt
, 1);
1276 INIT_RCU_HEAD(&p
->rcu_head
);
1278 neigh_rand_reach_time(p
->base_reachable_time
);
1279 if (dev
&& dev
->neigh_setup
&& dev
->neigh_setup(dev
, p
)) {
1283 p
->sysctl_table
= NULL
;
1284 write_lock_bh(&tbl
->lock
);
1285 p
->next
= tbl
->parms
.next
;
1286 tbl
->parms
.next
= p
;
1287 write_unlock_bh(&tbl
->lock
);
1292 static void neigh_rcu_free_parms(struct rcu_head
*head
)
1294 struct neigh_parms
*parms
=
1295 container_of(head
, struct neigh_parms
, rcu_head
);
1297 neigh_parms_put(parms
);
1300 void neigh_parms_release(struct neigh_table
*tbl
, struct neigh_parms
*parms
)
1302 struct neigh_parms
**p
;
1304 if (!parms
|| parms
== &tbl
->parms
)
1306 write_lock_bh(&tbl
->lock
);
1307 for (p
= &tbl
->parms
.next
; *p
; p
= &(*p
)->next
) {
1311 write_unlock_bh(&tbl
->lock
);
1312 call_rcu(&parms
->rcu_head
, neigh_rcu_free_parms
);
1316 write_unlock_bh(&tbl
->lock
);
1317 NEIGH_PRINTK1("neigh_parms_release: not found\n");
1320 void neigh_parms_destroy(struct neigh_parms
*parms
)
1326 void neigh_table_init(struct neigh_table
*tbl
)
1328 unsigned long now
= jiffies
;
1329 unsigned long phsize
;
1331 atomic_set(&tbl
->parms
.refcnt
, 1);
1332 INIT_RCU_HEAD(&tbl
->parms
.rcu_head
);
1333 tbl
->parms
.reachable_time
=
1334 neigh_rand_reach_time(tbl
->parms
.base_reachable_time
);
1336 if (!tbl
->kmem_cachep
)
1337 tbl
->kmem_cachep
= kmem_cache_create(tbl
->id
,
1339 0, SLAB_HWCACHE_ALIGN
,
1342 if (!tbl
->kmem_cachep
)
1343 panic("cannot create neighbour cache");
1345 tbl
->stats
= alloc_percpu(struct neigh_statistics
);
1347 panic("cannot create neighbour cache statistics");
1349 #ifdef CONFIG_PROC_FS
1350 tbl
->pde
= create_proc_entry(tbl
->id
, 0, proc_net_stat
);
1352 panic("cannot create neighbour proc dir entry");
1353 tbl
->pde
->proc_fops
= &neigh_stat_seq_fops
;
1354 tbl
->pde
->data
= tbl
;
1358 tbl
->hash_buckets
= neigh_hash_alloc(tbl
->hash_mask
+ 1);
1360 phsize
= (PNEIGH_HASHMASK
+ 1) * sizeof(struct pneigh_entry
*);
1361 tbl
->phash_buckets
= kmalloc(phsize
, GFP_KERNEL
);
1363 if (!tbl
->hash_buckets
|| !tbl
->phash_buckets
)
1364 panic("cannot allocate neighbour cache hashes");
1366 memset(tbl
->phash_buckets
, 0, phsize
);
1368 get_random_bytes(&tbl
->hash_rnd
, sizeof(tbl
->hash_rnd
));
1370 rwlock_init(&tbl
->lock
);
1371 init_timer(&tbl
->gc_timer
);
1372 tbl
->gc_timer
.data
= (unsigned long)tbl
;
1373 tbl
->gc_timer
.function
= neigh_periodic_timer
;
1374 tbl
->gc_timer
.expires
= now
+ 1;
1375 add_timer(&tbl
->gc_timer
);
1377 init_timer(&tbl
->proxy_timer
);
1378 tbl
->proxy_timer
.data
= (unsigned long)tbl
;
1379 tbl
->proxy_timer
.function
= neigh_proxy_process
;
1380 skb_queue_head_init(&tbl
->proxy_queue
);
1382 tbl
->last_flush
= now
;
1383 tbl
->last_rand
= now
+ tbl
->parms
.reachable_time
* 20;
1384 write_lock(&neigh_tbl_lock
);
1385 tbl
->next
= neigh_tables
;
1387 write_unlock(&neigh_tbl_lock
);
1390 int neigh_table_clear(struct neigh_table
*tbl
)
1392 struct neigh_table
**tp
;
1394 /* It is not clean... Fix it to unload IPv6 module safely */
1395 del_timer_sync(&tbl
->gc_timer
);
1396 del_timer_sync(&tbl
->proxy_timer
);
1397 pneigh_queue_purge(&tbl
->proxy_queue
);
1398 neigh_ifdown(tbl
, NULL
);
1399 if (atomic_read(&tbl
->entries
))
1400 printk(KERN_CRIT
"neighbour leakage\n");
1401 write_lock(&neigh_tbl_lock
);
1402 for (tp
= &neigh_tables
; *tp
; tp
= &(*tp
)->next
) {
1408 write_unlock(&neigh_tbl_lock
);
1410 neigh_hash_free(tbl
->hash_buckets
, tbl
->hash_mask
+ 1);
1411 tbl
->hash_buckets
= NULL
;
1413 kfree(tbl
->phash_buckets
);
1414 tbl
->phash_buckets
= NULL
;
1419 int neigh_delete(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
1421 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
1422 struct rtattr
**nda
= arg
;
1423 struct neigh_table
*tbl
;
1424 struct net_device
*dev
= NULL
;
1427 if (ndm
->ndm_ifindex
&&
1428 (dev
= dev_get_by_index(ndm
->ndm_ifindex
)) == NULL
)
1431 read_lock(&neigh_tbl_lock
);
1432 for (tbl
= neigh_tables
; tbl
; tbl
= tbl
->next
) {
1433 struct rtattr
*dst_attr
= nda
[NDA_DST
- 1];
1434 struct neighbour
*n
;
1436 if (tbl
->family
!= ndm
->ndm_family
)
1438 read_unlock(&neigh_tbl_lock
);
1441 if (!dst_attr
|| RTA_PAYLOAD(dst_attr
) < tbl
->key_len
)
1444 if (ndm
->ndm_flags
& NTF_PROXY
) {
1445 err
= pneigh_delete(tbl
, RTA_DATA(dst_attr
), dev
);
1452 n
= neigh_lookup(tbl
, RTA_DATA(dst_attr
), dev
);
1454 err
= neigh_update(n
, NULL
, NUD_FAILED
,
1455 NEIGH_UPDATE_F_OVERRIDE
|
1456 NEIGH_UPDATE_F_ADMIN
);
1461 read_unlock(&neigh_tbl_lock
);
1462 err
= -EADDRNOTAVAIL
;
1470 int neigh_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
1472 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
1473 struct rtattr
**nda
= arg
;
1474 struct neigh_table
*tbl
;
1475 struct net_device
*dev
= NULL
;
1478 if (ndm
->ndm_ifindex
&&
1479 (dev
= dev_get_by_index(ndm
->ndm_ifindex
)) == NULL
)
1482 read_lock(&neigh_tbl_lock
);
1483 for (tbl
= neigh_tables
; tbl
; tbl
= tbl
->next
) {
1484 struct rtattr
*lladdr_attr
= nda
[NDA_LLADDR
- 1];
1485 struct rtattr
*dst_attr
= nda
[NDA_DST
- 1];
1487 struct neighbour
*n
;
1489 if (tbl
->family
!= ndm
->ndm_family
)
1491 read_unlock(&neigh_tbl_lock
);
1494 if (!dst_attr
|| RTA_PAYLOAD(dst_attr
) < tbl
->key_len
)
1497 if (ndm
->ndm_flags
& NTF_PROXY
) {
1499 if (pneigh_lookup(tbl
, RTA_DATA(dst_attr
), dev
, 1))
1507 if (lladdr_attr
&& RTA_PAYLOAD(lladdr_attr
) < dev
->addr_len
)
1510 n
= neigh_lookup(tbl
, RTA_DATA(dst_attr
), dev
);
1512 if (nlh
->nlmsg_flags
& NLM_F_EXCL
) {
1518 override
= nlh
->nlmsg_flags
& NLM_F_REPLACE
;
1519 } else if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
1523 n
= __neigh_lookup_errno(tbl
, RTA_DATA(dst_attr
), dev
);
1530 err
= neigh_update(n
,
1531 lladdr_attr
? RTA_DATA(lladdr_attr
) : NULL
,
1533 (override
? NEIGH_UPDATE_F_OVERRIDE
: 0) |
1534 NEIGH_UPDATE_F_ADMIN
);
1540 read_unlock(&neigh_tbl_lock
);
1541 err
= -EADDRNOTAVAIL
;
1550 static int neigh_fill_info(struct sk_buff
*skb
, struct neighbour
*n
,
1551 u32 pid
, u32 seq
, int event
)
1553 unsigned long now
= jiffies
;
1554 unsigned char *b
= skb
->tail
;
1555 struct nda_cacheinfo ci
;
1558 struct nlmsghdr
*nlh
= NLMSG_PUT(skb
, pid
, seq
, event
,
1559 sizeof(struct ndmsg
));
1560 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
1562 nlh
->nlmsg_flags
= pid
? NLM_F_MULTI
: 0;
1563 ndm
->ndm_family
= n
->ops
->family
;
1564 ndm
->ndm_flags
= n
->flags
;
1565 ndm
->ndm_type
= n
->type
;
1566 ndm
->ndm_ifindex
= n
->dev
->ifindex
;
1567 RTA_PUT(skb
, NDA_DST
, n
->tbl
->key_len
, n
->primary_key
);
1568 read_lock_bh(&n
->lock
);
1570 ndm
->ndm_state
= n
->nud_state
;
1571 if (n
->nud_state
& NUD_VALID
)
1572 RTA_PUT(skb
, NDA_LLADDR
, n
->dev
->addr_len
, n
->ha
);
1573 ci
.ndm_used
= now
- n
->used
;
1574 ci
.ndm_confirmed
= now
- n
->confirmed
;
1575 ci
.ndm_updated
= now
- n
->updated
;
1576 ci
.ndm_refcnt
= atomic_read(&n
->refcnt
) - 1;
1577 probes
= atomic_read(&n
->probes
);
1578 read_unlock_bh(&n
->lock
);
1580 RTA_PUT(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
);
1581 RTA_PUT(skb
, NDA_PROBES
, sizeof(probes
), &probes
);
1582 nlh
->nlmsg_len
= skb
->tail
- b
;
1588 read_unlock_bh(&n
->lock
);
1589 skb_trim(skb
, b
- skb
->data
);
1594 static int neigh_dump_table(struct neigh_table
*tbl
, struct sk_buff
*skb
,
1595 struct netlink_callback
*cb
)
1597 struct neighbour
*n
;
1598 int rc
, h
, s_h
= cb
->args
[1];
1599 int idx
, s_idx
= idx
= cb
->args
[2];
1601 for (h
= 0; h
<= tbl
->hash_mask
; h
++) {
1606 read_lock_bh(&tbl
->lock
);
1607 for (n
= tbl
->hash_buckets
[h
], idx
= 0; n
; n
= n
->next
, idx
++) {
1610 if (neigh_fill_info(skb
, n
, NETLINK_CB(cb
->skb
).pid
,
1612 RTM_NEWNEIGH
) <= 0) {
1613 read_unlock_bh(&tbl
->lock
);
1618 read_unlock_bh(&tbl
->lock
);
1627 int neigh_dump_info(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1629 struct neigh_table
*tbl
;
1632 read_lock(&neigh_tbl_lock
);
1633 family
= ((struct rtgenmsg
*)NLMSG_DATA(cb
->nlh
))->rtgen_family
;
1636 for (tbl
= neigh_tables
, t
= 0; tbl
; tbl
= tbl
->next
, t
++) {
1637 if (t
< s_t
|| (family
&& tbl
->family
!= family
))
1640 memset(&cb
->args
[1], 0, sizeof(cb
->args
) -
1641 sizeof(cb
->args
[0]));
1642 if (neigh_dump_table(tbl
, skb
, cb
) < 0)
1645 read_unlock(&neigh_tbl_lock
);
1651 void neigh_for_each(struct neigh_table
*tbl
, void (*cb
)(struct neighbour
*, void *), void *cookie
)
1655 read_lock_bh(&tbl
->lock
);
1656 for (chain
= 0; chain
<= tbl
->hash_mask
; chain
++) {
1657 struct neighbour
*n
;
1659 for (n
= tbl
->hash_buckets
[chain
]; n
; n
= n
->next
)
1662 read_unlock_bh(&tbl
->lock
);
1664 EXPORT_SYMBOL(neigh_for_each
);
1666 /* The tbl->lock must be held as a writer and BH disabled. */
1667 void __neigh_for_each_release(struct neigh_table
*tbl
,
1668 int (*cb
)(struct neighbour
*))
1672 for (chain
= 0; chain
<= tbl
->hash_mask
; chain
++) {
1673 struct neighbour
*n
, **np
;
1675 np
= &tbl
->hash_buckets
[chain
];
1676 while ((n
= *np
) != NULL
) {
1679 write_lock(&n
->lock
);
1686 write_unlock(&n
->lock
);
1692 EXPORT_SYMBOL(__neigh_for_each_release
);
1694 #ifdef CONFIG_PROC_FS
1696 static struct neighbour
*neigh_get_first(struct seq_file
*seq
)
1698 struct neigh_seq_state
*state
= seq
->private;
1699 struct neigh_table
*tbl
= state
->tbl
;
1700 struct neighbour
*n
= NULL
;
1701 int bucket
= state
->bucket
;
1703 state
->flags
&= ~NEIGH_SEQ_IS_PNEIGH
;
1704 for (bucket
= 0; bucket
<= tbl
->hash_mask
; bucket
++) {
1705 n
= tbl
->hash_buckets
[bucket
];
1708 if (state
->neigh_sub_iter
) {
1712 v
= state
->neigh_sub_iter(state
, n
, &fakep
);
1716 if (!(state
->flags
& NEIGH_SEQ_SKIP_NOARP
))
1718 if (n
->nud_state
& ~NUD_NOARP
)
1727 state
->bucket
= bucket
;
1732 static struct neighbour
*neigh_get_next(struct seq_file
*seq
,
1733 struct neighbour
*n
,
1736 struct neigh_seq_state
*state
= seq
->private;
1737 struct neigh_table
*tbl
= state
->tbl
;
1739 if (state
->neigh_sub_iter
) {
1740 void *v
= state
->neigh_sub_iter(state
, n
, pos
);
1748 if (state
->neigh_sub_iter
) {
1749 void *v
= state
->neigh_sub_iter(state
, n
, pos
);
1754 if (!(state
->flags
& NEIGH_SEQ_SKIP_NOARP
))
1757 if (n
->nud_state
& ~NUD_NOARP
)
1766 if (++state
->bucket
> tbl
->hash_mask
)
1769 n
= tbl
->hash_buckets
[state
->bucket
];
1777 static struct neighbour
*neigh_get_idx(struct seq_file
*seq
, loff_t
*pos
)
1779 struct neighbour
*n
= neigh_get_first(seq
);
1783 n
= neigh_get_next(seq
, n
, pos
);
1788 return *pos
? NULL
: n
;
1791 static struct pneigh_entry
*pneigh_get_first(struct seq_file
*seq
)
1793 struct neigh_seq_state
*state
= seq
->private;
1794 struct neigh_table
*tbl
= state
->tbl
;
1795 struct pneigh_entry
*pn
= NULL
;
1796 int bucket
= state
->bucket
;
1798 state
->flags
|= NEIGH_SEQ_IS_PNEIGH
;
1799 for (bucket
= 0; bucket
<= PNEIGH_HASHMASK
; bucket
++) {
1800 pn
= tbl
->phash_buckets
[bucket
];
1804 state
->bucket
= bucket
;
1809 static struct pneigh_entry
*pneigh_get_next(struct seq_file
*seq
,
1810 struct pneigh_entry
*pn
,
1813 struct neigh_seq_state
*state
= seq
->private;
1814 struct neigh_table
*tbl
= state
->tbl
;
1818 if (++state
->bucket
> PNEIGH_HASHMASK
)
1820 pn
= tbl
->phash_buckets
[state
->bucket
];
1831 static struct pneigh_entry
*pneigh_get_idx(struct seq_file
*seq
, loff_t
*pos
)
1833 struct pneigh_entry
*pn
= pneigh_get_first(seq
);
1837 pn
= pneigh_get_next(seq
, pn
, pos
);
1842 return *pos
? NULL
: pn
;
1845 static void *neigh_get_idx_any(struct seq_file
*seq
, loff_t
*pos
)
1847 struct neigh_seq_state
*state
= seq
->private;
1850 rc
= neigh_get_idx(seq
, pos
);
1851 if (!rc
&& !(state
->flags
& NEIGH_SEQ_NEIGH_ONLY
))
1852 rc
= pneigh_get_idx(seq
, pos
);
1857 void *neigh_seq_start(struct seq_file
*seq
, loff_t
*pos
, struct neigh_table
*tbl
, unsigned int neigh_seq_flags
)
1859 struct neigh_seq_state
*state
= seq
->private;
1860 loff_t pos_minus_one
;
1864 state
->flags
= (neigh_seq_flags
& ~NEIGH_SEQ_IS_PNEIGH
);
1866 read_lock_bh(&tbl
->lock
);
1868 pos_minus_one
= *pos
- 1;
1869 return *pos
? neigh_get_idx_any(seq
, &pos_minus_one
) : SEQ_START_TOKEN
;
1871 EXPORT_SYMBOL(neigh_seq_start
);
1873 void *neigh_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1875 struct neigh_seq_state
*state
;
1878 if (v
== SEQ_START_TOKEN
) {
1879 rc
= neigh_get_idx(seq
, pos
);
1883 state
= seq
->private;
1884 if (!(state
->flags
& NEIGH_SEQ_IS_PNEIGH
)) {
1885 rc
= neigh_get_next(seq
, v
, NULL
);
1888 if (!(state
->flags
& NEIGH_SEQ_NEIGH_ONLY
))
1889 rc
= pneigh_get_first(seq
);
1891 BUG_ON(state
->flags
& NEIGH_SEQ_NEIGH_ONLY
);
1892 rc
= pneigh_get_next(seq
, v
, NULL
);
1898 EXPORT_SYMBOL(neigh_seq_next
);
1900 void neigh_seq_stop(struct seq_file
*seq
, void *v
)
1902 struct neigh_seq_state
*state
= seq
->private;
1903 struct neigh_table
*tbl
= state
->tbl
;
1905 read_unlock_bh(&tbl
->lock
);
1907 EXPORT_SYMBOL(neigh_seq_stop
);
1909 /* statistics via seq_file */
1911 static void *neigh_stat_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1913 struct proc_dir_entry
*pde
= seq
->private;
1914 struct neigh_table
*tbl
= pde
->data
;
1918 return SEQ_START_TOKEN
;
1920 for (cpu
= *pos
-1; cpu
< NR_CPUS
; ++cpu
) {
1921 if (!cpu_possible(cpu
))
1924 return per_cpu_ptr(tbl
->stats
, cpu
);
1929 static void *neigh_stat_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1931 struct proc_dir_entry
*pde
= seq
->private;
1932 struct neigh_table
*tbl
= pde
->data
;
1935 for (cpu
= *pos
; cpu
< NR_CPUS
; ++cpu
) {
1936 if (!cpu_possible(cpu
))
1939 return per_cpu_ptr(tbl
->stats
, cpu
);
1944 static void neigh_stat_seq_stop(struct seq_file
*seq
, void *v
)
1949 static int neigh_stat_seq_show(struct seq_file
*seq
, void *v
)
1951 struct proc_dir_entry
*pde
= seq
->private;
1952 struct neigh_table
*tbl
= pde
->data
;
1953 struct neigh_statistics
*st
= v
;
1955 if (v
== SEQ_START_TOKEN
) {
1956 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");
1960 seq_printf(seq
, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
1961 "%08lx %08lx %08lx %08lx\n",
1962 atomic_read(&tbl
->entries
),
1973 st
->rcv_probes_mcast
,
1974 st
->rcv_probes_ucast
,
1976 st
->periodic_gc_runs
,
1983 static struct seq_operations neigh_stat_seq_ops
= {
1984 .start
= neigh_stat_seq_start
,
1985 .next
= neigh_stat_seq_next
,
1986 .stop
= neigh_stat_seq_stop
,
1987 .show
= neigh_stat_seq_show
,
1990 static int neigh_stat_seq_open(struct inode
*inode
, struct file
*file
)
1992 int ret
= seq_open(file
, &neigh_stat_seq_ops
);
1995 struct seq_file
*sf
= file
->private_data
;
1996 sf
->private = PDE(inode
);
2001 static struct file_operations neigh_stat_seq_fops
= {
2002 .owner
= THIS_MODULE
,
2003 .open
= neigh_stat_seq_open
,
2005 .llseek
= seq_lseek
,
2006 .release
= seq_release
,
2009 #endif /* CONFIG_PROC_FS */
2012 void neigh_app_ns(struct neighbour
*n
)
2014 struct nlmsghdr
*nlh
;
2015 int size
= NLMSG_SPACE(sizeof(struct ndmsg
) + 256);
2016 struct sk_buff
*skb
= alloc_skb(size
, GFP_ATOMIC
);
2021 if (neigh_fill_info(skb
, n
, 0, 0, RTM_GETNEIGH
) < 0) {
2025 nlh
= (struct nlmsghdr
*)skb
->data
;
2026 nlh
->nlmsg_flags
= NLM_F_REQUEST
;
2027 NETLINK_CB(skb
).dst_groups
= RTMGRP_NEIGH
;
2028 netlink_broadcast(rtnl
, skb
, 0, RTMGRP_NEIGH
, GFP_ATOMIC
);
2031 static void neigh_app_notify(struct neighbour
*n
)
2033 struct nlmsghdr
*nlh
;
2034 int size
= NLMSG_SPACE(sizeof(struct ndmsg
) + 256);
2035 struct sk_buff
*skb
= alloc_skb(size
, GFP_ATOMIC
);
2040 if (neigh_fill_info(skb
, n
, 0, 0, RTM_NEWNEIGH
) < 0) {
2044 nlh
= (struct nlmsghdr
*)skb
->data
;
2045 NETLINK_CB(skb
).dst_groups
= RTMGRP_NEIGH
;
2046 netlink_broadcast(rtnl
, skb
, 0, RTMGRP_NEIGH
, GFP_ATOMIC
);
2049 #endif /* CONFIG_ARPD */
2051 #ifdef CONFIG_SYSCTL
2053 static struct neigh_sysctl_table
{
2054 struct ctl_table_header
*sysctl_header
;
2055 ctl_table neigh_vars
[__NET_NEIGH_MAX
];
2056 ctl_table neigh_dev
[2];
2057 ctl_table neigh_neigh_dir
[2];
2058 ctl_table neigh_proto_dir
[2];
2059 ctl_table neigh_root_dir
[2];
2060 } neigh_sysctl_template
= {
2063 .ctl_name
= NET_NEIGH_MCAST_SOLICIT
,
2064 .procname
= "mcast_solicit",
2065 .maxlen
= sizeof(int),
2067 .proc_handler
= &proc_dointvec
,
2070 .ctl_name
= NET_NEIGH_UCAST_SOLICIT
,
2071 .procname
= "ucast_solicit",
2072 .maxlen
= sizeof(int),
2074 .proc_handler
= &proc_dointvec
,
2077 .ctl_name
= NET_NEIGH_APP_SOLICIT
,
2078 .procname
= "app_solicit",
2079 .maxlen
= sizeof(int),
2081 .proc_handler
= &proc_dointvec
,
2084 .ctl_name
= NET_NEIGH_RETRANS_TIME
,
2085 .procname
= "retrans_time",
2086 .maxlen
= sizeof(int),
2088 .proc_handler
= &proc_dointvec_userhz_jiffies
,
2091 .ctl_name
= NET_NEIGH_REACHABLE_TIME
,
2092 .procname
= "base_reachable_time",
2093 .maxlen
= sizeof(int),
2095 .proc_handler
= &proc_dointvec_jiffies
,
2096 .strategy
= &sysctl_jiffies
,
2099 .ctl_name
= NET_NEIGH_DELAY_PROBE_TIME
,
2100 .procname
= "delay_first_probe_time",
2101 .maxlen
= sizeof(int),
2103 .proc_handler
= &proc_dointvec_jiffies
,
2104 .strategy
= &sysctl_jiffies
,
2107 .ctl_name
= NET_NEIGH_GC_STALE_TIME
,
2108 .procname
= "gc_stale_time",
2109 .maxlen
= sizeof(int),
2111 .proc_handler
= &proc_dointvec_jiffies
,
2112 .strategy
= &sysctl_jiffies
,
2115 .ctl_name
= NET_NEIGH_UNRES_QLEN
,
2116 .procname
= "unres_qlen",
2117 .maxlen
= sizeof(int),
2119 .proc_handler
= &proc_dointvec
,
2122 .ctl_name
= NET_NEIGH_PROXY_QLEN
,
2123 .procname
= "proxy_qlen",
2124 .maxlen
= sizeof(int),
2126 .proc_handler
= &proc_dointvec
,
2129 .ctl_name
= NET_NEIGH_ANYCAST_DELAY
,
2130 .procname
= "anycast_delay",
2131 .maxlen
= sizeof(int),
2133 .proc_handler
= &proc_dointvec_userhz_jiffies
,
2136 .ctl_name
= NET_NEIGH_PROXY_DELAY
,
2137 .procname
= "proxy_delay",
2138 .maxlen
= sizeof(int),
2140 .proc_handler
= &proc_dointvec_userhz_jiffies
,
2143 .ctl_name
= NET_NEIGH_LOCKTIME
,
2144 .procname
= "locktime",
2145 .maxlen
= sizeof(int),
2147 .proc_handler
= &proc_dointvec_userhz_jiffies
,
2150 .ctl_name
= NET_NEIGH_GC_INTERVAL
,
2151 .procname
= "gc_interval",
2152 .maxlen
= sizeof(int),
2154 .proc_handler
= &proc_dointvec_jiffies
,
2155 .strategy
= &sysctl_jiffies
,
2158 .ctl_name
= NET_NEIGH_GC_THRESH1
,
2159 .procname
= "gc_thresh1",
2160 .maxlen
= sizeof(int),
2162 .proc_handler
= &proc_dointvec
,
2165 .ctl_name
= NET_NEIGH_GC_THRESH2
,
2166 .procname
= "gc_thresh2",
2167 .maxlen
= sizeof(int),
2169 .proc_handler
= &proc_dointvec
,
2172 .ctl_name
= NET_NEIGH_GC_THRESH3
,
2173 .procname
= "gc_thresh3",
2174 .maxlen
= sizeof(int),
2176 .proc_handler
= &proc_dointvec
,
2179 .ctl_name
= NET_NEIGH_RETRANS_TIME_MS
,
2180 .procname
= "retrans_time_ms",
2181 .maxlen
= sizeof(int),
2183 .proc_handler
= &proc_dointvec_ms_jiffies
,
2184 .strategy
= &sysctl_ms_jiffies
,
2187 .ctl_name
= NET_NEIGH_REACHABLE_TIME_MS
,
2188 .procname
= "base_reachable_time_ms",
2189 .maxlen
= sizeof(int),
2191 .proc_handler
= &proc_dointvec_ms_jiffies
,
2192 .strategy
= &sysctl_ms_jiffies
,
2197 .ctl_name
= NET_PROTO_CONF_DEFAULT
,
2198 .procname
= "default",
2202 .neigh_neigh_dir
= {
2204 .procname
= "neigh",
2208 .neigh_proto_dir
= {
2215 .ctl_name
= CTL_NET
,
2222 int neigh_sysctl_register(struct net_device
*dev
, struct neigh_parms
*p
,
2223 int p_id
, int pdev_id
, char *p_name
,
2224 proc_handler
*handler
, ctl_handler
*strategy
)
2226 struct neigh_sysctl_table
*t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
2227 const char *dev_name_source
= NULL
;
2228 char *dev_name
= NULL
;
2233 memcpy(t
, &neigh_sysctl_template
, sizeof(*t
));
2234 t
->neigh_vars
[0].data
= &p
->mcast_probes
;
2235 t
->neigh_vars
[1].data
= &p
->ucast_probes
;
2236 t
->neigh_vars
[2].data
= &p
->app_probes
;
2237 t
->neigh_vars
[3].data
= &p
->retrans_time
;
2238 t
->neigh_vars
[4].data
= &p
->base_reachable_time
;
2239 t
->neigh_vars
[5].data
= &p
->delay_probe_time
;
2240 t
->neigh_vars
[6].data
= &p
->gc_staletime
;
2241 t
->neigh_vars
[7].data
= &p
->queue_len
;
2242 t
->neigh_vars
[8].data
= &p
->proxy_qlen
;
2243 t
->neigh_vars
[9].data
= &p
->anycast_delay
;
2244 t
->neigh_vars
[10].data
= &p
->proxy_delay
;
2245 t
->neigh_vars
[11].data
= &p
->locktime
;
2248 dev_name_source
= dev
->name
;
2249 t
->neigh_dev
[0].ctl_name
= dev
->ifindex
;
2250 t
->neigh_vars
[12].procname
= NULL
;
2251 t
->neigh_vars
[13].procname
= NULL
;
2252 t
->neigh_vars
[14].procname
= NULL
;
2253 t
->neigh_vars
[15].procname
= NULL
;
2255 dev_name_source
= t
->neigh_dev
[0].procname
;
2256 t
->neigh_vars
[12].data
= (int *)(p
+ 1);
2257 t
->neigh_vars
[13].data
= (int *)(p
+ 1) + 1;
2258 t
->neigh_vars
[14].data
= (int *)(p
+ 1) + 2;
2259 t
->neigh_vars
[15].data
= (int *)(p
+ 1) + 3;
2262 t
->neigh_vars
[16].data
= &p
->retrans_time
;
2263 t
->neigh_vars
[17].data
= &p
->base_reachable_time
;
2265 if (handler
|| strategy
) {
2267 t
->neigh_vars
[3].proc_handler
= handler
;
2268 t
->neigh_vars
[3].strategy
= strategy
;
2269 t
->neigh_vars
[3].extra1
= dev
;
2271 t
->neigh_vars
[4].proc_handler
= handler
;
2272 t
->neigh_vars
[4].strategy
= strategy
;
2273 t
->neigh_vars
[4].extra1
= dev
;
2274 /* RetransTime (in milliseconds)*/
2275 t
->neigh_vars
[16].proc_handler
= handler
;
2276 t
->neigh_vars
[16].strategy
= strategy
;
2277 t
->neigh_vars
[16].extra1
= dev
;
2278 /* ReachableTime (in milliseconds) */
2279 t
->neigh_vars
[17].proc_handler
= handler
;
2280 t
->neigh_vars
[17].strategy
= strategy
;
2281 t
->neigh_vars
[17].extra1
= dev
;
2284 dev_name
= net_sysctl_strdup(dev_name_source
);
2290 t
->neigh_dev
[0].procname
= dev_name
;
2292 t
->neigh_neigh_dir
[0].ctl_name
= pdev_id
;
2294 t
->neigh_proto_dir
[0].procname
= p_name
;
2295 t
->neigh_proto_dir
[0].ctl_name
= p_id
;
2297 t
->neigh_dev
[0].child
= t
->neigh_vars
;
2298 t
->neigh_neigh_dir
[0].child
= t
->neigh_dev
;
2299 t
->neigh_proto_dir
[0].child
= t
->neigh_neigh_dir
;
2300 t
->neigh_root_dir
[0].child
= t
->neigh_proto_dir
;
2302 t
->sysctl_header
= register_sysctl_table(t
->neigh_root_dir
, 0);
2303 if (!t
->sysctl_header
) {
2307 p
->sysctl_table
= t
;
2319 void neigh_sysctl_unregister(struct neigh_parms
*p
)
2321 if (p
->sysctl_table
) {
2322 struct neigh_sysctl_table
*t
= p
->sysctl_table
;
2323 p
->sysctl_table
= NULL
;
2324 unregister_sysctl_table(t
->sysctl_header
);
2325 kfree(t
->neigh_dev
[0].procname
);
2330 #endif /* CONFIG_SYSCTL */
2332 EXPORT_SYMBOL(__neigh_event_send
);
2333 EXPORT_SYMBOL(neigh_add
);
2334 EXPORT_SYMBOL(neigh_changeaddr
);
2335 EXPORT_SYMBOL(neigh_compat_output
);
2336 EXPORT_SYMBOL(neigh_connected_output
);
2337 EXPORT_SYMBOL(neigh_create
);
2338 EXPORT_SYMBOL(neigh_delete
);
2339 EXPORT_SYMBOL(neigh_destroy
);
2340 EXPORT_SYMBOL(neigh_dump_info
);
2341 EXPORT_SYMBOL(neigh_event_ns
);
2342 EXPORT_SYMBOL(neigh_ifdown
);
2343 EXPORT_SYMBOL(neigh_lookup
);
2344 EXPORT_SYMBOL(neigh_lookup_nodev
);
2345 EXPORT_SYMBOL(neigh_parms_alloc
);
2346 EXPORT_SYMBOL(neigh_parms_release
);
2347 EXPORT_SYMBOL(neigh_rand_reach_time
);
2348 EXPORT_SYMBOL(neigh_resolve_output
);
2349 EXPORT_SYMBOL(neigh_table_clear
);
2350 EXPORT_SYMBOL(neigh_table_init
);
2351 EXPORT_SYMBOL(neigh_update
);
2352 EXPORT_SYMBOL(neigh_update_hhs
);
2353 EXPORT_SYMBOL(pneigh_enqueue
);
2354 EXPORT_SYMBOL(pneigh_lookup
);
2357 EXPORT_SYMBOL(neigh_app_ns
);
2359 #ifdef CONFIG_SYSCTL
2360 EXPORT_SYMBOL(neigh_sysctl_register
);
2361 EXPORT_SYMBOL(neigh_sysctl_unregister
);