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