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