net: common metrics init helper for FIB entries
[linux-block.git] / net / ipv6 / ip6_fib.c
CommitLineData
1da177e4 1/*
1ab1457c 2 * Linux INET6 implementation
1da177e4
LT
3 * Forwarding Information Database
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4 7 *
1da177e4
LT
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
8db46f1d
WY
12 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
1da177e4 18 */
f3213831
JP
19
20#define pr_fmt(fmt) "IPv6: " fmt
21
1da177e4
LT
22#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
c71099ac 29#include <linux/list.h>
5a0e3ad6 30#include <linux/slab.h>
1da177e4 31
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);
93c2fb25 163 atomic_inc(&f6i->fib6_ref);
a64efe14
DA
164
165 return f6i;
166}
167
9b0a8da8 168void fib6_info_destroy_rcu(struct rcu_head *head)
a64efe14 169{
9b0a8da8 170 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
a64efe14 171 struct rt6_exception_bucket *bucket;
e6aed040 172 struct dst_metrics *m;
a64efe14 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
e6aed040
DM
204 m = f6i->fib6_metrics;
205 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
206 kfree(m);
93531c67 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));
33bd5ac5 938 struct fib6_info *iter = NULL;
8d1c802b 939 struct fib6_info __rcu **ins;
33bd5ac5 940 struct fib6_info __rcu **fallback_ins = NULL;
507c9b1e
DM
941 int replace = (info->nlh &&
942 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
943 int add = (!info->nlh ||
944 (info->nlh->nlmsg_flags & NLM_F_CREATE));
4a287eba 945 int found = 0;
33bd5ac5 946 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
73483c12 947 u16 nlflags = NLM_F_EXCL;
e5fd387a 948 int err;
1da177e4 949
33bd5ac5 950 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
1f5e29ce
DA
951 nlflags |= NLM_F_APPEND;
952
1da177e4
LT
953 ins = &fn->leaf;
954
66f5d6ce 955 for (iter = leaf; iter;
8fb11a9a 956 iter = rcu_dereference_protected(iter->fib6_next,
93c2fb25 957 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
1da177e4
LT
958 /*
959 * Search for duplicates
960 */
961
93c2fb25 962 if (iter->fib6_metric == rt->fib6_metric) {
1da177e4
LT
963 /*
964 * Same priority level
965 */
507c9b1e
DM
966 if (info->nlh &&
967 (info->nlh->nlmsg_flags & NLM_F_EXCL))
4a287eba 968 return -EEXIST;
73483c12
GN
969
970 nlflags &= ~NLM_F_EXCL;
4a287eba 971 if (replace) {
33bd5ac5
DA
972 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
973 found++;
974 break;
975 }
976 if (rt_can_ecmp)
977 fallback_ins = fallback_ins ?: ins;
978 goto next_iter;
4a287eba 979 }
1da177e4 980
f06b7549 981 if (rt6_duplicate_nexthop(iter, rt)) {
93c2fb25
DA
982 if (rt->fib6_nsiblings)
983 rt->fib6_nsiblings = 0;
984 if (!(iter->fib6_flags & RTF_EXPIRES))
1da177e4 985 return -EEXIST;
93c2fb25 986 if (!(rt->fib6_flags & RTF_EXPIRES))
14895687 987 fib6_clean_expires(iter);
1716a961 988 else
14895687 989 fib6_set_expires(iter, rt->expires);
15a81b41
DA
990
991 if (rt->fib6_pmtu)
992 fib6_metric_set(iter, RTAX_MTU,
993 rt->fib6_pmtu);
1da177e4
LT
994 return -EEXIST;
995 }
33bd5ac5
DA
996 /* If we have the same destination and the same metric,
997 * but not the same gateway, then the route we try to
998 * add is sibling to this route, increment our counter
999 * of siblings, and later we will add our route to the
1000 * list.
1001 * Only static routes (which don't have flag
1002 * RTF_EXPIRES) are used for ECMPv6.
1003 *
1004 * To avoid long list, we only had siblings if the
1005 * route have a gateway.
1006 */
1007 if (rt_can_ecmp &&
1008 rt6_qualify_for_ecmp(iter))
1009 rt->fib6_nsiblings++;
1da177e4
LT
1010 }
1011
93c2fb25 1012 if (iter->fib6_metric > rt->fib6_metric)
1da177e4
LT
1013 break;
1014
33bd5ac5 1015next_iter:
8fb11a9a 1016 ins = &iter->fib6_next;
27596472
MK
1017 }
1018
33bd5ac5
DA
1019 if (fallback_ins && !found) {
1020 /* No ECMP-able route found, replace first non-ECMP one */
1021 ins = fallback_ins;
1022 iter = rcu_dereference_protected(*ins,
1023 lockdep_is_held(&rt->fib6_table->tb6_lock));
1024 found++;
1025 }
1026
f11e6659
DM
1027 /* Reset round-robin state, if necessary */
1028 if (ins == &fn->leaf)
1029 fn->rr_ptr = NULL;
1030
51ebd318 1031 /* Link this route to others same route. */
33bd5ac5
DA
1032 if (rt->fib6_nsiblings) {
1033 unsigned int fib6_nsiblings;
8d1c802b 1034 struct fib6_info *sibling, *temp_sibling;
51ebd318 1035
33bd5ac5
DA
1036 /* Find the first route that have the same metric */
1037 sibling = leaf;
1038 while (sibling) {
1039 if (sibling->fib6_metric == rt->fib6_metric &&
1040 rt6_qualify_for_ecmp(sibling)) {
1041 list_add_tail(&rt->fib6_siblings,
1042 &sibling->fib6_siblings);
1043 break;
1044 }
1045 sibling = rcu_dereference_protected(sibling->fib6_next,
1046 lockdep_is_held(&rt->fib6_table->tb6_lock));
51ebd318
ND
1047 }
1048 /* For each sibling in the list, increment the counter of
1049 * siblings. BUG() if counters does not match, list of siblings
1050 * is broken!
1051 */
33bd5ac5 1052 fib6_nsiblings = 0;
51ebd318 1053 list_for_each_entry_safe(sibling, temp_sibling,
33bd5ac5 1054 &rt->fib6_siblings, fib6_siblings) {
93c2fb25 1055 sibling->fib6_nsiblings++;
33bd5ac5
DA
1056 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1057 fib6_nsiblings++;
51ebd318 1058 }
33bd5ac5
DA
1059 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1060 rt6_multipath_rebalance(temp_sibling);
51ebd318
ND
1061 }
1062
1da177e4
LT
1063 /*
1064 * insert node
1065 */
4a287eba
MV
1066 if (!replace) {
1067 if (!add)
f3213831 1068 pr_warn("NLM_F_CREATE should be set when creating new route\n");
4a287eba
MV
1069
1070add:
73483c12 1071 nlflags |= NLM_F_CREATE;
e715b6d3 1072
33bd5ac5
DA
1073 err = call_fib6_entry_notifiers(info->nl_net,
1074 FIB_EVENT_ENTRY_ADD,
1075 rt, extack);
2233000c
DA
1076 if (err)
1077 return err;
1078
8fb11a9a 1079 rcu_assign_pointer(rt->fib6_next, iter);
93c2fb25
DA
1080 atomic_inc(&rt->fib6_ref);
1081 rcu_assign_pointer(rt->fib6_node, fn);
66f5d6ce 1082 rcu_assign_pointer(*ins, rt);
3b1137fe
DA
1083 if (!info->skip_notify)
1084 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4a287eba
MV
1085 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1086
507c9b1e 1087 if (!(fn->fn_flags & RTN_RTINFO)) {
4a287eba
MV
1088 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1089 fn->fn_flags |= RTN_RTINFO;
1090 }
1da177e4 1091
4a287eba 1092 } else {
33bd5ac5 1093 int nsiblings;
27596472 1094
4a287eba
MV
1095 if (!found) {
1096 if (add)
1097 goto add;
f3213831 1098 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
4a287eba
MV
1099 return -ENOENT;
1100 }
e715b6d3 1101
2233000c
DA
1102 err = call_fib6_entry_notifiers(info->nl_net,
1103 FIB_EVENT_ENTRY_REPLACE,
1104 rt, extack);
1105 if (err)
1106 return err;
1107
93c2fb25
DA
1108 atomic_inc(&rt->fib6_ref);
1109 rcu_assign_pointer(rt->fib6_node, fn);
33bd5ac5 1110 rt->fib6_next = iter->fib6_next;
66f5d6ce 1111 rcu_assign_pointer(*ins, rt);
3b1137fe
DA
1112 if (!info->skip_notify)
1113 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
507c9b1e 1114 if (!(fn->fn_flags & RTN_RTINFO)) {
4a287eba
MV
1115 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1116 fn->fn_flags |= RTN_RTINFO;
1117 }
33bd5ac5
DA
1118 nsiblings = iter->fib6_nsiblings;
1119 iter->fib6_node = NULL;
1120 fib6_purge_rt(iter, fn, info->nl_net);
1121 if (rcu_access_pointer(fn->rr_ptr) == iter)
1122 fn->rr_ptr = NULL;
1123 fib6_info_release(iter);
27596472 1124
33bd5ac5 1125 if (nsiblings) {
27596472 1126 /* Replacing an ECMP route, remove all siblings */
33bd5ac5
DA
1127 ins = &rt->fib6_next;
1128 iter = rcu_dereference_protected(*ins,
1129 lockdep_is_held(&rt->fib6_table->tb6_lock));
1130 while (iter) {
1131 if (iter->fib6_metric > rt->fib6_metric)
1132 break;
1133 if (rt6_qualify_for_ecmp(iter)) {
1134 *ins = iter->fib6_next;
1135 iter->fib6_node = NULL;
1136 fib6_purge_rt(iter, fn, info->nl_net);
1137 if (rcu_access_pointer(fn->rr_ptr) == iter)
1138 fn->rr_ptr = NULL;
1139 fib6_info_release(iter);
1140 nsiblings--;
1141 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1142 } else {
1143 ins = &iter->fib6_next;
1144 }
1145 iter = rcu_dereference_protected(*ins,
1146 lockdep_is_held(&rt->fib6_table->tb6_lock));
27596472 1147 }
33bd5ac5 1148 WARN_ON(nsiblings != 0);
27596472 1149 }
1da177e4
LT
1150 }
1151
1152 return 0;
1153}
1154
8d1c802b 1155static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1da177e4 1156{
417f28bb 1157 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
93c2fb25 1158 (rt->fib6_flags & RTF_EXPIRES))
417f28bb 1159 mod_timer(&net->ipv6.ip6_fib_timer,
847499ce 1160 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1da177e4
LT
1161}
1162
63152fc0 1163void fib6_force_start_gc(struct net *net)
1da177e4 1164{
417f28bb
SH
1165 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1166 mod_timer(&net->ipv6.ip6_fib_timer,
847499ce 1167 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1da177e4
LT
1168}
1169
8d1c802b 1170static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
4a8e56ee 1171 int sernum)
bbd63f06 1172{
93c2fb25
DA
1173 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1174 lockdep_is_held(&rt->fib6_table->tb6_lock));
bbd63f06
WW
1175
1176 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1177 smp_wmb();
1178 while (fn) {
1179 fn->fn_sernum = sernum;
66f5d6ce 1180 fn = rcu_dereference_protected(fn->parent,
93c2fb25 1181 lockdep_is_held(&rt->fib6_table->tb6_lock));
bbd63f06
WW
1182 }
1183}
1184
8d1c802b 1185void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
4a8e56ee
IS
1186{
1187 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1188}
1189
1da177e4
LT
1190/*
1191 * Add routing information to the routing tree.
1192 * <destination addr>/<source addr>
1193 * with source addr info in sub-trees
66f5d6ce 1194 * Need to own table->tb6_lock
1da177e4
LT
1195 */
1196
8d1c802b 1197int fib6_add(struct fib6_node *root, struct fib6_info *rt,
d4ead6b3 1198 struct nl_info *info, struct netlink_ext_ack *extack)
1da177e4 1199{
93c2fb25 1200 struct fib6_table *table = rt->fib6_table;
66729e18 1201 struct fib6_node *fn, *pn = NULL;
1da177e4 1202 int err = -ENOMEM;
4a287eba
MV
1203 int allow_create = 1;
1204 int replace_required = 0;
812918c4 1205 int sernum = fib6_new_sernum(info->nl_net);
507c9b1e
DM
1206
1207 if (info->nlh) {
1208 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
4a287eba 1209 allow_create = 0;
507c9b1e 1210 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
4a287eba
MV
1211 replace_required = 1;
1212 }
1213 if (!allow_create && !replace_required)
f3213831 1214 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1da177e4 1215
81eb8447 1216 fn = fib6_add_1(info->nl_net, table, root,
93c2fb25
DA
1217 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1218 offsetof(struct fib6_info, fib6_dst), allow_create,
bbd63f06 1219 replace_required, extack);
4a287eba
MV
1220 if (IS_ERR(fn)) {
1221 err = PTR_ERR(fn);
ae7b4e1f 1222 fn = NULL;
1da177e4 1223 goto out;
188c517a 1224 }
1da177e4 1225
66729e18
YH
1226 pn = fn;
1227
1da177e4 1228#ifdef CONFIG_IPV6_SUBTREES
93c2fb25 1229 if (rt->fib6_src.plen) {
1da177e4
LT
1230 struct fib6_node *sn;
1231
66f5d6ce 1232 if (!rcu_access_pointer(fn->subtree)) {
1da177e4
LT
1233 struct fib6_node *sfn;
1234
1235 /*
1236 * Create subtree.
1237 *
1238 * fn[main tree]
1239 * |
1240 * sfn[subtree root]
1241 * \
1242 * sn[new leaf node]
1243 */
1244
1245 /* Create subtree root node */
81eb8447 1246 sfn = node_alloc(info->nl_net);
507c9b1e 1247 if (!sfn)
348a4002 1248 goto failure;
1da177e4 1249
93c2fb25 1250 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
66f5d6ce 1251 rcu_assign_pointer(sfn->leaf,
421842ed 1252 info->nl_net->ipv6.fib6_null_entry);
1da177e4 1253 sfn->fn_flags = RTN_ROOT;
1da177e4
LT
1254
1255 /* Now add the first leaf node to new subtree */
1256
81eb8447 1257 sn = fib6_add_1(info->nl_net, table, sfn,
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
f950c0ec 1262 if (IS_ERR(sn)) {
1da177e4 1263 /* If it is failed, discard just allocated
348a4002 1264 root, and then (in failure) stale node
1da177e4
LT
1265 in main tree.
1266 */
81eb8447 1267 node_free_immediate(info->nl_net, sfn);
188c517a 1268 err = PTR_ERR(sn);
348a4002 1269 goto failure;
1da177e4
LT
1270 }
1271
1272 /* Now link new subtree to main tree */
66f5d6ce
WW
1273 rcu_assign_pointer(sfn->parent, fn);
1274 rcu_assign_pointer(fn->subtree, sfn);
1da177e4 1275 } else {
81eb8447 1276 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
93c2fb25
DA
1277 &rt->fib6_src.addr, rt->fib6_src.plen,
1278 offsetof(struct fib6_info, fib6_src),
bbd63f06 1279 allow_create, replace_required, extack);
1da177e4 1280
4a287eba
MV
1281 if (IS_ERR(sn)) {
1282 err = PTR_ERR(sn);
348a4002 1283 goto failure;
188c517a 1284 }
1da177e4
LT
1285 }
1286
66f5d6ce 1287 if (!rcu_access_pointer(fn->leaf)) {
591ff9ea
WW
1288 if (fn->fn_flags & RTN_TL_ROOT) {
1289 /* put back null_entry for root node */
1290 rcu_assign_pointer(fn->leaf,
421842ed 1291 info->nl_net->ipv6.fib6_null_entry);
591ff9ea 1292 } else {
93c2fb25 1293 atomic_inc(&rt->fib6_ref);
591ff9ea
WW
1294 rcu_assign_pointer(fn->leaf, rt);
1295 }
66729e18 1296 }
1da177e4
LT
1297 fn = sn;
1298 }
1299#endif
1300
d4ead6b3 1301 err = fib6_add_rt2node(fn, rt, info, extack);
bbd63f06 1302 if (!err) {
4a8e56ee 1303 __fib6_update_sernum_upto_root(rt, sernum);
63152fc0 1304 fib6_start_gc(info->nl_net, rt);
bbd63f06 1305 }
1da177e4
LT
1306
1307out:
66729e18
YH
1308 if (err) {
1309#ifdef CONFIG_IPV6_SUBTREES
1310 /*
1311 * If fib6_add_1 has cleared the old leaf pointer in the
1312 * super-tree leaf node we have to find a new one for it.
1313 */
7bbfe00e 1314 if (pn != fn) {
8d1c802b 1315 struct fib6_info *pn_leaf =
7bbfe00e
WW
1316 rcu_dereference_protected(pn->leaf,
1317 lockdep_is_held(&table->tb6_lock));
1318 if (pn_leaf == rt) {
1319 pn_leaf = NULL;
1320 RCU_INIT_POINTER(pn->leaf, NULL);
93531c67 1321 fib6_info_release(rt);
66729e18 1322 }
7bbfe00e
WW
1323 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1324 pn_leaf = fib6_find_prefix(info->nl_net, table,
1325 pn);
1326#if RT6_DEBUG >= 2
1327 if (!pn_leaf) {
1328 WARN_ON(!pn_leaf);
1329 pn_leaf =
421842ed 1330 info->nl_net->ipv6.fib6_null_entry;
7bbfe00e 1331 }
66729e18 1332#endif
93531c67 1333 fib6_info_hold(pn_leaf);
7bbfe00e
WW
1334 rcu_assign_pointer(pn->leaf, pn_leaf);
1335 }
66729e18
YH
1336 }
1337#endif
348a4002 1338 goto failure;
66729e18 1339 }
1da177e4
LT
1340 return err;
1341
348a4002 1342failure:
4512c43e
WW
1343 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1344 * 1. fn is an intermediate node and we failed to add the new
1345 * route to it in both subtree creation failure and fib6_add_rt2node()
1346 * failure case.
1347 * 2. fn is the root node in the table and we fail to add the first
1348 * default route to it.
1da177e4 1349 */
4512c43e
WW
1350 if (fn &&
1351 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1352 (fn->fn_flags & RTN_TL_ROOT &&
1353 !rcu_access_pointer(fn->leaf))))
66f5d6ce 1354 fib6_repair_tree(info->nl_net, table, fn);
1da177e4 1355 return err;
1da177e4
LT
1356}
1357
1358/*
1359 * Routing tree lookup
1360 *
1361 */
1362
1363struct lookup_args {
8d1c802b 1364 int offset; /* key offset on fib6_info */
b71d1d42 1365 const struct in6_addr *addr; /* search key */
1da177e4
LT
1366};
1367
6454743b
DA
1368static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1369 struct lookup_args *args)
1da177e4
LT
1370{
1371 struct fib6_node *fn;
e69a4adc 1372 __be32 dir;
1da177e4 1373
825e288e
YH
1374 if (unlikely(args->offset == 0))
1375 return NULL;
1376
1da177e4
LT
1377 /*
1378 * Descend on a tree
1379 */
1380
1381 fn = root;
1382
1383 for (;;) {
1384 struct fib6_node *next;
1385
1386 dir = addr_bit_set(args->addr, fn->fn_bit);
1387
66f5d6ce
WW
1388 next = dir ? rcu_dereference(fn->right) :
1389 rcu_dereference(fn->left);
1da177e4
LT
1390
1391 if (next) {
1392 fn = next;
1393 continue;
1394 }
1da177e4
LT
1395 break;
1396 }
1397
507c9b1e 1398 while (fn) {
66f5d6ce
WW
1399 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1400
1401 if (subtree || fn->fn_flags & RTN_RTINFO) {
8d1c802b 1402 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1da177e4
LT
1403 struct rt6key *key;
1404
8d1040e8
WW
1405 if (!leaf)
1406 goto backtrack;
1407
1408 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1da177e4 1409
3fc5e044
YH
1410 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1411#ifdef CONFIG_IPV6_SUBTREES
66f5d6ce 1412 if (subtree) {
3e3be275 1413 struct fib6_node *sfn;
6454743b
DA
1414 sfn = fib6_node_lookup_1(subtree,
1415 args + 1);
3e3be275
HFS
1416 if (!sfn)
1417 goto backtrack;
1418 fn = sfn;
1419 }
3fc5e044 1420#endif
3e3be275 1421 if (fn->fn_flags & RTN_RTINFO)
3fc5e044
YH
1422 return fn;
1423 }
1da177e4 1424 }
3e3be275 1425backtrack:
3fc5e044
YH
1426 if (fn->fn_flags & RTN_ROOT)
1427 break;
1428
66f5d6ce 1429 fn = rcu_dereference(fn->parent);
1da177e4
LT
1430 }
1431
1432 return NULL;
1433}
1434
66f5d6ce
WW
1435/* called with rcu_read_lock() held
1436 */
6454743b
DA
1437struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1438 const struct in6_addr *daddr,
1439 const struct in6_addr *saddr)
1da177e4 1440{
1da177e4 1441 struct fib6_node *fn;
825e288e
YH
1442 struct lookup_args args[] = {
1443 {
93c2fb25 1444 .offset = offsetof(struct fib6_info, fib6_dst),
825e288e
YH
1445 .addr = daddr,
1446 },
1da177e4 1447#ifdef CONFIG_IPV6_SUBTREES
825e288e 1448 {
93c2fb25 1449 .offset = offsetof(struct fib6_info, fib6_src),
825e288e
YH
1450 .addr = saddr,
1451 },
1da177e4 1452#endif
825e288e
YH
1453 {
1454 .offset = 0, /* sentinel */
1455 }
1456 };
1da177e4 1457
6454743b 1458 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
507c9b1e 1459 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1da177e4
LT
1460 fn = root;
1461
1462 return fn;
1463}
1464
1465/*
1466 * Get node with specified destination prefix (and source prefix,
1467 * if subtrees are used)
38fbeeee
WW
1468 * exact_match == true means we try to find fn with exact match of
1469 * the passed in prefix addr
1470 * exact_match == false means we try to find fn with longest prefix
1471 * match of the passed in prefix addr. This is useful for finding fn
1472 * for cached route as it will be stored in the exception table under
1473 * the node with longest prefix length.
1da177e4
LT
1474 */
1475
1476
437de07c
WY
1477static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1478 const struct in6_addr *addr,
38fbeeee
WW
1479 int plen, int offset,
1480 bool exact_match)
1da177e4 1481{
38fbeeee 1482 struct fib6_node *fn, *prev = NULL;
1da177e4
LT
1483
1484 for (fn = root; fn ; ) {
8d1c802b 1485 struct fib6_info *leaf = rcu_dereference(fn->leaf);
8d1040e8
WW
1486 struct rt6key *key;
1487
1488 /* This node is being deleted */
1489 if (!leaf) {
1490 if (plen <= fn->fn_bit)
1491 goto out;
1492 else
1493 goto next;
1494 }
1495
1496 key = (struct rt6key *)((u8 *)leaf + offset);
1da177e4
LT
1497
1498 /*
1499 * Prefix match
1500 */
1501 if (plen < fn->fn_bit ||
1502 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
38fbeeee 1503 goto out;
1da177e4
LT
1504
1505 if (plen == fn->fn_bit)
1506 return fn;
1507
38fbeeee
WW
1508 prev = fn;
1509
8d1040e8 1510next:
1da177e4
LT
1511 /*
1512 * We have more bits to go
1513 */
1514 if (addr_bit_set(addr, fn->fn_bit))
66f5d6ce 1515 fn = rcu_dereference(fn->right);
1da177e4 1516 else
66f5d6ce 1517 fn = rcu_dereference(fn->left);
1da177e4 1518 }
38fbeeee
WW
1519out:
1520 if (exact_match)
1521 return NULL;
1522 else
1523 return prev;
1da177e4
LT
1524}
1525
437de07c
WY
1526struct fib6_node *fib6_locate(struct fib6_node *root,
1527 const struct in6_addr *daddr, int dst_len,
38fbeeee
WW
1528 const struct in6_addr *saddr, int src_len,
1529 bool exact_match)
1da177e4
LT
1530{
1531 struct fib6_node *fn;
1532
1533 fn = fib6_locate_1(root, daddr, dst_len,
93c2fb25 1534 offsetof(struct fib6_info, fib6_dst),
38fbeeee 1535 exact_match);
1da177e4
LT
1536
1537#ifdef CONFIG_IPV6_SUBTREES
1538 if (src_len) {
547b792c 1539 WARN_ON(saddr == NULL);
0e80193b
WW
1540 if (fn) {
1541 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1542
1543 if (subtree) {
1544 fn = fib6_locate_1(subtree, saddr, src_len,
93c2fb25 1545 offsetof(struct fib6_info, fib6_src),
38fbeeee 1546 exact_match);
0e80193b
WW
1547 }
1548 }
1da177e4
LT
1549 }
1550#endif
1551
507c9b1e 1552 if (fn && fn->fn_flags & RTN_RTINFO)
1da177e4
LT
1553 return fn;
1554
1555 return NULL;
1556}
1557
1558
1559/*
1560 * Deletion
1561 *
1562 */
1563
8d1c802b 1564static struct fib6_info *fib6_find_prefix(struct net *net,
66f5d6ce
WW
1565 struct fib6_table *table,
1566 struct fib6_node *fn)
1da177e4 1567{
66f5d6ce
WW
1568 struct fib6_node *child_left, *child_right;
1569
507c9b1e 1570 if (fn->fn_flags & RTN_ROOT)
421842ed 1571 return net->ipv6.fib6_null_entry;
1da177e4 1572
507c9b1e 1573 while (fn) {
66f5d6ce
WW
1574 child_left = rcu_dereference_protected(fn->left,
1575 lockdep_is_held(&table->tb6_lock));
1576 child_right = rcu_dereference_protected(fn->right,
1577 lockdep_is_held(&table->tb6_lock));
1578 if (child_left)
1579 return rcu_dereference_protected(child_left->leaf,
1580 lockdep_is_held(&table->tb6_lock));
1581 if (child_right)
1582 return rcu_dereference_protected(child_right->leaf,
1583 lockdep_is_held(&table->tb6_lock));
1da177e4 1584
7fc33165 1585 fn = FIB6_SUBTREE(fn);
1da177e4
LT
1586 }
1587 return NULL;
1588}
1589
1590/*
1591 * Called to trim the tree of intermediate nodes when possible. "fn"
1592 * is the node we want to try and remove.
66f5d6ce 1593 * Need to own table->tb6_lock
1da177e4
LT
1594 */
1595
8ed67789 1596static struct fib6_node *fib6_repair_tree(struct net *net,
66f5d6ce
WW
1597 struct fib6_table *table,
1598 struct fib6_node *fn)
1da177e4
LT
1599{
1600 int children;
1601 int nstate;
66f5d6ce 1602 struct fib6_node *child;
94b2cfe0 1603 struct fib6_walker *w;
1da177e4
LT
1604 int iter = 0;
1605
4512c43e
WW
1606 /* Set fn->leaf to null_entry for root node. */
1607 if (fn->fn_flags & RTN_TL_ROOT) {
421842ed 1608 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
4512c43e
WW
1609 return fn;
1610 }
1611
1da177e4 1612 for (;;) {
66f5d6ce
WW
1613 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1614 lockdep_is_held(&table->tb6_lock));
1615 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1616 lockdep_is_held(&table->tb6_lock));
1617 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1618 lockdep_is_held(&table->tb6_lock));
1619 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1620 lockdep_is_held(&table->tb6_lock));
1621 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1622 lockdep_is_held(&table->tb6_lock));
8d1c802b 1623 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
66f5d6ce 1624 lockdep_is_held(&table->tb6_lock));
8d1c802b 1625 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
66f5d6ce 1626 lockdep_is_held(&table->tb6_lock));
8d1c802b 1627 struct fib6_info *new_fn_leaf;
66f5d6ce 1628
1da177e4
LT
1629 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1630 iter++;
1631
547b792c
IJ
1632 WARN_ON(fn->fn_flags & RTN_RTINFO);
1633 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
66f5d6ce 1634 WARN_ON(fn_leaf);
1da177e4
LT
1635
1636 children = 0;
1637 child = NULL;
66f5d6ce
WW
1638 if (fn_r)
1639 child = fn_r, children |= 1;
1640 if (fn_l)
1641 child = fn_l, children |= 2;
1da177e4 1642
7fc33165 1643 if (children == 3 || FIB6_SUBTREE(fn)
1da177e4
LT
1644#ifdef CONFIG_IPV6_SUBTREES
1645 /* Subtree root (i.e. fn) may have one child */
507c9b1e 1646 || (children && fn->fn_flags & RTN_ROOT)
1da177e4
LT
1647#endif
1648 ) {
66f5d6ce 1649 new_fn_leaf = fib6_find_prefix(net, table, fn);
1da177e4 1650#if RT6_DEBUG >= 2
66f5d6ce
WW
1651 if (!new_fn_leaf) {
1652 WARN_ON(!new_fn_leaf);
421842ed 1653 new_fn_leaf = net->ipv6.fib6_null_entry;
1da177e4
LT
1654 }
1655#endif
93531c67 1656 fib6_info_hold(new_fn_leaf);
66f5d6ce
WW
1657 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1658 return pn;
1da177e4
LT
1659 }
1660
1da177e4 1661#ifdef CONFIG_IPV6_SUBTREES
7fc33165 1662 if (FIB6_SUBTREE(pn) == fn) {
547b792c 1663 WARN_ON(!(fn->fn_flags & RTN_ROOT));
66f5d6ce 1664 RCU_INIT_POINTER(pn->subtree, NULL);
1da177e4
LT
1665 nstate = FWS_L;
1666 } else {
547b792c 1667 WARN_ON(fn->fn_flags & RTN_ROOT);
1da177e4 1668#endif
66f5d6ce
WW
1669 if (pn_r == fn)
1670 rcu_assign_pointer(pn->right, child);
1671 else if (pn_l == fn)
1672 rcu_assign_pointer(pn->left, child);
1da177e4 1673#if RT6_DEBUG >= 2
547b792c
IJ
1674 else
1675 WARN_ON(1);
1da177e4
LT
1676#endif
1677 if (child)
66f5d6ce 1678 rcu_assign_pointer(child->parent, pn);
1da177e4
LT
1679 nstate = FWS_R;
1680#ifdef CONFIG_IPV6_SUBTREES
1681 }
1682#endif
1683
9a03cd8f
MK
1684 read_lock(&net->ipv6.fib6_walker_lock);
1685 FOR_WALKERS(net, w) {
507c9b1e 1686 if (!child) {
2b760fcf 1687 if (w->node == fn) {
1da177e4
LT
1688 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1689 w->node = pn;
1690 w->state = nstate;
1691 }
1692 } else {
1da177e4
LT
1693 if (w->node == fn) {
1694 w->node = child;
1695 if (children&2) {
1696 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
8db46f1d 1697 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1da177e4
LT
1698 } else {
1699 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
8db46f1d 1700 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1da177e4
LT
1701 }
1702 }
1703 }
1704 }
9a03cd8f 1705 read_unlock(&net->ipv6.fib6_walker_lock);
1da177e4 1706
81eb8447 1707 node_free(net, fn);
507c9b1e 1708 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1da177e4
LT
1709 return pn;
1710
66f5d6ce 1711 RCU_INIT_POINTER(pn->leaf, NULL);
93531c67 1712 fib6_info_release(pn_leaf);
1da177e4
LT
1713 fn = pn;
1714 }
1715}
1716
66f5d6ce 1717static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
8d1c802b 1718 struct fib6_info __rcu **rtp, struct nl_info *info)
1da177e4 1719{
94b2cfe0 1720 struct fib6_walker *w;
8d1c802b 1721 struct fib6_info *rt = rcu_dereference_protected(*rtp,
66f5d6ce 1722 lockdep_is_held(&table->tb6_lock));
c572872f 1723 struct net *net = info->nl_net;
1da177e4
LT
1724
1725 RT6_TRACE("fib6_del_route\n");
1726
1727 /* Unlink it */
8fb11a9a 1728 *rtp = rt->fib6_next;
93c2fb25 1729 rt->fib6_node = NULL;
c572872f
BT
1730 net->ipv6.rt6_stats->fib_rt_entries--;
1731 net->ipv6.rt6_stats->fib_discarded_routes++;
1da177e4 1732
2b760fcf
WW
1733 /* Flush all cached dst in exception table */
1734 rt6_flush_exceptions(rt);
1735
f11e6659 1736 /* Reset round-robin state, if necessary */
66f5d6ce 1737 if (rcu_access_pointer(fn->rr_ptr) == rt)
f11e6659
DM
1738 fn->rr_ptr = NULL;
1739
51ebd318 1740 /* Remove this entry from other siblings */
93c2fb25 1741 if (rt->fib6_nsiblings) {
8d1c802b 1742 struct fib6_info *sibling, *next_sibling;
51ebd318
ND
1743
1744 list_for_each_entry_safe(sibling, next_sibling,
93c2fb25
DA
1745 &rt->fib6_siblings, fib6_siblings)
1746 sibling->fib6_nsiblings--;
1747 rt->fib6_nsiblings = 0;
1748 list_del_init(&rt->fib6_siblings);
d7dedee1 1749 rt6_multipath_rebalance(next_sibling);
51ebd318
ND
1750 }
1751
1da177e4 1752 /* Adjust walkers */
9a03cd8f
MK
1753 read_lock(&net->ipv6.fib6_walker_lock);
1754 FOR_WALKERS(net, w) {
1da177e4
LT
1755 if (w->state == FWS_C && w->leaf == rt) {
1756 RT6_TRACE("walker %p adjusted by delroute\n", w);
8fb11a9a 1757 w->leaf = rcu_dereference_protected(rt->fib6_next,
66f5d6ce 1758 lockdep_is_held(&table->tb6_lock));
507c9b1e 1759 if (!w->leaf)
1da177e4
LT
1760 w->state = FWS_U;
1761 }
1762 }
9a03cd8f 1763 read_unlock(&net->ipv6.fib6_walker_lock);
1da177e4 1764
4512c43e
WW
1765 /* If it was last route, call fib6_repair_tree() to:
1766 * 1. For root node, put back null_entry as how the table was created.
1767 * 2. For other nodes, expunge its radix tree node.
1768 */
66f5d6ce 1769 if (!rcu_access_pointer(fn->leaf)) {
4512c43e
WW
1770 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1771 fn->fn_flags &= ~RTN_RTINFO;
1772 net->ipv6.rt6_stats->fib_route_nodes--;
1773 }
66f5d6ce 1774 fn = fib6_repair_tree(net, table, fn);
1da177e4
LT
1775 }
1776
6e9e16e6 1777 fib6_purge_rt(rt, fn, net);
1da177e4 1778
6c31e5a9 1779 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
16a16cd3
DA
1780 if (!info->skip_notify)
1781 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
93531c67 1782 fib6_info_release(rt);
1da177e4
LT
1783}
1784
66f5d6ce 1785/* Need to own table->tb6_lock */
8d1c802b 1786int fib6_del(struct fib6_info *rt, struct nl_info *info)
1da177e4 1787{
93c2fb25
DA
1788 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1789 lockdep_is_held(&rt->fib6_table->tb6_lock));
1790 struct fib6_table *table = rt->fib6_table;
8ed67789 1791 struct net *net = info->nl_net;
8d1c802b
DA
1792 struct fib6_info __rcu **rtp;
1793 struct fib6_info __rcu **rtp_next;
1da177e4 1794
421842ed 1795 if (!fn || rt == net->ipv6.fib6_null_entry)
1da177e4
LT
1796 return -ENOENT;
1797
547b792c 1798 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1da177e4 1799
1da177e4
LT
1800 /*
1801 * Walk the leaf entries looking for ourself
1802 */
1803
66f5d6ce 1804 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
8d1c802b 1805 struct fib6_info *cur = rcu_dereference_protected(*rtp,
66f5d6ce
WW
1806 lockdep_is_held(&table->tb6_lock));
1807 if (rt == cur) {
1808 fib6_del_route(table, fn, rtp, info);
1da177e4
LT
1809 return 0;
1810 }
8fb11a9a 1811 rtp_next = &cur->fib6_next;
1da177e4
LT
1812 }
1813 return -ENOENT;
1814}
1815
1816/*
1817 * Tree traversal function.
1818 *
1819 * Certainly, it is not interrupt safe.
1820 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1821 * It means, that we can modify tree during walking
1822 * and use this function for garbage collection, clone pruning,
1ab1457c 1823 * cleaning tree when a device goes down etc. etc.
1da177e4
LT
1824 *
1825 * It guarantees that every node will be traversed,
1826 * and that it will be traversed only once.
1827 *
1828 * Callback function w->func may return:
1829 * 0 -> continue walking.
1830 * positive value -> walking is suspended (used by tree dumps,
1831 * and probably by gc, if it will be split to several slices)
1832 * negative value -> terminate walking.
1833 *
1834 * The function itself returns:
1835 * 0 -> walk is complete.
1836 * >0 -> walk is incomplete (i.e. suspended)
1837 * <0 -> walk is terminated by an error.
66f5d6ce
WW
1838 *
1839 * This function is called with tb6_lock held.
1da177e4
LT
1840 */
1841
94b2cfe0 1842static int fib6_walk_continue(struct fib6_walker *w)
1da177e4 1843{
66f5d6ce 1844 struct fib6_node *fn, *pn, *left, *right;
1da177e4 1845
2b760fcf
WW
1846 /* w->root should always be table->tb6_root */
1847 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1848
1da177e4
LT
1849 for (;;) {
1850 fn = w->node;
507c9b1e 1851 if (!fn)
1da177e4
LT
1852 return 0;
1853
1da177e4
LT
1854 switch (w->state) {
1855#ifdef CONFIG_IPV6_SUBTREES
1856 case FWS_S:
7fc33165
YH
1857 if (FIB6_SUBTREE(fn)) {
1858 w->node = FIB6_SUBTREE(fn);
1da177e4
LT
1859 continue;
1860 }
1861 w->state = FWS_L;
1ab1457c 1862#endif
275757e6 1863 /* fall through */
1da177e4 1864 case FWS_L:
66f5d6ce
WW
1865 left = rcu_dereference_protected(fn->left, 1);
1866 if (left) {
1867 w->node = left;
1da177e4
LT
1868 w->state = FWS_INIT;
1869 continue;
1870 }
1871 w->state = FWS_R;
275757e6 1872 /* fall through */
1da177e4 1873 case FWS_R:
66f5d6ce
WW
1874 right = rcu_dereference_protected(fn->right, 1);
1875 if (right) {
1876 w->node = right;
1da177e4
LT
1877 w->state = FWS_INIT;
1878 continue;
1879 }
1880 w->state = FWS_C;
66f5d6ce 1881 w->leaf = rcu_dereference_protected(fn->leaf, 1);
275757e6 1882 /* fall through */
1da177e4 1883 case FWS_C:
507c9b1e 1884 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
2bec5a36
PM
1885 int err;
1886
fa809e2f
ED
1887 if (w->skip) {
1888 w->skip--;
1c265854 1889 goto skip;
2bec5a36
PM
1890 }
1891
1892 err = w->func(w);
1da177e4
LT
1893 if (err)
1894 return err;
2bec5a36
PM
1895
1896 w->count++;
1da177e4
LT
1897 continue;
1898 }
1c265854 1899skip:
1da177e4 1900 w->state = FWS_U;
275757e6 1901 /* fall through */
1da177e4
LT
1902 case FWS_U:
1903 if (fn == w->root)
1904 return 0;
66f5d6ce
WW
1905 pn = rcu_dereference_protected(fn->parent, 1);
1906 left = rcu_dereference_protected(pn->left, 1);
1907 right = rcu_dereference_protected(pn->right, 1);
1da177e4
LT
1908 w->node = pn;
1909#ifdef CONFIG_IPV6_SUBTREES
7fc33165 1910 if (FIB6_SUBTREE(pn) == fn) {
547b792c 1911 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1da177e4
LT
1912 w->state = FWS_L;
1913 continue;
1914 }
1915#endif
66f5d6ce 1916 if (left == fn) {
1da177e4
LT
1917 w->state = FWS_R;
1918 continue;
1919 }
66f5d6ce 1920 if (right == fn) {
1da177e4 1921 w->state = FWS_C;
66f5d6ce 1922 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1da177e4
LT
1923 continue;
1924 }
1925#if RT6_DEBUG >= 2
547b792c 1926 WARN_ON(1);
1da177e4
LT
1927#endif
1928 }
1929 }
1930}
1931
9a03cd8f 1932static int fib6_walk(struct net *net, struct fib6_walker *w)
1da177e4
LT
1933{
1934 int res;
1935
1936 w->state = FWS_INIT;
1937 w->node = w->root;
1938
9a03cd8f 1939 fib6_walker_link(net, w);
1da177e4
LT
1940 res = fib6_walk_continue(w);
1941 if (res <= 0)
9a03cd8f 1942 fib6_walker_unlink(net, w);
1da177e4
LT
1943 return res;
1944}
1945
94b2cfe0 1946static int fib6_clean_node(struct fib6_walker *w)
1da177e4
LT
1947{
1948 int res;
8d1c802b 1949 struct fib6_info *rt;
94b2cfe0 1950 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
ec7d43c2
BT
1951 struct nl_info info = {
1952 .nl_net = c->net,
1953 };
1da177e4 1954
327571cb
HFS
1955 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1956 w->node->fn_sernum != c->sernum)
1957 w->node->fn_sernum = c->sernum;
1958
1959 if (!c->func) {
1960 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1961 w->leaf = NULL;
1962 return 0;
1963 }
1964
66f5d6ce 1965 for_each_fib6_walker_rt(w) {
1da177e4 1966 res = c->func(rt, c->arg);
b5cb5a75 1967 if (res == -1) {
1da177e4 1968 w->leaf = rt;
528c4ceb 1969 res = fib6_del(rt, &info);
1da177e4
LT
1970 if (res) {
1971#if RT6_DEBUG >= 2
91df42be 1972 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
4e587ea7 1973 __func__, rt,
93c2fb25 1974 rcu_access_pointer(rt->fib6_node),
4e587ea7 1975 res);
1da177e4
LT
1976#endif
1977 continue;
1978 }
1979 return 0;
b5cb5a75 1980 } else if (res == -2) {
93c2fb25 1981 if (WARN_ON(!rt->fib6_nsiblings))
b5cb5a75 1982 continue;
93c2fb25
DA
1983 rt = list_last_entry(&rt->fib6_siblings,
1984 struct fib6_info, fib6_siblings);
b5cb5a75 1985 continue;
1da177e4 1986 }
547b792c 1987 WARN_ON(res != 0);
1da177e4
LT
1988 }
1989 w->leaf = rt;
1990 return 0;
1991}
1992
1993/*
1994 * Convenient frontend to tree walker.
1ab1457c 1995 *
1da177e4 1996 * func is called on each route.
b5cb5a75
IS
1997 * It may return -2 -> skip multipath route.
1998 * -1 -> delete this route.
1da177e4 1999 * 0 -> continue walking
1da177e4
LT
2000 */
2001
ec7d43c2 2002static void fib6_clean_tree(struct net *net, struct fib6_node *root,
8d1c802b 2003 int (*func)(struct fib6_info *, void *arg),
2b760fcf 2004 int sernum, void *arg)
1da177e4 2005{
94b2cfe0 2006 struct fib6_cleaner c;
1da177e4
LT
2007
2008 c.w.root = root;
2009 c.w.func = fib6_clean_node;
2bec5a36
PM
2010 c.w.count = 0;
2011 c.w.skip = 0;
1da177e4 2012 c.func = func;
327571cb 2013 c.sernum = sernum;
1da177e4 2014 c.arg = arg;
ec7d43c2 2015 c.net = net;
1da177e4 2016
9a03cd8f 2017 fib6_walk(net, &c.w);
1da177e4
LT
2018}
2019
327571cb 2020static void __fib6_clean_all(struct net *net,
8d1c802b 2021 int (*func)(struct fib6_info *, void *),
327571cb 2022 int sernum, void *arg)
c71099ac 2023{
c71099ac 2024 struct fib6_table *table;
58f09b78 2025 struct hlist_head *head;
1b43af54 2026 unsigned int h;
c71099ac 2027
1b43af54 2028 rcu_read_lock();
a33bc5c1 2029 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
f3db4851 2030 head = &net->ipv6.fib_table_hash[h];
b67bfe0d 2031 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
66f5d6ce 2032 spin_lock_bh(&table->tb6_lock);
ec7d43c2 2033 fib6_clean_tree(net, &table->tb6_root,
2b760fcf 2034 func, sernum, arg);
66f5d6ce 2035 spin_unlock_bh(&table->tb6_lock);
c71099ac
TG
2036 }
2037 }
1b43af54 2038 rcu_read_unlock();
c71099ac
TG
2039}
2040
8d1c802b 2041void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
327571cb
HFS
2042 void *arg)
2043{
2044 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2045}
2046
705f1c86
HFS
2047static void fib6_flush_trees(struct net *net)
2048{
812918c4 2049 int new_sernum = fib6_new_sernum(net);
705f1c86 2050
327571cb 2051 __fib6_clean_all(net, NULL, new_sernum, NULL);
705f1c86
HFS
2052}
2053
1da177e4
LT
2054/*
2055 * Garbage collection
2056 */
2057
8d1c802b 2058static int fib6_age(struct fib6_info *rt, void *arg)
1da177e4 2059{
3570df91 2060 struct fib6_gc_args *gc_args = arg;
1da177e4
LT
2061 unsigned long now = jiffies;
2062
2063 /*
2064 * check addrconf expiration here.
2065 * Routes are expired even if they are in use.
1da177e4
LT
2066 */
2067
93c2fb25 2068 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
14895687 2069 if (time_after(now, rt->expires)) {
1da177e4 2070 RT6_TRACE("expiring %p\n", rt);
1da177e4
LT
2071 return -1;
2072 }
3570df91 2073 gc_args->more++;
1da177e4
LT
2074 }
2075
c757faa8
WW
2076 /* Also age clones in the exception table.
2077 * Note, that clones are aged out
2078 * only if they are not in use now.
2079 */
2080 rt6_age_exceptions(rt, gc_args, now);
2081
1da177e4
LT
2082 return 0;
2083}
2084
2ac3ac8f 2085void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1da177e4 2086{
3570df91 2087 struct fib6_gc_args gc_args;
49a18d86
MK
2088 unsigned long now;
2089
2ac3ac8f 2090 if (force) {
3dc94f93
MK
2091 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2092 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2ac3ac8f
MK
2093 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2094 return;
1da177e4 2095 }
2ac3ac8f
MK
2096 gc_args.timeout = expires ? (int)expires :
2097 net->ipv6.sysctl.ip6_rt_gc_interval;
db916649 2098 gc_args.more = 0;
f3db4851 2099
3570df91 2100 fib6_clean_all(net, fib6_age, &gc_args);
49a18d86
MK
2101 now = jiffies;
2102 net->ipv6.ip6_rt_last_gc = now;
1da177e4
LT
2103
2104 if (gc_args.more)
c8a45222 2105 mod_timer(&net->ipv6.ip6_fib_timer,
49a18d86 2106 round_jiffies(now
c8a45222 2107 + net->ipv6.sysctl.ip6_rt_gc_interval));
417f28bb
SH
2108 else
2109 del_timer(&net->ipv6.ip6_fib_timer);
3dc94f93 2110 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
1da177e4
LT
2111}
2112
86cb30ec 2113static void fib6_gc_timer_cb(struct timer_list *t)
5b7c931d 2114{
86cb30ec
KC
2115 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2116
2117 fib6_run_gc(0, arg, true);
5b7c931d
DL
2118}
2119
2c8c1e72 2120static int __net_init fib6_net_init(struct net *net)
1da177e4 2121{
10da66f7 2122 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
16ab6d7d
IS
2123 int err;
2124
2125 err = fib6_notifier_init(net);
2126 if (err)
2127 return err;
10da66f7 2128
3dc94f93 2129 spin_lock_init(&net->ipv6.fib6_gc_lock);
9a03cd8f
MK
2130 rwlock_init(&net->ipv6.fib6_walker_lock);
2131 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
86cb30ec 2132 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
63152fc0 2133
c572872f
BT
2134 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2135 if (!net->ipv6.rt6_stats)
2136 goto out_timer;
2137
10da66f7
ED
2138 /* Avoid false sharing : Use at least a full cache line */
2139 size = max_t(size_t, size, L1_CACHE_BYTES);
2140
2141 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
58f09b78 2142 if (!net->ipv6.fib_table_hash)
c572872f 2143 goto out_rt6_stats;
e0b85590 2144
58f09b78
DL
2145 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2146 GFP_KERNEL);
2147 if (!net->ipv6.fib6_main_tbl)
e0b85590
DL
2148 goto out_fib_table_hash;
2149
58f09b78 2150 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
66f5d6ce 2151 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
421842ed 2152 net->ipv6.fib6_null_entry);
58f09b78
DL
2153 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2154 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
8e773277 2155 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
e0b85590
DL
2156
2157#ifdef CONFIG_IPV6_MULTIPLE_TABLES
58f09b78
DL
2158 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2159 GFP_KERNEL);
2160 if (!net->ipv6.fib6_local_tbl)
e0b85590 2161 goto out_fib6_main_tbl;
58f09b78 2162 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
66f5d6ce 2163 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
421842ed 2164 net->ipv6.fib6_null_entry);
58f09b78
DL
2165 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2166 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
8e773277 2167 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
e0b85590 2168#endif
58f09b78 2169 fib6_tables_init(net);
f845ab6b 2170
417f28bb 2171 return 0;
d63bddbe 2172
e0b85590 2173#ifdef CONFIG_IPV6_MULTIPLE_TABLES
e0b85590 2174out_fib6_main_tbl:
58f09b78 2175 kfree(net->ipv6.fib6_main_tbl);
e0b85590 2176#endif
e0b85590 2177out_fib_table_hash:
58f09b78 2178 kfree(net->ipv6.fib_table_hash);
c572872f
BT
2179out_rt6_stats:
2180 kfree(net->ipv6.rt6_stats);
63152fc0 2181out_timer:
16ab6d7d 2182 fib6_notifier_exit(net);
417f28bb 2183 return -ENOMEM;
8db46f1d 2184}
58f09b78
DL
2185
2186static void fib6_net_exit(struct net *net)
2187{
ba1cc08d
SD
2188 unsigned int i;
2189
417f28bb
SH
2190 del_timer_sync(&net->ipv6.ip6_fib_timer);
2191
32a805ba 2192 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
ba1cc08d
SD
2193 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2194 struct hlist_node *tmp;
2195 struct fib6_table *tb;
2196
2197 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2198 hlist_del(&tb->tb6_hlist);
2199 fib6_free_table(tb);
2200 }
2201 }
2202
58f09b78 2203 kfree(net->ipv6.fib_table_hash);
c572872f 2204 kfree(net->ipv6.rt6_stats);
16ab6d7d 2205 fib6_notifier_exit(net);
58f09b78
DL
2206}
2207
2208static struct pernet_operations fib6_net_ops = {
2209 .init = fib6_net_init,
2210 .exit = fib6_net_exit,
2211};
2212
2213int __init fib6_init(void)
2214{
2215 int ret = -ENOMEM;
63152fc0 2216
58f09b78
DL
2217 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2218 sizeof(struct fib6_node),
2219 0, SLAB_HWCACHE_ALIGN,
2220 NULL);
2221 if (!fib6_node_kmem)
2222 goto out;
2223
2224 ret = register_pernet_subsys(&fib6_net_ops);
2225 if (ret)
c572872f 2226 goto out_kmem_cache_create;
e8803b6c 2227
16feebcf
FW
2228 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2229 inet6_dump_fib, 0);
e8803b6c
DM
2230 if (ret)
2231 goto out_unregister_subsys;
705f1c86
HFS
2232
2233 __fib6_flush_trees = fib6_flush_trees;
58f09b78
DL
2234out:
2235 return ret;
2236
e8803b6c
DM
2237out_unregister_subsys:
2238 unregister_pernet_subsys(&fib6_net_ops);
d63bddbe
DL
2239out_kmem_cache_create:
2240 kmem_cache_destroy(fib6_node_kmem);
2241 goto out;
1da177e4
LT
2242}
2243
2244void fib6_gc_cleanup(void)
2245{
58f09b78 2246 unregister_pernet_subsys(&fib6_net_ops);
1da177e4
LT
2247 kmem_cache_destroy(fib6_node_kmem);
2248}
8d2ca1d7
HFS
2249
2250#ifdef CONFIG_PROC_FS
8d2ca1d7
HFS
2251static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2252{
8d1c802b 2253 struct fib6_info *rt = v;
8d2ca1d7 2254 struct ipv6_route_iter *iter = seq->private;
5e670d84 2255 const struct net_device *dev;
8d2ca1d7 2256
93c2fb25 2257 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
8d2ca1d7
HFS
2258
2259#ifdef CONFIG_IPV6_SUBTREES
93c2fb25 2260 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
8d2ca1d7
HFS
2261#else
2262 seq_puts(seq, "00000000000000000000000000000000 00 ");
2263#endif
93c2fb25 2264 if (rt->fib6_flags & RTF_GATEWAY)
5e670d84 2265 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
8d2ca1d7
HFS
2266 else
2267 seq_puts(seq, "00000000000000000000000000000000");
2268
5e670d84 2269 dev = rt->fib6_nh.nh_dev;
8d2ca1d7 2270 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
93c2fb25
DA
2271 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2272 rt->fib6_flags, dev ? dev->name : "");
8d2ca1d7
HFS
2273 iter->w.leaf = NULL;
2274 return 0;
2275}
2276
94b2cfe0 2277static int ipv6_route_yield(struct fib6_walker *w)
8d2ca1d7
HFS
2278{
2279 struct ipv6_route_iter *iter = w->args;
2280
2281 if (!iter->skip)
2282 return 1;
2283
2284 do {
66f5d6ce 2285 iter->w.leaf = rcu_dereference_protected(
8fb11a9a 2286 iter->w.leaf->fib6_next,
66f5d6ce 2287 lockdep_is_held(&iter->tbl->tb6_lock));
8d2ca1d7
HFS
2288 iter->skip--;
2289 if (!iter->skip && iter->w.leaf)
2290 return 1;
2291 } while (iter->w.leaf);
2292
2293 return 0;
2294}
2295
9a03cd8f
MK
2296static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2297 struct net *net)
8d2ca1d7
HFS
2298{
2299 memset(&iter->w, 0, sizeof(iter->w));
2300 iter->w.func = ipv6_route_yield;
2301 iter->w.root = &iter->tbl->tb6_root;
2302 iter->w.state = FWS_INIT;
2303 iter->w.node = iter->w.root;
2304 iter->w.args = iter;
0a67d3ef 2305 iter->sernum = iter->w.root->fn_sernum;
8d2ca1d7 2306 INIT_LIST_HEAD(&iter->w.lh);
9a03cd8f 2307 fib6_walker_link(net, &iter->w);
8d2ca1d7
HFS
2308}
2309
2310static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2311 struct net *net)
2312{
2313 unsigned int h;
2314 struct hlist_node *node;
2315
2316 if (tbl) {
2317 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2318 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2319 } else {
2320 h = 0;
2321 node = NULL;
2322 }
2323
2324 while (!node && h < FIB6_TABLE_HASHSZ) {
2325 node = rcu_dereference_bh(
2326 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2327 }
2328 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2329}
2330
0a67d3ef
HFS
2331static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2332{
2333 if (iter->sernum != iter->w.root->fn_sernum) {
2334 iter->sernum = iter->w.root->fn_sernum;
2335 iter->w.state = FWS_INIT;
2336 iter->w.node = iter->w.root;
2337 WARN_ON(iter->w.skip);
2338 iter->w.skip = iter->w.count;
2339 }
2340}
2341
8d2ca1d7
HFS
2342static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2343{
2344 int r;
8d1c802b 2345 struct fib6_info *n;
8d2ca1d7
HFS
2346 struct net *net = seq_file_net(seq);
2347 struct ipv6_route_iter *iter = seq->private;
2348
2349 if (!v)
2350 goto iter_table;
2351
8fb11a9a 2352 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
8d2ca1d7
HFS
2353 if (n) {
2354 ++*pos;
2355 return n;
2356 }
2357
2358iter_table:
0a67d3ef 2359 ipv6_route_check_sernum(iter);
66f5d6ce 2360 spin_lock_bh(&iter->tbl->tb6_lock);
8d2ca1d7 2361 r = fib6_walk_continue(&iter->w);
66f5d6ce 2362 spin_unlock_bh(&iter->tbl->tb6_lock);
8d2ca1d7
HFS
2363 if (r > 0) {
2364 if (v)
2365 ++*pos;
2366 return iter->w.leaf;
2367 } else if (r < 0) {
9a03cd8f 2368 fib6_walker_unlink(net, &iter->w);
8d2ca1d7
HFS
2369 return NULL;
2370 }
9a03cd8f 2371 fib6_walker_unlink(net, &iter->w);
8d2ca1d7
HFS
2372
2373 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2374 if (!iter->tbl)
2375 return NULL;
2376
9a03cd8f 2377 ipv6_route_seq_setup_walk(iter, net);
8d2ca1d7
HFS
2378 goto iter_table;
2379}
2380
2381static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2382 __acquires(RCU_BH)
2383{
2384 struct net *net = seq_file_net(seq);
2385 struct ipv6_route_iter *iter = seq->private;
2386
2387 rcu_read_lock_bh();
2388 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2389 iter->skip = *pos;
2390
2391 if (iter->tbl) {
9a03cd8f 2392 ipv6_route_seq_setup_walk(iter, net);
8d2ca1d7
HFS
2393 return ipv6_route_seq_next(seq, NULL, pos);
2394 } else {
2395 return NULL;
2396 }
2397}
2398
2399static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2400{
94b2cfe0 2401 struct fib6_walker *w = &iter->w;
8d2ca1d7
HFS
2402 return w->node && !(w->state == FWS_U && w->node == w->root);
2403}
2404
2405static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2406 __releases(RCU_BH)
2407{
9a03cd8f 2408 struct net *net = seq_file_net(seq);
8d2ca1d7
HFS
2409 struct ipv6_route_iter *iter = seq->private;
2410
2411 if (ipv6_route_iter_active(iter))
9a03cd8f 2412 fib6_walker_unlink(net, &iter->w);
8d2ca1d7
HFS
2413
2414 rcu_read_unlock_bh();
2415}
2416
c3506372 2417const struct seq_operations ipv6_route_seq_ops = {
8d2ca1d7
HFS
2418 .start = ipv6_route_seq_start,
2419 .next = ipv6_route_seq_next,
2420 .stop = ipv6_route_seq_stop,
2421 .show = ipv6_route_seq_show
2422};
8d2ca1d7 2423#endif /* CONFIG_PROC_FS */