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