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