blob: 332f1e0ff8e2c8363151cb7d15dad7ddc4eb3045 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
35
36#include <net/ip6_fib.h>
37#include <net/ip6_route.h>
38
39#define RT6_DEBUG 2
40
41#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000042#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define RT6_TRACE(x...) do { ; } while (0)
45#endif
46
Wang Yufen437de072014-03-28 12:07:04 +080047static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020049struct fib6_cleaner {
50 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080051 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int (*func)(struct rt6_info *, void *arg);
53 void *arg;
54};
55
Adrian Bunk90d41122006-08-14 23:49:16 -070056static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#ifdef CONFIG_IPV6_SUBTREES
59#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
Duan Jiong163cd4e2014-05-09 13:31:43 +080064static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080065static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
66static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020067static int fib6_walk(struct fib6_walker *w);
68static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * A routing update causes an increase of the serial number on the
72 * affected subtree. This allows for cached routes to be asynchronously
73 * tested when modifications are made to the destination cache as a
74 * result of redirects, path MTU changes, etc.
75 */
76
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020077static atomic_t rt_sernum = ATOMIC_INIT(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080079static void fib6_gc_timer_cb(unsigned long arg);
80
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000081static LIST_HEAD(fib6_walkers);
82#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020084static void fib6_walker_link(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070085{
86 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000087 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -070088 write_unlock_bh(&fib6_walker_lock);
89}
90
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020091static void fib6_walker_unlink(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070092{
93 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000094 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -070095 write_unlock_bh(&fib6_walker_lock);
96}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020097
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020098static int fib6_new_sernum(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200100 int new, old;
101
102 do {
103 old = atomic_read(&rt_sernum);
104 new = old < INT_MAX ? old + 1 : 1;
105 } while (atomic_cmpxchg(&rt_sernum, old, new) != old);
106 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * Auxiliary address test functions for the radix tree.
111 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900112 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * 64bit processors)
114 */
115
116/*
117 * test bit
118 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000119#if defined(__LITTLE_ENDIAN)
120# define BITOP_BE32_SWIZZLE (0x1F & ~7)
121#else
122# define BITOP_BE32_SWIZZLE 0
123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200125static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000127 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000128 /*
129 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800130 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000131 * is optimized version of
132 * htonl(1 << ((~fn_bit)&0x1F))
133 * See include/asm-generic/bitops/le.h.
134 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700135 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
136 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200139static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct fib6_node *fn;
142
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800143 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return fn;
146}
147
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200148static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 kmem_cache_free(fib6_node_kmem, fn);
151}
152
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200153static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700156 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800159static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700160{
161 unsigned int h;
162
Thomas Graf375216a2006-10-21 20:20:54 -0700163 /*
164 * Initialize table lock at a single place to give lockdep a key,
165 * tables aren't visible prior to being linked to the list.
166 */
167 rwlock_init(&tb->tb6_lock);
168
Neil Hormana33bc5c2009-07-30 18:52:15 -0700169 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700170
171 /*
172 * No protection necessary, this is the only list mutatation
173 * operation, tables never disappear once they exist.
174 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800175 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700176}
177
178#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800179
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800180static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700181{
182 struct fib6_table *table;
183
184 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500185 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700186 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800187 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700188 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700189 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700190 }
191
192 return table;
193}
194
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800195struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700196{
197 struct fib6_table *tb;
198
199 if (id == 0)
200 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800201 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700202 if (tb)
203 return tb;
204
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800205 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500206 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800207 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700208
209 return tb;
210}
211
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800212struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700213{
214 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800215 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700216 unsigned int h;
217
218 if (id == 0)
219 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700220 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800222 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800223 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700224 if (tb->tb6_id == id) {
225 rcu_read_unlock();
226 return tb;
227 }
228 }
229 rcu_read_unlock();
230
231 return NULL;
232}
233
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000234static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700235{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800236 fib6_link_table(net, net->ipv6.fib6_main_tbl);
237 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700238}
Thomas Grafc71099a2006-08-04 23:20:06 -0700239#else
240
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800241struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700242{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800243 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700244}
245
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800246struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700247{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800248 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700249}
250
David S. Miller4c9483b2011-03-12 16:22:43 -0500251struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800252 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700253{
David S. Miller4c9483b2011-03-12 16:22:43 -0500254 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700255}
256
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000257static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700258{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800259 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700260}
261
262#endif
263
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200264static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700265{
266 int res;
267 struct rt6_info *rt;
268
Changli Gaod8d1f302010-06-10 23:31:35 -0700269 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700270 res = rt6_dump_route(rt, w->args);
271 if (res < 0) {
272 /* Frame is full, suspend walking */
273 w->leaf = rt;
274 return 1;
275 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700276 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700277 }
278 w->leaf = NULL;
279 return 0;
280}
281
282static void fib6_dump_end(struct netlink_callback *cb)
283{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200284 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700285
286 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800287 if (cb->args[4]) {
288 cb->args[4] = 0;
289 fib6_walker_unlink(w);
290 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700291 cb->args[2] = 0;
292 kfree(w);
293 }
Wang Yufen437de072014-03-28 12:07:04 +0800294 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700295 cb->args[1] = 3;
296}
297
298static int fib6_dump_done(struct netlink_callback *cb)
299{
300 fib6_dump_end(cb);
301 return cb->done ? cb->done(cb) : 0;
302}
303
304static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
305 struct netlink_callback *cb)
306{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200307 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700308 int res;
309
310 w = (void *)cb->args[2];
311 w->root = &table->tb6_root;
312
313 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000314 w->count = 0;
315 w->skip = 0;
316
Patrick McHardy1b43af52006-08-10 23:11:17 -0700317 read_lock_bh(&table->tb6_lock);
318 res = fib6_walk(w);
319 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000320 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700321 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000322 cb->args[5] = w->root->fn_sernum;
323 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700324 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000325 if (cb->args[5] != w->root->fn_sernum) {
326 /* Begin at the root if the tree changed */
327 cb->args[5] = w->root->fn_sernum;
328 w->state = FWS_INIT;
329 w->node = w->root;
330 w->skip = w->count;
331 } else
332 w->skip = 0;
333
Patrick McHardy1b43af52006-08-10 23:11:17 -0700334 read_lock_bh(&table->tb6_lock);
335 res = fib6_walk_continue(w);
336 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800337 if (res <= 0) {
338 fib6_walker_unlink(w);
339 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700340 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700341 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800342
Patrick McHardy1b43af52006-08-10 23:11:17 -0700343 return res;
344}
345
Thomas Grafc127ea22007-03-22 11:58:32 -0700346static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700347{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900348 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700349 unsigned int h, s_h;
350 unsigned int e = 0, s_e;
351 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200352 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700353 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800354 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700355 int res = 0;
356
357 s_h = cb->args[0];
358 s_e = cb->args[1];
359
360 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500361 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700362 /* New dump:
363 *
364 * 1. hook callback destructor.
365 */
366 cb->args[3] = (long)cb->done;
367 cb->done = fib6_dump_done;
368
369 /*
370 * 2. allocate and initialize walker.
371 */
372 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500373 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700374 return -ENOMEM;
375 w->func = fib6_dump_node;
376 cb->args[2] = (long)w;
377 }
378
379 arg.skb = skb;
380 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700381 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700382 w->args = &arg;
383
Eric Dumazete67f88d2011-04-27 22:56:07 +0000384 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700385 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700386 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800387 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800388 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700389 if (e < s_e)
390 goto next;
391 res = fib6_dump_table(tb, skb, cb);
392 if (res != 0)
393 goto out;
394next:
395 e++;
396 }
397 }
398out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000399 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700400 cb->args[1] = e;
401 cb->args[0] = h;
402
403 res = res < 0 ? res : skb->len;
404 if (res <= 0)
405 fib6_dump_end(cb);
406 return res;
407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/*
410 * Routing Table
411 *
412 * return the appropriate node for a routing tree "add" operation
413 * by either creating and inserting or by returning an existing
414 * node.
415 */
416
fan.du9225b232013-07-22 14:21:09 +0800417static struct fib6_node *fib6_add_1(struct fib6_node *root,
418 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000419 int offset, int allow_create,
420 int replace_required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct fib6_node *fn, *in, *ln;
423 struct fib6_node *pn = NULL;
424 struct rt6key *key;
425 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900426 __be32 dir = 0;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200427 int sernum = fib6_new_sernum();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 RT6_TRACE("fib6_add_1\n");
430
431 /* insert node in tree */
432
433 fn = root;
434
435 do {
436 key = (struct rt6key *)((u8 *)fn->leaf + offset);
437
438 /*
439 * Prefix match
440 */
441 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000442 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000443 if (!allow_create) {
444 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000445 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000446 return ERR_PTR(-ENOENT);
447 }
Joe Perchesf3213832012-05-15 14:11:53 +0000448 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000451 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * Exact match ?
455 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (plen == fn->fn_bit) {
458 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500459 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 rt6_release(fn->leaf);
461 fn->leaf = NULL;
462 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return fn;
467 }
468
469 /*
470 * We have more bits to go
471 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* Try to walk down on tree. */
474 fn->fn_sernum = sernum;
475 dir = addr_bit_set(addr, fn->fn_bit);
476 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800477 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } while (fn);
479
Matti Vaittinen14df0152011-11-16 21:18:02 +0000480 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000481 /* We should not create new node because
482 * NLM_F_REPLACE was specified without NLM_F_CREATE
483 * I assume it is safe to require NLM_F_CREATE when
484 * REPLACE flag is used! Later we may want to remove the
485 * check for replace_required, because according
486 * to netlink specification, NLM_F_CREATE
487 * MUST be specified if new route is created.
488 * That would keep IPv6 consistent with IPv4
489 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000490 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000491 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000492 return ERR_PTR(-ENOENT);
493 }
Joe Perchesf3213832012-05-15 14:11:53 +0000494 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
497 * We walked to the bottom of tree.
498 * Create new leaf node without children.
499 */
500
501 ln = node_alloc();
502
David S. Miller507c9b12011-12-03 17:50:45 -0500503 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000504 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ln->parent = pn;
508 ln->fn_sernum = sernum;
509
510 if (dir)
511 pn->right = ln;
512 else
513 pn->left = ln;
514
515 return ln;
516
517
518insert_above:
519 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900520 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * we have a less significant route.
522 * we've to insert an intermediate node on the list
523 * this new node will point to the one we need to create
524 * and the current
525 */
526
527 pn = fn->parent;
528
529 /* find 1st bit in difference between the 2 addrs.
530
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800531 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 but if it is >= plen, the value is ignored in any case.
533 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900534
fan.du9225b232013-07-22 14:21:09 +0800535 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900537 /*
538 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * / \
540 * (new leaf node)[ln] (old node)[fn]
541 */
542 if (plen > bit) {
543 in = node_alloc();
544 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900545
David S. Miller507c9b12011-12-03 17:50:45 -0500546 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (in)
548 node_free(in);
549 if (ln)
550 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000551 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900554 /*
555 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * RTN_RTINFO will
557 * be off since that an address that chooses one of
558 * the branches would not match less specific routes
559 * in the other branch
560 */
561
562 in->fn_bit = bit;
563
564 in->parent = pn;
565 in->leaf = fn->leaf;
566 atomic_inc(&in->leaf->rt6i_ref);
567
568 in->fn_sernum = sernum;
569
570 /* update parent pointer */
571 if (dir)
572 pn->right = in;
573 else
574 pn->left = in;
575
576 ln->fn_bit = plen;
577
578 ln->parent = in;
579 fn->parent = in;
580
581 ln->fn_sernum = sernum;
582
583 if (addr_bit_set(addr, bit)) {
584 in->right = ln;
585 in->left = fn;
586 } else {
587 in->left = ln;
588 in->right = fn;
589 }
590 } else { /* plen <= bit */
591
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900592 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * (new leaf node)[ln]
594 * / \
595 * (old node)[fn] NULL
596 */
597
598 ln = node_alloc();
599
David S. Miller507c9b12011-12-03 17:50:45 -0500600 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000601 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 ln->fn_bit = plen;
604
605 ln->parent = pn;
606
607 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (dir)
610 pn->right = ln;
611 else
612 pn->left = ln;
613
614 if (addr_bit_set(&key->addr, plen))
615 ln->right = fn;
616 else
617 ln->left = fn;
618
619 fn->parent = ln;
620 }
621 return ln;
622}
623
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200624static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200625{
626 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
627 RTF_GATEWAY;
628}
629
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100630static int fib6_commit_metrics(struct dst_entry *dst,
631 struct nlattr *mx, int mx_len)
632{
633 struct nlattr *nla;
634 int remaining;
635 u32 *mp;
636
637 if (dst->flags & DST_HOST) {
638 mp = dst_metrics_write_ptr(dst);
639 } else {
Benjamin Block793c3b42014-08-21 19:37:48 +0200640 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100641 if (!mp)
642 return -ENOMEM;
643 dst_init_metrics(dst, mp, 0);
644 }
645
646 nla_for_each_attr(nla, mx, mx_len, remaining) {
647 int type = nla_type(nla);
648
649 if (type) {
650 if (type > RTAX_MAX)
651 return -EINVAL;
652
653 mp[type - 1] = nla_get_u32(nla);
654 }
655 }
656 return 0;
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * Insert routing information in a node.
661 */
662
663static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100664 struct nl_info *info, struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct rt6_info *iter = NULL;
667 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500668 int replace = (info->nlh &&
669 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
670 int add = (!info->nlh ||
671 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000672 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200673 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100674 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 ins = &fn->leaf;
677
David S. Miller507c9b12011-12-03 17:50:45 -0500678 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * Search for duplicates
681 */
682
683 if (iter->rt6i_metric == rt->rt6i_metric) {
684 /*
685 * Same priority level
686 */
David S. Miller507c9b12011-12-03 17:50:45 -0500687 if (info->nlh &&
688 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000689 return -EEXIST;
690 if (replace) {
691 found++;
692 break;
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David S. Millerd1918542011-12-28 20:19:20 -0500695 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 iter->rt6i_idev == rt->rt6i_idev &&
697 ipv6_addr_equal(&iter->rt6i_gateway,
698 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000699 if (rt->rt6i_nsiblings)
700 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500701 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000703 if (!(rt->rt6i_flags & RTF_EXPIRES))
704 rt6_clean_expires(iter);
705 else
706 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EEXIST;
708 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000709 /* If we have the same destination and the same metric,
710 * but not the same gateway, then the route we try to
711 * add is sibling to this route, increment our counter
712 * of siblings, and later we will add our route to the
713 * list.
714 * Only static routes (which don't have flag
715 * RTF_EXPIRES) are used for ECMPv6.
716 *
717 * To avoid long list, we only had siblings if the
718 * route have a gateway.
719 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200720 if (rt_can_ecmp &&
721 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000722 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 if (iter->rt6i_metric > rt->rt6i_metric)
726 break;
727
Changli Gaod8d1f302010-06-10 23:31:35 -0700728 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
David S. Millerf11e6652007-03-24 20:36:25 -0700731 /* Reset round-robin state, if necessary */
732 if (ins == &fn->leaf)
733 fn->rr_ptr = NULL;
734
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000735 /* Link this route to others same route. */
736 if (rt->rt6i_nsiblings) {
737 unsigned int rt6i_nsiblings;
738 struct rt6_info *sibling, *temp_sibling;
739
740 /* Find the first route that have the same metric */
741 sibling = fn->leaf;
742 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200743 if (sibling->rt6i_metric == rt->rt6i_metric &&
744 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000745 list_add_tail(&rt->rt6i_siblings,
746 &sibling->rt6i_siblings);
747 break;
748 }
749 sibling = sibling->dst.rt6_next;
750 }
751 /* For each sibling in the list, increment the counter of
752 * siblings. BUG() if counters does not match, list of siblings
753 * is broken!
754 */
755 rt6i_nsiblings = 0;
756 list_for_each_entry_safe(sibling, temp_sibling,
757 &rt->rt6i_siblings, rt6i_siblings) {
758 sibling->rt6i_nsiblings++;
759 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
760 rt6i_nsiblings++;
761 }
762 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
763 }
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * insert node
767 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000768 if (!replace) {
769 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000770 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000772add:
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100773 if (mx) {
774 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
775 if (err)
776 return err;
777 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000778 rt->dst.rt6_next = iter;
779 *ins = rt;
780 rt->rt6i_node = fn;
781 atomic_inc(&rt->rt6i_ref);
782 inet6_rt_notify(RTM_NEWROUTE, rt, info);
783 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
David S. Miller507c9b12011-12-03 17:50:45 -0500785 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000786 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
787 fn->fn_flags |= RTN_RTINFO;
788 }
789
790 } else {
791 if (!found) {
792 if (add)
793 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000794 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000795 return -ENOENT;
796 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100797 if (mx) {
798 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
799 if (err)
800 return err;
801 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000802 *ins = rt;
803 rt->rt6i_node = fn;
804 rt->dst.rt6_next = iter->dst.rt6_next;
805 atomic_inc(&rt->rt6i_ref);
806 inet6_rt_notify(RTM_NEWROUTE, rt, info);
807 rt6_release(iter);
David S. Miller507c9b12011-12-03 17:50:45 -0500808 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000809 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
810 fn->fn_flags |= RTN_RTINFO;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 return 0;
815}
816
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200817static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700819 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500820 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700821 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700822 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800825void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700827 if (!timer_pending(&net->ipv6.ip6_fib_timer))
828 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700829 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * Add routing information to the routing tree.
834 * <destination addr>/<source addr>
835 * with source addr info in sub-trees
836 */
837
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100838int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
839 struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700841 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000843 int allow_create = 1;
844 int replace_required = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500845
846 if (info->nlh) {
847 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000848 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500849 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000850 replace_required = 1;
851 }
852 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000853 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
fan.du9225b232013-07-22 14:21:09 +0800855 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
856 offsetof(struct rt6_info, rt6i_dst), allow_create,
857 replace_required);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000858 if (IS_ERR(fn)) {
859 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200860 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700864 pn = fn;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866#ifdef CONFIG_IPV6_SUBTREES
867 if (rt->rt6i_src.plen) {
868 struct fib6_node *sn;
869
David S. Miller507c9b12011-12-03 17:50:45 -0500870 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct fib6_node *sfn;
872
873 /*
874 * Create subtree.
875 *
876 * fn[main tree]
877 * |
878 * sfn[subtree root]
879 * \
880 * sn[new leaf node]
881 */
882
883 /* Create subtree root node */
884 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500885 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto st_failure;
887
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800888 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
889 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sfn->fn_flags = RTN_ROOT;
891 sfn->fn_sernum = fib6_new_sernum();
892
893 /* Now add the first leaf node to new subtree */
894
895 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800896 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000897 offsetof(struct rt6_info, rt6i_src),
898 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000900 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* If it is failed, discard just allocated
902 root, and then (in st_failure) stale node
903 in main tree.
904 */
905 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000906 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto st_failure;
908 }
909
910 /* Now link new subtree to main tree */
911 sfn->parent = fn;
912 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
914 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800915 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000916 offsetof(struct rt6_info, rt6i_src),
917 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000919 if (IS_ERR(sn)) {
920 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
David S. Miller507c9b12011-12-03 17:50:45 -0500925 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700926 fn->leaf = rt;
927 atomic_inc(&rt->rt6i_ref);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 fn = sn;
930 }
931#endif
932
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100933 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
David S. Miller507c9b12011-12-03 17:50:45 -0500934 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800935 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500936 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +0800937 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700941 if (err) {
942#ifdef CONFIG_IPV6_SUBTREES
943 /*
944 * If fib6_add_1 has cleared the old leaf pointer in the
945 * super-tree leaf node we have to find a new one for it.
946 */
David S. Miller3c051232008-04-18 01:46:19 -0700947 if (pn != fn && pn->leaf == rt) {
948 pn->leaf = NULL;
949 atomic_dec(&rt->rt6i_ref);
950 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700951 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800952 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700953#if RT6_DEBUG >= 2
954 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700955 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800956 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700957 }
958#endif
959 atomic_inc(&pn->leaf->rt6i_ref);
960 }
961#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700962 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return err;
965
966#ifdef CONFIG_IPV6_SUBTREES
967 /* Subtree creation failed, probably main tree node
968 is orphan. If it is, shoot it.
969 */
970st_failure:
971 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800972 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700973 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return err;
975#endif
976}
977
978/*
979 * Routing tree lookup
980 *
981 */
982
983struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -0500984 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000985 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986};
987
Wang Yufen437de072014-03-28 12:07:04 +0800988static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
989 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -0800992 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700994 if (unlikely(args->offset == 0))
995 return NULL;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * Descend on a tree
999 */
1000
1001 fn = root;
1002
1003 for (;;) {
1004 struct fib6_node *next;
1005
1006 dir = addr_bit_set(args->addr, fn->fn_bit);
1007
1008 next = dir ? fn->right : fn->left;
1009
1010 if (next) {
1011 fn = next;
1012 continue;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 break;
1015 }
1016
David S. Miller507c9b12011-12-03 17:50:45 -05001017 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001018 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct rt6key *key;
1020
1021 key = (struct rt6key *) ((u8 *) fn->leaf +
1022 args->offset);
1023
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001024 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1025#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001026 if (fn->subtree) {
1027 struct fib6_node *sfn;
1028 sfn = fib6_lookup_1(fn->subtree,
1029 args + 1);
1030 if (!sfn)
1031 goto backtrack;
1032 fn = sfn;
1033 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001034#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001035 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001036 return fn;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001039#ifdef CONFIG_IPV6_SUBTREES
1040backtrack:
1041#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001042 if (fn->fn_flags & RTN_ROOT)
1043 break;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 fn = fn->parent;
1046 }
1047
1048 return NULL;
1049}
1050
Wang Yufen437de072014-03-28 12:07:04 +08001051struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1052 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001055 struct lookup_args args[] = {
1056 {
1057 .offset = offsetof(struct rt6_info, rt6i_dst),
1058 .addr = daddr,
1059 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001061 {
1062 .offset = offsetof(struct rt6_info, rt6i_src),
1063 .addr = saddr,
1064 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001066 {
1067 .offset = 0, /* sentinel */
1068 }
1069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001071 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001072 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 fn = root;
1074
1075 return fn;
1076}
1077
1078/*
1079 * Get node with specified destination prefix (and source prefix,
1080 * if subtrees are used)
1081 */
1082
1083
Wang Yufen437de072014-03-28 12:07:04 +08001084static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1085 const struct in6_addr *addr,
1086 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct fib6_node *fn;
1089
1090 for (fn = root; fn ; ) {
1091 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1092
1093 /*
1094 * Prefix match
1095 */
1096 if (plen < fn->fn_bit ||
1097 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1098 return NULL;
1099
1100 if (plen == fn->fn_bit)
1101 return fn;
1102
1103 /*
1104 * We have more bits to go
1105 */
1106 if (addr_bit_set(addr, fn->fn_bit))
1107 fn = fn->right;
1108 else
1109 fn = fn->left;
1110 }
1111 return NULL;
1112}
1113
Wang Yufen437de072014-03-28 12:07:04 +08001114struct fib6_node *fib6_locate(struct fib6_node *root,
1115 const struct in6_addr *daddr, int dst_len,
1116 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 struct fib6_node *fn;
1119
1120 fn = fib6_locate_1(root, daddr, dst_len,
1121 offsetof(struct rt6_info, rt6i_dst));
1122
1123#ifdef CONFIG_IPV6_SUBTREES
1124 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001125 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001126 if (fn && fn->subtree)
1127 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 offsetof(struct rt6_info, rt6i_src));
1129 }
1130#endif
1131
David S. Miller507c9b12011-12-03 17:50:45 -05001132 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return fn;
1134
1135 return NULL;
1136}
1137
1138
1139/*
1140 * Deletion
1141 *
1142 */
1143
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001144static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
David S. Miller507c9b12011-12-03 17:50:45 -05001146 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001147 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
David S. Miller507c9b12011-12-03 17:50:45 -05001149 while (fn) {
1150 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001152 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return fn->right->leaf;
1154
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001155 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 return NULL;
1158}
1159
1160/*
1161 * Called to trim the tree of intermediate nodes when possible. "fn"
1162 * is the node we want to try and remove.
1163 */
1164
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001165static struct fib6_node *fib6_repair_tree(struct net *net,
1166 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 int children;
1169 int nstate;
1170 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001171 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int iter = 0;
1173
1174 for (;;) {
1175 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1176 iter++;
1177
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001178 WARN_ON(fn->fn_flags & RTN_RTINFO);
1179 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1180 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 children = 0;
1183 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001184 if (fn->right)
1185 child = fn->right, children |= 1;
1186 if (fn->left)
1187 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001189 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190#ifdef CONFIG_IPV6_SUBTREES
1191 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001192 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193#endif
1194 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001195 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001197 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001198 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001199 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201#endif
1202 atomic_inc(&fn->leaf->rt6i_ref);
1203 return fn->parent;
1204 }
1205
1206 pn = fn->parent;
1207#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001208 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001209 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001210 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 nstate = FWS_L;
1212 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001213 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001215 if (pn->right == fn)
1216 pn->right = child;
1217 else if (pn->left == fn)
1218 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001220 else
1221 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222#endif
1223 if (child)
1224 child->parent = pn;
1225 nstate = FWS_R;
1226#ifdef CONFIG_IPV6_SUBTREES
1227 }
1228#endif
1229
1230 read_lock(&fib6_walker_lock);
1231 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001232 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (w->root == fn) {
1234 w->root = w->node = NULL;
1235 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1236 } else if (w->node == fn) {
1237 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1238 w->node = pn;
1239 w->state = nstate;
1240 }
1241 } else {
1242 if (w->root == fn) {
1243 w->root = child;
1244 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1245 }
1246 if (w->node == fn) {
1247 w->node = child;
1248 if (children&2) {
1249 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001250 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 } else {
1252 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001253 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 }
1256 }
1257 }
1258 read_unlock(&fib6_walker_lock);
1259
1260 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001261 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return pn;
1263
1264 rt6_release(pn->leaf);
1265 pn->leaf = NULL;
1266 fn = pn;
1267 }
1268}
1269
1270static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001271 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001273 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001275 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 RT6_TRACE("fib6_del_route\n");
1278
1279 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001280 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001282 net->ipv6.rt6_stats->fib_rt_entries--;
1283 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
David S. Millerf11e6652007-03-24 20:36:25 -07001285 /* Reset round-robin state, if necessary */
1286 if (fn->rr_ptr == rt)
1287 fn->rr_ptr = NULL;
1288
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001289 /* Remove this entry from other siblings */
1290 if (rt->rt6i_nsiblings) {
1291 struct rt6_info *sibling, *next_sibling;
1292
1293 list_for_each_entry_safe(sibling, next_sibling,
1294 &rt->rt6i_siblings, rt6i_siblings)
1295 sibling->rt6i_nsiblings--;
1296 rt->rt6i_nsiblings = 0;
1297 list_del_init(&rt->rt6i_siblings);
1298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* Adjust walkers */
1301 read_lock(&fib6_walker_lock);
1302 FOR_WALKERS(w) {
1303 if (w->state == FWS_C && w->leaf == rt) {
1304 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001305 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001306 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 w->state = FWS_U;
1308 }
1309 }
1310 read_unlock(&fib6_walker_lock);
1311
Changli Gaod8d1f302010-06-10 23:31:35 -07001312 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001315 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001317 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001318 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 if (atomic_read(&rt->rt6i_ref) != 1) {
1322 /* This route is used as dummy address holder in some split
1323 * nodes. It is not leaked, but it still holds other resources,
1324 * which must be released in time. So, scan ascendant nodes
1325 * and replace dummy references to this route with references
1326 * to still alive ones.
1327 */
1328 while (fn) {
David S. Miller507c9b12011-12-03 17:50:45 -05001329 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001330 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 atomic_inc(&fn->leaf->rt6i_ref);
1332 rt6_release(rt);
1333 }
1334 fn = fn->parent;
1335 }
1336 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001337 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
Thomas Graf86872cb2006-08-22 00:01:08 -07001340 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 rt6_release(rt);
1342}
1343
Thomas Graf86872cb2006-08-22 00:01:08 -07001344int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001346 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct fib6_node *fn = rt->rt6i_node;
1348 struct rt6_info **rtp;
1349
1350#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001351 if (rt->dst.obsolete > 0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001352 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ENOENT;
1354 }
1355#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001356 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -ENOENT;
1358
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001359 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
David S. Miller507c9b12011-12-03 17:50:45 -05001361 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001362 struct fib6_node *pn = fn;
1363#ifdef CONFIG_IPV6_SUBTREES
1364 /* clones of this route might be in another subtree */
1365 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001366 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001367 pn = pn->parent;
1368 pn = pn->parent;
1369 }
1370#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001371 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /*
1375 * Walk the leaf entries looking for ourself
1376 */
1377
Changli Gaod8d1f302010-06-10 23:31:35 -07001378 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001380 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382 }
1383 }
1384 return -ENOENT;
1385}
1386
1387/*
1388 * Tree traversal function.
1389 *
1390 * Certainly, it is not interrupt safe.
1391 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1392 * It means, that we can modify tree during walking
1393 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001394 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *
1396 * It guarantees that every node will be traversed,
1397 * and that it will be traversed only once.
1398 *
1399 * Callback function w->func may return:
1400 * 0 -> continue walking.
1401 * positive value -> walking is suspended (used by tree dumps,
1402 * and probably by gc, if it will be split to several slices)
1403 * negative value -> terminate walking.
1404 *
1405 * The function itself returns:
1406 * 0 -> walk is complete.
1407 * >0 -> walk is incomplete (i.e. suspended)
1408 * <0 -> walk is terminated by an error.
1409 */
1410
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001411static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct fib6_node *fn, *pn;
1414
1415 for (;;) {
1416 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001417 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
1419
1420 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001421 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 w->state = FWS_C;
1423 w->leaf = fn->leaf;
1424 }
1425 switch (w->state) {
1426#ifdef CONFIG_IPV6_SUBTREES
1427 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001428 if (FIB6_SUBTREE(fn)) {
1429 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 continue;
1431 }
1432 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001433#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 case FWS_L:
1435 if (fn->left) {
1436 w->node = fn->left;
1437 w->state = FWS_INIT;
1438 continue;
1439 }
1440 w->state = FWS_R;
1441 case FWS_R:
1442 if (fn->right) {
1443 w->node = fn->right;
1444 w->state = FWS_INIT;
1445 continue;
1446 }
1447 w->state = FWS_C;
1448 w->leaf = fn->leaf;
1449 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001450 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001451 int err;
1452
Eric Dumazetfa809e22012-06-25 15:37:19 -07001453 if (w->skip) {
1454 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001455 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001456 }
1457
1458 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (err)
1460 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001461
1462 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 continue;
1464 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001465skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 w->state = FWS_U;
1467 case FWS_U:
1468 if (fn == w->root)
1469 return 0;
1470 pn = fn->parent;
1471 w->node = pn;
1472#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001473 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001474 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 w->state = FWS_L;
1476 continue;
1477 }
1478#endif
1479 if (pn->left == fn) {
1480 w->state = FWS_R;
1481 continue;
1482 }
1483 if (pn->right == fn) {
1484 w->state = FWS_C;
1485 w->leaf = w->node->leaf;
1486 continue;
1487 }
1488#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001489 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490#endif
1491 }
1492 }
1493}
1494
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001495static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 int res;
1498
1499 w->state = FWS_INIT;
1500 w->node = w->root;
1501
1502 fib6_walker_link(w);
1503 res = fib6_walk_continue(w);
1504 if (res <= 0)
1505 fib6_walker_unlink(w);
1506 return res;
1507}
1508
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001509static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int res;
1512 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001513 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001514 struct nl_info info = {
1515 .nl_net = c->net,
1516 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Changli Gaod8d1f302010-06-10 23:31:35 -07001518 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 res = c->func(rt, c->arg);
1520 if (res < 0) {
1521 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001522 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (res) {
1524#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001525 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1526 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527#endif
1528 continue;
1529 }
1530 return 0;
1531 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001532 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 w->leaf = rt;
1535 return 0;
1536}
1537
1538/*
1539 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001540 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * func is called on each route.
1542 * It may return -1 -> delete this route.
1543 * 0 -> continue walking
1544 *
1545 * prune==1 -> only immediate children of node (certainly,
1546 * ignoring pure split nodes) will be scanned.
1547 */
1548
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001549static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001550 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001551 bool prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001553 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 c.w.root = root;
1556 c.w.func = fib6_clean_node;
1557 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001558 c.w.count = 0;
1559 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 c.func = func;
1561 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001562 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 fib6_walk(&c.w);
1565}
1566
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001567void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Li RongQing0c3584d2013-12-27 16:32:38 +08001568 void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001569{
Thomas Grafc71099a2006-08-04 23:20:06 -07001570 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001571 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001572 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001573
Patrick McHardy1b43af52006-08-10 23:11:17 -07001574 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001575 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001576 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001577 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001578 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001579 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001580 func, false, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001581 write_unlock_bh(&table->tb6_lock);
1582 }
1583 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001584 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1588{
1589 if (rt->rt6i_flags & RTF_CACHE) {
1590 RT6_TRACE("pruning clone %p\n", rt);
1591 return -1;
1592 }
1593
1594 return 0;
1595}
1596
Duan Jiong163cd4e2014-05-09 13:31:43 +08001597static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001599 fib6_clean_tree(net, fn, fib6_prune_clone, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001602static int fib6_update_sernum(struct rt6_info *rt, void *arg)
1603{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001604 int sernum = *(int *)arg;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001605
1606 if (rt->rt6i_node &&
1607 rt->rt6i_node->fn_sernum != sernum)
1608 rt->rt6i_node->fn_sernum = sernum;
1609
1610 return 0;
1611}
1612
1613static void fib6_flush_trees(struct net *net)
1614{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001615 int new_sernum = fib6_new_sernum();
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001616
1617 fib6_clean_all(net, fib6_update_sernum, &new_sernum);
1618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620/*
1621 * Garbage collection
1622 */
1623
1624static struct fib6_gc_args
1625{
1626 int timeout;
1627 int more;
1628} gc_args;
1629
1630static int fib6_age(struct rt6_info *rt, void *arg)
1631{
1632 unsigned long now = jiffies;
1633
1634 /*
1635 * check addrconf expiration here.
1636 * Routes are expired even if they are in use.
1637 *
1638 * Also age clones. Note, that clones are aged out
1639 * only if they are not in use now.
1640 */
1641
David S. Millerd1918542011-12-28 20:19:20 -05001642 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1643 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return -1;
1646 }
1647 gc_args.more++;
1648 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001649 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1650 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 RT6_TRACE("aging clone %p\n", rt);
1652 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001653 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1654 struct neighbour *neigh;
1655 __u8 neigh_flags = 0;
1656
1657 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1658 if (neigh) {
1659 neigh_flags = neigh->flags;
1660 neigh_release(neigh);
1661 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001662 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001663 RT6_TRACE("purging route %p via non-router but gateway\n",
1664 rt);
1665 return -1;
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 gc_args.more++;
1669 }
1670
1671 return 0;
1672}
1673
1674static DEFINE_SPINLOCK(fib6_gc_lock);
1675
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001676void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001678 unsigned long now;
1679
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001680 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001682 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1683 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001686 gc_args.timeout = expires ? (int)expires :
1687 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001689 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001690
Li RongQing0c3584d2013-12-27 16:32:38 +08001691 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001692 now = jiffies;
1693 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001696 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001697 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001698 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001699 else
1700 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 spin_unlock_bh(&fib6_gc_lock);
1702}
1703
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001704static void fib6_gc_timer_cb(unsigned long arg)
1705{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001706 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001707}
1708
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001709static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001710{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001711 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1712
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001713 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001714
Benjamin Theryc5728722008-03-03 23:34:17 -08001715 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1716 if (!net->ipv6.rt6_stats)
1717 goto out_timer;
1718
Eric Dumazet10da66f2010-10-13 08:22:03 +00001719 /* Avoid false sharing : Use at least a full cache line */
1720 size = max_t(size_t, size, L1_CACHE_BYTES);
1721
1722 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001723 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001724 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001725
1726 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1727 GFP_KERNEL);
1728 if (!net->ipv6.fib6_main_tbl)
1729 goto out_fib_table_hash;
1730
1731 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001732 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001733 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1734 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001735 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001736
1737#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1738 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1739 GFP_KERNEL);
1740 if (!net->ipv6.fib6_local_tbl)
1741 goto out_fib6_main_tbl;
1742 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001743 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001744 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1745 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001746 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001747#endif
1748 fib6_tables_init(net);
1749
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001750 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001751
1752#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1753out_fib6_main_tbl:
1754 kfree(net->ipv6.fib6_main_tbl);
1755#endif
1756out_fib_table_hash:
1757 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001758out_rt6_stats:
1759 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001760out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001761 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001762}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001763
1764static void fib6_net_exit(struct net *net)
1765{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001766 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001767 del_timer_sync(&net->ipv6.ip6_fib_timer);
1768
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001769#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001770 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001771 kfree(net->ipv6.fib6_local_tbl);
1772#endif
David S. Miller8e773272012-06-11 00:01:52 -07001773 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001774 kfree(net->ipv6.fib6_main_tbl);
1775 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001776 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001777}
1778
1779static struct pernet_operations fib6_net_ops = {
1780 .init = fib6_net_init,
1781 .exit = fib6_net_exit,
1782};
1783
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001784int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001786 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1789 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001790 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001791 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001792 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001793 goto out;
1794
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001795 ret = register_pernet_subsys(&fib6_net_ops);
1796 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001797 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001798
1799 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1800 NULL);
1801 if (ret)
1802 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001803
1804 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001805out:
1806 return ret;
1807
David S. Millere8803b62012-06-16 01:12:19 -07001808out_unregister_subsys:
1809 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001810out_kmem_cache_create:
1811 kmem_cache_destroy(fib6_node_kmem);
1812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815void fib6_gc_cleanup(void)
1816{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001817 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 kmem_cache_destroy(fib6_node_kmem);
1819}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001820
1821#ifdef CONFIG_PROC_FS
1822
1823struct ipv6_route_iter {
1824 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001825 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001826 loff_t skip;
1827 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001828 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001829};
1830
1831static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1832{
1833 struct rt6_info *rt = v;
1834 struct ipv6_route_iter *iter = seq->private;
1835
1836 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1837
1838#ifdef CONFIG_IPV6_SUBTREES
1839 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1840#else
1841 seq_puts(seq, "00000000000000000000000000000000 00 ");
1842#endif
1843 if (rt->rt6i_flags & RTF_GATEWAY)
1844 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1845 else
1846 seq_puts(seq, "00000000000000000000000000000000");
1847
1848 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1849 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1850 rt->dst.__use, rt->rt6i_flags,
1851 rt->dst.dev ? rt->dst.dev->name : "");
1852 iter->w.leaf = NULL;
1853 return 0;
1854}
1855
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001856static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001857{
1858 struct ipv6_route_iter *iter = w->args;
1859
1860 if (!iter->skip)
1861 return 1;
1862
1863 do {
1864 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1865 iter->skip--;
1866 if (!iter->skip && iter->w.leaf)
1867 return 1;
1868 } while (iter->w.leaf);
1869
1870 return 0;
1871}
1872
1873static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1874{
1875 memset(&iter->w, 0, sizeof(iter->w));
1876 iter->w.func = ipv6_route_yield;
1877 iter->w.root = &iter->tbl->tb6_root;
1878 iter->w.state = FWS_INIT;
1879 iter->w.node = iter->w.root;
1880 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001881 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001882 INIT_LIST_HEAD(&iter->w.lh);
1883 fib6_walker_link(&iter->w);
1884}
1885
1886static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1887 struct net *net)
1888{
1889 unsigned int h;
1890 struct hlist_node *node;
1891
1892 if (tbl) {
1893 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1894 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1895 } else {
1896 h = 0;
1897 node = NULL;
1898 }
1899
1900 while (!node && h < FIB6_TABLE_HASHSZ) {
1901 node = rcu_dereference_bh(
1902 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1903 }
1904 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1905}
1906
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001907static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1908{
1909 if (iter->sernum != iter->w.root->fn_sernum) {
1910 iter->sernum = iter->w.root->fn_sernum;
1911 iter->w.state = FWS_INIT;
1912 iter->w.node = iter->w.root;
1913 WARN_ON(iter->w.skip);
1914 iter->w.skip = iter->w.count;
1915 }
1916}
1917
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001918static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1919{
1920 int r;
1921 struct rt6_info *n;
1922 struct net *net = seq_file_net(seq);
1923 struct ipv6_route_iter *iter = seq->private;
1924
1925 if (!v)
1926 goto iter_table;
1927
1928 n = ((struct rt6_info *)v)->dst.rt6_next;
1929 if (n) {
1930 ++*pos;
1931 return n;
1932 }
1933
1934iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001935 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001936 read_lock(&iter->tbl->tb6_lock);
1937 r = fib6_walk_continue(&iter->w);
1938 read_unlock(&iter->tbl->tb6_lock);
1939 if (r > 0) {
1940 if (v)
1941 ++*pos;
1942 return iter->w.leaf;
1943 } else if (r < 0) {
1944 fib6_walker_unlink(&iter->w);
1945 return NULL;
1946 }
1947 fib6_walker_unlink(&iter->w);
1948
1949 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1950 if (!iter->tbl)
1951 return NULL;
1952
1953 ipv6_route_seq_setup_walk(iter);
1954 goto iter_table;
1955}
1956
1957static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1958 __acquires(RCU_BH)
1959{
1960 struct net *net = seq_file_net(seq);
1961 struct ipv6_route_iter *iter = seq->private;
1962
1963 rcu_read_lock_bh();
1964 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1965 iter->skip = *pos;
1966
1967 if (iter->tbl) {
1968 ipv6_route_seq_setup_walk(iter);
1969 return ipv6_route_seq_next(seq, NULL, pos);
1970 } else {
1971 return NULL;
1972 }
1973}
1974
1975static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1976{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001977 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001978 return w->node && !(w->state == FWS_U && w->node == w->root);
1979}
1980
1981static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1982 __releases(RCU_BH)
1983{
1984 struct ipv6_route_iter *iter = seq->private;
1985
1986 if (ipv6_route_iter_active(iter))
1987 fib6_walker_unlink(&iter->w);
1988
1989 rcu_read_unlock_bh();
1990}
1991
1992static const struct seq_operations ipv6_route_seq_ops = {
1993 .start = ipv6_route_seq_start,
1994 .next = ipv6_route_seq_next,
1995 .stop = ipv6_route_seq_stop,
1996 .show = ipv6_route_seq_show
1997};
1998
1999int ipv6_route_open(struct inode *inode, struct file *file)
2000{
2001 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2002 sizeof(struct ipv6_route_iter));
2003}
2004
2005#endif /* CONFIG_PROC_FS */