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