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