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