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