blob: 6f9beb1f286155bdab7efcfc8d8ccb50d81ce871 [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
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080077static void fib6_gc_timer_cb(unsigned long arg);
78
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000079static LIST_HEAD(fib6_walkers);
80#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020082static void fib6_walker_link(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070083{
84 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000085 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -070086 write_unlock_bh(&fib6_walker_lock);
87}
88
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020089static void fib6_walker_unlink(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070090{
91 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000092 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -070093 write_unlock_bh(&fib6_walker_lock);
94}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020095
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020096static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020098 int new, old;
99
100 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200101 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200102 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200103 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
104 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200105 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
109 * Auxiliary address test functions for the radix tree.
110 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900111 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * 64bit processors)
113 */
114
115/*
116 * test bit
117 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000118#if defined(__LITTLE_ENDIAN)
119# define BITOP_BE32_SWIZZLE (0x1F & ~7)
120#else
121# define BITOP_BE32_SWIZZLE 0
122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200124static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000126 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000127 /*
128 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800129 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000130 * is optimized version of
131 * htonl(1 << ((~fn_bit)&0x1F))
132 * See include/asm-generic/bitops/le.h.
133 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700134 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
135 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200138static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct fib6_node *fn;
141
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800142 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return fn;
145}
146
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200147static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 kmem_cache_free(fib6_node_kmem, fn);
150}
151
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200152static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700155 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800158static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700159{
160 unsigned int h;
161
Thomas Graf375216a2006-10-21 20:20:54 -0700162 /*
163 * Initialize table lock at a single place to give lockdep a key,
164 * tables aren't visible prior to being linked to the list.
165 */
166 rwlock_init(&tb->tb6_lock);
167
Neil Hormana33bc5c2009-07-30 18:52:15 -0700168 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700169
170 /*
171 * No protection necessary, this is the only list mutatation
172 * operation, tables never disappear once they exist.
173 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800174 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700175}
176
177#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800178
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800179static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700180{
181 struct fib6_table *table;
182
183 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500184 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700185 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800186 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700187 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700188 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700189 }
190
191 return table;
192}
193
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800194struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700195{
196 struct fib6_table *tb;
197
198 if (id == 0)
199 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800200 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700201 if (tb)
202 return tb;
203
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800204 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500205 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800206 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700207
208 return tb;
209}
210
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800211struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700212{
213 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800214 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700215 unsigned int h;
216
217 if (id == 0)
218 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700219 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700220 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800221 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800222 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700223 if (tb->tb6_id == id) {
224 rcu_read_unlock();
225 return tb;
226 }
227 }
228 rcu_read_unlock();
229
230 return NULL;
231}
232
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000233static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700234{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800235 fib6_link_table(net, net->ipv6.fib6_main_tbl);
236 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700237}
Thomas Grafc71099a2006-08-04 23:20:06 -0700238#else
239
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700241{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800242 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700243}
244
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800245struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700246{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700248}
249
David S. Miller4c9483b2011-03-12 16:22:43 -0500250struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800251 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700252{
David S. Miller4c9483b2011-03-12 16:22:43 -0500253 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700254}
255
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000256static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700257{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259}
260
261#endif
262
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200263static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700264{
265 int res;
266 struct rt6_info *rt;
267
Changli Gaod8d1f302010-06-10 23:31:35 -0700268 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700269 res = rt6_dump_route(rt, w->args);
270 if (res < 0) {
271 /* Frame is full, suspend walking */
272 w->leaf = rt;
273 return 1;
274 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700275 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700276 }
277 w->leaf = NULL;
278 return 0;
279}
280
281static void fib6_dump_end(struct netlink_callback *cb)
282{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200283 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700284
285 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800286 if (cb->args[4]) {
287 cb->args[4] = 0;
288 fib6_walker_unlink(w);
289 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700290 cb->args[2] = 0;
291 kfree(w);
292 }
Wang Yufen437de072014-03-28 12:07:04 +0800293 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700294 cb->args[1] = 3;
295}
296
297static int fib6_dump_done(struct netlink_callback *cb)
298{
299 fib6_dump_end(cb);
300 return cb->done ? cb->done(cb) : 0;
301}
302
303static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
304 struct netlink_callback *cb)
305{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200306 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700307 int res;
308
309 w = (void *)cb->args[2];
310 w->root = &table->tb6_root;
311
312 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000313 w->count = 0;
314 w->skip = 0;
315
Patrick McHardy1b43af52006-08-10 23:11:17 -0700316 read_lock_bh(&table->tb6_lock);
317 res = fib6_walk(w);
318 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000319 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700320 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000321 cb->args[5] = w->root->fn_sernum;
322 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700323 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000324 if (cb->args[5] != w->root->fn_sernum) {
325 /* Begin at the root if the tree changed */
326 cb->args[5] = w->root->fn_sernum;
327 w->state = FWS_INIT;
328 w->node = w->root;
329 w->skip = w->count;
330 } else
331 w->skip = 0;
332
Patrick McHardy1b43af52006-08-10 23:11:17 -0700333 read_lock_bh(&table->tb6_lock);
334 res = fib6_walk_continue(w);
335 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800336 if (res <= 0) {
337 fib6_walker_unlink(w);
338 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700339 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700340 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800341
Patrick McHardy1b43af52006-08-10 23:11:17 -0700342 return res;
343}
344
Thomas Grafc127ea22007-03-22 11:58:32 -0700345static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700346{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900347 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700348 unsigned int h, s_h;
349 unsigned int e = 0, s_e;
350 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200351 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700352 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800353 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700354 int res = 0;
355
356 s_h = cb->args[0];
357 s_e = cb->args[1];
358
359 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500360 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700361 /* New dump:
362 *
363 * 1. hook callback destructor.
364 */
365 cb->args[3] = (long)cb->done;
366 cb->done = fib6_dump_done;
367
368 /*
369 * 2. allocate and initialize walker.
370 */
371 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500372 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700373 return -ENOMEM;
374 w->func = fib6_dump_node;
375 cb->args[2] = (long)w;
376 }
377
378 arg.skb = skb;
379 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700380 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700381 w->args = &arg;
382
Eric Dumazete67f88d2011-04-27 22:56:07 +0000383 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700384 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700385 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800386 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800387 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700388 if (e < s_e)
389 goto next;
390 res = fib6_dump_table(tb, skb, cb);
391 if (res != 0)
392 goto out;
393next:
394 e++;
395 }
396 }
397out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000398 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700399 cb->args[1] = e;
400 cb->args[0] = h;
401
402 res = res < 0 ? res : skb->len;
403 if (res <= 0)
404 fib6_dump_end(cb);
405 return res;
406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/*
409 * Routing Table
410 *
411 * return the appropriate node for a routing tree "add" operation
412 * by either creating and inserting or by returning an existing
413 * node.
414 */
415
fan.du9225b232013-07-22 14:21:09 +0800416static struct fib6_node *fib6_add_1(struct fib6_node *root,
417 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000418 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200419 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct fib6_node *fn, *in, *ln;
422 struct fib6_node *pn = NULL;
423 struct rt6key *key;
424 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900425 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 RT6_TRACE("fib6_add_1\n");
428
429 /* insert node in tree */
430
431 fn = root;
432
433 do {
434 key = (struct rt6key *)((u8 *)fn->leaf + offset);
435
436 /*
437 * Prefix match
438 */
439 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000440 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000441 if (!allow_create) {
442 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000443 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000444 return ERR_PTR(-ENOENT);
445 }
Joe Perchesf3213832012-05-15 14:11:53 +0000446 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000449 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
452 * Exact match ?
453 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (plen == fn->fn_bit) {
456 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500457 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 rt6_release(fn->leaf);
459 fn->leaf = NULL;
460 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return fn;
465 }
466
467 /*
468 * We have more bits to go
469 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Try to walk down on tree. */
472 fn->fn_sernum = sernum;
473 dir = addr_bit_set(addr, fn->fn_bit);
474 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800475 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } while (fn);
477
Matti Vaittinen14df0152011-11-16 21:18:02 +0000478 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000479 /* We should not create new node because
480 * NLM_F_REPLACE was specified without NLM_F_CREATE
481 * I assume it is safe to require NLM_F_CREATE when
482 * REPLACE flag is used! Later we may want to remove the
483 * check for replace_required, because according
484 * to netlink specification, NLM_F_CREATE
485 * MUST be specified if new route is created.
486 * That would keep IPv6 consistent with IPv4
487 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000488 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000489 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000490 return ERR_PTR(-ENOENT);
491 }
Joe Perchesf3213832012-05-15 14:11:53 +0000492 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * We walked to the bottom of tree.
496 * Create new leaf node without children.
497 */
498
499 ln = node_alloc();
500
David S. Miller507c9b12011-12-03 17:50:45 -0500501 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000502 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ln->parent = pn;
506 ln->fn_sernum = sernum;
507
508 if (dir)
509 pn->right = ln;
510 else
511 pn->left = ln;
512
513 return ln;
514
515
516insert_above:
517 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900518 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * we have a less significant route.
520 * we've to insert an intermediate node on the list
521 * this new node will point to the one we need to create
522 * and the current
523 */
524
525 pn = fn->parent;
526
527 /* find 1st bit in difference between the 2 addrs.
528
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800529 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 but if it is >= plen, the value is ignored in any case.
531 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900532
fan.du9225b232013-07-22 14:21:09 +0800533 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900535 /*
536 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * / \
538 * (new leaf node)[ln] (old node)[fn]
539 */
540 if (plen > bit) {
541 in = node_alloc();
542 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900543
David S. Miller507c9b12011-12-03 17:50:45 -0500544 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (in)
546 node_free(in);
547 if (ln)
548 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000549 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900552 /*
553 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * RTN_RTINFO will
555 * be off since that an address that chooses one of
556 * the branches would not match less specific routes
557 * in the other branch
558 */
559
560 in->fn_bit = bit;
561
562 in->parent = pn;
563 in->leaf = fn->leaf;
564 atomic_inc(&in->leaf->rt6i_ref);
565
566 in->fn_sernum = sernum;
567
568 /* update parent pointer */
569 if (dir)
570 pn->right = in;
571 else
572 pn->left = in;
573
574 ln->fn_bit = plen;
575
576 ln->parent = in;
577 fn->parent = in;
578
579 ln->fn_sernum = sernum;
580
581 if (addr_bit_set(addr, bit)) {
582 in->right = ln;
583 in->left = fn;
584 } else {
585 in->left = ln;
586 in->right = fn;
587 }
588 } else { /* plen <= bit */
589
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900590 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * (new leaf node)[ln]
592 * / \
593 * (old node)[fn] NULL
594 */
595
596 ln = node_alloc();
597
David S. Miller507c9b12011-12-03 17:50:45 -0500598 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000599 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 ln->fn_bit = plen;
602
603 ln->parent = pn;
604
605 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (dir)
608 pn->right = ln;
609 else
610 pn->left = ln;
611
612 if (addr_bit_set(&key->addr, plen))
613 ln->right = fn;
614 else
615 ln->left = fn;
616
617 fn->parent = ln;
618 }
619 return ln;
620}
621
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200622static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200623{
624 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
625 RTF_GATEWAY;
626}
627
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100628static int fib6_commit_metrics(struct dst_entry *dst,
629 struct nlattr *mx, int mx_len)
630{
631 struct nlattr *nla;
632 int remaining;
633 u32 *mp;
634
635 if (dst->flags & DST_HOST) {
636 mp = dst_metrics_write_ptr(dst);
637 } else {
Benjamin Block793c3b42014-08-21 19:37:48 +0200638 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100639 if (!mp)
640 return -ENOMEM;
641 dst_init_metrics(dst, mp, 0);
642 }
643
644 nla_for_each_attr(nla, mx, mx_len, remaining) {
645 int type = nla_type(nla);
646
647 if (type) {
648 if (type > RTAX_MAX)
649 return -EINVAL;
650
651 mp[type - 1] = nla_get_u32(nla);
652 }
653 }
654 return 0;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/*
658 * Insert routing information in a node.
659 */
660
661static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100662 struct nl_info *info, struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct rt6_info *iter = NULL;
665 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500666 int replace = (info->nlh &&
667 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
668 int add = (!info->nlh ||
669 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000670 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200671 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100672 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 ins = &fn->leaf;
675
David S. Miller507c9b12011-12-03 17:50:45 -0500676 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Search for duplicates
679 */
680
681 if (iter->rt6i_metric == rt->rt6i_metric) {
682 /*
683 * Same priority level
684 */
David S. Miller507c9b12011-12-03 17:50:45 -0500685 if (info->nlh &&
686 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000687 return -EEXIST;
688 if (replace) {
689 found++;
690 break;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
David S. Millerd1918542011-12-28 20:19:20 -0500693 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 iter->rt6i_idev == rt->rt6i_idev &&
695 ipv6_addr_equal(&iter->rt6i_gateway,
696 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000697 if (rt->rt6i_nsiblings)
698 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500699 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000701 if (!(rt->rt6i_flags & RTF_EXPIRES))
702 rt6_clean_expires(iter);
703 else
704 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EEXIST;
706 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000707 /* If we have the same destination and the same metric,
708 * but not the same gateway, then the route we try to
709 * add is sibling to this route, increment our counter
710 * of siblings, and later we will add our route to the
711 * list.
712 * Only static routes (which don't have flag
713 * RTF_EXPIRES) are used for ECMPv6.
714 *
715 * To avoid long list, we only had siblings if the
716 * route have a gateway.
717 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200718 if (rt_can_ecmp &&
719 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000720 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 if (iter->rt6i_metric > rt->rt6i_metric)
724 break;
725
Changli Gaod8d1f302010-06-10 23:31:35 -0700726 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
David S. Millerf11e6652007-03-24 20:36:25 -0700729 /* Reset round-robin state, if necessary */
730 if (ins == &fn->leaf)
731 fn->rr_ptr = NULL;
732
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000733 /* Link this route to others same route. */
734 if (rt->rt6i_nsiblings) {
735 unsigned int rt6i_nsiblings;
736 struct rt6_info *sibling, *temp_sibling;
737
738 /* Find the first route that have the same metric */
739 sibling = fn->leaf;
740 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200741 if (sibling->rt6i_metric == rt->rt6i_metric &&
742 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000743 list_add_tail(&rt->rt6i_siblings,
744 &sibling->rt6i_siblings);
745 break;
746 }
747 sibling = sibling->dst.rt6_next;
748 }
749 /* For each sibling in the list, increment the counter of
750 * siblings. BUG() if counters does not match, list of siblings
751 * is broken!
752 */
753 rt6i_nsiblings = 0;
754 list_for_each_entry_safe(sibling, temp_sibling,
755 &rt->rt6i_siblings, rt6i_siblings) {
756 sibling->rt6i_nsiblings++;
757 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
758 rt6i_nsiblings++;
759 }
760 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
761 }
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * insert node
765 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000766 if (!replace) {
767 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000768 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000770add:
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100771 if (mx) {
772 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
773 if (err)
774 return err;
775 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000776 rt->dst.rt6_next = iter;
777 *ins = rt;
778 rt->rt6i_node = fn;
779 atomic_inc(&rt->rt6i_ref);
780 inet6_rt_notify(RTM_NEWROUTE, rt, info);
781 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
David S. Miller507c9b12011-12-03 17:50:45 -0500783 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000784 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
785 fn->fn_flags |= RTN_RTINFO;
786 }
787
788 } else {
789 if (!found) {
790 if (add)
791 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000792 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000793 return -ENOENT;
794 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100795 if (mx) {
796 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
797 if (err)
798 return err;
799 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000800 *ins = rt;
801 rt->rt6i_node = fn;
802 rt->dst.rt6_next = iter->dst.rt6_next;
803 atomic_inc(&rt->rt6i_ref);
804 inet6_rt_notify(RTM_NEWROUTE, rt, info);
805 rt6_release(iter);
David S. Miller507c9b12011-12-03 17:50:45 -0500806 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000807 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
808 fn->fn_flags |= RTN_RTINFO;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 return 0;
813}
814
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200815static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700817 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500818 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700819 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700820 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800823void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700825 if (!timer_pending(&net->ipv6.ip6_fib_timer))
826 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700827 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830/*
831 * Add routing information to the routing tree.
832 * <destination addr>/<source addr>
833 * with source addr info in sub-trees
834 */
835
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100836int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
837 struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700839 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000841 int allow_create = 1;
842 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200843 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500844
845 if (info->nlh) {
846 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000847 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500848 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000849 replace_required = 1;
850 }
851 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000852 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
fan.du9225b232013-07-22 14:21:09 +0800854 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
855 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200856 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000857 if (IS_ERR(fn)) {
858 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200859 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700863 pn = fn;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865#ifdef CONFIG_IPV6_SUBTREES
866 if (rt->rt6i_src.plen) {
867 struct fib6_node *sn;
868
David S. Miller507c9b12011-12-03 17:50:45 -0500869 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct fib6_node *sfn;
871
872 /*
873 * Create subtree.
874 *
875 * fn[main tree]
876 * |
877 * sfn[subtree root]
878 * \
879 * sn[new leaf node]
880 */
881
882 /* Create subtree root node */
883 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500884 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto st_failure;
886
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800887 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
888 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200890 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /* Now add the first leaf node to new subtree */
893
894 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800895 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000896 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200897 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000899 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* If it is failed, discard just allocated
901 root, and then (in st_failure) stale node
902 in main tree.
903 */
904 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000905 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto st_failure;
907 }
908
909 /* Now link new subtree to main tree */
910 sfn->parent = fn;
911 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 } else {
913 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800914 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000915 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200916 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000918 if (IS_ERR(sn)) {
919 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
David S. Miller507c9b12011-12-03 17:50:45 -0500924 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700925 fn->leaf = rt;
926 atomic_inc(&rt->rt6i_ref);
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 fn = sn;
929 }
930#endif
931
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100932 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
David S. Miller507c9b12011-12-03 17:50:45 -0500933 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800934 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500935 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +0800936 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700940 if (err) {
941#ifdef CONFIG_IPV6_SUBTREES
942 /*
943 * If fib6_add_1 has cleared the old leaf pointer in the
944 * super-tree leaf node we have to find a new one for it.
945 */
David S. Miller3c051232008-04-18 01:46:19 -0700946 if (pn != fn && pn->leaf == rt) {
947 pn->leaf = NULL;
948 atomic_dec(&rt->rt6i_ref);
949 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700950 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800951 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700952#if RT6_DEBUG >= 2
953 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700954 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800955 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700956 }
957#endif
958 atomic_inc(&pn->leaf->rt6i_ref);
959 }
960#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700961 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return err;
964
965#ifdef CONFIG_IPV6_SUBTREES
966 /* Subtree creation failed, probably main tree node
967 is orphan. If it is, shoot it.
968 */
969st_failure:
970 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800971 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700972 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return err;
974#endif
975}
976
977/*
978 * Routing tree lookup
979 *
980 */
981
982struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -0500983 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000984 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985};
986
Wang Yufen437de072014-03-28 12:07:04 +0800987static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
988 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -0800991 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700993 if (unlikely(args->offset == 0))
994 return NULL;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /*
997 * Descend on a tree
998 */
999
1000 fn = root;
1001
1002 for (;;) {
1003 struct fib6_node *next;
1004
1005 dir = addr_bit_set(args->addr, fn->fn_bit);
1006
1007 next = dir ? fn->right : fn->left;
1008
1009 if (next) {
1010 fn = next;
1011 continue;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 }
1015
David S. Miller507c9b12011-12-03 17:50:45 -05001016 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001017 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct rt6key *key;
1019
1020 key = (struct rt6key *) ((u8 *) fn->leaf +
1021 args->offset);
1022
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001023 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1024#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001025 if (fn->subtree) {
1026 struct fib6_node *sfn;
1027 sfn = fib6_lookup_1(fn->subtree,
1028 args + 1);
1029 if (!sfn)
1030 goto backtrack;
1031 fn = sfn;
1032 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001033#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001034 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001035 return fn;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001038#ifdef CONFIG_IPV6_SUBTREES
1039backtrack:
1040#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001041 if (fn->fn_flags & RTN_ROOT)
1042 break;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 fn = fn->parent;
1045 }
1046
1047 return NULL;
1048}
1049
Wang Yufen437de072014-03-28 12:07:04 +08001050struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1051 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001054 struct lookup_args args[] = {
1055 {
1056 .offset = offsetof(struct rt6_info, rt6i_dst),
1057 .addr = daddr,
1058 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001060 {
1061 .offset = offsetof(struct rt6_info, rt6i_src),
1062 .addr = saddr,
1063 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001065 {
1066 .offset = 0, /* sentinel */
1067 }
1068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001070 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001071 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 fn = root;
1073
1074 return fn;
1075}
1076
1077/*
1078 * Get node with specified destination prefix (and source prefix,
1079 * if subtrees are used)
1080 */
1081
1082
Wang Yufen437de072014-03-28 12:07:04 +08001083static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1084 const struct in6_addr *addr,
1085 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 struct fib6_node *fn;
1088
1089 for (fn = root; fn ; ) {
1090 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1091
1092 /*
1093 * Prefix match
1094 */
1095 if (plen < fn->fn_bit ||
1096 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1097 return NULL;
1098
1099 if (plen == fn->fn_bit)
1100 return fn;
1101
1102 /*
1103 * We have more bits to go
1104 */
1105 if (addr_bit_set(addr, fn->fn_bit))
1106 fn = fn->right;
1107 else
1108 fn = fn->left;
1109 }
1110 return NULL;
1111}
1112
Wang Yufen437de072014-03-28 12:07:04 +08001113struct fib6_node *fib6_locate(struct fib6_node *root,
1114 const struct in6_addr *daddr, int dst_len,
1115 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct fib6_node *fn;
1118
1119 fn = fib6_locate_1(root, daddr, dst_len,
1120 offsetof(struct rt6_info, rt6i_dst));
1121
1122#ifdef CONFIG_IPV6_SUBTREES
1123 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001124 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001125 if (fn && fn->subtree)
1126 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 offsetof(struct rt6_info, rt6i_src));
1128 }
1129#endif
1130
David S. Miller507c9b12011-12-03 17:50:45 -05001131 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return fn;
1133
1134 return NULL;
1135}
1136
1137
1138/*
1139 * Deletion
1140 *
1141 */
1142
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001143static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
David S. Miller507c9b12011-12-03 17:50:45 -05001145 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001146 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
David S. Miller507c9b12011-12-03 17:50:45 -05001148 while (fn) {
1149 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001151 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return fn->right->leaf;
1153
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001154 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 return NULL;
1157}
1158
1159/*
1160 * Called to trim the tree of intermediate nodes when possible. "fn"
1161 * is the node we want to try and remove.
1162 */
1163
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001164static struct fib6_node *fib6_repair_tree(struct net *net,
1165 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 int children;
1168 int nstate;
1169 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001170 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int iter = 0;
1172
1173 for (;;) {
1174 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1175 iter++;
1176
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001177 WARN_ON(fn->fn_flags & RTN_RTINFO);
1178 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1179 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 children = 0;
1182 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001183 if (fn->right)
1184 child = fn->right, children |= 1;
1185 if (fn->left)
1186 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001188 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189#ifdef CONFIG_IPV6_SUBTREES
1190 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001191 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192#endif
1193 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001194 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001196 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001197 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001198 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200#endif
1201 atomic_inc(&fn->leaf->rt6i_ref);
1202 return fn->parent;
1203 }
1204
1205 pn = fn->parent;
1206#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001207 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001208 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001209 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 nstate = FWS_L;
1211 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001212 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001214 if (pn->right == fn)
1215 pn->right = child;
1216 else if (pn->left == fn)
1217 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001219 else
1220 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221#endif
1222 if (child)
1223 child->parent = pn;
1224 nstate = FWS_R;
1225#ifdef CONFIG_IPV6_SUBTREES
1226 }
1227#endif
1228
1229 read_lock(&fib6_walker_lock);
1230 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001231 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (w->root == fn) {
1233 w->root = w->node = NULL;
1234 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1235 } else if (w->node == fn) {
1236 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1237 w->node = pn;
1238 w->state = nstate;
1239 }
1240 } else {
1241 if (w->root == fn) {
1242 w->root = child;
1243 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1244 }
1245 if (w->node == fn) {
1246 w->node = child;
1247 if (children&2) {
1248 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001249 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 } else {
1251 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001252 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 }
1255 }
1256 }
1257 read_unlock(&fib6_walker_lock);
1258
1259 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001260 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return pn;
1262
1263 rt6_release(pn->leaf);
1264 pn->leaf = NULL;
1265 fn = pn;
1266 }
1267}
1268
1269static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001270 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001272 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001274 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 RT6_TRACE("fib6_del_route\n");
1277
1278 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001279 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001281 net->ipv6.rt6_stats->fib_rt_entries--;
1282 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
David S. Millerf11e6652007-03-24 20:36:25 -07001284 /* Reset round-robin state, if necessary */
1285 if (fn->rr_ptr == rt)
1286 fn->rr_ptr = NULL;
1287
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001288 /* Remove this entry from other siblings */
1289 if (rt->rt6i_nsiblings) {
1290 struct rt6_info *sibling, *next_sibling;
1291
1292 list_for_each_entry_safe(sibling, next_sibling,
1293 &rt->rt6i_siblings, rt6i_siblings)
1294 sibling->rt6i_nsiblings--;
1295 rt->rt6i_nsiblings = 0;
1296 list_del_init(&rt->rt6i_siblings);
1297 }
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* Adjust walkers */
1300 read_lock(&fib6_walker_lock);
1301 FOR_WALKERS(w) {
1302 if (w->state == FWS_C && w->leaf == rt) {
1303 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001304 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001305 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 w->state = FWS_U;
1307 }
1308 }
1309 read_unlock(&fib6_walker_lock);
1310
Changli Gaod8d1f302010-06-10 23:31:35 -07001311 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001314 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001316 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001317 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 if (atomic_read(&rt->rt6i_ref) != 1) {
1321 /* This route is used as dummy address holder in some split
1322 * nodes. It is not leaked, but it still holds other resources,
1323 * which must be released in time. So, scan ascendant nodes
1324 * and replace dummy references to this route with references
1325 * to still alive ones.
1326 */
1327 while (fn) {
David S. Miller507c9b12011-12-03 17:50:45 -05001328 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001329 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 atomic_inc(&fn->leaf->rt6i_ref);
1331 rt6_release(rt);
1332 }
1333 fn = fn->parent;
1334 }
1335 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001336 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Thomas Graf86872cb2006-08-22 00:01:08 -07001339 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 rt6_release(rt);
1341}
1342
Thomas Graf86872cb2006-08-22 00:01:08 -07001343int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001345 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct fib6_node *fn = rt->rt6i_node;
1347 struct rt6_info **rtp;
1348
1349#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001350 if (rt->dst.obsolete > 0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001351 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -ENOENT;
1353 }
1354#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001355 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -ENOENT;
1357
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001358 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
David S. Miller507c9b12011-12-03 17:50:45 -05001360 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001361 struct fib6_node *pn = fn;
1362#ifdef CONFIG_IPV6_SUBTREES
1363 /* clones of this route might be in another subtree */
1364 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001365 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001366 pn = pn->parent;
1367 pn = pn->parent;
1368 }
1369#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001370 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
1374 * Walk the leaf entries looking for ourself
1375 */
1376
Changli Gaod8d1f302010-06-10 23:31:35 -07001377 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001379 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return 0;
1381 }
1382 }
1383 return -ENOENT;
1384}
1385
1386/*
1387 * Tree traversal function.
1388 *
1389 * Certainly, it is not interrupt safe.
1390 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1391 * It means, that we can modify tree during walking
1392 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001393 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
1395 * It guarantees that every node will be traversed,
1396 * and that it will be traversed only once.
1397 *
1398 * Callback function w->func may return:
1399 * 0 -> continue walking.
1400 * positive value -> walking is suspended (used by tree dumps,
1401 * and probably by gc, if it will be split to several slices)
1402 * negative value -> terminate walking.
1403 *
1404 * The function itself returns:
1405 * 0 -> walk is complete.
1406 * >0 -> walk is incomplete (i.e. suspended)
1407 * <0 -> walk is terminated by an error.
1408 */
1409
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001410static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct fib6_node *fn, *pn;
1413
1414 for (;;) {
1415 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001416 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
1418
1419 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001420 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 w->state = FWS_C;
1422 w->leaf = fn->leaf;
1423 }
1424 switch (w->state) {
1425#ifdef CONFIG_IPV6_SUBTREES
1426 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001427 if (FIB6_SUBTREE(fn)) {
1428 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 continue;
1430 }
1431 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 case FWS_L:
1434 if (fn->left) {
1435 w->node = fn->left;
1436 w->state = FWS_INIT;
1437 continue;
1438 }
1439 w->state = FWS_R;
1440 case FWS_R:
1441 if (fn->right) {
1442 w->node = fn->right;
1443 w->state = FWS_INIT;
1444 continue;
1445 }
1446 w->state = FWS_C;
1447 w->leaf = fn->leaf;
1448 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001449 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001450 int err;
1451
Eric Dumazetfa809e22012-06-25 15:37:19 -07001452 if (w->skip) {
1453 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001454 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001455 }
1456
1457 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (err)
1459 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001460
1461 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 continue;
1463 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001464skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 w->state = FWS_U;
1466 case FWS_U:
1467 if (fn == w->root)
1468 return 0;
1469 pn = fn->parent;
1470 w->node = pn;
1471#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001472 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001473 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 w->state = FWS_L;
1475 continue;
1476 }
1477#endif
1478 if (pn->left == fn) {
1479 w->state = FWS_R;
1480 continue;
1481 }
1482 if (pn->right == fn) {
1483 w->state = FWS_C;
1484 w->leaf = w->node->leaf;
1485 continue;
1486 }
1487#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001488 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489#endif
1490 }
1491 }
1492}
1493
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001494static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 int res;
1497
1498 w->state = FWS_INIT;
1499 w->node = w->root;
1500
1501 fib6_walker_link(w);
1502 res = fib6_walk_continue(w);
1503 if (res <= 0)
1504 fib6_walker_unlink(w);
1505 return res;
1506}
1507
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001508static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 int res;
1511 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001512 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001513 struct nl_info info = {
1514 .nl_net = c->net,
1515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Changli Gaod8d1f302010-06-10 23:31:35 -07001517 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 res = c->func(rt, c->arg);
1519 if (res < 0) {
1520 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001521 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (res) {
1523#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001524 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1525 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#endif
1527 continue;
1528 }
1529 return 0;
1530 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001531 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 w->leaf = rt;
1534 return 0;
1535}
1536
1537/*
1538 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001539 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * func is called on each route.
1541 * It may return -1 -> delete this route.
1542 * 0 -> continue walking
1543 *
1544 * prune==1 -> only immediate children of node (certainly,
1545 * ignoring pure split nodes) will be scanned.
1546 */
1547
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001548static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001549 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001550 bool prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001552 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 c.w.root = root;
1555 c.w.func = fib6_clean_node;
1556 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001557 c.w.count = 0;
1558 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 c.func = func;
1560 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001561 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 fib6_walk(&c.w);
1564}
1565
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001566void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Li RongQing0c3584d2013-12-27 16:32:38 +08001567 void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001568{
Thomas Grafc71099a2006-08-04 23:20:06 -07001569 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001570 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001571 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001572
Patrick McHardy1b43af52006-08-10 23:11:17 -07001573 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001574 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001575 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001576 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001577 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001578 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001579 func, false, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001580 write_unlock_bh(&table->tb6_lock);
1581 }
1582 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001583 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1587{
1588 if (rt->rt6i_flags & RTF_CACHE) {
1589 RT6_TRACE("pruning clone %p\n", rt);
1590 return -1;
1591 }
1592
1593 return 0;
1594}
1595
Duan Jiong163cd4e2014-05-09 13:31:43 +08001596static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001598 fib6_clean_tree(net, fn, fib6_prune_clone, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001601static int fib6_update_sernum(struct rt6_info *rt, void *arg)
1602{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001603 int sernum = *(int *)arg;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001604
1605 if (rt->rt6i_node &&
1606 rt->rt6i_node->fn_sernum != sernum)
1607 rt->rt6i_node->fn_sernum = sernum;
1608
1609 return 0;
1610}
1611
1612static void fib6_flush_trees(struct net *net)
1613{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001614 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001615
1616 fib6_clean_all(net, fib6_update_sernum, &new_sernum);
1617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/*
1620 * Garbage collection
1621 */
1622
1623static struct fib6_gc_args
1624{
1625 int timeout;
1626 int more;
1627} gc_args;
1628
1629static int fib6_age(struct rt6_info *rt, void *arg)
1630{
1631 unsigned long now = jiffies;
1632
1633 /*
1634 * check addrconf expiration here.
1635 * Routes are expired even if they are in use.
1636 *
1637 * Also age clones. Note, that clones are aged out
1638 * only if they are not in use now.
1639 */
1640
David S. Millerd1918542011-12-28 20:19:20 -05001641 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1642 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -1;
1645 }
1646 gc_args.more++;
1647 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001648 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1649 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 RT6_TRACE("aging clone %p\n", rt);
1651 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001652 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1653 struct neighbour *neigh;
1654 __u8 neigh_flags = 0;
1655
1656 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1657 if (neigh) {
1658 neigh_flags = neigh->flags;
1659 neigh_release(neigh);
1660 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001661 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001662 RT6_TRACE("purging route %p via non-router but gateway\n",
1663 rt);
1664 return -1;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 gc_args.more++;
1668 }
1669
1670 return 0;
1671}
1672
1673static DEFINE_SPINLOCK(fib6_gc_lock);
1674
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001675void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001677 unsigned long now;
1678
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001679 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001681 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1682 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001685 gc_args.timeout = expires ? (int)expires :
1686 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001688 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001689
Li RongQing0c3584d2013-12-27 16:32:38 +08001690 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001691 now = jiffies;
1692 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001695 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001696 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001697 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001698 else
1699 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 spin_unlock_bh(&fib6_gc_lock);
1701}
1702
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001703static void fib6_gc_timer_cb(unsigned long arg)
1704{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001705 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001706}
1707
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001708static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001709{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001710 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1711
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001712 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001713
Benjamin Theryc5728722008-03-03 23:34:17 -08001714 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1715 if (!net->ipv6.rt6_stats)
1716 goto out_timer;
1717
Eric Dumazet10da66f2010-10-13 08:22:03 +00001718 /* Avoid false sharing : Use at least a full cache line */
1719 size = max_t(size_t, size, L1_CACHE_BYTES);
1720
1721 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001722 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001723 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001724
1725 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1726 GFP_KERNEL);
1727 if (!net->ipv6.fib6_main_tbl)
1728 goto out_fib_table_hash;
1729
1730 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001731 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001732 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1733 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001734 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001735
1736#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1737 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1738 GFP_KERNEL);
1739 if (!net->ipv6.fib6_local_tbl)
1740 goto out_fib6_main_tbl;
1741 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001742 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001743 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1744 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001745 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001746#endif
1747 fib6_tables_init(net);
1748
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001749 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001750
1751#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1752out_fib6_main_tbl:
1753 kfree(net->ipv6.fib6_main_tbl);
1754#endif
1755out_fib_table_hash:
1756 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001757out_rt6_stats:
1758 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001759out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001760 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001761}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001762
1763static void fib6_net_exit(struct net *net)
1764{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001765 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001766 del_timer_sync(&net->ipv6.ip6_fib_timer);
1767
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001768#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001769 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001770 kfree(net->ipv6.fib6_local_tbl);
1771#endif
David S. Miller8e773272012-06-11 00:01:52 -07001772 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001773 kfree(net->ipv6.fib6_main_tbl);
1774 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001775 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001776}
1777
1778static struct pernet_operations fib6_net_ops = {
1779 .init = fib6_net_init,
1780 .exit = fib6_net_exit,
1781};
1782
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001783int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001785 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1788 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001789 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001790 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001791 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001792 goto out;
1793
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001794 ret = register_pernet_subsys(&fib6_net_ops);
1795 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001796 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001797
1798 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1799 NULL);
1800 if (ret)
1801 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001802
1803 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001804out:
1805 return ret;
1806
David S. Millere8803b62012-06-16 01:12:19 -07001807out_unregister_subsys:
1808 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001809out_kmem_cache_create:
1810 kmem_cache_destroy(fib6_node_kmem);
1811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814void fib6_gc_cleanup(void)
1815{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001816 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 kmem_cache_destroy(fib6_node_kmem);
1818}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001819
1820#ifdef CONFIG_PROC_FS
1821
1822struct ipv6_route_iter {
1823 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001824 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001825 loff_t skip;
1826 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001827 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001828};
1829
1830static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1831{
1832 struct rt6_info *rt = v;
1833 struct ipv6_route_iter *iter = seq->private;
1834
1835 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1836
1837#ifdef CONFIG_IPV6_SUBTREES
1838 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1839#else
1840 seq_puts(seq, "00000000000000000000000000000000 00 ");
1841#endif
1842 if (rt->rt6i_flags & RTF_GATEWAY)
1843 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1844 else
1845 seq_puts(seq, "00000000000000000000000000000000");
1846
1847 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1848 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1849 rt->dst.__use, rt->rt6i_flags,
1850 rt->dst.dev ? rt->dst.dev->name : "");
1851 iter->w.leaf = NULL;
1852 return 0;
1853}
1854
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001855static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001856{
1857 struct ipv6_route_iter *iter = w->args;
1858
1859 if (!iter->skip)
1860 return 1;
1861
1862 do {
1863 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1864 iter->skip--;
1865 if (!iter->skip && iter->w.leaf)
1866 return 1;
1867 } while (iter->w.leaf);
1868
1869 return 0;
1870}
1871
1872static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1873{
1874 memset(&iter->w, 0, sizeof(iter->w));
1875 iter->w.func = ipv6_route_yield;
1876 iter->w.root = &iter->tbl->tb6_root;
1877 iter->w.state = FWS_INIT;
1878 iter->w.node = iter->w.root;
1879 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001880 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001881 INIT_LIST_HEAD(&iter->w.lh);
1882 fib6_walker_link(&iter->w);
1883}
1884
1885static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1886 struct net *net)
1887{
1888 unsigned int h;
1889 struct hlist_node *node;
1890
1891 if (tbl) {
1892 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1893 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1894 } else {
1895 h = 0;
1896 node = NULL;
1897 }
1898
1899 while (!node && h < FIB6_TABLE_HASHSZ) {
1900 node = rcu_dereference_bh(
1901 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1902 }
1903 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1904}
1905
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001906static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1907{
1908 if (iter->sernum != iter->w.root->fn_sernum) {
1909 iter->sernum = iter->w.root->fn_sernum;
1910 iter->w.state = FWS_INIT;
1911 iter->w.node = iter->w.root;
1912 WARN_ON(iter->w.skip);
1913 iter->w.skip = iter->w.count;
1914 }
1915}
1916
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001917static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1918{
1919 int r;
1920 struct rt6_info *n;
1921 struct net *net = seq_file_net(seq);
1922 struct ipv6_route_iter *iter = seq->private;
1923
1924 if (!v)
1925 goto iter_table;
1926
1927 n = ((struct rt6_info *)v)->dst.rt6_next;
1928 if (n) {
1929 ++*pos;
1930 return n;
1931 }
1932
1933iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001934 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001935 read_lock(&iter->tbl->tb6_lock);
1936 r = fib6_walk_continue(&iter->w);
1937 read_unlock(&iter->tbl->tb6_lock);
1938 if (r > 0) {
1939 if (v)
1940 ++*pos;
1941 return iter->w.leaf;
1942 } else if (r < 0) {
1943 fib6_walker_unlink(&iter->w);
1944 return NULL;
1945 }
1946 fib6_walker_unlink(&iter->w);
1947
1948 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1949 if (!iter->tbl)
1950 return NULL;
1951
1952 ipv6_route_seq_setup_walk(iter);
1953 goto iter_table;
1954}
1955
1956static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1957 __acquires(RCU_BH)
1958{
1959 struct net *net = seq_file_net(seq);
1960 struct ipv6_route_iter *iter = seq->private;
1961
1962 rcu_read_lock_bh();
1963 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1964 iter->skip = *pos;
1965
1966 if (iter->tbl) {
1967 ipv6_route_seq_setup_walk(iter);
1968 return ipv6_route_seq_next(seq, NULL, pos);
1969 } else {
1970 return NULL;
1971 }
1972}
1973
1974static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1975{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001976 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001977 return w->node && !(w->state == FWS_U && w->node == w->root);
1978}
1979
1980static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1981 __releases(RCU_BH)
1982{
1983 struct ipv6_route_iter *iter = seq->private;
1984
1985 if (ipv6_route_iter_active(iter))
1986 fib6_walker_unlink(&iter->w);
1987
1988 rcu_read_unlock_bh();
1989}
1990
1991static const struct seq_operations ipv6_route_seq_ops = {
1992 .start = ipv6_route_seq_start,
1993 .next = ipv6_route_seq_next,
1994 .stop = ipv6_route_seq_stop,
1995 .show = ipv6_route_seq_show
1996};
1997
1998int ipv6_route_open(struct inode *inode, struct file *file)
1999{
2000 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2001 sizeof(struct ipv6_route_iter));
2002}
2003
2004#endif /* CONFIG_PROC_FS */