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