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