blob: e68350bf838b1276d571e6ee121aa343c3e024c4 [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
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700936 if (WARN_ON_ONCE((rt->dst.flags & DST_NOCACHE) &&
937 !atomic_read(&rt->dst.__refcnt)))
938 return -EINVAL;
939
David S. Miller507c9b12011-12-03 17:50:45 -0500940 if (info->nlh) {
941 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000942 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500943 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000944 replace_required = 1;
945 }
946 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000947 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
fan.du9225b232013-07-22 14:21:09 +0800949 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
950 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200951 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000952 if (IS_ERR(fn)) {
953 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200954 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700958 pn = fn;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960#ifdef CONFIG_IPV6_SUBTREES
961 if (rt->rt6i_src.plen) {
962 struct fib6_node *sn;
963
David S. Miller507c9b12011-12-03 17:50:45 -0500964 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct fib6_node *sfn;
966
967 /*
968 * Create subtree.
969 *
970 * fn[main tree]
971 * |
972 * sfn[subtree root]
973 * \
974 * sn[new leaf node]
975 */
976
977 /* Create subtree root node */
978 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500979 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 goto st_failure;
981
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800982 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
983 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200985 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Now add the first leaf node to new subtree */
988
989 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800990 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000991 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200992 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000994 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* If it is failed, discard just allocated
996 root, and then (in st_failure) stale node
997 in main tree.
998 */
999 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001000 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto st_failure;
1002 }
1003
1004 /* Now link new subtree to main tree */
1005 sfn->parent = fn;
1006 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
1008 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001009 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001010 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001011 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001013 if (IS_ERR(sn)) {
1014 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
David S. Miller507c9b12011-12-03 17:50:45 -05001019 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001020 fn->leaf = rt;
1021 atomic_inc(&rt->rt6i_ref);
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 fn = sn;
1024 }
1025#endif
1026
Florian Westphale715b6d2015-01-05 23:57:44 +01001027 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001028 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001029 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001030 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001031 fib6_prune_clones(info->nl_net, pn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001032 rt->dst.flags &= ~DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001036 if (err) {
1037#ifdef CONFIG_IPV6_SUBTREES
1038 /*
1039 * If fib6_add_1 has cleared the old leaf pointer in the
1040 * super-tree leaf node we have to find a new one for it.
1041 */
David S. Miller3c051232008-04-18 01:46:19 -07001042 if (pn != fn && pn->leaf == rt) {
1043 pn->leaf = NULL;
1044 atomic_dec(&rt->rt6i_ref);
1045 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001046 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001047 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001048#if RT6_DEBUG >= 2
1049 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001050 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001051 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001052 }
1053#endif
1054 atomic_inc(&pn->leaf->rt6i_ref);
1055 }
1056#endif
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001057 if (!(rt->dst.flags & DST_NOCACHE))
1058 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return err;
1061
1062#ifdef CONFIG_IPV6_SUBTREES
1063 /* Subtree creation failed, probably main tree node
1064 is orphan. If it is, shoot it.
1065 */
1066st_failure:
1067 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001068 fib6_repair_tree(info->nl_net, fn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001069 if (!(rt->dst.flags & DST_NOCACHE))
1070 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return err;
1072#endif
1073}
1074
1075/*
1076 * Routing tree lookup
1077 *
1078 */
1079
1080struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001081 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001082 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083};
1084
Wang Yufen437de072014-03-28 12:07:04 +08001085static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1086 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001089 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001091 if (unlikely(args->offset == 0))
1092 return NULL;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /*
1095 * Descend on a tree
1096 */
1097
1098 fn = root;
1099
1100 for (;;) {
1101 struct fib6_node *next;
1102
1103 dir = addr_bit_set(args->addr, fn->fn_bit);
1104
1105 next = dir ? fn->right : fn->left;
1106
1107 if (next) {
1108 fn = next;
1109 continue;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112 }
1113
David S. Miller507c9b12011-12-03 17:50:45 -05001114 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001115 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct rt6key *key;
1117
1118 key = (struct rt6key *) ((u8 *) fn->leaf +
1119 args->offset);
1120
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001121 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1122#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001123 if (fn->subtree) {
1124 struct fib6_node *sfn;
1125 sfn = fib6_lookup_1(fn->subtree,
1126 args + 1);
1127 if (!sfn)
1128 goto backtrack;
1129 fn = sfn;
1130 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001131#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001132 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001133 return fn;
1134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001136#ifdef CONFIG_IPV6_SUBTREES
1137backtrack:
1138#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001139 if (fn->fn_flags & RTN_ROOT)
1140 break;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 fn = fn->parent;
1143 }
1144
1145 return NULL;
1146}
1147
Wang Yufen437de072014-03-28 12:07:04 +08001148struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1149 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001152 struct lookup_args args[] = {
1153 {
1154 .offset = offsetof(struct rt6_info, rt6i_dst),
1155 .addr = daddr,
1156 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001158 {
1159 .offset = offsetof(struct rt6_info, rt6i_src),
1160 .addr = saddr,
1161 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001163 {
1164 .offset = 0, /* sentinel */
1165 }
1166 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001168 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001169 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 fn = root;
1171
1172 return fn;
1173}
1174
1175/*
1176 * Get node with specified destination prefix (and source prefix,
1177 * if subtrees are used)
1178 */
1179
1180
Wang Yufen437de072014-03-28 12:07:04 +08001181static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1182 const struct in6_addr *addr,
1183 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct fib6_node *fn;
1186
1187 for (fn = root; fn ; ) {
1188 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1189
1190 /*
1191 * Prefix match
1192 */
1193 if (plen < fn->fn_bit ||
1194 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1195 return NULL;
1196
1197 if (plen == fn->fn_bit)
1198 return fn;
1199
1200 /*
1201 * We have more bits to go
1202 */
1203 if (addr_bit_set(addr, fn->fn_bit))
1204 fn = fn->right;
1205 else
1206 fn = fn->left;
1207 }
1208 return NULL;
1209}
1210
Wang Yufen437de072014-03-28 12:07:04 +08001211struct fib6_node *fib6_locate(struct fib6_node *root,
1212 const struct in6_addr *daddr, int dst_len,
1213 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct fib6_node *fn;
1216
1217 fn = fib6_locate_1(root, daddr, dst_len,
1218 offsetof(struct rt6_info, rt6i_dst));
1219
1220#ifdef CONFIG_IPV6_SUBTREES
1221 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001222 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001223 if (fn && fn->subtree)
1224 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 offsetof(struct rt6_info, rt6i_src));
1226 }
1227#endif
1228
David S. Miller507c9b12011-12-03 17:50:45 -05001229 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return fn;
1231
1232 return NULL;
1233}
1234
1235
1236/*
1237 * Deletion
1238 *
1239 */
1240
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001241static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
David S. Miller507c9b12011-12-03 17:50:45 -05001243 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001244 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
David S. Miller507c9b12011-12-03 17:50:45 -05001246 while (fn) {
1247 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001249 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return fn->right->leaf;
1251
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001252 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 return NULL;
1255}
1256
1257/*
1258 * Called to trim the tree of intermediate nodes when possible. "fn"
1259 * is the node we want to try and remove.
1260 */
1261
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001262static struct fib6_node *fib6_repair_tree(struct net *net,
1263 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 int children;
1266 int nstate;
1267 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001268 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int iter = 0;
1270
1271 for (;;) {
1272 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1273 iter++;
1274
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001275 WARN_ON(fn->fn_flags & RTN_RTINFO);
1276 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001277 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 children = 0;
1280 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001281 if (fn->right)
1282 child = fn->right, children |= 1;
1283 if (fn->left)
1284 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001286 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#ifdef CONFIG_IPV6_SUBTREES
1288 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001289 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290#endif
1291 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001292 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001294 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001295 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001296 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298#endif
1299 atomic_inc(&fn->leaf->rt6i_ref);
1300 return fn->parent;
1301 }
1302
1303 pn = fn->parent;
1304#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001305 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001306 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001307 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 nstate = FWS_L;
1309 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001310 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001312 if (pn->right == fn)
1313 pn->right = child;
1314 else if (pn->left == fn)
1315 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001317 else
1318 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#endif
1320 if (child)
1321 child->parent = pn;
1322 nstate = FWS_R;
1323#ifdef CONFIG_IPV6_SUBTREES
1324 }
1325#endif
1326
1327 read_lock(&fib6_walker_lock);
1328 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001329 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (w->root == fn) {
1331 w->root = w->node = NULL;
1332 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1333 } else if (w->node == fn) {
1334 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1335 w->node = pn;
1336 w->state = nstate;
1337 }
1338 } else {
1339 if (w->root == fn) {
1340 w->root = child;
1341 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1342 }
1343 if (w->node == fn) {
1344 w->node = child;
1345 if (children&2) {
1346 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001347 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 } else {
1349 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001350 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 }
1353 }
1354 }
1355 read_unlock(&fib6_walker_lock);
1356
1357 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001358 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return pn;
1360
1361 rt6_release(pn->leaf);
1362 pn->leaf = NULL;
1363 fn = pn;
1364 }
1365}
1366
1367static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001368 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001370 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001372 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 RT6_TRACE("fib6_del_route\n");
1375
1376 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001377 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001379 net->ipv6.rt6_stats->fib_rt_entries--;
1380 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
David S. Millerf11e6652007-03-24 20:36:25 -07001382 /* Reset round-robin state, if necessary */
1383 if (fn->rr_ptr == rt)
1384 fn->rr_ptr = NULL;
1385
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001386 /* Remove this entry from other siblings */
1387 if (rt->rt6i_nsiblings) {
1388 struct rt6_info *sibling, *next_sibling;
1389
1390 list_for_each_entry_safe(sibling, next_sibling,
1391 &rt->rt6i_siblings, rt6i_siblings)
1392 sibling->rt6i_nsiblings--;
1393 rt->rt6i_nsiblings = 0;
1394 list_del_init(&rt->rt6i_siblings);
1395 }
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* Adjust walkers */
1398 read_lock(&fib6_walker_lock);
1399 FOR_WALKERS(w) {
1400 if (w->state == FWS_C && w->leaf == rt) {
1401 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001402 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001403 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 w->state = FWS_U;
1405 }
1406 }
1407 read_unlock(&fib6_walker_lock);
1408
Changli Gaod8d1f302010-06-10 23:31:35 -07001409 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001412 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001414 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001415 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001418 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Thomas Graf86872cb2006-08-22 00:01:08 -07001420 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 rt6_release(rt);
1422}
1423
Thomas Graf86872cb2006-08-22 00:01:08 -07001424int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001426 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct fib6_node *fn = rt->rt6i_node;
1428 struct rt6_info **rtp;
1429
1430#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001431 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001432 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -ENOENT;
1434 }
1435#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001436 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -ENOENT;
1438
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001439 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
David S. Miller507c9b12011-12-03 17:50:45 -05001441 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001442 struct fib6_node *pn = fn;
1443#ifdef CONFIG_IPV6_SUBTREES
1444 /* clones of this route might be in another subtree */
1445 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001446 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001447 pn = pn->parent;
1448 pn = pn->parent;
1449 }
1450#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001451 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /*
1455 * Walk the leaf entries looking for ourself
1456 */
1457
Changli Gaod8d1f302010-06-10 23:31:35 -07001458 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001460 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462 }
1463 }
1464 return -ENOENT;
1465}
1466
1467/*
1468 * Tree traversal function.
1469 *
1470 * Certainly, it is not interrupt safe.
1471 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1472 * It means, that we can modify tree during walking
1473 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001474 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
1476 * It guarantees that every node will be traversed,
1477 * and that it will be traversed only once.
1478 *
1479 * Callback function w->func may return:
1480 * 0 -> continue walking.
1481 * positive value -> walking is suspended (used by tree dumps,
1482 * and probably by gc, if it will be split to several slices)
1483 * negative value -> terminate walking.
1484 *
1485 * The function itself returns:
1486 * 0 -> walk is complete.
1487 * >0 -> walk is incomplete (i.e. suspended)
1488 * <0 -> walk is terminated by an error.
1489 */
1490
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001491static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct fib6_node *fn, *pn;
1494
1495 for (;;) {
1496 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001497 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499
1500 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001501 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 w->state = FWS_C;
1503 w->leaf = fn->leaf;
1504 }
1505 switch (w->state) {
1506#ifdef CONFIG_IPV6_SUBTREES
1507 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001508 if (FIB6_SUBTREE(fn)) {
1509 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 continue;
1511 }
1512 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 case FWS_L:
1515 if (fn->left) {
1516 w->node = fn->left;
1517 w->state = FWS_INIT;
1518 continue;
1519 }
1520 w->state = FWS_R;
1521 case FWS_R:
1522 if (fn->right) {
1523 w->node = fn->right;
1524 w->state = FWS_INIT;
1525 continue;
1526 }
1527 w->state = FWS_C;
1528 w->leaf = fn->leaf;
1529 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001530 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001531 int err;
1532
Eric Dumazetfa809e22012-06-25 15:37:19 -07001533 if (w->skip) {
1534 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001535 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001536 }
1537
1538 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (err)
1540 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001541
1542 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 continue;
1544 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001545skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 w->state = FWS_U;
1547 case FWS_U:
1548 if (fn == w->root)
1549 return 0;
1550 pn = fn->parent;
1551 w->node = pn;
1552#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001553 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001554 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 w->state = FWS_L;
1556 continue;
1557 }
1558#endif
1559 if (pn->left == fn) {
1560 w->state = FWS_R;
1561 continue;
1562 }
1563 if (pn->right == fn) {
1564 w->state = FWS_C;
1565 w->leaf = w->node->leaf;
1566 continue;
1567 }
1568#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001569 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570#endif
1571 }
1572 }
1573}
1574
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001575static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 int res;
1578
1579 w->state = FWS_INIT;
1580 w->node = w->root;
1581
1582 fib6_walker_link(w);
1583 res = fib6_walk_continue(w);
1584 if (res <= 0)
1585 fib6_walker_unlink(w);
1586 return res;
1587}
1588
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001589static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 int res;
1592 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001593 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001594 struct nl_info info = {
1595 .nl_net = c->net,
1596 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001598 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1599 w->node->fn_sernum != c->sernum)
1600 w->node->fn_sernum = c->sernum;
1601
1602 if (!c->func) {
1603 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1604 w->leaf = NULL;
1605 return 0;
1606 }
1607
Changli Gaod8d1f302010-06-10 23:31:35 -07001608 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 res = c->func(rt, c->arg);
1610 if (res < 0) {
1611 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001612 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (res) {
1614#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001615 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1616 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617#endif
1618 continue;
1619 }
1620 return 0;
1621 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001622 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624 w->leaf = rt;
1625 return 0;
1626}
1627
1628/*
1629 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 * func is called on each route.
1632 * It may return -1 -> delete this route.
1633 * 0 -> continue walking
1634 *
1635 * prune==1 -> only immediate children of node (certainly,
1636 * ignoring pure split nodes) will be scanned.
1637 */
1638
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001639static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001640 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001641 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001643 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 c.w.root = root;
1646 c.w.func = fib6_clean_node;
1647 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001648 c.w.count = 0;
1649 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001651 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001653 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 fib6_walk(&c.w);
1656}
1657
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001658static void __fib6_clean_all(struct net *net,
1659 int (*func)(struct rt6_info *, void *),
1660 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001661{
Thomas Grafc71099a2006-08-04 23:20:06 -07001662 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001663 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001664 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001665
Patrick McHardy1b43af52006-08-10 23:11:17 -07001666 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001667 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001668 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001669 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001670 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001671 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001672 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001673 write_unlock_bh(&table->tb6_lock);
1674 }
1675 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001676 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001677}
1678
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001679void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1680 void *arg)
1681{
1682 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1683}
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1686{
1687 if (rt->rt6i_flags & RTF_CACHE) {
1688 RT6_TRACE("pruning clone %p\n", rt);
1689 return -1;
1690 }
1691
1692 return 0;
1693}
1694
Duan Jiong163cd4e2014-05-09 13:31:43 +08001695static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001697 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1698 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001699}
1700
1701static void fib6_flush_trees(struct net *net)
1702{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001703 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001704
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001705 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/*
1709 * Garbage collection
1710 */
1711
1712static struct fib6_gc_args
1713{
1714 int timeout;
1715 int more;
1716} gc_args;
1717
1718static int fib6_age(struct rt6_info *rt, void *arg)
1719{
1720 unsigned long now = jiffies;
1721
1722 /*
1723 * check addrconf expiration here.
1724 * Routes are expired even if they are in use.
1725 *
1726 * Also age clones. Note, that clones are aged out
1727 * only if they are not in use now.
1728 */
1729
David S. Millerd1918542011-12-28 20:19:20 -05001730 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1731 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return -1;
1734 }
1735 gc_args.more++;
1736 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001737 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1738 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 RT6_TRACE("aging clone %p\n", rt);
1740 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001741 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1742 struct neighbour *neigh;
1743 __u8 neigh_flags = 0;
1744
1745 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1746 if (neigh) {
1747 neigh_flags = neigh->flags;
1748 neigh_release(neigh);
1749 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001750 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001751 RT6_TRACE("purging route %p via non-router but gateway\n",
1752 rt);
1753 return -1;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 gc_args.more++;
1757 }
1758
1759 return 0;
1760}
1761
1762static DEFINE_SPINLOCK(fib6_gc_lock);
1763
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001764void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001766 unsigned long now;
1767
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001768 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001770 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1771 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1772 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001774 gc_args.timeout = expires ? (int)expires :
1775 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001777 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001778
Li RongQing0c3584d2013-12-27 16:32:38 +08001779 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001780 now = jiffies;
1781 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001784 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001785 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001786 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001787 else
1788 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 spin_unlock_bh(&fib6_gc_lock);
1790}
1791
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001792static void fib6_gc_timer_cb(unsigned long arg)
1793{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001794 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001795}
1796
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001797static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001798{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001799 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1800
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001801 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001802
Benjamin Theryc5728722008-03-03 23:34:17 -08001803 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1804 if (!net->ipv6.rt6_stats)
1805 goto out_timer;
1806
Eric Dumazet10da66f2010-10-13 08:22:03 +00001807 /* Avoid false sharing : Use at least a full cache line */
1808 size = max_t(size_t, size, L1_CACHE_BYTES);
1809
1810 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001811 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001812 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001813
1814 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1815 GFP_KERNEL);
1816 if (!net->ipv6.fib6_main_tbl)
1817 goto out_fib_table_hash;
1818
1819 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001820 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001821 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1822 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001823 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001824
1825#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1826 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1827 GFP_KERNEL);
1828 if (!net->ipv6.fib6_local_tbl)
1829 goto out_fib6_main_tbl;
1830 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001831 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001832 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1833 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001834 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001835#endif
1836 fib6_tables_init(net);
1837
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001838 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001839
1840#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1841out_fib6_main_tbl:
1842 kfree(net->ipv6.fib6_main_tbl);
1843#endif
1844out_fib_table_hash:
1845 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001846out_rt6_stats:
1847 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001848out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001849 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001850}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001851
1852static void fib6_net_exit(struct net *net)
1853{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001854 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001855 del_timer_sync(&net->ipv6.ip6_fib_timer);
1856
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001857#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001858 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001859 kfree(net->ipv6.fib6_local_tbl);
1860#endif
David S. Miller8e773272012-06-11 00:01:52 -07001861 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001862 kfree(net->ipv6.fib6_main_tbl);
1863 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001864 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001865}
1866
1867static struct pernet_operations fib6_net_ops = {
1868 .init = fib6_net_init,
1869 .exit = fib6_net_exit,
1870};
1871
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001872int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001874 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1877 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001878 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001879 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001880 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001881 goto out;
1882
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001883 ret = register_pernet_subsys(&fib6_net_ops);
1884 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001885 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001886
1887 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1888 NULL);
1889 if (ret)
1890 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001891
1892 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001893out:
1894 return ret;
1895
David S. Millere8803b62012-06-16 01:12:19 -07001896out_unregister_subsys:
1897 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001898out_kmem_cache_create:
1899 kmem_cache_destroy(fib6_node_kmem);
1900 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
1903void fib6_gc_cleanup(void)
1904{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001905 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 kmem_cache_destroy(fib6_node_kmem);
1907}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001908
1909#ifdef CONFIG_PROC_FS
1910
1911struct ipv6_route_iter {
1912 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001913 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001914 loff_t skip;
1915 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001916 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001917};
1918
1919static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1920{
1921 struct rt6_info *rt = v;
1922 struct ipv6_route_iter *iter = seq->private;
1923
1924 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1925
1926#ifdef CONFIG_IPV6_SUBTREES
1927 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1928#else
1929 seq_puts(seq, "00000000000000000000000000000000 00 ");
1930#endif
1931 if (rt->rt6i_flags & RTF_GATEWAY)
1932 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1933 else
1934 seq_puts(seq, "00000000000000000000000000000000");
1935
1936 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1937 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1938 rt->dst.__use, rt->rt6i_flags,
1939 rt->dst.dev ? rt->dst.dev->name : "");
1940 iter->w.leaf = NULL;
1941 return 0;
1942}
1943
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001944static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001945{
1946 struct ipv6_route_iter *iter = w->args;
1947
1948 if (!iter->skip)
1949 return 1;
1950
1951 do {
1952 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1953 iter->skip--;
1954 if (!iter->skip && iter->w.leaf)
1955 return 1;
1956 } while (iter->w.leaf);
1957
1958 return 0;
1959}
1960
1961static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1962{
1963 memset(&iter->w, 0, sizeof(iter->w));
1964 iter->w.func = ipv6_route_yield;
1965 iter->w.root = &iter->tbl->tb6_root;
1966 iter->w.state = FWS_INIT;
1967 iter->w.node = iter->w.root;
1968 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001969 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001970 INIT_LIST_HEAD(&iter->w.lh);
1971 fib6_walker_link(&iter->w);
1972}
1973
1974static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1975 struct net *net)
1976{
1977 unsigned int h;
1978 struct hlist_node *node;
1979
1980 if (tbl) {
1981 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1982 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1983 } else {
1984 h = 0;
1985 node = NULL;
1986 }
1987
1988 while (!node && h < FIB6_TABLE_HASHSZ) {
1989 node = rcu_dereference_bh(
1990 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1991 }
1992 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1993}
1994
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001995static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1996{
1997 if (iter->sernum != iter->w.root->fn_sernum) {
1998 iter->sernum = iter->w.root->fn_sernum;
1999 iter->w.state = FWS_INIT;
2000 iter->w.node = iter->w.root;
2001 WARN_ON(iter->w.skip);
2002 iter->w.skip = iter->w.count;
2003 }
2004}
2005
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002006static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2007{
2008 int r;
2009 struct rt6_info *n;
2010 struct net *net = seq_file_net(seq);
2011 struct ipv6_route_iter *iter = seq->private;
2012
2013 if (!v)
2014 goto iter_table;
2015
2016 n = ((struct rt6_info *)v)->dst.rt6_next;
2017 if (n) {
2018 ++*pos;
2019 return n;
2020 }
2021
2022iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002023 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002024 read_lock(&iter->tbl->tb6_lock);
2025 r = fib6_walk_continue(&iter->w);
2026 read_unlock(&iter->tbl->tb6_lock);
2027 if (r > 0) {
2028 if (v)
2029 ++*pos;
2030 return iter->w.leaf;
2031 } else if (r < 0) {
2032 fib6_walker_unlink(&iter->w);
2033 return NULL;
2034 }
2035 fib6_walker_unlink(&iter->w);
2036
2037 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2038 if (!iter->tbl)
2039 return NULL;
2040
2041 ipv6_route_seq_setup_walk(iter);
2042 goto iter_table;
2043}
2044
2045static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2046 __acquires(RCU_BH)
2047{
2048 struct net *net = seq_file_net(seq);
2049 struct ipv6_route_iter *iter = seq->private;
2050
2051 rcu_read_lock_bh();
2052 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2053 iter->skip = *pos;
2054
2055 if (iter->tbl) {
2056 ipv6_route_seq_setup_walk(iter);
2057 return ipv6_route_seq_next(seq, NULL, pos);
2058 } else {
2059 return NULL;
2060 }
2061}
2062
2063static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2064{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002065 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002066 return w->node && !(w->state == FWS_U && w->node == w->root);
2067}
2068
2069static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2070 __releases(RCU_BH)
2071{
2072 struct ipv6_route_iter *iter = seq->private;
2073
2074 if (ipv6_route_iter_active(iter))
2075 fib6_walker_unlink(&iter->w);
2076
2077 rcu_read_unlock_bh();
2078}
2079
2080static const struct seq_operations ipv6_route_seq_ops = {
2081 .start = ipv6_route_seq_start,
2082 .next = ipv6_route_seq_next,
2083 .stop = ipv6_route_seq_stop,
2084 .show = ipv6_route_seq_show
2085};
2086
2087int ipv6_route_open(struct inode *inode, struct file *file)
2088{
2089 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2090 sizeof(struct ipv6_route_iter));
2091}
2092
2093#endif /* CONFIG_PROC_FS */