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