blob: 418d9823692b6e78077d44c1ed8b15e998e2316b [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>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020035#include <net/lwtunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <net/ip6_fib.h>
38#include <net/ip6_route.h>
39
40#define RT6_DEBUG 2
41
42#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000043#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define RT6_TRACE(x...) do { ; } while (0)
46#endif
47
Wang Yufen437de072014-03-28 12:07:04 +080048static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020050struct fib6_cleaner {
51 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080052 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020054 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void *arg;
56};
57
Adrian Bunk90d41122006-08-14 23:49:16 -070058static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#ifdef CONFIG_IPV6_SUBTREES
61#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
63#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Duan Jiong163cd4e2014-05-09 13:31:43 +080066static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080067static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
68static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020069static int fib6_walk(struct fib6_walker *w);
70static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 * A routing update causes an increase of the serial number on the
74 * affected subtree. This allows for cached routes to be asynchronously
75 * tested when modifications are made to the destination cache as a
76 * result of redirects, path MTU changes, etc.
77 */
78
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 Sowa812918c2014-10-06 19:58:37 +020098static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200100 int new, old;
101
102 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200103 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200104 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200105 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
106 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200107 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200110enum {
111 FIB6_NO_SERNUM_CHANGE = 0,
112};
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * Auxiliary address test functions for the radix tree.
116 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900117 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * 64bit processors)
119 */
120
121/*
122 * test bit
123 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000124#if defined(__LITTLE_ENDIAN)
125# define BITOP_BE32_SWIZZLE (0x1F & ~7)
126#else
127# define BITOP_BE32_SWIZZLE 0
128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200130static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000132 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000133 /*
134 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800135 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000136 * is optimized version of
137 * htonl(1 << ((~fn_bit)&0x1F))
138 * See include/asm-generic/bitops/le.h.
139 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700140 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
141 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200144static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct fib6_node *fn;
147
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800148 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return fn;
151}
152
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200153static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 kmem_cache_free(fib6_node_kmem, fn);
156}
157
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700158static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
159{
160 int cpu;
161
162 if (!non_pcpu_rt->rt6i_pcpu)
163 return;
164
165 for_each_possible_cpu(cpu) {
166 struct rt6_info **ppcpu_rt;
167 struct rt6_info *pcpu_rt;
168
169 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
170 pcpu_rt = *ppcpu_rt;
171 if (pcpu_rt) {
172 dst_free(&pcpu_rt->dst);
173 *ppcpu_rt = NULL;
174 }
175 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700176
177 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700178}
179
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200180static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700182 if (atomic_dec_and_test(&rt->rt6i_ref)) {
183 rt6_free_pcpu(rt);
Changli Gaod8d1f302010-06-10 23:31:35 -0700184 dst_free(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800188static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700189{
190 unsigned int h;
191
Thomas Graf375216a2006-10-21 20:20:54 -0700192 /*
193 * Initialize table lock at a single place to give lockdep a key,
194 * tables aren't visible prior to being linked to the list.
195 */
196 rwlock_init(&tb->tb6_lock);
197
Neil Hormana33bc5c2009-07-30 18:52:15 -0700198 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700199
200 /*
201 * No protection necessary, this is the only list mutatation
202 * operation, tables never disappear once they exist.
203 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800204 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700205}
206
207#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800208
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800209static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700210{
211 struct fib6_table *table;
212
213 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500214 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700215 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800216 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700217 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700218 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700219 }
220
221 return table;
222}
223
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800224struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700225{
226 struct fib6_table *tb;
227
228 if (id == 0)
229 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800230 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700231 if (tb)
232 return tb;
233
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800234 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500235 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800236 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700237
238 return tb;
239}
240
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800241struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700242{
243 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800244 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700245 unsigned int h;
246
247 if (id == 0)
248 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700249 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700250 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800251 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800252 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700253 if (tb->tb6_id == id) {
254 rcu_read_unlock();
255 return tb;
256 }
257 }
258 rcu_read_unlock();
259
260 return NULL;
261}
262
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000263static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700264{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800265 fib6_link_table(net, net->ipv6.fib6_main_tbl);
266 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700267}
Thomas Grafc71099a2006-08-04 23:20:06 -0700268#else
269
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800270struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700271{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800272 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700273}
274
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800275struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700276{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800277 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700278}
279
David S. Miller4c9483b2011-03-12 16:22:43 -0500280struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800281 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700282{
David S. Miller4c9483b2011-03-12 16:22:43 -0500283 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700284}
285
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000286static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700287{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800288 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700289}
290
291#endif
292
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200293static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700294{
295 int res;
296 struct rt6_info *rt;
297
Changli Gaod8d1f302010-06-10 23:31:35 -0700298 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700299 res = rt6_dump_route(rt, w->args);
300 if (res < 0) {
301 /* Frame is full, suspend walking */
302 w->leaf = rt;
303 return 1;
304 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700305 }
306 w->leaf = NULL;
307 return 0;
308}
309
310static void fib6_dump_end(struct netlink_callback *cb)
311{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200312 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700313
314 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800315 if (cb->args[4]) {
316 cb->args[4] = 0;
317 fib6_walker_unlink(w);
318 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700319 cb->args[2] = 0;
320 kfree(w);
321 }
Wang Yufen437de072014-03-28 12:07:04 +0800322 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700323 cb->args[1] = 3;
324}
325
326static int fib6_dump_done(struct netlink_callback *cb)
327{
328 fib6_dump_end(cb);
329 return cb->done ? cb->done(cb) : 0;
330}
331
332static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
333 struct netlink_callback *cb)
334{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200335 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700336 int res;
337
338 w = (void *)cb->args[2];
339 w->root = &table->tb6_root;
340
341 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000342 w->count = 0;
343 w->skip = 0;
344
Patrick McHardy1b43af52006-08-10 23:11:17 -0700345 read_lock_bh(&table->tb6_lock);
346 res = fib6_walk(w);
347 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000348 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700349 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000350 cb->args[5] = w->root->fn_sernum;
351 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700352 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000353 if (cb->args[5] != w->root->fn_sernum) {
354 /* Begin at the root if the tree changed */
355 cb->args[5] = w->root->fn_sernum;
356 w->state = FWS_INIT;
357 w->node = w->root;
358 w->skip = w->count;
359 } else
360 w->skip = 0;
361
Patrick McHardy1b43af52006-08-10 23:11:17 -0700362 read_lock_bh(&table->tb6_lock);
363 res = fib6_walk_continue(w);
364 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800365 if (res <= 0) {
366 fib6_walker_unlink(w);
367 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700368 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700369 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800370
Patrick McHardy1b43af52006-08-10 23:11:17 -0700371 return res;
372}
373
Thomas Grafc127ea22007-03-22 11:58:32 -0700374static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700375{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900376 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700377 unsigned int h, s_h;
378 unsigned int e = 0, s_e;
379 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200380 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700381 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800382 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700383 int res = 0;
384
385 s_h = cb->args[0];
386 s_e = cb->args[1];
387
388 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500389 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700390 /* New dump:
391 *
392 * 1. hook callback destructor.
393 */
394 cb->args[3] = (long)cb->done;
395 cb->done = fib6_dump_done;
396
397 /*
398 * 2. allocate and initialize walker.
399 */
400 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500401 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700402 return -ENOMEM;
403 w->func = fib6_dump_node;
404 cb->args[2] = (long)w;
405 }
406
407 arg.skb = skb;
408 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700409 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700410 w->args = &arg;
411
Eric Dumazete67f88d2011-04-27 22:56:07 +0000412 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700413 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700414 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800415 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800416 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700417 if (e < s_e)
418 goto next;
419 res = fib6_dump_table(tb, skb, cb);
420 if (res != 0)
421 goto out;
422next:
423 e++;
424 }
425 }
426out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000427 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700428 cb->args[1] = e;
429 cb->args[0] = h;
430
431 res = res < 0 ? res : skb->len;
432 if (res <= 0)
433 fib6_dump_end(cb);
434 return res;
435}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/*
438 * Routing Table
439 *
440 * return the appropriate node for a routing tree "add" operation
441 * by either creating and inserting or by returning an existing
442 * node.
443 */
444
fan.du9225b232013-07-22 14:21:09 +0800445static struct fib6_node *fib6_add_1(struct fib6_node *root,
446 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000447 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200448 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct fib6_node *fn, *in, *ln;
451 struct fib6_node *pn = NULL;
452 struct rt6key *key;
453 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900454 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 RT6_TRACE("fib6_add_1\n");
457
458 /* insert node in tree */
459
460 fn = root;
461
462 do {
463 key = (struct rt6key *)((u8 *)fn->leaf + offset);
464
465 /*
466 * Prefix match
467 */
468 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000469 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000470 if (!allow_create) {
471 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000472 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000473 return ERR_PTR(-ENOENT);
474 }
Joe Perchesf3213832012-05-15 14:11:53 +0000475 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000478 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 * Exact match ?
482 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (plen == fn->fn_bit) {
485 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500486 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 rt6_release(fn->leaf);
488 fn->leaf = NULL;
489 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return fn;
494 }
495
496 /*
497 * We have more bits to go
498 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Try to walk down on tree. */
501 fn->fn_sernum = sernum;
502 dir = addr_bit_set(addr, fn->fn_bit);
503 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800504 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 } while (fn);
506
Matti Vaittinen14df0152011-11-16 21:18:02 +0000507 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000508 /* We should not create new node because
509 * NLM_F_REPLACE was specified without NLM_F_CREATE
510 * I assume it is safe to require NLM_F_CREATE when
511 * REPLACE flag is used! Later we may want to remove the
512 * check for replace_required, because according
513 * to netlink specification, NLM_F_CREATE
514 * MUST be specified if new route is created.
515 * That would keep IPv6 consistent with IPv4
516 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000517 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000518 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000519 return ERR_PTR(-ENOENT);
520 }
Joe Perchesf3213832012-05-15 14:11:53 +0000521 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /*
524 * We walked to the bottom of tree.
525 * Create new leaf node without children.
526 */
527
528 ln = node_alloc();
529
David S. Miller507c9b12011-12-03 17:50:45 -0500530 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000531 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ln->parent = pn;
535 ln->fn_sernum = sernum;
536
537 if (dir)
538 pn->right = ln;
539 else
540 pn->left = ln;
541
542 return ln;
543
544
545insert_above:
546 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900547 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * we have a less significant route.
549 * we've to insert an intermediate node on the list
550 * this new node will point to the one we need to create
551 * and the current
552 */
553
554 pn = fn->parent;
555
556 /* find 1st bit in difference between the 2 addrs.
557
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800558 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 but if it is >= plen, the value is ignored in any case.
560 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900561
fan.du9225b232013-07-22 14:21:09 +0800562 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900564 /*
565 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * / \
567 * (new leaf node)[ln] (old node)[fn]
568 */
569 if (plen > bit) {
570 in = node_alloc();
571 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900572
David S. Miller507c9b12011-12-03 17:50:45 -0500573 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (in)
575 node_free(in);
576 if (ln)
577 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000578 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900581 /*
582 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * RTN_RTINFO will
584 * be off since that an address that chooses one of
585 * the branches would not match less specific routes
586 * in the other branch
587 */
588
589 in->fn_bit = bit;
590
591 in->parent = pn;
592 in->leaf = fn->leaf;
593 atomic_inc(&in->leaf->rt6i_ref);
594
595 in->fn_sernum = sernum;
596
597 /* update parent pointer */
598 if (dir)
599 pn->right = in;
600 else
601 pn->left = in;
602
603 ln->fn_bit = plen;
604
605 ln->parent = in;
606 fn->parent = in;
607
608 ln->fn_sernum = sernum;
609
610 if (addr_bit_set(addr, bit)) {
611 in->right = ln;
612 in->left = fn;
613 } else {
614 in->left = ln;
615 in->right = fn;
616 }
617 } else { /* plen <= bit */
618
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900619 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * (new leaf node)[ln]
621 * / \
622 * (old node)[fn] NULL
623 */
624
625 ln = node_alloc();
626
David S. Miller507c9b12011-12-03 17:50:45 -0500627 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000628 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 ln->fn_bit = plen;
631
632 ln->parent = pn;
633
634 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (dir)
637 pn->right = ln;
638 else
639 pn->left = ln;
640
641 if (addr_bit_set(&key->addr, plen))
642 ln->right = fn;
643 else
644 ln->left = fn;
645
646 fn->parent = ln;
647 }
648 return ln;
649}
650
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200651static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200652{
653 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
654 RTF_GATEWAY;
655}
656
Florian Westphale715b6d2015-01-05 23:57:44 +0100657static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100658{
Florian Westphale715b6d2015-01-05 23:57:44 +0100659 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100660
Florian Westphale715b6d2015-01-05 23:57:44 +0100661 for (i = 0; i < RTAX_MAX; i++) {
662 if (test_bit(i, mxc->mx_valid))
663 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100664 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100665}
666
667static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
668{
669 if (!mxc->mx)
670 return 0;
671
672 if (dst->flags & DST_HOST) {
673 u32 *mp = dst_metrics_write_ptr(dst);
674
675 if (unlikely(!mp))
676 return -ENOMEM;
677
678 fib6_copy_metrics(mp, mxc);
679 } else {
680 dst_init_metrics(dst, mxc->mx, false);
681
682 /* We've stolen mx now. */
683 mxc->mx = NULL;
684 }
685
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100686 return 0;
687}
688
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100689static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
690 struct net *net)
691{
692 if (atomic_read(&rt->rt6i_ref) != 1) {
693 /* This route is used as dummy address holder in some split
694 * nodes. It is not leaked, but it still holds other resources,
695 * which must be released in time. So, scan ascendant nodes
696 * and replace dummy references to this route with references
697 * to still alive ones.
698 */
699 while (fn) {
700 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
701 fn->leaf = fib6_find_prefix(net, fn);
702 atomic_inc(&fn->leaf->rt6i_ref);
703 rt6_release(rt);
704 }
705 fn = fn->parent;
706 }
707 /* No more references are possible at this point. */
708 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
709 }
710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/*
713 * Insert routing information in a node.
714 */
715
716static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100717 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct rt6_info *iter = NULL;
720 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200721 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500722 int replace = (info->nlh &&
723 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
724 int add = (!info->nlh ||
725 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000726 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200727 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 ins = &fn->leaf;
731
David S. Miller507c9b12011-12-03 17:50:45 -0500732 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Search for duplicates
735 */
736
737 if (iter->rt6i_metric == rt->rt6i_metric) {
738 /*
739 * Same priority level
740 */
David S. Miller507c9b12011-12-03 17:50:45 -0500741 if (info->nlh &&
742 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000743 return -EEXIST;
744 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200745 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
746 found++;
747 break;
748 }
749 if (rt_can_ecmp)
750 fallback_ins = fallback_ins ?: ins;
751 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
David S. Millerd1918542011-12-28 20:19:20 -0500754 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 iter->rt6i_idev == rt->rt6i_idev &&
756 ipv6_addr_equal(&iter->rt6i_gateway,
757 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000758 if (rt->rt6i_nsiblings)
759 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500760 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000762 if (!(rt->rt6i_flags & RTF_EXPIRES))
763 rt6_clean_expires(iter);
764 else
765 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700766 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EEXIST;
768 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000769 /* If we have the same destination and the same metric,
770 * but not the same gateway, then the route we try to
771 * add is sibling to this route, increment our counter
772 * of siblings, and later we will add our route to the
773 * list.
774 * Only static routes (which don't have flag
775 * RTF_EXPIRES) are used for ECMPv6.
776 *
777 * To avoid long list, we only had siblings if the
778 * route have a gateway.
779 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200780 if (rt_can_ecmp &&
781 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000782 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 if (iter->rt6i_metric > rt->rt6i_metric)
786 break;
787
Michal Kubeček27596472015-05-18 20:54:00 +0200788next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700789 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Michal Kubeček27596472015-05-18 20:54:00 +0200792 if (fallback_ins && !found) {
793 /* No ECMP-able route found, replace first non-ECMP one */
794 ins = fallback_ins;
795 iter = *ins;
796 found++;
797 }
798
David S. Millerf11e6652007-03-24 20:36:25 -0700799 /* Reset round-robin state, if necessary */
800 if (ins == &fn->leaf)
801 fn->rr_ptr = NULL;
802
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000803 /* Link this route to others same route. */
804 if (rt->rt6i_nsiblings) {
805 unsigned int rt6i_nsiblings;
806 struct rt6_info *sibling, *temp_sibling;
807
808 /* Find the first route that have the same metric */
809 sibling = fn->leaf;
810 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200811 if (sibling->rt6i_metric == rt->rt6i_metric &&
812 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000813 list_add_tail(&rt->rt6i_siblings,
814 &sibling->rt6i_siblings);
815 break;
816 }
817 sibling = sibling->dst.rt6_next;
818 }
819 /* For each sibling in the list, increment the counter of
820 * siblings. BUG() if counters does not match, list of siblings
821 * is broken!
822 */
823 rt6i_nsiblings = 0;
824 list_for_each_entry_safe(sibling, temp_sibling,
825 &rt->rt6i_siblings, rt6i_siblings) {
826 sibling->rt6i_nsiblings++;
827 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
828 rt6i_nsiblings++;
829 }
830 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
831 }
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * insert node
835 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000836 if (!replace) {
837 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000838 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000840add:
Florian Westphale715b6d2015-01-05 23:57:44 +0100841 err = fib6_commit_metrics(&rt->dst, mxc);
842 if (err)
843 return err;
844
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000845 rt->dst.rt6_next = iter;
846 *ins = rt;
847 rt->rt6i_node = fn;
848 atomic_inc(&rt->rt6i_ref);
849 inet6_rt_notify(RTM_NEWROUTE, rt, info);
850 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
David S. Miller507c9b12011-12-03 17:50:45 -0500852 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000853 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
854 fn->fn_flags |= RTN_RTINFO;
855 }
856
857 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200858 int nsiblings;
859
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000860 if (!found) {
861 if (add)
862 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000863 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000864 return -ENOENT;
865 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100866
867 err = fib6_commit_metrics(&rt->dst, mxc);
868 if (err)
869 return err;
870
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000871 *ins = rt;
872 rt->rt6i_node = fn;
873 rt->dst.rt6_next = iter->dst.rt6_next;
874 atomic_inc(&rt->rt6i_ref);
875 inet6_rt_notify(RTM_NEWROUTE, rt, info);
David S. Miller507c9b12011-12-03 17:50:45 -0500876 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000877 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
878 fn->fn_flags |= RTN_RTINFO;
879 }
Michal Kubeček27596472015-05-18 20:54:00 +0200880 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100881 fib6_purge_rt(iter, fn, info->nl_net);
882 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200883
884 if (nsiblings) {
885 /* Replacing an ECMP route, remove all siblings */
886 ins = &rt->dst.rt6_next;
887 iter = *ins;
888 while (iter) {
889 if (rt6_qualify_for_ecmp(iter)) {
890 *ins = iter->dst.rt6_next;
891 fib6_purge_rt(iter, fn, info->nl_net);
892 rt6_release(iter);
893 nsiblings--;
894 } else {
895 ins = &iter->dst.rt6_next;
896 }
897 iter = *ins;
898 }
899 WARN_ON(nsiblings != 0);
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 return 0;
904}
905
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200906static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700908 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500909 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700910 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700911 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800914void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700916 if (!timer_pending(&net->ipv6.ip6_fib_timer))
917 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700918 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/*
922 * Add routing information to the routing tree.
923 * <destination addr>/<source addr>
924 * with source addr info in sub-trees
925 */
926
Florian Westphale715b6d2015-01-05 23:57:44 +0100927int fib6_add(struct fib6_node *root, struct rt6_info *rt,
928 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700930 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000932 int allow_create = 1;
933 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200934 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500935
936 if (info->nlh) {
937 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000938 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500939 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000940 replace_required = 1;
941 }
942 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000943 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
fan.du9225b232013-07-22 14:21:09 +0800945 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
946 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200947 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000948 if (IS_ERR(fn)) {
949 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200950 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700954 pn = fn;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956#ifdef CONFIG_IPV6_SUBTREES
957 if (rt->rt6i_src.plen) {
958 struct fib6_node *sn;
959
David S. Miller507c9b12011-12-03 17:50:45 -0500960 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct fib6_node *sfn;
962
963 /*
964 * Create subtree.
965 *
966 * fn[main tree]
967 * |
968 * sfn[subtree root]
969 * \
970 * sn[new leaf node]
971 */
972
973 /* Create subtree root node */
974 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500975 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto st_failure;
977
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800978 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
979 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200981 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /* Now add the first leaf node to new subtree */
984
985 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800986 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000987 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200988 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000990 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* If it is failed, discard just allocated
992 root, and then (in st_failure) stale node
993 in main tree.
994 */
995 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000996 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto st_failure;
998 }
999
1000 /* Now link new subtree to main tree */
1001 sfn->parent = fn;
1002 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } else {
1004 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001005 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001006 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001007 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001009 if (IS_ERR(sn)) {
1010 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
David S. Miller507c9b12011-12-03 17:50:45 -05001015 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001016 fn->leaf = rt;
1017 atomic_inc(&rt->rt6i_ref);
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 fn = sn;
1020 }
1021#endif
1022
Florian Westphale715b6d2015-01-05 23:57:44 +01001023 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001024 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001025 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001026 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001027 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001031 if (err) {
1032#ifdef CONFIG_IPV6_SUBTREES
1033 /*
1034 * If fib6_add_1 has cleared the old leaf pointer in the
1035 * super-tree leaf node we have to find a new one for it.
1036 */
David S. Miller3c051232008-04-18 01:46:19 -07001037 if (pn != fn && pn->leaf == rt) {
1038 pn->leaf = NULL;
1039 atomic_dec(&rt->rt6i_ref);
1040 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001041 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001042 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001043#if RT6_DEBUG >= 2
1044 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001045 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001046 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001047 }
1048#endif
1049 atomic_inc(&pn->leaf->rt6i_ref);
1050 }
1051#endif
Changli Gaod8d1f302010-06-10 23:31:35 -07001052 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return err;
1055
1056#ifdef CONFIG_IPV6_SUBTREES
1057 /* Subtree creation failed, probably main tree node
1058 is orphan. If it is, shoot it.
1059 */
1060st_failure:
1061 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001062 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -07001063 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return err;
1065#endif
1066}
1067
1068/*
1069 * Routing tree lookup
1070 *
1071 */
1072
1073struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001074 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001075 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076};
1077
Wang Yufen437de072014-03-28 12:07:04 +08001078static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1079 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -08001082 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001084 if (unlikely(args->offset == 0))
1085 return NULL;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 * Descend on a tree
1089 */
1090
1091 fn = root;
1092
1093 for (;;) {
1094 struct fib6_node *next;
1095
1096 dir = addr_bit_set(args->addr, fn->fn_bit);
1097
1098 next = dir ? fn->right : fn->left;
1099
1100 if (next) {
1101 fn = next;
1102 continue;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 break;
1105 }
1106
David S. Miller507c9b12011-12-03 17:50:45 -05001107 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001108 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct rt6key *key;
1110
1111 key = (struct rt6key *) ((u8 *) fn->leaf +
1112 args->offset);
1113
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001114 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1115#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001116 if (fn->subtree) {
1117 struct fib6_node *sfn;
1118 sfn = fib6_lookup_1(fn->subtree,
1119 args + 1);
1120 if (!sfn)
1121 goto backtrack;
1122 fn = sfn;
1123 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001124#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001125 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001126 return fn;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001129#ifdef CONFIG_IPV6_SUBTREES
1130backtrack:
1131#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001132 if (fn->fn_flags & RTN_ROOT)
1133 break;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 fn = fn->parent;
1136 }
1137
1138 return NULL;
1139}
1140
Wang Yufen437de072014-03-28 12:07:04 +08001141struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1142 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001145 struct lookup_args args[] = {
1146 {
1147 .offset = offsetof(struct rt6_info, rt6i_dst),
1148 .addr = daddr,
1149 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001151 {
1152 .offset = offsetof(struct rt6_info, rt6i_src),
1153 .addr = saddr,
1154 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001156 {
1157 .offset = 0, /* sentinel */
1158 }
1159 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001161 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001162 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 fn = root;
1164
1165 return fn;
1166}
1167
1168/*
1169 * Get node with specified destination prefix (and source prefix,
1170 * if subtrees are used)
1171 */
1172
1173
Wang Yufen437de072014-03-28 12:07:04 +08001174static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1175 const struct in6_addr *addr,
1176 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct fib6_node *fn;
1179
1180 for (fn = root; fn ; ) {
1181 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1182
1183 /*
1184 * Prefix match
1185 */
1186 if (plen < fn->fn_bit ||
1187 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1188 return NULL;
1189
1190 if (plen == fn->fn_bit)
1191 return fn;
1192
1193 /*
1194 * We have more bits to go
1195 */
1196 if (addr_bit_set(addr, fn->fn_bit))
1197 fn = fn->right;
1198 else
1199 fn = fn->left;
1200 }
1201 return NULL;
1202}
1203
Wang Yufen437de072014-03-28 12:07:04 +08001204struct fib6_node *fib6_locate(struct fib6_node *root,
1205 const struct in6_addr *daddr, int dst_len,
1206 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct fib6_node *fn;
1209
1210 fn = fib6_locate_1(root, daddr, dst_len,
1211 offsetof(struct rt6_info, rt6i_dst));
1212
1213#ifdef CONFIG_IPV6_SUBTREES
1214 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001215 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001216 if (fn && fn->subtree)
1217 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 offsetof(struct rt6_info, rt6i_src));
1219 }
1220#endif
1221
David S. Miller507c9b12011-12-03 17:50:45 -05001222 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return fn;
1224
1225 return NULL;
1226}
1227
1228
1229/*
1230 * Deletion
1231 *
1232 */
1233
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001234static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
David S. Miller507c9b12011-12-03 17:50:45 -05001236 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001237 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
David S. Miller507c9b12011-12-03 17:50:45 -05001239 while (fn) {
1240 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001242 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return fn->right->leaf;
1244
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001245 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 return NULL;
1248}
1249
1250/*
1251 * Called to trim the tree of intermediate nodes when possible. "fn"
1252 * is the node we want to try and remove.
1253 */
1254
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001255static struct fib6_node *fib6_repair_tree(struct net *net,
1256 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 int children;
1259 int nstate;
1260 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001261 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int iter = 0;
1263
1264 for (;;) {
1265 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1266 iter++;
1267
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001268 WARN_ON(fn->fn_flags & RTN_RTINFO);
1269 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001270 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 children = 0;
1273 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001274 if (fn->right)
1275 child = fn->right, children |= 1;
1276 if (fn->left)
1277 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001279 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280#ifdef CONFIG_IPV6_SUBTREES
1281 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001282 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283#endif
1284 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001285 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001287 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001288 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001289 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291#endif
1292 atomic_inc(&fn->leaf->rt6i_ref);
1293 return fn->parent;
1294 }
1295
1296 pn = fn->parent;
1297#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001298 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001299 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001300 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 nstate = FWS_L;
1302 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001303 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001305 if (pn->right == fn)
1306 pn->right = child;
1307 else if (pn->left == fn)
1308 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001310 else
1311 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#endif
1313 if (child)
1314 child->parent = pn;
1315 nstate = FWS_R;
1316#ifdef CONFIG_IPV6_SUBTREES
1317 }
1318#endif
1319
1320 read_lock(&fib6_walker_lock);
1321 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001322 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (w->root == fn) {
1324 w->root = w->node = NULL;
1325 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1326 } else if (w->node == fn) {
1327 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1328 w->node = pn;
1329 w->state = nstate;
1330 }
1331 } else {
1332 if (w->root == fn) {
1333 w->root = child;
1334 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1335 }
1336 if (w->node == fn) {
1337 w->node = child;
1338 if (children&2) {
1339 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001340 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 } else {
1342 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001343 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 }
1346 }
1347 }
1348 read_unlock(&fib6_walker_lock);
1349
1350 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001351 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return pn;
1353
1354 rt6_release(pn->leaf);
1355 pn->leaf = NULL;
1356 fn = pn;
1357 }
1358}
1359
1360static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001361 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001363 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001365 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 RT6_TRACE("fib6_del_route\n");
1368
1369 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001370 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001372 net->ipv6.rt6_stats->fib_rt_entries--;
1373 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David S. Millerf11e6652007-03-24 20:36:25 -07001375 /* Reset round-robin state, if necessary */
1376 if (fn->rr_ptr == rt)
1377 fn->rr_ptr = NULL;
1378
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001379 /* Remove this entry from other siblings */
1380 if (rt->rt6i_nsiblings) {
1381 struct rt6_info *sibling, *next_sibling;
1382
1383 list_for_each_entry_safe(sibling, next_sibling,
1384 &rt->rt6i_siblings, rt6i_siblings)
1385 sibling->rt6i_nsiblings--;
1386 rt->rt6i_nsiblings = 0;
1387 list_del_init(&rt->rt6i_siblings);
1388 }
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Adjust walkers */
1391 read_lock(&fib6_walker_lock);
1392 FOR_WALKERS(w) {
1393 if (w->state == FWS_C && w->leaf == rt) {
1394 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001395 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001396 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 w->state = FWS_U;
1398 }
1399 }
1400 read_unlock(&fib6_walker_lock);
1401
Changli Gaod8d1f302010-06-10 23:31:35 -07001402 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001405 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001407 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001408 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001411 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Thomas Graf86872cb2006-08-22 00:01:08 -07001413 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 rt6_release(rt);
1415}
1416
Thomas Graf86872cb2006-08-22 00:01:08 -07001417int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001419 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct fib6_node *fn = rt->rt6i_node;
1421 struct rt6_info **rtp;
1422
1423#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001424 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001425 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -ENOENT;
1427 }
1428#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001429 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -ENOENT;
1431
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001432 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
David S. Miller507c9b12011-12-03 17:50:45 -05001434 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001435 struct fib6_node *pn = fn;
1436#ifdef CONFIG_IPV6_SUBTREES
1437 /* clones of this route might be in another subtree */
1438 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001439 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001440 pn = pn->parent;
1441 pn = pn->parent;
1442 }
1443#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001444 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /*
1448 * Walk the leaf entries looking for ourself
1449 */
1450
Changli Gaod8d1f302010-06-10 23:31:35 -07001451 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001453 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455 }
1456 }
1457 return -ENOENT;
1458}
1459
1460/*
1461 * Tree traversal function.
1462 *
1463 * Certainly, it is not interrupt safe.
1464 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1465 * It means, that we can modify tree during walking
1466 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001467 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 *
1469 * It guarantees that every node will be traversed,
1470 * and that it will be traversed only once.
1471 *
1472 * Callback function w->func may return:
1473 * 0 -> continue walking.
1474 * positive value -> walking is suspended (used by tree dumps,
1475 * and probably by gc, if it will be split to several slices)
1476 * negative value -> terminate walking.
1477 *
1478 * The function itself returns:
1479 * 0 -> walk is complete.
1480 * >0 -> walk is incomplete (i.e. suspended)
1481 * <0 -> walk is terminated by an error.
1482 */
1483
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001484static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 struct fib6_node *fn, *pn;
1487
1488 for (;;) {
1489 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001490 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 0;
1492
1493 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001494 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 w->state = FWS_C;
1496 w->leaf = fn->leaf;
1497 }
1498 switch (w->state) {
1499#ifdef CONFIG_IPV6_SUBTREES
1500 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001501 if (FIB6_SUBTREE(fn)) {
1502 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 continue;
1504 }
1505 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 case FWS_L:
1508 if (fn->left) {
1509 w->node = fn->left;
1510 w->state = FWS_INIT;
1511 continue;
1512 }
1513 w->state = FWS_R;
1514 case FWS_R:
1515 if (fn->right) {
1516 w->node = fn->right;
1517 w->state = FWS_INIT;
1518 continue;
1519 }
1520 w->state = FWS_C;
1521 w->leaf = fn->leaf;
1522 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001523 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001524 int err;
1525
Eric Dumazetfa809e22012-06-25 15:37:19 -07001526 if (w->skip) {
1527 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001528 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001529 }
1530
1531 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (err)
1533 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001534
1535 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 continue;
1537 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001538skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 w->state = FWS_U;
1540 case FWS_U:
1541 if (fn == w->root)
1542 return 0;
1543 pn = fn->parent;
1544 w->node = pn;
1545#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001546 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001547 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 w->state = FWS_L;
1549 continue;
1550 }
1551#endif
1552 if (pn->left == fn) {
1553 w->state = FWS_R;
1554 continue;
1555 }
1556 if (pn->right == fn) {
1557 w->state = FWS_C;
1558 w->leaf = w->node->leaf;
1559 continue;
1560 }
1561#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001562 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563#endif
1564 }
1565 }
1566}
1567
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001568static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 int res;
1571
1572 w->state = FWS_INIT;
1573 w->node = w->root;
1574
1575 fib6_walker_link(w);
1576 res = fib6_walk_continue(w);
1577 if (res <= 0)
1578 fib6_walker_unlink(w);
1579 return res;
1580}
1581
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001582static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 int res;
1585 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001586 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001587 struct nl_info info = {
1588 .nl_net = c->net,
1589 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001591 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1592 w->node->fn_sernum != c->sernum)
1593 w->node->fn_sernum = c->sernum;
1594
1595 if (!c->func) {
1596 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1597 w->leaf = NULL;
1598 return 0;
1599 }
1600
Changli Gaod8d1f302010-06-10 23:31:35 -07001601 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 res = c->func(rt, c->arg);
1603 if (res < 0) {
1604 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001605 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (res) {
1607#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001608 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1609 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610#endif
1611 continue;
1612 }
1613 return 0;
1614 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001615 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 w->leaf = rt;
1618 return 0;
1619}
1620
1621/*
1622 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001623 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * func is called on each route.
1625 * It may return -1 -> delete this route.
1626 * 0 -> continue walking
1627 *
1628 * prune==1 -> only immediate children of node (certainly,
1629 * ignoring pure split nodes) will be scanned.
1630 */
1631
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001632static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001633 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001634 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001636 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 c.w.root = root;
1639 c.w.func = fib6_clean_node;
1640 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001641 c.w.count = 0;
1642 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001644 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001646 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 fib6_walk(&c.w);
1649}
1650
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001651static void __fib6_clean_all(struct net *net,
1652 int (*func)(struct rt6_info *, void *),
1653 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001654{
Thomas Grafc71099a2006-08-04 23:20:06 -07001655 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001656 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001657 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001658
Patrick McHardy1b43af52006-08-10 23:11:17 -07001659 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001660 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001661 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001662 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001663 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001664 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001665 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001666 write_unlock_bh(&table->tb6_lock);
1667 }
1668 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001669 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001670}
1671
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001672void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1673 void *arg)
1674{
1675 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1679{
1680 if (rt->rt6i_flags & RTF_CACHE) {
1681 RT6_TRACE("pruning clone %p\n", rt);
1682 return -1;
1683 }
1684
1685 return 0;
1686}
1687
Duan Jiong163cd4e2014-05-09 13:31:43 +08001688static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001690 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1691 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001692}
1693
1694static void fib6_flush_trees(struct net *net)
1695{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001696 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001697
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001698 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/*
1702 * Garbage collection
1703 */
1704
1705static struct fib6_gc_args
1706{
1707 int timeout;
1708 int more;
1709} gc_args;
1710
1711static int fib6_age(struct rt6_info *rt, void *arg)
1712{
1713 unsigned long now = jiffies;
1714
1715 /*
1716 * check addrconf expiration here.
1717 * Routes are expired even if they are in use.
1718 *
1719 * Also age clones. Note, that clones are aged out
1720 * only if they are not in use now.
1721 */
1722
David S. Millerd1918542011-12-28 20:19:20 -05001723 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1724 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return -1;
1727 }
1728 gc_args.more++;
1729 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001730 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1731 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 RT6_TRACE("aging clone %p\n", rt);
1733 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001734 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1735 struct neighbour *neigh;
1736 __u8 neigh_flags = 0;
1737
1738 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1739 if (neigh) {
1740 neigh_flags = neigh->flags;
1741 neigh_release(neigh);
1742 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001743 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001744 RT6_TRACE("purging route %p via non-router but gateway\n",
1745 rt);
1746 return -1;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 gc_args.more++;
1750 }
1751
1752 return 0;
1753}
1754
1755static DEFINE_SPINLOCK(fib6_gc_lock);
1756
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001757void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001759 unsigned long now;
1760
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001761 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001763 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1764 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1765 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001767 gc_args.timeout = expires ? (int)expires :
1768 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001770 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001771
Li RongQing0c3584d2013-12-27 16:32:38 +08001772 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001773 now = jiffies;
1774 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001777 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001778 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001779 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001780 else
1781 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 spin_unlock_bh(&fib6_gc_lock);
1783}
1784
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001785static void fib6_gc_timer_cb(unsigned long arg)
1786{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001787 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001788}
1789
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001790static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001791{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001792 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1793
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001794 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001795
Benjamin Theryc5728722008-03-03 23:34:17 -08001796 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1797 if (!net->ipv6.rt6_stats)
1798 goto out_timer;
1799
Eric Dumazet10da66f2010-10-13 08:22:03 +00001800 /* Avoid false sharing : Use at least a full cache line */
1801 size = max_t(size_t, size, L1_CACHE_BYTES);
1802
1803 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001804 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001805 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001806
1807 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1808 GFP_KERNEL);
1809 if (!net->ipv6.fib6_main_tbl)
1810 goto out_fib_table_hash;
1811
1812 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001813 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001814 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1815 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001816 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001817
1818#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1819 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1820 GFP_KERNEL);
1821 if (!net->ipv6.fib6_local_tbl)
1822 goto out_fib6_main_tbl;
1823 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001824 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001825 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1826 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001827 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001828#endif
1829 fib6_tables_init(net);
1830
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001831 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001832
1833#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1834out_fib6_main_tbl:
1835 kfree(net->ipv6.fib6_main_tbl);
1836#endif
1837out_fib_table_hash:
1838 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001839out_rt6_stats:
1840 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001841out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001842 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001843}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001844
1845static void fib6_net_exit(struct net *net)
1846{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001847 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001848 del_timer_sync(&net->ipv6.ip6_fib_timer);
1849
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001850#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001851 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001852 kfree(net->ipv6.fib6_local_tbl);
1853#endif
David S. Miller8e773272012-06-11 00:01:52 -07001854 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001855 kfree(net->ipv6.fib6_main_tbl);
1856 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001857 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001858}
1859
1860static struct pernet_operations fib6_net_ops = {
1861 .init = fib6_net_init,
1862 .exit = fib6_net_exit,
1863};
1864
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001865int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001867 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1870 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001871 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001872 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001873 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001874 goto out;
1875
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001876 ret = register_pernet_subsys(&fib6_net_ops);
1877 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001878 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001879
1880 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1881 NULL);
1882 if (ret)
1883 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001884
1885 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001886out:
1887 return ret;
1888
David S. Millere8803b62012-06-16 01:12:19 -07001889out_unregister_subsys:
1890 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001891out_kmem_cache_create:
1892 kmem_cache_destroy(fib6_node_kmem);
1893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896void fib6_gc_cleanup(void)
1897{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001898 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 kmem_cache_destroy(fib6_node_kmem);
1900}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001901
1902#ifdef CONFIG_PROC_FS
1903
1904struct ipv6_route_iter {
1905 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001906 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001907 loff_t skip;
1908 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001909 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001910};
1911
1912static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1913{
1914 struct rt6_info *rt = v;
1915 struct ipv6_route_iter *iter = seq->private;
1916
1917 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1918
1919#ifdef CONFIG_IPV6_SUBTREES
1920 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1921#else
1922 seq_puts(seq, "00000000000000000000000000000000 00 ");
1923#endif
1924 if (rt->rt6i_flags & RTF_GATEWAY)
1925 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1926 else
1927 seq_puts(seq, "00000000000000000000000000000000");
1928
1929 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1930 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1931 rt->dst.__use, rt->rt6i_flags,
1932 rt->dst.dev ? rt->dst.dev->name : "");
1933 iter->w.leaf = NULL;
1934 return 0;
1935}
1936
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001937static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001938{
1939 struct ipv6_route_iter *iter = w->args;
1940
1941 if (!iter->skip)
1942 return 1;
1943
1944 do {
1945 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1946 iter->skip--;
1947 if (!iter->skip && iter->w.leaf)
1948 return 1;
1949 } while (iter->w.leaf);
1950
1951 return 0;
1952}
1953
1954static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1955{
1956 memset(&iter->w, 0, sizeof(iter->w));
1957 iter->w.func = ipv6_route_yield;
1958 iter->w.root = &iter->tbl->tb6_root;
1959 iter->w.state = FWS_INIT;
1960 iter->w.node = iter->w.root;
1961 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001962 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001963 INIT_LIST_HEAD(&iter->w.lh);
1964 fib6_walker_link(&iter->w);
1965}
1966
1967static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1968 struct net *net)
1969{
1970 unsigned int h;
1971 struct hlist_node *node;
1972
1973 if (tbl) {
1974 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1975 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1976 } else {
1977 h = 0;
1978 node = NULL;
1979 }
1980
1981 while (!node && h < FIB6_TABLE_HASHSZ) {
1982 node = rcu_dereference_bh(
1983 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1984 }
1985 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1986}
1987
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001988static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1989{
1990 if (iter->sernum != iter->w.root->fn_sernum) {
1991 iter->sernum = iter->w.root->fn_sernum;
1992 iter->w.state = FWS_INIT;
1993 iter->w.node = iter->w.root;
1994 WARN_ON(iter->w.skip);
1995 iter->w.skip = iter->w.count;
1996 }
1997}
1998
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001999static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2000{
2001 int r;
2002 struct rt6_info *n;
2003 struct net *net = seq_file_net(seq);
2004 struct ipv6_route_iter *iter = seq->private;
2005
2006 if (!v)
2007 goto iter_table;
2008
2009 n = ((struct rt6_info *)v)->dst.rt6_next;
2010 if (n) {
2011 ++*pos;
2012 return n;
2013 }
2014
2015iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002016 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002017 read_lock(&iter->tbl->tb6_lock);
2018 r = fib6_walk_continue(&iter->w);
2019 read_unlock(&iter->tbl->tb6_lock);
2020 if (r > 0) {
2021 if (v)
2022 ++*pos;
2023 return iter->w.leaf;
2024 } else if (r < 0) {
2025 fib6_walker_unlink(&iter->w);
2026 return NULL;
2027 }
2028 fib6_walker_unlink(&iter->w);
2029
2030 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2031 if (!iter->tbl)
2032 return NULL;
2033
2034 ipv6_route_seq_setup_walk(iter);
2035 goto iter_table;
2036}
2037
2038static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2039 __acquires(RCU_BH)
2040{
2041 struct net *net = seq_file_net(seq);
2042 struct ipv6_route_iter *iter = seq->private;
2043
2044 rcu_read_lock_bh();
2045 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2046 iter->skip = *pos;
2047
2048 if (iter->tbl) {
2049 ipv6_route_seq_setup_walk(iter);
2050 return ipv6_route_seq_next(seq, NULL, pos);
2051 } else {
2052 return NULL;
2053 }
2054}
2055
2056static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2057{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002058 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002059 return w->node && !(w->state == FWS_U && w->node == w->root);
2060}
2061
2062static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2063 __releases(RCU_BH)
2064{
2065 struct ipv6_route_iter *iter = seq->private;
2066
2067 if (ipv6_route_iter_active(iter))
2068 fib6_walker_unlink(&iter->w);
2069
2070 rcu_read_unlock_bh();
2071}
2072
2073static const struct seq_operations ipv6_route_seq_ops = {
2074 .start = ipv6_route_seq_start,
2075 .next = ipv6_route_seq_next,
2076 .stop = ipv6_route_seq_stop,
2077 .show = ipv6_route_seq_show
2078};
2079
2080int ipv6_route_open(struct inode *inode, struct file *file)
2081{
2082 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2083 sizeof(struct ipv6_route_iter));
2084}
2085
2086#endif /* CONFIG_PROC_FS */