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