blob: 548c6237b1e706f8ef72575a6a7dbad544b60fcc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
35
36#include <net/ip6_fib.h>
37#include <net/ip6_route.h>
38
39#define RT6_DEBUG 2
40
41#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000042#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define RT6_TRACE(x...) do { ; } while (0)
45#endif
46
Wang Yufen437de072014-03-28 12:07:04 +080047static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020049struct fib6_cleaner {
50 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080051 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020053 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 void *arg;
55};
56
Adrian Bunk90d41122006-08-14 23:49:16 -070057static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#ifdef CONFIG_IPV6_SUBTREES
60#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
62#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Duan Jiong163cd4e2014-05-09 13:31:43 +080065static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080066static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
67static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020068static int fib6_walk(struct fib6_walker *w);
69static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
76 */
77
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080078static void fib6_gc_timer_cb(unsigned long arg);
79
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000080static LIST_HEAD(fib6_walkers);
81#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020083static void fib6_walker_link(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070084{
85 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000086 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -070087 write_unlock_bh(&fib6_walker_lock);
88}
89
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020090static void fib6_walker_unlink(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070091{
92 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000093 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -070094 write_unlock_bh(&fib6_walker_lock);
95}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020096
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020099 int new, old;
100
101 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200102 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200103 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200104 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
105 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200106 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200109enum {
110 FIB6_NO_SERNUM_CHANGE = 0,
111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * Auxiliary address test functions for the radix tree.
115 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900116 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * 64bit processors)
118 */
119
120/*
121 * test bit
122 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000123#if defined(__LITTLE_ENDIAN)
124# define BITOP_BE32_SWIZZLE (0x1F & ~7)
125#else
126# define BITOP_BE32_SWIZZLE 0
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200129static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000131 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000132 /*
133 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
138 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700139 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
140 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200143static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct fib6_node *fn;
146
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800147 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return fn;
150}
151
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200152static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 kmem_cache_free(fib6_node_kmem, fn);
155}
156
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700157static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
158{
159 int cpu;
160
161 if (!non_pcpu_rt->rt6i_pcpu)
162 return;
163
164 for_each_possible_cpu(cpu) {
165 struct rt6_info **ppcpu_rt;
166 struct rt6_info *pcpu_rt;
167
168 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
169 pcpu_rt = *ppcpu_rt;
170 if (pcpu_rt) {
171 dst_free(&pcpu_rt->dst);
172 *ppcpu_rt = NULL;
173 }
174 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700175
176 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700177}
178
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200179static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700181 if (atomic_dec_and_test(&rt->rt6i_ref)) {
182 rt6_free_pcpu(rt);
Changli Gaod8d1f302010-06-10 23:31:35 -0700183 dst_free(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800187static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700188{
189 unsigned int h;
190
Thomas Graf375216a2006-10-21 20:20:54 -0700191 /*
192 * Initialize table lock at a single place to give lockdep a key,
193 * tables aren't visible prior to being linked to the list.
194 */
195 rwlock_init(&tb->tb6_lock);
196
Neil Hormana33bc5c2009-07-30 18:52:15 -0700197 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700198
199 /*
200 * No protection necessary, this is the only list mutatation
201 * operation, tables never disappear once they exist.
202 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800203 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700204}
205
206#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800207
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800208static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700209{
210 struct fib6_table *table;
211
212 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500213 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700214 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800215 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700216 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700217 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700218 }
219
220 return table;
221}
222
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800223struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700224{
225 struct fib6_table *tb;
226
227 if (id == 0)
228 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800229 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700230 if (tb)
231 return tb;
232
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800233 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500234 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800235 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700236
237 return tb;
238}
239
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700241{
242 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800243 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700244 unsigned int h;
245
246 if (id == 0)
247 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700248 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700249 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800251 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700252 if (tb->tb6_id == id) {
253 rcu_read_unlock();
254 return tb;
255 }
256 }
257 rcu_read_unlock();
258
259 return NULL;
260}
261
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000262static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700263{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800264 fib6_link_table(net, net->ipv6.fib6_main_tbl);
265 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700266}
Thomas Grafc71099a2006-08-04 23:20:06 -0700267#else
268
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800269struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700270{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800271 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700272}
273
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800274struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700275{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800276 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700277}
278
David S. Miller4c9483b2011-03-12 16:22:43 -0500279struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800280 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700281{
David S. Miller4c9483b2011-03-12 16:22:43 -0500282 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700283}
284
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000285static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700286{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800287 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700288}
289
290#endif
291
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200292static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700293{
294 int res;
295 struct rt6_info *rt;
296
Changli Gaod8d1f302010-06-10 23:31:35 -0700297 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700298 res = rt6_dump_route(rt, w->args);
299 if (res < 0) {
300 /* Frame is full, suspend walking */
301 w->leaf = rt;
302 return 1;
303 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700304 }
305 w->leaf = NULL;
306 return 0;
307}
308
309static void fib6_dump_end(struct netlink_callback *cb)
310{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200311 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700312
313 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800314 if (cb->args[4]) {
315 cb->args[4] = 0;
316 fib6_walker_unlink(w);
317 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700318 cb->args[2] = 0;
319 kfree(w);
320 }
Wang Yufen437de072014-03-28 12:07:04 +0800321 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700322 cb->args[1] = 3;
323}
324
325static int fib6_dump_done(struct netlink_callback *cb)
326{
327 fib6_dump_end(cb);
328 return cb->done ? cb->done(cb) : 0;
329}
330
331static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
332 struct netlink_callback *cb)
333{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200334 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700335 int res;
336
337 w = (void *)cb->args[2];
338 w->root = &table->tb6_root;
339
340 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000341 w->count = 0;
342 w->skip = 0;
343
Patrick McHardy1b43af52006-08-10 23:11:17 -0700344 read_lock_bh(&table->tb6_lock);
345 res = fib6_walk(w);
346 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000347 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700348 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000349 cb->args[5] = w->root->fn_sernum;
350 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700351 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000352 if (cb->args[5] != w->root->fn_sernum) {
353 /* Begin at the root if the tree changed */
354 cb->args[5] = w->root->fn_sernum;
355 w->state = FWS_INIT;
356 w->node = w->root;
357 w->skip = w->count;
358 } else
359 w->skip = 0;
360
Patrick McHardy1b43af52006-08-10 23:11:17 -0700361 read_lock_bh(&table->tb6_lock);
362 res = fib6_walk_continue(w);
363 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800364 if (res <= 0) {
365 fib6_walker_unlink(w);
366 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700367 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700368 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800369
Patrick McHardy1b43af52006-08-10 23:11:17 -0700370 return res;
371}
372
Thomas Grafc127ea22007-03-22 11:58:32 -0700373static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700374{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900375 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700376 unsigned int h, s_h;
377 unsigned int e = 0, s_e;
378 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200379 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700380 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800381 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700382 int res = 0;
383
384 s_h = cb->args[0];
385 s_e = cb->args[1];
386
387 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500388 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700389 /* New dump:
390 *
391 * 1. hook callback destructor.
392 */
393 cb->args[3] = (long)cb->done;
394 cb->done = fib6_dump_done;
395
396 /*
397 * 2. allocate and initialize walker.
398 */
399 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500400 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700401 return -ENOMEM;
402 w->func = fib6_dump_node;
403 cb->args[2] = (long)w;
404 }
405
406 arg.skb = skb;
407 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700408 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700409 w->args = &arg;
410
Eric Dumazete67f88d2011-04-27 22:56:07 +0000411 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700412 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700413 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800414 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700416 if (e < s_e)
417 goto next;
418 res = fib6_dump_table(tb, skb, cb);
419 if (res != 0)
420 goto out;
421next:
422 e++;
423 }
424 }
425out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000426 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700427 cb->args[1] = e;
428 cb->args[0] = h;
429
430 res = res < 0 ? res : skb->len;
431 if (res <= 0)
432 fib6_dump_end(cb);
433 return res;
434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/*
437 * Routing Table
438 *
439 * return the appropriate node for a routing tree "add" operation
440 * by either creating and inserting or by returning an existing
441 * node.
442 */
443
fan.du9225b232013-07-22 14:21:09 +0800444static struct fib6_node *fib6_add_1(struct fib6_node *root,
445 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000446 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200447 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct fib6_node *fn, *in, *ln;
450 struct fib6_node *pn = NULL;
451 struct rt6key *key;
452 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900453 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 RT6_TRACE("fib6_add_1\n");
456
457 /* insert node in tree */
458
459 fn = root;
460
461 do {
462 key = (struct rt6key *)((u8 *)fn->leaf + offset);
463
464 /*
465 * Prefix match
466 */
467 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000468 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000469 if (!allow_create) {
470 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000471 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000472 return ERR_PTR(-ENOENT);
473 }
Joe Perchesf3213832012-05-15 14:11:53 +0000474 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000477 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /*
480 * Exact match ?
481 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (plen == fn->fn_bit) {
484 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500485 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 rt6_release(fn->leaf);
487 fn->leaf = NULL;
488 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return fn;
493 }
494
495 /*
496 * We have more bits to go
497 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Try to walk down on tree. */
500 fn->fn_sernum = sernum;
501 dir = addr_bit_set(addr, fn->fn_bit);
502 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800503 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 } while (fn);
505
Matti Vaittinen14df0152011-11-16 21:18:02 +0000506 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000507 /* We should not create new node because
508 * NLM_F_REPLACE was specified without NLM_F_CREATE
509 * I assume it is safe to require NLM_F_CREATE when
510 * REPLACE flag is used! Later we may want to remove the
511 * check for replace_required, because according
512 * to netlink specification, NLM_F_CREATE
513 * MUST be specified if new route is created.
514 * That would keep IPv6 consistent with IPv4
515 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000516 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000517 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000518 return ERR_PTR(-ENOENT);
519 }
Joe Perchesf3213832012-05-15 14:11:53 +0000520 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * We walked to the bottom of tree.
524 * Create new leaf node without children.
525 */
526
527 ln = node_alloc();
528
David S. Miller507c9b12011-12-03 17:50:45 -0500529 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000530 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ln->parent = pn;
534 ln->fn_sernum = sernum;
535
536 if (dir)
537 pn->right = ln;
538 else
539 pn->left = ln;
540
541 return ln;
542
543
544insert_above:
545 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900546 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * we have a less significant route.
548 * we've to insert an intermediate node on the list
549 * this new node will point to the one we need to create
550 * and the current
551 */
552
553 pn = fn->parent;
554
555 /* find 1st bit in difference between the 2 addrs.
556
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800557 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 but if it is >= plen, the value is ignored in any case.
559 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900560
fan.du9225b232013-07-22 14:21:09 +0800561 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900563 /*
564 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * / \
566 * (new leaf node)[ln] (old node)[fn]
567 */
568 if (plen > bit) {
569 in = node_alloc();
570 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900571
David S. Miller507c9b12011-12-03 17:50:45 -0500572 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (in)
574 node_free(in);
575 if (ln)
576 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000577 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900580 /*
581 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * RTN_RTINFO will
583 * be off since that an address that chooses one of
584 * the branches would not match less specific routes
585 * in the other branch
586 */
587
588 in->fn_bit = bit;
589
590 in->parent = pn;
591 in->leaf = fn->leaf;
592 atomic_inc(&in->leaf->rt6i_ref);
593
594 in->fn_sernum = sernum;
595
596 /* update parent pointer */
597 if (dir)
598 pn->right = in;
599 else
600 pn->left = in;
601
602 ln->fn_bit = plen;
603
604 ln->parent = in;
605 fn->parent = in;
606
607 ln->fn_sernum = sernum;
608
609 if (addr_bit_set(addr, bit)) {
610 in->right = ln;
611 in->left = fn;
612 } else {
613 in->left = ln;
614 in->right = fn;
615 }
616 } else { /* plen <= bit */
617
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900618 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * (new leaf node)[ln]
620 * / \
621 * (old node)[fn] NULL
622 */
623
624 ln = node_alloc();
625
David S. Miller507c9b12011-12-03 17:50:45 -0500626 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000627 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 ln->fn_bit = plen;
630
631 ln->parent = pn;
632
633 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (dir)
636 pn->right = ln;
637 else
638 pn->left = ln;
639
640 if (addr_bit_set(&key->addr, plen))
641 ln->right = fn;
642 else
643 ln->left = fn;
644
645 fn->parent = ln;
646 }
647 return ln;
648}
649
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200650static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200651{
652 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
653 RTF_GATEWAY;
654}
655
Florian Westphale715b6d2015-01-05 23:57:44 +0100656static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100657{
Florian Westphale715b6d2015-01-05 23:57:44 +0100658 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100659
Florian Westphale715b6d2015-01-05 23:57:44 +0100660 for (i = 0; i < RTAX_MAX; i++) {
661 if (test_bit(i, mxc->mx_valid))
662 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100663 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100664}
665
666static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
667{
668 if (!mxc->mx)
669 return 0;
670
671 if (dst->flags & DST_HOST) {
672 u32 *mp = dst_metrics_write_ptr(dst);
673
674 if (unlikely(!mp))
675 return -ENOMEM;
676
677 fib6_copy_metrics(mp, mxc);
678 } else {
679 dst_init_metrics(dst, mxc->mx, false);
680
681 /* We've stolen mx now. */
682 mxc->mx = NULL;
683 }
684
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100685 return 0;
686}
687
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100688static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
689 struct net *net)
690{
691 if (atomic_read(&rt->rt6i_ref) != 1) {
692 /* This route is used as dummy address holder in some split
693 * nodes. It is not leaked, but it still holds other resources,
694 * which must be released in time. So, scan ascendant nodes
695 * and replace dummy references to this route with references
696 * to still alive ones.
697 */
698 while (fn) {
699 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
700 fn->leaf = fib6_find_prefix(net, fn);
701 atomic_inc(&fn->leaf->rt6i_ref);
702 rt6_release(rt);
703 }
704 fn = fn->parent;
705 }
706 /* No more references are possible at this point. */
707 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
708 }
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * Insert routing information in a node.
713 */
714
715static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100716 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct rt6_info *iter = NULL;
719 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200720 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500721 int replace = (info->nlh &&
722 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
723 int add = (!info->nlh ||
724 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000725 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200726 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100727 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 ins = &fn->leaf;
730
David S. Miller507c9b12011-12-03 17:50:45 -0500731 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /*
733 * Search for duplicates
734 */
735
736 if (iter->rt6i_metric == rt->rt6i_metric) {
737 /*
738 * Same priority level
739 */
David S. Miller507c9b12011-12-03 17:50:45 -0500740 if (info->nlh &&
741 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000742 return -EEXIST;
743 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200744 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
745 found++;
746 break;
747 }
748 if (rt_can_ecmp)
749 fallback_ins = fallback_ins ?: ins;
750 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David S. Millerd1918542011-12-28 20:19:20 -0500753 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 iter->rt6i_idev == rt->rt6i_idev &&
755 ipv6_addr_equal(&iter->rt6i_gateway,
756 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000757 if (rt->rt6i_nsiblings)
758 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500759 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000761 if (!(rt->rt6i_flags & RTF_EXPIRES))
762 rt6_clean_expires(iter);
763 else
764 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700765 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EEXIST;
767 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000768 /* If we have the same destination and the same metric,
769 * but not the same gateway, then the route we try to
770 * add is sibling to this route, increment our counter
771 * of siblings, and later we will add our route to the
772 * list.
773 * Only static routes (which don't have flag
774 * RTF_EXPIRES) are used for ECMPv6.
775 *
776 * To avoid long list, we only had siblings if the
777 * route have a gateway.
778 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200779 if (rt_can_ecmp &&
780 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000781 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
784 if (iter->rt6i_metric > rt->rt6i_metric)
785 break;
786
Michal Kubeček27596472015-05-18 20:54:00 +0200787next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700788 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
Michal Kubeček27596472015-05-18 20:54:00 +0200791 if (fallback_ins && !found) {
792 /* No ECMP-able route found, replace first non-ECMP one */
793 ins = fallback_ins;
794 iter = *ins;
795 found++;
796 }
797
David S. Millerf11e6652007-03-24 20:36:25 -0700798 /* Reset round-robin state, if necessary */
799 if (ins == &fn->leaf)
800 fn->rr_ptr = NULL;
801
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000802 /* Link this route to others same route. */
803 if (rt->rt6i_nsiblings) {
804 unsigned int rt6i_nsiblings;
805 struct rt6_info *sibling, *temp_sibling;
806
807 /* Find the first route that have the same metric */
808 sibling = fn->leaf;
809 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200810 if (sibling->rt6i_metric == rt->rt6i_metric &&
811 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000812 list_add_tail(&rt->rt6i_siblings,
813 &sibling->rt6i_siblings);
814 break;
815 }
816 sibling = sibling->dst.rt6_next;
817 }
818 /* For each sibling in the list, increment the counter of
819 * siblings. BUG() if counters does not match, list of siblings
820 * is broken!
821 */
822 rt6i_nsiblings = 0;
823 list_for_each_entry_safe(sibling, temp_sibling,
824 &rt->rt6i_siblings, rt6i_siblings) {
825 sibling->rt6i_nsiblings++;
826 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
827 rt6i_nsiblings++;
828 }
829 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * insert node
834 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000835 if (!replace) {
836 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000837 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000839add:
Florian Westphale715b6d2015-01-05 23:57:44 +0100840 err = fib6_commit_metrics(&rt->dst, mxc);
841 if (err)
842 return err;
843
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000844 rt->dst.rt6_next = iter;
845 *ins = rt;
846 rt->rt6i_node = fn;
847 atomic_inc(&rt->rt6i_ref);
848 inet6_rt_notify(RTM_NEWROUTE, rt, info);
849 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David S. Miller507c9b12011-12-03 17:50:45 -0500851 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000852 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
853 fn->fn_flags |= RTN_RTINFO;
854 }
855
856 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200857 int nsiblings;
858
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000859 if (!found) {
860 if (add)
861 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000862 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000863 return -ENOENT;
864 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100865
866 err = fib6_commit_metrics(&rt->dst, mxc);
867 if (err)
868 return err;
869
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000870 *ins = rt;
871 rt->rt6i_node = fn;
872 rt->dst.rt6_next = iter->dst.rt6_next;
873 atomic_inc(&rt->rt6i_ref);
874 inet6_rt_notify(RTM_NEWROUTE, rt, info);
David S. Miller507c9b12011-12-03 17:50:45 -0500875 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000876 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
877 fn->fn_flags |= RTN_RTINFO;
878 }
Michal Kubeček27596472015-05-18 20:54:00 +0200879 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100880 fib6_purge_rt(iter, fn, info->nl_net);
881 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200882
883 if (nsiblings) {
884 /* Replacing an ECMP route, remove all siblings */
885 ins = &rt->dst.rt6_next;
886 iter = *ins;
887 while (iter) {
888 if (rt6_qualify_for_ecmp(iter)) {
889 *ins = iter->dst.rt6_next;
890 fib6_purge_rt(iter, fn, info->nl_net);
891 rt6_release(iter);
892 nsiblings--;
893 } else {
894 ins = &iter->dst.rt6_next;
895 }
896 iter = *ins;
897 }
898 WARN_ON(nsiblings != 0);
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
902 return 0;
903}
904
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200905static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700907 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500908 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700909 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700910 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800913void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700915 if (!timer_pending(&net->ipv6.ip6_fib_timer))
916 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700917 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920/*
921 * Add routing information to the routing tree.
922 * <destination addr>/<source addr>
923 * with source addr info in sub-trees
924 */
925
Florian Westphale715b6d2015-01-05 23:57:44 +0100926int fib6_add(struct fib6_node *root, struct rt6_info *rt,
927 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700929 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000931 int allow_create = 1;
932 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200933 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500934
935 if (info->nlh) {
936 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000937 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500938 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000939 replace_required = 1;
940 }
941 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000942 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
fan.du9225b232013-07-22 14:21:09 +0800944 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
945 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200946 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000947 if (IS_ERR(fn)) {
948 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200949 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700953 pn = fn;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#ifdef CONFIG_IPV6_SUBTREES
956 if (rt->rt6i_src.plen) {
957 struct fib6_node *sn;
958
David S. Miller507c9b12011-12-03 17:50:45 -0500959 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct fib6_node *sfn;
961
962 /*
963 * Create subtree.
964 *
965 * fn[main tree]
966 * |
967 * sfn[subtree root]
968 * \
969 * sn[new leaf node]
970 */
971
972 /* Create subtree root node */
973 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500974 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto st_failure;
976
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800977 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
978 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200980 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* Now add the first leaf node to new subtree */
983
984 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800985 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000986 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200987 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000989 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* If it is failed, discard just allocated
991 root, and then (in st_failure) stale node
992 in main tree.
993 */
994 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000995 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto st_failure;
997 }
998
999 /* Now link new subtree to main tree */
1000 sfn->parent = fn;
1001 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 } else {
1003 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001004 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001005 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001006 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001008 if (IS_ERR(sn)) {
1009 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
David S. Miller507c9b12011-12-03 17:50:45 -05001014 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001015 fn->leaf = rt;
1016 atomic_inc(&rt->rt6i_ref);
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 fn = sn;
1019 }
1020#endif
1021
Florian Westphale715b6d2015-01-05 23:57:44 +01001022 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001023 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001024 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001025 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001026 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
1029out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001030 if (err) {
1031#ifdef CONFIG_IPV6_SUBTREES
1032 /*
1033 * If fib6_add_1 has cleared the old leaf pointer in the
1034 * super-tree leaf node we have to find a new one for it.
1035 */
David S. Miller3c051232008-04-18 01:46:19 -07001036 if (pn != fn && pn->leaf == rt) {
1037 pn->leaf = NULL;
1038 atomic_dec(&rt->rt6i_ref);
1039 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001040 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001041 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001042#if RT6_DEBUG >= 2
1043 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001044 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001045 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001046 }
1047#endif
1048 atomic_inc(&pn->leaf->rt6i_ref);
1049 }
1050#endif
Changli Gaod8d1f302010-06-10 23:31:35 -07001051 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return err;
1054
1055#ifdef CONFIG_IPV6_SUBTREES
1056 /* Subtree creation failed, probably main tree node
1057 is orphan. If it is, shoot it.
1058 */
1059st_failure:
1060 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001061 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -07001062 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return err;
1064#endif
1065}
1066
1067/*
1068 * Routing tree lookup
1069 *
1070 */
1071
1072struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001073 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001074 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075};
1076
Wang Yufen437de072014-03-28 12:07:04 +08001077static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1078 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -08001081 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001083 if (unlikely(args->offset == 0))
1084 return NULL;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /*
1087 * Descend on a tree
1088 */
1089
1090 fn = root;
1091
1092 for (;;) {
1093 struct fib6_node *next;
1094
1095 dir = addr_bit_set(args->addr, fn->fn_bit);
1096
1097 next = dir ? fn->right : fn->left;
1098
1099 if (next) {
1100 fn = next;
1101 continue;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
1104 }
1105
David S. Miller507c9b12011-12-03 17:50:45 -05001106 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001107 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct rt6key *key;
1109
1110 key = (struct rt6key *) ((u8 *) fn->leaf +
1111 args->offset);
1112
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001113 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1114#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001115 if (fn->subtree) {
1116 struct fib6_node *sfn;
1117 sfn = fib6_lookup_1(fn->subtree,
1118 args + 1);
1119 if (!sfn)
1120 goto backtrack;
1121 fn = sfn;
1122 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001123#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001124 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001125 return fn;
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001128#ifdef CONFIG_IPV6_SUBTREES
1129backtrack:
1130#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001131 if (fn->fn_flags & RTN_ROOT)
1132 break;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 fn = fn->parent;
1135 }
1136
1137 return NULL;
1138}
1139
Wang Yufen437de072014-03-28 12:07:04 +08001140struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1141 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001144 struct lookup_args args[] = {
1145 {
1146 .offset = offsetof(struct rt6_info, rt6i_dst),
1147 .addr = daddr,
1148 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001150 {
1151 .offset = offsetof(struct rt6_info, rt6i_src),
1152 .addr = saddr,
1153 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001155 {
1156 .offset = 0, /* sentinel */
1157 }
1158 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001160 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001161 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 fn = root;
1163
1164 return fn;
1165}
1166
1167/*
1168 * Get node with specified destination prefix (and source prefix,
1169 * if subtrees are used)
1170 */
1171
1172
Wang Yufen437de072014-03-28 12:07:04 +08001173static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1174 const struct in6_addr *addr,
1175 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct fib6_node *fn;
1178
1179 for (fn = root; fn ; ) {
1180 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1181
1182 /*
1183 * Prefix match
1184 */
1185 if (plen < fn->fn_bit ||
1186 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1187 return NULL;
1188
1189 if (plen == fn->fn_bit)
1190 return fn;
1191
1192 /*
1193 * We have more bits to go
1194 */
1195 if (addr_bit_set(addr, fn->fn_bit))
1196 fn = fn->right;
1197 else
1198 fn = fn->left;
1199 }
1200 return NULL;
1201}
1202
Wang Yufen437de072014-03-28 12:07:04 +08001203struct fib6_node *fib6_locate(struct fib6_node *root,
1204 const struct in6_addr *daddr, int dst_len,
1205 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct fib6_node *fn;
1208
1209 fn = fib6_locate_1(root, daddr, dst_len,
1210 offsetof(struct rt6_info, rt6i_dst));
1211
1212#ifdef CONFIG_IPV6_SUBTREES
1213 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001214 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001215 if (fn && fn->subtree)
1216 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 offsetof(struct rt6_info, rt6i_src));
1218 }
1219#endif
1220
David S. Miller507c9b12011-12-03 17:50:45 -05001221 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return fn;
1223
1224 return NULL;
1225}
1226
1227
1228/*
1229 * Deletion
1230 *
1231 */
1232
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001233static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
David S. Miller507c9b12011-12-03 17:50:45 -05001235 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001236 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
David S. Miller507c9b12011-12-03 17:50:45 -05001238 while (fn) {
1239 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001241 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return fn->right->leaf;
1243
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001244 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 return NULL;
1247}
1248
1249/*
1250 * Called to trim the tree of intermediate nodes when possible. "fn"
1251 * is the node we want to try and remove.
1252 */
1253
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001254static struct fib6_node *fib6_repair_tree(struct net *net,
1255 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 int children;
1258 int nstate;
1259 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001260 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int iter = 0;
1262
1263 for (;;) {
1264 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1265 iter++;
1266
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001267 WARN_ON(fn->fn_flags & RTN_RTINFO);
1268 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001269 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 children = 0;
1272 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001273 if (fn->right)
1274 child = fn->right, children |= 1;
1275 if (fn->left)
1276 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001278 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279#ifdef CONFIG_IPV6_SUBTREES
1280 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001281 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282#endif
1283 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001284 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001286 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001287 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001288 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290#endif
1291 atomic_inc(&fn->leaf->rt6i_ref);
1292 return fn->parent;
1293 }
1294
1295 pn = fn->parent;
1296#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001297 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001298 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001299 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 nstate = FWS_L;
1301 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001302 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001304 if (pn->right == fn)
1305 pn->right = child;
1306 else if (pn->left == fn)
1307 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001309 else
1310 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#endif
1312 if (child)
1313 child->parent = pn;
1314 nstate = FWS_R;
1315#ifdef CONFIG_IPV6_SUBTREES
1316 }
1317#endif
1318
1319 read_lock(&fib6_walker_lock);
1320 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001321 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (w->root == fn) {
1323 w->root = w->node = NULL;
1324 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1325 } else if (w->node == fn) {
1326 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1327 w->node = pn;
1328 w->state = nstate;
1329 }
1330 } else {
1331 if (w->root == fn) {
1332 w->root = child;
1333 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1334 }
1335 if (w->node == fn) {
1336 w->node = child;
1337 if (children&2) {
1338 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001339 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 } else {
1341 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001342 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 }
1345 }
1346 }
1347 read_unlock(&fib6_walker_lock);
1348
1349 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001350 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return pn;
1352
1353 rt6_release(pn->leaf);
1354 pn->leaf = NULL;
1355 fn = pn;
1356 }
1357}
1358
1359static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001360 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001362 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001364 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 RT6_TRACE("fib6_del_route\n");
1367
1368 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001369 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001371 net->ipv6.rt6_stats->fib_rt_entries--;
1372 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David S. Millerf11e6652007-03-24 20:36:25 -07001374 /* Reset round-robin state, if necessary */
1375 if (fn->rr_ptr == rt)
1376 fn->rr_ptr = NULL;
1377
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001378 /* Remove this entry from other siblings */
1379 if (rt->rt6i_nsiblings) {
1380 struct rt6_info *sibling, *next_sibling;
1381
1382 list_for_each_entry_safe(sibling, next_sibling,
1383 &rt->rt6i_siblings, rt6i_siblings)
1384 sibling->rt6i_nsiblings--;
1385 rt->rt6i_nsiblings = 0;
1386 list_del_init(&rt->rt6i_siblings);
1387 }
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* Adjust walkers */
1390 read_lock(&fib6_walker_lock);
1391 FOR_WALKERS(w) {
1392 if (w->state == FWS_C && w->leaf == rt) {
1393 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001394 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001395 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 w->state = FWS_U;
1397 }
1398 }
1399 read_unlock(&fib6_walker_lock);
1400
Changli Gaod8d1f302010-06-10 23:31:35 -07001401 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001404 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001406 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001407 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001410 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Thomas Graf86872cb2006-08-22 00:01:08 -07001412 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 rt6_release(rt);
1414}
1415
Thomas Graf86872cb2006-08-22 00:01:08 -07001416int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001418 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct fib6_node *fn = rt->rt6i_node;
1420 struct rt6_info **rtp;
1421
1422#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001423 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001424 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -ENOENT;
1426 }
1427#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001428 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -ENOENT;
1430
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001431 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
David S. Miller507c9b12011-12-03 17:50:45 -05001433 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001434 struct fib6_node *pn = fn;
1435#ifdef CONFIG_IPV6_SUBTREES
1436 /* clones of this route might be in another subtree */
1437 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001438 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001439 pn = pn->parent;
1440 pn = pn->parent;
1441 }
1442#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001443 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /*
1447 * Walk the leaf entries looking for ourself
1448 */
1449
Changli Gaod8d1f302010-06-10 23:31:35 -07001450 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001452 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return 0;
1454 }
1455 }
1456 return -ENOENT;
1457}
1458
1459/*
1460 * Tree traversal function.
1461 *
1462 * Certainly, it is not interrupt safe.
1463 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1464 * It means, that we can modify tree during walking
1465 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001466 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 *
1468 * It guarantees that every node will be traversed,
1469 * and that it will be traversed only once.
1470 *
1471 * Callback function w->func may return:
1472 * 0 -> continue walking.
1473 * positive value -> walking is suspended (used by tree dumps,
1474 * and probably by gc, if it will be split to several slices)
1475 * negative value -> terminate walking.
1476 *
1477 * The function itself returns:
1478 * 0 -> walk is complete.
1479 * >0 -> walk is incomplete (i.e. suspended)
1480 * <0 -> walk is terminated by an error.
1481 */
1482
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001483static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct fib6_node *fn, *pn;
1486
1487 for (;;) {
1488 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001489 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491
1492 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001493 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 w->state = FWS_C;
1495 w->leaf = fn->leaf;
1496 }
1497 switch (w->state) {
1498#ifdef CONFIG_IPV6_SUBTREES
1499 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001500 if (FIB6_SUBTREE(fn)) {
1501 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 continue;
1503 }
1504 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 case FWS_L:
1507 if (fn->left) {
1508 w->node = fn->left;
1509 w->state = FWS_INIT;
1510 continue;
1511 }
1512 w->state = FWS_R;
1513 case FWS_R:
1514 if (fn->right) {
1515 w->node = fn->right;
1516 w->state = FWS_INIT;
1517 continue;
1518 }
1519 w->state = FWS_C;
1520 w->leaf = fn->leaf;
1521 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001522 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001523 int err;
1524
Eric Dumazetfa809e22012-06-25 15:37:19 -07001525 if (w->skip) {
1526 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001527 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001528 }
1529
1530 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (err)
1532 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001533
1534 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 continue;
1536 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001537skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 w->state = FWS_U;
1539 case FWS_U:
1540 if (fn == w->root)
1541 return 0;
1542 pn = fn->parent;
1543 w->node = pn;
1544#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001545 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001546 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 w->state = FWS_L;
1548 continue;
1549 }
1550#endif
1551 if (pn->left == fn) {
1552 w->state = FWS_R;
1553 continue;
1554 }
1555 if (pn->right == fn) {
1556 w->state = FWS_C;
1557 w->leaf = w->node->leaf;
1558 continue;
1559 }
1560#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001561 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#endif
1563 }
1564 }
1565}
1566
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001567static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 int res;
1570
1571 w->state = FWS_INIT;
1572 w->node = w->root;
1573
1574 fib6_walker_link(w);
1575 res = fib6_walk_continue(w);
1576 if (res <= 0)
1577 fib6_walker_unlink(w);
1578 return res;
1579}
1580
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001581static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 int res;
1584 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001585 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001586 struct nl_info info = {
1587 .nl_net = c->net,
1588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001590 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1591 w->node->fn_sernum != c->sernum)
1592 w->node->fn_sernum = c->sernum;
1593
1594 if (!c->func) {
1595 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1596 w->leaf = NULL;
1597 return 0;
1598 }
1599
Changli Gaod8d1f302010-06-10 23:31:35 -07001600 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 res = c->func(rt, c->arg);
1602 if (res < 0) {
1603 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001604 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (res) {
1606#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001607 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1608 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609#endif
1610 continue;
1611 }
1612 return 0;
1613 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001614 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 w->leaf = rt;
1617 return 0;
1618}
1619
1620/*
1621 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001622 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * func is called on each route.
1624 * It may return -1 -> delete this route.
1625 * 0 -> continue walking
1626 *
1627 * prune==1 -> only immediate children of node (certainly,
1628 * ignoring pure split nodes) will be scanned.
1629 */
1630
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001631static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001632 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001633 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001635 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 c.w.root = root;
1638 c.w.func = fib6_clean_node;
1639 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001640 c.w.count = 0;
1641 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001643 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001645 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 fib6_walk(&c.w);
1648}
1649
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001650static void __fib6_clean_all(struct net *net,
1651 int (*func)(struct rt6_info *, void *),
1652 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001653{
Thomas Grafc71099a2006-08-04 23:20:06 -07001654 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001655 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001656 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001657
Patrick McHardy1b43af52006-08-10 23:11:17 -07001658 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001659 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001660 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001661 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001662 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001663 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001664 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001665 write_unlock_bh(&table->tb6_lock);
1666 }
1667 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001668 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001669}
1670
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001671void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1672 void *arg)
1673{
1674 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1678{
1679 if (rt->rt6i_flags & RTF_CACHE) {
1680 RT6_TRACE("pruning clone %p\n", rt);
1681 return -1;
1682 }
1683
1684 return 0;
1685}
1686
Duan Jiong163cd4e2014-05-09 13:31:43 +08001687static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001689 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1690 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001691}
1692
1693static void fib6_flush_trees(struct net *net)
1694{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001695 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001696
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001697 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001698}
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700/*
1701 * Garbage collection
1702 */
1703
1704static struct fib6_gc_args
1705{
1706 int timeout;
1707 int more;
1708} gc_args;
1709
1710static int fib6_age(struct rt6_info *rt, void *arg)
1711{
1712 unsigned long now = jiffies;
1713
1714 /*
1715 * check addrconf expiration here.
1716 * Routes are expired even if they are in use.
1717 *
1718 * Also age clones. Note, that clones are aged out
1719 * only if they are not in use now.
1720 */
1721
David S. Millerd1918542011-12-28 20:19:20 -05001722 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1723 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return -1;
1726 }
1727 gc_args.more++;
1728 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001729 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1730 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 RT6_TRACE("aging clone %p\n", rt);
1732 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001733 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1734 struct neighbour *neigh;
1735 __u8 neigh_flags = 0;
1736
1737 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1738 if (neigh) {
1739 neigh_flags = neigh->flags;
1740 neigh_release(neigh);
1741 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001742 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001743 RT6_TRACE("purging route %p via non-router but gateway\n",
1744 rt);
1745 return -1;
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748 gc_args.more++;
1749 }
1750
1751 return 0;
1752}
1753
1754static DEFINE_SPINLOCK(fib6_gc_lock);
1755
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001756void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001758 unsigned long now;
1759
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001760 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001762 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1763 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1764 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001766 gc_args.timeout = expires ? (int)expires :
1767 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001769 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001770
Li RongQing0c3584d2013-12-27 16:32:38 +08001771 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001772 now = jiffies;
1773 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001776 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001777 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001778 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001779 else
1780 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 spin_unlock_bh(&fib6_gc_lock);
1782}
1783
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001784static void fib6_gc_timer_cb(unsigned long arg)
1785{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001786 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001787}
1788
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001789static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001790{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001791 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1792
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001793 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001794
Benjamin Theryc5728722008-03-03 23:34:17 -08001795 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1796 if (!net->ipv6.rt6_stats)
1797 goto out_timer;
1798
Eric Dumazet10da66f2010-10-13 08:22:03 +00001799 /* Avoid false sharing : Use at least a full cache line */
1800 size = max_t(size_t, size, L1_CACHE_BYTES);
1801
1802 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001803 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001804 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001805
1806 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1807 GFP_KERNEL);
1808 if (!net->ipv6.fib6_main_tbl)
1809 goto out_fib_table_hash;
1810
1811 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001812 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001813 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1814 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001815 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001816
1817#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1818 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1819 GFP_KERNEL);
1820 if (!net->ipv6.fib6_local_tbl)
1821 goto out_fib6_main_tbl;
1822 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001823 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001824 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1825 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001826 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001827#endif
1828 fib6_tables_init(net);
1829
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001830 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001831
1832#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1833out_fib6_main_tbl:
1834 kfree(net->ipv6.fib6_main_tbl);
1835#endif
1836out_fib_table_hash:
1837 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001838out_rt6_stats:
1839 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001840out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001841 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001842}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001843
1844static void fib6_net_exit(struct net *net)
1845{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001846 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001847 del_timer_sync(&net->ipv6.ip6_fib_timer);
1848
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001849#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001850 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001851 kfree(net->ipv6.fib6_local_tbl);
1852#endif
David S. Miller8e773272012-06-11 00:01:52 -07001853 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001854 kfree(net->ipv6.fib6_main_tbl);
1855 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001856 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001857}
1858
1859static struct pernet_operations fib6_net_ops = {
1860 .init = fib6_net_init,
1861 .exit = fib6_net_exit,
1862};
1863
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001864int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001866 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1869 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001870 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001871 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001872 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001873 goto out;
1874
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001875 ret = register_pernet_subsys(&fib6_net_ops);
1876 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001877 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001878
1879 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1880 NULL);
1881 if (ret)
1882 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001883
1884 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001885out:
1886 return ret;
1887
David S. Millere8803b62012-06-16 01:12:19 -07001888out_unregister_subsys:
1889 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001890out_kmem_cache_create:
1891 kmem_cache_destroy(fib6_node_kmem);
1892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895void fib6_gc_cleanup(void)
1896{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001897 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 kmem_cache_destroy(fib6_node_kmem);
1899}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001900
1901#ifdef CONFIG_PROC_FS
1902
1903struct ipv6_route_iter {
1904 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001905 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001906 loff_t skip;
1907 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001908 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001909};
1910
1911static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1912{
1913 struct rt6_info *rt = v;
1914 struct ipv6_route_iter *iter = seq->private;
1915
1916 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1917
1918#ifdef CONFIG_IPV6_SUBTREES
1919 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1920#else
1921 seq_puts(seq, "00000000000000000000000000000000 00 ");
1922#endif
1923 if (rt->rt6i_flags & RTF_GATEWAY)
1924 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1925 else
1926 seq_puts(seq, "00000000000000000000000000000000");
1927
1928 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1929 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1930 rt->dst.__use, rt->rt6i_flags,
1931 rt->dst.dev ? rt->dst.dev->name : "");
1932 iter->w.leaf = NULL;
1933 return 0;
1934}
1935
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001936static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001937{
1938 struct ipv6_route_iter *iter = w->args;
1939
1940 if (!iter->skip)
1941 return 1;
1942
1943 do {
1944 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1945 iter->skip--;
1946 if (!iter->skip && iter->w.leaf)
1947 return 1;
1948 } while (iter->w.leaf);
1949
1950 return 0;
1951}
1952
1953static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1954{
1955 memset(&iter->w, 0, sizeof(iter->w));
1956 iter->w.func = ipv6_route_yield;
1957 iter->w.root = &iter->tbl->tb6_root;
1958 iter->w.state = FWS_INIT;
1959 iter->w.node = iter->w.root;
1960 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001961 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001962 INIT_LIST_HEAD(&iter->w.lh);
1963 fib6_walker_link(&iter->w);
1964}
1965
1966static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1967 struct net *net)
1968{
1969 unsigned int h;
1970 struct hlist_node *node;
1971
1972 if (tbl) {
1973 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1974 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1975 } else {
1976 h = 0;
1977 node = NULL;
1978 }
1979
1980 while (!node && h < FIB6_TABLE_HASHSZ) {
1981 node = rcu_dereference_bh(
1982 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1983 }
1984 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1985}
1986
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001987static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1988{
1989 if (iter->sernum != iter->w.root->fn_sernum) {
1990 iter->sernum = iter->w.root->fn_sernum;
1991 iter->w.state = FWS_INIT;
1992 iter->w.node = iter->w.root;
1993 WARN_ON(iter->w.skip);
1994 iter->w.skip = iter->w.count;
1995 }
1996}
1997
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001998static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1999{
2000 int r;
2001 struct rt6_info *n;
2002 struct net *net = seq_file_net(seq);
2003 struct ipv6_route_iter *iter = seq->private;
2004
2005 if (!v)
2006 goto iter_table;
2007
2008 n = ((struct rt6_info *)v)->dst.rt6_next;
2009 if (n) {
2010 ++*pos;
2011 return n;
2012 }
2013
2014iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002015 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002016 read_lock(&iter->tbl->tb6_lock);
2017 r = fib6_walk_continue(&iter->w);
2018 read_unlock(&iter->tbl->tb6_lock);
2019 if (r > 0) {
2020 if (v)
2021 ++*pos;
2022 return iter->w.leaf;
2023 } else if (r < 0) {
2024 fib6_walker_unlink(&iter->w);
2025 return NULL;
2026 }
2027 fib6_walker_unlink(&iter->w);
2028
2029 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2030 if (!iter->tbl)
2031 return NULL;
2032
2033 ipv6_route_seq_setup_walk(iter);
2034 goto iter_table;
2035}
2036
2037static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2038 __acquires(RCU_BH)
2039{
2040 struct net *net = seq_file_net(seq);
2041 struct ipv6_route_iter *iter = seq->private;
2042
2043 rcu_read_lock_bh();
2044 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2045 iter->skip = *pos;
2046
2047 if (iter->tbl) {
2048 ipv6_route_seq_setup_walk(iter);
2049 return ipv6_route_seq_next(seq, NULL, pos);
2050 } else {
2051 return NULL;
2052 }
2053}
2054
2055static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2056{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002057 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002058 return w->node && !(w->state == FWS_U && w->node == w->root);
2059}
2060
2061static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2062 __releases(RCU_BH)
2063{
2064 struct ipv6_route_iter *iter = seq->private;
2065
2066 if (ipv6_route_iter_active(iter))
2067 fib6_walker_unlink(&iter->w);
2068
2069 rcu_read_unlock_bh();
2070}
2071
2072static const struct seq_operations ipv6_route_seq_ops = {
2073 .start = ipv6_route_seq_start,
2074 .next = ipv6_route_seq_next,
2075 .stop = ipv6_route_seq_stop,
2076 .show = ipv6_route_seq_show
2077};
2078
2079int ipv6_route_open(struct inode *inode, struct file *file)
2080{
2081 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2082 sizeof(struct ipv6_route_iter));
2083}
2084
2085#endif /* CONFIG_PROC_FS */