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