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. | |
12 | */ | |
13 | ||
14 | /* | |
15 | * Changes: | |
16 | * Yuji SEKIYA @USAGI: Support default route on router node; | |
17 | * remove ip6_null_entry from the top of | |
18 | * routing table. | |
c0bece9f | 19 | * Ville Nuorvala: Fixed routing subtrees. |
1da177e4 | 20 | */ |
f3213831 JP |
21 | |
22 | #define pr_fmt(fmt) "IPv6: " fmt | |
23 | ||
1da177e4 LT |
24 | #include <linux/errno.h> |
25 | #include <linux/types.h> | |
26 | #include <linux/net.h> | |
27 | #include <linux/route.h> | |
28 | #include <linux/netdevice.h> | |
29 | #include <linux/in6.h> | |
30 | #include <linux/init.h> | |
c71099ac | 31 | #include <linux/list.h> |
5a0e3ad6 | 32 | #include <linux/slab.h> |
1da177e4 | 33 | |
1da177e4 LT |
34 | #include <net/ipv6.h> |
35 | #include <net/ndisc.h> | |
36 | #include <net/addrconf.h> | |
37 | ||
38 | #include <net/ip6_fib.h> | |
39 | #include <net/ip6_route.h> | |
40 | ||
41 | #define RT6_DEBUG 2 | |
42 | ||
43 | #if RT6_DEBUG >= 3 | |
91df42be | 44 | #define RT6_TRACE(x...) pr_debug(x) |
1da177e4 LT |
45 | #else |
46 | #define RT6_TRACE(x...) do { ; } while (0) | |
47 | #endif | |
48 | ||
e18b890b | 49 | static struct kmem_cache * fib6_node_kmem __read_mostly; |
1da177e4 LT |
50 | |
51 | enum fib_walk_state_t | |
52 | { | |
53 | #ifdef CONFIG_IPV6_SUBTREES | |
54 | FWS_S, | |
55 | #endif | |
56 | FWS_L, | |
57 | FWS_R, | |
58 | FWS_C, | |
59 | FWS_U | |
60 | }; | |
61 | ||
62 | struct fib6_cleaner_t | |
63 | { | |
64 | struct fib6_walker_t w; | |
ec7d43c2 | 65 | struct net *net; |
1da177e4 LT |
66 | int (*func)(struct rt6_info *, void *arg); |
67 | void *arg; | |
68 | }; | |
69 | ||
90d41122 | 70 | static DEFINE_RWLOCK(fib6_walker_lock); |
1da177e4 LT |
71 | |
72 | #ifdef CONFIG_IPV6_SUBTREES | |
73 | #define FWS_INIT FWS_S | |
1da177e4 LT |
74 | #else |
75 | #define FWS_INIT FWS_L | |
1da177e4 LT |
76 | #endif |
77 | ||
ec7d43c2 BT |
78 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn, |
79 | struct rt6_info *rt); | |
8ed67789 DL |
80 | static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn); |
81 | static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn); | |
90d41122 AB |
82 | static int fib6_walk(struct fib6_walker_t *w); |
83 | static int fib6_walk_continue(struct fib6_walker_t *w); | |
1da177e4 LT |
84 | |
85 | /* | |
86 | * A routing update causes an increase of the serial number on the | |
87 | * affected subtree. This allows for cached routes to be asynchronously | |
88 | * tested when modifications are made to the destination cache as a | |
89 | * result of redirects, path MTU changes, etc. | |
90 | */ | |
91 | ||
92 | static __u32 rt_sernum; | |
93 | ||
5b7c931d DL |
94 | static void fib6_gc_timer_cb(unsigned long arg); |
95 | ||
bbef49da AD |
96 | static LIST_HEAD(fib6_walkers); |
97 | #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh) | |
1da177e4 | 98 | |
90d41122 AB |
99 | static inline void fib6_walker_link(struct fib6_walker_t *w) |
100 | { | |
101 | write_lock_bh(&fib6_walker_lock); | |
bbef49da | 102 | list_add(&w->lh, &fib6_walkers); |
90d41122 AB |
103 | write_unlock_bh(&fib6_walker_lock); |
104 | } | |
105 | ||
106 | static inline void fib6_walker_unlink(struct fib6_walker_t *w) | |
107 | { | |
108 | write_lock_bh(&fib6_walker_lock); | |
bbef49da | 109 | list_del(&w->lh); |
90d41122 AB |
110 | write_unlock_bh(&fib6_walker_lock); |
111 | } | |
1da177e4 LT |
112 | static __inline__ u32 fib6_new_sernum(void) |
113 | { | |
114 | u32 n = ++rt_sernum; | |
115 | if ((__s32)n <= 0) | |
116 | rt_sernum = n = 1; | |
117 | return n; | |
118 | } | |
119 | ||
120 | /* | |
121 | * Auxiliary address test functions for the radix tree. | |
122 | * | |
1ab1457c | 123 | * These assume a 32bit processor (although it will work on |
1da177e4 LT |
124 | * 64bit processors) |
125 | */ | |
126 | ||
127 | /* | |
128 | * test bit | |
129 | */ | |
02cdce53 YH |
130 | #if defined(__LITTLE_ENDIAN) |
131 | # define BITOP_BE32_SWIZZLE (0x1F & ~7) | |
132 | #else | |
133 | # define BITOP_BE32_SWIZZLE 0 | |
134 | #endif | |
1da177e4 | 135 | |
b71d1d42 | 136 | static __inline__ __be32 addr_bit_set(const void *token, int fn_bit) |
1da177e4 | 137 | { |
b71d1d42 | 138 | const __be32 *addr = token; |
02cdce53 YH |
139 | /* |
140 | * Here, | |
141 | * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f) | |
142 | * is optimized version of | |
143 | * htonl(1 << ((~fn_bit)&0x1F)) | |
144 | * See include/asm-generic/bitops/le.h. | |
145 | */ | |
0eae88f3 ED |
146 | return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) & |
147 | addr[fn_bit >> 5]; | |
1da177e4 LT |
148 | } |
149 | ||
1da177e4 LT |
150 | static __inline__ struct fib6_node * node_alloc(void) |
151 | { | |
152 | struct fib6_node *fn; | |
153 | ||
c3762229 | 154 | fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); |
1da177e4 LT |
155 | |
156 | return fn; | |
157 | } | |
158 | ||
159 | static __inline__ void node_free(struct fib6_node * fn) | |
160 | { | |
161 | kmem_cache_free(fib6_node_kmem, fn); | |
162 | } | |
163 | ||
164 | static __inline__ void rt6_release(struct rt6_info *rt) | |
165 | { | |
166 | if (atomic_dec_and_test(&rt->rt6i_ref)) | |
d8d1f30b | 167 | dst_free(&rt->dst); |
1da177e4 LT |
168 | } |
169 | ||
4e39d5e4 SD |
170 | static void fib6_free_table(struct fib6_table *table) |
171 | { | |
172 | inetpeer_invalidate_tree(&table->tb6_peers); | |
173 | kfree(table); | |
174 | } | |
175 | ||
58f09b78 | 176 | static void fib6_link_table(struct net *net, struct fib6_table *tb) |
1b43af54 PM |
177 | { |
178 | unsigned int h; | |
179 | ||
375216ad TG |
180 | /* |
181 | * Initialize table lock at a single place to give lockdep a key, | |
182 | * tables aren't visible prior to being linked to the list. | |
183 | */ | |
184 | rwlock_init(&tb->tb6_lock); | |
185 | ||
a33bc5c1 | 186 | h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); |
1b43af54 PM |
187 | |
188 | /* | |
189 | * No protection necessary, this is the only list mutatation | |
190 | * operation, tables never disappear once they exist. | |
191 | */ | |
58f09b78 | 192 | hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]); |
1b43af54 | 193 | } |
c71099ac | 194 | |
1b43af54 | 195 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
e0b85590 | 196 | |
8ed67789 | 197 | static struct fib6_table *fib6_alloc_table(struct net *net, u32 id) |
c71099ac TG |
198 | { |
199 | struct fib6_table *table; | |
200 | ||
201 | table = kzalloc(sizeof(*table), GFP_ATOMIC); | |
507c9b1e | 202 | if (table) { |
c71099ac | 203 | table->tb6_id = id; |
8ed67789 | 204 | table->tb6_root.leaf = net->ipv6.ip6_null_entry; |
c71099ac | 205 | table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
8e773277 | 206 | inet_peer_base_init(&table->tb6_peers); |
c71099ac TG |
207 | } |
208 | ||
209 | return table; | |
210 | } | |
211 | ||
58f09b78 | 212 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
c71099ac TG |
213 | { |
214 | struct fib6_table *tb; | |
215 | ||
216 | if (id == 0) | |
217 | id = RT6_TABLE_MAIN; | |
58f09b78 | 218 | tb = fib6_get_table(net, id); |
c71099ac TG |
219 | if (tb) |
220 | return tb; | |
221 | ||
8ed67789 | 222 | tb = fib6_alloc_table(net, id); |
507c9b1e | 223 | if (tb) |
58f09b78 | 224 | fib6_link_table(net, tb); |
c71099ac TG |
225 | |
226 | return tb; | |
227 | } | |
228 | ||
58f09b78 | 229 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
c71099ac TG |
230 | { |
231 | struct fib6_table *tb; | |
58f09b78 | 232 | struct hlist_head *head; |
c71099ac TG |
233 | unsigned int h; |
234 | ||
235 | if (id == 0) | |
236 | id = RT6_TABLE_MAIN; | |
a33bc5c1 | 237 | h = id & (FIB6_TABLE_HASHSZ - 1); |
c71099ac | 238 | rcu_read_lock(); |
58f09b78 | 239 | head = &net->ipv6.fib_table_hash[h]; |
b67bfe0d | 240 | hlist_for_each_entry_rcu(tb, head, tb6_hlist) { |
c71099ac TG |
241 | if (tb->tb6_id == id) { |
242 | rcu_read_unlock(); | |
243 | return tb; | |
244 | } | |
245 | } | |
246 | rcu_read_unlock(); | |
247 | ||
248 | return NULL; | |
249 | } | |
250 | ||
2c8c1e72 | 251 | static void __net_init fib6_tables_init(struct net *net) |
c71099ac | 252 | { |
58f09b78 DL |
253 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
254 | fib6_link_table(net, net->ipv6.fib6_local_tbl); | |
c71099ac | 255 | } |
c71099ac TG |
256 | #else |
257 | ||
58f09b78 | 258 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
c71099ac | 259 | { |
58f09b78 | 260 | return fib6_get_table(net, id); |
c71099ac TG |
261 | } |
262 | ||
58f09b78 | 263 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
c71099ac | 264 | { |
58f09b78 | 265 | return net->ipv6.fib6_main_tbl; |
c71099ac TG |
266 | } |
267 | ||
4c9483b2 | 268 | struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, |
58f09b78 | 269 | int flags, pol_lookup_t lookup) |
c71099ac | 270 | { |
4c9483b2 | 271 | return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags); |
c71099ac TG |
272 | } |
273 | ||
2c8c1e72 | 274 | static void __net_init fib6_tables_init(struct net *net) |
c71099ac | 275 | { |
58f09b78 | 276 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
c71099ac TG |
277 | } |
278 | ||
279 | #endif | |
280 | ||
1b43af54 PM |
281 | static int fib6_dump_node(struct fib6_walker_t *w) |
282 | { | |
283 | int res; | |
284 | struct rt6_info *rt; | |
285 | ||
d8d1f30b | 286 | for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { |
1b43af54 PM |
287 | res = rt6_dump_route(rt, w->args); |
288 | if (res < 0) { | |
289 | /* Frame is full, suspend walking */ | |
290 | w->leaf = rt; | |
291 | return 1; | |
292 | } | |
547b792c | 293 | WARN_ON(res == 0); |
1b43af54 PM |
294 | } |
295 | w->leaf = NULL; | |
296 | return 0; | |
297 | } | |
298 | ||
299 | static void fib6_dump_end(struct netlink_callback *cb) | |
300 | { | |
301 | struct fib6_walker_t *w = (void*)cb->args[2]; | |
302 | ||
303 | if (w) { | |
7891cc81 HX |
304 | if (cb->args[4]) { |
305 | cb->args[4] = 0; | |
306 | fib6_walker_unlink(w); | |
307 | } | |
1b43af54 PM |
308 | cb->args[2] = 0; |
309 | kfree(w); | |
310 | } | |
311 | cb->done = (void*)cb->args[3]; | |
312 | cb->args[1] = 3; | |
313 | } | |
314 | ||
315 | static int fib6_dump_done(struct netlink_callback *cb) | |
316 | { | |
317 | fib6_dump_end(cb); | |
318 | return cb->done ? cb->done(cb) : 0; | |
319 | } | |
320 | ||
321 | static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, | |
322 | struct netlink_callback *cb) | |
323 | { | |
324 | struct fib6_walker_t *w; | |
325 | int res; | |
326 | ||
327 | w = (void *)cb->args[2]; | |
328 | w->root = &table->tb6_root; | |
329 | ||
330 | if (cb->args[4] == 0) { | |
2bec5a36 PM |
331 | w->count = 0; |
332 | w->skip = 0; | |
333 | ||
1b43af54 PM |
334 | read_lock_bh(&table->tb6_lock); |
335 | res = fib6_walk(w); | |
336 | read_unlock_bh(&table->tb6_lock); | |
2bec5a36 | 337 | if (res > 0) { |
1b43af54 | 338 | cb->args[4] = 1; |
2bec5a36 PM |
339 | cb->args[5] = w->root->fn_sernum; |
340 | } | |
1b43af54 | 341 | } else { |
2bec5a36 PM |
342 | if (cb->args[5] != w->root->fn_sernum) { |
343 | /* Begin at the root if the tree changed */ | |
344 | cb->args[5] = w->root->fn_sernum; | |
345 | w->state = FWS_INIT; | |
346 | w->node = w->root; | |
347 | w->skip = w->count; | |
348 | } else | |
349 | w->skip = 0; | |
350 | ||
1b43af54 PM |
351 | read_lock_bh(&table->tb6_lock); |
352 | res = fib6_walk_continue(w); | |
353 | read_unlock_bh(&table->tb6_lock); | |
7891cc81 HX |
354 | if (res <= 0) { |
355 | fib6_walker_unlink(w); | |
356 | cb->args[4] = 0; | |
1b43af54 | 357 | } |
1b43af54 | 358 | } |
7891cc81 | 359 | |
1b43af54 PM |
360 | return res; |
361 | } | |
362 | ||
c127ea2c | 363 | static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) |
1b43af54 | 364 | { |
3b1e0a65 | 365 | struct net *net = sock_net(skb->sk); |
1b43af54 PM |
366 | unsigned int h, s_h; |
367 | unsigned int e = 0, s_e; | |
368 | struct rt6_rtnl_dump_arg arg; | |
369 | struct fib6_walker_t *w; | |
370 | struct fib6_table *tb; | |
58f09b78 | 371 | struct hlist_head *head; |
1b43af54 PM |
372 | int res = 0; |
373 | ||
374 | s_h = cb->args[0]; | |
375 | s_e = cb->args[1]; | |
376 | ||
377 | w = (void *)cb->args[2]; | |
507c9b1e | 378 | if (!w) { |
1b43af54 PM |
379 | /* New dump: |
380 | * | |
381 | * 1. hook callback destructor. | |
382 | */ | |
383 | cb->args[3] = (long)cb->done; | |
384 | cb->done = fib6_dump_done; | |
385 | ||
386 | /* | |
387 | * 2. allocate and initialize walker. | |
388 | */ | |
389 | w = kzalloc(sizeof(*w), GFP_ATOMIC); | |
507c9b1e | 390 | if (!w) |
1b43af54 PM |
391 | return -ENOMEM; |
392 | w->func = fib6_dump_node; | |
393 | cb->args[2] = (long)w; | |
394 | } | |
395 | ||
396 | arg.skb = skb; | |
397 | arg.cb = cb; | |
191cd582 | 398 | arg.net = net; |
1b43af54 PM |
399 | w->args = &arg; |
400 | ||
e67f88dd | 401 | rcu_read_lock(); |
a33bc5c1 | 402 | for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) { |
1b43af54 | 403 | e = 0; |
58f09b78 | 404 | head = &net->ipv6.fib_table_hash[h]; |
b67bfe0d | 405 | hlist_for_each_entry_rcu(tb, head, tb6_hlist) { |
1b43af54 PM |
406 | if (e < s_e) |
407 | goto next; | |
408 | res = fib6_dump_table(tb, skb, cb); | |
409 | if (res != 0) | |
410 | goto out; | |
411 | next: | |
412 | e++; | |
413 | } | |
414 | } | |
415 | out: | |
e67f88dd | 416 | rcu_read_unlock(); |
1b43af54 PM |
417 | cb->args[1] = e; |
418 | cb->args[0] = h; | |
419 | ||
420 | res = res < 0 ? res : skb->len; | |
421 | if (res <= 0) | |
422 | fib6_dump_end(cb); | |
423 | return res; | |
424 | } | |
1da177e4 LT |
425 | |
426 | /* | |
427 | * Routing Table | |
428 | * | |
429 | * return the appropriate node for a routing tree "add" operation | |
430 | * by either creating and inserting or by returning an existing | |
431 | * node. | |
432 | */ | |
433 | ||
434 | static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr, | |
435 | int addrlen, int plen, | |
4a287eba MV |
436 | int offset, int allow_create, |
437 | int replace_required) | |
1da177e4 LT |
438 | { |
439 | struct fib6_node *fn, *in, *ln; | |
440 | struct fib6_node *pn = NULL; | |
441 | struct rt6key *key; | |
442 | int bit; | |
1ab1457c | 443 | __be32 dir = 0; |
1da177e4 LT |
444 | __u32 sernum = fib6_new_sernum(); |
445 | ||
446 | RT6_TRACE("fib6_add_1\n"); | |
447 | ||
448 | /* insert node in tree */ | |
449 | ||
450 | fn = root; | |
451 | ||
452 | do { | |
453 | key = (struct rt6key *)((u8 *)fn->leaf + offset); | |
454 | ||
455 | /* | |
456 | * Prefix match | |
457 | */ | |
458 | if (plen < fn->fn_bit || | |
4a287eba | 459 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) { |
14df015b MV |
460 | if (!allow_create) { |
461 | if (replace_required) { | |
f3213831 | 462 | pr_warn("Can't replace route, no match found\n"); |
14df015b MV |
463 | return ERR_PTR(-ENOENT); |
464 | } | |
f3213831 | 465 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
14df015b | 466 | } |
1da177e4 | 467 | goto insert_above; |
4a287eba | 468 | } |
1ab1457c | 469 | |
1da177e4 LT |
470 | /* |
471 | * Exact match ? | |
472 | */ | |
1ab1457c | 473 | |
1da177e4 LT |
474 | if (plen == fn->fn_bit) { |
475 | /* clean up an intermediate node */ | |
507c9b1e | 476 | if (!(fn->fn_flags & RTN_RTINFO)) { |
1da177e4 LT |
477 | rt6_release(fn->leaf); |
478 | fn->leaf = NULL; | |
479 | } | |
1ab1457c | 480 | |
1da177e4 | 481 | fn->fn_sernum = sernum; |
1ab1457c | 482 | |
1da177e4 LT |
483 | return fn; |
484 | } | |
485 | ||
486 | /* | |
487 | * We have more bits to go | |
488 | */ | |
1ab1457c | 489 | |
1da177e4 LT |
490 | /* Try to walk down on tree. */ |
491 | fn->fn_sernum = sernum; | |
492 | dir = addr_bit_set(addr, fn->fn_bit); | |
493 | pn = fn; | |
494 | fn = dir ? fn->right: fn->left; | |
495 | } while (fn); | |
496 | ||
14df015b | 497 | if (!allow_create) { |
4a287eba MV |
498 | /* We should not create new node because |
499 | * NLM_F_REPLACE was specified without NLM_F_CREATE | |
500 | * I assume it is safe to require NLM_F_CREATE when | |
501 | * REPLACE flag is used! Later we may want to remove the | |
502 | * check for replace_required, because according | |
503 | * to netlink specification, NLM_F_CREATE | |
504 | * MUST be specified if new route is created. | |
505 | * That would keep IPv6 consistent with IPv4 | |
506 | */ | |
14df015b | 507 | if (replace_required) { |
f3213831 | 508 | pr_warn("Can't replace route, no match found\n"); |
14df015b MV |
509 | return ERR_PTR(-ENOENT); |
510 | } | |
f3213831 | 511 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
4a287eba | 512 | } |
1da177e4 LT |
513 | /* |
514 | * We walked to the bottom of tree. | |
515 | * Create new leaf node without children. | |
516 | */ | |
517 | ||
518 | ln = node_alloc(); | |
519 | ||
507c9b1e | 520 | if (!ln) |
188c517a | 521 | return ERR_PTR(-ENOMEM); |
1da177e4 | 522 | ln->fn_bit = plen; |
1ab1457c | 523 | |
1da177e4 LT |
524 | ln->parent = pn; |
525 | ln->fn_sernum = sernum; | |
526 | ||
527 | if (dir) | |
528 | pn->right = ln; | |
529 | else | |
530 | pn->left = ln; | |
531 | ||
532 | return ln; | |
533 | ||
534 | ||
535 | insert_above: | |
536 | /* | |
1ab1457c | 537 | * split since we don't have a common prefix anymore or |
1da177e4 LT |
538 | * we have a less significant route. |
539 | * we've to insert an intermediate node on the list | |
540 | * this new node will point to the one we need to create | |
541 | * and the current | |
542 | */ | |
543 | ||
544 | pn = fn->parent; | |
545 | ||
546 | /* find 1st bit in difference between the 2 addrs. | |
547 | ||
971f359d | 548 | See comment in __ipv6_addr_diff: bit may be an invalid value, |
1da177e4 LT |
549 | but if it is >= plen, the value is ignored in any case. |
550 | */ | |
1ab1457c | 551 | |
971f359d | 552 | bit = __ipv6_addr_diff(addr, &key->addr, addrlen); |
1da177e4 | 553 | |
1ab1457c YH |
554 | /* |
555 | * (intermediate)[in] | |
1da177e4 LT |
556 | * / \ |
557 | * (new leaf node)[ln] (old node)[fn] | |
558 | */ | |
559 | if (plen > bit) { | |
560 | in = node_alloc(); | |
561 | ln = node_alloc(); | |
1ab1457c | 562 | |
507c9b1e | 563 | if (!in || !ln) { |
1da177e4 LT |
564 | if (in) |
565 | node_free(in); | |
566 | if (ln) | |
567 | node_free(ln); | |
188c517a | 568 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
569 | } |
570 | ||
1ab1457c YH |
571 | /* |
572 | * new intermediate node. | |
1da177e4 LT |
573 | * RTN_RTINFO will |
574 | * be off since that an address that chooses one of | |
575 | * the branches would not match less specific routes | |
576 | * in the other branch | |
577 | */ | |
578 | ||
579 | in->fn_bit = bit; | |
580 | ||
581 | in->parent = pn; | |
582 | in->leaf = fn->leaf; | |
583 | atomic_inc(&in->leaf->rt6i_ref); | |
584 | ||
585 | in->fn_sernum = sernum; | |
586 | ||
587 | /* update parent pointer */ | |
588 | if (dir) | |
589 | pn->right = in; | |
590 | else | |
591 | pn->left = in; | |
592 | ||
593 | ln->fn_bit = plen; | |
594 | ||
595 | ln->parent = in; | |
596 | fn->parent = in; | |
597 | ||
598 | ln->fn_sernum = sernum; | |
599 | ||
600 | if (addr_bit_set(addr, bit)) { | |
601 | in->right = ln; | |
602 | in->left = fn; | |
603 | } else { | |
604 | in->left = ln; | |
605 | in->right = fn; | |
606 | } | |
607 | } else { /* plen <= bit */ | |
608 | ||
1ab1457c | 609 | /* |
1da177e4 LT |
610 | * (new leaf node)[ln] |
611 | * / \ | |
612 | * (old node)[fn] NULL | |
613 | */ | |
614 | ||
615 | ln = node_alloc(); | |
616 | ||
507c9b1e | 617 | if (!ln) |
188c517a | 618 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
619 | |
620 | ln->fn_bit = plen; | |
621 | ||
622 | ln->parent = pn; | |
623 | ||
624 | ln->fn_sernum = sernum; | |
1ab1457c | 625 | |
1da177e4 LT |
626 | if (dir) |
627 | pn->right = ln; | |
628 | else | |
629 | pn->left = ln; | |
630 | ||
631 | if (addr_bit_set(&key->addr, plen)) | |
632 | ln->right = fn; | |
633 | else | |
634 | ln->left = fn; | |
635 | ||
636 | fn->parent = ln; | |
637 | } | |
638 | return ln; | |
639 | } | |
640 | ||
d6245ef7 HFS |
641 | static inline bool rt6_qualify_for_ecmp(struct rt6_info *rt) |
642 | { | |
643 | return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) == | |
644 | RTF_GATEWAY; | |
645 | } | |
646 | ||
650a7901 HFS |
647 | static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn, |
648 | struct net *net) | |
649 | { | |
650 | if (atomic_read(&rt->rt6i_ref) != 1) { | |
651 | /* This route is used as dummy address holder in some split | |
652 | * nodes. It is not leaked, but it still holds other resources, | |
653 | * which must be released in time. So, scan ascendant nodes | |
654 | * and replace dummy references to this route with references | |
655 | * to still alive ones. | |
656 | */ | |
657 | while (fn) { | |
658 | if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) { | |
659 | fn->leaf = fib6_find_prefix(net, fn); | |
660 | atomic_inc(&fn->leaf->rt6i_ref); | |
661 | rt6_release(rt); | |
662 | } | |
663 | fn = fn->parent; | |
664 | } | |
665 | /* No more references are possible at this point. */ | |
666 | BUG_ON(atomic_read(&rt->rt6i_ref) != 1); | |
667 | } | |
668 | } | |
669 | ||
1da177e4 LT |
670 | /* |
671 | * Insert routing information in a node. | |
672 | */ | |
673 | ||
674 | static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt, | |
86872cb5 | 675 | struct nl_info *info) |
1da177e4 LT |
676 | { |
677 | struct rt6_info *iter = NULL; | |
678 | struct rt6_info **ins; | |
507c9b1e DM |
679 | int replace = (info->nlh && |
680 | (info->nlh->nlmsg_flags & NLM_F_REPLACE)); | |
681 | int add = (!info->nlh || | |
682 | (info->nlh->nlmsg_flags & NLM_F_CREATE)); | |
4a287eba | 683 | int found = 0; |
d6245ef7 | 684 | bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); |
1da177e4 LT |
685 | |
686 | ins = &fn->leaf; | |
687 | ||
507c9b1e | 688 | for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) { |
1da177e4 LT |
689 | /* |
690 | * Search for duplicates | |
691 | */ | |
692 | ||
693 | if (iter->rt6i_metric == rt->rt6i_metric) { | |
694 | /* | |
695 | * Same priority level | |
696 | */ | |
507c9b1e DM |
697 | if (info->nlh && |
698 | (info->nlh->nlmsg_flags & NLM_F_EXCL)) | |
4a287eba MV |
699 | return -EEXIST; |
700 | if (replace) { | |
701 | found++; | |
702 | break; | |
703 | } | |
1da177e4 | 704 | |
d1918542 | 705 | if (iter->dst.dev == rt->dst.dev && |
1da177e4 LT |
706 | iter->rt6i_idev == rt->rt6i_idev && |
707 | ipv6_addr_equal(&iter->rt6i_gateway, | |
708 | &rt->rt6i_gateway)) { | |
51ebd318 ND |
709 | if (rt->rt6i_nsiblings) |
710 | rt->rt6i_nsiblings = 0; | |
507c9b1e | 711 | if (!(iter->rt6i_flags & RTF_EXPIRES)) |
1da177e4 | 712 | return -EEXIST; |
1716a961 G |
713 | if (!(rt->rt6i_flags & RTF_EXPIRES)) |
714 | rt6_clean_expires(iter); | |
715 | else | |
716 | rt6_set_expires(iter, rt->dst.expires); | |
1da177e4 LT |
717 | return -EEXIST; |
718 | } | |
51ebd318 ND |
719 | /* If we have the same destination and the same metric, |
720 | * but not the same gateway, then the route we try to | |
721 | * add is sibling to this route, increment our counter | |
722 | * of siblings, and later we will add our route to the | |
723 | * list. | |
724 | * Only static routes (which don't have flag | |
725 | * RTF_EXPIRES) are used for ECMPv6. | |
726 | * | |
727 | * To avoid long list, we only had siblings if the | |
728 | * route have a gateway. | |
729 | */ | |
d6245ef7 HFS |
730 | if (rt_can_ecmp && |
731 | rt6_qualify_for_ecmp(iter)) | |
51ebd318 | 732 | rt->rt6i_nsiblings++; |
1da177e4 LT |
733 | } |
734 | ||
735 | if (iter->rt6i_metric > rt->rt6i_metric) | |
736 | break; | |
737 | ||
d8d1f30b | 738 | ins = &iter->dst.rt6_next; |
1da177e4 LT |
739 | } |
740 | ||
f11e6659 DM |
741 | /* Reset round-robin state, if necessary */ |
742 | if (ins == &fn->leaf) | |
743 | fn->rr_ptr = NULL; | |
744 | ||
51ebd318 ND |
745 | /* Link this route to others same route. */ |
746 | if (rt->rt6i_nsiblings) { | |
747 | unsigned int rt6i_nsiblings; | |
748 | struct rt6_info *sibling, *temp_sibling; | |
749 | ||
750 | /* Find the first route that have the same metric */ | |
751 | sibling = fn->leaf; | |
752 | while (sibling) { | |
d6245ef7 HFS |
753 | if (sibling->rt6i_metric == rt->rt6i_metric && |
754 | rt6_qualify_for_ecmp(sibling)) { | |
51ebd318 ND |
755 | list_add_tail(&rt->rt6i_siblings, |
756 | &sibling->rt6i_siblings); | |
757 | break; | |
758 | } | |
759 | sibling = sibling->dst.rt6_next; | |
760 | } | |
761 | /* For each sibling in the list, increment the counter of | |
762 | * siblings. BUG() if counters does not match, list of siblings | |
763 | * is broken! | |
764 | */ | |
765 | rt6i_nsiblings = 0; | |
766 | list_for_each_entry_safe(sibling, temp_sibling, | |
767 | &rt->rt6i_siblings, rt6i_siblings) { | |
768 | sibling->rt6i_nsiblings++; | |
769 | BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings); | |
770 | rt6i_nsiblings++; | |
771 | } | |
772 | BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings); | |
773 | } | |
774 | ||
1da177e4 LT |
775 | /* |
776 | * insert node | |
777 | */ | |
4a287eba MV |
778 | if (!replace) { |
779 | if (!add) | |
f3213831 | 780 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
4a287eba MV |
781 | |
782 | add: | |
783 | rt->dst.rt6_next = iter; | |
784 | *ins = rt; | |
785 | rt->rt6i_node = fn; | |
786 | atomic_inc(&rt->rt6i_ref); | |
787 | inet6_rt_notify(RTM_NEWROUTE, rt, info); | |
788 | info->nl_net->ipv6.rt6_stats->fib_rt_entries++; | |
789 | ||
507c9b1e | 790 | if (!(fn->fn_flags & RTN_RTINFO)) { |
4a287eba MV |
791 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
792 | fn->fn_flags |= RTN_RTINFO; | |
793 | } | |
1da177e4 | 794 | |
4a287eba MV |
795 | } else { |
796 | if (!found) { | |
797 | if (add) | |
798 | goto add; | |
f3213831 | 799 | pr_warn("NLM_F_REPLACE set, but no existing node found!\n"); |
4a287eba MV |
800 | return -ENOENT; |
801 | } | |
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); | |
507c9b1e | 807 | if (!(fn->fn_flags & RTN_RTINFO)) { |
4a287eba MV |
808 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
809 | fn->fn_flags |= RTN_RTINFO; | |
810 | } | |
650a7901 HFS |
811 | fib6_purge_rt(iter, fn, info->nl_net); |
812 | rt6_release(iter); | |
1da177e4 LT |
813 | } |
814 | ||
815 | return 0; | |
816 | } | |
817 | ||
63152fc0 | 818 | static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt) |
1da177e4 | 819 | { |
417f28bb | 820 | if (!timer_pending(&net->ipv6.ip6_fib_timer) && |
507c9b1e | 821 | (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE))) |
417f28bb | 822 | mod_timer(&net->ipv6.ip6_fib_timer, |
847499ce | 823 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
1da177e4 LT |
824 | } |
825 | ||
63152fc0 | 826 | void fib6_force_start_gc(struct net *net) |
1da177e4 | 827 | { |
417f28bb SH |
828 | if (!timer_pending(&net->ipv6.ip6_fib_timer)) |
829 | mod_timer(&net->ipv6.ip6_fib_timer, | |
847499ce | 830 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
1da177e4 LT |
831 | } |
832 | ||
833 | /* | |
834 | * Add routing information to the routing tree. | |
835 | * <destination addr>/<source addr> | |
836 | * with source addr info in sub-trees | |
837 | */ | |
838 | ||
86872cb5 | 839 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info) |
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 LT |
854 | |
855 | fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr), | |
507c9b1e DM |
856 | rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst), |
857 | allow_create, replace_required); | |
4a287eba MV |
858 | if (IS_ERR(fn)) { |
859 | err = PTR_ERR(fn); | |
45d268c6 | 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, | |
896 | sizeof(struct in6_addr), 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, | |
915 | sizeof(struct in6_addr), 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 | ||
86872cb5 | 933 | err = fib6_add_rt2node(fn, rt, info); |
507c9b1e | 934 | if (!err) { |
63152fc0 | 935 | fib6_start_gc(info->nl_net, rt); |
507c9b1e | 936 | if (!(rt->rt6i_flags & RTF_CACHE)) |
ec7d43c2 | 937 | fib6_prune_clones(info->nl_net, pn, rt); |
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 | ||
988 | static struct fib6_node * fib6_lookup_1(struct fib6_node *root, | |
989 | struct lookup_args *args) | |
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 | |
24e8ac72 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 |
24e8ac72 | 1035 | if (fn->fn_flags & RTN_RTINFO) |
3fc5e044 YH |
1036 | return fn; |
1037 | } | |
1da177e4 | 1038 | } |
24e8ac72 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 | ||
b71d1d42 ED |
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 | ||
1084 | static struct fib6_node * fib6_locate_1(struct fib6_node *root, | |
b71d1d42 | 1085 | const struct in6_addr *addr, |
1da177e4 LT |
1086 | int plen, int offset) |
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 | ||
1114 | struct fib6_node * fib6_locate(struct fib6_node *root, | |
b71d1d42 ED |
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; | |
1171 | struct fib6_walker_t *w; | |
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; | |
1184 | if (fn->right) child = fn->right, children |= 1; | |
1185 | if (fn->left) child = fn->left, children |= 2; | |
1186 | ||
7fc33165 | 1187 | if (children == 3 || FIB6_SUBTREE(fn) |
1da177e4 LT |
1188 | #ifdef CONFIG_IPV6_SUBTREES |
1189 | /* Subtree root (i.e. fn) may have one child */ | |
507c9b1e | 1190 | || (children && fn->fn_flags & RTN_ROOT) |
1da177e4 LT |
1191 | #endif |
1192 | ) { | |
8ed67789 | 1193 | fn->leaf = fib6_find_prefix(net, fn); |
1da177e4 | 1194 | #if RT6_DEBUG >= 2 |
507c9b1e | 1195 | if (!fn->leaf) { |
547b792c | 1196 | WARN_ON(!fn->leaf); |
8ed67789 | 1197 | fn->leaf = net->ipv6.ip6_null_entry; |
1da177e4 LT |
1198 | } |
1199 | #endif | |
1200 | atomic_inc(&fn->leaf->rt6i_ref); | |
1201 | return fn->parent; | |
1202 | } | |
1203 | ||
1204 | pn = fn->parent; | |
1205 | #ifdef CONFIG_IPV6_SUBTREES | |
7fc33165 | 1206 | if (FIB6_SUBTREE(pn) == fn) { |
547b792c | 1207 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
7fc33165 | 1208 | FIB6_SUBTREE(pn) = NULL; |
1da177e4 LT |
1209 | nstate = FWS_L; |
1210 | } else { | |
547b792c | 1211 | WARN_ON(fn->fn_flags & RTN_ROOT); |
1da177e4 LT |
1212 | #endif |
1213 | if (pn->right == fn) pn->right = child; | |
1214 | else if (pn->left == fn) pn->left = child; | |
1215 | #if RT6_DEBUG >= 2 | |
547b792c IJ |
1216 | else |
1217 | WARN_ON(1); | |
1da177e4 LT |
1218 | #endif |
1219 | if (child) | |
1220 | child->parent = pn; | |
1221 | nstate = FWS_R; | |
1222 | #ifdef CONFIG_IPV6_SUBTREES | |
1223 | } | |
1224 | #endif | |
1225 | ||
1226 | read_lock(&fib6_walker_lock); | |
1227 | FOR_WALKERS(w) { | |
507c9b1e | 1228 | if (!child) { |
1da177e4 LT |
1229 | if (w->root == fn) { |
1230 | w->root = w->node = NULL; | |
1231 | RT6_TRACE("W %p adjusted by delroot 1\n", w); | |
1232 | } else if (w->node == fn) { | |
1233 | RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); | |
1234 | w->node = pn; | |
1235 | w->state = nstate; | |
1236 | } | |
1237 | } else { | |
1238 | if (w->root == fn) { | |
1239 | w->root = child; | |
1240 | RT6_TRACE("W %p adjusted by delroot 2\n", w); | |
1241 | } | |
1242 | if (w->node == fn) { | |
1243 | w->node = child; | |
1244 | if (children&2) { | |
1245 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); | |
1246 | w->state = w->state>=FWS_R ? FWS_U : FWS_INIT; | |
1247 | } else { | |
1248 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); | |
1249 | w->state = w->state>=FWS_C ? FWS_U : FWS_INIT; | |
1250 | } | |
1251 | } | |
1252 | } | |
1253 | } | |
1254 | read_unlock(&fib6_walker_lock); | |
1255 | ||
1256 | node_free(fn); | |
507c9b1e | 1257 | if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn)) |
1da177e4 LT |
1258 | return pn; |
1259 | ||
1260 | rt6_release(pn->leaf); | |
1261 | pn->leaf = NULL; | |
1262 | fn = pn; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp, | |
86872cb5 | 1267 | struct nl_info *info) |
1da177e4 LT |
1268 | { |
1269 | struct fib6_walker_t *w; | |
1270 | struct rt6_info *rt = *rtp; | |
c572872f | 1271 | struct net *net = info->nl_net; |
1da177e4 LT |
1272 | |
1273 | RT6_TRACE("fib6_del_route\n"); | |
1274 | ||
1275 | /* Unlink it */ | |
d8d1f30b | 1276 | *rtp = rt->dst.rt6_next; |
1da177e4 | 1277 | rt->rt6i_node = NULL; |
c572872f BT |
1278 | net->ipv6.rt6_stats->fib_rt_entries--; |
1279 | net->ipv6.rt6_stats->fib_discarded_routes++; | |
1da177e4 | 1280 | |
f11e6659 DM |
1281 | /* Reset round-robin state, if necessary */ |
1282 | if (fn->rr_ptr == rt) | |
1283 | fn->rr_ptr = NULL; | |
1284 | ||
51ebd318 ND |
1285 | /* Remove this entry from other siblings */ |
1286 | if (rt->rt6i_nsiblings) { | |
1287 | struct rt6_info *sibling, *next_sibling; | |
1288 | ||
1289 | list_for_each_entry_safe(sibling, next_sibling, | |
1290 | &rt->rt6i_siblings, rt6i_siblings) | |
1291 | sibling->rt6i_nsiblings--; | |
1292 | rt->rt6i_nsiblings = 0; | |
1293 | list_del_init(&rt->rt6i_siblings); | |
1294 | } | |
1295 | ||
1da177e4 LT |
1296 | /* Adjust walkers */ |
1297 | read_lock(&fib6_walker_lock); | |
1298 | FOR_WALKERS(w) { | |
1299 | if (w->state == FWS_C && w->leaf == rt) { | |
1300 | RT6_TRACE("walker %p adjusted by delroute\n", w); | |
d8d1f30b | 1301 | w->leaf = rt->dst.rt6_next; |
507c9b1e | 1302 | if (!w->leaf) |
1da177e4 LT |
1303 | w->state = FWS_U; |
1304 | } | |
1305 | } | |
1306 | read_unlock(&fib6_walker_lock); | |
1307 | ||
d8d1f30b | 1308 | rt->dst.rt6_next = NULL; |
1da177e4 | 1309 | |
1da177e4 | 1310 | /* If it was last route, expunge its radix tree node */ |
507c9b1e | 1311 | if (!fn->leaf) { |
1da177e4 | 1312 | fn->fn_flags &= ~RTN_RTINFO; |
c572872f | 1313 | net->ipv6.rt6_stats->fib_route_nodes--; |
8ed67789 | 1314 | fn = fib6_repair_tree(net, fn); |
1da177e4 LT |
1315 | } |
1316 | ||
650a7901 | 1317 | fib6_purge_rt(rt, fn, net); |
1da177e4 | 1318 | |
86872cb5 | 1319 | inet6_rt_notify(RTM_DELROUTE, rt, info); |
1da177e4 LT |
1320 | rt6_release(rt); |
1321 | } | |
1322 | ||
86872cb5 | 1323 | int fib6_del(struct rt6_info *rt, struct nl_info *info) |
1da177e4 | 1324 | { |
8ed67789 | 1325 | struct net *net = info->nl_net; |
1da177e4 LT |
1326 | struct fib6_node *fn = rt->rt6i_node; |
1327 | struct rt6_info **rtp; | |
1328 | ||
1329 | #if RT6_DEBUG >= 2 | |
d8d1f30b | 1330 | if (rt->dst.obsolete>0) { |
547b792c | 1331 | WARN_ON(fn != NULL); |
1da177e4 LT |
1332 | return -ENOENT; |
1333 | } | |
1334 | #endif | |
507c9b1e | 1335 | if (!fn || rt == net->ipv6.ip6_null_entry) |
1da177e4 LT |
1336 | return -ENOENT; |
1337 | ||
547b792c | 1338 | WARN_ON(!(fn->fn_flags & RTN_RTINFO)); |
1da177e4 | 1339 | |
507c9b1e | 1340 | if (!(rt->rt6i_flags & RTF_CACHE)) { |
150730d5 YH |
1341 | struct fib6_node *pn = fn; |
1342 | #ifdef CONFIG_IPV6_SUBTREES | |
1343 | /* clones of this route might be in another subtree */ | |
1344 | if (rt->rt6i_src.plen) { | |
507c9b1e | 1345 | while (!(pn->fn_flags & RTN_ROOT)) |
150730d5 YH |
1346 | pn = pn->parent; |
1347 | pn = pn->parent; | |
1348 | } | |
1349 | #endif | |
ec7d43c2 | 1350 | fib6_prune_clones(info->nl_net, pn, rt); |
150730d5 | 1351 | } |
1da177e4 LT |
1352 | |
1353 | /* | |
1354 | * Walk the leaf entries looking for ourself | |
1355 | */ | |
1356 | ||
d8d1f30b | 1357 | for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) { |
1da177e4 | 1358 | if (*rtp == rt) { |
86872cb5 | 1359 | fib6_del_route(fn, rtp, info); |
1da177e4 LT |
1360 | return 0; |
1361 | } | |
1362 | } | |
1363 | return -ENOENT; | |
1364 | } | |
1365 | ||
1366 | /* | |
1367 | * Tree traversal function. | |
1368 | * | |
1369 | * Certainly, it is not interrupt safe. | |
1370 | * However, it is internally reenterable wrt itself and fib6_add/fib6_del. | |
1371 | * It means, that we can modify tree during walking | |
1372 | * and use this function for garbage collection, clone pruning, | |
1ab1457c | 1373 | * cleaning tree when a device goes down etc. etc. |
1da177e4 LT |
1374 | * |
1375 | * It guarantees that every node will be traversed, | |
1376 | * and that it will be traversed only once. | |
1377 | * | |
1378 | * Callback function w->func may return: | |
1379 | * 0 -> continue walking. | |
1380 | * positive value -> walking is suspended (used by tree dumps, | |
1381 | * and probably by gc, if it will be split to several slices) | |
1382 | * negative value -> terminate walking. | |
1383 | * | |
1384 | * The function itself returns: | |
1385 | * 0 -> walk is complete. | |
1386 | * >0 -> walk is incomplete (i.e. suspended) | |
1387 | * <0 -> walk is terminated by an error. | |
1388 | */ | |
1389 | ||
90d41122 | 1390 | static int fib6_walk_continue(struct fib6_walker_t *w) |
1da177e4 LT |
1391 | { |
1392 | struct fib6_node *fn, *pn; | |
1393 | ||
1394 | for (;;) { | |
1395 | fn = w->node; | |
507c9b1e | 1396 | if (!fn) |
1da177e4 LT |
1397 | return 0; |
1398 | ||
1399 | if (w->prune && fn != w->root && | |
507c9b1e | 1400 | fn->fn_flags & RTN_RTINFO && w->state < FWS_C) { |
1da177e4 LT |
1401 | w->state = FWS_C; |
1402 | w->leaf = fn->leaf; | |
1403 | } | |
1404 | switch (w->state) { | |
1405 | #ifdef CONFIG_IPV6_SUBTREES | |
1406 | case FWS_S: | |
7fc33165 YH |
1407 | if (FIB6_SUBTREE(fn)) { |
1408 | w->node = FIB6_SUBTREE(fn); | |
1da177e4 LT |
1409 | continue; |
1410 | } | |
1411 | w->state = FWS_L; | |
1ab1457c | 1412 | #endif |
1da177e4 LT |
1413 | case FWS_L: |
1414 | if (fn->left) { | |
1415 | w->node = fn->left; | |
1416 | w->state = FWS_INIT; | |
1417 | continue; | |
1418 | } | |
1419 | w->state = FWS_R; | |
1420 | case FWS_R: | |
1421 | if (fn->right) { | |
1422 | w->node = fn->right; | |
1423 | w->state = FWS_INIT; | |
1424 | continue; | |
1425 | } | |
1426 | w->state = FWS_C; | |
1427 | w->leaf = fn->leaf; | |
1428 | case FWS_C: | |
507c9b1e | 1429 | if (w->leaf && fn->fn_flags & RTN_RTINFO) { |
2bec5a36 PM |
1430 | int err; |
1431 | ||
fa809e2f ED |
1432 | if (w->skip) { |
1433 | w->skip--; | |
83445f0d | 1434 | goto skip; |
2bec5a36 PM |
1435 | } |
1436 | ||
1437 | err = w->func(w); | |
1da177e4 LT |
1438 | if (err) |
1439 | return err; | |
2bec5a36 PM |
1440 | |
1441 | w->count++; | |
1da177e4 LT |
1442 | continue; |
1443 | } | |
83445f0d | 1444 | skip: |
1da177e4 LT |
1445 | w->state = FWS_U; |
1446 | case FWS_U: | |
1447 | if (fn == w->root) | |
1448 | return 0; | |
1449 | pn = fn->parent; | |
1450 | w->node = pn; | |
1451 | #ifdef CONFIG_IPV6_SUBTREES | |
7fc33165 | 1452 | if (FIB6_SUBTREE(pn) == fn) { |
547b792c | 1453 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
1da177e4 LT |
1454 | w->state = FWS_L; |
1455 | continue; | |
1456 | } | |
1457 | #endif | |
1458 | if (pn->left == fn) { | |
1459 | w->state = FWS_R; | |
1460 | continue; | |
1461 | } | |
1462 | if (pn->right == fn) { | |
1463 | w->state = FWS_C; | |
1464 | w->leaf = w->node->leaf; | |
1465 | continue; | |
1466 | } | |
1467 | #if RT6_DEBUG >= 2 | |
547b792c | 1468 | WARN_ON(1); |
1da177e4 LT |
1469 | #endif |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | ||
90d41122 | 1474 | static int fib6_walk(struct fib6_walker_t *w) |
1da177e4 LT |
1475 | { |
1476 | int res; | |
1477 | ||
1478 | w->state = FWS_INIT; | |
1479 | w->node = w->root; | |
1480 | ||
1481 | fib6_walker_link(w); | |
1482 | res = fib6_walk_continue(w); | |
1483 | if (res <= 0) | |
1484 | fib6_walker_unlink(w); | |
1485 | return res; | |
1486 | } | |
1487 | ||
1488 | static int fib6_clean_node(struct fib6_walker_t *w) | |
1489 | { | |
1490 | int res; | |
1491 | struct rt6_info *rt; | |
0a8891a0 | 1492 | struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w); |
ec7d43c2 BT |
1493 | struct nl_info info = { |
1494 | .nl_net = c->net, | |
1495 | }; | |
1da177e4 | 1496 | |
d8d1f30b | 1497 | for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { |
1da177e4 LT |
1498 | res = c->func(rt, c->arg); |
1499 | if (res < 0) { | |
1500 | w->leaf = rt; | |
528c4ceb | 1501 | res = fib6_del(rt, &info); |
1da177e4 LT |
1502 | if (res) { |
1503 | #if RT6_DEBUG >= 2 | |
91df42be JP |
1504 | pr_debug("%s: del failed: rt=%p@%p err=%d\n", |
1505 | __func__, rt, rt->rt6i_node, res); | |
1da177e4 LT |
1506 | #endif |
1507 | continue; | |
1508 | } | |
1509 | return 0; | |
1510 | } | |
547b792c | 1511 | WARN_ON(res != 0); |
1da177e4 LT |
1512 | } |
1513 | w->leaf = rt; | |
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | /* | |
1518 | * Convenient frontend to tree walker. | |
1ab1457c | 1519 | * |
1da177e4 LT |
1520 | * func is called on each route. |
1521 | * It may return -1 -> delete this route. | |
1522 | * 0 -> continue walking | |
1523 | * | |
1524 | * prune==1 -> only immediate children of node (certainly, | |
1525 | * ignoring pure split nodes) will be scanned. | |
1526 | */ | |
1527 | ||
ec7d43c2 | 1528 | static void fib6_clean_tree(struct net *net, struct fib6_node *root, |
8ce11e6a AB |
1529 | int (*func)(struct rt6_info *, void *arg), |
1530 | int prune, void *arg) | |
1da177e4 LT |
1531 | { |
1532 | struct fib6_cleaner_t c; | |
1533 | ||
1534 | c.w.root = root; | |
1535 | c.w.func = fib6_clean_node; | |
1536 | c.w.prune = prune; | |
2bec5a36 PM |
1537 | c.w.count = 0; |
1538 | c.w.skip = 0; | |
1da177e4 LT |
1539 | c.func = func; |
1540 | c.arg = arg; | |
ec7d43c2 | 1541 | c.net = net; |
1da177e4 LT |
1542 | |
1543 | fib6_walk(&c.w); | |
1544 | } | |
1545 | ||
32b293a5 JH |
1546 | void fib6_clean_all_ro(struct net *net, int (*func)(struct rt6_info *, void *arg), |
1547 | int prune, void *arg) | |
1548 | { | |
1549 | struct fib6_table *table; | |
32b293a5 JH |
1550 | struct hlist_head *head; |
1551 | unsigned int h; | |
1552 | ||
1553 | rcu_read_lock(); | |
1554 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { | |
1555 | head = &net->ipv6.fib_table_hash[h]; | |
b67bfe0d | 1556 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
32b293a5 JH |
1557 | read_lock_bh(&table->tb6_lock); |
1558 | fib6_clean_tree(net, &table->tb6_root, | |
1559 | func, prune, arg); | |
1560 | read_unlock_bh(&table->tb6_lock); | |
1561 | } | |
1562 | } | |
1563 | rcu_read_unlock(); | |
1564 | } | |
f3db4851 | 1565 | void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), |
c71099ac TG |
1566 | int prune, void *arg) |
1567 | { | |
c71099ac | 1568 | struct fib6_table *table; |
58f09b78 | 1569 | struct hlist_head *head; |
1b43af54 | 1570 | unsigned int h; |
c71099ac | 1571 | |
1b43af54 | 1572 | rcu_read_lock(); |
a33bc5c1 | 1573 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
f3db4851 | 1574 | head = &net->ipv6.fib_table_hash[h]; |
b67bfe0d | 1575 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
c71099ac | 1576 | write_lock_bh(&table->tb6_lock); |
ec7d43c2 BT |
1577 | fib6_clean_tree(net, &table->tb6_root, |
1578 | func, prune, arg); | |
c71099ac TG |
1579 | write_unlock_bh(&table->tb6_lock); |
1580 | } | |
1581 | } | |
1b43af54 | 1582 | rcu_read_unlock(); |
c71099ac TG |
1583 | } |
1584 | ||
1da177e4 LT |
1585 | static int fib6_prune_clone(struct rt6_info *rt, void *arg) |
1586 | { | |
1587 | if (rt->rt6i_flags & RTF_CACHE) { | |
1588 | RT6_TRACE("pruning clone %p\n", rt); | |
1589 | return -1; | |
1590 | } | |
1591 | ||
1592 | return 0; | |
1593 | } | |
1594 | ||
ec7d43c2 BT |
1595 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn, |
1596 | struct rt6_info *rt) | |
1da177e4 | 1597 | { |
ec7d43c2 | 1598 | fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt); |
1da177e4 LT |
1599 | } |
1600 | ||
1601 | /* | |
1602 | * Garbage collection | |
1603 | */ | |
1604 | ||
1605 | static struct fib6_gc_args | |
1606 | { | |
1607 | int timeout; | |
1608 | int more; | |
1609 | } gc_args; | |
1610 | ||
1611 | static int fib6_age(struct rt6_info *rt, void *arg) | |
1612 | { | |
1613 | unsigned long now = jiffies; | |
1614 | ||
1615 | /* | |
1616 | * check addrconf expiration here. | |
1617 | * Routes are expired even if they are in use. | |
1618 | * | |
1619 | * Also age clones. Note, that clones are aged out | |
1620 | * only if they are not in use now. | |
1621 | */ | |
1622 | ||
d1918542 DM |
1623 | if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) { |
1624 | if (time_after(now, rt->dst.expires)) { | |
1da177e4 | 1625 | RT6_TRACE("expiring %p\n", rt); |
1da177e4 LT |
1626 | return -1; |
1627 | } | |
1628 | gc_args.more++; | |
1629 | } else if (rt->rt6i_flags & RTF_CACHE) { | |
d8d1f30b CG |
1630 | if (atomic_read(&rt->dst.__refcnt) == 0 && |
1631 | time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) { | |
1da177e4 LT |
1632 | RT6_TRACE("aging clone %p\n", rt); |
1633 | return -1; | |
5339ab8b DM |
1634 | } else if (rt->rt6i_flags & RTF_GATEWAY) { |
1635 | struct neighbour *neigh; | |
1636 | __u8 neigh_flags = 0; | |
1637 | ||
1638 | neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); | |
1639 | if (neigh) { | |
1640 | neigh_flags = neigh->flags; | |
1641 | neigh_release(neigh); | |
1642 | } | |
8bd74516 | 1643 | if (!(neigh_flags & NTF_ROUTER)) { |
5339ab8b DM |
1644 | RT6_TRACE("purging route %p via non-router but gateway\n", |
1645 | rt); | |
1646 | return -1; | |
1647 | } | |
1da177e4 LT |
1648 | } |
1649 | gc_args.more++; | |
1650 | } | |
1651 | ||
1652 | return 0; | |
1653 | } | |
1654 | ||
1655 | static DEFINE_SPINLOCK(fib6_gc_lock); | |
1656 | ||
ba310bc8 | 1657 | void fib6_run_gc(unsigned long expires, struct net *net, bool force) |
1da177e4 | 1658 | { |
c1e1eb15 MK |
1659 | unsigned long now; |
1660 | ||
ba310bc8 | 1661 | if (force) { |
1da177e4 | 1662 | spin_lock_bh(&fib6_gc_lock); |
ba310bc8 MK |
1663 | } else if (!spin_trylock_bh(&fib6_gc_lock)) { |
1664 | mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ); | |
1665 | return; | |
1da177e4 | 1666 | } |
ba310bc8 MK |
1667 | gc_args.timeout = expires ? (int)expires : |
1668 | net->ipv6.sysctl.ip6_rt_gc_interval; | |
1da177e4 | 1669 | |
3d0f24a7 | 1670 | gc_args.more = icmp6_dst_gc(); |
f3db4851 | 1671 | |
5b7c931d | 1672 | fib6_clean_all(net, fib6_age, 0, NULL); |
c1e1eb15 MK |
1673 | now = jiffies; |
1674 | net->ipv6.ip6_rt_last_gc = now; | |
1da177e4 LT |
1675 | |
1676 | if (gc_args.more) | |
c8a45222 | 1677 | mod_timer(&net->ipv6.ip6_fib_timer, |
c1e1eb15 | 1678 | round_jiffies(now |
c8a45222 | 1679 | + net->ipv6.sysctl.ip6_rt_gc_interval)); |
417f28bb SH |
1680 | else |
1681 | del_timer(&net->ipv6.ip6_fib_timer); | |
1da177e4 LT |
1682 | spin_unlock_bh(&fib6_gc_lock); |
1683 | } | |
1684 | ||
5b7c931d DL |
1685 | static void fib6_gc_timer_cb(unsigned long arg) |
1686 | { | |
ba310bc8 | 1687 | fib6_run_gc(0, (struct net *)arg, true); |
5b7c931d DL |
1688 | } |
1689 | ||
2c8c1e72 | 1690 | static int __net_init fib6_net_init(struct net *net) |
1da177e4 | 1691 | { |
10da66f7 ED |
1692 | size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ; |
1693 | ||
417f28bb | 1694 | setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net); |
63152fc0 | 1695 | |
c572872f BT |
1696 | net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL); |
1697 | if (!net->ipv6.rt6_stats) | |
1698 | goto out_timer; | |
1699 | ||
10da66f7 ED |
1700 | /* Avoid false sharing : Use at least a full cache line */ |
1701 | size = max_t(size_t, size, L1_CACHE_BYTES); | |
1702 | ||
1703 | net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL); | |
58f09b78 | 1704 | if (!net->ipv6.fib_table_hash) |
c572872f | 1705 | goto out_rt6_stats; |
e0b85590 | 1706 | |
58f09b78 DL |
1707 | net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl), |
1708 | GFP_KERNEL); | |
1709 | if (!net->ipv6.fib6_main_tbl) | |
e0b85590 DL |
1710 | goto out_fib_table_hash; |
1711 | ||
58f09b78 | 1712 | net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; |
8ed67789 | 1713 | net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; |
58f09b78 DL |
1714 | net->ipv6.fib6_main_tbl->tb6_root.fn_flags = |
1715 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; | |
8e773277 | 1716 | inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers); |
e0b85590 DL |
1717 | |
1718 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
58f09b78 DL |
1719 | net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl), |
1720 | GFP_KERNEL); | |
1721 | if (!net->ipv6.fib6_local_tbl) | |
e0b85590 | 1722 | goto out_fib6_main_tbl; |
58f09b78 | 1723 | net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; |
8ed67789 | 1724 | net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; |
58f09b78 DL |
1725 | net->ipv6.fib6_local_tbl->tb6_root.fn_flags = |
1726 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; | |
8e773277 | 1727 | inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers); |
e0b85590 | 1728 | #endif |
58f09b78 | 1729 | fib6_tables_init(net); |
f845ab6b | 1730 | |
417f28bb | 1731 | return 0; |
d63bddbe | 1732 | |
e0b85590 | 1733 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
e0b85590 | 1734 | out_fib6_main_tbl: |
58f09b78 | 1735 | kfree(net->ipv6.fib6_main_tbl); |
e0b85590 | 1736 | #endif |
e0b85590 | 1737 | out_fib_table_hash: |
58f09b78 | 1738 | kfree(net->ipv6.fib_table_hash); |
c572872f BT |
1739 | out_rt6_stats: |
1740 | kfree(net->ipv6.rt6_stats); | |
63152fc0 | 1741 | out_timer: |
417f28bb | 1742 | return -ENOMEM; |
58f09b78 DL |
1743 | } |
1744 | ||
1745 | static void fib6_net_exit(struct net *net) | |
1746 | { | |
4e39d5e4 SD |
1747 | unsigned int i; |
1748 | ||
8ed67789 | 1749 | rt6_ifdown(net, NULL); |
417f28bb SH |
1750 | del_timer_sync(&net->ipv6.ip6_fib_timer); |
1751 | ||
488ec7b2 | 1752 | for (i = 0; i < FIB6_TABLE_HASHSZ; i++) { |
4e39d5e4 SD |
1753 | struct hlist_head *head = &net->ipv6.fib_table_hash[i]; |
1754 | struct hlist_node *tmp; | |
1755 | struct fib6_table *tb; | |
1756 | ||
1757 | hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) { | |
1758 | hlist_del(&tb->tb6_hlist); | |
1759 | fib6_free_table(tb); | |
1760 | } | |
1761 | } | |
1762 | ||
58f09b78 | 1763 | kfree(net->ipv6.fib_table_hash); |
c572872f | 1764 | kfree(net->ipv6.rt6_stats); |
58f09b78 DL |
1765 | } |
1766 | ||
1767 | static struct pernet_operations fib6_net_ops = { | |
1768 | .init = fib6_net_init, | |
1769 | .exit = fib6_net_exit, | |
1770 | }; | |
1771 | ||
1772 | int __init fib6_init(void) | |
1773 | { | |
1774 | int ret = -ENOMEM; | |
63152fc0 | 1775 | |
58f09b78 DL |
1776 | fib6_node_kmem = kmem_cache_create("fib6_nodes", |
1777 | sizeof(struct fib6_node), | |
1778 | 0, SLAB_HWCACHE_ALIGN, | |
1779 | NULL); | |
1780 | if (!fib6_node_kmem) | |
1781 | goto out; | |
1782 | ||
1783 | ret = register_pernet_subsys(&fib6_net_ops); | |
1784 | if (ret) | |
c572872f | 1785 | goto out_kmem_cache_create; |
e8803b6c DM |
1786 | |
1787 | ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib, | |
1788 | NULL); | |
1789 | if (ret) | |
1790 | goto out_unregister_subsys; | |
58f09b78 DL |
1791 | out: |
1792 | return ret; | |
1793 | ||
e8803b6c DM |
1794 | out_unregister_subsys: |
1795 | unregister_pernet_subsys(&fib6_net_ops); | |
d63bddbe DL |
1796 | out_kmem_cache_create: |
1797 | kmem_cache_destroy(fib6_node_kmem); | |
1798 | goto out; | |
1da177e4 LT |
1799 | } |
1800 | ||
1801 | void fib6_gc_cleanup(void) | |
1802 | { | |
58f09b78 | 1803 | unregister_pernet_subsys(&fib6_net_ops); |
1da177e4 LT |
1804 | kmem_cache_destroy(fib6_node_kmem); |
1805 | } |