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

source code of linux/net/ipv6/ip6_fib.c