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