blob: 03c520a4ebeb62b228db91b89900e31e4fc29070 [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
Florian Westphale715b6d2015-01-05 23:57:44 +0100633static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100634{
Florian Westphale715b6d2015-01-05 23:57:44 +0100635 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100636
Florian Westphale715b6d2015-01-05 23:57:44 +0100637 for (i = 0; i < RTAX_MAX; i++) {
638 if (test_bit(i, mxc->mx_valid))
639 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100640 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100641}
642
643static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
644{
645 if (!mxc->mx)
646 return 0;
647
648 if (dst->flags & DST_HOST) {
649 u32 *mp = dst_metrics_write_ptr(dst);
650
651 if (unlikely(!mp))
652 return -ENOMEM;
653
654 fib6_copy_metrics(mp, mxc);
655 } else {
656 dst_init_metrics(dst, mxc->mx, false);
657
658 /* We've stolen mx now. */
659 mxc->mx = NULL;
660 }
661
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100662 return 0;
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
666 * Insert routing information in a node.
667 */
668
669static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100670 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct rt6_info *iter = NULL;
673 struct rt6_info **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500674 int replace = (info->nlh &&
675 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
676 int add = (!info->nlh ||
677 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000678 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200679 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100680 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 ins = &fn->leaf;
683
David S. Miller507c9b12011-12-03 17:50:45 -0500684 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * Search for duplicates
687 */
688
689 if (iter->rt6i_metric == rt->rt6i_metric) {
690 /*
691 * Same priority level
692 */
David S. Miller507c9b12011-12-03 17:50:45 -0500693 if (info->nlh &&
694 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000695 return -EEXIST;
696 if (replace) {
697 found++;
698 break;
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
David S. Millerd1918542011-12-28 20:19:20 -0500701 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 iter->rt6i_idev == rt->rt6i_idev &&
703 ipv6_addr_equal(&iter->rt6i_gateway,
704 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000705 if (rt->rt6i_nsiblings)
706 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500707 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000709 if (!(rt->rt6i_flags & RTF_EXPIRES))
710 rt6_clean_expires(iter);
711 else
712 rt6_set_expires(iter, rt->dst.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return -EEXIST;
714 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000715 /* If we have the same destination and the same metric,
716 * but not the same gateway, then the route we try to
717 * add is sibling to this route, increment our counter
718 * of siblings, and later we will add our route to the
719 * list.
720 * Only static routes (which don't have flag
721 * RTF_EXPIRES) are used for ECMPv6.
722 *
723 * To avoid long list, we only had siblings if the
724 * route have a gateway.
725 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200726 if (rt_can_ecmp &&
727 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000728 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
731 if (iter->rt6i_metric > rt->rt6i_metric)
732 break;
733
Changli Gaod8d1f302010-06-10 23:31:35 -0700734 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
David S. Millerf11e6652007-03-24 20:36:25 -0700737 /* Reset round-robin state, if necessary */
738 if (ins == &fn->leaf)
739 fn->rr_ptr = NULL;
740
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000741 /* Link this route to others same route. */
742 if (rt->rt6i_nsiblings) {
743 unsigned int rt6i_nsiblings;
744 struct rt6_info *sibling, *temp_sibling;
745
746 /* Find the first route that have the same metric */
747 sibling = fn->leaf;
748 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200749 if (sibling->rt6i_metric == rt->rt6i_metric &&
750 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000751 list_add_tail(&rt->rt6i_siblings,
752 &sibling->rt6i_siblings);
753 break;
754 }
755 sibling = sibling->dst.rt6_next;
756 }
757 /* For each sibling in the list, increment the counter of
758 * siblings. BUG() if counters does not match, list of siblings
759 * is broken!
760 */
761 rt6i_nsiblings = 0;
762 list_for_each_entry_safe(sibling, temp_sibling,
763 &rt->rt6i_siblings, rt6i_siblings) {
764 sibling->rt6i_nsiblings++;
765 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
766 rt6i_nsiblings++;
767 }
768 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
772 * insert node
773 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000774 if (!replace) {
775 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000776 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000778add:
Florian Westphale715b6d2015-01-05 23:57:44 +0100779 err = fib6_commit_metrics(&rt->dst, mxc);
780 if (err)
781 return err;
782
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000783 rt->dst.rt6_next = iter;
784 *ins = rt;
785 rt->rt6i_node = fn;
786 atomic_inc(&rt->rt6i_ref);
787 inet6_rt_notify(RTM_NEWROUTE, rt, info);
788 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David S. Miller507c9b12011-12-03 17:50:45 -0500790 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000791 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
792 fn->fn_flags |= RTN_RTINFO;
793 }
794
795 } else {
796 if (!found) {
797 if (add)
798 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000799 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000800 return -ENOENT;
801 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100802
803 err = fib6_commit_metrics(&rt->dst, mxc);
804 if (err)
805 return err;
806
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000807 *ins = rt;
808 rt->rt6i_node = fn;
809 rt->dst.rt6_next = iter->dst.rt6_next;
810 atomic_inc(&rt->rt6i_ref);
811 inet6_rt_notify(RTM_NEWROUTE, rt, info);
812 rt6_release(iter);
David S. Miller507c9b12011-12-03 17:50:45 -0500813 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000814 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
815 fn->fn_flags |= RTN_RTINFO;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
819 return 0;
820}
821
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200822static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700824 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500825 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700826 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700827 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800830void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700832 if (!timer_pending(&net->ipv6.ip6_fib_timer))
833 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700834 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837/*
838 * Add routing information to the routing tree.
839 * <destination addr>/<source addr>
840 * with source addr info in sub-trees
841 */
842
Florian Westphale715b6d2015-01-05 23:57:44 +0100843int fib6_add(struct fib6_node *root, struct rt6_info *rt,
844 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700846 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000848 int allow_create = 1;
849 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200850 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500851
852 if (info->nlh) {
853 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000854 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500855 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000856 replace_required = 1;
857 }
858 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000859 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
fan.du9225b232013-07-22 14:21:09 +0800861 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
862 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200863 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000864 if (IS_ERR(fn)) {
865 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200866 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700870 pn = fn;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#ifdef CONFIG_IPV6_SUBTREES
873 if (rt->rt6i_src.plen) {
874 struct fib6_node *sn;
875
David S. Miller507c9b12011-12-03 17:50:45 -0500876 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct fib6_node *sfn;
878
879 /*
880 * Create subtree.
881 *
882 * fn[main tree]
883 * |
884 * sfn[subtree root]
885 * \
886 * sn[new leaf node]
887 */
888
889 /* Create subtree root node */
890 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -0500891 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto st_failure;
893
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800894 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
895 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200897 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* Now add the first leaf node to new subtree */
900
901 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800902 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000903 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200904 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Wei Yongjunf950c0e2012-09-20 18:29:56 +0000906 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* If it is failed, discard just allocated
908 root, and then (in st_failure) stale node
909 in main tree.
910 */
911 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +0000912 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto st_failure;
914 }
915
916 /* Now link new subtree to main tree */
917 sfn->parent = fn;
918 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 } else {
920 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +0800921 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000922 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200923 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000925 if (IS_ERR(sn)) {
926 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +0000928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
David S. Miller507c9b12011-12-03 17:50:45 -0500931 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700932 fn->leaf = rt;
933 atomic_inc(&rt->rt6i_ref);
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 fn = sn;
936 }
937#endif
938
Florian Westphale715b6d2015-01-05 23:57:44 +0100939 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -0500940 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800941 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -0500942 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +0800943 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700947 if (err) {
948#ifdef CONFIG_IPV6_SUBTREES
949 /*
950 * If fib6_add_1 has cleared the old leaf pointer in the
951 * super-tree leaf node we have to find a new one for it.
952 */
David S. Miller3c051232008-04-18 01:46:19 -0700953 if (pn != fn && pn->leaf == rt) {
954 pn->leaf = NULL;
955 atomic_dec(&rt->rt6i_ref);
956 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700957 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800958 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700959#if RT6_DEBUG >= 2
960 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700961 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800962 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700963 }
964#endif
965 atomic_inc(&pn->leaf->rt6i_ref);
966 }
967#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700968 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return err;
971
972#ifdef CONFIG_IPV6_SUBTREES
973 /* Subtree creation failed, probably main tree node
974 is orphan. If it is, shoot it.
975 */
976st_failure:
977 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800978 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700979 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return err;
981#endif
982}
983
984/*
985 * Routing tree lookup
986 *
987 */
988
989struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -0500990 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000991 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992};
993
Wang Yufen437de072014-03-28 12:07:04 +0800994static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
995 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -0800998 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001000 if (unlikely(args->offset == 0))
1001 return NULL;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * Descend on a tree
1005 */
1006
1007 fn = root;
1008
1009 for (;;) {
1010 struct fib6_node *next;
1011
1012 dir = addr_bit_set(args->addr, fn->fn_bit);
1013
1014 next = dir ? fn->right : fn->left;
1015
1016 if (next) {
1017 fn = next;
1018 continue;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021 }
1022
David S. Miller507c9b12011-12-03 17:50:45 -05001023 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001024 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 struct rt6key *key;
1026
1027 key = (struct rt6key *) ((u8 *) fn->leaf +
1028 args->offset);
1029
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001030 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1031#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001032 if (fn->subtree) {
1033 struct fib6_node *sfn;
1034 sfn = fib6_lookup_1(fn->subtree,
1035 args + 1);
1036 if (!sfn)
1037 goto backtrack;
1038 fn = sfn;
1039 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001040#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001041 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001042 return fn;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001045#ifdef CONFIG_IPV6_SUBTREES
1046backtrack:
1047#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001048 if (fn->fn_flags & RTN_ROOT)
1049 break;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 fn = fn->parent;
1052 }
1053
1054 return NULL;
1055}
1056
Wang Yufen437de072014-03-28 12:07:04 +08001057struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1058 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001061 struct lookup_args args[] = {
1062 {
1063 .offset = offsetof(struct rt6_info, rt6i_dst),
1064 .addr = daddr,
1065 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001067 {
1068 .offset = offsetof(struct rt6_info, rt6i_src),
1069 .addr = saddr,
1070 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001072 {
1073 .offset = 0, /* sentinel */
1074 }
1075 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001077 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001078 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 fn = root;
1080
1081 return fn;
1082}
1083
1084/*
1085 * Get node with specified destination prefix (and source prefix,
1086 * if subtrees are used)
1087 */
1088
1089
Wang Yufen437de072014-03-28 12:07:04 +08001090static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1091 const struct in6_addr *addr,
1092 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct fib6_node *fn;
1095
1096 for (fn = root; fn ; ) {
1097 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1098
1099 /*
1100 * Prefix match
1101 */
1102 if (plen < fn->fn_bit ||
1103 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1104 return NULL;
1105
1106 if (plen == fn->fn_bit)
1107 return fn;
1108
1109 /*
1110 * We have more bits to go
1111 */
1112 if (addr_bit_set(addr, fn->fn_bit))
1113 fn = fn->right;
1114 else
1115 fn = fn->left;
1116 }
1117 return NULL;
1118}
1119
Wang Yufen437de072014-03-28 12:07:04 +08001120struct fib6_node *fib6_locate(struct fib6_node *root,
1121 const struct in6_addr *daddr, int dst_len,
1122 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct fib6_node *fn;
1125
1126 fn = fib6_locate_1(root, daddr, dst_len,
1127 offsetof(struct rt6_info, rt6i_dst));
1128
1129#ifdef CONFIG_IPV6_SUBTREES
1130 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001131 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001132 if (fn && fn->subtree)
1133 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 offsetof(struct rt6_info, rt6i_src));
1135 }
1136#endif
1137
David S. Miller507c9b12011-12-03 17:50:45 -05001138 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return fn;
1140
1141 return NULL;
1142}
1143
1144
1145/*
1146 * Deletion
1147 *
1148 */
1149
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001150static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
David S. Miller507c9b12011-12-03 17:50:45 -05001152 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001153 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
David S. Miller507c9b12011-12-03 17:50:45 -05001155 while (fn) {
1156 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001158 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return fn->right->leaf;
1160
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001161 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 return NULL;
1164}
1165
1166/*
1167 * Called to trim the tree of intermediate nodes when possible. "fn"
1168 * is the node we want to try and remove.
1169 */
1170
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001171static struct fib6_node *fib6_repair_tree(struct net *net,
1172 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 int children;
1175 int nstate;
1176 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001177 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 int iter = 0;
1179
1180 for (;;) {
1181 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1182 iter++;
1183
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001184 WARN_ON(fn->fn_flags & RTN_RTINFO);
1185 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1186 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 children = 0;
1189 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001190 if (fn->right)
1191 child = fn->right, children |= 1;
1192 if (fn->left)
1193 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001195 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#ifdef CONFIG_IPV6_SUBTREES
1197 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001198 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#endif
1200 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001201 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001203 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001204 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001205 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207#endif
1208 atomic_inc(&fn->leaf->rt6i_ref);
1209 return fn->parent;
1210 }
1211
1212 pn = fn->parent;
1213#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001214 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001215 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001216 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 nstate = FWS_L;
1218 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001219 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001221 if (pn->right == fn)
1222 pn->right = child;
1223 else if (pn->left == fn)
1224 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001226 else
1227 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228#endif
1229 if (child)
1230 child->parent = pn;
1231 nstate = FWS_R;
1232#ifdef CONFIG_IPV6_SUBTREES
1233 }
1234#endif
1235
1236 read_lock(&fib6_walker_lock);
1237 FOR_WALKERS(w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001238 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (w->root == fn) {
1240 w->root = w->node = NULL;
1241 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1242 } else if (w->node == fn) {
1243 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1244 w->node = pn;
1245 w->state = nstate;
1246 }
1247 } else {
1248 if (w->root == fn) {
1249 w->root = child;
1250 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1251 }
1252 if (w->node == fn) {
1253 w->node = child;
1254 if (children&2) {
1255 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001256 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 } else {
1258 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001259 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 }
1262 }
1263 }
1264 read_unlock(&fib6_walker_lock);
1265
1266 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001267 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return pn;
1269
1270 rt6_release(pn->leaf);
1271 pn->leaf = NULL;
1272 fn = pn;
1273 }
1274}
1275
1276static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001277 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001279 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001281 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 RT6_TRACE("fib6_del_route\n");
1284
1285 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001286 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001288 net->ipv6.rt6_stats->fib_rt_entries--;
1289 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
David S. Millerf11e6652007-03-24 20:36:25 -07001291 /* Reset round-robin state, if necessary */
1292 if (fn->rr_ptr == rt)
1293 fn->rr_ptr = NULL;
1294
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001295 /* Remove this entry from other siblings */
1296 if (rt->rt6i_nsiblings) {
1297 struct rt6_info *sibling, *next_sibling;
1298
1299 list_for_each_entry_safe(sibling, next_sibling,
1300 &rt->rt6i_siblings, rt6i_siblings)
1301 sibling->rt6i_nsiblings--;
1302 rt->rt6i_nsiblings = 0;
1303 list_del_init(&rt->rt6i_siblings);
1304 }
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Adjust walkers */
1307 read_lock(&fib6_walker_lock);
1308 FOR_WALKERS(w) {
1309 if (w->state == FWS_C && w->leaf == rt) {
1310 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001311 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001312 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 w->state = FWS_U;
1314 }
1315 }
1316 read_unlock(&fib6_walker_lock);
1317
Changli Gaod8d1f302010-06-10 23:31:35 -07001318 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001321 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001323 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001324 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 if (atomic_read(&rt->rt6i_ref) != 1) {
1328 /* This route is used as dummy address holder in some split
1329 * nodes. It is not leaked, but it still holds other resources,
1330 * which must be released in time. So, scan ascendant nodes
1331 * and replace dummy references to this route with references
1332 * to still alive ones.
1333 */
1334 while (fn) {
David S. Miller507c9b12011-12-03 17:50:45 -05001335 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001336 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 atomic_inc(&fn->leaf->rt6i_ref);
1338 rt6_release(rt);
1339 }
1340 fn = fn->parent;
1341 }
1342 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001343 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
Thomas Graf86872cb2006-08-22 00:01:08 -07001346 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 rt6_release(rt);
1348}
1349
Thomas Graf86872cb2006-08-22 00:01:08 -07001350int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001352 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct fib6_node *fn = rt->rt6i_node;
1354 struct rt6_info **rtp;
1355
1356#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001357 if (rt->dst.obsolete > 0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001358 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -ENOENT;
1360 }
1361#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001362 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -ENOENT;
1364
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001365 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
David S. Miller507c9b12011-12-03 17:50:45 -05001367 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001368 struct fib6_node *pn = fn;
1369#ifdef CONFIG_IPV6_SUBTREES
1370 /* clones of this route might be in another subtree */
1371 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001372 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001373 pn = pn->parent;
1374 pn = pn->parent;
1375 }
1376#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001377 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /*
1381 * Walk the leaf entries looking for ourself
1382 */
1383
Changli Gaod8d1f302010-06-10 23:31:35 -07001384 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001386 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388 }
1389 }
1390 return -ENOENT;
1391}
1392
1393/*
1394 * Tree traversal function.
1395 *
1396 * Certainly, it is not interrupt safe.
1397 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1398 * It means, that we can modify tree during walking
1399 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001400 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 *
1402 * It guarantees that every node will be traversed,
1403 * and that it will be traversed only once.
1404 *
1405 * Callback function w->func may return:
1406 * 0 -> continue walking.
1407 * positive value -> walking is suspended (used by tree dumps,
1408 * and probably by gc, if it will be split to several slices)
1409 * negative value -> terminate walking.
1410 *
1411 * The function itself returns:
1412 * 0 -> walk is complete.
1413 * >0 -> walk is incomplete (i.e. suspended)
1414 * <0 -> walk is terminated by an error.
1415 */
1416
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001417static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct fib6_node *fn, *pn;
1420
1421 for (;;) {
1422 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001423 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425
1426 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001427 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 w->state = FWS_C;
1429 w->leaf = fn->leaf;
1430 }
1431 switch (w->state) {
1432#ifdef CONFIG_IPV6_SUBTREES
1433 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001434 if (FIB6_SUBTREE(fn)) {
1435 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 continue;
1437 }
1438 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 case FWS_L:
1441 if (fn->left) {
1442 w->node = fn->left;
1443 w->state = FWS_INIT;
1444 continue;
1445 }
1446 w->state = FWS_R;
1447 case FWS_R:
1448 if (fn->right) {
1449 w->node = fn->right;
1450 w->state = FWS_INIT;
1451 continue;
1452 }
1453 w->state = FWS_C;
1454 w->leaf = fn->leaf;
1455 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001456 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001457 int err;
1458
Eric Dumazetfa809e22012-06-25 15:37:19 -07001459 if (w->skip) {
1460 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001461 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001462 }
1463
1464 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (err)
1466 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001467
1468 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 continue;
1470 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001471skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 w->state = FWS_U;
1473 case FWS_U:
1474 if (fn == w->root)
1475 return 0;
1476 pn = fn->parent;
1477 w->node = pn;
1478#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001479 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001480 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 w->state = FWS_L;
1482 continue;
1483 }
1484#endif
1485 if (pn->left == fn) {
1486 w->state = FWS_R;
1487 continue;
1488 }
1489 if (pn->right == fn) {
1490 w->state = FWS_C;
1491 w->leaf = w->node->leaf;
1492 continue;
1493 }
1494#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001495 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496#endif
1497 }
1498 }
1499}
1500
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001501static int fib6_walk(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 int res;
1504
1505 w->state = FWS_INIT;
1506 w->node = w->root;
1507
1508 fib6_walker_link(w);
1509 res = fib6_walk_continue(w);
1510 if (res <= 0)
1511 fib6_walker_unlink(w);
1512 return res;
1513}
1514
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001515static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 int res;
1518 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001519 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001520 struct nl_info info = {
1521 .nl_net = c->net,
1522 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001524 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1525 w->node->fn_sernum != c->sernum)
1526 w->node->fn_sernum = c->sernum;
1527
1528 if (!c->func) {
1529 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1530 w->leaf = NULL;
1531 return 0;
1532 }
1533
Changli Gaod8d1f302010-06-10 23:31:35 -07001534 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 res = c->func(rt, c->arg);
1536 if (res < 0) {
1537 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001538 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (res) {
1540#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001541 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1542 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543#endif
1544 continue;
1545 }
1546 return 0;
1547 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001548 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 w->leaf = rt;
1551 return 0;
1552}
1553
1554/*
1555 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001556 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * func is called on each route.
1558 * It may return -1 -> delete this route.
1559 * 0 -> continue walking
1560 *
1561 * prune==1 -> only immediate children of node (certainly,
1562 * ignoring pure split nodes) will be scanned.
1563 */
1564
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001565static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001566 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001567 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001569 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 c.w.root = root;
1572 c.w.func = fib6_clean_node;
1573 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001574 c.w.count = 0;
1575 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001577 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001579 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 fib6_walk(&c.w);
1582}
1583
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001584static void __fib6_clean_all(struct net *net,
1585 int (*func)(struct rt6_info *, void *),
1586 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001587{
Thomas Grafc71099a2006-08-04 23:20:06 -07001588 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001589 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001590 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001591
Patrick McHardy1b43af52006-08-10 23:11:17 -07001592 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001593 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001594 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001595 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001596 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001597 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001598 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001599 write_unlock_bh(&table->tb6_lock);
1600 }
1601 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001602 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001603}
1604
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001605void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1606 void *arg)
1607{
1608 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1612{
1613 if (rt->rt6i_flags & RTF_CACHE) {
1614 RT6_TRACE("pruning clone %p\n", rt);
1615 return -1;
1616 }
1617
1618 return 0;
1619}
1620
Duan Jiong163cd4e2014-05-09 13:31:43 +08001621static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001623 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1624 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001625}
1626
1627static void fib6_flush_trees(struct net *net)
1628{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001629 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001630
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001631 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/*
1635 * Garbage collection
1636 */
1637
1638static struct fib6_gc_args
1639{
1640 int timeout;
1641 int more;
1642} gc_args;
1643
1644static int fib6_age(struct rt6_info *rt, void *arg)
1645{
1646 unsigned long now = jiffies;
1647
1648 /*
1649 * check addrconf expiration here.
1650 * Routes are expired even if they are in use.
1651 *
1652 * Also age clones. Note, that clones are aged out
1653 * only if they are not in use now.
1654 */
1655
David S. Millerd1918542011-12-28 20:19:20 -05001656 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1657 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -1;
1660 }
1661 gc_args.more++;
1662 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001663 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1664 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 RT6_TRACE("aging clone %p\n", rt);
1666 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001667 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1668 struct neighbour *neigh;
1669 __u8 neigh_flags = 0;
1670
1671 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1672 if (neigh) {
1673 neigh_flags = neigh->flags;
1674 neigh_release(neigh);
1675 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001676 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001677 RT6_TRACE("purging route %p via non-router but gateway\n",
1678 rt);
1679 return -1;
1680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 gc_args.more++;
1683 }
1684
1685 return 0;
1686}
1687
1688static DEFINE_SPINLOCK(fib6_gc_lock);
1689
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001690void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Michal Kubeček49a18d82013-08-01 10:04:24 +02001692 unsigned long now;
1693
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001694 if (force) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 spin_lock_bh(&fib6_gc_lock);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001696 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1697 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001700 gc_args.timeout = expires ? (int)expires :
1701 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001703 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001704
Li RongQing0c3584d2013-12-27 16:32:38 +08001705 fib6_clean_all(net, fib6_age, NULL);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001706 now = jiffies;
1707 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001710 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001711 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001712 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001713 else
1714 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 spin_unlock_bh(&fib6_gc_lock);
1716}
1717
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001718static void fib6_gc_timer_cb(unsigned long arg)
1719{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001720 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001721}
1722
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001723static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001724{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001725 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1726
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001727 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001728
Benjamin Theryc5728722008-03-03 23:34:17 -08001729 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1730 if (!net->ipv6.rt6_stats)
1731 goto out_timer;
1732
Eric Dumazet10da66f2010-10-13 08:22:03 +00001733 /* Avoid false sharing : Use at least a full cache line */
1734 size = max_t(size_t, size, L1_CACHE_BYTES);
1735
1736 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001737 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001738 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001739
1740 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1741 GFP_KERNEL);
1742 if (!net->ipv6.fib6_main_tbl)
1743 goto out_fib_table_hash;
1744
1745 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001746 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001747 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1748 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001749 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001750
1751#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1752 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1753 GFP_KERNEL);
1754 if (!net->ipv6.fib6_local_tbl)
1755 goto out_fib6_main_tbl;
1756 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001757 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001758 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1759 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001760 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001761#endif
1762 fib6_tables_init(net);
1763
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001764 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001765
1766#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1767out_fib6_main_tbl:
1768 kfree(net->ipv6.fib6_main_tbl);
1769#endif
1770out_fib_table_hash:
1771 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001772out_rt6_stats:
1773 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001774out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001775 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001776}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001777
1778static void fib6_net_exit(struct net *net)
1779{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001780 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001781 del_timer_sync(&net->ipv6.ip6_fib_timer);
1782
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001783#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001784 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001785 kfree(net->ipv6.fib6_local_tbl);
1786#endif
David S. Miller8e773272012-06-11 00:01:52 -07001787 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001788 kfree(net->ipv6.fib6_main_tbl);
1789 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001790 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001791}
1792
1793static struct pernet_operations fib6_net_ops = {
1794 .init = fib6_net_init,
1795 .exit = fib6_net_exit,
1796};
1797
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001798int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001800 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1803 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001804 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001805 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001806 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001807 goto out;
1808
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001809 ret = register_pernet_subsys(&fib6_net_ops);
1810 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001811 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001812
1813 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1814 NULL);
1815 if (ret)
1816 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001817
1818 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001819out:
1820 return ret;
1821
David S. Millere8803b62012-06-16 01:12:19 -07001822out_unregister_subsys:
1823 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001824out_kmem_cache_create:
1825 kmem_cache_destroy(fib6_node_kmem);
1826 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
1829void fib6_gc_cleanup(void)
1830{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001831 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 kmem_cache_destroy(fib6_node_kmem);
1833}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001834
1835#ifdef CONFIG_PROC_FS
1836
1837struct ipv6_route_iter {
1838 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001839 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001840 loff_t skip;
1841 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001842 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001843};
1844
1845static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1846{
1847 struct rt6_info *rt = v;
1848 struct ipv6_route_iter *iter = seq->private;
1849
1850 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1851
1852#ifdef CONFIG_IPV6_SUBTREES
1853 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1854#else
1855 seq_puts(seq, "00000000000000000000000000000000 00 ");
1856#endif
1857 if (rt->rt6i_flags & RTF_GATEWAY)
1858 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1859 else
1860 seq_puts(seq, "00000000000000000000000000000000");
1861
1862 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1863 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1864 rt->dst.__use, rt->rt6i_flags,
1865 rt->dst.dev ? rt->dst.dev->name : "");
1866 iter->w.leaf = NULL;
1867 return 0;
1868}
1869
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001870static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001871{
1872 struct ipv6_route_iter *iter = w->args;
1873
1874 if (!iter->skip)
1875 return 1;
1876
1877 do {
1878 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1879 iter->skip--;
1880 if (!iter->skip && iter->w.leaf)
1881 return 1;
1882 } while (iter->w.leaf);
1883
1884 return 0;
1885}
1886
1887static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1888{
1889 memset(&iter->w, 0, sizeof(iter->w));
1890 iter->w.func = ipv6_route_yield;
1891 iter->w.root = &iter->tbl->tb6_root;
1892 iter->w.state = FWS_INIT;
1893 iter->w.node = iter->w.root;
1894 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001895 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001896 INIT_LIST_HEAD(&iter->w.lh);
1897 fib6_walker_link(&iter->w);
1898}
1899
1900static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1901 struct net *net)
1902{
1903 unsigned int h;
1904 struct hlist_node *node;
1905
1906 if (tbl) {
1907 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1908 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1909 } else {
1910 h = 0;
1911 node = NULL;
1912 }
1913
1914 while (!node && h < FIB6_TABLE_HASHSZ) {
1915 node = rcu_dereference_bh(
1916 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1917 }
1918 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1919}
1920
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001921static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1922{
1923 if (iter->sernum != iter->w.root->fn_sernum) {
1924 iter->sernum = iter->w.root->fn_sernum;
1925 iter->w.state = FWS_INIT;
1926 iter->w.node = iter->w.root;
1927 WARN_ON(iter->w.skip);
1928 iter->w.skip = iter->w.count;
1929 }
1930}
1931
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001932static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1933{
1934 int r;
1935 struct rt6_info *n;
1936 struct net *net = seq_file_net(seq);
1937 struct ipv6_route_iter *iter = seq->private;
1938
1939 if (!v)
1940 goto iter_table;
1941
1942 n = ((struct rt6_info *)v)->dst.rt6_next;
1943 if (n) {
1944 ++*pos;
1945 return n;
1946 }
1947
1948iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001949 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001950 read_lock(&iter->tbl->tb6_lock);
1951 r = fib6_walk_continue(&iter->w);
1952 read_unlock(&iter->tbl->tb6_lock);
1953 if (r > 0) {
1954 if (v)
1955 ++*pos;
1956 return iter->w.leaf;
1957 } else if (r < 0) {
1958 fib6_walker_unlink(&iter->w);
1959 return NULL;
1960 }
1961 fib6_walker_unlink(&iter->w);
1962
1963 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1964 if (!iter->tbl)
1965 return NULL;
1966
1967 ipv6_route_seq_setup_walk(iter);
1968 goto iter_table;
1969}
1970
1971static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1972 __acquires(RCU_BH)
1973{
1974 struct net *net = seq_file_net(seq);
1975 struct ipv6_route_iter *iter = seq->private;
1976
1977 rcu_read_lock_bh();
1978 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1979 iter->skip = *pos;
1980
1981 if (iter->tbl) {
1982 ipv6_route_seq_setup_walk(iter);
1983 return ipv6_route_seq_next(seq, NULL, pos);
1984 } else {
1985 return NULL;
1986 }
1987}
1988
1989static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1990{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001991 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001992 return w->node && !(w->state == FWS_U && w->node == w->root);
1993}
1994
1995static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1996 __releases(RCU_BH)
1997{
1998 struct ipv6_route_iter *iter = seq->private;
1999
2000 if (ipv6_route_iter_active(iter))
2001 fib6_walker_unlink(&iter->w);
2002
2003 rcu_read_unlock_bh();
2004}
2005
2006static const struct seq_operations ipv6_route_seq_ops = {
2007 .start = ipv6_route_seq_start,
2008 .next = ipv6_route_seq_next,
2009 .stop = ipv6_route_seq_stop,
2010 .show = ipv6_route_seq_show
2011};
2012
2013int ipv6_route_open(struct inode *inode, struct file *file)
2014{
2015 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2016 sizeof(struct ipv6_route_iter));
2017}
2018
2019#endif /* CONFIG_PROC_FS */