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