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