blob: 96dbffff5a2400bfca0a7b0bee9072d76ec92e88 [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
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200157static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700160 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800163static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700164{
165 unsigned int h;
166
Thomas Graf375216a2006-10-21 20:20:54 -0700167 /*
168 * Initialize table lock at a single place to give lockdep a key,
169 * tables aren't visible prior to being linked to the list.
170 */
171 rwlock_init(&tb->tb6_lock);
172
Neil Hormana33bc5c2009-07-30 18:52:15 -0700173 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700174
175 /*
176 * No protection necessary, this is the only list mutatation
177 * operation, tables never disappear once they exist.
178 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800179 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700180}
181
182#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800183
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800184static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700185{
186 struct fib6_table *table;
187
188 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500189 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700190 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800191 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700192 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700193 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700194 }
195
196 return table;
197}
198
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800199struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700200{
201 struct fib6_table *tb;
202
203 if (id == 0)
204 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800205 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700206 if (tb)
207 return tb;
208
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800209 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500210 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800211 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700212
213 return tb;
214}
215
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800216struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700217{
218 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800219 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700220 unsigned int h;
221
222 if (id == 0)
223 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700224 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700225 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800226 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800227 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700228 if (tb->tb6_id == id) {
229 rcu_read_unlock();
230 return tb;
231 }
232 }
233 rcu_read_unlock();
234
235 return NULL;
236}
237
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000238static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700239{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240 fib6_link_table(net, net->ipv6.fib6_main_tbl);
241 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700242}
Thomas Grafc71099a2006-08-04 23:20:06 -0700243#else
244
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800245struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700246{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700248}
249
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700251{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800252 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700253}
254
David S. Miller4c9483b2011-03-12 16:22:43 -0500255struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700257{
David S. Miller4c9483b2011-03-12 16:22:43 -0500258 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259}
260
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000261static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700262{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800263 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700264}
265
266#endif
267
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200268static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700269{
270 int res;
271 struct rt6_info *rt;
272
Changli Gaod8d1f302010-06-10 23:31:35 -0700273 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700274 res = rt6_dump_route(rt, w->args);
275 if (res < 0) {
276 /* Frame is full, suspend walking */
277 w->leaf = rt;
278 return 1;
279 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700280 }
281 w->leaf = NULL;
282 return 0;
283}
284
285static void fib6_dump_end(struct netlink_callback *cb)
286{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200287 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700288
289 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800290 if (cb->args[4]) {
291 cb->args[4] = 0;
292 fib6_walker_unlink(w);
293 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700294 cb->args[2] = 0;
295 kfree(w);
296 }
Wang Yufen437de072014-03-28 12:07:04 +0800297 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700298 cb->args[1] = 3;
299}
300
301static int fib6_dump_done(struct netlink_callback *cb)
302{
303 fib6_dump_end(cb);
304 return cb->done ? cb->done(cb) : 0;
305}
306
307static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
308 struct netlink_callback *cb)
309{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200310 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700311 int res;
312
313 w = (void *)cb->args[2];
314 w->root = &table->tb6_root;
315
316 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000317 w->count = 0;
318 w->skip = 0;
319
Patrick McHardy1b43af52006-08-10 23:11:17 -0700320 read_lock_bh(&table->tb6_lock);
321 res = fib6_walk(w);
322 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000323 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700324 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000325 cb->args[5] = w->root->fn_sernum;
326 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700327 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000328 if (cb->args[5] != w->root->fn_sernum) {
329 /* Begin at the root if the tree changed */
330 cb->args[5] = w->root->fn_sernum;
331 w->state = FWS_INIT;
332 w->node = w->root;
333 w->skip = w->count;
334 } else
335 w->skip = 0;
336
Patrick McHardy1b43af52006-08-10 23:11:17 -0700337 read_lock_bh(&table->tb6_lock);
338 res = fib6_walk_continue(w);
339 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800340 if (res <= 0) {
341 fib6_walker_unlink(w);
342 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700343 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700344 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800345
Patrick McHardy1b43af52006-08-10 23:11:17 -0700346 return res;
347}
348
Thomas Grafc127ea22007-03-22 11:58:32 -0700349static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700350{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900351 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700352 unsigned int h, s_h;
353 unsigned int e = 0, s_e;
354 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200355 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700356 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800357 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700358 int res = 0;
359
360 s_h = cb->args[0];
361 s_e = cb->args[1];
362
363 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500364 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700365 /* New dump:
366 *
367 * 1. hook callback destructor.
368 */
369 cb->args[3] = (long)cb->done;
370 cb->done = fib6_dump_done;
371
372 /*
373 * 2. allocate and initialize walker.
374 */
375 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500376 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700377 return -ENOMEM;
378 w->func = fib6_dump_node;
379 cb->args[2] = (long)w;
380 }
381
382 arg.skb = skb;
383 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700384 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700385 w->args = &arg;
386
Eric Dumazete67f88d2011-04-27 22:56:07 +0000387 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700388 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700389 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800390 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800391 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700392 if (e < s_e)
393 goto next;
394 res = fib6_dump_table(tb, skb, cb);
395 if (res != 0)
396 goto out;
397next:
398 e++;
399 }
400 }
401out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000402 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700403 cb->args[1] = e;
404 cb->args[0] = h;
405
406 res = res < 0 ? res : skb->len;
407 if (res <= 0)
408 fib6_dump_end(cb);
409 return res;
410}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/*
413 * Routing Table
414 *
415 * return the appropriate node for a routing tree "add" operation
416 * by either creating and inserting or by returning an existing
417 * node.
418 */
419
fan.du9225b232013-07-22 14:21:09 +0800420static struct fib6_node *fib6_add_1(struct fib6_node *root,
421 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000422 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200423 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct fib6_node *fn, *in, *ln;
426 struct fib6_node *pn = NULL;
427 struct rt6key *key;
428 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900429 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 RT6_TRACE("fib6_add_1\n");
432
433 /* insert node in tree */
434
435 fn = root;
436
437 do {
438 key = (struct rt6key *)((u8 *)fn->leaf + offset);
439
440 /*
441 * Prefix match
442 */
443 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000444 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000445 if (!allow_create) {
446 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000447 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000448 return ERR_PTR(-ENOENT);
449 }
Joe Perchesf3213832012-05-15 14:11:53 +0000450 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000453 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 * Exact match ?
457 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (plen == fn->fn_bit) {
460 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500461 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 rt6_release(fn->leaf);
463 fn->leaf = NULL;
464 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return fn;
469 }
470
471 /*
472 * We have more bits to go
473 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* Try to walk down on tree. */
476 fn->fn_sernum = sernum;
477 dir = addr_bit_set(addr, fn->fn_bit);
478 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800479 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } while (fn);
481
Matti Vaittinen14df0152011-11-16 21:18:02 +0000482 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000483 /* We should not create new node because
484 * NLM_F_REPLACE was specified without NLM_F_CREATE
485 * I assume it is safe to require NLM_F_CREATE when
486 * REPLACE flag is used! Later we may want to remove the
487 * check for replace_required, because according
488 * to netlink specification, NLM_F_CREATE
489 * MUST be specified if new route is created.
490 * That would keep IPv6 consistent with IPv4
491 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000492 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000493 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000494 return ERR_PTR(-ENOENT);
495 }
Joe Perchesf3213832012-05-15 14:11:53 +0000496 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /*
499 * We walked to the bottom of tree.
500 * Create new leaf node without children.
501 */
502
503 ln = node_alloc();
504
David S. Miller507c9b12011-12-03 17:50:45 -0500505 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000506 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 ln->parent = pn;
510 ln->fn_sernum = sernum;
511
512 if (dir)
513 pn->right = ln;
514 else
515 pn->left = ln;
516
517 return ln;
518
519
520insert_above:
521 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900522 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * we have a less significant route.
524 * we've to insert an intermediate node on the list
525 * this new node will point to the one we need to create
526 * and the current
527 */
528
529 pn = fn->parent;
530
531 /* find 1st bit in difference between the 2 addrs.
532
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800533 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 but if it is >= plen, the value is ignored in any case.
535 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900536
fan.du9225b232013-07-22 14:21:09 +0800537 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900539 /*
540 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * / \
542 * (new leaf node)[ln] (old node)[fn]
543 */
544 if (plen > bit) {
545 in = node_alloc();
546 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900547
David S. Miller507c9b12011-12-03 17:50:45 -0500548 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (in)
550 node_free(in);
551 if (ln)
552 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000553 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900556 /*
557 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * RTN_RTINFO will
559 * be off since that an address that chooses one of
560 * the branches would not match less specific routes
561 * in the other branch
562 */
563
564 in->fn_bit = bit;
565
566 in->parent = pn;
567 in->leaf = fn->leaf;
568 atomic_inc(&in->leaf->rt6i_ref);
569
570 in->fn_sernum = sernum;
571
572 /* update parent pointer */
573 if (dir)
574 pn->right = in;
575 else
576 pn->left = in;
577
578 ln->fn_bit = plen;
579
580 ln->parent = in;
581 fn->parent = in;
582
583 ln->fn_sernum = sernum;
584
585 if (addr_bit_set(addr, bit)) {
586 in->right = ln;
587 in->left = fn;
588 } else {
589 in->left = ln;
590 in->right = fn;
591 }
592 } else { /* plen <= bit */
593
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900594 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * (new leaf node)[ln]
596 * / \
597 * (old node)[fn] NULL
598 */
599
600 ln = node_alloc();
601
David S. Miller507c9b12011-12-03 17:50:45 -0500602 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000603 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 ln->fn_bit = plen;
606
607 ln->parent = pn;
608
609 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (dir)
612 pn->right = ln;
613 else
614 pn->left = ln;
615
616 if (addr_bit_set(&key->addr, plen))
617 ln->right = fn;
618 else
619 ln->left = fn;
620
621 fn->parent = ln;
622 }
623 return ln;
624}
625
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200626static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200627{
628 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
629 RTF_GATEWAY;
630}
631
Florian Westphale715b6d2015-01-05 23:57:44 +0100632static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100633{
Florian Westphale715b6d2015-01-05 23:57:44 +0100634 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100635
Florian Westphale715b6d2015-01-05 23:57:44 +0100636 for (i = 0; i < RTAX_MAX; i++) {
637 if (test_bit(i, mxc->mx_valid))
638 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100639 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100640}
641
642static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
643{
644 if (!mxc->mx)
645 return 0;
646
647 if (dst->flags & DST_HOST) {
648 u32 *mp = dst_metrics_write_ptr(dst);
649
650 if (unlikely(!mp))
651 return -ENOMEM;
652
653 fib6_copy_metrics(mp, mxc);
654 } else {
655 dst_init_metrics(dst, mxc->mx, false);
656
657 /* We've stolen mx now. */
658 mxc->mx = NULL;
659 }
660
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100661 return 0;
662}
663
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100664static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
665 struct net *net)
666{
667 if (atomic_read(&rt->rt6i_ref) != 1) {
668 /* This route is used as dummy address holder in some split
669 * nodes. It is not leaked, but it still holds other resources,
670 * which must be released in time. So, scan ascendant nodes
671 * and replace dummy references to this route with references
672 * to still alive ones.
673 */
674 while (fn) {
675 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
676 fn->leaf = fib6_find_prefix(net, fn);
677 atomic_inc(&fn->leaf->rt6i_ref);
678 rt6_release(rt);
679 }
680 fn = fn->parent;
681 }
682 /* No more references are possible at this point. */
683 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
684 }
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/*
688 * Insert routing information in a node.
689 */
690
691static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100692 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct rt6_info *iter = NULL;
695 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500696 int replace = (info->nlh &&
697 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
698 int add = (!info->nlh ||
699 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000700 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200701 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100702 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 ins = &fn->leaf;
705
David S. Miller507c9b12011-12-03 17:50:45 -0500706 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
708 * Search for duplicates
709 */
710
711 if (iter->rt6i_metric == rt->rt6i_metric) {
712 /*
713 * Same priority level
714 */
David S. Miller507c9b12011-12-03 17:50:45 -0500715 if (info->nlh &&
716 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000717 return -EEXIST;
718 if (replace) {
719 found++;
720 break;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David S. Millerd1918542011-12-28 20:19:20 -0500723 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 iter->rt6i_idev == rt->rt6i_idev &&
725 ipv6_addr_equal(&iter->rt6i_gateway,
726 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000727 if (rt->rt6i_nsiblings)
728 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500729 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000731 if (!(rt->rt6i_flags & RTF_EXPIRES))
732 rt6_clean_expires(iter);
733 else
734 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EEXIST;
736 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000737 /* If we have the same destination and the same metric,
738 * but not the same gateway, then the route we try to
739 * add is sibling to this route, increment our counter
740 * of siblings, and later we will add our route to the
741 * list.
742 * Only static routes (which don't have flag
743 * RTF_EXPIRES) are used for ECMPv6.
744 *
745 * To avoid long list, we only had siblings if the
746 * route have a gateway.
747 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200748 if (rt_can_ecmp &&
749 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000750 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 if (iter->rt6i_metric > rt->rt6i_metric)
754 break;
755
Changli Gaod8d1f302010-06-10 23:31:35 -0700756 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
David S. Millerf11e6652007-03-24 20:36:25 -0700759 /* Reset round-robin state, if necessary */
760 if (ins == &fn->leaf)
761 fn->rr_ptr = NULL;
762
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000763 /* Link this route to others same route. */
764 if (rt->rt6i_nsiblings) {
765 unsigned int rt6i_nsiblings;
766 struct rt6_info *sibling, *temp_sibling;
767
768 /* Find the first route that have the same metric */
769 sibling = fn->leaf;
770 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200771 if (sibling->rt6i_metric == rt->rt6i_metric &&
772 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000773 list_add_tail(&rt->rt6i_siblings,
774 &sibling->rt6i_siblings);
775 break;
776 }
777 sibling = sibling->dst.rt6_next;
778 }
779 /* For each sibling in the list, increment the counter of
780 * siblings. BUG() if counters does not match, list of siblings
781 * is broken!
782 */
783 rt6i_nsiblings = 0;
784 list_for_each_entry_safe(sibling, temp_sibling,
785 &rt->rt6i_siblings, rt6i_siblings) {
786 sibling->rt6i_nsiblings++;
787 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
788 rt6i_nsiblings++;
789 }
790 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * insert node
795 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000796 if (!replace) {
797 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000798 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000800add:
Florian Westphale715b6d2015-01-05 23:57:44 +0100801 err = fib6_commit_metrics(&rt->dst, mxc);
802 if (err)
803 return err;
804
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000805 rt->dst.rt6_next = iter;
806 *ins = rt;
807 rt->rt6i_node = fn;
808 atomic_inc(&rt->rt6i_ref);
809 inet6_rt_notify(RTM_NEWROUTE, rt, info);
810 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David S. Miller507c9b12011-12-03 17:50:45 -0500812 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000813 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
814 fn->fn_flags |= RTN_RTINFO;
815 }
816
817 } else {
818 if (!found) {
819 if (add)
820 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000821 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000822 return -ENOENT;
823 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100824
825 err = fib6_commit_metrics(&rt->dst, mxc);
826 if (err)
827 return err;
828
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000829 *ins = rt;
830 rt->rt6i_node = fn;
831 rt->dst.rt6_next = iter->dst.rt6_next;
832 atomic_inc(&rt->rt6i_ref);
833 inet6_rt_notify(RTM_NEWROUTE, rt, info);
David S. Miller507c9b12011-12-03 17:50:45 -0500834 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000835 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
836 fn->fn_flags |= RTN_RTINFO;
837 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100838 fib6_purge_rt(iter, fn, info->nl_net);
839 rt6_release(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 return 0;
843}
844
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200845static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700847 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500848 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700849 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700850 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800853void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700855 if (!timer_pending(&net->ipv6.ip6_fib_timer))
856 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700857 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/*
861 * Add routing information to the routing tree.
862 * <destination addr>/<source addr>
863 * with source addr info in sub-trees
864 */
865
Florian Westphale715b6d2015-01-05 23:57:44 +0100866int fib6_add(struct fib6_node *root, struct rt6_info *rt,
867 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700869 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000871 int allow_create = 1;
872 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200873 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500874
875 if (info->nlh) {
876 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000877 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500878 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000879 replace_required = 1;
880 }
881 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000882 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
fan.du9225b232013-07-22 14:21:09 +0800884 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
885 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200886 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000887 if (IS_ERR(fn)) {
888 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200889 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700893 pn = fn;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#ifdef CONFIG_IPV6_SUBTREES
896 if (rt->rt6i_src.plen) {
897 struct fib6_node *sn;
898
David S. Miller507c9b12011-12-03 17:50:45 -0500899 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct fib6_node *sfn;
901
902 /*
903 * Create subtree.
904 *
905 * fn[main tree]
906 * |
907 * sfn[subtree root]
908 * \
909 * sn[new leaf node]
910 */
911
912 /* Create subtree root node */
913 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500914 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto st_failure;
916
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800917 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
918 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200920 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* Now add the first leaf node to new subtree */
923
924 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800925 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000926 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200927 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000929 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* If it is failed, discard just allocated
931 root, and then (in st_failure) stale node
932 in main tree.
933 */
934 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000935 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto st_failure;
937 }
938
939 /* Now link new subtree to main tree */
940 sfn->parent = fn;
941 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 } else {
943 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800944 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000945 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200946 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000948 if (IS_ERR(sn)) {
949 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
David S. Miller507c9b12011-12-03 17:50:45 -0500954 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700955 fn->leaf = rt;
956 atomic_inc(&rt->rt6i_ref);
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 fn = sn;
959 }
960#endif
961
Florian Westphale715b6d2015-01-05 23:57:44 +0100962 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -0500963 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800964 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500965 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +0800966 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700970 if (err) {
971#ifdef CONFIG_IPV6_SUBTREES
972 /*
973 * If fib6_add_1 has cleared the old leaf pointer in the
974 * super-tree leaf node we have to find a new one for it.
975 */
David S. Miller3c051232008-04-18 01:46:19 -0700976 if (pn != fn && pn->leaf == rt) {
977 pn->leaf = NULL;
978 atomic_dec(&rt->rt6i_ref);
979 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700980 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800981 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700982#if RT6_DEBUG >= 2
983 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700984 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800985 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700986 }
987#endif
988 atomic_inc(&pn->leaf->rt6i_ref);
989 }
990#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700991 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return err;
994
995#ifdef CONFIG_IPV6_SUBTREES
996 /* Subtree creation failed, probably main tree node
997 is orphan. If it is, shoot it.
998 */
999st_failure:
1000 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001001 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -07001002 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return err;
1004#endif
1005}
1006
1007/*
1008 * Routing tree lookup
1009 *
1010 */
1011
1012struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001013 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001014 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015};
1016
Wang Yufen437de072014-03-28 12:07:04 +08001017static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1018 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -08001021 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001023 if (unlikely(args->offset == 0))
1024 return NULL;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * Descend on a tree
1028 */
1029
1030 fn = root;
1031
1032 for (;;) {
1033 struct fib6_node *next;
1034
1035 dir = addr_bit_set(args->addr, fn->fn_bit);
1036
1037 next = dir ? fn->right : fn->left;
1038
1039 if (next) {
1040 fn = next;
1041 continue;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 }
1045
David S. Miller507c9b12011-12-03 17:50:45 -05001046 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001047 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct rt6key *key;
1049
1050 key = (struct rt6key *) ((u8 *) fn->leaf +
1051 args->offset);
1052
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001053 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1054#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001055 if (fn->subtree) {
1056 struct fib6_node *sfn;
1057 sfn = fib6_lookup_1(fn->subtree,
1058 args + 1);
1059 if (!sfn)
1060 goto backtrack;
1061 fn = sfn;
1062 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001063#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001064 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001065 return fn;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001068#ifdef CONFIG_IPV6_SUBTREES
1069backtrack:
1070#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001071 if (fn->fn_flags & RTN_ROOT)
1072 break;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 fn = fn->parent;
1075 }
1076
1077 return NULL;
1078}
1079
Wang Yufen437de072014-03-28 12:07:04 +08001080struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1081 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001084 struct lookup_args args[] = {
1085 {
1086 .offset = offsetof(struct rt6_info, rt6i_dst),
1087 .addr = daddr,
1088 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001090 {
1091 .offset = offsetof(struct rt6_info, rt6i_src),
1092 .addr = saddr,
1093 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001095 {
1096 .offset = 0, /* sentinel */
1097 }
1098 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001100 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001101 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 fn = root;
1103
1104 return fn;
1105}
1106
1107/*
1108 * Get node with specified destination prefix (and source prefix,
1109 * if subtrees are used)
1110 */
1111
1112
Wang Yufen437de072014-03-28 12:07:04 +08001113static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1114 const struct in6_addr *addr,
1115 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct fib6_node *fn;
1118
1119 for (fn = root; fn ; ) {
1120 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1121
1122 /*
1123 * Prefix match
1124 */
1125 if (plen < fn->fn_bit ||
1126 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1127 return NULL;
1128
1129 if (plen == fn->fn_bit)
1130 return fn;
1131
1132 /*
1133 * We have more bits to go
1134 */
1135 if (addr_bit_set(addr, fn->fn_bit))
1136 fn = fn->right;
1137 else
1138 fn = fn->left;
1139 }
1140 return NULL;
1141}
1142
Wang Yufen437de072014-03-28 12:07:04 +08001143struct fib6_node *fib6_locate(struct fib6_node *root,
1144 const struct in6_addr *daddr, int dst_len,
1145 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct fib6_node *fn;
1148
1149 fn = fib6_locate_1(root, daddr, dst_len,
1150 offsetof(struct rt6_info, rt6i_dst));
1151
1152#ifdef CONFIG_IPV6_SUBTREES
1153 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001154 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001155 if (fn && fn->subtree)
1156 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 offsetof(struct rt6_info, rt6i_src));
1158 }
1159#endif
1160
David S. Miller507c9b12011-12-03 17:50:45 -05001161 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return fn;
1163
1164 return NULL;
1165}
1166
1167
1168/*
1169 * Deletion
1170 *
1171 */
1172
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001173static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
David S. Miller507c9b12011-12-03 17:50:45 -05001175 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001176 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
David S. Miller507c9b12011-12-03 17:50:45 -05001178 while (fn) {
1179 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001181 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return fn->right->leaf;
1183
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001184 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 return NULL;
1187}
1188
1189/*
1190 * Called to trim the tree of intermediate nodes when possible. "fn"
1191 * is the node we want to try and remove.
1192 */
1193
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001194static struct fib6_node *fib6_repair_tree(struct net *net,
1195 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 int children;
1198 int nstate;
1199 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001200 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 int iter = 0;
1202
1203 for (;;) {
1204 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1205 iter++;
1206
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001207 WARN_ON(fn->fn_flags & RTN_RTINFO);
1208 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001209 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 children = 0;
1212 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001213 if (fn->right)
1214 child = fn->right, children |= 1;
1215 if (fn->left)
1216 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001218 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#ifdef CONFIG_IPV6_SUBTREES
1220 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001221 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222#endif
1223 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001224 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001226 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001227 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001228 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230#endif
1231 atomic_inc(&fn->leaf->rt6i_ref);
1232 return fn->parent;
1233 }
1234
1235 pn = fn->parent;
1236#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001237 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001238 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001239 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 nstate = FWS_L;
1241 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001242 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001244 if (pn->right == fn)
1245 pn->right = child;
1246 else if (pn->left == fn)
1247 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001249 else
1250 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#endif
1252 if (child)
1253 child->parent = pn;
1254 nstate = FWS_R;
1255#ifdef CONFIG_IPV6_SUBTREES
1256 }
1257#endif
1258
1259 read_lock(&fib6_walker_lock);
1260 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001261 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (w->root == fn) {
1263 w->root = w->node = NULL;
1264 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1265 } else if (w->node == fn) {
1266 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1267 w->node = pn;
1268 w->state = nstate;
1269 }
1270 } else {
1271 if (w->root == fn) {
1272 w->root = child;
1273 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1274 }
1275 if (w->node == fn) {
1276 w->node = child;
1277 if (children&2) {
1278 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001279 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else {
1281 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001282 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 }
1285 }
1286 }
1287 read_unlock(&fib6_walker_lock);
1288
1289 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001290 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return pn;
1292
1293 rt6_release(pn->leaf);
1294 pn->leaf = NULL;
1295 fn = pn;
1296 }
1297}
1298
1299static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001300 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001302 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001304 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 RT6_TRACE("fib6_del_route\n");
1307
1308 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001309 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001311 net->ipv6.rt6_stats->fib_rt_entries--;
1312 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
David S. Millerf11e6652007-03-24 20:36:25 -07001314 /* Reset round-robin state, if necessary */
1315 if (fn->rr_ptr == rt)
1316 fn->rr_ptr = NULL;
1317
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001318 /* Remove this entry from other siblings */
1319 if (rt->rt6i_nsiblings) {
1320 struct rt6_info *sibling, *next_sibling;
1321
1322 list_for_each_entry_safe(sibling, next_sibling,
1323 &rt->rt6i_siblings, rt6i_siblings)
1324 sibling->rt6i_nsiblings--;
1325 rt->rt6i_nsiblings = 0;
1326 list_del_init(&rt->rt6i_siblings);
1327 }
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /* Adjust walkers */
1330 read_lock(&fib6_walker_lock);
1331 FOR_WALKERS(w) {
1332 if (w->state == FWS_C && w->leaf == rt) {
1333 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001334 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001335 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 w->state = FWS_U;
1337 }
1338 }
1339 read_unlock(&fib6_walker_lock);
1340
Changli Gaod8d1f302010-06-10 23:31:35 -07001341 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001344 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001346 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001347 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001350 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Thomas Graf86872cb2006-08-22 00:01:08 -07001352 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 rt6_release(rt);
1354}
1355
Thomas Graf86872cb2006-08-22 00:01:08 -07001356int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001358 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct fib6_node *fn = rt->rt6i_node;
1360 struct rt6_info **rtp;
1361
1362#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001363 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001364 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -ENOENT;
1366 }
1367#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001368 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -ENOENT;
1370
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001371 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
David S. Miller507c9b12011-12-03 17:50:45 -05001373 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001374 struct fib6_node *pn = fn;
1375#ifdef CONFIG_IPV6_SUBTREES
1376 /* clones of this route might be in another subtree */
1377 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001378 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001379 pn = pn->parent;
1380 pn = pn->parent;
1381 }
1382#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001383 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /*
1387 * Walk the leaf entries looking for ourself
1388 */
1389
Changli Gaod8d1f302010-06-10 23:31:35 -07001390 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001392 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 0;
1394 }
1395 }
1396 return -ENOENT;
1397}
1398
1399/*
1400 * Tree traversal function.
1401 *
1402 * Certainly, it is not interrupt safe.
1403 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1404 * It means, that we can modify tree during walking
1405 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001406 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 *
1408 * It guarantees that every node will be traversed,
1409 * and that it will be traversed only once.
1410 *
1411 * Callback function w->func may return:
1412 * 0 -> continue walking.
1413 * positive value -> walking is suspended (used by tree dumps,
1414 * and probably by gc, if it will be split to several slices)
1415 * negative value -> terminate walking.
1416 *
1417 * The function itself returns:
1418 * 0 -> walk is complete.
1419 * >0 -> walk is incomplete (i.e. suspended)
1420 * <0 -> walk is terminated by an error.
1421 */
1422
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001423static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct fib6_node *fn, *pn;
1426
1427 for (;;) {
1428 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001429 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431
1432 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001433 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 w->state = FWS_C;
1435 w->leaf = fn->leaf;
1436 }
1437 switch (w->state) {
1438#ifdef CONFIG_IPV6_SUBTREES
1439 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001440 if (FIB6_SUBTREE(fn)) {
1441 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
1443 }
1444 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001445#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 case FWS_L:
1447 if (fn->left) {
1448 w->node = fn->left;
1449 w->state = FWS_INIT;
1450 continue;
1451 }
1452 w->state = FWS_R;
1453 case FWS_R:
1454 if (fn->right) {
1455 w->node = fn->right;
1456 w->state = FWS_INIT;
1457 continue;
1458 }
1459 w->state = FWS_C;
1460 w->leaf = fn->leaf;
1461 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001462 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001463 int err;
1464
Eric Dumazetfa809e22012-06-25 15:37:19 -07001465 if (w->skip) {
1466 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001467 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001468 }
1469
1470 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (err)
1472 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001473
1474 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 continue;
1476 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001477skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 w->state = FWS_U;
1479 case FWS_U:
1480 if (fn == w->root)
1481 return 0;
1482 pn = fn->parent;
1483 w->node = pn;
1484#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001485 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001486 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 w->state = FWS_L;
1488 continue;
1489 }
1490#endif
1491 if (pn->left == fn) {
1492 w->state = FWS_R;
1493 continue;
1494 }
1495 if (pn->right == fn) {
1496 w->state = FWS_C;
1497 w->leaf = w->node->leaf;
1498 continue;
1499 }
1500#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001501 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502#endif
1503 }
1504 }
1505}
1506
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001507static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 int res;
1510
1511 w->state = FWS_INIT;
1512 w->node = w->root;
1513
1514 fib6_walker_link(w);
1515 res = fib6_walk_continue(w);
1516 if (res <= 0)
1517 fib6_walker_unlink(w);
1518 return res;
1519}
1520
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001521static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 int res;
1524 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001525 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001526 struct nl_info info = {
1527 .nl_net = c->net,
1528 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001530 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1531 w->node->fn_sernum != c->sernum)
1532 w->node->fn_sernum = c->sernum;
1533
1534 if (!c->func) {
1535 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1536 w->leaf = NULL;
1537 return 0;
1538 }
1539
Changli Gaod8d1f302010-06-10 23:31:35 -07001540 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 res = c->func(rt, c->arg);
1542 if (res < 0) {
1543 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001544 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (res) {
1546#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001547 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1548 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549#endif
1550 continue;
1551 }
1552 return 0;
1553 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001554 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 w->leaf = rt;
1557 return 0;
1558}
1559
1560/*
1561 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001562 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * func is called on each route.
1564 * It may return -1 -> delete this route.
1565 * 0 -> continue walking
1566 *
1567 * prune==1 -> only immediate children of node (certainly,
1568 * ignoring pure split nodes) will be scanned.
1569 */
1570
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001571static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001572 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001573 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001575 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 c.w.root = root;
1578 c.w.func = fib6_clean_node;
1579 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001580 c.w.count = 0;
1581 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001583 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001585 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 fib6_walk(&c.w);
1588}
1589
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001590static void __fib6_clean_all(struct net *net,
1591 int (*func)(struct rt6_info *, void *),
1592 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001593{
Thomas Grafc71099a2006-08-04 23:20:06 -07001594 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001595 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001596 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001597
Patrick McHardy1b43af52006-08-10 23:11:17 -07001598 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001599 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001600 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001601 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001602 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001603 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001604 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001605 write_unlock_bh(&table->tb6_lock);
1606 }
1607 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001608 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001609}
1610
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001611void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1612 void *arg)
1613{
1614 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1618{
1619 if (rt->rt6i_flags & RTF_CACHE) {
1620 RT6_TRACE("pruning clone %p\n", rt);
1621 return -1;
1622 }
1623
1624 return 0;
1625}
1626
Duan Jiong163cd4e2014-05-09 13:31:43 +08001627static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001629 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1630 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001631}
1632
1633static void fib6_flush_trees(struct net *net)
1634{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001635 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001636
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001637 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640/*
1641 * Garbage collection
1642 */
1643
1644static struct fib6_gc_args
1645{
1646 int timeout;
1647 int more;
1648} gc_args;
1649
1650static int fib6_age(struct rt6_info *rt, void *arg)
1651{
1652 unsigned long now = jiffies;
1653
1654 /*
1655 * check addrconf expiration here.
1656 * Routes are expired even if they are in use.
1657 *
1658 * Also age clones. Note, that clones are aged out
1659 * only if they are not in use now.
1660 */
1661
David S. Millerd1918542011-12-28 20:19:20 -05001662 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1663 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return -1;
1666 }
1667 gc_args.more++;
1668 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001669 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1670 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 RT6_TRACE("aging clone %p\n", rt);
1672 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001673 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1674 struct neighbour *neigh;
1675 __u8 neigh_flags = 0;
1676
1677 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1678 if (neigh) {
1679 neigh_flags = neigh->flags;
1680 neigh_release(neigh);
1681 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001682 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001683 RT6_TRACE("purging route %p via non-router but gateway\n",
1684 rt);
1685 return -1;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688 gc_args.more++;
1689 }
1690
1691 return 0;
1692}
1693
1694static DEFINE_SPINLOCK(fib6_gc_lock);
1695
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001696void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001698 unsigned long now;
1699
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001700 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001702 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1703 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001706 gc_args.timeout = expires ? (int)expires :
1707 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001709 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001710
Li RongQing0c3584d2013-12-27 16:32:38 +08001711 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001712 now = jiffies;
1713 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001716 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001717 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001718 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001719 else
1720 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 spin_unlock_bh(&fib6_gc_lock);
1722}
1723
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001724static void fib6_gc_timer_cb(unsigned long arg)
1725{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001726 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001727}
1728
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001729static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001730{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001731 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1732
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001733 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001734
Benjamin Theryc5728722008-03-03 23:34:17 -08001735 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1736 if (!net->ipv6.rt6_stats)
1737 goto out_timer;
1738
Eric Dumazet10da66f2010-10-13 08:22:03 +00001739 /* Avoid false sharing : Use at least a full cache line */
1740 size = max_t(size_t, size, L1_CACHE_BYTES);
1741
1742 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001743 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001744 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001745
1746 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1747 GFP_KERNEL);
1748 if (!net->ipv6.fib6_main_tbl)
1749 goto out_fib_table_hash;
1750
1751 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001752 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001753 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1754 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001755 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001756
1757#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1758 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1759 GFP_KERNEL);
1760 if (!net->ipv6.fib6_local_tbl)
1761 goto out_fib6_main_tbl;
1762 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001763 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001764 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1765 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001766 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001767#endif
1768 fib6_tables_init(net);
1769
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001770 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001771
1772#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1773out_fib6_main_tbl:
1774 kfree(net->ipv6.fib6_main_tbl);
1775#endif
1776out_fib_table_hash:
1777 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001778out_rt6_stats:
1779 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001780out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001781 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001782}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001783
1784static void fib6_net_exit(struct net *net)
1785{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001786 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001787 del_timer_sync(&net->ipv6.ip6_fib_timer);
1788
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001789#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001790 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001791 kfree(net->ipv6.fib6_local_tbl);
1792#endif
David S. Miller8e773272012-06-11 00:01:52 -07001793 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001794 kfree(net->ipv6.fib6_main_tbl);
1795 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001796 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001797}
1798
1799static struct pernet_operations fib6_net_ops = {
1800 .init = fib6_net_init,
1801 .exit = fib6_net_exit,
1802};
1803
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001804int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001806 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1809 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001810 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001811 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001812 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001813 goto out;
1814
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001815 ret = register_pernet_subsys(&fib6_net_ops);
1816 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001817 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001818
1819 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1820 NULL);
1821 if (ret)
1822 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001823
1824 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001825out:
1826 return ret;
1827
David S. Millere8803b62012-06-16 01:12:19 -07001828out_unregister_subsys:
1829 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001830out_kmem_cache_create:
1831 kmem_cache_destroy(fib6_node_kmem);
1832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
1835void fib6_gc_cleanup(void)
1836{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001837 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 kmem_cache_destroy(fib6_node_kmem);
1839}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001840
1841#ifdef CONFIG_PROC_FS
1842
1843struct ipv6_route_iter {
1844 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001845 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001846 loff_t skip;
1847 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001848 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001849};
1850
1851static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1852{
1853 struct rt6_info *rt = v;
1854 struct ipv6_route_iter *iter = seq->private;
1855
1856 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1857
1858#ifdef CONFIG_IPV6_SUBTREES
1859 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1860#else
1861 seq_puts(seq, "00000000000000000000000000000000 00 ");
1862#endif
1863 if (rt->rt6i_flags & RTF_GATEWAY)
1864 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1865 else
1866 seq_puts(seq, "00000000000000000000000000000000");
1867
1868 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1869 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1870 rt->dst.__use, rt->rt6i_flags,
1871 rt->dst.dev ? rt->dst.dev->name : "");
1872 iter->w.leaf = NULL;
1873 return 0;
1874}
1875
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001876static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001877{
1878 struct ipv6_route_iter *iter = w->args;
1879
1880 if (!iter->skip)
1881 return 1;
1882
1883 do {
1884 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1885 iter->skip--;
1886 if (!iter->skip && iter->w.leaf)
1887 return 1;
1888 } while (iter->w.leaf);
1889
1890 return 0;
1891}
1892
1893static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1894{
1895 memset(&iter->w, 0, sizeof(iter->w));
1896 iter->w.func = ipv6_route_yield;
1897 iter->w.root = &iter->tbl->tb6_root;
1898 iter->w.state = FWS_INIT;
1899 iter->w.node = iter->w.root;
1900 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001901 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001902 INIT_LIST_HEAD(&iter->w.lh);
1903 fib6_walker_link(&iter->w);
1904}
1905
1906static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1907 struct net *net)
1908{
1909 unsigned int h;
1910 struct hlist_node *node;
1911
1912 if (tbl) {
1913 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1914 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1915 } else {
1916 h = 0;
1917 node = NULL;
1918 }
1919
1920 while (!node && h < FIB6_TABLE_HASHSZ) {
1921 node = rcu_dereference_bh(
1922 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1923 }
1924 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1925}
1926
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001927static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1928{
1929 if (iter->sernum != iter->w.root->fn_sernum) {
1930 iter->sernum = iter->w.root->fn_sernum;
1931 iter->w.state = FWS_INIT;
1932 iter->w.node = iter->w.root;
1933 WARN_ON(iter->w.skip);
1934 iter->w.skip = iter->w.count;
1935 }
1936}
1937
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001938static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1939{
1940 int r;
1941 struct rt6_info *n;
1942 struct net *net = seq_file_net(seq);
1943 struct ipv6_route_iter *iter = seq->private;
1944
1945 if (!v)
1946 goto iter_table;
1947
1948 n = ((struct rt6_info *)v)->dst.rt6_next;
1949 if (n) {
1950 ++*pos;
1951 return n;
1952 }
1953
1954iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001955 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001956 read_lock(&iter->tbl->tb6_lock);
1957 r = fib6_walk_continue(&iter->w);
1958 read_unlock(&iter->tbl->tb6_lock);
1959 if (r > 0) {
1960 if (v)
1961 ++*pos;
1962 return iter->w.leaf;
1963 } else if (r < 0) {
1964 fib6_walker_unlink(&iter->w);
1965 return NULL;
1966 }
1967 fib6_walker_unlink(&iter->w);
1968
1969 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1970 if (!iter->tbl)
1971 return NULL;
1972
1973 ipv6_route_seq_setup_walk(iter);
1974 goto iter_table;
1975}
1976
1977static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1978 __acquires(RCU_BH)
1979{
1980 struct net *net = seq_file_net(seq);
1981 struct ipv6_route_iter *iter = seq->private;
1982
1983 rcu_read_lock_bh();
1984 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1985 iter->skip = *pos;
1986
1987 if (iter->tbl) {
1988 ipv6_route_seq_setup_walk(iter);
1989 return ipv6_route_seq_next(seq, NULL, pos);
1990 } else {
1991 return NULL;
1992 }
1993}
1994
1995static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1996{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001997 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001998 return w->node && !(w->state == FWS_U && w->node == w->root);
1999}
2000
2001static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2002 __releases(RCU_BH)
2003{
2004 struct ipv6_route_iter *iter = seq->private;
2005
2006 if (ipv6_route_iter_active(iter))
2007 fib6_walker_unlink(&iter->w);
2008
2009 rcu_read_unlock_bh();
2010}
2011
2012static const struct seq_operations ipv6_route_seq_ops = {
2013 .start = ipv6_route_seq_start,
2014 .next = ipv6_route_seq_next,
2015 .stop = ipv6_route_seq_stop,
2016 .show = ipv6_route_seq_show
2017};
2018
2019int ipv6_route_open(struct inode *inode, struct file *file)
2020{
2021 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2022 sizeof(struct ipv6_route_iter));
2023}
2024
2025#endif /* CONFIG_PROC_FS */