Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / ip6_fib.c
1 /*
2 * Linux INET6 implementation
3 * Forwarding Information Database
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
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.
19 * Ville Nuorvala: Fixed routing subtrees.
20 */
21
22 #define pr_fmt(fmt) "IPv6: " fmt
23
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>
31 #include <linux/list.h>
32 #include <linux/slab.h>
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...) pr_debug(x)
45 #else
46 #define RT6_TRACE(x...) do { ; } while (0)
47 #endif
48
49 static struct kmem_cache * fib6_node_kmem __read_mostly;
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;
65 struct net *net;
66 int (*func)(struct rt6_info *, void *arg);
67 void *arg;
68 };
69
70 static DEFINE_RWLOCK(fib6_walker_lock);
71
72 #ifdef CONFIG_IPV6_SUBTREES
73 #define FWS_INIT FWS_S
74 #else
75 #define FWS_INIT FWS_L
76 #endif
77
78 static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
79 struct rt6_info *rt);
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);
82 static int fib6_walk(struct fib6_walker_t *w);
83 static int fib6_walk_continue(struct fib6_walker_t *w);
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
94 static void fib6_gc_timer_cb(unsigned long arg);
95
96 static LIST_HEAD(fib6_walkers);
97 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
98
99 static inline void fib6_walker_link(struct fib6_walker_t *w)
100 {
101 write_lock_bh(&fib6_walker_lock);
102 list_add(&w->lh, &fib6_walkers);
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);
109 list_del(&w->lh);
110 write_unlock_bh(&fib6_walker_lock);
111 }
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 *
123 * These assume a 32bit processor (although it will work on
124 * 64bit processors)
125 */
126
127 /*
128 * test bit
129 */
130 #if defined(__LITTLE_ENDIAN)
131 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
132 #else
133 # define BITOP_BE32_SWIZZLE 0
134 #endif
135
136 static __inline__ __be32 addr_bit_set(const void *token, int fn_bit)
137 {
138 const __be32 *addr = token;
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 */
146 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
147 addr[fn_bit >> 5];
148 }
149
150 static __inline__ struct fib6_node * node_alloc(void)
151 {
152 struct fib6_node *fn;
153
154 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
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))
167 dst_free(&rt->dst);
168 }
169
170 static void fib6_free_table(struct fib6_table *table)
171 {
172 inetpeer_invalidate_tree(&table->tb6_peers);
173 kfree(table);
174 }
175
176 static void fib6_link_table(struct net *net, struct fib6_table *tb)
177 {
178 unsigned int h;
179
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
186 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
187
188 /*
189 * No protection necessary, this is the only list mutatation
190 * operation, tables never disappear once they exist.
191 */
192 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
193 }
194
195 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
196
197 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
198 {
199 struct fib6_table *table;
200
201 table = kzalloc(sizeof(*table), GFP_ATOMIC);
202 if (table) {
203 table->tb6_id = id;
204 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
205 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
206 inet_peer_base_init(&table->tb6_peers);
207 }
208
209 return table;
210 }
211
212 struct fib6_table *fib6_new_table(struct net *net, u32 id)
213 {
214 struct fib6_table *tb;
215
216 if (id == 0)
217 id = RT6_TABLE_MAIN;
218 tb = fib6_get_table(net, id);
219 if (tb)
220 return tb;
221
222 tb = fib6_alloc_table(net, id);
223 if (tb)
224 fib6_link_table(net, tb);
225
226 return tb;
227 }
228
229 struct fib6_table *fib6_get_table(struct net *net, u32 id)
230 {
231 struct fib6_table *tb;
232 struct hlist_head *head;
233 unsigned int h;
234
235 if (id == 0)
236 id = RT6_TABLE_MAIN;
237 h = id & (FIB6_TABLE_HASHSZ - 1);
238 rcu_read_lock();
239 head = &net->ipv6.fib_table_hash[h];
240 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
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
251 static void __net_init fib6_tables_init(struct net *net)
252 {
253 fib6_link_table(net, net->ipv6.fib6_main_tbl);
254 fib6_link_table(net, net->ipv6.fib6_local_tbl);
255 }
256 #else
257
258 struct fib6_table *fib6_new_table(struct net *net, u32 id)
259 {
260 return fib6_get_table(net, id);
261 }
262
263 struct fib6_table *fib6_get_table(struct net *net, u32 id)
264 {
265 return net->ipv6.fib6_main_tbl;
266 }
267
268 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
269 int flags, pol_lookup_t lookup)
270 {
271 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
272 }
273
274 static void __net_init fib6_tables_init(struct net *net)
275 {
276 fib6_link_table(net, net->ipv6.fib6_main_tbl);
277 }
278
279 #endif
280
281 static int fib6_dump_node(struct fib6_walker_t *w)
282 {
283 int res;
284 struct rt6_info *rt;
285
286 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
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 }
293 WARN_ON(res == 0);
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) {
304 if (cb->args[4]) {
305 cb->args[4] = 0;
306 fib6_walker_unlink(w);
307 }
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) {
331 w->count = 0;
332 w->skip = 0;
333
334 read_lock_bh(&table->tb6_lock);
335 res = fib6_walk(w);
336 read_unlock_bh(&table->tb6_lock);
337 if (res > 0) {
338 cb->args[4] = 1;
339 cb->args[5] = w->root->fn_sernum;
340 }
341 } else {
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
351 read_lock_bh(&table->tb6_lock);
352 res = fib6_walk_continue(w);
353 read_unlock_bh(&table->tb6_lock);
354 if (res <= 0) {
355 fib6_walker_unlink(w);
356 cb->args[4] = 0;
357 }
358 }
359
360 return res;
361 }
362
363 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
364 {
365 struct net *net = sock_net(skb->sk);
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;
371 struct hlist_head *head;
372 int res = 0;
373
374 s_h = cb->args[0];
375 s_e = cb->args[1];
376
377 w = (void *)cb->args[2];
378 if (!w) {
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);
390 if (!w)
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;
398 arg.net = net;
399 w->args = &arg;
400
401 rcu_read_lock();
402 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
403 e = 0;
404 head = &net->ipv6.fib_table_hash[h];
405 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
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:
416 rcu_read_unlock();
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 }
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,
436 int offset, int allow_create,
437 int replace_required)
438 {
439 struct fib6_node *fn, *in, *ln;
440 struct fib6_node *pn = NULL;
441 struct rt6key *key;
442 int bit;
443 __be32 dir = 0;
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 ||
459 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
460 if (!allow_create) {
461 if (replace_required) {
462 pr_warn("Can't replace route, no match found\n");
463 return ERR_PTR(-ENOENT);
464 }
465 pr_warn("NLM_F_CREATE should be set when creating new route\n");
466 }
467 goto insert_above;
468 }
469
470 /*
471 * Exact match ?
472 */
473
474 if (plen == fn->fn_bit) {
475 /* clean up an intermediate node */
476 if (!(fn->fn_flags & RTN_RTINFO)) {
477 rt6_release(fn->leaf);
478 fn->leaf = NULL;
479 }
480
481 fn->fn_sernum = sernum;
482
483 return fn;
484 }
485
486 /*
487 * We have more bits to go
488 */
489
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
497 if (!allow_create) {
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 */
507 if (replace_required) {
508 pr_warn("Can't replace route, no match found\n");
509 return ERR_PTR(-ENOENT);
510 }
511 pr_warn("NLM_F_CREATE should be set when creating new route\n");
512 }
513 /*
514 * We walked to the bottom of tree.
515 * Create new leaf node without children.
516 */
517
518 ln = node_alloc();
519
520 if (!ln)
521 return ERR_PTR(-ENOMEM);
522 ln->fn_bit = plen;
523
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 /*
537 * split since we don't have a common prefix anymore or
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
548 See comment in __ipv6_addr_diff: bit may be an invalid value,
549 but if it is >= plen, the value is ignored in any case.
550 */
551
552 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
553
554 /*
555 * (intermediate)[in]
556 * / \
557 * (new leaf node)[ln] (old node)[fn]
558 */
559 if (plen > bit) {
560 in = node_alloc();
561 ln = node_alloc();
562
563 if (!in || !ln) {
564 if (in)
565 node_free(in);
566 if (ln)
567 node_free(ln);
568 return ERR_PTR(-ENOMEM);
569 }
570
571 /*
572 * new intermediate node.
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
609 /*
610 * (new leaf node)[ln]
611 * / \
612 * (old node)[fn] NULL
613 */
614
615 ln = node_alloc();
616
617 if (!ln)
618 return ERR_PTR(-ENOMEM);
619
620 ln->fn_bit = plen;
621
622 ln->parent = pn;
623
624 ln->fn_sernum = sernum;
625
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
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
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
670 /*
671 * Insert routing information in a node.
672 */
673
674 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
675 struct nl_info *info)
676 {
677 struct rt6_info *iter = NULL;
678 struct rt6_info **ins;
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));
683 int found = 0;
684 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
685
686 ins = &fn->leaf;
687
688 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
689 /*
690 * Search for duplicates
691 */
692
693 if (iter->rt6i_metric == rt->rt6i_metric) {
694 /*
695 * Same priority level
696 */
697 if (info->nlh &&
698 (info->nlh->nlmsg_flags & NLM_F_EXCL))
699 return -EEXIST;
700 if (replace) {
701 found++;
702 break;
703 }
704
705 if (iter->dst.dev == rt->dst.dev &&
706 iter->rt6i_idev == rt->rt6i_idev &&
707 ipv6_addr_equal(&iter->rt6i_gateway,
708 &rt->rt6i_gateway)) {
709 if (rt->rt6i_nsiblings)
710 rt->rt6i_nsiblings = 0;
711 if (!(iter->rt6i_flags & RTF_EXPIRES))
712 return -EEXIST;
713 if (!(rt->rt6i_flags & RTF_EXPIRES))
714 rt6_clean_expires(iter);
715 else
716 rt6_set_expires(iter, rt->dst.expires);
717 return -EEXIST;
718 }
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 */
730 if (rt_can_ecmp &&
731 rt6_qualify_for_ecmp(iter))
732 rt->rt6i_nsiblings++;
733 }
734
735 if (iter->rt6i_metric > rt->rt6i_metric)
736 break;
737
738 ins = &iter->dst.rt6_next;
739 }
740
741 /* Reset round-robin state, if necessary */
742 if (ins == &fn->leaf)
743 fn->rr_ptr = NULL;
744
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) {
753 if (sibling->rt6i_metric == rt->rt6i_metric &&
754 rt6_qualify_for_ecmp(sibling)) {
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
775 /*
776 * insert node
777 */
778 if (!replace) {
779 if (!add)
780 pr_warn("NLM_F_CREATE should be set when creating new route\n");
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
790 if (!(fn->fn_flags & RTN_RTINFO)) {
791 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
792 fn->fn_flags |= RTN_RTINFO;
793 }
794
795 } else {
796 if (!found) {
797 if (add)
798 goto add;
799 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
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);
807 if (!(fn->fn_flags & RTN_RTINFO)) {
808 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
809 fn->fn_flags |= RTN_RTINFO;
810 }
811 fib6_purge_rt(iter, fn, info->nl_net);
812 rt6_release(iter);
813 }
814
815 return 0;
816 }
817
818 static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
819 {
820 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
821 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
822 mod_timer(&net->ipv6.ip6_fib_timer,
823 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
824 }
825
826 void fib6_force_start_gc(struct net *net)
827 {
828 if (!timer_pending(&net->ipv6.ip6_fib_timer))
829 mod_timer(&net->ipv6.ip6_fib_timer,
830 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
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
839 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
840 {
841 struct fib6_node *fn, *pn = NULL;
842 int err = -ENOMEM;
843 int allow_create = 1;
844 int replace_required = 0;
845
846 if (info->nlh) {
847 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
848 allow_create = 0;
849 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
850 replace_required = 1;
851 }
852 if (!allow_create && !replace_required)
853 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
854
855 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
856 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst),
857 allow_create, replace_required);
858 if (IS_ERR(fn)) {
859 err = PTR_ERR(fn);
860 fn = NULL;
861 goto out;
862 }
863
864 pn = fn;
865
866 #ifdef CONFIG_IPV6_SUBTREES
867 if (rt->rt6i_src.plen) {
868 struct fib6_node *sn;
869
870 if (!fn->subtree) {
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();
885 if (!sfn)
886 goto st_failure;
887
888 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
889 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
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,
897 offsetof(struct rt6_info, rt6i_src),
898 allow_create, replace_required);
899
900 if (IS_ERR(sn)) {
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);
906 err = PTR_ERR(sn);
907 goto st_failure;
908 }
909
910 /* Now link new subtree to main tree */
911 sfn->parent = fn;
912 fn->subtree = sfn;
913 } else {
914 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
915 sizeof(struct in6_addr), rt->rt6i_src.plen,
916 offsetof(struct rt6_info, rt6i_src),
917 allow_create, replace_required);
918
919 if (IS_ERR(sn)) {
920 err = PTR_ERR(sn);
921 goto st_failure;
922 }
923 }
924
925 if (!fn->leaf) {
926 fn->leaf = rt;
927 atomic_inc(&rt->rt6i_ref);
928 }
929 fn = sn;
930 }
931 #endif
932
933 err = fib6_add_rt2node(fn, rt, info);
934 if (!err) {
935 fib6_start_gc(info->nl_net, rt);
936 if (!(rt->rt6i_flags & RTF_CACHE))
937 fib6_prune_clones(info->nl_net, pn, rt);
938 }
939
940 out:
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 */
947 if (pn != fn && pn->leaf == rt) {
948 pn->leaf = NULL;
949 atomic_dec(&rt->rt6i_ref);
950 }
951 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
952 pn->leaf = fib6_find_prefix(info->nl_net, pn);
953 #if RT6_DEBUG >= 2
954 if (!pn->leaf) {
955 WARN_ON(pn->leaf == NULL);
956 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
957 }
958 #endif
959 atomic_inc(&pn->leaf->rt6i_ref);
960 }
961 #endif
962 dst_free(&rt->dst);
963 }
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)))
972 fib6_repair_tree(info->nl_net, fn);
973 dst_free(&rt->dst);
974 return err;
975 #endif
976 }
977
978 /*
979 * Routing tree lookup
980 *
981 */
982
983 struct lookup_args {
984 int offset; /* key offset on rt6_info */
985 const struct in6_addr *addr; /* search key */
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;
992 __be32 dir;
993
994 if (unlikely(args->offset == 0))
995 return NULL;
996
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 }
1014 break;
1015 }
1016
1017 while (fn) {
1018 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1019 struct rt6key *key;
1020
1021 key = (struct rt6key *) ((u8 *) fn->leaf +
1022 args->offset);
1023
1024 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1025 #ifdef CONFIG_IPV6_SUBTREES
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 }
1034 #endif
1035 if (fn->fn_flags & RTN_RTINFO)
1036 return fn;
1037 }
1038 }
1039 #ifdef CONFIG_IPV6_SUBTREES
1040 backtrack:
1041 #endif
1042 if (fn->fn_flags & RTN_ROOT)
1043 break;
1044
1045 fn = fn->parent;
1046 }
1047
1048 return NULL;
1049 }
1050
1051 struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1052 const struct in6_addr *saddr)
1053 {
1054 struct fib6_node *fn;
1055 struct lookup_args args[] = {
1056 {
1057 .offset = offsetof(struct rt6_info, rt6i_dst),
1058 .addr = daddr,
1059 },
1060 #ifdef CONFIG_IPV6_SUBTREES
1061 {
1062 .offset = offsetof(struct rt6_info, rt6i_src),
1063 .addr = saddr,
1064 },
1065 #endif
1066 {
1067 .offset = 0, /* sentinel */
1068 }
1069 };
1070
1071 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1072 if (!fn || fn->fn_flags & RTN_TL_ROOT)
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,
1085 const struct in6_addr *addr,
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,
1115 const struct in6_addr *daddr, int dst_len,
1116 const struct in6_addr *saddr, int src_len)
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) {
1125 WARN_ON(saddr == NULL);
1126 if (fn && fn->subtree)
1127 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1128 offsetof(struct rt6_info, rt6i_src));
1129 }
1130 #endif
1131
1132 if (fn && fn->fn_flags & RTN_RTINFO)
1133 return fn;
1134
1135 return NULL;
1136 }
1137
1138
1139 /*
1140 * Deletion
1141 *
1142 */
1143
1144 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1145 {
1146 if (fn->fn_flags & RTN_ROOT)
1147 return net->ipv6.ip6_null_entry;
1148
1149 while (fn) {
1150 if (fn->left)
1151 return fn->left->leaf;
1152 if (fn->right)
1153 return fn->right->leaf;
1154
1155 fn = FIB6_SUBTREE(fn);
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
1165 static struct fib6_node *fib6_repair_tree(struct net *net,
1166 struct fib6_node *fn)
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
1178 WARN_ON(fn->fn_flags & RTN_RTINFO);
1179 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1180 WARN_ON(fn->leaf != NULL);
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
1187 if (children == 3 || FIB6_SUBTREE(fn)
1188 #ifdef CONFIG_IPV6_SUBTREES
1189 /* Subtree root (i.e. fn) may have one child */
1190 || (children && fn->fn_flags & RTN_ROOT)
1191 #endif
1192 ) {
1193 fn->leaf = fib6_find_prefix(net, fn);
1194 #if RT6_DEBUG >= 2
1195 if (!fn->leaf) {
1196 WARN_ON(!fn->leaf);
1197 fn->leaf = net->ipv6.ip6_null_entry;
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
1206 if (FIB6_SUBTREE(pn) == fn) {
1207 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1208 FIB6_SUBTREE(pn) = NULL;
1209 nstate = FWS_L;
1210 } else {
1211 WARN_ON(fn->fn_flags & RTN_ROOT);
1212 #endif
1213 if (pn->right == fn) pn->right = child;
1214 else if (pn->left == fn) pn->left = child;
1215 #if RT6_DEBUG >= 2
1216 else
1217 WARN_ON(1);
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) {
1228 if (!child) {
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);
1257 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
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,
1267 struct nl_info *info)
1268 {
1269 struct fib6_walker_t *w;
1270 struct rt6_info *rt = *rtp;
1271 struct net *net = info->nl_net;
1272
1273 RT6_TRACE("fib6_del_route\n");
1274
1275 /* Unlink it */
1276 *rtp = rt->dst.rt6_next;
1277 rt->rt6i_node = NULL;
1278 net->ipv6.rt6_stats->fib_rt_entries--;
1279 net->ipv6.rt6_stats->fib_discarded_routes++;
1280
1281 /* Reset round-robin state, if necessary */
1282 if (fn->rr_ptr == rt)
1283 fn->rr_ptr = NULL;
1284
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
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);
1301 w->leaf = rt->dst.rt6_next;
1302 if (!w->leaf)
1303 w->state = FWS_U;
1304 }
1305 }
1306 read_unlock(&fib6_walker_lock);
1307
1308 rt->dst.rt6_next = NULL;
1309
1310 /* If it was last route, expunge its radix tree node */
1311 if (!fn->leaf) {
1312 fn->fn_flags &= ~RTN_RTINFO;
1313 net->ipv6.rt6_stats->fib_route_nodes--;
1314 fn = fib6_repair_tree(net, fn);
1315 }
1316
1317 fib6_purge_rt(rt, fn, net);
1318
1319 inet6_rt_notify(RTM_DELROUTE, rt, info);
1320 rt6_release(rt);
1321 }
1322
1323 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1324 {
1325 struct net *net = info->nl_net;
1326 struct fib6_node *fn = rt->rt6i_node;
1327 struct rt6_info **rtp;
1328
1329 #if RT6_DEBUG >= 2
1330 if (rt->dst.obsolete>0) {
1331 WARN_ON(fn != NULL);
1332 return -ENOENT;
1333 }
1334 #endif
1335 if (!fn || rt == net->ipv6.ip6_null_entry)
1336 return -ENOENT;
1337
1338 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1339
1340 if (!(rt->rt6i_flags & RTF_CACHE)) {
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) {
1345 while (!(pn->fn_flags & RTN_ROOT))
1346 pn = pn->parent;
1347 pn = pn->parent;
1348 }
1349 #endif
1350 fib6_prune_clones(info->nl_net, pn, rt);
1351 }
1352
1353 /*
1354 * Walk the leaf entries looking for ourself
1355 */
1356
1357 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1358 if (*rtp == rt) {
1359 fib6_del_route(fn, rtp, info);
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,
1373 * cleaning tree when a device goes down etc. etc.
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
1390 static int fib6_walk_continue(struct fib6_walker_t *w)
1391 {
1392 struct fib6_node *fn, *pn;
1393
1394 for (;;) {
1395 fn = w->node;
1396 if (!fn)
1397 return 0;
1398
1399 if (w->prune && fn != w->root &&
1400 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1401 w->state = FWS_C;
1402 w->leaf = fn->leaf;
1403 }
1404 switch (w->state) {
1405 #ifdef CONFIG_IPV6_SUBTREES
1406 case FWS_S:
1407 if (FIB6_SUBTREE(fn)) {
1408 w->node = FIB6_SUBTREE(fn);
1409 continue;
1410 }
1411 w->state = FWS_L;
1412 #endif
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:
1429 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1430 int err;
1431
1432 if (w->skip) {
1433 w->skip--;
1434 goto skip;
1435 }
1436
1437 err = w->func(w);
1438 if (err)
1439 return err;
1440
1441 w->count++;
1442 continue;
1443 }
1444 skip:
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
1452 if (FIB6_SUBTREE(pn) == fn) {
1453 WARN_ON(!(fn->fn_flags & RTN_ROOT));
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
1468 WARN_ON(1);
1469 #endif
1470 }
1471 }
1472 }
1473
1474 static int fib6_walk(struct fib6_walker_t *w)
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;
1492 struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
1493 struct nl_info info = {
1494 .nl_net = c->net,
1495 };
1496
1497 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1498 res = c->func(rt, c->arg);
1499 if (res < 0) {
1500 w->leaf = rt;
1501 res = fib6_del(rt, &info);
1502 if (res) {
1503 #if RT6_DEBUG >= 2
1504 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1505 __func__, rt, rt->rt6i_node, res);
1506 #endif
1507 continue;
1508 }
1509 return 0;
1510 }
1511 WARN_ON(res != 0);
1512 }
1513 w->leaf = rt;
1514 return 0;
1515 }
1516
1517 /*
1518 * Convenient frontend to tree walker.
1519 *
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
1528 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1529 int (*func)(struct rt6_info *, void *arg),
1530 int prune, void *arg)
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;
1537 c.w.count = 0;
1538 c.w.skip = 0;
1539 c.func = func;
1540 c.arg = arg;
1541 c.net = net;
1542
1543 fib6_walk(&c.w);
1544 }
1545
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;
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];
1556 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
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 }
1565 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
1566 int prune, void *arg)
1567 {
1568 struct fib6_table *table;
1569 struct hlist_head *head;
1570 unsigned int h;
1571
1572 rcu_read_lock();
1573 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1574 head = &net->ipv6.fib_table_hash[h];
1575 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1576 write_lock_bh(&table->tb6_lock);
1577 fib6_clean_tree(net, &table->tb6_root,
1578 func, prune, arg);
1579 write_unlock_bh(&table->tb6_lock);
1580 }
1581 }
1582 rcu_read_unlock();
1583 }
1584
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
1595 static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
1596 struct rt6_info *rt)
1597 {
1598 fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
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
1623 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1624 if (time_after(now, rt->dst.expires)) {
1625 RT6_TRACE("expiring %p\n", rt);
1626 return -1;
1627 }
1628 gc_args.more++;
1629 } else if (rt->rt6i_flags & RTF_CACHE) {
1630 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1631 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
1632 RT6_TRACE("aging clone %p\n", rt);
1633 return -1;
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 }
1643 if (!(neigh_flags & NTF_ROUTER)) {
1644 RT6_TRACE("purging route %p via non-router but gateway\n",
1645 rt);
1646 return -1;
1647 }
1648 }
1649 gc_args.more++;
1650 }
1651
1652 return 0;
1653 }
1654
1655 static DEFINE_SPINLOCK(fib6_gc_lock);
1656
1657 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1658 {
1659 unsigned long now;
1660
1661 if (force) {
1662 spin_lock_bh(&fib6_gc_lock);
1663 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1664 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1665 return;
1666 }
1667 gc_args.timeout = expires ? (int)expires :
1668 net->ipv6.sysctl.ip6_rt_gc_interval;
1669
1670 gc_args.more = icmp6_dst_gc();
1671
1672 fib6_clean_all(net, fib6_age, 0, NULL);
1673 now = jiffies;
1674 net->ipv6.ip6_rt_last_gc = now;
1675
1676 if (gc_args.more)
1677 mod_timer(&net->ipv6.ip6_fib_timer,
1678 round_jiffies(now
1679 + net->ipv6.sysctl.ip6_rt_gc_interval));
1680 else
1681 del_timer(&net->ipv6.ip6_fib_timer);
1682 spin_unlock_bh(&fib6_gc_lock);
1683 }
1684
1685 static void fib6_gc_timer_cb(unsigned long arg)
1686 {
1687 fib6_run_gc(0, (struct net *)arg, true);
1688 }
1689
1690 static int __net_init fib6_net_init(struct net *net)
1691 {
1692 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1693
1694 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1695
1696 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1697 if (!net->ipv6.rt6_stats)
1698 goto out_timer;
1699
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);
1704 if (!net->ipv6.fib_table_hash)
1705 goto out_rt6_stats;
1706
1707 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1708 GFP_KERNEL);
1709 if (!net->ipv6.fib6_main_tbl)
1710 goto out_fib_table_hash;
1711
1712 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1713 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1714 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1715 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1716 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
1717
1718 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1719 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1720 GFP_KERNEL);
1721 if (!net->ipv6.fib6_local_tbl)
1722 goto out_fib6_main_tbl;
1723 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1724 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1725 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1726 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1727 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
1728 #endif
1729 fib6_tables_init(net);
1730
1731 return 0;
1732
1733 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1734 out_fib6_main_tbl:
1735 kfree(net->ipv6.fib6_main_tbl);
1736 #endif
1737 out_fib_table_hash:
1738 kfree(net->ipv6.fib_table_hash);
1739 out_rt6_stats:
1740 kfree(net->ipv6.rt6_stats);
1741 out_timer:
1742 return -ENOMEM;
1743 }
1744
1745 static void fib6_net_exit(struct net *net)
1746 {
1747 unsigned int i;
1748
1749 rt6_ifdown(net, NULL);
1750 del_timer_sync(&net->ipv6.ip6_fib_timer);
1751
1752 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
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
1763 kfree(net->ipv6.fib_table_hash);
1764 kfree(net->ipv6.rt6_stats);
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;
1775
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)
1785 goto out_kmem_cache_create;
1786
1787 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1788 NULL);
1789 if (ret)
1790 goto out_unregister_subsys;
1791 out:
1792 return ret;
1793
1794 out_unregister_subsys:
1795 unregister_pernet_subsys(&fib6_net_ops);
1796 out_kmem_cache_create:
1797 kmem_cache_destroy(fib6_node_kmem);
1798 goto out;
1799 }
1800
1801 void fib6_gc_cleanup(void)
1802 {
1803 unregister_pernet_subsys(&fib6_net_ops);
1804 kmem_cache_destroy(fib6_node_kmem);
1805 }