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