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