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