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