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