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 | */ |
1da177e4 LT |
21 | #include <linux/errno.h> |
22 | #include <linux/types.h> | |
23 | #include <linux/net.h> | |
24 | #include <linux/route.h> | |
25 | #include <linux/netdevice.h> | |
26 | #include <linux/in6.h> | |
27 | #include <linux/init.h> | |
c71099ac | 28 | #include <linux/list.h> |
1da177e4 LT |
29 | |
30 | #ifdef CONFIG_PROC_FS | |
31 | #include <linux/proc_fs.h> | |
32 | #endif | |
33 | ||
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 | |
44 | #define RT6_TRACE(x...) printk(KERN_DEBUG x) | |
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 | */ | |
130 | ||
e69a4adc | 131 | static __inline__ __be32 addr_bit_set(void *token, int fn_bit) |
1da177e4 | 132 | { |
e69a4adc | 133 | __be32 *addr = token; |
1da177e4 LT |
134 | |
135 | return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5]; | |
136 | } | |
137 | ||
1da177e4 LT |
138 | static __inline__ struct fib6_node * node_alloc(void) |
139 | { | |
140 | struct fib6_node *fn; | |
141 | ||
c3762229 | 142 | fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); |
1da177e4 LT |
143 | |
144 | return fn; | |
145 | } | |
146 | ||
147 | static __inline__ void node_free(struct fib6_node * fn) | |
148 | { | |
149 | kmem_cache_free(fib6_node_kmem, fn); | |
150 | } | |
151 | ||
152 | static __inline__ void rt6_release(struct rt6_info *rt) | |
153 | { | |
154 | if (atomic_dec_and_test(&rt->rt6i_ref)) | |
155 | dst_free(&rt->u.dst); | |
156 | } | |
157 | ||
58f09b78 | 158 | static void fib6_link_table(struct net *net, struct fib6_table *tb) |
1b43af54 PM |
159 | { |
160 | unsigned int h; | |
161 | ||
375216ad TG |
162 | /* |
163 | * Initialize table lock at a single place to give lockdep a key, | |
164 | * tables aren't visible prior to being linked to the list. | |
165 | */ | |
166 | rwlock_init(&tb->tb6_lock); | |
167 | ||
a33bc5c1 | 168 | h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); |
1b43af54 PM |
169 | |
170 | /* | |
171 | * No protection necessary, this is the only list mutatation | |
172 | * operation, tables never disappear once they exist. | |
173 | */ | |
58f09b78 | 174 | hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]); |
1b43af54 | 175 | } |
c71099ac | 176 | |
1b43af54 | 177 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
e0b85590 | 178 | |
8ed67789 | 179 | static struct fib6_table *fib6_alloc_table(struct net *net, u32 id) |
c71099ac TG |
180 | { |
181 | struct fib6_table *table; | |
182 | ||
183 | table = kzalloc(sizeof(*table), GFP_ATOMIC); | |
184 | if (table != NULL) { | |
185 | table->tb6_id = id; | |
8ed67789 | 186 | table->tb6_root.leaf = net->ipv6.ip6_null_entry; |
c71099ac TG |
187 | table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
188 | } | |
189 | ||
190 | return table; | |
191 | } | |
192 | ||
58f09b78 | 193 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
c71099ac TG |
194 | { |
195 | struct fib6_table *tb; | |
196 | ||
197 | if (id == 0) | |
198 | id = RT6_TABLE_MAIN; | |
58f09b78 | 199 | tb = fib6_get_table(net, id); |
c71099ac TG |
200 | if (tb) |
201 | return tb; | |
202 | ||
8ed67789 | 203 | tb = fib6_alloc_table(net, id); |
c71099ac | 204 | if (tb != NULL) |
58f09b78 | 205 | fib6_link_table(net, tb); |
c71099ac TG |
206 | |
207 | return tb; | |
208 | } | |
209 | ||
58f09b78 | 210 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
c71099ac TG |
211 | { |
212 | struct fib6_table *tb; | |
58f09b78 | 213 | struct hlist_head *head; |
c71099ac TG |
214 | struct hlist_node *node; |
215 | unsigned int h; | |
216 | ||
217 | if (id == 0) | |
218 | id = RT6_TABLE_MAIN; | |
a33bc5c1 | 219 | h = id & (FIB6_TABLE_HASHSZ - 1); |
c71099ac | 220 | rcu_read_lock(); |
58f09b78 DL |
221 | head = &net->ipv6.fib_table_hash[h]; |
222 | hlist_for_each_entry_rcu(tb, node, head, tb6_hlist) { | |
c71099ac TG |
223 | if (tb->tb6_id == id) { |
224 | rcu_read_unlock(); | |
225 | return tb; | |
226 | } | |
227 | } | |
228 | rcu_read_unlock(); | |
229 | ||
230 | return NULL; | |
231 | } | |
232 | ||
2c8c1e72 | 233 | static void __net_init fib6_tables_init(struct net *net) |
c71099ac | 234 | { |
58f09b78 DL |
235 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
236 | fib6_link_table(net, net->ipv6.fib6_local_tbl); | |
c71099ac | 237 | } |
c71099ac TG |
238 | #else |
239 | ||
58f09b78 | 240 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
c71099ac | 241 | { |
58f09b78 | 242 | return fib6_get_table(net, id); |
c71099ac TG |
243 | } |
244 | ||
58f09b78 | 245 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
c71099ac | 246 | { |
58f09b78 | 247 | return net->ipv6.fib6_main_tbl; |
c71099ac TG |
248 | } |
249 | ||
58f09b78 DL |
250 | struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl, |
251 | int flags, pol_lookup_t lookup) | |
c71099ac | 252 | { |
8ed67789 | 253 | return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl, flags); |
c71099ac TG |
254 | } |
255 | ||
2c8c1e72 | 256 | static void __net_init fib6_tables_init(struct net *net) |
c71099ac | 257 | { |
58f09b78 | 258 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
c71099ac TG |
259 | } |
260 | ||
261 | #endif | |
262 | ||
1b43af54 PM |
263 | static int fib6_dump_node(struct fib6_walker_t *w) |
264 | { | |
265 | int res; | |
266 | struct rt6_info *rt; | |
267 | ||
7cc48263 | 268 | for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) { |
1b43af54 PM |
269 | res = rt6_dump_route(rt, w->args); |
270 | if (res < 0) { | |
271 | /* Frame is full, suspend walking */ | |
272 | w->leaf = rt; | |
273 | return 1; | |
274 | } | |
547b792c | 275 | WARN_ON(res == 0); |
1b43af54 PM |
276 | } |
277 | w->leaf = NULL; | |
278 | return 0; | |
279 | } | |
280 | ||
281 | static void fib6_dump_end(struct netlink_callback *cb) | |
282 | { | |
283 | struct fib6_walker_t *w = (void*)cb->args[2]; | |
284 | ||
285 | if (w) { | |
7891cc81 HX |
286 | if (cb->args[4]) { |
287 | cb->args[4] = 0; | |
288 | fib6_walker_unlink(w); | |
289 | } | |
1b43af54 PM |
290 | cb->args[2] = 0; |
291 | kfree(w); | |
292 | } | |
293 | cb->done = (void*)cb->args[3]; | |
294 | cb->args[1] = 3; | |
295 | } | |
296 | ||
297 | static int fib6_dump_done(struct netlink_callback *cb) | |
298 | { | |
299 | fib6_dump_end(cb); | |
300 | return cb->done ? cb->done(cb) : 0; | |
301 | } | |
302 | ||
303 | static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, | |
304 | struct netlink_callback *cb) | |
305 | { | |
306 | struct fib6_walker_t *w; | |
307 | int res; | |
308 | ||
309 | w = (void *)cb->args[2]; | |
310 | w->root = &table->tb6_root; | |
311 | ||
312 | if (cb->args[4] == 0) { | |
2bec5a36 PM |
313 | w->count = 0; |
314 | w->skip = 0; | |
315 | ||
1b43af54 PM |
316 | read_lock_bh(&table->tb6_lock); |
317 | res = fib6_walk(w); | |
318 | read_unlock_bh(&table->tb6_lock); | |
2bec5a36 | 319 | if (res > 0) { |
1b43af54 | 320 | cb->args[4] = 1; |
2bec5a36 PM |
321 | cb->args[5] = w->root->fn_sernum; |
322 | } | |
1b43af54 | 323 | } else { |
2bec5a36 PM |
324 | if (cb->args[5] != w->root->fn_sernum) { |
325 | /* Begin at the root if the tree changed */ | |
326 | cb->args[5] = w->root->fn_sernum; | |
327 | w->state = FWS_INIT; | |
328 | w->node = w->root; | |
329 | w->skip = w->count; | |
330 | } else | |
331 | w->skip = 0; | |
332 | ||
1b43af54 PM |
333 | read_lock_bh(&table->tb6_lock); |
334 | res = fib6_walk_continue(w); | |
335 | read_unlock_bh(&table->tb6_lock); | |
7891cc81 HX |
336 | if (res <= 0) { |
337 | fib6_walker_unlink(w); | |
338 | cb->args[4] = 0; | |
1b43af54 | 339 | } |
1b43af54 | 340 | } |
7891cc81 | 341 | |
1b43af54 PM |
342 | return res; |
343 | } | |
344 | ||
c127ea2c | 345 | static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) |
1b43af54 | 346 | { |
3b1e0a65 | 347 | struct net *net = sock_net(skb->sk); |
1b43af54 PM |
348 | unsigned int h, s_h; |
349 | unsigned int e = 0, s_e; | |
350 | struct rt6_rtnl_dump_arg arg; | |
351 | struct fib6_walker_t *w; | |
352 | struct fib6_table *tb; | |
353 | struct hlist_node *node; | |
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]; | |
361 | if (w == NULL) { | |
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); | |
373 | if (w == NULL) | |
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 | ||
a33bc5c1 | 384 | for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) { |
1b43af54 | 385 | e = 0; |
58f09b78 DL |
386 | head = &net->ipv6.fib_table_hash[h]; |
387 | hlist_for_each_entry(tb, node, head, tb6_hlist) { | |
1b43af54 PM |
388 | if (e < s_e) |
389 | goto next; | |
390 | res = fib6_dump_table(tb, skb, cb); | |
391 | if (res != 0) | |
392 | goto out; | |
393 | next: | |
394 | e++; | |
395 | } | |
396 | } | |
397 | out: | |
398 | cb->args[1] = e; | |
399 | cb->args[0] = h; | |
400 | ||
401 | res = res < 0 ? res : skb->len; | |
402 | if (res <= 0) | |
403 | fib6_dump_end(cb); | |
404 | return res; | |
405 | } | |
1da177e4 LT |
406 | |
407 | /* | |
408 | * Routing Table | |
409 | * | |
410 | * return the appropriate node for a routing tree "add" operation | |
411 | * by either creating and inserting or by returning an existing | |
412 | * node. | |
413 | */ | |
414 | ||
415 | static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr, | |
416 | int addrlen, int plen, | |
417 | int offset) | |
418 | { | |
419 | struct fib6_node *fn, *in, *ln; | |
420 | struct fib6_node *pn = NULL; | |
421 | struct rt6key *key; | |
422 | int bit; | |
1ab1457c | 423 | __be32 dir = 0; |
1da177e4 LT |
424 | __u32 sernum = fib6_new_sernum(); |
425 | ||
426 | RT6_TRACE("fib6_add_1\n"); | |
427 | ||
428 | /* insert node in tree */ | |
429 | ||
430 | fn = root; | |
431 | ||
432 | do { | |
433 | key = (struct rt6key *)((u8 *)fn->leaf + offset); | |
434 | ||
435 | /* | |
436 | * Prefix match | |
437 | */ | |
438 | if (plen < fn->fn_bit || | |
439 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) | |
440 | goto insert_above; | |
1ab1457c | 441 | |
1da177e4 LT |
442 | /* |
443 | * Exact match ? | |
444 | */ | |
1ab1457c | 445 | |
1da177e4 LT |
446 | if (plen == fn->fn_bit) { |
447 | /* clean up an intermediate node */ | |
448 | if ((fn->fn_flags & RTN_RTINFO) == 0) { | |
449 | rt6_release(fn->leaf); | |
450 | fn->leaf = NULL; | |
451 | } | |
1ab1457c | 452 | |
1da177e4 | 453 | fn->fn_sernum = sernum; |
1ab1457c | 454 | |
1da177e4 LT |
455 | return fn; |
456 | } | |
457 | ||
458 | /* | |
459 | * We have more bits to go | |
460 | */ | |
1ab1457c | 461 | |
1da177e4 LT |
462 | /* Try to walk down on tree. */ |
463 | fn->fn_sernum = sernum; | |
464 | dir = addr_bit_set(addr, fn->fn_bit); | |
465 | pn = fn; | |
466 | fn = dir ? fn->right: fn->left; | |
467 | } while (fn); | |
468 | ||
469 | /* | |
470 | * We walked to the bottom of tree. | |
471 | * Create new leaf node without children. | |
472 | */ | |
473 | ||
474 | ln = node_alloc(); | |
475 | ||
476 | if (ln == NULL) | |
477 | return NULL; | |
478 | ln->fn_bit = plen; | |
1ab1457c | 479 | |
1da177e4 LT |
480 | ln->parent = pn; |
481 | ln->fn_sernum = sernum; | |
482 | ||
483 | if (dir) | |
484 | pn->right = ln; | |
485 | else | |
486 | pn->left = ln; | |
487 | ||
488 | return ln; | |
489 | ||
490 | ||
491 | insert_above: | |
492 | /* | |
1ab1457c | 493 | * split since we don't have a common prefix anymore or |
1da177e4 LT |
494 | * we have a less significant route. |
495 | * we've to insert an intermediate node on the list | |
496 | * this new node will point to the one we need to create | |
497 | * and the current | |
498 | */ | |
499 | ||
500 | pn = fn->parent; | |
501 | ||
502 | /* find 1st bit in difference between the 2 addrs. | |
503 | ||
971f359d | 504 | See comment in __ipv6_addr_diff: bit may be an invalid value, |
1da177e4 LT |
505 | but if it is >= plen, the value is ignored in any case. |
506 | */ | |
1ab1457c | 507 | |
971f359d | 508 | bit = __ipv6_addr_diff(addr, &key->addr, addrlen); |
1da177e4 | 509 | |
1ab1457c YH |
510 | /* |
511 | * (intermediate)[in] | |
1da177e4 LT |
512 | * / \ |
513 | * (new leaf node)[ln] (old node)[fn] | |
514 | */ | |
515 | if (plen > bit) { | |
516 | in = node_alloc(); | |
517 | ln = node_alloc(); | |
1ab1457c | 518 | |
1da177e4 LT |
519 | if (in == NULL || ln == NULL) { |
520 | if (in) | |
521 | node_free(in); | |
522 | if (ln) | |
523 | node_free(ln); | |
524 | return NULL; | |
525 | } | |
526 | ||
1ab1457c YH |
527 | /* |
528 | * new intermediate node. | |
1da177e4 LT |
529 | * RTN_RTINFO will |
530 | * be off since that an address that chooses one of | |
531 | * the branches would not match less specific routes | |
532 | * in the other branch | |
533 | */ | |
534 | ||
535 | in->fn_bit = bit; | |
536 | ||
537 | in->parent = pn; | |
538 | in->leaf = fn->leaf; | |
539 | atomic_inc(&in->leaf->rt6i_ref); | |
540 | ||
541 | in->fn_sernum = sernum; | |
542 | ||
543 | /* update parent pointer */ | |
544 | if (dir) | |
545 | pn->right = in; | |
546 | else | |
547 | pn->left = in; | |
548 | ||
549 | ln->fn_bit = plen; | |
550 | ||
551 | ln->parent = in; | |
552 | fn->parent = in; | |
553 | ||
554 | ln->fn_sernum = sernum; | |
555 | ||
556 | if (addr_bit_set(addr, bit)) { | |
557 | in->right = ln; | |
558 | in->left = fn; | |
559 | } else { | |
560 | in->left = ln; | |
561 | in->right = fn; | |
562 | } | |
563 | } else { /* plen <= bit */ | |
564 | ||
1ab1457c | 565 | /* |
1da177e4 LT |
566 | * (new leaf node)[ln] |
567 | * / \ | |
568 | * (old node)[fn] NULL | |
569 | */ | |
570 | ||
571 | ln = node_alloc(); | |
572 | ||
573 | if (ln == NULL) | |
574 | return NULL; | |
575 | ||
576 | ln->fn_bit = plen; | |
577 | ||
578 | ln->parent = pn; | |
579 | ||
580 | ln->fn_sernum = sernum; | |
1ab1457c | 581 | |
1da177e4 LT |
582 | if (dir) |
583 | pn->right = ln; | |
584 | else | |
585 | pn->left = ln; | |
586 | ||
587 | if (addr_bit_set(&key->addr, plen)) | |
588 | ln->right = fn; | |
589 | else | |
590 | ln->left = fn; | |
591 | ||
592 | fn->parent = ln; | |
593 | } | |
594 | return ln; | |
595 | } | |
596 | ||
597 | /* | |
598 | * Insert routing information in a node. | |
599 | */ | |
600 | ||
601 | static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt, | |
86872cb5 | 602 | struct nl_info *info) |
1da177e4 LT |
603 | { |
604 | struct rt6_info *iter = NULL; | |
605 | struct rt6_info **ins; | |
606 | ||
607 | ins = &fn->leaf; | |
608 | ||
7cc48263 | 609 | for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) { |
1da177e4 LT |
610 | /* |
611 | * Search for duplicates | |
612 | */ | |
613 | ||
614 | if (iter->rt6i_metric == rt->rt6i_metric) { | |
615 | /* | |
616 | * Same priority level | |
617 | */ | |
618 | ||
619 | if (iter->rt6i_dev == rt->rt6i_dev && | |
620 | iter->rt6i_idev == rt->rt6i_idev && | |
621 | ipv6_addr_equal(&iter->rt6i_gateway, | |
622 | &rt->rt6i_gateway)) { | |
623 | if (!(iter->rt6i_flags&RTF_EXPIRES)) | |
624 | return -EEXIST; | |
625 | iter->rt6i_expires = rt->rt6i_expires; | |
626 | if (!(rt->rt6i_flags&RTF_EXPIRES)) { | |
627 | iter->rt6i_flags &= ~RTF_EXPIRES; | |
628 | iter->rt6i_expires = 0; | |
629 | } | |
630 | return -EEXIST; | |
631 | } | |
632 | } | |
633 | ||
634 | if (iter->rt6i_metric > rt->rt6i_metric) | |
635 | break; | |
636 | ||
7cc48263 | 637 | ins = &iter->u.dst.rt6_next; |
1da177e4 LT |
638 | } |
639 | ||
f11e6659 DM |
640 | /* Reset round-robin state, if necessary */ |
641 | if (ins == &fn->leaf) | |
642 | fn->rr_ptr = NULL; | |
643 | ||
1da177e4 LT |
644 | /* |
645 | * insert node | |
646 | */ | |
647 | ||
7cc48263 | 648 | rt->u.dst.rt6_next = iter; |
1da177e4 LT |
649 | *ins = rt; |
650 | rt->rt6i_node = fn; | |
651 | atomic_inc(&rt->rt6i_ref); | |
86872cb5 | 652 | inet6_rt_notify(RTM_NEWROUTE, rt, info); |
c572872f | 653 | info->nl_net->ipv6.rt6_stats->fib_rt_entries++; |
1da177e4 LT |
654 | |
655 | if ((fn->fn_flags & RTN_RTINFO) == 0) { | |
c572872f | 656 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
1da177e4 LT |
657 | fn->fn_flags |= RTN_RTINFO; |
658 | } | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
63152fc0 | 663 | static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt) |
1da177e4 | 664 | { |
417f28bb | 665 | if (!timer_pending(&net->ipv6.ip6_fib_timer) && |
1da177e4 | 666 | (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE))) |
417f28bb | 667 | mod_timer(&net->ipv6.ip6_fib_timer, |
847499ce | 668 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
1da177e4 LT |
669 | } |
670 | ||
63152fc0 | 671 | void fib6_force_start_gc(struct net *net) |
1da177e4 | 672 | { |
417f28bb SH |
673 | if (!timer_pending(&net->ipv6.ip6_fib_timer)) |
674 | mod_timer(&net->ipv6.ip6_fib_timer, | |
847499ce | 675 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
1da177e4 LT |
676 | } |
677 | ||
678 | /* | |
679 | * Add routing information to the routing tree. | |
680 | * <destination addr>/<source addr> | |
681 | * with source addr info in sub-trees | |
682 | */ | |
683 | ||
86872cb5 | 684 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info) |
1da177e4 | 685 | { |
66729e18 | 686 | struct fib6_node *fn, *pn = NULL; |
1da177e4 LT |
687 | int err = -ENOMEM; |
688 | ||
689 | fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr), | |
690 | rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst)); | |
691 | ||
692 | if (fn == NULL) | |
693 | goto out; | |
694 | ||
66729e18 YH |
695 | pn = fn; |
696 | ||
1da177e4 LT |
697 | #ifdef CONFIG_IPV6_SUBTREES |
698 | if (rt->rt6i_src.plen) { | |
699 | struct fib6_node *sn; | |
700 | ||
701 | if (fn->subtree == NULL) { | |
702 | struct fib6_node *sfn; | |
703 | ||
704 | /* | |
705 | * Create subtree. | |
706 | * | |
707 | * fn[main tree] | |
708 | * | | |
709 | * sfn[subtree root] | |
710 | * \ | |
711 | * sn[new leaf node] | |
712 | */ | |
713 | ||
714 | /* Create subtree root node */ | |
715 | sfn = node_alloc(); | |
716 | if (sfn == NULL) | |
717 | goto st_failure; | |
718 | ||
8ed67789 DL |
719 | sfn->leaf = info->nl_net->ipv6.ip6_null_entry; |
720 | atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref); | |
1da177e4 LT |
721 | sfn->fn_flags = RTN_ROOT; |
722 | sfn->fn_sernum = fib6_new_sernum(); | |
723 | ||
724 | /* Now add the first leaf node to new subtree */ | |
725 | ||
726 | sn = fib6_add_1(sfn, &rt->rt6i_src.addr, | |
727 | sizeof(struct in6_addr), rt->rt6i_src.plen, | |
728 | offsetof(struct rt6_info, rt6i_src)); | |
729 | ||
730 | if (sn == NULL) { | |
731 | /* If it is failed, discard just allocated | |
732 | root, and then (in st_failure) stale node | |
733 | in main tree. | |
734 | */ | |
735 | node_free(sfn); | |
736 | goto st_failure; | |
737 | } | |
738 | ||
739 | /* Now link new subtree to main tree */ | |
740 | sfn->parent = fn; | |
741 | fn->subtree = sfn; | |
1da177e4 LT |
742 | } else { |
743 | sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr, | |
744 | sizeof(struct in6_addr), rt->rt6i_src.plen, | |
745 | offsetof(struct rt6_info, rt6i_src)); | |
746 | ||
747 | if (sn == NULL) | |
748 | goto st_failure; | |
749 | } | |
750 | ||
66729e18 YH |
751 | if (fn->leaf == NULL) { |
752 | fn->leaf = rt; | |
753 | atomic_inc(&rt->rt6i_ref); | |
754 | } | |
1da177e4 LT |
755 | fn = sn; |
756 | } | |
757 | #endif | |
758 | ||
86872cb5 | 759 | err = fib6_add_rt2node(fn, rt, info); |
1da177e4 LT |
760 | |
761 | if (err == 0) { | |
63152fc0 | 762 | fib6_start_gc(info->nl_net, rt); |
1da177e4 | 763 | if (!(rt->rt6i_flags&RTF_CACHE)) |
ec7d43c2 | 764 | fib6_prune_clones(info->nl_net, pn, rt); |
1da177e4 LT |
765 | } |
766 | ||
767 | out: | |
66729e18 YH |
768 | if (err) { |
769 | #ifdef CONFIG_IPV6_SUBTREES | |
770 | /* | |
771 | * If fib6_add_1 has cleared the old leaf pointer in the | |
772 | * super-tree leaf node we have to find a new one for it. | |
773 | */ | |
3c051235 DM |
774 | if (pn != fn && pn->leaf == rt) { |
775 | pn->leaf = NULL; | |
776 | atomic_dec(&rt->rt6i_ref); | |
777 | } | |
66729e18 | 778 | if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) { |
8ed67789 | 779 | pn->leaf = fib6_find_prefix(info->nl_net, pn); |
66729e18 YH |
780 | #if RT6_DEBUG >= 2 |
781 | if (!pn->leaf) { | |
547b792c | 782 | WARN_ON(pn->leaf == NULL); |
8ed67789 | 783 | pn->leaf = info->nl_net->ipv6.ip6_null_entry; |
66729e18 YH |
784 | } |
785 | #endif | |
786 | atomic_inc(&pn->leaf->rt6i_ref); | |
787 | } | |
788 | #endif | |
1da177e4 | 789 | dst_free(&rt->u.dst); |
66729e18 | 790 | } |
1da177e4 LT |
791 | return err; |
792 | ||
793 | #ifdef CONFIG_IPV6_SUBTREES | |
794 | /* Subtree creation failed, probably main tree node | |
795 | is orphan. If it is, shoot it. | |
796 | */ | |
797 | st_failure: | |
798 | if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT))) | |
8ed67789 | 799 | fib6_repair_tree(info->nl_net, fn); |
1da177e4 LT |
800 | dst_free(&rt->u.dst); |
801 | return err; | |
802 | #endif | |
803 | } | |
804 | ||
805 | /* | |
806 | * Routing tree lookup | |
807 | * | |
808 | */ | |
809 | ||
810 | struct lookup_args { | |
811 | int offset; /* key offset on rt6_info */ | |
812 | struct in6_addr *addr; /* search key */ | |
813 | }; | |
814 | ||
815 | static struct fib6_node * fib6_lookup_1(struct fib6_node *root, | |
816 | struct lookup_args *args) | |
817 | { | |
818 | struct fib6_node *fn; | |
e69a4adc | 819 | __be32 dir; |
1da177e4 | 820 | |
825e288e YH |
821 | if (unlikely(args->offset == 0)) |
822 | return NULL; | |
823 | ||
1da177e4 LT |
824 | /* |
825 | * Descend on a tree | |
826 | */ | |
827 | ||
828 | fn = root; | |
829 | ||
830 | for (;;) { | |
831 | struct fib6_node *next; | |
832 | ||
833 | dir = addr_bit_set(args->addr, fn->fn_bit); | |
834 | ||
835 | next = dir ? fn->right : fn->left; | |
836 | ||
837 | if (next) { | |
838 | fn = next; | |
839 | continue; | |
840 | } | |
841 | ||
842 | break; | |
843 | } | |
844 | ||
3fc5e044 | 845 | while(fn) { |
7fc33165 | 846 | if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) { |
1da177e4 LT |
847 | struct rt6key *key; |
848 | ||
849 | key = (struct rt6key *) ((u8 *) fn->leaf + | |
850 | args->offset); | |
851 | ||
3fc5e044 YH |
852 | if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) { |
853 | #ifdef CONFIG_IPV6_SUBTREES | |
854 | if (fn->subtree) | |
855 | fn = fib6_lookup_1(fn->subtree, args + 1); | |
856 | #endif | |
857 | if (!fn || fn->fn_flags & RTN_RTINFO) | |
858 | return fn; | |
859 | } | |
1da177e4 LT |
860 | } |
861 | ||
3fc5e044 YH |
862 | if (fn->fn_flags & RTN_ROOT) |
863 | break; | |
864 | ||
1da177e4 LT |
865 | fn = fn->parent; |
866 | } | |
867 | ||
868 | return NULL; | |
869 | } | |
870 | ||
871 | struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr, | |
872 | struct in6_addr *saddr) | |
873 | { | |
1da177e4 | 874 | struct fib6_node *fn; |
825e288e YH |
875 | struct lookup_args args[] = { |
876 | { | |
877 | .offset = offsetof(struct rt6_info, rt6i_dst), | |
878 | .addr = daddr, | |
879 | }, | |
1da177e4 | 880 | #ifdef CONFIG_IPV6_SUBTREES |
825e288e YH |
881 | { |
882 | .offset = offsetof(struct rt6_info, rt6i_src), | |
883 | .addr = saddr, | |
884 | }, | |
1da177e4 | 885 | #endif |
825e288e YH |
886 | { |
887 | .offset = 0, /* sentinel */ | |
888 | } | |
889 | }; | |
1da177e4 | 890 | |
fefc2a6c | 891 | fn = fib6_lookup_1(root, daddr ? args : args + 1); |
1da177e4 LT |
892 | |
893 | if (fn == NULL || fn->fn_flags & RTN_TL_ROOT) | |
894 | fn = root; | |
895 | ||
896 | return fn; | |
897 | } | |
898 | ||
899 | /* | |
900 | * Get node with specified destination prefix (and source prefix, | |
901 | * if subtrees are used) | |
902 | */ | |
903 | ||
904 | ||
905 | static struct fib6_node * fib6_locate_1(struct fib6_node *root, | |
906 | struct in6_addr *addr, | |
907 | int plen, int offset) | |
908 | { | |
909 | struct fib6_node *fn; | |
910 | ||
911 | for (fn = root; fn ; ) { | |
912 | struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset); | |
913 | ||
914 | /* | |
915 | * Prefix match | |
916 | */ | |
917 | if (plen < fn->fn_bit || | |
918 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) | |
919 | return NULL; | |
920 | ||
921 | if (plen == fn->fn_bit) | |
922 | return fn; | |
923 | ||
924 | /* | |
925 | * We have more bits to go | |
926 | */ | |
927 | if (addr_bit_set(addr, fn->fn_bit)) | |
928 | fn = fn->right; | |
929 | else | |
930 | fn = fn->left; | |
931 | } | |
932 | return NULL; | |
933 | } | |
934 | ||
935 | struct fib6_node * fib6_locate(struct fib6_node *root, | |
936 | struct in6_addr *daddr, int dst_len, | |
937 | struct in6_addr *saddr, int src_len) | |
938 | { | |
939 | struct fib6_node *fn; | |
940 | ||
941 | fn = fib6_locate_1(root, daddr, dst_len, | |
942 | offsetof(struct rt6_info, rt6i_dst)); | |
943 | ||
944 | #ifdef CONFIG_IPV6_SUBTREES | |
945 | if (src_len) { | |
547b792c | 946 | WARN_ON(saddr == NULL); |
3fc5e044 YH |
947 | if (fn && fn->subtree) |
948 | fn = fib6_locate_1(fn->subtree, saddr, src_len, | |
1da177e4 LT |
949 | offsetof(struct rt6_info, rt6i_src)); |
950 | } | |
951 | #endif | |
952 | ||
953 | if (fn && fn->fn_flags&RTN_RTINFO) | |
954 | return fn; | |
955 | ||
956 | return NULL; | |
957 | } | |
958 | ||
959 | ||
960 | /* | |
961 | * Deletion | |
962 | * | |
963 | */ | |
964 | ||
8ed67789 | 965 | static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn) |
1da177e4 LT |
966 | { |
967 | if (fn->fn_flags&RTN_ROOT) | |
8ed67789 | 968 | return net->ipv6.ip6_null_entry; |
1da177e4 LT |
969 | |
970 | while(fn) { | |
971 | if(fn->left) | |
972 | return fn->left->leaf; | |
973 | ||
974 | if(fn->right) | |
975 | return fn->right->leaf; | |
976 | ||
7fc33165 | 977 | fn = FIB6_SUBTREE(fn); |
1da177e4 LT |
978 | } |
979 | return NULL; | |
980 | } | |
981 | ||
982 | /* | |
983 | * Called to trim the tree of intermediate nodes when possible. "fn" | |
984 | * is the node we want to try and remove. | |
985 | */ | |
986 | ||
8ed67789 DL |
987 | static struct fib6_node *fib6_repair_tree(struct net *net, |
988 | struct fib6_node *fn) | |
1da177e4 LT |
989 | { |
990 | int children; | |
991 | int nstate; | |
992 | struct fib6_node *child, *pn; | |
993 | struct fib6_walker_t *w; | |
994 | int iter = 0; | |
995 | ||
996 | for (;;) { | |
997 | RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter); | |
998 | iter++; | |
999 | ||
547b792c IJ |
1000 | WARN_ON(fn->fn_flags & RTN_RTINFO); |
1001 | WARN_ON(fn->fn_flags & RTN_TL_ROOT); | |
1002 | WARN_ON(fn->leaf != NULL); | |
1da177e4 LT |
1003 | |
1004 | children = 0; | |
1005 | child = NULL; | |
1006 | if (fn->right) child = fn->right, children |= 1; | |
1007 | if (fn->left) child = fn->left, children |= 2; | |
1008 | ||
7fc33165 | 1009 | if (children == 3 || FIB6_SUBTREE(fn) |
1da177e4 LT |
1010 | #ifdef CONFIG_IPV6_SUBTREES |
1011 | /* Subtree root (i.e. fn) may have one child */ | |
1012 | || (children && fn->fn_flags&RTN_ROOT) | |
1013 | #endif | |
1014 | ) { | |
8ed67789 | 1015 | fn->leaf = fib6_find_prefix(net, fn); |
1da177e4 LT |
1016 | #if RT6_DEBUG >= 2 |
1017 | if (fn->leaf==NULL) { | |
547b792c | 1018 | WARN_ON(!fn->leaf); |
8ed67789 | 1019 | fn->leaf = net->ipv6.ip6_null_entry; |
1da177e4 LT |
1020 | } |
1021 | #endif | |
1022 | atomic_inc(&fn->leaf->rt6i_ref); | |
1023 | return fn->parent; | |
1024 | } | |
1025 | ||
1026 | pn = fn->parent; | |
1027 | #ifdef CONFIG_IPV6_SUBTREES | |
7fc33165 | 1028 | if (FIB6_SUBTREE(pn) == fn) { |
547b792c | 1029 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
7fc33165 | 1030 | FIB6_SUBTREE(pn) = NULL; |
1da177e4 LT |
1031 | nstate = FWS_L; |
1032 | } else { | |
547b792c | 1033 | WARN_ON(fn->fn_flags & RTN_ROOT); |
1da177e4 LT |
1034 | #endif |
1035 | if (pn->right == fn) pn->right = child; | |
1036 | else if (pn->left == fn) pn->left = child; | |
1037 | #if RT6_DEBUG >= 2 | |
547b792c IJ |
1038 | else |
1039 | WARN_ON(1); | |
1da177e4 LT |
1040 | #endif |
1041 | if (child) | |
1042 | child->parent = pn; | |
1043 | nstate = FWS_R; | |
1044 | #ifdef CONFIG_IPV6_SUBTREES | |
1045 | } | |
1046 | #endif | |
1047 | ||
1048 | read_lock(&fib6_walker_lock); | |
1049 | FOR_WALKERS(w) { | |
1050 | if (child == NULL) { | |
1051 | if (w->root == fn) { | |
1052 | w->root = w->node = NULL; | |
1053 | RT6_TRACE("W %p adjusted by delroot 1\n", w); | |
1054 | } else if (w->node == fn) { | |
1055 | RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); | |
1056 | w->node = pn; | |
1057 | w->state = nstate; | |
1058 | } | |
1059 | } else { | |
1060 | if (w->root == fn) { | |
1061 | w->root = child; | |
1062 | RT6_TRACE("W %p adjusted by delroot 2\n", w); | |
1063 | } | |
1064 | if (w->node == fn) { | |
1065 | w->node = child; | |
1066 | if (children&2) { | |
1067 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); | |
1068 | w->state = w->state>=FWS_R ? FWS_U : FWS_INIT; | |
1069 | } else { | |
1070 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); | |
1071 | w->state = w->state>=FWS_C ? FWS_U : FWS_INIT; | |
1072 | } | |
1073 | } | |
1074 | } | |
1075 | } | |
1076 | read_unlock(&fib6_walker_lock); | |
1077 | ||
1078 | node_free(fn); | |
7fc33165 | 1079 | if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn)) |
1da177e4 LT |
1080 | return pn; |
1081 | ||
1082 | rt6_release(pn->leaf); | |
1083 | pn->leaf = NULL; | |
1084 | fn = pn; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp, | |
86872cb5 | 1089 | struct nl_info *info) |
1da177e4 LT |
1090 | { |
1091 | struct fib6_walker_t *w; | |
1092 | struct rt6_info *rt = *rtp; | |
c572872f | 1093 | struct net *net = info->nl_net; |
1da177e4 LT |
1094 | |
1095 | RT6_TRACE("fib6_del_route\n"); | |
1096 | ||
1097 | /* Unlink it */ | |
7cc48263 | 1098 | *rtp = rt->u.dst.rt6_next; |
1da177e4 | 1099 | rt->rt6i_node = NULL; |
c572872f BT |
1100 | net->ipv6.rt6_stats->fib_rt_entries--; |
1101 | net->ipv6.rt6_stats->fib_discarded_routes++; | |
1da177e4 | 1102 | |
f11e6659 DM |
1103 | /* Reset round-robin state, if necessary */ |
1104 | if (fn->rr_ptr == rt) | |
1105 | fn->rr_ptr = NULL; | |
1106 | ||
1da177e4 LT |
1107 | /* Adjust walkers */ |
1108 | read_lock(&fib6_walker_lock); | |
1109 | FOR_WALKERS(w) { | |
1110 | if (w->state == FWS_C && w->leaf == rt) { | |
1111 | RT6_TRACE("walker %p adjusted by delroute\n", w); | |
7cc48263 | 1112 | w->leaf = rt->u.dst.rt6_next; |
1da177e4 LT |
1113 | if (w->leaf == NULL) |
1114 | w->state = FWS_U; | |
1115 | } | |
1116 | } | |
1117 | read_unlock(&fib6_walker_lock); | |
1118 | ||
7cc48263 | 1119 | rt->u.dst.rt6_next = NULL; |
1da177e4 | 1120 | |
1da177e4 LT |
1121 | /* If it was last route, expunge its radix tree node */ |
1122 | if (fn->leaf == NULL) { | |
1123 | fn->fn_flags &= ~RTN_RTINFO; | |
c572872f | 1124 | net->ipv6.rt6_stats->fib_route_nodes--; |
8ed67789 | 1125 | fn = fib6_repair_tree(net, fn); |
1da177e4 LT |
1126 | } |
1127 | ||
1128 | if (atomic_read(&rt->rt6i_ref) != 1) { | |
1129 | /* This route is used as dummy address holder in some split | |
1130 | * nodes. It is not leaked, but it still holds other resources, | |
1131 | * which must be released in time. So, scan ascendant nodes | |
1132 | * and replace dummy references to this route with references | |
1133 | * to still alive ones. | |
1134 | */ | |
1135 | while (fn) { | |
1136 | if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) { | |
8ed67789 | 1137 | fn->leaf = fib6_find_prefix(net, fn); |
1da177e4 LT |
1138 | atomic_inc(&fn->leaf->rt6i_ref); |
1139 | rt6_release(rt); | |
1140 | } | |
1141 | fn = fn->parent; | |
1142 | } | |
1143 | /* No more references are possible at this point. */ | |
2df96af0 | 1144 | BUG_ON(atomic_read(&rt->rt6i_ref) != 1); |
1da177e4 LT |
1145 | } |
1146 | ||
86872cb5 | 1147 | inet6_rt_notify(RTM_DELROUTE, rt, info); |
1da177e4 LT |
1148 | rt6_release(rt); |
1149 | } | |
1150 | ||
86872cb5 | 1151 | int fib6_del(struct rt6_info *rt, struct nl_info *info) |
1da177e4 | 1152 | { |
8ed67789 | 1153 | struct net *net = info->nl_net; |
1da177e4 LT |
1154 | struct fib6_node *fn = rt->rt6i_node; |
1155 | struct rt6_info **rtp; | |
1156 | ||
1157 | #if RT6_DEBUG >= 2 | |
1158 | if (rt->u.dst.obsolete>0) { | |
547b792c | 1159 | WARN_ON(fn != NULL); |
1da177e4 LT |
1160 | return -ENOENT; |
1161 | } | |
1162 | #endif | |
8ed67789 | 1163 | if (fn == NULL || rt == net->ipv6.ip6_null_entry) |
1da177e4 LT |
1164 | return -ENOENT; |
1165 | ||
547b792c | 1166 | WARN_ON(!(fn->fn_flags & RTN_RTINFO)); |
1da177e4 | 1167 | |
150730d5 YH |
1168 | if (!(rt->rt6i_flags&RTF_CACHE)) { |
1169 | struct fib6_node *pn = fn; | |
1170 | #ifdef CONFIG_IPV6_SUBTREES | |
1171 | /* clones of this route might be in another subtree */ | |
1172 | if (rt->rt6i_src.plen) { | |
1173 | while (!(pn->fn_flags&RTN_ROOT)) | |
1174 | pn = pn->parent; | |
1175 | pn = pn->parent; | |
1176 | } | |
1177 | #endif | |
ec7d43c2 | 1178 | fib6_prune_clones(info->nl_net, pn, rt); |
150730d5 | 1179 | } |
1da177e4 LT |
1180 | |
1181 | /* | |
1182 | * Walk the leaf entries looking for ourself | |
1183 | */ | |
1184 | ||
7cc48263 | 1185 | for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) { |
1da177e4 | 1186 | if (*rtp == rt) { |
86872cb5 | 1187 | fib6_del_route(fn, rtp, info); |
1da177e4 LT |
1188 | return 0; |
1189 | } | |
1190 | } | |
1191 | return -ENOENT; | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | * Tree traversal function. | |
1196 | * | |
1197 | * Certainly, it is not interrupt safe. | |
1198 | * However, it is internally reenterable wrt itself and fib6_add/fib6_del. | |
1199 | * It means, that we can modify tree during walking | |
1200 | * and use this function for garbage collection, clone pruning, | |
1ab1457c | 1201 | * cleaning tree when a device goes down etc. etc. |
1da177e4 LT |
1202 | * |
1203 | * It guarantees that every node will be traversed, | |
1204 | * and that it will be traversed only once. | |
1205 | * | |
1206 | * Callback function w->func may return: | |
1207 | * 0 -> continue walking. | |
1208 | * positive value -> walking is suspended (used by tree dumps, | |
1209 | * and probably by gc, if it will be split to several slices) | |
1210 | * negative value -> terminate walking. | |
1211 | * | |
1212 | * The function itself returns: | |
1213 | * 0 -> walk is complete. | |
1214 | * >0 -> walk is incomplete (i.e. suspended) | |
1215 | * <0 -> walk is terminated by an error. | |
1216 | */ | |
1217 | ||
90d41122 | 1218 | static int fib6_walk_continue(struct fib6_walker_t *w) |
1da177e4 LT |
1219 | { |
1220 | struct fib6_node *fn, *pn; | |
1221 | ||
1222 | for (;;) { | |
1223 | fn = w->node; | |
1224 | if (fn == NULL) | |
1225 | return 0; | |
1226 | ||
1227 | if (w->prune && fn != w->root && | |
1228 | fn->fn_flags&RTN_RTINFO && w->state < FWS_C) { | |
1229 | w->state = FWS_C; | |
1230 | w->leaf = fn->leaf; | |
1231 | } | |
1232 | switch (w->state) { | |
1233 | #ifdef CONFIG_IPV6_SUBTREES | |
1234 | case FWS_S: | |
7fc33165 YH |
1235 | if (FIB6_SUBTREE(fn)) { |
1236 | w->node = FIB6_SUBTREE(fn); | |
1da177e4 LT |
1237 | continue; |
1238 | } | |
1239 | w->state = FWS_L; | |
1ab1457c | 1240 | #endif |
1da177e4 LT |
1241 | case FWS_L: |
1242 | if (fn->left) { | |
1243 | w->node = fn->left; | |
1244 | w->state = FWS_INIT; | |
1245 | continue; | |
1246 | } | |
1247 | w->state = FWS_R; | |
1248 | case FWS_R: | |
1249 | if (fn->right) { | |
1250 | w->node = fn->right; | |
1251 | w->state = FWS_INIT; | |
1252 | continue; | |
1253 | } | |
1254 | w->state = FWS_C; | |
1255 | w->leaf = fn->leaf; | |
1256 | case FWS_C: | |
1257 | if (w->leaf && fn->fn_flags&RTN_RTINFO) { | |
2bec5a36 PM |
1258 | int err; |
1259 | ||
1260 | if (w->count < w->skip) { | |
1261 | w->count++; | |
1262 | continue; | |
1263 | } | |
1264 | ||
1265 | err = w->func(w); | |
1da177e4 LT |
1266 | if (err) |
1267 | return err; | |
2bec5a36 PM |
1268 | |
1269 | w->count++; | |
1da177e4 LT |
1270 | continue; |
1271 | } | |
1272 | w->state = FWS_U; | |
1273 | case FWS_U: | |
1274 | if (fn == w->root) | |
1275 | return 0; | |
1276 | pn = fn->parent; | |
1277 | w->node = pn; | |
1278 | #ifdef CONFIG_IPV6_SUBTREES | |
7fc33165 | 1279 | if (FIB6_SUBTREE(pn) == fn) { |
547b792c | 1280 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
1da177e4 LT |
1281 | w->state = FWS_L; |
1282 | continue; | |
1283 | } | |
1284 | #endif | |
1285 | if (pn->left == fn) { | |
1286 | w->state = FWS_R; | |
1287 | continue; | |
1288 | } | |
1289 | if (pn->right == fn) { | |
1290 | w->state = FWS_C; | |
1291 | w->leaf = w->node->leaf; | |
1292 | continue; | |
1293 | } | |
1294 | #if RT6_DEBUG >= 2 | |
547b792c | 1295 | WARN_ON(1); |
1da177e4 LT |
1296 | #endif |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | ||
90d41122 | 1301 | static int fib6_walk(struct fib6_walker_t *w) |
1da177e4 LT |
1302 | { |
1303 | int res; | |
1304 | ||
1305 | w->state = FWS_INIT; | |
1306 | w->node = w->root; | |
1307 | ||
1308 | fib6_walker_link(w); | |
1309 | res = fib6_walk_continue(w); | |
1310 | if (res <= 0) | |
1311 | fib6_walker_unlink(w); | |
1312 | return res; | |
1313 | } | |
1314 | ||
1315 | static int fib6_clean_node(struct fib6_walker_t *w) | |
1316 | { | |
1317 | int res; | |
1318 | struct rt6_info *rt; | |
0a8891a0 | 1319 | struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w); |
ec7d43c2 BT |
1320 | struct nl_info info = { |
1321 | .nl_net = c->net, | |
1322 | }; | |
1da177e4 | 1323 | |
7cc48263 | 1324 | for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) { |
1da177e4 LT |
1325 | res = c->func(rt, c->arg); |
1326 | if (res < 0) { | |
1327 | w->leaf = rt; | |
528c4ceb | 1328 | res = fib6_del(rt, &info); |
1da177e4 LT |
1329 | if (res) { |
1330 | #if RT6_DEBUG >= 2 | |
1331 | printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res); | |
1332 | #endif | |
1333 | continue; | |
1334 | } | |
1335 | return 0; | |
1336 | } | |
547b792c | 1337 | WARN_ON(res != 0); |
1da177e4 LT |
1338 | } |
1339 | w->leaf = rt; | |
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | /* | |
1344 | * Convenient frontend to tree walker. | |
1ab1457c | 1345 | * |
1da177e4 LT |
1346 | * func is called on each route. |
1347 | * It may return -1 -> delete this route. | |
1348 | * 0 -> continue walking | |
1349 | * | |
1350 | * prune==1 -> only immediate children of node (certainly, | |
1351 | * ignoring pure split nodes) will be scanned. | |
1352 | */ | |
1353 | ||
ec7d43c2 | 1354 | static void fib6_clean_tree(struct net *net, struct fib6_node *root, |
8ce11e6a AB |
1355 | int (*func)(struct rt6_info *, void *arg), |
1356 | int prune, void *arg) | |
1da177e4 LT |
1357 | { |
1358 | struct fib6_cleaner_t c; | |
1359 | ||
1360 | c.w.root = root; | |
1361 | c.w.func = fib6_clean_node; | |
1362 | c.w.prune = prune; | |
2bec5a36 PM |
1363 | c.w.count = 0; |
1364 | c.w.skip = 0; | |
1da177e4 LT |
1365 | c.func = func; |
1366 | c.arg = arg; | |
ec7d43c2 | 1367 | c.net = net; |
1da177e4 LT |
1368 | |
1369 | fib6_walk(&c.w); | |
1370 | } | |
1371 | ||
f3db4851 | 1372 | void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), |
c71099ac TG |
1373 | int prune, void *arg) |
1374 | { | |
c71099ac | 1375 | struct fib6_table *table; |
1b43af54 | 1376 | struct hlist_node *node; |
58f09b78 | 1377 | struct hlist_head *head; |
1b43af54 | 1378 | unsigned int h; |
c71099ac | 1379 | |
1b43af54 | 1380 | rcu_read_lock(); |
a33bc5c1 | 1381 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
f3db4851 | 1382 | head = &net->ipv6.fib_table_hash[h]; |
58f09b78 | 1383 | hlist_for_each_entry_rcu(table, node, head, tb6_hlist) { |
c71099ac | 1384 | write_lock_bh(&table->tb6_lock); |
ec7d43c2 BT |
1385 | fib6_clean_tree(net, &table->tb6_root, |
1386 | func, prune, arg); | |
c71099ac TG |
1387 | write_unlock_bh(&table->tb6_lock); |
1388 | } | |
1389 | } | |
1b43af54 | 1390 | rcu_read_unlock(); |
c71099ac TG |
1391 | } |
1392 | ||
1da177e4 LT |
1393 | static int fib6_prune_clone(struct rt6_info *rt, void *arg) |
1394 | { | |
1395 | if (rt->rt6i_flags & RTF_CACHE) { | |
1396 | RT6_TRACE("pruning clone %p\n", rt); | |
1397 | return -1; | |
1398 | } | |
1399 | ||
1400 | return 0; | |
1401 | } | |
1402 | ||
ec7d43c2 BT |
1403 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn, |
1404 | struct rt6_info *rt) | |
1da177e4 | 1405 | { |
ec7d43c2 | 1406 | fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt); |
1da177e4 LT |
1407 | } |
1408 | ||
1409 | /* | |
1410 | * Garbage collection | |
1411 | */ | |
1412 | ||
1413 | static struct fib6_gc_args | |
1414 | { | |
1415 | int timeout; | |
1416 | int more; | |
1417 | } gc_args; | |
1418 | ||
1419 | static int fib6_age(struct rt6_info *rt, void *arg) | |
1420 | { | |
1421 | unsigned long now = jiffies; | |
1422 | ||
1423 | /* | |
1424 | * check addrconf expiration here. | |
1425 | * Routes are expired even if they are in use. | |
1426 | * | |
1427 | * Also age clones. Note, that clones are aged out | |
1428 | * only if they are not in use now. | |
1429 | */ | |
1430 | ||
1431 | if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) { | |
1432 | if (time_after(now, rt->rt6i_expires)) { | |
1433 | RT6_TRACE("expiring %p\n", rt); | |
1da177e4 LT |
1434 | return -1; |
1435 | } | |
1436 | gc_args.more++; | |
1437 | } else if (rt->rt6i_flags & RTF_CACHE) { | |
1438 | if (atomic_read(&rt->u.dst.__refcnt) == 0 && | |
1439 | time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) { | |
1440 | RT6_TRACE("aging clone %p\n", rt); | |
1441 | return -1; | |
1442 | } else if ((rt->rt6i_flags & RTF_GATEWAY) && | |
1443 | (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) { | |
1444 | RT6_TRACE("purging route %p via non-router but gateway\n", | |
1445 | rt); | |
1446 | return -1; | |
1447 | } | |
1448 | gc_args.more++; | |
1449 | } | |
1450 | ||
1451 | return 0; | |
1452 | } | |
1453 | ||
1454 | static DEFINE_SPINLOCK(fib6_gc_lock); | |
1455 | ||
5b7c931d | 1456 | void fib6_run_gc(unsigned long expires, struct net *net) |
1da177e4 | 1457 | { |
5b7c931d | 1458 | if (expires != ~0UL) { |
1da177e4 | 1459 | spin_lock_bh(&fib6_gc_lock); |
5b7c931d DL |
1460 | gc_args.timeout = expires ? (int)expires : |
1461 | net->ipv6.sysctl.ip6_rt_gc_interval; | |
1da177e4 | 1462 | } else { |
a76d7345 | 1463 | if (!spin_trylock_bh(&fib6_gc_lock)) { |
417f28bb | 1464 | mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ); |
1da177e4 LT |
1465 | return; |
1466 | } | |
5b7c931d | 1467 | gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval; |
1da177e4 | 1468 | } |
1da177e4 | 1469 | |
3d0f24a7 | 1470 | gc_args.more = icmp6_dst_gc(); |
f3db4851 | 1471 | |
5b7c931d | 1472 | fib6_clean_all(net, fib6_age, 0, NULL); |
1da177e4 LT |
1473 | |
1474 | if (gc_args.more) | |
c8a45222 SH |
1475 | mod_timer(&net->ipv6.ip6_fib_timer, |
1476 | round_jiffies(jiffies | |
1477 | + net->ipv6.sysctl.ip6_rt_gc_interval)); | |
417f28bb SH |
1478 | else |
1479 | del_timer(&net->ipv6.ip6_fib_timer); | |
1da177e4 LT |
1480 | spin_unlock_bh(&fib6_gc_lock); |
1481 | } | |
1482 | ||
5b7c931d DL |
1483 | static void fib6_gc_timer_cb(unsigned long arg) |
1484 | { | |
1485 | fib6_run_gc(0, (struct net *)arg); | |
1486 | } | |
1487 | ||
2c8c1e72 | 1488 | static int __net_init fib6_net_init(struct net *net) |
1da177e4 | 1489 | { |
417f28bb | 1490 | setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net); |
63152fc0 | 1491 | |
c572872f BT |
1492 | net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL); |
1493 | if (!net->ipv6.rt6_stats) | |
1494 | goto out_timer; | |
1495 | ||
a33bc5c1 | 1496 | net->ipv6.fib_table_hash = kcalloc(FIB6_TABLE_HASHSZ, |
75307c0f SH |
1497 | sizeof(*net->ipv6.fib_table_hash), |
1498 | GFP_KERNEL); | |
58f09b78 | 1499 | if (!net->ipv6.fib_table_hash) |
c572872f | 1500 | goto out_rt6_stats; |
e0b85590 | 1501 | |
58f09b78 DL |
1502 | net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl), |
1503 | GFP_KERNEL); | |
1504 | if (!net->ipv6.fib6_main_tbl) | |
e0b85590 DL |
1505 | goto out_fib_table_hash; |
1506 | ||
58f09b78 | 1507 | net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; |
8ed67789 | 1508 | net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; |
58f09b78 DL |
1509 | net->ipv6.fib6_main_tbl->tb6_root.fn_flags = |
1510 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; | |
e0b85590 DL |
1511 | |
1512 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
58f09b78 DL |
1513 | net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl), |
1514 | GFP_KERNEL); | |
1515 | if (!net->ipv6.fib6_local_tbl) | |
e0b85590 | 1516 | goto out_fib6_main_tbl; |
58f09b78 | 1517 | net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; |
8ed67789 | 1518 | net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry; |
58f09b78 DL |
1519 | net->ipv6.fib6_local_tbl->tb6_root.fn_flags = |
1520 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; | |
e0b85590 | 1521 | #endif |
58f09b78 | 1522 | fib6_tables_init(net); |
f845ab6b | 1523 | |
417f28bb | 1524 | return 0; |
d63bddbe | 1525 | |
e0b85590 | 1526 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
e0b85590 | 1527 | out_fib6_main_tbl: |
58f09b78 | 1528 | kfree(net->ipv6.fib6_main_tbl); |
e0b85590 | 1529 | #endif |
e0b85590 | 1530 | out_fib_table_hash: |
58f09b78 | 1531 | kfree(net->ipv6.fib_table_hash); |
c572872f BT |
1532 | out_rt6_stats: |
1533 | kfree(net->ipv6.rt6_stats); | |
63152fc0 | 1534 | out_timer: |
417f28bb | 1535 | return -ENOMEM; |
58f09b78 DL |
1536 | } |
1537 | ||
1538 | static void fib6_net_exit(struct net *net) | |
1539 | { | |
8ed67789 | 1540 | rt6_ifdown(net, NULL); |
417f28bb SH |
1541 | del_timer_sync(&net->ipv6.ip6_fib_timer); |
1542 | ||
58f09b78 DL |
1543 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
1544 | kfree(net->ipv6.fib6_local_tbl); | |
1545 | #endif | |
1546 | kfree(net->ipv6.fib6_main_tbl); | |
1547 | kfree(net->ipv6.fib_table_hash); | |
c572872f | 1548 | kfree(net->ipv6.rt6_stats); |
58f09b78 DL |
1549 | } |
1550 | ||
1551 | static struct pernet_operations fib6_net_ops = { | |
1552 | .init = fib6_net_init, | |
1553 | .exit = fib6_net_exit, | |
1554 | }; | |
1555 | ||
1556 | int __init fib6_init(void) | |
1557 | { | |
1558 | int ret = -ENOMEM; | |
63152fc0 | 1559 | |
58f09b78 DL |
1560 | fib6_node_kmem = kmem_cache_create("fib6_nodes", |
1561 | sizeof(struct fib6_node), | |
1562 | 0, SLAB_HWCACHE_ALIGN, | |
1563 | NULL); | |
1564 | if (!fib6_node_kmem) | |
1565 | goto out; | |
1566 | ||
1567 | ret = register_pernet_subsys(&fib6_net_ops); | |
1568 | if (ret) | |
c572872f | 1569 | goto out_kmem_cache_create; |
58f09b78 DL |
1570 | |
1571 | ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib); | |
1572 | if (ret) | |
1573 | goto out_unregister_subsys; | |
1574 | out: | |
1575 | return ret; | |
1576 | ||
1577 | out_unregister_subsys: | |
1578 | unregister_pernet_subsys(&fib6_net_ops); | |
d63bddbe DL |
1579 | out_kmem_cache_create: |
1580 | kmem_cache_destroy(fib6_node_kmem); | |
1581 | goto out; | |
1da177e4 LT |
1582 | } |
1583 | ||
1584 | void fib6_gc_cleanup(void) | |
1585 | { | |
58f09b78 | 1586 | unregister_pernet_subsys(&fib6_net_ops); |
1da177e4 LT |
1587 | kmem_cache_destroy(fib6_node_kmem); |
1588 | } |