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