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