blob: 595a57cbbc7ba855a6d2b2feb2ed9fdd236b45b9 [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>
Ido Schimmeldf77fe42017-08-03 13:28:17 +020036#include <net/fib_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <net/ip6_fib.h>
39#include <net/ip6_route.h>
40
41#define RT6_DEBUG 2
42
43#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000044#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
46#define RT6_TRACE(x...) do { ; } while (0)
47#endif
48
Wang Yufen437de072014-03-28 12:07:04 +080049static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020051struct fib6_cleaner {
52 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080053 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020055 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 void *arg;
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_IPV6_SUBTREES
60#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
62#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Duan Jiong163cd4e2014-05-09 13:31:43 +080065static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080066static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
67static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010068static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020069static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
76 */
77
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080078static void fib6_gc_timer_cb(unsigned long arg);
79
Michal Kubeček9a03cd82016-03-08 14:44:35 +010080#define FOR_WALKERS(net, w) \
81 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Michal Kubeček9a03cd82016-03-08 14:44:35 +010083static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070084{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010085 write_lock_bh(&net->ipv6.fib6_walker_lock);
86 list_add(&w->lh, &net->ipv6.fib6_walkers);
87 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070088}
89
Michal Kubeček9a03cd82016-03-08 14:44:35 +010090static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070091{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010092 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000093 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010094 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070095}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020096
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020099 int new, old;
100
101 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200102 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200103 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200104 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
105 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200106 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200109enum {
110 FIB6_NO_SERNUM_CHANGE = 0,
111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * Auxiliary address test functions for the radix tree.
115 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900116 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * 64bit processors)
118 */
119
120/*
121 * test bit
122 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000123#if defined(__LITTLE_ENDIAN)
124# define BITOP_BE32_SWIZZLE (0x1F & ~7)
125#else
126# define BITOP_BE32_SWIZZLE 0
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200129static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000131 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000132 /*
133 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
138 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700139 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
140 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200143static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct fib6_node *fn;
146
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800147 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return fn;
150}
151
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200152static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 kmem_cache_free(fib6_node_kmem, fn);
155}
156
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700157static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
158{
159 int cpu;
160
161 if (!non_pcpu_rt->rt6i_pcpu)
162 return;
163
164 for_each_possible_cpu(cpu) {
165 struct rt6_info **ppcpu_rt;
166 struct rt6_info *pcpu_rt;
167
168 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
169 pcpu_rt = *ppcpu_rt;
170 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700171 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700172 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700173 *ppcpu_rt = NULL;
174 }
175 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700176
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700177 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700178 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700179}
180
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200181static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700183 if (atomic_dec_and_test(&rt->rt6i_ref)) {
184 rt6_free_pcpu(rt);
Wei Wang95145282017-06-17 10:42:34 -0700185 dst_dev_put(&rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700186 dst_release(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800190static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700191{
192 unsigned int h;
193
Thomas Graf375216a2006-10-21 20:20:54 -0700194 /*
195 * Initialize table lock at a single place to give lockdep a key,
196 * tables aren't visible prior to being linked to the list.
197 */
198 rwlock_init(&tb->tb6_lock);
199
Neil Hormana33bc5c2009-07-30 18:52:15 -0700200 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700201
202 /*
203 * No protection necessary, this is the only list mutatation
204 * operation, tables never disappear once they exist.
205 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800206 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700207}
208
209#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800210
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800211static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700212{
213 struct fib6_table *table;
214
215 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500216 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700217 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800218 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700219 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700220 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 }
222
223 return table;
224}
225
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800226struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700227{
228 struct fib6_table *tb;
229
230 if (id == 0)
231 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800232 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700233 if (tb)
234 return tb;
235
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800236 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500237 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800238 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700239
240 return tb;
241}
David Ahernb3b46632016-05-04 21:46:12 -0700242EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700243
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800244struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700245{
246 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700248 unsigned int h;
249
250 if (id == 0)
251 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700252 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700253 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800254 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800255 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700256 if (tb->tb6_id == id) {
257 rcu_read_unlock();
258 return tb;
259 }
260 }
261 rcu_read_unlock();
262
263 return NULL;
264}
David Ahernc4850682015-10-12 11:47:08 -0700265EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700266
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000267static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700268{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800269 fib6_link_table(net, net->ipv6.fib6_main_tbl);
270 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700271}
Thomas Grafc71099a2006-08-04 23:20:06 -0700272#else
273
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800274struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700275{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800276 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700277}
278
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800279struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700280{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800281 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700282}
283
David S. Miller4c9483b2011-03-12 16:22:43 -0500284struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700286{
lucienab997ad2015-10-23 15:36:53 +0800287 struct rt6_info *rt;
288
289 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300290 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800291 ip6_rt_put(rt);
292 rt = net->ipv6.ip6_null_entry;
293 dst_hold(&rt->dst);
294 }
295
296 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700297}
298
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000299static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700300{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800301 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700302}
303
304#endif
305
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200306static int call_fib6_entry_notifiers(struct net *net,
307 enum fib_event_type event_type,
308 struct rt6_info *rt)
309{
310 struct fib6_entry_notifier_info info = {
311 .rt = rt,
312 };
313
314 return call_fib6_notifiers(net, event_type, &info.info);
315}
316
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200317static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700318{
319 int res;
320 struct rt6_info *rt;
321
Changli Gaod8d1f302010-06-10 23:31:35 -0700322 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700323 res = rt6_dump_route(rt, w->args);
324 if (res < 0) {
325 /* Frame is full, suspend walking */
326 w->leaf = rt;
327 return 1;
328 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800329
330 /* Multipath routes are dumped in one route with the
331 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
332 * last sibling of this route (no need to dump the
333 * sibling routes again)
334 */
335 if (rt->rt6i_nsiblings)
336 rt = list_last_entry(&rt->rt6i_siblings,
337 struct rt6_info,
338 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700339 }
340 w->leaf = NULL;
341 return 0;
342}
343
344static void fib6_dump_end(struct netlink_callback *cb)
345{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100346 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200347 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700348
349 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800350 if (cb->args[4]) {
351 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100352 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800353 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700354 cb->args[2] = 0;
355 kfree(w);
356 }
Wang Yufen437de072014-03-28 12:07:04 +0800357 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700358 cb->args[1] = 3;
359}
360
361static int fib6_dump_done(struct netlink_callback *cb)
362{
363 fib6_dump_end(cb);
364 return cb->done ? cb->done(cb) : 0;
365}
366
367static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
368 struct netlink_callback *cb)
369{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100370 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200371 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700372 int res;
373
374 w = (void *)cb->args[2];
375 w->root = &table->tb6_root;
376
377 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000378 w->count = 0;
379 w->skip = 0;
380
Patrick McHardy1b43af52006-08-10 23:11:17 -0700381 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100382 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700383 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000384 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700385 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000386 cb->args[5] = w->root->fn_sernum;
387 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700388 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000389 if (cb->args[5] != w->root->fn_sernum) {
390 /* Begin at the root if the tree changed */
391 cb->args[5] = w->root->fn_sernum;
392 w->state = FWS_INIT;
393 w->node = w->root;
394 w->skip = w->count;
395 } else
396 w->skip = 0;
397
Patrick McHardy1b43af52006-08-10 23:11:17 -0700398 read_lock_bh(&table->tb6_lock);
399 res = fib6_walk_continue(w);
400 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800401 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100402 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800403 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700404 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700405 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800406
Patrick McHardy1b43af52006-08-10 23:11:17 -0700407 return res;
408}
409
Thomas Grafc127ea22007-03-22 11:58:32 -0700410static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700411{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900412 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700413 unsigned int h, s_h;
414 unsigned int e = 0, s_e;
415 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200416 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700417 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800418 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700419 int res = 0;
420
421 s_h = cb->args[0];
422 s_e = cb->args[1];
423
424 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500425 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700426 /* New dump:
427 *
428 * 1. hook callback destructor.
429 */
430 cb->args[3] = (long)cb->done;
431 cb->done = fib6_dump_done;
432
433 /*
434 * 2. allocate and initialize walker.
435 */
436 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500437 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700438 return -ENOMEM;
439 w->func = fib6_dump_node;
440 cb->args[2] = (long)w;
441 }
442
443 arg.skb = skb;
444 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700445 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700446 w->args = &arg;
447
Eric Dumazete67f88d2011-04-27 22:56:07 +0000448 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700449 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700450 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800451 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800452 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700453 if (e < s_e)
454 goto next;
455 res = fib6_dump_table(tb, skb, cb);
456 if (res != 0)
457 goto out;
458next:
459 e++;
460 }
461 }
462out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000463 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700464 cb->args[1] = e;
465 cb->args[0] = h;
466
467 res = res < 0 ? res : skb->len;
468 if (res <= 0)
469 fib6_dump_end(cb);
470 return res;
471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473/*
474 * Routing Table
475 *
476 * return the appropriate node for a routing tree "add" operation
477 * by either creating and inserting or by returning an existing
478 * node.
479 */
480
fan.du9225b232013-07-22 14:21:09 +0800481static struct fib6_node *fib6_add_1(struct fib6_node *root,
482 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000483 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600484 int replace_required, int sernum,
485 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct fib6_node *fn, *in, *ln;
488 struct fib6_node *pn = NULL;
489 struct rt6key *key;
490 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900491 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 RT6_TRACE("fib6_add_1\n");
494
495 /* insert node in tree */
496
497 fn = root;
498
499 do {
500 key = (struct rt6key *)((u8 *)fn->leaf + offset);
501
502 /*
503 * Prefix match
504 */
505 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000506 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000507 if (!allow_create) {
508 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600509 NL_SET_ERR_MSG(extack,
510 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000511 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000512 return ERR_PTR(-ENOENT);
513 }
Joe Perchesf3213832012-05-15 14:11:53 +0000514 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000517 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * Exact match ?
521 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (plen == fn->fn_bit) {
524 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500525 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rt6_release(fn->leaf);
527 fn->leaf = NULL;
528 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return fn;
533 }
534
535 /*
536 * We have more bits to go
537 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Try to walk down on tree. */
540 fn->fn_sernum = sernum;
541 dir = addr_bit_set(addr, fn->fn_bit);
542 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800543 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } while (fn);
545
Matti Vaittinen14df0152011-11-16 21:18:02 +0000546 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000547 /* We should not create new node because
548 * NLM_F_REPLACE was specified without NLM_F_CREATE
549 * I assume it is safe to require NLM_F_CREATE when
550 * REPLACE flag is used! Later we may want to remove the
551 * check for replace_required, because according
552 * to netlink specification, NLM_F_CREATE
553 * MUST be specified if new route is created.
554 * That would keep IPv6 consistent with IPv4
555 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000556 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600557 NL_SET_ERR_MSG(extack,
558 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000559 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000560 return ERR_PTR(-ENOENT);
561 }
Joe Perchesf3213832012-05-15 14:11:53 +0000562 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * We walked to the bottom of tree.
566 * Create new leaf node without children.
567 */
568
569 ln = node_alloc();
570
David S. Miller507c9b12011-12-03 17:50:45 -0500571 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000572 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ln->parent = pn;
576 ln->fn_sernum = sernum;
577
578 if (dir)
579 pn->right = ln;
580 else
581 pn->left = ln;
582
583 return ln;
584
585
586insert_above:
587 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900588 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * we have a less significant route.
590 * we've to insert an intermediate node on the list
591 * this new node will point to the one we need to create
592 * and the current
593 */
594
595 pn = fn->parent;
596
597 /* find 1st bit in difference between the 2 addrs.
598
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800599 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 but if it is >= plen, the value is ignored in any case.
601 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900602
fan.du9225b232013-07-22 14:21:09 +0800603 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900605 /*
606 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * / \
608 * (new leaf node)[ln] (old node)[fn]
609 */
610 if (plen > bit) {
611 in = node_alloc();
612 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900613
David S. Miller507c9b12011-12-03 17:50:45 -0500614 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (in)
616 node_free(in);
617 if (ln)
618 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000619 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900622 /*
623 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * RTN_RTINFO will
625 * be off since that an address that chooses one of
626 * the branches would not match less specific routes
627 * in the other branch
628 */
629
630 in->fn_bit = bit;
631
632 in->parent = pn;
633 in->leaf = fn->leaf;
634 atomic_inc(&in->leaf->rt6i_ref);
635
636 in->fn_sernum = sernum;
637
638 /* update parent pointer */
639 if (dir)
640 pn->right = in;
641 else
642 pn->left = in;
643
644 ln->fn_bit = plen;
645
646 ln->parent = in;
647 fn->parent = in;
648
649 ln->fn_sernum = sernum;
650
651 if (addr_bit_set(addr, bit)) {
652 in->right = ln;
653 in->left = fn;
654 } else {
655 in->left = ln;
656 in->right = fn;
657 }
658 } else { /* plen <= bit */
659
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900660 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * (new leaf node)[ln]
662 * / \
663 * (old node)[fn] NULL
664 */
665
666 ln = node_alloc();
667
David S. Miller507c9b12011-12-03 17:50:45 -0500668 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000669 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 ln->fn_bit = plen;
672
673 ln->parent = pn;
674
675 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (dir)
678 pn->right = ln;
679 else
680 pn->left = ln;
681
682 if (addr_bit_set(&key->addr, plen))
683 ln->right = fn;
684 else
685 ln->left = fn;
686
687 fn->parent = ln;
688 }
689 return ln;
690}
691
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200692static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200693{
694 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
695 RTF_GATEWAY;
696}
697
Florian Westphale715b6d2015-01-05 23:57:44 +0100698static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100699{
Florian Westphale715b6d2015-01-05 23:57:44 +0100700 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100701
Florian Westphale715b6d2015-01-05 23:57:44 +0100702 for (i = 0; i < RTAX_MAX; i++) {
703 if (test_bit(i, mxc->mx_valid))
704 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100705 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100706}
707
708static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
709{
710 if (!mxc->mx)
711 return 0;
712
713 if (dst->flags & DST_HOST) {
714 u32 *mp = dst_metrics_write_ptr(dst);
715
716 if (unlikely(!mp))
717 return -ENOMEM;
718
719 fib6_copy_metrics(mp, mxc);
720 } else {
721 dst_init_metrics(dst, mxc->mx, false);
722
723 /* We've stolen mx now. */
724 mxc->mx = NULL;
725 }
726
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100727 return 0;
728}
729
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100730static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
731 struct net *net)
732{
733 if (atomic_read(&rt->rt6i_ref) != 1) {
734 /* This route is used as dummy address holder in some split
735 * nodes. It is not leaked, but it still holds other resources,
736 * which must be released in time. So, scan ascendant nodes
737 * and replace dummy references to this route with references
738 * to still alive ones.
739 */
740 while (fn) {
741 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
742 fn->leaf = fib6_find_prefix(net, fn);
743 atomic_inc(&fn->leaf->rt6i_ref);
744 rt6_release(rt);
745 }
746 fn = fn->parent;
747 }
748 /* No more references are possible at this point. */
749 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
750 }
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Insert routing information in a node.
755 */
756
757static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100758 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct rt6_info *iter = NULL;
761 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200762 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500763 int replace = (info->nlh &&
764 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
765 int add = (!info->nlh ||
766 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000767 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200768 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200769 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100770 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
David Ahern1f5e29c2017-01-31 16:51:37 -0800772 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
773 nlflags |= NLM_F_APPEND;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ins = &fn->leaf;
776
David S. Miller507c9b12011-12-03 17:50:45 -0500777 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Search for duplicates
780 */
781
782 if (iter->rt6i_metric == rt->rt6i_metric) {
783 /*
784 * Same priority level
785 */
David S. Miller507c9b12011-12-03 17:50:45 -0500786 if (info->nlh &&
787 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000788 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200789
790 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000791 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200792 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
793 found++;
794 break;
795 }
796 if (rt_can_ecmp)
797 fallback_ins = fallback_ins ?: ins;
798 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
David Ahernf06b7542017-07-05 14:41:46 -0600801 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000802 if (rt->rt6i_nsiblings)
803 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500804 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000806 if (!(rt->rt6i_flags & RTF_EXPIRES))
807 rt6_clean_expires(iter);
808 else
809 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700810 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -EEXIST;
812 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000813 /* If we have the same destination and the same metric,
814 * but not the same gateway, then the route we try to
815 * add is sibling to this route, increment our counter
816 * of siblings, and later we will add our route to the
817 * list.
818 * Only static routes (which don't have flag
819 * RTF_EXPIRES) are used for ECMPv6.
820 *
821 * To avoid long list, we only had siblings if the
822 * route have a gateway.
823 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200824 if (rt_can_ecmp &&
825 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000826 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 if (iter->rt6i_metric > rt->rt6i_metric)
830 break;
831
Michal Kubeček27596472015-05-18 20:54:00 +0200832next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700833 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Michal Kubeček27596472015-05-18 20:54:00 +0200836 if (fallback_ins && !found) {
837 /* No ECMP-able route found, replace first non-ECMP one */
838 ins = fallback_ins;
839 iter = *ins;
840 found++;
841 }
842
David S. Millerf11e6652007-03-24 20:36:25 -0700843 /* Reset round-robin state, if necessary */
844 if (ins == &fn->leaf)
845 fn->rr_ptr = NULL;
846
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000847 /* Link this route to others same route. */
848 if (rt->rt6i_nsiblings) {
849 unsigned int rt6i_nsiblings;
850 struct rt6_info *sibling, *temp_sibling;
851
852 /* Find the first route that have the same metric */
853 sibling = fn->leaf;
854 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200855 if (sibling->rt6i_metric == rt->rt6i_metric &&
856 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000857 list_add_tail(&rt->rt6i_siblings,
858 &sibling->rt6i_siblings);
859 break;
860 }
861 sibling = sibling->dst.rt6_next;
862 }
863 /* For each sibling in the list, increment the counter of
864 * siblings. BUG() if counters does not match, list of siblings
865 * is broken!
866 */
867 rt6i_nsiblings = 0;
868 list_for_each_entry_safe(sibling, temp_sibling,
869 &rt->rt6i_siblings, rt6i_siblings) {
870 sibling->rt6i_nsiblings++;
871 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
872 rt6i_nsiblings++;
873 }
874 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * insert node
879 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000880 if (!replace) {
881 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000882 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000884add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200885 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100886 err = fib6_commit_metrics(&rt->dst, mxc);
887 if (err)
888 return err;
889
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000890 rt->dst.rt6_next = iter;
891 *ins = rt;
892 rt->rt6i_node = fn;
893 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200894 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
895 rt);
David Ahern3b1137f2017-02-02 12:37:10 -0800896 if (!info->skip_notify)
897 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000898 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
David S. Miller507c9b12011-12-03 17:50:45 -0500900 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000901 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
902 fn->fn_flags |= RTN_RTINFO;
903 }
904
905 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200906 int nsiblings;
907
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000908 if (!found) {
909 if (add)
910 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +0000911 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000912 return -ENOENT;
913 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100914
915 err = fib6_commit_metrics(&rt->dst, mxc);
916 if (err)
917 return err;
918
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000919 *ins = rt;
920 rt->rt6i_node = fn;
921 rt->dst.rt6_next = iter->dst.rt6_next;
922 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200923 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
924 rt);
David Ahern3b1137f2017-02-02 12:37:10 -0800925 if (!info->skip_notify)
926 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -0500927 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000928 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
929 fn->fn_flags |= RTN_RTINFO;
930 }
Michal Kubeček27596472015-05-18 20:54:00 +0200931 nsiblings = iter->rt6i_nsiblings;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100932 fib6_purge_rt(iter, fn, info->nl_net);
933 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +0200934
935 if (nsiblings) {
936 /* Replacing an ECMP route, remove all siblings */
937 ins = &rt->dst.rt6_next;
938 iter = *ins;
939 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +0100940 if (iter->rt6i_metric > rt->rt6i_metric)
941 break;
Michal Kubeček27596472015-05-18 20:54:00 +0200942 if (rt6_qualify_for_ecmp(iter)) {
943 *ins = iter->dst.rt6_next;
944 fib6_purge_rt(iter, fn, info->nl_net);
945 rt6_release(iter);
946 nsiblings--;
947 } else {
948 ins = &iter->dst.rt6_next;
949 }
950 iter = *ins;
951 }
952 WARN_ON(nsiblings != 0);
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956 return 0;
957}
958
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200959static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700961 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -0500962 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700963 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700964 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800967void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700969 if (!timer_pending(&net->ipv6.ip6_fib_timer))
970 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700971 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * Add routing information to the routing tree.
976 * <destination addr>/<source addr>
977 * with source addr info in sub-trees
978 */
979
Florian Westphale715b6d2015-01-05 23:57:44 +0100980int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -0600981 struct nl_info *info, struct mx6_config *mxc,
982 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700984 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000986 int allow_create = 1;
987 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200988 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -0500989
Wei Wanga4c2fd72017-06-17 10:42:42 -0700990 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -0700991 return -EINVAL;
992
David S. Miller507c9b12011-12-03 17:50:45 -0500993 if (info->nlh) {
994 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000995 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500996 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000997 replace_required = 1;
998 }
999 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001000 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
fan.du9225b232013-07-22 14:21:09 +08001002 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1003 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -06001004 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001005 if (IS_ERR(fn)) {
1006 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001007 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001011 pn = fn;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#ifdef CONFIG_IPV6_SUBTREES
1014 if (rt->rt6i_src.plen) {
1015 struct fib6_node *sn;
1016
David S. Miller507c9b12011-12-03 17:50:45 -05001017 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct fib6_node *sfn;
1019
1020 /*
1021 * Create subtree.
1022 *
1023 * fn[main tree]
1024 * |
1025 * sfn[subtree root]
1026 * \
1027 * sn[new leaf node]
1028 */
1029
1030 /* Create subtree root node */
1031 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001032 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto st_failure;
1034
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001035 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1036 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001038 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* Now add the first leaf node to new subtree */
1041
1042 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001043 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001044 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001045 allow_create, replace_required, sernum,
1046 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001048 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* If it is failed, discard just allocated
1050 root, and then (in st_failure) stale node
1051 in main tree.
1052 */
1053 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001054 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto st_failure;
1056 }
1057
1058 /* Now link new subtree to main tree */
1059 sfn->parent = fn;
1060 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 } else {
1062 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001063 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001064 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001065 allow_create, replace_required, sernum,
1066 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001068 if (IS_ERR(sn)) {
1069 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
David S. Miller507c9b12011-12-03 17:50:45 -05001074 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001075 fn->leaf = rt;
1076 atomic_inc(&rt->rt6i_ref);
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 fn = sn;
1079 }
1080#endif
1081
Florian Westphale715b6d2015-01-05 23:57:44 +01001082 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001083 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001084 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001085 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001086 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001090 if (err) {
1091#ifdef CONFIG_IPV6_SUBTREES
1092 /*
1093 * If fib6_add_1 has cleared the old leaf pointer in the
1094 * super-tree leaf node we have to find a new one for it.
1095 */
David S. Miller3c051232008-04-18 01:46:19 -07001096 if (pn != fn && pn->leaf == rt) {
1097 pn->leaf = NULL;
1098 atomic_dec(&rt->rt6i_ref);
1099 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001100 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001101 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001102#if RT6_DEBUG >= 2
1103 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001104 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001105 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001106 }
1107#endif
1108 atomic_inc(&pn->leaf->rt6i_ref);
1109 }
1110#endif
Wei Wang1cfb71e2017-06-17 10:42:33 -07001111 /* Always release dst as dst->__refcnt is guaranteed
1112 * to be taken before entering this function
1113 */
Wei Wang587fea72017-06-17 10:42:36 -07001114 dst_release_immediate(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return err;
1117
1118#ifdef CONFIG_IPV6_SUBTREES
1119 /* Subtree creation failed, probably main tree node
1120 is orphan. If it is, shoot it.
1121 */
1122st_failure:
1123 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001124 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001125 /* Always release dst as dst->__refcnt is guaranteed
1126 * to be taken before entering this function
1127 */
Wei Wang587fea72017-06-17 10:42:36 -07001128 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return err;
1130#endif
1131}
1132
1133/*
1134 * Routing tree lookup
1135 *
1136 */
1137
1138struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001139 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001140 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141};
1142
Wang Yufen437de072014-03-28 12:07:04 +08001143static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1144 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001147 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001149 if (unlikely(args->offset == 0))
1150 return NULL;
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
1153 * Descend on a tree
1154 */
1155
1156 fn = root;
1157
1158 for (;;) {
1159 struct fib6_node *next;
1160
1161 dir = addr_bit_set(args->addr, fn->fn_bit);
1162
1163 next = dir ? fn->right : fn->left;
1164
1165 if (next) {
1166 fn = next;
1167 continue;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 break;
1170 }
1171
David S. Miller507c9b12011-12-03 17:50:45 -05001172 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001173 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct rt6key *key;
1175
1176 key = (struct rt6key *) ((u8 *) fn->leaf +
1177 args->offset);
1178
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001179 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1180#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001181 if (fn->subtree) {
1182 struct fib6_node *sfn;
1183 sfn = fib6_lookup_1(fn->subtree,
1184 args + 1);
1185 if (!sfn)
1186 goto backtrack;
1187 fn = sfn;
1188 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001189#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001190 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001191 return fn;
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001194#ifdef CONFIG_IPV6_SUBTREES
1195backtrack:
1196#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001197 if (fn->fn_flags & RTN_ROOT)
1198 break;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 fn = fn->parent;
1201 }
1202
1203 return NULL;
1204}
1205
Wang Yufen437de072014-03-28 12:07:04 +08001206struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1207 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001210 struct lookup_args args[] = {
1211 {
1212 .offset = offsetof(struct rt6_info, rt6i_dst),
1213 .addr = daddr,
1214 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001216 {
1217 .offset = offsetof(struct rt6_info, rt6i_src),
1218 .addr = saddr,
1219 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001221 {
1222 .offset = 0, /* sentinel */
1223 }
1224 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001226 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001227 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 fn = root;
1229
1230 return fn;
1231}
1232
1233/*
1234 * Get node with specified destination prefix (and source prefix,
1235 * if subtrees are used)
1236 */
1237
1238
Wang Yufen437de072014-03-28 12:07:04 +08001239static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1240 const struct in6_addr *addr,
1241 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct fib6_node *fn;
1244
1245 for (fn = root; fn ; ) {
1246 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1247
1248 /*
1249 * Prefix match
1250 */
1251 if (plen < fn->fn_bit ||
1252 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1253 return NULL;
1254
1255 if (plen == fn->fn_bit)
1256 return fn;
1257
1258 /*
1259 * We have more bits to go
1260 */
1261 if (addr_bit_set(addr, fn->fn_bit))
1262 fn = fn->right;
1263 else
1264 fn = fn->left;
1265 }
1266 return NULL;
1267}
1268
Wang Yufen437de072014-03-28 12:07:04 +08001269struct fib6_node *fib6_locate(struct fib6_node *root,
1270 const struct in6_addr *daddr, int dst_len,
1271 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct fib6_node *fn;
1274
1275 fn = fib6_locate_1(root, daddr, dst_len,
1276 offsetof(struct rt6_info, rt6i_dst));
1277
1278#ifdef CONFIG_IPV6_SUBTREES
1279 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001280 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001281 if (fn && fn->subtree)
1282 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 offsetof(struct rt6_info, rt6i_src));
1284 }
1285#endif
1286
David S. Miller507c9b12011-12-03 17:50:45 -05001287 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return fn;
1289
1290 return NULL;
1291}
1292
1293
1294/*
1295 * Deletion
1296 *
1297 */
1298
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001299static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
David S. Miller507c9b12011-12-03 17:50:45 -05001301 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001302 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
David S. Miller507c9b12011-12-03 17:50:45 -05001304 while (fn) {
1305 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001307 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return fn->right->leaf;
1309
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001310 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312 return NULL;
1313}
1314
1315/*
1316 * Called to trim the tree of intermediate nodes when possible. "fn"
1317 * is the node we want to try and remove.
1318 */
1319
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001320static struct fib6_node *fib6_repair_tree(struct net *net,
1321 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int children;
1324 int nstate;
1325 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001326 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int iter = 0;
1328
1329 for (;;) {
1330 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1331 iter++;
1332
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001333 WARN_ON(fn->fn_flags & RTN_RTINFO);
1334 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001335 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 children = 0;
1338 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001339 if (fn->right)
1340 child = fn->right, children |= 1;
1341 if (fn->left)
1342 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001344 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345#ifdef CONFIG_IPV6_SUBTREES
1346 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001347 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348#endif
1349 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001350 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001352 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001353 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001354 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356#endif
1357 atomic_inc(&fn->leaf->rt6i_ref);
1358 return fn->parent;
1359 }
1360
1361 pn = fn->parent;
1362#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001363 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001364 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001365 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 nstate = FWS_L;
1367 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001368 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001370 if (pn->right == fn)
1371 pn->right = child;
1372 else if (pn->left == fn)
1373 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001375 else
1376 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377#endif
1378 if (child)
1379 child->parent = pn;
1380 nstate = FWS_R;
1381#ifdef CONFIG_IPV6_SUBTREES
1382 }
1383#endif
1384
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001385 read_lock(&net->ipv6.fib6_walker_lock);
1386 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001387 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (w->root == fn) {
1389 w->root = w->node = NULL;
1390 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1391 } else if (w->node == fn) {
1392 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1393 w->node = pn;
1394 w->state = nstate;
1395 }
1396 } else {
1397 if (w->root == fn) {
1398 w->root = child;
1399 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1400 }
1401 if (w->node == fn) {
1402 w->node = child;
1403 if (children&2) {
1404 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001405 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 } else {
1407 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001408 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 }
1411 }
1412 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001413 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001416 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return pn;
1418
1419 rt6_release(pn->leaf);
1420 pn->leaf = NULL;
1421 fn = pn;
1422 }
1423}
1424
1425static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001426 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001428 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001430 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 RT6_TRACE("fib6_del_route\n");
1433
1434 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001435 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001437 net->ipv6.rt6_stats->fib_rt_entries--;
1438 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
David S. Millerf11e6652007-03-24 20:36:25 -07001440 /* Reset round-robin state, if necessary */
1441 if (fn->rr_ptr == rt)
1442 fn->rr_ptr = NULL;
1443
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001444 /* Remove this entry from other siblings */
1445 if (rt->rt6i_nsiblings) {
1446 struct rt6_info *sibling, *next_sibling;
1447
1448 list_for_each_entry_safe(sibling, next_sibling,
1449 &rt->rt6i_siblings, rt6i_siblings)
1450 sibling->rt6i_nsiblings--;
1451 rt->rt6i_nsiblings = 0;
1452 list_del_init(&rt->rt6i_siblings);
1453 }
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001456 read_lock(&net->ipv6.fib6_walker_lock);
1457 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (w->state == FWS_C && w->leaf == rt) {
1459 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001460 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001461 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 w->state = FWS_U;
1463 }
1464 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001465 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Changli Gaod8d1f302010-06-10 23:31:35 -07001467 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001470 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001472 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001473 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001476 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001478 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
David Ahern16a16cd2017-02-02 12:37:11 -08001479 if (!info->skip_notify)
1480 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 rt6_release(rt);
1482}
1483
Thomas Graf86872cb2006-08-22 00:01:08 -07001484int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001486 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct fib6_node *fn = rt->rt6i_node;
1488 struct rt6_info **rtp;
1489
1490#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001491 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001492 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return -ENOENT;
1494 }
1495#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001496 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -ENOENT;
1498
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001499 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
David S. Miller507c9b12011-12-03 17:50:45 -05001501 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001502 struct fib6_node *pn = fn;
1503#ifdef CONFIG_IPV6_SUBTREES
1504 /* clones of this route might be in another subtree */
1505 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001506 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001507 pn = pn->parent;
1508 pn = pn->parent;
1509 }
1510#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001511 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /*
1515 * Walk the leaf entries looking for ourself
1516 */
1517
Changli Gaod8d1f302010-06-10 23:31:35 -07001518 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001520 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 0;
1522 }
1523 }
1524 return -ENOENT;
1525}
1526
1527/*
1528 * Tree traversal function.
1529 *
1530 * Certainly, it is not interrupt safe.
1531 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1532 * It means, that we can modify tree during walking
1533 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001534 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
1536 * It guarantees that every node will be traversed,
1537 * and that it will be traversed only once.
1538 *
1539 * Callback function w->func may return:
1540 * 0 -> continue walking.
1541 * positive value -> walking is suspended (used by tree dumps,
1542 * and probably by gc, if it will be split to several slices)
1543 * negative value -> terminate walking.
1544 *
1545 * The function itself returns:
1546 * 0 -> walk is complete.
1547 * >0 -> walk is incomplete (i.e. suspended)
1548 * <0 -> walk is terminated by an error.
1549 */
1550
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001551static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 struct fib6_node *fn, *pn;
1554
1555 for (;;) {
1556 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001557 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559
1560 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001561 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 w->state = FWS_C;
1563 w->leaf = fn->leaf;
1564 }
1565 switch (w->state) {
1566#ifdef CONFIG_IPV6_SUBTREES
1567 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001568 if (FIB6_SUBTREE(fn)) {
1569 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 continue;
1571 }
1572 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 case FWS_L:
1575 if (fn->left) {
1576 w->node = fn->left;
1577 w->state = FWS_INIT;
1578 continue;
1579 }
1580 w->state = FWS_R;
1581 case FWS_R:
1582 if (fn->right) {
1583 w->node = fn->right;
1584 w->state = FWS_INIT;
1585 continue;
1586 }
1587 w->state = FWS_C;
1588 w->leaf = fn->leaf;
1589 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001590 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001591 int err;
1592
Eric Dumazetfa809e22012-06-25 15:37:19 -07001593 if (w->skip) {
1594 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001595 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001596 }
1597
1598 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (err)
1600 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001601
1602 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 continue;
1604 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001605skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 w->state = FWS_U;
1607 case FWS_U:
1608 if (fn == w->root)
1609 return 0;
1610 pn = fn->parent;
1611 w->node = pn;
1612#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001613 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001614 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 w->state = FWS_L;
1616 continue;
1617 }
1618#endif
1619 if (pn->left == fn) {
1620 w->state = FWS_R;
1621 continue;
1622 }
1623 if (pn->right == fn) {
1624 w->state = FWS_C;
1625 w->leaf = w->node->leaf;
1626 continue;
1627 }
1628#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001629 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630#endif
1631 }
1632 }
1633}
1634
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001635static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 int res;
1638
1639 w->state = FWS_INIT;
1640 w->node = w->root;
1641
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001642 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 res = fib6_walk_continue(w);
1644 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001645 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return res;
1647}
1648
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001649static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 int res;
1652 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001653 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001654 struct nl_info info = {
1655 .nl_net = c->net,
1656 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001658 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1659 w->node->fn_sernum != c->sernum)
1660 w->node->fn_sernum = c->sernum;
1661
1662 if (!c->func) {
1663 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1664 w->leaf = NULL;
1665 return 0;
1666 }
1667
Changli Gaod8d1f302010-06-10 23:31:35 -07001668 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 res = c->func(rt, c->arg);
1670 if (res < 0) {
1671 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001672 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (res) {
1674#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001675 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1676 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677#endif
1678 continue;
1679 }
1680 return 0;
1681 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001682 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684 w->leaf = rt;
1685 return 0;
1686}
1687
1688/*
1689 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001690 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * func is called on each route.
1692 * It may return -1 -> delete this route.
1693 * 0 -> continue walking
1694 *
1695 * prune==1 -> only immediate children of node (certainly,
1696 * ignoring pure split nodes) will be scanned.
1697 */
1698
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001699static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001700 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001701 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001703 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 c.w.root = root;
1706 c.w.func = fib6_clean_node;
1707 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001708 c.w.count = 0;
1709 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001711 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001713 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001715 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001718static void __fib6_clean_all(struct net *net,
1719 int (*func)(struct rt6_info *, void *),
1720 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001721{
Thomas Grafc71099a2006-08-04 23:20:06 -07001722 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001723 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001724 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001725
Patrick McHardy1b43af52006-08-10 23:11:17 -07001726 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001727 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001728 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001729 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001730 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001731 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001732 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001733 write_unlock_bh(&table->tb6_lock);
1734 }
1735 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001736 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001737}
1738
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001739void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1740 void *arg)
1741{
1742 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1746{
1747 if (rt->rt6i_flags & RTF_CACHE) {
1748 RT6_TRACE("pruning clone %p\n", rt);
1749 return -1;
1750 }
1751
1752 return 0;
1753}
1754
Duan Jiong163cd4e2014-05-09 13:31:43 +08001755static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001757 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1758 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001759}
1760
1761static void fib6_flush_trees(struct net *net)
1762{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001763 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001764
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001765 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/*
1769 * Garbage collection
1770 */
1771
Michal Kubeček3570df92016-03-08 14:44:25 +01001772struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 int timeout;
1775 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001776};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778static int fib6_age(struct rt6_info *rt, void *arg)
1779{
Michal Kubeček3570df92016-03-08 14:44:25 +01001780 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned long now = jiffies;
1782
1783 /*
1784 * check addrconf expiration here.
1785 * Routes are expired even if they are in use.
1786 *
1787 * Also age clones. Note, that clones are aged out
1788 * only if they are not in use now.
1789 */
1790
David S. Millerd1918542011-12-28 20:19:20 -05001791 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1792 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return -1;
1795 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001796 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 } else if (rt->rt6i_flags & RTF_CACHE) {
Wei Wang1cfb71e2017-06-17 10:42:33 -07001798 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001799 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 RT6_TRACE("aging clone %p\n", rt);
1801 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001802 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1803 struct neighbour *neigh;
1804 __u8 neigh_flags = 0;
1805
1806 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1807 if (neigh) {
1808 neigh_flags = neigh->flags;
1809 neigh_release(neigh);
1810 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001811 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001812 RT6_TRACE("purging route %p via non-router but gateway\n",
1813 rt);
1814 return -1;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001817 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
1820 return 0;
1821}
1822
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001823void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Michal Kubeček3570df92016-03-08 14:44:25 +01001825 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001826 unsigned long now;
1827
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001828 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001829 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1830 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001831 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1832 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001834 gc_args.timeout = expires ? (int)expires :
1835 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001836 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001837
Michal Kubeček3570df92016-03-08 14:44:25 +01001838 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001839 now = jiffies;
1840 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001843 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001844 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001845 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001846 else
1847 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001848 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001851static void fib6_gc_timer_cb(unsigned long arg)
1852{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001853 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001854}
1855
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001856static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001857{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001858 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001859 int err;
1860
1861 err = fib6_notifier_init(net);
1862 if (err)
1863 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001864
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001865 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001866 rwlock_init(&net->ipv6.fib6_walker_lock);
1867 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001868 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001869
Benjamin Theryc5728722008-03-03 23:34:17 -08001870 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1871 if (!net->ipv6.rt6_stats)
1872 goto out_timer;
1873
Eric Dumazet10da66f2010-10-13 08:22:03 +00001874 /* Avoid false sharing : Use at least a full cache line */
1875 size = max_t(size_t, size, L1_CACHE_BYTES);
1876
1877 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001878 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001879 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001880
1881 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1882 GFP_KERNEL);
1883 if (!net->ipv6.fib6_main_tbl)
1884 goto out_fib_table_hash;
1885
1886 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001887 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001888 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1889 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001890 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001891
1892#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1893 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1894 GFP_KERNEL);
1895 if (!net->ipv6.fib6_local_tbl)
1896 goto out_fib6_main_tbl;
1897 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001898 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001899 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1900 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001901 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001902#endif
1903 fib6_tables_init(net);
1904
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001905 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001906
1907#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1908out_fib6_main_tbl:
1909 kfree(net->ipv6.fib6_main_tbl);
1910#endif
1911out_fib_table_hash:
1912 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001913out_rt6_stats:
1914 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001915out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001916 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001917 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08001918}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001919
1920static void fib6_net_exit(struct net *net)
1921{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001922 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001923 del_timer_sync(&net->ipv6.ip6_fib_timer);
1924
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001925#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07001926 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001927 kfree(net->ipv6.fib6_local_tbl);
1928#endif
David S. Miller8e773272012-06-11 00:01:52 -07001929 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001930 kfree(net->ipv6.fib6_main_tbl);
1931 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001932 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001933 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001934}
1935
1936static struct pernet_operations fib6_net_ops = {
1937 .init = fib6_net_init,
1938 .exit = fib6_net_exit,
1939};
1940
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001941int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001943 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1946 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001947 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001948 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001949 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001950 goto out;
1951
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001952 ret = register_pernet_subsys(&fib6_net_ops);
1953 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001954 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07001955
1956 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1957 NULL);
1958 if (ret)
1959 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001960
1961 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001962out:
1963 return ret;
1964
David S. Millere8803b62012-06-16 01:12:19 -07001965out_unregister_subsys:
1966 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001967out_kmem_cache_create:
1968 kmem_cache_destroy(fib6_node_kmem);
1969 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972void fib6_gc_cleanup(void)
1973{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001974 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 kmem_cache_destroy(fib6_node_kmem);
1976}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001977
1978#ifdef CONFIG_PROC_FS
1979
1980struct ipv6_route_iter {
1981 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001982 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001983 loff_t skip;
1984 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02001985 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02001986};
1987
1988static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1989{
1990 struct rt6_info *rt = v;
1991 struct ipv6_route_iter *iter = seq->private;
1992
1993 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1994
1995#ifdef CONFIG_IPV6_SUBTREES
1996 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1997#else
1998 seq_puts(seq, "00000000000000000000000000000000 00 ");
1999#endif
2000 if (rt->rt6i_flags & RTF_GATEWAY)
2001 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2002 else
2003 seq_puts(seq, "00000000000000000000000000000000");
2004
2005 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2006 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2007 rt->dst.__use, rt->rt6i_flags,
2008 rt->dst.dev ? rt->dst.dev->name : "");
2009 iter->w.leaf = NULL;
2010 return 0;
2011}
2012
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002013static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002014{
2015 struct ipv6_route_iter *iter = w->args;
2016
2017 if (!iter->skip)
2018 return 1;
2019
2020 do {
2021 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2022 iter->skip--;
2023 if (!iter->skip && iter->w.leaf)
2024 return 1;
2025 } while (iter->w.leaf);
2026
2027 return 0;
2028}
2029
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002030static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2031 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002032{
2033 memset(&iter->w, 0, sizeof(iter->w));
2034 iter->w.func = ipv6_route_yield;
2035 iter->w.root = &iter->tbl->tb6_root;
2036 iter->w.state = FWS_INIT;
2037 iter->w.node = iter->w.root;
2038 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002039 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002040 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002041 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002042}
2043
2044static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2045 struct net *net)
2046{
2047 unsigned int h;
2048 struct hlist_node *node;
2049
2050 if (tbl) {
2051 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2052 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2053 } else {
2054 h = 0;
2055 node = NULL;
2056 }
2057
2058 while (!node && h < FIB6_TABLE_HASHSZ) {
2059 node = rcu_dereference_bh(
2060 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2061 }
2062 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2063}
2064
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002065static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2066{
2067 if (iter->sernum != iter->w.root->fn_sernum) {
2068 iter->sernum = iter->w.root->fn_sernum;
2069 iter->w.state = FWS_INIT;
2070 iter->w.node = iter->w.root;
2071 WARN_ON(iter->w.skip);
2072 iter->w.skip = iter->w.count;
2073 }
2074}
2075
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002076static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2077{
2078 int r;
2079 struct rt6_info *n;
2080 struct net *net = seq_file_net(seq);
2081 struct ipv6_route_iter *iter = seq->private;
2082
2083 if (!v)
2084 goto iter_table;
2085
2086 n = ((struct rt6_info *)v)->dst.rt6_next;
2087 if (n) {
2088 ++*pos;
2089 return n;
2090 }
2091
2092iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002093 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002094 read_lock(&iter->tbl->tb6_lock);
2095 r = fib6_walk_continue(&iter->w);
2096 read_unlock(&iter->tbl->tb6_lock);
2097 if (r > 0) {
2098 if (v)
2099 ++*pos;
2100 return iter->w.leaf;
2101 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002102 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002103 return NULL;
2104 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002105 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002106
2107 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2108 if (!iter->tbl)
2109 return NULL;
2110
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002111 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002112 goto iter_table;
2113}
2114
2115static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2116 __acquires(RCU_BH)
2117{
2118 struct net *net = seq_file_net(seq);
2119 struct ipv6_route_iter *iter = seq->private;
2120
2121 rcu_read_lock_bh();
2122 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2123 iter->skip = *pos;
2124
2125 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002126 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002127 return ipv6_route_seq_next(seq, NULL, pos);
2128 } else {
2129 return NULL;
2130 }
2131}
2132
2133static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2134{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002135 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002136 return w->node && !(w->state == FWS_U && w->node == w->root);
2137}
2138
2139static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2140 __releases(RCU_BH)
2141{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002142 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002143 struct ipv6_route_iter *iter = seq->private;
2144
2145 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002146 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002147
2148 rcu_read_unlock_bh();
2149}
2150
2151static const struct seq_operations ipv6_route_seq_ops = {
2152 .start = ipv6_route_seq_start,
2153 .next = ipv6_route_seq_next,
2154 .stop = ipv6_route_seq_stop,
2155 .show = ipv6_route_seq_show
2156};
2157
2158int ipv6_route_open(struct inode *inode, struct file *file)
2159{
2160 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2161 sizeof(struct ipv6_route_iter));
2162}
2163
2164#endif /* CONFIG_PROC_FS */