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