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