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/ipv6.h> 33 #include <net/ndisc.h> 34 #include <net/addrconf.h> 35 #include <net/lwtunnel.h> 36 37 #include <net/ip6_fib.h> 38 #include <net/ip6_route.h> 39 40 #define RT6_DEBUG 2 41 42 #if RT6_DEBUG >= 3 43 #define RT6_TRACE(x...) pr_debug(x) 44 #else 45 #define RT6_TRACE(x...) do { ; } while (0) 46 #endif 47 48 static struct kmem_cache *fib6_node_kmem __read_mostly; 49 50 struct fib6_cleaner { 51 struct fib6_walker w; 52 struct net *net; 53 int (*func)(struct rt6_info *, void *arg); 54 int sernum; 55 void *arg; 56 }; 57 58 static DEFINE_RWLOCK(fib6_walker_lock); 59 60 #ifdef CONFIG_IPV6_SUBTREES 61 #define FWS_INIT FWS_S 62 #else 63 #define FWS_INIT FWS_L 64 #endif 65 66 static void fib6_prune_clones(struct net *net, struct fib6_node *fn); 67 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn); 68 static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn); 69 static int fib6_walk(struct fib6_walker *w); 70 static int fib6_walk_continue(struct fib6_walker *w); 71 72 /* 73 * A routing update causes an increase of the serial number on the 74 * affected subtree. This allows for cached routes to be asynchronously 75 * tested when modifications are made to the destination cache as a 76 * result of redirects, path MTU changes, etc. 77 */ 78 79 static void fib6_gc_timer_cb(unsigned long arg); 80 81 static LIST_HEAD(fib6_walkers); 82 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh) 83 84 static void fib6_walker_link(struct fib6_walker *w) 85 { 86 write_lock_bh(&fib6_walker_lock); 87 list_add(&w->lh, &fib6_walkers); 88 write_unlock_bh(&fib6_walker_lock); 89 } 90 91 static void fib6_walker_unlink(struct fib6_walker *w) 92 { 93 write_lock_bh(&fib6_walker_lock); 94 list_del(&w->lh); 95 write_unlock_bh(&fib6_walker_lock); 96 } 97 98 static int fib6_new_sernum(struct net *net) 99 { 100 int new, old; 101 102 do { 103 old = atomic_read(&net->ipv6.fib6_sernum); 104 new = old < INT_MAX ? old + 1 : 1; 105 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum, 106 old, new) != old); 107 return new; 108 } 109 110 enum { 111 FIB6_NO_SERNUM_CHANGE = 0, 112 }; 113 114 /* 115 * Auxiliary address test functions for the radix tree. 116 * 117 * These assume a 32bit processor (although it will work on 118 * 64bit processors) 119 */ 120 121 /* 122 * test bit 123 */ 124 #if defined(__LITTLE_ENDIAN) 125 # define BITOP_BE32_SWIZZLE (0x1F & ~7) 126 #else 127 # define BITOP_BE32_SWIZZLE 0 128 #endif 129 130 static __be32 addr_bit_set(const void *token, int fn_bit) 131 { 132 const __be32 *addr = token; 133 /* 134 * Here, 135 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f) 136 * is optimized version of 137 * htonl(1 << ((~fn_bit)&0x1F)) 138 * See include/asm-generic/bitops/le.h. 139 */ 140 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) & 141 addr[fn_bit >> 5]; 142 } 143 144 static struct fib6_node *node_alloc(void) 145 { 146 struct fib6_node *fn; 147 148 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); 149 150 return fn; 151 } 152 153 static void node_free(struct fib6_node *fn) 154 { 155 kmem_cache_free(fib6_node_kmem, fn); 156 } 157 158 static void rt6_rcu_free(struct rt6_info *rt) 159 { 160 call_rcu(&rt->dst.rcu_head, dst_rcu_free); 161 } 162 163 static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt) 164 { 165 int cpu; 166 167 if (!non_pcpu_rt->rt6i_pcpu) 168 return; 169 170 for_each_possible_cpu(cpu) { 171 struct rt6_info **ppcpu_rt; 172 struct rt6_info *pcpu_rt; 173 174 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu); 175 pcpu_rt = *ppcpu_rt; 176 if (pcpu_rt) { 177 rt6_rcu_free(pcpu_rt); 178 *ppcpu_rt = NULL; 179 } 180 } 181 182 non_pcpu_rt->rt6i_pcpu = NULL; 183 } 184 185 static void rt6_release(struct rt6_info *rt) 186 { 187 if (atomic_dec_and_test(&rt->rt6i_ref)) { 188 rt6_free_pcpu(rt); 189 rt6_rcu_free(rt); 190 } 191 } 192 193 static void fib6_link_table(struct net *net, struct fib6_table *tb) 194 { 195 unsigned int h; 196 197 /* 198 * Initialize table lock at a single place to give lockdep a key, 199 * tables aren't visible prior to being linked to the list. 200 */ 201 rwlock_init(&tb->tb6_lock); 202 203 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); 204 205 /* 206 * No protection necessary, this is the only list mutatation 207 * operation, tables never disappear once they exist. 208 */ 209 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]); 210 } 211 212 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 213 214 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id) 215 { 216 struct fib6_table *table; 217 218 table = kzalloc(sizeof(*table), GFP_ATOMIC); 219 if (table) { 220 table->tb6_id = id; 221 table->tb6_root.leaf = net->ipv6.ip6_null_entry; 222 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; 223 inet_peer_base_init(&table->tb6_peers); 224 } 225 226 return table; 227 } 228 229 struct fib6_table *fib6_new_table(struct net *net, u32 id) 230 { 231 struct fib6_table *tb; 232 233 if (id == 0) 234 id = RT6_TABLE_MAIN; 235 tb = fib6_get_table(net, id); 236 if (tb) 237 return tb; 238 239 tb = fib6_alloc_table(net, id); 240 if (tb) 241 fib6_link_table(net, tb); 242 243 return tb; 244 } 245 246 struct fib6_table *fib6_get_table(struct net *net, u32 id) 247 { 248 struct fib6_table *tb; 249 struct hlist_head *head; 250 unsigned int h; 251 252 if (id == 0) 253 id = RT6_TABLE_MAIN; 254 h = id & (FIB6_TABLE_HASHSZ - 1); 255 rcu_read_lock(); 256 head = &net->ipv6.fib_table_hash[h]; 257 hlist_for_each_entry_rcu(tb, head, tb6_hlist) { 258 if (tb->tb6_id == id) { 259 rcu_read_unlock(); 260 return tb; 261 } 262 } 263 rcu_read_unlock(); 264 265 return NULL; 266 } 267 268 static void __net_init fib6_tables_init(struct net *net) 269 { 270 fib6_link_table(net, net->ipv6.fib6_main_tbl); 271 fib6_link_table(net, net->ipv6.fib6_local_tbl); 272 } 273 #else 274 275 struct fib6_table *fib6_new_table(struct net *net, u32 id) 276 { 277 return fib6_get_table(net, id); 278 } 279 280 struct fib6_table *fib6_get_table(struct net *net, u32 id) 281 { 282 return net->ipv6.fib6_main_tbl; 283 } 284 285 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, 286 int flags, pol_lookup_t lookup) 287 { 288 struct rt6_info *rt; 289 290 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags); 291 if (rt->rt6i_flags & RTF_REJECT && 292 rt->dst.error == -EAGAIN) { 293 ip6_rt_put(rt); 294 rt = net->ipv6.ip6_null_entry; 295 dst_hold(&rt->dst); 296 } 297 298 return &rt->dst; 299 } 300 301 static void __net_init fib6_tables_init(struct net *net) 302 { 303 fib6_link_table(net, net->ipv6.fib6_main_tbl); 304 } 305 306 #endif 307 308 static int fib6_dump_node(struct fib6_walker *w) 309 { 310 int res; 311 struct rt6_info *rt; 312 313 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { 314 res = rt6_dump_route(rt, w->args); 315 if (res < 0) { 316 /* Frame is full, suspend walking */ 317 w->leaf = rt; 318 return 1; 319 } 320 } 321 w->leaf = NULL; 322 return 0; 323 } 324 325 static void fib6_dump_end(struct netlink_callback *cb) 326 { 327 struct fib6_walker *w = (void *)cb->args[2]; 328 329 if (w) { 330 if (cb->args[4]) { 331 cb->args[4] = 0; 332 fib6_walker_unlink(w); 333 } 334 cb->args[2] = 0; 335 kfree(w); 336 } 337 cb->done = (void *)cb->args[3]; 338 cb->args[1] = 3; 339 } 340 341 static int fib6_dump_done(struct netlink_callback *cb) 342 { 343 fib6_dump_end(cb); 344 return cb->done ? cb->done(cb) : 0; 345 } 346 347 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, 348 struct netlink_callback *cb) 349 { 350 struct fib6_walker *w; 351 int res; 352 353 w = (void *)cb->args[2]; 354 w->root = &table->tb6_root; 355 356 if (cb->args[4] == 0) { 357 w->count = 0; 358 w->skip = 0; 359 360 read_lock_bh(&table->tb6_lock); 361 res = fib6_walk(w); 362 read_unlock_bh(&table->tb6_lock); 363 if (res > 0) { 364 cb->args[4] = 1; 365 cb->args[5] = w->root->fn_sernum; 366 } 367 } else { 368 if (cb->args[5] != w->root->fn_sernum) { 369 /* Begin at the root if the tree changed */ 370 cb->args[5] = w->root->fn_sernum; 371 w->state = FWS_INIT; 372 w->node = w->root; 373 w->skip = w->count; 374 } else 375 w->skip = 0; 376 377 read_lock_bh(&table->tb6_lock); 378 res = fib6_walk_continue(w); 379 read_unlock_bh(&table->tb6_lock); 380 if (res <= 0) { 381 fib6_walker_unlink(w); 382 cb->args[4] = 0; 383 } 384 } 385 386 return res; 387 } 388 389 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) 390 { 391 struct net *net = sock_net(skb->sk); 392 unsigned int h, s_h; 393 unsigned int e = 0, s_e; 394 struct rt6_rtnl_dump_arg arg; 395 struct fib6_walker *w; 396 struct fib6_table *tb; 397 struct hlist_head *head; 398 int res = 0; 399 400 s_h = cb->args[0]; 401 s_e = cb->args[1]; 402 403 w = (void *)cb->args[2]; 404 if (!w) { 405 /* New dump: 406 * 407 * 1. hook callback destructor. 408 */ 409 cb->args[3] = (long)cb->done; 410 cb->done = fib6_dump_done; 411 412 /* 413 * 2. allocate and initialize walker. 414 */ 415 w = kzalloc(sizeof(*w), GFP_ATOMIC); 416 if (!w) 417 return -ENOMEM; 418 w->func = fib6_dump_node; 419 cb->args[2] = (long)w; 420 } 421 422 arg.skb = skb; 423 arg.cb = cb; 424 arg.net = net; 425 w->args = &arg; 426 427 rcu_read_lock(); 428 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) { 429 e = 0; 430 head = &net->ipv6.fib_table_hash[h]; 431 hlist_for_each_entry_rcu(tb, head, tb6_hlist) { 432 if (e < s_e) 433 goto next; 434 res = fib6_dump_table(tb, skb, cb); 435 if (res != 0) 436 goto out; 437 next: 438 e++; 439 } 440 } 441 out: 442 rcu_read_unlock(); 443 cb->args[1] = e; 444 cb->args[0] = h; 445 446 res = res < 0 ? res : skb->len; 447 if (res <= 0) 448 fib6_dump_end(cb); 449 return res; 450 } 451 452 /* 453 * Routing Table 454 * 455 * return the appropriate node for a routing tree "add" operation 456 * by either creating and inserting or by returning an existing 457 * node. 458 */ 459 460 static struct fib6_node *fib6_add_1(struct fib6_node *root, 461 struct in6_addr *addr, int plen, 462 int offset, int allow_create, 463 int replace_required, int sernum) 464 { 465 struct fib6_node *fn, *in, *ln; 466 struct fib6_node *pn = NULL; 467 struct rt6key *key; 468 int bit; 469 __be32 dir = 0; 470 471 RT6_TRACE("fib6_add_1\n"); 472 473 /* insert node in tree */ 474 475 fn = root; 476 477 do { 478 key = (struct rt6key *)((u8 *)fn->leaf + offset); 479 480 /* 481 * Prefix match 482 */ 483 if (plen < fn->fn_bit || 484 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) { 485 if (!allow_create) { 486 if (replace_required) { 487 pr_warn("Can't replace route, no match found\n"); 488 return ERR_PTR(-ENOENT); 489 } 490 pr_warn("NLM_F_CREATE should be set when creating new route\n"); 491 } 492 goto insert_above; 493 } 494 495 /* 496 * Exact match ? 497 */ 498 499 if (plen == fn->fn_bit) { 500 /* clean up an intermediate node */ 501 if (!(fn->fn_flags & RTN_RTINFO)) { 502 rt6_release(fn->leaf); 503 fn->leaf = NULL; 504 } 505 506 fn->fn_sernum = sernum; 507 508 return fn; 509 } 510 511 /* 512 * We have more bits to go 513 */ 514 515 /* Try to walk down on tree. */ 516 fn->fn_sernum = sernum; 517 dir = addr_bit_set(addr, fn->fn_bit); 518 pn = fn; 519 fn = dir ? fn->right : fn->left; 520 } while (fn); 521 522 if (!allow_create) { 523 /* We should not create new node because 524 * NLM_F_REPLACE was specified without NLM_F_CREATE 525 * I assume it is safe to require NLM_F_CREATE when 526 * REPLACE flag is used! Later we may want to remove the 527 * check for replace_required, because according 528 * to netlink specification, NLM_F_CREATE 529 * MUST be specified if new route is created. 530 * That would keep IPv6 consistent with IPv4 531 */ 532 if (replace_required) { 533 pr_warn("Can't replace route, no match found\n"); 534 return ERR_PTR(-ENOENT); 535 } 536 pr_warn("NLM_F_CREATE should be set when creating new route\n"); 537 } 538 /* 539 * We walked to the bottom of tree. 540 * Create new leaf node without children. 541 */ 542 543 ln = node_alloc(); 544 545 if (!ln) 546 return ERR_PTR(-ENOMEM); 547 ln->fn_bit = plen; 548 549 ln->parent = pn; 550 ln->fn_sernum = sernum; 551 552 if (dir) 553 pn->right = ln; 554 else 555 pn->left = ln; 556 557 return ln; 558 559 560 insert_above: 561 /* 562 * split since we don't have a common prefix anymore or 563 * we have a less significant route. 564 * we've to insert an intermediate node on the list 565 * this new node will point to the one we need to create 566 * and the current 567 */ 568 569 pn = fn->parent; 570 571 /* find 1st bit in difference between the 2 addrs. 572 573 See comment in __ipv6_addr_diff: bit may be an invalid value, 574 but if it is >= plen, the value is ignored in any case. 575 */ 576 577 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr)); 578 579 /* 580 * (intermediate)[in] 581 * / \ 582 * (new leaf node)[ln] (old node)[fn] 583 */ 584 if (plen > bit) { 585 in = node_alloc(); 586 ln = node_alloc(); 587 588 if (!in || !ln) { 589 if (in) 590 node_free(in); 591 if (ln) 592 node_free(ln); 593 return ERR_PTR(-ENOMEM); 594 } 595 596 /* 597 * new intermediate node. 598 * RTN_RTINFO will 599 * be off since that an address that chooses one of 600 * the branches would not match less specific routes 601 * in the other branch 602 */ 603 604 in->fn_bit = bit; 605 606 in->parent = pn; 607 in->leaf = fn->leaf; 608 atomic_inc(&in->leaf->rt6i_ref); 609 610 in->fn_sernum = sernum; 611 612 /* update parent pointer */ 613 if (dir) 614 pn->right = in; 615 else 616 pn->left = in; 617 618 ln->fn_bit = plen; 619 620 ln->parent = in; 621 fn->parent = in; 622 623 ln->fn_sernum = sernum; 624 625 if (addr_bit_set(addr, bit)) { 626 in->right = ln; 627 in->left = fn; 628 } else { 629 in->left = ln; 630 in->right = fn; 631 } 632 } else { /* plen <= bit */ 633 634 /* 635 * (new leaf node)[ln] 636 * / \ 637 * (old node)[fn] NULL 638 */ 639 640 ln = node_alloc(); 641 642 if (!ln) 643 return ERR_PTR(-ENOMEM); 644 645 ln->fn_bit = plen; 646 647 ln->parent = pn; 648 649 ln->fn_sernum = sernum; 650 651 if (dir) 652 pn->right = ln; 653 else 654 pn->left = ln; 655 656 if (addr_bit_set(&key->addr, plen)) 657 ln->right = fn; 658 else 659 ln->left = fn; 660 661 fn->parent = ln; 662 } 663 return ln; 664 } 665 666 static bool rt6_qualify_for_ecmp(struct rt6_info *rt) 667 { 668 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) == 669 RTF_GATEWAY; 670 } 671 672 static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc) 673 { 674 int i; 675 676 for (i = 0; i < RTAX_MAX; i++) { 677 if (test_bit(i, mxc->mx_valid)) 678 mp[i] = mxc->mx[i]; 679 } 680 } 681 682 static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc) 683 { 684 if (!mxc->mx) 685 return 0; 686 687 if (dst->flags & DST_HOST) { 688 u32 *mp = dst_metrics_write_ptr(dst); 689 690 if (unlikely(!mp)) 691 return -ENOMEM; 692 693 fib6_copy_metrics(mp, mxc); 694 } else { 695 dst_init_metrics(dst, mxc->mx, false); 696 697 /* We've stolen mx now. */ 698 mxc->mx = NULL; 699 } 700 701 return 0; 702 } 703 704 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn, 705 struct net *net) 706 { 707 if (atomic_read(&rt->rt6i_ref) != 1) { 708 /* This route is used as dummy address holder in some split 709 * nodes. It is not leaked, but it still holds other resources, 710 * which must be released in time. So, scan ascendant nodes 711 * and replace dummy references to this route with references 712 * to still alive ones. 713 */ 714 while (fn) { 715 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) { 716 fn->leaf = fib6_find_prefix(net, fn); 717 atomic_inc(&fn->leaf->rt6i_ref); 718 rt6_release(rt); 719 } 720 fn = fn->parent; 721 } 722 /* No more references are possible at this point. */ 723 BUG_ON(atomic_read(&rt->rt6i_ref) != 1); 724 } 725 } 726 727 /* 728 * Insert routing information in a node. 729 */ 730 731 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt, 732 struct nl_info *info, struct mx6_config *mxc) 733 { 734 struct rt6_info *iter = NULL; 735 struct rt6_info **ins; 736 struct rt6_info **fallback_ins = NULL; 737 int replace = (info->nlh && 738 (info->nlh->nlmsg_flags & NLM_F_REPLACE)); 739 int add = (!info->nlh || 740 (info->nlh->nlmsg_flags & NLM_F_CREATE)); 741 int found = 0; 742 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); 743 int err; 744 745 ins = &fn->leaf; 746 747 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) { 748 /* 749 * Search for duplicates 750 */ 751 752 if (iter->rt6i_metric == rt->rt6i_metric) { 753 /* 754 * Same priority level 755 */ 756 if (info->nlh && 757 (info->nlh->nlmsg_flags & NLM_F_EXCL)) 758 return -EEXIST; 759 if (replace) { 760 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) { 761 found++; 762 break; 763 } 764 if (rt_can_ecmp) 765 fallback_ins = fallback_ins ?: ins; 766 goto next_iter; 767 } 768 769 if (iter->dst.dev == rt->dst.dev && 770 iter->rt6i_idev == rt->rt6i_idev && 771 ipv6_addr_equal(&iter->rt6i_gateway, 772 &rt->rt6i_gateway)) { 773 if (rt->rt6i_nsiblings) 774 rt->rt6i_nsiblings = 0; 775 if (!(iter->rt6i_flags & RTF_EXPIRES)) 776 return -EEXIST; 777 if (!(rt->rt6i_flags & RTF_EXPIRES)) 778 rt6_clean_expires(iter); 779 else 780 rt6_set_expires(iter, rt->dst.expires); 781 iter->rt6i_pmtu = rt->rt6i_pmtu; 782 return -EEXIST; 783 } 784 /* If we have the same destination and the same metric, 785 * but not the same gateway, then the route we try to 786 * add is sibling to this route, increment our counter 787 * of siblings, and later we will add our route to the 788 * list. 789 * Only static routes (which don't have flag 790 * RTF_EXPIRES) are used for ECMPv6. 791 * 792 * To avoid long list, we only had siblings if the 793 * route have a gateway. 794 */ 795 if (rt_can_ecmp && 796 rt6_qualify_for_ecmp(iter)) 797 rt->rt6i_nsiblings++; 798 } 799 800 if (iter->rt6i_metric > rt->rt6i_metric) 801 break; 802 803 next_iter: 804 ins = &iter->dst.rt6_next; 805 } 806 807 if (fallback_ins && !found) { 808 /* No ECMP-able route found, replace first non-ECMP one */ 809 ins = fallback_ins; 810 iter = *ins; 811 found++; 812 } 813 814 /* Reset round-robin state, if necessary */ 815 if (ins == &fn->leaf) 816 fn->rr_ptr = NULL; 817 818 /* Link this route to others same route. */ 819 if (rt->rt6i_nsiblings) { 820 unsigned int rt6i_nsiblings; 821 struct rt6_info *sibling, *temp_sibling; 822 823 /* Find the first route that have the same metric */ 824 sibling = fn->leaf; 825 while (sibling) { 826 if (sibling->rt6i_metric == rt->rt6i_metric && 827 rt6_qualify_for_ecmp(sibling)) { 828 list_add_tail(&rt->rt6i_siblings, 829 &sibling->rt6i_siblings); 830 break; 831 } 832 sibling = sibling->dst.rt6_next; 833 } 834 /* For each sibling in the list, increment the counter of 835 * siblings. BUG() if counters does not match, list of siblings 836 * is broken! 837 */ 838 rt6i_nsiblings = 0; 839 list_for_each_entry_safe(sibling, temp_sibling, 840 &rt->rt6i_siblings, rt6i_siblings) { 841 sibling->rt6i_nsiblings++; 842 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings); 843 rt6i_nsiblings++; 844 } 845 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings); 846 } 847 848 /* 849 * insert node 850 */ 851 if (!replace) { 852 if (!add) 853 pr_warn("NLM_F_CREATE should be set when creating new route\n"); 854 855 add: 856 err = fib6_commit_metrics(&rt->dst, mxc); 857 if (err) 858 return err; 859 860 rt->dst.rt6_next = iter; 861 *ins = rt; 862 rt->rt6i_node = fn; 863 atomic_inc(&rt->rt6i_ref); 864 inet6_rt_notify(RTM_NEWROUTE, rt, info, 0); 865 info->nl_net->ipv6.rt6_stats->fib_rt_entries++; 866 867 if (!(fn->fn_flags & RTN_RTINFO)) { 868 info->nl_net->ipv6.rt6_stats->fib_route_nodes++; 869 fn->fn_flags |= RTN_RTINFO; 870 } 871 872 } else { 873 int nsiblings; 874 875 if (!found) { 876 if (add) 877 goto add; 878 pr_warn("NLM_F_REPLACE set, but no existing node found!\n"); 879 return -ENOENT; 880 } 881 882 err = fib6_commit_metrics(&rt->dst, mxc); 883 if (err) 884 return err; 885 886 *ins = rt; 887 rt->rt6i_node = fn; 888 rt->dst.rt6_next = iter->dst.rt6_next; 889 atomic_inc(&rt->rt6i_ref); 890 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE); 891 if (!(fn->fn_flags & RTN_RTINFO)) { 892 info->nl_net->ipv6.rt6_stats->fib_route_nodes++; 893 fn->fn_flags |= RTN_RTINFO; 894 } 895 nsiblings = iter->rt6i_nsiblings; 896 fib6_purge_rt(iter, fn, info->nl_net); 897 rt6_release(iter); 898 899 if (nsiblings) { 900 /* Replacing an ECMP route, remove all siblings */ 901 ins = &rt->dst.rt6_next; 902 iter = *ins; 903 while (iter) { 904 if (rt6_qualify_for_ecmp(iter)) { 905 *ins = iter->dst.rt6_next; 906 fib6_purge_rt(iter, fn, info->nl_net); 907 rt6_release(iter); 908 nsiblings--; 909 } else { 910 ins = &iter->dst.rt6_next; 911 } 912 iter = *ins; 913 } 914 WARN_ON(nsiblings != 0); 915 } 916 } 917 918 return 0; 919 } 920 921 static void fib6_start_gc(struct net *net, struct rt6_info *rt) 922 { 923 if (!timer_pending(&net->ipv6.ip6_fib_timer) && 924 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE))) 925 mod_timer(&net->ipv6.ip6_fib_timer, 926 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); 927 } 928 929 void fib6_force_start_gc(struct net *net) 930 { 931 if (!timer_pending(&net->ipv6.ip6_fib_timer)) 932 mod_timer(&net->ipv6.ip6_fib_timer, 933 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); 934 } 935 936 /* 937 * Add routing information to the routing tree. 938 * <destination addr>/<source addr> 939 * with source addr info in sub-trees 940 */ 941 942 int fib6_add(struct fib6_node *root, struct rt6_info *rt, 943 struct nl_info *info, struct mx6_config *mxc) 944 { 945 struct fib6_node *fn, *pn = NULL; 946 int err = -ENOMEM; 947 int allow_create = 1; 948 int replace_required = 0; 949 int sernum = fib6_new_sernum(info->nl_net); 950 951 if (WARN_ON_ONCE((rt->dst.flags & DST_NOCACHE) && 952 !atomic_read(&rt->dst.__refcnt))) 953 return -EINVAL; 954 955 if (info->nlh) { 956 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) 957 allow_create = 0; 958 if (info->nlh->nlmsg_flags & NLM_F_REPLACE) 959 replace_required = 1; 960 } 961 if (!allow_create && !replace_required) 962 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n"); 963 964 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen, 965 offsetof(struct rt6_info, rt6i_dst), allow_create, 966 replace_required, sernum); 967 if (IS_ERR(fn)) { 968 err = PTR_ERR(fn); 969 fn = NULL; 970 goto out; 971 } 972 973 pn = fn; 974 975 #ifdef CONFIG_IPV6_SUBTREES 976 if (rt->rt6i_src.plen) { 977 struct fib6_node *sn; 978 979 if (!fn->subtree) { 980 struct fib6_node *sfn; 981 982 /* 983 * Create subtree. 984 * 985 * fn[main tree] 986 * | 987 * sfn[subtree root] 988 * \ 989 * sn[new leaf node] 990 */ 991 992 /* Create subtree root node */ 993 sfn = node_alloc(); 994 if (!sfn) 995 goto st_failure; 996 997 sfn->leaf = info->nl_net->ipv6.ip6_null_entry; 998 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref); 999 sfn->fn_flags = RTN_ROOT; 1000 sfn->fn_sernum = sernum; 1001 1002 /* Now add the first leaf node to new subtree */ 1003 1004 sn = fib6_add_1(sfn, &rt->rt6i_src.addr, 1005 rt->rt6i_src.plen, 1006 offsetof(struct rt6_info, rt6i_src), 1007 allow_create, replace_required, sernum); 1008 1009 if (IS_ERR(sn)) { 1010 /* If it is failed, discard just allocated 1011 root, and then (in st_failure) stale node 1012 in main tree. 1013 */ 1014 node_free(sfn); 1015 err = PTR_ERR(sn); 1016 goto st_failure; 1017 } 1018 1019 /* Now link new subtree to main tree */ 1020 sfn->parent = fn; 1021 fn->subtree = sfn; 1022 } else { 1023 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr, 1024 rt->rt6i_src.plen, 1025 offsetof(struct rt6_info, rt6i_src), 1026 allow_create, replace_required, sernum); 1027 1028 if (IS_ERR(sn)) { 1029 err = PTR_ERR(sn); 1030 goto st_failure; 1031 } 1032 } 1033 1034 if (!fn->leaf) { 1035 fn->leaf = rt; 1036 atomic_inc(&rt->rt6i_ref); 1037 } 1038 fn = sn; 1039 } 1040 #endif 1041 1042 err = fib6_add_rt2node(fn, rt, info, mxc); 1043 if (!err) { 1044 fib6_start_gc(info->nl_net, rt); 1045 if (!(rt->rt6i_flags & RTF_CACHE)) 1046 fib6_prune_clones(info->nl_net, pn); 1047 rt->dst.flags &= ~DST_NOCACHE; 1048 } 1049 1050 out: 1051 if (err) { 1052 #ifdef CONFIG_IPV6_SUBTREES 1053 /* 1054 * If fib6_add_1 has cleared the old leaf pointer in the 1055 * super-tree leaf node we have to find a new one for it. 1056 */ 1057 if (pn != fn && pn->leaf == rt) { 1058 pn->leaf = NULL; 1059 atomic_dec(&rt->rt6i_ref); 1060 } 1061 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) { 1062 pn->leaf = fib6_find_prefix(info->nl_net, pn); 1063 #if RT6_DEBUG >= 2 1064 if (!pn->leaf) { 1065 WARN_ON(pn->leaf == NULL); 1066 pn->leaf = info->nl_net->ipv6.ip6_null_entry; 1067 } 1068 #endif 1069 atomic_inc(&pn->leaf->rt6i_ref); 1070 } 1071 #endif 1072 if (!(rt->dst.flags & DST_NOCACHE)) 1073 dst_free(&rt->dst); 1074 } 1075 return err; 1076 1077 #ifdef CONFIG_IPV6_SUBTREES 1078 /* Subtree creation failed, probably main tree node 1079 is orphan. If it is, shoot it. 1080 */ 1081 st_failure: 1082 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT))) 1083 fib6_repair_tree(info->nl_net, fn); 1084 if (!(rt->dst.flags & DST_NOCACHE)) 1085 dst_free(&rt->dst); 1086 return err; 1087 #endif 1088 } 1089 1090 /* 1091 * Routing tree lookup 1092 * 1093 */ 1094 1095 struct lookup_args { 1096 int offset; /* key offset on rt6_info */ 1097 const struct in6_addr *addr; /* search key */ 1098 }; 1099 1100 static struct fib6_node *fib6_lookup_1(struct fib6_node *root, 1101 struct lookup_args *args) 1102 { 1103 struct fib6_node *fn; 1104 __be32 dir; 1105 1106 if (unlikely(args->offset == 0)) 1107 return NULL; 1108 1109 /* 1110 * Descend on a tree 1111 */ 1112 1113 fn = root; 1114 1115 for (;;) { 1116 struct fib6_node *next; 1117 1118 dir = addr_bit_set(args->addr, fn->fn_bit); 1119 1120 next = dir ? fn->right : fn->left; 1121 1122 if (next) { 1123 fn = next; 1124 continue; 1125 } 1126 break; 1127 } 1128 1129 while (fn) { 1130 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) { 1131 struct rt6key *key; 1132 1133 key = (struct rt6key *) ((u8 *) fn->leaf + 1134 args->offset); 1135 1136 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) { 1137 #ifdef CONFIG_IPV6_SUBTREES 1138 if (fn->subtree) { 1139 struct fib6_node *sfn; 1140 sfn = fib6_lookup_1(fn->subtree, 1141 args + 1); 1142 if (!sfn) 1143 goto backtrack; 1144 fn = sfn; 1145 } 1146 #endif 1147 if (fn->fn_flags & RTN_RTINFO) 1148 return fn; 1149 } 1150 } 1151 #ifdef CONFIG_IPV6_SUBTREES 1152 backtrack: 1153 #endif 1154 if (fn->fn_flags & RTN_ROOT) 1155 break; 1156 1157 fn = fn->parent; 1158 } 1159 1160 return NULL; 1161 } 1162 1163 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr, 1164 const struct in6_addr *saddr) 1165 { 1166 struct fib6_node *fn; 1167 struct lookup_args args[] = { 1168 { 1169 .offset = offsetof(struct rt6_info, rt6i_dst), 1170 .addr = daddr, 1171 }, 1172 #ifdef CONFIG_IPV6_SUBTREES 1173 { 1174 .offset = offsetof(struct rt6_info, rt6i_src), 1175 .addr = saddr, 1176 }, 1177 #endif 1178 { 1179 .offset = 0, /* sentinel */ 1180 } 1181 }; 1182 1183 fn = fib6_lookup_1(root, daddr ? args : args + 1); 1184 if (!fn || fn->fn_flags & RTN_TL_ROOT) 1185 fn = root; 1186 1187 return fn; 1188 } 1189 1190 /* 1191 * Get node with specified destination prefix (and source prefix, 1192 * if subtrees are used) 1193 */ 1194 1195 1196 static struct fib6_node *fib6_locate_1(struct fib6_node *root, 1197 const struct in6_addr *addr, 1198 int plen, int offset) 1199 { 1200 struct fib6_node *fn; 1201 1202 for (fn = root; fn ; ) { 1203 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset); 1204 1205 /* 1206 * Prefix match 1207 */ 1208 if (plen < fn->fn_bit || 1209 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) 1210 return NULL; 1211 1212 if (plen == fn->fn_bit) 1213 return fn; 1214 1215 /* 1216 * We have more bits to go 1217 */ 1218 if (addr_bit_set(addr, fn->fn_bit)) 1219 fn = fn->right; 1220 else 1221 fn = fn->left; 1222 } 1223 return NULL; 1224 } 1225 1226 struct fib6_node *fib6_locate(struct fib6_node *root, 1227 const struct in6_addr *daddr, int dst_len, 1228 const struct in6_addr *saddr, int src_len) 1229 { 1230 struct fib6_node *fn; 1231 1232 fn = fib6_locate_1(root, daddr, dst_len, 1233 offsetof(struct rt6_info, rt6i_dst)); 1234 1235 #ifdef CONFIG_IPV6_SUBTREES 1236 if (src_len) { 1237 WARN_ON(saddr == NULL); 1238 if (fn && fn->subtree) 1239 fn = fib6_locate_1(fn->subtree, saddr, src_len, 1240 offsetof(struct rt6_info, rt6i_src)); 1241 } 1242 #endif 1243 1244 if (fn && fn->fn_flags & RTN_RTINFO) 1245 return fn; 1246 1247 return NULL; 1248 } 1249 1250 1251 /* 1252 * Deletion 1253 * 1254 */ 1255 1256 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn) 1257 { 1258 if (fn->fn_flags & RTN_ROOT) 1259 return net->ipv6.ip6_null_entry; 1260 1261 while (fn) { 1262 if (fn->left) 1263 return fn->left->leaf; 1264 if (fn->right) 1265 return fn->right->leaf; 1266 1267 fn = FIB6_SUBTREE(fn); 1268 } 1269 return NULL; 1270 } 1271 1272 /* 1273 * Called to trim the tree of intermediate nodes when possible. "fn" 1274 * is the node we want to try and remove. 1275 */ 1276 1277 static struct fib6_node *fib6_repair_tree(struct net *net, 1278 struct fib6_node *fn) 1279 { 1280 int children; 1281 int nstate; 1282 struct fib6_node *child, *pn; 1283 struct fib6_walker *w; 1284 int iter = 0; 1285 1286 for (;;) { 1287 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter); 1288 iter++; 1289 1290 WARN_ON(fn->fn_flags & RTN_RTINFO); 1291 WARN_ON(fn->fn_flags & RTN_TL_ROOT); 1292 WARN_ON(fn->leaf); 1293 1294 children = 0; 1295 child = NULL; 1296 if (fn->right) 1297 child = fn->right, children |= 1; 1298 if (fn->left) 1299 child = fn->left, children |= 2; 1300 1301 if (children == 3 || FIB6_SUBTREE(fn) 1302 #ifdef CONFIG_IPV6_SUBTREES 1303 /* Subtree root (i.e. fn) may have one child */ 1304 || (children && fn->fn_flags & RTN_ROOT) 1305 #endif 1306 ) { 1307 fn->leaf = fib6_find_prefix(net, fn); 1308 #if RT6_DEBUG >= 2 1309 if (!fn->leaf) { 1310 WARN_ON(!fn->leaf); 1311 fn->leaf = net->ipv6.ip6_null_entry; 1312 } 1313 #endif 1314 atomic_inc(&fn->leaf->rt6i_ref); 1315 return fn->parent; 1316 } 1317 1318 pn = fn->parent; 1319 #ifdef CONFIG_IPV6_SUBTREES 1320 if (FIB6_SUBTREE(pn) == fn) { 1321 WARN_ON(!(fn->fn_flags & RTN_ROOT)); 1322 FIB6_SUBTREE(pn) = NULL; 1323 nstate = FWS_L; 1324 } else { 1325 WARN_ON(fn->fn_flags & RTN_ROOT); 1326 #endif 1327 if (pn->right == fn) 1328 pn->right = child; 1329 else if (pn->left == fn) 1330 pn->left = child; 1331 #if RT6_DEBUG >= 2 1332 else 1333 WARN_ON(1); 1334 #endif 1335 if (child) 1336 child->parent = pn; 1337 nstate = FWS_R; 1338 #ifdef CONFIG_IPV6_SUBTREES 1339 } 1340 #endif 1341 1342 read_lock(&fib6_walker_lock); 1343 FOR_WALKERS(w) { 1344 if (!child) { 1345 if (w->root == fn) { 1346 w->root = w->node = NULL; 1347 RT6_TRACE("W %p adjusted by delroot 1\n", w); 1348 } else if (w->node == fn) { 1349 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); 1350 w->node = pn; 1351 w->state = nstate; 1352 } 1353 } else { 1354 if (w->root == fn) { 1355 w->root = child; 1356 RT6_TRACE("W %p adjusted by delroot 2\n", w); 1357 } 1358 if (w->node == fn) { 1359 w->node = child; 1360 if (children&2) { 1361 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); 1362 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT; 1363 } else { 1364 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); 1365 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT; 1366 } 1367 } 1368 } 1369 } 1370 read_unlock(&fib6_walker_lock); 1371 1372 node_free(fn); 1373 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn)) 1374 return pn; 1375 1376 rt6_release(pn->leaf); 1377 pn->leaf = NULL; 1378 fn = pn; 1379 } 1380 } 1381 1382 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp, 1383 struct nl_info *info) 1384 { 1385 struct fib6_walker *w; 1386 struct rt6_info *rt = *rtp; 1387 struct net *net = info->nl_net; 1388 1389 RT6_TRACE("fib6_del_route\n"); 1390 1391 /* Unlink it */ 1392 *rtp = rt->dst.rt6_next; 1393 rt->rt6i_node = NULL; 1394 net->ipv6.rt6_stats->fib_rt_entries--; 1395 net->ipv6.rt6_stats->fib_discarded_routes++; 1396 1397 /* Reset round-robin state, if necessary */ 1398 if (fn->rr_ptr == rt) 1399 fn->rr_ptr = NULL; 1400 1401 /* Remove this entry from other siblings */ 1402 if (rt->rt6i_nsiblings) { 1403 struct rt6_info *sibling, *next_sibling; 1404 1405 list_for_each_entry_safe(sibling, next_sibling, 1406 &rt->rt6i_siblings, rt6i_siblings) 1407 sibling->rt6i_nsiblings--; 1408 rt->rt6i_nsiblings = 0; 1409 list_del_init(&rt->rt6i_siblings); 1410 } 1411 1412 /* Adjust walkers */ 1413 read_lock(&fib6_walker_lock); 1414 FOR_WALKERS(w) { 1415 if (w->state == FWS_C && w->leaf == rt) { 1416 RT6_TRACE("walker %p adjusted by delroute\n", w); 1417 w->leaf = rt->dst.rt6_next; 1418 if (!w->leaf) 1419 w->state = FWS_U; 1420 } 1421 } 1422 read_unlock(&fib6_walker_lock); 1423 1424 rt->dst.rt6_next = NULL; 1425 1426 /* If it was last route, expunge its radix tree node */ 1427 if (!fn->leaf) { 1428 fn->fn_flags &= ~RTN_RTINFO; 1429 net->ipv6.rt6_stats->fib_route_nodes--; 1430 fn = fib6_repair_tree(net, fn); 1431 } 1432 1433 fib6_purge_rt(rt, fn, net); 1434 1435 inet6_rt_notify(RTM_DELROUTE, rt, info, 0); 1436 rt6_release(rt); 1437 } 1438 1439 int fib6_del(struct rt6_info *rt, struct nl_info *info) 1440 { 1441 struct net *net = info->nl_net; 1442 struct fib6_node *fn = rt->rt6i_node; 1443 struct rt6_info **rtp; 1444 1445 #if RT6_DEBUG >= 2 1446 if (rt->dst.obsolete > 0) { 1447 WARN_ON(fn); 1448 return -ENOENT; 1449 } 1450 #endif 1451 if (!fn || rt == net->ipv6.ip6_null_entry) 1452 return -ENOENT; 1453 1454 WARN_ON(!(fn->fn_flags & RTN_RTINFO)); 1455 1456 if (!(rt->rt6i_flags & RTF_CACHE)) { 1457 struct fib6_node *pn = fn; 1458 #ifdef CONFIG_IPV6_SUBTREES 1459 /* clones of this route might be in another subtree */ 1460 if (rt->rt6i_src.plen) { 1461 while (!(pn->fn_flags & RTN_ROOT)) 1462 pn = pn->parent; 1463 pn = pn->parent; 1464 } 1465 #endif 1466 fib6_prune_clones(info->nl_net, pn); 1467 } 1468 1469 /* 1470 * Walk the leaf entries looking for ourself 1471 */ 1472 1473 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) { 1474 if (*rtp == rt) { 1475 fib6_del_route(fn, rtp, info); 1476 return 0; 1477 } 1478 } 1479 return -ENOENT; 1480 } 1481 1482 /* 1483 * Tree traversal function. 1484 * 1485 * Certainly, it is not interrupt safe. 1486 * However, it is internally reenterable wrt itself and fib6_add/fib6_del. 1487 * It means, that we can modify tree during walking 1488 * and use this function for garbage collection, clone pruning, 1489 * cleaning tree when a device goes down etc. etc. 1490 * 1491 * It guarantees that every node will be traversed, 1492 * and that it will be traversed only once. 1493 * 1494 * Callback function w->func may return: 1495 * 0 -> continue walking. 1496 * positive value -> walking is suspended (used by tree dumps, 1497 * and probably by gc, if it will be split to several slices) 1498 * negative value -> terminate walking. 1499 * 1500 * The function itself returns: 1501 * 0 -> walk is complete. 1502 * >0 -> walk is incomplete (i.e. suspended) 1503 * <0 -> walk is terminated by an error. 1504 */ 1505 1506 static int fib6_walk_continue(struct fib6_walker *w) 1507 { 1508 struct fib6_node *fn, *pn; 1509 1510 for (;;) { 1511 fn = w->node; 1512 if (!fn) 1513 return 0; 1514 1515 if (w->prune && fn != w->root && 1516 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) { 1517 w->state = FWS_C; 1518 w->leaf = fn->leaf; 1519 } 1520 switch (w->state) { 1521 #ifdef CONFIG_IPV6_SUBTREES 1522 case FWS_S: 1523 if (FIB6_SUBTREE(fn)) { 1524 w->node = FIB6_SUBTREE(fn); 1525 continue; 1526 } 1527 w->state = FWS_L; 1528 #endif 1529 case FWS_L: 1530 if (fn->left) { 1531 w->node = fn->left; 1532 w->state = FWS_INIT; 1533 continue; 1534 } 1535 w->state = FWS_R; 1536 case FWS_R: 1537 if (fn->right) { 1538 w->node = fn->right; 1539 w->state = FWS_INIT; 1540 continue; 1541 } 1542 w->state = FWS_C; 1543 w->leaf = fn->leaf; 1544 case FWS_C: 1545 if (w->leaf && fn->fn_flags & RTN_RTINFO) { 1546 int err; 1547 1548 if (w->skip) { 1549 w->skip--; 1550 goto skip; 1551 } 1552 1553 err = w->func(w); 1554 if (err) 1555 return err; 1556 1557 w->count++; 1558 continue; 1559 } 1560 skip: 1561 w->state = FWS_U; 1562 case FWS_U: 1563 if (fn == w->root) 1564 return 0; 1565 pn = fn->parent; 1566 w->node = pn; 1567 #ifdef CONFIG_IPV6_SUBTREES 1568 if (FIB6_SUBTREE(pn) == fn) { 1569 WARN_ON(!(fn->fn_flags & RTN_ROOT)); 1570 w->state = FWS_L; 1571 continue; 1572 } 1573 #endif 1574 if (pn->left == fn) { 1575 w->state = FWS_R; 1576 continue; 1577 } 1578 if (pn->right == fn) { 1579 w->state = FWS_C; 1580 w->leaf = w->node->leaf; 1581 continue; 1582 } 1583 #if RT6_DEBUG >= 2 1584 WARN_ON(1); 1585 #endif 1586 } 1587 } 1588 } 1589 1590 static int fib6_walk(struct fib6_walker *w) 1591 { 1592 int res; 1593 1594 w->state = FWS_INIT; 1595 w->node = w->root; 1596 1597 fib6_walker_link(w); 1598 res = fib6_walk_continue(w); 1599 if (res <= 0) 1600 fib6_walker_unlink(w); 1601 return res; 1602 } 1603 1604 static int fib6_clean_node(struct fib6_walker *w) 1605 { 1606 int res; 1607 struct rt6_info *rt; 1608 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w); 1609 struct nl_info info = { 1610 .nl_net = c->net, 1611 }; 1612 1613 if (c->sernum != FIB6_NO_SERNUM_CHANGE && 1614 w->node->fn_sernum != c->sernum) 1615 w->node->fn_sernum = c->sernum; 1616 1617 if (!c->func) { 1618 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE); 1619 w->leaf = NULL; 1620 return 0; 1621 } 1622 1623 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { 1624 res = c->func(rt, c->arg); 1625 if (res < 0) { 1626 w->leaf = rt; 1627 res = fib6_del(rt, &info); 1628 if (res) { 1629 #if RT6_DEBUG >= 2 1630 pr_debug("%s: del failed: rt=%p@%p err=%d\n", 1631 __func__, rt, rt->rt6i_node, res); 1632 #endif 1633 continue; 1634 } 1635 return 0; 1636 } 1637 WARN_ON(res != 0); 1638 } 1639 w->leaf = rt; 1640 return 0; 1641 } 1642 1643 /* 1644 * Convenient frontend to tree walker. 1645 * 1646 * func is called on each route. 1647 * It may return -1 -> delete this route. 1648 * 0 -> continue walking 1649 * 1650 * prune==1 -> only immediate children of node (certainly, 1651 * ignoring pure split nodes) will be scanned. 1652 */ 1653 1654 static void fib6_clean_tree(struct net *net, struct fib6_node *root, 1655 int (*func)(struct rt6_info *, void *arg), 1656 bool prune, int sernum, void *arg) 1657 { 1658 struct fib6_cleaner c; 1659 1660 c.w.root = root; 1661 c.w.func = fib6_clean_node; 1662 c.w.prune = prune; 1663 c.w.count = 0; 1664 c.w.skip = 0; 1665 c.func = func; 1666 c.sernum = sernum; 1667 c.arg = arg; 1668 c.net = net; 1669 1670 fib6_walk(&c.w); 1671 } 1672 1673 static void __fib6_clean_all(struct net *net, 1674 int (*func)(struct rt6_info *, void *), 1675 int sernum, void *arg) 1676 { 1677 struct fib6_table *table; 1678 struct hlist_head *head; 1679 unsigned int h; 1680 1681 rcu_read_lock(); 1682 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 1683 head = &net->ipv6.fib_table_hash[h]; 1684 hlist_for_each_entry_rcu(table, head, tb6_hlist) { 1685 write_lock_bh(&table->tb6_lock); 1686 fib6_clean_tree(net, &table->tb6_root, 1687 func, false, sernum, arg); 1688 write_unlock_bh(&table->tb6_lock); 1689 } 1690 } 1691 rcu_read_unlock(); 1692 } 1693 1694 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *), 1695 void *arg) 1696 { 1697 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg); 1698 } 1699 1700 static int fib6_prune_clone(struct rt6_info *rt, void *arg) 1701 { 1702 if (rt->rt6i_flags & RTF_CACHE) { 1703 RT6_TRACE("pruning clone %p\n", rt); 1704 return -1; 1705 } 1706 1707 return 0; 1708 } 1709 1710 static void fib6_prune_clones(struct net *net, struct fib6_node *fn) 1711 { 1712 fib6_clean_tree(net, fn, fib6_prune_clone, true, 1713 FIB6_NO_SERNUM_CHANGE, NULL); 1714 } 1715 1716 static void fib6_flush_trees(struct net *net) 1717 { 1718 int new_sernum = fib6_new_sernum(net); 1719 1720 __fib6_clean_all(net, NULL, new_sernum, NULL); 1721 } 1722 1723 /* 1724 * Garbage collection 1725 */ 1726 1727 static struct fib6_gc_args 1728 { 1729 int timeout; 1730 int more; 1731 } gc_args; 1732 1733 static int fib6_age(struct rt6_info *rt, void *arg) 1734 { 1735 unsigned long now = jiffies; 1736 1737 /* 1738 * check addrconf expiration here. 1739 * Routes are expired even if they are in use. 1740 * 1741 * Also age clones. Note, that clones are aged out 1742 * only if they are not in use now. 1743 */ 1744 1745 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) { 1746 if (time_after(now, rt->dst.expires)) { 1747 RT6_TRACE("expiring %p\n", rt); 1748 return -1; 1749 } 1750 gc_args.more++; 1751 } else if (rt->rt6i_flags & RTF_CACHE) { 1752 if (atomic_read(&rt->dst.__refcnt) == 0 && 1753 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) { 1754 RT6_TRACE("aging clone %p\n", rt); 1755 return -1; 1756 } else if (rt->rt6i_flags & RTF_GATEWAY) { 1757 struct neighbour *neigh; 1758 __u8 neigh_flags = 0; 1759 1760 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1761 if (neigh) { 1762 neigh_flags = neigh->flags; 1763 neigh_release(neigh); 1764 } 1765 if (!(neigh_flags & NTF_ROUTER)) { 1766 RT6_TRACE("purging route %p via non-router but gateway\n", 1767 rt); 1768 return -1; 1769 } 1770 } 1771 gc_args.more++; 1772 } 1773 1774 return 0; 1775 } 1776 1777 static DEFINE_SPINLOCK(fib6_gc_lock); 1778 1779 void fib6_run_gc(unsigned long expires, struct net *net, bool force) 1780 { 1781 unsigned long now; 1782 1783 if (force) { 1784 spin_lock_bh(&fib6_gc_lock); 1785 } else if (!spin_trylock_bh(&fib6_gc_lock)) { 1786 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ); 1787 return; 1788 } 1789 gc_args.timeout = expires ? (int)expires : 1790 net->ipv6.sysctl.ip6_rt_gc_interval; 1791 1792 gc_args.more = icmp6_dst_gc(); 1793 1794 fib6_clean_all(net, fib6_age, NULL); 1795 now = jiffies; 1796 net->ipv6.ip6_rt_last_gc = now; 1797 1798 if (gc_args.more) 1799 mod_timer(&net->ipv6.ip6_fib_timer, 1800 round_jiffies(now 1801 + net->ipv6.sysctl.ip6_rt_gc_interval)); 1802 else 1803 del_timer(&net->ipv6.ip6_fib_timer); 1804 spin_unlock_bh(&fib6_gc_lock); 1805 } 1806 1807 static void fib6_gc_timer_cb(unsigned long arg) 1808 { 1809 fib6_run_gc(0, (struct net *)arg, true); 1810 } 1811 1812 static int __net_init fib6_net_init(struct net *net) 1813 { 1814 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ; 1815 1816 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net); 1817 1818 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL); 1819 if (!net->ipv6.rt6_stats) 1820 goto out_timer; 1821 1822 /* Avoid false sharing : Use at least a full cache line */ 1823 size = max_t(size_t, size, L1_CACHE_BYTES); 1824 1825 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL); 1826 if (!net->ipv6.fib_table_hash) 1827 goto out_rt6_stats; 1828 1829 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl), 1830 GFP_KERNEL); 1831 if (!net->ipv6.fib6_main_tbl) 1832 goto out_fib_table_hash; 1833 1834 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; 1835 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; 1836 net->ipv6.fib6_main_tbl->tb6_root.fn_flags = 1837 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; 1838 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers); 1839 1840 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 1841 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl), 1842 GFP_KERNEL); 1843 if (!net->ipv6.fib6_local_tbl) 1844 goto out_fib6_main_tbl; 1845 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; 1846 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; 1847 net->ipv6.fib6_local_tbl->tb6_root.fn_flags = 1848 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; 1849 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers); 1850 #endif 1851 fib6_tables_init(net); 1852 1853 return 0; 1854 1855 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 1856 out_fib6_main_tbl: 1857 kfree(net->ipv6.fib6_main_tbl); 1858 #endif 1859 out_fib_table_hash: 1860 kfree(net->ipv6.fib_table_hash); 1861 out_rt6_stats: 1862 kfree(net->ipv6.rt6_stats); 1863 out_timer: 1864 return -ENOMEM; 1865 } 1866 1867 static void fib6_net_exit(struct net *net) 1868 { 1869 rt6_ifdown(net, NULL); 1870 del_timer_sync(&net->ipv6.ip6_fib_timer); 1871 1872 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 1873 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers); 1874 kfree(net->ipv6.fib6_local_tbl); 1875 #endif 1876 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers); 1877 kfree(net->ipv6.fib6_main_tbl); 1878 kfree(net->ipv6.fib_table_hash); 1879 kfree(net->ipv6.rt6_stats); 1880 } 1881 1882 static struct pernet_operations fib6_net_ops = { 1883 .init = fib6_net_init, 1884 .exit = fib6_net_exit, 1885 }; 1886 1887 int __init fib6_init(void) 1888 { 1889 int ret = -ENOMEM; 1890 1891 fib6_node_kmem = kmem_cache_create("fib6_nodes", 1892 sizeof(struct fib6_node), 1893 0, SLAB_HWCACHE_ALIGN, 1894 NULL); 1895 if (!fib6_node_kmem) 1896 goto out; 1897 1898 ret = register_pernet_subsys(&fib6_net_ops); 1899 if (ret) 1900 goto out_kmem_cache_create; 1901 1902 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib, 1903 NULL); 1904 if (ret) 1905 goto out_unregister_subsys; 1906 1907 __fib6_flush_trees = fib6_flush_trees; 1908 out: 1909 return ret; 1910 1911 out_unregister_subsys: 1912 unregister_pernet_subsys(&fib6_net_ops); 1913 out_kmem_cache_create: 1914 kmem_cache_destroy(fib6_node_kmem); 1915 goto out; 1916 } 1917 1918 void fib6_gc_cleanup(void) 1919 { 1920 unregister_pernet_subsys(&fib6_net_ops); 1921 kmem_cache_destroy(fib6_node_kmem); 1922 } 1923 1924 #ifdef CONFIG_PROC_FS 1925 1926 struct ipv6_route_iter { 1927 struct seq_net_private p; 1928 struct fib6_walker w; 1929 loff_t skip; 1930 struct fib6_table *tbl; 1931 int sernum; 1932 }; 1933 1934 static int ipv6_route_seq_show(struct seq_file *seq, void *v) 1935 { 1936 struct rt6_info *rt = v; 1937 struct ipv6_route_iter *iter = seq->private; 1938 1939 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 1940 1941 #ifdef CONFIG_IPV6_SUBTREES 1942 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 1943 #else 1944 seq_puts(seq, "00000000000000000000000000000000 00 "); 1945 #endif 1946 if (rt->rt6i_flags & RTF_GATEWAY) 1947 seq_printf(seq, "%pi6", &rt->rt6i_gateway); 1948 else 1949 seq_puts(seq, "00000000000000000000000000000000"); 1950 1951 seq_printf(seq, " %08x %08x %08x %08x %8s\n", 1952 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 1953 rt->dst.__use, rt->rt6i_flags, 1954 rt->dst.dev ? rt->dst.dev->name : ""); 1955 iter->w.leaf = NULL; 1956 return 0; 1957 } 1958 1959 static int ipv6_route_yield(struct fib6_walker *w) 1960 { 1961 struct ipv6_route_iter *iter = w->args; 1962 1963 if (!iter->skip) 1964 return 1; 1965 1966 do { 1967 iter->w.leaf = iter->w.leaf->dst.rt6_next; 1968 iter->skip--; 1969 if (!iter->skip && iter->w.leaf) 1970 return 1; 1971 } while (iter->w.leaf); 1972 1973 return 0; 1974 } 1975 1976 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter) 1977 { 1978 memset(&iter->w, 0, sizeof(iter->w)); 1979 iter->w.func = ipv6_route_yield; 1980 iter->w.root = &iter->tbl->tb6_root; 1981 iter->w.state = FWS_INIT; 1982 iter->w.node = iter->w.root; 1983 iter->w.args = iter; 1984 iter->sernum = iter->w.root->fn_sernum; 1985 INIT_LIST_HEAD(&iter->w.lh); 1986 fib6_walker_link(&iter->w); 1987 } 1988 1989 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl, 1990 struct net *net) 1991 { 1992 unsigned int h; 1993 struct hlist_node *node; 1994 1995 if (tbl) { 1996 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1; 1997 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist)); 1998 } else { 1999 h = 0; 2000 node = NULL; 2001 } 2002 2003 while (!node && h < FIB6_TABLE_HASHSZ) { 2004 node = rcu_dereference_bh( 2005 hlist_first_rcu(&net->ipv6.fib_table_hash[h++])); 2006 } 2007 return hlist_entry_safe(node, struct fib6_table, tb6_hlist); 2008 } 2009 2010 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter) 2011 { 2012 if (iter->sernum != iter->w.root->fn_sernum) { 2013 iter->sernum = iter->w.root->fn_sernum; 2014 iter->w.state = FWS_INIT; 2015 iter->w.node = iter->w.root; 2016 WARN_ON(iter->w.skip); 2017 iter->w.skip = iter->w.count; 2018 } 2019 } 2020 2021 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2022 { 2023 int r; 2024 struct rt6_info *n; 2025 struct net *net = seq_file_net(seq); 2026 struct ipv6_route_iter *iter = seq->private; 2027 2028 if (!v) 2029 goto iter_table; 2030 2031 n = ((struct rt6_info *)v)->dst.rt6_next; 2032 if (n) { 2033 ++*pos; 2034 return n; 2035 } 2036 2037 iter_table: 2038 ipv6_route_check_sernum(iter); 2039 read_lock(&iter->tbl->tb6_lock); 2040 r = fib6_walk_continue(&iter->w); 2041 read_unlock(&iter->tbl->tb6_lock); 2042 if (r > 0) { 2043 if (v) 2044 ++*pos; 2045 return iter->w.leaf; 2046 } else if (r < 0) { 2047 fib6_walker_unlink(&iter->w); 2048 return NULL; 2049 } 2050 fib6_walker_unlink(&iter->w); 2051 2052 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net); 2053 if (!iter->tbl) 2054 return NULL; 2055 2056 ipv6_route_seq_setup_walk(iter); 2057 goto iter_table; 2058 } 2059 2060 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos) 2061 __acquires(RCU_BH) 2062 { 2063 struct net *net = seq_file_net(seq); 2064 struct ipv6_route_iter *iter = seq->private; 2065 2066 rcu_read_lock_bh(); 2067 iter->tbl = ipv6_route_seq_next_table(NULL, net); 2068 iter->skip = *pos; 2069 2070 if (iter->tbl) { 2071 ipv6_route_seq_setup_walk(iter); 2072 return ipv6_route_seq_next(seq, NULL, pos); 2073 } else { 2074 return NULL; 2075 } 2076 } 2077 2078 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter) 2079 { 2080 struct fib6_walker *w = &iter->w; 2081 return w->node && !(w->state == FWS_U && w->node == w->root); 2082 } 2083 2084 static void ipv6_route_seq_stop(struct seq_file *seq, void *v) 2085 __releases(RCU_BH) 2086 { 2087 struct ipv6_route_iter *iter = seq->private; 2088 2089 if (ipv6_route_iter_active(iter)) 2090 fib6_walker_unlink(&iter->w); 2091 2092 rcu_read_unlock_bh(); 2093 } 2094 2095 static const struct seq_operations ipv6_route_seq_ops = { 2096 .start = ipv6_route_seq_start, 2097 .next = ipv6_route_seq_next, 2098 .stop = ipv6_route_seq_stop, 2099 .show = ipv6_route_seq_show 2100 }; 2101 2102 int ipv6_route_open(struct inode *inode, struct file *file) 2103 { 2104 return seq_open_net(inode, file, &ipv6_route_seq_ops, 2105 sizeof(struct ipv6_route_iter)); 2106 } 2107 2108 #endif /* CONFIG_PROC_FS */ 2109