blob: 7ba9a6ebf19d925b5b1486d650a002f0ab14057a [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
Wei Wang7f8f23f2017-08-21 09:47:10 -0700151static void node_free_immediate(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 kmem_cache_free(fib6_node_kmem, fn);
154}
155
Wei Wang7f8f23f2017-08-21 09:47:10 -0700156static void node_free_rcu(struct rcu_head *head)
157{
158 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
159
160 kmem_cache_free(fib6_node_kmem, fn);
161}
162
163static void node_free(struct fib6_node *fn)
164{
165 call_rcu(&fn->rcu, node_free_rcu);
166}
167
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700168static void rt6_rcu_free(struct rt6_info *rt)
169{
170 call_rcu(&rt->dst.rcu_head, dst_rcu_free);
171}
172
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700173static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
174{
175 int cpu;
176
177 if (!non_pcpu_rt->rt6i_pcpu)
178 return;
179
180 for_each_possible_cpu(cpu) {
181 struct rt6_info **ppcpu_rt;
182 struct rt6_info *pcpu_rt;
183
184 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
185 pcpu_rt = *ppcpu_rt;
186 if (pcpu_rt) {
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700187 rt6_rcu_free(pcpu_rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700188 *ppcpu_rt = NULL;
189 }
190 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700191
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700192 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700193 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700194}
195
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200196static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700198 if (atomic_dec_and_test(&rt->rt6i_ref)) {
199 rt6_free_pcpu(rt);
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700200 rt6_rcu_free(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Sabrina Dubrocac9335db2017-09-08 10:26:19 +0200204static void fib6_free_table(struct fib6_table *table)
205{
206 inetpeer_invalidate_tree(&table->tb6_peers);
207 kfree(table);
208}
209
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800210static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700211{
212 unsigned int h;
213
Thomas Graf375216a2006-10-21 20:20:54 -0700214 /*
215 * Initialize table lock at a single place to give lockdep a key,
216 * tables aren't visible prior to being linked to the list.
217 */
218 rwlock_init(&tb->tb6_lock);
219
Neil Hormana33bc5c2009-07-30 18:52:15 -0700220 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700221
222 /*
223 * No protection necessary, this is the only list mutatation
224 * operation, tables never disappear once they exist.
225 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800226 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700227}
228
229#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800230
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800231static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700232{
233 struct fib6_table *table;
234
235 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500236 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700237 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800238 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700239 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700240 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700241 }
242
243 return table;
244}
245
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800246struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700247{
248 struct fib6_table *tb;
249
250 if (id == 0)
251 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800252 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700253 if (tb)
254 return tb;
255
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800256 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500257 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259
260 return tb;
261}
David Ahernb3b46632016-05-04 21:46:12 -0700262EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700263
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800264struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700265{
266 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800267 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700268 unsigned int h;
269
270 if (id == 0)
271 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700272 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700273 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800274 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800275 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700276 if (tb->tb6_id == id) {
277 rcu_read_unlock();
278 return tb;
279 }
280 }
281 rcu_read_unlock();
282
283 return NULL;
284}
David Ahernc4850682015-10-12 11:47:08 -0700285EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700286
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000287static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700288{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800289 fib6_link_table(net, net->ipv6.fib6_main_tbl);
290 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700291}
Thomas Grafc71099a2006-08-04 23:20:06 -0700292#else
293
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800294struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700295{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800296 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700297}
298
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800299struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700300{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800301 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700302}
303
David S. Miller4c9483b2011-03-12 16:22:43 -0500304struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800305 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700306{
lucienab997ad2015-10-23 15:36:53 +0800307 struct rt6_info *rt;
308
309 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovychb9ca9b02017-06-20 13:29:25 +0300310 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800311 ip6_rt_put(rt);
312 rt = net->ipv6.ip6_null_entry;
313 dst_hold(&rt->dst);
314 }
315
316 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700317}
318
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000319static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700320{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800321 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700322}
323
324#endif
325
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200326static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700327{
328 int res;
329 struct rt6_info *rt;
330
Changli Gaod8d1f302010-06-10 23:31:35 -0700331 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700332 res = rt6_dump_route(rt, w->args);
333 if (res < 0) {
334 /* Frame is full, suspend walking */
335 w->leaf = rt;
336 return 1;
337 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700338 }
339 w->leaf = NULL;
340 return 0;
341}
342
343static void fib6_dump_end(struct netlink_callback *cb)
344{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100345 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200346 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700347
348 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800349 if (cb->args[4]) {
350 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100351 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800352 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700353 cb->args[2] = 0;
354 kfree(w);
355 }
Wang Yufen437de072014-03-28 12:07:04 +0800356 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700357 cb->args[1] = 3;
358}
359
360static int fib6_dump_done(struct netlink_callback *cb)
361{
362 fib6_dump_end(cb);
363 return cb->done ? cb->done(cb) : 0;
364}
365
366static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
367 struct netlink_callback *cb)
368{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100369 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200370 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700371 int res;
372
373 w = (void *)cb->args[2];
374 w->root = &table->tb6_root;
375
376 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000377 w->count = 0;
378 w->skip = 0;
379
Patrick McHardy1b43af52006-08-10 23:11:17 -0700380 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100381 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700382 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000383 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700384 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000385 cb->args[5] = w->root->fn_sernum;
386 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700387 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000388 if (cb->args[5] != w->root->fn_sernum) {
389 /* Begin at the root if the tree changed */
390 cb->args[5] = w->root->fn_sernum;
391 w->state = FWS_INIT;
392 w->node = w->root;
393 w->skip = w->count;
394 } else
395 w->skip = 0;
396
Patrick McHardy1b43af52006-08-10 23:11:17 -0700397 read_lock_bh(&table->tb6_lock);
398 res = fib6_walk_continue(w);
399 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800400 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100401 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800402 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700403 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700404 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800405
Patrick McHardy1b43af52006-08-10 23:11:17 -0700406 return res;
407}
408
Thomas Grafc127ea22007-03-22 11:58:32 -0700409static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700410{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900411 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700412 unsigned int h, s_h;
413 unsigned int e = 0, s_e;
414 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200415 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700416 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800417 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700418 int res = 0;
419
420 s_h = cb->args[0];
421 s_e = cb->args[1];
422
423 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500424 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700425 /* New dump:
426 *
427 * 1. hook callback destructor.
428 */
429 cb->args[3] = (long)cb->done;
430 cb->done = fib6_dump_done;
431
432 /*
433 * 2. allocate and initialize walker.
434 */
435 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500436 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700437 return -ENOMEM;
438 w->func = fib6_dump_node;
439 cb->args[2] = (long)w;
440 }
441
442 arg.skb = skb;
443 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700444 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700445 w->args = &arg;
446
Eric Dumazete67f88d2011-04-27 22:56:07 +0000447 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700448 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700449 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800450 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800451 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700452 if (e < s_e)
453 goto next;
454 res = fib6_dump_table(tb, skb, cb);
455 if (res != 0)
456 goto out;
457next:
458 e++;
459 }
460 }
461out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000462 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700463 cb->args[1] = e;
464 cb->args[0] = h;
465
466 res = res < 0 ? res : skb->len;
467 if (res <= 0)
468 fib6_dump_end(cb);
469 return res;
470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472/*
473 * Routing Table
474 *
475 * return the appropriate node for a routing tree "add" operation
476 * by either creating and inserting or by returning an existing
477 * node.
478 */
479
fan.du9225b232013-07-22 14:21:09 +0800480static struct fib6_node *fib6_add_1(struct fib6_node *root,
481 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000482 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200483 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct fib6_node *fn, *in, *ln;
486 struct fib6_node *pn = NULL;
487 struct rt6key *key;
488 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900489 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 RT6_TRACE("fib6_add_1\n");
492
493 /* insert node in tree */
494
495 fn = root;
496
497 do {
498 key = (struct rt6key *)((u8 *)fn->leaf + offset);
499
500 /*
501 * Prefix match
502 */
503 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000504 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000505 if (!allow_create) {
506 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000507 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000508 return ERR_PTR(-ENOENT);
509 }
Joe Perchesf3213832012-05-15 14:11:53 +0000510 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000513 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * Exact match ?
517 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (plen == fn->fn_bit) {
520 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500521 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rt6_release(fn->leaf);
523 fn->leaf = NULL;
524 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return fn;
529 }
530
531 /*
532 * We have more bits to go
533 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Try to walk down on tree. */
536 fn->fn_sernum = sernum;
537 dir = addr_bit_set(addr, fn->fn_bit);
538 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800539 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 } while (fn);
541
Matti Vaittinen14df0152011-11-16 21:18:02 +0000542 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000543 /* We should not create new node because
544 * NLM_F_REPLACE was specified without NLM_F_CREATE
545 * I assume it is safe to require NLM_F_CREATE when
546 * REPLACE flag is used! Later we may want to remove the
547 * check for replace_required, because according
548 * to netlink specification, NLM_F_CREATE
549 * MUST be specified if new route is created.
550 * That would keep IPv6 consistent with IPv4
551 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000552 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000553 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000554 return ERR_PTR(-ENOENT);
555 }
Joe Perchesf3213832012-05-15 14:11:53 +0000556 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * We walked to the bottom of tree.
560 * Create new leaf node without children.
561 */
562
563 ln = node_alloc();
564
David S. Miller507c9b12011-12-03 17:50:45 -0500565 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000566 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ln->parent = pn;
570 ln->fn_sernum = sernum;
571
572 if (dir)
573 pn->right = ln;
574 else
575 pn->left = ln;
576
577 return ln;
578
579
580insert_above:
581 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900582 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * we have a less significant route.
584 * we've to insert an intermediate node on the list
585 * this new node will point to the one we need to create
586 * and the current
587 */
588
589 pn = fn->parent;
590
591 /* find 1st bit in difference between the 2 addrs.
592
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800593 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 but if it is >= plen, the value is ignored in any case.
595 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900596
fan.du9225b232013-07-22 14:21:09 +0800597 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900599 /*
600 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * / \
602 * (new leaf node)[ln] (old node)[fn]
603 */
604 if (plen > bit) {
605 in = node_alloc();
606 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900607
David S. Miller507c9b12011-12-03 17:50:45 -0500608 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (in)
Wei Wang7f8f23f2017-08-21 09:47:10 -0700610 node_free_immediate(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (ln)
Wei Wang7f8f23f2017-08-21 09:47:10 -0700612 node_free_immediate(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000613 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900616 /*
617 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * RTN_RTINFO will
619 * be off since that an address that chooses one of
620 * the branches would not match less specific routes
621 * in the other branch
622 */
623
624 in->fn_bit = bit;
625
626 in->parent = pn;
627 in->leaf = fn->leaf;
628 atomic_inc(&in->leaf->rt6i_ref);
629
630 in->fn_sernum = sernum;
631
632 /* update parent pointer */
633 if (dir)
634 pn->right = in;
635 else
636 pn->left = in;
637
638 ln->fn_bit = plen;
639
640 ln->parent = in;
641 fn->parent = in;
642
643 ln->fn_sernum = sernum;
644
645 if (addr_bit_set(addr, bit)) {
646 in->right = ln;
647 in->left = fn;
648 } else {
649 in->left = ln;
650 in->right = fn;
651 }
652 } else { /* plen <= bit */
653
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900654 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * (new leaf node)[ln]
656 * / \
657 * (old node)[fn] NULL
658 */
659
660 ln = node_alloc();
661
David S. Miller507c9b12011-12-03 17:50:45 -0500662 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000663 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 ln->fn_bit = plen;
666
667 ln->parent = pn;
668
669 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (dir)
672 pn->right = ln;
673 else
674 pn->left = ln;
675
676 if (addr_bit_set(&key->addr, plen))
677 ln->right = fn;
678 else
679 ln->left = fn;
680
681 fn->parent = ln;
682 }
683 return ln;
684}
685
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200686static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200687{
688 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
689 RTF_GATEWAY;
690}
691
Florian Westphale715b6d2015-01-05 23:57:44 +0100692static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100693{
Florian Westphale715b6d2015-01-05 23:57:44 +0100694 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100695
Florian Westphale715b6d2015-01-05 23:57:44 +0100696 for (i = 0; i < RTAX_MAX; i++) {
697 if (test_bit(i, mxc->mx_valid))
698 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100699 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100700}
701
702static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
703{
704 if (!mxc->mx)
705 return 0;
706
707 if (dst->flags & DST_HOST) {
708 u32 *mp = dst_metrics_write_ptr(dst);
709
710 if (unlikely(!mp))
711 return -ENOMEM;
712
713 fib6_copy_metrics(mp, mxc);
714 } else {
715 dst_init_metrics(dst, mxc->mx, false);
716
717 /* We've stolen mx now. */
718 mxc->mx = NULL;
719 }
720
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100721 return 0;
722}
723
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100724static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
725 struct net *net)
726{
727 if (atomic_read(&rt->rt6i_ref) != 1) {
728 /* This route is used as dummy address holder in some split
729 * nodes. It is not leaked, but it still holds other resources,
730 * which must be released in time. So, scan ascendant nodes
731 * and replace dummy references to this route with references
732 * to still alive ones.
733 */
734 while (fn) {
735 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
736 fn->leaf = fib6_find_prefix(net, fn);
737 atomic_inc(&fn->leaf->rt6i_ref);
738 rt6_release(rt);
739 }
740 fn = fn->parent;
741 }
742 /* No more references are possible at this point. */
743 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
744 }
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * Insert routing information in a node.
749 */
750
751static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100752 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct rt6_info *iter = NULL;
755 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200756 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500757 int replace = (info->nlh &&
758 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
759 int add = (!info->nlh ||
760 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000761 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200762 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200763 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100764 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 ins = &fn->leaf;
767
David S. Miller507c9b12011-12-03 17:50:45 -0500768 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * Search for duplicates
771 */
772
773 if (iter->rt6i_metric == rt->rt6i_metric) {
774 /*
775 * Same priority level
776 */
David S. Miller507c9b12011-12-03 17:50:45 -0500777 if (info->nlh &&
778 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000779 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200780
781 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000782 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200783 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
784 found++;
785 break;
786 }
787 if (rt_can_ecmp)
788 fallback_ins = fallback_ins ?: ins;
789 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
David Ahern0bc26d12017-07-05 14:41:46 -0600792 if (rt6_duplicate_nexthop(iter, rt)) {
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;
Wei Wang43c792a2017-08-25 15:03:10 -0700883 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000884 atomic_inc(&rt->rt6i_ref);
Guillaume Nault73483c12016-09-07 17:21:40 +0200885 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000886 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
David S. Miller507c9b12011-12-03 17:50:45 -0500888 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000889 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
890 fn->fn_flags |= RTN_RTINFO;
891 }
892
893 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200894 int nsiblings;
895
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000896 if (!found) {
897 if (add)
898 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000899 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000900 return -ENOENT;
901 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100902
903 err = fib6_commit_metrics(&rt->dst, mxc);
904 if (err)
905 return err;
906
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000907 *ins = rt;
Wei Wang43c792a2017-08-25 15:03:10 -0700908 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000909 rt->dst.rt6_next = iter->dst.rt6_next;
910 atomic_inc(&rt->rt6i_ref);
Roopa Prabhu37a1d362015-09-13 10:18:33 -0700911 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -0500912 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000913 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
914 fn->fn_flags |= RTN_RTINFO;
915 }
Michal Kubeček27596472015-05-18 20:54:00 +0200916 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel9b2b0812017-08-03 13:28:22 +0200917 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100918 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang62e9a282017-08-16 11:18:09 -0700919 if (fn->rr_ptr == iter)
920 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100921 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200922
923 if (nsiblings) {
924 /* Replacing an ECMP route, remove all siblings */
925 ins = &rt->dst.rt6_next;
926 iter = *ins;
927 while (iter) {
Sabrina Dubroca4a8d3bb2017-03-13 13:28:09 +0100928 if (iter->rt6i_metric > rt->rt6i_metric)
929 break;
Michal Kubeček27596472015-05-18 20:54:00 +0200930 if (rt6_qualify_for_ecmp(iter)) {
931 *ins = iter->dst.rt6_next;
Ido Schimmel9b2b0812017-08-03 13:28:22 +0200932 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +0200933 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang62e9a282017-08-16 11:18:09 -0700934 if (fn->rr_ptr == iter)
935 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +0200936 rt6_release(iter);
937 nsiblings--;
938 } else {
939 ins = &iter->dst.rt6_next;
940 }
941 iter = *ins;
942 }
943 WARN_ON(nsiblings != 0);
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 return 0;
948}
949
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200950static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700952 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500953 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700954 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700955 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800958void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700960 if (!timer_pending(&net->ipv6.ip6_fib_timer))
961 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700962 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/*
966 * Add routing information to the routing tree.
967 * <destination addr>/<source addr>
968 * with source addr info in sub-trees
969 */
970
Florian Westphale715b6d2015-01-05 23:57:44 +0100971int fib6_add(struct fib6_node *root, struct rt6_info *rt,
972 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700974 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000976 int allow_create = 1;
977 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200978 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500979
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700980 if (WARN_ON_ONCE((rt->dst.flags & DST_NOCACHE) &&
981 !atomic_read(&rt->dst.__refcnt)))
982 return -EINVAL;
983
David S. Miller507c9b12011-12-03 17:50:45 -0500984 if (info->nlh) {
985 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000986 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500987 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000988 replace_required = 1;
989 }
990 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000991 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
fan.du9225b232013-07-22 14:21:09 +0800993 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
994 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200995 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000996 if (IS_ERR(fn)) {
997 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200998 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001002 pn = fn;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#ifdef CONFIG_IPV6_SUBTREES
1005 if (rt->rt6i_src.plen) {
1006 struct fib6_node *sn;
1007
David S. Miller507c9b12011-12-03 17:50:45 -05001008 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct fib6_node *sfn;
1010
1011 /*
1012 * Create subtree.
1013 *
1014 * fn[main tree]
1015 * |
1016 * sfn[subtree root]
1017 * \
1018 * sn[new leaf node]
1019 */
1020
1021 /* Create subtree root node */
1022 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001023 if (!sfn)
Wei Wang1c18f932017-08-18 17:14:49 -07001024 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001026 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1027 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001029 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* Now add the first leaf node to new subtree */
1032
1033 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001034 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001035 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001036 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001038 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* If it is failed, discard just allocated
Wei Wang1c18f932017-08-18 17:14:49 -07001040 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 in main tree.
1042 */
Wei Wang7f8f23f2017-08-21 09:47:10 -07001043 node_free_immediate(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001044 err = PTR_ERR(sn);
Wei Wang1c18f932017-08-18 17:14:49 -07001045 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
1048 /* Now link new subtree to main tree */
1049 sfn->parent = fn;
1050 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else {
1052 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001053 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001054 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001055 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001057 if (IS_ERR(sn)) {
1058 err = PTR_ERR(sn);
Wei Wang1c18f932017-08-18 17:14:49 -07001059 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
David S. Miller507c9b12011-12-03 17:50:45 -05001063 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001064 fn->leaf = rt;
1065 atomic_inc(&rt->rt6i_ref);
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 fn = sn;
1068 }
1069#endif
1070
Florian Westphale715b6d2015-01-05 23:57:44 +01001071 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001072 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001073 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001074 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001075 fib6_prune_clones(info->nl_net, pn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001076 rt->dst.flags &= ~DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
1079out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001080 if (err) {
1081#ifdef CONFIG_IPV6_SUBTREES
1082 /*
1083 * If fib6_add_1 has cleared the old leaf pointer in the
1084 * super-tree leaf node we have to find a new one for it.
1085 */
David S. Miller3c051232008-04-18 01:46:19 -07001086 if (pn != fn && pn->leaf == rt) {
1087 pn->leaf = NULL;
1088 atomic_dec(&rt->rt6i_ref);
1089 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001090 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001091 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001092#if RT6_DEBUG >= 2
1093 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001094 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001095 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001096 }
1097#endif
1098 atomic_inc(&pn->leaf->rt6i_ref);
1099 }
1100#endif
Wei Wang1c18f932017-08-18 17:14:49 -07001101 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return err;
1104
Wei Wang1c18f932017-08-18 17:14:49 -07001105failure:
1106 /* fn->leaf could be NULL if fn is an intermediate node and we
1107 * failed to add the new route to it in both subtree creation
1108 * failure and fib6_add_rt2node() failure case.
1109 * In both cases, fib6_repair_tree() should be called to fix
1110 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001113 fib6_repair_tree(info->nl_net, fn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001114 if (!(rt->dst.flags & DST_NOCACHE))
1115 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119/*
1120 * Routing tree lookup
1121 *
1122 */
1123
1124struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001125 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001126 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127};
1128
Wang Yufen437de072014-03-28 12:07:04 +08001129static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1130 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -08001133 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001135 if (unlikely(args->offset == 0))
1136 return NULL;
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
1139 * Descend on a tree
1140 */
1141
1142 fn = root;
1143
1144 for (;;) {
1145 struct fib6_node *next;
1146
1147 dir = addr_bit_set(args->addr, fn->fn_bit);
1148
1149 next = dir ? fn->right : fn->left;
1150
1151 if (next) {
1152 fn = next;
1153 continue;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 }
1157
David S. Miller507c9b12011-12-03 17:50:45 -05001158 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001159 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct rt6key *key;
1161
1162 key = (struct rt6key *) ((u8 *) fn->leaf +
1163 args->offset);
1164
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001165 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1166#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001167 if (fn->subtree) {
1168 struct fib6_node *sfn;
1169 sfn = fib6_lookup_1(fn->subtree,
1170 args + 1);
1171 if (!sfn)
1172 goto backtrack;
1173 fn = sfn;
1174 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001175#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001176 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001177 return fn;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001180#ifdef CONFIG_IPV6_SUBTREES
1181backtrack:
1182#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001183 if (fn->fn_flags & RTN_ROOT)
1184 break;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 fn = fn->parent;
1187 }
1188
1189 return NULL;
1190}
1191
Wang Yufen437de072014-03-28 12:07:04 +08001192struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1193 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001196 struct lookup_args args[] = {
1197 {
1198 .offset = offsetof(struct rt6_info, rt6i_dst),
1199 .addr = daddr,
1200 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001202 {
1203 .offset = offsetof(struct rt6_info, rt6i_src),
1204 .addr = saddr,
1205 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001207 {
1208 .offset = 0, /* sentinel */
1209 }
1210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001212 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001213 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 fn = root;
1215
1216 return fn;
1217}
1218
1219/*
1220 * Get node with specified destination prefix (and source prefix,
1221 * if subtrees are used)
1222 */
1223
1224
Wang Yufen437de072014-03-28 12:07:04 +08001225static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1226 const struct in6_addr *addr,
1227 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct fib6_node *fn;
1230
1231 for (fn = root; fn ; ) {
1232 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1233
1234 /*
1235 * Prefix match
1236 */
1237 if (plen < fn->fn_bit ||
1238 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1239 return NULL;
1240
1241 if (plen == fn->fn_bit)
1242 return fn;
1243
1244 /*
1245 * We have more bits to go
1246 */
1247 if (addr_bit_set(addr, fn->fn_bit))
1248 fn = fn->right;
1249 else
1250 fn = fn->left;
1251 }
1252 return NULL;
1253}
1254
Wang Yufen437de072014-03-28 12:07:04 +08001255struct fib6_node *fib6_locate(struct fib6_node *root,
1256 const struct in6_addr *daddr, int dst_len,
1257 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct fib6_node *fn;
1260
1261 fn = fib6_locate_1(root, daddr, dst_len,
1262 offsetof(struct rt6_info, rt6i_dst));
1263
1264#ifdef CONFIG_IPV6_SUBTREES
1265 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001266 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001267 if (fn && fn->subtree)
1268 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 offsetof(struct rt6_info, rt6i_src));
1270 }
1271#endif
1272
David S. Miller507c9b12011-12-03 17:50:45 -05001273 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return fn;
1275
1276 return NULL;
1277}
1278
1279
1280/*
1281 * Deletion
1282 *
1283 */
1284
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001285static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
David S. Miller507c9b12011-12-03 17:50:45 -05001287 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001288 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David S. Miller507c9b12011-12-03 17:50:45 -05001290 while (fn) {
1291 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001293 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return fn->right->leaf;
1295
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001296 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 return NULL;
1299}
1300
1301/*
1302 * Called to trim the tree of intermediate nodes when possible. "fn"
1303 * is the node we want to try and remove.
1304 */
1305
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001306static struct fib6_node *fib6_repair_tree(struct net *net,
1307 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int children;
1310 int nstate;
1311 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001312 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int iter = 0;
1314
1315 for (;;) {
1316 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1317 iter++;
1318
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001319 WARN_ON(fn->fn_flags & RTN_RTINFO);
1320 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001321 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 children = 0;
1324 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001325 if (fn->right)
1326 child = fn->right, children |= 1;
1327 if (fn->left)
1328 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001330 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331#ifdef CONFIG_IPV6_SUBTREES
1332 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001333 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#endif
1335 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001336 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001338 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001339 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001340 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342#endif
1343 atomic_inc(&fn->leaf->rt6i_ref);
1344 return fn->parent;
1345 }
1346
1347 pn = fn->parent;
1348#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001349 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001350 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001351 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 nstate = FWS_L;
1353 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001354 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001356 if (pn->right == fn)
1357 pn->right = child;
1358 else if (pn->left == fn)
1359 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001361 else
1362 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#endif
1364 if (child)
1365 child->parent = pn;
1366 nstate = FWS_R;
1367#ifdef CONFIG_IPV6_SUBTREES
1368 }
1369#endif
1370
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001371 read_lock(&net->ipv6.fib6_walker_lock);
1372 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001373 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (w->root == fn) {
1375 w->root = w->node = NULL;
1376 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1377 } else if (w->node == fn) {
1378 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1379 w->node = pn;
1380 w->state = nstate;
1381 }
1382 } else {
1383 if (w->root == fn) {
1384 w->root = child;
1385 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1386 }
1387 if (w->node == fn) {
1388 w->node = child;
1389 if (children&2) {
1390 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001391 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 } else {
1393 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001394 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 }
1397 }
1398 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001399 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001402 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return pn;
1404
1405 rt6_release(pn->leaf);
1406 pn->leaf = NULL;
1407 fn = pn;
1408 }
1409}
1410
1411static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001412 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001414 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001416 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 RT6_TRACE("fib6_del_route\n");
1419
1420 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001421 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001423 net->ipv6.rt6_stats->fib_rt_entries--;
1424 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
David S. Millerf11e6652007-03-24 20:36:25 -07001426 /* Reset round-robin state, if necessary */
1427 if (fn->rr_ptr == rt)
1428 fn->rr_ptr = NULL;
1429
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001430 /* Remove this entry from other siblings */
1431 if (rt->rt6i_nsiblings) {
1432 struct rt6_info *sibling, *next_sibling;
1433
1434 list_for_each_entry_safe(sibling, next_sibling,
1435 &rt->rt6i_siblings, rt6i_siblings)
1436 sibling->rt6i_nsiblings--;
1437 rt->rt6i_nsiblings = 0;
1438 list_del_init(&rt->rt6i_siblings);
1439 }
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001442 read_lock(&net->ipv6.fib6_walker_lock);
1443 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (w->state == FWS_C && w->leaf == rt) {
1445 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001446 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001447 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 w->state = FWS_U;
1449 }
1450 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001451 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Changli Gaod8d1f302010-06-10 23:31:35 -07001453 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001456 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001458 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001459 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001462 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Roopa Prabhu37a1d362015-09-13 10:18:33 -07001464 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{
Wei Wang43c792a2017-08-25 15:03:10 -07001470 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1471 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001472 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct rt6_info **rtp;
1474
1475#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001476 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001477 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -ENOENT;
1479 }
1480#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001481 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -ENOENT;
1483
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001484 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
David S. Miller507c9b12011-12-03 17:50:45 -05001486 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001487 struct fib6_node *pn = fn;
1488#ifdef CONFIG_IPV6_SUBTREES
1489 /* clones of this route might be in another subtree */
1490 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001491 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001492 pn = pn->parent;
1493 pn = pn->parent;
1494 }
1495#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001496 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /*
1500 * Walk the leaf entries looking for ourself
1501 */
1502
Changli Gaod8d1f302010-06-10 23:31:35 -07001503 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001505 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
1507 }
1508 }
1509 return -ENOENT;
1510}
1511
1512/*
1513 * Tree traversal function.
1514 *
1515 * Certainly, it is not interrupt safe.
1516 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1517 * It means, that we can modify tree during walking
1518 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001519 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *
1521 * It guarantees that every node will be traversed,
1522 * and that it will be traversed only once.
1523 *
1524 * Callback function w->func may return:
1525 * 0 -> continue walking.
1526 * positive value -> walking is suspended (used by tree dumps,
1527 * and probably by gc, if it will be split to several slices)
1528 * negative value -> terminate walking.
1529 *
1530 * The function itself returns:
1531 * 0 -> walk is complete.
1532 * >0 -> walk is incomplete (i.e. suspended)
1533 * <0 -> walk is terminated by an error.
1534 */
1535
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001536static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct fib6_node *fn, *pn;
1539
1540 for (;;) {
1541 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001542 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544
1545 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001546 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 w->state = FWS_C;
1548 w->leaf = fn->leaf;
1549 }
1550 switch (w->state) {
1551#ifdef CONFIG_IPV6_SUBTREES
1552 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001553 if (FIB6_SUBTREE(fn)) {
1554 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 continue;
1556 }
1557 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 case FWS_L:
1560 if (fn->left) {
1561 w->node = fn->left;
1562 w->state = FWS_INIT;
1563 continue;
1564 }
1565 w->state = FWS_R;
1566 case FWS_R:
1567 if (fn->right) {
1568 w->node = fn->right;
1569 w->state = FWS_INIT;
1570 continue;
1571 }
1572 w->state = FWS_C;
1573 w->leaf = fn->leaf;
1574 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001575 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001576 int err;
1577
Eric Dumazetfa809e22012-06-25 15:37:19 -07001578 if (w->skip) {
1579 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001580 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001581 }
1582
1583 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (err)
1585 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001586
1587 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 continue;
1589 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001590skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 w->state = FWS_U;
1592 case FWS_U:
1593 if (fn == w->root)
1594 return 0;
1595 pn = fn->parent;
1596 w->node = pn;
1597#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001598 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001599 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 w->state = FWS_L;
1601 continue;
1602 }
1603#endif
1604 if (pn->left == fn) {
1605 w->state = FWS_R;
1606 continue;
1607 }
1608 if (pn->right == fn) {
1609 w->state = FWS_C;
1610 w->leaf = w->node->leaf;
1611 continue;
1612 }
1613#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001614 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615#endif
1616 }
1617 }
1618}
1619
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001620static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 int res;
1623
1624 w->state = FWS_INIT;
1625 w->node = w->root;
1626
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001627 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 res = fib6_walk_continue(w);
1629 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001630 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return res;
1632}
1633
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001634static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 int res;
1637 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001638 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001639 struct nl_info info = {
1640 .nl_net = c->net,
1641 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001643 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1644 w->node->fn_sernum != c->sernum)
1645 w->node->fn_sernum = c->sernum;
1646
1647 if (!c->func) {
1648 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1649 w->leaf = NULL;
1650 return 0;
1651 }
1652
Changli Gaod8d1f302010-06-10 23:31:35 -07001653 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 res = c->func(rt, c->arg);
1655 if (res < 0) {
1656 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001657 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (res) {
1659#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001660 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang43c792a2017-08-25 15:03:10 -07001661 __func__, rt,
1662 rcu_access_pointer(rt->rt6i_node),
1663 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664#endif
1665 continue;
1666 }
1667 return 0;
1668 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001669 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 w->leaf = rt;
1672 return 0;
1673}
1674
1675/*
1676 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001677 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * func is called on each route.
1679 * It may return -1 -> delete this route.
1680 * 0 -> continue walking
1681 *
1682 * prune==1 -> only immediate children of node (certainly,
1683 * ignoring pure split nodes) will be scanned.
1684 */
1685
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001686static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001687 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001688 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001690 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 c.w.root = root;
1693 c.w.func = fib6_clean_node;
1694 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001695 c.w.count = 0;
1696 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001698 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001700 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001702 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001705static void __fib6_clean_all(struct net *net,
1706 int (*func)(struct rt6_info *, void *),
1707 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001708{
Thomas Grafc71099a2006-08-04 23:20:06 -07001709 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001710 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001711 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001712
Patrick McHardy1b43af52006-08-10 23:11:17 -07001713 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001714 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001715 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001716 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001717 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001718 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001719 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001720 write_unlock_bh(&table->tb6_lock);
1721 }
1722 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001723 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001724}
1725
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001726void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1727 void *arg)
1728{
1729 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1733{
1734 if (rt->rt6i_flags & RTF_CACHE) {
1735 RT6_TRACE("pruning clone %p\n", rt);
1736 return -1;
1737 }
1738
1739 return 0;
1740}
1741
Duan Jiong163cd4e2014-05-09 13:31:43 +08001742static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001744 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1745 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001746}
1747
1748static void fib6_flush_trees(struct net *net)
1749{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001750 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001751
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001752 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*
1756 * Garbage collection
1757 */
1758
Michal Kubeček3570df92016-03-08 14:44:25 +01001759struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 int timeout;
1762 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001763};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765static int fib6_age(struct rt6_info *rt, void *arg)
1766{
Michal Kubeček3570df92016-03-08 14:44:25 +01001767 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 unsigned long now = jiffies;
1769
1770 /*
1771 * check addrconf expiration here.
1772 * Routes are expired even if they are in use.
1773 *
1774 * Also age clones. Note, that clones are aged out
1775 * only if they are not in use now.
1776 */
1777
David S. Millerd1918542011-12-28 20:19:20 -05001778 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1779 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return -1;
1782 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001783 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001785 if (atomic_read(&rt->dst.__refcnt) == 0 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001786 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 RT6_TRACE("aging clone %p\n", rt);
1788 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001789 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1790 struct neighbour *neigh;
1791 __u8 neigh_flags = 0;
1792
1793 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1794 if (neigh) {
1795 neigh_flags = neigh->flags;
1796 neigh_release(neigh);
1797 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001798 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001799 RT6_TRACE("purging route %p via non-router but gateway\n",
1800 rt);
1801 return -1;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001804 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
1807 return 0;
1808}
1809
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001810void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Michal Kubeček3570df92016-03-08 14:44:25 +01001812 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001813 unsigned long now;
1814
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001815 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001816 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1817 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001818 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001821 gc_args.timeout = expires ? (int)expires :
1822 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001824 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001825
Michal Kubeček3570df92016-03-08 14:44:25 +01001826 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001827 now = jiffies;
1828 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001831 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001832 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001833 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001834 else
1835 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001836 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001839static void fib6_gc_timer_cb(unsigned long arg)
1840{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001841 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001842}
1843
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001844static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001845{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001846 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1847
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001848 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001849 rwlock_init(&net->ipv6.fib6_walker_lock);
1850 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001851 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001852
Benjamin Theryc5728722008-03-03 23:34:17 -08001853 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1854 if (!net->ipv6.rt6_stats)
1855 goto out_timer;
1856
Eric Dumazet10da66f2010-10-13 08:22:03 +00001857 /* Avoid false sharing : Use at least a full cache line */
1858 size = max_t(size_t, size, L1_CACHE_BYTES);
1859
1860 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001861 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001862 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001863
1864 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1865 GFP_KERNEL);
1866 if (!net->ipv6.fib6_main_tbl)
1867 goto out_fib_table_hash;
1868
1869 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001870 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001871 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1872 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001873 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001874
1875#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1876 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1877 GFP_KERNEL);
1878 if (!net->ipv6.fib6_local_tbl)
1879 goto out_fib6_main_tbl;
1880 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001881 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001882 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1883 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001884 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001885#endif
1886 fib6_tables_init(net);
1887
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001888 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001889
1890#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1891out_fib6_main_tbl:
1892 kfree(net->ipv6.fib6_main_tbl);
1893#endif
1894out_fib_table_hash:
1895 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001896out_rt6_stats:
1897 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001898out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001899 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001900}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001901
1902static void fib6_net_exit(struct net *net)
1903{
Sabrina Dubrocac9335db2017-09-08 10:26:19 +02001904 unsigned int i;
1905
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001906 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001907 del_timer_sync(&net->ipv6.ip6_fib_timer);
1908
Eric Dumazetbf8ed952017-09-08 15:48:47 -07001909 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocac9335db2017-09-08 10:26:19 +02001910 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
1911 struct hlist_node *tmp;
1912 struct fib6_table *tb;
1913
1914 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
1915 hlist_del(&tb->tb6_hlist);
1916 fib6_free_table(tb);
1917 }
1918 }
1919
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001920 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001921 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001922}
1923
1924static struct pernet_operations fib6_net_ops = {
1925 .init = fib6_net_init,
1926 .exit = fib6_net_exit,
1927};
1928
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001929int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001931 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1934 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001935 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001936 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001937 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001938 goto out;
1939
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001940 ret = register_pernet_subsys(&fib6_net_ops);
1941 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001942 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001943
1944 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1945 NULL);
1946 if (ret)
1947 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001948
1949 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001950out:
1951 return ret;
1952
David S. Millere8803b62012-06-16 01:12:19 -07001953out_unregister_subsys:
1954 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001955out_kmem_cache_create:
1956 kmem_cache_destroy(fib6_node_kmem);
1957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960void fib6_gc_cleanup(void)
1961{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001962 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 kmem_cache_destroy(fib6_node_kmem);
1964}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001965
1966#ifdef CONFIG_PROC_FS
1967
1968struct ipv6_route_iter {
1969 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001970 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001971 loff_t skip;
1972 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001973 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001974};
1975
1976static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1977{
1978 struct rt6_info *rt = v;
1979 struct ipv6_route_iter *iter = seq->private;
1980
1981 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1982
1983#ifdef CONFIG_IPV6_SUBTREES
1984 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1985#else
1986 seq_puts(seq, "00000000000000000000000000000000 00 ");
1987#endif
1988 if (rt->rt6i_flags & RTF_GATEWAY)
1989 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1990 else
1991 seq_puts(seq, "00000000000000000000000000000000");
1992
1993 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1994 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1995 rt->dst.__use, rt->rt6i_flags,
1996 rt->dst.dev ? rt->dst.dev->name : "");
1997 iter->w.leaf = NULL;
1998 return 0;
1999}
2000
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002001static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002002{
2003 struct ipv6_route_iter *iter = w->args;
2004
2005 if (!iter->skip)
2006 return 1;
2007
2008 do {
2009 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2010 iter->skip--;
2011 if (!iter->skip && iter->w.leaf)
2012 return 1;
2013 } while (iter->w.leaf);
2014
2015 return 0;
2016}
2017
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002018static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2019 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002020{
2021 memset(&iter->w, 0, sizeof(iter->w));
2022 iter->w.func = ipv6_route_yield;
2023 iter->w.root = &iter->tbl->tb6_root;
2024 iter->w.state = FWS_INIT;
2025 iter->w.node = iter->w.root;
2026 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002027 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002028 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002029 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002030}
2031
2032static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2033 struct net *net)
2034{
2035 unsigned int h;
2036 struct hlist_node *node;
2037
2038 if (tbl) {
2039 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2040 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2041 } else {
2042 h = 0;
2043 node = NULL;
2044 }
2045
2046 while (!node && h < FIB6_TABLE_HASHSZ) {
2047 node = rcu_dereference_bh(
2048 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2049 }
2050 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2051}
2052
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002053static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2054{
2055 if (iter->sernum != iter->w.root->fn_sernum) {
2056 iter->sernum = iter->w.root->fn_sernum;
2057 iter->w.state = FWS_INIT;
2058 iter->w.node = iter->w.root;
2059 WARN_ON(iter->w.skip);
2060 iter->w.skip = iter->w.count;
2061 }
2062}
2063
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002064static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2065{
2066 int r;
2067 struct rt6_info *n;
2068 struct net *net = seq_file_net(seq);
2069 struct ipv6_route_iter *iter = seq->private;
2070
2071 if (!v)
2072 goto iter_table;
2073
2074 n = ((struct rt6_info *)v)->dst.rt6_next;
2075 if (n) {
2076 ++*pos;
2077 return n;
2078 }
2079
2080iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002081 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002082 read_lock(&iter->tbl->tb6_lock);
2083 r = fib6_walk_continue(&iter->w);
2084 read_unlock(&iter->tbl->tb6_lock);
2085 if (r > 0) {
2086 if (v)
2087 ++*pos;
2088 return iter->w.leaf;
2089 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002090 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002091 return NULL;
2092 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002093 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002094
2095 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2096 if (!iter->tbl)
2097 return NULL;
2098
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002099 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002100 goto iter_table;
2101}
2102
2103static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2104 __acquires(RCU_BH)
2105{
2106 struct net *net = seq_file_net(seq);
2107 struct ipv6_route_iter *iter = seq->private;
2108
2109 rcu_read_lock_bh();
2110 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2111 iter->skip = *pos;
2112
2113 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002114 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002115 return ipv6_route_seq_next(seq, NULL, pos);
2116 } else {
2117 return NULL;
2118 }
2119}
2120
2121static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2122{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002123 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002124 return w->node && !(w->state == FWS_U && w->node == w->root);
2125}
2126
2127static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2128 __releases(RCU_BH)
2129{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002130 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002131 struct ipv6_route_iter *iter = seq->private;
2132
2133 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002134 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002135
2136 rcu_read_unlock_bh();
2137}
2138
2139static const struct seq_operations ipv6_route_seq_ops = {
2140 .start = ipv6_route_seq_start,
2141 .next = ipv6_route_seq_next,
2142 .stop = ipv6_route_seq_stop,
2143 .show = ipv6_route_seq_show
2144};
2145
2146int ipv6_route_open(struct inode *inode, struct file *file)
2147{
2148 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2149 sizeof(struct ipv6_route_iter));
2150}
2151
2152#endif /* CONFIG_PROC_FS */