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