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