mlx4: Fixing wrong error codes in communication channel
[linux-2.6-block.git] / net / core / neighbour.c
CommitLineData
1da177e4
LT
1/*
2 * Generic address resolution entity
3 *
4 * Authors:
5 * Pedro Roque <roque@di.fc.ul.pt>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 *
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.
12 *
13 * Fixes:
14 * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
15 * Harald Welte Add neighbour cache statistics like rtstat
16 */
17
5a0e3ad6 18#include <linux/slab.h>
1da177e4
LT
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/socket.h>
1da177e4
LT
23#include <linux/netdevice.h>
24#include <linux/proc_fs.h>
25#ifdef CONFIG_SYSCTL
26#include <linux/sysctl.h>
27#endif
28#include <linux/times.h>
457c4cbc 29#include <net/net_namespace.h>
1da177e4
LT
30#include <net/neighbour.h>
31#include <net/dst.h>
32#include <net/sock.h>
8d71740c 33#include <net/netevent.h>
a14a49d2 34#include <net/netlink.h>
1da177e4
LT
35#include <linux/rtnetlink.h>
36#include <linux/random.h>
543537bd 37#include <linux/string.h>
c3609d51 38#include <linux/log2.h>
1da177e4
LT
39
40#define NEIGH_DEBUG 1
41
42#define NEIGH_PRINTK(x...) printk(x)
43#define NEIGH_NOPRINTK(x...) do { ; } while(0)
1da177e4
LT
44#define NEIGH_PRINTK1 NEIGH_NOPRINTK
45#define NEIGH_PRINTK2 NEIGH_NOPRINTK
46
47#if NEIGH_DEBUG >= 1
48#undef NEIGH_PRINTK1
49#define NEIGH_PRINTK1 NEIGH_PRINTK
50#endif
51#if NEIGH_DEBUG >= 2
52#undef NEIGH_PRINTK2
53#define NEIGH_PRINTK2 NEIGH_PRINTK
54#endif
55
56#define PNEIGH_HASHMASK 0xF
57
58static void neigh_timer_handler(unsigned long arg);
d961db35
TG
59static void __neigh_notify(struct neighbour *n, int type, int flags);
60static void neigh_update_notify(struct neighbour *neigh);
1da177e4 61static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
1da177e4
LT
62
63static struct neigh_table *neigh_tables;
45fc3b11 64#ifdef CONFIG_PROC_FS
9a32144e 65static const struct file_operations neigh_stat_seq_fops;
45fc3b11 66#endif
1da177e4
LT
67
68/*
69 Neighbour hash table buckets are protected with rwlock tbl->lock.
70
71 - All the scans/updates to hash buckets MUST be made under this lock.
72 - NOTHING clever should be made under this lock: no callbacks
73 to protocol backends, no attempts to send something to network.
74 It will result in deadlocks, if backend/driver wants to use neighbour
75 cache.
76 - If the entry requires some non-trivial actions, increase
77 its reference count and release table lock.
78
79 Neighbour entries are protected:
80 - with reference count.
81 - with rwlock neigh->lock
82
83 Reference count prevents destruction.
84
85 neigh->lock mainly serializes ll address data and its validity state.
86 However, the same lock is used to protect another entry fields:
87 - timer
88 - resolution queue
89
90 Again, nothing clever shall be made under neigh->lock,
91 the most complicated procedure, which we allow is dev->hard_header.
92 It is supposed, that dev->hard_header is simplistic and does
93 not make callbacks to neighbour tables.
94
95 The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
96 list of neighbour tables. This list is used only in process context,
97 */
98
99static DEFINE_RWLOCK(neigh_tbl_lock);
100
8f40b161 101static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
1da177e4
LT
102{
103 kfree_skb(skb);
104 return -ENETDOWN;
105}
106
4f494554
TG
107static void neigh_cleanup_and_release(struct neighbour *neigh)
108{
109 if (neigh->parms->neigh_cleanup)
110 neigh->parms->neigh_cleanup(neigh);
111
d961db35 112 __neigh_notify(neigh, RTM_DELNEIGH, 0);
4f494554
TG
113 neigh_release(neigh);
114}
115
1da177e4
LT
116/*
117 * It is random distribution in the interval (1/2)*base...(3/2)*base.
118 * It corresponds to default IPv6 settings and is not overridable,
119 * because it is really reasonable choice.
120 */
121
122unsigned long neigh_rand_reach_time(unsigned long base)
123{
a02cec21 124 return base ? (net_random() % base) + (base >> 1) : 0;
1da177e4 125}
0a204500 126EXPORT_SYMBOL(neigh_rand_reach_time);
1da177e4
LT
127
128
129static int neigh_forced_gc(struct neigh_table *tbl)
130{
131 int shrunk = 0;
132 int i;
d6bf7817 133 struct neigh_hash_table *nht;
1da177e4
LT
134
135 NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
136
137 write_lock_bh(&tbl->lock);
d6bf7817
ED
138 nht = rcu_dereference_protected(tbl->nht,
139 lockdep_is_held(&tbl->lock));
cd089336 140 for (i = 0; i < (1 << nht->hash_shift); i++) {
767e97e1
ED
141 struct neighbour *n;
142 struct neighbour __rcu **np;
1da177e4 143
d6bf7817 144 np = &nht->hash_buckets[i];
767e97e1
ED
145 while ((n = rcu_dereference_protected(*np,
146 lockdep_is_held(&tbl->lock))) != NULL) {
1da177e4
LT
147 /* Neighbour record may be discarded if:
148 * - nobody refers to it.
149 * - it is not permanent
150 */
151 write_lock(&n->lock);
152 if (atomic_read(&n->refcnt) == 1 &&
153 !(n->nud_state & NUD_PERMANENT)) {
767e97e1
ED
154 rcu_assign_pointer(*np,
155 rcu_dereference_protected(n->next,
156 lockdep_is_held(&tbl->lock)));
1da177e4
LT
157 n->dead = 1;
158 shrunk = 1;
159 write_unlock(&n->lock);
4f494554 160 neigh_cleanup_and_release(n);
1da177e4
LT
161 continue;
162 }
163 write_unlock(&n->lock);
164 np = &n->next;
165 }
166 }
167
168 tbl->last_flush = jiffies;
169
170 write_unlock_bh(&tbl->lock);
171
172 return shrunk;
173}
174
a43d8994
PE
175static void neigh_add_timer(struct neighbour *n, unsigned long when)
176{
177 neigh_hold(n);
178 if (unlikely(mod_timer(&n->timer, when))) {
179 printk("NEIGH: BUG, double timer add, state is %x\n",
180 n->nud_state);
181 dump_stack();
182 }
183}
184
1da177e4
LT
185static int neigh_del_timer(struct neighbour *n)
186{
187 if ((n->nud_state & NUD_IN_TIMER) &&
188 del_timer(&n->timer)) {
189 neigh_release(n);
190 return 1;
191 }
192 return 0;
193}
194
195static void pneigh_queue_purge(struct sk_buff_head *list)
196{
197 struct sk_buff *skb;
198
199 while ((skb = skb_dequeue(list)) != NULL) {
200 dev_put(skb->dev);
201 kfree_skb(skb);
202 }
203}
204
49636bb1 205static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
1da177e4
LT
206{
207 int i;
d6bf7817 208 struct neigh_hash_table *nht;
1da177e4 209
d6bf7817
ED
210 nht = rcu_dereference_protected(tbl->nht,
211 lockdep_is_held(&tbl->lock));
212
cd089336 213 for (i = 0; i < (1 << nht->hash_shift); i++) {
767e97e1
ED
214 struct neighbour *n;
215 struct neighbour __rcu **np = &nht->hash_buckets[i];
1da177e4 216
767e97e1
ED
217 while ((n = rcu_dereference_protected(*np,
218 lockdep_is_held(&tbl->lock))) != NULL) {
1da177e4
LT
219 if (dev && n->dev != dev) {
220 np = &n->next;
221 continue;
222 }
767e97e1
ED
223 rcu_assign_pointer(*np,
224 rcu_dereference_protected(n->next,
225 lockdep_is_held(&tbl->lock)));
1da177e4
LT
226 write_lock(&n->lock);
227 neigh_del_timer(n);
228 n->dead = 1;
229
230 if (atomic_read(&n->refcnt) != 1) {
231 /* The most unpleasant situation.
232 We must destroy neighbour entry,
233 but someone still uses it.
234
235 The destroy will be delayed until
236 the last user releases us, but
237 we must kill timers etc. and move
238 it to safe state.
239 */
240 skb_queue_purge(&n->arp_queue);
8b5c171b 241 n->arp_queue_len_bytes = 0;
1da177e4
LT
242 n->output = neigh_blackhole;
243 if (n->nud_state & NUD_VALID)
244 n->nud_state = NUD_NOARP;
245 else
246 n->nud_state = NUD_NONE;
247 NEIGH_PRINTK2("neigh %p is stray.\n", n);
248 }
249 write_unlock(&n->lock);
4f494554 250 neigh_cleanup_and_release(n);
1da177e4
LT
251 }
252 }
49636bb1 253}
1da177e4 254
49636bb1
HX
255void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
256{
257 write_lock_bh(&tbl->lock);
258 neigh_flush_dev(tbl, dev);
259 write_unlock_bh(&tbl->lock);
260}
0a204500 261EXPORT_SYMBOL(neigh_changeaddr);
49636bb1
HX
262
263int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
264{
265 write_lock_bh(&tbl->lock);
266 neigh_flush_dev(tbl, dev);
1da177e4
LT
267 pneigh_ifdown(tbl, dev);
268 write_unlock_bh(&tbl->lock);
269
270 del_timer_sync(&tbl->proxy_timer);
271 pneigh_queue_purge(&tbl->proxy_queue);
272 return 0;
273}
0a204500 274EXPORT_SYMBOL(neigh_ifdown);
1da177e4 275
596b9b68 276static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
1da177e4
LT
277{
278 struct neighbour *n = NULL;
279 unsigned long now = jiffies;
280 int entries;
281
282 entries = atomic_inc_return(&tbl->entries) - 1;
283 if (entries >= tbl->gc_thresh3 ||
284 (entries >= tbl->gc_thresh2 &&
285 time_after(now, tbl->last_flush + 5 * HZ))) {
286 if (!neigh_forced_gc(tbl) &&
287 entries >= tbl->gc_thresh3)
288 goto out_entries;
289 }
290
596b9b68
DM
291 if (tbl->entry_size)
292 n = kzalloc(tbl->entry_size, GFP_ATOMIC);
293 else {
294 int sz = sizeof(*n) + tbl->key_len;
295
296 sz = ALIGN(sz, NEIGH_PRIV_ALIGN);
297 sz += dev->neigh_priv_len;
298 n = kzalloc(sz, GFP_ATOMIC);
299 }
1da177e4
LT
300 if (!n)
301 goto out_entries;
302
1da177e4
LT
303 skb_queue_head_init(&n->arp_queue);
304 rwlock_init(&n->lock);
0ed8ddf4 305 seqlock_init(&n->ha_lock);
1da177e4
LT
306 n->updated = n->used = now;
307 n->nud_state = NUD_NONE;
308 n->output = neigh_blackhole;
f6b72b62 309 seqlock_init(&n->hh.hh_lock);
1da177e4 310 n->parms = neigh_parms_clone(&tbl->parms);
b24b8a24 311 setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
1da177e4
LT
312
313 NEIGH_CACHE_STAT_INC(tbl, allocs);
314 n->tbl = tbl;
315 atomic_set(&n->refcnt, 1);
316 n->dead = 1;
317out:
318 return n;
319
320out_entries:
321 atomic_dec(&tbl->entries);
322 goto out;
323}
324
cd089336 325static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
1da177e4 326{
cd089336 327 size_t size = (1 << shift) * sizeof(struct neighbour *);
d6bf7817 328 struct neigh_hash_table *ret;
6193d2be 329 struct neighbour __rcu **buckets;
1da177e4 330
d6bf7817
ED
331 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
332 if (!ret)
333 return NULL;
334 if (size <= PAGE_SIZE)
335 buckets = kzalloc(size, GFP_ATOMIC);
336 else
6193d2be 337 buckets = (struct neighbour __rcu **)
d6bf7817
ED
338 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
339 get_order(size));
340 if (!buckets) {
341 kfree(ret);
342 return NULL;
1da177e4 343 }
6193d2be 344 ret->hash_buckets = buckets;
cd089336 345 ret->hash_shift = shift;
d6bf7817 346 get_random_bytes(&ret->hash_rnd, sizeof(ret->hash_rnd));
f610b74b 347 ret->hash_rnd |= 1;
1da177e4
LT
348 return ret;
349}
350
d6bf7817 351static void neigh_hash_free_rcu(struct rcu_head *head)
1da177e4 352{
d6bf7817
ED
353 struct neigh_hash_table *nht = container_of(head,
354 struct neigh_hash_table,
355 rcu);
cd089336 356 size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
6193d2be 357 struct neighbour __rcu **buckets = nht->hash_buckets;
1da177e4
LT
358
359 if (size <= PAGE_SIZE)
d6bf7817 360 kfree(buckets);
1da177e4 361 else
d6bf7817
ED
362 free_pages((unsigned long)buckets, get_order(size));
363 kfree(nht);
1da177e4
LT
364}
365
d6bf7817 366static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
cd089336 367 unsigned long new_shift)
1da177e4 368{
d6bf7817
ED
369 unsigned int i, hash;
370 struct neigh_hash_table *new_nht, *old_nht;
1da177e4
LT
371
372 NEIGH_CACHE_STAT_INC(tbl, hash_grows);
373
d6bf7817
ED
374 old_nht = rcu_dereference_protected(tbl->nht,
375 lockdep_is_held(&tbl->lock));
cd089336 376 new_nht = neigh_hash_alloc(new_shift);
d6bf7817
ED
377 if (!new_nht)
378 return old_nht;
1da177e4 379
cd089336 380 for (i = 0; i < (1 << old_nht->hash_shift); i++) {
1da177e4
LT
381 struct neighbour *n, *next;
382
767e97e1
ED
383 for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
384 lockdep_is_held(&tbl->lock));
d6bf7817
ED
385 n != NULL;
386 n = next) {
387 hash = tbl->hash(n->primary_key, n->dev,
388 new_nht->hash_rnd);
1da177e4 389
cd089336 390 hash >>= (32 - new_nht->hash_shift);
767e97e1
ED
391 next = rcu_dereference_protected(n->next,
392 lockdep_is_held(&tbl->lock));
393
394 rcu_assign_pointer(n->next,
395 rcu_dereference_protected(
396 new_nht->hash_buckets[hash],
397 lockdep_is_held(&tbl->lock)));
398 rcu_assign_pointer(new_nht->hash_buckets[hash], n);
1da177e4
LT
399 }
400 }
1da177e4 401
d6bf7817
ED
402 rcu_assign_pointer(tbl->nht, new_nht);
403 call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
404 return new_nht;
1da177e4
LT
405}
406
407struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
408 struct net_device *dev)
409{
410 struct neighbour *n;
411 int key_len = tbl->key_len;
bc4bf5f3 412 u32 hash_val;
d6bf7817 413 struct neigh_hash_table *nht;
4ec93edb 414
1da177e4
LT
415 NEIGH_CACHE_STAT_INC(tbl, lookups);
416
d6bf7817
ED
417 rcu_read_lock_bh();
418 nht = rcu_dereference_bh(tbl->nht);
cd089336 419 hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
767e97e1
ED
420
421 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
422 n != NULL;
423 n = rcu_dereference_bh(n->next)) {
1da177e4 424 if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
767e97e1
ED
425 if (!atomic_inc_not_zero(&n->refcnt))
426 n = NULL;
1da177e4
LT
427 NEIGH_CACHE_STAT_INC(tbl, hits);
428 break;
429 }
430 }
767e97e1 431
d6bf7817 432 rcu_read_unlock_bh();
1da177e4
LT
433 return n;
434}
0a204500 435EXPORT_SYMBOL(neigh_lookup);
1da177e4 436
426b5303
EB
437struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
438 const void *pkey)
1da177e4
LT
439{
440 struct neighbour *n;
441 int key_len = tbl->key_len;
bc4bf5f3 442 u32 hash_val;
d6bf7817 443 struct neigh_hash_table *nht;
1da177e4
LT
444
445 NEIGH_CACHE_STAT_INC(tbl, lookups);
446
d6bf7817
ED
447 rcu_read_lock_bh();
448 nht = rcu_dereference_bh(tbl->nht);
cd089336 449 hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
767e97e1
ED
450
451 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
452 n != NULL;
453 n = rcu_dereference_bh(n->next)) {
426b5303 454 if (!memcmp(n->primary_key, pkey, key_len) &&
878628fb 455 net_eq(dev_net(n->dev), net)) {
767e97e1
ED
456 if (!atomic_inc_not_zero(&n->refcnt))
457 n = NULL;
1da177e4
LT
458 NEIGH_CACHE_STAT_INC(tbl, hits);
459 break;
460 }
461 }
767e97e1 462
d6bf7817 463 rcu_read_unlock_bh();
1da177e4
LT
464 return n;
465}
0a204500 466EXPORT_SYMBOL(neigh_lookup_nodev);
1da177e4
LT
467
468struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
469 struct net_device *dev)
470{
471 u32 hash_val;
472 int key_len = tbl->key_len;
473 int error;
596b9b68 474 struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
d6bf7817 475 struct neigh_hash_table *nht;
1da177e4
LT
476
477 if (!n) {
478 rc = ERR_PTR(-ENOBUFS);
479 goto out;
480 }
481
482 memcpy(n->primary_key, pkey, key_len);
483 n->dev = dev;
484 dev_hold(dev);
485
486 /* Protocol specific setup. */
487 if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
488 rc = ERR_PTR(error);
489 goto out_neigh_release;
490 }
491
da6a8fa0
DM
492 if (dev->netdev_ops->ndo_neigh_construct) {
493 error = dev->netdev_ops->ndo_neigh_construct(n);
494 if (error < 0) {
495 rc = ERR_PTR(error);
496 goto out_neigh_release;
497 }
498 }
499
1da177e4
LT
500 n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
501
502 write_lock_bh(&tbl->lock);
d6bf7817
ED
503 nht = rcu_dereference_protected(tbl->nht,
504 lockdep_is_held(&tbl->lock));
1da177e4 505
cd089336
DM
506 if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
507 nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
1da177e4 508
cd089336 509 hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
1da177e4
LT
510
511 if (n->parms->dead) {
512 rc = ERR_PTR(-EINVAL);
513 goto out_tbl_unlock;
514 }
515
767e97e1
ED
516 for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
517 lockdep_is_held(&tbl->lock));
518 n1 != NULL;
519 n1 = rcu_dereference_protected(n1->next,
520 lockdep_is_held(&tbl->lock))) {
1da177e4
LT
521 if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
522 neigh_hold(n1);
523 rc = n1;
524 goto out_tbl_unlock;
525 }
526 }
527
1da177e4
LT
528 n->dead = 0;
529 neigh_hold(n);
767e97e1
ED
530 rcu_assign_pointer(n->next,
531 rcu_dereference_protected(nht->hash_buckets[hash_val],
532 lockdep_is_held(&tbl->lock)));
533 rcu_assign_pointer(nht->hash_buckets[hash_val], n);
1da177e4
LT
534 write_unlock_bh(&tbl->lock);
535 NEIGH_PRINTK2("neigh %p is created.\n", n);
536 rc = n;
537out:
538 return rc;
539out_tbl_unlock:
540 write_unlock_bh(&tbl->lock);
541out_neigh_release:
542 neigh_release(n);
543 goto out;
544}
0a204500 545EXPORT_SYMBOL(neigh_create);
1da177e4 546
be01d655 547static u32 pneigh_hash(const void *pkey, int key_len)
fa86d322 548{
fa86d322 549 u32 hash_val = *(u32 *)(pkey + key_len - 4);
fa86d322
PE
550 hash_val ^= (hash_val >> 16);
551 hash_val ^= hash_val >> 8;
552 hash_val ^= hash_val >> 4;
553 hash_val &= PNEIGH_HASHMASK;
be01d655
YH
554 return hash_val;
555}
fa86d322 556
be01d655
YH
557static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
558 struct net *net,
559 const void *pkey,
560 int key_len,
561 struct net_device *dev)
562{
563 while (n) {
fa86d322 564 if (!memcmp(n->key, pkey, key_len) &&
be01d655 565 net_eq(pneigh_net(n), net) &&
fa86d322 566 (n->dev == dev || !n->dev))
be01d655
YH
567 return n;
568 n = n->next;
fa86d322 569 }
be01d655
YH
570 return NULL;
571}
fa86d322 572
be01d655
YH
573struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
574 struct net *net, const void *pkey, struct net_device *dev)
575{
576 int key_len = tbl->key_len;
577 u32 hash_val = pneigh_hash(pkey, key_len);
578
579 return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
580 net, pkey, key_len, dev);
fa86d322 581}
0a204500 582EXPORT_SYMBOL_GPL(__pneigh_lookup);
fa86d322 583
426b5303
EB
584struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
585 struct net *net, const void *pkey,
1da177e4
LT
586 struct net_device *dev, int creat)
587{
588 struct pneigh_entry *n;
589 int key_len = tbl->key_len;
be01d655 590 u32 hash_val = pneigh_hash(pkey, key_len);
1da177e4
LT
591
592 read_lock_bh(&tbl->lock);
be01d655
YH
593 n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
594 net, pkey, key_len, dev);
1da177e4 595 read_unlock_bh(&tbl->lock);
be01d655
YH
596
597 if (n || !creat)
1da177e4
LT
598 goto out;
599
4ae28944
PE
600 ASSERT_RTNL();
601
1da177e4
LT
602 n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
603 if (!n)
604 goto out;
605
e42ea986 606 write_pnet(&n->net, hold_net(net));
1da177e4
LT
607 memcpy(n->key, pkey, key_len);
608 n->dev = dev;
609 if (dev)
610 dev_hold(dev);
611
612 if (tbl->pconstructor && tbl->pconstructor(n)) {
613 if (dev)
614 dev_put(dev);
da12f735 615 release_net(net);
1da177e4
LT
616 kfree(n);
617 n = NULL;
618 goto out;
619 }
620
621 write_lock_bh(&tbl->lock);
622 n->next = tbl->phash_buckets[hash_val];
623 tbl->phash_buckets[hash_val] = n;
624 write_unlock_bh(&tbl->lock);
625out:
626 return n;
627}
0a204500 628EXPORT_SYMBOL(pneigh_lookup);
1da177e4
LT
629
630
426b5303 631int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
1da177e4
LT
632 struct net_device *dev)
633{
634 struct pneigh_entry *n, **np;
635 int key_len = tbl->key_len;
be01d655 636 u32 hash_val = pneigh_hash(pkey, key_len);
1da177e4
LT
637
638 write_lock_bh(&tbl->lock);
639 for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
640 np = &n->next) {
426b5303 641 if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
878628fb 642 net_eq(pneigh_net(n), net)) {
1da177e4
LT
643 *np = n->next;
644 write_unlock_bh(&tbl->lock);
645 if (tbl->pdestructor)
646 tbl->pdestructor(n);
647 if (n->dev)
648 dev_put(n->dev);
57da52c1 649 release_net(pneigh_net(n));
1da177e4
LT
650 kfree(n);
651 return 0;
652 }
653 }
654 write_unlock_bh(&tbl->lock);
655 return -ENOENT;
656}
657
658static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
659{
660 struct pneigh_entry *n, **np;
661 u32 h;
662
663 for (h = 0; h <= PNEIGH_HASHMASK; h++) {
664 np = &tbl->phash_buckets[h];
665 while ((n = *np) != NULL) {
666 if (!dev || n->dev == dev) {
667 *np = n->next;
668 if (tbl->pdestructor)
669 tbl->pdestructor(n);
670 if (n->dev)
671 dev_put(n->dev);
57da52c1 672 release_net(pneigh_net(n));
1da177e4
LT
673 kfree(n);
674 continue;
675 }
676 np = &n->next;
677 }
678 }
679 return -ENOENT;
680}
681
06f0511d
DL
682static void neigh_parms_destroy(struct neigh_parms *parms);
683
684static inline void neigh_parms_put(struct neigh_parms *parms)
685{
686 if (atomic_dec_and_test(&parms->refcnt))
687 neigh_parms_destroy(parms);
688}
1da177e4
LT
689
690/*
691 * neighbour must already be out of the table;
692 *
693 */
694void neigh_destroy(struct neighbour *neigh)
695{
da6a8fa0
DM
696 struct net_device *dev = neigh->dev;
697
1da177e4
LT
698 NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
699
700 if (!neigh->dead) {
701 printk(KERN_WARNING
702 "Destroying alive neighbour %p\n", neigh);
703 dump_stack();
704 return;
705 }
706
707 if (neigh_del_timer(neigh))
708 printk(KERN_WARNING "Impossible event.\n");
709
1da177e4 710 skb_queue_purge(&neigh->arp_queue);
8b5c171b 711 neigh->arp_queue_len_bytes = 0;
1da177e4 712
da6a8fa0 713 dev_put(dev);
1da177e4
LT
714 neigh_parms_put(neigh->parms);
715
716 NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
717
718 atomic_dec(&neigh->tbl->entries);
5b8b0060 719 kfree_rcu(neigh, rcu);
1da177e4 720}
0a204500 721EXPORT_SYMBOL(neigh_destroy);
1da177e4
LT
722
723/* Neighbour state is suspicious;
724 disable fast path.
725
726 Called with write_locked neigh.
727 */
728static void neigh_suspect(struct neighbour *neigh)
729{
1da177e4
LT
730 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
731
732 neigh->output = neigh->ops->output;
1da177e4
LT
733}
734
735/* Neighbour state is OK;
736 enable fast path.
737
738 Called with write_locked neigh.
739 */
740static void neigh_connect(struct neighbour *neigh)
741{
1da177e4
LT
742 NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
743
744 neigh->output = neigh->ops->connected_output;
1da177e4
LT
745}
746
e4c4e448 747static void neigh_periodic_work(struct work_struct *work)
1da177e4 748{
e4c4e448 749 struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
767e97e1
ED
750 struct neighbour *n;
751 struct neighbour __rcu **np;
e4c4e448 752 unsigned int i;
d6bf7817 753 struct neigh_hash_table *nht;
1da177e4
LT
754
755 NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
756
e4c4e448 757 write_lock_bh(&tbl->lock);
d6bf7817
ED
758 nht = rcu_dereference_protected(tbl->nht,
759 lockdep_is_held(&tbl->lock));
1da177e4
LT
760
761 /*
762 * periodically recompute ReachableTime from random function
763 */
764
e4c4e448 765 if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
1da177e4 766 struct neigh_parms *p;
e4c4e448 767 tbl->last_rand = jiffies;
1da177e4
LT
768 for (p = &tbl->parms; p; p = p->next)
769 p->reachable_time =
770 neigh_rand_reach_time(p->base_reachable_time);
771 }
772
cd089336 773 for (i = 0 ; i < (1 << nht->hash_shift); i++) {
d6bf7817 774 np = &nht->hash_buckets[i];
1da177e4 775
767e97e1
ED
776 while ((n = rcu_dereference_protected(*np,
777 lockdep_is_held(&tbl->lock))) != NULL) {
e4c4e448 778 unsigned int state;
1da177e4 779
e4c4e448 780 write_lock(&n->lock);
1da177e4 781
e4c4e448
ED
782 state = n->nud_state;
783 if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
784 write_unlock(&n->lock);
785 goto next_elt;
786 }
1da177e4 787
e4c4e448
ED
788 if (time_before(n->used, n->confirmed))
789 n->used = n->confirmed;
1da177e4 790
e4c4e448
ED
791 if (atomic_read(&n->refcnt) == 1 &&
792 (state == NUD_FAILED ||
793 time_after(jiffies, n->used + n->parms->gc_staletime))) {
794 *np = n->next;
795 n->dead = 1;
796 write_unlock(&n->lock);
797 neigh_cleanup_and_release(n);
798 continue;
799 }
1da177e4 800 write_unlock(&n->lock);
1da177e4
LT
801
802next_elt:
e4c4e448
ED
803 np = &n->next;
804 }
805 /*
806 * It's fine to release lock here, even if hash table
807 * grows while we are preempted.
808 */
809 write_unlock_bh(&tbl->lock);
810 cond_resched();
811 write_lock_bh(&tbl->lock);
1da177e4 812 }
4ec93edb
YH
813 /* Cycle through all hash buckets every base_reachable_time/2 ticks.
814 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
815 * base_reachable_time.
1da177e4 816 */
e4c4e448
ED
817 schedule_delayed_work(&tbl->gc_work,
818 tbl->parms.base_reachable_time >> 1);
819 write_unlock_bh(&tbl->lock);
1da177e4
LT
820}
821
822static __inline__ int neigh_max_probes(struct neighbour *n)
823{
824 struct neigh_parms *p = n->parms;
a02cec21 825 return (n->nud_state & NUD_PROBE) ?
1da177e4 826 p->ucast_probes :
a02cec21 827 p->ucast_probes + p->app_probes + p->mcast_probes;
1da177e4
LT
828}
829
5ef12d98 830static void neigh_invalidate(struct neighbour *neigh)
0a141509
ED
831 __releases(neigh->lock)
832 __acquires(neigh->lock)
5ef12d98
TT
833{
834 struct sk_buff *skb;
835
836 NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
837 NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
838 neigh->updated = jiffies;
839
840 /* It is very thin place. report_unreachable is very complicated
841 routine. Particularly, it can hit the same neighbour entry!
842
843 So that, we try to be accurate and avoid dead loop. --ANK
844 */
845 while (neigh->nud_state == NUD_FAILED &&
846 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
847 write_unlock(&neigh->lock);
848 neigh->ops->error_report(neigh, skb);
849 write_lock(&neigh->lock);
850 }
851 skb_queue_purge(&neigh->arp_queue);
8b5c171b 852 neigh->arp_queue_len_bytes = 0;
5ef12d98
TT
853}
854
cd28ca0a
ED
855static void neigh_probe(struct neighbour *neigh)
856 __releases(neigh->lock)
857{
858 struct sk_buff *skb = skb_peek(&neigh->arp_queue);
859 /* keep skb alive even if arp_queue overflows */
860 if (skb)
861 skb = skb_copy(skb, GFP_ATOMIC);
862 write_unlock(&neigh->lock);
863 neigh->ops->solicit(neigh, skb);
864 atomic_inc(&neigh->probes);
865 kfree_skb(skb);
866}
867
1da177e4
LT
868/* Called when a timer expires for a neighbour entry. */
869
870static void neigh_timer_handler(unsigned long arg)
871{
872 unsigned long now, next;
873 struct neighbour *neigh = (struct neighbour *)arg;
874 unsigned state;
875 int notify = 0;
876
877 write_lock(&neigh->lock);
878
879 state = neigh->nud_state;
880 now = jiffies;
881 next = now + HZ;
882
045f7b3b 883 if (!(state & NUD_IN_TIMER))
1da177e4 884 goto out;
1da177e4
LT
885
886 if (state & NUD_REACHABLE) {
4ec93edb 887 if (time_before_eq(now,
1da177e4
LT
888 neigh->confirmed + neigh->parms->reachable_time)) {
889 NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
890 next = neigh->confirmed + neigh->parms->reachable_time;
891 } else if (time_before_eq(now,
892 neigh->used + neigh->parms->delay_probe_time)) {
893 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
894 neigh->nud_state = NUD_DELAY;
955aaa2f 895 neigh->updated = jiffies;
1da177e4
LT
896 neigh_suspect(neigh);
897 next = now + neigh->parms->delay_probe_time;
898 } else {
899 NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
900 neigh->nud_state = NUD_STALE;
955aaa2f 901 neigh->updated = jiffies;
1da177e4 902 neigh_suspect(neigh);
8d71740c 903 notify = 1;
1da177e4
LT
904 }
905 } else if (state & NUD_DELAY) {
4ec93edb 906 if (time_before_eq(now,
1da177e4
LT
907 neigh->confirmed + neigh->parms->delay_probe_time)) {
908 NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh);
909 neigh->nud_state = NUD_REACHABLE;
955aaa2f 910 neigh->updated = jiffies;
1da177e4 911 neigh_connect(neigh);
8d71740c 912 notify = 1;
1da177e4
LT
913 next = neigh->confirmed + neigh->parms->reachable_time;
914 } else {
915 NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
916 neigh->nud_state = NUD_PROBE;
955aaa2f 917 neigh->updated = jiffies;
1da177e4
LT
918 atomic_set(&neigh->probes, 0);
919 next = now + neigh->parms->retrans_time;
920 }
921 } else {
922 /* NUD_PROBE|NUD_INCOMPLETE */
923 next = now + neigh->parms->retrans_time;
924 }
925
926 if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
927 atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
1da177e4
LT
928 neigh->nud_state = NUD_FAILED;
929 notify = 1;
5ef12d98 930 neigh_invalidate(neigh);
1da177e4
LT
931 }
932
933 if (neigh->nud_state & NUD_IN_TIMER) {
1da177e4
LT
934 if (time_before(next, jiffies + HZ/2))
935 next = jiffies + HZ/2;
6fb9974f
HX
936 if (!mod_timer(&neigh->timer, next))
937 neigh_hold(neigh);
1da177e4
LT
938 }
939 if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
cd28ca0a 940 neigh_probe(neigh);
9ff56607 941 } else {
69cc64d8 942out:
9ff56607
DM
943 write_unlock(&neigh->lock);
944 }
d961db35 945
8d71740c 946 if (notify)
d961db35 947 neigh_update_notify(neigh);
1da177e4 948
1da177e4
LT
949 neigh_release(neigh);
950}
951
952int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
953{
954 int rc;
cd28ca0a 955 bool immediate_probe = false;
1da177e4
LT
956
957 write_lock_bh(&neigh->lock);
958
959 rc = 0;
960 if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
961 goto out_unlock_bh;
962
1da177e4
LT
963 if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
964 if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
cd28ca0a
ED
965 unsigned long next, now = jiffies;
966
1da177e4
LT
967 atomic_set(&neigh->probes, neigh->parms->ucast_probes);
968 neigh->nud_state = NUD_INCOMPLETE;
cd28ca0a
ED
969 neigh->updated = now;
970 next = now + max(neigh->parms->retrans_time, HZ/2);
971 neigh_add_timer(neigh, next);
972 immediate_probe = true;
1da177e4
LT
973 } else {
974 neigh->nud_state = NUD_FAILED;
955aaa2f 975 neigh->updated = jiffies;
1da177e4
LT
976 write_unlock_bh(&neigh->lock);
977
f3fbbe0f 978 kfree_skb(skb);
1da177e4
LT
979 return 1;
980 }
981 } else if (neigh->nud_state & NUD_STALE) {
982 NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
1da177e4 983 neigh->nud_state = NUD_DELAY;
955aaa2f 984 neigh->updated = jiffies;
667347f1
DM
985 neigh_add_timer(neigh,
986 jiffies + neigh->parms->delay_probe_time);
1da177e4
LT
987 }
988
989 if (neigh->nud_state == NUD_INCOMPLETE) {
990 if (skb) {
8b5c171b
ED
991 while (neigh->arp_queue_len_bytes + skb->truesize >
992 neigh->parms->queue_len_bytes) {
1da177e4 993 struct sk_buff *buff;
8b5c171b 994
f72051b0 995 buff = __skb_dequeue(&neigh->arp_queue);
8b5c171b
ED
996 if (!buff)
997 break;
998 neigh->arp_queue_len_bytes -= buff->truesize;
1da177e4 999 kfree_skb(buff);
9a6d276e 1000 NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
1da177e4 1001 }
a4731138 1002 skb_dst_force(skb);
1da177e4 1003 __skb_queue_tail(&neigh->arp_queue, skb);
8b5c171b 1004 neigh->arp_queue_len_bytes += skb->truesize;
1da177e4
LT
1005 }
1006 rc = 1;
1007 }
1008out_unlock_bh:
cd28ca0a
ED
1009 if (immediate_probe)
1010 neigh_probe(neigh);
1011 else
1012 write_unlock(&neigh->lock);
1013 local_bh_enable();
1da177e4
LT
1014 return rc;
1015}
0a204500 1016EXPORT_SYMBOL(__neigh_event_send);
1da177e4 1017
f6b72b62 1018static void neigh_update_hhs(struct neighbour *neigh)
1da177e4
LT
1019{
1020 struct hh_cache *hh;
3b04ddde 1021 void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
91a72a70
DK
1022 = NULL;
1023
1024 if (neigh->dev->header_ops)
1025 update = neigh->dev->header_ops->cache_update;
1da177e4
LT
1026
1027 if (update) {
f6b72b62
DM
1028 hh = &neigh->hh;
1029 if (hh->hh_len) {
3644f0ce 1030 write_seqlock_bh(&hh->hh_lock);
1da177e4 1031 update(hh, neigh->dev, neigh->ha);
3644f0ce 1032 write_sequnlock_bh(&hh->hh_lock);
1da177e4
LT
1033 }
1034 }
1035}
1036
1037
1038
1039/* Generic update routine.
1040 -- lladdr is new lladdr or NULL, if it is not supplied.
1041 -- new is new state.
1042 -- flags
1043 NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
1044 if it is different.
1045 NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
4ec93edb 1046 lladdr instead of overriding it
1da177e4
LT
1047 if it is different.
1048 It also allows to retain current state
1049 if lladdr is unchanged.
1050 NEIGH_UPDATE_F_ADMIN means that the change is administrative.
1051
4ec93edb 1052 NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
1da177e4
LT
1053 NTF_ROUTER flag.
1054 NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
1055 a router.
1056
1057 Caller MUST hold reference count on the entry.
1058 */
1059
1060int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1061 u32 flags)
1062{
1063 u8 old;
1064 int err;
1da177e4 1065 int notify = 0;
1da177e4
LT
1066 struct net_device *dev;
1067 int update_isrouter = 0;
1068
1069 write_lock_bh(&neigh->lock);
1070
1071 dev = neigh->dev;
1072 old = neigh->nud_state;
1073 err = -EPERM;
1074
4ec93edb 1075 if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
1da177e4
LT
1076 (old & (NUD_NOARP | NUD_PERMANENT)))
1077 goto out;
1078
1079 if (!(new & NUD_VALID)) {
1080 neigh_del_timer(neigh);
1081 if (old & NUD_CONNECTED)
1082 neigh_suspect(neigh);
1083 neigh->nud_state = new;
1084 err = 0;
1da177e4 1085 notify = old & NUD_VALID;
5ef12d98
TT
1086 if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
1087 (new & NUD_FAILED)) {
1088 neigh_invalidate(neigh);
1089 notify = 1;
1090 }
1da177e4
LT
1091 goto out;
1092 }
1093
1094 /* Compare new lladdr with cached one */
1095 if (!dev->addr_len) {
1096 /* First case: device needs no address. */
1097 lladdr = neigh->ha;
1098 } else if (lladdr) {
1099 /* The second case: if something is already cached
1100 and a new address is proposed:
1101 - compare new & old
1102 - if they are different, check override flag
1103 */
4ec93edb 1104 if ((old & NUD_VALID) &&
1da177e4
LT
1105 !memcmp(lladdr, neigh->ha, dev->addr_len))
1106 lladdr = neigh->ha;
1107 } else {
1108 /* No address is supplied; if we know something,
1109 use it, otherwise discard the request.
1110 */
1111 err = -EINVAL;
1112 if (!(old & NUD_VALID))
1113 goto out;
1114 lladdr = neigh->ha;
1115 }
1116
1117 if (new & NUD_CONNECTED)
1118 neigh->confirmed = jiffies;
1119 neigh->updated = jiffies;
1120
1121 /* If entry was valid and address is not changed,
1122 do not change entry state, if new one is STALE.
1123 */
1124 err = 0;
1125 update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
1126 if (old & NUD_VALID) {
1127 if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
1128 update_isrouter = 0;
1129 if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
1130 (old & NUD_CONNECTED)) {
1131 lladdr = neigh->ha;
1132 new = NUD_STALE;
1133 } else
1134 goto out;
1135 } else {
1136 if (lladdr == neigh->ha && new == NUD_STALE &&
1137 ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
1138 (old & NUD_CONNECTED))
1139 )
1140 new = old;
1141 }
1142 }
1143
1144 if (new != old) {
1145 neigh_del_timer(neigh);
a43d8994 1146 if (new & NUD_IN_TIMER)
4ec93edb
YH
1147 neigh_add_timer(neigh, (jiffies +
1148 ((new & NUD_REACHABLE) ?
667347f1
DM
1149 neigh->parms->reachable_time :
1150 0)));
1da177e4
LT
1151 neigh->nud_state = new;
1152 }
1153
1154 if (lladdr != neigh->ha) {
0ed8ddf4 1155 write_seqlock(&neigh->ha_lock);
1da177e4 1156 memcpy(&neigh->ha, lladdr, dev->addr_len);
0ed8ddf4 1157 write_sequnlock(&neigh->ha_lock);
1da177e4
LT
1158 neigh_update_hhs(neigh);
1159 if (!(new & NUD_CONNECTED))
1160 neigh->confirmed = jiffies -
1161 (neigh->parms->base_reachable_time << 1);
1da177e4 1162 notify = 1;
1da177e4
LT
1163 }
1164 if (new == old)
1165 goto out;
1166 if (new & NUD_CONNECTED)
1167 neigh_connect(neigh);
1168 else
1169 neigh_suspect(neigh);
1170 if (!(old & NUD_VALID)) {
1171 struct sk_buff *skb;
1172
1173 /* Again: avoid dead loop if something went wrong */
1174
1175 while (neigh->nud_state & NUD_VALID &&
1176 (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
69cce1d1
DM
1177 struct dst_entry *dst = skb_dst(skb);
1178 struct neighbour *n2, *n1 = neigh;
1da177e4 1179 write_unlock_bh(&neigh->lock);
e049f288 1180
1181 rcu_read_lock();
1da177e4 1182 /* On shaper/eql skb->dst->neighbour != neigh :( */
27217455 1183 if (dst && (n2 = dst_get_neighbour_noref(dst)) != NULL)
69cce1d1 1184 n1 = n2;
8f40b161 1185 n1->output(n1, skb);
e049f288 1186 rcu_read_unlock();
1187
1da177e4
LT
1188 write_lock_bh(&neigh->lock);
1189 }
1190 skb_queue_purge(&neigh->arp_queue);
8b5c171b 1191 neigh->arp_queue_len_bytes = 0;
1da177e4
LT
1192 }
1193out:
1194 if (update_isrouter) {
1195 neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
1196 (neigh->flags | NTF_ROUTER) :
1197 (neigh->flags & ~NTF_ROUTER);
1198 }
1199 write_unlock_bh(&neigh->lock);
8d71740c
TT
1200
1201 if (notify)
d961db35
TG
1202 neigh_update_notify(neigh);
1203
1da177e4
LT
1204 return err;
1205}
0a204500 1206EXPORT_SYMBOL(neigh_update);
1da177e4
LT
1207
1208struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1209 u8 *lladdr, void *saddr,
1210 struct net_device *dev)
1211{
1212 struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1213 lladdr || !dev->addr_len);
1214 if (neigh)
4ec93edb 1215 neigh_update(neigh, lladdr, NUD_STALE,
1da177e4
LT
1216 NEIGH_UPDATE_F_OVERRIDE);
1217 return neigh;
1218}
0a204500 1219EXPORT_SYMBOL(neigh_event_ns);
1da177e4 1220
34d101dd 1221/* called with read_lock_bh(&n->lock); */
f6b72b62 1222static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst)
1da177e4 1223{
1da177e4 1224 struct net_device *dev = dst->dev;
f6b72b62
DM
1225 __be16 prot = dst->ops->protocol;
1226 struct hh_cache *hh = &n->hh;
0ed8ddf4
ED
1227
1228 write_lock_bh(&n->lock);
34d101dd 1229
f6b72b62
DM
1230 /* Only one thread can come in here and initialize the
1231 * hh_cache entry.
1232 */
b23b5455
DM
1233 if (!hh->hh_len)
1234 dev->header_ops->cache(n, hh, prot);
34d101dd 1235
0ed8ddf4 1236 write_unlock_bh(&n->lock);
1da177e4
LT
1237}
1238
1239/* This function can be used in contexts, where only old dev_queue_xmit
767e97e1
ED
1240 * worked, f.e. if you want to override normal output path (eql, shaper),
1241 * but resolution is not made yet.
1da177e4
LT
1242 */
1243
8f40b161 1244int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb)
1da177e4
LT
1245{
1246 struct net_device *dev = skb->dev;
1247
bbe735e4 1248 __skb_pull(skb, skb_network_offset(skb));
1da177e4 1249
0c4e8581
SH
1250 if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
1251 skb->len) < 0 &&
3b04ddde 1252 dev->header_ops->rebuild(skb))
1da177e4
LT
1253 return 0;
1254
1255 return dev_queue_xmit(skb);
1256}
0a204500 1257EXPORT_SYMBOL(neigh_compat_output);
1da177e4
LT
1258
1259/* Slow and careful. */
1260
8f40b161 1261int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1da177e4 1262{
adf30907 1263 struct dst_entry *dst = skb_dst(skb);
1da177e4
LT
1264 int rc = 0;
1265
8f40b161 1266 if (!dst)
1da177e4
LT
1267 goto discard;
1268
bbe735e4 1269 __skb_pull(skb, skb_network_offset(skb));
1da177e4
LT
1270
1271 if (!neigh_event_send(neigh, skb)) {
1272 int err;
1273 struct net_device *dev = neigh->dev;
0ed8ddf4 1274 unsigned int seq;
34d101dd 1275
f6b72b62
DM
1276 if (dev->header_ops->cache && !neigh->hh.hh_len)
1277 neigh_hh_init(neigh, dst);
34d101dd 1278
0ed8ddf4
ED
1279 do {
1280 seq = read_seqbegin(&neigh->ha_lock);
1281 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1282 neigh->ha, NULL, skb->len);
1283 } while (read_seqretry(&neigh->ha_lock, seq));
34d101dd 1284
1da177e4 1285 if (err >= 0)
542d4d68 1286 rc = dev_queue_xmit(skb);
1da177e4
LT
1287 else
1288 goto out_kfree_skb;
1289 }
1290out:
1291 return rc;
1292discard:
1293 NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
8f40b161 1294 dst, neigh);
1da177e4
LT
1295out_kfree_skb:
1296 rc = -EINVAL;
1297 kfree_skb(skb);
1298 goto out;
1299}
0a204500 1300EXPORT_SYMBOL(neigh_resolve_output);
1da177e4
LT
1301
1302/* As fast as possible without hh cache */
1303
8f40b161 1304int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
1da177e4 1305{
1da177e4 1306 struct net_device *dev = neigh->dev;
0ed8ddf4 1307 unsigned int seq;
8f40b161 1308 int err;
1da177e4 1309
bbe735e4 1310 __skb_pull(skb, skb_network_offset(skb));
1da177e4 1311
0ed8ddf4
ED
1312 do {
1313 seq = read_seqbegin(&neigh->ha_lock);
1314 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
1315 neigh->ha, NULL, skb->len);
1316 } while (read_seqretry(&neigh->ha_lock, seq));
1317
1da177e4 1318 if (err >= 0)
542d4d68 1319 err = dev_queue_xmit(skb);
1da177e4
LT
1320 else {
1321 err = -EINVAL;
1322 kfree_skb(skb);
1323 }
1324 return err;
1325}
0a204500 1326EXPORT_SYMBOL(neigh_connected_output);
1da177e4 1327
8f40b161
DM
1328int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
1329{
1330 return dev_queue_xmit(skb);
1331}
1332EXPORT_SYMBOL(neigh_direct_output);
1333
1da177e4
LT
1334static void neigh_proxy_process(unsigned long arg)
1335{
1336 struct neigh_table *tbl = (struct neigh_table *)arg;
1337 long sched_next = 0;
1338 unsigned long now = jiffies;
f72051b0 1339 struct sk_buff *skb, *n;
1da177e4
LT
1340
1341 spin_lock(&tbl->proxy_queue.lock);
1342
f72051b0
DM
1343 skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
1344 long tdif = NEIGH_CB(skb)->sched_next - now;
1da177e4 1345
1da177e4 1346 if (tdif <= 0) {
f72051b0 1347 struct net_device *dev = skb->dev;
20e6074e 1348
f72051b0 1349 __skb_unlink(skb, &tbl->proxy_queue);
20e6074e
ED
1350 if (tbl->proxy_redo && netif_running(dev)) {
1351 rcu_read_lock();
f72051b0 1352 tbl->proxy_redo(skb);
20e6074e
ED
1353 rcu_read_unlock();
1354 } else {
f72051b0 1355 kfree_skb(skb);
20e6074e 1356 }
1da177e4
LT
1357
1358 dev_put(dev);
1359 } else if (!sched_next || tdif < sched_next)
1360 sched_next = tdif;
1361 }
1362 del_timer(&tbl->proxy_timer);
1363 if (sched_next)
1364 mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1365 spin_unlock(&tbl->proxy_queue.lock);
1366}
1367
1368void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1369 struct sk_buff *skb)
1370{
1371 unsigned long now = jiffies;
1372 unsigned long sched_next = now + (net_random() % p->proxy_delay);
1373
1374 if (tbl->proxy_queue.qlen > p->proxy_qlen) {
1375 kfree_skb(skb);
1376 return;
1377 }
a61bbcf2
PM
1378
1379 NEIGH_CB(skb)->sched_next = sched_next;
1380 NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1da177e4
LT
1381
1382 spin_lock(&tbl->proxy_queue.lock);
1383 if (del_timer(&tbl->proxy_timer)) {
1384 if (time_before(tbl->proxy_timer.expires, sched_next))
1385 sched_next = tbl->proxy_timer.expires;
1386 }
adf30907 1387 skb_dst_drop(skb);
1da177e4
LT
1388 dev_hold(skb->dev);
1389 __skb_queue_tail(&tbl->proxy_queue, skb);
1390 mod_timer(&tbl->proxy_timer, sched_next);
1391 spin_unlock(&tbl->proxy_queue.lock);
1392}
0a204500 1393EXPORT_SYMBOL(pneigh_enqueue);
1da177e4 1394
97fd5bc7 1395static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
426b5303
EB
1396 struct net *net, int ifindex)
1397{
1398 struct neigh_parms *p;
1399
1400 for (p = &tbl->parms; p; p = p->next) {
878628fb 1401 if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
426b5303
EB
1402 (!p->dev && !ifindex))
1403 return p;
1404 }
1405
1406 return NULL;
1407}
1da177e4
LT
1408
1409struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1410 struct neigh_table *tbl)
1411{
426b5303 1412 struct neigh_parms *p, *ref;
00829823
SH
1413 struct net *net = dev_net(dev);
1414 const struct net_device_ops *ops = dev->netdev_ops;
426b5303 1415
97fd5bc7 1416 ref = lookup_neigh_parms(tbl, net, 0);
426b5303
EB
1417 if (!ref)
1418 return NULL;
1da177e4 1419
426b5303 1420 p = kmemdup(ref, sizeof(*p), GFP_KERNEL);
1da177e4 1421 if (p) {
1da177e4
LT
1422 p->tbl = tbl;
1423 atomic_set(&p->refcnt, 1);
1da177e4
LT
1424 p->reachable_time =
1425 neigh_rand_reach_time(p->base_reachable_time);
c7fb64db 1426
00829823 1427 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
486b51d3
DL
1428 kfree(p);
1429 return NULL;
1da177e4 1430 }
486b51d3
DL
1431
1432 dev_hold(dev);
1433 p->dev = dev;
e42ea986 1434 write_pnet(&p->net, hold_net(net));
1da177e4
LT
1435 p->sysctl_table = NULL;
1436 write_lock_bh(&tbl->lock);
1437 p->next = tbl->parms.next;
1438 tbl->parms.next = p;
1439 write_unlock_bh(&tbl->lock);
1440 }
1441 return p;
1442}
0a204500 1443EXPORT_SYMBOL(neigh_parms_alloc);
1da177e4
LT
1444
1445static void neigh_rcu_free_parms(struct rcu_head *head)
1446{
1447 struct neigh_parms *parms =
1448 container_of(head, struct neigh_parms, rcu_head);
1449
1450 neigh_parms_put(parms);
1451}
1452
1453void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1454{
1455 struct neigh_parms **p;
1456
1457 if (!parms || parms == &tbl->parms)
1458 return;
1459 write_lock_bh(&tbl->lock);
1460 for (p = &tbl->parms.next; *p; p = &(*p)->next) {
1461 if (*p == parms) {
1462 *p = parms->next;
1463 parms->dead = 1;
1464 write_unlock_bh(&tbl->lock);
cecbb639
DM
1465 if (parms->dev)
1466 dev_put(parms->dev);
1da177e4
LT
1467 call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1468 return;
1469 }
1470 }
1471 write_unlock_bh(&tbl->lock);
1472 NEIGH_PRINTK1("neigh_parms_release: not found\n");
1473}
0a204500 1474EXPORT_SYMBOL(neigh_parms_release);
1da177e4 1475
06f0511d 1476static void neigh_parms_destroy(struct neigh_parms *parms)
1da177e4 1477{
57da52c1 1478 release_net(neigh_parms_net(parms));
1da177e4
LT
1479 kfree(parms);
1480}
1481
c2ecba71
PE
1482static struct lock_class_key neigh_table_proxy_queue_class;
1483
bd89efc5 1484void neigh_table_init_no_netlink(struct neigh_table *tbl)
1da177e4
LT
1485{
1486 unsigned long now = jiffies;
1487 unsigned long phsize;
1488
e42ea986 1489 write_pnet(&tbl->parms.net, &init_net);
1da177e4 1490 atomic_set(&tbl->parms.refcnt, 1);
1da177e4
LT
1491 tbl->parms.reachable_time =
1492 neigh_rand_reach_time(tbl->parms.base_reachable_time);
1493
1da177e4
LT
1494 tbl->stats = alloc_percpu(struct neigh_statistics);
1495 if (!tbl->stats)
1496 panic("cannot create neighbour cache statistics");
4ec93edb 1497
1da177e4 1498#ifdef CONFIG_PROC_FS
9b739ba5
AD
1499 if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
1500 &neigh_stat_seq_fops, tbl))
1da177e4 1501 panic("cannot create neighbour proc dir entry");
1da177e4
LT
1502#endif
1503
cd089336 1504 RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
1da177e4
LT
1505
1506 phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
77d04bd9 1507 tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
1da177e4 1508
d6bf7817 1509 if (!tbl->nht || !tbl->phash_buckets)
1da177e4
LT
1510 panic("cannot allocate neighbour cache hashes");
1511
1da177e4 1512 rwlock_init(&tbl->lock);
e4c4e448
ED
1513 INIT_DELAYED_WORK_DEFERRABLE(&tbl->gc_work, neigh_periodic_work);
1514 schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time);
b24b8a24 1515 setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
c2ecba71
PE
1516 skb_queue_head_init_class(&tbl->proxy_queue,
1517 &neigh_table_proxy_queue_class);
1da177e4
LT
1518
1519 tbl->last_flush = now;
1520 tbl->last_rand = now + tbl->parms.reachable_time * 20;
bd89efc5 1521}
0a204500 1522EXPORT_SYMBOL(neigh_table_init_no_netlink);
bd89efc5
SK
1523
1524void neigh_table_init(struct neigh_table *tbl)
1525{
1526 struct neigh_table *tmp;
1527
1528 neigh_table_init_no_netlink(tbl);
1da177e4 1529 write_lock(&neigh_tbl_lock);
bd89efc5
SK
1530 for (tmp = neigh_tables; tmp; tmp = tmp->next) {
1531 if (tmp->family == tbl->family)
1532 break;
1533 }
1da177e4
LT
1534 tbl->next = neigh_tables;
1535 neigh_tables = tbl;
1536 write_unlock(&neigh_tbl_lock);
bd89efc5
SK
1537
1538 if (unlikely(tmp)) {
1539 printk(KERN_ERR "NEIGH: Registering multiple tables for "
1540 "family %d\n", tbl->family);
1541 dump_stack();
1542 }
1da177e4 1543}
0a204500 1544EXPORT_SYMBOL(neigh_table_init);
1da177e4
LT
1545
1546int neigh_table_clear(struct neigh_table *tbl)
1547{
1548 struct neigh_table **tp;
1549
1550 /* It is not clean... Fix it to unload IPv6 module safely */
a5c30b34 1551 cancel_delayed_work_sync(&tbl->gc_work);
1da177e4
LT
1552 del_timer_sync(&tbl->proxy_timer);
1553 pneigh_queue_purge(&tbl->proxy_queue);
1554 neigh_ifdown(tbl, NULL);
1555 if (atomic_read(&tbl->entries))
1556 printk(KERN_CRIT "neighbour leakage\n");
1557 write_lock(&neigh_tbl_lock);
1558 for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
1559 if (*tp == tbl) {
1560 *tp = tbl->next;
1561 break;
1562 }
1563 }
1564 write_unlock(&neigh_tbl_lock);
1565
6193d2be
ED
1566 call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
1567 neigh_hash_free_rcu);
d6bf7817 1568 tbl->nht = NULL;
1da177e4
LT
1569
1570 kfree(tbl->phash_buckets);
1571 tbl->phash_buckets = NULL;
1572
3f192b5c
AD
1573 remove_proc_entry(tbl->id, init_net.proc_net_stat);
1574
3fcde74b
KK
1575 free_percpu(tbl->stats);
1576 tbl->stats = NULL;
1577
1da177e4
LT
1578 return 0;
1579}
0a204500 1580EXPORT_SYMBOL(neigh_table_clear);
1da177e4 1581
c8822a4e 1582static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1da177e4 1583{
3b1e0a65 1584 struct net *net = sock_net(skb->sk);
a14a49d2
TG
1585 struct ndmsg *ndm;
1586 struct nlattr *dst_attr;
1da177e4
LT
1587 struct neigh_table *tbl;
1588 struct net_device *dev = NULL;
a14a49d2 1589 int err = -EINVAL;
1da177e4 1590
110b2499 1591 ASSERT_RTNL();
a14a49d2 1592 if (nlmsg_len(nlh) < sizeof(*ndm))
1da177e4
LT
1593 goto out;
1594
a14a49d2
TG
1595 dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
1596 if (dst_attr == NULL)
1597 goto out;
1598
1599 ndm = nlmsg_data(nlh);
1600 if (ndm->ndm_ifindex) {
110b2499 1601 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
a14a49d2
TG
1602 if (dev == NULL) {
1603 err = -ENODEV;
1604 goto out;
1605 }
1606 }
1607
1da177e4
LT
1608 read_lock(&neigh_tbl_lock);
1609 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
a14a49d2 1610 struct neighbour *neigh;
1da177e4
LT
1611
1612 if (tbl->family != ndm->ndm_family)
1613 continue;
1614 read_unlock(&neigh_tbl_lock);
1615
a14a49d2 1616 if (nla_len(dst_attr) < tbl->key_len)
110b2499 1617 goto out;
1da177e4
LT
1618
1619 if (ndm->ndm_flags & NTF_PROXY) {
426b5303 1620 err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
110b2499 1621 goto out;
1da177e4
LT
1622 }
1623
a14a49d2 1624 if (dev == NULL)
110b2499 1625 goto out;
1da177e4 1626
a14a49d2
TG
1627 neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
1628 if (neigh == NULL) {
1629 err = -ENOENT;
110b2499 1630 goto out;
1da177e4 1631 }
a14a49d2
TG
1632
1633 err = neigh_update(neigh, NULL, NUD_FAILED,
1634 NEIGH_UPDATE_F_OVERRIDE |
1635 NEIGH_UPDATE_F_ADMIN);
1636 neigh_release(neigh);
110b2499 1637 goto out;
1da177e4
LT
1638 }
1639 read_unlock(&neigh_tbl_lock);
a14a49d2
TG
1640 err = -EAFNOSUPPORT;
1641
1da177e4
LT
1642out:
1643 return err;
1644}
1645
c8822a4e 1646static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1da177e4 1647{
3b1e0a65 1648 struct net *net = sock_net(skb->sk);
5208debd
TG
1649 struct ndmsg *ndm;
1650 struct nlattr *tb[NDA_MAX+1];
1da177e4
LT
1651 struct neigh_table *tbl;
1652 struct net_device *dev = NULL;
5208debd 1653 int err;
1da177e4 1654
110b2499 1655 ASSERT_RTNL();
5208debd
TG
1656 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
1657 if (err < 0)
1da177e4
LT
1658 goto out;
1659
5208debd
TG
1660 err = -EINVAL;
1661 if (tb[NDA_DST] == NULL)
1662 goto out;
1663
1664 ndm = nlmsg_data(nlh);
1665 if (ndm->ndm_ifindex) {
110b2499 1666 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
5208debd
TG
1667 if (dev == NULL) {
1668 err = -ENODEV;
1669 goto out;
1670 }
1671
1672 if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
110b2499 1673 goto out;
5208debd
TG
1674 }
1675
1da177e4
LT
1676 read_lock(&neigh_tbl_lock);
1677 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
5208debd
TG
1678 int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
1679 struct neighbour *neigh;
1680 void *dst, *lladdr;
1da177e4
LT
1681
1682 if (tbl->family != ndm->ndm_family)
1683 continue;
1684 read_unlock(&neigh_tbl_lock);
1685
5208debd 1686 if (nla_len(tb[NDA_DST]) < tbl->key_len)
110b2499 1687 goto out;
5208debd
TG
1688 dst = nla_data(tb[NDA_DST]);
1689 lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
1da177e4
LT
1690
1691 if (ndm->ndm_flags & NTF_PROXY) {
62dd9318
VN
1692 struct pneigh_entry *pn;
1693
1694 err = -ENOBUFS;
426b5303 1695 pn = pneigh_lookup(tbl, net, dst, dev, 1);
62dd9318
VN
1696 if (pn) {
1697 pn->flags = ndm->ndm_flags;
1698 err = 0;
1699 }
110b2499 1700 goto out;
1da177e4
LT
1701 }
1702
5208debd 1703 if (dev == NULL)
110b2499 1704 goto out;
5208debd
TG
1705
1706 neigh = neigh_lookup(tbl, dst, dev);
1707 if (neigh == NULL) {
1708 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1709 err = -ENOENT;
110b2499 1710 goto out;
5208debd 1711 }
4ec93edb 1712
5208debd
TG
1713 neigh = __neigh_lookup_errno(tbl, dst, dev);
1714 if (IS_ERR(neigh)) {
1715 err = PTR_ERR(neigh);
110b2499 1716 goto out;
1da177e4 1717 }
1da177e4 1718 } else {
5208debd
TG
1719 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1720 err = -EEXIST;
1721 neigh_release(neigh);
110b2499 1722 goto out;
1da177e4 1723 }
1da177e4 1724
5208debd
TG
1725 if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
1726 flags &= ~NEIGH_UPDATE_F_OVERRIDE;
1727 }
1da177e4 1728
0c5c2d30
EB
1729 if (ndm->ndm_flags & NTF_USE) {
1730 neigh_event_send(neigh, NULL);
1731 err = 0;
1732 } else
1733 err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
5208debd 1734 neigh_release(neigh);
110b2499 1735 goto out;
1da177e4
LT
1736 }
1737
1738 read_unlock(&neigh_tbl_lock);
5208debd 1739 err = -EAFNOSUPPORT;
1da177e4
LT
1740out:
1741 return err;
1742}
1743
c7fb64db
TG
1744static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1745{
ca860fb3
TG
1746 struct nlattr *nest;
1747
1748 nest = nla_nest_start(skb, NDTA_PARMS);
1749 if (nest == NULL)
1750 return -ENOBUFS;
c7fb64db
TG
1751
1752 if (parms->dev)
ca860fb3
TG
1753 NLA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex);
1754
1755 NLA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt));
8b5c171b
ED
1756 NLA_PUT_U32(skb, NDTPA_QUEUE_LENBYTES, parms->queue_len_bytes);
1757 /* approximative value for deprecated QUEUE_LEN (in packets) */
1758 NLA_PUT_U32(skb, NDTPA_QUEUE_LEN,
1759 DIV_ROUND_UP(parms->queue_len_bytes,
1760 SKB_TRUESIZE(ETH_FRAME_LEN)));
ca860fb3
TG
1761 NLA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen);
1762 NLA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes);
1763 NLA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes);
1764 NLA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes);
1765 NLA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time);
1766 NLA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME,
c7fb64db 1767 parms->base_reachable_time);
ca860fb3
TG
1768 NLA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime);
1769 NLA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time);
1770 NLA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time);
1771 NLA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay);
1772 NLA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay);
1773 NLA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime);
c7fb64db 1774
ca860fb3 1775 return nla_nest_end(skb, nest);
c7fb64db 1776
ca860fb3 1777nla_put_failure:
bc3ed28c
TG
1778 nla_nest_cancel(skb, nest);
1779 return -EMSGSIZE;
c7fb64db
TG
1780}
1781
ca860fb3
TG
1782static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
1783 u32 pid, u32 seq, int type, int flags)
c7fb64db
TG
1784{
1785 struct nlmsghdr *nlh;
1786 struct ndtmsg *ndtmsg;
1787
ca860fb3
TG
1788 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1789 if (nlh == NULL)
26932566 1790 return -EMSGSIZE;
c7fb64db 1791
ca860fb3 1792 ndtmsg = nlmsg_data(nlh);
c7fb64db
TG
1793
1794 read_lock_bh(&tbl->lock);
1795 ndtmsg->ndtm_family = tbl->family;
9ef1d4c7
PM
1796 ndtmsg->ndtm_pad1 = 0;
1797 ndtmsg->ndtm_pad2 = 0;
c7fb64db 1798
ca860fb3
TG
1799 NLA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1800 NLA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval);
1801 NLA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1);
1802 NLA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2);
1803 NLA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3);
c7fb64db
TG
1804
1805 {
1806 unsigned long now = jiffies;
1807 unsigned int flush_delta = now - tbl->last_flush;
1808 unsigned int rand_delta = now - tbl->last_rand;
d6bf7817 1809 struct neigh_hash_table *nht;
c7fb64db
TG
1810 struct ndt_config ndc = {
1811 .ndtc_key_len = tbl->key_len,
1812 .ndtc_entry_size = tbl->entry_size,
1813 .ndtc_entries = atomic_read(&tbl->entries),
1814 .ndtc_last_flush = jiffies_to_msecs(flush_delta),
1815 .ndtc_last_rand = jiffies_to_msecs(rand_delta),
c7fb64db
TG
1816 .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
1817 };
1818
d6bf7817
ED
1819 rcu_read_lock_bh();
1820 nht = rcu_dereference_bh(tbl->nht);
1821 ndc.ndtc_hash_rnd = nht->hash_rnd;
cd089336 1822 ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
d6bf7817
ED
1823 rcu_read_unlock_bh();
1824
ca860fb3 1825 NLA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc);
c7fb64db
TG
1826 }
1827
1828 {
1829 int cpu;
1830 struct ndt_stats ndst;
1831
1832 memset(&ndst, 0, sizeof(ndst));
1833
6f912042 1834 for_each_possible_cpu(cpu) {
c7fb64db
TG
1835 struct neigh_statistics *st;
1836
c7fb64db
TG
1837 st = per_cpu_ptr(tbl->stats, cpu);
1838 ndst.ndts_allocs += st->allocs;
1839 ndst.ndts_destroys += st->destroys;
1840 ndst.ndts_hash_grows += st->hash_grows;
1841 ndst.ndts_res_failed += st->res_failed;
1842 ndst.ndts_lookups += st->lookups;
1843 ndst.ndts_hits += st->hits;
1844 ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
1845 ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
1846 ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
1847 ndst.ndts_forced_gc_runs += st->forced_gc_runs;
1848 }
1849
ca860fb3 1850 NLA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst);
c7fb64db
TG
1851 }
1852
1853 BUG_ON(tbl->parms.dev);
1854 if (neightbl_fill_parms(skb, &tbl->parms) < 0)
ca860fb3 1855 goto nla_put_failure;
c7fb64db
TG
1856
1857 read_unlock_bh(&tbl->lock);
ca860fb3 1858 return nlmsg_end(skb, nlh);
c7fb64db 1859
ca860fb3 1860nla_put_failure:
c7fb64db 1861 read_unlock_bh(&tbl->lock);
26932566
PM
1862 nlmsg_cancel(skb, nlh);
1863 return -EMSGSIZE;
c7fb64db
TG
1864}
1865
ca860fb3
TG
1866static int neightbl_fill_param_info(struct sk_buff *skb,
1867 struct neigh_table *tbl,
c7fb64db 1868 struct neigh_parms *parms,
ca860fb3
TG
1869 u32 pid, u32 seq, int type,
1870 unsigned int flags)
c7fb64db
TG
1871{
1872 struct ndtmsg *ndtmsg;
1873 struct nlmsghdr *nlh;
1874
ca860fb3
TG
1875 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
1876 if (nlh == NULL)
26932566 1877 return -EMSGSIZE;
c7fb64db 1878
ca860fb3 1879 ndtmsg = nlmsg_data(nlh);
c7fb64db
TG
1880
1881 read_lock_bh(&tbl->lock);
1882 ndtmsg->ndtm_family = tbl->family;
9ef1d4c7
PM
1883 ndtmsg->ndtm_pad1 = 0;
1884 ndtmsg->ndtm_pad2 = 0;
c7fb64db 1885
ca860fb3
TG
1886 if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
1887 neightbl_fill_parms(skb, parms) < 0)
1888 goto errout;
c7fb64db
TG
1889
1890 read_unlock_bh(&tbl->lock);
ca860fb3
TG
1891 return nlmsg_end(skb, nlh);
1892errout:
c7fb64db 1893 read_unlock_bh(&tbl->lock);
26932566
PM
1894 nlmsg_cancel(skb, nlh);
1895 return -EMSGSIZE;
c7fb64db 1896}
4ec93edb 1897
ef7c79ed 1898static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
6b3f8674
TG
1899 [NDTA_NAME] = { .type = NLA_STRING },
1900 [NDTA_THRESH1] = { .type = NLA_U32 },
1901 [NDTA_THRESH2] = { .type = NLA_U32 },
1902 [NDTA_THRESH3] = { .type = NLA_U32 },
1903 [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
1904 [NDTA_PARMS] = { .type = NLA_NESTED },
1905};
1906
ef7c79ed 1907static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
6b3f8674
TG
1908 [NDTPA_IFINDEX] = { .type = NLA_U32 },
1909 [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
1910 [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
1911 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
1912 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
1913 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
1914 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
1915 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
1916 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
1917 [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
1918 [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
1919 [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
1920 [NDTPA_LOCKTIME] = { .type = NLA_U64 },
1921};
1922
c8822a4e 1923static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
c7fb64db 1924{
3b1e0a65 1925 struct net *net = sock_net(skb->sk);
c7fb64db 1926 struct neigh_table *tbl;
6b3f8674
TG
1927 struct ndtmsg *ndtmsg;
1928 struct nlattr *tb[NDTA_MAX+1];
1929 int err;
c7fb64db 1930
6b3f8674
TG
1931 err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
1932 nl_neightbl_policy);
1933 if (err < 0)
1934 goto errout;
c7fb64db 1935
6b3f8674
TG
1936 if (tb[NDTA_NAME] == NULL) {
1937 err = -EINVAL;
1938 goto errout;
1939 }
1940
1941 ndtmsg = nlmsg_data(nlh);
c7fb64db
TG
1942 read_lock(&neigh_tbl_lock);
1943 for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1944 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1945 continue;
1946
6b3f8674 1947 if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0)
c7fb64db
TG
1948 break;
1949 }
1950
1951 if (tbl == NULL) {
1952 err = -ENOENT;
6b3f8674 1953 goto errout_locked;
c7fb64db
TG
1954 }
1955
4ec93edb 1956 /*
c7fb64db
TG
1957 * We acquire tbl->lock to be nice to the periodic timers and
1958 * make sure they always see a consistent set of values.
1959 */
1960 write_lock_bh(&tbl->lock);
1961
6b3f8674
TG
1962 if (tb[NDTA_PARMS]) {
1963 struct nlattr *tbp[NDTPA_MAX+1];
c7fb64db 1964 struct neigh_parms *p;
6b3f8674 1965 int i, ifindex = 0;
c7fb64db 1966
6b3f8674
TG
1967 err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
1968 nl_ntbl_parm_policy);
1969 if (err < 0)
1970 goto errout_tbl_lock;
c7fb64db 1971
6b3f8674
TG
1972 if (tbp[NDTPA_IFINDEX])
1973 ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
c7fb64db 1974
97fd5bc7 1975 p = lookup_neigh_parms(tbl, net, ifindex);
c7fb64db
TG
1976 if (p == NULL) {
1977 err = -ENOENT;
6b3f8674 1978 goto errout_tbl_lock;
c7fb64db 1979 }
c7fb64db 1980
6b3f8674
TG
1981 for (i = 1; i <= NDTPA_MAX; i++) {
1982 if (tbp[i] == NULL)
1983 continue;
c7fb64db 1984
6b3f8674
TG
1985 switch (i) {
1986 case NDTPA_QUEUE_LEN:
8b5c171b
ED
1987 p->queue_len_bytes = nla_get_u32(tbp[i]) *
1988 SKB_TRUESIZE(ETH_FRAME_LEN);
1989 break;
1990 case NDTPA_QUEUE_LENBYTES:
1991 p->queue_len_bytes = nla_get_u32(tbp[i]);
6b3f8674
TG
1992 break;
1993 case NDTPA_PROXY_QLEN:
1994 p->proxy_qlen = nla_get_u32(tbp[i]);
1995 break;
1996 case NDTPA_APP_PROBES:
1997 p->app_probes = nla_get_u32(tbp[i]);
1998 break;
1999 case NDTPA_UCAST_PROBES:
2000 p->ucast_probes = nla_get_u32(tbp[i]);
2001 break;
2002 case NDTPA_MCAST_PROBES:
2003 p->mcast_probes = nla_get_u32(tbp[i]);
2004 break;
2005 case NDTPA_BASE_REACHABLE_TIME:
2006 p->base_reachable_time = nla_get_msecs(tbp[i]);
2007 break;
2008 case NDTPA_GC_STALETIME:
2009 p->gc_staletime = nla_get_msecs(tbp[i]);
2010 break;
2011 case NDTPA_DELAY_PROBE_TIME:
2012 p->delay_probe_time = nla_get_msecs(tbp[i]);
2013 break;
2014 case NDTPA_RETRANS_TIME:
2015 p->retrans_time = nla_get_msecs(tbp[i]);
2016 break;
2017 case NDTPA_ANYCAST_DELAY:
2018 p->anycast_delay = nla_get_msecs(tbp[i]);
2019 break;
2020 case NDTPA_PROXY_DELAY:
2021 p->proxy_delay = nla_get_msecs(tbp[i]);
2022 break;
2023 case NDTPA_LOCKTIME:
2024 p->locktime = nla_get_msecs(tbp[i]);
2025 break;
2026 }
2027 }
2028 }
c7fb64db 2029
6b3f8674
TG
2030 if (tb[NDTA_THRESH1])
2031 tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
c7fb64db 2032
6b3f8674
TG
2033 if (tb[NDTA_THRESH2])
2034 tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
c7fb64db 2035
6b3f8674
TG
2036 if (tb[NDTA_THRESH3])
2037 tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
c7fb64db 2038
6b3f8674
TG
2039 if (tb[NDTA_GC_INTERVAL])
2040 tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
c7fb64db
TG
2041
2042 err = 0;
2043
6b3f8674 2044errout_tbl_lock:
c7fb64db 2045 write_unlock_bh(&tbl->lock);
6b3f8674 2046errout_locked:
c7fb64db 2047 read_unlock(&neigh_tbl_lock);
6b3f8674 2048errout:
c7fb64db
TG
2049 return err;
2050}
2051
c8822a4e 2052static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
c7fb64db 2053{
3b1e0a65 2054 struct net *net = sock_net(skb->sk);
ca860fb3
TG
2055 int family, tidx, nidx = 0;
2056 int tbl_skip = cb->args[0];
2057 int neigh_skip = cb->args[1];
c7fb64db
TG
2058 struct neigh_table *tbl;
2059
ca860fb3 2060 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
c7fb64db
TG
2061
2062 read_lock(&neigh_tbl_lock);
ca860fb3 2063 for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) {
c7fb64db
TG
2064 struct neigh_parms *p;
2065
ca860fb3 2066 if (tidx < tbl_skip || (family && tbl->family != family))
c7fb64db
TG
2067 continue;
2068
ca860fb3
TG
2069 if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid,
2070 cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
2071 NLM_F_MULTI) <= 0)
c7fb64db
TG
2072 break;
2073
426b5303 2074 for (nidx = 0, p = tbl->parms.next; p; p = p->next) {
878628fb 2075 if (!net_eq(neigh_parms_net(p), net))
426b5303
EB
2076 continue;
2077
efc683fc
GK
2078 if (nidx < neigh_skip)
2079 goto next;
c7fb64db 2080
ca860fb3
TG
2081 if (neightbl_fill_param_info(skb, tbl, p,
2082 NETLINK_CB(cb->skb).pid,
2083 cb->nlh->nlmsg_seq,
2084 RTM_NEWNEIGHTBL,
2085 NLM_F_MULTI) <= 0)
c7fb64db 2086 goto out;
efc683fc
GK
2087 next:
2088 nidx++;
c7fb64db
TG
2089 }
2090
ca860fb3 2091 neigh_skip = 0;
c7fb64db
TG
2092 }
2093out:
2094 read_unlock(&neigh_tbl_lock);
ca860fb3
TG
2095 cb->args[0] = tidx;
2096 cb->args[1] = nidx;
c7fb64db
TG
2097
2098 return skb->len;
2099}
1da177e4 2100
8b8aec50
TG
2101static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
2102 u32 pid, u32 seq, int type, unsigned int flags)
1da177e4
LT
2103{
2104 unsigned long now = jiffies;
1da177e4 2105 struct nda_cacheinfo ci;
8b8aec50
TG
2106 struct nlmsghdr *nlh;
2107 struct ndmsg *ndm;
2108
2109 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
2110 if (nlh == NULL)
26932566 2111 return -EMSGSIZE;
1da177e4 2112
8b8aec50
TG
2113 ndm = nlmsg_data(nlh);
2114 ndm->ndm_family = neigh->ops->family;
9ef1d4c7
PM
2115 ndm->ndm_pad1 = 0;
2116 ndm->ndm_pad2 = 0;
8b8aec50
TG
2117 ndm->ndm_flags = neigh->flags;
2118 ndm->ndm_type = neigh->type;
2119 ndm->ndm_ifindex = neigh->dev->ifindex;
1da177e4 2120
8b8aec50
TG
2121 NLA_PUT(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key);
2122
2123 read_lock_bh(&neigh->lock);
2124 ndm->ndm_state = neigh->nud_state;
0ed8ddf4
ED
2125 if (neigh->nud_state & NUD_VALID) {
2126 char haddr[MAX_ADDR_LEN];
2127
2128 neigh_ha_snapshot(haddr, neigh, neigh->dev);
2129 if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
2130 read_unlock_bh(&neigh->lock);
2131 goto nla_put_failure;
2132 }
8b8aec50
TG
2133 }
2134
b9f5f52c
SH
2135 ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
2136 ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
2137 ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
8b8aec50
TG
2138 ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1;
2139 read_unlock_bh(&neigh->lock);
2140
2141 NLA_PUT_U32(skb, NDA_PROBES, atomic_read(&neigh->probes));
2142 NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
2143
2144 return nlmsg_end(skb, nlh);
2145
2146nla_put_failure:
26932566
PM
2147 nlmsg_cancel(skb, nlh);
2148 return -EMSGSIZE;
1da177e4
LT
2149}
2150
d961db35
TG
2151static void neigh_update_notify(struct neighbour *neigh)
2152{
2153 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
2154 __neigh_notify(neigh, RTM_NEWNEIGH, 0);
2155}
1da177e4
LT
2156
2157static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
2158 struct netlink_callback *cb)
2159{
767e97e1 2160 struct net *net = sock_net(skb->sk);
1da177e4
LT
2161 struct neighbour *n;
2162 int rc, h, s_h = cb->args[1];
2163 int idx, s_idx = idx = cb->args[2];
d6bf7817 2164 struct neigh_hash_table *nht;
1da177e4 2165
d6bf7817
ED
2166 rcu_read_lock_bh();
2167 nht = rcu_dereference_bh(tbl->nht);
2168
cd089336 2169 for (h = 0; h < (1 << nht->hash_shift); h++) {
1da177e4
LT
2170 if (h < s_h)
2171 continue;
2172 if (h > s_h)
2173 s_idx = 0;
767e97e1
ED
2174 for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
2175 n != NULL;
2176 n = rcu_dereference_bh(n->next)) {
09ad9bc7 2177 if (!net_eq(dev_net(n->dev), net))
426b5303 2178 continue;
efc683fc
GK
2179 if (idx < s_idx)
2180 goto next;
1da177e4
LT
2181 if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
2182 cb->nlh->nlmsg_seq,
b6544c0b
JHS
2183 RTM_NEWNEIGH,
2184 NLM_F_MULTI) <= 0) {
1da177e4
LT
2185 rc = -1;
2186 goto out;
2187 }
767e97e1 2188next:
efc683fc 2189 idx++;
1da177e4 2190 }
1da177e4
LT
2191 }
2192 rc = skb->len;
2193out:
d6bf7817 2194 rcu_read_unlock_bh();
1da177e4
LT
2195 cb->args[1] = h;
2196 cb->args[2] = idx;
2197 return rc;
2198}
2199
c8822a4e 2200static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
2201{
2202 struct neigh_table *tbl;
2203 int t, family, s_t;
2204
2205 read_lock(&neigh_tbl_lock);
8b8aec50 2206 family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
1da177e4
LT
2207 s_t = cb->args[0];
2208
2209 for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) {
2210 if (t < s_t || (family && tbl->family != family))
2211 continue;
2212 if (t > s_t)
2213 memset(&cb->args[1], 0, sizeof(cb->args) -
2214 sizeof(cb->args[0]));
2215 if (neigh_dump_table(tbl, skb, cb) < 0)
2216 break;
2217 }
2218 read_unlock(&neigh_tbl_lock);
2219
2220 cb->args[0] = t;
2221 return skb->len;
2222}
2223
2224void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
2225{
2226 int chain;
d6bf7817 2227 struct neigh_hash_table *nht;
1da177e4 2228
d6bf7817
ED
2229 rcu_read_lock_bh();
2230 nht = rcu_dereference_bh(tbl->nht);
2231
767e97e1 2232 read_lock(&tbl->lock); /* avoid resizes */
cd089336 2233 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
1da177e4
LT
2234 struct neighbour *n;
2235
767e97e1
ED
2236 for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
2237 n != NULL;
2238 n = rcu_dereference_bh(n->next))
1da177e4
LT
2239 cb(n, cookie);
2240 }
d6bf7817
ED
2241 read_unlock(&tbl->lock);
2242 rcu_read_unlock_bh();
1da177e4
LT
2243}
2244EXPORT_SYMBOL(neigh_for_each);
2245
2246/* The tbl->lock must be held as a writer and BH disabled. */
2247void __neigh_for_each_release(struct neigh_table *tbl,
2248 int (*cb)(struct neighbour *))
2249{
2250 int chain;
d6bf7817 2251 struct neigh_hash_table *nht;
1da177e4 2252
d6bf7817
ED
2253 nht = rcu_dereference_protected(tbl->nht,
2254 lockdep_is_held(&tbl->lock));
cd089336 2255 for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
767e97e1
ED
2256 struct neighbour *n;
2257 struct neighbour __rcu **np;
1da177e4 2258
d6bf7817 2259 np = &nht->hash_buckets[chain];
767e97e1
ED
2260 while ((n = rcu_dereference_protected(*np,
2261 lockdep_is_held(&tbl->lock))) != NULL) {
1da177e4
LT
2262 int release;
2263
2264 write_lock(&n->lock);
2265 release = cb(n);
2266 if (release) {
767e97e1
ED
2267 rcu_assign_pointer(*np,
2268 rcu_dereference_protected(n->next,
2269 lockdep_is_held(&tbl->lock)));
1da177e4
LT
2270 n->dead = 1;
2271 } else
2272 np = &n->next;
2273 write_unlock(&n->lock);
4f494554
TG
2274 if (release)
2275 neigh_cleanup_and_release(n);
1da177e4
LT
2276 }
2277 }
2278}
2279EXPORT_SYMBOL(__neigh_for_each_release);
2280
2281#ifdef CONFIG_PROC_FS
2282
2283static struct neighbour *neigh_get_first(struct seq_file *seq)
2284{
2285 struct neigh_seq_state *state = seq->private;
1218854a 2286 struct net *net = seq_file_net(seq);
d6bf7817 2287 struct neigh_hash_table *nht = state->nht;
1da177e4
LT
2288 struct neighbour *n = NULL;
2289 int bucket = state->bucket;
2290
2291 state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
cd089336 2292 for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
767e97e1 2293 n = rcu_dereference_bh(nht->hash_buckets[bucket]);
1da177e4
LT
2294
2295 while (n) {
878628fb 2296 if (!net_eq(dev_net(n->dev), net))
426b5303 2297 goto next;
1da177e4
LT
2298 if (state->neigh_sub_iter) {
2299 loff_t fakep = 0;
2300 void *v;
2301
2302 v = state->neigh_sub_iter(state, n, &fakep);
2303 if (!v)
2304 goto next;
2305 }
2306 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2307 break;
2308 if (n->nud_state & ~NUD_NOARP)
2309 break;
767e97e1
ED
2310next:
2311 n = rcu_dereference_bh(n->next);
1da177e4
LT
2312 }
2313
2314 if (n)
2315 break;
2316 }
2317 state->bucket = bucket;
2318
2319 return n;
2320}
2321
2322static struct neighbour *neigh_get_next(struct seq_file *seq,
2323 struct neighbour *n,
2324 loff_t *pos)
2325{
2326 struct neigh_seq_state *state = seq->private;
1218854a 2327 struct net *net = seq_file_net(seq);
d6bf7817 2328 struct neigh_hash_table *nht = state->nht;
1da177e4
LT
2329
2330 if (state->neigh_sub_iter) {
2331 void *v = state->neigh_sub_iter(state, n, pos);
2332 if (v)
2333 return n;
2334 }
767e97e1 2335 n = rcu_dereference_bh(n->next);
1da177e4
LT
2336
2337 while (1) {
2338 while (n) {
878628fb 2339 if (!net_eq(dev_net(n->dev), net))
426b5303 2340 goto next;
1da177e4
LT
2341 if (state->neigh_sub_iter) {
2342 void *v = state->neigh_sub_iter(state, n, pos);
2343 if (v)
2344 return n;
2345 goto next;
2346 }
2347 if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2348 break;
2349
2350 if (n->nud_state & ~NUD_NOARP)
2351 break;
767e97e1
ED
2352next:
2353 n = rcu_dereference_bh(n->next);
1da177e4
LT
2354 }
2355
2356 if (n)
2357 break;
2358
cd089336 2359 if (++state->bucket >= (1 << nht->hash_shift))
1da177e4
LT
2360 break;
2361
767e97e1 2362 n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
1da177e4
LT
2363 }
2364
2365 if (n && pos)
2366 --(*pos);
2367 return n;
2368}
2369
2370static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2371{
2372 struct neighbour *n = neigh_get_first(seq);
2373
2374 if (n) {
745e2031 2375 --(*pos);
1da177e4
LT
2376 while (*pos) {
2377 n = neigh_get_next(seq, n, pos);
2378 if (!n)
2379 break;
2380 }
2381 }
2382 return *pos ? NULL : n;
2383}
2384
2385static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2386{
2387 struct neigh_seq_state *state = seq->private;
1218854a 2388 struct net *net = seq_file_net(seq);
1da177e4
LT
2389 struct neigh_table *tbl = state->tbl;
2390 struct pneigh_entry *pn = NULL;
2391 int bucket = state->bucket;
2392
2393 state->flags |= NEIGH_SEQ_IS_PNEIGH;
2394 for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2395 pn = tbl->phash_buckets[bucket];
878628fb 2396 while (pn && !net_eq(pneigh_net(pn), net))
426b5303 2397 pn = pn->next;
1da177e4
LT
2398 if (pn)
2399 break;
2400 }
2401 state->bucket = bucket;
2402
2403 return pn;
2404}
2405
2406static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2407 struct pneigh_entry *pn,
2408 loff_t *pos)
2409{
2410 struct neigh_seq_state *state = seq->private;
1218854a 2411 struct net *net = seq_file_net(seq);
1da177e4
LT
2412 struct neigh_table *tbl = state->tbl;
2413
df07a94c
JBD
2414 do {
2415 pn = pn->next;
2416 } while (pn && !net_eq(pneigh_net(pn), net));
2417
1da177e4
LT
2418 while (!pn) {
2419 if (++state->bucket > PNEIGH_HASHMASK)
2420 break;
2421 pn = tbl->phash_buckets[state->bucket];
878628fb 2422 while (pn && !net_eq(pneigh_net(pn), net))
426b5303 2423 pn = pn->next;
1da177e4
LT
2424 if (pn)
2425 break;
2426 }
2427
2428 if (pn && pos)
2429 --(*pos);
2430
2431 return pn;
2432}
2433
2434static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2435{
2436 struct pneigh_entry *pn = pneigh_get_first(seq);
2437
2438 if (pn) {
745e2031 2439 --(*pos);
1da177e4
LT
2440 while (*pos) {
2441 pn = pneigh_get_next(seq, pn, pos);
2442 if (!pn)
2443 break;
2444 }
2445 }
2446 return *pos ? NULL : pn;
2447}
2448
2449static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2450{
2451 struct neigh_seq_state *state = seq->private;
2452 void *rc;
745e2031 2453 loff_t idxpos = *pos;
1da177e4 2454
745e2031 2455 rc = neigh_get_idx(seq, &idxpos);
1da177e4 2456 if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
745e2031 2457 rc = pneigh_get_idx(seq, &idxpos);
1da177e4
LT
2458
2459 return rc;
2460}
2461
2462void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
d6bf7817 2463 __acquires(rcu_bh)
1da177e4
LT
2464{
2465 struct neigh_seq_state *state = seq->private;
1da177e4
LT
2466
2467 state->tbl = tbl;
2468 state->bucket = 0;
2469 state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2470
d6bf7817
ED
2471 rcu_read_lock_bh();
2472 state->nht = rcu_dereference_bh(tbl->nht);
767e97e1 2473
745e2031 2474 return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
1da177e4
LT
2475}
2476EXPORT_SYMBOL(neigh_seq_start);
2477
2478void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2479{
2480 struct neigh_seq_state *state;
2481 void *rc;
2482
2483 if (v == SEQ_START_TOKEN) {
bff69732 2484 rc = neigh_get_first(seq);
1da177e4
LT
2485 goto out;
2486 }
2487
2488 state = seq->private;
2489 if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
2490 rc = neigh_get_next(seq, v, NULL);
2491 if (rc)
2492 goto out;
2493 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2494 rc = pneigh_get_first(seq);
2495 } else {
2496 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
2497 rc = pneigh_get_next(seq, v, NULL);
2498 }
2499out:
2500 ++(*pos);
2501 return rc;
2502}
2503EXPORT_SYMBOL(neigh_seq_next);
2504
2505void neigh_seq_stop(struct seq_file *seq, void *v)
d6bf7817 2506 __releases(rcu_bh)
1da177e4 2507{
d6bf7817 2508 rcu_read_unlock_bh();
1da177e4
LT
2509}
2510EXPORT_SYMBOL(neigh_seq_stop);
2511
2512/* statistics via seq_file */
2513
2514static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
2515{
81c1ebfc 2516 struct neigh_table *tbl = seq->private;
1da177e4
LT
2517 int cpu;
2518
2519 if (*pos == 0)
2520 return SEQ_START_TOKEN;
4ec93edb 2521
0f23174a 2522 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
1da177e4
LT
2523 if (!cpu_possible(cpu))
2524 continue;
2525 *pos = cpu+1;
2526 return per_cpu_ptr(tbl->stats, cpu);
2527 }
2528 return NULL;
2529}
2530
2531static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2532{
81c1ebfc 2533 struct neigh_table *tbl = seq->private;
1da177e4
LT
2534 int cpu;
2535
0f23174a 2536 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
1da177e4
LT
2537 if (!cpu_possible(cpu))
2538 continue;
2539 *pos = cpu+1;
2540 return per_cpu_ptr(tbl->stats, cpu);
2541 }
2542 return NULL;
2543}
2544
2545static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
2546{
2547
2548}
2549
2550static int neigh_stat_seq_show(struct seq_file *seq, void *v)
2551{
81c1ebfc 2552 struct neigh_table *tbl = seq->private;
1da177e4
LT
2553 struct neigh_statistics *st = v;
2554
2555 if (v == SEQ_START_TOKEN) {
9a6d276e 2556 seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards\n");
1da177e4
LT
2557 return 0;
2558 }
2559
2560 seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
9a6d276e 2561 "%08lx %08lx %08lx %08lx %08lx\n",
1da177e4
LT
2562 atomic_read(&tbl->entries),
2563
2564 st->allocs,
2565 st->destroys,
2566 st->hash_grows,
2567
2568 st->lookups,
2569 st->hits,
2570
2571 st->res_failed,
2572
2573 st->rcv_probes_mcast,
2574 st->rcv_probes_ucast,
2575
2576 st->periodic_gc_runs,
9a6d276e
NH
2577 st->forced_gc_runs,
2578 st->unres_discards
1da177e4
LT
2579 );
2580
2581 return 0;
2582}
2583
f690808e 2584static const struct seq_operations neigh_stat_seq_ops = {
1da177e4
LT
2585 .start = neigh_stat_seq_start,
2586 .next = neigh_stat_seq_next,
2587 .stop = neigh_stat_seq_stop,
2588 .show = neigh_stat_seq_show,
2589};
2590
2591static int neigh_stat_seq_open(struct inode *inode, struct file *file)
2592{
2593 int ret = seq_open(file, &neigh_stat_seq_ops);
2594
2595 if (!ret) {
2596 struct seq_file *sf = file->private_data;
81c1ebfc 2597 sf->private = PDE(inode)->data;
1da177e4
LT
2598 }
2599 return ret;
2600};
2601
9a32144e 2602static const struct file_operations neigh_stat_seq_fops = {
1da177e4
LT
2603 .owner = THIS_MODULE,
2604 .open = neigh_stat_seq_open,
2605 .read = seq_read,
2606 .llseek = seq_lseek,
2607 .release = seq_release,
2608};
2609
2610#endif /* CONFIG_PROC_FS */
2611
339bf98f
TG
2612static inline size_t neigh_nlmsg_size(void)
2613{
2614 return NLMSG_ALIGN(sizeof(struct ndmsg))
2615 + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
2616 + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
2617 + nla_total_size(sizeof(struct nda_cacheinfo))
2618 + nla_total_size(4); /* NDA_PROBES */
2619}
2620
b8673311 2621static void __neigh_notify(struct neighbour *n, int type, int flags)
1da177e4 2622{
c346dca1 2623 struct net *net = dev_net(n->dev);
8b8aec50 2624 struct sk_buff *skb;
b8673311 2625 int err = -ENOBUFS;
1da177e4 2626
339bf98f 2627 skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
8b8aec50 2628 if (skb == NULL)
b8673311 2629 goto errout;
1da177e4 2630
b8673311 2631 err = neigh_fill_info(skb, n, 0, 0, type, flags);
26932566
PM
2632 if (err < 0) {
2633 /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
2634 WARN_ON(err == -EMSGSIZE);
2635 kfree_skb(skb);
2636 goto errout;
2637 }
1ce85fe4
PNA
2638 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2639 return;
b8673311
TG
2640errout:
2641 if (err < 0)
426b5303 2642 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
1da177e4
LT
2643}
2644
d961db35 2645#ifdef CONFIG_ARPD
b8673311 2646void neigh_app_ns(struct neighbour *n)
1da177e4 2647{
b8673311
TG
2648 __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
2649}
0a204500 2650EXPORT_SYMBOL(neigh_app_ns);
1da177e4
LT
2651#endif /* CONFIG_ARPD */
2652
2653#ifdef CONFIG_SYSCTL
2654
8b5c171b
ED
2655static int proc_unres_qlen(ctl_table *ctl, int write, void __user *buffer,
2656 size_t *lenp, loff_t *ppos)
2657{
2658 int size, ret;
2659 ctl_table tmp = *ctl;
2660
2661 tmp.data = &size;
2662 size = DIV_ROUND_UP(*(int *)ctl->data, SKB_TRUESIZE(ETH_FRAME_LEN));
2663 ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
2664 if (write && !ret)
2665 *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
2666 return ret;
2667}
2668
2669enum {
2670 NEIGH_VAR_MCAST_PROBE,
2671 NEIGH_VAR_UCAST_PROBE,
2672 NEIGH_VAR_APP_PROBE,
2673 NEIGH_VAR_RETRANS_TIME,
2674 NEIGH_VAR_BASE_REACHABLE_TIME,
2675 NEIGH_VAR_DELAY_PROBE_TIME,
2676 NEIGH_VAR_GC_STALETIME,
2677 NEIGH_VAR_QUEUE_LEN,
2678 NEIGH_VAR_QUEUE_LEN_BYTES,
2679 NEIGH_VAR_PROXY_QLEN,
2680 NEIGH_VAR_ANYCAST_DELAY,
2681 NEIGH_VAR_PROXY_DELAY,
2682 NEIGH_VAR_LOCKTIME,
2683 NEIGH_VAR_RETRANS_TIME_MS,
2684 NEIGH_VAR_BASE_REACHABLE_TIME_MS,
2685 NEIGH_VAR_GC_INTERVAL,
2686 NEIGH_VAR_GC_THRESH1,
2687 NEIGH_VAR_GC_THRESH2,
2688 NEIGH_VAR_GC_THRESH3,
2689 NEIGH_VAR_MAX
2690};
54716e3b 2691
1da177e4
LT
2692static struct neigh_sysctl_table {
2693 struct ctl_table_header *sysctl_header;
8b5c171b 2694 struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
c3bac5a7 2695 char *dev_name;
ab32ea5d 2696} neigh_sysctl_template __read_mostly = {
1da177e4 2697 .neigh_vars = {
8b5c171b 2698 [NEIGH_VAR_MCAST_PROBE] = {
1da177e4
LT
2699 .procname = "mcast_solicit",
2700 .maxlen = sizeof(int),
2701 .mode = 0644,
6d9f239a 2702 .proc_handler = proc_dointvec,
1da177e4 2703 },
8b5c171b 2704 [NEIGH_VAR_UCAST_PROBE] = {
1da177e4
LT
2705 .procname = "ucast_solicit",
2706 .maxlen = sizeof(int),
2707 .mode = 0644,
6d9f239a 2708 .proc_handler = proc_dointvec,
1da177e4 2709 },
8b5c171b 2710 [NEIGH_VAR_APP_PROBE] = {
1da177e4
LT
2711 .procname = "app_solicit",
2712 .maxlen = sizeof(int),
2713 .mode = 0644,
6d9f239a 2714 .proc_handler = proc_dointvec,
1da177e4 2715 },
8b5c171b 2716 [NEIGH_VAR_RETRANS_TIME] = {
1da177e4
LT
2717 .procname = "retrans_time",
2718 .maxlen = sizeof(int),
2719 .mode = 0644,
6d9f239a 2720 .proc_handler = proc_dointvec_userhz_jiffies,
1da177e4 2721 },
8b5c171b 2722 [NEIGH_VAR_BASE_REACHABLE_TIME] = {
1da177e4
LT
2723 .procname = "base_reachable_time",
2724 .maxlen = sizeof(int),
2725 .mode = 0644,
6d9f239a 2726 .proc_handler = proc_dointvec_jiffies,
1da177e4 2727 },
8b5c171b 2728 [NEIGH_VAR_DELAY_PROBE_TIME] = {
1da177e4
LT
2729 .procname = "delay_first_probe_time",
2730 .maxlen = sizeof(int),
2731 .mode = 0644,
6d9f239a 2732 .proc_handler = proc_dointvec_jiffies,
1da177e4 2733 },
8b5c171b 2734 [NEIGH_VAR_GC_STALETIME] = {
1da177e4
LT
2735 .procname = "gc_stale_time",
2736 .maxlen = sizeof(int),
2737 .mode = 0644,
6d9f239a 2738 .proc_handler = proc_dointvec_jiffies,
1da177e4 2739 },
8b5c171b 2740 [NEIGH_VAR_QUEUE_LEN] = {
1da177e4
LT
2741 .procname = "unres_qlen",
2742 .maxlen = sizeof(int),
2743 .mode = 0644,
8b5c171b
ED
2744 .proc_handler = proc_unres_qlen,
2745 },
2746 [NEIGH_VAR_QUEUE_LEN_BYTES] = {
2747 .procname = "unres_qlen_bytes",
2748 .maxlen = sizeof(int),
2749 .mode = 0644,
6d9f239a 2750 .proc_handler = proc_dointvec,
1da177e4 2751 },
8b5c171b 2752 [NEIGH_VAR_PROXY_QLEN] = {
1da177e4
LT
2753 .procname = "proxy_qlen",
2754 .maxlen = sizeof(int),
2755 .mode = 0644,
6d9f239a 2756 .proc_handler = proc_dointvec,
1da177e4 2757 },
8b5c171b 2758 [NEIGH_VAR_ANYCAST_DELAY] = {
1da177e4
LT
2759 .procname = "anycast_delay",
2760 .maxlen = sizeof(int),
2761 .mode = 0644,
6d9f239a 2762 .proc_handler = proc_dointvec_userhz_jiffies,
1da177e4 2763 },
8b5c171b 2764 [NEIGH_VAR_PROXY_DELAY] = {
1da177e4
LT
2765 .procname = "proxy_delay",
2766 .maxlen = sizeof(int),
2767 .mode = 0644,
6d9f239a 2768 .proc_handler = proc_dointvec_userhz_jiffies,
1da177e4 2769 },
8b5c171b 2770 [NEIGH_VAR_LOCKTIME] = {
1da177e4
LT
2771 .procname = "locktime",
2772 .maxlen = sizeof(int),
2773 .mode = 0644,
6d9f239a 2774 .proc_handler = proc_dointvec_userhz_jiffies,
1da177e4 2775 },
8b5c171b 2776 [NEIGH_VAR_RETRANS_TIME_MS] = {
d12af679
EB
2777 .procname = "retrans_time_ms",
2778 .maxlen = sizeof(int),
2779 .mode = 0644,
6d9f239a 2780 .proc_handler = proc_dointvec_ms_jiffies,
d12af679 2781 },
8b5c171b 2782 [NEIGH_VAR_BASE_REACHABLE_TIME_MS] = {
d12af679
EB
2783 .procname = "base_reachable_time_ms",
2784 .maxlen = sizeof(int),
2785 .mode = 0644,
6d9f239a 2786 .proc_handler = proc_dointvec_ms_jiffies,
d12af679 2787 },
8b5c171b 2788 [NEIGH_VAR_GC_INTERVAL] = {
1da177e4
LT
2789 .procname = "gc_interval",
2790 .maxlen = sizeof(int),
2791 .mode = 0644,
6d9f239a 2792 .proc_handler = proc_dointvec_jiffies,
1da177e4 2793 },
8b5c171b 2794 [NEIGH_VAR_GC_THRESH1] = {
1da177e4
LT
2795 .procname = "gc_thresh1",
2796 .maxlen = sizeof(int),
2797 .mode = 0644,
6d9f239a 2798 .proc_handler = proc_dointvec,
1da177e4 2799 },
8b5c171b 2800 [NEIGH_VAR_GC_THRESH2] = {
1da177e4
LT
2801 .procname = "gc_thresh2",
2802 .maxlen = sizeof(int),
2803 .mode = 0644,
6d9f239a 2804 .proc_handler = proc_dointvec,
1da177e4 2805 },
8b5c171b 2806 [NEIGH_VAR_GC_THRESH3] = {
1da177e4
LT
2807 .procname = "gc_thresh3",
2808 .maxlen = sizeof(int),
2809 .mode = 0644,
6d9f239a 2810 .proc_handler = proc_dointvec,
1da177e4 2811 },
c3bac5a7 2812 {},
1da177e4
LT
2813 },
2814};
2815
2816int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
54716e3b 2817 char *p_name, proc_handler *handler)
1da177e4 2818{
3c607bbb 2819 struct neigh_sysctl_table *t;
1da177e4 2820 const char *dev_name_source = NULL;
c3bac5a7
PE
2821
2822#define NEIGH_CTL_PATH_ROOT 0
2823#define NEIGH_CTL_PATH_PROTO 1
2824#define NEIGH_CTL_PATH_NEIGH 2
2825#define NEIGH_CTL_PATH_DEV 3
2826
2827 struct ctl_path neigh_path[] = {
f8572d8f
EB
2828 { .procname = "net", },
2829 { .procname = "proto", },
2830 { .procname = "neigh", },
2831 { .procname = "default", },
c3bac5a7
PE
2832 { },
2833 };
1da177e4 2834
3c607bbb 2835 t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
1da177e4 2836 if (!t)
3c607bbb
PE
2837 goto err;
2838
8b5c171b
ED
2839 t->neigh_vars[NEIGH_VAR_MCAST_PROBE].data = &p->mcast_probes;
2840 t->neigh_vars[NEIGH_VAR_UCAST_PROBE].data = &p->ucast_probes;
2841 t->neigh_vars[NEIGH_VAR_APP_PROBE].data = &p->app_probes;
2842 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].data = &p->retrans_time;
2843 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].data = &p->base_reachable_time;
2844 t->neigh_vars[NEIGH_VAR_DELAY_PROBE_TIME].data = &p->delay_probe_time;
2845 t->neigh_vars[NEIGH_VAR_GC_STALETIME].data = &p->gc_staletime;
2846 t->neigh_vars[NEIGH_VAR_QUEUE_LEN].data = &p->queue_len_bytes;
2847 t->neigh_vars[NEIGH_VAR_QUEUE_LEN_BYTES].data = &p->queue_len_bytes;
2848 t->neigh_vars[NEIGH_VAR_PROXY_QLEN].data = &p->proxy_qlen;
2849 t->neigh_vars[NEIGH_VAR_ANYCAST_DELAY].data = &p->anycast_delay;
2850 t->neigh_vars[NEIGH_VAR_PROXY_DELAY].data = &p->proxy_delay;
2851 t->neigh_vars[NEIGH_VAR_LOCKTIME].data = &p->locktime;
2852 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].data = &p->retrans_time;
2853 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].data = &p->base_reachable_time;
1da177e4
LT
2854
2855 if (dev) {
2856 dev_name_source = dev->name;
d12af679 2857 /* Terminate the table early */
8b5c171b
ED
2858 memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
2859 sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
1da177e4 2860 } else {
c3bac5a7 2861 dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname;
8b5c171b
ED
2862 t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = (int *)(p + 1);
2863 t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = (int *)(p + 1) + 1;
2864 t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = (int *)(p + 1) + 2;
2865 t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = (int *)(p + 1) + 3;
1da177e4
LT
2866 }
2867
1da177e4 2868
f8572d8f 2869 if (handler) {
1da177e4 2870 /* RetransTime */
8b5c171b
ED
2871 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
2872 t->neigh_vars[NEIGH_VAR_RETRANS_TIME].extra1 = dev;
1da177e4 2873 /* ReachableTime */
8b5c171b
ED
2874 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
2875 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].extra1 = dev;
1da177e4 2876 /* RetransTime (in milliseconds)*/
8b5c171b
ED
2877 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
2878 t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].extra1 = dev;
1da177e4 2879 /* ReachableTime (in milliseconds) */
8b5c171b
ED
2880 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
2881 t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].extra1 = dev;
1da177e4
LT
2882 }
2883
c3bac5a7
PE
2884 t->dev_name = kstrdup(dev_name_source, GFP_KERNEL);
2885 if (!t->dev_name)
1da177e4 2886 goto free;
1da177e4 2887
c3bac5a7 2888 neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name;
c3bac5a7 2889 neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name;
1da177e4 2890
4ab438fc 2891 t->sysctl_header =
57da52c1 2892 register_net_sysctl_table(neigh_parms_net(p), neigh_path, t->neigh_vars);
3c607bbb 2893 if (!t->sysctl_header)
1da177e4 2894 goto free_procname;
3c607bbb 2895
1da177e4
LT
2896 p->sysctl_table = t;
2897 return 0;
2898
3c607bbb 2899free_procname:
c3bac5a7 2900 kfree(t->dev_name);
3c607bbb 2901free:
1da177e4 2902 kfree(t);
3c607bbb
PE
2903err:
2904 return -ENOBUFS;
1da177e4 2905}
0a204500 2906EXPORT_SYMBOL(neigh_sysctl_register);
1da177e4
LT
2907
2908void neigh_sysctl_unregister(struct neigh_parms *p)
2909{
2910 if (p->sysctl_table) {
2911 struct neigh_sysctl_table *t = p->sysctl_table;
2912 p->sysctl_table = NULL;
2913 unregister_sysctl_table(t->sysctl_header);
c3bac5a7 2914 kfree(t->dev_name);
1da177e4
LT
2915 kfree(t);
2916 }
2917}
0a204500 2918EXPORT_SYMBOL(neigh_sysctl_unregister);
1da177e4
LT
2919
2920#endif /* CONFIG_SYSCTL */
2921
c8822a4e
TG
2922static int __init neigh_init(void)
2923{
c7ac8679
GR
2924 rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
2925 rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
2926 rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
c8822a4e 2927
c7ac8679
GR
2928 rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
2929 NULL);
2930 rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
c8822a4e
TG
2931
2932 return 0;
2933}
2934
2935subsys_initcall(neigh_init);
2936