blob: 487faffeae287e87ca803f0edabb6ee34641d63f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020035#include <net/lwtunnel.h>
Ido Schimmeldf77fe42017-08-03 13:28:17 +020036#include <net/fib_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <net/ip6_fib.h>
39#include <net/ip6_route.h>
40
Wang Yufen437de072014-03-28 12:07:04 +080041static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020043struct fib6_cleaner {
44 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080045 struct net *net;
David Ahern8d1c8022018-04-17 17:33:26 -070046 int (*func)(struct fib6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020047 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 void *arg;
49};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_IPV6_SUBTREES
52#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#else
54#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56
David Ahern8d1c8022018-04-17 17:33:26 -070057static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -070058 struct fib6_table *table,
59 struct fib6_node *fn);
60static struct fib6_node *fib6_repair_tree(struct net *net,
61 struct fib6_table *table,
62 struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010063static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020064static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * A routing update causes an increase of the serial number on the
68 * affected subtree. This allows for cached routes to be asynchronously
69 * tested when modifications are made to the destination cache as a
70 * result of redirects, path MTU changes, etc.
71 */
72
Kees Cook86cb30e2017-10-17 20:21:24 -070073static void fib6_gc_timer_cb(struct timer_list *t);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080074
Michal Kubeček9a03cd82016-03-08 14:44:35 +010075#define FOR_WALKERS(net, w) \
76 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Michal Kubeček9a03cd82016-03-08 14:44:35 +010078static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070079{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010080 write_lock_bh(&net->ipv6.fib6_walker_lock);
81 list_add(&w->lh, &net->ipv6.fib6_walkers);
82 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070083}
84
Michal Kubeček9a03cd82016-03-08 14:44:35 +010085static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070086{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010087 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000088 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010089 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070090}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020091
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020092static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020094 int new, old;
95
96 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020098 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020099 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
100 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200101 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200104enum {
105 FIB6_NO_SERNUM_CHANGE = 0,
106};
107
David Ahern93c2fb22018-04-18 15:38:59 -0700108void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
Wei Wang180ca442017-10-06 12:05:56 -0700109{
Wei Wang180ca442017-10-06 12:05:56 -0700110 struct fib6_node *fn;
111
David Ahern93c2fb22018-04-18 15:38:59 -0700112 fn = rcu_dereference_protected(f6i->fib6_node,
113 lockdep_is_held(&f6i->fib6_table->tb6_lock));
Wei Wang180ca442017-10-06 12:05:56 -0700114 if (fn)
115 fn->fn_sernum = fib6_new_sernum(net);
Wei Wang180ca442017-10-06 12:05:56 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Auxiliary address test functions for the radix tree.
120 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900121 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * 64bit processors)
123 */
124
125/*
126 * test bit
127 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000128#if defined(__LITTLE_ENDIAN)
129# define BITOP_BE32_SWIZZLE (0x1F & ~7)
130#else
131# define BITOP_BE32_SWIZZLE 0
132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200134static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000136 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000137 /*
138 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800139 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000140 * is optimized version of
141 * htonl(1 << ((~fn_bit)&0x1F))
142 * See include/asm-generic/bitops/le.h.
143 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700144 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
145 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
David Ahern8d1c8022018-04-17 17:33:26 -0700148struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
David Aherna64efe142018-04-17 17:33:24 -0700149{
David Ahern8d1c8022018-04-17 17:33:26 -0700150 struct fib6_info *f6i;
David Aherna64efe142018-04-17 17:33:24 -0700151
152 f6i = kzalloc(sizeof(*f6i), gfp_flags);
153 if (!f6i)
154 return NULL;
155
156 f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
157 if (!f6i->rt6i_pcpu) {
158 kfree(f6i);
159 return NULL;
160 }
161
David Ahern93c2fb22018-04-18 15:38:59 -0700162 INIT_LIST_HEAD(&f6i->fib6_siblings);
David Aherna64efe142018-04-17 17:33:24 -0700163 f6i->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
164
David Ahern93c2fb22018-04-18 15:38:59 -0700165 atomic_inc(&f6i->fib6_ref);
David Aherna64efe142018-04-17 17:33:24 -0700166
167 return f6i;
168}
169
David Ahern8d1c8022018-04-17 17:33:26 -0700170void fib6_info_destroy(struct fib6_info *f6i)
David Aherna64efe142018-04-17 17:33:24 -0700171{
172 struct rt6_exception_bucket *bucket;
David Ahern93531c62018-04-17 17:33:25 -0700173 struct dst_metrics *m;
David Aherna64efe142018-04-17 17:33:24 -0700174
David Ahern93c2fb22018-04-18 15:38:59 -0700175 WARN_ON(f6i->fib6_node);
David Aherna64efe142018-04-17 17:33:24 -0700176
177 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
178 if (bucket) {
179 f6i->rt6i_exception_bucket = NULL;
180 kfree(bucket);
181 }
182
183 if (f6i->rt6i_pcpu) {
184 int cpu;
185
186 for_each_possible_cpu(cpu) {
187 struct rt6_info **ppcpu_rt;
188 struct rt6_info *pcpu_rt;
189
190 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
191 pcpu_rt = *ppcpu_rt;
192 if (pcpu_rt) {
193 dst_dev_put(&pcpu_rt->dst);
194 dst_release(&pcpu_rt->dst);
195 *ppcpu_rt = NULL;
196 }
197 }
198 }
199
David Aherna64efe142018-04-17 17:33:24 -0700200 if (f6i->fib6_nh.nh_dev)
201 dev_put(f6i->fib6_nh.nh_dev);
202
David Ahern93531c62018-04-17 17:33:25 -0700203 m = f6i->fib6_metrics;
204 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
205 kfree(m);
206
David Aherna64efe142018-04-17 17:33:24 -0700207 kfree(f6i);
208}
209EXPORT_SYMBOL_GPL(fib6_info_destroy);
210
Wei Wang81eb8442017-10-06 12:06:11 -0700211static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct fib6_node *fn;
214
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800215 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700216 if (fn)
217 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 return fn;
220}
221
Wei Wang81eb8442017-10-06 12:06:11 -0700222static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700225 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Wei Wangc5cff852017-08-21 09:47:10 -0700228static void node_free_rcu(struct rcu_head *head)
229{
230 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
231
232 kmem_cache_free(fib6_node_kmem, fn);
233}
234
Wei Wang81eb8442017-10-06 12:06:11 -0700235static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700236{
237 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700238 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700239}
240
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200241static void fib6_free_table(struct fib6_table *table)
242{
243 inetpeer_invalidate_tree(&table->tb6_peers);
244 kfree(table);
245}
246
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700248{
249 unsigned int h;
250
Thomas Graf375216a2006-10-21 20:20:54 -0700251 /*
252 * Initialize table lock at a single place to give lockdep a key,
253 * tables aren't visible prior to being linked to the list.
254 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700255 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700256 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700257
258 /*
259 * No protection necessary, this is the only list mutatation
260 * operation, tables never disappear once they exist.
261 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800262 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700263}
264
265#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800266
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800267static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700268{
269 struct fib6_table *table;
270
271 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500272 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700273 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700274 rcu_assign_pointer(table->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -0700275 net->ipv6.fib6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700276 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700277 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700278 }
279
280 return table;
281}
282
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800283struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700284{
285 struct fib6_table *tb;
286
287 if (id == 0)
288 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800289 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700290 if (tb)
291 return tb;
292
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800293 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500294 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800295 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700296
297 return tb;
298}
David Ahernb3b46632016-05-04 21:46:12 -0700299EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700300
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800301struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700302{
303 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800304 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700305 unsigned int h;
306
307 if (id == 0)
308 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700309 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700310 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800311 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800312 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700313 if (tb->tb6_id == id) {
314 rcu_read_unlock();
315 return tb;
316 }
317 }
318 rcu_read_unlock();
319
320 return NULL;
321}
David Ahernc4850682015-10-12 11:47:08 -0700322EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700323
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000324static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700325{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800326 fib6_link_table(net, net->ipv6.fib6_main_tbl);
327 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700328}
Thomas Grafc71099a2006-08-04 23:20:06 -0700329#else
330
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800331struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700332{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800333 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700334}
335
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800336struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700337{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800338 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700339}
340
David S. Miller4c9483b2011-03-12 16:22:43 -0500341struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -0800342 const struct sk_buff *skb,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800343 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700344{
lucienab997ad2015-10-23 15:36:53 +0800345 struct rt6_info *rt;
346
David Ahernb75cc8f2018-03-02 08:32:17 -0800347 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300348 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800349 ip6_rt_put(rt);
350 rt = net->ipv6.ip6_null_entry;
351 dst_hold(&rt->dst);
352 }
353
354 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700355}
356
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000357static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700358{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800359 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700360}
361
362#endif
363
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200364unsigned int fib6_tables_seq_read(struct net *net)
365{
366 unsigned int h, fib_seq = 0;
367
368 rcu_read_lock();
369 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
370 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
371 struct fib6_table *tb;
372
Wei Wang66f5d6c2017-10-06 12:06:10 -0700373 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200374 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200375 }
376 rcu_read_unlock();
377
378 return fib_seq;
379}
380
381static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
382 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700383 struct fib6_info *rt)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200384{
385 struct fib6_entry_notifier_info info = {
386 .rt = rt,
387 };
388
389 return call_fib6_notifier(nb, net, event_type, &info.info);
390}
391
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200392static int call_fib6_entry_notifiers(struct net *net,
393 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700394 struct fib6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700395 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200396{
397 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700398 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200399 .rt = rt,
400 };
401
David Ahern93c2fb22018-04-18 15:38:59 -0700402 rt->fib6_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200403 return call_fib6_notifiers(net, event_type, &info.info);
404}
405
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200406struct fib6_dump_arg {
407 struct net *net;
408 struct notifier_block *nb;
409};
410
David Ahern8d1c8022018-04-17 17:33:26 -0700411static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200412{
David Ahern421842e2018-04-17 17:33:18 -0700413 if (rt == arg->net->ipv6.fib6_null_entry)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200414 return;
415 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
416}
417
418static int fib6_node_dump(struct fib6_walker *w)
419{
David Ahern8d1c8022018-04-17 17:33:26 -0700420 struct fib6_info *rt;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200421
Wei Wang66f5d6c2017-10-06 12:06:10 -0700422 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200423 fib6_rt_dump(rt, w->args);
424 w->leaf = NULL;
425 return 0;
426}
427
428static void fib6_table_dump(struct net *net, struct fib6_table *tb,
429 struct fib6_walker *w)
430{
431 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700432 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200433 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700434 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200435}
436
437/* Called with rcu_read_lock() */
438int fib6_tables_dump(struct net *net, struct notifier_block *nb)
439{
440 struct fib6_dump_arg arg;
441 struct fib6_walker *w;
442 unsigned int h;
443
444 w = kzalloc(sizeof(*w), GFP_ATOMIC);
445 if (!w)
446 return -ENOMEM;
447
448 w->func = fib6_node_dump;
449 arg.net = net;
450 arg.nb = nb;
451 w->args = &arg;
452
453 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
454 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
455 struct fib6_table *tb;
456
457 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
458 fib6_table_dump(net, tb, w);
459 }
460
461 kfree(w);
462
463 return 0;
464}
465
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200466static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700467{
468 int res;
David Ahern8d1c8022018-04-17 17:33:26 -0700469 struct fib6_info *rt;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700470
Wei Wang66f5d6c2017-10-06 12:06:10 -0700471 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700472 res = rt6_dump_route(rt, w->args);
473 if (res < 0) {
474 /* Frame is full, suspend walking */
475 w->leaf = rt;
476 return 1;
477 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800478
479 /* Multipath routes are dumped in one route with the
480 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
481 * last sibling of this route (no need to dump the
482 * sibling routes again)
483 */
David Ahern93c2fb22018-04-18 15:38:59 -0700484 if (rt->fib6_nsiblings)
485 rt = list_last_entry(&rt->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -0700486 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -0700487 fib6_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700488 }
489 w->leaf = NULL;
490 return 0;
491}
492
493static void fib6_dump_end(struct netlink_callback *cb)
494{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100495 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200496 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700497
498 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800499 if (cb->args[4]) {
500 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100501 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800502 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700503 cb->args[2] = 0;
504 kfree(w);
505 }
Wang Yufen437de072014-03-28 12:07:04 +0800506 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507 cb->args[1] = 3;
508}
509
510static int fib6_dump_done(struct netlink_callback *cb)
511{
512 fib6_dump_end(cb);
513 return cb->done ? cb->done(cb) : 0;
514}
515
516static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
517 struct netlink_callback *cb)
518{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100519 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200520 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700521 int res;
522
523 w = (void *)cb->args[2];
524 w->root = &table->tb6_root;
525
526 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000527 w->count = 0;
528 w->skip = 0;
529
Wei Wang66f5d6c2017-10-06 12:06:10 -0700530 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100531 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700532 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000533 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700534 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000535 cb->args[5] = w->root->fn_sernum;
536 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700537 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000538 if (cb->args[5] != w->root->fn_sernum) {
539 /* Begin at the root if the tree changed */
540 cb->args[5] = w->root->fn_sernum;
541 w->state = FWS_INIT;
542 w->node = w->root;
543 w->skip = w->count;
544 } else
545 w->skip = 0;
546
Wei Wang66f5d6c2017-10-06 12:06:10 -0700547 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700548 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700549 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800550 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100551 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800552 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700553 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700554 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800555
Patrick McHardy1b43af52006-08-10 23:11:17 -0700556 return res;
557}
558
Thomas Grafc127ea22007-03-22 11:58:32 -0700559static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700560{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900561 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700562 unsigned int h, s_h;
563 unsigned int e = 0, s_e;
564 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200565 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700566 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800567 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700568 int res = 0;
569
570 s_h = cb->args[0];
571 s_e = cb->args[1];
572
573 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500574 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700575 /* New dump:
576 *
577 * 1. hook callback destructor.
578 */
579 cb->args[3] = (long)cb->done;
580 cb->done = fib6_dump_done;
581
582 /*
583 * 2. allocate and initialize walker.
584 */
585 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500586 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700587 return -ENOMEM;
588 w->func = fib6_dump_node;
589 cb->args[2] = (long)w;
590 }
591
592 arg.skb = skb;
593 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700594 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700595 w->args = &arg;
596
Eric Dumazete67f88d2011-04-27 22:56:07 +0000597 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700598 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700599 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800600 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800601 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700602 if (e < s_e)
603 goto next;
604 res = fib6_dump_table(tb, skb, cb);
605 if (res != 0)
606 goto out;
607next:
608 e++;
609 }
610 }
611out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000612 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700613 cb->args[1] = e;
614 cb->args[0] = h;
615
616 res = res < 0 ? res : skb->len;
617 if (res <= 0)
618 fib6_dump_end(cb);
619 return res;
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
David Ahern8d1c8022018-04-17 17:33:26 -0700622void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
David Ahernd4ead6b2018-04-17 17:33:16 -0700623{
624 if (!f6i)
625 return;
626
627 if (f6i->fib6_metrics == &dst_default_metrics) {
628 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
629
630 if (!p)
631 return;
632
633 refcount_set(&p->refcnt, 1);
634 f6i->fib6_metrics = p;
635 }
636
637 f6i->fib6_metrics->metrics[metric - 1] = val;
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/*
641 * Routing Table
642 *
643 * return the appropriate node for a routing tree "add" operation
644 * by either creating and inserting or by returning an existing
645 * node.
646 */
647
Wei Wang81eb8442017-10-06 12:06:11 -0700648static struct fib6_node *fib6_add_1(struct net *net,
649 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700650 struct fib6_node *root,
651 struct in6_addr *addr, int plen,
652 int offset, int allow_create,
653 int replace_required,
654 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 struct fib6_node *fn, *in, *ln;
657 struct fib6_node *pn = NULL;
658 struct rt6key *key;
659 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900660 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 RT6_TRACE("fib6_add_1\n");
663
664 /* insert node in tree */
665
666 fn = root;
667
668 do {
David Ahern8d1c8022018-04-17 17:33:26 -0700669 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700670 lockdep_is_held(&table->tb6_lock));
671 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /*
674 * Prefix match
675 */
676 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000677 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000678 if (!allow_create) {
679 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600680 NL_SET_ERR_MSG(extack,
681 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000682 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000683 return ERR_PTR(-ENOENT);
684 }
Joe Perchesf3213832012-05-15 14:11:53 +0000685 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000688 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Exact match ?
692 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (plen == fn->fn_bit) {
695 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500696 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700697 RCU_INIT_POINTER(fn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700698 fib6_info_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800699 /* remove null_entry in the root node */
700 } else if (fn->fn_flags & RTN_TL_ROOT &&
701 rcu_access_pointer(fn->leaf) ==
David Ahern421842e2018-04-17 17:33:18 -0700702 net->ipv6.fib6_null_entry) {
Wei Wang4512c432018-01-08 10:34:00 -0800703 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return fn;
707 }
708
709 /*
710 * We have more bits to go
711 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dir = addr_bit_set(addr, fn->fn_bit);
715 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700716 fn = dir ?
717 rcu_dereference_protected(fn->right,
718 lockdep_is_held(&table->tb6_lock)) :
719 rcu_dereference_protected(fn->left,
720 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 } while (fn);
722
Matti Vaittinen14df0152011-11-16 21:18:02 +0000723 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000724 /* We should not create new node because
725 * NLM_F_REPLACE was specified without NLM_F_CREATE
726 * I assume it is safe to require NLM_F_CREATE when
727 * REPLACE flag is used! Later we may want to remove the
728 * check for replace_required, because according
729 * to netlink specification, NLM_F_CREATE
730 * MUST be specified if new route is created.
731 * That would keep IPv6 consistent with IPv4
732 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000733 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600734 NL_SET_ERR_MSG(extack,
735 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000736 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000737 return ERR_PTR(-ENOENT);
738 }
Joe Perchesf3213832012-05-15 14:11:53 +0000739 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
742 * We walked to the bottom of tree.
743 * Create new leaf node without children.
744 */
745
Wei Wang81eb8442017-10-06 12:06:11 -0700746 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
David S. Miller507c9b12011-12-03 17:50:45 -0500748 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000749 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700751 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700754 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700756 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 return ln;
759
760
761insert_above:
762 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900763 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * we have a less significant route.
765 * we've to insert an intermediate node on the list
766 * this new node will point to the one we need to create
767 * and the current
768 */
769
Wei Wang66f5d6c2017-10-06 12:06:10 -0700770 pn = rcu_dereference_protected(fn->parent,
771 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* find 1st bit in difference between the 2 addrs.
774
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800775 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 but if it is >= plen, the value is ignored in any case.
777 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900778
fan.du9225b232013-07-22 14:21:09 +0800779 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900781 /*
782 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * / \
784 * (new leaf node)[ln] (old node)[fn]
785 */
786 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700787 in = node_alloc(net);
788 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900789
David S. Miller507c9b12011-12-03 17:50:45 -0500790 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700792 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700794 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000795 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900798 /*
799 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * RTN_RTINFO will
801 * be off since that an address that chooses one of
802 * the branches would not match less specific routes
803 * in the other branch
804 */
805
806 in->fn_bit = bit;
807
Wei Wang66f5d6c2017-10-06 12:06:10 -0700808 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700810 atomic_inc(&rcu_dereference_protected(in->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700811 lockdep_is_held(&table->tb6_lock))->fib6_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* update parent pointer */
814 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700815 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700817 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 ln->fn_bit = plen;
820
Wei Wang66f5d6c2017-10-06 12:06:10 -0700821 RCU_INIT_POINTER(ln->parent, in);
822 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700825 rcu_assign_pointer(in->right, ln);
826 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700828 rcu_assign_pointer(in->left, ln);
829 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 } else { /* plen <= bit */
832
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900833 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * (new leaf node)[ln]
835 * / \
836 * (old node)[fn] NULL
837 */
838
Wei Wang81eb8442017-10-06 12:06:11 -0700839 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David S. Miller507c9b12011-12-03 17:50:45 -0500841 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000842 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 ln->fn_bit = plen;
845
Wei Wang66f5d6c2017-10-06 12:06:10 -0700846 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700849 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700851 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Wei Wang66f5d6c2017-10-06 12:06:10 -0700853 rcu_assign_pointer(fn->parent, ln);
854
855 if (dir)
856 rcu_assign_pointer(pn->right, ln);
857 else
858 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 return ln;
861}
862
David Ahern5bcaa412018-04-20 15:38:01 -0700863static void fib6_drop_pcpu_from(struct fib6_info *f6i,
864 const struct fib6_table *table)
865{
866 int cpu;
867
868 /* release the reference to this fib entry from
869 * all of its cached pcpu routes
870 */
871 for_each_possible_cpu(cpu) {
872 struct rt6_info **ppcpu_rt;
873 struct rt6_info *pcpu_rt;
874
875 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
876 pcpu_rt = *ppcpu_rt;
877 if (pcpu_rt) {
David Aherna68886a2018-04-20 15:38:02 -0700878 struct fib6_info *from;
879
880 from = rcu_dereference_protected(pcpu_rt->from,
881 lockdep_is_held(&table->tb6_lock));
882 rcu_assign_pointer(pcpu_rt->from, NULL);
883 fib6_info_release(from);
David Ahern5bcaa412018-04-20 15:38:01 -0700884 }
885 }
886}
887
David Ahern8d1c8022018-04-17 17:33:26 -0700888static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100889 struct net *net)
890{
David Ahern93c2fb22018-04-18 15:38:59 -0700891 struct fib6_table *table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700892
David Ahern93c2fb22018-04-18 15:38:59 -0700893 if (atomic_read(&rt->fib6_ref) != 1) {
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100894 /* This route is used as dummy address holder in some split
895 * nodes. It is not leaked, but it still holds other resources,
896 * which must be released in time. So, scan ascendant nodes
897 * and replace dummy references to this route with references
898 * to still alive ones.
899 */
900 while (fn) {
David Ahern8d1c8022018-04-17 17:33:26 -0700901 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700902 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700903 struct fib6_info *new_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700904 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
905 new_leaf = fib6_find_prefix(net, table, fn);
David Ahern93c2fb22018-04-18 15:38:59 -0700906 atomic_inc(&new_leaf->fib6_ref);
David Ahern93531c62018-04-17 17:33:25 -0700907
Wei Wang66f5d6c2017-10-06 12:06:10 -0700908 rcu_assign_pointer(fn->leaf, new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700909 fib6_info_release(rt);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100910 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700911 fn = rcu_dereference_protected(fn->parent,
912 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100913 }
David Ahern93531c62018-04-17 17:33:25 -0700914
David Ahern5bcaa412018-04-20 15:38:01 -0700915 if (rt->rt6i_pcpu)
916 fib6_drop_pcpu_from(rt, table);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100917 }
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * Insert routing information in a node.
922 */
923
David Ahern8d1c8022018-04-17 17:33:26 -0700924static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700925 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700926 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
David Ahern8d1c8022018-04-17 17:33:26 -0700928 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700929 lockdep_is_held(&rt->fib6_table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700930 struct fib6_info *iter = NULL;
931 struct fib6_info __rcu **ins;
932 struct fib6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500933 int replace = (info->nlh &&
934 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
935 int add = (!info->nlh ||
936 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000937 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200938 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200939 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100940 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
David Ahern1f5e29c2017-01-31 16:51:37 -0800942 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
943 nlflags |= NLM_F_APPEND;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ins = &fn->leaf;
946
Wei Wang66f5d6c2017-10-06 12:06:10 -0700947 for (iter = leaf; iter;
David Ahern8fb11a92018-05-04 13:54:24 -0700948 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -0700949 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /*
951 * Search for duplicates
952 */
953
David Ahern93c2fb22018-04-18 15:38:59 -0700954 if (iter->fib6_metric == rt->fib6_metric) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * Same priority level
957 */
David S. Miller507c9b12011-12-03 17:50:45 -0500958 if (info->nlh &&
959 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000960 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200961
962 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000963 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200964 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
965 found++;
966 break;
967 }
968 if (rt_can_ecmp)
969 fallback_ins = fallback_ins ?: ins;
970 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
David Ahernf06b7542017-07-05 14:41:46 -0600973 if (rt6_duplicate_nexthop(iter, rt)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700974 if (rt->fib6_nsiblings)
975 rt->fib6_nsiblings = 0;
976 if (!(iter->fib6_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -EEXIST;
David Ahern93c2fb22018-04-18 15:38:59 -0700978 if (!(rt->fib6_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -0700979 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +0000980 else
David Ahern14895682018-04-17 17:33:17 -0700981 fib6_set_expires(iter, rt->expires);
David Ahernd4ead6b2018-04-17 17:33:16 -0700982 fib6_metric_set(iter, RTAX_MTU, rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EEXIST;
984 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000985 /* If we have the same destination and the same metric,
986 * but not the same gateway, then the route we try to
987 * add is sibling to this route, increment our counter
988 * of siblings, and later we will add our route to the
989 * list.
990 * Only static routes (which don't have flag
991 * RTF_EXPIRES) are used for ECMPv6.
992 *
993 * To avoid long list, we only had siblings if the
994 * route have a gateway.
995 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200996 if (rt_can_ecmp &&
997 rt6_qualify_for_ecmp(iter))
David Ahern93c2fb22018-04-18 15:38:59 -0700998 rt->fib6_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
David Ahern93c2fb22018-04-18 15:38:59 -07001001 if (iter->fib6_metric > rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003
Michal Kubeček27596472015-05-18 20:54:00 +02001004next_iter:
David Ahern8fb11a92018-05-04 13:54:24 -07001005 ins = &iter->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Michal Kubeček27596472015-05-18 20:54:00 +02001008 if (fallback_ins && !found) {
1009 /* No ECMP-able route found, replace first non-ECMP one */
1010 ins = fallback_ins;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001011 iter = rcu_dereference_protected(*ins,
David Ahern93c2fb22018-04-18 15:38:59 -07001012 lockdep_is_held(&rt->fib6_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001013 found++;
1014 }
1015
David S. Millerf11e6652007-03-24 20:36:25 -07001016 /* Reset round-robin state, if necessary */
1017 if (ins == &fn->leaf)
1018 fn->rr_ptr = NULL;
1019
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001020 /* Link this route to others same route. */
David Ahern93c2fb22018-04-18 15:38:59 -07001021 if (rt->fib6_nsiblings) {
1022 unsigned int fib6_nsiblings;
David Ahern8d1c8022018-04-17 17:33:26 -07001023 struct fib6_info *sibling, *temp_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001024
1025 /* Find the first route that have the same metric */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001026 sibling = leaf;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001027 while (sibling) {
David Ahern93c2fb22018-04-18 15:38:59 -07001028 if (sibling->fib6_metric == rt->fib6_metric &&
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +02001029 rt6_qualify_for_ecmp(sibling)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001030 list_add_tail(&rt->fib6_siblings,
1031 &sibling->fib6_siblings);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001032 break;
1033 }
David Ahern8fb11a92018-05-04 13:54:24 -07001034 sibling = rcu_dereference_protected(sibling->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07001035 lockdep_is_held(&rt->fib6_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001036 }
1037 /* For each sibling in the list, increment the counter of
1038 * siblings. BUG() if counters does not match, list of siblings
1039 * is broken!
1040 */
David Ahern93c2fb22018-04-18 15:38:59 -07001041 fib6_nsiblings = 0;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001042 list_for_each_entry_safe(sibling, temp_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001043 &rt->fib6_siblings, fib6_siblings) {
1044 sibling->fib6_nsiblings++;
1045 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1046 fib6_nsiblings++;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001047 }
David Ahern93c2fb22018-04-18 15:38:59 -07001048 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001049 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001050 }
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /*
1053 * insert node
1054 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001055 if (!replace) {
1056 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001057 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001059add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001060 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001061
David Ahern22330002018-03-27 18:21:59 -07001062 err = call_fib6_entry_notifiers(info->nl_net,
1063 FIB_EVENT_ENTRY_ADD,
1064 rt, extack);
1065 if (err)
1066 return err;
1067
David Ahern8fb11a92018-05-04 13:54:24 -07001068 rcu_assign_pointer(rt->fib6_next, iter);
David Ahern93c2fb22018-04-18 15:38:59 -07001069 atomic_inc(&rt->fib6_ref);
1070 rcu_assign_pointer(rt->fib6_node, fn);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001071 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001072 if (!info->skip_notify)
1073 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001074 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
David S. Miller507c9b12011-12-03 17:50:45 -05001076 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001077 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1078 fn->fn_flags |= RTN_RTINFO;
1079 }
1080
1081 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001082 int nsiblings;
1083
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001084 if (!found) {
1085 if (add)
1086 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001087 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001088 return -ENOENT;
1089 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001090
David Ahern22330002018-03-27 18:21:59 -07001091 err = call_fib6_entry_notifiers(info->nl_net,
1092 FIB_EVENT_ENTRY_REPLACE,
1093 rt, extack);
1094 if (err)
1095 return err;
1096
David Ahern93c2fb22018-04-18 15:38:59 -07001097 atomic_inc(&rt->fib6_ref);
1098 rcu_assign_pointer(rt->fib6_node, fn);
David Ahern8fb11a92018-05-04 13:54:24 -07001099 rt->fib6_next = iter->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001100 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001101 if (!info->skip_notify)
1102 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001103 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001104 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1105 fn->fn_flags |= RTN_RTINFO;
1106 }
David Ahern93c2fb22018-04-18 15:38:59 -07001107 nsiblings = iter->fib6_nsiblings;
1108 iter->fib6_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001109 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001110 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001111 fn->rr_ptr = NULL;
David Ahern93531c62018-04-17 17:33:25 -07001112 fib6_info_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001113
1114 if (nsiblings) {
1115 /* Replacing an ECMP route, remove all siblings */
David Ahern8fb11a92018-05-04 13:54:24 -07001116 ins = &rt->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001117 iter = rcu_dereference_protected(*ins,
David Ahern93c2fb22018-04-18 15:38:59 -07001118 lockdep_is_held(&rt->fib6_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001119 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07001120 if (iter->fib6_metric > rt->fib6_metric)
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001121 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001122 if (rt6_qualify_for_ecmp(iter)) {
David Ahern8fb11a92018-05-04 13:54:24 -07001123 *ins = iter->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001124 iter->fib6_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001125 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001126 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001127 fn->rr_ptr = NULL;
David Ahern93531c62018-04-17 17:33:25 -07001128 fib6_info_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001129 nsiblings--;
Wei Wang81eb8442017-10-06 12:06:11 -07001130 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001131 } else {
David Ahern8fb11a92018-05-04 13:54:24 -07001132 ins = &iter->fib6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001133 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001134 iter = rcu_dereference_protected(*ins,
David Ahern93c2fb22018-04-18 15:38:59 -07001135 lockdep_is_held(&rt->fib6_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001136 }
1137 WARN_ON(nsiblings != 0);
1138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 return 0;
1142}
1143
David Ahern8d1c8022018-04-17 17:33:26 -07001144static void fib6_start_gc(struct net *net, struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001146 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001147 (rt->fib6_flags & RTF_EXPIRES))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001148 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001149 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001152void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001154 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1155 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001156 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
David Ahern8d1c8022018-04-17 17:33:26 -07001159static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001160 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001161{
David Ahern93c2fb22018-04-18 15:38:59 -07001162 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1163 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001164
1165 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1166 smp_wmb();
1167 while (fn) {
1168 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001169 fn = rcu_dereference_protected(fn->parent,
David Ahern93c2fb22018-04-18 15:38:59 -07001170 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001171 }
1172}
1173
David Ahern8d1c8022018-04-17 17:33:26 -07001174void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001175{
1176 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179/*
1180 * Add routing information to the routing tree.
1181 * <destination addr>/<source addr>
1182 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001183 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
1185
David Ahern8d1c8022018-04-17 17:33:26 -07001186int fib6_add(struct fib6_node *root, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001187 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
David Ahern93c2fb22018-04-18 15:38:59 -07001189 struct fib6_table *table = rt->fib6_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001190 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001192 int allow_create = 1;
1193 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001194 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001195
1196 if (info->nlh) {
1197 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001198 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001199 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001200 replace_required = 1;
1201 }
1202 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001203 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Wei Wang81eb8442017-10-06 12:06:11 -07001205 fn = fib6_add_1(info->nl_net, table, root,
David Ahern93c2fb22018-04-18 15:38:59 -07001206 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1207 offsetof(struct fib6_info, fib6_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001208 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001209 if (IS_ERR(fn)) {
1210 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001211 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001215 pn = fn;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07001218 if (rt->fib6_src.plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct fib6_node *sn;
1220
Wei Wang66f5d6c2017-10-06 12:06:10 -07001221 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct fib6_node *sfn;
1223
1224 /*
1225 * Create subtree.
1226 *
1227 * fn[main tree]
1228 * |
1229 * sfn[subtree root]
1230 * \
1231 * sn[new leaf node]
1232 */
1233
1234 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001235 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001236 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001237 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
David Ahern93c2fb22018-04-18 15:38:59 -07001239 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001240 rcu_assign_pointer(sfn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001241 info->nl_net->ipv6.fib6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /* Now add the first leaf node to new subtree */
1245
Wei Wang81eb8442017-10-06 12:06:11 -07001246 sn = fib6_add_1(info->nl_net, table, sfn,
David Ahern93c2fb22018-04-18 15:38:59 -07001247 &rt->fib6_src.addr, rt->fib6_src.plen,
1248 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001249 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001251 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001253 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 in main tree.
1255 */
Wei Wang81eb8442017-10-06 12:06:11 -07001256 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001257 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001258 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
1261 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001262 rcu_assign_pointer(sfn->parent, fn);
1263 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001265 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
David Ahern93c2fb22018-04-18 15:38:59 -07001266 &rt->fib6_src.addr, rt->fib6_src.plen,
1267 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001268 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001270 if (IS_ERR(sn)) {
1271 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001272 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Wei Wang66f5d6c2017-10-06 12:06:10 -07001276 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001277 if (fn->fn_flags & RTN_TL_ROOT) {
1278 /* put back null_entry for root node */
1279 rcu_assign_pointer(fn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001280 info->nl_net->ipv6.fib6_null_entry);
Wei Wang591ff9e2018-01-18 10:40:03 -08001281 } else {
David Ahern93c2fb22018-04-18 15:38:59 -07001282 atomic_inc(&rt->fib6_ref);
Wei Wang591ff9e2018-01-18 10:40:03 -08001283 rcu_assign_pointer(fn->leaf, rt);
1284 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 fn = sn;
1287 }
1288#endif
1289
David Ahernd4ead6b2018-04-17 17:33:16 -07001290 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001291 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001292 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001293 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001297 if (err) {
1298#ifdef CONFIG_IPV6_SUBTREES
1299 /*
1300 * If fib6_add_1 has cleared the old leaf pointer in the
1301 * super-tree leaf node we have to find a new one for it.
1302 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001303 if (pn != fn) {
David Ahern8d1c8022018-04-17 17:33:26 -07001304 struct fib6_info *pn_leaf =
Wei Wang7bbfe002018-01-03 14:11:59 -08001305 rcu_dereference_protected(pn->leaf,
1306 lockdep_is_held(&table->tb6_lock));
1307 if (pn_leaf == rt) {
1308 pn_leaf = NULL;
1309 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001310 fib6_info_release(rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001311 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001312 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1313 pn_leaf = fib6_find_prefix(info->nl_net, table,
1314 pn);
1315#if RT6_DEBUG >= 2
1316 if (!pn_leaf) {
1317 WARN_ON(!pn_leaf);
1318 pn_leaf =
David Ahern421842e2018-04-17 17:33:18 -07001319 info->nl_net->ipv6.fib6_null_entry;
Wei Wang7bbfe002018-01-03 14:11:59 -08001320 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001321#endif
David Ahern93531c62018-04-17 17:33:25 -07001322 fib6_info_hold(pn_leaf);
Wei Wang7bbfe002018-01-03 14:11:59 -08001323 rcu_assign_pointer(pn->leaf, pn_leaf);
1324 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001325 }
1326#endif
Wei Wang348a4002017-08-18 17:14:49 -07001327 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return err;
1330
Wei Wang348a4002017-08-18 17:14:49 -07001331failure:
Wei Wang4512c432018-01-08 10:34:00 -08001332 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1333 * 1. fn is an intermediate node and we failed to add the new
1334 * route to it in both subtree creation failure and fib6_add_rt2node()
1335 * failure case.
1336 * 2. fn is the root node in the table and we fail to add the first
1337 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Wei Wang4512c432018-01-08 10:34:00 -08001339 if (fn &&
1340 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1341 (fn->fn_flags & RTN_TL_ROOT &&
1342 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001343 fib6_repair_tree(info->nl_net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347/*
1348 * Routing tree lookup
1349 *
1350 */
1351
1352struct lookup_args {
David Ahern8d1c8022018-04-17 17:33:26 -07001353 int offset; /* key offset on fib6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001354 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355};
1356
David Ahern64547432018-05-09 20:34:19 -07001357static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1358 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001361 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001363 if (unlikely(args->offset == 0))
1364 return NULL;
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /*
1367 * Descend on a tree
1368 */
1369
1370 fn = root;
1371
1372 for (;;) {
1373 struct fib6_node *next;
1374
1375 dir = addr_bit_set(args->addr, fn->fn_bit);
1376
Wei Wang66f5d6c2017-10-06 12:06:10 -07001377 next = dir ? rcu_dereference(fn->right) :
1378 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (next) {
1381 fn = next;
1382 continue;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 }
1386
David S. Miller507c9b12011-12-03 17:50:45 -05001387 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001388 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1389
1390 if (subtree || fn->fn_flags & RTN_RTINFO) {
David Ahern8d1c8022018-04-17 17:33:26 -07001391 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 struct rt6key *key;
1393
Wei Wang8d1040e2017-10-06 12:06:08 -07001394 if (!leaf)
1395 goto backtrack;
1396
1397 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001399 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1400#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001401 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001402 struct fib6_node *sfn;
David Ahern64547432018-05-09 20:34:19 -07001403 sfn = fib6_node_lookup_1(subtree,
1404 args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001405 if (!sfn)
1406 goto backtrack;
1407 fn = sfn;
1408 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001409#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001410 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001411 return fn;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001414backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001415 if (fn->fn_flags & RTN_ROOT)
1416 break;
1417
Wei Wang66f5d6c2017-10-06 12:06:10 -07001418 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
1421 return NULL;
1422}
1423
Wei Wang66f5d6c2017-10-06 12:06:10 -07001424/* called with rcu_read_lock() held
1425 */
David Ahern64547432018-05-09 20:34:19 -07001426struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1427 const struct in6_addr *daddr,
1428 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001431 struct lookup_args args[] = {
1432 {
David Ahern93c2fb22018-04-18 15:38:59 -07001433 .offset = offsetof(struct fib6_info, fib6_dst),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001434 .addr = daddr,
1435 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001437 {
David Ahern93c2fb22018-04-18 15:38:59 -07001438 .offset = offsetof(struct fib6_info, fib6_src),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001439 .addr = saddr,
1440 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001442 {
1443 .offset = 0, /* sentinel */
1444 }
1445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
David Ahern64547432018-05-09 20:34:19 -07001447 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001448 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 fn = root;
1450
1451 return fn;
1452}
1453
1454/*
1455 * Get node with specified destination prefix (and source prefix,
1456 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001457 * exact_match == true means we try to find fn with exact match of
1458 * the passed in prefix addr
1459 * exact_match == false means we try to find fn with longest prefix
1460 * match of the passed in prefix addr. This is useful for finding fn
1461 * for cached route as it will be stored in the exception table under
1462 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464
1465
Wang Yufen437de072014-03-28 12:07:04 +08001466static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1467 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001468 int plen, int offset,
1469 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Wei Wang38fbeee2017-10-06 12:06:02 -07001471 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 for (fn = root; fn ; ) {
David Ahern8d1c8022018-04-17 17:33:26 -07001474 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001475 struct rt6key *key;
1476
1477 /* This node is being deleted */
1478 if (!leaf) {
1479 if (plen <= fn->fn_bit)
1480 goto out;
1481 else
1482 goto next;
1483 }
1484
1485 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 /*
1488 * Prefix match
1489 */
1490 if (plen < fn->fn_bit ||
1491 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 if (plen == fn->fn_bit)
1495 return fn;
1496
Wei Wang38fbeee2017-10-06 12:06:02 -07001497 prev = fn;
1498
Wei Wang8d1040e2017-10-06 12:06:08 -07001499next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /*
1501 * We have more bits to go
1502 */
1503 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001504 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001506 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001508out:
1509 if (exact_match)
1510 return NULL;
1511 else
1512 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Wang Yufen437de072014-03-28 12:07:04 +08001515struct fib6_node *fib6_locate(struct fib6_node *root,
1516 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001517 const struct in6_addr *saddr, int src_len,
1518 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct fib6_node *fn;
1521
1522 fn = fib6_locate_1(root, daddr, dst_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001523 offsetof(struct fib6_info, fib6_dst),
Wei Wang38fbeee2017-10-06 12:06:02 -07001524 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526#ifdef CONFIG_IPV6_SUBTREES
1527 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001528 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001529 if (fn) {
1530 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1531
1532 if (subtree) {
1533 fn = fib6_locate_1(subtree, saddr, src_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001534 offsetof(struct fib6_info, fib6_src),
Wei Wang38fbeee2017-10-06 12:06:02 -07001535 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001536 }
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539#endif
1540
David S. Miller507c9b12011-12-03 17:50:45 -05001541 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return fn;
1543
1544 return NULL;
1545}
1546
1547
1548/*
1549 * Deletion
1550 *
1551 */
1552
David Ahern8d1c8022018-04-17 17:33:26 -07001553static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001554 struct fib6_table *table,
1555 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001557 struct fib6_node *child_left, *child_right;
1558
David S. Miller507c9b12011-12-03 17:50:45 -05001559 if (fn->fn_flags & RTN_ROOT)
David Ahern421842e2018-04-17 17:33:18 -07001560 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
David S. Miller507c9b12011-12-03 17:50:45 -05001562 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001563 child_left = rcu_dereference_protected(fn->left,
1564 lockdep_is_held(&table->tb6_lock));
1565 child_right = rcu_dereference_protected(fn->right,
1566 lockdep_is_held(&table->tb6_lock));
1567 if (child_left)
1568 return rcu_dereference_protected(child_left->leaf,
1569 lockdep_is_held(&table->tb6_lock));
1570 if (child_right)
1571 return rcu_dereference_protected(child_right->leaf,
1572 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001574 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 return NULL;
1577}
1578
1579/*
1580 * Called to trim the tree of intermediate nodes when possible. "fn"
1581 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001582 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
1584
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001585static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001586 struct fib6_table *table,
1587 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 int children;
1590 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001591 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001592 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int iter = 0;
1594
Wei Wang4512c432018-01-08 10:34:00 -08001595 /* Set fn->leaf to null_entry for root node. */
1596 if (fn->fn_flags & RTN_TL_ROOT) {
David Ahern421842e2018-04-17 17:33:18 -07001597 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
Wei Wang4512c432018-01-08 10:34:00 -08001598 return fn;
1599 }
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001602 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1603 lockdep_is_held(&table->tb6_lock));
1604 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1605 lockdep_is_held(&table->tb6_lock));
1606 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1607 lockdep_is_held(&table->tb6_lock));
1608 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1609 lockdep_is_held(&table->tb6_lock));
1610 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1611 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001612 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001613 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001614 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001615 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001616 struct fib6_info *new_fn_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1619 iter++;
1620
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001621 WARN_ON(fn->fn_flags & RTN_RTINFO);
1622 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001623 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 children = 0;
1626 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001627 if (fn_r)
1628 child = fn_r, children |= 1;
1629 if (fn_l)
1630 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001632 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633#ifdef CONFIG_IPV6_SUBTREES
1634 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001635 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636#endif
1637 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001638 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001640 if (!new_fn_leaf) {
1641 WARN_ON(!new_fn_leaf);
David Ahern421842e2018-04-17 17:33:18 -07001642 new_fn_leaf = net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644#endif
David Ahern93531c62018-04-17 17:33:25 -07001645 fib6_info_hold(new_fn_leaf);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001646 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1647 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001651 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001652 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001653 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 nstate = FWS_L;
1655 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001656 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001658 if (pn_r == fn)
1659 rcu_assign_pointer(pn->right, child);
1660 else if (pn_l == fn)
1661 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001663 else
1664 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665#endif
1666 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001667 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 nstate = FWS_R;
1669#ifdef CONFIG_IPV6_SUBTREES
1670 }
1671#endif
1672
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001673 read_lock(&net->ipv6.fib6_walker_lock);
1674 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001675 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001676 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1678 w->node = pn;
1679 w->state = nstate;
1680 }
1681 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (w->node == fn) {
1683 w->node = child;
1684 if (children&2) {
1685 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001686 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 } else {
1688 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001689 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 }
1692 }
1693 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001694 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Wei Wang81eb8442017-10-06 12:06:11 -07001696 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001697 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return pn;
1699
Wei Wang66f5d6c2017-10-06 12:06:10 -07001700 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001701 fib6_info_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 fn = pn;
1703 }
1704}
1705
Wei Wang66f5d6c2017-10-06 12:06:10 -07001706static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
David Ahern8d1c8022018-04-17 17:33:26 -07001707 struct fib6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001709 struct fib6_walker *w;
David Ahern8d1c8022018-04-17 17:33:26 -07001710 struct fib6_info *rt = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001711 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001712 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 RT6_TRACE("fib6_del_route\n");
1715
1716 /* Unlink it */
David Ahern8fb11a92018-05-04 13:54:24 -07001717 *rtp = rt->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001718 rt->fib6_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001719 net->ipv6.rt6_stats->fib_rt_entries--;
1720 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Wei Wang2b760fc2017-10-06 12:06:03 -07001722 /* Flush all cached dst in exception table */
1723 rt6_flush_exceptions(rt);
1724
David S. Millerf11e6652007-03-24 20:36:25 -07001725 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001726 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001727 fn->rr_ptr = NULL;
1728
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001729 /* Remove this entry from other siblings */
David Ahern93c2fb22018-04-18 15:38:59 -07001730 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07001731 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001732
1733 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001734 &rt->fib6_siblings, fib6_siblings)
1735 sibling->fib6_nsiblings--;
1736 rt->fib6_nsiblings = 0;
1737 list_del_init(&rt->fib6_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001738 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001739 }
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001742 read_lock(&net->ipv6.fib6_walker_lock);
1743 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (w->state == FWS_C && w->leaf == rt) {
1745 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Ahern8fb11a92018-05-04 13:54:24 -07001746 w->leaf = rcu_dereference_protected(rt->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001747 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001748 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 w->state = FWS_U;
1750 }
1751 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001752 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Wei Wang4512c432018-01-08 10:34:00 -08001754 /* If it was last route, call fib6_repair_tree() to:
1755 * 1. For root node, put back null_entry as how the table was created.
1756 * 2. For other nodes, expunge its radix tree node.
1757 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001758 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001759 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1760 fn->fn_flags &= ~RTN_RTINFO;
1761 net->ipv6.rt6_stats->fib_route_nodes--;
1762 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001763 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001766 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
David Ahern6c31e5a2017-10-27 17:37:13 -07001768 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001769 if (!info->skip_notify)
1770 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
David Ahern93531c62018-04-17 17:33:25 -07001771 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Wei Wang66f5d6c2017-10-06 12:06:10 -07001774/* Need to own table->tb6_lock */
David Ahern8d1c8022018-04-17 17:33:26 -07001775int fib6_del(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
David Ahern93c2fb22018-04-18 15:38:59 -07001777 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1778 lockdep_is_held(&rt->fib6_table->tb6_lock));
1779 struct fib6_table *table = rt->fib6_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001780 struct net *net = info->nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07001781 struct fib6_info __rcu **rtp;
1782 struct fib6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
David Ahern421842e2018-04-17 17:33:18 -07001784 if (!fn || rt == net->ipv6.fib6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -ENOENT;
1786
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001787 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * Walk the leaf entries looking for ourself
1791 */
1792
Wei Wang66f5d6c2017-10-06 12:06:10 -07001793 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
David Ahern8d1c8022018-04-17 17:33:26 -07001794 struct fib6_info *cur = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001795 lockdep_is_held(&table->tb6_lock));
1796 if (rt == cur) {
1797 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799 }
David Ahern8fb11a92018-05-04 13:54:24 -07001800 rtp_next = &cur->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 return -ENOENT;
1803}
1804
1805/*
1806 * Tree traversal function.
1807 *
1808 * Certainly, it is not interrupt safe.
1809 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1810 * It means, that we can modify tree during walking
1811 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001812 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 *
1814 * It guarantees that every node will be traversed,
1815 * and that it will be traversed only once.
1816 *
1817 * Callback function w->func may return:
1818 * 0 -> continue walking.
1819 * positive value -> walking is suspended (used by tree dumps,
1820 * and probably by gc, if it will be split to several slices)
1821 * negative value -> terminate walking.
1822 *
1823 * The function itself returns:
1824 * 0 -> walk is complete.
1825 * >0 -> walk is incomplete (i.e. suspended)
1826 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001827 *
1828 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
1830
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001831static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001833 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Wei Wang2b760fc2017-10-06 12:06:03 -07001835 /* w->root should always be table->tb6_root */
1836 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 for (;;) {
1839 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001840 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return 0;
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 switch (w->state) {
1844#ifdef CONFIG_IPV6_SUBTREES
1845 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001846 if (FIB6_SUBTREE(fn)) {
1847 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 continue;
1849 }
1850 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001851#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001852 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001854 left = rcu_dereference_protected(fn->left, 1);
1855 if (left) {
1856 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 w->state = FWS_INIT;
1858 continue;
1859 }
1860 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001861 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001863 right = rcu_dereference_protected(fn->right, 1);
1864 if (right) {
1865 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 w->state = FWS_INIT;
1867 continue;
1868 }
1869 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001870 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001871 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001873 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001874 int err;
1875
Eric Dumazetfa809e22012-06-25 15:37:19 -07001876 if (w->skip) {
1877 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001878 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001879 }
1880
1881 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (err)
1883 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001884
1885 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 continue;
1887 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001888skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001890 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 case FWS_U:
1892 if (fn == w->root)
1893 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001894 pn = rcu_dereference_protected(fn->parent, 1);
1895 left = rcu_dereference_protected(pn->left, 1);
1896 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 w->node = pn;
1898#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001899 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001900 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 w->state = FWS_L;
1902 continue;
1903 }
1904#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001905 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 w->state = FWS_R;
1907 continue;
1908 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001909 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001911 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 continue;
1913 }
1914#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001915 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916#endif
1917 }
1918 }
1919}
1920
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001921static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 int res;
1924
1925 w->state = FWS_INIT;
1926 w->node = w->root;
1927
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001928 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 res = fib6_walk_continue(w);
1930 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001931 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return res;
1933}
1934
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001935static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 int res;
David Ahern8d1c8022018-04-17 17:33:26 -07001938 struct fib6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001939 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001940 struct nl_info info = {
1941 .nl_net = c->net,
1942 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001944 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1945 w->node->fn_sernum != c->sernum)
1946 w->node->fn_sernum = c->sernum;
1947
1948 if (!c->func) {
1949 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1950 w->leaf = NULL;
1951 return 0;
1952 }
1953
Wei Wang66f5d6c2017-10-06 12:06:10 -07001954 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001956 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001958 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (res) {
1960#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001961 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001962 __func__, rt,
David Ahern93c2fb22018-04-18 15:38:59 -07001963 rcu_access_pointer(rt->fib6_node),
Wei Wang4e587ea2017-08-25 15:03:10 -07001964 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965#endif
1966 continue;
1967 }
1968 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001969 } else if (res == -2) {
David Ahern93c2fb22018-04-18 15:38:59 -07001970 if (WARN_ON(!rt->fib6_nsiblings))
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001971 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07001972 rt = list_last_entry(&rt->fib6_siblings,
1973 struct fib6_info, fib6_siblings);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001974 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001976 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 w->leaf = rt;
1979 return 0;
1980}
1981
1982/*
1983 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001986 * It may return -2 -> skip multipath route.
1987 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
1990
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001991static void fib6_clean_tree(struct net *net, struct fib6_node *root,
David Ahern8d1c8022018-04-17 17:33:26 -07001992 int (*func)(struct fib6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001993 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001995 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 c.w.root = root;
1998 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001999 c.w.count = 0;
2000 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002002 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002004 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002006 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002009static void __fib6_clean_all(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07002010 int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002011 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07002012{
Thomas Grafc71099a2006-08-04 23:20:06 -07002013 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002014 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07002015 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07002016
Patrick McHardy1b43af52006-08-10 23:11:17 -07002017 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07002018 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002019 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002020 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002021 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002022 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07002023 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002024 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07002025 }
2026 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07002027 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07002028}
2029
David Ahern8d1c8022018-04-17 17:33:26 -07002030void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002031 void *arg)
2032{
2033 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2034}
2035
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002036static void fib6_flush_trees(struct net *net)
2037{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002038 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002039
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002040 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002041}
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
2044 * Garbage collection
2045 */
2046
David Ahern8d1c8022018-04-17 17:33:26 -07002047static int fib6_age(struct fib6_info *rt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Michal Kubeček3570df92016-03-08 14:44:25 +01002049 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 unsigned long now = jiffies;
2051
2052 /*
2053 * check addrconf expiration here.
2054 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
2056
David Ahern93c2fb22018-04-18 15:38:59 -07002057 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
David Ahern14895682018-04-17 17:33:17 -07002058 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return -1;
2061 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002062 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064
Wei Wangc757faa2017-10-06 12:06:01 -07002065 /* Also age clones in the exception table.
2066 * Note, that clones are aged out
2067 * only if they are not in use now.
2068 */
2069 rt6_age_exceptions(rt, gc_args, now);
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072}
2073
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002074void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Michal Kubeček3570df92016-03-08 14:44:25 +01002076 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002077 unsigned long now;
2078
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002079 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002080 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2081 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002082 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2083 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002085 gc_args.timeout = expires ? (int)expires :
2086 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002087 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002088
Michal Kubeček3570df92016-03-08 14:44:25 +01002089 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002090 now = jiffies;
2091 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002094 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002095 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002096 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002097 else
2098 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002099 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Kees Cook86cb30e2017-10-17 20:21:24 -07002102static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002103{
Kees Cook86cb30e2017-10-17 20:21:24 -07002104 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2105
2106 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002107}
2108
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002109static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002110{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002111 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002112 int err;
2113
2114 err = fib6_notifier_init(net);
2115 if (err)
2116 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002117
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002118 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002119 rwlock_init(&net->ipv6.fib6_walker_lock);
2120 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002121 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002122
Benjamin Theryc5728722008-03-03 23:34:17 -08002123 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2124 if (!net->ipv6.rt6_stats)
2125 goto out_timer;
2126
Eric Dumazet10da66f2010-10-13 08:22:03 +00002127 /* Avoid false sharing : Use at least a full cache line */
2128 size = max_t(size_t, size, L1_CACHE_BYTES);
2129
2130 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002131 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002132 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002133
2134 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2135 GFP_KERNEL);
2136 if (!net->ipv6.fib6_main_tbl)
2137 goto out_fib_table_hash;
2138
2139 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002140 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002141 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002142 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2143 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002144 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002145
2146#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2147 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2148 GFP_KERNEL);
2149 if (!net->ipv6.fib6_local_tbl)
2150 goto out_fib6_main_tbl;
2151 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002152 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002153 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002154 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2155 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002156 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002157#endif
2158 fib6_tables_init(net);
2159
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002160 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002161
2162#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2163out_fib6_main_tbl:
2164 kfree(net->ipv6.fib6_main_tbl);
2165#endif
2166out_fib_table_hash:
2167 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002168out_rt6_stats:
2169 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002170out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002171 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002172 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002173}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002174
2175static void fib6_net_exit(struct net *net)
2176{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002177 unsigned int i;
2178
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002179 del_timer_sync(&net->ipv6.ip6_fib_timer);
2180
Eric Dumazet32a805b2017-09-08 15:48:47 -07002181 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002182 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2183 struct hlist_node *tmp;
2184 struct fib6_table *tb;
2185
2186 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2187 hlist_del(&tb->tb6_hlist);
2188 fib6_free_table(tb);
2189 }
2190 }
2191
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002192 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002193 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002194 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002195}
2196
2197static struct pernet_operations fib6_net_ops = {
2198 .init = fib6_net_init,
2199 .exit = fib6_net_exit,
2200};
2201
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002202int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002204 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2207 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002208 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002209 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002210 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002211 goto out;
2212
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002213 ret = register_pernet_subsys(&fib6_net_ops);
2214 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002215 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002216
Florian Westphal16feebc2017-12-02 21:44:08 +01002217 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2218 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002219 if (ret)
2220 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002221
2222 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002223out:
2224 return ret;
2225
David S. Millere8803b62012-06-16 01:12:19 -07002226out_unregister_subsys:
2227 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002228out_kmem_cache_create:
2229 kmem_cache_destroy(fib6_node_kmem);
2230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233void fib6_gc_cleanup(void)
2234{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002235 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 kmem_cache_destroy(fib6_node_kmem);
2237}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002238
2239#ifdef CONFIG_PROC_FS
2240
2241struct ipv6_route_iter {
2242 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002243 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002244 loff_t skip;
2245 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002246 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002247};
2248
2249static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2250{
David Ahern8d1c8022018-04-17 17:33:26 -07002251 struct fib6_info *rt = v;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002252 struct ipv6_route_iter *iter = seq->private;
David Ahern5e670d82018-04-17 17:33:14 -07002253 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002254
David Ahern93c2fb22018-04-18 15:38:59 -07002255 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002256
2257#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002258 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002259#else
2260 seq_puts(seq, "00000000000000000000000000000000 00 ");
2261#endif
David Ahern93c2fb22018-04-18 15:38:59 -07002262 if (rt->fib6_flags & RTF_GATEWAY)
David Ahern5e670d82018-04-17 17:33:14 -07002263 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002264 else
2265 seq_puts(seq, "00000000000000000000000000000000");
2266
David Ahern5e670d82018-04-17 17:33:14 -07002267 dev = rt->fib6_nh.nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002268 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
David Ahern93c2fb22018-04-18 15:38:59 -07002269 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2270 rt->fib6_flags, dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002271 iter->w.leaf = NULL;
2272 return 0;
2273}
2274
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002275static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002276{
2277 struct ipv6_route_iter *iter = w->args;
2278
2279 if (!iter->skip)
2280 return 1;
2281
2282 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002283 iter->w.leaf = rcu_dereference_protected(
David Ahern8fb11a92018-05-04 13:54:24 -07002284 iter->w.leaf->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002285 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002286 iter->skip--;
2287 if (!iter->skip && iter->w.leaf)
2288 return 1;
2289 } while (iter->w.leaf);
2290
2291 return 0;
2292}
2293
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002294static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2295 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002296{
2297 memset(&iter->w, 0, sizeof(iter->w));
2298 iter->w.func = ipv6_route_yield;
2299 iter->w.root = &iter->tbl->tb6_root;
2300 iter->w.state = FWS_INIT;
2301 iter->w.node = iter->w.root;
2302 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002303 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002304 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002305 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002306}
2307
2308static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2309 struct net *net)
2310{
2311 unsigned int h;
2312 struct hlist_node *node;
2313
2314 if (tbl) {
2315 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2316 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2317 } else {
2318 h = 0;
2319 node = NULL;
2320 }
2321
2322 while (!node && h < FIB6_TABLE_HASHSZ) {
2323 node = rcu_dereference_bh(
2324 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2325 }
2326 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2327}
2328
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002329static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2330{
2331 if (iter->sernum != iter->w.root->fn_sernum) {
2332 iter->sernum = iter->w.root->fn_sernum;
2333 iter->w.state = FWS_INIT;
2334 iter->w.node = iter->w.root;
2335 WARN_ON(iter->w.skip);
2336 iter->w.skip = iter->w.count;
2337 }
2338}
2339
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002340static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2341{
2342 int r;
David Ahern8d1c8022018-04-17 17:33:26 -07002343 struct fib6_info *n;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002344 struct net *net = seq_file_net(seq);
2345 struct ipv6_route_iter *iter = seq->private;
2346
2347 if (!v)
2348 goto iter_table;
2349
David Ahern8fb11a92018-05-04 13:54:24 -07002350 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002351 if (n) {
2352 ++*pos;
2353 return n;
2354 }
2355
2356iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002357 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002358 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002359 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002360 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002361 if (r > 0) {
2362 if (v)
2363 ++*pos;
2364 return iter->w.leaf;
2365 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002366 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002367 return NULL;
2368 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002369 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002370
2371 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2372 if (!iter->tbl)
2373 return NULL;
2374
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002375 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002376 goto iter_table;
2377}
2378
2379static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2380 __acquires(RCU_BH)
2381{
2382 struct net *net = seq_file_net(seq);
2383 struct ipv6_route_iter *iter = seq->private;
2384
2385 rcu_read_lock_bh();
2386 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2387 iter->skip = *pos;
2388
2389 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002390 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002391 return ipv6_route_seq_next(seq, NULL, pos);
2392 } else {
2393 return NULL;
2394 }
2395}
2396
2397static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2398{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002399 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002400 return w->node && !(w->state == FWS_U && w->node == w->root);
2401}
2402
2403static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2404 __releases(RCU_BH)
2405{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002406 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002407 struct ipv6_route_iter *iter = seq->private;
2408
2409 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002410 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002411
2412 rcu_read_unlock_bh();
2413}
2414
2415static const struct seq_operations ipv6_route_seq_ops = {
2416 .start = ipv6_route_seq_start,
2417 .next = ipv6_route_seq_next,
2418 .stop = ipv6_route_seq_stop,
2419 .show = ipv6_route_seq_show
2420};
2421
2422int ipv6_route_open(struct inode *inode, struct file *file)
2423{
2424 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2425 sizeof(struct ipv6_route_iter));
2426}
2427
2428#endif /* CONFIG_PROC_FS */