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