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