blob: e6f878067c68348b396cf3c2dead5aee10d7862e [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 Laud52d3992015-05-22 20:56:06 -0700156static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
157{
158 int cpu;
159
160 if (!non_pcpu_rt->rt6i_pcpu)
161 return;
162
163 for_each_possible_cpu(cpu) {
164 struct rt6_info **ppcpu_rt;
165 struct rt6_info *pcpu_rt;
166
167 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
168 pcpu_rt = *ppcpu_rt;
169 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700170 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700171 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700172 *ppcpu_rt = NULL;
173 }
174 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700175
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700176 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700177 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700178}
179
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200180static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700182 if (atomic_dec_and_test(&rt->rt6i_ref)) {
183 rt6_free_pcpu(rt);
Wei Wang95145282017-06-17 10:42:34 -0700184 dst_dev_put(&rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700185 dst_release(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800189static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700190{
191 unsigned int h;
192
Thomas Graf375216a2006-10-21 20:20:54 -0700193 /*
194 * Initialize table lock at a single place to give lockdep a key,
195 * tables aren't visible prior to being linked to the list.
196 */
197 rwlock_init(&tb->tb6_lock);
198
Neil Hormana33bc5c2009-07-30 18:52:15 -0700199 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700200
201 /*
202 * No protection necessary, this is the only list mutatation
203 * operation, tables never disappear once they exist.
204 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800205 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700206}
207
208#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800209
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800210static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700211{
212 struct fib6_table *table;
213
214 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500215 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700216 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800217 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700218 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700219 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700220 }
221
222 return table;
223}
224
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800225struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700226{
227 struct fib6_table *tb;
228
229 if (id == 0)
230 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800231 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700232 if (tb)
233 return tb;
234
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800235 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500236 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800237 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700238
239 return tb;
240}
David Ahernb3b46632016-05-04 21:46:12 -0700241EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700242
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800243struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700244{
245 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800246 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700247 unsigned int h;
248
249 if (id == 0)
250 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700251 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700252 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800253 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800254 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700255 if (tb->tb6_id == id) {
256 rcu_read_unlock();
257 return tb;
258 }
259 }
260 rcu_read_unlock();
261
262 return NULL;
263}
David Ahernc4850682015-10-12 11:47:08 -0700264EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700265
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000266static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700267{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800268 fib6_link_table(net, net->ipv6.fib6_main_tbl);
269 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700270}
Thomas Grafc71099a2006-08-04 23:20:06 -0700271#else
272
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800273struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700274{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800275 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700276}
277
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800278struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700279{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800280 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700281}
282
David S. Miller4c9483b2011-03-12 16:22:43 -0500283struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800284 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700285{
lucienab997ad2015-10-23 15:36:53 +0800286 struct rt6_info *rt;
287
288 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300289 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800290 ip6_rt_put(rt);
291 rt = net->ipv6.ip6_null_entry;
292 dst_hold(&rt->dst);
293 }
294
295 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700296}
297
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000298static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700299{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800300 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700301}
302
303#endif
304
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200305static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700306{
307 int res;
308 struct rt6_info *rt;
309
Changli Gaod8d1f302010-06-10 23:31:35 -0700310 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700311 res = rt6_dump_route(rt, w->args);
312 if (res < 0) {
313 /* Frame is full, suspend walking */
314 w->leaf = rt;
315 return 1;
316 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800317
318 /* Multipath routes are dumped in one route with the
319 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
320 * last sibling of this route (no need to dump the
321 * sibling routes again)
322 */
323 if (rt->rt6i_nsiblings)
324 rt = list_last_entry(&rt->rt6i_siblings,
325 struct rt6_info,
326 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700327 }
328 w->leaf = NULL;
329 return 0;
330}
331
332static void fib6_dump_end(struct netlink_callback *cb)
333{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100334 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200335 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700336
337 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800338 if (cb->args[4]) {
339 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100340 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800341 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700342 cb->args[2] = 0;
343 kfree(w);
344 }
Wang Yufen437de072014-03-28 12:07:04 +0800345 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700346 cb->args[1] = 3;
347}
348
349static int fib6_dump_done(struct netlink_callback *cb)
350{
351 fib6_dump_end(cb);
352 return cb->done ? cb->done(cb) : 0;
353}
354
355static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
356 struct netlink_callback *cb)
357{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100358 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200359 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700360 int res;
361
362 w = (void *)cb->args[2];
363 w->root = &table->tb6_root;
364
365 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000366 w->count = 0;
367 w->skip = 0;
368
Patrick McHardy1b43af52006-08-10 23:11:17 -0700369 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100370 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700371 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000372 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700373 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000374 cb->args[5] = w->root->fn_sernum;
375 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700376 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000377 if (cb->args[5] != w->root->fn_sernum) {
378 /* Begin at the root if the tree changed */
379 cb->args[5] = w->root->fn_sernum;
380 w->state = FWS_INIT;
381 w->node = w->root;
382 w->skip = w->count;
383 } else
384 w->skip = 0;
385
Patrick McHardy1b43af52006-08-10 23:11:17 -0700386 read_lock_bh(&table->tb6_lock);
387 res = fib6_walk_continue(w);
388 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800389 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100390 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800391 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700392 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700393 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800394
Patrick McHardy1b43af52006-08-10 23:11:17 -0700395 return res;
396}
397
Thomas Grafc127ea22007-03-22 11:58:32 -0700398static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700399{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900400 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700401 unsigned int h, s_h;
402 unsigned int e = 0, s_e;
403 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200404 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700405 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800406 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700407 int res = 0;
408
409 s_h = cb->args[0];
410 s_e = cb->args[1];
411
412 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500413 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700414 /* New dump:
415 *
416 * 1. hook callback destructor.
417 */
418 cb->args[3] = (long)cb->done;
419 cb->done = fib6_dump_done;
420
421 /*
422 * 2. allocate and initialize walker.
423 */
424 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500425 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700426 return -ENOMEM;
427 w->func = fib6_dump_node;
428 cb->args[2] = (long)w;
429 }
430
431 arg.skb = skb;
432 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700433 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700434 w->args = &arg;
435
Eric Dumazete67f88d2011-04-27 22:56:07 +0000436 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700437 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700438 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800439 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800440 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700441 if (e < s_e)
442 goto next;
443 res = fib6_dump_table(tb, skb, cb);
444 if (res != 0)
445 goto out;
446next:
447 e++;
448 }
449 }
450out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000451 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700452 cb->args[1] = e;
453 cb->args[0] = h;
454
455 res = res < 0 ? res : skb->len;
456 if (res <= 0)
457 fib6_dump_end(cb);
458 return res;
459}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461/*
462 * Routing Table
463 *
464 * return the appropriate node for a routing tree "add" operation
465 * by either creating and inserting or by returning an existing
466 * node.
467 */
468
fan.du9225b232013-07-22 14:21:09 +0800469static struct fib6_node *fib6_add_1(struct fib6_node *root,
470 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000471 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600472 int replace_required, int sernum,
473 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct fib6_node *fn, *in, *ln;
476 struct fib6_node *pn = NULL;
477 struct rt6key *key;
478 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900479 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 RT6_TRACE("fib6_add_1\n");
482
483 /* insert node in tree */
484
485 fn = root;
486
487 do {
488 key = (struct rt6key *)((u8 *)fn->leaf + offset);
489
490 /*
491 * Prefix match
492 */
493 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000494 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000495 if (!allow_create) {
496 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600497 NL_SET_ERR_MSG(extack,
498 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000499 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000500 return ERR_PTR(-ENOENT);
501 }
Joe Perchesf3213832012-05-15 14:11:53 +0000502 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000505 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * Exact match ?
509 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (plen == fn->fn_bit) {
512 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500513 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rt6_release(fn->leaf);
515 fn->leaf = NULL;
516 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return fn;
521 }
522
523 /*
524 * We have more bits to go
525 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Try to walk down on tree. */
528 fn->fn_sernum = sernum;
529 dir = addr_bit_set(addr, fn->fn_bit);
530 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800531 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } while (fn);
533
Matti Vaittinen14df0152011-11-16 21:18:02 +0000534 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000535 /* We should not create new node because
536 * NLM_F_REPLACE was specified without NLM_F_CREATE
537 * I assume it is safe to require NLM_F_CREATE when
538 * REPLACE flag is used! Later we may want to remove the
539 * check for replace_required, because according
540 * to netlink specification, NLM_F_CREATE
541 * MUST be specified if new route is created.
542 * That would keep IPv6 consistent with IPv4
543 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000544 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600545 NL_SET_ERR_MSG(extack,
546 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000547 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000548 return ERR_PTR(-ENOENT);
549 }
Joe Perchesf3213832012-05-15 14:11:53 +0000550 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
553 * We walked to the bottom of tree.
554 * Create new leaf node without children.
555 */
556
557 ln = node_alloc();
558
David S. Miller507c9b12011-12-03 17:50:45 -0500559 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000560 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ln->parent = pn;
564 ln->fn_sernum = sernum;
565
566 if (dir)
567 pn->right = ln;
568 else
569 pn->left = ln;
570
571 return ln;
572
573
574insert_above:
575 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900576 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * we have a less significant route.
578 * we've to insert an intermediate node on the list
579 * this new node will point to the one we need to create
580 * and the current
581 */
582
583 pn = fn->parent;
584
585 /* find 1st bit in difference between the 2 addrs.
586
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800587 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 but if it is >= plen, the value is ignored in any case.
589 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900590
fan.du9225b232013-07-22 14:21:09 +0800591 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900593 /*
594 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * / \
596 * (new leaf node)[ln] (old node)[fn]
597 */
598 if (plen > bit) {
599 in = node_alloc();
600 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900601
David S. Miller507c9b12011-12-03 17:50:45 -0500602 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (in)
604 node_free(in);
605 if (ln)
606 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000607 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900610 /*
611 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * RTN_RTINFO will
613 * be off since that an address that chooses one of
614 * the branches would not match less specific routes
615 * in the other branch
616 */
617
618 in->fn_bit = bit;
619
620 in->parent = pn;
621 in->leaf = fn->leaf;
622 atomic_inc(&in->leaf->rt6i_ref);
623
624 in->fn_sernum = sernum;
625
626 /* update parent pointer */
627 if (dir)
628 pn->right = in;
629 else
630 pn->left = in;
631
632 ln->fn_bit = plen;
633
634 ln->parent = in;
635 fn->parent = in;
636
637 ln->fn_sernum = sernum;
638
639 if (addr_bit_set(addr, bit)) {
640 in->right = ln;
641 in->left = fn;
642 } else {
643 in->left = ln;
644 in->right = fn;
645 }
646 } else { /* plen <= bit */
647
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900648 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * (new leaf node)[ln]
650 * / \
651 * (old node)[fn] NULL
652 */
653
654 ln = node_alloc();
655
David S. Miller507c9b12011-12-03 17:50:45 -0500656 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000657 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 ln->fn_bit = plen;
660
661 ln->parent = pn;
662
663 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (dir)
666 pn->right = ln;
667 else
668 pn->left = ln;
669
670 if (addr_bit_set(&key->addr, plen))
671 ln->right = fn;
672 else
673 ln->left = fn;
674
675 fn->parent = ln;
676 }
677 return ln;
678}
679
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200680static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200681{
682 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
683 RTF_GATEWAY;
684}
685
Florian Westphale715b6d2015-01-05 23:57:44 +0100686static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100687{
Florian Westphale715b6d2015-01-05 23:57:44 +0100688 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100689
Florian Westphale715b6d2015-01-05 23:57:44 +0100690 for (i = 0; i < RTAX_MAX; i++) {
691 if (test_bit(i, mxc->mx_valid))
692 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100693 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100694}
695
696static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
697{
698 if (!mxc->mx)
699 return 0;
700
701 if (dst->flags & DST_HOST) {
702 u32 *mp = dst_metrics_write_ptr(dst);
703
704 if (unlikely(!mp))
705 return -ENOMEM;
706
707 fib6_copy_metrics(mp, mxc);
708 } else {
709 dst_init_metrics(dst, mxc->mx, false);
710
711 /* We've stolen mx now. */
712 mxc->mx = NULL;
713 }
714
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100715 return 0;
716}
717
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100718static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
719 struct net *net)
720{
721 if (atomic_read(&rt->rt6i_ref) != 1) {
722 /* This route is used as dummy address holder in some split
723 * nodes. It is not leaked, but it still holds other resources,
724 * which must be released in time. So, scan ascendant nodes
725 * and replace dummy references to this route with references
726 * to still alive ones.
727 */
728 while (fn) {
729 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
730 fn->leaf = fib6_find_prefix(net, fn);
731 atomic_inc(&fn->leaf->rt6i_ref);
732 rt6_release(rt);
733 }
734 fn = fn->parent;
735 }
736 /* No more references are possible at this point. */
737 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
738 }
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
742 * Insert routing information in a node.
743 */
744
745static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100746 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct rt6_info *iter = NULL;
749 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200750 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500751 int replace = (info->nlh &&
752 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
753 int add = (!info->nlh ||
754 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000755 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200756 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200757 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100758 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David Ahern1f5e29c2017-01-31 16:51:37 -0800760 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
761 nlflags |= NLM_F_APPEND;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ins = &fn->leaf;
764
David S. Miller507c9b12011-12-03 17:50:45 -0500765 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Search for duplicates
768 */
769
770 if (iter->rt6i_metric == rt->rt6i_metric) {
771 /*
772 * Same priority level
773 */
David S. Miller507c9b12011-12-03 17:50:45 -0500774 if (info->nlh &&
775 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000776 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200777
778 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000779 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200780 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
781 found++;
782 break;
783 }
784 if (rt_can_ecmp)
785 fallback_ins = fallback_ins ?: ins;
786 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David Ahernf06b7542017-07-05 14:41:46 -0600789 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000790 if (rt->rt6i_nsiblings)
791 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500792 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000794 if (!(rt->rt6i_flags & RTF_EXPIRES))
795 rt6_clean_expires(iter);
796 else
797 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700798 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -EEXIST;
800 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000801 /* If we have the same destination and the same metric,
802 * but not the same gateway, then the route we try to
803 * add is sibling to this route, increment our counter
804 * of siblings, and later we will add our route to the
805 * list.
806 * Only static routes (which don't have flag
807 * RTF_EXPIRES) are used for ECMPv6.
808 *
809 * To avoid long list, we only had siblings if the
810 * route have a gateway.
811 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200812 if (rt_can_ecmp &&
813 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000814 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 if (iter->rt6i_metric > rt->rt6i_metric)
818 break;
819
Michal Kubeček27596472015-05-18 20:54:00 +0200820next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700821 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Michal Kubeček27596472015-05-18 20:54:00 +0200824 if (fallback_ins && !found) {
825 /* No ECMP-able route found, replace first non-ECMP one */
826 ins = fallback_ins;
827 iter = *ins;
828 found++;
829 }
830
David S. Millerf11e6652007-03-24 20:36:25 -0700831 /* Reset round-robin state, if necessary */
832 if (ins == &fn->leaf)
833 fn->rr_ptr = NULL;
834
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000835 /* Link this route to others same route. */
836 if (rt->rt6i_nsiblings) {
837 unsigned int rt6i_nsiblings;
838 struct rt6_info *sibling, *temp_sibling;
839
840 /* Find the first route that have the same metric */
841 sibling = fn->leaf;
842 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200843 if (sibling->rt6i_metric == rt->rt6i_metric &&
844 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000845 list_add_tail(&rt->rt6i_siblings,
846 &sibling->rt6i_siblings);
847 break;
848 }
849 sibling = sibling->dst.rt6_next;
850 }
851 /* For each sibling in the list, increment the counter of
852 * siblings. BUG() if counters does not match, list of siblings
853 * is broken!
854 */
855 rt6i_nsiblings = 0;
856 list_for_each_entry_safe(sibling, temp_sibling,
857 &rt->rt6i_siblings, rt6i_siblings) {
858 sibling->rt6i_nsiblings++;
859 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
860 rt6i_nsiblings++;
861 }
862 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
863 }
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /*
866 * insert node
867 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000868 if (!replace) {
869 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000870 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000872add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200873 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100874 err = fib6_commit_metrics(&rt->dst, mxc);
875 if (err)
876 return err;
877
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000878 rt->dst.rt6_next = iter;
879 *ins = rt;
880 rt->rt6i_node = fn;
881 atomic_inc(&rt->rt6i_ref);
David Ahern3b1137f2017-02-02 12:37:10 -0800882 if (!info->skip_notify)
883 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000884 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David S. Miller507c9b12011-12-03 17:50:45 -0500886 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000887 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
888 fn->fn_flags |= RTN_RTINFO;
889 }
890
891 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200892 int nsiblings;
893
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000894 if (!found) {
895 if (add)
896 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000897 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000898 return -ENOENT;
899 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100900
901 err = fib6_commit_metrics(&rt->dst, mxc);
902 if (err)
903 return err;
904
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000905 *ins = rt;
906 rt->rt6i_node = fn;
907 rt->dst.rt6_next = iter->dst.rt6_next;
908 atomic_inc(&rt->rt6i_ref);
David Ahern3b1137f2017-02-02 12:37:10 -0800909 if (!info->skip_notify)
910 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -0500911 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000912 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
913 fn->fn_flags |= RTN_RTINFO;
914 }
Michal Kubeček27596472015-05-18 20:54:00 +0200915 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100916 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -0700917 if (fn->rr_ptr == iter)
918 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100919 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200920
921 if (nsiblings) {
922 /* Replacing an ECMP route, remove all siblings */
923 ins = &rt->dst.rt6_next;
924 iter = *ins;
925 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +0100926 if (iter->rt6i_metric > rt->rt6i_metric)
927 break;
Michal Kubeček27596472015-05-18 20:54:00 +0200928 if (rt6_qualify_for_ecmp(iter)) {
929 *ins = iter->dst.rt6_next;
930 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -0700931 if (fn->rr_ptr == iter)
932 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +0200933 rt6_release(iter);
934 nsiblings--;
935 } else {
936 ins = &iter->dst.rt6_next;
937 }
938 iter = *ins;
939 }
940 WARN_ON(nsiblings != 0);
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
944 return 0;
945}
946
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200947static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700949 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500950 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700951 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700952 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800955void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700957 if (!timer_pending(&net->ipv6.ip6_fib_timer))
958 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700959 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962/*
963 * Add routing information to the routing tree.
964 * <destination addr>/<source addr>
965 * with source addr info in sub-trees
966 */
967
Florian Westphale715b6d2015-01-05 23:57:44 +0100968int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -0600969 struct nl_info *info, struct mx6_config *mxc,
970 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700972 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000974 int allow_create = 1;
975 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200976 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500977
Wei Wanga4c2fd72017-06-17 10:42:42 -0700978 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700979 return -EINVAL;
980
David S. Miller507c9b12011-12-03 17:50:45 -0500981 if (info->nlh) {
982 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000983 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500984 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000985 replace_required = 1;
986 }
987 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +0000988 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
fan.du9225b232013-07-22 14:21:09 +0800990 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
991 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600992 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000993 if (IS_ERR(fn)) {
994 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +0200995 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto out;
Lin Ming188c5172012-09-25 15:17:07 +0000997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700999 pn = fn;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001#ifdef CONFIG_IPV6_SUBTREES
1002 if (rt->rt6i_src.plen) {
1003 struct fib6_node *sn;
1004
David S. Miller507c9b12011-12-03 17:50:45 -05001005 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct fib6_node *sfn;
1007
1008 /*
1009 * Create subtree.
1010 *
1011 * fn[main tree]
1012 * |
1013 * sfn[subtree root]
1014 * \
1015 * sn[new leaf node]
1016 */
1017
1018 /* Create subtree root node */
1019 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001020 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto st_failure;
1022
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001023 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1024 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001026 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /* Now add the first leaf node to new subtree */
1029
1030 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001031 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001032 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001033 allow_create, replace_required, sernum,
1034 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001036 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* If it is failed, discard just allocated
1038 root, and then (in st_failure) stale node
1039 in main tree.
1040 */
1041 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001042 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto st_failure;
1044 }
1045
1046 /* Now link new subtree to main tree */
1047 sfn->parent = fn;
1048 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 } else {
1050 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001051 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001052 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001053 allow_create, replace_required, sernum,
1054 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001056 if (IS_ERR(sn)) {
1057 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061
David S. Miller507c9b12011-12-03 17:50:45 -05001062 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001063 fn->leaf = rt;
1064 atomic_inc(&rt->rt6i_ref);
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 fn = sn;
1067 }
1068#endif
1069
Florian Westphale715b6d2015-01-05 23:57:44 +01001070 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001071 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001072 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001073 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001074 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001078 if (err) {
1079#ifdef CONFIG_IPV6_SUBTREES
1080 /*
1081 * If fib6_add_1 has cleared the old leaf pointer in the
1082 * super-tree leaf node we have to find a new one for it.
1083 */
David S. Miller3c051232008-04-18 01:46:19 -07001084 if (pn != fn && pn->leaf == rt) {
1085 pn->leaf = NULL;
1086 atomic_dec(&rt->rt6i_ref);
1087 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001088 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001089 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001090#if RT6_DEBUG >= 2
1091 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001092 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001093 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001094 }
1095#endif
1096 atomic_inc(&pn->leaf->rt6i_ref);
1097 }
1098#endif
Wei Wang1cfb71e2017-06-17 10:42:33 -07001099 /* Always release dst as dst->__refcnt is guaranteed
1100 * to be taken before entering this function
1101 */
Wei Wang587fea72017-06-17 10:42:36 -07001102 dst_release_immediate(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return err;
1105
1106#ifdef CONFIG_IPV6_SUBTREES
1107 /* Subtree creation failed, probably main tree node
1108 is orphan. If it is, shoot it.
1109 */
1110st_failure:
1111 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001112 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001113 /* Always release dst as dst->__refcnt is guaranteed
1114 * to be taken before entering this function
1115 */
Wei Wang587fea72017-06-17 10:42:36 -07001116 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return err;
1118#endif
1119}
1120
1121/*
1122 * Routing tree lookup
1123 *
1124 */
1125
1126struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001127 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001128 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129};
1130
Wang Yufen437de072014-03-28 12:07:04 +08001131static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1132 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001135 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001137 if (unlikely(args->offset == 0))
1138 return NULL;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /*
1141 * Descend on a tree
1142 */
1143
1144 fn = root;
1145
1146 for (;;) {
1147 struct fib6_node *next;
1148
1149 dir = addr_bit_set(args->addr, fn->fn_bit);
1150
1151 next = dir ? fn->right : fn->left;
1152
1153 if (next) {
1154 fn = next;
1155 continue;
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 }
1159
David S. Miller507c9b12011-12-03 17:50:45 -05001160 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001161 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct rt6key *key;
1163
1164 key = (struct rt6key *) ((u8 *) fn->leaf +
1165 args->offset);
1166
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001167 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1168#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001169 if (fn->subtree) {
1170 struct fib6_node *sfn;
1171 sfn = fib6_lookup_1(fn->subtree,
1172 args + 1);
1173 if (!sfn)
1174 goto backtrack;
1175 fn = sfn;
1176 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001177#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001178 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001179 return fn;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001182#ifdef CONFIG_IPV6_SUBTREES
1183backtrack:
1184#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001185 if (fn->fn_flags & RTN_ROOT)
1186 break;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 fn = fn->parent;
1189 }
1190
1191 return NULL;
1192}
1193
Wang Yufen437de072014-03-28 12:07:04 +08001194struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1195 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001198 struct lookup_args args[] = {
1199 {
1200 .offset = offsetof(struct rt6_info, rt6i_dst),
1201 .addr = daddr,
1202 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001204 {
1205 .offset = offsetof(struct rt6_info, rt6i_src),
1206 .addr = saddr,
1207 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001209 {
1210 .offset = 0, /* sentinel */
1211 }
1212 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001214 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001215 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 fn = root;
1217
1218 return fn;
1219}
1220
1221/*
1222 * Get node with specified destination prefix (and source prefix,
1223 * if subtrees are used)
1224 */
1225
1226
Wang Yufen437de072014-03-28 12:07:04 +08001227static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1228 const struct in6_addr *addr,
1229 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct fib6_node *fn;
1232
1233 for (fn = root; fn ; ) {
1234 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1235
1236 /*
1237 * Prefix match
1238 */
1239 if (plen < fn->fn_bit ||
1240 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1241 return NULL;
1242
1243 if (plen == fn->fn_bit)
1244 return fn;
1245
1246 /*
1247 * We have more bits to go
1248 */
1249 if (addr_bit_set(addr, fn->fn_bit))
1250 fn = fn->right;
1251 else
1252 fn = fn->left;
1253 }
1254 return NULL;
1255}
1256
Wang Yufen437de072014-03-28 12:07:04 +08001257struct fib6_node *fib6_locate(struct fib6_node *root,
1258 const struct in6_addr *daddr, int dst_len,
1259 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct fib6_node *fn;
1262
1263 fn = fib6_locate_1(root, daddr, dst_len,
1264 offsetof(struct rt6_info, rt6i_dst));
1265
1266#ifdef CONFIG_IPV6_SUBTREES
1267 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001268 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001269 if (fn && fn->subtree)
1270 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 offsetof(struct rt6_info, rt6i_src));
1272 }
1273#endif
1274
David S. Miller507c9b12011-12-03 17:50:45 -05001275 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return fn;
1277
1278 return NULL;
1279}
1280
1281
1282/*
1283 * Deletion
1284 *
1285 */
1286
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001287static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
David S. Miller507c9b12011-12-03 17:50:45 -05001289 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001290 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
David S. Miller507c9b12011-12-03 17:50:45 -05001292 while (fn) {
1293 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001295 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return fn->right->leaf;
1297
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001298 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 return NULL;
1301}
1302
1303/*
1304 * Called to trim the tree of intermediate nodes when possible. "fn"
1305 * is the node we want to try and remove.
1306 */
1307
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001308static struct fib6_node *fib6_repair_tree(struct net *net,
1309 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 int children;
1312 int nstate;
1313 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001314 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int iter = 0;
1316
1317 for (;;) {
1318 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1319 iter++;
1320
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001321 WARN_ON(fn->fn_flags & RTN_RTINFO);
1322 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001323 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 children = 0;
1326 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001327 if (fn->right)
1328 child = fn->right, children |= 1;
1329 if (fn->left)
1330 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001332 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333#ifdef CONFIG_IPV6_SUBTREES
1334 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001335 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336#endif
1337 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001338 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001340 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001341 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001342 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344#endif
1345 atomic_inc(&fn->leaf->rt6i_ref);
1346 return fn->parent;
1347 }
1348
1349 pn = fn->parent;
1350#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001351 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001352 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001353 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 nstate = FWS_L;
1355 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001356 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001358 if (pn->right == fn)
1359 pn->right = child;
1360 else if (pn->left == fn)
1361 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001363 else
1364 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365#endif
1366 if (child)
1367 child->parent = pn;
1368 nstate = FWS_R;
1369#ifdef CONFIG_IPV6_SUBTREES
1370 }
1371#endif
1372
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001373 read_lock(&net->ipv6.fib6_walker_lock);
1374 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001375 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (w->root == fn) {
1377 w->root = w->node = NULL;
1378 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1379 } else if (w->node == fn) {
1380 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1381 w->node = pn;
1382 w->state = nstate;
1383 }
1384 } else {
1385 if (w->root == fn) {
1386 w->root = child;
1387 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1388 }
1389 if (w->node == fn) {
1390 w->node = child;
1391 if (children&2) {
1392 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001393 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 } else {
1395 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001396 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 }
1399 }
1400 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001401 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001404 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return pn;
1406
1407 rt6_release(pn->leaf);
1408 pn->leaf = NULL;
1409 fn = pn;
1410 }
1411}
1412
1413static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001414 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001416 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001418 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 RT6_TRACE("fib6_del_route\n");
1421
1422 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001423 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001425 net->ipv6.rt6_stats->fib_rt_entries--;
1426 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
David S. Millerf11e6652007-03-24 20:36:25 -07001428 /* Reset round-robin state, if necessary */
1429 if (fn->rr_ptr == rt)
1430 fn->rr_ptr = NULL;
1431
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001432 /* Remove this entry from other siblings */
1433 if (rt->rt6i_nsiblings) {
1434 struct rt6_info *sibling, *next_sibling;
1435
1436 list_for_each_entry_safe(sibling, next_sibling,
1437 &rt->rt6i_siblings, rt6i_siblings)
1438 sibling->rt6i_nsiblings--;
1439 rt->rt6i_nsiblings = 0;
1440 list_del_init(&rt->rt6i_siblings);
1441 }
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001444 read_lock(&net->ipv6.fib6_walker_lock);
1445 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (w->state == FWS_C && w->leaf == rt) {
1447 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001448 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001449 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 w->state = FWS_U;
1451 }
1452 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001453 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Changli Gaod8d1f302010-06-10 23:31:35 -07001455 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001458 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001460 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001461 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001464 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
David Ahern16a16cd2017-02-02 12:37:11 -08001466 if (!info->skip_notify)
1467 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 rt6_release(rt);
1469}
1470
Thomas Graf86872cb2006-08-22 00:01:08 -07001471int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001473 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct fib6_node *fn = rt->rt6i_node;
1475 struct rt6_info **rtp;
1476
1477#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001478 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001479 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -ENOENT;
1481 }
1482#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001483 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -ENOENT;
1485
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001486 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
David S. Miller507c9b12011-12-03 17:50:45 -05001488 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001489 struct fib6_node *pn = fn;
1490#ifdef CONFIG_IPV6_SUBTREES
1491 /* clones of this route might be in another subtree */
1492 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001493 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001494 pn = pn->parent;
1495 pn = pn->parent;
1496 }
1497#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001498 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /*
1502 * Walk the leaf entries looking for ourself
1503 */
1504
Changli Gaod8d1f302010-06-10 23:31:35 -07001505 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001507 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509 }
1510 }
1511 return -ENOENT;
1512}
1513
1514/*
1515 * Tree traversal function.
1516 *
1517 * Certainly, it is not interrupt safe.
1518 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1519 * It means, that we can modify tree during walking
1520 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001521 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 *
1523 * It guarantees that every node will be traversed,
1524 * and that it will be traversed only once.
1525 *
1526 * Callback function w->func may return:
1527 * 0 -> continue walking.
1528 * positive value -> walking is suspended (used by tree dumps,
1529 * and probably by gc, if it will be split to several slices)
1530 * negative value -> terminate walking.
1531 *
1532 * The function itself returns:
1533 * 0 -> walk is complete.
1534 * >0 -> walk is incomplete (i.e. suspended)
1535 * <0 -> walk is terminated by an error.
1536 */
1537
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001538static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct fib6_node *fn, *pn;
1541
1542 for (;;) {
1543 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001544 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return 0;
1546
1547 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001548 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 w->state = FWS_C;
1550 w->leaf = fn->leaf;
1551 }
1552 switch (w->state) {
1553#ifdef CONFIG_IPV6_SUBTREES
1554 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001555 if (FIB6_SUBTREE(fn)) {
1556 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 continue;
1558 }
1559 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 case FWS_L:
1562 if (fn->left) {
1563 w->node = fn->left;
1564 w->state = FWS_INIT;
1565 continue;
1566 }
1567 w->state = FWS_R;
1568 case FWS_R:
1569 if (fn->right) {
1570 w->node = fn->right;
1571 w->state = FWS_INIT;
1572 continue;
1573 }
1574 w->state = FWS_C;
1575 w->leaf = fn->leaf;
1576 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001577 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001578 int err;
1579
Eric Dumazetfa809e22012-06-25 15:37:19 -07001580 if (w->skip) {
1581 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001582 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001583 }
1584
1585 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (err)
1587 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001588
1589 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 continue;
1591 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001592skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 w->state = FWS_U;
1594 case FWS_U:
1595 if (fn == w->root)
1596 return 0;
1597 pn = fn->parent;
1598 w->node = pn;
1599#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001600 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001601 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 w->state = FWS_L;
1603 continue;
1604 }
1605#endif
1606 if (pn->left == fn) {
1607 w->state = FWS_R;
1608 continue;
1609 }
1610 if (pn->right == fn) {
1611 w->state = FWS_C;
1612 w->leaf = w->node->leaf;
1613 continue;
1614 }
1615#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001616 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617#endif
1618 }
1619 }
1620}
1621
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001622static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 int res;
1625
1626 w->state = FWS_INIT;
1627 w->node = w->root;
1628
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001629 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 res = fib6_walk_continue(w);
1631 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001632 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return res;
1634}
1635
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001636static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 int res;
1639 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001640 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001641 struct nl_info info = {
1642 .nl_net = c->net,
1643 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001645 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1646 w->node->fn_sernum != c->sernum)
1647 w->node->fn_sernum = c->sernum;
1648
1649 if (!c->func) {
1650 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1651 w->leaf = NULL;
1652 return 0;
1653 }
1654
Changli Gaod8d1f302010-06-10 23:31:35 -07001655 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 res = c->func(rt, c->arg);
1657 if (res < 0) {
1658 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001659 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (res) {
1661#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001662 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1663 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664#endif
1665 continue;
1666 }
1667 return 0;
1668 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001669 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 w->leaf = rt;
1672 return 0;
1673}
1674
1675/*
1676 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001677 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * func is called on each route.
1679 * It may return -1 -> delete this route.
1680 * 0 -> continue walking
1681 *
1682 * prune==1 -> only immediate children of node (certainly,
1683 * ignoring pure split nodes) will be scanned.
1684 */
1685
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001686static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001687 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001688 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001690 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 c.w.root = root;
1693 c.w.func = fib6_clean_node;
1694 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001695 c.w.count = 0;
1696 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001698 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001700 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001702 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001705static void __fib6_clean_all(struct net *net,
1706 int (*func)(struct rt6_info *, void *),
1707 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001708{
Thomas Grafc71099a2006-08-04 23:20:06 -07001709 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001710 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001711 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001712
Patrick McHardy1b43af52006-08-10 23:11:17 -07001713 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001714 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001715 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001716 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001717 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001718 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001719 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001720 write_unlock_bh(&table->tb6_lock);
1721 }
1722 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001723 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001724}
1725
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001726void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1727 void *arg)
1728{
1729 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1733{
1734 if (rt->rt6i_flags & RTF_CACHE) {
1735 RT6_TRACE("pruning clone %p\n", rt);
1736 return -1;
1737 }
1738
1739 return 0;
1740}
1741
Duan Jiong163cd4e2014-05-09 13:31:43 +08001742static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001744 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1745 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001746}
1747
1748static void fib6_flush_trees(struct net *net)
1749{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001750 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001751
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001752 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*
1756 * Garbage collection
1757 */
1758
Michal Kubeček3570df92016-03-08 14:44:25 +01001759struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 int timeout;
1762 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001763};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765static int fib6_age(struct rt6_info *rt, void *arg)
1766{
Michal Kubeček3570df92016-03-08 14:44:25 +01001767 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 unsigned long now = jiffies;
1769
1770 /*
1771 * check addrconf expiration here.
1772 * Routes are expired even if they are in use.
1773 *
1774 * Also age clones. Note, that clones are aged out
1775 * only if they are not in use now.
1776 */
1777
David S. Millerd1918542011-12-28 20:19:20 -05001778 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1779 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return -1;
1782 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001783 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 } else if (rt->rt6i_flags & RTF_CACHE) {
Wei Wang1cfb71e2017-06-17 10:42:33 -07001785 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001786 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 RT6_TRACE("aging clone %p\n", rt);
1788 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001789 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1790 struct neighbour *neigh;
1791 __u8 neigh_flags = 0;
1792
1793 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1794 if (neigh) {
1795 neigh_flags = neigh->flags;
1796 neigh_release(neigh);
1797 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001798 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001799 RT6_TRACE("purging route %p via non-router but gateway\n",
1800 rt);
1801 return -1;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001804 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
1807 return 0;
1808}
1809
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001810void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Michal Kubeček3570df92016-03-08 14:44:25 +01001812 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001813 unsigned long now;
1814
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001815 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001816 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1817 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001818 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001821 gc_args.timeout = expires ? (int)expires :
1822 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001823 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001824
Michal Kubeček3570df92016-03-08 14:44:25 +01001825 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001826 now = jiffies;
1827 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001830 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001831 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001832 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001833 else
1834 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001835 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001838static void fib6_gc_timer_cb(unsigned long arg)
1839{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001840 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001841}
1842
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001843static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001844{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001845 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1846
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001847 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001848 rwlock_init(&net->ipv6.fib6_walker_lock);
1849 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001850 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001851
Benjamin Theryc5728722008-03-03 23:34:17 -08001852 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1853 if (!net->ipv6.rt6_stats)
1854 goto out_timer;
1855
Eric Dumazet10da66f2010-10-13 08:22:03 +00001856 /* Avoid false sharing : Use at least a full cache line */
1857 size = max_t(size_t, size, L1_CACHE_BYTES);
1858
1859 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001860 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001861 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001862
1863 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1864 GFP_KERNEL);
1865 if (!net->ipv6.fib6_main_tbl)
1866 goto out_fib_table_hash;
1867
1868 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001869 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001870 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1871 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001872 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001873
1874#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1875 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1876 GFP_KERNEL);
1877 if (!net->ipv6.fib6_local_tbl)
1878 goto out_fib6_main_tbl;
1879 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001880 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001881 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1882 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001883 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001884#endif
1885 fib6_tables_init(net);
1886
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001887 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001888
1889#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1890out_fib6_main_tbl:
1891 kfree(net->ipv6.fib6_main_tbl);
1892#endif
1893out_fib_table_hash:
1894 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001895out_rt6_stats:
1896 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001897out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001898 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001899}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001900
1901static void fib6_net_exit(struct net *net)
1902{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001903 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001904 del_timer_sync(&net->ipv6.ip6_fib_timer);
1905
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001906#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001907 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001908 kfree(net->ipv6.fib6_local_tbl);
1909#endif
David S. Miller8e773272012-06-11 00:01:52 -07001910 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001911 kfree(net->ipv6.fib6_main_tbl);
1912 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001913 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001914}
1915
1916static struct pernet_operations fib6_net_ops = {
1917 .init = fib6_net_init,
1918 .exit = fib6_net_exit,
1919};
1920
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001921int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001923 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1926 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001927 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001928 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001929 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001930 goto out;
1931
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001932 ret = register_pernet_subsys(&fib6_net_ops);
1933 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001934 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001935
1936 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1937 NULL);
1938 if (ret)
1939 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001940
1941 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001942out:
1943 return ret;
1944
David S. Millere8803b62012-06-16 01:12:19 -07001945out_unregister_subsys:
1946 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001947out_kmem_cache_create:
1948 kmem_cache_destroy(fib6_node_kmem);
1949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952void fib6_gc_cleanup(void)
1953{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001954 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 kmem_cache_destroy(fib6_node_kmem);
1956}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001957
1958#ifdef CONFIG_PROC_FS
1959
1960struct ipv6_route_iter {
1961 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001962 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001963 loff_t skip;
1964 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001965 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001966};
1967
1968static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1969{
1970 struct rt6_info *rt = v;
1971 struct ipv6_route_iter *iter = seq->private;
1972
1973 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1974
1975#ifdef CONFIG_IPV6_SUBTREES
1976 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1977#else
1978 seq_puts(seq, "00000000000000000000000000000000 00 ");
1979#endif
1980 if (rt->rt6i_flags & RTF_GATEWAY)
1981 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1982 else
1983 seq_puts(seq, "00000000000000000000000000000000");
1984
1985 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1986 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1987 rt->dst.__use, rt->rt6i_flags,
1988 rt->dst.dev ? rt->dst.dev->name : "");
1989 iter->w.leaf = NULL;
1990 return 0;
1991}
1992
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001993static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001994{
1995 struct ipv6_route_iter *iter = w->args;
1996
1997 if (!iter->skip)
1998 return 1;
1999
2000 do {
2001 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2002 iter->skip--;
2003 if (!iter->skip && iter->w.leaf)
2004 return 1;
2005 } while (iter->w.leaf);
2006
2007 return 0;
2008}
2009
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002010static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2011 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002012{
2013 memset(&iter->w, 0, sizeof(iter->w));
2014 iter->w.func = ipv6_route_yield;
2015 iter->w.root = &iter->tbl->tb6_root;
2016 iter->w.state = FWS_INIT;
2017 iter->w.node = iter->w.root;
2018 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002019 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002020 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002021 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002022}
2023
2024static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2025 struct net *net)
2026{
2027 unsigned int h;
2028 struct hlist_node *node;
2029
2030 if (tbl) {
2031 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2032 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2033 } else {
2034 h = 0;
2035 node = NULL;
2036 }
2037
2038 while (!node && h < FIB6_TABLE_HASHSZ) {
2039 node = rcu_dereference_bh(
2040 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2041 }
2042 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2043}
2044
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002045static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2046{
2047 if (iter->sernum != iter->w.root->fn_sernum) {
2048 iter->sernum = iter->w.root->fn_sernum;
2049 iter->w.state = FWS_INIT;
2050 iter->w.node = iter->w.root;
2051 WARN_ON(iter->w.skip);
2052 iter->w.skip = iter->w.count;
2053 }
2054}
2055
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002056static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2057{
2058 int r;
2059 struct rt6_info *n;
2060 struct net *net = seq_file_net(seq);
2061 struct ipv6_route_iter *iter = seq->private;
2062
2063 if (!v)
2064 goto iter_table;
2065
2066 n = ((struct rt6_info *)v)->dst.rt6_next;
2067 if (n) {
2068 ++*pos;
2069 return n;
2070 }
2071
2072iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002073 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002074 read_lock(&iter->tbl->tb6_lock);
2075 r = fib6_walk_continue(&iter->w);
2076 read_unlock(&iter->tbl->tb6_lock);
2077 if (r > 0) {
2078 if (v)
2079 ++*pos;
2080 return iter->w.leaf;
2081 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002082 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002083 return NULL;
2084 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002085 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002086
2087 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2088 if (!iter->tbl)
2089 return NULL;
2090
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002091 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002092 goto iter_table;
2093}
2094
2095static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2096 __acquires(RCU_BH)
2097{
2098 struct net *net = seq_file_net(seq);
2099 struct ipv6_route_iter *iter = seq->private;
2100
2101 rcu_read_lock_bh();
2102 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2103 iter->skip = *pos;
2104
2105 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002106 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002107 return ipv6_route_seq_next(seq, NULL, pos);
2108 } else {
2109 return NULL;
2110 }
2111}
2112
2113static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2114{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002115 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002116 return w->node && !(w->state == FWS_U && w->node == w->root);
2117}
2118
2119static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2120 __releases(RCU_BH)
2121{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002122 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002123 struct ipv6_route_iter *iter = seq->private;
2124
2125 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002126 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002127
2128 rcu_read_unlock_bh();
2129}
2130
2131static const struct seq_operations ipv6_route_seq_ops = {
2132 .start = ipv6_route_seq_start,
2133 .next = ipv6_route_seq_next,
2134 .stop = ipv6_route_seq_stop,
2135 .show = ipv6_route_seq_show
2136};
2137
2138int ipv6_route_open(struct inode *inode, struct file *file)
2139{
2140 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2141 sizeof(struct ipv6_route_iter));
2142}
2143
2144#endif /* CONFIG_PROC_FS */