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