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