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