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