1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linux INET6 implementation 4 * Forwarding Information Database 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * 9 * Changes: 10 * Yuji SEKIYA @USAGI: Support default route on router node; 11 * remove ip6_null_entry from the top of 12 * routing table. 13 * Ville Nuorvala: Fixed routing subtrees. 14 */ 15 16 #define pr_fmt(fmt) "IPv6: " fmt 17 18 #include <linux/bpf.h> 19 #include <linux/errno.h> 20 #include <linux/types.h> 21 #include <linux/net.h> 22 #include <linux/route.h> 23 #include <linux/netdevice.h> 24 #include <linux/in6.h> 25 #include <linux/init.h> 26 #include <linux/list.h> 27 #include <linux/slab.h> 28 29 #include <net/ip.h> 30 #include <net/ipv6.h> 31 #include <net/ndisc.h> 32 #include <net/addrconf.h> 33 #include <net/lwtunnel.h> 34 #include <net/fib_notifier.h> 35 36 #include <net/ip_fib.h> 37 #include <net/ip6_fib.h> 38 #include <net/ip6_route.h> 39 40 static struct kmem_cache *fib6_node_kmem __read_mostly; 41 42 struct fib6_cleaner { 43 struct fib6_walker w; 44 struct net *net; 45 int (*func)(struct fib6_info *, void *arg); 46 int sernum; 47 void *arg; 48 bool skip_notify; 49 }; 50 51 #ifdef CONFIG_IPV6_SUBTREES 52 #define FWS_INIT FWS_S 53 #else 54 #define FWS_INIT FWS_L 55 #endif 56 57 static struct fib6_info *fib6_find_prefix(struct net *net, 58 struct fib6_table *table, 59 struct fib6_node *fn); 60 static struct fib6_node *fib6_repair_tree(struct net *net, 61 struct fib6_table *table, 62 struct fib6_node *fn); 63 static int fib6_walk(struct net *net, struct fib6_walker *w); 64 static int fib6_walk_continue(struct fib6_walker *w); 65 66 /* 67 * A routing update causes an increase of the serial number on the 68 * affected subtree. This allows for cached routes to be asynchronously 69 * tested when modifications are made to the destination cache as a 70 * result of redirects, path MTU changes, etc. 71 */ 72 73 static void fib6_gc_timer_cb(struct timer_list *t); 74 75 #define FOR_WALKERS(net, w) \ 76 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh) 77 78 static void fib6_walker_link(struct net *net, struct fib6_walker *w) 79 { 80 write_lock_bh(&net->ipv6.fib6_walker_lock); 81 list_add(&w->lh, &net->ipv6.fib6_walkers); 82 write_unlock_bh(&net->ipv6.fib6_walker_lock); 83 } 84 85 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w) 86 { 87 write_lock_bh(&net->ipv6.fib6_walker_lock); 88 list_del(&w->lh); 89 write_unlock_bh(&net->ipv6.fib6_walker_lock); 90 } 91 92 static int fib6_new_sernum(struct net *net) 93 { 94 int new, old = atomic_read(&net->ipv6.fib6_sernum); 95 96 do { 97 new = old < INT_MAX ? old + 1 : 1; 98 } while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new)); 99 100 return new; 101 } 102 103 enum { 104 FIB6_NO_SERNUM_CHANGE = 0, 105 }; 106 107 void fib6_update_sernum(struct net *net, struct fib6_info *f6i) 108 { 109 struct fib6_node *fn; 110 111 fn = rcu_dereference_protected(f6i->fib6_node, 112 lockdep_is_held(&f6i->fib6_table->tb6_lock)); 113 if (fn) 114 WRITE_ONCE(fn->fn_sernum, fib6_new_sernum(net)); 115 } 116 117 /* 118 * Auxiliary address test functions for the radix tree. 119 * 120 * These assume a 32bit processor (although it will work on 121 * 64bit processors) 122 */ 123 124 /* 125 * test bit 126 */ 127 #if defined(__LITTLE_ENDIAN) 128 # define BITOP_BE32_SWIZZLE (0x1F & ~7) 129 #else 130 # define BITOP_BE32_SWIZZLE 0 131 #endif 132 133 static __be32 addr_bit_set(const void *token, int fn_bit) 134 { 135 const __be32 *addr = token; 136 /* 137 * Here, 138 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f) 139 * is optimized version of 140 * htonl(1 << ((~fn_bit)&0x1F)) 141 * See include/asm-generic/bitops/le.h. 142 */ 143 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) & 144 addr[fn_bit >> 5]; 145 } 146 147 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh) 148 { 149 struct fib6_info *f6i; 150 size_t sz = sizeof(*f6i); 151 152 if (with_fib6_nh) 153 sz += sizeof(struct fib6_nh); 154 155 f6i = kzalloc(sz, gfp_flags); 156 if (!f6i) 157 return NULL; 158 159 /* fib6_siblings is a union with nh_list, so this initializes both */ 160 INIT_LIST_HEAD(&f6i->fib6_siblings); 161 refcount_set(&f6i->fib6_ref, 1); 162 163 INIT_HLIST_NODE(&f6i->gc_link); 164 165 return f6i; 166 } 167 168 void fib6_info_destroy_rcu(struct rcu_head *head) 169 { 170 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu); 171 172 WARN_ON(f6i->fib6_node); 173 174 if (f6i->nh) 175 nexthop_put(f6i->nh); 176 else 177 fib6_nh_release(f6i->fib6_nh); 178 179 ip_fib_metrics_put(f6i->fib6_metrics); 180 kfree(f6i); 181 } 182 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu); 183 184 static struct fib6_node *node_alloc(struct net *net) 185 { 186 struct fib6_node *fn; 187 188 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); 189 if (fn) 190 net->ipv6.rt6_stats->fib_nodes++; 191 192 return fn; 193 } 194 195 static void node_free_immediate(struct net *net, struct fib6_node *fn) 196 { 197 kmem_cache_free(fib6_node_kmem, fn); 198 net->ipv6.rt6_stats->fib_nodes--; 199 } 200 201 static void node_free_rcu(struct rcu_head *head) 202 { 203 struct fib6_node *fn = container_of(head, struct fib6_node, rcu); 204 205 kmem_cache_free(fib6_node_kmem, fn); 206 } 207 208 static void node_free(struct net *net, struct fib6_node *fn) 209 { 210 call_rcu(&fn->rcu, node_free_rcu); 211 net->ipv6.rt6_stats->fib_nodes--; 212 } 213 214 static void fib6_free_table(struct fib6_table *table) 215 { 216 inetpeer_invalidate_tree(&table->tb6_peers); 217 kfree(table); 218 } 219 220 static void fib6_link_table(struct net *net, struct fib6_table *tb) 221 { 222 unsigned int h; 223 224 /* 225 * Initialize table lock at a single place to give lockdep a key, 226 * tables aren't visible prior to being linked to the list. 227 */ 228 spin_lock_init(&tb->tb6_lock); 229 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); 230 231 /* 232 * No protection necessary, this is the only list mutatation 233 * operation, tables never disappear once they exist. 234 */ 235 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]); 236 } 237 238 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 239 240 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id) 241 { 242 struct fib6_table *table; 243 244 table = kzalloc(sizeof(*table), GFP_ATOMIC); 245 if (table) { 246 table->tb6_id = id; 247 rcu_assign_pointer(table->tb6_root.leaf, 248 net->ipv6.fib6_null_entry); 249 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; 250 inet_peer_base_init(&table->tb6_peers); 251 INIT_HLIST_HEAD(&table->tb6_gc_hlist); 252 } 253 254 return table; 255 } 256 257 struct fib6_table *fib6_new_table(struct net *net, u32 id) 258 { 259 struct fib6_table *tb; 260 261 if (id == 0) 262 id = RT6_TABLE_MAIN; 263 tb = fib6_get_table(net, id); 264 if (tb) 265 return tb; 266 267 tb = fib6_alloc_table(net, id); 268 if (tb) 269 fib6_link_table(net, tb); 270 271 return tb; 272 } 273 EXPORT_SYMBOL_GPL(fib6_new_table); 274 275 struct fib6_table *fib6_get_table(struct net *net, u32 id) 276 { 277 struct fib6_table *tb; 278 struct hlist_head *head; 279 unsigned int h; 280 281 if (id == 0) 282 id = RT6_TABLE_MAIN; 283 h = id & (FIB6_TABLE_HASHSZ - 1); 284 rcu_read_lock(); 285 head = &net->ipv6.fib_table_hash[h]; 286 hlist_for_each_entry_rcu(tb, head, tb6_hlist) { 287 if (tb->tb6_id == id) { 288 rcu_read_unlock(); 289 return tb; 290 } 291 } 292 rcu_read_unlock(); 293 294 return NULL; 295 } 296 EXPORT_SYMBOL_GPL(fib6_get_table); 297 298 static void __net_init fib6_tables_init(struct net *net) 299 { 300 fib6_link_table(net, net->ipv6.fib6_main_tbl); 301 fib6_link_table(net, net->ipv6.fib6_local_tbl); 302 } 303 #else 304 305 struct fib6_table *fib6_new_table(struct net *net, u32 id) 306 { 307 return fib6_get_table(net, id); 308 } 309 310 struct fib6_table *fib6_get_table(struct net *net, u32 id) 311 { 312 return net->ipv6.fib6_main_tbl; 313 } 314 315 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, 316 const struct sk_buff *skb, 317 int flags, pol_lookup_t lookup) 318 { 319 struct rt6_info *rt; 320 321 rt = pol_lookup_func(lookup, 322 net, net->ipv6.fib6_main_tbl, fl6, skb, flags); 323 if (rt->dst.error == -EAGAIN) { 324 ip6_rt_put_flags(rt, flags); 325 rt = net->ipv6.ip6_null_entry; 326 if (!(flags & RT6_LOOKUP_F_DST_NOREF)) 327 dst_hold(&rt->dst); 328 } 329 330 return &rt->dst; 331 } 332 333 /* called with rcu lock held; no reference taken on fib6_info */ 334 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6, 335 struct fib6_result *res, int flags) 336 { 337 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, 338 res, flags); 339 } 340 341 static void __net_init fib6_tables_init(struct net *net) 342 { 343 fib6_link_table(net, net->ipv6.fib6_main_tbl); 344 } 345 346 #endif 347 348 unsigned int fib6_tables_seq_read(struct net *net) 349 { 350 unsigned int h, fib_seq = 0; 351 352 rcu_read_lock(); 353 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 354 struct hlist_head *head = &net->ipv6.fib_table_hash[h]; 355 struct fib6_table *tb; 356 357 hlist_for_each_entry_rcu(tb, head, tb6_hlist) 358 fib_seq += tb->fib_seq; 359 } 360 rcu_read_unlock(); 361 362 return fib_seq; 363 } 364 365 static int call_fib6_entry_notifier(struct notifier_block *nb, 366 enum fib_event_type event_type, 367 struct fib6_info *rt, 368 struct netlink_ext_ack *extack) 369 { 370 struct fib6_entry_notifier_info info = { 371 .info.extack = extack, 372 .rt = rt, 373 }; 374 375 return call_fib6_notifier(nb, event_type, &info.info); 376 } 377 378 static int call_fib6_multipath_entry_notifier(struct notifier_block *nb, 379 enum fib_event_type event_type, 380 struct fib6_info *rt, 381 unsigned int nsiblings, 382 struct netlink_ext_ack *extack) 383 { 384 struct fib6_entry_notifier_info info = { 385 .info.extack = extack, 386 .rt = rt, 387 .nsiblings = nsiblings, 388 }; 389 390 return call_fib6_notifier(nb, event_type, &info.info); 391 } 392 393 int call_fib6_entry_notifiers(struct net *net, 394 enum fib_event_type event_type, 395 struct fib6_info *rt, 396 struct netlink_ext_ack *extack) 397 { 398 struct fib6_entry_notifier_info info = { 399 .info.extack = extack, 400 .rt = rt, 401 }; 402 403 rt->fib6_table->fib_seq++; 404 return call_fib6_notifiers(net, event_type, &info.info); 405 } 406 407 int call_fib6_multipath_entry_notifiers(struct net *net, 408 enum fib_event_type event_type, 409 struct fib6_info *rt, 410 unsigned int nsiblings, 411 struct netlink_ext_ack *extack) 412 { 413 struct fib6_entry_notifier_info info = { 414 .info.extack = extack, 415 .rt = rt, 416 .nsiblings = nsiblings, 417 }; 418 419 rt->fib6_table->fib_seq++; 420 return call_fib6_notifiers(net, event_type, &info.info); 421 } 422 423 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt) 424 { 425 struct fib6_entry_notifier_info info = { 426 .rt = rt, 427 .nsiblings = rt->fib6_nsiblings, 428 }; 429 430 rt->fib6_table->fib_seq++; 431 return call_fib6_notifiers(net, FIB_EVENT_ENTRY_REPLACE, &info.info); 432 } 433 434 struct fib6_dump_arg { 435 struct net *net; 436 struct notifier_block *nb; 437 struct netlink_ext_ack *extack; 438 }; 439 440 static int fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg) 441 { 442 enum fib_event_type fib_event = FIB_EVENT_ENTRY_REPLACE; 443 int err; 444 445 if (!rt || rt == arg->net->ipv6.fib6_null_entry) 446 return 0; 447 448 if (rt->fib6_nsiblings) 449 err = call_fib6_multipath_entry_notifier(arg->nb, fib_event, 450 rt, 451 rt->fib6_nsiblings, 452 arg->extack); 453 else 454 err = call_fib6_entry_notifier(arg->nb, fib_event, rt, 455 arg->extack); 456 457 return err; 458 } 459 460 static int fib6_node_dump(struct fib6_walker *w) 461 { 462 int err; 463 464 err = fib6_rt_dump(w->leaf, w->args); 465 w->leaf = NULL; 466 return err; 467 } 468 469 static int fib6_table_dump(struct net *net, struct fib6_table *tb, 470 struct fib6_walker *w) 471 { 472 int err; 473 474 w->root = &tb->tb6_root; 475 spin_lock_bh(&tb->tb6_lock); 476 err = fib6_walk(net, w); 477 spin_unlock_bh(&tb->tb6_lock); 478 return err; 479 } 480 481 /* Called with rcu_read_lock() */ 482 int fib6_tables_dump(struct net *net, struct notifier_block *nb, 483 struct netlink_ext_ack *extack) 484 { 485 struct fib6_dump_arg arg; 486 struct fib6_walker *w; 487 unsigned int h; 488 int err = 0; 489 490 w = kzalloc(sizeof(*w), GFP_ATOMIC); 491 if (!w) 492 return -ENOMEM; 493 494 w->func = fib6_node_dump; 495 arg.net = net; 496 arg.nb = nb; 497 arg.extack = extack; 498 w->args = &arg; 499 500 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 501 struct hlist_head *head = &net->ipv6.fib_table_hash[h]; 502 struct fib6_table *tb; 503 504 hlist_for_each_entry_rcu(tb, head, tb6_hlist) { 505 err = fib6_table_dump(net, tb, w); 506 if (err) 507 goto out; 508 } 509 } 510 511 out: 512 kfree(w); 513 514 /* The tree traversal function should never return a positive value. */ 515 return err > 0 ? -EINVAL : err; 516 } 517 518 static int fib6_dump_node(struct fib6_walker *w) 519 { 520 int res; 521 struct fib6_info *rt; 522 523 for_each_fib6_walker_rt(w) { 524 res = rt6_dump_route(rt, w->args, w->skip_in_node); 525 if (res >= 0) { 526 /* Frame is full, suspend walking */ 527 w->leaf = rt; 528 529 /* We'll restart from this node, so if some routes were 530 * already dumped, skip them next time. 531 */ 532 w->skip_in_node += res; 533 534 return 1; 535 } 536 w->skip_in_node = 0; 537 538 /* Multipath routes are dumped in one route with the 539 * RTA_MULTIPATH attribute. Jump 'rt' to point to the 540 * last sibling of this route (no need to dump the 541 * sibling routes again) 542 */ 543 if (rt->fib6_nsiblings) 544 rt = list_last_entry(&rt->fib6_siblings, 545 struct fib6_info, 546 fib6_siblings); 547 } 548 w->leaf = NULL; 549 return 0; 550 } 551 552 static void fib6_dump_end(struct netlink_callback *cb) 553 { 554 struct net *net = sock_net(cb->skb->sk); 555 struct fib6_walker *w = (void *)cb->args[2]; 556 557 if (w) { 558 if (cb->args[4]) { 559 cb->args[4] = 0; 560 fib6_walker_unlink(net, w); 561 } 562 cb->args[2] = 0; 563 kfree(w); 564 } 565 cb->done = (void *)cb->args[3]; 566 cb->args[1] = 3; 567 } 568 569 static int fib6_dump_done(struct netlink_callback *cb) 570 { 571 fib6_dump_end(cb); 572 return cb->done ? cb->done(cb) : 0; 573 } 574 575 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, 576 struct netlink_callback *cb) 577 { 578 struct net *net = sock_net(skb->sk); 579 struct fib6_walker *w; 580 int res; 581 582 w = (void *)cb->args[2]; 583 w->root = &table->tb6_root; 584 585 if (cb->args[4] == 0) { 586 w->count = 0; 587 w->skip = 0; 588 w->skip_in_node = 0; 589 590 spin_lock_bh(&table->tb6_lock); 591 res = fib6_walk(net, w); 592 spin_unlock_bh(&table->tb6_lock); 593 if (res > 0) { 594 cb->args[4] = 1; 595 cb->args[5] = READ_ONCE(w->root->fn_sernum); 596 } 597 } else { 598 int sernum = READ_ONCE(w->root->fn_sernum); 599 if (cb->args[5] != sernum) { 600 /* Begin at the root if the tree changed */ 601 cb->args[5] = sernum; 602 w->state = FWS_INIT; 603 w->node = w->root; 604 w->skip = w->count; 605 w->skip_in_node = 0; 606 } else 607 w->skip = 0; 608 609 spin_lock_bh(&table->tb6_lock); 610 res = fib6_walk_continue(w); 611 spin_unlock_bh(&table->tb6_lock); 612 if (res <= 0) { 613 fib6_walker_unlink(net, w); 614 cb->args[4] = 0; 615 } 616 } 617 618 return res; 619 } 620 621 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) 622 { 623 struct rt6_rtnl_dump_arg arg = { .filter.dump_exceptions = true, 624 .filter.dump_routes = true }; 625 const struct nlmsghdr *nlh = cb->nlh; 626 struct net *net = sock_net(skb->sk); 627 unsigned int h, s_h; 628 unsigned int e = 0, s_e; 629 struct fib6_walker *w; 630 struct fib6_table *tb; 631 struct hlist_head *head; 632 int res = 0; 633 634 if (cb->strict_check) { 635 int err; 636 637 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb); 638 if (err < 0) 639 return err; 640 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) { 641 struct rtmsg *rtm = nlmsg_data(nlh); 642 643 if (rtm->rtm_flags & RTM_F_PREFIX) 644 arg.filter.flags = RTM_F_PREFIX; 645 } 646 647 w = (void *)cb->args[2]; 648 if (!w) { 649 /* New dump: 650 * 651 * 1. hook callback destructor. 652 */ 653 cb->args[3] = (long)cb->done; 654 cb->done = fib6_dump_done; 655 656 /* 657 * 2. allocate and initialize walker. 658 */ 659 w = kzalloc(sizeof(*w), GFP_ATOMIC); 660 if (!w) 661 return -ENOMEM; 662 w->func = fib6_dump_node; 663 cb->args[2] = (long)w; 664 } 665 666 arg.skb = skb; 667 arg.cb = cb; 668 arg.net = net; 669 w->args = &arg; 670 671 if (arg.filter.table_id) { 672 tb = fib6_get_table(net, arg.filter.table_id); 673 if (!tb) { 674 if (rtnl_msg_family(cb->nlh) != PF_INET6) 675 goto out; 676 677 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist"); 678 return -ENOENT; 679 } 680 681 if (!cb->args[0]) { 682 res = fib6_dump_table(tb, skb, cb); 683 if (!res) 684 cb->args[0] = 1; 685 } 686 goto out; 687 } 688 689 s_h = cb->args[0]; 690 s_e = cb->args[1]; 691 692 rcu_read_lock(); 693 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) { 694 e = 0; 695 head = &net->ipv6.fib_table_hash[h]; 696 hlist_for_each_entry_rcu(tb, head, tb6_hlist) { 697 if (e < s_e) 698 goto next; 699 res = fib6_dump_table(tb, skb, cb); 700 if (res != 0) 701 goto out_unlock; 702 next: 703 e++; 704 } 705 } 706 out_unlock: 707 rcu_read_unlock(); 708 cb->args[1] = e; 709 cb->args[0] = h; 710 out: 711 res = res < 0 ? res : skb->len; 712 if (res <= 0) 713 fib6_dump_end(cb); 714 return res; 715 } 716 717 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val) 718 { 719 if (!f6i) 720 return; 721 722 if (f6i->fib6_metrics == &dst_default_metrics) { 723 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC); 724 725 if (!p) 726 return; 727 728 refcount_set(&p->refcnt, 1); 729 f6i->fib6_metrics = p; 730 } 731 732 f6i->fib6_metrics->metrics[metric - 1] = val; 733 } 734 735 /* 736 * Routing Table 737 * 738 * return the appropriate node for a routing tree "add" operation 739 * by either creating and inserting or by returning an existing 740 * node. 741 */ 742 743 static struct fib6_node *fib6_add_1(struct net *net, 744 struct fib6_table *table, 745 struct fib6_node *root, 746 struct in6_addr *addr, int plen, 747 int offset, int allow_create, 748 int replace_required, 749 struct netlink_ext_ack *extack) 750 { 751 struct fib6_node *fn, *in, *ln; 752 struct fib6_node *pn = NULL; 753 struct rt6key *key; 754 int bit; 755 __be32 dir = 0; 756 757 RT6_TRACE("fib6_add_1\n"); 758 759 /* insert node in tree */ 760 761 fn = root; 762 763 do { 764 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf, 765 lockdep_is_held(&table->tb6_lock)); 766 key = (struct rt6key *)((u8 *)leaf + offset); 767 768 /* 769 * Prefix match 770 */ 771 if (plen < fn->fn_bit || 772 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) { 773 if (!allow_create) { 774 if (replace_required) { 775 NL_SET_ERR_MSG(extack, 776 "Can not replace route - no match found"); 777 pr_warn("Can't replace route, no match found\n"); 778 return ERR_PTR(-ENOENT); 779 } 780 pr_warn("NLM_F_CREATE should be set when creating new route\n"); 781 } 782 goto insert_above; 783 } 784 785 /* 786 * Exact match ? 787 */ 788 789 if (plen == fn->fn_bit) { 790 /* clean up an intermediate node */ 791 if (!(fn->fn_flags & RTN_RTINFO)) { 792 RCU_INIT_POINTER(fn->leaf, NULL); 793 fib6_info_release(leaf); 794 /* remove null_entry in the root node */ 795 } else if (fn->fn_flags & RTN_TL_ROOT && 796 rcu_access_pointer(fn->leaf) == 797 net->ipv6.fib6_null_entry) { 798 RCU_INIT_POINTER(fn->leaf, NULL); 799 } 800 801 return fn; 802 } 803 804 /* 805 * We have more bits to go 806 */ 807 808 /* Try to walk down on tree. */ 809 dir = addr_bit_set(addr, fn->fn_bit); 810 pn = fn; 811 fn = dir ? 812 rcu_dereference_protected(fn->right, 813 lockdep_is_held(&table->tb6_lock)) : 814 rcu_dereference_protected(fn->left, 815 lockdep_is_held(&table->tb6_lock)); 816 } while (fn); 817 818 if (!allow_create) { 819 /* We should not create new node because 820 * NLM_F_REPLACE was specified without NLM_F_CREATE 821 * I assume it is safe to require NLM_F_CREATE when 822 * REPLACE flag is used! Later we may want to remove the 823 * check for replace_required, because according 824 * to netlink specification, NLM_F_CREATE 825 * MUST be specified if new route is created. 826 * That would keep IPv6 consistent with IPv4 827 */ 828 if (replace_required) { 829 NL_SET_ERR_MSG(extack, 830 "Can not replace route - no match found"); 831 pr_warn("Can't replace route, no match found\n"); 832 return ERR_PTR(-ENOENT); 833 } 834 pr_warn("NLM_F_CREATE should be set when creating new route\n"); 835 } 836 /* 837 * We walked to the bottom of tree. 838 * Create new leaf node without children. 839 */ 840 841 ln = node_alloc(net); 842 843 if (!ln) 844 return ERR_PTR(-ENOMEM); 845 ln->fn_bit = plen; 846 RCU_INIT_POINTER(ln->parent, pn); 847 848 if (dir) 849 rcu_assign_pointer(pn->right, ln); 850 else 851 rcu_assign_pointer(pn->left, ln); 852 853 return ln; 854 855 856 insert_above: 857 /* 858 * split since we don't have a common prefix anymore or 859 * we have a less significant route. 860 * we've to insert an intermediate node on the list 861 * this new node will point to the one we need to create 862 * and the current 863 */ 864 865 pn = rcu_dereference_protected(fn->parent, 866 lockdep_is_held(&table->tb6_lock)); 867 868 /* find 1st bit in difference between the 2 addrs. 869 870 See comment in __ipv6_addr_diff: bit may be an invalid value, 871 but if it is >= plen, the value is ignored in any case. 872 */ 873 874 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr)); 875 876 /* 877 * (intermediate)[in] 878 * / \ 879 * (new leaf node)[ln] (old node)[fn] 880 */ 881 if (plen > bit) { 882 in = node_alloc(net); 883 ln = node_alloc(net); 884 885 if (!in || !ln) { 886 if (in) 887 node_free_immediate(net, in); 888 if (ln) 889 node_free_immediate(net, ln); 890 return ERR_PTR(-ENOMEM); 891 } 892 893 /* 894 * new intermediate node. 895 * RTN_RTINFO will 896 * be off since that an address that chooses one of 897 * the branches would not match less specific routes 898 * in the other branch 899 */ 900 901 in->fn_bit = bit; 902 903 RCU_INIT_POINTER(in->parent, pn); 904 in->leaf = fn->leaf; 905 fib6_info_hold(rcu_dereference_protected(in->leaf, 906 lockdep_is_held(&table->tb6_lock))); 907 908 /* update parent pointer */ 909 if (dir) 910 rcu_assign_pointer(pn->right, in); 911 else 912 rcu_assign_pointer(pn->left, in); 913 914 ln->fn_bit = plen; 915 916 RCU_INIT_POINTER(ln->parent, in); 917 rcu_assign_pointer(fn->parent, in); 918 919 if (addr_bit_set(addr, bit)) { 920 rcu_assign_pointer(in->right, ln); 921 rcu_assign_pointer(in->left, fn); 922 } else { 923 rcu_assign_pointer(in->left, ln); 924 rcu_assign_pointer(in->right, fn); 925 } 926 } else { /* plen <= bit */ 927 928 /* 929 * (new leaf node)[ln] 930 * / \ 931 * (old node)[fn] NULL 932 */ 933 934 ln = node_alloc(net); 935 936 if (!ln) 937 return ERR_PTR(-ENOMEM); 938 939 ln->fn_bit = plen; 940 941 RCU_INIT_POINTER(ln->parent, pn); 942 943 if (addr_bit_set(&key->addr, plen)) 944 RCU_INIT_POINTER(ln->right, fn); 945 else 946 RCU_INIT_POINTER(ln->left, fn); 947 948 rcu_assign_pointer(fn->parent, ln); 949 950 if (dir) 951 rcu_assign_pointer(pn->right, ln); 952 else 953 rcu_assign_pointer(pn->left, ln); 954 } 955 return ln; 956 } 957 958 static void __fib6_drop_pcpu_from(struct fib6_nh *fib6_nh, 959 const struct fib6_info *match, 960 const struct fib6_table *table) 961 { 962 int cpu; 963 964 if (!fib6_nh->rt6i_pcpu) 965 return; 966 967 /* release the reference to this fib entry from 968 * all of its cached pcpu routes 969 */ 970 for_each_possible_cpu(cpu) { 971 struct rt6_info **ppcpu_rt; 972 struct rt6_info *pcpu_rt; 973 974 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); 975 pcpu_rt = *ppcpu_rt; 976 977 /* only dropping the 'from' reference if the cached route 978 * is using 'match'. The cached pcpu_rt->from only changes 979 * from a fib6_info to NULL (ip6_dst_destroy); it can never 980 * change from one fib6_info reference to another 981 */ 982 if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) { 983 struct fib6_info *from; 984 985 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 986 fib6_info_release(from); 987 } 988 } 989 } 990 991 struct fib6_nh_pcpu_arg { 992 struct fib6_info *from; 993 const struct fib6_table *table; 994 }; 995 996 static int fib6_nh_drop_pcpu_from(struct fib6_nh *nh, void *_arg) 997 { 998 struct fib6_nh_pcpu_arg *arg = _arg; 999 1000 __fib6_drop_pcpu_from(nh, arg->from, arg->table); 1001 return 0; 1002 } 1003 1004 static void fib6_drop_pcpu_from(struct fib6_info *f6i, 1005 const struct fib6_table *table) 1006 { 1007 /* Make sure rt6_make_pcpu_route() wont add other percpu routes 1008 * while we are cleaning them here. 1009 */ 1010 f6i->fib6_destroying = 1; 1011 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */ 1012 1013 if (f6i->nh) { 1014 struct fib6_nh_pcpu_arg arg = { 1015 .from = f6i, 1016 .table = table 1017 }; 1018 1019 nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_drop_pcpu_from, 1020 &arg); 1021 } else { 1022 struct fib6_nh *fib6_nh; 1023 1024 fib6_nh = f6i->fib6_nh; 1025 __fib6_drop_pcpu_from(fib6_nh, f6i, table); 1026 } 1027 } 1028 1029 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn, 1030 struct net *net) 1031 { 1032 struct fib6_table *table = rt->fib6_table; 1033 1034 /* Flush all cached dst in exception table */ 1035 rt6_flush_exceptions(rt); 1036 fib6_drop_pcpu_from(rt, table); 1037 1038 if (rt->nh && !list_empty(&rt->nh_list)) 1039 list_del_init(&rt->nh_list); 1040 1041 if (refcount_read(&rt->fib6_ref) != 1) { 1042 /* This route is used as dummy address holder in some split 1043 * nodes. It is not leaked, but it still holds other resources, 1044 * which must be released in time. So, scan ascendant nodes 1045 * and replace dummy references to this route with references 1046 * to still alive ones. 1047 */ 1048 while (fn) { 1049 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf, 1050 lockdep_is_held(&table->tb6_lock)); 1051 struct fib6_info *new_leaf; 1052 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) { 1053 new_leaf = fib6_find_prefix(net, table, fn); 1054 fib6_info_hold(new_leaf); 1055 1056 rcu_assign_pointer(fn->leaf, new_leaf); 1057 fib6_info_release(rt); 1058 } 1059 fn = rcu_dereference_protected(fn->parent, 1060 lockdep_is_held(&table->tb6_lock)); 1061 } 1062 } 1063 1064 fib6_clean_expires_locked(rt); 1065 } 1066 1067 /* 1068 * Insert routing information in a node. 1069 */ 1070 1071 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt, 1072 struct nl_info *info, 1073 struct netlink_ext_ack *extack) 1074 { 1075 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf, 1076 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1077 struct fib6_info *iter = NULL; 1078 struct fib6_info __rcu **ins; 1079 struct fib6_info __rcu **fallback_ins = NULL; 1080 int replace = (info->nlh && 1081 (info->nlh->nlmsg_flags & NLM_F_REPLACE)); 1082 int add = (!info->nlh || 1083 (info->nlh->nlmsg_flags & NLM_F_CREATE)); 1084 int found = 0; 1085 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); 1086 bool notify_sibling_rt = false; 1087 u16 nlflags = NLM_F_EXCL; 1088 int err; 1089 1090 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND)) 1091 nlflags |= NLM_F_APPEND; 1092 1093 ins = &fn->leaf; 1094 1095 for (iter = leaf; iter; 1096 iter = rcu_dereference_protected(iter->fib6_next, 1097 lockdep_is_held(&rt->fib6_table->tb6_lock))) { 1098 /* 1099 * Search for duplicates 1100 */ 1101 1102 if (iter->fib6_metric == rt->fib6_metric) { 1103 /* 1104 * Same priority level 1105 */ 1106 if (info->nlh && 1107 (info->nlh->nlmsg_flags & NLM_F_EXCL)) 1108 return -EEXIST; 1109 1110 nlflags &= ~NLM_F_EXCL; 1111 if (replace) { 1112 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) { 1113 found++; 1114 break; 1115 } 1116 fallback_ins = fallback_ins ?: ins; 1117 goto next_iter; 1118 } 1119 1120 if (rt6_duplicate_nexthop(iter, rt)) { 1121 if (rt->fib6_nsiblings) 1122 rt->fib6_nsiblings = 0; 1123 if (!(iter->fib6_flags & RTF_EXPIRES)) 1124 return -EEXIST; 1125 if (!(rt->fib6_flags & RTF_EXPIRES)) 1126 fib6_clean_expires_locked(iter); 1127 else 1128 fib6_set_expires_locked(iter, 1129 rt->expires); 1130 1131 if (rt->fib6_pmtu) 1132 fib6_metric_set(iter, RTAX_MTU, 1133 rt->fib6_pmtu); 1134 return -EEXIST; 1135 } 1136 /* If we have the same destination and the same metric, 1137 * but not the same gateway, then the route we try to 1138 * add is sibling to this route, increment our counter 1139 * of siblings, and later we will add our route to the 1140 * list. 1141 * Only static routes (which don't have flag 1142 * RTF_EXPIRES) are used for ECMPv6. 1143 * 1144 * To avoid long list, we only had siblings if the 1145 * route have a gateway. 1146 */ 1147 if (rt_can_ecmp && 1148 rt6_qualify_for_ecmp(iter)) 1149 rt->fib6_nsiblings++; 1150 } 1151 1152 if (iter->fib6_metric > rt->fib6_metric) 1153 break; 1154 1155 next_iter: 1156 ins = &iter->fib6_next; 1157 } 1158 1159 if (fallback_ins && !found) { 1160 /* No matching route with same ecmp-able-ness found, replace 1161 * first matching route 1162 */ 1163 ins = fallback_ins; 1164 iter = rcu_dereference_protected(*ins, 1165 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1166 found++; 1167 } 1168 1169 /* Reset round-robin state, if necessary */ 1170 if (ins == &fn->leaf) 1171 fn->rr_ptr = NULL; 1172 1173 /* Link this route to others same route. */ 1174 if (rt->fib6_nsiblings) { 1175 unsigned int fib6_nsiblings; 1176 struct fib6_info *sibling, *temp_sibling; 1177 1178 /* Find the first route that have the same metric */ 1179 sibling = leaf; 1180 notify_sibling_rt = true; 1181 while (sibling) { 1182 if (sibling->fib6_metric == rt->fib6_metric && 1183 rt6_qualify_for_ecmp(sibling)) { 1184 list_add_tail(&rt->fib6_siblings, 1185 &sibling->fib6_siblings); 1186 break; 1187 } 1188 sibling = rcu_dereference_protected(sibling->fib6_next, 1189 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1190 notify_sibling_rt = false; 1191 } 1192 /* For each sibling in the list, increment the counter of 1193 * siblings. BUG() if counters does not match, list of siblings 1194 * is broken! 1195 */ 1196 fib6_nsiblings = 0; 1197 list_for_each_entry_safe(sibling, temp_sibling, 1198 &rt->fib6_siblings, fib6_siblings) { 1199 sibling->fib6_nsiblings++; 1200 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings); 1201 fib6_nsiblings++; 1202 } 1203 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings); 1204 rt6_multipath_rebalance(temp_sibling); 1205 } 1206 1207 /* 1208 * insert node 1209 */ 1210 if (!replace) { 1211 if (!add) 1212 pr_warn("NLM_F_CREATE should be set when creating new route\n"); 1213 1214 add: 1215 nlflags |= NLM_F_CREATE; 1216 1217 /* The route should only be notified if it is the first 1218 * route in the node or if it is added as a sibling 1219 * route to the first route in the node. 1220 */ 1221 if (!info->skip_notify_kernel && 1222 (notify_sibling_rt || ins == &fn->leaf)) { 1223 enum fib_event_type fib_event; 1224 1225 if (notify_sibling_rt) 1226 fib_event = FIB_EVENT_ENTRY_APPEND; 1227 else 1228 fib_event = FIB_EVENT_ENTRY_REPLACE; 1229 err = call_fib6_entry_notifiers(info->nl_net, 1230 fib_event, rt, 1231 extack); 1232 if (err) { 1233 struct fib6_info *sibling, *next_sibling; 1234 1235 /* If the route has siblings, then it first 1236 * needs to be unlinked from them. 1237 */ 1238 if (!rt->fib6_nsiblings) 1239 return err; 1240 1241 list_for_each_entry_safe(sibling, next_sibling, 1242 &rt->fib6_siblings, 1243 fib6_siblings) 1244 sibling->fib6_nsiblings--; 1245 rt->fib6_nsiblings = 0; 1246 list_del_init(&rt->fib6_siblings); 1247 rt6_multipath_rebalance(next_sibling); 1248 return err; 1249 } 1250 } 1251 1252 rcu_assign_pointer(rt->fib6_next, iter); 1253 fib6_info_hold(rt); 1254 rcu_assign_pointer(rt->fib6_node, fn); 1255 rcu_assign_pointer(*ins, rt); 1256 if (!info->skip_notify) 1257 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 1258 info->nl_net->ipv6.rt6_stats->fib_rt_entries++; 1259 1260 if (!(fn->fn_flags & RTN_RTINFO)) { 1261 info->nl_net->ipv6.rt6_stats->fib_route_nodes++; 1262 fn->fn_flags |= RTN_RTINFO; 1263 } 1264 1265 } else { 1266 int nsiblings; 1267 1268 if (!found) { 1269 if (add) 1270 goto add; 1271 pr_warn("NLM_F_REPLACE set, but no existing node found!\n"); 1272 return -ENOENT; 1273 } 1274 1275 if (!info->skip_notify_kernel && ins == &fn->leaf) { 1276 err = call_fib6_entry_notifiers(info->nl_net, 1277 FIB_EVENT_ENTRY_REPLACE, 1278 rt, extack); 1279 if (err) 1280 return err; 1281 } 1282 1283 fib6_info_hold(rt); 1284 rcu_assign_pointer(rt->fib6_node, fn); 1285 rt->fib6_next = iter->fib6_next; 1286 rcu_assign_pointer(*ins, rt); 1287 if (!info->skip_notify) 1288 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE); 1289 if (!(fn->fn_flags & RTN_RTINFO)) { 1290 info->nl_net->ipv6.rt6_stats->fib_route_nodes++; 1291 fn->fn_flags |= RTN_RTINFO; 1292 } 1293 nsiblings = iter->fib6_nsiblings; 1294 iter->fib6_node = NULL; 1295 fib6_purge_rt(iter, fn, info->nl_net); 1296 if (rcu_access_pointer(fn->rr_ptr) == iter) 1297 fn->rr_ptr = NULL; 1298 fib6_info_release(iter); 1299 1300 if (nsiblings) { 1301 /* Replacing an ECMP route, remove all siblings */ 1302 ins = &rt->fib6_next; 1303 iter = rcu_dereference_protected(*ins, 1304 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1305 while (iter) { 1306 if (iter->fib6_metric > rt->fib6_metric) 1307 break; 1308 if (rt6_qualify_for_ecmp(iter)) { 1309 *ins = iter->fib6_next; 1310 iter->fib6_node = NULL; 1311 fib6_purge_rt(iter, fn, info->nl_net); 1312 if (rcu_access_pointer(fn->rr_ptr) == iter) 1313 fn->rr_ptr = NULL; 1314 fib6_info_release(iter); 1315 nsiblings--; 1316 info->nl_net->ipv6.rt6_stats->fib_rt_entries--; 1317 } else { 1318 ins = &iter->fib6_next; 1319 } 1320 iter = rcu_dereference_protected(*ins, 1321 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1322 } 1323 WARN_ON(nsiblings != 0); 1324 } 1325 } 1326 1327 return 0; 1328 } 1329 1330 static void fib6_start_gc(struct net *net, struct fib6_info *rt) 1331 { 1332 if (!timer_pending(&net->ipv6.ip6_fib_timer) && 1333 (rt->fib6_flags & RTF_EXPIRES)) 1334 mod_timer(&net->ipv6.ip6_fib_timer, 1335 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); 1336 } 1337 1338 void fib6_force_start_gc(struct net *net) 1339 { 1340 if (!timer_pending(&net->ipv6.ip6_fib_timer)) 1341 mod_timer(&net->ipv6.ip6_fib_timer, 1342 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); 1343 } 1344 1345 static void __fib6_update_sernum_upto_root(struct fib6_info *rt, 1346 int sernum) 1347 { 1348 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node, 1349 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1350 1351 /* paired with smp_rmb() in fib6_get_cookie_safe() */ 1352 smp_wmb(); 1353 while (fn) { 1354 WRITE_ONCE(fn->fn_sernum, sernum); 1355 fn = rcu_dereference_protected(fn->parent, 1356 lockdep_is_held(&rt->fib6_table->tb6_lock)); 1357 } 1358 } 1359 1360 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt) 1361 { 1362 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net)); 1363 } 1364 1365 /* allow ipv4 to update sernum via ipv6_stub */ 1366 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i) 1367 { 1368 spin_lock_bh(&f6i->fib6_table->tb6_lock); 1369 fib6_update_sernum_upto_root(net, f6i); 1370 spin_unlock_bh(&f6i->fib6_table->tb6_lock); 1371 } 1372 1373 /* 1374 * Add routing information to the routing tree. 1375 * <destination addr>/<source addr> 1376 * with source addr info in sub-trees 1377 * Need to own table->tb6_lock 1378 */ 1379 1380 int fib6_add(struct fib6_node *root, struct fib6_info *rt, 1381 struct nl_info *info, struct netlink_ext_ack *extack) 1382 { 1383 struct fib6_table *table = rt->fib6_table; 1384 struct fib6_node *fn, *pn = NULL; 1385 int err = -ENOMEM; 1386 int allow_create = 1; 1387 int replace_required = 0; 1388 1389 if (info->nlh) { 1390 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) 1391 allow_create = 0; 1392 if (info->nlh->nlmsg_flags & NLM_F_REPLACE) 1393 replace_required = 1; 1394 } 1395 if (!allow_create && !replace_required) 1396 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n"); 1397 1398 fn = fib6_add_1(info->nl_net, table, root, 1399 &rt->fib6_dst.addr, rt->fib6_dst.plen, 1400 offsetof(struct fib6_info, fib6_dst), allow_create, 1401 replace_required, extack); 1402 if (IS_ERR(fn)) { 1403 err = PTR_ERR(fn); 1404 fn = NULL; 1405 goto out; 1406 } 1407 1408 pn = fn; 1409 1410 #ifdef CONFIG_IPV6_SUBTREES 1411 if (rt->fib6_src.plen) { 1412 struct fib6_node *sn; 1413 1414 if (!rcu_access_pointer(fn->subtree)) { 1415 struct fib6_node *sfn; 1416 1417 /* 1418 * Create subtree. 1419 * 1420 * fn[main tree] 1421 * | 1422 * sfn[subtree root] 1423 * \ 1424 * sn[new leaf node] 1425 */ 1426 1427 /* Create subtree root node */ 1428 sfn = node_alloc(info->nl_net); 1429 if (!sfn) 1430 goto failure; 1431 1432 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry); 1433 rcu_assign_pointer(sfn->leaf, 1434 info->nl_net->ipv6.fib6_null_entry); 1435 sfn->fn_flags = RTN_ROOT; 1436 1437 /* Now add the first leaf node to new subtree */ 1438 1439 sn = fib6_add_1(info->nl_net, table, sfn, 1440 &rt->fib6_src.addr, rt->fib6_src.plen, 1441 offsetof(struct fib6_info, fib6_src), 1442 allow_create, replace_required, extack); 1443 1444 if (IS_ERR(sn)) { 1445 /* If it is failed, discard just allocated 1446 root, and then (in failure) stale node 1447 in main tree. 1448 */ 1449 node_free_immediate(info->nl_net, sfn); 1450 err = PTR_ERR(sn); 1451 goto failure; 1452 } 1453 1454 /* Now link new subtree to main tree */ 1455 rcu_assign_pointer(sfn->parent, fn); 1456 rcu_assign_pointer(fn->subtree, sfn); 1457 } else { 1458 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn), 1459 &rt->fib6_src.addr, rt->fib6_src.plen, 1460 offsetof(struct fib6_info, fib6_src), 1461 allow_create, replace_required, extack); 1462 1463 if (IS_ERR(sn)) { 1464 err = PTR_ERR(sn); 1465 goto failure; 1466 } 1467 } 1468 1469 if (!rcu_access_pointer(fn->leaf)) { 1470 if (fn->fn_flags & RTN_TL_ROOT) { 1471 /* put back null_entry for root node */ 1472 rcu_assign_pointer(fn->leaf, 1473 info->nl_net->ipv6.fib6_null_entry); 1474 } else { 1475 fib6_info_hold(rt); 1476 rcu_assign_pointer(fn->leaf, rt); 1477 } 1478 } 1479 fn = sn; 1480 } 1481 #endif 1482 1483 err = fib6_add_rt2node(fn, rt, info, extack); 1484 if (!err) { 1485 if (rt->nh) 1486 list_add(&rt->nh_list, &rt->nh->f6i_list); 1487 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(info->nl_net)); 1488 1489 if (fib6_has_expires(rt)) 1490 hlist_add_head(&rt->gc_link, &table->tb6_gc_hlist); 1491 1492 fib6_start_gc(info->nl_net, rt); 1493 } 1494 1495 out: 1496 if (err) { 1497 #ifdef CONFIG_IPV6_SUBTREES 1498 /* 1499 * If fib6_add_1 has cleared the old leaf pointer in the 1500 * super-tree leaf node we have to find a new one for it. 1501 */ 1502 if (pn != fn) { 1503 struct fib6_info *pn_leaf = 1504 rcu_dereference_protected(pn->leaf, 1505 lockdep_is_held(&table->tb6_lock)); 1506 if (pn_leaf == rt) { 1507 pn_leaf = NULL; 1508 RCU_INIT_POINTER(pn->leaf, NULL); 1509 fib6_info_release(rt); 1510 } 1511 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) { 1512 pn_leaf = fib6_find_prefix(info->nl_net, table, 1513 pn); 1514 if (!pn_leaf) 1515 pn_leaf = 1516 info->nl_net->ipv6.fib6_null_entry; 1517 fib6_info_hold(pn_leaf); 1518 rcu_assign_pointer(pn->leaf, pn_leaf); 1519 } 1520 } 1521 #endif 1522 goto failure; 1523 } else if (fib6_requires_src(rt)) { 1524 fib6_routes_require_src_inc(info->nl_net); 1525 } 1526 return err; 1527 1528 failure: 1529 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if: 1530 * 1. fn is an intermediate node and we failed to add the new 1531 * route to it in both subtree creation failure and fib6_add_rt2node() 1532 * failure case. 1533 * 2. fn is the root node in the table and we fail to add the first 1534 * default route to it. 1535 */ 1536 if (fn && 1537 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) || 1538 (fn->fn_flags & RTN_TL_ROOT && 1539 !rcu_access_pointer(fn->leaf)))) 1540 fib6_repair_tree(info->nl_net, table, fn); 1541 return err; 1542 } 1543 1544 /* 1545 * Routing tree lookup 1546 * 1547 */ 1548 1549 struct lookup_args { 1550 int offset; /* key offset on fib6_info */ 1551 const struct in6_addr *addr; /* search key */ 1552 }; 1553 1554 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root, 1555 struct lookup_args *args) 1556 { 1557 struct fib6_node *fn; 1558 __be32 dir; 1559 1560 if (unlikely(args->offset == 0)) 1561 return NULL; 1562 1563 /* 1564 * Descend on a tree 1565 */ 1566 1567 fn = root; 1568 1569 for (;;) { 1570 struct fib6_node *next; 1571 1572 dir = addr_bit_set(args->addr, fn->fn_bit); 1573 1574 next = dir ? rcu_dereference(fn->right) : 1575 rcu_dereference(fn->left); 1576 1577 if (next) { 1578 fn = next; 1579 continue; 1580 } 1581 break; 1582 } 1583 1584 while (fn) { 1585 struct fib6_node *subtree = FIB6_SUBTREE(fn); 1586 1587 if (subtree || fn->fn_flags & RTN_RTINFO) { 1588 struct fib6_info *leaf = rcu_dereference(fn->leaf); 1589 struct rt6key *key; 1590 1591 if (!leaf) 1592 goto backtrack; 1593 1594 key = (struct rt6key *) ((u8 *)leaf + args->offset); 1595 1596 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) { 1597 #ifdef CONFIG_IPV6_SUBTREES 1598 if (subtree) { 1599 struct fib6_node *sfn; 1600 sfn = fib6_node_lookup_1(subtree, 1601 args + 1); 1602 if (!sfn) 1603 goto backtrack; 1604 fn = sfn; 1605 } 1606 #endif 1607 if (fn->fn_flags & RTN_RTINFO) 1608 return fn; 1609 } 1610 } 1611 backtrack: 1612 if (fn->fn_flags & RTN_ROOT) 1613 break; 1614 1615 fn = rcu_dereference(fn->parent); 1616 } 1617 1618 return NULL; 1619 } 1620 1621 /* called with rcu_read_lock() held 1622 */ 1623 struct fib6_node *fib6_node_lookup(struct fib6_node *root, 1624 const struct in6_addr *daddr, 1625 const struct in6_addr *saddr) 1626 { 1627 struct fib6_node *fn; 1628 struct lookup_args args[] = { 1629 { 1630 .offset = offsetof(struct fib6_info, fib6_dst), 1631 .addr = daddr, 1632 }, 1633 #ifdef CONFIG_IPV6_SUBTREES 1634 { 1635 .offset = offsetof(struct fib6_info, fib6_src), 1636 .addr = saddr, 1637 }, 1638 #endif 1639 { 1640 .offset = 0, /* sentinel */ 1641 } 1642 }; 1643 1644 fn = fib6_node_lookup_1(root, daddr ? args : args + 1); 1645 if (!fn || fn->fn_flags & RTN_TL_ROOT) 1646 fn = root; 1647 1648 return fn; 1649 } 1650 1651 /* 1652 * Get node with specified destination prefix (and source prefix, 1653 * if subtrees are used) 1654 * exact_match == true means we try to find fn with exact match of 1655 * the passed in prefix addr 1656 * exact_match == false means we try to find fn with longest prefix 1657 * match of the passed in prefix addr. This is useful for finding fn 1658 * for cached route as it will be stored in the exception table under 1659 * the node with longest prefix length. 1660 */ 1661 1662 1663 static struct fib6_node *fib6_locate_1(struct fib6_node *root, 1664 const struct in6_addr *addr, 1665 int plen, int offset, 1666 bool exact_match) 1667 { 1668 struct fib6_node *fn, *prev = NULL; 1669 1670 for (fn = root; fn ; ) { 1671 struct fib6_info *leaf = rcu_dereference(fn->leaf); 1672 struct rt6key *key; 1673 1674 /* This node is being deleted */ 1675 if (!leaf) { 1676 if (plen <= fn->fn_bit) 1677 goto out; 1678 else 1679 goto next; 1680 } 1681 1682 key = (struct rt6key *)((u8 *)leaf + offset); 1683 1684 /* 1685 * Prefix match 1686 */ 1687 if (plen < fn->fn_bit || 1688 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) 1689 goto out; 1690 1691 if (plen == fn->fn_bit) 1692 return fn; 1693 1694 if (fn->fn_flags & RTN_RTINFO) 1695 prev = fn; 1696 1697 next: 1698 /* 1699 * We have more bits to go 1700 */ 1701 if (addr_bit_set(addr, fn->fn_bit)) 1702 fn = rcu_dereference(fn->right); 1703 else 1704 fn = rcu_dereference(fn->left); 1705 } 1706 out: 1707 if (exact_match) 1708 return NULL; 1709 else 1710 return prev; 1711 } 1712 1713 struct fib6_node *fib6_locate(struct fib6_node *root, 1714 const struct in6_addr *daddr, int dst_len, 1715 const struct in6_addr *saddr, int src_len, 1716 bool exact_match) 1717 { 1718 struct fib6_node *fn; 1719 1720 fn = fib6_locate_1(root, daddr, dst_len, 1721 offsetof(struct fib6_info, fib6_dst), 1722 exact_match); 1723 1724 #ifdef CONFIG_IPV6_SUBTREES 1725 if (src_len) { 1726 WARN_ON(saddr == NULL); 1727 if (fn) { 1728 struct fib6_node *subtree = FIB6_SUBTREE(fn); 1729 1730 if (subtree) { 1731 fn = fib6_locate_1(subtree, saddr, src_len, 1732 offsetof(struct fib6_info, fib6_src), 1733 exact_match); 1734 } 1735 } 1736 } 1737 #endif 1738 1739 if (fn && fn->fn_flags & RTN_RTINFO) 1740 return fn; 1741 1742 return NULL; 1743 } 1744 1745 1746 /* 1747 * Deletion 1748 * 1749 */ 1750 1751 static struct fib6_info *fib6_find_prefix(struct net *net, 1752 struct fib6_table *table, 1753 struct fib6_node *fn) 1754 { 1755 struct fib6_node *child_left, *child_right; 1756 1757 if (fn->fn_flags & RTN_ROOT) 1758 return net->ipv6.fib6_null_entry; 1759 1760 while (fn) { 1761 child_left = rcu_dereference_protected(fn->left, 1762 lockdep_is_held(&table->tb6_lock)); 1763 child_right = rcu_dereference_protected(fn->right, 1764 lockdep_is_held(&table->tb6_lock)); 1765 if (child_left) 1766 return rcu_dereference_protected(child_left->leaf, 1767 lockdep_is_held(&table->tb6_lock)); 1768 if (child_right) 1769 return rcu_dereference_protected(child_right->leaf, 1770 lockdep_is_held(&table->tb6_lock)); 1771 1772 fn = FIB6_SUBTREE(fn); 1773 } 1774 return NULL; 1775 } 1776 1777 /* 1778 * Called to trim the tree of intermediate nodes when possible. "fn" 1779 * is the node we want to try and remove. 1780 * Need to own table->tb6_lock 1781 */ 1782 1783 static struct fib6_node *fib6_repair_tree(struct net *net, 1784 struct fib6_table *table, 1785 struct fib6_node *fn) 1786 { 1787 int children; 1788 int nstate; 1789 struct fib6_node *child; 1790 struct fib6_walker *w; 1791 int iter = 0; 1792 1793 /* Set fn->leaf to null_entry for root node. */ 1794 if (fn->fn_flags & RTN_TL_ROOT) { 1795 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry); 1796 return fn; 1797 } 1798 1799 for (;;) { 1800 struct fib6_node *fn_r = rcu_dereference_protected(fn->right, 1801 lockdep_is_held(&table->tb6_lock)); 1802 struct fib6_node *fn_l = rcu_dereference_protected(fn->left, 1803 lockdep_is_held(&table->tb6_lock)); 1804 struct fib6_node *pn = rcu_dereference_protected(fn->parent, 1805 lockdep_is_held(&table->tb6_lock)); 1806 struct fib6_node *pn_r = rcu_dereference_protected(pn->right, 1807 lockdep_is_held(&table->tb6_lock)); 1808 struct fib6_node *pn_l = rcu_dereference_protected(pn->left, 1809 lockdep_is_held(&table->tb6_lock)); 1810 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf, 1811 lockdep_is_held(&table->tb6_lock)); 1812 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf, 1813 lockdep_is_held(&table->tb6_lock)); 1814 struct fib6_info *new_fn_leaf; 1815 1816 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter); 1817 iter++; 1818 1819 WARN_ON(fn->fn_flags & RTN_RTINFO); 1820 WARN_ON(fn->fn_flags & RTN_TL_ROOT); 1821 WARN_ON(fn_leaf); 1822 1823 children = 0; 1824 child = NULL; 1825 if (fn_r) { 1826 child = fn_r; 1827 children |= 1; 1828 } 1829 if (fn_l) { 1830 child = fn_l; 1831 children |= 2; 1832 } 1833 1834 if (children == 3 || FIB6_SUBTREE(fn) 1835 #ifdef CONFIG_IPV6_SUBTREES 1836 /* Subtree root (i.e. fn) may have one child */ 1837 || (children && fn->fn_flags & RTN_ROOT) 1838 #endif 1839 ) { 1840 new_fn_leaf = fib6_find_prefix(net, table, fn); 1841 #if RT6_DEBUG >= 2 1842 if (!new_fn_leaf) { 1843 WARN_ON(!new_fn_leaf); 1844 new_fn_leaf = net->ipv6.fib6_null_entry; 1845 } 1846 #endif 1847 fib6_info_hold(new_fn_leaf); 1848 rcu_assign_pointer(fn->leaf, new_fn_leaf); 1849 return pn; 1850 } 1851 1852 #ifdef CONFIG_IPV6_SUBTREES 1853 if (FIB6_SUBTREE(pn) == fn) { 1854 WARN_ON(!(fn->fn_flags & RTN_ROOT)); 1855 RCU_INIT_POINTER(pn->subtree, NULL); 1856 nstate = FWS_L; 1857 } else { 1858 WARN_ON(fn->fn_flags & RTN_ROOT); 1859 #endif 1860 if (pn_r == fn) 1861 rcu_assign_pointer(pn->right, child); 1862 else if (pn_l == fn) 1863 rcu_assign_pointer(pn->left, child); 1864 #if RT6_DEBUG >= 2 1865 else 1866 WARN_ON(1); 1867 #endif 1868 if (child) 1869 rcu_assign_pointer(child->parent, pn); 1870 nstate = FWS_R; 1871 #ifdef CONFIG_IPV6_SUBTREES 1872 } 1873 #endif 1874 1875 read_lock(&net->ipv6.fib6_walker_lock); 1876 FOR_WALKERS(net, w) { 1877 if (!child) { 1878 if (w->node == fn) { 1879 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); 1880 w->node = pn; 1881 w->state = nstate; 1882 } 1883 } else { 1884 if (w->node == fn) { 1885 w->node = child; 1886 if (children&2) { 1887 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); 1888 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT; 1889 } else { 1890 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); 1891 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT; 1892 } 1893 } 1894 } 1895 } 1896 read_unlock(&net->ipv6.fib6_walker_lock); 1897 1898 node_free(net, fn); 1899 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn)) 1900 return pn; 1901 1902 RCU_INIT_POINTER(pn->leaf, NULL); 1903 fib6_info_release(pn_leaf); 1904 fn = pn; 1905 } 1906 } 1907 1908 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn, 1909 struct fib6_info __rcu **rtp, struct nl_info *info) 1910 { 1911 struct fib6_info *leaf, *replace_rt = NULL; 1912 struct fib6_walker *w; 1913 struct fib6_info *rt = rcu_dereference_protected(*rtp, 1914 lockdep_is_held(&table->tb6_lock)); 1915 struct net *net = info->nl_net; 1916 bool notify_del = false; 1917 1918 RT6_TRACE("fib6_del_route\n"); 1919 1920 /* If the deleted route is the first in the node and it is not part of 1921 * a multipath route, then we need to replace it with the next route 1922 * in the node, if exists. 1923 */ 1924 leaf = rcu_dereference_protected(fn->leaf, 1925 lockdep_is_held(&table->tb6_lock)); 1926 if (leaf == rt && !rt->fib6_nsiblings) { 1927 if (rcu_access_pointer(rt->fib6_next)) 1928 replace_rt = rcu_dereference_protected(rt->fib6_next, 1929 lockdep_is_held(&table->tb6_lock)); 1930 else 1931 notify_del = true; 1932 } 1933 1934 /* Unlink it */ 1935 *rtp = rt->fib6_next; 1936 rt->fib6_node = NULL; 1937 net->ipv6.rt6_stats->fib_rt_entries--; 1938 net->ipv6.rt6_stats->fib_discarded_routes++; 1939 1940 /* Reset round-robin state, if necessary */ 1941 if (rcu_access_pointer(fn->rr_ptr) == rt) 1942 fn->rr_ptr = NULL; 1943 1944 /* Remove this entry from other siblings */ 1945 if (rt->fib6_nsiblings) { 1946 struct fib6_info *sibling, *next_sibling; 1947 1948 /* The route is deleted from a multipath route. If this 1949 * multipath route is the first route in the node, then we need 1950 * to emit a delete notification. Otherwise, we need to skip 1951 * the notification. 1952 */ 1953 if (rt->fib6_metric == leaf->fib6_metric && 1954 rt6_qualify_for_ecmp(leaf)) 1955 notify_del = true; 1956 list_for_each_entry_safe(sibling, next_sibling, 1957 &rt->fib6_siblings, fib6_siblings) 1958 sibling->fib6_nsiblings--; 1959 rt->fib6_nsiblings = 0; 1960 list_del_init(&rt->fib6_siblings); 1961 rt6_multipath_rebalance(next_sibling); 1962 } 1963 1964 /* Adjust walkers */ 1965 read_lock(&net->ipv6.fib6_walker_lock); 1966 FOR_WALKERS(net, w) { 1967 if (w->state == FWS_C && w->leaf == rt) { 1968 RT6_TRACE("walker %p adjusted by delroute\n", w); 1969 w->leaf = rcu_dereference_protected(rt->fib6_next, 1970 lockdep_is_held(&table->tb6_lock)); 1971 if (!w->leaf) 1972 w->state = FWS_U; 1973 } 1974 } 1975 read_unlock(&net->ipv6.fib6_walker_lock); 1976 1977 /* If it was last route, call fib6_repair_tree() to: 1978 * 1. For root node, put back null_entry as how the table was created. 1979 * 2. For other nodes, expunge its radix tree node. 1980 */ 1981 if (!rcu_access_pointer(fn->leaf)) { 1982 if (!(fn->fn_flags & RTN_TL_ROOT)) { 1983 fn->fn_flags &= ~RTN_RTINFO; 1984 net->ipv6.rt6_stats->fib_route_nodes--; 1985 } 1986 fn = fib6_repair_tree(net, table, fn); 1987 } 1988 1989 fib6_purge_rt(rt, fn, net); 1990 1991 if (!info->skip_notify_kernel) { 1992 if (notify_del) 1993 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, 1994 rt, NULL); 1995 else if (replace_rt) 1996 call_fib6_entry_notifiers_replace(net, replace_rt); 1997 } 1998 if (!info->skip_notify) 1999 inet6_rt_notify(RTM_DELROUTE, rt, info, 0); 2000 2001 fib6_info_release(rt); 2002 } 2003 2004 /* Need to own table->tb6_lock */ 2005 int fib6_del(struct fib6_info *rt, struct nl_info *info) 2006 { 2007 struct net *net = info->nl_net; 2008 struct fib6_info __rcu **rtp; 2009 struct fib6_info __rcu **rtp_next; 2010 struct fib6_table *table; 2011 struct fib6_node *fn; 2012 2013 if (rt == net->ipv6.fib6_null_entry) 2014 return -ENOENT; 2015 2016 table = rt->fib6_table; 2017 fn = rcu_dereference_protected(rt->fib6_node, 2018 lockdep_is_held(&table->tb6_lock)); 2019 if (!fn) 2020 return -ENOENT; 2021 2022 WARN_ON(!(fn->fn_flags & RTN_RTINFO)); 2023 2024 /* 2025 * Walk the leaf entries looking for ourself 2026 */ 2027 2028 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) { 2029 struct fib6_info *cur = rcu_dereference_protected(*rtp, 2030 lockdep_is_held(&table->tb6_lock)); 2031 if (rt == cur) { 2032 if (fib6_requires_src(cur)) 2033 fib6_routes_require_src_dec(info->nl_net); 2034 fib6_del_route(table, fn, rtp, info); 2035 return 0; 2036 } 2037 rtp_next = &cur->fib6_next; 2038 } 2039 return -ENOENT; 2040 } 2041 2042 /* 2043 * Tree traversal function. 2044 * 2045 * Certainly, it is not interrupt safe. 2046 * However, it is internally reenterable wrt itself and fib6_add/fib6_del. 2047 * It means, that we can modify tree during walking 2048 * and use this function for garbage collection, clone pruning, 2049 * cleaning tree when a device goes down etc. etc. 2050 * 2051 * It guarantees that every node will be traversed, 2052 * and that it will be traversed only once. 2053 * 2054 * Callback function w->func may return: 2055 * 0 -> continue walking. 2056 * positive value -> walking is suspended (used by tree dumps, 2057 * and probably by gc, if it will be split to several slices) 2058 * negative value -> terminate walking. 2059 * 2060 * The function itself returns: 2061 * 0 -> walk is complete. 2062 * >0 -> walk is incomplete (i.e. suspended) 2063 * <0 -> walk is terminated by an error. 2064 * 2065 * This function is called with tb6_lock held. 2066 */ 2067 2068 static int fib6_walk_continue(struct fib6_walker *w) 2069 { 2070 struct fib6_node *fn, *pn, *left, *right; 2071 2072 /* w->root should always be table->tb6_root */ 2073 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT)); 2074 2075 for (;;) { 2076 fn = w->node; 2077 if (!fn) 2078 return 0; 2079 2080 switch (w->state) { 2081 #ifdef CONFIG_IPV6_SUBTREES 2082 case FWS_S: 2083 if (FIB6_SUBTREE(fn)) { 2084 w->node = FIB6_SUBTREE(fn); 2085 continue; 2086 } 2087 w->state = FWS_L; 2088 fallthrough; 2089 #endif 2090 case FWS_L: 2091 left = rcu_dereference_protected(fn->left, 1); 2092 if (left) { 2093 w->node = left; 2094 w->state = FWS_INIT; 2095 continue; 2096 } 2097 w->state = FWS_R; 2098 fallthrough; 2099 case FWS_R: 2100 right = rcu_dereference_protected(fn->right, 1); 2101 if (right) { 2102 w->node = right; 2103 w->state = FWS_INIT; 2104 continue; 2105 } 2106 w->state = FWS_C; 2107 w->leaf = rcu_dereference_protected(fn->leaf, 1); 2108 fallthrough; 2109 case FWS_C: 2110 if (w->leaf && fn->fn_flags & RTN_RTINFO) { 2111 int err; 2112 2113 if (w->skip) { 2114 w->skip--; 2115 goto skip; 2116 } 2117 2118 err = w->func(w); 2119 if (err) 2120 return err; 2121 2122 w->count++; 2123 continue; 2124 } 2125 skip: 2126 w->state = FWS_U; 2127 fallthrough; 2128 case FWS_U: 2129 if (fn == w->root) 2130 return 0; 2131 pn = rcu_dereference_protected(fn->parent, 1); 2132 left = rcu_dereference_protected(pn->left, 1); 2133 right = rcu_dereference_protected(pn->right, 1); 2134 w->node = pn; 2135 #ifdef CONFIG_IPV6_SUBTREES 2136 if (FIB6_SUBTREE(pn) == fn) { 2137 WARN_ON(!(fn->fn_flags & RTN_ROOT)); 2138 w->state = FWS_L; 2139 continue; 2140 } 2141 #endif 2142 if (left == fn) { 2143 w->state = FWS_R; 2144 continue; 2145 } 2146 if (right == fn) { 2147 w->state = FWS_C; 2148 w->leaf = rcu_dereference_protected(w->node->leaf, 1); 2149 continue; 2150 } 2151 #if RT6_DEBUG >= 2 2152 WARN_ON(1); 2153 #endif 2154 } 2155 } 2156 } 2157 2158 static int fib6_walk(struct net *net, struct fib6_walker *w) 2159 { 2160 int res; 2161 2162 w->state = FWS_INIT; 2163 w->node = w->root; 2164 2165 fib6_walker_link(net, w); 2166 res = fib6_walk_continue(w); 2167 if (res <= 0) 2168 fib6_walker_unlink(net, w); 2169 return res; 2170 } 2171 2172 static int fib6_clean_node(struct fib6_walker *w) 2173 { 2174 int res; 2175 struct fib6_info *rt; 2176 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w); 2177 struct nl_info info = { 2178 .nl_net = c->net, 2179 .skip_notify = c->skip_notify, 2180 }; 2181 2182 if (c->sernum != FIB6_NO_SERNUM_CHANGE && 2183 READ_ONCE(w->node->fn_sernum) != c->sernum) 2184 WRITE_ONCE(w->node->fn_sernum, c->sernum); 2185 2186 if (!c->func) { 2187 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE); 2188 w->leaf = NULL; 2189 return 0; 2190 } 2191 2192 for_each_fib6_walker_rt(w) { 2193 res = c->func(rt, c->arg); 2194 if (res == -1) { 2195 w->leaf = rt; 2196 res = fib6_del(rt, &info); 2197 if (res) { 2198 #if RT6_DEBUG >= 2 2199 pr_debug("%s: del failed: rt=%p@%p err=%d\n", 2200 __func__, rt, 2201 rcu_access_pointer(rt->fib6_node), 2202 res); 2203 #endif 2204 continue; 2205 } 2206 return 0; 2207 } else if (res == -2) { 2208 if (WARN_ON(!rt->fib6_nsiblings)) 2209 continue; 2210 rt = list_last_entry(&rt->fib6_siblings, 2211 struct fib6_info, fib6_siblings); 2212 continue; 2213 } 2214 WARN_ON(res != 0); 2215 } 2216 w->leaf = rt; 2217 return 0; 2218 } 2219 2220 /* 2221 * Convenient frontend to tree walker. 2222 * 2223 * func is called on each route. 2224 * It may return -2 -> skip multipath route. 2225 * -1 -> delete this route. 2226 * 0 -> continue walking 2227 */ 2228 2229 static void fib6_clean_tree(struct net *net, struct fib6_node *root, 2230 int (*func)(struct fib6_info *, void *arg), 2231 int sernum, void *arg, bool skip_notify) 2232 { 2233 struct fib6_cleaner c; 2234 2235 c.w.root = root; 2236 c.w.func = fib6_clean_node; 2237 c.w.count = 0; 2238 c.w.skip = 0; 2239 c.w.skip_in_node = 0; 2240 c.func = func; 2241 c.sernum = sernum; 2242 c.arg = arg; 2243 c.net = net; 2244 c.skip_notify = skip_notify; 2245 2246 fib6_walk(net, &c.w); 2247 } 2248 2249 static void __fib6_clean_all(struct net *net, 2250 int (*func)(struct fib6_info *, void *), 2251 int sernum, void *arg, bool skip_notify) 2252 { 2253 struct fib6_table *table; 2254 struct hlist_head *head; 2255 unsigned int h; 2256 2257 rcu_read_lock(); 2258 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2259 head = &net->ipv6.fib_table_hash[h]; 2260 hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2261 spin_lock_bh(&table->tb6_lock); 2262 fib6_clean_tree(net, &table->tb6_root, 2263 func, sernum, arg, skip_notify); 2264 spin_unlock_bh(&table->tb6_lock); 2265 } 2266 } 2267 rcu_read_unlock(); 2268 } 2269 2270 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *), 2271 void *arg) 2272 { 2273 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false); 2274 } 2275 2276 void fib6_clean_all_skip_notify(struct net *net, 2277 int (*func)(struct fib6_info *, void *), 2278 void *arg) 2279 { 2280 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true); 2281 } 2282 2283 static void fib6_flush_trees(struct net *net) 2284 { 2285 int new_sernum = fib6_new_sernum(net); 2286 2287 __fib6_clean_all(net, NULL, new_sernum, NULL, false); 2288 } 2289 2290 /* 2291 * Garbage collection 2292 */ 2293 2294 static int fib6_age(struct fib6_info *rt, struct fib6_gc_args *gc_args) 2295 { 2296 unsigned long now = jiffies; 2297 2298 /* 2299 * check addrconf expiration here. 2300 * Routes are expired even if they are in use. 2301 */ 2302 2303 if (fib6_has_expires(rt) && rt->expires) { 2304 if (time_after(now, rt->expires)) { 2305 RT6_TRACE("expiring %p\n", rt); 2306 return -1; 2307 } 2308 gc_args->more++; 2309 } 2310 2311 /* Also age clones in the exception table. 2312 * Note, that clones are aged out 2313 * only if they are not in use now. 2314 */ 2315 rt6_age_exceptions(rt, gc_args, now); 2316 2317 return 0; 2318 } 2319 2320 static void fib6_gc_table(struct net *net, 2321 struct fib6_table *tb6, 2322 struct fib6_gc_args *gc_args) 2323 { 2324 struct fib6_info *rt; 2325 struct hlist_node *n; 2326 struct nl_info info = { 2327 .nl_net = net, 2328 .skip_notify = false, 2329 }; 2330 2331 hlist_for_each_entry_safe(rt, n, &tb6->tb6_gc_hlist, gc_link) 2332 if (fib6_age(rt, gc_args) == -1) 2333 fib6_del(rt, &info); 2334 } 2335 2336 static void fib6_gc_all(struct net *net, struct fib6_gc_args *gc_args) 2337 { 2338 struct fib6_table *table; 2339 struct hlist_head *head; 2340 unsigned int h; 2341 2342 rcu_read_lock(); 2343 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2344 head = &net->ipv6.fib_table_hash[h]; 2345 hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2346 spin_lock_bh(&table->tb6_lock); 2347 fib6_gc_table(net, table, gc_args); 2348 spin_unlock_bh(&table->tb6_lock); 2349 } 2350 } 2351 rcu_read_unlock(); 2352 } 2353 2354 void fib6_run_gc(unsigned long expires, struct net *net, bool force) 2355 { 2356 struct fib6_gc_args gc_args; 2357 unsigned long now; 2358 2359 if (force) { 2360 spin_lock_bh(&net->ipv6.fib6_gc_lock); 2361 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) { 2362 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ); 2363 return; 2364 } 2365 gc_args.timeout = expires ? (int)expires : 2366 net->ipv6.sysctl.ip6_rt_gc_interval; 2367 gc_args.more = 0; 2368 2369 fib6_gc_all(net, &gc_args); 2370 now = jiffies; 2371 net->ipv6.ip6_rt_last_gc = now; 2372 2373 if (gc_args.more) 2374 mod_timer(&net->ipv6.ip6_fib_timer, 2375 round_jiffies(now 2376 + net->ipv6.sysctl.ip6_rt_gc_interval)); 2377 else 2378 del_timer(&net->ipv6.ip6_fib_timer); 2379 spin_unlock_bh(&net->ipv6.fib6_gc_lock); 2380 } 2381 2382 static void fib6_gc_timer_cb(struct timer_list *t) 2383 { 2384 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer); 2385 2386 fib6_run_gc(0, arg, true); 2387 } 2388 2389 static int __net_init fib6_net_init(struct net *net) 2390 { 2391 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ; 2392 int err; 2393 2394 err = fib6_notifier_init(net); 2395 if (err) 2396 return err; 2397 2398 /* Default to 3-tuple */ 2399 net->ipv6.sysctl.multipath_hash_fields = 2400 FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK; 2401 2402 spin_lock_init(&net->ipv6.fib6_gc_lock); 2403 rwlock_init(&net->ipv6.fib6_walker_lock); 2404 INIT_LIST_HEAD(&net->ipv6.fib6_walkers); 2405 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0); 2406 2407 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL); 2408 if (!net->ipv6.rt6_stats) 2409 goto out_notifier; 2410 2411 /* Avoid false sharing : Use at least a full cache line */ 2412 size = max_t(size_t, size, L1_CACHE_BYTES); 2413 2414 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL); 2415 if (!net->ipv6.fib_table_hash) 2416 goto out_rt6_stats; 2417 2418 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl), 2419 GFP_KERNEL); 2420 if (!net->ipv6.fib6_main_tbl) 2421 goto out_fib_table_hash; 2422 2423 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; 2424 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf, 2425 net->ipv6.fib6_null_entry); 2426 net->ipv6.fib6_main_tbl->tb6_root.fn_flags = 2427 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; 2428 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers); 2429 2430 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2431 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl), 2432 GFP_KERNEL); 2433 if (!net->ipv6.fib6_local_tbl) 2434 goto out_fib6_main_tbl; 2435 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; 2436 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf, 2437 net->ipv6.fib6_null_entry); 2438 net->ipv6.fib6_local_tbl->tb6_root.fn_flags = 2439 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; 2440 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers); 2441 #endif 2442 fib6_tables_init(net); 2443 2444 return 0; 2445 2446 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2447 out_fib6_main_tbl: 2448 kfree(net->ipv6.fib6_main_tbl); 2449 #endif 2450 out_fib_table_hash: 2451 kfree(net->ipv6.fib_table_hash); 2452 out_rt6_stats: 2453 kfree(net->ipv6.rt6_stats); 2454 out_notifier: 2455 fib6_notifier_exit(net); 2456 return -ENOMEM; 2457 } 2458 2459 static void fib6_net_exit(struct net *net) 2460 { 2461 unsigned int i; 2462 2463 del_timer_sync(&net->ipv6.ip6_fib_timer); 2464 2465 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) { 2466 struct hlist_head *head = &net->ipv6.fib_table_hash[i]; 2467 struct hlist_node *tmp; 2468 struct fib6_table *tb; 2469 2470 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) { 2471 hlist_del(&tb->tb6_hlist); 2472 fib6_free_table(tb); 2473 } 2474 } 2475 2476 kfree(net->ipv6.fib_table_hash); 2477 kfree(net->ipv6.rt6_stats); 2478 fib6_notifier_exit(net); 2479 } 2480 2481 static struct pernet_operations fib6_net_ops = { 2482 .init = fib6_net_init, 2483 .exit = fib6_net_exit, 2484 }; 2485 2486 int __init fib6_init(void) 2487 { 2488 int ret = -ENOMEM; 2489 2490 fib6_node_kmem = kmem_cache_create("fib6_nodes", 2491 sizeof(struct fib6_node), 0, 2492 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 2493 NULL); 2494 if (!fib6_node_kmem) 2495 goto out; 2496 2497 ret = register_pernet_subsys(&fib6_net_ops); 2498 if (ret) 2499 goto out_kmem_cache_create; 2500 2501 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL, 2502 inet6_dump_fib, 0); 2503 if (ret) 2504 goto out_unregister_subsys; 2505 2506 __fib6_flush_trees = fib6_flush_trees; 2507 out: 2508 return ret; 2509 2510 out_unregister_subsys: 2511 unregister_pernet_subsys(&fib6_net_ops); 2512 out_kmem_cache_create: 2513 kmem_cache_destroy(fib6_node_kmem); 2514 goto out; 2515 } 2516 2517 void fib6_gc_cleanup(void) 2518 { 2519 unregister_pernet_subsys(&fib6_net_ops); 2520 kmem_cache_destroy(fib6_node_kmem); 2521 } 2522 2523 #ifdef CONFIG_PROC_FS 2524 static int ipv6_route_native_seq_show(struct seq_file *seq, void *v) 2525 { 2526 struct fib6_info *rt = v; 2527 struct ipv6_route_iter *iter = seq->private; 2528 struct fib6_nh *fib6_nh = rt->fib6_nh; 2529 unsigned int flags = rt->fib6_flags; 2530 const struct net_device *dev; 2531 2532 if (rt->nh) 2533 fib6_nh = nexthop_fib6_nh(rt->nh); 2534 2535 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen); 2536 2537 #ifdef CONFIG_IPV6_SUBTREES 2538 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen); 2539 #else 2540 seq_puts(seq, "00000000000000000000000000000000 00 "); 2541 #endif 2542 if (fib6_nh->fib_nh_gw_family) { 2543 flags |= RTF_GATEWAY; 2544 seq_printf(seq, "%pi6", &fib6_nh->fib_nh_gw6); 2545 } else { 2546 seq_puts(seq, "00000000000000000000000000000000"); 2547 } 2548 2549 dev = fib6_nh->fib_nh_dev; 2550 seq_printf(seq, " %08x %08x %08x %08x %8s\n", 2551 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0, 2552 flags, dev ? dev->name : ""); 2553 iter->w.leaf = NULL; 2554 return 0; 2555 } 2556 2557 static int ipv6_route_yield(struct fib6_walker *w) 2558 { 2559 struct ipv6_route_iter *iter = w->args; 2560 2561 if (!iter->skip) 2562 return 1; 2563 2564 do { 2565 iter->w.leaf = rcu_dereference_protected( 2566 iter->w.leaf->fib6_next, 2567 lockdep_is_held(&iter->tbl->tb6_lock)); 2568 iter->skip--; 2569 if (!iter->skip && iter->w.leaf) 2570 return 1; 2571 } while (iter->w.leaf); 2572 2573 return 0; 2574 } 2575 2576 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter, 2577 struct net *net) 2578 { 2579 memset(&iter->w, 0, sizeof(iter->w)); 2580 iter->w.func = ipv6_route_yield; 2581 iter->w.root = &iter->tbl->tb6_root; 2582 iter->w.state = FWS_INIT; 2583 iter->w.node = iter->w.root; 2584 iter->w.args = iter; 2585 iter->sernum = READ_ONCE(iter->w.root->fn_sernum); 2586 INIT_LIST_HEAD(&iter->w.lh); 2587 fib6_walker_link(net, &iter->w); 2588 } 2589 2590 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl, 2591 struct net *net) 2592 { 2593 unsigned int h; 2594 struct hlist_node *node; 2595 2596 if (tbl) { 2597 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1; 2598 node = rcu_dereference(hlist_next_rcu(&tbl->tb6_hlist)); 2599 } else { 2600 h = 0; 2601 node = NULL; 2602 } 2603 2604 while (!node && h < FIB6_TABLE_HASHSZ) { 2605 node = rcu_dereference( 2606 hlist_first_rcu(&net->ipv6.fib_table_hash[h++])); 2607 } 2608 return hlist_entry_safe(node, struct fib6_table, tb6_hlist); 2609 } 2610 2611 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter) 2612 { 2613 int sernum = READ_ONCE(iter->w.root->fn_sernum); 2614 2615 if (iter->sernum != sernum) { 2616 iter->sernum = sernum; 2617 iter->w.state = FWS_INIT; 2618 iter->w.node = iter->w.root; 2619 WARN_ON(iter->w.skip); 2620 iter->w.skip = iter->w.count; 2621 } 2622 } 2623 2624 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2625 { 2626 int r; 2627 struct fib6_info *n; 2628 struct net *net = seq_file_net(seq); 2629 struct ipv6_route_iter *iter = seq->private; 2630 2631 ++(*pos); 2632 if (!v) 2633 goto iter_table; 2634 2635 n = rcu_dereference(((struct fib6_info *)v)->fib6_next); 2636 if (n) 2637 return n; 2638 2639 iter_table: 2640 ipv6_route_check_sernum(iter); 2641 spin_lock_bh(&iter->tbl->tb6_lock); 2642 r = fib6_walk_continue(&iter->w); 2643 spin_unlock_bh(&iter->tbl->tb6_lock); 2644 if (r > 0) { 2645 return iter->w.leaf; 2646 } else if (r < 0) { 2647 fib6_walker_unlink(net, &iter->w); 2648 return NULL; 2649 } 2650 fib6_walker_unlink(net, &iter->w); 2651 2652 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net); 2653 if (!iter->tbl) 2654 return NULL; 2655 2656 ipv6_route_seq_setup_walk(iter, net); 2657 goto iter_table; 2658 } 2659 2660 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos) 2661 __acquires(RCU) 2662 { 2663 struct net *net = seq_file_net(seq); 2664 struct ipv6_route_iter *iter = seq->private; 2665 2666 rcu_read_lock(); 2667 iter->tbl = ipv6_route_seq_next_table(NULL, net); 2668 iter->skip = *pos; 2669 2670 if (iter->tbl) { 2671 loff_t p = 0; 2672 2673 ipv6_route_seq_setup_walk(iter, net); 2674 return ipv6_route_seq_next(seq, NULL, &p); 2675 } else { 2676 return NULL; 2677 } 2678 } 2679 2680 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter) 2681 { 2682 struct fib6_walker *w = &iter->w; 2683 return w->node && !(w->state == FWS_U && w->node == w->root); 2684 } 2685 2686 static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v) 2687 __releases(RCU) 2688 { 2689 struct net *net = seq_file_net(seq); 2690 struct ipv6_route_iter *iter = seq->private; 2691 2692 if (ipv6_route_iter_active(iter)) 2693 fib6_walker_unlink(net, &iter->w); 2694 2695 rcu_read_unlock(); 2696 } 2697 2698 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL) 2699 static int ipv6_route_prog_seq_show(struct bpf_prog *prog, 2700 struct bpf_iter_meta *meta, 2701 void *v) 2702 { 2703 struct bpf_iter__ipv6_route ctx; 2704 2705 ctx.meta = meta; 2706 ctx.rt = v; 2707 return bpf_iter_run_prog(prog, &ctx); 2708 } 2709 2710 static int ipv6_route_seq_show(struct seq_file *seq, void *v) 2711 { 2712 struct ipv6_route_iter *iter = seq->private; 2713 struct bpf_iter_meta meta; 2714 struct bpf_prog *prog; 2715 int ret; 2716 2717 meta.seq = seq; 2718 prog = bpf_iter_get_info(&meta, false); 2719 if (!prog) 2720 return ipv6_route_native_seq_show(seq, v); 2721 2722 ret = ipv6_route_prog_seq_show(prog, &meta, v); 2723 iter->w.leaf = NULL; 2724 2725 return ret; 2726 } 2727 2728 static void ipv6_route_seq_stop(struct seq_file *seq, void *v) 2729 { 2730 struct bpf_iter_meta meta; 2731 struct bpf_prog *prog; 2732 2733 if (!v) { 2734 meta.seq = seq; 2735 prog = bpf_iter_get_info(&meta, true); 2736 if (prog) 2737 (void)ipv6_route_prog_seq_show(prog, &meta, v); 2738 } 2739 2740 ipv6_route_native_seq_stop(seq, v); 2741 } 2742 #else 2743 static int ipv6_route_seq_show(struct seq_file *seq, void *v) 2744 { 2745 return ipv6_route_native_seq_show(seq, v); 2746 } 2747 2748 static void ipv6_route_seq_stop(struct seq_file *seq, void *v) 2749 { 2750 ipv6_route_native_seq_stop(seq, v); 2751 } 2752 #endif 2753 2754 const struct seq_operations ipv6_route_seq_ops = { 2755 .start = ipv6_route_seq_start, 2756 .next = ipv6_route_seq_next, 2757 .stop = ipv6_route_seq_stop, 2758 .show = ipv6_route_seq_show 2759 }; 2760 #endif /* CONFIG_PROC_FS */ 2761