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