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