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