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