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