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