blob: c1197e167d3e48754c7dcadd7b220e4b7c0e95ec [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>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020035#include <net/lwtunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <net/ip6_fib.h>
38#include <net/ip6_route.h>
39
40#define RT6_DEBUG 2
41
42#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000043#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define RT6_TRACE(x...) do { ; } while (0)
46#endif
47
Wang Yufen437de072014-03-28 12:07:04 +080048static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020050struct fib6_cleaner {
51 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080052 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020054 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void *arg;
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifdef CONFIG_IPV6_SUBTREES
59#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
Duan Jiong163cd4e2014-05-09 13:31:43 +080064static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080065static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
66static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010067static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020068static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * A routing update causes an increase of the serial number on the
72 * affected subtree. This allows for cached routes to be asynchronously
73 * tested when modifications are made to the destination cache as a
74 * result of redirects, path MTU changes, etc.
75 */
76
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080077static void fib6_gc_timer_cb(unsigned long arg);
78
Michal Kubeček9a03cd82016-03-08 14:44:35 +010079#define FOR_WALKERS(net, w) \
80 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Michal Kubeček9a03cd82016-03-08 14:44:35 +010082static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070083{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010084 write_lock_bh(&net->ipv6.fib6_walker_lock);
85 list_add(&w->lh, &net->ipv6.fib6_walkers);
86 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070087}
88
Michal Kubeček9a03cd82016-03-08 14:44:35 +010089static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070090{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010091 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000092 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010093 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070094}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020095
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020096static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020098 int new, old;
99
100 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200101 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200102 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200103 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
104 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200105 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200108enum {
109 FIB6_NO_SERNUM_CHANGE = 0,
110};
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Auxiliary address test functions for the radix tree.
114 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900115 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * 64bit processors)
117 */
118
119/*
120 * test bit
121 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000122#if defined(__LITTLE_ENDIAN)
123# define BITOP_BE32_SWIZZLE (0x1F & ~7)
124#else
125# define BITOP_BE32_SWIZZLE 0
126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200128static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000130 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000131 /*
132 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800133 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000134 * is optimized version of
135 * htonl(1 << ((~fn_bit)&0x1F))
136 * See include/asm-generic/bitops/le.h.
137 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700138 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
139 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200142static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct fib6_node *fn;
145
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800146 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return fn;
149}
150
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200151static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 kmem_cache_free(fib6_node_kmem, fn);
154}
155
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700156static void rt6_rcu_free(struct rt6_info *rt)
157{
158 call_rcu(&rt->dst.rcu_head, dst_rcu_free);
159}
160
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700161static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
162{
163 int cpu;
164
165 if (!non_pcpu_rt->rt6i_pcpu)
166 return;
167
168 for_each_possible_cpu(cpu) {
169 struct rt6_info **ppcpu_rt;
170 struct rt6_info *pcpu_rt;
171
172 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
173 pcpu_rt = *ppcpu_rt;
174 if (pcpu_rt) {
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700175 rt6_rcu_free(pcpu_rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700176 *ppcpu_rt = NULL;
177 }
178 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700179
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700180 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700181 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700182}
183
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200184static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700186 if (atomic_dec_and_test(&rt->rt6i_ref)) {
187 rt6_free_pcpu(rt);
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700188 rt6_rcu_free(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800192static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700193{
194 unsigned int h;
195
Thomas Graf375216a2006-10-21 20:20:54 -0700196 /*
197 * Initialize table lock at a single place to give lockdep a key,
198 * tables aren't visible prior to being linked to the list.
199 */
200 rwlock_init(&tb->tb6_lock);
201
Neil Hormana33bc5c2009-07-30 18:52:15 -0700202 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700203
204 /*
205 * No protection necessary, this is the only list mutatation
206 * operation, tables never disappear once they exist.
207 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800208 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700209}
210
211#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800212
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800213static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700214{
215 struct fib6_table *table;
216
217 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500218 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700219 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800220 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700222 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700223 }
224
225 return table;
226}
227
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800228struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700229{
230 struct fib6_table *tb;
231
232 if (id == 0)
233 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800234 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700235 if (tb)
236 return tb;
237
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800238 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500239 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700241
242 return tb;
243}
David Ahernb3b46632016-05-04 21:46:12 -0700244EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700245
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800246struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700247{
248 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800249 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700250 unsigned int h;
251
252 if (id == 0)
253 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700254 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700255 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700258 if (tb->tb6_id == id) {
259 rcu_read_unlock();
260 return tb;
261 }
262 }
263 rcu_read_unlock();
264
265 return NULL;
266}
David Ahernc4850682015-10-12 11:47:08 -0700267EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700268
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000269static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700270{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800271 fib6_link_table(net, net->ipv6.fib6_main_tbl);
272 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700273}
Thomas Grafc71099a2006-08-04 23:20:06 -0700274#else
275
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800276struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700277{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800278 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700279}
280
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800281struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700282{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800283 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700284}
285
David S. Miller4c9483b2011-03-12 16:22:43 -0500286struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800287 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700288{
lucienab997ad2015-10-23 15:36:53 +0800289 struct rt6_info *rt;
290
291 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
292 if (rt->rt6i_flags & RTF_REJECT &&
293 rt->dst.error == -EAGAIN) {
294 ip6_rt_put(rt);
295 rt = net->ipv6.ip6_null_entry;
296 dst_hold(&rt->dst);
297 }
298
299 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700300}
301
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000302static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700303{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800304 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700305}
306
307#endif
308
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200309static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700310{
311 int res;
312 struct rt6_info *rt;
313
Changli Gaod8d1f302010-06-10 23:31:35 -0700314 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700315 res = rt6_dump_route(rt, w->args);
316 if (res < 0) {
317 /* Frame is full, suspend walking */
318 w->leaf = rt;
319 return 1;
320 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800321
322 /* Multipath routes are dumped in one route with the
323 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
324 * last sibling of this route (no need to dump the
325 * sibling routes again)
326 */
327 if (rt->rt6i_nsiblings)
328 rt = list_last_entry(&rt->rt6i_siblings,
329 struct rt6_info,
330 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700331 }
332 w->leaf = NULL;
333 return 0;
334}
335
336static void fib6_dump_end(struct netlink_callback *cb)
337{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100338 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200339 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700340
341 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800342 if (cb->args[4]) {
343 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100344 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800345 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700346 cb->args[2] = 0;
347 kfree(w);
348 }
Wang Yufen437de072014-03-28 12:07:04 +0800349 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700350 cb->args[1] = 3;
351}
352
353static int fib6_dump_done(struct netlink_callback *cb)
354{
355 fib6_dump_end(cb);
356 return cb->done ? cb->done(cb) : 0;
357}
358
359static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
360 struct netlink_callback *cb)
361{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100362 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200363 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700364 int res;
365
366 w = (void *)cb->args[2];
367 w->root = &table->tb6_root;
368
369 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000370 w->count = 0;
371 w->skip = 0;
372
Patrick McHardy1b43af52006-08-10 23:11:17 -0700373 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100374 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700375 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000376 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700377 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000378 cb->args[5] = w->root->fn_sernum;
379 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700380 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000381 if (cb->args[5] != w->root->fn_sernum) {
382 /* Begin at the root if the tree changed */
383 cb->args[5] = w->root->fn_sernum;
384 w->state = FWS_INIT;
385 w->node = w->root;
386 w->skip = w->count;
387 } else
388 w->skip = 0;
389
Patrick McHardy1b43af52006-08-10 23:11:17 -0700390 read_lock_bh(&table->tb6_lock);
391 res = fib6_walk_continue(w);
392 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800393 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100394 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800395 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700396 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700397 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800398
Patrick McHardy1b43af52006-08-10 23:11:17 -0700399 return res;
400}
401
Thomas Grafc127ea22007-03-22 11:58:32 -0700402static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700403{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900404 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700405 unsigned int h, s_h;
406 unsigned int e = 0, s_e;
407 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200408 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700409 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800410 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700411 int res = 0;
412
413 s_h = cb->args[0];
414 s_e = cb->args[1];
415
416 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500417 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700418 /* New dump:
419 *
420 * 1. hook callback destructor.
421 */
422 cb->args[3] = (long)cb->done;
423 cb->done = fib6_dump_done;
424
425 /*
426 * 2. allocate and initialize walker.
427 */
428 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500429 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700430 return -ENOMEM;
431 w->func = fib6_dump_node;
432 cb->args[2] = (long)w;
433 }
434
435 arg.skb = skb;
436 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700437 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700438 w->args = &arg;
439
Eric Dumazete67f88d2011-04-27 22:56:07 +0000440 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700441 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700442 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800443 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800444 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700445 if (e < s_e)
446 goto next;
447 res = fib6_dump_table(tb, skb, cb);
448 if (res != 0)
449 goto out;
450next:
451 e++;
452 }
453 }
454out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000455 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700456 cb->args[1] = e;
457 cb->args[0] = h;
458
459 res = res < 0 ? res : skb->len;
460 if (res <= 0)
461 fib6_dump_end(cb);
462 return res;
463}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/*
466 * Routing Table
467 *
468 * return the appropriate node for a routing tree "add" operation
469 * by either creating and inserting or by returning an existing
470 * node.
471 */
472
fan.du9225b232013-07-22 14:21:09 +0800473static struct fib6_node *fib6_add_1(struct fib6_node *root,
474 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000475 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600476 int replace_required, int sernum,
477 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct fib6_node *fn, *in, *ln;
480 struct fib6_node *pn = NULL;
481 struct rt6key *key;
482 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900483 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 RT6_TRACE("fib6_add_1\n");
486
487 /* insert node in tree */
488
489 fn = root;
490
491 do {
492 key = (struct rt6key *)((u8 *)fn->leaf + offset);
493
494 /*
495 * Prefix match
496 */
497 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000498 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000499 if (!allow_create) {
500 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000501 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000502 return ERR_PTR(-ENOENT);
503 }
Joe Perchesf3213832012-05-15 14:11:53 +0000504 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000507 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /*
510 * Exact match ?
511 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (plen == fn->fn_bit) {
514 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500515 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 rt6_release(fn->leaf);
517 fn->leaf = NULL;
518 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return fn;
523 }
524
525 /*
526 * We have more bits to go
527 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Try to walk down on tree. */
530 fn->fn_sernum = sernum;
531 dir = addr_bit_set(addr, fn->fn_bit);
532 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800533 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } while (fn);
535
Matti Vaittinen14df0152011-11-16 21:18:02 +0000536 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000537 /* We should not create new node because
538 * NLM_F_REPLACE was specified without NLM_F_CREATE
539 * I assume it is safe to require NLM_F_CREATE when
540 * REPLACE flag is used! Later we may want to remove the
541 * check for replace_required, because according
542 * to netlink specification, NLM_F_CREATE
543 * MUST be specified if new route is created.
544 * That would keep IPv6 consistent with IPv4
545 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000546 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000547 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000548 return ERR_PTR(-ENOENT);
549 }
Joe Perchesf3213832012-05-15 14:11:53 +0000550 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
553 * We walked to the bottom of tree.
554 * Create new leaf node without children.
555 */
556
557 ln = node_alloc();
558
David S. Miller507c9b12011-12-03 17:50:45 -0500559 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000560 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ln->parent = pn;
564 ln->fn_sernum = sernum;
565
566 if (dir)
567 pn->right = ln;
568 else
569 pn->left = ln;
570
571 return ln;
572
573
574insert_above:
575 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900576 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * we have a less significant route.
578 * we've to insert an intermediate node on the list
579 * this new node will point to the one we need to create
580 * and the current
581 */
582
583 pn = fn->parent;
584
585 /* find 1st bit in difference between the 2 addrs.
586
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800587 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 but if it is >= plen, the value is ignored in any case.
589 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900590
fan.du9225b232013-07-22 14:21:09 +0800591 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900593 /*
594 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * / \
596 * (new leaf node)[ln] (old node)[fn]
597 */
598 if (plen > bit) {
599 in = node_alloc();
600 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900601
David S. Miller507c9b12011-12-03 17:50:45 -0500602 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (in)
604 node_free(in);
605 if (ln)
606 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000607 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900610 /*
611 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * RTN_RTINFO will
613 * be off since that an address that chooses one of
614 * the branches would not match less specific routes
615 * in the other branch
616 */
617
618 in->fn_bit = bit;
619
620 in->parent = pn;
621 in->leaf = fn->leaf;
622 atomic_inc(&in->leaf->rt6i_ref);
623
624 in->fn_sernum = sernum;
625
626 /* update parent pointer */
627 if (dir)
628 pn->right = in;
629 else
630 pn->left = in;
631
632 ln->fn_bit = plen;
633
634 ln->parent = in;
635 fn->parent = in;
636
637 ln->fn_sernum = sernum;
638
639 if (addr_bit_set(addr, bit)) {
640 in->right = ln;
641 in->left = fn;
642 } else {
643 in->left = ln;
644 in->right = fn;
645 }
646 } else { /* plen <= bit */
647
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900648 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * (new leaf node)[ln]
650 * / \
651 * (old node)[fn] NULL
652 */
653
654 ln = node_alloc();
655
David S. Miller507c9b12011-12-03 17:50:45 -0500656 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000657 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 ln->fn_bit = plen;
660
661 ln->parent = pn;
662
663 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (dir)
666 pn->right = ln;
667 else
668 pn->left = ln;
669
670 if (addr_bit_set(&key->addr, plen))
671 ln->right = fn;
672 else
673 ln->left = fn;
674
675 fn->parent = ln;
676 }
677 return ln;
678}
679
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200680static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200681{
682 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
683 RTF_GATEWAY;
684}
685
Florian Westphale715b6d2015-01-05 23:57:44 +0100686static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100687{
Florian Westphale715b6d2015-01-05 23:57:44 +0100688 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100689
Florian Westphale715b6d2015-01-05 23:57:44 +0100690 for (i = 0; i < RTAX_MAX; i++) {
691 if (test_bit(i, mxc->mx_valid))
692 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100693 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100694}
695
696static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
697{
698 if (!mxc->mx)
699 return 0;
700
701 if (dst->flags & DST_HOST) {
702 u32 *mp = dst_metrics_write_ptr(dst);
703
704 if (unlikely(!mp))
705 return -ENOMEM;
706
707 fib6_copy_metrics(mp, mxc);
708 } else {
709 dst_init_metrics(dst, mxc->mx, false);
710
711 /* We've stolen mx now. */
712 mxc->mx = NULL;
713 }
714
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100715 return 0;
716}
717
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100718static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
719 struct net *net)
720{
721 if (atomic_read(&rt->rt6i_ref) != 1) {
722 /* This route is used as dummy address holder in some split
723 * nodes. It is not leaked, but it still holds other resources,
724 * which must be released in time. So, scan ascendant nodes
725 * and replace dummy references to this route with references
726 * to still alive ones.
727 */
728 while (fn) {
729 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
730 fn->leaf = fib6_find_prefix(net, fn);
731 atomic_inc(&fn->leaf->rt6i_ref);
732 rt6_release(rt);
733 }
734 fn = fn->parent;
735 }
736 /* No more references are possible at this point. */
737 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
738 }
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
742 * Insert routing information in a node.
743 */
744
745static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100746 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct rt6_info *iter = NULL;
749 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200750 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500751 int replace = (info->nlh &&
752 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
753 int add = (!info->nlh ||
754 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000755 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200756 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200757 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100758 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David Ahern1f5e29c2017-01-31 16:51:37 -0800760 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
761 nlflags |= NLM_F_APPEND;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ins = &fn->leaf;
764
David S. Miller507c9b12011-12-03 17:50:45 -0500765 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Search for duplicates
768 */
769
770 if (iter->rt6i_metric == rt->rt6i_metric) {
771 /*
772 * Same priority level
773 */
David S. Miller507c9b12011-12-03 17:50:45 -0500774 if (info->nlh &&
775 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000776 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200777
778 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000779 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200780 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
781 found++;
782 break;
783 }
784 if (rt_can_ecmp)
785 fallback_ins = fallback_ins ?: ins;
786 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David S. Millerd1918542011-12-28 20:19:20 -0500789 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 iter->rt6i_idev == rt->rt6i_idev &&
791 ipv6_addr_equal(&iter->rt6i_gateway,
792 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000793 if (rt->rt6i_nsiblings)
794 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500795 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000797 if (!(rt->rt6i_flags & RTF_EXPIRES))
798 rt6_clean_expires(iter);
799 else
800 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700801 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EEXIST;
803 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000804 /* If we have the same destination and the same metric,
805 * but not the same gateway, then the route we try to
806 * add is sibling to this route, increment our counter
807 * of siblings, and later we will add our route to the
808 * list.
809 * Only static routes (which don't have flag
810 * RTF_EXPIRES) are used for ECMPv6.
811 *
812 * To avoid long list, we only had siblings if the
813 * route have a gateway.
814 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200815 if (rt_can_ecmp &&
816 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000817 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
820 if (iter->rt6i_metric > rt->rt6i_metric)
821 break;
822
Michal Kubeček27596472015-05-18 20:54:00 +0200823next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700824 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Michal Kubeček27596472015-05-18 20:54:00 +0200827 if (fallback_ins && !found) {
828 /* No ECMP-able route found, replace first non-ECMP one */
829 ins = fallback_ins;
830 iter = *ins;
831 found++;
832 }
833
David S. Millerf11e6652007-03-24 20:36:25 -0700834 /* Reset round-robin state, if necessary */
835 if (ins == &fn->leaf)
836 fn->rr_ptr = NULL;
837
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000838 /* Link this route to others same route. */
839 if (rt->rt6i_nsiblings) {
840 unsigned int rt6i_nsiblings;
841 struct rt6_info *sibling, *temp_sibling;
842
843 /* Find the first route that have the same metric */
844 sibling = fn->leaf;
845 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200846 if (sibling->rt6i_metric == rt->rt6i_metric &&
847 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000848 list_add_tail(&rt->rt6i_siblings,
849 &sibling->rt6i_siblings);
850 break;
851 }
852 sibling = sibling->dst.rt6_next;
853 }
854 /* For each sibling in the list, increment the counter of
855 * siblings. BUG() if counters does not match, list of siblings
856 * is broken!
857 */
858 rt6i_nsiblings = 0;
859 list_for_each_entry_safe(sibling, temp_sibling,
860 &rt->rt6i_siblings, rt6i_siblings) {
861 sibling->rt6i_nsiblings++;
862 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
863 rt6i_nsiblings++;
864 }
865 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * insert node
870 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000871 if (!replace) {
872 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000873 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000875add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200876 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100877 err = fib6_commit_metrics(&rt->dst, mxc);
878 if (err)
879 return err;
880
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000881 rt->dst.rt6_next = iter;
882 *ins = rt;
883 rt->rt6i_node = fn;
884 atomic_inc(&rt->rt6i_ref);
David Ahern3b1137f2017-02-02 12:37:10 -0800885 if (!info->skip_notify)
886 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000887 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David S. Miller507c9b12011-12-03 17:50:45 -0500889 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000890 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
891 fn->fn_flags |= RTN_RTINFO;
892 }
893
894 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200895 int nsiblings;
896
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000897 if (!found) {
898 if (add)
899 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000900 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000901 return -ENOENT;
902 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100903
904 err = fib6_commit_metrics(&rt->dst, mxc);
905 if (err)
906 return err;
907
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000908 *ins = rt;
909 rt->rt6i_node = fn;
910 rt->dst.rt6_next = iter->dst.rt6_next;
911 atomic_inc(&rt->rt6i_ref);
David Ahern3b1137f2017-02-02 12:37:10 -0800912 if (!info->skip_notify)
913 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -0500914 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000915 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
916 fn->fn_flags |= RTN_RTINFO;
917 }
Michal Kubeček27596472015-05-18 20:54:00 +0200918 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100919 fib6_purge_rt(iter, fn, info->nl_net);
920 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200921
922 if (nsiblings) {
923 /* Replacing an ECMP route, remove all siblings */
924 ins = &rt->dst.rt6_next;
925 iter = *ins;
926 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +0100927 if (iter->rt6i_metric > rt->rt6i_metric)
928 break;
Michal Kubeček27596472015-05-18 20:54:00 +0200929 if (rt6_qualify_for_ecmp(iter)) {
930 *ins = iter->dst.rt6_next;
931 fib6_purge_rt(iter, fn, info->nl_net);
932 rt6_release(iter);
933 nsiblings--;
934 } else {
935 ins = &iter->dst.rt6_next;
936 }
937 iter = *ins;
938 }
939 WARN_ON(nsiblings != 0);
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 return 0;
944}
945
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200946static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700948 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500949 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700950 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700951 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800954void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700956 if (!timer_pending(&net->ipv6.ip6_fib_timer))
957 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700958 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * Add routing information to the routing tree.
963 * <destination addr>/<source addr>
964 * with source addr info in sub-trees
965 */
966
Florian Westphale715b6d2015-01-05 23:57:44 +0100967int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -0600968 struct nl_info *info, struct mx6_config *mxc,
969 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700971 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000973 int allow_create = 1;
974 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200975 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500976
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700977 if (WARN_ON_ONCE((rt->dst.flags & DST_NOCACHE) &&
978 !atomic_read(&rt->dst.__refcnt)))
979 return -EINVAL;
980
David S. Miller507c9b12011-12-03 17:50:45 -0500981 if (info->nlh) {
982 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000983 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500984 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000985 replace_required = 1;
986 }
987 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000988 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
fan.du9225b232013-07-22 14:21:09 +0800990 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
991 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600992 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000993 if (IS_ERR(fn)) {
994 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200995 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700999 pn = fn;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001#ifdef CONFIG_IPV6_SUBTREES
1002 if (rt->rt6i_src.plen) {
1003 struct fib6_node *sn;
1004
David S. Miller507c9b12011-12-03 17:50:45 -05001005 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct fib6_node *sfn;
1007
1008 /*
1009 * Create subtree.
1010 *
1011 * fn[main tree]
1012 * |
1013 * sfn[subtree root]
1014 * \
1015 * sn[new leaf node]
1016 */
1017
1018 /* Create subtree root node */
1019 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001020 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto st_failure;
1022
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001023 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1024 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001026 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /* Now add the first leaf node to new subtree */
1029
1030 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001031 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001032 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001033 allow_create, replace_required, sernum,
1034 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001036 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* If it is failed, discard just allocated
1038 root, and then (in st_failure) stale node
1039 in main tree.
1040 */
1041 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001042 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto st_failure;
1044 }
1045
1046 /* Now link new subtree to main tree */
1047 sfn->parent = fn;
1048 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 } else {
1050 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001051 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001052 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001053 allow_create, replace_required, sernum,
1054 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001056 if (IS_ERR(sn)) {
1057 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061
David S. Miller507c9b12011-12-03 17:50:45 -05001062 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001063 fn->leaf = rt;
1064 atomic_inc(&rt->rt6i_ref);
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 fn = sn;
1067 }
1068#endif
1069
Florian Westphale715b6d2015-01-05 23:57:44 +01001070 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001071 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001072 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001073 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001074 fib6_prune_clones(info->nl_net, pn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001075 rt->dst.flags &= ~DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001079 if (err) {
1080#ifdef CONFIG_IPV6_SUBTREES
1081 /*
1082 * If fib6_add_1 has cleared the old leaf pointer in the
1083 * super-tree leaf node we have to find a new one for it.
1084 */
David S. Miller3c051232008-04-18 01:46:19 -07001085 if (pn != fn && pn->leaf == rt) {
1086 pn->leaf = NULL;
1087 atomic_dec(&rt->rt6i_ref);
1088 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001089 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001090 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001091#if RT6_DEBUG >= 2
1092 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001093 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001094 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001095 }
1096#endif
1097 atomic_inc(&pn->leaf->rt6i_ref);
1098 }
1099#endif
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001100 if (!(rt->dst.flags & DST_NOCACHE))
1101 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return err;
1104
1105#ifdef CONFIG_IPV6_SUBTREES
1106 /* Subtree creation failed, probably main tree node
1107 is orphan. If it is, shoot it.
1108 */
1109st_failure:
1110 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001111 fib6_repair_tree(info->nl_net, fn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001112 if (!(rt->dst.flags & DST_NOCACHE))
1113 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return err;
1115#endif
1116}
1117
1118/*
1119 * Routing tree lookup
1120 *
1121 */
1122
1123struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001124 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001125 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126};
1127
Wang Yufen437de072014-03-28 12:07:04 +08001128static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1129 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001132 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001134 if (unlikely(args->offset == 0))
1135 return NULL;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * Descend on a tree
1139 */
1140
1141 fn = root;
1142
1143 for (;;) {
1144 struct fib6_node *next;
1145
1146 dir = addr_bit_set(args->addr, fn->fn_bit);
1147
1148 next = dir ? fn->right : fn->left;
1149
1150 if (next) {
1151 fn = next;
1152 continue;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 }
1156
David S. Miller507c9b12011-12-03 17:50:45 -05001157 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001158 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct rt6key *key;
1160
1161 key = (struct rt6key *) ((u8 *) fn->leaf +
1162 args->offset);
1163
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001164 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1165#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001166 if (fn->subtree) {
1167 struct fib6_node *sfn;
1168 sfn = fib6_lookup_1(fn->subtree,
1169 args + 1);
1170 if (!sfn)
1171 goto backtrack;
1172 fn = sfn;
1173 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001174#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001175 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001176 return fn;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001179#ifdef CONFIG_IPV6_SUBTREES
1180backtrack:
1181#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001182 if (fn->fn_flags & RTN_ROOT)
1183 break;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 fn = fn->parent;
1186 }
1187
1188 return NULL;
1189}
1190
Wang Yufen437de072014-03-28 12:07:04 +08001191struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1192 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001195 struct lookup_args args[] = {
1196 {
1197 .offset = offsetof(struct rt6_info, rt6i_dst),
1198 .addr = daddr,
1199 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001201 {
1202 .offset = offsetof(struct rt6_info, rt6i_src),
1203 .addr = saddr,
1204 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001206 {
1207 .offset = 0, /* sentinel */
1208 }
1209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001211 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001212 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 fn = root;
1214
1215 return fn;
1216}
1217
1218/*
1219 * Get node with specified destination prefix (and source prefix,
1220 * if subtrees are used)
1221 */
1222
1223
Wang Yufen437de072014-03-28 12:07:04 +08001224static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1225 const struct in6_addr *addr,
1226 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct fib6_node *fn;
1229
1230 for (fn = root; fn ; ) {
1231 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1232
1233 /*
1234 * Prefix match
1235 */
1236 if (plen < fn->fn_bit ||
1237 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1238 return NULL;
1239
1240 if (plen == fn->fn_bit)
1241 return fn;
1242
1243 /*
1244 * We have more bits to go
1245 */
1246 if (addr_bit_set(addr, fn->fn_bit))
1247 fn = fn->right;
1248 else
1249 fn = fn->left;
1250 }
1251 return NULL;
1252}
1253
Wang Yufen437de072014-03-28 12:07:04 +08001254struct fib6_node *fib6_locate(struct fib6_node *root,
1255 const struct in6_addr *daddr, int dst_len,
1256 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct fib6_node *fn;
1259
1260 fn = fib6_locate_1(root, daddr, dst_len,
1261 offsetof(struct rt6_info, rt6i_dst));
1262
1263#ifdef CONFIG_IPV6_SUBTREES
1264 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001265 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001266 if (fn && fn->subtree)
1267 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 offsetof(struct rt6_info, rt6i_src));
1269 }
1270#endif
1271
David S. Miller507c9b12011-12-03 17:50:45 -05001272 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return fn;
1274
1275 return NULL;
1276}
1277
1278
1279/*
1280 * Deletion
1281 *
1282 */
1283
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001284static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
David S. Miller507c9b12011-12-03 17:50:45 -05001286 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001287 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
David S. Miller507c9b12011-12-03 17:50:45 -05001289 while (fn) {
1290 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001292 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return fn->right->leaf;
1294
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001295 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 return NULL;
1298}
1299
1300/*
1301 * Called to trim the tree of intermediate nodes when possible. "fn"
1302 * is the node we want to try and remove.
1303 */
1304
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001305static struct fib6_node *fib6_repair_tree(struct net *net,
1306 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int children;
1309 int nstate;
1310 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001311 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int iter = 0;
1313
1314 for (;;) {
1315 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1316 iter++;
1317
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001318 WARN_ON(fn->fn_flags & RTN_RTINFO);
1319 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001320 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 children = 0;
1323 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001324 if (fn->right)
1325 child = fn->right, children |= 1;
1326 if (fn->left)
1327 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001329 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#ifdef CONFIG_IPV6_SUBTREES
1331 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001332 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333#endif
1334 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001335 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001337 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001338 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001339 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341#endif
1342 atomic_inc(&fn->leaf->rt6i_ref);
1343 return fn->parent;
1344 }
1345
1346 pn = fn->parent;
1347#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001348 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001349 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001350 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 nstate = FWS_L;
1352 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001353 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001355 if (pn->right == fn)
1356 pn->right = child;
1357 else if (pn->left == fn)
1358 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001360 else
1361 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362#endif
1363 if (child)
1364 child->parent = pn;
1365 nstate = FWS_R;
1366#ifdef CONFIG_IPV6_SUBTREES
1367 }
1368#endif
1369
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001370 read_lock(&net->ipv6.fib6_walker_lock);
1371 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001372 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (w->root == fn) {
1374 w->root = w->node = NULL;
1375 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1376 } else if (w->node == fn) {
1377 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1378 w->node = pn;
1379 w->state = nstate;
1380 }
1381 } else {
1382 if (w->root == fn) {
1383 w->root = child;
1384 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1385 }
1386 if (w->node == fn) {
1387 w->node = child;
1388 if (children&2) {
1389 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001390 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } else {
1392 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001393 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395 }
1396 }
1397 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001398 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001401 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return pn;
1403
1404 rt6_release(pn->leaf);
1405 pn->leaf = NULL;
1406 fn = pn;
1407 }
1408}
1409
1410static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001411 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001413 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001415 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 RT6_TRACE("fib6_del_route\n");
1418
1419 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001420 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001422 net->ipv6.rt6_stats->fib_rt_entries--;
1423 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
David S. Millerf11e6652007-03-24 20:36:25 -07001425 /* Reset round-robin state, if necessary */
1426 if (fn->rr_ptr == rt)
1427 fn->rr_ptr = NULL;
1428
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001429 /* Remove this entry from other siblings */
1430 if (rt->rt6i_nsiblings) {
1431 struct rt6_info *sibling, *next_sibling;
1432
1433 list_for_each_entry_safe(sibling, next_sibling,
1434 &rt->rt6i_siblings, rt6i_siblings)
1435 sibling->rt6i_nsiblings--;
1436 rt->rt6i_nsiblings = 0;
1437 list_del_init(&rt->rt6i_siblings);
1438 }
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001441 read_lock(&net->ipv6.fib6_walker_lock);
1442 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (w->state == FWS_C && w->leaf == rt) {
1444 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001445 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001446 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 w->state = FWS_U;
1448 }
1449 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001450 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Changli Gaod8d1f302010-06-10 23:31:35 -07001452 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001455 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001457 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001458 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001461 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
David Ahern16a16cd2017-02-02 12:37:11 -08001463 if (!info->skip_notify)
1464 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 rt6_release(rt);
1466}
1467
Thomas Graf86872cb2006-08-22 00:01:08 -07001468int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001470 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct fib6_node *fn = rt->rt6i_node;
1472 struct rt6_info **rtp;
1473
1474#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001475 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001476 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -ENOENT;
1478 }
1479#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001480 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return -ENOENT;
1482
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001483 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
David S. Miller507c9b12011-12-03 17:50:45 -05001485 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001486 struct fib6_node *pn = fn;
1487#ifdef CONFIG_IPV6_SUBTREES
1488 /* clones of this route might be in another subtree */
1489 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001490 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001491 pn = pn->parent;
1492 pn = pn->parent;
1493 }
1494#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001495 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /*
1499 * Walk the leaf entries looking for ourself
1500 */
1501
Changli Gaod8d1f302010-06-10 23:31:35 -07001502 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001504 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return 0;
1506 }
1507 }
1508 return -ENOENT;
1509}
1510
1511/*
1512 * Tree traversal function.
1513 *
1514 * Certainly, it is not interrupt safe.
1515 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1516 * It means, that we can modify tree during walking
1517 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001518 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *
1520 * It guarantees that every node will be traversed,
1521 * and that it will be traversed only once.
1522 *
1523 * Callback function w->func may return:
1524 * 0 -> continue walking.
1525 * positive value -> walking is suspended (used by tree dumps,
1526 * and probably by gc, if it will be split to several slices)
1527 * negative value -> terminate walking.
1528 *
1529 * The function itself returns:
1530 * 0 -> walk is complete.
1531 * >0 -> walk is incomplete (i.e. suspended)
1532 * <0 -> walk is terminated by an error.
1533 */
1534
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001535static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct fib6_node *fn, *pn;
1538
1539 for (;;) {
1540 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001541 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 0;
1543
1544 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001545 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 w->state = FWS_C;
1547 w->leaf = fn->leaf;
1548 }
1549 switch (w->state) {
1550#ifdef CONFIG_IPV6_SUBTREES
1551 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001552 if (FIB6_SUBTREE(fn)) {
1553 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 continue;
1555 }
1556 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 case FWS_L:
1559 if (fn->left) {
1560 w->node = fn->left;
1561 w->state = FWS_INIT;
1562 continue;
1563 }
1564 w->state = FWS_R;
1565 case FWS_R:
1566 if (fn->right) {
1567 w->node = fn->right;
1568 w->state = FWS_INIT;
1569 continue;
1570 }
1571 w->state = FWS_C;
1572 w->leaf = fn->leaf;
1573 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001574 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001575 int err;
1576
Eric Dumazetfa809e22012-06-25 15:37:19 -07001577 if (w->skip) {
1578 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001579 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001580 }
1581
1582 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (err)
1584 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001585
1586 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 continue;
1588 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001589skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 w->state = FWS_U;
1591 case FWS_U:
1592 if (fn == w->root)
1593 return 0;
1594 pn = fn->parent;
1595 w->node = pn;
1596#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001597 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001598 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 w->state = FWS_L;
1600 continue;
1601 }
1602#endif
1603 if (pn->left == fn) {
1604 w->state = FWS_R;
1605 continue;
1606 }
1607 if (pn->right == fn) {
1608 w->state = FWS_C;
1609 w->leaf = w->node->leaf;
1610 continue;
1611 }
1612#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001613 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614#endif
1615 }
1616 }
1617}
1618
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001619static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 int res;
1622
1623 w->state = FWS_INIT;
1624 w->node = w->root;
1625
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001626 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 res = fib6_walk_continue(w);
1628 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001629 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return res;
1631}
1632
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001633static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int res;
1636 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001637 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001638 struct nl_info info = {
1639 .nl_net = c->net,
1640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001642 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1643 w->node->fn_sernum != c->sernum)
1644 w->node->fn_sernum = c->sernum;
1645
1646 if (!c->func) {
1647 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1648 w->leaf = NULL;
1649 return 0;
1650 }
1651
Changli Gaod8d1f302010-06-10 23:31:35 -07001652 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 res = c->func(rt, c->arg);
1654 if (res < 0) {
1655 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001656 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (res) {
1658#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001659 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1660 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661#endif
1662 continue;
1663 }
1664 return 0;
1665 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001666 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 w->leaf = rt;
1669 return 0;
1670}
1671
1672/*
1673 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001674 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * func is called on each route.
1676 * It may return -1 -> delete this route.
1677 * 0 -> continue walking
1678 *
1679 * prune==1 -> only immediate children of node (certainly,
1680 * ignoring pure split nodes) will be scanned.
1681 */
1682
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001683static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001684 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001685 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001687 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 c.w.root = root;
1690 c.w.func = fib6_clean_node;
1691 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001692 c.w.count = 0;
1693 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001695 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001697 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001699 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001702static void __fib6_clean_all(struct net *net,
1703 int (*func)(struct rt6_info *, void *),
1704 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001705{
Thomas Grafc71099a2006-08-04 23:20:06 -07001706 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001707 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001708 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001709
Patrick McHardy1b43af52006-08-10 23:11:17 -07001710 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001711 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001712 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001713 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001714 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001715 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001716 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001717 write_unlock_bh(&table->tb6_lock);
1718 }
1719 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001720 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001721}
1722
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001723void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1724 void *arg)
1725{
1726 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1730{
1731 if (rt->rt6i_flags & RTF_CACHE) {
1732 RT6_TRACE("pruning clone %p\n", rt);
1733 return -1;
1734 }
1735
1736 return 0;
1737}
1738
Duan Jiong163cd4e2014-05-09 13:31:43 +08001739static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001741 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1742 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001743}
1744
1745static void fib6_flush_trees(struct net *net)
1746{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001747 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001748
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001749 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752/*
1753 * Garbage collection
1754 */
1755
Michal Kubeček3570df92016-03-08 14:44:25 +01001756struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 int timeout;
1759 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001760};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762static int fib6_age(struct rt6_info *rt, void *arg)
1763{
Michal Kubeček3570df92016-03-08 14:44:25 +01001764 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unsigned long now = jiffies;
1766
1767 /*
1768 * check addrconf expiration here.
1769 * Routes are expired even if they are in use.
1770 *
1771 * Also age clones. Note, that clones are aged out
1772 * only if they are not in use now.
1773 */
1774
David S. Millerd1918542011-12-28 20:19:20 -05001775 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1776 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return -1;
1779 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001780 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001782 if (atomic_read(&rt->dst.__refcnt) == 0 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001783 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 RT6_TRACE("aging clone %p\n", rt);
1785 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001786 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1787 struct neighbour *neigh;
1788 __u8 neigh_flags = 0;
1789
1790 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1791 if (neigh) {
1792 neigh_flags = neigh->flags;
1793 neigh_release(neigh);
1794 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001795 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001796 RT6_TRACE("purging route %p via non-router but gateway\n",
1797 rt);
1798 return -1;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001801 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803
1804 return 0;
1805}
1806
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001807void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Michal Kubeček3570df92016-03-08 14:44:25 +01001809 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001810 unsigned long now;
1811
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001812 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001813 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1814 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001815 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1816 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001818 gc_args.timeout = expires ? (int)expires :
1819 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001821 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001822
Michal Kubeček3570df92016-03-08 14:44:25 +01001823 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001824 now = jiffies;
1825 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001828 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001829 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001830 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001831 else
1832 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001833 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001836static void fib6_gc_timer_cb(unsigned long arg)
1837{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001838 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001839}
1840
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001841static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001842{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001843 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1844
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001845 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001846 rwlock_init(&net->ipv6.fib6_walker_lock);
1847 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001848 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001849
Benjamin Theryc5728722008-03-03 23:34:17 -08001850 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1851 if (!net->ipv6.rt6_stats)
1852 goto out_timer;
1853
Eric Dumazet10da66f2010-10-13 08:22:03 +00001854 /* Avoid false sharing : Use at least a full cache line */
1855 size = max_t(size_t, size, L1_CACHE_BYTES);
1856
1857 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001858 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001859 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001860
1861 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1862 GFP_KERNEL);
1863 if (!net->ipv6.fib6_main_tbl)
1864 goto out_fib_table_hash;
1865
1866 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001867 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001868 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1869 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001870 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001871
1872#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1873 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1874 GFP_KERNEL);
1875 if (!net->ipv6.fib6_local_tbl)
1876 goto out_fib6_main_tbl;
1877 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001878 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001879 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1880 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001881 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001882#endif
1883 fib6_tables_init(net);
1884
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001885 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001886
1887#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1888out_fib6_main_tbl:
1889 kfree(net->ipv6.fib6_main_tbl);
1890#endif
1891out_fib_table_hash:
1892 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001893out_rt6_stats:
1894 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001895out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001896 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001897}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001898
1899static void fib6_net_exit(struct net *net)
1900{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001901 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001902 del_timer_sync(&net->ipv6.ip6_fib_timer);
1903
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001904#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001905 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001906 kfree(net->ipv6.fib6_local_tbl);
1907#endif
David S. Miller8e773272012-06-11 00:01:52 -07001908 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001909 kfree(net->ipv6.fib6_main_tbl);
1910 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001911 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001912}
1913
1914static struct pernet_operations fib6_net_ops = {
1915 .init = fib6_net_init,
1916 .exit = fib6_net_exit,
1917};
1918
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001919int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001921 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1924 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001925 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001926 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001927 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001928 goto out;
1929
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001930 ret = register_pernet_subsys(&fib6_net_ops);
1931 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001932 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001933
1934 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1935 NULL);
1936 if (ret)
1937 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001938
1939 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001940out:
1941 return ret;
1942
David S. Millere8803b62012-06-16 01:12:19 -07001943out_unregister_subsys:
1944 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001945out_kmem_cache_create:
1946 kmem_cache_destroy(fib6_node_kmem);
1947 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
1950void fib6_gc_cleanup(void)
1951{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001952 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 kmem_cache_destroy(fib6_node_kmem);
1954}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001955
1956#ifdef CONFIG_PROC_FS
1957
1958struct ipv6_route_iter {
1959 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001960 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001961 loff_t skip;
1962 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001963 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001964};
1965
1966static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1967{
1968 struct rt6_info *rt = v;
1969 struct ipv6_route_iter *iter = seq->private;
1970
1971 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1972
1973#ifdef CONFIG_IPV6_SUBTREES
1974 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1975#else
1976 seq_puts(seq, "00000000000000000000000000000000 00 ");
1977#endif
1978 if (rt->rt6i_flags & RTF_GATEWAY)
1979 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1980 else
1981 seq_puts(seq, "00000000000000000000000000000000");
1982
1983 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1984 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1985 rt->dst.__use, rt->rt6i_flags,
1986 rt->dst.dev ? rt->dst.dev->name : "");
1987 iter->w.leaf = NULL;
1988 return 0;
1989}
1990
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001991static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001992{
1993 struct ipv6_route_iter *iter = w->args;
1994
1995 if (!iter->skip)
1996 return 1;
1997
1998 do {
1999 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2000 iter->skip--;
2001 if (!iter->skip && iter->w.leaf)
2002 return 1;
2003 } while (iter->w.leaf);
2004
2005 return 0;
2006}
2007
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002008static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2009 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002010{
2011 memset(&iter->w, 0, sizeof(iter->w));
2012 iter->w.func = ipv6_route_yield;
2013 iter->w.root = &iter->tbl->tb6_root;
2014 iter->w.state = FWS_INIT;
2015 iter->w.node = iter->w.root;
2016 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002017 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002018 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002019 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002020}
2021
2022static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2023 struct net *net)
2024{
2025 unsigned int h;
2026 struct hlist_node *node;
2027
2028 if (tbl) {
2029 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2030 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2031 } else {
2032 h = 0;
2033 node = NULL;
2034 }
2035
2036 while (!node && h < FIB6_TABLE_HASHSZ) {
2037 node = rcu_dereference_bh(
2038 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2039 }
2040 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2041}
2042
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002043static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2044{
2045 if (iter->sernum != iter->w.root->fn_sernum) {
2046 iter->sernum = iter->w.root->fn_sernum;
2047 iter->w.state = FWS_INIT;
2048 iter->w.node = iter->w.root;
2049 WARN_ON(iter->w.skip);
2050 iter->w.skip = iter->w.count;
2051 }
2052}
2053
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002054static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2055{
2056 int r;
2057 struct rt6_info *n;
2058 struct net *net = seq_file_net(seq);
2059 struct ipv6_route_iter *iter = seq->private;
2060
2061 if (!v)
2062 goto iter_table;
2063
2064 n = ((struct rt6_info *)v)->dst.rt6_next;
2065 if (n) {
2066 ++*pos;
2067 return n;
2068 }
2069
2070iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002071 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002072 read_lock(&iter->tbl->tb6_lock);
2073 r = fib6_walk_continue(&iter->w);
2074 read_unlock(&iter->tbl->tb6_lock);
2075 if (r > 0) {
2076 if (v)
2077 ++*pos;
2078 return iter->w.leaf;
2079 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002080 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002081 return NULL;
2082 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002083 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002084
2085 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2086 if (!iter->tbl)
2087 return NULL;
2088
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002089 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002090 goto iter_table;
2091}
2092
2093static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2094 __acquires(RCU_BH)
2095{
2096 struct net *net = seq_file_net(seq);
2097 struct ipv6_route_iter *iter = seq->private;
2098
2099 rcu_read_lock_bh();
2100 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2101 iter->skip = *pos;
2102
2103 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002104 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002105 return ipv6_route_seq_next(seq, NULL, pos);
2106 } else {
2107 return NULL;
2108 }
2109}
2110
2111static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2112{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002113 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002114 return w->node && !(w->state == FWS_U && w->node == w->root);
2115}
2116
2117static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2118 __releases(RCU_BH)
2119{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002120 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002121 struct ipv6_route_iter *iter = seq->private;
2122
2123 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002124 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002125
2126 rcu_read_unlock_bh();
2127}
2128
2129static const struct seq_operations ipv6_route_seq_ops = {
2130 .start = ipv6_route_seq_start,
2131 .next = ipv6_route_seq_next,
2132 .stop = ipv6_route_seq_stop,
2133 .show = ipv6_route_seq_show
2134};
2135
2136int ipv6_route_open(struct inode *inode, struct file *file)
2137{
2138 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2139 sizeof(struct ipv6_route_iter));
2140}
2141
2142#endif /* CONFIG_PROC_FS */