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