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