blob: b2d1838897c933f6f1ceb2a6f64a2f67495ce3c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
35
36#include <net/ip6_fib.h>
37#include <net/ip6_route.h>
38
39#define RT6_DEBUG 2
40
41#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000042#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define RT6_TRACE(x...) do { ; } while (0)
45#endif
46
Wang Yufen437de072014-03-28 12:07:04 +080047static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020049struct fib6_cleaner {
50 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080051 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020053 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 void *arg;
55};
56
Adrian Bunk90d41122006-08-14 23:49:16 -070057static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#ifdef CONFIG_IPV6_SUBTREES
60#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
62#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Duan Jiong163cd4e2014-05-09 13:31:43 +080065static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080066static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
67static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020068static int fib6_walk(struct fib6_walker *w);
69static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
76 */
77
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080078static void fib6_gc_timer_cb(unsigned long arg);
79
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000080static LIST_HEAD(fib6_walkers);
81#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020083static void fib6_walker_link(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070084{
85 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000086 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -070087 write_unlock_bh(&fib6_walker_lock);
88}
89
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020090static void fib6_walker_unlink(struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070091{
92 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000093 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -070094 write_unlock_bh(&fib6_walker_lock);
95}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020096
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020099 int new, old;
100
101 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200102 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200103 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200104 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
105 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200106 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200109enum {
110 FIB6_NO_SERNUM_CHANGE = 0,
111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * Auxiliary address test functions for the radix tree.
115 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900116 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * 64bit processors)
118 */
119
120/*
121 * test bit
122 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000123#if defined(__LITTLE_ENDIAN)
124# define BITOP_BE32_SWIZZLE (0x1F & ~7)
125#else
126# define BITOP_BE32_SWIZZLE 0
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200129static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000131 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000132 /*
133 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
138 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700139 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
140 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200143static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct fib6_node *fn;
146
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800147 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return fn;
150}
151
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200152static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 kmem_cache_free(fib6_node_kmem, fn);
155}
156
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200157static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700160 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800163static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700164{
165 unsigned int h;
166
Thomas Graf375216a2006-10-21 20:20:54 -0700167 /*
168 * Initialize table lock at a single place to give lockdep a key,
169 * tables aren't visible prior to being linked to the list.
170 */
171 rwlock_init(&tb->tb6_lock);
172
Neil Hormana33bc5c2009-07-30 18:52:15 -0700173 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700174
175 /*
176 * No protection necessary, this is the only list mutatation
177 * operation, tables never disappear once they exist.
178 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800179 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700180}
181
182#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800183
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800184static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700185{
186 struct fib6_table *table;
187
188 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500189 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700190 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800191 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700192 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700193 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700194 }
195
196 return table;
197}
198
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800199struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700200{
201 struct fib6_table *tb;
202
203 if (id == 0)
204 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800205 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700206 if (tb)
207 return tb;
208
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800209 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500210 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800211 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700212
213 return tb;
214}
215
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800216struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700217{
218 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800219 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700220 unsigned int h;
221
222 if (id == 0)
223 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700224 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700225 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800226 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800227 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700228 if (tb->tb6_id == id) {
229 rcu_read_unlock();
230 return tb;
231 }
232 }
233 rcu_read_unlock();
234
235 return NULL;
236}
237
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000238static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700239{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240 fib6_link_table(net, net->ipv6.fib6_main_tbl);
241 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700242}
Thomas Grafc71099a2006-08-04 23:20:06 -0700243#else
244
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800245struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700246{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700248}
249
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700251{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800252 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700253}
254
David S. Miller4c9483b2011-03-12 16:22:43 -0500255struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700257{
David S. Miller4c9483b2011-03-12 16:22:43 -0500258 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259}
260
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000261static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700262{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800263 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700264}
265
266#endif
267
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200268static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700269{
270 int res;
271 struct rt6_info *rt;
272
Changli Gaod8d1f302010-06-10 23:31:35 -0700273 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700274 res = rt6_dump_route(rt, w->args);
275 if (res < 0) {
276 /* Frame is full, suspend walking */
277 w->leaf = rt;
278 return 1;
279 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700280 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700281 }
282 w->leaf = NULL;
283 return 0;
284}
285
286static void fib6_dump_end(struct netlink_callback *cb)
287{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200288 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700289
290 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800291 if (cb->args[4]) {
292 cb->args[4] = 0;
293 fib6_walker_unlink(w);
294 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700295 cb->args[2] = 0;
296 kfree(w);
297 }
Wang Yufen437de072014-03-28 12:07:04 +0800298 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700299 cb->args[1] = 3;
300}
301
302static int fib6_dump_done(struct netlink_callback *cb)
303{
304 fib6_dump_end(cb);
305 return cb->done ? cb->done(cb) : 0;
306}
307
308static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
309 struct netlink_callback *cb)
310{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200311 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700312 int res;
313
314 w = (void *)cb->args[2];
315 w->root = &table->tb6_root;
316
317 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000318 w->count = 0;
319 w->skip = 0;
320
Patrick McHardy1b43af52006-08-10 23:11:17 -0700321 read_lock_bh(&table->tb6_lock);
322 res = fib6_walk(w);
323 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000324 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700325 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000326 cb->args[5] = w->root->fn_sernum;
327 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700328 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000329 if (cb->args[5] != w->root->fn_sernum) {
330 /* Begin at the root if the tree changed */
331 cb->args[5] = w->root->fn_sernum;
332 w->state = FWS_INIT;
333 w->node = w->root;
334 w->skip = w->count;
335 } else
336 w->skip = 0;
337
Patrick McHardy1b43af52006-08-10 23:11:17 -0700338 read_lock_bh(&table->tb6_lock);
339 res = fib6_walk_continue(w);
340 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800341 if (res <= 0) {
342 fib6_walker_unlink(w);
343 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700344 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700345 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800346
Patrick McHardy1b43af52006-08-10 23:11:17 -0700347 return res;
348}
349
Thomas Grafc127ea22007-03-22 11:58:32 -0700350static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700351{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900352 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700353 unsigned int h, s_h;
354 unsigned int e = 0, s_e;
355 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200356 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700357 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800358 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700359 int res = 0;
360
361 s_h = cb->args[0];
362 s_e = cb->args[1];
363
364 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500365 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700366 /* New dump:
367 *
368 * 1. hook callback destructor.
369 */
370 cb->args[3] = (long)cb->done;
371 cb->done = fib6_dump_done;
372
373 /*
374 * 2. allocate and initialize walker.
375 */
376 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500377 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700378 return -ENOMEM;
379 w->func = fib6_dump_node;
380 cb->args[2] = (long)w;
381 }
382
383 arg.skb = skb;
384 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700385 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700386 w->args = &arg;
387
Eric Dumazete67f88d2011-04-27 22:56:07 +0000388 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700389 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700390 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800391 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800392 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700393 if (e < s_e)
394 goto next;
395 res = fib6_dump_table(tb, skb, cb);
396 if (res != 0)
397 goto out;
398next:
399 e++;
400 }
401 }
402out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000403 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700404 cb->args[1] = e;
405 cb->args[0] = h;
406
407 res = res < 0 ? res : skb->len;
408 if (res <= 0)
409 fib6_dump_end(cb);
410 return res;
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/*
414 * Routing Table
415 *
416 * return the appropriate node for a routing tree "add" operation
417 * by either creating and inserting or by returning an existing
418 * node.
419 */
420
fan.du9225b232013-07-22 14:21:09 +0800421static struct fib6_node *fib6_add_1(struct fib6_node *root,
422 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000423 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200424 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct fib6_node *fn, *in, *ln;
427 struct fib6_node *pn = NULL;
428 struct rt6key *key;
429 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900430 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 RT6_TRACE("fib6_add_1\n");
433
434 /* insert node in tree */
435
436 fn = root;
437
438 do {
439 key = (struct rt6key *)((u8 *)fn->leaf + offset);
440
441 /*
442 * Prefix match
443 */
444 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000445 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000446 if (!allow_create) {
447 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000448 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000449 return ERR_PTR(-ENOENT);
450 }
Joe Perchesf3213832012-05-15 14:11:53 +0000451 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000454 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * Exact match ?
458 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (plen == fn->fn_bit) {
461 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500462 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 rt6_release(fn->leaf);
464 fn->leaf = NULL;
465 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return fn;
470 }
471
472 /*
473 * We have more bits to go
474 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Try to walk down on tree. */
477 fn->fn_sernum = sernum;
478 dir = addr_bit_set(addr, fn->fn_bit);
479 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800480 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } while (fn);
482
Matti Vaittinen14df0152011-11-16 21:18:02 +0000483 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000484 /* We should not create new node because
485 * NLM_F_REPLACE was specified without NLM_F_CREATE
486 * I assume it is safe to require NLM_F_CREATE when
487 * REPLACE flag is used! Later we may want to remove the
488 * check for replace_required, because according
489 * to netlink specification, NLM_F_CREATE
490 * MUST be specified if new route is created.
491 * That would keep IPv6 consistent with IPv4
492 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000493 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000494 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000495 return ERR_PTR(-ENOENT);
496 }
Joe Perchesf3213832012-05-15 14:11:53 +0000497 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * We walked to the bottom of tree.
501 * Create new leaf node without children.
502 */
503
504 ln = node_alloc();
505
David S. Miller507c9b12011-12-03 17:50:45 -0500506 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000507 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ln->parent = pn;
511 ln->fn_sernum = sernum;
512
513 if (dir)
514 pn->right = ln;
515 else
516 pn->left = ln;
517
518 return ln;
519
520
521insert_above:
522 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900523 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * we have a less significant route.
525 * we've to insert an intermediate node on the list
526 * this new node will point to the one we need to create
527 * and the current
528 */
529
530 pn = fn->parent;
531
532 /* find 1st bit in difference between the 2 addrs.
533
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800534 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 but if it is >= plen, the value is ignored in any case.
536 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900537
fan.du9225b232013-07-22 14:21:09 +0800538 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900540 /*
541 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * / \
543 * (new leaf node)[ln] (old node)[fn]
544 */
545 if (plen > bit) {
546 in = node_alloc();
547 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900548
David S. Miller507c9b12011-12-03 17:50:45 -0500549 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (in)
551 node_free(in);
552 if (ln)
553 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000554 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900557 /*
558 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * RTN_RTINFO will
560 * be off since that an address that chooses one of
561 * the branches would not match less specific routes
562 * in the other branch
563 */
564
565 in->fn_bit = bit;
566
567 in->parent = pn;
568 in->leaf = fn->leaf;
569 atomic_inc(&in->leaf->rt6i_ref);
570
571 in->fn_sernum = sernum;
572
573 /* update parent pointer */
574 if (dir)
575 pn->right = in;
576 else
577 pn->left = in;
578
579 ln->fn_bit = plen;
580
581 ln->parent = in;
582 fn->parent = in;
583
584 ln->fn_sernum = sernum;
585
586 if (addr_bit_set(addr, bit)) {
587 in->right = ln;
588 in->left = fn;
589 } else {
590 in->left = ln;
591 in->right = fn;
592 }
593 } else { /* plen <= bit */
594
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900595 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * (new leaf node)[ln]
597 * / \
598 * (old node)[fn] NULL
599 */
600
601 ln = node_alloc();
602
David S. Miller507c9b12011-12-03 17:50:45 -0500603 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000604 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 ln->fn_bit = plen;
607
608 ln->parent = pn;
609
610 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (dir)
613 pn->right = ln;
614 else
615 pn->left = ln;
616
617 if (addr_bit_set(&key->addr, plen))
618 ln->right = fn;
619 else
620 ln->left = fn;
621
622 fn->parent = ln;
623 }
624 return ln;
625}
626
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200627static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200628{
629 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
630 RTF_GATEWAY;
631}
632
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100633static int fib6_commit_metrics(struct dst_entry *dst,
634 struct nlattr *mx, int mx_len)
635{
636 struct nlattr *nla;
637 int remaining;
638 u32 *mp;
639
640 if (dst->flags & DST_HOST) {
641 mp = dst_metrics_write_ptr(dst);
642 } else {
Benjamin Block793c3b42014-08-21 19:37:48 +0200643 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100644 if (!mp)
645 return -ENOMEM;
646 dst_init_metrics(dst, mp, 0);
647 }
648
649 nla_for_each_attr(nla, mx, mx_len, remaining) {
650 int type = nla_type(nla);
651
652 if (type) {
653 if (type > RTAX_MAX)
654 return -EINVAL;
655
656 mp[type - 1] = nla_get_u32(nla);
657 }
658 }
659 return 0;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*
663 * Insert routing information in a node.
664 */
665
666static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100667 struct nl_info *info, struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct rt6_info *iter = NULL;
670 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500671 int replace = (info->nlh &&
672 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
673 int add = (!info->nlh ||
674 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000675 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200676 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100677 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 ins = &fn->leaf;
680
David S. Miller507c9b12011-12-03 17:50:45 -0500681 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Search for duplicates
684 */
685
686 if (iter->rt6i_metric == rt->rt6i_metric) {
687 /*
688 * Same priority level
689 */
David S. Miller507c9b12011-12-03 17:50:45 -0500690 if (info->nlh &&
691 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000692 return -EEXIST;
693 if (replace) {
694 found++;
695 break;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David S. Millerd1918542011-12-28 20:19:20 -0500698 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 iter->rt6i_idev == rt->rt6i_idev &&
700 ipv6_addr_equal(&iter->rt6i_gateway,
701 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000702 if (rt->rt6i_nsiblings)
703 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500704 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000706 if (!(rt->rt6i_flags & RTF_EXPIRES))
707 rt6_clean_expires(iter);
708 else
709 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EEXIST;
711 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000712 /* If we have the same destination and the same metric,
713 * but not the same gateway, then the route we try to
714 * add is sibling to this route, increment our counter
715 * of siblings, and later we will add our route to the
716 * list.
717 * Only static routes (which don't have flag
718 * RTF_EXPIRES) are used for ECMPv6.
719 *
720 * To avoid long list, we only had siblings if the
721 * route have a gateway.
722 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200723 if (rt_can_ecmp &&
724 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000725 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 if (iter->rt6i_metric > rt->rt6i_metric)
729 break;
730
Changli Gaod8d1f302010-06-10 23:31:35 -0700731 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
David S. Millerf11e6652007-03-24 20:36:25 -0700734 /* Reset round-robin state, if necessary */
735 if (ins == &fn->leaf)
736 fn->rr_ptr = NULL;
737
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000738 /* Link this route to others same route. */
739 if (rt->rt6i_nsiblings) {
740 unsigned int rt6i_nsiblings;
741 struct rt6_info *sibling, *temp_sibling;
742
743 /* Find the first route that have the same metric */
744 sibling = fn->leaf;
745 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200746 if (sibling->rt6i_metric == rt->rt6i_metric &&
747 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000748 list_add_tail(&rt->rt6i_siblings,
749 &sibling->rt6i_siblings);
750 break;
751 }
752 sibling = sibling->dst.rt6_next;
753 }
754 /* For each sibling in the list, increment the counter of
755 * siblings. BUG() if counters does not match, list of siblings
756 * is broken!
757 */
758 rt6i_nsiblings = 0;
759 list_for_each_entry_safe(sibling, temp_sibling,
760 &rt->rt6i_siblings, rt6i_siblings) {
761 sibling->rt6i_nsiblings++;
762 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
763 rt6i_nsiblings++;
764 }
765 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * insert node
770 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000771 if (!replace) {
772 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000773 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000775add:
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100776 if (mx) {
777 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
778 if (err)
779 return err;
780 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000781 rt->dst.rt6_next = iter;
782 *ins = rt;
783 rt->rt6i_node = fn;
784 atomic_inc(&rt->rt6i_ref);
785 inet6_rt_notify(RTM_NEWROUTE, rt, info);
786 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David S. Miller507c9b12011-12-03 17:50:45 -0500788 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000789 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
790 fn->fn_flags |= RTN_RTINFO;
791 }
792
793 } else {
794 if (!found) {
795 if (add)
796 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000797 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000798 return -ENOENT;
799 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100800 if (mx) {
801 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
802 if (err)
803 return err;
804 }
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000805 *ins = rt;
806 rt->rt6i_node = fn;
807 rt->dst.rt6_next = iter->dst.rt6_next;
808 atomic_inc(&rt->rt6i_ref);
809 inet6_rt_notify(RTM_NEWROUTE, rt, info);
810 rt6_release(iter);
David S. Miller507c9b12011-12-03 17:50:45 -0500811 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000812 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
813 fn->fn_flags |= RTN_RTINFO;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 return 0;
818}
819
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200820static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700822 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500823 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700824 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700825 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800828void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700830 if (!timer_pending(&net->ipv6.ip6_fib_timer))
831 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700832 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
836 * Add routing information to the routing tree.
837 * <destination addr>/<source addr>
838 * with source addr info in sub-trees
839 */
840
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100841int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
842 struct nlattr *mx, int mx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700844 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000846 int allow_create = 1;
847 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200848 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500849
850 if (info->nlh) {
851 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000852 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500853 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000854 replace_required = 1;
855 }
856 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000857 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
fan.du9225b232013-07-22 14:21:09 +0800859 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
860 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200861 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000862 if (IS_ERR(fn)) {
863 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200864 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700868 pn = fn;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#ifdef CONFIG_IPV6_SUBTREES
871 if (rt->rt6i_src.plen) {
872 struct fib6_node *sn;
873
David S. Miller507c9b12011-12-03 17:50:45 -0500874 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct fib6_node *sfn;
876
877 /*
878 * Create subtree.
879 *
880 * fn[main tree]
881 * |
882 * sfn[subtree root]
883 * \
884 * sn[new leaf node]
885 */
886
887 /* Create subtree root node */
888 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500889 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto st_failure;
891
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800892 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
893 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200895 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* Now add the first leaf node to new subtree */
898
899 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800900 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000901 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200902 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000904 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* If it is failed, discard just allocated
906 root, and then (in st_failure) stale node
907 in main tree.
908 */
909 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000910 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto st_failure;
912 }
913
914 /* Now link new subtree to main tree */
915 sfn->parent = fn;
916 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 } else {
918 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800919 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000920 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200921 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000923 if (IS_ERR(sn)) {
924 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
David S. Miller507c9b12011-12-03 17:50:45 -0500929 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700930 fn->leaf = rt;
931 atomic_inc(&rt->rt6i_ref);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 fn = sn;
934 }
935#endif
936
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100937 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
David S. Miller507c9b12011-12-03 17:50:45 -0500938 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800939 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500940 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +0800941 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
944out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700945 if (err) {
946#ifdef CONFIG_IPV6_SUBTREES
947 /*
948 * If fib6_add_1 has cleared the old leaf pointer in the
949 * super-tree leaf node we have to find a new one for it.
950 */
David S. Miller3c051232008-04-18 01:46:19 -0700951 if (pn != fn && pn->leaf == rt) {
952 pn->leaf = NULL;
953 atomic_dec(&rt->rt6i_ref);
954 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700955 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800956 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700957#if RT6_DEBUG >= 2
958 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700959 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800960 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700961 }
962#endif
963 atomic_inc(&pn->leaf->rt6i_ref);
964 }
965#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700966 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return err;
969
970#ifdef CONFIG_IPV6_SUBTREES
971 /* Subtree creation failed, probably main tree node
972 is orphan. If it is, shoot it.
973 */
974st_failure:
975 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800976 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700977 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return err;
979#endif
980}
981
982/*
983 * Routing tree lookup
984 *
985 */
986
987struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -0500988 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000989 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990};
991
Wang Yufen437de072014-03-28 12:07:04 +0800992static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
993 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -0800996 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700998 if (unlikely(args->offset == 0))
999 return NULL;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
1002 * Descend on a tree
1003 */
1004
1005 fn = root;
1006
1007 for (;;) {
1008 struct fib6_node *next;
1009
1010 dir = addr_bit_set(args->addr, fn->fn_bit);
1011
1012 next = dir ? fn->right : fn->left;
1013
1014 if (next) {
1015 fn = next;
1016 continue;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019 }
1020
David S. Miller507c9b12011-12-03 17:50:45 -05001021 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001022 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct rt6key *key;
1024
1025 key = (struct rt6key *) ((u8 *) fn->leaf +
1026 args->offset);
1027
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001028 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1029#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001030 if (fn->subtree) {
1031 struct fib6_node *sfn;
1032 sfn = fib6_lookup_1(fn->subtree,
1033 args + 1);
1034 if (!sfn)
1035 goto backtrack;
1036 fn = sfn;
1037 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001038#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001039 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001040 return fn;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001043#ifdef CONFIG_IPV6_SUBTREES
1044backtrack:
1045#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001046 if (fn->fn_flags & RTN_ROOT)
1047 break;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 fn = fn->parent;
1050 }
1051
1052 return NULL;
1053}
1054
Wang Yufen437de072014-03-28 12:07:04 +08001055struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1056 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001059 struct lookup_args args[] = {
1060 {
1061 .offset = offsetof(struct rt6_info, rt6i_dst),
1062 .addr = daddr,
1063 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001065 {
1066 .offset = offsetof(struct rt6_info, rt6i_src),
1067 .addr = saddr,
1068 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001070 {
1071 .offset = 0, /* sentinel */
1072 }
1073 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001075 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001076 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 fn = root;
1078
1079 return fn;
1080}
1081
1082/*
1083 * Get node with specified destination prefix (and source prefix,
1084 * if subtrees are used)
1085 */
1086
1087
Wang Yufen437de072014-03-28 12:07:04 +08001088static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1089 const struct in6_addr *addr,
1090 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct fib6_node *fn;
1093
1094 for (fn = root; fn ; ) {
1095 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1096
1097 /*
1098 * Prefix match
1099 */
1100 if (plen < fn->fn_bit ||
1101 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1102 return NULL;
1103
1104 if (plen == fn->fn_bit)
1105 return fn;
1106
1107 /*
1108 * We have more bits to go
1109 */
1110 if (addr_bit_set(addr, fn->fn_bit))
1111 fn = fn->right;
1112 else
1113 fn = fn->left;
1114 }
1115 return NULL;
1116}
1117
Wang Yufen437de072014-03-28 12:07:04 +08001118struct fib6_node *fib6_locate(struct fib6_node *root,
1119 const struct in6_addr *daddr, int dst_len,
1120 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct fib6_node *fn;
1123
1124 fn = fib6_locate_1(root, daddr, dst_len,
1125 offsetof(struct rt6_info, rt6i_dst));
1126
1127#ifdef CONFIG_IPV6_SUBTREES
1128 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001129 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001130 if (fn && fn->subtree)
1131 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 offsetof(struct rt6_info, rt6i_src));
1133 }
1134#endif
1135
David S. Miller507c9b12011-12-03 17:50:45 -05001136 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return fn;
1138
1139 return NULL;
1140}
1141
1142
1143/*
1144 * Deletion
1145 *
1146 */
1147
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001148static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
David S. Miller507c9b12011-12-03 17:50:45 -05001150 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001151 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
David S. Miller507c9b12011-12-03 17:50:45 -05001153 while (fn) {
1154 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001156 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return fn->right->leaf;
1158
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001159 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 return NULL;
1162}
1163
1164/*
1165 * Called to trim the tree of intermediate nodes when possible. "fn"
1166 * is the node we want to try and remove.
1167 */
1168
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001169static struct fib6_node *fib6_repair_tree(struct net *net,
1170 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int children;
1173 int nstate;
1174 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001175 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int iter = 0;
1177
1178 for (;;) {
1179 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1180 iter++;
1181
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001182 WARN_ON(fn->fn_flags & RTN_RTINFO);
1183 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1184 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 children = 0;
1187 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001188 if (fn->right)
1189 child = fn->right, children |= 1;
1190 if (fn->left)
1191 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001193 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#ifdef CONFIG_IPV6_SUBTREES
1195 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001196 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197#endif
1198 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001199 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001201 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001202 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001203 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205#endif
1206 atomic_inc(&fn->leaf->rt6i_ref);
1207 return fn->parent;
1208 }
1209
1210 pn = fn->parent;
1211#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001212 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001213 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001214 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 nstate = FWS_L;
1216 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001217 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001219 if (pn->right == fn)
1220 pn->right = child;
1221 else if (pn->left == fn)
1222 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001224 else
1225 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226#endif
1227 if (child)
1228 child->parent = pn;
1229 nstate = FWS_R;
1230#ifdef CONFIG_IPV6_SUBTREES
1231 }
1232#endif
1233
1234 read_lock(&fib6_walker_lock);
1235 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001236 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (w->root == fn) {
1238 w->root = w->node = NULL;
1239 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1240 } else if (w->node == fn) {
1241 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1242 w->node = pn;
1243 w->state = nstate;
1244 }
1245 } else {
1246 if (w->root == fn) {
1247 w->root = child;
1248 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1249 }
1250 if (w->node == fn) {
1251 w->node = child;
1252 if (children&2) {
1253 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001254 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 } else {
1256 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001257 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 }
1260 }
1261 }
1262 read_unlock(&fib6_walker_lock);
1263
1264 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001265 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return pn;
1267
1268 rt6_release(pn->leaf);
1269 pn->leaf = NULL;
1270 fn = pn;
1271 }
1272}
1273
1274static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001275 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001277 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001279 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 RT6_TRACE("fib6_del_route\n");
1282
1283 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001284 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001286 net->ipv6.rt6_stats->fib_rt_entries--;
1287 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
David S. Millerf11e6652007-03-24 20:36:25 -07001289 /* Reset round-robin state, if necessary */
1290 if (fn->rr_ptr == rt)
1291 fn->rr_ptr = NULL;
1292
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001293 /* Remove this entry from other siblings */
1294 if (rt->rt6i_nsiblings) {
1295 struct rt6_info *sibling, *next_sibling;
1296
1297 list_for_each_entry_safe(sibling, next_sibling,
1298 &rt->rt6i_siblings, rt6i_siblings)
1299 sibling->rt6i_nsiblings--;
1300 rt->rt6i_nsiblings = 0;
1301 list_del_init(&rt->rt6i_siblings);
1302 }
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* Adjust walkers */
1305 read_lock(&fib6_walker_lock);
1306 FOR_WALKERS(w) {
1307 if (w->state == FWS_C && w->leaf == rt) {
1308 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001309 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001310 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 w->state = FWS_U;
1312 }
1313 }
1314 read_unlock(&fib6_walker_lock);
1315
Changli Gaod8d1f302010-06-10 23:31:35 -07001316 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001319 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001321 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001322 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
1325 if (atomic_read(&rt->rt6i_ref) != 1) {
1326 /* This route is used as dummy address holder in some split
1327 * nodes. It is not leaked, but it still holds other resources,
1328 * which must be released in time. So, scan ascendant nodes
1329 * and replace dummy references to this route with references
1330 * to still alive ones.
1331 */
1332 while (fn) {
David S. Miller507c9b12011-12-03 17:50:45 -05001333 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001334 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 atomic_inc(&fn->leaf->rt6i_ref);
1336 rt6_release(rt);
1337 }
1338 fn = fn->parent;
1339 }
1340 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001341 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Thomas Graf86872cb2006-08-22 00:01:08 -07001344 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 rt6_release(rt);
1346}
1347
Thomas Graf86872cb2006-08-22 00:01:08 -07001348int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001350 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct fib6_node *fn = rt->rt6i_node;
1352 struct rt6_info **rtp;
1353
1354#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001355 if (rt->dst.obsolete > 0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001356 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -ENOENT;
1358 }
1359#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001360 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -ENOENT;
1362
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001363 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
David S. Miller507c9b12011-12-03 17:50:45 -05001365 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001366 struct fib6_node *pn = fn;
1367#ifdef CONFIG_IPV6_SUBTREES
1368 /* clones of this route might be in another subtree */
1369 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001370 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001371 pn = pn->parent;
1372 pn = pn->parent;
1373 }
1374#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001375 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /*
1379 * Walk the leaf entries looking for ourself
1380 */
1381
Changli Gaod8d1f302010-06-10 23:31:35 -07001382 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001384 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386 }
1387 }
1388 return -ENOENT;
1389}
1390
1391/*
1392 * Tree traversal function.
1393 *
1394 * Certainly, it is not interrupt safe.
1395 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1396 * It means, that we can modify tree during walking
1397 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001398 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *
1400 * It guarantees that every node will be traversed,
1401 * and that it will be traversed only once.
1402 *
1403 * Callback function w->func may return:
1404 * 0 -> continue walking.
1405 * positive value -> walking is suspended (used by tree dumps,
1406 * and probably by gc, if it will be split to several slices)
1407 * negative value -> terminate walking.
1408 *
1409 * The function itself returns:
1410 * 0 -> walk is complete.
1411 * >0 -> walk is incomplete (i.e. suspended)
1412 * <0 -> walk is terminated by an error.
1413 */
1414
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001415static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct fib6_node *fn, *pn;
1418
1419 for (;;) {
1420 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001421 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return 0;
1423
1424 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001425 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 w->state = FWS_C;
1427 w->leaf = fn->leaf;
1428 }
1429 switch (w->state) {
1430#ifdef CONFIG_IPV6_SUBTREES
1431 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001432 if (FIB6_SUBTREE(fn)) {
1433 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 continue;
1435 }
1436 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 case FWS_L:
1439 if (fn->left) {
1440 w->node = fn->left;
1441 w->state = FWS_INIT;
1442 continue;
1443 }
1444 w->state = FWS_R;
1445 case FWS_R:
1446 if (fn->right) {
1447 w->node = fn->right;
1448 w->state = FWS_INIT;
1449 continue;
1450 }
1451 w->state = FWS_C;
1452 w->leaf = fn->leaf;
1453 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001454 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001455 int err;
1456
Eric Dumazetfa809e22012-06-25 15:37:19 -07001457 if (w->skip) {
1458 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001459 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001460 }
1461
1462 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (err)
1464 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001465
1466 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 continue;
1468 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001469skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 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
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001499static int fib6_walk(struct fib6_walker *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
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001513static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int res;
1516 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001517 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, 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
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001522 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1523 w->node->fn_sernum != c->sernum)
1524 w->node->fn_sernum = c->sernum;
1525
1526 if (!c->func) {
1527 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1528 w->leaf = NULL;
1529 return 0;
1530 }
1531
Changli Gaod8d1f302010-06-10 23:31:35 -07001532 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 res = c->func(rt, c->arg);
1534 if (res < 0) {
1535 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001536 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (res) {
1538#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001539 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1540 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541#endif
1542 continue;
1543 }
1544 return 0;
1545 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001546 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 w->leaf = rt;
1549 return 0;
1550}
1551
1552/*
1553 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001554 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 * func is called on each route.
1556 * It may return -1 -> delete this route.
1557 * 0 -> continue walking
1558 *
1559 * prune==1 -> only immediate children of node (certainly,
1560 * ignoring pure split nodes) will be scanned.
1561 */
1562
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001563static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001564 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001565 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001567 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 c.w.root = root;
1570 c.w.func = fib6_clean_node;
1571 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001572 c.w.count = 0;
1573 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001575 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001577 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 fib6_walk(&c.w);
1580}
1581
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001582static void __fib6_clean_all(struct net *net,
1583 int (*func)(struct rt6_info *, void *),
1584 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001585{
Thomas Grafc71099a2006-08-04 23:20:06 -07001586 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001587 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001588 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001589
Patrick McHardy1b43af52006-08-10 23:11:17 -07001590 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001591 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001592 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001593 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001594 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001595 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001596 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001597 write_unlock_bh(&table->tb6_lock);
1598 }
1599 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001600 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001601}
1602
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001603void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1604 void *arg)
1605{
1606 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1610{
1611 if (rt->rt6i_flags & RTF_CACHE) {
1612 RT6_TRACE("pruning clone %p\n", rt);
1613 return -1;
1614 }
1615
1616 return 0;
1617}
1618
Duan Jiong163cd4e2014-05-09 13:31:43 +08001619static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001621 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1622 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001623}
1624
1625static void fib6_flush_trees(struct net *net)
1626{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001627 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001628
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001629 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001630}
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
1633 * Garbage collection
1634 */
1635
1636static struct fib6_gc_args
1637{
1638 int timeout;
1639 int more;
1640} gc_args;
1641
1642static int fib6_age(struct rt6_info *rt, void *arg)
1643{
1644 unsigned long now = jiffies;
1645
1646 /*
1647 * check addrconf expiration here.
1648 * Routes are expired even if they are in use.
1649 *
1650 * Also age clones. Note, that clones are aged out
1651 * only if they are not in use now.
1652 */
1653
David S. Millerd1918542011-12-28 20:19:20 -05001654 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1655 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return -1;
1658 }
1659 gc_args.more++;
1660 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001661 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1662 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 RT6_TRACE("aging clone %p\n", rt);
1664 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001665 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1666 struct neighbour *neigh;
1667 __u8 neigh_flags = 0;
1668
1669 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1670 if (neigh) {
1671 neigh_flags = neigh->flags;
1672 neigh_release(neigh);
1673 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001674 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001675 RT6_TRACE("purging route %p via non-router but gateway\n",
1676 rt);
1677 return -1;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680 gc_args.more++;
1681 }
1682
1683 return 0;
1684}
1685
1686static DEFINE_SPINLOCK(fib6_gc_lock);
1687
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001688void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001690 unsigned long now;
1691
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001692 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001694 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1695 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001698 gc_args.timeout = expires ? (int)expires :
1699 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001701 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001702
Li RongQing0c3584d2013-12-27 16:32:38 +08001703 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001704 now = jiffies;
1705 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001708 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001709 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001710 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001711 else
1712 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 spin_unlock_bh(&fib6_gc_lock);
1714}
1715
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001716static void fib6_gc_timer_cb(unsigned long arg)
1717{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001718 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001719}
1720
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001721static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001722{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001723 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1724
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001725 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001726
Benjamin Theryc5728722008-03-03 23:34:17 -08001727 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1728 if (!net->ipv6.rt6_stats)
1729 goto out_timer;
1730
Eric Dumazet10da66f2010-10-13 08:22:03 +00001731 /* Avoid false sharing : Use at least a full cache line */
1732 size = max_t(size_t, size, L1_CACHE_BYTES);
1733
1734 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001735 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001736 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001737
1738 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1739 GFP_KERNEL);
1740 if (!net->ipv6.fib6_main_tbl)
1741 goto out_fib_table_hash;
1742
1743 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001744 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001745 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1746 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001747 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001748
1749#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1750 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1751 GFP_KERNEL);
1752 if (!net->ipv6.fib6_local_tbl)
1753 goto out_fib6_main_tbl;
1754 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001755 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001756 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1757 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001758 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001759#endif
1760 fib6_tables_init(net);
1761
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001762 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001763
1764#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1765out_fib6_main_tbl:
1766 kfree(net->ipv6.fib6_main_tbl);
1767#endif
1768out_fib_table_hash:
1769 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001770out_rt6_stats:
1771 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001772out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001773 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001774}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001775
1776static void fib6_net_exit(struct net *net)
1777{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001778 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001779 del_timer_sync(&net->ipv6.ip6_fib_timer);
1780
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001781#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001782 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001783 kfree(net->ipv6.fib6_local_tbl);
1784#endif
David S. Miller8e773272012-06-11 00:01:52 -07001785 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001786 kfree(net->ipv6.fib6_main_tbl);
1787 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001788 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001789}
1790
1791static struct pernet_operations fib6_net_ops = {
1792 .init = fib6_net_init,
1793 .exit = fib6_net_exit,
1794};
1795
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001796int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001798 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1801 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001802 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001803 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001804 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001805 goto out;
1806
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001807 ret = register_pernet_subsys(&fib6_net_ops);
1808 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001809 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001810
1811 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1812 NULL);
1813 if (ret)
1814 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001815
1816 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001817out:
1818 return ret;
1819
David S. Millere8803b62012-06-16 01:12:19 -07001820out_unregister_subsys:
1821 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001822out_kmem_cache_create:
1823 kmem_cache_destroy(fib6_node_kmem);
1824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827void fib6_gc_cleanup(void)
1828{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001829 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 kmem_cache_destroy(fib6_node_kmem);
1831}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001832
1833#ifdef CONFIG_PROC_FS
1834
1835struct ipv6_route_iter {
1836 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001837 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001838 loff_t skip;
1839 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001840 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001841};
1842
1843static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1844{
1845 struct rt6_info *rt = v;
1846 struct ipv6_route_iter *iter = seq->private;
1847
1848 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1849
1850#ifdef CONFIG_IPV6_SUBTREES
1851 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1852#else
1853 seq_puts(seq, "00000000000000000000000000000000 00 ");
1854#endif
1855 if (rt->rt6i_flags & RTF_GATEWAY)
1856 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1857 else
1858 seq_puts(seq, "00000000000000000000000000000000");
1859
1860 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1861 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1862 rt->dst.__use, rt->rt6i_flags,
1863 rt->dst.dev ? rt->dst.dev->name : "");
1864 iter->w.leaf = NULL;
1865 return 0;
1866}
1867
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001868static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001869{
1870 struct ipv6_route_iter *iter = w->args;
1871
1872 if (!iter->skip)
1873 return 1;
1874
1875 do {
1876 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1877 iter->skip--;
1878 if (!iter->skip && iter->w.leaf)
1879 return 1;
1880 } while (iter->w.leaf);
1881
1882 return 0;
1883}
1884
1885static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1886{
1887 memset(&iter->w, 0, sizeof(iter->w));
1888 iter->w.func = ipv6_route_yield;
1889 iter->w.root = &iter->tbl->tb6_root;
1890 iter->w.state = FWS_INIT;
1891 iter->w.node = iter->w.root;
1892 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001893 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001894 INIT_LIST_HEAD(&iter->w.lh);
1895 fib6_walker_link(&iter->w);
1896}
1897
1898static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1899 struct net *net)
1900{
1901 unsigned int h;
1902 struct hlist_node *node;
1903
1904 if (tbl) {
1905 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1906 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1907 } else {
1908 h = 0;
1909 node = NULL;
1910 }
1911
1912 while (!node && h < FIB6_TABLE_HASHSZ) {
1913 node = rcu_dereference_bh(
1914 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1915 }
1916 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1917}
1918
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001919static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1920{
1921 if (iter->sernum != iter->w.root->fn_sernum) {
1922 iter->sernum = iter->w.root->fn_sernum;
1923 iter->w.state = FWS_INIT;
1924 iter->w.node = iter->w.root;
1925 WARN_ON(iter->w.skip);
1926 iter->w.skip = iter->w.count;
1927 }
1928}
1929
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001930static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1931{
1932 int r;
1933 struct rt6_info *n;
1934 struct net *net = seq_file_net(seq);
1935 struct ipv6_route_iter *iter = seq->private;
1936
1937 if (!v)
1938 goto iter_table;
1939
1940 n = ((struct rt6_info *)v)->dst.rt6_next;
1941 if (n) {
1942 ++*pos;
1943 return n;
1944 }
1945
1946iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001947 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001948 read_lock(&iter->tbl->tb6_lock);
1949 r = fib6_walk_continue(&iter->w);
1950 read_unlock(&iter->tbl->tb6_lock);
1951 if (r > 0) {
1952 if (v)
1953 ++*pos;
1954 return iter->w.leaf;
1955 } else if (r < 0) {
1956 fib6_walker_unlink(&iter->w);
1957 return NULL;
1958 }
1959 fib6_walker_unlink(&iter->w);
1960
1961 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1962 if (!iter->tbl)
1963 return NULL;
1964
1965 ipv6_route_seq_setup_walk(iter);
1966 goto iter_table;
1967}
1968
1969static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1970 __acquires(RCU_BH)
1971{
1972 struct net *net = seq_file_net(seq);
1973 struct ipv6_route_iter *iter = seq->private;
1974
1975 rcu_read_lock_bh();
1976 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1977 iter->skip = *pos;
1978
1979 if (iter->tbl) {
1980 ipv6_route_seq_setup_walk(iter);
1981 return ipv6_route_seq_next(seq, NULL, pos);
1982 } else {
1983 return NULL;
1984 }
1985}
1986
1987static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1988{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001989 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001990 return w->node && !(w->state == FWS_U && w->node == w->root);
1991}
1992
1993static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1994 __releases(RCU_BH)
1995{
1996 struct ipv6_route_iter *iter = seq->private;
1997
1998 if (ipv6_route_iter_active(iter))
1999 fib6_walker_unlink(&iter->w);
2000
2001 rcu_read_unlock_bh();
2002}
2003
2004static const struct seq_operations ipv6_route_seq_ops = {
2005 .start = ipv6_route_seq_start,
2006 .next = ipv6_route_seq_next,
2007 .stop = ipv6_route_seq_stop,
2008 .show = ipv6_route_seq_show
2009};
2010
2011int ipv6_route_open(struct inode *inode, struct file *file)
2012{
2013 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2014 sizeof(struct ipv6_route_iter));
2015}
2016
2017#endif /* CONFIG_PROC_FS */