blob: 6613d8dbb0e5a5c3ba883c957e5bc4ba2bf00777 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David Aherncc5f0eb2018-10-04 20:07:52 -070032#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <net/ipv6.h>
34#include <net/ndisc.h>
35#include <net/addrconf.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020036#include <net/lwtunnel.h>
Ido Schimmeldf77fe42017-08-03 13:28:17 +020037#include <net/fib_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <net/ip6_fib.h>
40#include <net/ip6_route.h>
41
Wang Yufen437de072014-03-28 12:07:04 +080042static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020044struct fib6_cleaner {
45 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080046 struct net *net;
David Ahern8d1c8022018-04-17 17:33:26 -070047 int (*func)(struct fib6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020048 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 void *arg;
David Ahern7c6bb7d2018-10-11 20:17:21 -070050 bool skip_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_IPV6_SUBTREES
54#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
56#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
David Ahern8d1c8022018-04-17 17:33:26 -070059static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -070060 struct fib6_table *table,
61 struct fib6_node *fn);
62static struct fib6_node *fib6_repair_tree(struct net *net,
63 struct fib6_table *table,
64 struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010065static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020066static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * A routing update causes an increase of the serial number on the
70 * affected subtree. This allows for cached routes to be asynchronously
71 * tested when modifications are made to the destination cache as a
72 * result of redirects, path MTU changes, etc.
73 */
74
Kees Cook86cb30e2017-10-17 20:21:24 -070075static void fib6_gc_timer_cb(struct timer_list *t);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080076
Michal Kubeček9a03cd82016-03-08 14:44:35 +010077#define FOR_WALKERS(net, w) \
78 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Michal Kubeček9a03cd82016-03-08 14:44:35 +010080static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070081{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010082 write_lock_bh(&net->ipv6.fib6_walker_lock);
83 list_add(&w->lh, &net->ipv6.fib6_walkers);
84 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070085}
86
Michal Kubeček9a03cd82016-03-08 14:44:35 +010087static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070088{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010089 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000090 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010091 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070092}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020093
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020094static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020096 int new, old;
97
98 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020099 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200100 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200101 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
102 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200103 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200106enum {
107 FIB6_NO_SERNUM_CHANGE = 0,
108};
109
David Ahern93c2fb22018-04-18 15:38:59 -0700110void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
Wei Wang180ca442017-10-06 12:05:56 -0700111{
Wei Wang180ca442017-10-06 12:05:56 -0700112 struct fib6_node *fn;
113
David Ahern93c2fb22018-04-18 15:38:59 -0700114 fn = rcu_dereference_protected(f6i->fib6_node,
115 lockdep_is_held(&f6i->fib6_table->tb6_lock));
Wei Wang180ca442017-10-06 12:05:56 -0700116 if (fn)
117 fn->fn_sernum = fib6_new_sernum(net);
Wei Wang180ca442017-10-06 12:05:56 -0700118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * Auxiliary address test functions for the radix tree.
122 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900123 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * 64bit processors)
125 */
126
127/*
128 * test bit
129 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000130#if defined(__LITTLE_ENDIAN)
131# define BITOP_BE32_SWIZZLE (0x1F & ~7)
132#else
133# define BITOP_BE32_SWIZZLE 0
134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200136static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000138 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000139 /*
140 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800141 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000142 * is optimized version of
143 * htonl(1 << ((~fn_bit)&0x1F))
144 * See include/asm-generic/bitops/le.h.
145 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700146 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
147 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
David Ahern8d1c8022018-04-17 17:33:26 -0700150struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
David Aherna64efe142018-04-17 17:33:24 -0700151{
David Ahern8d1c8022018-04-17 17:33:26 -0700152 struct fib6_info *f6i;
David Aherna64efe142018-04-17 17:33:24 -0700153
154 f6i = kzalloc(sizeof(*f6i), gfp_flags);
155 if (!f6i)
156 return NULL;
157
158 f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
159 if (!f6i->rt6i_pcpu) {
160 kfree(f6i);
161 return NULL;
162 }
163
David Ahern93c2fb22018-04-18 15:38:59 -0700164 INIT_LIST_HEAD(&f6i->fib6_siblings);
David Ahern93c2fb22018-04-18 15:38:59 -0700165 atomic_inc(&f6i->fib6_ref);
David Aherna64efe142018-04-17 17:33:24 -0700166
167 return f6i;
168}
169
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700170void fib6_info_destroy_rcu(struct rcu_head *head)
David Aherna64efe142018-04-17 17:33:24 -0700171{
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700172 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
David Aherna64efe142018-04-17 17:33:24 -0700173 struct rt6_exception_bucket *bucket;
174
David Ahern93c2fb22018-04-18 15:38:59 -0700175 WARN_ON(f6i->fib6_node);
David Aherna64efe142018-04-17 17:33:24 -0700176
177 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
178 if (bucket) {
179 f6i->rt6i_exception_bucket = NULL;
180 kfree(bucket);
181 }
182
183 if (f6i->rt6i_pcpu) {
184 int cpu;
185
186 for_each_possible_cpu(cpu) {
187 struct rt6_info **ppcpu_rt;
188 struct rt6_info *pcpu_rt;
189
190 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
191 pcpu_rt = *ppcpu_rt;
192 if (pcpu_rt) {
193 dst_dev_put(&pcpu_rt->dst);
194 dst_release(&pcpu_rt->dst);
195 *ppcpu_rt = NULL;
196 }
197 }
Mike Rapoport7abab7b2018-10-09 07:02:01 +0300198
199 free_percpu(f6i->rt6i_pcpu);
David Aherna64efe142018-04-17 17:33:24 -0700200 }
201
David Ahern80f1a0f2018-08-20 13:02:41 -0700202 lwtstate_put(f6i->fib6_nh.nh_lwtstate);
203
David Aherna64efe142018-04-17 17:33:24 -0700204 if (f6i->fib6_nh.nh_dev)
205 dev_put(f6i->fib6_nh.nh_dev);
206
David Aherncc5f0eb2018-10-04 20:07:52 -0700207 ip_fib_metrics_put(f6i->fib6_metrics);
David Ahern93531c62018-04-17 17:33:25 -0700208
David Aherna64efe142018-04-17 17:33:24 -0700209 kfree(f6i);
210}
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700211EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
David Aherna64efe142018-04-17 17:33:24 -0700212
Wei Wang81eb8442017-10-06 12:06:11 -0700213static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct fib6_node *fn;
216
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800217 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700218 if (fn)
219 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return fn;
222}
223
Wei Wang81eb8442017-10-06 12:06:11 -0700224static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700227 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Wei Wangc5cff852017-08-21 09:47:10 -0700230static void node_free_rcu(struct rcu_head *head)
231{
232 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
233
234 kmem_cache_free(fib6_node_kmem, fn);
235}
236
Wei Wang81eb8442017-10-06 12:06:11 -0700237static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700238{
239 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700240 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700241}
242
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200243static void fib6_free_table(struct fib6_table *table)
244{
245 inetpeer_invalidate_tree(&table->tb6_peers);
246 kfree(table);
247}
248
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800249static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700250{
251 unsigned int h;
252
Thomas Graf375216a2006-10-21 20:20:54 -0700253 /*
254 * Initialize table lock at a single place to give lockdep a key,
255 * tables aren't visible prior to being linked to the list.
256 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700257 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700258 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700259
260 /*
261 * No protection necessary, this is the only list mutatation
262 * operation, tables never disappear once they exist.
263 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800264 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700265}
266
267#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800268
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800269static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700270{
271 struct fib6_table *table;
272
273 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500274 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700275 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700276 rcu_assign_pointer(table->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -0700277 net->ipv6.fib6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700278 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700279 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700280 }
281
282 return table;
283}
284
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700286{
287 struct fib6_table *tb;
288
289 if (id == 0)
290 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800291 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700292 if (tb)
293 return tb;
294
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800295 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500296 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800297 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700298
299 return tb;
300}
David Ahernb3b46632016-05-04 21:46:12 -0700301EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700302
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800303struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700304{
305 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800306 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700307 unsigned int h;
308
309 if (id == 0)
310 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700311 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700312 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800313 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800314 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700315 if (tb->tb6_id == id) {
316 rcu_read_unlock();
317 return tb;
318 }
319 }
320 rcu_read_unlock();
321
322 return NULL;
323}
David Ahernc4850682015-10-12 11:47:08 -0700324EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700325
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000326static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700327{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800328 fib6_link_table(net, net->ipv6.fib6_main_tbl);
329 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700330}
Thomas Grafc71099a2006-08-04 23:20:06 -0700331#else
332
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800333struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700334{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800335 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700336}
337
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800338struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700339{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800340 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700341}
342
David S. Miller4c9483b2011-03-12 16:22:43 -0500343struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -0800344 const struct sk_buff *skb,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800345 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700346{
lucienab997ad2015-10-23 15:36:53 +0800347 struct rt6_info *rt;
348
David Ahernb75cc8f2018-03-02 08:32:17 -0800349 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300350 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800351 ip6_rt_put(rt);
352 rt = net->ipv6.ip6_null_entry;
353 dst_hold(&rt->dst);
354 }
355
356 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700357}
358
David Ahern138118e2018-05-09 20:34:23 -0700359/* called with rcu lock held; no reference taken on fib6_info */
360struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
361 int flags)
362{
363 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, flags);
364}
365
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000366static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700367{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800368 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700369}
370
371#endif
372
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200373unsigned int fib6_tables_seq_read(struct net *net)
374{
375 unsigned int h, fib_seq = 0;
376
377 rcu_read_lock();
378 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
379 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
380 struct fib6_table *tb;
381
Wei Wang66f5d6c2017-10-06 12:06:10 -0700382 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200383 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200384 }
385 rcu_read_unlock();
386
387 return fib_seq;
388}
389
390static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
391 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700392 struct fib6_info *rt)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200393{
394 struct fib6_entry_notifier_info info = {
395 .rt = rt,
396 };
397
398 return call_fib6_notifier(nb, net, event_type, &info.info);
399}
400
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200401static int call_fib6_entry_notifiers(struct net *net,
402 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700403 struct fib6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700404 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200405{
406 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700407 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200408 .rt = rt,
409 };
410
David Ahern93c2fb22018-04-18 15:38:59 -0700411 rt->fib6_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200412 return call_fib6_notifiers(net, event_type, &info.info);
413}
414
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200415struct fib6_dump_arg {
416 struct net *net;
417 struct notifier_block *nb;
418};
419
David Ahern8d1c8022018-04-17 17:33:26 -0700420static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200421{
David Ahern421842e2018-04-17 17:33:18 -0700422 if (rt == arg->net->ipv6.fib6_null_entry)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200423 return;
424 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
425}
426
427static int fib6_node_dump(struct fib6_walker *w)
428{
David Ahern8d1c8022018-04-17 17:33:26 -0700429 struct fib6_info *rt;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200430
Wei Wang66f5d6c2017-10-06 12:06:10 -0700431 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200432 fib6_rt_dump(rt, w->args);
433 w->leaf = NULL;
434 return 0;
435}
436
437static void fib6_table_dump(struct net *net, struct fib6_table *tb,
438 struct fib6_walker *w)
439{
440 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700441 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200442 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700443 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200444}
445
446/* Called with rcu_read_lock() */
447int fib6_tables_dump(struct net *net, struct notifier_block *nb)
448{
449 struct fib6_dump_arg arg;
450 struct fib6_walker *w;
451 unsigned int h;
452
453 w = kzalloc(sizeof(*w), GFP_ATOMIC);
454 if (!w)
455 return -ENOMEM;
456
457 w->func = fib6_node_dump;
458 arg.net = net;
459 arg.nb = nb;
460 w->args = &arg;
461
462 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
463 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
464 struct fib6_table *tb;
465
466 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
467 fib6_table_dump(net, tb, w);
468 }
469
470 kfree(w);
471
472 return 0;
473}
474
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200475static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700476{
477 int res;
David Ahern8d1c8022018-04-17 17:33:26 -0700478 struct fib6_info *rt;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700479
Wei Wang66f5d6c2017-10-06 12:06:10 -0700480 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700481 res = rt6_dump_route(rt, w->args);
482 if (res < 0) {
483 /* Frame is full, suspend walking */
484 w->leaf = rt;
485 return 1;
486 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800487
488 /* Multipath routes are dumped in one route with the
489 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
490 * last sibling of this route (no need to dump the
491 * sibling routes again)
492 */
David Ahern93c2fb22018-04-18 15:38:59 -0700493 if (rt->fib6_nsiblings)
494 rt = list_last_entry(&rt->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -0700495 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -0700496 fib6_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700497 }
498 w->leaf = NULL;
499 return 0;
500}
501
502static void fib6_dump_end(struct netlink_callback *cb)
503{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100504 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200505 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700506
507 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800508 if (cb->args[4]) {
509 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100510 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800511 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700512 cb->args[2] = 0;
513 kfree(w);
514 }
Wang Yufen437de072014-03-28 12:07:04 +0800515 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700516 cb->args[1] = 3;
517}
518
519static int fib6_dump_done(struct netlink_callback *cb)
520{
521 fib6_dump_end(cb);
522 return cb->done ? cb->done(cb) : 0;
523}
524
525static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
526 struct netlink_callback *cb)
527{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100528 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200529 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700530 int res;
531
532 w = (void *)cb->args[2];
533 w->root = &table->tb6_root;
534
535 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000536 w->count = 0;
537 w->skip = 0;
538
Wei Wang66f5d6c2017-10-06 12:06:10 -0700539 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100540 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700541 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000542 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700543 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000544 cb->args[5] = w->root->fn_sernum;
545 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700546 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000547 if (cb->args[5] != w->root->fn_sernum) {
548 /* Begin at the root if the tree changed */
549 cb->args[5] = w->root->fn_sernum;
550 w->state = FWS_INIT;
551 w->node = w->root;
552 w->skip = w->count;
553 } else
554 w->skip = 0;
555
Wei Wang66f5d6c2017-10-06 12:06:10 -0700556 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700557 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700558 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800559 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100560 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800561 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700562 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700563 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800564
Patrick McHardy1b43af52006-08-10 23:11:17 -0700565 return res;
566}
567
Thomas Grafc127ea22007-03-22 11:58:32 -0700568static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700569{
David Aherne8ba3302018-10-07 20:16:35 -0700570 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900571 struct net *net = sock_net(skb->sk);
David Ahern47246762018-10-15 18:56:42 -0700572 struct rt6_rtnl_dump_arg arg = {};
Patrick McHardy1b43af52006-08-10 23:11:17 -0700573 unsigned int h, s_h;
574 unsigned int e = 0, s_e;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200575 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700576 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800577 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700578 int res = 0;
579
David Aherne8ba3302018-10-07 20:16:35 -0700580 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -0700581 int err;
David Aherne8ba3302018-10-07 20:16:35 -0700582
David Aherneffe6792018-10-15 18:56:48 -0700583 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -0700584 if (err < 0)
585 return err;
David Ahern13e38902018-10-15 18:56:44 -0700586 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
587 struct rtmsg *rtm = nlmsg_data(nlh);
David Aherne8ba3302018-10-07 20:16:35 -0700588
David Ahern08e814c2018-10-15 18:56:50 -0700589 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
David Ahern13e38902018-10-15 18:56:44 -0700590 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700591
David Ahern08e814c2018-10-15 18:56:50 -0700592 /* fib entries are never clones */
593 if (arg.filter.flags & RTM_F_CLONED)
Alexey Kodaneve22d0bf2018-11-02 19:11:05 +0300594 goto out;
David Ahern08e814c2018-10-15 18:56:50 -0700595
Patrick McHardy1b43af52006-08-10 23:11:17 -0700596 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500597 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700598 /* New dump:
599 *
600 * 1. hook callback destructor.
601 */
602 cb->args[3] = (long)cb->done;
603 cb->done = fib6_dump_done;
604
605 /*
606 * 2. allocate and initialize walker.
607 */
608 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500609 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700610 return -ENOMEM;
611 w->func = fib6_dump_node;
612 cb->args[2] = (long)w;
613 }
614
615 arg.skb = skb;
616 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700617 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700618 w->args = &arg;
619
David Ahern13e38902018-10-15 18:56:44 -0700620 if (arg.filter.table_id) {
621 tb = fib6_get_table(net, arg.filter.table_id);
622 if (!tb) {
David Ahernae677bb2018-10-24 12:59:01 -0700623 if (arg.filter.dump_all_families)
Alexey Kodaneve22d0bf2018-11-02 19:11:05 +0300624 goto out;
David Ahernae677bb2018-10-24 12:59:01 -0700625
David Ahern13e38902018-10-15 18:56:44 -0700626 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
627 return -ENOENT;
628 }
629
David Ahern73155872019-01-02 18:26:13 -0800630 if (!cb->args[0]) {
631 res = fib6_dump_table(tb, skb, cb);
632 if (!res)
633 cb->args[0] = 1;
634 }
David Ahern13e38902018-10-15 18:56:44 -0700635 goto out;
636 }
637
638 s_h = cb->args[0];
639 s_e = cb->args[1];
640
Eric Dumazete67f88d2011-04-27 22:56:07 +0000641 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700642 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700643 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800644 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800645 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700646 if (e < s_e)
647 goto next;
648 res = fib6_dump_table(tb, skb, cb);
649 if (res != 0)
David Ahern13e38902018-10-15 18:56:44 -0700650 goto out_unlock;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700651next:
652 e++;
653 }
654 }
David Ahern13e38902018-10-15 18:56:44 -0700655out_unlock:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000656 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700657 cb->args[1] = e;
658 cb->args[0] = h;
David Ahern13e38902018-10-15 18:56:44 -0700659out:
Patrick McHardy1b43af52006-08-10 23:11:17 -0700660 res = res < 0 ? res : skb->len;
661 if (res <= 0)
662 fib6_dump_end(cb);
663 return res;
664}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
David Ahern8d1c8022018-04-17 17:33:26 -0700666void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
David Ahernd4ead6b2018-04-17 17:33:16 -0700667{
668 if (!f6i)
669 return;
670
671 if (f6i->fib6_metrics == &dst_default_metrics) {
672 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
673
674 if (!p)
675 return;
676
677 refcount_set(&p->refcnt, 1);
678 f6i->fib6_metrics = p;
679 }
680
681 f6i->fib6_metrics->metrics[metric - 1] = val;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
685 * Routing Table
686 *
687 * return the appropriate node for a routing tree "add" operation
688 * by either creating and inserting or by returning an existing
689 * node.
690 */
691
Wei Wang81eb8442017-10-06 12:06:11 -0700692static struct fib6_node *fib6_add_1(struct net *net,
693 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700694 struct fib6_node *root,
695 struct in6_addr *addr, int plen,
696 int offset, int allow_create,
697 int replace_required,
698 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct fib6_node *fn, *in, *ln;
701 struct fib6_node *pn = NULL;
702 struct rt6key *key;
703 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900704 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 RT6_TRACE("fib6_add_1\n");
707
708 /* insert node in tree */
709
710 fn = root;
711
712 do {
David Ahern8d1c8022018-04-17 17:33:26 -0700713 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700714 lockdep_is_held(&table->tb6_lock));
715 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /*
718 * Prefix match
719 */
720 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000721 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000722 if (!allow_create) {
723 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600724 NL_SET_ERR_MSG(extack,
725 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000726 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000727 return ERR_PTR(-ENOENT);
728 }
Joe Perchesf3213832012-05-15 14:11:53 +0000729 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000732 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Exact match ?
736 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (plen == fn->fn_bit) {
739 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500740 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700741 RCU_INIT_POINTER(fn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700742 fib6_info_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800743 /* remove null_entry in the root node */
744 } else if (fn->fn_flags & RTN_TL_ROOT &&
745 rcu_access_pointer(fn->leaf) ==
David Ahern421842e2018-04-17 17:33:18 -0700746 net->ipv6.fib6_null_entry) {
Wei Wang4512c432018-01-08 10:34:00 -0800747 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return fn;
751 }
752
753 /*
754 * We have more bits to go
755 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 dir = addr_bit_set(addr, fn->fn_bit);
759 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700760 fn = dir ?
761 rcu_dereference_protected(fn->right,
762 lockdep_is_held(&table->tb6_lock)) :
763 rcu_dereference_protected(fn->left,
764 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 } while (fn);
766
Matti Vaittinen14df0152011-11-16 21:18:02 +0000767 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000768 /* We should not create new node because
769 * NLM_F_REPLACE was specified without NLM_F_CREATE
770 * I assume it is safe to require NLM_F_CREATE when
771 * REPLACE flag is used! Later we may want to remove the
772 * check for replace_required, because according
773 * to netlink specification, NLM_F_CREATE
774 * MUST be specified if new route is created.
775 * That would keep IPv6 consistent with IPv4
776 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000777 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600778 NL_SET_ERR_MSG(extack,
779 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000780 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000781 return ERR_PTR(-ENOENT);
782 }
Joe Perchesf3213832012-05-15 14:11:53 +0000783 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
786 * We walked to the bottom of tree.
787 * Create new leaf node without children.
788 */
789
Wei Wang81eb8442017-10-06 12:06:11 -0700790 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
David S. Miller507c9b12011-12-03 17:50:45 -0500792 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000793 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700795 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700798 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700800 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 return ln;
803
804
805insert_above:
806 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900807 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * we have a less significant route.
809 * we've to insert an intermediate node on the list
810 * this new node will point to the one we need to create
811 * and the current
812 */
813
Wei Wang66f5d6c2017-10-06 12:06:10 -0700814 pn = rcu_dereference_protected(fn->parent,
815 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* find 1st bit in difference between the 2 addrs.
818
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800819 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 but if it is >= plen, the value is ignored in any case.
821 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900822
fan.du9225b232013-07-22 14:21:09 +0800823 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900825 /*
826 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * / \
828 * (new leaf node)[ln] (old node)[fn]
829 */
830 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700831 in = node_alloc(net);
832 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900833
David S. Miller507c9b12011-12-03 17:50:45 -0500834 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700836 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700838 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000839 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900842 /*
843 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * RTN_RTINFO will
845 * be off since that an address that chooses one of
846 * the branches would not match less specific routes
847 * in the other branch
848 */
849
850 in->fn_bit = bit;
851
Wei Wang66f5d6c2017-10-06 12:06:10 -0700852 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700854 atomic_inc(&rcu_dereference_protected(in->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700855 lockdep_is_held(&table->tb6_lock))->fib6_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* update parent pointer */
858 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700859 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700861 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 ln->fn_bit = plen;
864
Wei Wang66f5d6c2017-10-06 12:06:10 -0700865 RCU_INIT_POINTER(ln->parent, in);
866 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700869 rcu_assign_pointer(in->right, ln);
870 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700872 rcu_assign_pointer(in->left, ln);
873 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 } else { /* plen <= bit */
876
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900877 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * (new leaf node)[ln]
879 * / \
880 * (old node)[fn] NULL
881 */
882
Wei Wang81eb8442017-10-06 12:06:11 -0700883 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
David S. Miller507c9b12011-12-03 17:50:45 -0500885 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000886 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 ln->fn_bit = plen;
889
Wei Wang66f5d6c2017-10-06 12:06:10 -0700890 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700893 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700895 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Wei Wang66f5d6c2017-10-06 12:06:10 -0700897 rcu_assign_pointer(fn->parent, ln);
898
899 if (dir)
900 rcu_assign_pointer(pn->right, ln);
901 else
902 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 return ln;
905}
906
David Ahern5bcaa412018-04-20 15:38:01 -0700907static void fib6_drop_pcpu_from(struct fib6_info *f6i,
908 const struct fib6_table *table)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100909{
David Ahern5bcaa412018-04-20 15:38:01 -0700910 int cpu;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100911
David Ahern5bcaa412018-04-20 15:38:01 -0700912 /* release the reference to this fib entry from
913 * all of its cached pcpu routes
914 */
915 for_each_possible_cpu(cpu) {
916 struct rt6_info **ppcpu_rt;
917 struct rt6_info *pcpu_rt;
918
919 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
920 pcpu_rt = *ppcpu_rt;
921 if (pcpu_rt) {
David Aherna68886a2018-04-20 15:38:02 -0700922 struct fib6_info *from;
923
924 from = rcu_dereference_protected(pcpu_rt->from,
925 lockdep_is_held(&table->tb6_lock));
926 rcu_assign_pointer(pcpu_rt->from, NULL);
927 fib6_info_release(from);
David Ahern5bcaa412018-04-20 15:38:01 -0700928 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100929 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100930}
931
David Ahern8d1c8022018-04-17 17:33:26 -0700932static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100933 struct net *net)
934{
David Ahern93c2fb22018-04-18 15:38:59 -0700935 struct fib6_table *table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700936
David Ahern93c2fb22018-04-18 15:38:59 -0700937 if (atomic_read(&rt->fib6_ref) != 1) {
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100938 /* This route is used as dummy address holder in some split
939 * nodes. It is not leaked, but it still holds other resources,
940 * which must be released in time. So, scan ascendant nodes
941 * and replace dummy references to this route with references
942 * to still alive ones.
943 */
944 while (fn) {
David Ahern8d1c8022018-04-17 17:33:26 -0700945 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700946 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700947 struct fib6_info *new_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700948 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
949 new_leaf = fib6_find_prefix(net, table, fn);
David Ahern93c2fb22018-04-18 15:38:59 -0700950 atomic_inc(&new_leaf->fib6_ref);
David Ahern93531c62018-04-17 17:33:25 -0700951
Wei Wang66f5d6c2017-10-06 12:06:10 -0700952 rcu_assign_pointer(fn->leaf, new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700953 fib6_info_release(rt);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100954 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700955 fn = rcu_dereference_protected(fn->parent,
956 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100957 }
David Ahern93531c62018-04-17 17:33:25 -0700958
David Ahern5bcaa412018-04-20 15:38:01 -0700959 if (rt->rt6i_pcpu)
960 fib6_drop_pcpu_from(rt, table);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100961 }
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * Insert routing information in a node.
966 */
967
David Ahern8d1c8022018-04-17 17:33:26 -0700968static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700969 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700970 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
David Ahern8d1c8022018-04-17 17:33:26 -0700972 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700973 lockdep_is_held(&rt->fib6_table->tb6_lock));
David Ahern33bd5ac2018-07-03 14:36:21 -0700974 struct fib6_info *iter = NULL;
David Ahern8d1c8022018-04-17 17:33:26 -0700975 struct fib6_info __rcu **ins;
David Ahern33bd5ac2018-07-03 14:36:21 -0700976 struct fib6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500977 int replace = (info->nlh &&
978 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
979 int add = (!info->nlh ||
980 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000981 int found = 0;
David Ahern33bd5ac2018-07-03 14:36:21 -0700982 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200983 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100984 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
David Ahern33bd5ac2018-07-03 14:36:21 -0700986 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
David Ahern1f5e29c2017-01-31 16:51:37 -0800987 nlflags |= NLM_F_APPEND;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ins = &fn->leaf;
990
Wei Wang66f5d6c2017-10-06 12:06:10 -0700991 for (iter = leaf; iter;
David Ahern8fb11a92018-05-04 13:54:24 -0700992 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -0700993 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * Search for duplicates
996 */
997
David Ahern93c2fb22018-04-18 15:38:59 -0700998 if (iter->fib6_metric == rt->fib6_metric) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
1000 * Same priority level
1001 */
David S. Miller507c9b12011-12-03 17:50:45 -05001002 if (info->nlh &&
1003 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001004 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +02001005
1006 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001007 if (replace) {
David Ahern33bd5ac2018-07-03 14:36:21 -07001008 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1009 found++;
1010 break;
1011 }
1012 if (rt_can_ecmp)
1013 fallback_ins = fallback_ins ?: ins;
1014 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
David Ahernf06b7542017-07-05 14:41:46 -06001017 if (rt6_duplicate_nexthop(iter, rt)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001018 if (rt->fib6_nsiblings)
1019 rt->fib6_nsiblings = 0;
1020 if (!(iter->fib6_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EEXIST;
David Ahern93c2fb22018-04-18 15:38:59 -07001022 if (!(rt->fib6_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -07001023 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +00001024 else
David Ahern14895682018-04-17 17:33:17 -07001025 fib6_set_expires(iter, rt->expires);
David Ahern15a81b42018-08-30 14:15:43 -07001026
1027 if (rt->fib6_pmtu)
1028 fib6_metric_set(iter, RTAX_MTU,
1029 rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -EEXIST;
1031 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001032 /* If we have the same destination and the same metric,
1033 * but not the same gateway, then the route we try to
1034 * add is sibling to this route, increment our counter
1035 * of siblings, and later we will add our route to the
1036 * list.
1037 * Only static routes (which don't have flag
1038 * RTF_EXPIRES) are used for ECMPv6.
1039 *
1040 * To avoid long list, we only had siblings if the
1041 * route have a gateway.
1042 */
1043 if (rt_can_ecmp &&
1044 rt6_qualify_for_ecmp(iter))
1045 rt->fib6_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
David Ahern93c2fb22018-04-18 15:38:59 -07001048 if (iter->fib6_metric > rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050
David Ahern33bd5ac2018-07-03 14:36:21 -07001051next_iter:
David Ahern8fb11a92018-05-04 13:54:24 -07001052 ins = &iter->fib6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001053 }
1054
David Ahern33bd5ac2018-07-03 14:36:21 -07001055 if (fallback_ins && !found) {
1056 /* No ECMP-able route found, replace first non-ECMP one */
1057 ins = fallback_ins;
1058 iter = rcu_dereference_protected(*ins,
1059 lockdep_is_held(&rt->fib6_table->tb6_lock));
1060 found++;
1061 }
1062
David S. Millerf11e6652007-03-24 20:36:25 -07001063 /* Reset round-robin state, if necessary */
1064 if (ins == &fn->leaf)
1065 fn->rr_ptr = NULL;
1066
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001067 /* Link this route to others same route. */
David Ahern33bd5ac2018-07-03 14:36:21 -07001068 if (rt->fib6_nsiblings) {
1069 unsigned int fib6_nsiblings;
David Ahern8d1c8022018-04-17 17:33:26 -07001070 struct fib6_info *sibling, *temp_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001071
David Ahern33bd5ac2018-07-03 14:36:21 -07001072 /* Find the first route that have the same metric */
1073 sibling = leaf;
1074 while (sibling) {
1075 if (sibling->fib6_metric == rt->fib6_metric &&
1076 rt6_qualify_for_ecmp(sibling)) {
1077 list_add_tail(&rt->fib6_siblings,
1078 &sibling->fib6_siblings);
1079 break;
1080 }
1081 sibling = rcu_dereference_protected(sibling->fib6_next,
1082 lockdep_is_held(&rt->fib6_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001083 }
1084 /* For each sibling in the list, increment the counter of
1085 * siblings. BUG() if counters does not match, list of siblings
1086 * is broken!
1087 */
David Ahern33bd5ac2018-07-03 14:36:21 -07001088 fib6_nsiblings = 0;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001089 list_for_each_entry_safe(sibling, temp_sibling,
David Ahern33bd5ac2018-07-03 14:36:21 -07001090 &rt->fib6_siblings, fib6_siblings) {
David Ahern93c2fb22018-04-18 15:38:59 -07001091 sibling->fib6_nsiblings++;
David Ahern33bd5ac2018-07-03 14:36:21 -07001092 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1093 fib6_nsiblings++;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001094 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001095 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1096 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001097 }
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * insert node
1101 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001102 if (!replace) {
1103 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001104 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001106add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001107 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001108
David Ahern33bd5ac2018-07-03 14:36:21 -07001109 err = call_fib6_entry_notifiers(info->nl_net,
1110 FIB_EVENT_ENTRY_ADD,
1111 rt, extack);
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001112 if (err)
1113 return err;
1114
David Ahern8fb11a92018-05-04 13:54:24 -07001115 rcu_assign_pointer(rt->fib6_next, iter);
David Ahern93c2fb22018-04-18 15:38:59 -07001116 atomic_inc(&rt->fib6_ref);
1117 rcu_assign_pointer(rt->fib6_node, fn);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001118 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001119 if (!info->skip_notify)
1120 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001121 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
David S. Miller507c9b12011-12-03 17:50:45 -05001123 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001124 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1125 fn->fn_flags |= RTN_RTINFO;
1126 }
1127
1128 } else {
David Ahern33bd5ac2018-07-03 14:36:21 -07001129 int nsiblings;
Michal Kubeček27596472015-05-18 20:54:00 +02001130
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001131 if (!found) {
1132 if (add)
1133 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001134 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001135 return -ENOENT;
1136 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001137
David Ahern22330002018-03-27 18:21:59 -07001138 err = call_fib6_entry_notifiers(info->nl_net,
1139 FIB_EVENT_ENTRY_REPLACE,
1140 rt, extack);
1141 if (err)
1142 return err;
1143
David Ahern93c2fb22018-04-18 15:38:59 -07001144 atomic_inc(&rt->fib6_ref);
1145 rcu_assign_pointer(rt->fib6_node, fn);
David Ahern33bd5ac2018-07-03 14:36:21 -07001146 rt->fib6_next = iter->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001147 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001148 if (!info->skip_notify)
1149 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001150 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001151 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1152 fn->fn_flags |= RTN_RTINFO;
1153 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001154 nsiblings = iter->fib6_nsiblings;
1155 iter->fib6_node = NULL;
1156 fib6_purge_rt(iter, fn, info->nl_net);
1157 if (rcu_access_pointer(fn->rr_ptr) == iter)
David Ahernf34436a2018-05-21 10:26:53 -07001158 fn->rr_ptr = NULL;
David Ahern33bd5ac2018-07-03 14:36:21 -07001159 fib6_info_release(iter);
1160
1161 if (nsiblings) {
1162 /* Replacing an ECMP route, remove all siblings */
1163 ins = &rt->fib6_next;
1164 iter = rcu_dereference_protected(*ins,
1165 lockdep_is_held(&rt->fib6_table->tb6_lock));
1166 while (iter) {
1167 if (iter->fib6_metric > rt->fib6_metric)
1168 break;
1169 if (rt6_qualify_for_ecmp(iter)) {
1170 *ins = iter->fib6_next;
1171 iter->fib6_node = NULL;
1172 fib6_purge_rt(iter, fn, info->nl_net);
1173 if (rcu_access_pointer(fn->rr_ptr) == iter)
1174 fn->rr_ptr = NULL;
1175 fib6_info_release(iter);
1176 nsiblings--;
1177 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1178 } else {
1179 ins = &iter->fib6_next;
1180 }
1181 iter = rcu_dereference_protected(*ins,
1182 lockdep_is_held(&rt->fib6_table->tb6_lock));
1183 }
1184 WARN_ON(nsiblings != 0);
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 return 0;
1189}
1190
David Ahern8d1c8022018-04-17 17:33:26 -07001191static void fib6_start_gc(struct net *net, struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001193 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001194 (rt->fib6_flags & RTF_EXPIRES))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001195 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001196 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001199void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001201 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1202 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001203 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
David Ahern8d1c8022018-04-17 17:33:26 -07001206static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001207 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001208{
David Ahern93c2fb22018-04-18 15:38:59 -07001209 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1210 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001211
1212 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1213 smp_wmb();
1214 while (fn) {
1215 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001216 fn = rcu_dereference_protected(fn->parent,
David Ahern93c2fb22018-04-18 15:38:59 -07001217 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001218 }
1219}
1220
David Ahern8d1c8022018-04-17 17:33:26 -07001221void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001222{
1223 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/*
1227 * Add routing information to the routing tree.
1228 * <destination addr>/<source addr>
1229 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001230 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
1232
David Ahern8d1c8022018-04-17 17:33:26 -07001233int fib6_add(struct fib6_node *root, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001234 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
David Ahern93c2fb22018-04-18 15:38:59 -07001236 struct fib6_table *table = rt->fib6_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001237 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001239 int allow_create = 1;
1240 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001241 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001242
1243 if (info->nlh) {
1244 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001245 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001246 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001247 replace_required = 1;
1248 }
1249 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001250 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Wei Wang81eb8442017-10-06 12:06:11 -07001252 fn = fib6_add_1(info->nl_net, table, root,
David Ahern93c2fb22018-04-18 15:38:59 -07001253 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1254 offsetof(struct fib6_info, fib6_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001255 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001256 if (IS_ERR(fn)) {
1257 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001258 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001262 pn = fn;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07001265 if (rt->fib6_src.plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct fib6_node *sn;
1267
Wei Wang66f5d6c2017-10-06 12:06:10 -07001268 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct fib6_node *sfn;
1270
1271 /*
1272 * Create subtree.
1273 *
1274 * fn[main tree]
1275 * |
1276 * sfn[subtree root]
1277 * \
1278 * sn[new leaf node]
1279 */
1280
1281 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001282 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001283 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001284 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
David Ahern93c2fb22018-04-18 15:38:59 -07001286 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001287 rcu_assign_pointer(sfn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001288 info->nl_net->ipv6.fib6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* Now add the first leaf node to new subtree */
1292
Wei Wang81eb8442017-10-06 12:06:11 -07001293 sn = fib6_add_1(info->nl_net, table, sfn,
David Ahern93c2fb22018-04-18 15:38:59 -07001294 &rt->fib6_src.addr, rt->fib6_src.plen,
1295 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001296 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001298 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001300 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 in main tree.
1302 */
Wei Wang81eb8442017-10-06 12:06:11 -07001303 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001304 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001305 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001309 rcu_assign_pointer(sfn->parent, fn);
1310 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001312 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
David Ahern93c2fb22018-04-18 15:38:59 -07001313 &rt->fib6_src.addr, rt->fib6_src.plen,
1314 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001315 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001317 if (IS_ERR(sn)) {
1318 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001319 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
Wei Wang66f5d6c2017-10-06 12:06:10 -07001323 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001324 if (fn->fn_flags & RTN_TL_ROOT) {
1325 /* put back null_entry for root node */
1326 rcu_assign_pointer(fn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001327 info->nl_net->ipv6.fib6_null_entry);
Wei Wang591ff9e2018-01-18 10:40:03 -08001328 } else {
David Ahern93c2fb22018-04-18 15:38:59 -07001329 atomic_inc(&rt->fib6_ref);
Wei Wang591ff9e2018-01-18 10:40:03 -08001330 rcu_assign_pointer(fn->leaf, rt);
1331 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 fn = sn;
1334 }
1335#endif
1336
David Ahernd4ead6b2018-04-17 17:33:16 -07001337 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001338 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001339 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001340 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001344 if (err) {
1345#ifdef CONFIG_IPV6_SUBTREES
1346 /*
1347 * If fib6_add_1 has cleared the old leaf pointer in the
1348 * super-tree leaf node we have to find a new one for it.
1349 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001350 if (pn != fn) {
David Ahern8d1c8022018-04-17 17:33:26 -07001351 struct fib6_info *pn_leaf =
Wei Wang7bbfe002018-01-03 14:11:59 -08001352 rcu_dereference_protected(pn->leaf,
1353 lockdep_is_held(&table->tb6_lock));
1354 if (pn_leaf == rt) {
1355 pn_leaf = NULL;
1356 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001357 fib6_info_release(rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001358 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001359 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1360 pn_leaf = fib6_find_prefix(info->nl_net, table,
1361 pn);
1362#if RT6_DEBUG >= 2
1363 if (!pn_leaf) {
1364 WARN_ON(!pn_leaf);
1365 pn_leaf =
David Ahern421842e2018-04-17 17:33:18 -07001366 info->nl_net->ipv6.fib6_null_entry;
Wei Wang7bbfe002018-01-03 14:11:59 -08001367 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001368#endif
David Ahern93531c62018-04-17 17:33:25 -07001369 fib6_info_hold(pn_leaf);
Wei Wang7bbfe002018-01-03 14:11:59 -08001370 rcu_assign_pointer(pn->leaf, pn_leaf);
1371 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001372 }
1373#endif
Wei Wang348a4002017-08-18 17:14:49 -07001374 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return err;
1377
Wei Wang348a4002017-08-18 17:14:49 -07001378failure:
Wei Wang4512c432018-01-08 10:34:00 -08001379 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1380 * 1. fn is an intermediate node and we failed to add the new
1381 * route to it in both subtree creation failure and fib6_add_rt2node()
1382 * failure case.
1383 * 2. fn is the root node in the table and we fail to add the first
1384 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
Wei Wang4512c432018-01-08 10:34:00 -08001386 if (fn &&
1387 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1388 (fn->fn_flags & RTN_TL_ROOT &&
1389 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001390 fib6_repair_tree(info->nl_net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394/*
1395 * Routing tree lookup
1396 *
1397 */
1398
1399struct lookup_args {
David Ahern8d1c8022018-04-17 17:33:26 -07001400 int offset; /* key offset on fib6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001401 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402};
1403
David Ahern64547432018-05-09 20:34:19 -07001404static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1405 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001408 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001410 if (unlikely(args->offset == 0))
1411 return NULL;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * Descend on a tree
1415 */
1416
1417 fn = root;
1418
1419 for (;;) {
1420 struct fib6_node *next;
1421
1422 dir = addr_bit_set(args->addr, fn->fn_bit);
1423
Wei Wang66f5d6c2017-10-06 12:06:10 -07001424 next = dir ? rcu_dereference(fn->right) :
1425 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 if (next) {
1428 fn = next;
1429 continue;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
1432 }
1433
David S. Miller507c9b12011-12-03 17:50:45 -05001434 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001435 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1436
1437 if (subtree || fn->fn_flags & RTN_RTINFO) {
David Ahern8d1c8022018-04-17 17:33:26 -07001438 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct rt6key *key;
1440
Wei Wang8d1040e2017-10-06 12:06:08 -07001441 if (!leaf)
1442 goto backtrack;
1443
1444 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001446 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1447#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001448 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001449 struct fib6_node *sfn;
David Ahern64547432018-05-09 20:34:19 -07001450 sfn = fib6_node_lookup_1(subtree,
1451 args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001452 if (!sfn)
1453 goto backtrack;
1454 fn = sfn;
1455 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001456#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001457 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001458 return fn;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001461backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001462 if (fn->fn_flags & RTN_ROOT)
1463 break;
1464
Wei Wang66f5d6c2017-10-06 12:06:10 -07001465 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 return NULL;
1469}
1470
Wei Wang66f5d6c2017-10-06 12:06:10 -07001471/* called with rcu_read_lock() held
1472 */
David Ahern64547432018-05-09 20:34:19 -07001473struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1474 const struct in6_addr *daddr,
1475 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001478 struct lookup_args args[] = {
1479 {
David Ahern93c2fb22018-04-18 15:38:59 -07001480 .offset = offsetof(struct fib6_info, fib6_dst),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001481 .addr = daddr,
1482 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001484 {
David Ahern93c2fb22018-04-18 15:38:59 -07001485 .offset = offsetof(struct fib6_info, fib6_src),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001486 .addr = saddr,
1487 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001489 {
1490 .offset = 0, /* sentinel */
1491 }
1492 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
David Ahern64547432018-05-09 20:34:19 -07001494 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001495 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 fn = root;
1497
1498 return fn;
1499}
1500
1501/*
1502 * Get node with specified destination prefix (and source prefix,
1503 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001504 * exact_match == true means we try to find fn with exact match of
1505 * the passed in prefix addr
1506 * exact_match == false means we try to find fn with longest prefix
1507 * match of the passed in prefix addr. This is useful for finding fn
1508 * for cached route as it will be stored in the exception table under
1509 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
1511
1512
Wang Yufen437de072014-03-28 12:07:04 +08001513static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1514 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001515 int plen, int offset,
1516 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Wei Wang38fbeee2017-10-06 12:06:02 -07001518 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 for (fn = root; fn ; ) {
David Ahern8d1c8022018-04-17 17:33:26 -07001521 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001522 struct rt6key *key;
1523
1524 /* This node is being deleted */
1525 if (!leaf) {
1526 if (plen <= fn->fn_bit)
1527 goto out;
1528 else
1529 goto next;
1530 }
1531
1532 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 /*
1535 * Prefix match
1536 */
1537 if (plen < fn->fn_bit ||
1538 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001539 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (plen == fn->fn_bit)
1542 return fn;
1543
Wei Wang38fbeee2017-10-06 12:06:02 -07001544 prev = fn;
1545
Wei Wang8d1040e2017-10-06 12:06:08 -07001546next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * We have more bits to go
1549 */
1550 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001551 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001553 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001555out:
1556 if (exact_match)
1557 return NULL;
1558 else
1559 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Wang Yufen437de072014-03-28 12:07:04 +08001562struct fib6_node *fib6_locate(struct fib6_node *root,
1563 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001564 const struct in6_addr *saddr, int src_len,
1565 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct fib6_node *fn;
1568
1569 fn = fib6_locate_1(root, daddr, dst_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001570 offsetof(struct fib6_info, fib6_dst),
Wei Wang38fbeee2017-10-06 12:06:02 -07001571 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573#ifdef CONFIG_IPV6_SUBTREES
1574 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001575 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001576 if (fn) {
1577 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1578
1579 if (subtree) {
1580 fn = fib6_locate_1(subtree, saddr, src_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001581 offsetof(struct fib6_info, fib6_src),
Wei Wang38fbeee2017-10-06 12:06:02 -07001582 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001583 }
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586#endif
1587
David S. Miller507c9b12011-12-03 17:50:45 -05001588 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return fn;
1590
1591 return NULL;
1592}
1593
1594
1595/*
1596 * Deletion
1597 *
1598 */
1599
David Ahern8d1c8022018-04-17 17:33:26 -07001600static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001601 struct fib6_table *table,
1602 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001604 struct fib6_node *child_left, *child_right;
1605
David S. Miller507c9b12011-12-03 17:50:45 -05001606 if (fn->fn_flags & RTN_ROOT)
David Ahern421842e2018-04-17 17:33:18 -07001607 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
David S. Miller507c9b12011-12-03 17:50:45 -05001609 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001610 child_left = rcu_dereference_protected(fn->left,
1611 lockdep_is_held(&table->tb6_lock));
1612 child_right = rcu_dereference_protected(fn->right,
1613 lockdep_is_held(&table->tb6_lock));
1614 if (child_left)
1615 return rcu_dereference_protected(child_left->leaf,
1616 lockdep_is_held(&table->tb6_lock));
1617 if (child_right)
1618 return rcu_dereference_protected(child_right->leaf,
1619 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001621 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623 return NULL;
1624}
1625
1626/*
1627 * Called to trim the tree of intermediate nodes when possible. "fn"
1628 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001629 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
1631
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001632static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001633 struct fib6_table *table,
1634 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 int children;
1637 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001638 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001639 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int iter = 0;
1641
Wei Wang4512c432018-01-08 10:34:00 -08001642 /* Set fn->leaf to null_entry for root node. */
1643 if (fn->fn_flags & RTN_TL_ROOT) {
David Ahern421842e2018-04-17 17:33:18 -07001644 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
Wei Wang4512c432018-01-08 10:34:00 -08001645 return fn;
1646 }
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001649 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1650 lockdep_is_held(&table->tb6_lock));
1651 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1652 lockdep_is_held(&table->tb6_lock));
1653 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1654 lockdep_is_held(&table->tb6_lock));
1655 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1656 lockdep_is_held(&table->tb6_lock));
1657 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1658 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001659 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001660 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001661 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001662 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001663 struct fib6_info *new_fn_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1666 iter++;
1667
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001668 WARN_ON(fn->fn_flags & RTN_RTINFO);
1669 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001670 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 children = 0;
1673 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001674 if (fn_r)
1675 child = fn_r, children |= 1;
1676 if (fn_l)
1677 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001679 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680#ifdef CONFIG_IPV6_SUBTREES
1681 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001682 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683#endif
1684 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001685 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001687 if (!new_fn_leaf) {
1688 WARN_ON(!new_fn_leaf);
David Ahern421842e2018-04-17 17:33:18 -07001689 new_fn_leaf = net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691#endif
David Ahern93531c62018-04-17 17:33:25 -07001692 fib6_info_hold(new_fn_leaf);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001693 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1694 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001698 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001699 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001700 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 nstate = FWS_L;
1702 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001703 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001705 if (pn_r == fn)
1706 rcu_assign_pointer(pn->right, child);
1707 else if (pn_l == fn)
1708 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001710 else
1711 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#endif
1713 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001714 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 nstate = FWS_R;
1716#ifdef CONFIG_IPV6_SUBTREES
1717 }
1718#endif
1719
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001720 read_lock(&net->ipv6.fib6_walker_lock);
1721 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001722 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001723 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1725 w->node = pn;
1726 w->state = nstate;
1727 }
1728 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (w->node == fn) {
1730 w->node = child;
1731 if (children&2) {
1732 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001733 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 } else {
1735 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001736 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 }
1739 }
1740 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001741 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Wei Wang81eb8442017-10-06 12:06:11 -07001743 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001744 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return pn;
1746
Wei Wang66f5d6c2017-10-06 12:06:10 -07001747 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001748 fib6_info_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 fn = pn;
1750 }
1751}
1752
Wei Wang66f5d6c2017-10-06 12:06:10 -07001753static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
David Ahern8d1c8022018-04-17 17:33:26 -07001754 struct fib6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001756 struct fib6_walker *w;
David Ahern8d1c8022018-04-17 17:33:26 -07001757 struct fib6_info *rt = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001758 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001759 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 RT6_TRACE("fib6_del_route\n");
1762
1763 /* Unlink it */
David Ahern8fb11a92018-05-04 13:54:24 -07001764 *rtp = rt->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001765 rt->fib6_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001766 net->ipv6.rt6_stats->fib_rt_entries--;
1767 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Wei Wang2b760fc2017-10-06 12:06:03 -07001769 /* Flush all cached dst in exception table */
1770 rt6_flush_exceptions(rt);
1771
David S. Millerf11e6652007-03-24 20:36:25 -07001772 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001773 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001774 fn->rr_ptr = NULL;
1775
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001776 /* Remove this entry from other siblings */
David Ahern93c2fb22018-04-18 15:38:59 -07001777 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07001778 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001779
1780 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001781 &rt->fib6_siblings, fib6_siblings)
1782 sibling->fib6_nsiblings--;
1783 rt->fib6_nsiblings = 0;
1784 list_del_init(&rt->fib6_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001785 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001786 }
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001789 read_lock(&net->ipv6.fib6_walker_lock);
1790 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (w->state == FWS_C && w->leaf == rt) {
1792 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Ahern8fb11a92018-05-04 13:54:24 -07001793 w->leaf = rcu_dereference_protected(rt->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001794 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001795 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 w->state = FWS_U;
1797 }
1798 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001799 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Wei Wang4512c432018-01-08 10:34:00 -08001801 /* If it was last route, call fib6_repair_tree() to:
1802 * 1. For root node, put back null_entry as how the table was created.
1803 * 2. For other nodes, expunge its radix tree node.
1804 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001805 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001806 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1807 fn->fn_flags &= ~RTN_RTINFO;
1808 net->ipv6.rt6_stats->fib_route_nodes--;
1809 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001810 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001813 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
David Ahern6c31e5a2017-10-27 17:37:13 -07001815 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001816 if (!info->skip_notify)
1817 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
David Ahern93531c62018-04-17 17:33:25 -07001818 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Wei Wang66f5d6c2017-10-06 12:06:10 -07001821/* Need to own table->tb6_lock */
David Ahern8d1c8022018-04-17 17:33:26 -07001822int fib6_del(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
David Ahern93c2fb22018-04-18 15:38:59 -07001824 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1825 lockdep_is_held(&rt->fib6_table->tb6_lock));
1826 struct fib6_table *table = rt->fib6_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001827 struct net *net = info->nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07001828 struct fib6_info __rcu **rtp;
1829 struct fib6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
David Ahern421842e2018-04-17 17:33:18 -07001831 if (!fn || rt == net->ipv6.fib6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -ENOENT;
1833
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001834 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /*
1837 * Walk the leaf entries looking for ourself
1838 */
1839
Wei Wang66f5d6c2017-10-06 12:06:10 -07001840 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
David Ahern8d1c8022018-04-17 17:33:26 -07001841 struct fib6_info *cur = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001842 lockdep_is_held(&table->tb6_lock));
1843 if (rt == cur) {
1844 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return 0;
1846 }
David Ahern8fb11a92018-05-04 13:54:24 -07001847 rtp_next = &cur->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849 return -ENOENT;
1850}
1851
1852/*
1853 * Tree traversal function.
1854 *
1855 * Certainly, it is not interrupt safe.
1856 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1857 * It means, that we can modify tree during walking
1858 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001859 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 *
1861 * It guarantees that every node will be traversed,
1862 * and that it will be traversed only once.
1863 *
1864 * Callback function w->func may return:
1865 * 0 -> continue walking.
1866 * positive value -> walking is suspended (used by tree dumps,
1867 * and probably by gc, if it will be split to several slices)
1868 * negative value -> terminate walking.
1869 *
1870 * The function itself returns:
1871 * 0 -> walk is complete.
1872 * >0 -> walk is incomplete (i.e. suspended)
1873 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001874 *
1875 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
1877
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001878static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001880 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Wei Wang2b760fc2017-10-06 12:06:03 -07001882 /* w->root should always be table->tb6_root */
1883 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 for (;;) {
1886 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001887 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 switch (w->state) {
1891#ifdef CONFIG_IPV6_SUBTREES
1892 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001893 if (FIB6_SUBTREE(fn)) {
1894 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 continue;
1896 }
1897 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001898#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001899 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001901 left = rcu_dereference_protected(fn->left, 1);
1902 if (left) {
1903 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 w->state = FWS_INIT;
1905 continue;
1906 }
1907 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001908 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001910 right = rcu_dereference_protected(fn->right, 1);
1911 if (right) {
1912 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 w->state = FWS_INIT;
1914 continue;
1915 }
1916 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001917 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001918 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001920 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001921 int err;
1922
Eric Dumazetfa809e22012-06-25 15:37:19 -07001923 if (w->skip) {
1924 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001925 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001926 }
1927
1928 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (err)
1930 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001931
1932 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 continue;
1934 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001935skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001937 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 case FWS_U:
1939 if (fn == w->root)
1940 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001941 pn = rcu_dereference_protected(fn->parent, 1);
1942 left = rcu_dereference_protected(pn->left, 1);
1943 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 w->node = pn;
1945#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001946 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001947 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 w->state = FWS_L;
1949 continue;
1950 }
1951#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001952 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 w->state = FWS_R;
1954 continue;
1955 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001956 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001958 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 continue;
1960 }
1961#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001962 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963#endif
1964 }
1965 }
1966}
1967
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001968static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 int res;
1971
1972 w->state = FWS_INIT;
1973 w->node = w->root;
1974
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001975 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 res = fib6_walk_continue(w);
1977 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001978 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return res;
1980}
1981
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001982static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 int res;
David Ahern8d1c8022018-04-17 17:33:26 -07001985 struct fib6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001986 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001987 struct nl_info info = {
1988 .nl_net = c->net,
David Ahern7c6bb7d2018-10-11 20:17:21 -07001989 .skip_notify = c->skip_notify,
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001990 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001992 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1993 w->node->fn_sernum != c->sernum)
1994 w->node->fn_sernum = c->sernum;
1995
1996 if (!c->func) {
1997 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1998 w->leaf = NULL;
1999 return 0;
2000 }
2001
Wei Wang66f5d6c2017-10-06 12:06:10 -07002002 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002004 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002006 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (res) {
2008#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00002009 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07002010 __func__, rt,
David Ahern93c2fb22018-04-18 15:38:59 -07002011 rcu_access_pointer(rt->fib6_node),
Wei Wang4e587ea2017-08-25 15:03:10 -07002012 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013#endif
2014 continue;
2015 }
2016 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002017 } else if (res == -2) {
David Ahern93c2fb22018-04-18 15:38:59 -07002018 if (WARN_ON(!rt->fib6_nsiblings))
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002019 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002020 rt = list_last_entry(&rt->fib6_siblings,
2021 struct fib6_info, fib6_siblings);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002022 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002024 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 w->leaf = rt;
2027 return 0;
2028}
2029
2030/*
2031 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002032 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002034 * It may return -2 -> skip multipath route.
2035 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002039static void fib6_clean_tree(struct net *net, struct fib6_node *root,
David Ahern8d1c8022018-04-17 17:33:26 -07002040 int (*func)(struct fib6_info *, void *arg),
David Ahern7c6bb7d2018-10-11 20:17:21 -07002041 int sernum, void *arg, bool skip_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002043 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 c.w.root = root;
2046 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00002047 c.w.count = 0;
2048 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002050 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002052 c.net = net;
David Ahern7c6bb7d2018-10-11 20:17:21 -07002053 c.skip_notify = skip_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002055 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002058static void __fib6_clean_all(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07002059 int (*func)(struct fib6_info *, void *),
David Ahern7c6bb7d2018-10-11 20:17:21 -07002060 int sernum, void *arg, bool skip_notify)
Thomas Grafc71099a2006-08-04 23:20:06 -07002061{
Thomas Grafc71099a2006-08-04 23:20:06 -07002062 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002063 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07002064 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07002065
Patrick McHardy1b43af52006-08-10 23:11:17 -07002066 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07002067 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002068 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002069 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002070 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002071 fib6_clean_tree(net, &table->tb6_root,
David Ahern7c6bb7d2018-10-11 20:17:21 -07002072 func, sernum, arg, skip_notify);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002073 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07002074 }
2075 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07002076 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07002077}
2078
David Ahern8d1c8022018-04-17 17:33:26 -07002079void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002080 void *arg)
2081{
David Ahern7c6bb7d2018-10-11 20:17:21 -07002082 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2083}
2084
2085void fib6_clean_all_skip_notify(struct net *net,
2086 int (*func)(struct fib6_info *, void *),
2087 void *arg)
2088{
2089 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002090}
2091
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002092static void fib6_flush_trees(struct net *net)
2093{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002094 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002095
David Ahern7c6bb7d2018-10-11 20:17:21 -07002096 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099/*
2100 * Garbage collection
2101 */
2102
David Ahern8d1c8022018-04-17 17:33:26 -07002103static int fib6_age(struct fib6_info *rt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Michal Kubeček3570df92016-03-08 14:44:25 +01002105 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 unsigned long now = jiffies;
2107
2108 /*
2109 * check addrconf expiration here.
2110 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 */
2112
David Ahern93c2fb22018-04-18 15:38:59 -07002113 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
David Ahern14895682018-04-17 17:33:17 -07002114 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return -1;
2117 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002118 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
Wei Wangc757faa2017-10-06 12:06:01 -07002121 /* Also age clones in the exception table.
2122 * Note, that clones are aged out
2123 * only if they are not in use now.
2124 */
2125 rt6_age_exceptions(rt, gc_args, now);
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return 0;
2128}
2129
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002130void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Michal Kubeček3570df92016-03-08 14:44:25 +01002132 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002133 unsigned long now;
2134
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002135 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002136 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2137 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002138 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002141 gc_args.timeout = expires ? (int)expires :
2142 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002143 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002144
Michal Kubeček3570df92016-03-08 14:44:25 +01002145 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002146 now = jiffies;
2147 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002150 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002151 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002152 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002153 else
2154 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002155 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
Kees Cook86cb30e2017-10-17 20:21:24 -07002158static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002159{
Kees Cook86cb30e2017-10-17 20:21:24 -07002160 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2161
2162 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002163}
2164
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002165static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002166{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002167 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002168 int err;
2169
2170 err = fib6_notifier_init(net);
2171 if (err)
2172 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002173
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002174 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002175 rwlock_init(&net->ipv6.fib6_walker_lock);
2176 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002177 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002178
Benjamin Theryc5728722008-03-03 23:34:17 -08002179 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2180 if (!net->ipv6.rt6_stats)
2181 goto out_timer;
2182
Eric Dumazet10da66f2010-10-13 08:22:03 +00002183 /* Avoid false sharing : Use at least a full cache line */
2184 size = max_t(size_t, size, L1_CACHE_BYTES);
2185
2186 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002187 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002188 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002189
2190 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2191 GFP_KERNEL);
2192 if (!net->ipv6.fib6_main_tbl)
2193 goto out_fib_table_hash;
2194
2195 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002196 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002197 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002198 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2199 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002200 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002201
2202#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2203 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2204 GFP_KERNEL);
2205 if (!net->ipv6.fib6_local_tbl)
2206 goto out_fib6_main_tbl;
2207 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002208 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002209 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002210 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2211 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002212 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002213#endif
2214 fib6_tables_init(net);
2215
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002216 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002217
2218#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2219out_fib6_main_tbl:
2220 kfree(net->ipv6.fib6_main_tbl);
2221#endif
2222out_fib_table_hash:
2223 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002224out_rt6_stats:
2225 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002226out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002227 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002228 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002229}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002230
2231static void fib6_net_exit(struct net *net)
2232{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002233 unsigned int i;
2234
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002235 del_timer_sync(&net->ipv6.ip6_fib_timer);
2236
Eric Dumazet32a805b2017-09-08 15:48:47 -07002237 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002238 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2239 struct hlist_node *tmp;
2240 struct fib6_table *tb;
2241
2242 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2243 hlist_del(&tb->tb6_hlist);
2244 fib6_free_table(tb);
2245 }
2246 }
2247
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002248 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002249 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002250 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002251}
2252
2253static struct pernet_operations fib6_net_ops = {
2254 .init = fib6_net_init,
2255 .exit = fib6_net_exit,
2256};
2257
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002258int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002260 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2263 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002264 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002265 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002266 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002267 goto out;
2268
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002269 ret = register_pernet_subsys(&fib6_net_ops);
2270 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002271 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002272
Florian Westphal16feebc2017-12-02 21:44:08 +01002273 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2274 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002275 if (ret)
2276 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002277
2278 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002279out:
2280 return ret;
2281
David S. Millere8803b62012-06-16 01:12:19 -07002282out_unregister_subsys:
2283 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002284out_kmem_cache_create:
2285 kmem_cache_destroy(fib6_node_kmem);
2286 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
2289void fib6_gc_cleanup(void)
2290{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002291 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 kmem_cache_destroy(fib6_node_kmem);
2293}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002294
2295#ifdef CONFIG_PROC_FS
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002296static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2297{
David Ahern8d1c8022018-04-17 17:33:26 -07002298 struct fib6_info *rt = v;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002299 struct ipv6_route_iter *iter = seq->private;
David Ahern5e670d82018-04-17 17:33:14 -07002300 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002301
David Ahern93c2fb22018-04-18 15:38:59 -07002302 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002303
2304#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002305 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002306#else
2307 seq_puts(seq, "00000000000000000000000000000000 00 ");
2308#endif
David Ahern93c2fb22018-04-18 15:38:59 -07002309 if (rt->fib6_flags & RTF_GATEWAY)
David Ahern5e670d82018-04-17 17:33:14 -07002310 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002311 else
2312 seq_puts(seq, "00000000000000000000000000000000");
2313
David Ahern5e670d82018-04-17 17:33:14 -07002314 dev = rt->fib6_nh.nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002315 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
David Ahern93c2fb22018-04-18 15:38:59 -07002316 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2317 rt->fib6_flags, dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002318 iter->w.leaf = NULL;
2319 return 0;
2320}
2321
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002322static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002323{
2324 struct ipv6_route_iter *iter = w->args;
2325
2326 if (!iter->skip)
2327 return 1;
2328
2329 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002330 iter->w.leaf = rcu_dereference_protected(
David Ahern8fb11a92018-05-04 13:54:24 -07002331 iter->w.leaf->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002332 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002333 iter->skip--;
2334 if (!iter->skip && iter->w.leaf)
2335 return 1;
2336 } while (iter->w.leaf);
2337
2338 return 0;
2339}
2340
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002341static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2342 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002343{
2344 memset(&iter->w, 0, sizeof(iter->w));
2345 iter->w.func = ipv6_route_yield;
2346 iter->w.root = &iter->tbl->tb6_root;
2347 iter->w.state = FWS_INIT;
2348 iter->w.node = iter->w.root;
2349 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002350 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002351 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002352 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002353}
2354
2355static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2356 struct net *net)
2357{
2358 unsigned int h;
2359 struct hlist_node *node;
2360
2361 if (tbl) {
2362 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2363 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2364 } else {
2365 h = 0;
2366 node = NULL;
2367 }
2368
2369 while (!node && h < FIB6_TABLE_HASHSZ) {
2370 node = rcu_dereference_bh(
2371 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2372 }
2373 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2374}
2375
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002376static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2377{
2378 if (iter->sernum != iter->w.root->fn_sernum) {
2379 iter->sernum = iter->w.root->fn_sernum;
2380 iter->w.state = FWS_INIT;
2381 iter->w.node = iter->w.root;
2382 WARN_ON(iter->w.skip);
2383 iter->w.skip = iter->w.count;
2384 }
2385}
2386
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002387static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2388{
2389 int r;
David Ahern8d1c8022018-04-17 17:33:26 -07002390 struct fib6_info *n;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002391 struct net *net = seq_file_net(seq);
2392 struct ipv6_route_iter *iter = seq->private;
2393
2394 if (!v)
2395 goto iter_table;
2396
David Ahern8fb11a92018-05-04 13:54:24 -07002397 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002398 if (n) {
2399 ++*pos;
2400 return n;
2401 }
2402
2403iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002404 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002405 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002406 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002407 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002408 if (r > 0) {
2409 if (v)
2410 ++*pos;
2411 return iter->w.leaf;
2412 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002413 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002414 return NULL;
2415 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002416 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002417
2418 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2419 if (!iter->tbl)
2420 return NULL;
2421
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002422 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002423 goto iter_table;
2424}
2425
2426static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2427 __acquires(RCU_BH)
2428{
2429 struct net *net = seq_file_net(seq);
2430 struct ipv6_route_iter *iter = seq->private;
2431
2432 rcu_read_lock_bh();
2433 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2434 iter->skip = *pos;
2435
2436 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002437 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002438 return ipv6_route_seq_next(seq, NULL, pos);
2439 } else {
2440 return NULL;
2441 }
2442}
2443
2444static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2445{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002446 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002447 return w->node && !(w->state == FWS_U && w->node == w->root);
2448}
2449
2450static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2451 __releases(RCU_BH)
2452{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002453 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002454 struct ipv6_route_iter *iter = seq->private;
2455
2456 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002457 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002458
2459 rcu_read_unlock_bh();
2460}
2461
Christoph Hellwigc3506372018-04-10 19:42:55 +02002462const struct seq_operations ipv6_route_seq_ops = {
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002463 .start = ipv6_route_seq_start,
2464 .next = ipv6_route_seq_next,
2465 .stop = ipv6_route_seq_stop,
2466 .show = ipv6_route_seq_show
2467};
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002468#endif /* CONFIG_PROC_FS */