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