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