blob: 6fdcf6c7a31eaeb729d806b3e5879dd19bd5e7de [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
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache * fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49enum fib_walk_state_t
50{
51#ifdef CONFIG_IPV6_SUBTREES
52 FWS_S,
53#endif
54 FWS_L,
55 FWS_R,
56 FWS_C,
57 FWS_U
58};
59
60struct fib6_cleaner_t
61{
62 struct fib6_walker_t w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080063 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int (*func)(struct rt6_info *, void *arg);
65 void *arg;
66};
67
Adrian Bunk90d41122006-08-14 23:49:16 -070068static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#ifdef CONFIG_IPV6_SUBTREES
71#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
73#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75
Benjamin Theryec7d43c2008-03-03 23:31:57 -080076static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
77 struct rt6_info *rt);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080078static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
79static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Adrian Bunk90d41122006-08-14 23:49:16 -070080static int fib6_walk(struct fib6_walker_t *w);
81static int fib6_walk_continue(struct fib6_walker_t *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * A routing update causes an increase of the serial number on the
85 * affected subtree. This allows for cached routes to be asynchronously
86 * tested when modifications are made to the destination cache as a
87 * result of redirects, path MTU changes, etc.
88 */
89
90static __u32 rt_sernum;
91
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080092static void fib6_gc_timer_cb(unsigned long arg);
93
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000094static LIST_HEAD(fib6_walkers);
95#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Adrian Bunk90d41122006-08-14 23:49:16 -070097static inline void fib6_walker_link(struct fib6_walker_t *w)
98{
99 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000100 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -0700101 write_unlock_bh(&fib6_walker_lock);
102}
103
104static inline void fib6_walker_unlink(struct fib6_walker_t *w)
105{
106 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000107 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -0700108 write_unlock_bh(&fib6_walker_lock);
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static __inline__ u32 fib6_new_sernum(void)
111{
112 u32 n = ++rt_sernum;
113 if ((__s32)n <= 0)
114 rt_sernum = n = 1;
115 return n;
116}
117
118/*
119 * Auxiliary address test functions for the radix tree.
120 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900121 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * 64bit processors)
123 */
124
125/*
126 * test bit
127 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000128#if defined(__LITTLE_ENDIAN)
129# define BITOP_BE32_SWIZZLE (0x1F & ~7)
130#else
131# define BITOP_BE32_SWIZZLE 0
132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000134static __inline__ __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000136 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000137 /*
138 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800139 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000140 * is optimized version of
141 * htonl(1 << ((~fn_bit)&0x1F))
142 * See include/asm-generic/bitops/le.h.
143 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700144 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
145 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static __inline__ struct fib6_node * node_alloc(void)
149{
150 struct fib6_node *fn;
151
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800152 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return fn;
155}
156
157static __inline__ void node_free(struct fib6_node * fn)
158{
159 kmem_cache_free(fib6_node_kmem, fn);
160}
161
162static __inline__ void rt6_release(struct rt6_info *rt)
163{
164 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700165 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800168static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700169{
170 unsigned int h;
171
Thomas Graf375216a2006-10-21 20:20:54 -0700172 /*
173 * Initialize table lock at a single place to give lockdep a key,
174 * tables aren't visible prior to being linked to the list.
175 */
176 rwlock_init(&tb->tb6_lock);
177
Neil Hormana33bc5c2009-07-30 18:52:15 -0700178 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700179
180 /*
181 * No protection necessary, this is the only list mutatation
182 * operation, tables never disappear once they exist.
183 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800184 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700185}
186
187#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800188
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800189static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700190{
191 struct fib6_table *table;
192
193 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500194 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700195 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800196 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700197 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700198 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700199 }
200
201 return table;
202}
203
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800204struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700205{
206 struct fib6_table *tb;
207
208 if (id == 0)
209 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800210 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700211 if (tb)
212 return tb;
213
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800214 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500215 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800216 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700217
218 return tb;
219}
220
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800221struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700222{
223 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800224 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700225 unsigned int h;
226
227 if (id == 0)
228 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700229 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700230 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800231 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800232 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700233 if (tb->tb6_id == id) {
234 rcu_read_unlock();
235 return tb;
236 }
237 }
238 rcu_read_unlock();
239
240 return NULL;
241}
242
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000243static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700244{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800245 fib6_link_table(net, net->ipv6.fib6_main_tbl);
246 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700247}
Thomas Grafc71099a2006-08-04 23:20:06 -0700248#else
249
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700251{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800252 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700253}
254
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800255struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700256{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800257 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700258}
259
David S. Miller4c9483b2011-03-12 16:22:43 -0500260struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800261 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700262{
David S. Miller4c9483b2011-03-12 16:22:43 -0500263 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700264}
265
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000266static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700267{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800268 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700269}
270
271#endif
272
Patrick McHardy1b43af52006-08-10 23:11:17 -0700273static int fib6_dump_node(struct fib6_walker_t *w)
274{
275 int res;
276 struct rt6_info *rt;
277
Changli Gaod8d1f302010-06-10 23:31:35 -0700278 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700279 res = rt6_dump_route(rt, w->args);
280 if (res < 0) {
281 /* Frame is full, suspend walking */
282 w->leaf = rt;
283 return 1;
284 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700285 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700286 }
287 w->leaf = NULL;
288 return 0;
289}
290
291static void fib6_dump_end(struct netlink_callback *cb)
292{
293 struct fib6_walker_t *w = (void*)cb->args[2];
294
295 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800296 if (cb->args[4]) {
297 cb->args[4] = 0;
298 fib6_walker_unlink(w);
299 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700300 cb->args[2] = 0;
301 kfree(w);
302 }
303 cb->done = (void*)cb->args[3];
304 cb->args[1] = 3;
305}
306
307static int fib6_dump_done(struct netlink_callback *cb)
308{
309 fib6_dump_end(cb);
310 return cb->done ? cb->done(cb) : 0;
311}
312
313static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
314 struct netlink_callback *cb)
315{
316 struct fib6_walker_t *w;
317 int res;
318
319 w = (void *)cb->args[2];
320 w->root = &table->tb6_root;
321
322 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000323 w->count = 0;
324 w->skip = 0;
325
Patrick McHardy1b43af52006-08-10 23:11:17 -0700326 read_lock_bh(&table->tb6_lock);
327 res = fib6_walk(w);
328 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000329 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700330 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000331 cb->args[5] = w->root->fn_sernum;
332 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700333 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000334 if (cb->args[5] != w->root->fn_sernum) {
335 /* Begin at the root if the tree changed */
336 cb->args[5] = w->root->fn_sernum;
337 w->state = FWS_INIT;
338 w->node = w->root;
339 w->skip = w->count;
340 } else
341 w->skip = 0;
342
Patrick McHardy1b43af52006-08-10 23:11:17 -0700343 read_lock_bh(&table->tb6_lock);
344 res = fib6_walk_continue(w);
345 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800346 if (res <= 0) {
347 fib6_walker_unlink(w);
348 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700349 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700350 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800351
Patrick McHardy1b43af52006-08-10 23:11:17 -0700352 return res;
353}
354
Thomas Grafc127ea22007-03-22 11:58:32 -0700355static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700356{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900357 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700358 unsigned int h, s_h;
359 unsigned int e = 0, s_e;
360 struct rt6_rtnl_dump_arg arg;
361 struct fib6_walker_t *w;
362 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800363 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700364 int res = 0;
365
366 s_h = cb->args[0];
367 s_e = cb->args[1];
368
369 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500370 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700371 /* New dump:
372 *
373 * 1. hook callback destructor.
374 */
375 cb->args[3] = (long)cb->done;
376 cb->done = fib6_dump_done;
377
378 /*
379 * 2. allocate and initialize walker.
380 */
381 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500382 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700383 return -ENOMEM;
384 w->func = fib6_dump_node;
385 cb->args[2] = (long)w;
386 }
387
388 arg.skb = skb;
389 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700390 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700391 w->args = &arg;
392
Eric Dumazete67f88d2011-04-27 22:56:07 +0000393 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700394 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700395 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800396 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800397 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700398 if (e < s_e)
399 goto next;
400 res = fib6_dump_table(tb, skb, cb);
401 if (res != 0)
402 goto out;
403next:
404 e++;
405 }
406 }
407out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000408 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700409 cb->args[1] = e;
410 cb->args[0] = h;
411
412 res = res < 0 ? res : skb->len;
413 if (res <= 0)
414 fib6_dump_end(cb);
415 return res;
416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418/*
419 * Routing Table
420 *
421 * return the appropriate node for a routing tree "add" operation
422 * by either creating and inserting or by returning an existing
423 * node.
424 */
425
fan.du9225b232013-07-22 14:21:09 +0800426static struct fib6_node *fib6_add_1(struct fib6_node *root,
427 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000428 int offset, int allow_create,
429 int replace_required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct fib6_node *fn, *in, *ln;
432 struct fib6_node *pn = NULL;
433 struct rt6key *key;
434 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900435 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 __u32 sernum = fib6_new_sernum();
437
438 RT6_TRACE("fib6_add_1\n");
439
440 /* insert node in tree */
441
442 fn = root;
443
444 do {
445 key = (struct rt6key *)((u8 *)fn->leaf + offset);
446
447 /*
448 * Prefix match
449 */
450 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000451 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000452 if (!allow_create) {
453 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000454 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000455 return ERR_PTR(-ENOENT);
456 }
Joe Perchesf3213832012-05-15 14:11:53 +0000457 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000460 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
463 * Exact match ?
464 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (plen == fn->fn_bit) {
467 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500468 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 rt6_release(fn->leaf);
470 fn->leaf = NULL;
471 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return fn;
476 }
477
478 /*
479 * We have more bits to go
480 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Try to walk down on tree. */
483 fn->fn_sernum = sernum;
484 dir = addr_bit_set(addr, fn->fn_bit);
485 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800486 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } while (fn);
488
Matti Vaittinen14df0152011-11-16 21:18:02 +0000489 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000490 /* We should not create new node because
491 * NLM_F_REPLACE was specified without NLM_F_CREATE
492 * I assume it is safe to require NLM_F_CREATE when
493 * REPLACE flag is used! Later we may want to remove the
494 * check for replace_required, because according
495 * to netlink specification, NLM_F_CREATE
496 * MUST be specified if new route is created.
497 * That would keep IPv6 consistent with IPv4
498 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000499 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000500 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000501 return ERR_PTR(-ENOENT);
502 }
Joe Perchesf3213832012-05-15 14:11:53 +0000503 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /*
506 * We walked to the bottom of tree.
507 * Create new leaf node without children.
508 */
509
510 ln = node_alloc();
511
David S. Miller507c9b12011-12-03 17:50:45 -0500512 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000513 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ln->parent = pn;
517 ln->fn_sernum = sernum;
518
519 if (dir)
520 pn->right = ln;
521 else
522 pn->left = ln;
523
524 return ln;
525
526
527insert_above:
528 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900529 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * we have a less significant route.
531 * we've to insert an intermediate node on the list
532 * this new node will point to the one we need to create
533 * and the current
534 */
535
536 pn = fn->parent;
537
538 /* find 1st bit in difference between the 2 addrs.
539
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800540 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 but if it is >= plen, the value is ignored in any case.
542 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900543
fan.du9225b232013-07-22 14:21:09 +0800544 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900546 /*
547 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * / \
549 * (new leaf node)[ln] (old node)[fn]
550 */
551 if (plen > bit) {
552 in = node_alloc();
553 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900554
David S. Miller507c9b12011-12-03 17:50:45 -0500555 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (in)
557 node_free(in);
558 if (ln)
559 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000560 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900563 /*
564 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * RTN_RTINFO will
566 * be off since that an address that chooses one of
567 * the branches would not match less specific routes
568 * in the other branch
569 */
570
571 in->fn_bit = bit;
572
573 in->parent = pn;
574 in->leaf = fn->leaf;
575 atomic_inc(&in->leaf->rt6i_ref);
576
577 in->fn_sernum = sernum;
578
579 /* update parent pointer */
580 if (dir)
581 pn->right = in;
582 else
583 pn->left = in;
584
585 ln->fn_bit = plen;
586
587 ln->parent = in;
588 fn->parent = in;
589
590 ln->fn_sernum = sernum;
591
592 if (addr_bit_set(addr, bit)) {
593 in->right = ln;
594 in->left = fn;
595 } else {
596 in->left = ln;
597 in->right = fn;
598 }
599 } else { /* plen <= bit */
600
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900601 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * (new leaf node)[ln]
603 * / \
604 * (old node)[fn] NULL
605 */
606
607 ln = node_alloc();
608
David S. Miller507c9b12011-12-03 17:50:45 -0500609 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000610 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 ln->fn_bit = plen;
613
614 ln->parent = pn;
615
616 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (dir)
619 pn->right = ln;
620 else
621 pn->left = ln;
622
623 if (addr_bit_set(&key->addr, plen))
624 ln->right = fn;
625 else
626 ln->left = fn;
627
628 fn->parent = ln;
629 }
630 return ln;
631}
632
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200633static inline bool rt6_qualify_for_ecmp(struct rt6_info *rt)
634{
635 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
636 RTF_GATEWAY;
637}
638
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100639static int fib6_commit_metrics(struct dst_entry *dst,
640 struct nlattr *mx, int mx_len)
641{
642 struct nlattr *nla;
643 int remaining;
644 u32 *mp;
645
646 if (dst->flags & DST_HOST) {
647 mp = dst_metrics_write_ptr(dst);
648 } else {
649 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
650 if (!mp)
651 return -ENOMEM;
652 dst_init_metrics(dst, mp, 0);
653 }
654
655 nla_for_each_attr(nla, mx, mx_len, remaining) {
656 int type = nla_type(nla);
657
658 if (type) {
659 if (type > RTAX_MAX)
660 return -EINVAL;
661
662 mp[type - 1] = nla_get_u32(nla);
663 }
664 }
665 return 0;
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
669 * Insert routing information in a node.
670 */
671
672static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100673 struct nl_info *info, struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct rt6_info *iter = NULL;
676 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500677 int replace = (info->nlh &&
678 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
679 int add = (!info->nlh ||
680 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000681 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200682 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 ins = &fn->leaf;
686
David S. Miller507c9b12011-12-03 17:50:45 -0500687 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * Search for duplicates
690 */
691
692 if (iter->rt6i_metric == rt->rt6i_metric) {
693 /*
694 * Same priority level
695 */
David S. Miller507c9b12011-12-03 17:50:45 -0500696 if (info->nlh &&
697 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000698 return -EEXIST;
699 if (replace) {
700 found++;
701 break;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David S. Millerd1918542011-12-28 20:19:20 -0500704 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 iter->rt6i_idev == rt->rt6i_idev &&
706 ipv6_addr_equal(&iter->rt6i_gateway,
707 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000708 if (rt->rt6i_nsiblings)
709 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500710 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000712 if (!(rt->rt6i_flags & RTF_EXPIRES))
713 rt6_clean_expires(iter);
714 else
715 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -EEXIST;
717 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000718 /* If we have the same destination and the same metric,
719 * but not the same gateway, then the route we try to
720 * add is sibling to this route, increment our counter
721 * of siblings, and later we will add our route to the
722 * list.
723 * Only static routes (which don't have flag
724 * RTF_EXPIRES) are used for ECMPv6.
725 *
726 * To avoid long list, we only had siblings if the
727 * route have a gateway.
728 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200729 if (rt_can_ecmp &&
730 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000731 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
734 if (iter->rt6i_metric > rt->rt6i_metric)
735 break;
736
Changli Gaod8d1f302010-06-10 23:31:35 -0700737 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
David S. Millerf11e6652007-03-24 20:36:25 -0700740 /* Reset round-robin state, if necessary */
741 if (ins == &fn->leaf)
742 fn->rr_ptr = NULL;
743
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000744 /* Link this route to others same route. */
745 if (rt->rt6i_nsiblings) {
746 unsigned int rt6i_nsiblings;
747 struct rt6_info *sibling, *temp_sibling;
748
749 /* Find the first route that have the same metric */
750 sibling = fn->leaf;
751 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200752 if (sibling->rt6i_metric == rt->rt6i_metric &&
753 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000754 list_add_tail(&rt->rt6i_siblings,
755 &sibling->rt6i_siblings);
756 break;
757 }
758 sibling = sibling->dst.rt6_next;
759 }
760 /* For each sibling in the list, increment the counter of
761 * siblings. BUG() if counters does not match, list of siblings
762 * is broken!
763 */
764 rt6i_nsiblings = 0;
765 list_for_each_entry_safe(sibling, temp_sibling,
766 &rt->rt6i_siblings, rt6i_siblings) {
767 sibling->rt6i_nsiblings++;
768 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
769 rt6i_nsiblings++;
770 }
771 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
772 }
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /*
775 * insert node
776 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000777 if (!replace) {
778 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000779 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000781add:
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100782 if (mx) {
783 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
784 if (err)
785 return err;
786 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000787 rt->dst.rt6_next = iter;
788 *ins = rt;
789 rt->rt6i_node = fn;
790 atomic_inc(&rt->rt6i_ref);
791 inet6_rt_notify(RTM_NEWROUTE, rt, info);
792 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
David S. Miller507c9b12011-12-03 17:50:45 -0500794 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000795 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
796 fn->fn_flags |= RTN_RTINFO;
797 }
798
799 } else {
800 if (!found) {
801 if (add)
802 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000803 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000804 return -ENOENT;
805 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100806 if (mx) {
807 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
808 if (err)
809 return err;
810 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000811 *ins = rt;
812 rt->rt6i_node = fn;
813 rt->dst.rt6_next = iter->dst.rt6_next;
814 atomic_inc(&rt->rt6i_ref);
815 inet6_rt_notify(RTM_NEWROUTE, rt, info);
816 rt6_release(iter);
David S. Miller507c9b12011-12-03 17:50:45 -0500817 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000818 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
819 fn->fn_flags |= RTN_RTINFO;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 return 0;
824}
825
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800826static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700828 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500829 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700830 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700831 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800834void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700836 if (!timer_pending(&net->ipv6.ip6_fib_timer))
837 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700838 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/*
842 * Add routing information to the routing tree.
843 * <destination addr>/<source addr>
844 * with source addr info in sub-trees
845 */
846
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100847int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
848 struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700850 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000852 int allow_create = 1;
853 int replace_required = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500854
855 if (info->nlh) {
856 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000857 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500858 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000859 replace_required = 1;
860 }
861 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000862 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
fan.du9225b232013-07-22 14:21:09 +0800864 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
865 offsetof(struct rt6_info, rt6i_dst), allow_create,
866 replace_required);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000867 if (IS_ERR(fn)) {
868 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200869 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700873 pn = fn;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875#ifdef CONFIG_IPV6_SUBTREES
876 if (rt->rt6i_src.plen) {
877 struct fib6_node *sn;
878
David S. Miller507c9b12011-12-03 17:50:45 -0500879 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct fib6_node *sfn;
881
882 /*
883 * Create subtree.
884 *
885 * fn[main tree]
886 * |
887 * sfn[subtree root]
888 * \
889 * sn[new leaf node]
890 */
891
892 /* Create subtree root node */
893 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500894 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto st_failure;
896
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800897 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
898 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sfn->fn_flags = RTN_ROOT;
900 sfn->fn_sernum = fib6_new_sernum();
901
902 /* Now add the first leaf node to new subtree */
903
904 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800905 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000906 offsetof(struct rt6_info, rt6i_src),
907 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000909 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* If it is failed, discard just allocated
911 root, and then (in st_failure) stale node
912 in main tree.
913 */
914 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000915 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 goto st_failure;
917 }
918
919 /* Now link new subtree to main tree */
920 sfn->parent = fn;
921 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 } else {
923 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800924 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000925 offsetof(struct rt6_info, rt6i_src),
926 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000928 if (IS_ERR(sn)) {
929 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
David S. Miller507c9b12011-12-03 17:50:45 -0500934 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700935 fn->leaf = rt;
936 atomic_inc(&rt->rt6i_ref);
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 fn = sn;
939 }
940#endif
941
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100942 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
David S. Miller507c9b12011-12-03 17:50:45 -0500943 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800944 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500945 if (!(rt->rt6i_flags & RTF_CACHE))
Benjamin Theryec7d43c2008-03-03 23:31:57 -0800946 fib6_prune_clones(info->nl_net, pn, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700950 if (err) {
951#ifdef CONFIG_IPV6_SUBTREES
952 /*
953 * If fib6_add_1 has cleared the old leaf pointer in the
954 * super-tree leaf node we have to find a new one for it.
955 */
David S. Miller3c051232008-04-18 01:46:19 -0700956 if (pn != fn && pn->leaf == rt) {
957 pn->leaf = NULL;
958 atomic_dec(&rt->rt6i_ref);
959 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700960 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800961 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700962#if RT6_DEBUG >= 2
963 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700964 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800965 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700966 }
967#endif
968 atomic_inc(&pn->leaf->rt6i_ref);
969 }
970#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700971 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return err;
974
975#ifdef CONFIG_IPV6_SUBTREES
976 /* Subtree creation failed, probably main tree node
977 is orphan. If it is, shoot it.
978 */
979st_failure:
980 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800981 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700982 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return err;
984#endif
985}
986
987/*
988 * Routing tree lookup
989 *
990 */
991
992struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -0500993 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000994 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995};
996
997static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
998 struct lookup_args *args)
999{
1000 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001001 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001003 if (unlikely(args->offset == 0))
1004 return NULL;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
1007 * Descend on a tree
1008 */
1009
1010 fn = root;
1011
1012 for (;;) {
1013 struct fib6_node *next;
1014
1015 dir = addr_bit_set(args->addr, fn->fn_bit);
1016
1017 next = dir ? fn->right : fn->left;
1018
1019 if (next) {
1020 fn = next;
1021 continue;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024 }
1025
David S. Miller507c9b12011-12-03 17:50:45 -05001026 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001027 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct rt6key *key;
1029
1030 key = (struct rt6key *) ((u8 *) fn->leaf +
1031 args->offset);
1032
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001033 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1034#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001035 if (fn->subtree) {
1036 struct fib6_node *sfn;
1037 sfn = fib6_lookup_1(fn->subtree,
1038 args + 1);
1039 if (!sfn)
1040 goto backtrack;
1041 fn = sfn;
1042 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001043#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001044 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001045 return fn;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001048#ifdef CONFIG_IPV6_SUBTREES
1049backtrack:
1050#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001051 if (fn->fn_flags & RTN_ROOT)
1052 break;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 fn = fn->parent;
1055 }
1056
1057 return NULL;
1058}
1059
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001060struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1061 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001064 struct lookup_args args[] = {
1065 {
1066 .offset = offsetof(struct rt6_info, rt6i_dst),
1067 .addr = daddr,
1068 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001070 {
1071 .offset = offsetof(struct rt6_info, rt6i_src),
1072 .addr = saddr,
1073 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001075 {
1076 .offset = 0, /* sentinel */
1077 }
1078 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001080 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001081 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 fn = root;
1083
1084 return fn;
1085}
1086
1087/*
1088 * Get node with specified destination prefix (and source prefix,
1089 * if subtrees are used)
1090 */
1091
1092
1093static struct fib6_node * fib6_locate_1(struct fib6_node *root,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001094 const struct in6_addr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int plen, int offset)
1096{
1097 struct fib6_node *fn;
1098
1099 for (fn = root; fn ; ) {
1100 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1101
1102 /*
1103 * Prefix match
1104 */
1105 if (plen < fn->fn_bit ||
1106 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1107 return NULL;
1108
1109 if (plen == fn->fn_bit)
1110 return fn;
1111
1112 /*
1113 * We have more bits to go
1114 */
1115 if (addr_bit_set(addr, fn->fn_bit))
1116 fn = fn->right;
1117 else
1118 fn = fn->left;
1119 }
1120 return NULL;
1121}
1122
1123struct fib6_node * fib6_locate(struct fib6_node *root,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001124 const struct in6_addr *daddr, int dst_len,
1125 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct fib6_node *fn;
1128
1129 fn = fib6_locate_1(root, daddr, dst_len,
1130 offsetof(struct rt6_info, rt6i_dst));
1131
1132#ifdef CONFIG_IPV6_SUBTREES
1133 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001134 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001135 if (fn && fn->subtree)
1136 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 offsetof(struct rt6_info, rt6i_src));
1138 }
1139#endif
1140
David S. Miller507c9b12011-12-03 17:50:45 -05001141 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return fn;
1143
1144 return NULL;
1145}
1146
1147
1148/*
1149 * Deletion
1150 *
1151 */
1152
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001153static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
David S. Miller507c9b12011-12-03 17:50:45 -05001155 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001156 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
David S. Miller507c9b12011-12-03 17:50:45 -05001158 while (fn) {
1159 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001161 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return fn->right->leaf;
1163
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001164 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 return NULL;
1167}
1168
1169/*
1170 * Called to trim the tree of intermediate nodes when possible. "fn"
1171 * is the node we want to try and remove.
1172 */
1173
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001174static struct fib6_node *fib6_repair_tree(struct net *net,
1175 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 int children;
1178 int nstate;
1179 struct fib6_node *child, *pn;
1180 struct fib6_walker_t *w;
1181 int iter = 0;
1182
1183 for (;;) {
1184 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1185 iter++;
1186
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001187 WARN_ON(fn->fn_flags & RTN_RTINFO);
1188 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1189 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 children = 0;
1192 child = NULL;
1193 if (fn->right) child = fn->right, children |= 1;
1194 if (fn->left) child = fn->left, children |= 2;
1195
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001196 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197#ifdef CONFIG_IPV6_SUBTREES
1198 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001199 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif
1201 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001202 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001204 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001205 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001206 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208#endif
1209 atomic_inc(&fn->leaf->rt6i_ref);
1210 return fn->parent;
1211 }
1212
1213 pn = fn->parent;
1214#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001215 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001216 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001217 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 nstate = FWS_L;
1219 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001220 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221#endif
1222 if (pn->right == fn) pn->right = child;
1223 else if (pn->left == fn) pn->left = child;
1224#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001225 else
1226 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227#endif
1228 if (child)
1229 child->parent = pn;
1230 nstate = FWS_R;
1231#ifdef CONFIG_IPV6_SUBTREES
1232 }
1233#endif
1234
1235 read_lock(&fib6_walker_lock);
1236 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001237 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (w->root == fn) {
1239 w->root = w->node = NULL;
1240 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1241 } else if (w->node == fn) {
1242 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1243 w->node = pn;
1244 w->state = nstate;
1245 }
1246 } else {
1247 if (w->root == fn) {
1248 w->root = child;
1249 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1250 }
1251 if (w->node == fn) {
1252 w->node = child;
1253 if (children&2) {
1254 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001255 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 } else {
1257 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001258 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 }
1261 }
1262 }
1263 read_unlock(&fib6_walker_lock);
1264
1265 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001266 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return pn;
1268
1269 rt6_release(pn->leaf);
1270 pn->leaf = NULL;
1271 fn = pn;
1272 }
1273}
1274
1275static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001276 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct fib6_walker_t *w;
1279 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001280 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 RT6_TRACE("fib6_del_route\n");
1283
1284 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001285 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001287 net->ipv6.rt6_stats->fib_rt_entries--;
1288 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David S. Millerf11e6652007-03-24 20:36:25 -07001290 /* Reset round-robin state, if necessary */
1291 if (fn->rr_ptr == rt)
1292 fn->rr_ptr = NULL;
1293
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001294 /* Remove this entry from other siblings */
1295 if (rt->rt6i_nsiblings) {
1296 struct rt6_info *sibling, *next_sibling;
1297
1298 list_for_each_entry_safe(sibling, next_sibling,
1299 &rt->rt6i_siblings, rt6i_siblings)
1300 sibling->rt6i_nsiblings--;
1301 rt->rt6i_nsiblings = 0;
1302 list_del_init(&rt->rt6i_siblings);
1303 }
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* Adjust walkers */
1306 read_lock(&fib6_walker_lock);
1307 FOR_WALKERS(w) {
1308 if (w->state == FWS_C && w->leaf == rt) {
1309 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001310 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001311 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 w->state = FWS_U;
1313 }
1314 }
1315 read_unlock(&fib6_walker_lock);
1316
Changli Gaod8d1f302010-06-10 23:31:35 -07001317 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001320 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001322 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001323 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
1326 if (atomic_read(&rt->rt6i_ref) != 1) {
1327 /* This route is used as dummy address holder in some split
1328 * nodes. It is not leaked, but it still holds other resources,
1329 * which must be released in time. So, scan ascendant nodes
1330 * and replace dummy references to this route with references
1331 * to still alive ones.
1332 */
1333 while (fn) {
David S. Miller507c9b12011-12-03 17:50:45 -05001334 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001335 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 atomic_inc(&fn->leaf->rt6i_ref);
1337 rt6_release(rt);
1338 }
1339 fn = fn->parent;
1340 }
1341 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001342 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Thomas Graf86872cb2006-08-22 00:01:08 -07001345 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 rt6_release(rt);
1347}
1348
Thomas Graf86872cb2006-08-22 00:01:08 -07001349int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001351 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct fib6_node *fn = rt->rt6i_node;
1353 struct rt6_info **rtp;
1354
1355#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001356 if (rt->dst.obsolete > 0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001357 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -ENOENT;
1359 }
1360#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001361 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -ENOENT;
1363
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001364 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
David S. Miller507c9b12011-12-03 17:50:45 -05001366 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001367 struct fib6_node *pn = fn;
1368#ifdef CONFIG_IPV6_SUBTREES
1369 /* clones of this route might be in another subtree */
1370 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001371 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001372 pn = pn->parent;
1373 pn = pn->parent;
1374 }
1375#endif
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001376 fib6_prune_clones(info->nl_net, pn, rt);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /*
1380 * Walk the leaf entries looking for ourself
1381 */
1382
Changli Gaod8d1f302010-06-10 23:31:35 -07001383 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001385 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return 0;
1387 }
1388 }
1389 return -ENOENT;
1390}
1391
1392/*
1393 * Tree traversal function.
1394 *
1395 * Certainly, it is not interrupt safe.
1396 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1397 * It means, that we can modify tree during walking
1398 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001399 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
1401 * It guarantees that every node will be traversed,
1402 * and that it will be traversed only once.
1403 *
1404 * Callback function w->func may return:
1405 * 0 -> continue walking.
1406 * positive value -> walking is suspended (used by tree dumps,
1407 * and probably by gc, if it will be split to several slices)
1408 * negative value -> terminate walking.
1409 *
1410 * The function itself returns:
1411 * 0 -> walk is complete.
1412 * >0 -> walk is incomplete (i.e. suspended)
1413 * <0 -> walk is terminated by an error.
1414 */
1415
Adrian Bunk90d41122006-08-14 23:49:16 -07001416static int fib6_walk_continue(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct fib6_node *fn, *pn;
1419
1420 for (;;) {
1421 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001422 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return 0;
1424
1425 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001426 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 w->state = FWS_C;
1428 w->leaf = fn->leaf;
1429 }
1430 switch (w->state) {
1431#ifdef CONFIG_IPV6_SUBTREES
1432 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001433 if (FIB6_SUBTREE(fn)) {
1434 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 continue;
1436 }
1437 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001438#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 case FWS_L:
1440 if (fn->left) {
1441 w->node = fn->left;
1442 w->state = FWS_INIT;
1443 continue;
1444 }
1445 w->state = FWS_R;
1446 case FWS_R:
1447 if (fn->right) {
1448 w->node = fn->right;
1449 w->state = FWS_INIT;
1450 continue;
1451 }
1452 w->state = FWS_C;
1453 w->leaf = fn->leaf;
1454 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001455 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001456 int err;
1457
Eric Dumazetfa809e22012-06-25 15:37:19 -07001458 if (w->skip) {
1459 w->skip--;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001460 continue;
1461 }
1462
1463 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (err)
1465 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001466
1467 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 continue;
1469 }
1470 w->state = FWS_U;
1471 case FWS_U:
1472 if (fn == w->root)
1473 return 0;
1474 pn = fn->parent;
1475 w->node = pn;
1476#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001477 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001478 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 w->state = FWS_L;
1480 continue;
1481 }
1482#endif
1483 if (pn->left == fn) {
1484 w->state = FWS_R;
1485 continue;
1486 }
1487 if (pn->right == fn) {
1488 w->state = FWS_C;
1489 w->leaf = w->node->leaf;
1490 continue;
1491 }
1492#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001493 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494#endif
1495 }
1496 }
1497}
1498
Adrian Bunk90d41122006-08-14 23:49:16 -07001499static int fib6_walk(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 int res;
1502
1503 w->state = FWS_INIT;
1504 w->node = w->root;
1505
1506 fib6_walker_link(w);
1507 res = fib6_walk_continue(w);
1508 if (res <= 0)
1509 fib6_walker_unlink(w);
1510 return res;
1511}
1512
1513static int fib6_clean_node(struct fib6_walker_t *w)
1514{
1515 int res;
1516 struct rt6_info *rt;
Benjamin Thery0a8891a2007-10-08 20:39:36 -07001517 struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001518 struct nl_info info = {
1519 .nl_net = c->net,
1520 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Changli Gaod8d1f302010-06-10 23:31:35 -07001522 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 res = c->func(rt, c->arg);
1524 if (res < 0) {
1525 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001526 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (res) {
1528#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001529 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1530 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531#endif
1532 continue;
1533 }
1534 return 0;
1535 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001536 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 w->leaf = rt;
1539 return 0;
1540}
1541
1542/*
1543 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001544 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * func is called on each route.
1546 * It may return -1 -> delete this route.
1547 * 0 -> continue walking
1548 *
1549 * prune==1 -> only immediate children of node (certainly,
1550 * ignoring pure split nodes) will be scanned.
1551 */
1552
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001553static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001554 int (*func)(struct rt6_info *, void *arg),
1555 int prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct fib6_cleaner_t c;
1558
1559 c.w.root = root;
1560 c.w.func = fib6_clean_node;
1561 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001562 c.w.count = 0;
1563 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 c.func = func;
1565 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001566 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 fib6_walk(&c.w);
1569}
1570
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001571void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Li RongQing0c3584d2013-12-27 16:32:38 +08001572 void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001573{
Thomas Grafc71099a2006-08-04 23:20:06 -07001574 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001575 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001576 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001577
Patrick McHardy1b43af52006-08-10 23:11:17 -07001578 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001579 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001580 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001581 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001582 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001583 fib6_clean_tree(net, &table->tb6_root,
Li RongQing0c3584d2013-12-27 16:32:38 +08001584 func, 0, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001585 write_unlock_bh(&table->tb6_lock);
1586 }
1587 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001588 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001589}
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1592{
1593 if (rt->rt6i_flags & RTF_CACHE) {
1594 RT6_TRACE("pruning clone %p\n", rt);
1595 return -1;
1596 }
1597
1598 return 0;
1599}
1600
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001601static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
1602 struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001604 fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
1607/*
1608 * Garbage collection
1609 */
1610
1611static struct fib6_gc_args
1612{
1613 int timeout;
1614 int more;
1615} gc_args;
1616
1617static int fib6_age(struct rt6_info *rt, void *arg)
1618{
1619 unsigned long now = jiffies;
1620
1621 /*
1622 * check addrconf expiration here.
1623 * Routes are expired even if they are in use.
1624 *
1625 * Also age clones. Note, that clones are aged out
1626 * only if they are not in use now.
1627 */
1628
David S. Millerd1918542011-12-28 20:19:20 -05001629 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1630 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return -1;
1633 }
1634 gc_args.more++;
1635 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001636 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1637 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 RT6_TRACE("aging clone %p\n", rt);
1639 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001640 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1641 struct neighbour *neigh;
1642 __u8 neigh_flags = 0;
1643
1644 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1645 if (neigh) {
1646 neigh_flags = neigh->flags;
1647 neigh_release(neigh);
1648 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001649 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001650 RT6_TRACE("purging route %p via non-router but gateway\n",
1651 rt);
1652 return -1;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655 gc_args.more++;
1656 }
1657
1658 return 0;
1659}
1660
1661static DEFINE_SPINLOCK(fib6_gc_lock);
1662
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001663void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001665 unsigned long now;
1666
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001667 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001669 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1670 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001673 gc_args.timeout = expires ? (int)expires :
1674 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001676 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001677
Li RongQing0c3584d2013-12-27 16:32:38 +08001678 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001679 now = jiffies;
1680 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001683 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001684 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001685 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001686 else
1687 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 spin_unlock_bh(&fib6_gc_lock);
1689}
1690
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001691static void fib6_gc_timer_cb(unsigned long arg)
1692{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001693 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001694}
1695
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001696static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001697{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001698 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1699
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001700 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001701
Benjamin Theryc5728722008-03-03 23:34:17 -08001702 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1703 if (!net->ipv6.rt6_stats)
1704 goto out_timer;
1705
Eric Dumazet10da66f2010-10-13 08:22:03 +00001706 /* Avoid false sharing : Use at least a full cache line */
1707 size = max_t(size_t, size, L1_CACHE_BYTES);
1708
1709 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001710 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001711 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001712
1713 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1714 GFP_KERNEL);
1715 if (!net->ipv6.fib6_main_tbl)
1716 goto out_fib_table_hash;
1717
1718 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001719 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001720 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1721 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001722 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001723
1724#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1725 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1726 GFP_KERNEL);
1727 if (!net->ipv6.fib6_local_tbl)
1728 goto out_fib6_main_tbl;
1729 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001730 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001731 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1732 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001733 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001734#endif
1735 fib6_tables_init(net);
1736
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001737 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001738
1739#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1740out_fib6_main_tbl:
1741 kfree(net->ipv6.fib6_main_tbl);
1742#endif
1743out_fib_table_hash:
1744 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001745out_rt6_stats:
1746 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001747out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001748 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001749}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001750
1751static void fib6_net_exit(struct net *net)
1752{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001753 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001754 del_timer_sync(&net->ipv6.ip6_fib_timer);
1755
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001756#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001757 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001758 kfree(net->ipv6.fib6_local_tbl);
1759#endif
David S. Miller8e773272012-06-11 00:01:52 -07001760 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001761 kfree(net->ipv6.fib6_main_tbl);
1762 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001763 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001764}
1765
1766static struct pernet_operations fib6_net_ops = {
1767 .init = fib6_net_init,
1768 .exit = fib6_net_exit,
1769};
1770
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001771int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001773 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1776 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001777 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001778 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001779 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001780 goto out;
1781
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001782 ret = register_pernet_subsys(&fib6_net_ops);
1783 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001784 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001785
1786 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1787 NULL);
1788 if (ret)
1789 goto out_unregister_subsys;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001790out:
1791 return ret;
1792
David S. Millere8803b62012-06-16 01:12:19 -07001793out_unregister_subsys:
1794 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001795out_kmem_cache_create:
1796 kmem_cache_destroy(fib6_node_kmem);
1797 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800void fib6_gc_cleanup(void)
1801{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001802 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 kmem_cache_destroy(fib6_node_kmem);
1804}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001805
1806#ifdef CONFIG_PROC_FS
1807
1808struct ipv6_route_iter {
1809 struct seq_net_private p;
1810 struct fib6_walker_t w;
1811 loff_t skip;
1812 struct fib6_table *tbl;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001813 __u32 sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001814};
1815
1816static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1817{
1818 struct rt6_info *rt = v;
1819 struct ipv6_route_iter *iter = seq->private;
1820
1821 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1822
1823#ifdef CONFIG_IPV6_SUBTREES
1824 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1825#else
1826 seq_puts(seq, "00000000000000000000000000000000 00 ");
1827#endif
1828 if (rt->rt6i_flags & RTF_GATEWAY)
1829 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1830 else
1831 seq_puts(seq, "00000000000000000000000000000000");
1832
1833 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1834 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1835 rt->dst.__use, rt->rt6i_flags,
1836 rt->dst.dev ? rt->dst.dev->name : "");
1837 iter->w.leaf = NULL;
1838 return 0;
1839}
1840
1841static int ipv6_route_yield(struct fib6_walker_t *w)
1842{
1843 struct ipv6_route_iter *iter = w->args;
1844
1845 if (!iter->skip)
1846 return 1;
1847
1848 do {
1849 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1850 iter->skip--;
1851 if (!iter->skip && iter->w.leaf)
1852 return 1;
1853 } while (iter->w.leaf);
1854
1855 return 0;
1856}
1857
1858static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1859{
1860 memset(&iter->w, 0, sizeof(iter->w));
1861 iter->w.func = ipv6_route_yield;
1862 iter->w.root = &iter->tbl->tb6_root;
1863 iter->w.state = FWS_INIT;
1864 iter->w.node = iter->w.root;
1865 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001866 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001867 INIT_LIST_HEAD(&iter->w.lh);
1868 fib6_walker_link(&iter->w);
1869}
1870
1871static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1872 struct net *net)
1873{
1874 unsigned int h;
1875 struct hlist_node *node;
1876
1877 if (tbl) {
1878 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1879 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1880 } else {
1881 h = 0;
1882 node = NULL;
1883 }
1884
1885 while (!node && h < FIB6_TABLE_HASHSZ) {
1886 node = rcu_dereference_bh(
1887 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1888 }
1889 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1890}
1891
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001892static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1893{
1894 if (iter->sernum != iter->w.root->fn_sernum) {
1895 iter->sernum = iter->w.root->fn_sernum;
1896 iter->w.state = FWS_INIT;
1897 iter->w.node = iter->w.root;
1898 WARN_ON(iter->w.skip);
1899 iter->w.skip = iter->w.count;
1900 }
1901}
1902
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001903static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1904{
1905 int r;
1906 struct rt6_info *n;
1907 struct net *net = seq_file_net(seq);
1908 struct ipv6_route_iter *iter = seq->private;
1909
1910 if (!v)
1911 goto iter_table;
1912
1913 n = ((struct rt6_info *)v)->dst.rt6_next;
1914 if (n) {
1915 ++*pos;
1916 return n;
1917 }
1918
1919iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001920 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001921 read_lock(&iter->tbl->tb6_lock);
1922 r = fib6_walk_continue(&iter->w);
1923 read_unlock(&iter->tbl->tb6_lock);
1924 if (r > 0) {
1925 if (v)
1926 ++*pos;
1927 return iter->w.leaf;
1928 } else if (r < 0) {
1929 fib6_walker_unlink(&iter->w);
1930 return NULL;
1931 }
1932 fib6_walker_unlink(&iter->w);
1933
1934 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1935 if (!iter->tbl)
1936 return NULL;
1937
1938 ipv6_route_seq_setup_walk(iter);
1939 goto iter_table;
1940}
1941
1942static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1943 __acquires(RCU_BH)
1944{
1945 struct net *net = seq_file_net(seq);
1946 struct ipv6_route_iter *iter = seq->private;
1947
1948 rcu_read_lock_bh();
1949 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1950 iter->skip = *pos;
1951
1952 if (iter->tbl) {
1953 ipv6_route_seq_setup_walk(iter);
1954 return ipv6_route_seq_next(seq, NULL, pos);
1955 } else {
1956 return NULL;
1957 }
1958}
1959
1960static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1961{
1962 struct fib6_walker_t *w = &iter->w;
1963 return w->node && !(w->state == FWS_U && w->node == w->root);
1964}
1965
1966static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1967 __releases(RCU_BH)
1968{
1969 struct ipv6_route_iter *iter = seq->private;
1970
1971 if (ipv6_route_iter_active(iter))
1972 fib6_walker_unlink(&iter->w);
1973
1974 rcu_read_unlock_bh();
1975}
1976
1977static const struct seq_operations ipv6_route_seq_ops = {
1978 .start = ipv6_route_seq_start,
1979 .next = ipv6_route_seq_next,
1980 .stop = ipv6_route_seq_stop,
1981 .show = ipv6_route_seq_show
1982};
1983
1984int ipv6_route_open(struct inode *inode, struct file *file)
1985{
1986 return seq_open_net(inode, file, &ipv6_route_seq_ops,
1987 sizeof(struct ipv6_route_iter));
1988}
1989
1990#endif /* CONFIG_PROC_FS */