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