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