blob: e8d7465b1597fa7db6114e7c28f09f03eed0e828 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
35
36#include <net/ip6_fib.h>
37#include <net/ip6_route.h>
38
39#define RT6_DEBUG 2
40
41#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000042#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define RT6_TRACE(x...) do { ; } while (0)
45#endif
46
Wang Yufen437de072014-03-28 12:07:04 +080047static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020049struct fib6_cleaner {
50 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080051 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int (*func)(struct rt6_info *, void *arg);
53 void *arg;
54};
55
Adrian Bunk90d41122006-08-14 23:49:16 -070056static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#ifdef CONFIG_IPV6_SUBTREES
59#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
Duan Jiong163cd4e2014-05-09 13:31:43 +080064static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080065static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
66static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020067static int fib6_walk(struct fib6_walker *w);
68static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * A routing update causes an increase of the serial number on the
72 * affected subtree. This allows for cached routes to be asynchronously
73 * tested when modifications are made to the destination cache as a
74 * result of redirects, path MTU changes, etc.
75 */
76
77static __u32 rt_sernum;
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
98static u32 fib6_new_sernum(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 u32 n = ++rt_sernum;
101 if ((__s32)n <= 0)
102 rt_sernum = n = 1;
103 return n;
104}
105
106/*
107 * Auxiliary address test functions for the radix tree.
108 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900109 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * 64bit processors)
111 */
112
113/*
114 * test bit
115 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000116#if defined(__LITTLE_ENDIAN)
117# define BITOP_BE32_SWIZZLE (0x1F & ~7)
118#else
119# define BITOP_BE32_SWIZZLE 0
120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200122static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000124 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000125 /*
126 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800127 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000128 * is optimized version of
129 * htonl(1 << ((~fn_bit)&0x1F))
130 * See include/asm-generic/bitops/le.h.
131 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700132 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
133 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200136static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct fib6_node *fn;
139
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800140 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return fn;
143}
144
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200145static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 kmem_cache_free(fib6_node_kmem, fn);
148}
149
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200150static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700153 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800156static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700157{
158 unsigned int h;
159
Thomas Graf375216a2006-10-21 20:20:54 -0700160 /*
161 * Initialize table lock at a single place to give lockdep a key,
162 * tables aren't visible prior to being linked to the list.
163 */
164 rwlock_init(&tb->tb6_lock);
165
Neil Hormana33bc5c2009-07-30 18:52:15 -0700166 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700167
168 /*
169 * No protection necessary, this is the only list mutatation
170 * operation, tables never disappear once they exist.
171 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800172 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700173}
174
175#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800176
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800177static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700178{
179 struct fib6_table *table;
180
181 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500182 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700183 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800184 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700185 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700186 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700187 }
188
189 return table;
190}
191
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800192struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700193{
194 struct fib6_table *tb;
195
196 if (id == 0)
197 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800198 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700199 if (tb)
200 return tb;
201
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800202 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500203 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800204 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700205
206 return tb;
207}
208
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800209struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700210{
211 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800212 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700213 unsigned int h;
214
215 if (id == 0)
216 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700217 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700218 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800219 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800220 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 if (tb->tb6_id == id) {
222 rcu_read_unlock();
223 return tb;
224 }
225 }
226 rcu_read_unlock();
227
228 return NULL;
229}
230
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000231static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700232{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800233 fib6_link_table(net, net->ipv6.fib6_main_tbl);
234 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700235}
Thomas Grafc71099a2006-08-04 23:20:06 -0700236#else
237
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800238struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700239{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700241}
242
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800243struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700244{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800245 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700246}
247
David S. Miller4c9483b2011-03-12 16:22:43 -0500248struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800249 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700250{
David S. Miller4c9483b2011-03-12 16:22:43 -0500251 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700252}
253
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000254static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700255{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700257}
258
259#endif
260
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200261static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700262{
263 int res;
264 struct rt6_info *rt;
265
Changli Gaod8d1f302010-06-10 23:31:35 -0700266 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700267 res = rt6_dump_route(rt, w->args);
268 if (res < 0) {
269 /* Frame is full, suspend walking */
270 w->leaf = rt;
271 return 1;
272 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700273 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700274 }
275 w->leaf = NULL;
276 return 0;
277}
278
279static void fib6_dump_end(struct netlink_callback *cb)
280{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200281 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700282
283 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800284 if (cb->args[4]) {
285 cb->args[4] = 0;
286 fib6_walker_unlink(w);
287 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700288 cb->args[2] = 0;
289 kfree(w);
290 }
Wang Yufen437de072014-03-28 12:07:04 +0800291 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700292 cb->args[1] = 3;
293}
294
295static int fib6_dump_done(struct netlink_callback *cb)
296{
297 fib6_dump_end(cb);
298 return cb->done ? cb->done(cb) : 0;
299}
300
301static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
302 struct netlink_callback *cb)
303{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200304 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700305 int res;
306
307 w = (void *)cb->args[2];
308 w->root = &table->tb6_root;
309
310 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000311 w->count = 0;
312 w->skip = 0;
313
Patrick McHardy1b43af52006-08-10 23:11:17 -0700314 read_lock_bh(&table->tb6_lock);
315 res = fib6_walk(w);
316 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000317 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700318 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000319 cb->args[5] = w->root->fn_sernum;
320 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700321 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000322 if (cb->args[5] != w->root->fn_sernum) {
323 /* Begin at the root if the tree changed */
324 cb->args[5] = w->root->fn_sernum;
325 w->state = FWS_INIT;
326 w->node = w->root;
327 w->skip = w->count;
328 } else
329 w->skip = 0;
330
Patrick McHardy1b43af52006-08-10 23:11:17 -0700331 read_lock_bh(&table->tb6_lock);
332 res = fib6_walk_continue(w);
333 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800334 if (res <= 0) {
335 fib6_walker_unlink(w);
336 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700337 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700338 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800339
Patrick McHardy1b43af52006-08-10 23:11:17 -0700340 return res;
341}
342
Thomas Grafc127ea22007-03-22 11:58:32 -0700343static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700344{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900345 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700346 unsigned int h, s_h;
347 unsigned int e = 0, s_e;
348 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200349 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700350 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800351 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700352 int res = 0;
353
354 s_h = cb->args[0];
355 s_e = cb->args[1];
356
357 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500358 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700359 /* New dump:
360 *
361 * 1. hook callback destructor.
362 */
363 cb->args[3] = (long)cb->done;
364 cb->done = fib6_dump_done;
365
366 /*
367 * 2. allocate and initialize walker.
368 */
369 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500370 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700371 return -ENOMEM;
372 w->func = fib6_dump_node;
373 cb->args[2] = (long)w;
374 }
375
376 arg.skb = skb;
377 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700378 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700379 w->args = &arg;
380
Eric Dumazete67f88d2011-04-27 22:56:07 +0000381 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700382 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700383 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800384 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800385 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700386 if (e < s_e)
387 goto next;
388 res = fib6_dump_table(tb, skb, cb);
389 if (res != 0)
390 goto out;
391next:
392 e++;
393 }
394 }
395out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000396 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700397 cb->args[1] = e;
398 cb->args[0] = h;
399
400 res = res < 0 ? res : skb->len;
401 if (res <= 0)
402 fib6_dump_end(cb);
403 return res;
404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/*
407 * Routing Table
408 *
409 * return the appropriate node for a routing tree "add" operation
410 * by either creating and inserting or by returning an existing
411 * node.
412 */
413
fan.du9225b232013-07-22 14:21:09 +0800414static struct fib6_node *fib6_add_1(struct fib6_node *root,
415 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000416 int offset, int allow_create,
417 int replace_required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct fib6_node *fn, *in, *ln;
420 struct fib6_node *pn = NULL;
421 struct rt6key *key;
422 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900423 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 __u32 sernum = fib6_new_sernum();
425
426 RT6_TRACE("fib6_add_1\n");
427
428 /* insert node in tree */
429
430 fn = root;
431
432 do {
433 key = (struct rt6key *)((u8 *)fn->leaf + offset);
434
435 /*
436 * Prefix match
437 */
438 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000439 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000440 if (!allow_create) {
441 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000442 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000443 return ERR_PTR(-ENOENT);
444 }
Joe Perchesf3213832012-05-15 14:11:53 +0000445 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000448 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /*
451 * Exact match ?
452 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (plen == fn->fn_bit) {
455 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500456 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 rt6_release(fn->leaf);
458 fn->leaf = NULL;
459 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return fn;
464 }
465
466 /*
467 * We have more bits to go
468 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* Try to walk down on tree. */
471 fn->fn_sernum = sernum;
472 dir = addr_bit_set(addr, fn->fn_bit);
473 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800474 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } while (fn);
476
Matti Vaittinen14df0152011-11-16 21:18:02 +0000477 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000478 /* We should not create new node because
479 * NLM_F_REPLACE was specified without NLM_F_CREATE
480 * I assume it is safe to require NLM_F_CREATE when
481 * REPLACE flag is used! Later we may want to remove the
482 * check for replace_required, because according
483 * to netlink specification, NLM_F_CREATE
484 * MUST be specified if new route is created.
485 * That would keep IPv6 consistent with IPv4
486 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000487 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000488 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000489 return ERR_PTR(-ENOENT);
490 }
Joe Perchesf3213832012-05-15 14:11:53 +0000491 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * We walked to the bottom of tree.
495 * Create new leaf node without children.
496 */
497
498 ln = node_alloc();
499
David S. Miller507c9b12011-12-03 17:50:45 -0500500 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000501 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 ln->parent = pn;
505 ln->fn_sernum = sernum;
506
507 if (dir)
508 pn->right = ln;
509 else
510 pn->left = ln;
511
512 return ln;
513
514
515insert_above:
516 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900517 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * we have a less significant route.
519 * we've to insert an intermediate node on the list
520 * this new node will point to the one we need to create
521 * and the current
522 */
523
524 pn = fn->parent;
525
526 /* find 1st bit in difference between the 2 addrs.
527
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800528 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 but if it is >= plen, the value is ignored in any case.
530 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900531
fan.du9225b232013-07-22 14:21:09 +0800532 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900534 /*
535 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * / \
537 * (new leaf node)[ln] (old node)[fn]
538 */
539 if (plen > bit) {
540 in = node_alloc();
541 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900542
David S. Miller507c9b12011-12-03 17:50:45 -0500543 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (in)
545 node_free(in);
546 if (ln)
547 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000548 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900551 /*
552 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * RTN_RTINFO will
554 * be off since that an address that chooses one of
555 * the branches would not match less specific routes
556 * in the other branch
557 */
558
559 in->fn_bit = bit;
560
561 in->parent = pn;
562 in->leaf = fn->leaf;
563 atomic_inc(&in->leaf->rt6i_ref);
564
565 in->fn_sernum = sernum;
566
567 /* update parent pointer */
568 if (dir)
569 pn->right = in;
570 else
571 pn->left = in;
572
573 ln->fn_bit = plen;
574
575 ln->parent = in;
576 fn->parent = in;
577
578 ln->fn_sernum = sernum;
579
580 if (addr_bit_set(addr, bit)) {
581 in->right = ln;
582 in->left = fn;
583 } else {
584 in->left = ln;
585 in->right = fn;
586 }
587 } else { /* plen <= bit */
588
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900589 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * (new leaf node)[ln]
591 * / \
592 * (old node)[fn] NULL
593 */
594
595 ln = node_alloc();
596
David S. Miller507c9b12011-12-03 17:50:45 -0500597 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000598 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 ln->fn_bit = plen;
601
602 ln->parent = pn;
603
604 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (dir)
607 pn->right = ln;
608 else
609 pn->left = ln;
610
611 if (addr_bit_set(&key->addr, plen))
612 ln->right = fn;
613 else
614 ln->left = fn;
615
616 fn->parent = ln;
617 }
618 return ln;
619}
620
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200621static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200622{
623 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
624 RTF_GATEWAY;
625}
626
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100627static int fib6_commit_metrics(struct dst_entry *dst,
628 struct nlattr *mx, int mx_len)
629{
630 struct nlattr *nla;
631 int remaining;
632 u32 *mp;
633
634 if (dst->flags & DST_HOST) {
635 mp = dst_metrics_write_ptr(dst);
636 } else {
Benjamin Block793c3b42014-08-21 19:37:48 +0200637 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100638 if (!mp)
639 return -ENOMEM;
640 dst_init_metrics(dst, mp, 0);
641 }
642
643 nla_for_each_attr(nla, mx, mx_len, remaining) {
644 int type = nla_type(nla);
645
646 if (type) {
647 if (type > RTAX_MAX)
648 return -EINVAL;
649
650 mp[type - 1] = nla_get_u32(nla);
651 }
652 }
653 return 0;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
657 * Insert routing information in a node.
658 */
659
660static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100661 struct nl_info *info, struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct rt6_info *iter = NULL;
664 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500665 int replace = (info->nlh &&
666 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
667 int add = (!info->nlh ||
668 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000669 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200670 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100671 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 ins = &fn->leaf;
674
David S. Miller507c9b12011-12-03 17:50:45 -0500675 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * Search for duplicates
678 */
679
680 if (iter->rt6i_metric == rt->rt6i_metric) {
681 /*
682 * Same priority level
683 */
David S. Miller507c9b12011-12-03 17:50:45 -0500684 if (info->nlh &&
685 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000686 return -EEXIST;
687 if (replace) {
688 found++;
689 break;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
David S. Millerd1918542011-12-28 20:19:20 -0500692 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 iter->rt6i_idev == rt->rt6i_idev &&
694 ipv6_addr_equal(&iter->rt6i_gateway,
695 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000696 if (rt->rt6i_nsiblings)
697 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500698 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000700 if (!(rt->rt6i_flags & RTF_EXPIRES))
701 rt6_clean_expires(iter);
702 else
703 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -EEXIST;
705 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000706 /* If we have the same destination and the same metric,
707 * but not the same gateway, then the route we try to
708 * add is sibling to this route, increment our counter
709 * of siblings, and later we will add our route to the
710 * list.
711 * Only static routes (which don't have flag
712 * RTF_EXPIRES) are used for ECMPv6.
713 *
714 * To avoid long list, we only had siblings if the
715 * route have a gateway.
716 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200717 if (rt_can_ecmp &&
718 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000719 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
722 if (iter->rt6i_metric > rt->rt6i_metric)
723 break;
724
Changli Gaod8d1f302010-06-10 23:31:35 -0700725 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
David S. Millerf11e6652007-03-24 20:36:25 -0700728 /* Reset round-robin state, if necessary */
729 if (ins == &fn->leaf)
730 fn->rr_ptr = NULL;
731
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000732 /* Link this route to others same route. */
733 if (rt->rt6i_nsiblings) {
734 unsigned int rt6i_nsiblings;
735 struct rt6_info *sibling, *temp_sibling;
736
737 /* Find the first route that have the same metric */
738 sibling = fn->leaf;
739 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200740 if (sibling->rt6i_metric == rt->rt6i_metric &&
741 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000742 list_add_tail(&rt->rt6i_siblings,
743 &sibling->rt6i_siblings);
744 break;
745 }
746 sibling = sibling->dst.rt6_next;
747 }
748 /* For each sibling in the list, increment the counter of
749 * siblings. BUG() if counters does not match, list of siblings
750 * is broken!
751 */
752 rt6i_nsiblings = 0;
753 list_for_each_entry_safe(sibling, temp_sibling,
754 &rt->rt6i_siblings, rt6i_siblings) {
755 sibling->rt6i_nsiblings++;
756 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
757 rt6i_nsiblings++;
758 }
759 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /*
763 * insert node
764 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000765 if (!replace) {
766 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000767 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000769add:
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100770 if (mx) {
771 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
772 if (err)
773 return err;
774 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000775 rt->dst.rt6_next = iter;
776 *ins = rt;
777 rt->rt6i_node = fn;
778 atomic_inc(&rt->rt6i_ref);
779 inet6_rt_notify(RTM_NEWROUTE, rt, info);
780 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
David S. Miller507c9b12011-12-03 17:50:45 -0500782 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000783 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
784 fn->fn_flags |= RTN_RTINFO;
785 }
786
787 } else {
788 if (!found) {
789 if (add)
790 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000791 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000792 return -ENOENT;
793 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100794 if (mx) {
795 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
796 if (err)
797 return err;
798 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000799 *ins = rt;
800 rt->rt6i_node = fn;
801 rt->dst.rt6_next = iter->dst.rt6_next;
802 atomic_inc(&rt->rt6i_ref);
803 inet6_rt_notify(RTM_NEWROUTE, rt, info);
804 rt6_release(iter);
David S. Miller507c9b12011-12-03 17:50:45 -0500805 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000806 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
807 fn->fn_flags |= RTN_RTINFO;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 return 0;
812}
813
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200814static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Stephen Hemminger417f28bb2008-07-22 14:33:45 -0700816 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500817 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28bb2008-07-22 14:33:45 -0700818 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700819 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800822void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Stephen Hemminger417f28bb2008-07-22 14:33:45 -0700824 if (!timer_pending(&net->ipv6.ip6_fib_timer))
825 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700826 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829/*
830 * Add routing information to the routing tree.
831 * <destination addr>/<source addr>
832 * with source addr info in sub-trees
833 */
834
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100835int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
836 struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700838 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000840 int allow_create = 1;
841 int replace_required = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500842
843 if (info->nlh) {
844 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000845 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500846 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000847 replace_required = 1;
848 }
849 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000850 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
fan.du9225b232013-07-22 14:21:09 +0800852 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
853 offsetof(struct rt6_info, rt6i_dst), allow_create,
854 replace_required);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000855 if (IS_ERR(fn)) {
856 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200857 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700861 pn = fn;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#ifdef CONFIG_IPV6_SUBTREES
864 if (rt->rt6i_src.plen) {
865 struct fib6_node *sn;
866
David S. Miller507c9b12011-12-03 17:50:45 -0500867 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct fib6_node *sfn;
869
870 /*
871 * Create subtree.
872 *
873 * fn[main tree]
874 * |
875 * sfn[subtree root]
876 * \
877 * sn[new leaf node]
878 */
879
880 /* Create subtree root node */
881 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500882 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto st_failure;
884
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800885 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
886 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sfn->fn_flags = RTN_ROOT;
888 sfn->fn_sernum = fib6_new_sernum();
889
890 /* Now add the first leaf node to new subtree */
891
892 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800893 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000894 offsetof(struct rt6_info, rt6i_src),
895 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000897 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* If it is failed, discard just allocated
899 root, and then (in st_failure) stale node
900 in main tree.
901 */
902 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000903 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto st_failure;
905 }
906
907 /* Now link new subtree to main tree */
908 sfn->parent = fn;
909 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 } else {
911 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800912 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000913 offsetof(struct rt6_info, rt6i_src),
914 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000916 if (IS_ERR(sn)) {
917 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
David S. Miller507c9b12011-12-03 17:50:45 -0500922 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700923 fn->leaf = rt;
924 atomic_inc(&rt->rt6i_ref);
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 fn = sn;
927 }
928#endif
929
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100930 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
David S. Miller507c9b12011-12-03 17:50:45 -0500931 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800932 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500933 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +0800934 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700938 if (err) {
939#ifdef CONFIG_IPV6_SUBTREES
940 /*
941 * If fib6_add_1 has cleared the old leaf pointer in the
942 * super-tree leaf node we have to find a new one for it.
943 */
David S. Miller3c051232008-04-18 01:46:19 -0700944 if (pn != fn && pn->leaf == rt) {
945 pn->leaf = NULL;
946 atomic_dec(&rt->rt6i_ref);
947 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700948 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800949 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700950#if RT6_DEBUG >= 2
951 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700952 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800953 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700954 }
955#endif
956 atomic_inc(&pn->leaf->rt6i_ref);
957 }
958#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700959 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return err;
962
963#ifdef CONFIG_IPV6_SUBTREES
964 /* Subtree creation failed, probably main tree node
965 is orphan. If it is, shoot it.
966 */
967st_failure:
968 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800969 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700970 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return err;
972#endif
973}
974
975/*
976 * Routing tree lookup
977 *
978 */
979
980struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -0500981 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000982 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983};
984
Wang Yufen437de072014-03-28 12:07:04 +0800985static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
986 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -0800989 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700991 if (unlikely(args->offset == 0))
992 return NULL;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * Descend on a tree
996 */
997
998 fn = root;
999
1000 for (;;) {
1001 struct fib6_node *next;
1002
1003 dir = addr_bit_set(args->addr, fn->fn_bit);
1004
1005 next = dir ? fn->right : fn->left;
1006
1007 if (next) {
1008 fn = next;
1009 continue;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
1012 }
1013
David S. Miller507c9b12011-12-03 17:50:45 -05001014 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001015 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct rt6key *key;
1017
1018 key = (struct rt6key *) ((u8 *) fn->leaf +
1019 args->offset);
1020
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001021 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1022#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001023 if (fn->subtree) {
1024 struct fib6_node *sfn;
1025 sfn = fib6_lookup_1(fn->subtree,
1026 args + 1);
1027 if (!sfn)
1028 goto backtrack;
1029 fn = sfn;
1030 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001031#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001032 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001033 return fn;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001036#ifdef CONFIG_IPV6_SUBTREES
1037backtrack:
1038#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001039 if (fn->fn_flags & RTN_ROOT)
1040 break;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 fn = fn->parent;
1043 }
1044
1045 return NULL;
1046}
1047
Wang Yufen437de072014-03-28 12:07:04 +08001048struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1049 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001052 struct lookup_args args[] = {
1053 {
1054 .offset = offsetof(struct rt6_info, rt6i_dst),
1055 .addr = daddr,
1056 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001058 {
1059 .offset = offsetof(struct rt6_info, rt6i_src),
1060 .addr = saddr,
1061 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001063 {
1064 .offset = 0, /* sentinel */
1065 }
1066 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001068 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001069 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 fn = root;
1071
1072 return fn;
1073}
1074
1075/*
1076 * Get node with specified destination prefix (and source prefix,
1077 * if subtrees are used)
1078 */
1079
1080
Wang Yufen437de072014-03-28 12:07:04 +08001081static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1082 const struct in6_addr *addr,
1083 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct fib6_node *fn;
1086
1087 for (fn = root; fn ; ) {
1088 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1089
1090 /*
1091 * Prefix match
1092 */
1093 if (plen < fn->fn_bit ||
1094 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1095 return NULL;
1096
1097 if (plen == fn->fn_bit)
1098 return fn;
1099
1100 /*
1101 * We have more bits to go
1102 */
1103 if (addr_bit_set(addr, fn->fn_bit))
1104 fn = fn->right;
1105 else
1106 fn = fn->left;
1107 }
1108 return NULL;
1109}
1110
Wang Yufen437de072014-03-28 12:07:04 +08001111struct fib6_node *fib6_locate(struct fib6_node *root,
1112 const struct in6_addr *daddr, int dst_len,
1113 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct fib6_node *fn;
1116
1117 fn = fib6_locate_1(root, daddr, dst_len,
1118 offsetof(struct rt6_info, rt6i_dst));
1119
1120#ifdef CONFIG_IPV6_SUBTREES
1121 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001122 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001123 if (fn && fn->subtree)
1124 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 offsetof(struct rt6_info, rt6i_src));
1126 }
1127#endif
1128
David S. Miller507c9b12011-12-03 17:50:45 -05001129 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return fn;
1131
1132 return NULL;
1133}
1134
1135
1136/*
1137 * Deletion
1138 *
1139 */
1140
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001141static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
David S. Miller507c9b12011-12-03 17:50:45 -05001143 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001144 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
David S. Miller507c9b12011-12-03 17:50:45 -05001146 while (fn) {
1147 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001149 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return fn->right->leaf;
1151
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001152 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 return NULL;
1155}
1156
1157/*
1158 * Called to trim the tree of intermediate nodes when possible. "fn"
1159 * is the node we want to try and remove.
1160 */
1161
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001162static struct fib6_node *fib6_repair_tree(struct net *net,
1163 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 int children;
1166 int nstate;
1167 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001168 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int iter = 0;
1170
1171 for (;;) {
1172 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1173 iter++;
1174
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001175 WARN_ON(fn->fn_flags & RTN_RTINFO);
1176 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1177 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 children = 0;
1180 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001181 if (fn->right)
1182 child = fn->right, children |= 1;
1183 if (fn->left)
1184 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001186 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187#ifdef CONFIG_IPV6_SUBTREES
1188 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001189 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190#endif
1191 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001192 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001194 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001195 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001196 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198#endif
1199 atomic_inc(&fn->leaf->rt6i_ref);
1200 return fn->parent;
1201 }
1202
1203 pn = fn->parent;
1204#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001205 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001206 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001207 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 nstate = FWS_L;
1209 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001210 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001212 if (pn->right == fn)
1213 pn->right = child;
1214 else if (pn->left == fn)
1215 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001217 else
1218 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#endif
1220 if (child)
1221 child->parent = pn;
1222 nstate = FWS_R;
1223#ifdef CONFIG_IPV6_SUBTREES
1224 }
1225#endif
1226
1227 read_lock(&fib6_walker_lock);
1228 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001229 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (w->root == fn) {
1231 w->root = w->node = NULL;
1232 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1233 } else if (w->node == fn) {
1234 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1235 w->node = pn;
1236 w->state = nstate;
1237 }
1238 } else {
1239 if (w->root == fn) {
1240 w->root = child;
1241 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1242 }
1243 if (w->node == fn) {
1244 w->node = child;
1245 if (children&2) {
1246 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001247 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 } else {
1249 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001250 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 }
1253 }
1254 }
1255 read_unlock(&fib6_walker_lock);
1256
1257 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001258 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return pn;
1260
1261 rt6_release(pn->leaf);
1262 pn->leaf = NULL;
1263 fn = pn;
1264 }
1265}
1266
1267static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001268 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001270 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001272 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 RT6_TRACE("fib6_del_route\n");
1275
1276 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001277 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001279 net->ipv6.rt6_stats->fib_rt_entries--;
1280 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
David S. Millerf11e6652007-03-24 20:36:25 -07001282 /* Reset round-robin state, if necessary */
1283 if (fn->rr_ptr == rt)
1284 fn->rr_ptr = NULL;
1285
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001286 /* Remove this entry from other siblings */
1287 if (rt->rt6i_nsiblings) {
1288 struct rt6_info *sibling, *next_sibling;
1289
1290 list_for_each_entry_safe(sibling, next_sibling,
1291 &rt->rt6i_siblings, rt6i_siblings)
1292 sibling->rt6i_nsiblings--;
1293 rt->rt6i_nsiblings = 0;
1294 list_del_init(&rt->rt6i_siblings);
1295 }
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* Adjust walkers */
1298 read_lock(&fib6_walker_lock);
1299 FOR_WALKERS(w) {
1300 if (w->state == FWS_C && w->leaf == rt) {
1301 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001302 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001303 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 w->state = FWS_U;
1305 }
1306 }
1307 read_unlock(&fib6_walker_lock);
1308
Changli Gaod8d1f302010-06-10 23:31:35 -07001309 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001312 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001314 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001315 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 if (atomic_read(&rt->rt6i_ref) != 1) {
1319 /* This route is used as dummy address holder in some split
1320 * nodes. It is not leaked, but it still holds other resources,
1321 * which must be released in time. So, scan ascendant nodes
1322 * and replace dummy references to this route with references
1323 * to still alive ones.
1324 */
1325 while (fn) {
David S. Miller507c9b12011-12-03 17:50:45 -05001326 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001327 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 atomic_inc(&fn->leaf->rt6i_ref);
1329 rt6_release(rt);
1330 }
1331 fn = fn->parent;
1332 }
1333 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001334 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336
Thomas Graf86872cb2006-08-22 00:01:08 -07001337 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 rt6_release(rt);
1339}
1340
Thomas Graf86872cb2006-08-22 00:01:08 -07001341int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001343 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct fib6_node *fn = rt->rt6i_node;
1345 struct rt6_info **rtp;
1346
1347#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001348 if (rt->dst.obsolete > 0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001349 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -ENOENT;
1351 }
1352#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001353 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -ENOENT;
1355
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001356 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
David S. Miller507c9b12011-12-03 17:50:45 -05001358 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001359 struct fib6_node *pn = fn;
1360#ifdef CONFIG_IPV6_SUBTREES
1361 /* clones of this route might be in another subtree */
1362 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001363 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001364 pn = pn->parent;
1365 pn = pn->parent;
1366 }
1367#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001368 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /*
1372 * Walk the leaf entries looking for ourself
1373 */
1374
Changli Gaod8d1f302010-06-10 23:31:35 -07001375 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001377 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 0;
1379 }
1380 }
1381 return -ENOENT;
1382}
1383
1384/*
1385 * Tree traversal function.
1386 *
1387 * Certainly, it is not interrupt safe.
1388 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1389 * It means, that we can modify tree during walking
1390 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001391 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 *
1393 * It guarantees that every node will be traversed,
1394 * and that it will be traversed only once.
1395 *
1396 * Callback function w->func may return:
1397 * 0 -> continue walking.
1398 * positive value -> walking is suspended (used by tree dumps,
1399 * and probably by gc, if it will be split to several slices)
1400 * negative value -> terminate walking.
1401 *
1402 * The function itself returns:
1403 * 0 -> walk is complete.
1404 * >0 -> walk is incomplete (i.e. suspended)
1405 * <0 -> walk is terminated by an error.
1406 */
1407
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001408static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct fib6_node *fn, *pn;
1411
1412 for (;;) {
1413 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001414 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 0;
1416
1417 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001418 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 w->state = FWS_C;
1420 w->leaf = fn->leaf;
1421 }
1422 switch (w->state) {
1423#ifdef CONFIG_IPV6_SUBTREES
1424 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001425 if (FIB6_SUBTREE(fn)) {
1426 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 continue;
1428 }
1429 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001430#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 case FWS_L:
1432 if (fn->left) {
1433 w->node = fn->left;
1434 w->state = FWS_INIT;
1435 continue;
1436 }
1437 w->state = FWS_R;
1438 case FWS_R:
1439 if (fn->right) {
1440 w->node = fn->right;
1441 w->state = FWS_INIT;
1442 continue;
1443 }
1444 w->state = FWS_C;
1445 w->leaf = fn->leaf;
1446 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001447 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001448 int err;
1449
Eric Dumazetfa809e22012-06-25 15:37:19 -07001450 if (w->skip) {
1451 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001452 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001453 }
1454
1455 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (err)
1457 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001458
1459 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 continue;
1461 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001462skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 w->state = FWS_U;
1464 case FWS_U:
1465 if (fn == w->root)
1466 return 0;
1467 pn = fn->parent;
1468 w->node = pn;
1469#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001470 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001471 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 w->state = FWS_L;
1473 continue;
1474 }
1475#endif
1476 if (pn->left == fn) {
1477 w->state = FWS_R;
1478 continue;
1479 }
1480 if (pn->right == fn) {
1481 w->state = FWS_C;
1482 w->leaf = w->node->leaf;
1483 continue;
1484 }
1485#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001486 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487#endif
1488 }
1489 }
1490}
1491
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001492static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 int res;
1495
1496 w->state = FWS_INIT;
1497 w->node = w->root;
1498
1499 fib6_walker_link(w);
1500 res = fib6_walk_continue(w);
1501 if (res <= 0)
1502 fib6_walker_unlink(w);
1503 return res;
1504}
1505
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001506static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 int res;
1509 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001510 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001511 struct nl_info info = {
1512 .nl_net = c->net,
1513 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Changli Gaod8d1f302010-06-10 23:31:35 -07001515 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 res = c->func(rt, c->arg);
1517 if (res < 0) {
1518 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001519 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (res) {
1521#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001522 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1523 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524#endif
1525 continue;
1526 }
1527 return 0;
1528 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001529 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 w->leaf = rt;
1532 return 0;
1533}
1534
1535/*
1536 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 * func is called on each route.
1539 * It may return -1 -> delete this route.
1540 * 0 -> continue walking
1541 *
1542 * prune==1 -> only immediate children of node (certainly,
1543 * ignoring pure split nodes) will be scanned.
1544 */
1545
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001546static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001547 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001548 bool prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001550 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 c.w.root = root;
1553 c.w.func = fib6_clean_node;
1554 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001555 c.w.count = 0;
1556 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 c.func = func;
1558 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001559 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 fib6_walk(&c.w);
1562}
1563
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001564void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Li RongQing0c3584d2013-12-27 16:32:38 +08001565 void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001566{
Thomas Grafc71099a2006-08-04 23:20:06 -07001567 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001568 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001569 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001570
Patrick McHardy1b43af52006-08-10 23:11:17 -07001571 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001572 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001573 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001574 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001575 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001576 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001577 func, false, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001578 write_unlock_bh(&table->tb6_lock);
1579 }
1580 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001581 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1585{
1586 if (rt->rt6i_flags & RTF_CACHE) {
1587 RT6_TRACE("pruning clone %p\n", rt);
1588 return -1;
1589 }
1590
1591 return 0;
1592}
1593
Duan Jiong163cd4e2014-05-09 13:31:43 +08001594static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001596 fib6_clean_tree(net, fn, fib6_prune_clone, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001599static int fib6_update_sernum(struct rt6_info *rt, void *arg)
1600{
1601 __u32 sernum = *(__u32 *)arg;
1602
1603 if (rt->rt6i_node &&
1604 rt->rt6i_node->fn_sernum != sernum)
1605 rt->rt6i_node->fn_sernum = sernum;
1606
1607 return 0;
1608}
1609
1610static void fib6_flush_trees(struct net *net)
1611{
1612 __u32 new_sernum = fib6_new_sernum();
1613
1614 fib6_clean_all(net, fib6_update_sernum, &new_sernum);
1615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/*
1618 * Garbage collection
1619 */
1620
1621static struct fib6_gc_args
1622{
1623 int timeout;
1624 int more;
1625} gc_args;
1626
1627static int fib6_age(struct rt6_info *rt, void *arg)
1628{
1629 unsigned long now = jiffies;
1630
1631 /*
1632 * check addrconf expiration here.
1633 * Routes are expired even if they are in use.
1634 *
1635 * Also age clones. Note, that clones are aged out
1636 * only if they are not in use now.
1637 */
1638
David S. Millerd1918542011-12-28 20:19:20 -05001639 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1640 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return -1;
1643 }
1644 gc_args.more++;
1645 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001646 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1647 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 RT6_TRACE("aging clone %p\n", rt);
1649 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001650 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1651 struct neighbour *neigh;
1652 __u8 neigh_flags = 0;
1653
1654 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1655 if (neigh) {
1656 neigh_flags = neigh->flags;
1657 neigh_release(neigh);
1658 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001659 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001660 RT6_TRACE("purging route %p via non-router but gateway\n",
1661 rt);
1662 return -1;
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 gc_args.more++;
1666 }
1667
1668 return 0;
1669}
1670
1671static DEFINE_SPINLOCK(fib6_gc_lock);
1672
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001673void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001675 unsigned long now;
1676
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001677 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001679 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1680 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1681 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001683 gc_args.timeout = expires ? (int)expires :
1684 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001686 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001687
Li RongQing0c3584d2013-12-27 16:32:38 +08001688 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001689 now = jiffies;
1690 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001693 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001694 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001695 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001696 else
1697 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 spin_unlock_bh(&fib6_gc_lock);
1699}
1700
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001701static void fib6_gc_timer_cb(unsigned long arg)
1702{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001703 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001704}
1705
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001706static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001707{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001708 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1709
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001710 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001711
Benjamin Theryc5728722008-03-03 23:34:17 -08001712 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1713 if (!net->ipv6.rt6_stats)
1714 goto out_timer;
1715
Eric Dumazet10da66f2010-10-13 08:22:03 +00001716 /* Avoid false sharing : Use at least a full cache line */
1717 size = max_t(size_t, size, L1_CACHE_BYTES);
1718
1719 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001720 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001721 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001722
1723 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1724 GFP_KERNEL);
1725 if (!net->ipv6.fib6_main_tbl)
1726 goto out_fib_table_hash;
1727
1728 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001729 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001730 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1731 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001732 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001733
1734#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1735 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1736 GFP_KERNEL);
1737 if (!net->ipv6.fib6_local_tbl)
1738 goto out_fib6_main_tbl;
1739 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001740 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001741 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1742 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001743 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001744#endif
1745 fib6_tables_init(net);
1746
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001747 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001748
1749#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1750out_fib6_main_tbl:
1751 kfree(net->ipv6.fib6_main_tbl);
1752#endif
1753out_fib_table_hash:
1754 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001755out_rt6_stats:
1756 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001757out_timer:
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001758 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001759}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001760
1761static void fib6_net_exit(struct net *net)
1762{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001763 rt6_ifdown(net, NULL);
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001764 del_timer_sync(&net->ipv6.ip6_fib_timer);
1765
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001766#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001767 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001768 kfree(net->ipv6.fib6_local_tbl);
1769#endif
David S. Miller8e773272012-06-11 00:01:52 -07001770 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001771 kfree(net->ipv6.fib6_main_tbl);
1772 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001773 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001774}
1775
1776static struct pernet_operations fib6_net_ops = {
1777 .init = fib6_net_init,
1778 .exit = fib6_net_exit,
1779};
1780
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001781int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001783 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1786 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001787 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001788 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001789 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001790 goto out;
1791
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001792 ret = register_pernet_subsys(&fib6_net_ops);
1793 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001794 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001795
1796 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1797 NULL);
1798 if (ret)
1799 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001800
1801 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001802out:
1803 return ret;
1804
David S. Millere8803b62012-06-16 01:12:19 -07001805out_unregister_subsys:
1806 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001807out_kmem_cache_create:
1808 kmem_cache_destroy(fib6_node_kmem);
1809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812void fib6_gc_cleanup(void)
1813{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001814 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 kmem_cache_destroy(fib6_node_kmem);
1816}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001817
1818#ifdef CONFIG_PROC_FS
1819
1820struct ipv6_route_iter {
1821 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001822 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001823 loff_t skip;
1824 struct fib6_table *tbl;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001825 __u32 sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001826};
1827
1828static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1829{
1830 struct rt6_info *rt = v;
1831 struct ipv6_route_iter *iter = seq->private;
1832
1833 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1834
1835#ifdef CONFIG_IPV6_SUBTREES
1836 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1837#else
1838 seq_puts(seq, "00000000000000000000000000000000 00 ");
1839#endif
1840 if (rt->rt6i_flags & RTF_GATEWAY)
1841 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1842 else
1843 seq_puts(seq, "00000000000000000000000000000000");
1844
1845 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1846 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1847 rt->dst.__use, rt->rt6i_flags,
1848 rt->dst.dev ? rt->dst.dev->name : "");
1849 iter->w.leaf = NULL;
1850 return 0;
1851}
1852
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001853static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001854{
1855 struct ipv6_route_iter *iter = w->args;
1856
1857 if (!iter->skip)
1858 return 1;
1859
1860 do {
1861 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1862 iter->skip--;
1863 if (!iter->skip && iter->w.leaf)
1864 return 1;
1865 } while (iter->w.leaf);
1866
1867 return 0;
1868}
1869
1870static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1871{
1872 memset(&iter->w, 0, sizeof(iter->w));
1873 iter->w.func = ipv6_route_yield;
1874 iter->w.root = &iter->tbl->tb6_root;
1875 iter->w.state = FWS_INIT;
1876 iter->w.node = iter->w.root;
1877 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001878 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001879 INIT_LIST_HEAD(&iter->w.lh);
1880 fib6_walker_link(&iter->w);
1881}
1882
1883static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1884 struct net *net)
1885{
1886 unsigned int h;
1887 struct hlist_node *node;
1888
1889 if (tbl) {
1890 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1891 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1892 } else {
1893 h = 0;
1894 node = NULL;
1895 }
1896
1897 while (!node && h < FIB6_TABLE_HASHSZ) {
1898 node = rcu_dereference_bh(
1899 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1900 }
1901 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1902}
1903
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001904static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1905{
1906 if (iter->sernum != iter->w.root->fn_sernum) {
1907 iter->sernum = iter->w.root->fn_sernum;
1908 iter->w.state = FWS_INIT;
1909 iter->w.node = iter->w.root;
1910 WARN_ON(iter->w.skip);
1911 iter->w.skip = iter->w.count;
1912 }
1913}
1914
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001915static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1916{
1917 int r;
1918 struct rt6_info *n;
1919 struct net *net = seq_file_net(seq);
1920 struct ipv6_route_iter *iter = seq->private;
1921
1922 if (!v)
1923 goto iter_table;
1924
1925 n = ((struct rt6_info *)v)->dst.rt6_next;
1926 if (n) {
1927 ++*pos;
1928 return n;
1929 }
1930
1931iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001932 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001933 read_lock(&iter->tbl->tb6_lock);
1934 r = fib6_walk_continue(&iter->w);
1935 read_unlock(&iter->tbl->tb6_lock);
1936 if (r > 0) {
1937 if (v)
1938 ++*pos;
1939 return iter->w.leaf;
1940 } else if (r < 0) {
1941 fib6_walker_unlink(&iter->w);
1942 return NULL;
1943 }
1944 fib6_walker_unlink(&iter->w);
1945
1946 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1947 if (!iter->tbl)
1948 return NULL;
1949
1950 ipv6_route_seq_setup_walk(iter);
1951 goto iter_table;
1952}
1953
1954static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1955 __acquires(RCU_BH)
1956{
1957 struct net *net = seq_file_net(seq);
1958 struct ipv6_route_iter *iter = seq->private;
1959
1960 rcu_read_lock_bh();
1961 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1962 iter->skip = *pos;
1963
1964 if (iter->tbl) {
1965 ipv6_route_seq_setup_walk(iter);
1966 return ipv6_route_seq_next(seq, NULL, pos);
1967 } else {
1968 return NULL;
1969 }
1970}
1971
1972static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1973{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001974 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001975 return w->node && !(w->state == FWS_U && w->node == w->root);
1976}
1977
1978static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1979 __releases(RCU_BH)
1980{
1981 struct ipv6_route_iter *iter = seq->private;
1982
1983 if (ipv6_route_iter_active(iter))
1984 fib6_walker_unlink(&iter->w);
1985
1986 rcu_read_unlock_bh();
1987}
1988
1989static const struct seq_operations ipv6_route_seq_ops = {
1990 .start = ipv6_route_seq_start,
1991 .next = ipv6_route_seq_next,
1992 .stop = ipv6_route_seq_stop,
1993 .show = ipv6_route_seq_show
1994};
1995
1996int ipv6_route_open(struct inode *inode, struct file *file)
1997{
1998 return seq_open_net(inode, file, &ipv6_route_seq_ops,
1999 sizeof(struct ipv6_route_iter));
2000}
2001
2002#endif /* CONFIG_PROC_FS */