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