blob: 10b4b1f8b838854fc2f2d3b9ac0b04c06a700b2f [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 Wangc5cff852017-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 Wangc5cff852017-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 Laud52d3992015-05-22 20:56:06 -0700168static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
169{
170 int cpu;
171
172 if (!non_pcpu_rt->rt6i_pcpu)
173 return;
174
175 for_each_possible_cpu(cpu) {
176 struct rt6_info **ppcpu_rt;
177 struct rt6_info *pcpu_rt;
178
179 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
180 pcpu_rt = *ppcpu_rt;
181 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700182 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700183 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700184 *ppcpu_rt = NULL;
185 }
186 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700187
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700188 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700189 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700190}
191
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200192static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700194 if (atomic_dec_and_test(&rt->rt6i_ref)) {
195 rt6_free_pcpu(rt);
Wei Wang95145282017-06-17 10:42:34 -0700196 dst_dev_put(&rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700197 dst_release(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800201static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700202{
203 unsigned int h;
204
Thomas Graf375216a2006-10-21 20:20:54 -0700205 /*
206 * Initialize table lock at a single place to give lockdep a key,
207 * tables aren't visible prior to being linked to the list.
208 */
209 rwlock_init(&tb->tb6_lock);
210
Neil Hormana33bc5c2009-07-30 18:52:15 -0700211 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700212
213 /*
214 * No protection necessary, this is the only list mutatation
215 * operation, tables never disappear once they exist.
216 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800217 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700218}
219
220#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800221
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800222static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700223{
224 struct fib6_table *table;
225
226 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500227 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700228 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800229 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700230 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700231 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700232 }
233
234 return table;
235}
236
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800237struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700238{
239 struct fib6_table *tb;
240
241 if (id == 0)
242 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800243 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700244 if (tb)
245 return tb;
246
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800247 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500248 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800249 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700250
251 return tb;
252}
David Ahernb3b46632016-05-04 21:46:12 -0700253EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700254
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800255struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700256{
257 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700259 unsigned int h;
260
261 if (id == 0)
262 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700263 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700264 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800265 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800266 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700267 if (tb->tb6_id == id) {
268 rcu_read_unlock();
269 return tb;
270 }
271 }
272 rcu_read_unlock();
273
274 return NULL;
275}
David Ahernc4850682015-10-12 11:47:08 -0700276EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700277
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000278static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700279{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800280 fib6_link_table(net, net->ipv6.fib6_main_tbl);
281 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700282}
Thomas Grafc71099a2006-08-04 23:20:06 -0700283#else
284
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700286{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800287 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700288}
289
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800290struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700291{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800292 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700293}
294
David S. Miller4c9483b2011-03-12 16:22:43 -0500295struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800296 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700297{
lucienab997ad2015-10-23 15:36:53 +0800298 struct rt6_info *rt;
299
300 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300301 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800302 ip6_rt_put(rt);
303 rt = net->ipv6.ip6_null_entry;
304 dst_hold(&rt->dst);
305 }
306
307 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700308}
309
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000310static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700311{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800312 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700313}
314
315#endif
316
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200317static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700318{
319 int res;
320 struct rt6_info *rt;
321
Changli Gaod8d1f302010-06-10 23:31:35 -0700322 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700323 res = rt6_dump_route(rt, w->args);
324 if (res < 0) {
325 /* Frame is full, suspend walking */
326 w->leaf = rt;
327 return 1;
328 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800329
330 /* Multipath routes are dumped in one route with the
331 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
332 * last sibling of this route (no need to dump the
333 * sibling routes again)
334 */
335 if (rt->rt6i_nsiblings)
336 rt = list_last_entry(&rt->rt6i_siblings,
337 struct rt6_info,
338 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700339 }
340 w->leaf = NULL;
341 return 0;
342}
343
344static void fib6_dump_end(struct netlink_callback *cb)
345{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100346 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200347 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700348
349 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800350 if (cb->args[4]) {
351 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100352 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800353 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700354 cb->args[2] = 0;
355 kfree(w);
356 }
Wang Yufen437de072014-03-28 12:07:04 +0800357 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700358 cb->args[1] = 3;
359}
360
361static int fib6_dump_done(struct netlink_callback *cb)
362{
363 fib6_dump_end(cb);
364 return cb->done ? cb->done(cb) : 0;
365}
366
367static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
368 struct netlink_callback *cb)
369{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100370 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200371 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700372 int res;
373
374 w = (void *)cb->args[2];
375 w->root = &table->tb6_root;
376
377 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000378 w->count = 0;
379 w->skip = 0;
380
Patrick McHardy1b43af52006-08-10 23:11:17 -0700381 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100382 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700383 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000384 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700385 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000386 cb->args[5] = w->root->fn_sernum;
387 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700388 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000389 if (cb->args[5] != w->root->fn_sernum) {
390 /* Begin at the root if the tree changed */
391 cb->args[5] = w->root->fn_sernum;
392 w->state = FWS_INIT;
393 w->node = w->root;
394 w->skip = w->count;
395 } else
396 w->skip = 0;
397
Patrick McHardy1b43af52006-08-10 23:11:17 -0700398 read_lock_bh(&table->tb6_lock);
399 res = fib6_walk_continue(w);
400 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800401 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100402 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800403 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700404 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700405 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800406
Patrick McHardy1b43af52006-08-10 23:11:17 -0700407 return res;
408}
409
Thomas Grafc127ea22007-03-22 11:58:32 -0700410static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700411{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900412 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700413 unsigned int h, s_h;
414 unsigned int e = 0, s_e;
415 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200416 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700417 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800418 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700419 int res = 0;
420
421 s_h = cb->args[0];
422 s_e = cb->args[1];
423
424 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500425 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700426 /* New dump:
427 *
428 * 1. hook callback destructor.
429 */
430 cb->args[3] = (long)cb->done;
431 cb->done = fib6_dump_done;
432
433 /*
434 * 2. allocate and initialize walker.
435 */
436 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500437 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700438 return -ENOMEM;
439 w->func = fib6_dump_node;
440 cb->args[2] = (long)w;
441 }
442
443 arg.skb = skb;
444 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700445 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700446 w->args = &arg;
447
Eric Dumazete67f88d2011-04-27 22:56:07 +0000448 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700449 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700450 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800451 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800452 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700453 if (e < s_e)
454 goto next;
455 res = fib6_dump_table(tb, skb, cb);
456 if (res != 0)
457 goto out;
458next:
459 e++;
460 }
461 }
462out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000463 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700464 cb->args[1] = e;
465 cb->args[0] = h;
466
467 res = res < 0 ? res : skb->len;
468 if (res <= 0)
469 fib6_dump_end(cb);
470 return res;
471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473/*
474 * Routing Table
475 *
476 * return the appropriate node for a routing tree "add" operation
477 * by either creating and inserting or by returning an existing
478 * node.
479 */
480
fan.du9225b232013-07-22 14:21:09 +0800481static struct fib6_node *fib6_add_1(struct fib6_node *root,
482 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000483 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600484 int replace_required, int sernum,
485 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct fib6_node *fn, *in, *ln;
488 struct fib6_node *pn = NULL;
489 struct rt6key *key;
490 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900491 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 RT6_TRACE("fib6_add_1\n");
494
495 /* insert node in tree */
496
497 fn = root;
498
499 do {
500 key = (struct rt6key *)((u8 *)fn->leaf + offset);
501
502 /*
503 * Prefix match
504 */
505 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000506 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000507 if (!allow_create) {
508 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600509 NL_SET_ERR_MSG(extack,
510 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000511 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000512 return ERR_PTR(-ENOENT);
513 }
Joe Perchesf3213832012-05-15 14:11:53 +0000514 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000517 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * Exact match ?
521 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (plen == fn->fn_bit) {
524 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500525 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rt6_release(fn->leaf);
527 fn->leaf = NULL;
528 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return fn;
533 }
534
535 /*
536 * We have more bits to go
537 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Try to walk down on tree. */
540 fn->fn_sernum = sernum;
541 dir = addr_bit_set(addr, fn->fn_bit);
542 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800543 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } while (fn);
545
Matti Vaittinen14df0152011-11-16 21:18:02 +0000546 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000547 /* We should not create new node because
548 * NLM_F_REPLACE was specified without NLM_F_CREATE
549 * I assume it is safe to require NLM_F_CREATE when
550 * REPLACE flag is used! Later we may want to remove the
551 * check for replace_required, because according
552 * to netlink specification, NLM_F_CREATE
553 * MUST be specified if new route is created.
554 * That would keep IPv6 consistent with IPv4
555 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000556 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600557 NL_SET_ERR_MSG(extack,
558 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000559 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000560 return ERR_PTR(-ENOENT);
561 }
Joe Perchesf3213832012-05-15 14:11:53 +0000562 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * We walked to the bottom of tree.
566 * Create new leaf node without children.
567 */
568
569 ln = node_alloc();
570
David S. Miller507c9b12011-12-03 17:50:45 -0500571 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000572 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ln->parent = pn;
576 ln->fn_sernum = sernum;
577
578 if (dir)
579 pn->right = ln;
580 else
581 pn->left = ln;
582
583 return ln;
584
585
586insert_above:
587 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900588 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * we have a less significant route.
590 * we've to insert an intermediate node on the list
591 * this new node will point to the one we need to create
592 * and the current
593 */
594
595 pn = fn->parent;
596
597 /* find 1st bit in difference between the 2 addrs.
598
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800599 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 but if it is >= plen, the value is ignored in any case.
601 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900602
fan.du9225b232013-07-22 14:21:09 +0800603 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900605 /*
606 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * / \
608 * (new leaf node)[ln] (old node)[fn]
609 */
610 if (plen > bit) {
611 in = node_alloc();
612 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900613
David S. Miller507c9b12011-12-03 17:50:45 -0500614 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (in)
Wei Wangc5cff852017-08-21 09:47:10 -0700616 node_free_immediate(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (ln)
Wei Wangc5cff852017-08-21 09:47:10 -0700618 node_free_immediate(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000619 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900622 /*
623 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * RTN_RTINFO will
625 * be off since that an address that chooses one of
626 * the branches would not match less specific routes
627 * in the other branch
628 */
629
630 in->fn_bit = bit;
631
632 in->parent = pn;
633 in->leaf = fn->leaf;
634 atomic_inc(&in->leaf->rt6i_ref);
635
636 in->fn_sernum = sernum;
637
638 /* update parent pointer */
639 if (dir)
640 pn->right = in;
641 else
642 pn->left = in;
643
644 ln->fn_bit = plen;
645
646 ln->parent = in;
647 fn->parent = in;
648
649 ln->fn_sernum = sernum;
650
651 if (addr_bit_set(addr, bit)) {
652 in->right = ln;
653 in->left = fn;
654 } else {
655 in->left = ln;
656 in->right = fn;
657 }
658 } else { /* plen <= bit */
659
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900660 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * (new leaf node)[ln]
662 * / \
663 * (old node)[fn] NULL
664 */
665
666 ln = node_alloc();
667
David S. Miller507c9b12011-12-03 17:50:45 -0500668 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000669 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 ln->fn_bit = plen;
672
673 ln->parent = pn;
674
675 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (dir)
678 pn->right = ln;
679 else
680 pn->left = ln;
681
682 if (addr_bit_set(&key->addr, plen))
683 ln->right = fn;
684 else
685 ln->left = fn;
686
687 fn->parent = ln;
688 }
689 return ln;
690}
691
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200692static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200693{
694 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
695 RTF_GATEWAY;
696}
697
Florian Westphale715b6d2015-01-05 23:57:44 +0100698static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100699{
Florian Westphale715b6d2015-01-05 23:57:44 +0100700 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100701
Florian Westphale715b6d2015-01-05 23:57:44 +0100702 for (i = 0; i < RTAX_MAX; i++) {
703 if (test_bit(i, mxc->mx_valid))
704 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100705 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100706}
707
708static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
709{
710 if (!mxc->mx)
711 return 0;
712
713 if (dst->flags & DST_HOST) {
714 u32 *mp = dst_metrics_write_ptr(dst);
715
716 if (unlikely(!mp))
717 return -ENOMEM;
718
719 fib6_copy_metrics(mp, mxc);
720 } else {
721 dst_init_metrics(dst, mxc->mx, false);
722
723 /* We've stolen mx now. */
724 mxc->mx = NULL;
725 }
726
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100727 return 0;
728}
729
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100730static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
731 struct net *net)
732{
733 if (atomic_read(&rt->rt6i_ref) != 1) {
734 /* This route is used as dummy address holder in some split
735 * nodes. It is not leaked, but it still holds other resources,
736 * which must be released in time. So, scan ascendant nodes
737 * and replace dummy references to this route with references
738 * to still alive ones.
739 */
740 while (fn) {
741 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
742 fn->leaf = fib6_find_prefix(net, fn);
743 atomic_inc(&fn->leaf->rt6i_ref);
744 rt6_release(rt);
745 }
746 fn = fn->parent;
747 }
748 /* No more references are possible at this point. */
749 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
750 }
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Insert routing information in a node.
755 */
756
757static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100758 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct rt6_info *iter = NULL;
761 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200762 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500763 int replace = (info->nlh &&
764 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
765 int add = (!info->nlh ||
766 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000767 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200768 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200769 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100770 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
David Ahern1f5e29c2017-01-31 16:51:37 -0800772 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
773 nlflags |= NLM_F_APPEND;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ins = &fn->leaf;
776
David S. Miller507c9b12011-12-03 17:50:45 -0500777 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Search for duplicates
780 */
781
782 if (iter->rt6i_metric == rt->rt6i_metric) {
783 /*
784 * Same priority level
785 */
David S. Miller507c9b12011-12-03 17:50:45 -0500786 if (info->nlh &&
787 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000788 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200789
790 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000791 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200792 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
793 found++;
794 break;
795 }
796 if (rt_can_ecmp)
797 fallback_ins = fallback_ins ?: ins;
798 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
David Ahernf06b7542017-07-05 14:41:46 -0600801 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000802 if (rt->rt6i_nsiblings)
803 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500804 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000806 if (!(rt->rt6i_flags & RTF_EXPIRES))
807 rt6_clean_expires(iter);
808 else
809 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700810 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -EEXIST;
812 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000813 /* If we have the same destination and the same metric,
814 * but not the same gateway, then the route we try to
815 * add is sibling to this route, increment our counter
816 * of siblings, and later we will add our route to the
817 * list.
818 * Only static routes (which don't have flag
819 * RTF_EXPIRES) are used for ECMPv6.
820 *
821 * To avoid long list, we only had siblings if the
822 * route have a gateway.
823 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200824 if (rt_can_ecmp &&
825 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000826 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 if (iter->rt6i_metric > rt->rt6i_metric)
830 break;
831
Michal Kubeček27596472015-05-18 20:54:00 +0200832next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700833 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Michal Kubeček27596472015-05-18 20:54:00 +0200836 if (fallback_ins && !found) {
837 /* No ECMP-able route found, replace first non-ECMP one */
838 ins = fallback_ins;
839 iter = *ins;
840 found++;
841 }
842
David S. Millerf11e6652007-03-24 20:36:25 -0700843 /* Reset round-robin state, if necessary */
844 if (ins == &fn->leaf)
845 fn->rr_ptr = NULL;
846
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000847 /* Link this route to others same route. */
848 if (rt->rt6i_nsiblings) {
849 unsigned int rt6i_nsiblings;
850 struct rt6_info *sibling, *temp_sibling;
851
852 /* Find the first route that have the same metric */
853 sibling = fn->leaf;
854 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200855 if (sibling->rt6i_metric == rt->rt6i_metric &&
856 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000857 list_add_tail(&rt->rt6i_siblings,
858 &sibling->rt6i_siblings);
859 break;
860 }
861 sibling = sibling->dst.rt6_next;
862 }
863 /* For each sibling in the list, increment the counter of
864 * siblings. BUG() if counters does not match, list of siblings
865 * is broken!
866 */
867 rt6i_nsiblings = 0;
868 list_for_each_entry_safe(sibling, temp_sibling,
869 &rt->rt6i_siblings, rt6i_siblings) {
870 sibling->rt6i_nsiblings++;
871 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
872 rt6i_nsiblings++;
873 }
874 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * insert node
879 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000880 if (!replace) {
881 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000882 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000884add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200885 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100886 err = fib6_commit_metrics(&rt->dst, mxc);
887 if (err)
888 return err;
889
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000890 rt->dst.rt6_next = iter;
891 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -0700892 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000893 atomic_inc(&rt->rt6i_ref);
David Ahern3b1137f2017-02-02 12:37:10 -0800894 if (!info->skip_notify)
895 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000896 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
David S. Miller507c9b12011-12-03 17:50:45 -0500898 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000899 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
900 fn->fn_flags |= RTN_RTINFO;
901 }
902
903 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200904 int nsiblings;
905
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000906 if (!found) {
907 if (add)
908 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000909 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000910 return -ENOENT;
911 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100912
913 err = fib6_commit_metrics(&rt->dst, mxc);
914 if (err)
915 return err;
916
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000917 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -0700918 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000919 rt->dst.rt6_next = iter->dst.rt6_next;
920 atomic_inc(&rt->rt6i_ref);
David Ahern3b1137f2017-02-02 12:37:10 -0800921 if (!info->skip_notify)
922 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -0500923 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000924 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
925 fn->fn_flags |= RTN_RTINFO;
926 }
Michal Kubeček27596472015-05-18 20:54:00 +0200927 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100928 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -0700929 if (fn->rr_ptr == iter)
930 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100931 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200932
933 if (nsiblings) {
934 /* Replacing an ECMP route, remove all siblings */
935 ins = &rt->dst.rt6_next;
936 iter = *ins;
937 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +0100938 if (iter->rt6i_metric > rt->rt6i_metric)
939 break;
Michal Kubeček27596472015-05-18 20:54:00 +0200940 if (rt6_qualify_for_ecmp(iter)) {
941 *ins = iter->dst.rt6_next;
942 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -0700943 if (fn->rr_ptr == iter)
944 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +0200945 rt6_release(iter);
946 nsiblings--;
947 } else {
948 ins = &iter->dst.rt6_next;
949 }
950 iter = *ins;
951 }
952 WARN_ON(nsiblings != 0);
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956 return 0;
957}
958
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200959static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700961 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500962 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700963 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700964 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800967void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700969 if (!timer_pending(&net->ipv6.ip6_fib_timer))
970 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700971 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * Add routing information to the routing tree.
976 * <destination addr>/<source addr>
977 * with source addr info in sub-trees
978 */
979
Florian Westphale715b6d2015-01-05 23:57:44 +0100980int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -0600981 struct nl_info *info, struct mx6_config *mxc,
982 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700984 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000986 int allow_create = 1;
987 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200988 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500989
Wei Wanga4c2fd72017-06-17 10:42:42 -0700990 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700991 return -EINVAL;
992
David S. Miller507c9b12011-12-03 17:50:45 -0500993 if (info->nlh) {
994 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000995 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500996 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000997 replace_required = 1;
998 }
999 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001000 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
fan.du9225b232013-07-22 14:21:09 +08001002 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1003 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -06001004 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001005 if (IS_ERR(fn)) {
1006 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001007 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001011 pn = fn;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#ifdef CONFIG_IPV6_SUBTREES
1014 if (rt->rt6i_src.plen) {
1015 struct fib6_node *sn;
1016
David S. Miller507c9b12011-12-03 17:50:45 -05001017 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct fib6_node *sfn;
1019
1020 /*
1021 * Create subtree.
1022 *
1023 * fn[main tree]
1024 * |
1025 * sfn[subtree root]
1026 * \
1027 * sn[new leaf node]
1028 */
1029
1030 /* Create subtree root node */
1031 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001032 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001033 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001035 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1036 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001038 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* Now add the first leaf node to new subtree */
1041
1042 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001043 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001044 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001045 allow_create, replace_required, sernum,
1046 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001048 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001050 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 in main tree.
1052 */
Wei Wangc5cff852017-08-21 09:47:10 -07001053 node_free_immediate(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001054 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001055 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
1058 /* Now link new subtree to main tree */
1059 sfn->parent = fn;
1060 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 } else {
1062 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001063 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001064 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001065 allow_create, replace_required, sernum,
1066 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001068 if (IS_ERR(sn)) {
1069 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001070 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
David S. Miller507c9b12011-12-03 17:50:45 -05001074 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001075 fn->leaf = rt;
1076 atomic_inc(&rt->rt6i_ref);
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 fn = sn;
1079 }
1080#endif
1081
Florian Westphale715b6d2015-01-05 23:57:44 +01001082 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001083 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001084 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001085 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001086 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001090 if (err) {
1091#ifdef CONFIG_IPV6_SUBTREES
1092 /*
1093 * If fib6_add_1 has cleared the old leaf pointer in the
1094 * super-tree leaf node we have to find a new one for it.
1095 */
David S. Miller3c051232008-04-18 01:46:19 -07001096 if (pn != fn && pn->leaf == rt) {
1097 pn->leaf = NULL;
1098 atomic_dec(&rt->rt6i_ref);
1099 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001100 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001101 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001102#if RT6_DEBUG >= 2
1103 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001104 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001105 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001106 }
1107#endif
1108 atomic_inc(&pn->leaf->rt6i_ref);
1109 }
1110#endif
Wei Wang348a4002017-08-18 17:14:49 -07001111 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return err;
1114
Wei Wang348a4002017-08-18 17:14:49 -07001115failure:
1116 /* fn->leaf could be NULL if fn is an intermediate node and we
1117 * failed to add the new route to it in both subtree creation
1118 * failure and fib6_add_rt2node() failure case.
1119 * In both cases, fib6_repair_tree() should be called to fix
1120 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001123 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001124 /* Always release dst as dst->__refcnt is guaranteed
1125 * to be taken before entering this function
1126 */
Wei Wang587fea72017-06-17 10:42:36 -07001127 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131/*
1132 * Routing tree lookup
1133 *
1134 */
1135
1136struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001137 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001138 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139};
1140
Wang Yufen437de072014-03-28 12:07:04 +08001141static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1142 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001145 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001147 if (unlikely(args->offset == 0))
1148 return NULL;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * Descend on a tree
1152 */
1153
1154 fn = root;
1155
1156 for (;;) {
1157 struct fib6_node *next;
1158
1159 dir = addr_bit_set(args->addr, fn->fn_bit);
1160
1161 next = dir ? fn->right : fn->left;
1162
1163 if (next) {
1164 fn = next;
1165 continue;
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 }
1169
David S. Miller507c9b12011-12-03 17:50:45 -05001170 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001171 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct rt6key *key;
1173
1174 key = (struct rt6key *) ((u8 *) fn->leaf +
1175 args->offset);
1176
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001177 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1178#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001179 if (fn->subtree) {
1180 struct fib6_node *sfn;
1181 sfn = fib6_lookup_1(fn->subtree,
1182 args + 1);
1183 if (!sfn)
1184 goto backtrack;
1185 fn = sfn;
1186 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001187#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001188 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001189 return fn;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001192#ifdef CONFIG_IPV6_SUBTREES
1193backtrack:
1194#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001195 if (fn->fn_flags & RTN_ROOT)
1196 break;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 fn = fn->parent;
1199 }
1200
1201 return NULL;
1202}
1203
Wang Yufen437de072014-03-28 12:07:04 +08001204struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1205 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001208 struct lookup_args args[] = {
1209 {
1210 .offset = offsetof(struct rt6_info, rt6i_dst),
1211 .addr = daddr,
1212 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001214 {
1215 .offset = offsetof(struct rt6_info, rt6i_src),
1216 .addr = saddr,
1217 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001219 {
1220 .offset = 0, /* sentinel */
1221 }
1222 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001224 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001225 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 fn = root;
1227
1228 return fn;
1229}
1230
1231/*
1232 * Get node with specified destination prefix (and source prefix,
1233 * if subtrees are used)
1234 */
1235
1236
Wang Yufen437de072014-03-28 12:07:04 +08001237static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1238 const struct in6_addr *addr,
1239 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct fib6_node *fn;
1242
1243 for (fn = root; fn ; ) {
1244 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1245
1246 /*
1247 * Prefix match
1248 */
1249 if (plen < fn->fn_bit ||
1250 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1251 return NULL;
1252
1253 if (plen == fn->fn_bit)
1254 return fn;
1255
1256 /*
1257 * We have more bits to go
1258 */
1259 if (addr_bit_set(addr, fn->fn_bit))
1260 fn = fn->right;
1261 else
1262 fn = fn->left;
1263 }
1264 return NULL;
1265}
1266
Wang Yufen437de072014-03-28 12:07:04 +08001267struct fib6_node *fib6_locate(struct fib6_node *root,
1268 const struct in6_addr *daddr, int dst_len,
1269 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct fib6_node *fn;
1272
1273 fn = fib6_locate_1(root, daddr, dst_len,
1274 offsetof(struct rt6_info, rt6i_dst));
1275
1276#ifdef CONFIG_IPV6_SUBTREES
1277 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001278 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001279 if (fn && fn->subtree)
1280 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 offsetof(struct rt6_info, rt6i_src));
1282 }
1283#endif
1284
David S. Miller507c9b12011-12-03 17:50:45 -05001285 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return fn;
1287
1288 return NULL;
1289}
1290
1291
1292/*
1293 * Deletion
1294 *
1295 */
1296
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001297static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
David S. Miller507c9b12011-12-03 17:50:45 -05001299 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001300 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
David S. Miller507c9b12011-12-03 17:50:45 -05001302 while (fn) {
1303 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001305 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return fn->right->leaf;
1307
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001308 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 return NULL;
1311}
1312
1313/*
1314 * Called to trim the tree of intermediate nodes when possible. "fn"
1315 * is the node we want to try and remove.
1316 */
1317
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001318static struct fib6_node *fib6_repair_tree(struct net *net,
1319 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 int children;
1322 int nstate;
1323 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001324 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int iter = 0;
1326
1327 for (;;) {
1328 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1329 iter++;
1330
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001331 WARN_ON(fn->fn_flags & RTN_RTINFO);
1332 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001333 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 children = 0;
1336 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001337 if (fn->right)
1338 child = fn->right, children |= 1;
1339 if (fn->left)
1340 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001342 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343#ifdef CONFIG_IPV6_SUBTREES
1344 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001345 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346#endif
1347 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001348 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001350 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001351 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001352 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354#endif
1355 atomic_inc(&fn->leaf->rt6i_ref);
1356 return fn->parent;
1357 }
1358
1359 pn = fn->parent;
1360#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001361 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001362 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001363 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 nstate = FWS_L;
1365 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001366 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001368 if (pn->right == fn)
1369 pn->right = child;
1370 else if (pn->left == fn)
1371 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001373 else
1374 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375#endif
1376 if (child)
1377 child->parent = pn;
1378 nstate = FWS_R;
1379#ifdef CONFIG_IPV6_SUBTREES
1380 }
1381#endif
1382
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001383 read_lock(&net->ipv6.fib6_walker_lock);
1384 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001385 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (w->root == fn) {
1387 w->root = w->node = NULL;
1388 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1389 } else if (w->node == fn) {
1390 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1391 w->node = pn;
1392 w->state = nstate;
1393 }
1394 } else {
1395 if (w->root == fn) {
1396 w->root = child;
1397 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1398 }
1399 if (w->node == fn) {
1400 w->node = child;
1401 if (children&2) {
1402 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001403 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 } else {
1405 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001406 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 }
1409 }
1410 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001411 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001414 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return pn;
1416
1417 rt6_release(pn->leaf);
1418 pn->leaf = NULL;
1419 fn = pn;
1420 }
1421}
1422
1423static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001424 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001426 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001428 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 RT6_TRACE("fib6_del_route\n");
1431
1432 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001433 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001435 net->ipv6.rt6_stats->fib_rt_entries--;
1436 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
David S. Millerf11e6652007-03-24 20:36:25 -07001438 /* Reset round-robin state, if necessary */
1439 if (fn->rr_ptr == rt)
1440 fn->rr_ptr = NULL;
1441
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001442 /* Remove this entry from other siblings */
1443 if (rt->rt6i_nsiblings) {
1444 struct rt6_info *sibling, *next_sibling;
1445
1446 list_for_each_entry_safe(sibling, next_sibling,
1447 &rt->rt6i_siblings, rt6i_siblings)
1448 sibling->rt6i_nsiblings--;
1449 rt->rt6i_nsiblings = 0;
1450 list_del_init(&rt->rt6i_siblings);
1451 }
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001454 read_lock(&net->ipv6.fib6_walker_lock);
1455 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (w->state == FWS_C && w->leaf == rt) {
1457 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001458 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001459 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 w->state = FWS_U;
1461 }
1462 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001463 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Changli Gaod8d1f302010-06-10 23:31:35 -07001465 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001468 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001470 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001471 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001474 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
David Ahern16a16cd2017-02-02 12:37:11 -08001476 if (!info->skip_notify)
1477 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 rt6_release(rt);
1479}
1480
Thomas Graf86872cb2006-08-22 00:01:08 -07001481int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Wei Wang4e587ea2017-08-25 15:03:10 -07001483 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1484 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001485 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct rt6_info **rtp;
1487
1488#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001489 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001490 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -ENOENT;
1492 }
1493#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001494 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -ENOENT;
1496
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001497 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
David S. Miller507c9b12011-12-03 17:50:45 -05001499 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001500 struct fib6_node *pn = fn;
1501#ifdef CONFIG_IPV6_SUBTREES
1502 /* clones of this route might be in another subtree */
1503 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001504 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001505 pn = pn->parent;
1506 pn = pn->parent;
1507 }
1508#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001509 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /*
1513 * Walk the leaf entries looking for ourself
1514 */
1515
Changli Gaod8d1f302010-06-10 23:31:35 -07001516 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001518 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520 }
1521 }
1522 return -ENOENT;
1523}
1524
1525/*
1526 * Tree traversal function.
1527 *
1528 * Certainly, it is not interrupt safe.
1529 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1530 * It means, that we can modify tree during walking
1531 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001532 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 *
1534 * It guarantees that every node will be traversed,
1535 * and that it will be traversed only once.
1536 *
1537 * Callback function w->func may return:
1538 * 0 -> continue walking.
1539 * positive value -> walking is suspended (used by tree dumps,
1540 * and probably by gc, if it will be split to several slices)
1541 * negative value -> terminate walking.
1542 *
1543 * The function itself returns:
1544 * 0 -> walk is complete.
1545 * >0 -> walk is incomplete (i.e. suspended)
1546 * <0 -> walk is terminated by an error.
1547 */
1548
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001549static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct fib6_node *fn, *pn;
1552
1553 for (;;) {
1554 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001555 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557
1558 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001559 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 w->state = FWS_C;
1561 w->leaf = fn->leaf;
1562 }
1563 switch (w->state) {
1564#ifdef CONFIG_IPV6_SUBTREES
1565 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001566 if (FIB6_SUBTREE(fn)) {
1567 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 continue;
1569 }
1570 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 case FWS_L:
1573 if (fn->left) {
1574 w->node = fn->left;
1575 w->state = FWS_INIT;
1576 continue;
1577 }
1578 w->state = FWS_R;
1579 case FWS_R:
1580 if (fn->right) {
1581 w->node = fn->right;
1582 w->state = FWS_INIT;
1583 continue;
1584 }
1585 w->state = FWS_C;
1586 w->leaf = fn->leaf;
1587 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001588 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001589 int err;
1590
Eric Dumazetfa809e22012-06-25 15:37:19 -07001591 if (w->skip) {
1592 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001593 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001594 }
1595
1596 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (err)
1598 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001599
1600 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 continue;
1602 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001603skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 w->state = FWS_U;
1605 case FWS_U:
1606 if (fn == w->root)
1607 return 0;
1608 pn = fn->parent;
1609 w->node = pn;
1610#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001611 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001612 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 w->state = FWS_L;
1614 continue;
1615 }
1616#endif
1617 if (pn->left == fn) {
1618 w->state = FWS_R;
1619 continue;
1620 }
1621 if (pn->right == fn) {
1622 w->state = FWS_C;
1623 w->leaf = w->node->leaf;
1624 continue;
1625 }
1626#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001627 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628#endif
1629 }
1630 }
1631}
1632
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001633static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int res;
1636
1637 w->state = FWS_INIT;
1638 w->node = w->root;
1639
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001640 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 res = fib6_walk_continue(w);
1642 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001643 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return res;
1645}
1646
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001647static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 int res;
1650 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001651 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001652 struct nl_info info = {
1653 .nl_net = c->net,
1654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001656 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1657 w->node->fn_sernum != c->sernum)
1658 w->node->fn_sernum = c->sernum;
1659
1660 if (!c->func) {
1661 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1662 w->leaf = NULL;
1663 return 0;
1664 }
1665
Changli Gaod8d1f302010-06-10 23:31:35 -07001666 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 res = c->func(rt, c->arg);
1668 if (res < 0) {
1669 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001670 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (res) {
1672#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001673 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001674 __func__, rt,
1675 rcu_access_pointer(rt->rt6i_node),
1676 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677#endif
1678 continue;
1679 }
1680 return 0;
1681 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001682 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684 w->leaf = rt;
1685 return 0;
1686}
1687
1688/*
1689 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001690 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * func is called on each route.
1692 * It may return -1 -> delete this route.
1693 * 0 -> continue walking
1694 *
1695 * prune==1 -> only immediate children of node (certainly,
1696 * ignoring pure split nodes) will be scanned.
1697 */
1698
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001699static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001700 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001701 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001703 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 c.w.root = root;
1706 c.w.func = fib6_clean_node;
1707 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001708 c.w.count = 0;
1709 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001711 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001713 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001715 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001718static void __fib6_clean_all(struct net *net,
1719 int (*func)(struct rt6_info *, void *),
1720 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001721{
Thomas Grafc71099a2006-08-04 23:20:06 -07001722 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001723 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001724 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001725
Patrick McHardy1b43af52006-08-10 23:11:17 -07001726 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001727 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001728 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001729 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001730 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001731 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001732 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001733 write_unlock_bh(&table->tb6_lock);
1734 }
1735 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001736 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001737}
1738
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001739void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1740 void *arg)
1741{
1742 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1746{
1747 if (rt->rt6i_flags & RTF_CACHE) {
1748 RT6_TRACE("pruning clone %p\n", rt);
1749 return -1;
1750 }
1751
1752 return 0;
1753}
1754
Duan Jiong163cd4e2014-05-09 13:31:43 +08001755static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001757 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1758 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001759}
1760
1761static void fib6_flush_trees(struct net *net)
1762{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001763 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001764
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001765 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/*
1769 * Garbage collection
1770 */
1771
Michal Kubeček3570df92016-03-08 14:44:25 +01001772struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 int timeout;
1775 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001776};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778static int fib6_age(struct rt6_info *rt, void *arg)
1779{
Michal Kubeček3570df92016-03-08 14:44:25 +01001780 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned long now = jiffies;
1782
1783 /*
1784 * check addrconf expiration here.
1785 * Routes are expired even if they are in use.
1786 *
1787 * Also age clones. Note, that clones are aged out
1788 * only if they are not in use now.
1789 */
1790
David S. Millerd1918542011-12-28 20:19:20 -05001791 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1792 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return -1;
1795 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001796 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 } else if (rt->rt6i_flags & RTF_CACHE) {
Wei Wang1cfb71e2017-06-17 10:42:33 -07001798 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001799 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 RT6_TRACE("aging clone %p\n", rt);
1801 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001802 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1803 struct neighbour *neigh;
1804 __u8 neigh_flags = 0;
1805
1806 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1807 if (neigh) {
1808 neigh_flags = neigh->flags;
1809 neigh_release(neigh);
1810 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001811 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001812 RT6_TRACE("purging route %p via non-router but gateway\n",
1813 rt);
1814 return -1;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001817 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
1820 return 0;
1821}
1822
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001823void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Michal Kubeček3570df92016-03-08 14:44:25 +01001825 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001826 unsigned long now;
1827
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001828 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001829 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1830 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001831 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1832 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001834 gc_args.timeout = expires ? (int)expires :
1835 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001836 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001837
Michal Kubeček3570df92016-03-08 14:44:25 +01001838 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001839 now = jiffies;
1840 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001843 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001844 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001845 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001846 else
1847 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001848 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001851static void fib6_gc_timer_cb(unsigned long arg)
1852{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001853 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001854}
1855
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001856static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001857{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001858 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1859
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001860 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001861 rwlock_init(&net->ipv6.fib6_walker_lock);
1862 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001863 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001864
Benjamin Theryc5728722008-03-03 23:34:17 -08001865 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1866 if (!net->ipv6.rt6_stats)
1867 goto out_timer;
1868
Eric Dumazet10da66f2010-10-13 08:22:03 +00001869 /* Avoid false sharing : Use at least a full cache line */
1870 size = max_t(size_t, size, L1_CACHE_BYTES);
1871
1872 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001873 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001874 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001875
1876 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1877 GFP_KERNEL);
1878 if (!net->ipv6.fib6_main_tbl)
1879 goto out_fib_table_hash;
1880
1881 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001882 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001883 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1884 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001885 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001886
1887#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1888 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1889 GFP_KERNEL);
1890 if (!net->ipv6.fib6_local_tbl)
1891 goto out_fib6_main_tbl;
1892 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001893 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001894 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1895 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001896 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001897#endif
1898 fib6_tables_init(net);
1899
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001900 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001901
1902#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1903out_fib6_main_tbl:
1904 kfree(net->ipv6.fib6_main_tbl);
1905#endif
1906out_fib_table_hash:
1907 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001908out_rt6_stats:
1909 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001910out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001911 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001912}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001913
1914static void fib6_net_exit(struct net *net)
1915{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001916 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001917 del_timer_sync(&net->ipv6.ip6_fib_timer);
1918
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001919#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001920 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001921 kfree(net->ipv6.fib6_local_tbl);
1922#endif
David S. Miller8e773272012-06-11 00:01:52 -07001923 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001924 kfree(net->ipv6.fib6_main_tbl);
1925 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001926 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001927}
1928
1929static struct pernet_operations fib6_net_ops = {
1930 .init = fib6_net_init,
1931 .exit = fib6_net_exit,
1932};
1933
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001934int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001936 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1939 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001940 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001941 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001942 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001943 goto out;
1944
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001945 ret = register_pernet_subsys(&fib6_net_ops);
1946 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001947 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001948
1949 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1950 NULL);
1951 if (ret)
1952 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001953
1954 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001955out:
1956 return ret;
1957
David S. Millere8803b62012-06-16 01:12:19 -07001958out_unregister_subsys:
1959 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001960out_kmem_cache_create:
1961 kmem_cache_destroy(fib6_node_kmem);
1962 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965void fib6_gc_cleanup(void)
1966{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001967 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 kmem_cache_destroy(fib6_node_kmem);
1969}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001970
1971#ifdef CONFIG_PROC_FS
1972
1973struct ipv6_route_iter {
1974 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001975 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001976 loff_t skip;
1977 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001978 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001979};
1980
1981static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1982{
1983 struct rt6_info *rt = v;
1984 struct ipv6_route_iter *iter = seq->private;
1985
1986 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1987
1988#ifdef CONFIG_IPV6_SUBTREES
1989 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1990#else
1991 seq_puts(seq, "00000000000000000000000000000000 00 ");
1992#endif
1993 if (rt->rt6i_flags & RTF_GATEWAY)
1994 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1995 else
1996 seq_puts(seq, "00000000000000000000000000000000");
1997
1998 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1999 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2000 rt->dst.__use, rt->rt6i_flags,
2001 rt->dst.dev ? rt->dst.dev->name : "");
2002 iter->w.leaf = NULL;
2003 return 0;
2004}
2005
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002006static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002007{
2008 struct ipv6_route_iter *iter = w->args;
2009
2010 if (!iter->skip)
2011 return 1;
2012
2013 do {
2014 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2015 iter->skip--;
2016 if (!iter->skip && iter->w.leaf)
2017 return 1;
2018 } while (iter->w.leaf);
2019
2020 return 0;
2021}
2022
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002023static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2024 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002025{
2026 memset(&iter->w, 0, sizeof(iter->w));
2027 iter->w.func = ipv6_route_yield;
2028 iter->w.root = &iter->tbl->tb6_root;
2029 iter->w.state = FWS_INIT;
2030 iter->w.node = iter->w.root;
2031 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002032 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002033 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002034 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002035}
2036
2037static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2038 struct net *net)
2039{
2040 unsigned int h;
2041 struct hlist_node *node;
2042
2043 if (tbl) {
2044 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2045 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2046 } else {
2047 h = 0;
2048 node = NULL;
2049 }
2050
2051 while (!node && h < FIB6_TABLE_HASHSZ) {
2052 node = rcu_dereference_bh(
2053 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2054 }
2055 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2056}
2057
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002058static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2059{
2060 if (iter->sernum != iter->w.root->fn_sernum) {
2061 iter->sernum = iter->w.root->fn_sernum;
2062 iter->w.state = FWS_INIT;
2063 iter->w.node = iter->w.root;
2064 WARN_ON(iter->w.skip);
2065 iter->w.skip = iter->w.count;
2066 }
2067}
2068
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002069static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2070{
2071 int r;
2072 struct rt6_info *n;
2073 struct net *net = seq_file_net(seq);
2074 struct ipv6_route_iter *iter = seq->private;
2075
2076 if (!v)
2077 goto iter_table;
2078
2079 n = ((struct rt6_info *)v)->dst.rt6_next;
2080 if (n) {
2081 ++*pos;
2082 return n;
2083 }
2084
2085iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002086 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002087 read_lock(&iter->tbl->tb6_lock);
2088 r = fib6_walk_continue(&iter->w);
2089 read_unlock(&iter->tbl->tb6_lock);
2090 if (r > 0) {
2091 if (v)
2092 ++*pos;
2093 return iter->w.leaf;
2094 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002095 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002096 return NULL;
2097 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002098 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002099
2100 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2101 if (!iter->tbl)
2102 return NULL;
2103
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 goto iter_table;
2106}
2107
2108static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2109 __acquires(RCU_BH)
2110{
2111 struct net *net = seq_file_net(seq);
2112 struct ipv6_route_iter *iter = seq->private;
2113
2114 rcu_read_lock_bh();
2115 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2116 iter->skip = *pos;
2117
2118 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002119 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002120 return ipv6_route_seq_next(seq, NULL, pos);
2121 } else {
2122 return NULL;
2123 }
2124}
2125
2126static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2127{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002128 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002129 return w->node && !(w->state == FWS_U && w->node == w->root);
2130}
2131
2132static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2133 __releases(RCU_BH)
2134{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002135 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002136 struct ipv6_route_iter *iter = seq->private;
2137
2138 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002139 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002140
2141 rcu_read_unlock_bh();
2142}
2143
2144static const struct seq_operations ipv6_route_seq_ops = {
2145 .start = ipv6_route_seq_start,
2146 .next = ipv6_route_seq_next,
2147 .stop = ipv6_route_seq_stop,
2148 .show = ipv6_route_seq_show
2149};
2150
2151int ipv6_route_open(struct inode *inode, struct file *file)
2152{
2153 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2154 sizeof(struct ipv6_route_iter));
2155}
2156
2157#endif /* CONFIG_PROC_FS */