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