blob: 9180c8b6f764267a41a8aff4a8bcca5c052eb43e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Forwarding Information Database
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Wang Yufen8db46f12014-03-28 12:07:02 +08009 * Changes:
10 * Yuji SEKIYA @USAGI: Support default route on router node;
11 * remove ip6_null_entry from the top of
12 * routing table.
13 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
Joe Perchesf3213832012-05-15 14:11:53 +000015
16#define pr_fmt(fmt) "IPv6: " fmt
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
19#include <linux/types.h>
20#include <linux/net.h>
21#include <linux/route.h>
22#include <linux/netdevice.h>
23#include <linux/in6.h>
24#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070025#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Aherncc5f0eb2018-10-04 20:07:52 -070028#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/ipv6.h>
30#include <net/ndisc.h>
31#include <net/addrconf.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020032#include <net/lwtunnel.h>
Ido Schimmeldf77fe42017-08-03 13:28:17 +020033#include <net/fib_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <net/ip6_fib.h>
36#include <net/ip6_route.h>
37
Wang Yufen437de072014-03-28 12:07:04 +080038static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020040struct fib6_cleaner {
41 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080042 struct net *net;
David Ahern8d1c8022018-04-17 17:33:26 -070043 int (*func)(struct fib6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020044 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 void *arg;
David Ahern7c6bb7d2018-10-11 20:17:21 -070046 bool skip_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef CONFIG_IPV6_SUBTREES
50#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
52#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
54
David Ahern8d1c8022018-04-17 17:33:26 -070055static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -070056 struct fib6_table *table,
57 struct fib6_node *fn);
58static struct fib6_node *fib6_repair_tree(struct net *net,
59 struct fib6_table *table,
60 struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010061static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020062static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * A routing update causes an increase of the serial number on the
66 * affected subtree. This allows for cached routes to be asynchronously
67 * tested when modifications are made to the destination cache as a
68 * result of redirects, path MTU changes, etc.
69 */
70
Kees Cook86cb30e2017-10-17 20:21:24 -070071static void fib6_gc_timer_cb(struct timer_list *t);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080072
Michal Kubeček9a03cd82016-03-08 14:44:35 +010073#define FOR_WALKERS(net, w) \
74 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Michal Kubeček9a03cd82016-03-08 14:44:35 +010076static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070077{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010078 write_lock_bh(&net->ipv6.fib6_walker_lock);
79 list_add(&w->lh, &net->ipv6.fib6_walkers);
80 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070081}
82
Michal Kubeček9a03cd82016-03-08 14:44:35 +010083static void fib6_walker_unlink(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);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000086 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010087 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070088}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020089
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020090static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020092 int new, old;
93
94 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020095 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020096 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
98 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020099 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200102enum {
103 FIB6_NO_SERNUM_CHANGE = 0,
104};
105
David Ahern93c2fb22018-04-18 15:38:59 -0700106void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
Wei Wang180ca442017-10-06 12:05:56 -0700107{
Wei Wang180ca442017-10-06 12:05:56 -0700108 struct fib6_node *fn;
109
David Ahern93c2fb22018-04-18 15:38:59 -0700110 fn = rcu_dereference_protected(f6i->fib6_node,
111 lockdep_is_held(&f6i->fib6_table->tb6_lock));
Wei Wang180ca442017-10-06 12:05:56 -0700112 if (fn)
113 fn->fn_sernum = fib6_new_sernum(net);
Wei Wang180ca442017-10-06 12:05:56 -0700114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * Auxiliary address test functions for the radix tree.
118 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900119 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * 64bit processors)
121 */
122
123/*
124 * test bit
125 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000126#if defined(__LITTLE_ENDIAN)
127# define BITOP_BE32_SWIZZLE (0x1F & ~7)
128#else
129# define BITOP_BE32_SWIZZLE 0
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200132static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000134 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000135 /*
136 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800137 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000138 * is optimized version of
139 * htonl(1 << ((~fn_bit)&0x1F))
140 * See include/asm-generic/bitops/le.h.
141 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700142 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
143 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
David Ahern8d1c8022018-04-17 17:33:26 -0700146struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
David Aherna64efe142018-04-17 17:33:24 -0700147{
David Ahern8d1c8022018-04-17 17:33:26 -0700148 struct fib6_info *f6i;
David Aherna64efe142018-04-17 17:33:24 -0700149
150 f6i = kzalloc(sizeof(*f6i), gfp_flags);
151 if (!f6i)
152 return NULL;
153
154 f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
155 if (!f6i->rt6i_pcpu) {
156 kfree(f6i);
157 return NULL;
158 }
159
David Ahern93c2fb22018-04-18 15:38:59 -0700160 INIT_LIST_HEAD(&f6i->fib6_siblings);
Eric Dumazetf05713e2019-04-22 18:35:03 -0700161 refcount_set(&f6i->fib6_ref, 1);
David Aherna64efe142018-04-17 17:33:24 -0700162
163 return f6i;
164}
165
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700166void fib6_info_destroy_rcu(struct rcu_head *head)
David Aherna64efe142018-04-17 17:33:24 -0700167{
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700168 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
David Aherna64efe142018-04-17 17:33:24 -0700169 struct rt6_exception_bucket *bucket;
170
David Ahern93c2fb22018-04-18 15:38:59 -0700171 WARN_ON(f6i->fib6_node);
David Aherna64efe142018-04-17 17:33:24 -0700172
173 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
Eric Dumazetb0270552019-04-22 18:35:01 -0700174 kfree(bucket);
David Aherna64efe142018-04-17 17:33:24 -0700175
176 if (f6i->rt6i_pcpu) {
177 int cpu;
178
179 for_each_possible_cpu(cpu) {
180 struct rt6_info **ppcpu_rt;
181 struct rt6_info *pcpu_rt;
182
183 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
184 pcpu_rt = *ppcpu_rt;
185 if (pcpu_rt) {
186 dst_dev_put(&pcpu_rt->dst);
187 dst_release(&pcpu_rt->dst);
188 *ppcpu_rt = NULL;
189 }
190 }
Mike Rapoport7abab7b2018-10-09 07:02:01 +0300191
192 free_percpu(f6i->rt6i_pcpu);
David Aherna64efe142018-04-17 17:33:24 -0700193 }
194
David Aherndac7d0f2019-03-27 20:53:51 -0700195 fib6_nh_release(&f6i->fib6_nh);
David Aherna64efe142018-04-17 17:33:24 -0700196
David Aherncc5f0eb2018-10-04 20:07:52 -0700197 ip_fib_metrics_put(f6i->fib6_metrics);
David Ahern93531c62018-04-17 17:33:25 -0700198
David Aherna64efe142018-04-17 17:33:24 -0700199 kfree(f6i);
200}
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700201EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
David Aherna64efe142018-04-17 17:33:24 -0700202
Wei Wang81eb8442017-10-06 12:06:11 -0700203static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct fib6_node *fn;
206
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800207 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700208 if (fn)
209 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return fn;
212}
213
Wei Wang81eb8442017-10-06 12:06:11 -0700214static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700217 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Wei Wangc5cff852017-08-21 09:47:10 -0700220static void node_free_rcu(struct rcu_head *head)
221{
222 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
223
224 kmem_cache_free(fib6_node_kmem, fn);
225}
226
Wei Wang81eb8442017-10-06 12:06:11 -0700227static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700228{
229 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700230 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700231}
232
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200233static void fib6_free_table(struct fib6_table *table)
234{
235 inetpeer_invalidate_tree(&table->tb6_peers);
236 kfree(table);
237}
238
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800239static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700240{
241 unsigned int h;
242
Thomas Graf375216a2006-10-21 20:20:54 -0700243 /*
244 * Initialize table lock at a single place to give lockdep a key,
245 * tables aren't visible prior to being linked to the list.
246 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700247 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700248 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700249
250 /*
251 * No protection necessary, this is the only list mutatation
252 * operation, tables never disappear once they exist.
253 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800254 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700255}
256
257#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800258
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800259static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700260{
261 struct fib6_table *table;
262
263 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500264 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700265 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700266 rcu_assign_pointer(table->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -0700267 net->ipv6.fib6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700268 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700269 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700270 }
271
272 return table;
273}
274
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800275struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700276{
277 struct fib6_table *tb;
278
279 if (id == 0)
280 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800281 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700282 if (tb)
283 return tb;
284
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800285 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500286 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800287 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700288
289 return tb;
290}
David Ahernb3b46632016-05-04 21:46:12 -0700291EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700292
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800293struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700294{
295 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800296 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700297 unsigned int h;
298
299 if (id == 0)
300 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700301 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700302 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800303 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800304 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700305 if (tb->tb6_id == id) {
306 rcu_read_unlock();
307 return tb;
308 }
309 }
310 rcu_read_unlock();
311
312 return NULL;
313}
David Ahernc4850682015-10-12 11:47:08 -0700314EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700315
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000316static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700317{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800318 fib6_link_table(net, net->ipv6.fib6_main_tbl);
319 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700320}
Thomas Grafc71099a2006-08-04 23:20:06 -0700321#else
322
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800323struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700324{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800325 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700326}
327
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800328struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700329{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800330 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700331}
332
David S. Miller4c9483b2011-03-12 16:22:43 -0500333struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -0800334 const struct sk_buff *skb,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800335 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700336{
lucienab997ad2015-10-23 15:36:53 +0800337 struct rt6_info *rt;
338
David Ahernb75cc8f2018-03-02 08:32:17 -0800339 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300340 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800341 ip6_rt_put(rt);
342 rt = net->ipv6.ip6_null_entry;
343 dst_hold(&rt->dst);
344 }
345
346 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700347}
348
David Ahern138118e2018-05-09 20:34:23 -0700349/* called with rcu lock held; no reference taken on fib6_info */
David Aherneffda4d2019-04-16 14:36:10 -0700350int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
351 struct fib6_result *res, int flags)
David Ahern138118e2018-05-09 20:34:23 -0700352{
David Aherneffda4d2019-04-16 14:36:10 -0700353 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
354 res, flags);
David Ahern138118e2018-05-09 20:34:23 -0700355}
356
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000357static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700358{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800359 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700360}
361
362#endif
363
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200364unsigned int fib6_tables_seq_read(struct net *net)
365{
366 unsigned int h, fib_seq = 0;
367
368 rcu_read_lock();
369 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
370 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
371 struct fib6_table *tb;
372
Wei Wang66f5d6c2017-10-06 12:06:10 -0700373 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200374 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200375 }
376 rcu_read_unlock();
377
378 return fib_seq;
379}
380
381static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
382 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700383 struct fib6_info *rt)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200384{
385 struct fib6_entry_notifier_info info = {
386 .rt = rt,
387 };
388
389 return call_fib6_notifier(nb, net, event_type, &info.info);
390}
391
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200392static int call_fib6_entry_notifiers(struct net *net,
393 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700394 struct fib6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700395 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200396{
397 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700398 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200399 .rt = rt,
400 };
401
David Ahern93c2fb22018-04-18 15:38:59 -0700402 rt->fib6_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200403 return call_fib6_notifiers(net, event_type, &info.info);
404}
405
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200406struct fib6_dump_arg {
407 struct net *net;
408 struct notifier_block *nb;
409};
410
David Ahern8d1c8022018-04-17 17:33:26 -0700411static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200412{
David Ahern421842e2018-04-17 17:33:18 -0700413 if (rt == arg->net->ipv6.fib6_null_entry)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200414 return;
415 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
416}
417
418static int fib6_node_dump(struct fib6_walker *w)
419{
David Ahern8d1c8022018-04-17 17:33:26 -0700420 struct fib6_info *rt;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200421
Wei Wang66f5d6c2017-10-06 12:06:10 -0700422 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200423 fib6_rt_dump(rt, w->args);
424 w->leaf = NULL;
425 return 0;
426}
427
428static void fib6_table_dump(struct net *net, struct fib6_table *tb,
429 struct fib6_walker *w)
430{
431 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700432 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200433 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700434 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200435}
436
437/* Called with rcu_read_lock() */
438int fib6_tables_dump(struct net *net, struct notifier_block *nb)
439{
440 struct fib6_dump_arg arg;
441 struct fib6_walker *w;
442 unsigned int h;
443
444 w = kzalloc(sizeof(*w), GFP_ATOMIC);
445 if (!w)
446 return -ENOMEM;
447
448 w->func = fib6_node_dump;
449 arg.net = net;
450 arg.nb = nb;
451 w->args = &arg;
452
453 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
454 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
455 struct fib6_table *tb;
456
457 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
458 fib6_table_dump(net, tb, w);
459 }
460
461 kfree(w);
462
463 return 0;
464}
465
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200466static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700467{
468 int res;
David Ahern8d1c8022018-04-17 17:33:26 -0700469 struct fib6_info *rt;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700470
Wei Wang66f5d6c2017-10-06 12:06:10 -0700471 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700472 res = rt6_dump_route(rt, w->args);
473 if (res < 0) {
474 /* Frame is full, suspend walking */
475 w->leaf = rt;
476 return 1;
477 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800478
479 /* Multipath routes are dumped in one route with the
480 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
481 * last sibling of this route (no need to dump the
482 * sibling routes again)
483 */
David Ahern93c2fb22018-04-18 15:38:59 -0700484 if (rt->fib6_nsiblings)
485 rt = list_last_entry(&rt->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -0700486 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -0700487 fib6_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700488 }
489 w->leaf = NULL;
490 return 0;
491}
492
493static void fib6_dump_end(struct netlink_callback *cb)
494{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100495 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200496 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700497
498 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800499 if (cb->args[4]) {
500 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100501 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800502 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700503 cb->args[2] = 0;
504 kfree(w);
505 }
Wang Yufen437de072014-03-28 12:07:04 +0800506 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507 cb->args[1] = 3;
508}
509
510static int fib6_dump_done(struct netlink_callback *cb)
511{
512 fib6_dump_end(cb);
513 return cb->done ? cb->done(cb) : 0;
514}
515
516static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
517 struct netlink_callback *cb)
518{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100519 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200520 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700521 int res;
522
523 w = (void *)cb->args[2];
524 w->root = &table->tb6_root;
525
526 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000527 w->count = 0;
528 w->skip = 0;
529
Wei Wang66f5d6c2017-10-06 12:06:10 -0700530 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100531 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700532 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000533 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700534 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000535 cb->args[5] = w->root->fn_sernum;
536 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700537 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000538 if (cb->args[5] != w->root->fn_sernum) {
539 /* Begin at the root if the tree changed */
540 cb->args[5] = w->root->fn_sernum;
541 w->state = FWS_INIT;
542 w->node = w->root;
543 w->skip = w->count;
544 } else
545 w->skip = 0;
546
Wei Wang66f5d6c2017-10-06 12:06:10 -0700547 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700548 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700549 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800550 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100551 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800552 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700553 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700554 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800555
Patrick McHardy1b43af52006-08-10 23:11:17 -0700556 return res;
557}
558
Thomas Grafc127ea22007-03-22 11:58:32 -0700559static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700560{
David Aherne8ba3302018-10-07 20:16:35 -0700561 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900562 struct net *net = sock_net(skb->sk);
David Ahern47246762018-10-15 18:56:42 -0700563 struct rt6_rtnl_dump_arg arg = {};
Patrick McHardy1b43af52006-08-10 23:11:17 -0700564 unsigned int h, s_h;
565 unsigned int e = 0, s_e;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200566 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700567 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800568 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700569 int res = 0;
570
David Aherne8ba3302018-10-07 20:16:35 -0700571 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -0700572 int err;
David Aherne8ba3302018-10-07 20:16:35 -0700573
David Aherneffe6792018-10-15 18:56:48 -0700574 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -0700575 if (err < 0)
576 return err;
David Ahern13e38902018-10-15 18:56:44 -0700577 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
578 struct rtmsg *rtm = nlmsg_data(nlh);
David Aherne8ba3302018-10-07 20:16:35 -0700579
David Ahern08e814c2018-10-15 18:56:50 -0700580 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
David Ahern13e38902018-10-15 18:56:44 -0700581 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700582
David Ahern08e814c2018-10-15 18:56:50 -0700583 /* fib entries are never clones */
584 if (arg.filter.flags & RTM_F_CLONED)
Alexey Kodaneve22d0bf2018-11-02 19:11:05 +0300585 goto out;
David Ahern08e814c2018-10-15 18:56:50 -0700586
Patrick McHardy1b43af52006-08-10 23:11:17 -0700587 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500588 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700589 /* New dump:
590 *
591 * 1. hook callback destructor.
592 */
593 cb->args[3] = (long)cb->done;
594 cb->done = fib6_dump_done;
595
596 /*
597 * 2. allocate and initialize walker.
598 */
599 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500600 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700601 return -ENOMEM;
602 w->func = fib6_dump_node;
603 cb->args[2] = (long)w;
604 }
605
606 arg.skb = skb;
607 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700608 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700609 w->args = &arg;
610
David Ahern13e38902018-10-15 18:56:44 -0700611 if (arg.filter.table_id) {
612 tb = fib6_get_table(net, arg.filter.table_id);
613 if (!tb) {
David Ahernae677bb2018-10-24 12:59:01 -0700614 if (arg.filter.dump_all_families)
Alexey Kodaneve22d0bf2018-11-02 19:11:05 +0300615 goto out;
David Ahernae677bb2018-10-24 12:59:01 -0700616
David Ahern13e38902018-10-15 18:56:44 -0700617 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
618 return -ENOENT;
619 }
620
David Ahern73155872019-01-02 18:26:13 -0800621 if (!cb->args[0]) {
622 res = fib6_dump_table(tb, skb, cb);
623 if (!res)
624 cb->args[0] = 1;
625 }
David Ahern13e38902018-10-15 18:56:44 -0700626 goto out;
627 }
628
629 s_h = cb->args[0];
630 s_e = cb->args[1];
631
Eric Dumazete67f88d2011-04-27 22:56:07 +0000632 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700633 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700634 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800635 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800636 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700637 if (e < s_e)
638 goto next;
639 res = fib6_dump_table(tb, skb, cb);
640 if (res != 0)
David Ahern13e38902018-10-15 18:56:44 -0700641 goto out_unlock;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700642next:
643 e++;
644 }
645 }
David Ahern13e38902018-10-15 18:56:44 -0700646out_unlock:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000647 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700648 cb->args[1] = e;
649 cb->args[0] = h;
David Ahern13e38902018-10-15 18:56:44 -0700650out:
Patrick McHardy1b43af52006-08-10 23:11:17 -0700651 res = res < 0 ? res : skb->len;
652 if (res <= 0)
653 fib6_dump_end(cb);
654 return res;
655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
David Ahern8d1c8022018-04-17 17:33:26 -0700657void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
David Ahernd4ead6b2018-04-17 17:33:16 -0700658{
659 if (!f6i)
660 return;
661
662 if (f6i->fib6_metrics == &dst_default_metrics) {
663 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
664
665 if (!p)
666 return;
667
668 refcount_set(&p->refcnt, 1);
669 f6i->fib6_metrics = p;
670 }
671
672 f6i->fib6_metrics->metrics[metric - 1] = val;
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/*
676 * Routing Table
677 *
678 * return the appropriate node for a routing tree "add" operation
679 * by either creating and inserting or by returning an existing
680 * node.
681 */
682
Wei Wang81eb8442017-10-06 12:06:11 -0700683static struct fib6_node *fib6_add_1(struct net *net,
684 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700685 struct fib6_node *root,
686 struct in6_addr *addr, int plen,
687 int offset, int allow_create,
688 int replace_required,
689 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct fib6_node *fn, *in, *ln;
692 struct fib6_node *pn = NULL;
693 struct rt6key *key;
694 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900695 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 RT6_TRACE("fib6_add_1\n");
698
699 /* insert node in tree */
700
701 fn = root;
702
703 do {
David Ahern8d1c8022018-04-17 17:33:26 -0700704 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700705 lockdep_is_held(&table->tb6_lock));
706 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /*
709 * Prefix match
710 */
711 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000712 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000713 if (!allow_create) {
714 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600715 NL_SET_ERR_MSG(extack,
716 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000717 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000718 return ERR_PTR(-ENOENT);
719 }
Joe Perchesf3213832012-05-15 14:11:53 +0000720 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000723 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * Exact match ?
727 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (plen == fn->fn_bit) {
730 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500731 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700732 RCU_INIT_POINTER(fn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700733 fib6_info_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800734 /* remove null_entry in the root node */
735 } else if (fn->fn_flags & RTN_TL_ROOT &&
736 rcu_access_pointer(fn->leaf) ==
David Ahern421842e2018-04-17 17:33:18 -0700737 net->ipv6.fib6_null_entry) {
Wei Wang4512c432018-01-08 10:34:00 -0800738 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return fn;
742 }
743
744 /*
745 * We have more bits to go
746 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 dir = addr_bit_set(addr, fn->fn_bit);
750 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700751 fn = dir ?
752 rcu_dereference_protected(fn->right,
753 lockdep_is_held(&table->tb6_lock)) :
754 rcu_dereference_protected(fn->left,
755 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } while (fn);
757
Matti Vaittinen14df0152011-11-16 21:18:02 +0000758 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000759 /* We should not create new node because
760 * NLM_F_REPLACE was specified without NLM_F_CREATE
761 * I assume it is safe to require NLM_F_CREATE when
762 * REPLACE flag is used! Later we may want to remove the
763 * check for replace_required, because according
764 * to netlink specification, NLM_F_CREATE
765 * MUST be specified if new route is created.
766 * That would keep IPv6 consistent with IPv4
767 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000768 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600769 NL_SET_ERR_MSG(extack,
770 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000771 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000772 return ERR_PTR(-ENOENT);
773 }
Joe Perchesf3213832012-05-15 14:11:53 +0000774 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * We walked to the bottom of tree.
778 * Create new leaf node without children.
779 */
780
Wei Wang81eb8442017-10-06 12:06:11 -0700781 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
David S. Miller507c9b12011-12-03 17:50:45 -0500783 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000784 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700786 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700789 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700791 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 return ln;
794
795
796insert_above:
797 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900798 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * we have a less significant route.
800 * we've to insert an intermediate node on the list
801 * this new node will point to the one we need to create
802 * and the current
803 */
804
Wei Wang66f5d6c2017-10-06 12:06:10 -0700805 pn = rcu_dereference_protected(fn->parent,
806 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* find 1st bit in difference between the 2 addrs.
809
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800810 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 but if it is >= plen, the value is ignored in any case.
812 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900813
fan.du9225b232013-07-22 14:21:09 +0800814 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900816 /*
817 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * / \
819 * (new leaf node)[ln] (old node)[fn]
820 */
821 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700822 in = node_alloc(net);
823 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900824
David S. Miller507c9b12011-12-03 17:50:45 -0500825 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700827 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700829 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000830 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900833 /*
834 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * RTN_RTINFO will
836 * be off since that an address that chooses one of
837 * the branches would not match less specific routes
838 * in the other branch
839 */
840
841 in->fn_bit = bit;
842
Wei Wang66f5d6c2017-10-06 12:06:10 -0700843 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 in->leaf = fn->leaf;
Eric Dumazet5ea71522019-04-22 18:35:02 -0700845 fib6_info_hold(rcu_dereference_protected(in->leaf,
846 lockdep_is_held(&table->tb6_lock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* update parent pointer */
849 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700850 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700852 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 ln->fn_bit = plen;
855
Wei Wang66f5d6c2017-10-06 12:06:10 -0700856 RCU_INIT_POINTER(ln->parent, in);
857 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700860 rcu_assign_pointer(in->right, ln);
861 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700863 rcu_assign_pointer(in->left, ln);
864 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 } else { /* plen <= bit */
867
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900868 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * (new leaf node)[ln]
870 * / \
871 * (old node)[fn] NULL
872 */
873
Wei Wang81eb8442017-10-06 12:06:11 -0700874 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
David S. Miller507c9b12011-12-03 17:50:45 -0500876 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000877 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 ln->fn_bit = plen;
880
Wei Wang66f5d6c2017-10-06 12:06:10 -0700881 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700884 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700886 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Wei Wang66f5d6c2017-10-06 12:06:10 -0700888 rcu_assign_pointer(fn->parent, ln);
889
890 if (dir)
891 rcu_assign_pointer(pn->right, ln);
892 else
893 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 return ln;
896}
897
David Ahern5bcaa412018-04-20 15:38:01 -0700898static void fib6_drop_pcpu_from(struct fib6_info *f6i,
899 const struct fib6_table *table)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100900{
David Ahern5bcaa412018-04-20 15:38:01 -0700901 int cpu;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100902
Eric Dumazet61fb0d02019-05-15 19:39:52 -0700903 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
904 * while we are cleaning them here.
905 */
906 f6i->fib6_destroying = 1;
907 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
908
David Ahern5bcaa412018-04-20 15:38:01 -0700909 /* release the reference to this fib entry from
910 * all of its cached pcpu routes
911 */
912 for_each_possible_cpu(cpu) {
913 struct rt6_info **ppcpu_rt;
914 struct rt6_info *pcpu_rt;
915
916 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
917 pcpu_rt = *ppcpu_rt;
918 if (pcpu_rt) {
David Aherna68886a2018-04-20 15:38:02 -0700919 struct fib6_info *from;
920
Eric Dumazet0e233872019-04-28 12:22:25 -0700921 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
David Aherna68886a2018-04-20 15:38:02 -0700922 fib6_info_release(from);
David Ahern5bcaa412018-04-20 15:38:01 -0700923 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100924 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100925}
926
David Ahern8d1c8022018-04-17 17:33:26 -0700927static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100928 struct net *net)
929{
David Ahern93c2fb22018-04-18 15:38:59 -0700930 struct fib6_table *table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700931
Eric Dumazet61fb0d02019-05-15 19:39:52 -0700932 if (rt->rt6i_pcpu)
933 fib6_drop_pcpu_from(rt, table);
934
Eric Dumazetf05713e2019-04-22 18:35:03 -0700935 if (refcount_read(&rt->fib6_ref) != 1) {
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100936 /* This route is used as dummy address holder in some split
937 * nodes. It is not leaked, but it still holds other resources,
938 * which must be released in time. So, scan ascendant nodes
939 * and replace dummy references to this route with references
940 * to still alive ones.
941 */
942 while (fn) {
David Ahern8d1c8022018-04-17 17:33:26 -0700943 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700944 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700945 struct fib6_info *new_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700946 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
947 new_leaf = fib6_find_prefix(net, table, fn);
Eric Dumazet5ea71522019-04-22 18:35:02 -0700948 fib6_info_hold(new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700949
Wei Wang66f5d6c2017-10-06 12:06:10 -0700950 rcu_assign_pointer(fn->leaf, new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700951 fib6_info_release(rt);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100952 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700953 fn = rcu_dereference_protected(fn->parent,
954 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100955 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100956 }
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*
960 * Insert routing information in a node.
961 */
962
David Ahern8d1c8022018-04-17 17:33:26 -0700963static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700964 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700965 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
David Ahern8d1c8022018-04-17 17:33:26 -0700967 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700968 lockdep_is_held(&rt->fib6_table->tb6_lock));
David Ahern33bd5ac2018-07-03 14:36:21 -0700969 struct fib6_info *iter = NULL;
David Ahern8d1c8022018-04-17 17:33:26 -0700970 struct fib6_info __rcu **ins;
David Ahern33bd5ac2018-07-03 14:36:21 -0700971 struct fib6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500972 int replace = (info->nlh &&
973 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
974 int add = (!info->nlh ||
975 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000976 int found = 0;
David Ahern33bd5ac2018-07-03 14:36:21 -0700977 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200978 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100979 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
David Ahern33bd5ac2018-07-03 14:36:21 -0700981 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
David Ahern1f5e29c2017-01-31 16:51:37 -0800982 nlflags |= NLM_F_APPEND;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ins = &fn->leaf;
985
Wei Wang66f5d6c2017-10-06 12:06:10 -0700986 for (iter = leaf; iter;
David Ahern8fb11a92018-05-04 13:54:24 -0700987 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -0700988 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /*
990 * Search for duplicates
991 */
992
David Ahern93c2fb22018-04-18 15:38:59 -0700993 if (iter->fib6_metric == rt->fib6_metric) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * Same priority level
996 */
David S. Miller507c9b12011-12-03 17:50:45 -0500997 if (info->nlh &&
998 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000999 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +02001000
1001 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001002 if (replace) {
David Ahern33bd5ac2018-07-03 14:36:21 -07001003 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1004 found++;
1005 break;
1006 }
1007 if (rt_can_ecmp)
1008 fallback_ins = fallback_ins ?: ins;
1009 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David Ahernf06b7542017-07-05 14:41:46 -06001012 if (rt6_duplicate_nexthop(iter, rt)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001013 if (rt->fib6_nsiblings)
1014 rt->fib6_nsiblings = 0;
1015 if (!(iter->fib6_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EEXIST;
David Ahern93c2fb22018-04-18 15:38:59 -07001017 if (!(rt->fib6_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -07001018 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +00001019 else
David Ahern14895682018-04-17 17:33:17 -07001020 fib6_set_expires(iter, rt->expires);
David Ahern15a81b42018-08-30 14:15:43 -07001021
1022 if (rt->fib6_pmtu)
1023 fib6_metric_set(iter, RTAX_MTU,
1024 rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -EEXIST;
1026 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001027 /* If we have the same destination and the same metric,
1028 * but not the same gateway, then the route we try to
1029 * add is sibling to this route, increment our counter
1030 * of siblings, and later we will add our route to the
1031 * list.
1032 * Only static routes (which don't have flag
1033 * RTF_EXPIRES) are used for ECMPv6.
1034 *
1035 * To avoid long list, we only had siblings if the
1036 * route have a gateway.
1037 */
1038 if (rt_can_ecmp &&
1039 rt6_qualify_for_ecmp(iter))
1040 rt->fib6_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
David Ahern93c2fb22018-04-18 15:38:59 -07001043 if (iter->fib6_metric > rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
1045
David Ahern33bd5ac2018-07-03 14:36:21 -07001046next_iter:
David Ahern8fb11a92018-05-04 13:54:24 -07001047 ins = &iter->fib6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001048 }
1049
David Ahern33bd5ac2018-07-03 14:36:21 -07001050 if (fallback_ins && !found) {
1051 /* No ECMP-able route found, replace first non-ECMP one */
1052 ins = fallback_ins;
1053 iter = rcu_dereference_protected(*ins,
1054 lockdep_is_held(&rt->fib6_table->tb6_lock));
1055 found++;
1056 }
1057
David S. Millerf11e6652007-03-24 20:36:25 -07001058 /* Reset round-robin state, if necessary */
1059 if (ins == &fn->leaf)
1060 fn->rr_ptr = NULL;
1061
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001062 /* Link this route to others same route. */
David Ahern33bd5ac2018-07-03 14:36:21 -07001063 if (rt->fib6_nsiblings) {
1064 unsigned int fib6_nsiblings;
David Ahern8d1c8022018-04-17 17:33:26 -07001065 struct fib6_info *sibling, *temp_sibling;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001066
David Ahern33bd5ac2018-07-03 14:36:21 -07001067 /* Find the first route that have the same metric */
1068 sibling = leaf;
1069 while (sibling) {
1070 if (sibling->fib6_metric == rt->fib6_metric &&
1071 rt6_qualify_for_ecmp(sibling)) {
1072 list_add_tail(&rt->fib6_siblings,
1073 &sibling->fib6_siblings);
1074 break;
1075 }
1076 sibling = rcu_dereference_protected(sibling->fib6_next,
1077 lockdep_is_held(&rt->fib6_table->tb6_lock));
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001078 }
1079 /* For each sibling in the list, increment the counter of
1080 * siblings. BUG() if counters does not match, list of siblings
1081 * is broken!
1082 */
David Ahern33bd5ac2018-07-03 14:36:21 -07001083 fib6_nsiblings = 0;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001084 list_for_each_entry_safe(sibling, temp_sibling,
David Ahern33bd5ac2018-07-03 14:36:21 -07001085 &rt->fib6_siblings, fib6_siblings) {
David Ahern93c2fb22018-04-18 15:38:59 -07001086 sibling->fib6_nsiblings++;
David Ahern33bd5ac2018-07-03 14:36:21 -07001087 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1088 fib6_nsiblings++;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001089 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001090 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1091 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /*
1095 * insert node
1096 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001097 if (!replace) {
1098 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001099 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001101add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001102 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001103
David Ahern33bd5ac2018-07-03 14:36:21 -07001104 err = call_fib6_entry_notifiers(info->nl_net,
1105 FIB_EVENT_ENTRY_ADD,
1106 rt, extack);
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001107 if (err)
1108 return err;
1109
David Ahern8fb11a92018-05-04 13:54:24 -07001110 rcu_assign_pointer(rt->fib6_next, iter);
Eric Dumazet5ea71522019-04-22 18:35:02 -07001111 fib6_info_hold(rt);
David Ahern93c2fb22018-04-18 15:38:59 -07001112 rcu_assign_pointer(rt->fib6_node, fn);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001113 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001114 if (!info->skip_notify)
1115 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001116 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
David S. Miller507c9b12011-12-03 17:50:45 -05001118 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001119 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1120 fn->fn_flags |= RTN_RTINFO;
1121 }
1122
1123 } else {
David Ahern33bd5ac2018-07-03 14:36:21 -07001124 int nsiblings;
Michal Kubeček27596472015-05-18 20:54:00 +02001125
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001126 if (!found) {
1127 if (add)
1128 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001129 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001130 return -ENOENT;
1131 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001132
David Ahern22330002018-03-27 18:21:59 -07001133 err = call_fib6_entry_notifiers(info->nl_net,
1134 FIB_EVENT_ENTRY_REPLACE,
1135 rt, extack);
1136 if (err)
1137 return err;
1138
Eric Dumazet5ea71522019-04-22 18:35:02 -07001139 fib6_info_hold(rt);
David Ahern93c2fb22018-04-18 15:38:59 -07001140 rcu_assign_pointer(rt->fib6_node, fn);
David Ahern33bd5ac2018-07-03 14:36:21 -07001141 rt->fib6_next = iter->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001142 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001143 if (!info->skip_notify)
1144 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001145 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001146 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1147 fn->fn_flags |= RTN_RTINFO;
1148 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001149 nsiblings = iter->fib6_nsiblings;
1150 iter->fib6_node = NULL;
1151 fib6_purge_rt(iter, fn, info->nl_net);
1152 if (rcu_access_pointer(fn->rr_ptr) == iter)
David Ahernf34436a2018-05-21 10:26:53 -07001153 fn->rr_ptr = NULL;
David Ahern33bd5ac2018-07-03 14:36:21 -07001154 fib6_info_release(iter);
1155
1156 if (nsiblings) {
1157 /* Replacing an ECMP route, remove all siblings */
1158 ins = &rt->fib6_next;
1159 iter = rcu_dereference_protected(*ins,
1160 lockdep_is_held(&rt->fib6_table->tb6_lock));
1161 while (iter) {
1162 if (iter->fib6_metric > rt->fib6_metric)
1163 break;
1164 if (rt6_qualify_for_ecmp(iter)) {
1165 *ins = iter->fib6_next;
1166 iter->fib6_node = NULL;
1167 fib6_purge_rt(iter, fn, info->nl_net);
1168 if (rcu_access_pointer(fn->rr_ptr) == iter)
1169 fn->rr_ptr = NULL;
1170 fib6_info_release(iter);
1171 nsiblings--;
1172 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1173 } else {
1174 ins = &iter->fib6_next;
1175 }
1176 iter = rcu_dereference_protected(*ins,
1177 lockdep_is_held(&rt->fib6_table->tb6_lock));
1178 }
1179 WARN_ON(nsiblings != 0);
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 return 0;
1184}
1185
David Ahern8d1c8022018-04-17 17:33:26 -07001186static void fib6_start_gc(struct net *net, struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001188 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001189 (rt->fib6_flags & RTF_EXPIRES))
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001190 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001191 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001194void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07001196 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1197 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001198 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
David Ahern8d1c8022018-04-17 17:33:26 -07001201static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001202 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001203{
David Ahern93c2fb22018-04-18 15:38:59 -07001204 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1205 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001206
1207 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1208 smp_wmb();
1209 while (fn) {
1210 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001211 fn = rcu_dereference_protected(fn->parent,
David Ahern93c2fb22018-04-18 15:38:59 -07001212 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001213 }
1214}
1215
David Ahern8d1c8022018-04-17 17:33:26 -07001216void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001217{
1218 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/*
1222 * Add routing information to the routing tree.
1223 * <destination addr>/<source addr>
1224 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001225 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
1227
David Ahern8d1c8022018-04-17 17:33:26 -07001228int fib6_add(struct fib6_node *root, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001229 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
David Ahern93c2fb22018-04-18 15:38:59 -07001231 struct fib6_table *table = rt->fib6_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001232 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001234 int allow_create = 1;
1235 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001236 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001237
1238 if (info->nlh) {
1239 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001240 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001241 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001242 replace_required = 1;
1243 }
1244 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001245 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Wei Wang81eb8442017-10-06 12:06:11 -07001247 fn = fib6_add_1(info->nl_net, table, root,
David Ahern93c2fb22018-04-18 15:38:59 -07001248 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1249 offsetof(struct fib6_info, fib6_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001250 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001251 if (IS_ERR(fn)) {
1252 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001253 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001257 pn = fn;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07001260 if (rt->fib6_src.plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct fib6_node *sn;
1262
Wei Wang66f5d6c2017-10-06 12:06:10 -07001263 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct fib6_node *sfn;
1265
1266 /*
1267 * Create subtree.
1268 *
1269 * fn[main tree]
1270 * |
1271 * sfn[subtree root]
1272 * \
1273 * sn[new leaf node]
1274 */
1275
1276 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001277 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001278 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001279 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Eric Dumazet5ea71522019-04-22 18:35:02 -07001281 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001282 rcu_assign_pointer(sfn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001283 info->nl_net->ipv6.fib6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /* Now add the first leaf node to new subtree */
1287
Wei Wang81eb8442017-10-06 12:06:11 -07001288 sn = fib6_add_1(info->nl_net, table, sfn,
David Ahern93c2fb22018-04-18 15:38:59 -07001289 &rt->fib6_src.addr, rt->fib6_src.plen,
1290 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001291 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001293 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001295 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 in main tree.
1297 */
Wei Wang81eb8442017-10-06 12:06:11 -07001298 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001299 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001300 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001304 rcu_assign_pointer(sfn->parent, fn);
1305 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001307 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
David Ahern93c2fb22018-04-18 15:38:59 -07001308 &rt->fib6_src.addr, rt->fib6_src.plen,
1309 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001310 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001312 if (IS_ERR(sn)) {
1313 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001314 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Wei Wang66f5d6c2017-10-06 12:06:10 -07001318 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001319 if (fn->fn_flags & RTN_TL_ROOT) {
1320 /* put back null_entry for root node */
1321 rcu_assign_pointer(fn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001322 info->nl_net->ipv6.fib6_null_entry);
Wei Wang591ff9e2018-01-18 10:40:03 -08001323 } else {
Eric Dumazet5ea71522019-04-22 18:35:02 -07001324 fib6_info_hold(rt);
Wei Wang591ff9e2018-01-18 10:40:03 -08001325 rcu_assign_pointer(fn->leaf, rt);
1326 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 fn = sn;
1329 }
1330#endif
1331
David Ahernd4ead6b2018-04-17 17:33:16 -07001332 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001333 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001334 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001335 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001339 if (err) {
1340#ifdef CONFIG_IPV6_SUBTREES
1341 /*
1342 * If fib6_add_1 has cleared the old leaf pointer in the
1343 * super-tree leaf node we have to find a new one for it.
1344 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001345 if (pn != fn) {
David Ahern8d1c8022018-04-17 17:33:26 -07001346 struct fib6_info *pn_leaf =
Wei Wang7bbfe002018-01-03 14:11:59 -08001347 rcu_dereference_protected(pn->leaf,
1348 lockdep_is_held(&table->tb6_lock));
1349 if (pn_leaf == rt) {
1350 pn_leaf = NULL;
1351 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001352 fib6_info_release(rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001353 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001354 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1355 pn_leaf = fib6_find_prefix(info->nl_net, table,
1356 pn);
1357#if RT6_DEBUG >= 2
1358 if (!pn_leaf) {
1359 WARN_ON(!pn_leaf);
1360 pn_leaf =
David Ahern421842e2018-04-17 17:33:18 -07001361 info->nl_net->ipv6.fib6_null_entry;
Wei Wang7bbfe002018-01-03 14:11:59 -08001362 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001363#endif
David Ahern93531c62018-04-17 17:33:25 -07001364 fib6_info_hold(pn_leaf);
Wei Wang7bbfe002018-01-03 14:11:59 -08001365 rcu_assign_pointer(pn->leaf, pn_leaf);
1366 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001367 }
1368#endif
Wei Wang348a4002017-08-18 17:14:49 -07001369 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return err;
1372
Wei Wang348a4002017-08-18 17:14:49 -07001373failure:
Wei Wang4512c432018-01-08 10:34:00 -08001374 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1375 * 1. fn is an intermediate node and we failed to add the new
1376 * route to it in both subtree creation failure and fib6_add_rt2node()
1377 * failure case.
1378 * 2. fn is the root node in the table and we fail to add the first
1379 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 */
Wei Wang4512c432018-01-08 10:34:00 -08001381 if (fn &&
1382 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1383 (fn->fn_flags & RTN_TL_ROOT &&
1384 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001385 fib6_repair_tree(info->nl_net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/*
1390 * Routing tree lookup
1391 *
1392 */
1393
1394struct lookup_args {
David Ahern8d1c8022018-04-17 17:33:26 -07001395 int offset; /* key offset on fib6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001396 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397};
1398
David Ahern64547432018-05-09 20:34:19 -07001399static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1400 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct fib6_node *fn;
Al Viroe69a4adc2006-11-14 20:56:00 -08001403 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001405 if (unlikely(args->offset == 0))
1406 return NULL;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /*
1409 * Descend on a tree
1410 */
1411
1412 fn = root;
1413
1414 for (;;) {
1415 struct fib6_node *next;
1416
1417 dir = addr_bit_set(args->addr, fn->fn_bit);
1418
Wei Wang66f5d6c2017-10-06 12:06:10 -07001419 next = dir ? rcu_dereference(fn->right) :
1420 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (next) {
1423 fn = next;
1424 continue;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 }
1428
David S. Miller507c9b12011-12-03 17:50:45 -05001429 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001430 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1431
1432 if (subtree || fn->fn_flags & RTN_RTINFO) {
David Ahern8d1c8022018-04-17 17:33:26 -07001433 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct rt6key *key;
1435
Wei Wang8d1040e2017-10-06 12:06:08 -07001436 if (!leaf)
1437 goto backtrack;
1438
1439 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001441 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1442#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001443 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001444 struct fib6_node *sfn;
David Ahern64547432018-05-09 20:34:19 -07001445 sfn = fib6_node_lookup_1(subtree,
1446 args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001447 if (!sfn)
1448 goto backtrack;
1449 fn = sfn;
1450 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001451#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001452 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001453 return fn;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001456backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001457 if (fn->fn_flags & RTN_ROOT)
1458 break;
1459
Wei Wang66f5d6c2017-10-06 12:06:10 -07001460 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
1463 return NULL;
1464}
1465
Wei Wang66f5d6c2017-10-06 12:06:10 -07001466/* called with rcu_read_lock() held
1467 */
David Ahern64547432018-05-09 20:34:19 -07001468struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1469 const struct in6_addr *daddr,
1470 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001473 struct lookup_args args[] = {
1474 {
David Ahern93c2fb22018-04-18 15:38:59 -07001475 .offset = offsetof(struct fib6_info, fib6_dst),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001476 .addr = daddr,
1477 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001479 {
David Ahern93c2fb22018-04-18 15:38:59 -07001480 .offset = offsetof(struct fib6_info, fib6_src),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001481 .addr = saddr,
1482 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001484 {
1485 .offset = 0, /* sentinel */
1486 }
1487 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
David Ahern64547432018-05-09 20:34:19 -07001489 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001490 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 fn = root;
1492
1493 return fn;
1494}
1495
1496/*
1497 * Get node with specified destination prefix (and source prefix,
1498 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001499 * exact_match == true means we try to find fn with exact match of
1500 * the passed in prefix addr
1501 * exact_match == false means we try to find fn with longest prefix
1502 * match of the passed in prefix addr. This is useful for finding fn
1503 * for cached route as it will be stored in the exception table under
1504 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 */
1506
1507
Wang Yufen437de072014-03-28 12:07:04 +08001508static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1509 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001510 int plen, int offset,
1511 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Wei Wang38fbeee2017-10-06 12:06:02 -07001513 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 for (fn = root; fn ; ) {
David Ahern8d1c8022018-04-17 17:33:26 -07001516 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001517 struct rt6key *key;
1518
1519 /* This node is being deleted */
1520 if (!leaf) {
1521 if (plen <= fn->fn_bit)
1522 goto out;
1523 else
1524 goto next;
1525 }
1526
1527 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * Prefix match
1531 */
1532 if (plen < fn->fn_bit ||
1533 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (plen == fn->fn_bit)
1537 return fn;
1538
Wei Wang38fbeee2017-10-06 12:06:02 -07001539 prev = fn;
1540
Wei Wang8d1040e2017-10-06 12:06:08 -07001541next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * We have more bits to go
1544 */
1545 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001546 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001548 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001550out:
1551 if (exact_match)
1552 return NULL;
1553 else
1554 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Wang Yufen437de072014-03-28 12:07:04 +08001557struct fib6_node *fib6_locate(struct fib6_node *root,
1558 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001559 const struct in6_addr *saddr, int src_len,
1560 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct fib6_node *fn;
1563
1564 fn = fib6_locate_1(root, daddr, dst_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001565 offsetof(struct fib6_info, fib6_dst),
Wei Wang38fbeee2017-10-06 12:06:02 -07001566 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568#ifdef CONFIG_IPV6_SUBTREES
1569 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001570 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001571 if (fn) {
1572 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1573
1574 if (subtree) {
1575 fn = fib6_locate_1(subtree, saddr, src_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001576 offsetof(struct fib6_info, fib6_src),
Wei Wang38fbeee2017-10-06 12:06:02 -07001577 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001578 }
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581#endif
1582
David S. Miller507c9b12011-12-03 17:50:45 -05001583 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return fn;
1585
1586 return NULL;
1587}
1588
1589
1590/*
1591 * Deletion
1592 *
1593 */
1594
David Ahern8d1c8022018-04-17 17:33:26 -07001595static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001596 struct fib6_table *table,
1597 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001599 struct fib6_node *child_left, *child_right;
1600
David S. Miller507c9b12011-12-03 17:50:45 -05001601 if (fn->fn_flags & RTN_ROOT)
David Ahern421842e2018-04-17 17:33:18 -07001602 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
David S. Miller507c9b12011-12-03 17:50:45 -05001604 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001605 child_left = rcu_dereference_protected(fn->left,
1606 lockdep_is_held(&table->tb6_lock));
1607 child_right = rcu_dereference_protected(fn->right,
1608 lockdep_is_held(&table->tb6_lock));
1609 if (child_left)
1610 return rcu_dereference_protected(child_left->leaf,
1611 lockdep_is_held(&table->tb6_lock));
1612 if (child_right)
1613 return rcu_dereference_protected(child_right->leaf,
1614 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001616 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 return NULL;
1619}
1620
1621/*
1622 * Called to trim the tree of intermediate nodes when possible. "fn"
1623 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001624 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
1626
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001627static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001628 struct fib6_table *table,
1629 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 int children;
1632 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001633 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001634 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int iter = 0;
1636
Wei Wang4512c432018-01-08 10:34:00 -08001637 /* Set fn->leaf to null_entry for root node. */
1638 if (fn->fn_flags & RTN_TL_ROOT) {
David Ahern421842e2018-04-17 17:33:18 -07001639 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
Wei Wang4512c432018-01-08 10:34:00 -08001640 return fn;
1641 }
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001644 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1645 lockdep_is_held(&table->tb6_lock));
1646 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1647 lockdep_is_held(&table->tb6_lock));
1648 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1649 lockdep_is_held(&table->tb6_lock));
1650 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1651 lockdep_is_held(&table->tb6_lock));
1652 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1653 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001654 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001655 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001656 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001657 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001658 struct fib6_info *new_fn_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1661 iter++;
1662
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001663 WARN_ON(fn->fn_flags & RTN_RTINFO);
1664 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001665 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 children = 0;
1668 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001669 if (fn_r)
1670 child = fn_r, children |= 1;
1671 if (fn_l)
1672 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001674 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675#ifdef CONFIG_IPV6_SUBTREES
1676 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001677 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678#endif
1679 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001680 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001682 if (!new_fn_leaf) {
1683 WARN_ON(!new_fn_leaf);
David Ahern421842e2018-04-17 17:33:18 -07001684 new_fn_leaf = net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686#endif
David Ahern93531c62018-04-17 17:33:25 -07001687 fib6_info_hold(new_fn_leaf);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001688 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1689 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001693 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001694 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001695 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 nstate = FWS_L;
1697 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001698 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001700 if (pn_r == fn)
1701 rcu_assign_pointer(pn->right, child);
1702 else if (pn_l == fn)
1703 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001705 else
1706 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707#endif
1708 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001709 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 nstate = FWS_R;
1711#ifdef CONFIG_IPV6_SUBTREES
1712 }
1713#endif
1714
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001715 read_lock(&net->ipv6.fib6_walker_lock);
1716 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001717 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001718 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1720 w->node = pn;
1721 w->state = nstate;
1722 }
1723 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (w->node == fn) {
1725 w->node = child;
1726 if (children&2) {
1727 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001728 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else {
1730 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001731 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 }
1734 }
1735 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001736 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Wei Wang81eb8442017-10-06 12:06:11 -07001738 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001739 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return pn;
1741
Wei Wang66f5d6c2017-10-06 12:06:10 -07001742 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001743 fib6_info_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 fn = pn;
1745 }
1746}
1747
Wei Wang66f5d6c2017-10-06 12:06:10 -07001748static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
David Ahern8d1c8022018-04-17 17:33:26 -07001749 struct fib6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001751 struct fib6_walker *w;
David Ahern8d1c8022018-04-17 17:33:26 -07001752 struct fib6_info *rt = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001753 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001754 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 RT6_TRACE("fib6_del_route\n");
1757
1758 /* Unlink it */
David Ahern8fb11a92018-05-04 13:54:24 -07001759 *rtp = rt->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001760 rt->fib6_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001761 net->ipv6.rt6_stats->fib_rt_entries--;
1762 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Wei Wang2b760fc2017-10-06 12:06:03 -07001764 /* Flush all cached dst in exception table */
1765 rt6_flush_exceptions(rt);
1766
David S. Millerf11e6652007-03-24 20:36:25 -07001767 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001768 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001769 fn->rr_ptr = NULL;
1770
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001771 /* Remove this entry from other siblings */
David Ahern93c2fb22018-04-18 15:38:59 -07001772 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07001773 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001774
1775 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001776 &rt->fib6_siblings, fib6_siblings)
1777 sibling->fib6_nsiblings--;
1778 rt->fib6_nsiblings = 0;
1779 list_del_init(&rt->fib6_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001780 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001781 }
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001784 read_lock(&net->ipv6.fib6_walker_lock);
1785 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (w->state == FWS_C && w->leaf == rt) {
1787 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Ahern8fb11a92018-05-04 13:54:24 -07001788 w->leaf = rcu_dereference_protected(rt->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001789 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001790 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 w->state = FWS_U;
1792 }
1793 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001794 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Wei Wang4512c432018-01-08 10:34:00 -08001796 /* If it was last route, call fib6_repair_tree() to:
1797 * 1. For root node, put back null_entry as how the table was created.
1798 * 2. For other nodes, expunge its radix tree node.
1799 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001800 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001801 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1802 fn->fn_flags &= ~RTN_RTINFO;
1803 net->ipv6.rt6_stats->fib_route_nodes--;
1804 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001805 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001808 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
David Ahern6c31e5a2017-10-27 17:37:13 -07001810 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001811 if (!info->skip_notify)
1812 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
David Ahern93531c62018-04-17 17:33:25 -07001813 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Wei Wang66f5d6c2017-10-06 12:06:10 -07001816/* Need to own table->tb6_lock */
David Ahern8d1c8022018-04-17 17:33:26 -07001817int fib6_del(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
David Ahern93c2fb22018-04-18 15:38:59 -07001819 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1820 lockdep_is_held(&rt->fib6_table->tb6_lock));
1821 struct fib6_table *table = rt->fib6_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001822 struct net *net = info->nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07001823 struct fib6_info __rcu **rtp;
1824 struct fib6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
David Ahern421842e2018-04-17 17:33:18 -07001826 if (!fn || rt == net->ipv6.fib6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -ENOENT;
1828
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001829 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * Walk the leaf entries looking for ourself
1833 */
1834
Wei Wang66f5d6c2017-10-06 12:06:10 -07001835 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
David Ahern8d1c8022018-04-17 17:33:26 -07001836 struct fib6_info *cur = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001837 lockdep_is_held(&table->tb6_lock));
1838 if (rt == cur) {
1839 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 0;
1841 }
David Ahern8fb11a92018-05-04 13:54:24 -07001842 rtp_next = &cur->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 return -ENOENT;
1845}
1846
1847/*
1848 * Tree traversal function.
1849 *
1850 * Certainly, it is not interrupt safe.
1851 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1852 * It means, that we can modify tree during walking
1853 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001854 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
1856 * It guarantees that every node will be traversed,
1857 * and that it will be traversed only once.
1858 *
1859 * Callback function w->func may return:
1860 * 0 -> continue walking.
1861 * positive value -> walking is suspended (used by tree dumps,
1862 * and probably by gc, if it will be split to several slices)
1863 * negative value -> terminate walking.
1864 *
1865 * The function itself returns:
1866 * 0 -> walk is complete.
1867 * >0 -> walk is incomplete (i.e. suspended)
1868 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001869 *
1870 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
1872
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001873static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001875 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Wei Wang2b760fc2017-10-06 12:06:03 -07001877 /* w->root should always be table->tb6_root */
1878 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 for (;;) {
1881 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001882 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 switch (w->state) {
1886#ifdef CONFIG_IPV6_SUBTREES
1887 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001888 if (FIB6_SUBTREE(fn)) {
1889 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 continue;
1891 }
1892 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001893#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001894 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001896 left = rcu_dereference_protected(fn->left, 1);
1897 if (left) {
1898 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 w->state = FWS_INIT;
1900 continue;
1901 }
1902 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001903 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001905 right = rcu_dereference_protected(fn->right, 1);
1906 if (right) {
1907 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 w->state = FWS_INIT;
1909 continue;
1910 }
1911 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001912 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001913 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001915 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001916 int err;
1917
Eric Dumazetfa809e22012-06-25 15:37:19 -07001918 if (w->skip) {
1919 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001920 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001921 }
1922
1923 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (err)
1925 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001926
1927 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 continue;
1929 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001930skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001932 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 case FWS_U:
1934 if (fn == w->root)
1935 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001936 pn = rcu_dereference_protected(fn->parent, 1);
1937 left = rcu_dereference_protected(pn->left, 1);
1938 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 w->node = pn;
1940#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001941 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001942 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 w->state = FWS_L;
1944 continue;
1945 }
1946#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001947 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 w->state = FWS_R;
1949 continue;
1950 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001951 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001953 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 continue;
1955 }
1956#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001957 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958#endif
1959 }
1960 }
1961}
1962
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001963static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int res;
1966
1967 w->state = FWS_INIT;
1968 w->node = w->root;
1969
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001970 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 res = fib6_walk_continue(w);
1972 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001973 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return res;
1975}
1976
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001977static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 int res;
David Ahern8d1c8022018-04-17 17:33:26 -07001980 struct fib6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001981 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001982 struct nl_info info = {
1983 .nl_net = c->net,
David Ahern7c6bb7d2018-10-11 20:17:21 -07001984 .skip_notify = c->skip_notify,
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001987 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1988 w->node->fn_sernum != c->sernum)
1989 w->node->fn_sernum = c->sernum;
1990
1991 if (!c->func) {
1992 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1993 w->leaf = NULL;
1994 return 0;
1995 }
1996
Wei Wang66f5d6c2017-10-06 12:06:10 -07001997 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001999 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002001 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (res) {
2003#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00002004 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07002005 __func__, rt,
David Ahern93c2fb22018-04-18 15:38:59 -07002006 rcu_access_pointer(rt->fib6_node),
Wei Wang4e587ea2017-08-25 15:03:10 -07002007 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008#endif
2009 continue;
2010 }
2011 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002012 } else if (res == -2) {
David Ahern93c2fb22018-04-18 15:38:59 -07002013 if (WARN_ON(!rt->fib6_nsiblings))
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002014 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002015 rt = list_last_entry(&rt->fib6_siblings,
2016 struct fib6_info, fib6_siblings);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002017 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002019 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 w->leaf = rt;
2022 return 0;
2023}
2024
2025/*
2026 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002029 * It may return -2 -> skip multipath route.
2030 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
2033
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002034static void fib6_clean_tree(struct net *net, struct fib6_node *root,
David Ahern8d1c8022018-04-17 17:33:26 -07002035 int (*func)(struct fib6_info *, void *arg),
David Ahern7c6bb7d2018-10-11 20:17:21 -07002036 int sernum, void *arg, bool skip_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002038 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 c.w.root = root;
2041 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00002042 c.w.count = 0;
2043 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002045 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002047 c.net = net;
David Ahern7c6bb7d2018-10-11 20:17:21 -07002048 c.skip_notify = skip_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002050 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002053static void __fib6_clean_all(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07002054 int (*func)(struct fib6_info *, void *),
David Ahern7c6bb7d2018-10-11 20:17:21 -07002055 int sernum, void *arg, bool skip_notify)
Thomas Grafc71099a2006-08-04 23:20:06 -07002056{
Thomas Grafc71099a2006-08-04 23:20:06 -07002057 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002058 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07002059 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07002060
Patrick McHardy1b43af52006-08-10 23:11:17 -07002061 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07002062 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002063 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002064 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002065 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002066 fib6_clean_tree(net, &table->tb6_root,
David Ahern7c6bb7d2018-10-11 20:17:21 -07002067 func, sernum, arg, skip_notify);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002068 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07002069 }
2070 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07002071 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07002072}
2073
David Ahern8d1c8022018-04-17 17:33:26 -07002074void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002075 void *arg)
2076{
David Ahern7c6bb7d2018-10-11 20:17:21 -07002077 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2078}
2079
2080void fib6_clean_all_skip_notify(struct net *net,
2081 int (*func)(struct fib6_info *, void *),
2082 void *arg)
2083{
2084 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002085}
2086
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002087static void fib6_flush_trees(struct net *net)
2088{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002089 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002090
David Ahern7c6bb7d2018-10-11 20:17:21 -07002091 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002092}
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/*
2095 * Garbage collection
2096 */
2097
David Ahern8d1c8022018-04-17 17:33:26 -07002098static int fib6_age(struct fib6_info *rt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Michal Kubeček3570df92016-03-08 14:44:25 +01002100 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 unsigned long now = jiffies;
2102
2103 /*
2104 * check addrconf expiration here.
2105 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 */
2107
David Ahern93c2fb22018-04-18 15:38:59 -07002108 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
David Ahern14895682018-04-17 17:33:17 -07002109 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return -1;
2112 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002113 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
Wei Wangc757faa2017-10-06 12:06:01 -07002116 /* Also age clones in the exception table.
2117 * Note, that clones are aged out
2118 * only if they are not in use now.
2119 */
2120 rt6_age_exceptions(rt, gc_args, now);
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return 0;
2123}
2124
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002125void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Michal Kubeček3570df92016-03-08 14:44:25 +01002127 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002128 unsigned long now;
2129
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002130 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002131 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2132 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002133 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2134 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002136 gc_args.timeout = expires ? (int)expires :
2137 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002138 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002139
Michal Kubeček3570df92016-03-08 14:44:25 +01002140 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002141 now = jiffies;
2142 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002145 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002146 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002147 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07002148 else
2149 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002150 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Kees Cook86cb30e2017-10-17 20:21:24 -07002153static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002154{
Kees Cook86cb30e2017-10-17 20:21:24 -07002155 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2156
2157 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002158}
2159
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002160static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002161{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002162 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002163 int err;
2164
2165 err = fib6_notifier_init(net);
2166 if (err)
2167 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002168
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002169 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002170 rwlock_init(&net->ipv6.fib6_walker_lock);
2171 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002172 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002173
Benjamin Theryc5728722008-03-03 23:34:17 -08002174 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2175 if (!net->ipv6.rt6_stats)
2176 goto out_timer;
2177
Eric Dumazet10da66f2010-10-13 08:22:03 +00002178 /* Avoid false sharing : Use at least a full cache line */
2179 size = max_t(size_t, size, L1_CACHE_BYTES);
2180
2181 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002182 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002183 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002184
2185 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2186 GFP_KERNEL);
2187 if (!net->ipv6.fib6_main_tbl)
2188 goto out_fib_table_hash;
2189
2190 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002191 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002192 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002193 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2194 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002195 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002196
2197#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2198 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2199 GFP_KERNEL);
2200 if (!net->ipv6.fib6_local_tbl)
2201 goto out_fib6_main_tbl;
2202 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002203 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002204 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002205 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2206 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002207 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002208#endif
2209 fib6_tables_init(net);
2210
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07002211 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002212
2213#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2214out_fib6_main_tbl:
2215 kfree(net->ipv6.fib6_main_tbl);
2216#endif
2217out_fib_table_hash:
2218 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002219out_rt6_stats:
2220 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002221out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002222 fib6_notifier_exit(net);
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07002223 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002224}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002225
2226static void fib6_net_exit(struct net *net)
2227{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002228 unsigned int i;
2229
Stephen Hemminger417f28bb2008-07-22 14:33:45 -07002230 del_timer_sync(&net->ipv6.ip6_fib_timer);
2231
Eric Dumazet32a805b2017-09-08 15:48:47 -07002232 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002233 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2234 struct hlist_node *tmp;
2235 struct fib6_table *tb;
2236
2237 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2238 hlist_del(&tb->tb6_hlist);
2239 fib6_free_table(tb);
2240 }
2241 }
2242
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002243 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002244 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002245 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002246}
2247
2248static struct pernet_operations fib6_net_ops = {
2249 .init = fib6_net_init,
2250 .exit = fib6_net_exit,
2251};
2252
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002253int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002255 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2258 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002259 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002260 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002261 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002262 goto out;
2263
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002264 ret = register_pernet_subsys(&fib6_net_ops);
2265 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002266 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002267
Florian Westphal16feebc2017-12-02 21:44:08 +01002268 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2269 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002270 if (ret)
2271 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002272
2273 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002274out:
2275 return ret;
2276
David S. Millere8803b62012-06-16 01:12:19 -07002277out_unregister_subsys:
2278 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002279out_kmem_cache_create:
2280 kmem_cache_destroy(fib6_node_kmem);
2281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
2284void fib6_gc_cleanup(void)
2285{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002286 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 kmem_cache_destroy(fib6_node_kmem);
2288}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002289
2290#ifdef CONFIG_PROC_FS
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002291static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2292{
David Ahern8d1c8022018-04-17 17:33:26 -07002293 struct fib6_info *rt = v;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002294 struct ipv6_route_iter *iter = seq->private;
David Ahern2b2450c2019-03-27 20:53:52 -07002295 unsigned int flags = rt->fib6_flags;
David Ahern5e670d82018-04-17 17:33:14 -07002296 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002297
David Ahern93c2fb22018-04-18 15:38:59 -07002298 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002299
2300#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002301 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002302#else
2303 seq_puts(seq, "00000000000000000000000000000000 00 ");
2304#endif
David Ahernbdf00462019-04-05 16:30:26 -07002305 if (rt->fib6_nh.fib_nh_gw_family) {
David Ahern2b2450c2019-03-27 20:53:52 -07002306 flags |= RTF_GATEWAY;
David Ahernad1601a2019-03-27 20:53:56 -07002307 seq_printf(seq, "%pi6", &rt->fib6_nh.fib_nh_gw6);
David Ahern2b2450c2019-03-27 20:53:52 -07002308 } else {
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002309 seq_puts(seq, "00000000000000000000000000000000");
David Ahern2b2450c2019-03-27 20:53:52 -07002310 }
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002311
David Ahernad1601a2019-03-27 20:53:56 -07002312 dev = rt->fib6_nh.fib_nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002313 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
Eric Dumazetf05713e2019-04-22 18:35:03 -07002314 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
David Ahern2b2450c2019-03-27 20:53:52 -07002315 flags, dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002316 iter->w.leaf = NULL;
2317 return 0;
2318}
2319
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002320static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002321{
2322 struct ipv6_route_iter *iter = w->args;
2323
2324 if (!iter->skip)
2325 return 1;
2326
2327 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002328 iter->w.leaf = rcu_dereference_protected(
David Ahern8fb11a92018-05-04 13:54:24 -07002329 iter->w.leaf->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002330 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002331 iter->skip--;
2332 if (!iter->skip && iter->w.leaf)
2333 return 1;
2334 } while (iter->w.leaf);
2335
2336 return 0;
2337}
2338
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002339static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2340 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002341{
2342 memset(&iter->w, 0, sizeof(iter->w));
2343 iter->w.func = ipv6_route_yield;
2344 iter->w.root = &iter->tbl->tb6_root;
2345 iter->w.state = FWS_INIT;
2346 iter->w.node = iter->w.root;
2347 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002348 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002349 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002350 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002351}
2352
2353static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2354 struct net *net)
2355{
2356 unsigned int h;
2357 struct hlist_node *node;
2358
2359 if (tbl) {
2360 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2361 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2362 } else {
2363 h = 0;
2364 node = NULL;
2365 }
2366
2367 while (!node && h < FIB6_TABLE_HASHSZ) {
2368 node = rcu_dereference_bh(
2369 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2370 }
2371 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2372}
2373
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002374static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2375{
2376 if (iter->sernum != iter->w.root->fn_sernum) {
2377 iter->sernum = iter->w.root->fn_sernum;
2378 iter->w.state = FWS_INIT;
2379 iter->w.node = iter->w.root;
2380 WARN_ON(iter->w.skip);
2381 iter->w.skip = iter->w.count;
2382 }
2383}
2384
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002385static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2386{
2387 int r;
David Ahern8d1c8022018-04-17 17:33:26 -07002388 struct fib6_info *n;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002389 struct net *net = seq_file_net(seq);
2390 struct ipv6_route_iter *iter = seq->private;
2391
2392 if (!v)
2393 goto iter_table;
2394
David Ahern8fb11a92018-05-04 13:54:24 -07002395 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002396 if (n) {
2397 ++*pos;
2398 return n;
2399 }
2400
2401iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002402 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002403 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002404 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002405 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002406 if (r > 0) {
2407 if (v)
2408 ++*pos;
2409 return iter->w.leaf;
2410 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002411 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002412 return NULL;
2413 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002414 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002415
2416 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2417 if (!iter->tbl)
2418 return NULL;
2419
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002420 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002421 goto iter_table;
2422}
2423
2424static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2425 __acquires(RCU_BH)
2426{
2427 struct net *net = seq_file_net(seq);
2428 struct ipv6_route_iter *iter = seq->private;
2429
2430 rcu_read_lock_bh();
2431 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2432 iter->skip = *pos;
2433
2434 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002435 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002436 return ipv6_route_seq_next(seq, NULL, pos);
2437 } else {
2438 return NULL;
2439 }
2440}
2441
2442static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2443{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002444 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002445 return w->node && !(w->state == FWS_U && w->node == w->root);
2446}
2447
2448static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2449 __releases(RCU_BH)
2450{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002451 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002452 struct ipv6_route_iter *iter = seq->private;
2453
2454 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002455 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002456
2457 rcu_read_unlock_bh();
2458}
2459
Christoph Hellwigc3506372018-04-10 19:42:55 +02002460const struct seq_operations ipv6_route_seq_ops = {
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002461 .start = ipv6_route_seq_start,
2462 .next = ipv6_route_seq_next,
2463 .stop = ipv6_route_seq_stop,
2464 .show = ipv6_route_seq_show
2465};
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002466#endif /* CONFIG_PROC_FS */