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