blob: 008421b550c6bfd449665aa5e7ba5505fcabe53d [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);
Eric Dumazetf05713e2019-04-22 18:35:03 -0700165 refcount_set(&f6i->fib6_ref, 1);
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);
Eric Dumazetb0270552019-04-22 18:35:01 -0700178 kfree(bucket);
David Aherna64efe142018-04-17 17:33:24 -0700179
180 if (f6i->rt6i_pcpu) {
181 int cpu;
182
183 for_each_possible_cpu(cpu) {
184 struct rt6_info **ppcpu_rt;
185 struct rt6_info *pcpu_rt;
186
187 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
188 pcpu_rt = *ppcpu_rt;
189 if (pcpu_rt) {
190 dst_dev_put(&pcpu_rt->dst);
191 dst_release(&pcpu_rt->dst);
192 *ppcpu_rt = NULL;
193 }
194 }
Mike Rapoport7abab7b2018-10-09 07:02:01 +0300195
196 free_percpu(f6i->rt6i_pcpu);
David Aherna64efe142018-04-17 17:33:24 -0700197 }
198
David Aherndac7d0f2019-03-27 20:53:51 -0700199 fib6_nh_release(&f6i->fib6_nh);
David Aherna64efe142018-04-17 17:33:24 -0700200
David Aherncc5f0eb2018-10-04 20:07:52 -0700201 ip_fib_metrics_put(f6i->fib6_metrics);
David Ahern93531c62018-04-17 17:33:25 -0700202
David Aherna64efe142018-04-17 17:33:24 -0700203 kfree(f6i);
204}
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700205EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
David Aherna64efe142018-04-17 17:33:24 -0700206
Wei Wang81eb8442017-10-06 12:06:11 -0700207static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct fib6_node *fn;
210
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800211 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700212 if (fn)
213 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 return fn;
216}
217
Wei Wang81eb8442017-10-06 12:06:11 -0700218static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700221 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Wei Wangc5cff852017-08-21 09:47:10 -0700224static void node_free_rcu(struct rcu_head *head)
225{
226 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
227
228 kmem_cache_free(fib6_node_kmem, fn);
229}
230
Wei Wang81eb8442017-10-06 12:06:11 -0700231static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700232{
233 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700234 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700235}
236
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200237static void fib6_free_table(struct fib6_table *table)
238{
239 inetpeer_invalidate_tree(&table->tb6_peers);
240 kfree(table);
241}
242
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800243static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700244{
245 unsigned int h;
246
Thomas Graf375216a2006-10-21 20:20:54 -0700247 /*
248 * Initialize table lock at a single place to give lockdep a key,
249 * tables aren't visible prior to being linked to the list.
250 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700251 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700252 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700253
254 /*
255 * No protection necessary, this is the only list mutatation
256 * operation, tables never disappear once they exist.
257 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700259}
260
261#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800262
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800263static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700264{
265 struct fib6_table *table;
266
267 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500268 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700269 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700270 rcu_assign_pointer(table->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -0700271 net->ipv6.fib6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700272 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700273 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700274 }
275
276 return table;
277}
278
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800279struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700280{
281 struct fib6_table *tb;
282
283 if (id == 0)
284 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700286 if (tb)
287 return tb;
288
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800289 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500290 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800291 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700292
293 return tb;
294}
David Ahernb3b46632016-05-04 21:46:12 -0700295EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700296
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800297struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700298{
299 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800300 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700301 unsigned int h;
302
303 if (id == 0)
304 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700305 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700306 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800307 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800308 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700309 if (tb->tb6_id == id) {
310 rcu_read_unlock();
311 return tb;
312 }
313 }
314 rcu_read_unlock();
315
316 return NULL;
317}
David Ahernc4850682015-10-12 11:47:08 -0700318EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700319
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000320static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700321{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800322 fib6_link_table(net, net->ipv6.fib6_main_tbl);
323 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700324}
Thomas Grafc71099a2006-08-04 23:20:06 -0700325#else
326
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800327struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700328{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800329 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700330}
331
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800332struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700333{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800334 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700335}
336
David S. Miller4c9483b2011-03-12 16:22:43 -0500337struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -0800338 const struct sk_buff *skb,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800339 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700340{
lucienab997ad2015-10-23 15:36:53 +0800341 struct rt6_info *rt;
342
David Ahernb75cc8f2018-03-02 08:32:17 -0800343 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300344 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800345 ip6_rt_put(rt);
346 rt = net->ipv6.ip6_null_entry;
347 dst_hold(&rt->dst);
348 }
349
350 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700351}
352
David Ahern138118e2018-05-09 20:34:23 -0700353/* called with rcu lock held; no reference taken on fib6_info */
David Aherneffda4d2019-04-16 14:36:10 -0700354int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
355 struct fib6_result *res, int flags)
David Ahern138118e2018-05-09 20:34:23 -0700356{
David Aherneffda4d2019-04-16 14:36:10 -0700357 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
358 res, flags);
David Ahern138118e2018-05-09 20:34:23 -0700359}
360
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000361static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700362{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800363 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700364}
365
366#endif
367
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200368unsigned int fib6_tables_seq_read(struct net *net)
369{
370 unsigned int h, fib_seq = 0;
371
372 rcu_read_lock();
373 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
374 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
375 struct fib6_table *tb;
376
Wei Wang66f5d6c2017-10-06 12:06:10 -0700377 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200378 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200379 }
380 rcu_read_unlock();
381
382 return fib_seq;
383}
384
385static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
386 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700387 struct fib6_info *rt)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200388{
389 struct fib6_entry_notifier_info info = {
390 .rt = rt,
391 };
392
393 return call_fib6_notifier(nb, net, event_type, &info.info);
394}
395
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200396static int call_fib6_entry_notifiers(struct net *net,
397 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700398 struct fib6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700399 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200400{
401 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700402 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200403 .rt = rt,
404 };
405
David Ahern93c2fb22018-04-18 15:38:59 -0700406 rt->fib6_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200407 return call_fib6_notifiers(net, event_type, &info.info);
408}
409
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200410struct fib6_dump_arg {
411 struct net *net;
412 struct notifier_block *nb;
413};
414
David Ahern8d1c8022018-04-17 17:33:26 -0700415static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200416{
David Ahern421842e2018-04-17 17:33:18 -0700417 if (rt == arg->net->ipv6.fib6_null_entry)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200418 return;
419 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
420}
421
422static int fib6_node_dump(struct fib6_walker *w)
423{
David Ahern8d1c8022018-04-17 17:33:26 -0700424 struct fib6_info *rt;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200425
Wei Wang66f5d6c2017-10-06 12:06:10 -0700426 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200427 fib6_rt_dump(rt, w->args);
428 w->leaf = NULL;
429 return 0;
430}
431
432static void fib6_table_dump(struct net *net, struct fib6_table *tb,
433 struct fib6_walker *w)
434{
435 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700436 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200437 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700438 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200439}
440
441/* Called with rcu_read_lock() */
442int fib6_tables_dump(struct net *net, struct notifier_block *nb)
443{
444 struct fib6_dump_arg arg;
445 struct fib6_walker *w;
446 unsigned int h;
447
448 w = kzalloc(sizeof(*w), GFP_ATOMIC);
449 if (!w)
450 return -ENOMEM;
451
452 w->func = fib6_node_dump;
453 arg.net = net;
454 arg.nb = nb;
455 w->args = &arg;
456
457 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
458 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
459 struct fib6_table *tb;
460
461 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
462 fib6_table_dump(net, tb, w);
463 }
464
465 kfree(w);
466
467 return 0;
468}
469
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200470static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700471{
472 int res;
David Ahern8d1c8022018-04-17 17:33:26 -0700473 struct fib6_info *rt;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700474
Wei Wang66f5d6c2017-10-06 12:06:10 -0700475 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700476 res = rt6_dump_route(rt, w->args);
477 if (res < 0) {
478 /* Frame is full, suspend walking */
479 w->leaf = rt;
480 return 1;
481 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800482
483 /* Multipath routes are dumped in one route with the
484 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
485 * last sibling of this route (no need to dump the
486 * sibling routes again)
487 */
David Ahern93c2fb22018-04-18 15:38:59 -0700488 if (rt->fib6_nsiblings)
489 rt = list_last_entry(&rt->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -0700490 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -0700491 fib6_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700492 }
493 w->leaf = NULL;
494 return 0;
495}
496
497static void fib6_dump_end(struct netlink_callback *cb)
498{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100499 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200500 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700501
502 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800503 if (cb->args[4]) {
504 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100505 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800506 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507 cb->args[2] = 0;
508 kfree(w);
509 }
Wang Yufen437de072014-03-28 12:07:04 +0800510 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700511 cb->args[1] = 3;
512}
513
514static int fib6_dump_done(struct netlink_callback *cb)
515{
516 fib6_dump_end(cb);
517 return cb->done ? cb->done(cb) : 0;
518}
519
520static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
521 struct netlink_callback *cb)
522{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100523 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200524 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700525 int res;
526
527 w = (void *)cb->args[2];
528 w->root = &table->tb6_root;
529
530 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000531 w->count = 0;
532 w->skip = 0;
533
Wei Wang66f5d6c2017-10-06 12:06:10 -0700534 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100535 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700536 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000537 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700538 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000539 cb->args[5] = w->root->fn_sernum;
540 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700541 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000542 if (cb->args[5] != w->root->fn_sernum) {
543 /* Begin at the root if the tree changed */
544 cb->args[5] = w->root->fn_sernum;
545 w->state = FWS_INIT;
546 w->node = w->root;
547 w->skip = w->count;
548 } else
549 w->skip = 0;
550
Wei Wang66f5d6c2017-10-06 12:06:10 -0700551 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700552 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700553 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800554 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100555 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800556 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700557 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700558 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800559
Patrick McHardy1b43af52006-08-10 23:11:17 -0700560 return res;
561}
562
Thomas Grafc127ea22007-03-22 11:58:32 -0700563static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700564{
David Aherne8ba3302018-10-07 20:16:35 -0700565 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900566 struct net *net = sock_net(skb->sk);
David Ahern47246762018-10-15 18:56:42 -0700567 struct rt6_rtnl_dump_arg arg = {};
Patrick McHardy1b43af52006-08-10 23:11:17 -0700568 unsigned int h, s_h;
569 unsigned int e = 0, s_e;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200570 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700571 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800572 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700573 int res = 0;
574
David Aherne8ba3302018-10-07 20:16:35 -0700575 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -0700576 int err;
David Aherne8ba3302018-10-07 20:16:35 -0700577
David Aherneffe6792018-10-15 18:56:48 -0700578 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -0700579 if (err < 0)
580 return err;
David Ahern13e38902018-10-15 18:56:44 -0700581 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
582 struct rtmsg *rtm = nlmsg_data(nlh);
David Aherne8ba3302018-10-07 20:16:35 -0700583
David Ahern08e814c2018-10-15 18:56:50 -0700584 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
David Ahern13e38902018-10-15 18:56:44 -0700585 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700586
David Ahern08e814c2018-10-15 18:56:50 -0700587 /* fib entries are never clones */
588 if (arg.filter.flags & RTM_F_CLONED)
Alexey Kodaneve22d0bf2018-11-02 19:11:05 +0300589 goto out;
David Ahern08e814c2018-10-15 18:56:50 -0700590
Patrick McHardy1b43af52006-08-10 23:11:17 -0700591 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500592 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700593 /* New dump:
594 *
595 * 1. hook callback destructor.
596 */
597 cb->args[3] = (long)cb->done;
598 cb->done = fib6_dump_done;
599
600 /*
601 * 2. allocate and initialize walker.
602 */
603 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500604 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700605 return -ENOMEM;
606 w->func = fib6_dump_node;
607 cb->args[2] = (long)w;
608 }
609
610 arg.skb = skb;
611 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700612 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700613 w->args = &arg;
614
David Ahern13e38902018-10-15 18:56:44 -0700615 if (arg.filter.table_id) {
616 tb = fib6_get_table(net, arg.filter.table_id);
617 if (!tb) {
David Ahernae677bb2018-10-24 12:59:01 -0700618 if (arg.filter.dump_all_families)
Alexey Kodaneve22d0bf2018-11-02 19:11:05 +0300619 goto out;
David Ahernae677bb2018-10-24 12:59:01 -0700620
David Ahern13e38902018-10-15 18:56:44 -0700621 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
622 return -ENOENT;
623 }
624
David Ahern73155872019-01-02 18:26:13 -0800625 if (!cb->args[0]) {
626 res = fib6_dump_table(tb, skb, cb);
627 if (!res)
628 cb->args[0] = 1;
629 }
David Ahern13e38902018-10-15 18:56:44 -0700630 goto out;
631 }
632
633 s_h = cb->args[0];
634 s_e = cb->args[1];
635
Eric Dumazete67f88d2011-04-27 22:56:07 +0000636 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700637 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700638 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800639 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800640 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700641 if (e < s_e)
642 goto next;
643 res = fib6_dump_table(tb, skb, cb);
644 if (res != 0)
David Ahern13e38902018-10-15 18:56:44 -0700645 goto out_unlock;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700646next:
647 e++;
648 }
649 }
David Ahern13e38902018-10-15 18:56:44 -0700650out_unlock:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000651 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700652 cb->args[1] = e;
653 cb->args[0] = h;
David Ahern13e38902018-10-15 18:56:44 -0700654out:
Patrick McHardy1b43af52006-08-10 23:11:17 -0700655 res = res < 0 ? res : skb->len;
656 if (res <= 0)
657 fib6_dump_end(cb);
658 return res;
659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Ahern8d1c8022018-04-17 17:33:26 -0700661void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
David Ahernd4ead6b2018-04-17 17:33:16 -0700662{
663 if (!f6i)
664 return;
665
666 if (f6i->fib6_metrics == &dst_default_metrics) {
667 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
668
669 if (!p)
670 return;
671
672 refcount_set(&p->refcnt, 1);
673 f6i->fib6_metrics = p;
674 }
675
676 f6i->fib6_metrics->metrics[metric - 1] = val;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*
680 * Routing Table
681 *
682 * return the appropriate node for a routing tree "add" operation
683 * by either creating and inserting or by returning an existing
684 * node.
685 */
686
Wei Wang81eb8442017-10-06 12:06:11 -0700687static struct fib6_node *fib6_add_1(struct net *net,
688 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700689 struct fib6_node *root,
690 struct in6_addr *addr, int plen,
691 int offset, int allow_create,
692 int replace_required,
693 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 struct fib6_node *fn, *in, *ln;
696 struct fib6_node *pn = NULL;
697 struct rt6key *key;
698 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900699 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 RT6_TRACE("fib6_add_1\n");
702
703 /* insert node in tree */
704
705 fn = root;
706
707 do {
David Ahern8d1c8022018-04-17 17:33:26 -0700708 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700709 lockdep_is_held(&table->tb6_lock));
710 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /*
713 * Prefix match
714 */
715 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000716 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000717 if (!allow_create) {
718 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600719 NL_SET_ERR_MSG(extack,
720 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000721 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000722 return ERR_PTR(-ENOENT);
723 }
Joe Perchesf3213832012-05-15 14:11:53 +0000724 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000727 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * Exact match ?
731 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (plen == fn->fn_bit) {
734 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500735 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700736 RCU_INIT_POINTER(fn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700737 fib6_info_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800738 /* remove null_entry in the root node */
739 } else if (fn->fn_flags & RTN_TL_ROOT &&
740 rcu_access_pointer(fn->leaf) ==
David Ahern421842e2018-04-17 17:33:18 -0700741 net->ipv6.fib6_null_entry) {
Wei Wang4512c432018-01-08 10:34:00 -0800742 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return fn;
746 }
747
748 /*
749 * We have more bits to go
750 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 dir = addr_bit_set(addr, fn->fn_bit);
754 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700755 fn = dir ?
756 rcu_dereference_protected(fn->right,
757 lockdep_is_held(&table->tb6_lock)) :
758 rcu_dereference_protected(fn->left,
759 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } while (fn);
761
Matti Vaittinen14df0152011-11-16 21:18:02 +0000762 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000763 /* We should not create new node because
764 * NLM_F_REPLACE was specified without NLM_F_CREATE
765 * I assume it is safe to require NLM_F_CREATE when
766 * REPLACE flag is used! Later we may want to remove the
767 * check for replace_required, because according
768 * to netlink specification, NLM_F_CREATE
769 * MUST be specified if new route is created.
770 * That would keep IPv6 consistent with IPv4
771 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000772 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600773 NL_SET_ERR_MSG(extack,
774 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000775 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000776 return ERR_PTR(-ENOENT);
777 }
Joe Perchesf3213832012-05-15 14:11:53 +0000778 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * We walked to the bottom of tree.
782 * Create new leaf node without children.
783 */
784
Wei Wang81eb8442017-10-06 12:06:11 -0700785 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
David S. Miller507c9b12011-12-03 17:50:45 -0500787 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000788 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700790 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700793 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700795 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 return ln;
798
799
800insert_above:
801 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900802 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * we have a less significant route.
804 * we've to insert an intermediate node on the list
805 * this new node will point to the one we need to create
806 * and the current
807 */
808
Wei Wang66f5d6c2017-10-06 12:06:10 -0700809 pn = rcu_dereference_protected(fn->parent,
810 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* find 1st bit in difference between the 2 addrs.
813
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800814 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 but if it is >= plen, the value is ignored in any case.
816 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900817
fan.du9225b232013-07-22 14:21:09 +0800818 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900820 /*
821 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * / \
823 * (new leaf node)[ln] (old node)[fn]
824 */
825 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700826 in = node_alloc(net);
827 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900828
David S. Miller507c9b12011-12-03 17:50:45 -0500829 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700831 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700833 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000834 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900837 /*
838 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * RTN_RTINFO will
840 * be off since that an address that chooses one of
841 * the branches would not match less specific routes
842 * in the other branch
843 */
844
845 in->fn_bit = bit;
846
Wei Wang66f5d6c2017-10-06 12:06:10 -0700847 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 in->leaf = fn->leaf;
Eric Dumazet5ea71522019-04-22 18:35:02 -0700849 fib6_info_hold(rcu_dereference_protected(in->leaf,
850 lockdep_is_held(&table->tb6_lock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* update parent pointer */
853 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700854 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700856 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 ln->fn_bit = plen;
859
Wei Wang66f5d6c2017-10-06 12:06:10 -0700860 RCU_INIT_POINTER(ln->parent, in);
861 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700864 rcu_assign_pointer(in->right, ln);
865 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700867 rcu_assign_pointer(in->left, ln);
868 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 } else { /* plen <= bit */
871
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900872 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * (new leaf node)[ln]
874 * / \
875 * (old node)[fn] NULL
876 */
877
Wei Wang81eb8442017-10-06 12:06:11 -0700878 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
David S. Miller507c9b12011-12-03 17:50:45 -0500880 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000881 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 ln->fn_bit = plen;
884
Wei Wang66f5d6c2017-10-06 12:06:10 -0700885 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700888 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700890 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Wei Wang66f5d6c2017-10-06 12:06:10 -0700892 rcu_assign_pointer(fn->parent, ln);
893
894 if (dir)
895 rcu_assign_pointer(pn->right, ln);
896 else
897 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 return ln;
900}
901
David Ahern5bcaa412018-04-20 15:38:01 -0700902static void fib6_drop_pcpu_from(struct fib6_info *f6i,
903 const struct fib6_table *table)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100904{
David Ahern5bcaa412018-04-20 15:38:01 -0700905 int cpu;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100906
Eric Dumazet61fb0d02019-05-15 19:39:52 -0700907 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
908 * while we are cleaning them here.
909 */
910 f6i->fib6_destroying = 1;
911 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
912
David Ahern5bcaa412018-04-20 15:38:01 -0700913 /* release the reference to this fib entry from
914 * all of its cached pcpu routes
915 */
916 for_each_possible_cpu(cpu) {
917 struct rt6_info **ppcpu_rt;
918 struct rt6_info *pcpu_rt;
919
920 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
921 pcpu_rt = *ppcpu_rt;
922 if (pcpu_rt) {
David Aherna68886a2018-04-20 15:38:02 -0700923 struct fib6_info *from;
924
Eric Dumazet0e233872019-04-28 12:22:25 -0700925 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
David Aherna68886a2018-04-20 15:38:02 -0700926 fib6_info_release(from);
David Ahern5bcaa412018-04-20 15:38:01 -0700927 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100928 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100929}
930
David Ahern8d1c8022018-04-17 17:33:26 -0700931static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100932 struct net *net)
933{
David Ahern93c2fb22018-04-18 15:38:59 -0700934 struct fib6_table *table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700935
Eric Dumazet61fb0d02019-05-15 19:39:52 -0700936 if (rt->rt6i_pcpu)
937 fib6_drop_pcpu_from(rt, table);
938
Eric Dumazetf05713e2019-04-22 18:35:03 -0700939 if (refcount_read(&rt->fib6_ref) != 1) {
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100940 /* This route is used as dummy address holder in some split
941 * nodes. It is not leaked, but it still holds other resources,
942 * which must be released in time. So, scan ascendant nodes
943 * and replace dummy references to this route with references
944 * to still alive ones.
945 */
946 while (fn) {
David Ahern8d1c8022018-04-17 17:33:26 -0700947 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700948 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700949 struct fib6_info *new_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700950 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
951 new_leaf = fib6_find_prefix(net, table, fn);
Eric Dumazet5ea71522019-04-22 18:35:02 -0700952 fib6_info_hold(new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700953
Wei Wang66f5d6c2017-10-06 12:06:10 -0700954 rcu_assign_pointer(fn->leaf, new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700955 fib6_info_release(rt);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100956 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700957 fn = rcu_dereference_protected(fn->parent,
958 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100959 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100960 }
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/*
964 * Insert routing information in a node.
965 */
966
David Ahern8d1c8022018-04-17 17:33:26 -0700967static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700968 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700969 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
David Ahern8d1c8022018-04-17 17:33:26 -0700971 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700972 lockdep_is_held(&rt->fib6_table->tb6_lock));
David Ahern33bd5ac2018-07-03 14:36:21 -0700973 struct fib6_info *iter = NULL;
David Ahern8d1c8022018-04-17 17:33:26 -0700974 struct fib6_info __rcu **ins;
David Ahern33bd5ac2018-07-03 14:36:21 -0700975 struct fib6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500976 int replace = (info->nlh &&
977 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
978 int add = (!info->nlh ||
979 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000980 int found = 0;
David Ahern33bd5ac2018-07-03 14:36:21 -0700981 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200982 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100983 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
David Ahern33bd5ac2018-07-03 14:36:21 -0700985 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
David Ahern1f5e29c2017-01-31 16:51:37 -0800986 nlflags |= NLM_F_APPEND;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ins = &fn->leaf;
989
Wei Wang66f5d6c2017-10-06 12:06:10 -0700990 for (iter = leaf; iter;
David Ahern8fb11a92018-05-04 13:54:24 -0700991 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -0700992 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /*
994 * Search for duplicates
995 */
996
David Ahern93c2fb22018-04-18 15:38:59 -0700997 if (iter->fib6_metric == rt->fib6_metric) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
999 * Same priority level
1000 */
David S. Miller507c9b12011-12-03 17:50:45 -05001001 if (info->nlh &&
1002 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001003 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +02001004
1005 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001006 if (replace) {
David Ahern33bd5ac2018-07-03 14:36:21 -07001007 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1008 found++;
1009 break;
1010 }
1011 if (rt_can_ecmp)
1012 fallback_ins = fallback_ins ?: ins;
1013 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
David Ahernf06b7542017-07-05 14:41:46 -06001016 if (rt6_duplicate_nexthop(iter, rt)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001017 if (rt->fib6_nsiblings)
1018 rt->fib6_nsiblings = 0;
1019 if (!(iter->fib6_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EEXIST;
David Ahern93c2fb22018-04-18 15:38:59 -07001021 if (!(rt->fib6_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -07001022 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +00001023 else
David Ahern14895682018-04-17 17:33:17 -07001024 fib6_set_expires(iter, rt->expires);
David Ahern15a81b42018-08-30 14:15:43 -07001025
1026 if (rt->fib6_pmtu)
1027 fib6_metric_set(iter, RTAX_MTU,
1028 rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EEXIST;
1030 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001031 /* If we have the same destination and the same metric,
1032 * but not the same gateway, then the route we try to
1033 * add is sibling to this route, increment our counter
1034 * of siblings, and later we will add our route to the
1035 * list.
1036 * Only static routes (which don't have flag
1037 * RTF_EXPIRES) are used for ECMPv6.
1038 *
1039 * To avoid long list, we only had siblings if the
1040 * route have a gateway.
1041 */
1042 if (rt_can_ecmp &&
1043 rt6_qualify_for_ecmp(iter))
1044 rt->fib6_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
David Ahern93c2fb22018-04-18 15:38:59 -07001047 if (iter->fib6_metric > rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
1049
David Ahern33bd5ac2018-07-03 14:36:21 -07001050next_iter:
David Ahern8fb11a92018-05-04 13:54:24 -07001051 ins = &iter->fib6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001052 }
1053
David Ahern33bd5ac2018-07-03 14:36:21 -07001054 if (fallback_ins && !found) {
1055 /* No ECMP-able route found, replace first non-ECMP one */
1056 ins = fallback_ins;
1057 iter = rcu_dereference_protected(*ins,
1058 lockdep_is_held(&rt->fib6_table->tb6_lock));
1059 found++;
1060 }
1061
David S. Millerf11e6652007-03-24 20:36:25 -07001062 /* Reset round-robin state, if necessary */
1063 if (ins == &fn->leaf)
1064 fn->rr_ptr = NULL;
1065
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001066 /* Link this route to others same route. */
David Ahern33bd5ac2018-07-03 14:36:21 -07001067 if (rt->fib6_nsiblings) {
1068 unsigned int fib6_nsiblings;
David Ahern8d1c8022018-04-17 17:33:26 -07001069 struct fib6_info *sibling, *temp_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001070
David Ahern33bd5ac2018-07-03 14:36:21 -07001071 /* Find the first route that have the same metric */
1072 sibling = leaf;
1073 while (sibling) {
1074 if (sibling->fib6_metric == rt->fib6_metric &&
1075 rt6_qualify_for_ecmp(sibling)) {
1076 list_add_tail(&rt->fib6_siblings,
1077 &sibling->fib6_siblings);
1078 break;
1079 }
1080 sibling = rcu_dereference_protected(sibling->fib6_next,
1081 lockdep_is_held(&rt->fib6_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001082 }
1083 /* For each sibling in the list, increment the counter of
1084 * siblings. BUG() if counters does not match, list of siblings
1085 * is broken!
1086 */
David Ahern33bd5ac2018-07-03 14:36:21 -07001087 fib6_nsiblings = 0;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001088 list_for_each_entry_safe(sibling, temp_sibling,
David Ahern33bd5ac2018-07-03 14:36:21 -07001089 &rt->fib6_siblings, fib6_siblings) {
David Ahern93c2fb22018-04-18 15:38:59 -07001090 sibling->fib6_nsiblings++;
David Ahern33bd5ac2018-07-03 14:36:21 -07001091 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1092 fib6_nsiblings++;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001093 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001094 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1095 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001096 }
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
1099 * insert node
1100 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001101 if (!replace) {
1102 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001103 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001105add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001106 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001107
David Ahern33bd5ac2018-07-03 14:36:21 -07001108 err = call_fib6_entry_notifiers(info->nl_net,
1109 FIB_EVENT_ENTRY_ADD,
1110 rt, extack);
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001111 if (err)
1112 return err;
1113
David Ahern8fb11a92018-05-04 13:54:24 -07001114 rcu_assign_pointer(rt->fib6_next, iter);
Eric Dumazet5ea71522019-04-22 18:35:02 -07001115 fib6_info_hold(rt);
David Ahern93c2fb22018-04-18 15:38:59 -07001116 rcu_assign_pointer(rt->fib6_node, fn);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001117 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001118 if (!info->skip_notify)
1119 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001120 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
David S. Miller507c9b12011-12-03 17:50:45 -05001122 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001123 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1124 fn->fn_flags |= RTN_RTINFO;
1125 }
1126
1127 } else {
David Ahern33bd5ac2018-07-03 14:36:21 -07001128 int nsiblings;
Michal Kubeček27596472015-05-18 20:54:00 +02001129
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001130 if (!found) {
1131 if (add)
1132 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001133 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001134 return -ENOENT;
1135 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001136
David Ahern22330002018-03-27 18:21:59 -07001137 err = call_fib6_entry_notifiers(info->nl_net,
1138 FIB_EVENT_ENTRY_REPLACE,
1139 rt, extack);
1140 if (err)
1141 return err;
1142
Eric Dumazet5ea71522019-04-22 18:35:02 -07001143 fib6_info_hold(rt);
David Ahern93c2fb22018-04-18 15:38:59 -07001144 rcu_assign_pointer(rt->fib6_node, fn);
David Ahern33bd5ac2018-07-03 14:36:21 -07001145 rt->fib6_next = iter->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001146 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001147 if (!info->skip_notify)
1148 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001149 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001150 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1151 fn->fn_flags |= RTN_RTINFO;
1152 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001153 nsiblings = iter->fib6_nsiblings;
1154 iter->fib6_node = NULL;
1155 fib6_purge_rt(iter, fn, info->nl_net);
1156 if (rcu_access_pointer(fn->rr_ptr) == iter)
David Ahernf34436a2018-05-21 10:26:53 -07001157 fn->rr_ptr = NULL;
David Ahern33bd5ac2018-07-03 14:36:21 -07001158 fib6_info_release(iter);
1159
1160 if (nsiblings) {
1161 /* Replacing an ECMP route, remove all siblings */
1162 ins = &rt->fib6_next;
1163 iter = rcu_dereference_protected(*ins,
1164 lockdep_is_held(&rt->fib6_table->tb6_lock));
1165 while (iter) {
1166 if (iter->fib6_metric > rt->fib6_metric)
1167 break;
1168 if (rt6_qualify_for_ecmp(iter)) {
1169 *ins = iter->fib6_next;
1170 iter->fib6_node = NULL;
1171 fib6_purge_rt(iter, fn, info->nl_net);
1172 if (rcu_access_pointer(fn->rr_ptr) == iter)
1173 fn->rr_ptr = NULL;
1174 fib6_info_release(iter);
1175 nsiblings--;
1176 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1177 } else {
1178 ins = &iter->fib6_next;
1179 }
1180 iter = rcu_dereference_protected(*ins,
1181 lockdep_is_held(&rt->fib6_table->tb6_lock));
1182 }
1183 WARN_ON(nsiblings != 0);
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 return 0;
1188}
1189
David Ahern8d1c8022018-04-17 17:33:26 -07001190static void fib6_start_gc(struct net *net, struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001192 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001193 (rt->fib6_flags & RTF_EXPIRES))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001194 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001195 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001198void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001200 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1201 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001202 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
David Ahern8d1c8022018-04-17 17:33:26 -07001205static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001206 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001207{
David Ahern93c2fb22018-04-18 15:38:59 -07001208 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1209 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001210
1211 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1212 smp_wmb();
1213 while (fn) {
1214 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001215 fn = rcu_dereference_protected(fn->parent,
David Ahern93c2fb22018-04-18 15:38:59 -07001216 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001217 }
1218}
1219
David Ahern8d1c8022018-04-17 17:33:26 -07001220void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001221{
1222 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/*
1226 * Add routing information to the routing tree.
1227 * <destination addr>/<source addr>
1228 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001229 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
1231
David Ahern8d1c8022018-04-17 17:33:26 -07001232int fib6_add(struct fib6_node *root, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001233 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
David Ahern93c2fb22018-04-18 15:38:59 -07001235 struct fib6_table *table = rt->fib6_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001236 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001238 int allow_create = 1;
1239 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001240 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001241
1242 if (info->nlh) {
1243 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001244 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001245 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001246 replace_required = 1;
1247 }
1248 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001249 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Wei Wang81eb8442017-10-06 12:06:11 -07001251 fn = fib6_add_1(info->nl_net, table, root,
David Ahern93c2fb22018-04-18 15:38:59 -07001252 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1253 offsetof(struct fib6_info, fib6_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001254 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001255 if (IS_ERR(fn)) {
1256 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001257 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001261 pn = fn;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07001264 if (rt->fib6_src.plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 struct fib6_node *sn;
1266
Wei Wang66f5d6c2017-10-06 12:06:10 -07001267 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct fib6_node *sfn;
1269
1270 /*
1271 * Create subtree.
1272 *
1273 * fn[main tree]
1274 * |
1275 * sfn[subtree root]
1276 * \
1277 * sn[new leaf node]
1278 */
1279
1280 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001281 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001282 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001283 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Eric Dumazet5ea71522019-04-22 18:35:02 -07001285 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001286 rcu_assign_pointer(sfn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001287 info->nl_net->ipv6.fib6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /* Now add the first leaf node to new subtree */
1291
Wei Wang81eb8442017-10-06 12:06:11 -07001292 sn = fib6_add_1(info->nl_net, table, sfn,
David Ahern93c2fb22018-04-18 15:38:59 -07001293 &rt->fib6_src.addr, rt->fib6_src.plen,
1294 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001295 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001297 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001299 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 in main tree.
1301 */
Wei Wang81eb8442017-10-06 12:06:11 -07001302 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001303 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001304 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
1307 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001308 rcu_assign_pointer(sfn->parent, fn);
1309 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001311 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
David Ahern93c2fb22018-04-18 15:38:59 -07001312 &rt->fib6_src.addr, rt->fib6_src.plen,
1313 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001314 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001316 if (IS_ERR(sn)) {
1317 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001318 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
Wei Wang66f5d6c2017-10-06 12:06:10 -07001322 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001323 if (fn->fn_flags & RTN_TL_ROOT) {
1324 /* put back null_entry for root node */
1325 rcu_assign_pointer(fn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001326 info->nl_net->ipv6.fib6_null_entry);
Wei Wang591ff9e2018-01-18 10:40:03 -08001327 } else {
Eric Dumazet5ea71522019-04-22 18:35:02 -07001328 fib6_info_hold(rt);
Wei Wang591ff9e2018-01-18 10:40:03 -08001329 rcu_assign_pointer(fn->leaf, rt);
1330 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 fn = sn;
1333 }
1334#endif
1335
David Ahernd4ead6b2018-04-17 17:33:16 -07001336 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001337 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001338 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001339 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001343 if (err) {
1344#ifdef CONFIG_IPV6_SUBTREES
1345 /*
1346 * If fib6_add_1 has cleared the old leaf pointer in the
1347 * super-tree leaf node we have to find a new one for it.
1348 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001349 if (pn != fn) {
David Ahern8d1c8022018-04-17 17:33:26 -07001350 struct fib6_info *pn_leaf =
Wei Wang7bbfe002018-01-03 14:11:59 -08001351 rcu_dereference_protected(pn->leaf,
1352 lockdep_is_held(&table->tb6_lock));
1353 if (pn_leaf == rt) {
1354 pn_leaf = NULL;
1355 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001356 fib6_info_release(rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001357 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001358 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1359 pn_leaf = fib6_find_prefix(info->nl_net, table,
1360 pn);
1361#if RT6_DEBUG >= 2
1362 if (!pn_leaf) {
1363 WARN_ON(!pn_leaf);
1364 pn_leaf =
David Ahern421842e2018-04-17 17:33:18 -07001365 info->nl_net->ipv6.fib6_null_entry;
Wei Wang7bbfe002018-01-03 14:11:59 -08001366 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001367#endif
David Ahern93531c62018-04-17 17:33:25 -07001368 fib6_info_hold(pn_leaf);
Wei Wang7bbfe002018-01-03 14:11:59 -08001369 rcu_assign_pointer(pn->leaf, pn_leaf);
1370 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001371 }
1372#endif
Wei Wang348a4002017-08-18 17:14:49 -07001373 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return err;
1376
Wei Wang348a4002017-08-18 17:14:49 -07001377failure:
Wei Wang4512c432018-01-08 10:34:00 -08001378 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1379 * 1. fn is an intermediate node and we failed to add the new
1380 * route to it in both subtree creation failure and fib6_add_rt2node()
1381 * failure case.
1382 * 2. fn is the root node in the table and we fail to add the first
1383 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Wei Wang4512c432018-01-08 10:34:00 -08001385 if (fn &&
1386 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1387 (fn->fn_flags & RTN_TL_ROOT &&
1388 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001389 fib6_repair_tree(info->nl_net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/*
1394 * Routing tree lookup
1395 *
1396 */
1397
1398struct lookup_args {
David Ahern8d1c8022018-04-17 17:33:26 -07001399 int offset; /* key offset on fib6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001400 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401};
1402
David Ahern64547432018-05-09 20:34:19 -07001403static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1404 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001407 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001409 if (unlikely(args->offset == 0))
1410 return NULL;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
1413 * Descend on a tree
1414 */
1415
1416 fn = root;
1417
1418 for (;;) {
1419 struct fib6_node *next;
1420
1421 dir = addr_bit_set(args->addr, fn->fn_bit);
1422
Wei Wang66f5d6c2017-10-06 12:06:10 -07001423 next = dir ? rcu_dereference(fn->right) :
1424 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (next) {
1427 fn = next;
1428 continue;
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 break;
1431 }
1432
David S. Miller507c9b12011-12-03 17:50:45 -05001433 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001434 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1435
1436 if (subtree || fn->fn_flags & RTN_RTINFO) {
David Ahern8d1c8022018-04-17 17:33:26 -07001437 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct rt6key *key;
1439
Wei Wang8d1040e2017-10-06 12:06:08 -07001440 if (!leaf)
1441 goto backtrack;
1442
1443 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001445 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1446#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001447 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001448 struct fib6_node *sfn;
David Ahern64547432018-05-09 20:34:19 -07001449 sfn = fib6_node_lookup_1(subtree,
1450 args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001451 if (!sfn)
1452 goto backtrack;
1453 fn = sfn;
1454 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001455#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001456 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001457 return fn;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001460backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001461 if (fn->fn_flags & RTN_ROOT)
1462 break;
1463
Wei Wang66f5d6c2017-10-06 12:06:10 -07001464 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
1467 return NULL;
1468}
1469
Wei Wang66f5d6c2017-10-06 12:06:10 -07001470/* called with rcu_read_lock() held
1471 */
David Ahern64547432018-05-09 20:34:19 -07001472struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1473 const struct in6_addr *daddr,
1474 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001477 struct lookup_args args[] = {
1478 {
David Ahern93c2fb22018-04-18 15:38:59 -07001479 .offset = offsetof(struct fib6_info, fib6_dst),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001480 .addr = daddr,
1481 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001483 {
David Ahern93c2fb22018-04-18 15:38:59 -07001484 .offset = offsetof(struct fib6_info, fib6_src),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001485 .addr = saddr,
1486 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001488 {
1489 .offset = 0, /* sentinel */
1490 }
1491 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
David Ahern64547432018-05-09 20:34:19 -07001493 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001494 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 fn = root;
1496
1497 return fn;
1498}
1499
1500/*
1501 * Get node with specified destination prefix (and source prefix,
1502 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001503 * exact_match == true means we try to find fn with exact match of
1504 * the passed in prefix addr
1505 * exact_match == false means we try to find fn with longest prefix
1506 * match of the passed in prefix addr. This is useful for finding fn
1507 * for cached route as it will be stored in the exception table under
1508 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
1510
1511
Wang Yufen437de072014-03-28 12:07:04 +08001512static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1513 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001514 int plen, int offset,
1515 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Wei Wang38fbeee2017-10-06 12:06:02 -07001517 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 for (fn = root; fn ; ) {
David Ahern8d1c8022018-04-17 17:33:26 -07001520 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001521 struct rt6key *key;
1522
1523 /* This node is being deleted */
1524 if (!leaf) {
1525 if (plen <= fn->fn_bit)
1526 goto out;
1527 else
1528 goto next;
1529 }
1530
1531 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * Prefix match
1535 */
1536 if (plen < fn->fn_bit ||
1537 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001538 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 if (plen == fn->fn_bit)
1541 return fn;
1542
Wei Wang38fbeee2017-10-06 12:06:02 -07001543 prev = fn;
1544
Wei Wang8d1040e2017-10-06 12:06:08 -07001545next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /*
1547 * We have more bits to go
1548 */
1549 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001550 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001552 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001554out:
1555 if (exact_match)
1556 return NULL;
1557 else
1558 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Wang Yufen437de072014-03-28 12:07:04 +08001561struct fib6_node *fib6_locate(struct fib6_node *root,
1562 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001563 const struct in6_addr *saddr, int src_len,
1564 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct fib6_node *fn;
1567
1568 fn = fib6_locate_1(root, daddr, dst_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001569 offsetof(struct fib6_info, fib6_dst),
Wei Wang38fbeee2017-10-06 12:06:02 -07001570 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572#ifdef CONFIG_IPV6_SUBTREES
1573 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001574 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001575 if (fn) {
1576 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1577
1578 if (subtree) {
1579 fn = fib6_locate_1(subtree, saddr, src_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001580 offsetof(struct fib6_info, fib6_src),
Wei Wang38fbeee2017-10-06 12:06:02 -07001581 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001582 }
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585#endif
1586
David S. Miller507c9b12011-12-03 17:50:45 -05001587 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return fn;
1589
1590 return NULL;
1591}
1592
1593
1594/*
1595 * Deletion
1596 *
1597 */
1598
David Ahern8d1c8022018-04-17 17:33:26 -07001599static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001600 struct fib6_table *table,
1601 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001603 struct fib6_node *child_left, *child_right;
1604
David S. Miller507c9b12011-12-03 17:50:45 -05001605 if (fn->fn_flags & RTN_ROOT)
David Ahern421842e2018-04-17 17:33:18 -07001606 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David S. Miller507c9b12011-12-03 17:50:45 -05001608 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001609 child_left = rcu_dereference_protected(fn->left,
1610 lockdep_is_held(&table->tb6_lock));
1611 child_right = rcu_dereference_protected(fn->right,
1612 lockdep_is_held(&table->tb6_lock));
1613 if (child_left)
1614 return rcu_dereference_protected(child_left->leaf,
1615 lockdep_is_held(&table->tb6_lock));
1616 if (child_right)
1617 return rcu_dereference_protected(child_right->leaf,
1618 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001620 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 return NULL;
1623}
1624
1625/*
1626 * Called to trim the tree of intermediate nodes when possible. "fn"
1627 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001628 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
1630
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001631static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001632 struct fib6_table *table,
1633 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int children;
1636 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001637 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001638 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int iter = 0;
1640
Wei Wang4512c432018-01-08 10:34:00 -08001641 /* Set fn->leaf to null_entry for root node. */
1642 if (fn->fn_flags & RTN_TL_ROOT) {
David Ahern421842e2018-04-17 17:33:18 -07001643 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
Wei Wang4512c432018-01-08 10:34:00 -08001644 return fn;
1645 }
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001648 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1649 lockdep_is_held(&table->tb6_lock));
1650 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1651 lockdep_is_held(&table->tb6_lock));
1652 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1653 lockdep_is_held(&table->tb6_lock));
1654 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1655 lockdep_is_held(&table->tb6_lock));
1656 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1657 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001658 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001659 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001660 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001661 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001662 struct fib6_info *new_fn_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1665 iter++;
1666
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001667 WARN_ON(fn->fn_flags & RTN_RTINFO);
1668 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001669 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 children = 0;
1672 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001673 if (fn_r)
1674 child = fn_r, children |= 1;
1675 if (fn_l)
1676 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001678 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679#ifdef CONFIG_IPV6_SUBTREES
1680 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001681 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682#endif
1683 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001684 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001686 if (!new_fn_leaf) {
1687 WARN_ON(!new_fn_leaf);
David Ahern421842e2018-04-17 17:33:18 -07001688 new_fn_leaf = net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690#endif
David Ahern93531c62018-04-17 17:33:25 -07001691 fib6_info_hold(new_fn_leaf);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001692 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1693 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001697 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001698 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001699 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 nstate = FWS_L;
1701 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001702 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001704 if (pn_r == fn)
1705 rcu_assign_pointer(pn->right, child);
1706 else if (pn_l == fn)
1707 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001709 else
1710 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711#endif
1712 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001713 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 nstate = FWS_R;
1715#ifdef CONFIG_IPV6_SUBTREES
1716 }
1717#endif
1718
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001719 read_lock(&net->ipv6.fib6_walker_lock);
1720 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001721 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001722 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1724 w->node = pn;
1725 w->state = nstate;
1726 }
1727 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (w->node == fn) {
1729 w->node = child;
1730 if (children&2) {
1731 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001732 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 } else {
1734 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001735 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 }
1738 }
1739 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001740 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Wei Wang81eb8442017-10-06 12:06:11 -07001742 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001743 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return pn;
1745
Wei Wang66f5d6c2017-10-06 12:06:10 -07001746 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001747 fib6_info_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 fn = pn;
1749 }
1750}
1751
Wei Wang66f5d6c2017-10-06 12:06:10 -07001752static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
David Ahern8d1c8022018-04-17 17:33:26 -07001753 struct fib6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001755 struct fib6_walker *w;
David Ahern8d1c8022018-04-17 17:33:26 -07001756 struct fib6_info *rt = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001757 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001758 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 RT6_TRACE("fib6_del_route\n");
1761
1762 /* Unlink it */
David Ahern8fb11a92018-05-04 13:54:24 -07001763 *rtp = rt->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001764 rt->fib6_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001765 net->ipv6.rt6_stats->fib_rt_entries--;
1766 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Wei Wang2b760fc2017-10-06 12:06:03 -07001768 /* Flush all cached dst in exception table */
1769 rt6_flush_exceptions(rt);
1770
David S. Millerf11e6652007-03-24 20:36:25 -07001771 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001772 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001773 fn->rr_ptr = NULL;
1774
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001775 /* Remove this entry from other siblings */
David Ahern93c2fb22018-04-18 15:38:59 -07001776 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07001777 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001778
1779 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001780 &rt->fib6_siblings, fib6_siblings)
1781 sibling->fib6_nsiblings--;
1782 rt->fib6_nsiblings = 0;
1783 list_del_init(&rt->fib6_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001784 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001785 }
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001788 read_lock(&net->ipv6.fib6_walker_lock);
1789 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (w->state == FWS_C && w->leaf == rt) {
1791 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Ahern8fb11a92018-05-04 13:54:24 -07001792 w->leaf = rcu_dereference_protected(rt->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001793 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001794 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 w->state = FWS_U;
1796 }
1797 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001798 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Wei Wang4512c432018-01-08 10:34:00 -08001800 /* If it was last route, call fib6_repair_tree() to:
1801 * 1. For root node, put back null_entry as how the table was created.
1802 * 2. For other nodes, expunge its radix tree node.
1803 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001804 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001805 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1806 fn->fn_flags &= ~RTN_RTINFO;
1807 net->ipv6.rt6_stats->fib_route_nodes--;
1808 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001809 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001812 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
David Ahern6c31e5a2017-10-27 17:37:13 -07001814 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001815 if (!info->skip_notify)
1816 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
David Ahern93531c62018-04-17 17:33:25 -07001817 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Wei Wang66f5d6c2017-10-06 12:06:10 -07001820/* Need to own table->tb6_lock */
David Ahern8d1c8022018-04-17 17:33:26 -07001821int fib6_del(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
David Ahern93c2fb22018-04-18 15:38:59 -07001823 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1824 lockdep_is_held(&rt->fib6_table->tb6_lock));
1825 struct fib6_table *table = rt->fib6_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001826 struct net *net = info->nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07001827 struct fib6_info __rcu **rtp;
1828 struct fib6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
David Ahern421842e2018-04-17 17:33:18 -07001830 if (!fn || rt == net->ipv6.fib6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -ENOENT;
1832
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001833 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /*
1836 * Walk the leaf entries looking for ourself
1837 */
1838
Wei Wang66f5d6c2017-10-06 12:06:10 -07001839 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
David Ahern8d1c8022018-04-17 17:33:26 -07001840 struct fib6_info *cur = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001841 lockdep_is_held(&table->tb6_lock));
1842 if (rt == cur) {
1843 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return 0;
1845 }
David Ahern8fb11a92018-05-04 13:54:24 -07001846 rtp_next = &cur->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 return -ENOENT;
1849}
1850
1851/*
1852 * Tree traversal function.
1853 *
1854 * Certainly, it is not interrupt safe.
1855 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1856 * It means, that we can modify tree during walking
1857 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001858 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 *
1860 * It guarantees that every node will be traversed,
1861 * and that it will be traversed only once.
1862 *
1863 * Callback function w->func may return:
1864 * 0 -> continue walking.
1865 * positive value -> walking is suspended (used by tree dumps,
1866 * and probably by gc, if it will be split to several slices)
1867 * negative value -> terminate walking.
1868 *
1869 * The function itself returns:
1870 * 0 -> walk is complete.
1871 * >0 -> walk is incomplete (i.e. suspended)
1872 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001873 *
1874 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
1876
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001877static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001879 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Wei Wang2b760fc2017-10-06 12:06:03 -07001881 /* w->root should always be table->tb6_root */
1882 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 for (;;) {
1885 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001886 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 switch (w->state) {
1890#ifdef CONFIG_IPV6_SUBTREES
1891 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001892 if (FIB6_SUBTREE(fn)) {
1893 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 continue;
1895 }
1896 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001897#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001898 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001900 left = rcu_dereference_protected(fn->left, 1);
1901 if (left) {
1902 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 w->state = FWS_INIT;
1904 continue;
1905 }
1906 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001907 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001909 right = rcu_dereference_protected(fn->right, 1);
1910 if (right) {
1911 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 w->state = FWS_INIT;
1913 continue;
1914 }
1915 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001916 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001917 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001919 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001920 int err;
1921
Eric Dumazetfa809e22012-06-25 15:37:19 -07001922 if (w->skip) {
1923 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001924 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001925 }
1926
1927 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (err)
1929 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001930
1931 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 continue;
1933 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001934skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001936 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 case FWS_U:
1938 if (fn == w->root)
1939 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001940 pn = rcu_dereference_protected(fn->parent, 1);
1941 left = rcu_dereference_protected(pn->left, 1);
1942 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 w->node = pn;
1944#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001945 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001946 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 w->state = FWS_L;
1948 continue;
1949 }
1950#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001951 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 w->state = FWS_R;
1953 continue;
1954 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001955 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001957 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 continue;
1959 }
1960#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001961 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962#endif
1963 }
1964 }
1965}
1966
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001967static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 int res;
1970
1971 w->state = FWS_INIT;
1972 w->node = w->root;
1973
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001974 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 res = fib6_walk_continue(w);
1976 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001977 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return res;
1979}
1980
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001981static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 int res;
David Ahern8d1c8022018-04-17 17:33:26 -07001984 struct fib6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001985 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001986 struct nl_info info = {
1987 .nl_net = c->net,
David Ahern7c6bb7d2018-10-11 20:17:21 -07001988 .skip_notify = c->skip_notify,
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001991 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1992 w->node->fn_sernum != c->sernum)
1993 w->node->fn_sernum = c->sernum;
1994
1995 if (!c->func) {
1996 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1997 w->leaf = NULL;
1998 return 0;
1999 }
2000
Wei Wang66f5d6c2017-10-06 12:06:10 -07002001 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002003 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08002005 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (res) {
2007#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00002008 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07002009 __func__, rt,
David Ahern93c2fb22018-04-18 15:38:59 -07002010 rcu_access_pointer(rt->fib6_node),
Wei Wang4e587ea2017-08-25 15:03:10 -07002011 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012#endif
2013 continue;
2014 }
2015 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002016 } else if (res == -2) {
David Ahern93c2fb22018-04-18 15:38:59 -07002017 if (WARN_ON(!rt->fib6_nsiblings))
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002018 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002019 rt = list_last_entry(&rt->fib6_siblings,
2020 struct fib6_info, fib6_siblings);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002021 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002023 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 w->leaf = rt;
2026 return 0;
2027}
2028
2029/*
2030 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02002033 * It may return -2 -> skip multipath route.
2034 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
2037
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002038static void fib6_clean_tree(struct net *net, struct fib6_node *root,
David Ahern8d1c8022018-04-17 17:33:26 -07002039 int (*func)(struct fib6_info *, void *arg),
David Ahern7c6bb7d2018-10-11 20:17:21 -07002040 int sernum, void *arg, bool skip_notify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002042 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 c.w.root = root;
2045 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00002046 c.w.count = 0;
2047 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002049 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002051 c.net = net;
David Ahern7c6bb7d2018-10-11 20:17:21 -07002052 c.skip_notify = skip_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002054 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002057static void __fib6_clean_all(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07002058 int (*func)(struct fib6_info *, void *),
David Ahern7c6bb7d2018-10-11 20:17:21 -07002059 int sernum, void *arg, bool skip_notify)
Thomas Grafc71099a2006-08-04 23:20:06 -07002060{
Thomas Grafc71099a2006-08-04 23:20:06 -07002061 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002062 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07002063 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07002064
Patrick McHardy1b43af52006-08-10 23:11:17 -07002065 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07002066 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002067 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002068 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002069 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002070 fib6_clean_tree(net, &table->tb6_root,
David Ahern7c6bb7d2018-10-11 20:17:21 -07002071 func, sernum, arg, skip_notify);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002072 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07002073 }
2074 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07002075 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07002076}
2077
David Ahern8d1c8022018-04-17 17:33:26 -07002078void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002079 void *arg)
2080{
David Ahern7c6bb7d2018-10-11 20:17:21 -07002081 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2082}
2083
2084void fib6_clean_all_skip_notify(struct net *net,
2085 int (*func)(struct fib6_info *, void *),
2086 void *arg)
2087{
2088 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002089}
2090
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002091static void fib6_flush_trees(struct net *net)
2092{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002093 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002094
David Ahern7c6bb7d2018-10-11 20:17:21 -07002095 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002096}
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098/*
2099 * Garbage collection
2100 */
2101
David Ahern8d1c8022018-04-17 17:33:26 -07002102static int fib6_age(struct fib6_info *rt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Michal Kubeček3570df92016-03-08 14:44:25 +01002104 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 unsigned long now = jiffies;
2106
2107 /*
2108 * check addrconf expiration here.
2109 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
2111
David Ahern93c2fb22018-04-18 15:38:59 -07002112 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
David Ahern14895682018-04-17 17:33:17 -07002113 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return -1;
2116 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002117 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
Wei Wangc757faa2017-10-06 12:06:01 -07002120 /* Also age clones in the exception table.
2121 * Note, that clones are aged out
2122 * only if they are not in use now.
2123 */
2124 rt6_age_exceptions(rt, gc_args, now);
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return 0;
2127}
2128
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002129void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Michal Kubeček3570df92016-03-08 14:44:25 +01002131 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002132 unsigned long now;
2133
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002134 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002135 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2136 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002137 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2138 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002140 gc_args.timeout = expires ? (int)expires :
2141 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002142 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002143
Michal Kubeček3570df92016-03-08 14:44:25 +01002144 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002145 now = jiffies;
2146 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002149 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002150 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002151 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002152 else
2153 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002154 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Kees Cook86cb30e2017-10-17 20:21:24 -07002157static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002158{
Kees Cook86cb30e2017-10-17 20:21:24 -07002159 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2160
2161 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002162}
2163
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002164static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002165{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002166 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002167 int err;
2168
2169 err = fib6_notifier_init(net);
2170 if (err)
2171 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002172
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002173 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002174 rwlock_init(&net->ipv6.fib6_walker_lock);
2175 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002176 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002177
Benjamin Theryc5728722008-03-03 23:34:17 -08002178 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2179 if (!net->ipv6.rt6_stats)
2180 goto out_timer;
2181
Eric Dumazet10da66f2010-10-13 08:22:03 +00002182 /* Avoid false sharing : Use at least a full cache line */
2183 size = max_t(size_t, size, L1_CACHE_BYTES);
2184
2185 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002186 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002187 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002188
2189 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2190 GFP_KERNEL);
2191 if (!net->ipv6.fib6_main_tbl)
2192 goto out_fib_table_hash;
2193
2194 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002195 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002196 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002197 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2198 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002199 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002200
2201#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2202 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2203 GFP_KERNEL);
2204 if (!net->ipv6.fib6_local_tbl)
2205 goto out_fib6_main_tbl;
2206 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002207 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002208 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002209 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2210 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002211 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002212#endif
2213 fib6_tables_init(net);
2214
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002215 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002216
2217#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2218out_fib6_main_tbl:
2219 kfree(net->ipv6.fib6_main_tbl);
2220#endif
2221out_fib_table_hash:
2222 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002223out_rt6_stats:
2224 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002225out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002226 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002227 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002228}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002229
2230static void fib6_net_exit(struct net *net)
2231{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002232 unsigned int i;
2233
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002234 del_timer_sync(&net->ipv6.ip6_fib_timer);
2235
Eric Dumazet32a805b2017-09-08 15:48:47 -07002236 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002237 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2238 struct hlist_node *tmp;
2239 struct fib6_table *tb;
2240
2241 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2242 hlist_del(&tb->tb6_hlist);
2243 fib6_free_table(tb);
2244 }
2245 }
2246
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002247 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002248 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002249 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002250}
2251
2252static struct pernet_operations fib6_net_ops = {
2253 .init = fib6_net_init,
2254 .exit = fib6_net_exit,
2255};
2256
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002257int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002259 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2262 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002263 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002264 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002265 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002266 goto out;
2267
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002268 ret = register_pernet_subsys(&fib6_net_ops);
2269 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002270 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002271
Florian Westphal16feebc2017-12-02 21:44:08 +01002272 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2273 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002274 if (ret)
2275 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002276
2277 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002278out:
2279 return ret;
2280
David S. Millere8803b62012-06-16 01:12:19 -07002281out_unregister_subsys:
2282 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002283out_kmem_cache_create:
2284 kmem_cache_destroy(fib6_node_kmem);
2285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288void fib6_gc_cleanup(void)
2289{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002290 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 kmem_cache_destroy(fib6_node_kmem);
2292}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002293
2294#ifdef CONFIG_PROC_FS
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002295static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2296{
David Ahern8d1c8022018-04-17 17:33:26 -07002297 struct fib6_info *rt = v;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002298 struct ipv6_route_iter *iter = seq->private;
David Ahern2b2450c2019-03-27 20:53:52 -07002299 unsigned int flags = rt->fib6_flags;
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 Ahernbdf00462019-04-05 16:30:26 -07002309 if (rt->fib6_nh.fib_nh_gw_family) {
David Ahern2b2450c2019-03-27 20:53:52 -07002310 flags |= RTF_GATEWAY;
David Ahernad1601a2019-03-27 20:53:56 -07002311 seq_printf(seq, "%pi6", &rt->fib6_nh.fib_nh_gw6);
David Ahern2b2450c2019-03-27 20:53:52 -07002312 } else {
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002313 seq_puts(seq, "00000000000000000000000000000000");
David Ahern2b2450c2019-03-27 20:53:52 -07002314 }
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002315
David Ahernad1601a2019-03-27 20:53:56 -07002316 dev = rt->fib6_nh.fib_nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002317 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
Eric Dumazetf05713e2019-04-22 18:35:03 -07002318 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
David Ahern2b2450c2019-03-27 20:53:52 -07002319 flags, dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002320 iter->w.leaf = NULL;
2321 return 0;
2322}
2323
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002324static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002325{
2326 struct ipv6_route_iter *iter = w->args;
2327
2328 if (!iter->skip)
2329 return 1;
2330
2331 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002332 iter->w.leaf = rcu_dereference_protected(
David Ahern8fb11a92018-05-04 13:54:24 -07002333 iter->w.leaf->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002334 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002335 iter->skip--;
2336 if (!iter->skip && iter->w.leaf)
2337 return 1;
2338 } while (iter->w.leaf);
2339
2340 return 0;
2341}
2342
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002343static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2344 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002345{
2346 memset(&iter->w, 0, sizeof(iter->w));
2347 iter->w.func = ipv6_route_yield;
2348 iter->w.root = &iter->tbl->tb6_root;
2349 iter->w.state = FWS_INIT;
2350 iter->w.node = iter->w.root;
2351 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002352 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002353 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002354 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002355}
2356
2357static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2358 struct net *net)
2359{
2360 unsigned int h;
2361 struct hlist_node *node;
2362
2363 if (tbl) {
2364 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2365 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2366 } else {
2367 h = 0;
2368 node = NULL;
2369 }
2370
2371 while (!node && h < FIB6_TABLE_HASHSZ) {
2372 node = rcu_dereference_bh(
2373 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2374 }
2375 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2376}
2377
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002378static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2379{
2380 if (iter->sernum != iter->w.root->fn_sernum) {
2381 iter->sernum = iter->w.root->fn_sernum;
2382 iter->w.state = FWS_INIT;
2383 iter->w.node = iter->w.root;
2384 WARN_ON(iter->w.skip);
2385 iter->w.skip = iter->w.count;
2386 }
2387}
2388
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002389static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2390{
2391 int r;
David Ahern8d1c8022018-04-17 17:33:26 -07002392 struct fib6_info *n;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002393 struct net *net = seq_file_net(seq);
2394 struct ipv6_route_iter *iter = seq->private;
2395
2396 if (!v)
2397 goto iter_table;
2398
David Ahern8fb11a92018-05-04 13:54:24 -07002399 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002400 if (n) {
2401 ++*pos;
2402 return n;
2403 }
2404
2405iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002406 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002407 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002408 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002409 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002410 if (r > 0) {
2411 if (v)
2412 ++*pos;
2413 return iter->w.leaf;
2414 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002415 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002416 return NULL;
2417 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002418 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002419
2420 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2421 if (!iter->tbl)
2422 return NULL;
2423
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002424 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002425 goto iter_table;
2426}
2427
2428static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2429 __acquires(RCU_BH)
2430{
2431 struct net *net = seq_file_net(seq);
2432 struct ipv6_route_iter *iter = seq->private;
2433
2434 rcu_read_lock_bh();
2435 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2436 iter->skip = *pos;
2437
2438 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002439 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002440 return ipv6_route_seq_next(seq, NULL, pos);
2441 } else {
2442 return NULL;
2443 }
2444}
2445
2446static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2447{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002448 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002449 return w->node && !(w->state == FWS_U && w->node == w->root);
2450}
2451
2452static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2453 __releases(RCU_BH)
2454{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002455 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002456 struct ipv6_route_iter *iter = seq->private;
2457
2458 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002459 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002460
2461 rcu_read_unlock_bh();
2462}
2463
Christoph Hellwigc3506372018-04-10 19:42:55 +02002464const struct seq_operations ipv6_route_seq_ops = {
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002465 .start = ipv6_route_seq_start,
2466 .next = ipv6_route_seq_next,
2467 .stop = ipv6_route_seq_stop,
2468 .show = ipv6_route_seq_show
2469};
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002470#endif /* CONFIG_PROC_FS */