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