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