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