blob: 636d4d893085c109067ea3daf43169d17ba9da4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020035#include <net/lwtunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <net/ip6_fib.h>
38#include <net/ip6_route.h>
39
40#define RT6_DEBUG 2
41
42#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000043#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define RT6_TRACE(x...) do { ; } while (0)
46#endif
47
Wang Yufen437de072014-03-28 12:07:04 +080048static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020050struct fib6_cleaner {
51 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080052 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020054 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void *arg;
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifdef CONFIG_IPV6_SUBTREES
59#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
Duan Jiong163cd4e2014-05-09 13:31:43 +080064static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080065static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
66static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010067static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020068static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * A routing update causes an increase of the serial number on the
72 * affected subtree. This allows for cached routes to be asynchronously
73 * tested when modifications are made to the destination cache as a
74 * result of redirects, path MTU changes, etc.
75 */
76
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080077static void fib6_gc_timer_cb(unsigned long arg);
78
Michal Kubeček9a03cd82016-03-08 14:44:35 +010079#define FOR_WALKERS(net, w) \
80 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Michal Kubeček9a03cd82016-03-08 14:44:35 +010082static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070083{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010084 write_lock_bh(&net->ipv6.fib6_walker_lock);
85 list_add(&w->lh, &net->ipv6.fib6_walkers);
86 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070087}
88
Michal Kubeček9a03cd82016-03-08 14:44:35 +010089static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070090{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010091 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000092 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010093 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070094}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020095
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020096static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020098 int new, old;
99
100 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200101 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200102 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200103 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
104 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200105 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200108enum {
109 FIB6_NO_SERNUM_CHANGE = 0,
110};
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Auxiliary address test functions for the radix tree.
114 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900115 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * 64bit processors)
117 */
118
119/*
120 * test bit
121 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000122#if defined(__LITTLE_ENDIAN)
123# define BITOP_BE32_SWIZZLE (0x1F & ~7)
124#else
125# define BITOP_BE32_SWIZZLE 0
126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200128static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000130 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000131 /*
132 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800133 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000134 * is optimized version of
135 * htonl(1 << ((~fn_bit)&0x1F))
136 * See include/asm-generic/bitops/le.h.
137 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700138 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
139 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200142static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct fib6_node *fn;
145
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800146 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return fn;
149}
150
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200151static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 kmem_cache_free(fib6_node_kmem, fn);
154}
155
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700156static void rt6_rcu_free(struct rt6_info *rt)
157{
158 call_rcu(&rt->dst.rcu_head, dst_rcu_free);
159}
160
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700161static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
162{
163 int cpu;
164
165 if (!non_pcpu_rt->rt6i_pcpu)
166 return;
167
168 for_each_possible_cpu(cpu) {
169 struct rt6_info **ppcpu_rt;
170 struct rt6_info *pcpu_rt;
171
172 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
173 pcpu_rt = *ppcpu_rt;
174 if (pcpu_rt) {
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700175 rt6_rcu_free(pcpu_rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700176 *ppcpu_rt = NULL;
177 }
178 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700179
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700180 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700181 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700182}
183
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200184static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700186 if (atomic_dec_and_test(&rt->rt6i_ref)) {
187 rt6_free_pcpu(rt);
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700188 rt6_rcu_free(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800192static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700193{
194 unsigned int h;
195
Thomas Graf375216a2006-10-21 20:20:54 -0700196 /*
197 * Initialize table lock at a single place to give lockdep a key,
198 * tables aren't visible prior to being linked to the list.
199 */
200 rwlock_init(&tb->tb6_lock);
201
Neil Hormana33bc5c2009-07-30 18:52:15 -0700202 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700203
204 /*
205 * No protection necessary, this is the only list mutatation
206 * operation, tables never disappear once they exist.
207 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800208 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700209}
210
211#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800212
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800213static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700214{
215 struct fib6_table *table;
216
217 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500218 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700219 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800220 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700222 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700223 }
224
225 return table;
226}
227
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800228struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700229{
230 struct fib6_table *tb;
231
232 if (id == 0)
233 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800234 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700235 if (tb)
236 return tb;
237
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800238 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500239 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700241
242 return tb;
243}
David Ahernb3b46632016-05-04 21:46:12 -0700244EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700245
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800246struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700247{
248 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800249 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700250 unsigned int h;
251
252 if (id == 0)
253 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700254 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700255 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700258 if (tb->tb6_id == id) {
259 rcu_read_unlock();
260 return tb;
261 }
262 }
263 rcu_read_unlock();
264
265 return NULL;
266}
David Ahernc4850682015-10-12 11:47:08 -0700267EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700268
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000269static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700270{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800271 fib6_link_table(net, net->ipv6.fib6_main_tbl);
272 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700273}
Thomas Grafc71099a2006-08-04 23:20:06 -0700274#else
275
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800276struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700277{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800278 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700279}
280
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800281struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700282{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800283 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700284}
285
David S. Miller4c9483b2011-03-12 16:22:43 -0500286struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800287 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700288{
lucienab997ad2015-10-23 15:36:53 +0800289 struct rt6_info *rt;
290
291 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovychb9ca9b02017-06-20 13:29:25 +0300292 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800293 ip6_rt_put(rt);
294 rt = net->ipv6.ip6_null_entry;
295 dst_hold(&rt->dst);
296 }
297
298 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700299}
300
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000301static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700302{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800303 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700304}
305
306#endif
307
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200308static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700309{
310 int res;
311 struct rt6_info *rt;
312
Changli Gaod8d1f302010-06-10 23:31:35 -0700313 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700314 res = rt6_dump_route(rt, w->args);
315 if (res < 0) {
316 /* Frame is full, suspend walking */
317 w->leaf = rt;
318 return 1;
319 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700320 }
321 w->leaf = NULL;
322 return 0;
323}
324
325static void fib6_dump_end(struct netlink_callback *cb)
326{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100327 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200328 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700329
330 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800331 if (cb->args[4]) {
332 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100333 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800334 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700335 cb->args[2] = 0;
336 kfree(w);
337 }
Wang Yufen437de072014-03-28 12:07:04 +0800338 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700339 cb->args[1] = 3;
340}
341
342static int fib6_dump_done(struct netlink_callback *cb)
343{
344 fib6_dump_end(cb);
345 return cb->done ? cb->done(cb) : 0;
346}
347
348static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
349 struct netlink_callback *cb)
350{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100351 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200352 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700353 int res;
354
355 w = (void *)cb->args[2];
356 w->root = &table->tb6_root;
357
358 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000359 w->count = 0;
360 w->skip = 0;
361
Patrick McHardy1b43af52006-08-10 23:11:17 -0700362 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100363 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700364 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000365 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700366 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000367 cb->args[5] = w->root->fn_sernum;
368 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700369 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000370 if (cb->args[5] != w->root->fn_sernum) {
371 /* Begin at the root if the tree changed */
372 cb->args[5] = w->root->fn_sernum;
373 w->state = FWS_INIT;
374 w->node = w->root;
375 w->skip = w->count;
376 } else
377 w->skip = 0;
378
Patrick McHardy1b43af52006-08-10 23:11:17 -0700379 read_lock_bh(&table->tb6_lock);
380 res = fib6_walk_continue(w);
381 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800382 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100383 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800384 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700385 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700386 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800387
Patrick McHardy1b43af52006-08-10 23:11:17 -0700388 return res;
389}
390
Thomas Grafc127ea22007-03-22 11:58:32 -0700391static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700392{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900393 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700394 unsigned int h, s_h;
395 unsigned int e = 0, s_e;
396 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200397 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700398 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800399 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700400 int res = 0;
401
402 s_h = cb->args[0];
403 s_e = cb->args[1];
404
405 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500406 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700407 /* New dump:
408 *
409 * 1. hook callback destructor.
410 */
411 cb->args[3] = (long)cb->done;
412 cb->done = fib6_dump_done;
413
414 /*
415 * 2. allocate and initialize walker.
416 */
417 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500418 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700419 return -ENOMEM;
420 w->func = fib6_dump_node;
421 cb->args[2] = (long)w;
422 }
423
424 arg.skb = skb;
425 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700426 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700427 w->args = &arg;
428
Eric Dumazete67f88d2011-04-27 22:56:07 +0000429 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700430 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700431 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800432 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800433 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700434 if (e < s_e)
435 goto next;
436 res = fib6_dump_table(tb, skb, cb);
437 if (res != 0)
438 goto out;
439next:
440 e++;
441 }
442 }
443out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000444 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700445 cb->args[1] = e;
446 cb->args[0] = h;
447
448 res = res < 0 ? res : skb->len;
449 if (res <= 0)
450 fib6_dump_end(cb);
451 return res;
452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/*
455 * Routing Table
456 *
457 * return the appropriate node for a routing tree "add" operation
458 * by either creating and inserting or by returning an existing
459 * node.
460 */
461
fan.du9225b232013-07-22 14:21:09 +0800462static struct fib6_node *fib6_add_1(struct fib6_node *root,
463 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000464 int offset, int allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200465 int replace_required, int sernum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct fib6_node *fn, *in, *ln;
468 struct fib6_node *pn = NULL;
469 struct rt6key *key;
470 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900471 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 RT6_TRACE("fib6_add_1\n");
474
475 /* insert node in tree */
476
477 fn = root;
478
479 do {
480 key = (struct rt6key *)((u8 *)fn->leaf + offset);
481
482 /*
483 * Prefix match
484 */
485 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000486 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000487 if (!allow_create) {
488 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000489 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000490 return ERR_PTR(-ENOENT);
491 }
Joe Perchesf3213832012-05-15 14:11:53 +0000492 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000495 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * Exact match ?
499 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (plen == fn->fn_bit) {
502 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500503 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 rt6_release(fn->leaf);
505 fn->leaf = NULL;
506 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return fn;
511 }
512
513 /*
514 * We have more bits to go
515 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* Try to walk down on tree. */
518 fn->fn_sernum = sernum;
519 dir = addr_bit_set(addr, fn->fn_bit);
520 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800521 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 } while (fn);
523
Matti Vaittinen14df0152011-11-16 21:18:02 +0000524 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000525 /* We should not create new node because
526 * NLM_F_REPLACE was specified without NLM_F_CREATE
527 * I assume it is safe to require NLM_F_CREATE when
528 * REPLACE flag is used! Later we may want to remove the
529 * check for replace_required, because according
530 * to netlink specification, NLM_F_CREATE
531 * MUST be specified if new route is created.
532 * That would keep IPv6 consistent with IPv4
533 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000534 if (replace_required) {
Joe Perchesf3213832012-05-15 14:11:53 +0000535 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000536 return ERR_PTR(-ENOENT);
537 }
Joe Perchesf3213832012-05-15 14:11:53 +0000538 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * We walked to the bottom of tree.
542 * Create new leaf node without children.
543 */
544
545 ln = node_alloc();
546
David S. Miller507c9b12011-12-03 17:50:45 -0500547 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000548 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ln->parent = pn;
552 ln->fn_sernum = sernum;
553
554 if (dir)
555 pn->right = ln;
556 else
557 pn->left = ln;
558
559 return ln;
560
561
562insert_above:
563 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900564 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * we have a less significant route.
566 * we've to insert an intermediate node on the list
567 * this new node will point to the one we need to create
568 * and the current
569 */
570
571 pn = fn->parent;
572
573 /* find 1st bit in difference between the 2 addrs.
574
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800575 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 but if it is >= plen, the value is ignored in any case.
577 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900578
fan.du9225b232013-07-22 14:21:09 +0800579 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900581 /*
582 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * / \
584 * (new leaf node)[ln] (old node)[fn]
585 */
586 if (plen > bit) {
587 in = node_alloc();
588 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900589
David S. Miller507c9b12011-12-03 17:50:45 -0500590 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (in)
592 node_free(in);
593 if (ln)
594 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000595 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900598 /*
599 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * RTN_RTINFO will
601 * be off since that an address that chooses one of
602 * the branches would not match less specific routes
603 * in the other branch
604 */
605
606 in->fn_bit = bit;
607
608 in->parent = pn;
609 in->leaf = fn->leaf;
610 atomic_inc(&in->leaf->rt6i_ref);
611
612 in->fn_sernum = sernum;
613
614 /* update parent pointer */
615 if (dir)
616 pn->right = in;
617 else
618 pn->left = in;
619
620 ln->fn_bit = plen;
621
622 ln->parent = in;
623 fn->parent = in;
624
625 ln->fn_sernum = sernum;
626
627 if (addr_bit_set(addr, bit)) {
628 in->right = ln;
629 in->left = fn;
630 } else {
631 in->left = ln;
632 in->right = fn;
633 }
634 } else { /* plen <= bit */
635
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900636 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * (new leaf node)[ln]
638 * / \
639 * (old node)[fn] NULL
640 */
641
642 ln = node_alloc();
643
David S. Miller507c9b12011-12-03 17:50:45 -0500644 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000645 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 ln->fn_bit = plen;
648
649 ln->parent = pn;
650
651 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (dir)
654 pn->right = ln;
655 else
656 pn->left = ln;
657
658 if (addr_bit_set(&key->addr, plen))
659 ln->right = fn;
660 else
661 ln->left = fn;
662
663 fn->parent = ln;
664 }
665 return ln;
666}
667
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200668static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200669{
670 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
671 RTF_GATEWAY;
672}
673
Florian Westphale715b6d2015-01-05 23:57:44 +0100674static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100675{
Florian Westphale715b6d2015-01-05 23:57:44 +0100676 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100677
Florian Westphale715b6d2015-01-05 23:57:44 +0100678 for (i = 0; i < RTAX_MAX; i++) {
679 if (test_bit(i, mxc->mx_valid))
680 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100681 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100682}
683
684static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
685{
686 if (!mxc->mx)
687 return 0;
688
689 if (dst->flags & DST_HOST) {
690 u32 *mp = dst_metrics_write_ptr(dst);
691
692 if (unlikely(!mp))
693 return -ENOMEM;
694
695 fib6_copy_metrics(mp, mxc);
696 } else {
697 dst_init_metrics(dst, mxc->mx, false);
698
699 /* We've stolen mx now. */
700 mxc->mx = NULL;
701 }
702
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100703 return 0;
704}
705
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100706static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
707 struct net *net)
708{
709 if (atomic_read(&rt->rt6i_ref) != 1) {
710 /* This route is used as dummy address holder in some split
711 * nodes. It is not leaked, but it still holds other resources,
712 * which must be released in time. So, scan ascendant nodes
713 * and replace dummy references to this route with references
714 * to still alive ones.
715 */
716 while (fn) {
717 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
718 fn->leaf = fib6_find_prefix(net, fn);
719 atomic_inc(&fn->leaf->rt6i_ref);
720 rt6_release(rt);
721 }
722 fn = fn->parent;
723 }
724 /* No more references are possible at this point. */
725 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
726 }
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * Insert routing information in a node.
731 */
732
733static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100734 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 struct rt6_info *iter = NULL;
737 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200738 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500739 int replace = (info->nlh &&
740 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
741 int add = (!info->nlh ||
742 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000743 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200744 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200745 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100746 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 ins = &fn->leaf;
749
David S. Miller507c9b12011-12-03 17:50:45 -0500750 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
752 * Search for duplicates
753 */
754
755 if (iter->rt6i_metric == rt->rt6i_metric) {
756 /*
757 * Same priority level
758 */
David S. Miller507c9b12011-12-03 17:50:45 -0500759 if (info->nlh &&
760 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000761 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200762
763 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000764 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200765 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
766 found++;
767 break;
768 }
769 if (rt_can_ecmp)
770 fallback_ins = fallback_ins ?: ins;
771 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
David S. Millerd1918542011-12-28 20:19:20 -0500774 if (iter->dst.dev == rt->dst.dev &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 iter->rt6i_idev == rt->rt6i_idev &&
776 ipv6_addr_equal(&iter->rt6i_gateway,
777 &rt->rt6i_gateway)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000778 if (rt->rt6i_nsiblings)
779 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500780 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000782 if (!(rt->rt6i_flags & RTF_EXPIRES))
783 rt6_clean_expires(iter);
784 else
785 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700786 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EEXIST;
788 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000789 /* If we have the same destination and the same metric,
790 * but not the same gateway, then the route we try to
791 * add is sibling to this route, increment our counter
792 * of siblings, and later we will add our route to the
793 * list.
794 * Only static routes (which don't have flag
795 * RTF_EXPIRES) are used for ECMPv6.
796 *
797 * To avoid long list, we only had siblings if the
798 * route have a gateway.
799 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200800 if (rt_can_ecmp &&
801 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000802 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805 if (iter->rt6i_metric > rt->rt6i_metric)
806 break;
807
Michal Kubeček27596472015-05-18 20:54:00 +0200808next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700809 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
Michal Kubeček27596472015-05-18 20:54:00 +0200812 if (fallback_ins && !found) {
813 /* No ECMP-able route found, replace first non-ECMP one */
814 ins = fallback_ins;
815 iter = *ins;
816 found++;
817 }
818
David S. Millerf11e6652007-03-24 20:36:25 -0700819 /* Reset round-robin state, if necessary */
820 if (ins == &fn->leaf)
821 fn->rr_ptr = NULL;
822
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000823 /* Link this route to others same route. */
824 if (rt->rt6i_nsiblings) {
825 unsigned int rt6i_nsiblings;
826 struct rt6_info *sibling, *temp_sibling;
827
828 /* Find the first route that have the same metric */
829 sibling = fn->leaf;
830 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200831 if (sibling->rt6i_metric == rt->rt6i_metric &&
832 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000833 list_add_tail(&rt->rt6i_siblings,
834 &sibling->rt6i_siblings);
835 break;
836 }
837 sibling = sibling->dst.rt6_next;
838 }
839 /* For each sibling in the list, increment the counter of
840 * siblings. BUG() if counters does not match, list of siblings
841 * is broken!
842 */
843 rt6i_nsiblings = 0;
844 list_for_each_entry_safe(sibling, temp_sibling,
845 &rt->rt6i_siblings, rt6i_siblings) {
846 sibling->rt6i_nsiblings++;
847 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
848 rt6i_nsiblings++;
849 }
850 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * insert node
855 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000856 if (!replace) {
857 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000858 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000860add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200861 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100862 err = fib6_commit_metrics(&rt->dst, mxc);
863 if (err)
864 return err;
865
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000866 rt->dst.rt6_next = iter;
867 *ins = rt;
868 rt->rt6i_node = fn;
869 atomic_inc(&rt->rt6i_ref);
Guillaume Nault73483c12016-09-07 17:21:40 +0200870 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000871 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
David S. Miller507c9b12011-12-03 17:50:45 -0500873 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000874 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
875 fn->fn_flags |= RTN_RTINFO;
876 }
877
878 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200879 int nsiblings;
880
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000881 if (!found) {
882 if (add)
883 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000884 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000885 return -ENOENT;
886 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100887
888 err = fib6_commit_metrics(&rt->dst, mxc);
889 if (err)
890 return err;
891
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000892 *ins = rt;
893 rt->rt6i_node = fn;
894 rt->dst.rt6_next = iter->dst.rt6_next;
895 atomic_inc(&rt->rt6i_ref);
Roopa Prabhu37a1d362015-09-13 10:18:33 -0700896 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -0500897 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000898 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
899 fn->fn_flags |= RTN_RTINFO;
900 }
Michal Kubeček27596472015-05-18 20:54:00 +0200901 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100902 fib6_purge_rt(iter, fn, info->nl_net);
903 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200904
905 if (nsiblings) {
906 /* Replacing an ECMP route, remove all siblings */
907 ins = &rt->dst.rt6_next;
908 iter = *ins;
909 while (iter) {
Sabrina Dubroca4a8d3bb2017-03-13 13:28:09 +0100910 if (iter->rt6i_metric > rt->rt6i_metric)
911 break;
Michal Kubeček27596472015-05-18 20:54:00 +0200912 if (rt6_qualify_for_ecmp(iter)) {
913 *ins = iter->dst.rt6_next;
914 fib6_purge_rt(iter, fn, info->nl_net);
915 rt6_release(iter);
916 nsiblings--;
917 } else {
918 ins = &iter->dst.rt6_next;
919 }
920 iter = *ins;
921 }
922 WARN_ON(nsiblings != 0);
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
926 return 0;
927}
928
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200929static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700931 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500932 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700933 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700934 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800937void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700939 if (!timer_pending(&net->ipv6.ip6_fib_timer))
940 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700941 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
945 * Add routing information to the routing tree.
946 * <destination addr>/<source addr>
947 * with source addr info in sub-trees
948 */
949
Florian Westphale715b6d2015-01-05 23:57:44 +0100950int fib6_add(struct fib6_node *root, struct rt6_info *rt,
951 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700953 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000955 int allow_create = 1;
956 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200957 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500958
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700959 if (WARN_ON_ONCE((rt->dst.flags & DST_NOCACHE) &&
960 !atomic_read(&rt->dst.__refcnt)))
961 return -EINVAL;
962
David S. Miller507c9b12011-12-03 17:50:45 -0500963 if (info->nlh) {
964 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000965 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500966 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000967 replace_required = 1;
968 }
969 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000970 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
fan.du9225b232013-07-22 14:21:09 +0800972 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
973 offsetof(struct rt6_info, rt6i_dst), allow_create,
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +0200974 replace_required, sernum);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000975 if (IS_ERR(fn)) {
976 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200977 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700981 pn = fn;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983#ifdef CONFIG_IPV6_SUBTREES
984 if (rt->rt6i_src.plen) {
985 struct fib6_node *sn;
986
David S. Miller507c9b12011-12-03 17:50:45 -0500987 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct fib6_node *sfn;
989
990 /*
991 * Create subtree.
992 *
993 * fn[main tree]
994 * |
995 * sfn[subtree root]
996 * \
997 * sn[new leaf node]
998 */
999
1000 /* Create subtree root node */
1001 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001002 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 goto st_failure;
1004
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001005 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1006 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001008 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /* Now add the first leaf node to new subtree */
1011
1012 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001013 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001014 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001015 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001017 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* If it is failed, discard just allocated
1019 root, and then (in st_failure) stale node
1020 in main tree.
1021 */
1022 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001023 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto st_failure;
1025 }
1026
1027 /* Now link new subtree to main tree */
1028 sfn->parent = fn;
1029 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 } else {
1031 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001032 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001033 offsetof(struct rt6_info, rt6i_src),
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001034 allow_create, replace_required, sernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001036 if (IS_ERR(sn)) {
1037 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041
David S. Miller507c9b12011-12-03 17:50:45 -05001042 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001043 fn->leaf = rt;
1044 atomic_inc(&rt->rt6i_ref);
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 fn = sn;
1047 }
1048#endif
1049
Florian Westphale715b6d2015-01-05 23:57:44 +01001050 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001051 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001052 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001053 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001054 fib6_prune_clones(info->nl_net, pn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001055 rt->dst.flags &= ~DST_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
1058out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001059 if (err) {
1060#ifdef CONFIG_IPV6_SUBTREES
1061 /*
1062 * If fib6_add_1 has cleared the old leaf pointer in the
1063 * super-tree leaf node we have to find a new one for it.
1064 */
David S. Miller3c051232008-04-18 01:46:19 -07001065 if (pn != fn && pn->leaf == rt) {
1066 pn->leaf = NULL;
1067 atomic_dec(&rt->rt6i_ref);
1068 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001069 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001070 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001071#if RT6_DEBUG >= 2
1072 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001073 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001074 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001075 }
1076#endif
1077 atomic_inc(&pn->leaf->rt6i_ref);
1078 }
1079#endif
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001080 if (!(rt->dst.flags & DST_NOCACHE))
1081 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return err;
1084
1085#ifdef CONFIG_IPV6_SUBTREES
1086 /* Subtree creation failed, probably main tree node
1087 is orphan. If it is, shoot it.
1088 */
1089st_failure:
1090 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001091 fib6_repair_tree(info->nl_net, fn);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001092 if (!(rt->dst.flags & DST_NOCACHE))
1093 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return err;
1095#endif
1096}
1097
1098/*
1099 * Routing tree lookup
1100 *
1101 */
1102
1103struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001104 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001105 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106};
1107
Wang Yufen437de072014-03-28 12:07:04 +08001108static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1109 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -08001112 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001114 if (unlikely(args->offset == 0))
1115 return NULL;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /*
1118 * Descend on a tree
1119 */
1120
1121 fn = root;
1122
1123 for (;;) {
1124 struct fib6_node *next;
1125
1126 dir = addr_bit_set(args->addr, fn->fn_bit);
1127
1128 next = dir ? fn->right : fn->left;
1129
1130 if (next) {
1131 fn = next;
1132 continue;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 }
1136
David S. Miller507c9b12011-12-03 17:50:45 -05001137 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001138 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct rt6key *key;
1140
1141 key = (struct rt6key *) ((u8 *) fn->leaf +
1142 args->offset);
1143
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001144 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1145#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001146 if (fn->subtree) {
1147 struct fib6_node *sfn;
1148 sfn = fib6_lookup_1(fn->subtree,
1149 args + 1);
1150 if (!sfn)
1151 goto backtrack;
1152 fn = sfn;
1153 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001154#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001155 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001156 return fn;
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001159#ifdef CONFIG_IPV6_SUBTREES
1160backtrack:
1161#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001162 if (fn->fn_flags & RTN_ROOT)
1163 break;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 fn = fn->parent;
1166 }
1167
1168 return NULL;
1169}
1170
Wang Yufen437de072014-03-28 12:07:04 +08001171struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1172 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001175 struct lookup_args args[] = {
1176 {
1177 .offset = offsetof(struct rt6_info, rt6i_dst),
1178 .addr = daddr,
1179 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001181 {
1182 .offset = offsetof(struct rt6_info, rt6i_src),
1183 .addr = saddr,
1184 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001186 {
1187 .offset = 0, /* sentinel */
1188 }
1189 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001191 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001192 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 fn = root;
1194
1195 return fn;
1196}
1197
1198/*
1199 * Get node with specified destination prefix (and source prefix,
1200 * if subtrees are used)
1201 */
1202
1203
Wang Yufen437de072014-03-28 12:07:04 +08001204static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1205 const struct in6_addr *addr,
1206 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct fib6_node *fn;
1209
1210 for (fn = root; fn ; ) {
1211 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1212
1213 /*
1214 * Prefix match
1215 */
1216 if (plen < fn->fn_bit ||
1217 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1218 return NULL;
1219
1220 if (plen == fn->fn_bit)
1221 return fn;
1222
1223 /*
1224 * We have more bits to go
1225 */
1226 if (addr_bit_set(addr, fn->fn_bit))
1227 fn = fn->right;
1228 else
1229 fn = fn->left;
1230 }
1231 return NULL;
1232}
1233
Wang Yufen437de072014-03-28 12:07:04 +08001234struct fib6_node *fib6_locate(struct fib6_node *root,
1235 const struct in6_addr *daddr, int dst_len,
1236 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct fib6_node *fn;
1239
1240 fn = fib6_locate_1(root, daddr, dst_len,
1241 offsetof(struct rt6_info, rt6i_dst));
1242
1243#ifdef CONFIG_IPV6_SUBTREES
1244 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001245 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001246 if (fn && fn->subtree)
1247 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 offsetof(struct rt6_info, rt6i_src));
1249 }
1250#endif
1251
David S. Miller507c9b12011-12-03 17:50:45 -05001252 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return fn;
1254
1255 return NULL;
1256}
1257
1258
1259/*
1260 * Deletion
1261 *
1262 */
1263
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001264static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
David S. Miller507c9b12011-12-03 17:50:45 -05001266 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001267 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
David S. Miller507c9b12011-12-03 17:50:45 -05001269 while (fn) {
1270 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001272 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return fn->right->leaf;
1274
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001275 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 return NULL;
1278}
1279
1280/*
1281 * Called to trim the tree of intermediate nodes when possible. "fn"
1282 * is the node we want to try and remove.
1283 */
1284
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001285static struct fib6_node *fib6_repair_tree(struct net *net,
1286 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 int children;
1289 int nstate;
1290 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001291 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int iter = 0;
1293
1294 for (;;) {
1295 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1296 iter++;
1297
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001298 WARN_ON(fn->fn_flags & RTN_RTINFO);
1299 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001300 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 children = 0;
1303 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001304 if (fn->right)
1305 child = fn->right, children |= 1;
1306 if (fn->left)
1307 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001309 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310#ifdef CONFIG_IPV6_SUBTREES
1311 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001312 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#endif
1314 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001315 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001317 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001318 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001319 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321#endif
1322 atomic_inc(&fn->leaf->rt6i_ref);
1323 return fn->parent;
1324 }
1325
1326 pn = fn->parent;
1327#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001328 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001329 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001330 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 nstate = FWS_L;
1332 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001333 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001335 if (pn->right == fn)
1336 pn->right = child;
1337 else if (pn->left == fn)
1338 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001340 else
1341 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342#endif
1343 if (child)
1344 child->parent = pn;
1345 nstate = FWS_R;
1346#ifdef CONFIG_IPV6_SUBTREES
1347 }
1348#endif
1349
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001350 read_lock(&net->ipv6.fib6_walker_lock);
1351 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001352 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (w->root == fn) {
1354 w->root = w->node = NULL;
1355 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1356 } else if (w->node == fn) {
1357 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1358 w->node = pn;
1359 w->state = nstate;
1360 }
1361 } else {
1362 if (w->root == fn) {
1363 w->root = child;
1364 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1365 }
1366 if (w->node == fn) {
1367 w->node = child;
1368 if (children&2) {
1369 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001370 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } else {
1372 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001373 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 }
1376 }
1377 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001378 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001381 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return pn;
1383
1384 rt6_release(pn->leaf);
1385 pn->leaf = NULL;
1386 fn = pn;
1387 }
1388}
1389
1390static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001391 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001393 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001395 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 RT6_TRACE("fib6_del_route\n");
1398
1399 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001400 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001402 net->ipv6.rt6_stats->fib_rt_entries--;
1403 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
David S. Millerf11e6652007-03-24 20:36:25 -07001405 /* Reset round-robin state, if necessary */
1406 if (fn->rr_ptr == rt)
1407 fn->rr_ptr = NULL;
1408
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001409 /* Remove this entry from other siblings */
1410 if (rt->rt6i_nsiblings) {
1411 struct rt6_info *sibling, *next_sibling;
1412
1413 list_for_each_entry_safe(sibling, next_sibling,
1414 &rt->rt6i_siblings, rt6i_siblings)
1415 sibling->rt6i_nsiblings--;
1416 rt->rt6i_nsiblings = 0;
1417 list_del_init(&rt->rt6i_siblings);
1418 }
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001421 read_lock(&net->ipv6.fib6_walker_lock);
1422 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (w->state == FWS_C && w->leaf == rt) {
1424 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001425 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001426 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 w->state = FWS_U;
1428 }
1429 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001430 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Changli Gaod8d1f302010-06-10 23:31:35 -07001432 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001435 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001437 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001438 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001441 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Roopa Prabhu37a1d362015-09-13 10:18:33 -07001443 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 rt6_release(rt);
1445}
1446
Thomas Graf86872cb2006-08-22 00:01:08 -07001447int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001449 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct fib6_node *fn = rt->rt6i_node;
1451 struct rt6_info **rtp;
1452
1453#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001454 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001455 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -ENOENT;
1457 }
1458#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001459 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -ENOENT;
1461
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001462 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
David S. Miller507c9b12011-12-03 17:50:45 -05001464 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001465 struct fib6_node *pn = fn;
1466#ifdef CONFIG_IPV6_SUBTREES
1467 /* clones of this route might be in another subtree */
1468 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001469 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001470 pn = pn->parent;
1471 pn = pn->parent;
1472 }
1473#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001474 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /*
1478 * Walk the leaf entries looking for ourself
1479 */
1480
Changli Gaod8d1f302010-06-10 23:31:35 -07001481 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001483 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return 0;
1485 }
1486 }
1487 return -ENOENT;
1488}
1489
1490/*
1491 * Tree traversal function.
1492 *
1493 * Certainly, it is not interrupt safe.
1494 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1495 * It means, that we can modify tree during walking
1496 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001497 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * It guarantees that every node will be traversed,
1500 * and that it will be traversed only once.
1501 *
1502 * Callback function w->func may return:
1503 * 0 -> continue walking.
1504 * positive value -> walking is suspended (used by tree dumps,
1505 * and probably by gc, if it will be split to several slices)
1506 * negative value -> terminate walking.
1507 *
1508 * The function itself returns:
1509 * 0 -> walk is complete.
1510 * >0 -> walk is incomplete (i.e. suspended)
1511 * <0 -> walk is terminated by an error.
1512 */
1513
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001514static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct fib6_node *fn, *pn;
1517
1518 for (;;) {
1519 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001520 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 0;
1522
1523 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001524 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 w->state = FWS_C;
1526 w->leaf = fn->leaf;
1527 }
1528 switch (w->state) {
1529#ifdef CONFIG_IPV6_SUBTREES
1530 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001531 if (FIB6_SUBTREE(fn)) {
1532 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 continue;
1534 }
1535 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 case FWS_L:
1538 if (fn->left) {
1539 w->node = fn->left;
1540 w->state = FWS_INIT;
1541 continue;
1542 }
1543 w->state = FWS_R;
1544 case FWS_R:
1545 if (fn->right) {
1546 w->node = fn->right;
1547 w->state = FWS_INIT;
1548 continue;
1549 }
1550 w->state = FWS_C;
1551 w->leaf = fn->leaf;
1552 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001553 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001554 int err;
1555
Eric Dumazetfa809e22012-06-25 15:37:19 -07001556 if (w->skip) {
1557 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001558 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001559 }
1560
1561 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (err)
1563 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001564
1565 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 continue;
1567 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001568skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 w->state = FWS_U;
1570 case FWS_U:
1571 if (fn == w->root)
1572 return 0;
1573 pn = fn->parent;
1574 w->node = pn;
1575#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001576 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001577 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 w->state = FWS_L;
1579 continue;
1580 }
1581#endif
1582 if (pn->left == fn) {
1583 w->state = FWS_R;
1584 continue;
1585 }
1586 if (pn->right == fn) {
1587 w->state = FWS_C;
1588 w->leaf = w->node->leaf;
1589 continue;
1590 }
1591#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001592 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593#endif
1594 }
1595 }
1596}
1597
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001598static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 int res;
1601
1602 w->state = FWS_INIT;
1603 w->node = w->root;
1604
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001605 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 res = fib6_walk_continue(w);
1607 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001608 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return res;
1610}
1611
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001612static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 int res;
1615 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001616 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001617 struct nl_info info = {
1618 .nl_net = c->net,
1619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001621 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1622 w->node->fn_sernum != c->sernum)
1623 w->node->fn_sernum = c->sernum;
1624
1625 if (!c->func) {
1626 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1627 w->leaf = NULL;
1628 return 0;
1629 }
1630
Changli Gaod8d1f302010-06-10 23:31:35 -07001631 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 res = c->func(rt, c->arg);
1633 if (res < 0) {
1634 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001635 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (res) {
1637#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001638 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1639 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640#endif
1641 continue;
1642 }
1643 return 0;
1644 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001645 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647 w->leaf = rt;
1648 return 0;
1649}
1650
1651/*
1652 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001653 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 * func is called on each route.
1655 * It may return -1 -> delete this route.
1656 * 0 -> continue walking
1657 *
1658 * prune==1 -> only immediate children of node (certainly,
1659 * ignoring pure split nodes) will be scanned.
1660 */
1661
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001662static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001663 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001664 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001666 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 c.w.root = root;
1669 c.w.func = fib6_clean_node;
1670 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001671 c.w.count = 0;
1672 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001674 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001676 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001678 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001681static void __fib6_clean_all(struct net *net,
1682 int (*func)(struct rt6_info *, void *),
1683 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001684{
Thomas Grafc71099a2006-08-04 23:20:06 -07001685 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001686 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001687 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001688
Patrick McHardy1b43af52006-08-10 23:11:17 -07001689 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001690 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001691 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001692 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001693 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001694 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001695 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001696 write_unlock_bh(&table->tb6_lock);
1697 }
1698 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001699 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001700}
1701
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001702void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1703 void *arg)
1704{
1705 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1709{
1710 if (rt->rt6i_flags & RTF_CACHE) {
1711 RT6_TRACE("pruning clone %p\n", rt);
1712 return -1;
1713 }
1714
1715 return 0;
1716}
1717
Duan Jiong163cd4e2014-05-09 13:31:43 +08001718static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001720 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1721 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001722}
1723
1724static void fib6_flush_trees(struct net *net)
1725{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001726 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001727
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001728 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/*
1732 * Garbage collection
1733 */
1734
Michal Kubeček3570df92016-03-08 14:44:25 +01001735struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 int timeout;
1738 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001739};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741static int fib6_age(struct rt6_info *rt, void *arg)
1742{
Michal Kubeček3570df92016-03-08 14:44:25 +01001743 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 unsigned long now = jiffies;
1745
1746 /*
1747 * check addrconf expiration here.
1748 * Routes are expired even if they are in use.
1749 *
1750 * Also age clones. Note, that clones are aged out
1751 * only if they are not in use now.
1752 */
1753
David S. Millerd1918542011-12-28 20:19:20 -05001754 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1755 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return -1;
1758 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001759 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001761 if (atomic_read(&rt->dst.__refcnt) == 0 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001762 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 RT6_TRACE("aging clone %p\n", rt);
1764 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001765 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1766 struct neighbour *neigh;
1767 __u8 neigh_flags = 0;
1768
1769 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1770 if (neigh) {
1771 neigh_flags = neigh->flags;
1772 neigh_release(neigh);
1773 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001774 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001775 RT6_TRACE("purging route %p via non-router but gateway\n",
1776 rt);
1777 return -1;
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001780 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782
1783 return 0;
1784}
1785
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001786void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Michal Kubeček3570df92016-03-08 14:44:25 +01001788 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001789 unsigned long now;
1790
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001791 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001792 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1793 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001794 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1795 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001797 gc_args.timeout = expires ? (int)expires :
1798 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001800 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001801
Michal Kubeček3570df92016-03-08 14:44:25 +01001802 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001803 now = jiffies;
1804 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001807 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001808 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001809 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001810 else
1811 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001812 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001815static void fib6_gc_timer_cb(unsigned long arg)
1816{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001817 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001818}
1819
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001820static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001821{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001822 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1823
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001824 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001825 rwlock_init(&net->ipv6.fib6_walker_lock);
1826 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001827 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001828
Benjamin Theryc5728722008-03-03 23:34:17 -08001829 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1830 if (!net->ipv6.rt6_stats)
1831 goto out_timer;
1832
Eric Dumazet10da66f2010-10-13 08:22:03 +00001833 /* Avoid false sharing : Use at least a full cache line */
1834 size = max_t(size_t, size, L1_CACHE_BYTES);
1835
1836 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001837 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001838 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001839
1840 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1841 GFP_KERNEL);
1842 if (!net->ipv6.fib6_main_tbl)
1843 goto out_fib_table_hash;
1844
1845 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001846 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001847 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1848 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001849 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001850
1851#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1852 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1853 GFP_KERNEL);
1854 if (!net->ipv6.fib6_local_tbl)
1855 goto out_fib6_main_tbl;
1856 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001857 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001858 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1859 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001860 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001861#endif
1862 fib6_tables_init(net);
1863
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001864 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001865
1866#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1867out_fib6_main_tbl:
1868 kfree(net->ipv6.fib6_main_tbl);
1869#endif
1870out_fib_table_hash:
1871 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001872out_rt6_stats:
1873 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001874out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001875 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001876}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001877
1878static void fib6_net_exit(struct net *net)
1879{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001880 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001881 del_timer_sync(&net->ipv6.ip6_fib_timer);
1882
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001883#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001884 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001885 kfree(net->ipv6.fib6_local_tbl);
1886#endif
David S. Miller8e773272012-06-11 00:01:52 -07001887 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001888 kfree(net->ipv6.fib6_main_tbl);
1889 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001890 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001891}
1892
1893static struct pernet_operations fib6_net_ops = {
1894 .init = fib6_net_init,
1895 .exit = fib6_net_exit,
1896};
1897
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001898int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001900 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1903 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001904 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001905 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001906 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001907 goto out;
1908
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001909 ret = register_pernet_subsys(&fib6_net_ops);
1910 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001911 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001912
1913 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1914 NULL);
1915 if (ret)
1916 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001917
1918 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001919out:
1920 return ret;
1921
David S. Millere8803b62012-06-16 01:12:19 -07001922out_unregister_subsys:
1923 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001924out_kmem_cache_create:
1925 kmem_cache_destroy(fib6_node_kmem);
1926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929void fib6_gc_cleanup(void)
1930{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001931 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 kmem_cache_destroy(fib6_node_kmem);
1933}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001934
1935#ifdef CONFIG_PROC_FS
1936
1937struct ipv6_route_iter {
1938 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001939 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001940 loff_t skip;
1941 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001942 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001943};
1944
1945static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1946{
1947 struct rt6_info *rt = v;
1948 struct ipv6_route_iter *iter = seq->private;
1949
1950 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1951
1952#ifdef CONFIG_IPV6_SUBTREES
1953 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1954#else
1955 seq_puts(seq, "00000000000000000000000000000000 00 ");
1956#endif
1957 if (rt->rt6i_flags & RTF_GATEWAY)
1958 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1959 else
1960 seq_puts(seq, "00000000000000000000000000000000");
1961
1962 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1963 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1964 rt->dst.__use, rt->rt6i_flags,
1965 rt->dst.dev ? rt->dst.dev->name : "");
1966 iter->w.leaf = NULL;
1967 return 0;
1968}
1969
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001970static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001971{
1972 struct ipv6_route_iter *iter = w->args;
1973
1974 if (!iter->skip)
1975 return 1;
1976
1977 do {
1978 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1979 iter->skip--;
1980 if (!iter->skip && iter->w.leaf)
1981 return 1;
1982 } while (iter->w.leaf);
1983
1984 return 0;
1985}
1986
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001987static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
1988 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001989{
1990 memset(&iter->w, 0, sizeof(iter->w));
1991 iter->w.func = ipv6_route_yield;
1992 iter->w.root = &iter->tbl->tb6_root;
1993 iter->w.state = FWS_INIT;
1994 iter->w.node = iter->w.root;
1995 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02001996 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001997 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001998 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001999}
2000
2001static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2002 struct net *net)
2003{
2004 unsigned int h;
2005 struct hlist_node *node;
2006
2007 if (tbl) {
2008 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2009 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2010 } else {
2011 h = 0;
2012 node = NULL;
2013 }
2014
2015 while (!node && h < FIB6_TABLE_HASHSZ) {
2016 node = rcu_dereference_bh(
2017 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2018 }
2019 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2020}
2021
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002022static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2023{
2024 if (iter->sernum != iter->w.root->fn_sernum) {
2025 iter->sernum = iter->w.root->fn_sernum;
2026 iter->w.state = FWS_INIT;
2027 iter->w.node = iter->w.root;
2028 WARN_ON(iter->w.skip);
2029 iter->w.skip = iter->w.count;
2030 }
2031}
2032
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002033static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2034{
2035 int r;
2036 struct rt6_info *n;
2037 struct net *net = seq_file_net(seq);
2038 struct ipv6_route_iter *iter = seq->private;
2039
2040 if (!v)
2041 goto iter_table;
2042
2043 n = ((struct rt6_info *)v)->dst.rt6_next;
2044 if (n) {
2045 ++*pos;
2046 return n;
2047 }
2048
2049iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002050 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002051 read_lock(&iter->tbl->tb6_lock);
2052 r = fib6_walk_continue(&iter->w);
2053 read_unlock(&iter->tbl->tb6_lock);
2054 if (r > 0) {
2055 if (v)
2056 ++*pos;
2057 return iter->w.leaf;
2058 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002059 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002060 return NULL;
2061 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002062 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002063
2064 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2065 if (!iter->tbl)
2066 return NULL;
2067
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002068 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002069 goto iter_table;
2070}
2071
2072static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2073 __acquires(RCU_BH)
2074{
2075 struct net *net = seq_file_net(seq);
2076 struct ipv6_route_iter *iter = seq->private;
2077
2078 rcu_read_lock_bh();
2079 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2080 iter->skip = *pos;
2081
2082 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002083 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002084 return ipv6_route_seq_next(seq, NULL, pos);
2085 } else {
2086 return NULL;
2087 }
2088}
2089
2090static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2091{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002092 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002093 return w->node && !(w->state == FWS_U && w->node == w->root);
2094}
2095
2096static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2097 __releases(RCU_BH)
2098{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002099 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002100 struct ipv6_route_iter *iter = seq->private;
2101
2102 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002103 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002104
2105 rcu_read_unlock_bh();
2106}
2107
2108static const struct seq_operations ipv6_route_seq_ops = {
2109 .start = ipv6_route_seq_start,
2110 .next = ipv6_route_seq_next,
2111 .stop = ipv6_route_seq_stop,
2112 .show = ipv6_route_seq_show
2113};
2114
2115int ipv6_route_open(struct inode *inode, struct file *file)
2116{
2117 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2118 sizeof(struct ipv6_route_iter));
2119}
2120
2121#endif /* CONFIG_PROC_FS */