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