blob: f9132a6de917449e871a9d0b0c07b7bf91145d1a [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
David Ahern138118e2018-05-09 20:34:23 -0700357/* called with rcu lock held; no reference taken on fib6_info */
358struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
359 int flags)
360{
361 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, flags);
362}
363
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000364static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700365{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800366 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700367}
368
369#endif
370
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200371unsigned int fib6_tables_seq_read(struct net *net)
372{
373 unsigned int h, fib_seq = 0;
374
375 rcu_read_lock();
376 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
377 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
378 struct fib6_table *tb;
379
Wei Wang66f5d6c2017-10-06 12:06:10 -0700380 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200381 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200382 }
383 rcu_read_unlock();
384
385 return fib_seq;
386}
387
388static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
389 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700390 struct fib6_info *rt)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200391{
392 struct fib6_entry_notifier_info info = {
393 .rt = rt,
394 };
395
396 return call_fib6_notifier(nb, net, event_type, &info.info);
397}
398
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200399static int call_fib6_entry_notifiers(struct net *net,
400 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700401 struct fib6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700402 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200403{
404 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700405 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200406 .rt = rt,
407 };
408
David Ahern93c2fb22018-04-18 15:38:59 -0700409 rt->fib6_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200410 return call_fib6_notifiers(net, event_type, &info.info);
411}
412
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200413struct fib6_dump_arg {
414 struct net *net;
415 struct notifier_block *nb;
416};
417
David Ahern8d1c8022018-04-17 17:33:26 -0700418static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200419{
David Ahern421842e2018-04-17 17:33:18 -0700420 if (rt == arg->net->ipv6.fib6_null_entry)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200421 return;
422 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
423}
424
425static int fib6_node_dump(struct fib6_walker *w)
426{
David Ahern8d1c8022018-04-17 17:33:26 -0700427 struct fib6_info *rt;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200428
Wei Wang66f5d6c2017-10-06 12:06:10 -0700429 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200430 fib6_rt_dump(rt, w->args);
431 w->leaf = NULL;
432 return 0;
433}
434
435static void fib6_table_dump(struct net *net, struct fib6_table *tb,
436 struct fib6_walker *w)
437{
438 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700439 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200440 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700441 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200442}
443
444/* Called with rcu_read_lock() */
445int fib6_tables_dump(struct net *net, struct notifier_block *nb)
446{
447 struct fib6_dump_arg arg;
448 struct fib6_walker *w;
449 unsigned int h;
450
451 w = kzalloc(sizeof(*w), GFP_ATOMIC);
452 if (!w)
453 return -ENOMEM;
454
455 w->func = fib6_node_dump;
456 arg.net = net;
457 arg.nb = nb;
458 w->args = &arg;
459
460 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
461 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
462 struct fib6_table *tb;
463
464 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
465 fib6_table_dump(net, tb, w);
466 }
467
468 kfree(w);
469
470 return 0;
471}
472
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200473static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700474{
475 int res;
David Ahern8d1c8022018-04-17 17:33:26 -0700476 struct fib6_info *rt;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700477
Wei Wang66f5d6c2017-10-06 12:06:10 -0700478 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700479 res = rt6_dump_route(rt, w->args);
480 if (res < 0) {
481 /* Frame is full, suspend walking */
482 w->leaf = rt;
483 return 1;
484 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800485
486 /* Multipath routes are dumped in one route with the
487 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
488 * last sibling of this route (no need to dump the
489 * sibling routes again)
490 */
David Ahern93c2fb22018-04-18 15:38:59 -0700491 if (rt->fib6_nsiblings)
492 rt = list_last_entry(&rt->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -0700493 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -0700494 fib6_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700495 }
496 w->leaf = NULL;
497 return 0;
498}
499
500static void fib6_dump_end(struct netlink_callback *cb)
501{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100502 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200503 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700504
505 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800506 if (cb->args[4]) {
507 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100508 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800509 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700510 cb->args[2] = 0;
511 kfree(w);
512 }
Wang Yufen437de072014-03-28 12:07:04 +0800513 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700514 cb->args[1] = 3;
515}
516
517static int fib6_dump_done(struct netlink_callback *cb)
518{
519 fib6_dump_end(cb);
520 return cb->done ? cb->done(cb) : 0;
521}
522
523static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
524 struct netlink_callback *cb)
525{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100526 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200527 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700528 int res;
529
530 w = (void *)cb->args[2];
531 w->root = &table->tb6_root;
532
533 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000534 w->count = 0;
535 w->skip = 0;
536
Wei Wang66f5d6c2017-10-06 12:06:10 -0700537 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100538 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700539 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000540 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700541 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000542 cb->args[5] = w->root->fn_sernum;
543 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700544 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000545 if (cb->args[5] != w->root->fn_sernum) {
546 /* Begin at the root if the tree changed */
547 cb->args[5] = w->root->fn_sernum;
548 w->state = FWS_INIT;
549 w->node = w->root;
550 w->skip = w->count;
551 } else
552 w->skip = 0;
553
Wei Wang66f5d6c2017-10-06 12:06:10 -0700554 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700555 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700556 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800557 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100558 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800559 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700560 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700561 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800562
Patrick McHardy1b43af52006-08-10 23:11:17 -0700563 return res;
564}
565
Thomas Grafc127ea22007-03-22 11:58:32 -0700566static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700567{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900568 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700569 unsigned int h, s_h;
570 unsigned int e = 0, s_e;
571 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200572 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700573 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800574 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700575 int res = 0;
576
577 s_h = cb->args[0];
578 s_e = cb->args[1];
579
580 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500581 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700582 /* New dump:
583 *
584 * 1. hook callback destructor.
585 */
586 cb->args[3] = (long)cb->done;
587 cb->done = fib6_dump_done;
588
589 /*
590 * 2. allocate and initialize walker.
591 */
592 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500593 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700594 return -ENOMEM;
595 w->func = fib6_dump_node;
596 cb->args[2] = (long)w;
597 }
598
599 arg.skb = skb;
600 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700601 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700602 w->args = &arg;
603
Eric Dumazete67f88d2011-04-27 22:56:07 +0000604 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700605 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700606 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800607 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800608 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700609 if (e < s_e)
610 goto next;
611 res = fib6_dump_table(tb, skb, cb);
612 if (res != 0)
613 goto out;
614next:
615 e++;
616 }
617 }
618out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000619 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700620 cb->args[1] = e;
621 cb->args[0] = h;
622
623 res = res < 0 ? res : skb->len;
624 if (res <= 0)
625 fib6_dump_end(cb);
626 return res;
627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
David Ahern8d1c8022018-04-17 17:33:26 -0700629void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
David Ahernd4ead6b2018-04-17 17:33:16 -0700630{
631 if (!f6i)
632 return;
633
634 if (f6i->fib6_metrics == &dst_default_metrics) {
635 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
636
637 if (!p)
638 return;
639
640 refcount_set(&p->refcnt, 1);
641 f6i->fib6_metrics = p;
642 }
643
644 f6i->fib6_metrics->metrics[metric - 1] = val;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
648 * Routing Table
649 *
650 * return the appropriate node for a routing tree "add" operation
651 * by either creating and inserting or by returning an existing
652 * node.
653 */
654
Wei Wang81eb8442017-10-06 12:06:11 -0700655static struct fib6_node *fib6_add_1(struct net *net,
656 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700657 struct fib6_node *root,
658 struct in6_addr *addr, int plen,
659 int offset, int allow_create,
660 int replace_required,
661 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct fib6_node *fn, *in, *ln;
664 struct fib6_node *pn = NULL;
665 struct rt6key *key;
666 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900667 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 RT6_TRACE("fib6_add_1\n");
670
671 /* insert node in tree */
672
673 fn = root;
674
675 do {
David Ahern8d1c8022018-04-17 17:33:26 -0700676 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700677 lockdep_is_held(&table->tb6_lock));
678 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /*
681 * Prefix match
682 */
683 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000684 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000685 if (!allow_create) {
686 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600687 NL_SET_ERR_MSG(extack,
688 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000689 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000690 return ERR_PTR(-ENOENT);
691 }
Joe Perchesf3213832012-05-15 14:11:53 +0000692 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000695 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * Exact match ?
699 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (plen == fn->fn_bit) {
702 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500703 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700704 RCU_INIT_POINTER(fn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700705 fib6_info_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800706 /* remove null_entry in the root node */
707 } else if (fn->fn_flags & RTN_TL_ROOT &&
708 rcu_access_pointer(fn->leaf) ==
David Ahern421842e2018-04-17 17:33:18 -0700709 net->ipv6.fib6_null_entry) {
Wei Wang4512c432018-01-08 10:34:00 -0800710 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return fn;
714 }
715
716 /*
717 * We have more bits to go
718 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 dir = addr_bit_set(addr, fn->fn_bit);
722 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700723 fn = dir ?
724 rcu_dereference_protected(fn->right,
725 lockdep_is_held(&table->tb6_lock)) :
726 rcu_dereference_protected(fn->left,
727 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } while (fn);
729
Matti Vaittinen14df0152011-11-16 21:18:02 +0000730 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000731 /* We should not create new node because
732 * NLM_F_REPLACE was specified without NLM_F_CREATE
733 * I assume it is safe to require NLM_F_CREATE when
734 * REPLACE flag is used! Later we may want to remove the
735 * check for replace_required, because according
736 * to netlink specification, NLM_F_CREATE
737 * MUST be specified if new route is created.
738 * That would keep IPv6 consistent with IPv4
739 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000740 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600741 NL_SET_ERR_MSG(extack,
742 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000743 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000744 return ERR_PTR(-ENOENT);
745 }
Joe Perchesf3213832012-05-15 14:11:53 +0000746 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * We walked to the bottom of tree.
750 * Create new leaf node without children.
751 */
752
Wei Wang81eb8442017-10-06 12:06:11 -0700753 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
David S. Miller507c9b12011-12-03 17:50:45 -0500755 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000756 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700758 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700761 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700763 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 return ln;
766
767
768insert_above:
769 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900770 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * we have a less significant route.
772 * we've to insert an intermediate node on the list
773 * this new node will point to the one we need to create
774 * and the current
775 */
776
Wei Wang66f5d6c2017-10-06 12:06:10 -0700777 pn = rcu_dereference_protected(fn->parent,
778 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* find 1st bit in difference between the 2 addrs.
781
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800782 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 but if it is >= plen, the value is ignored in any case.
784 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900785
fan.du9225b232013-07-22 14:21:09 +0800786 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900788 /*
789 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * / \
791 * (new leaf node)[ln] (old node)[fn]
792 */
793 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700794 in = node_alloc(net);
795 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900796
David S. Miller507c9b12011-12-03 17:50:45 -0500797 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700799 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700801 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000802 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900805 /*
806 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * RTN_RTINFO will
808 * be off since that an address that chooses one of
809 * the branches would not match less specific routes
810 * in the other branch
811 */
812
813 in->fn_bit = bit;
814
Wei Wang66f5d6c2017-10-06 12:06:10 -0700815 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700817 atomic_inc(&rcu_dereference_protected(in->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700818 lockdep_is_held(&table->tb6_lock))->fib6_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* update parent pointer */
821 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700822 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700824 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 ln->fn_bit = plen;
827
Wei Wang66f5d6c2017-10-06 12:06:10 -0700828 RCU_INIT_POINTER(ln->parent, in);
829 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700832 rcu_assign_pointer(in->right, ln);
833 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700835 rcu_assign_pointer(in->left, ln);
836 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 } else { /* plen <= bit */
839
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900840 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * (new leaf node)[ln]
842 * / \
843 * (old node)[fn] NULL
844 */
845
Wei Wang81eb8442017-10-06 12:06:11 -0700846 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
David S. Miller507c9b12011-12-03 17:50:45 -0500848 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000849 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 ln->fn_bit = plen;
852
Wei Wang66f5d6c2017-10-06 12:06:10 -0700853 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700856 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700858 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Wei Wang66f5d6c2017-10-06 12:06:10 -0700860 rcu_assign_pointer(fn->parent, ln);
861
862 if (dir)
863 rcu_assign_pointer(pn->right, ln);
864 else
865 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 return ln;
868}
869
David Ahern5bcaa412018-04-20 15:38:01 -0700870static void fib6_drop_pcpu_from(struct fib6_info *f6i,
871 const struct fib6_table *table)
872{
873 int cpu;
874
875 /* release the reference to this fib entry from
876 * all of its cached pcpu routes
877 */
878 for_each_possible_cpu(cpu) {
879 struct rt6_info **ppcpu_rt;
880 struct rt6_info *pcpu_rt;
881
882 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
883 pcpu_rt = *ppcpu_rt;
884 if (pcpu_rt) {
David Aherna68886a2018-04-20 15:38:02 -0700885 struct fib6_info *from;
886
887 from = rcu_dereference_protected(pcpu_rt->from,
888 lockdep_is_held(&table->tb6_lock));
889 rcu_assign_pointer(pcpu_rt->from, NULL);
890 fib6_info_release(from);
David Ahern5bcaa412018-04-20 15:38:01 -0700891 }
892 }
893}
894
David Ahern8d1c8022018-04-17 17:33:26 -0700895static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100896 struct net *net)
897{
David Ahern93c2fb22018-04-18 15:38:59 -0700898 struct fib6_table *table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700899
David Ahern93c2fb22018-04-18 15:38:59 -0700900 if (atomic_read(&rt->fib6_ref) != 1) {
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100901 /* This route is used as dummy address holder in some split
902 * nodes. It is not leaked, but it still holds other resources,
903 * which must be released in time. So, scan ascendant nodes
904 * and replace dummy references to this route with references
905 * to still alive ones.
906 */
907 while (fn) {
David Ahern8d1c8022018-04-17 17:33:26 -0700908 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700909 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700910 struct fib6_info *new_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700911 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
912 new_leaf = fib6_find_prefix(net, table, fn);
David Ahern93c2fb22018-04-18 15:38:59 -0700913 atomic_inc(&new_leaf->fib6_ref);
David Ahern93531c62018-04-17 17:33:25 -0700914
Wei Wang66f5d6c2017-10-06 12:06:10 -0700915 rcu_assign_pointer(fn->leaf, new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700916 fib6_info_release(rt);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100917 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700918 fn = rcu_dereference_protected(fn->parent,
919 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100920 }
David Ahern93531c62018-04-17 17:33:25 -0700921
David Ahern5bcaa412018-04-20 15:38:01 -0700922 if (rt->rt6i_pcpu)
923 fib6_drop_pcpu_from(rt, table);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100924 }
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927/*
928 * Insert routing information in a node.
929 */
930
David Ahern8d1c8022018-04-17 17:33:26 -0700931static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700932 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700933 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
David Ahern8d1c8022018-04-17 17:33:26 -0700935 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700936 lockdep_is_held(&rt->fib6_table->tb6_lock));
David Ahernf34436a2018-05-21 10:26:53 -0700937 struct fib6_info *iter = NULL, *match = NULL;
David Ahern8d1c8022018-04-17 17:33:26 -0700938 struct fib6_info __rcu **ins;
David S. Miller507c9b12011-12-03 17:50:45 -0500939 int replace = (info->nlh &&
940 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
David Ahernf34436a2018-05-21 10:26:53 -0700941 int append = (info->nlh &&
942 (info->nlh->nlmsg_flags & NLM_F_APPEND));
David S. Miller507c9b12011-12-03 17:50:45 -0500943 int add = (!info->nlh ||
944 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000945 int found = 0;
Guillaume Nault73483c12016-09-07 17:21:40 +0200946 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David Ahernf34436a2018-05-21 10:26:53 -0700949 if (append)
David Ahern1f5e29c2017-01-31 16:51:37 -0800950 nlflags |= NLM_F_APPEND;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ins = &fn->leaf;
953
Wei Wang66f5d6c2017-10-06 12:06:10 -0700954 for (iter = leaf; iter;
David Ahern8fb11a92018-05-04 13:54:24 -0700955 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -0700956 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * Search for duplicates
959 */
960
David Ahern93c2fb22018-04-18 15:38:59 -0700961 if (iter->fib6_metric == rt->fib6_metric) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /*
963 * Same priority level
964 */
David S. Miller507c9b12011-12-03 17:50:45 -0500965 if (info->nlh &&
966 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000967 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200968
969 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000970 if (replace) {
David Ahernf34436a2018-05-21 10:26:53 -0700971 found++;
972 break;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
David Ahernf06b7542017-07-05 14:41:46 -0600975 if (rt6_duplicate_nexthop(iter, rt)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700976 if (rt->fib6_nsiblings)
977 rt->fib6_nsiblings = 0;
978 if (!(iter->fib6_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -EEXIST;
David Ahern93c2fb22018-04-18 15:38:59 -0700980 if (!(rt->fib6_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -0700981 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +0000982 else
David Ahern14895682018-04-17 17:33:17 -0700983 fib6_set_expires(iter, rt->expires);
David Ahernd4ead6b2018-04-17 17:33:16 -0700984 fib6_metric_set(iter, RTAX_MTU, rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -EEXIST;
986 }
David Ahernf34436a2018-05-21 10:26:53 -0700987
988 /* first route that matches */
989 if (!match)
990 match = iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
David Ahern93c2fb22018-04-18 15:38:59 -0700993 if (iter->fib6_metric > rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995
David Ahern8fb11a92018-05-04 13:54:24 -0700996 ins = &iter->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
David S. Millerf11e6652007-03-24 20:36:25 -0700999 /* Reset round-robin state, if necessary */
1000 if (ins == &fn->leaf)
1001 fn->rr_ptr = NULL;
1002
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001003 /* Link this route to others same route. */
David Ahernf34436a2018-05-21 10:26:53 -07001004 if (append && match) {
David Ahern8d1c8022018-04-17 17:33:26 -07001005 struct fib6_info *sibling, *temp_sibling;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001006
David Ahernf34436a2018-05-21 10:26:53 -07001007 if (rt->fib6_flags & RTF_REJECT) {
1008 NL_SET_ERR_MSG(extack,
1009 "Can not append a REJECT route");
1010 return -EINVAL;
1011 } else if (match->fib6_flags & RTF_REJECT) {
1012 NL_SET_ERR_MSG(extack,
1013 "Can not append to a REJECT route");
1014 return -EINVAL;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001015 }
David Ahernf34436a2018-05-21 10:26:53 -07001016 rt->fib6_nsiblings = match->fib6_nsiblings;
1017 list_add_tail(&rt->fib6_siblings, &match->fib6_siblings);
1018 match->fib6_nsiblings++;
1019
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001020 /* For each sibling in the list, increment the counter of
1021 * siblings. BUG() if counters does not match, list of siblings
1022 * is broken!
1023 */
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001024 list_for_each_entry_safe(sibling, temp_sibling,
David Ahernf34436a2018-05-21 10:26:53 -07001025 &match->fib6_siblings, fib6_siblings) {
David Ahern93c2fb22018-04-18 15:38:59 -07001026 sibling->fib6_nsiblings++;
David Ahernf34436a2018-05-21 10:26:53 -07001027 BUG_ON(sibling->fib6_nsiblings != match->fib6_nsiblings);
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001028 }
David Ahernf34436a2018-05-21 10:26:53 -07001029
1030 rt6_multipath_rebalance(match);
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001031 }
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
1034 * insert node
1035 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001036 if (!replace) {
David Ahernf34436a2018-05-21 10:26:53 -07001037 enum fib_event_type event;
1038
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001039 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001040 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001042add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001043 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001044
David Ahernf34436a2018-05-21 10:26:53 -07001045 event = append ? FIB_EVENT_ENTRY_APPEND : FIB_EVENT_ENTRY_ADD;
1046 err = call_fib6_entry_notifiers(info->nl_net, event, rt,
1047 extack);
David Ahern22330002018-03-27 18:21:59 -07001048 if (err)
1049 return err;
1050
David Ahern8fb11a92018-05-04 13:54:24 -07001051 rcu_assign_pointer(rt->fib6_next, iter);
David Ahern93c2fb22018-04-18 15:38:59 -07001052 atomic_inc(&rt->fib6_ref);
1053 rcu_assign_pointer(rt->fib6_node, fn);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001054 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001055 if (!info->skip_notify)
1056 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001057 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
David S. Miller507c9b12011-12-03 17:50:45 -05001059 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001060 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1061 fn->fn_flags |= RTN_RTINFO;
1062 }
1063
1064 } else {
David Ahernf34436a2018-05-21 10:26:53 -07001065 struct fib6_info *tmp;
Michal Kubeček27596472015-05-18 20:54:00 +02001066
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001067 if (!found) {
1068 if (add)
1069 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001070 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001071 return -ENOENT;
1072 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001073
David Ahern22330002018-03-27 18:21:59 -07001074 err = call_fib6_entry_notifiers(info->nl_net,
1075 FIB_EVENT_ENTRY_REPLACE,
1076 rt, extack);
1077 if (err)
1078 return err;
1079
David Ahernf34436a2018-05-21 10:26:53 -07001080 /* if route being replaced has siblings, set tmp to
1081 * last one, otherwise tmp is current route. this is
1082 * used to set fib6_next for new route
1083 */
1084 if (iter->fib6_nsiblings)
1085 tmp = list_last_entry(&iter->fib6_siblings,
1086 struct fib6_info,
1087 fib6_siblings);
1088 else
1089 tmp = iter;
1090
1091 /* insert new route */
David Ahern93c2fb22018-04-18 15:38:59 -07001092 atomic_inc(&rt->fib6_ref);
1093 rcu_assign_pointer(rt->fib6_node, fn);
David Ahernf34436a2018-05-21 10:26:53 -07001094 rt->fib6_next = tmp->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001095 rcu_assign_pointer(*ins, rt);
David Ahernf34436a2018-05-21 10:26:53 -07001096
David Ahern3b1137f2017-02-02 12:37:10 -08001097 if (!info->skip_notify)
1098 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001099 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001100 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1101 fn->fn_flags |= RTN_RTINFO;
1102 }
Michal Kubeček27596472015-05-18 20:54:00 +02001103
David Ahernf34436a2018-05-21 10:26:53 -07001104 /* delete old route */
1105 rt = iter;
1106
1107 if (rt->fib6_nsiblings) {
1108 struct fib6_info *tmp;
1109
Michal Kubeček27596472015-05-18 20:54:00 +02001110 /* Replacing an ECMP route, remove all siblings */
David Ahernf34436a2018-05-21 10:26:53 -07001111 list_for_each_entry_safe(iter, tmp, &rt->fib6_siblings,
1112 fib6_siblings) {
1113 iter->fib6_node = NULL;
1114 fib6_purge_rt(iter, fn, info->nl_net);
1115 if (rcu_access_pointer(fn->rr_ptr) == iter)
1116 fn->rr_ptr = NULL;
1117 fib6_info_release(iter);
1118
1119 rt->fib6_nsiblings--;
1120 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001121 }
Michal Kubeček27596472015-05-18 20:54:00 +02001122 }
David Ahernf34436a2018-05-21 10:26:53 -07001123
1124 WARN_ON(rt->fib6_nsiblings != 0);
1125
1126 rt->fib6_node = NULL;
1127 fib6_purge_rt(rt, fn, info->nl_net);
1128 if (rcu_access_pointer(fn->rr_ptr) == rt)
1129 fn->rr_ptr = NULL;
1130 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
1133 return 0;
1134}
1135
David Ahern8d1c8022018-04-17 17:33:26 -07001136static void fib6_start_gc(struct net *net, struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001138 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001139 (rt->fib6_flags & RTF_EXPIRES))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001140 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001141 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001144void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001146 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1147 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001148 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
David Ahern8d1c8022018-04-17 17:33:26 -07001151static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001152 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001153{
David Ahern93c2fb22018-04-18 15:38:59 -07001154 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1155 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001156
1157 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1158 smp_wmb();
1159 while (fn) {
1160 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001161 fn = rcu_dereference_protected(fn->parent,
David Ahern93c2fb22018-04-18 15:38:59 -07001162 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001163 }
1164}
1165
David Ahern8d1c8022018-04-17 17:33:26 -07001166void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001167{
1168 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/*
1172 * Add routing information to the routing tree.
1173 * <destination addr>/<source addr>
1174 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001175 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
1177
David Ahern8d1c8022018-04-17 17:33:26 -07001178int fib6_add(struct fib6_node *root, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001179 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
David Ahern93c2fb22018-04-18 15:38:59 -07001181 struct fib6_table *table = rt->fib6_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001182 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001184 int allow_create = 1;
1185 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001186 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001187
1188 if (info->nlh) {
1189 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001190 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001191 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001192 replace_required = 1;
1193 }
1194 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001195 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Wei Wang81eb8442017-10-06 12:06:11 -07001197 fn = fib6_add_1(info->nl_net, table, root,
David Ahern93c2fb22018-04-18 15:38:59 -07001198 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1199 offsetof(struct fib6_info, fib6_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001200 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001201 if (IS_ERR(fn)) {
1202 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001203 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001207 pn = fn;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07001210 if (rt->fib6_src.plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 struct fib6_node *sn;
1212
Wei Wang66f5d6c2017-10-06 12:06:10 -07001213 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct fib6_node *sfn;
1215
1216 /*
1217 * Create subtree.
1218 *
1219 * fn[main tree]
1220 * |
1221 * sfn[subtree root]
1222 * \
1223 * sn[new leaf node]
1224 */
1225
1226 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001227 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001228 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001229 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
David Ahern93c2fb22018-04-18 15:38:59 -07001231 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001232 rcu_assign_pointer(sfn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001233 info->nl_net->ipv6.fib6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* Now add the first leaf node to new subtree */
1237
Wei Wang81eb8442017-10-06 12:06:11 -07001238 sn = fib6_add_1(info->nl_net, table, sfn,
David Ahern93c2fb22018-04-18 15:38:59 -07001239 &rt->fib6_src.addr, rt->fib6_src.plen,
1240 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001241 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001243 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001245 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 in main tree.
1247 */
Wei Wang81eb8442017-10-06 12:06:11 -07001248 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001249 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001250 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
1253 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001254 rcu_assign_pointer(sfn->parent, fn);
1255 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001257 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
David Ahern93c2fb22018-04-18 15:38:59 -07001258 &rt->fib6_src.addr, rt->fib6_src.plen,
1259 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001260 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001262 if (IS_ERR(sn)) {
1263 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001264 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Wei Wang66f5d6c2017-10-06 12:06:10 -07001268 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001269 if (fn->fn_flags & RTN_TL_ROOT) {
1270 /* put back null_entry for root node */
1271 rcu_assign_pointer(fn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001272 info->nl_net->ipv6.fib6_null_entry);
Wei Wang591ff9e2018-01-18 10:40:03 -08001273 } else {
David Ahern93c2fb22018-04-18 15:38:59 -07001274 atomic_inc(&rt->fib6_ref);
Wei Wang591ff9e2018-01-18 10:40:03 -08001275 rcu_assign_pointer(fn->leaf, rt);
1276 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 fn = sn;
1279 }
1280#endif
1281
David Ahernd4ead6b2018-04-17 17:33:16 -07001282 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001283 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001284 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001285 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001289 if (err) {
1290#ifdef CONFIG_IPV6_SUBTREES
1291 /*
1292 * If fib6_add_1 has cleared the old leaf pointer in the
1293 * super-tree leaf node we have to find a new one for it.
1294 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001295 if (pn != fn) {
David Ahern8d1c8022018-04-17 17:33:26 -07001296 struct fib6_info *pn_leaf =
Wei Wang7bbfe002018-01-03 14:11:59 -08001297 rcu_dereference_protected(pn->leaf,
1298 lockdep_is_held(&table->tb6_lock));
1299 if (pn_leaf == rt) {
1300 pn_leaf = NULL;
1301 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001302 fib6_info_release(rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001303 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001304 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1305 pn_leaf = fib6_find_prefix(info->nl_net, table,
1306 pn);
1307#if RT6_DEBUG >= 2
1308 if (!pn_leaf) {
1309 WARN_ON(!pn_leaf);
1310 pn_leaf =
David Ahern421842e2018-04-17 17:33:18 -07001311 info->nl_net->ipv6.fib6_null_entry;
Wei Wang7bbfe002018-01-03 14:11:59 -08001312 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001313#endif
David Ahern93531c62018-04-17 17:33:25 -07001314 fib6_info_hold(pn_leaf);
Wei Wang7bbfe002018-01-03 14:11:59 -08001315 rcu_assign_pointer(pn->leaf, pn_leaf);
1316 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001317 }
1318#endif
Wei Wang348a4002017-08-18 17:14:49 -07001319 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return err;
1322
Wei Wang348a4002017-08-18 17:14:49 -07001323failure:
Wei Wang4512c432018-01-08 10:34:00 -08001324 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1325 * 1. fn is an intermediate node and we failed to add the new
1326 * route to it in both subtree creation failure and fib6_add_rt2node()
1327 * failure case.
1328 * 2. fn is the root node in the table and we fail to add the first
1329 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Wei Wang4512c432018-01-08 10:34:00 -08001331 if (fn &&
1332 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1333 (fn->fn_flags & RTN_TL_ROOT &&
1334 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001335 fib6_repair_tree(info->nl_net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339/*
1340 * Routing tree lookup
1341 *
1342 */
1343
1344struct lookup_args {
David Ahern8d1c8022018-04-17 17:33:26 -07001345 int offset; /* key offset on fib6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001346 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347};
1348
David Ahern64547432018-05-09 20:34:19 -07001349static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1350 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001353 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001355 if (unlikely(args->offset == 0))
1356 return NULL;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /*
1359 * Descend on a tree
1360 */
1361
1362 fn = root;
1363
1364 for (;;) {
1365 struct fib6_node *next;
1366
1367 dir = addr_bit_set(args->addr, fn->fn_bit);
1368
Wei Wang66f5d6c2017-10-06 12:06:10 -07001369 next = dir ? rcu_dereference(fn->right) :
1370 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (next) {
1373 fn = next;
1374 continue;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
1377 }
1378
David S. Miller507c9b12011-12-03 17:50:45 -05001379 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001380 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1381
1382 if (subtree || fn->fn_flags & RTN_RTINFO) {
David Ahern8d1c8022018-04-17 17:33:26 -07001383 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct rt6key *key;
1385
Wei Wang8d1040e2017-10-06 12:06:08 -07001386 if (!leaf)
1387 goto backtrack;
1388
1389 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001391 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1392#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001393 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001394 struct fib6_node *sfn;
David Ahern64547432018-05-09 20:34:19 -07001395 sfn = fib6_node_lookup_1(subtree,
1396 args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001397 if (!sfn)
1398 goto backtrack;
1399 fn = sfn;
1400 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001401#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001402 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001403 return fn;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001406backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001407 if (fn->fn_flags & RTN_ROOT)
1408 break;
1409
Wei Wang66f5d6c2017-10-06 12:06:10 -07001410 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
1413 return NULL;
1414}
1415
Wei Wang66f5d6c2017-10-06 12:06:10 -07001416/* called with rcu_read_lock() held
1417 */
David Ahern64547432018-05-09 20:34:19 -07001418struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1419 const struct in6_addr *daddr,
1420 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001423 struct lookup_args args[] = {
1424 {
David Ahern93c2fb22018-04-18 15:38:59 -07001425 .offset = offsetof(struct fib6_info, fib6_dst),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001426 .addr = daddr,
1427 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001429 {
David Ahern93c2fb22018-04-18 15:38:59 -07001430 .offset = offsetof(struct fib6_info, fib6_src),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001431 .addr = saddr,
1432 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001434 {
1435 .offset = 0, /* sentinel */
1436 }
1437 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
David Ahern64547432018-05-09 20:34:19 -07001439 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001440 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 fn = root;
1442
1443 return fn;
1444}
1445
1446/*
1447 * Get node with specified destination prefix (and source prefix,
1448 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001449 * exact_match == true means we try to find fn with exact match of
1450 * the passed in prefix addr
1451 * exact_match == false means we try to find fn with longest prefix
1452 * match of the passed in prefix addr. This is useful for finding fn
1453 * for cached route as it will be stored in the exception table under
1454 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
1456
1457
Wang Yufen437de072014-03-28 12:07:04 +08001458static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1459 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001460 int plen, int offset,
1461 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Wei Wang38fbeee2017-10-06 12:06:02 -07001463 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 for (fn = root; fn ; ) {
David Ahern8d1c8022018-04-17 17:33:26 -07001466 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001467 struct rt6key *key;
1468
1469 /* This node is being deleted */
1470 if (!leaf) {
1471 if (plen <= fn->fn_bit)
1472 goto out;
1473 else
1474 goto next;
1475 }
1476
1477 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /*
1480 * Prefix match
1481 */
1482 if (plen < fn->fn_bit ||
1483 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (plen == fn->fn_bit)
1487 return fn;
1488
Wei Wang38fbeee2017-10-06 12:06:02 -07001489 prev = fn;
1490
Wei Wang8d1040e2017-10-06 12:06:08 -07001491next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /*
1493 * We have more bits to go
1494 */
1495 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001496 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001498 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001500out:
1501 if (exact_match)
1502 return NULL;
1503 else
1504 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Wang Yufen437de072014-03-28 12:07:04 +08001507struct fib6_node *fib6_locate(struct fib6_node *root,
1508 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001509 const struct in6_addr *saddr, int src_len,
1510 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 struct fib6_node *fn;
1513
1514 fn = fib6_locate_1(root, daddr, dst_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001515 offsetof(struct fib6_info, fib6_dst),
Wei Wang38fbeee2017-10-06 12:06:02 -07001516 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518#ifdef CONFIG_IPV6_SUBTREES
1519 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001520 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001521 if (fn) {
1522 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1523
1524 if (subtree) {
1525 fn = fib6_locate_1(subtree, saddr, src_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001526 offsetof(struct fib6_info, fib6_src),
Wei Wang38fbeee2017-10-06 12:06:02 -07001527 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001528 }
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531#endif
1532
David S. Miller507c9b12011-12-03 17:50:45 -05001533 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return fn;
1535
1536 return NULL;
1537}
1538
1539
1540/*
1541 * Deletion
1542 *
1543 */
1544
David Ahern8d1c8022018-04-17 17:33:26 -07001545static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001546 struct fib6_table *table,
1547 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001549 struct fib6_node *child_left, *child_right;
1550
David S. Miller507c9b12011-12-03 17:50:45 -05001551 if (fn->fn_flags & RTN_ROOT)
David Ahern421842e2018-04-17 17:33:18 -07001552 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
David S. Miller507c9b12011-12-03 17:50:45 -05001554 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001555 child_left = rcu_dereference_protected(fn->left,
1556 lockdep_is_held(&table->tb6_lock));
1557 child_right = rcu_dereference_protected(fn->right,
1558 lockdep_is_held(&table->tb6_lock));
1559 if (child_left)
1560 return rcu_dereference_protected(child_left->leaf,
1561 lockdep_is_held(&table->tb6_lock));
1562 if (child_right)
1563 return rcu_dereference_protected(child_right->leaf,
1564 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001566 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 return NULL;
1569}
1570
1571/*
1572 * Called to trim the tree of intermediate nodes when possible. "fn"
1573 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001574 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
1576
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001577static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001578 struct fib6_table *table,
1579 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 int children;
1582 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001583 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001584 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 int iter = 0;
1586
Wei Wang4512c432018-01-08 10:34:00 -08001587 /* Set fn->leaf to null_entry for root node. */
1588 if (fn->fn_flags & RTN_TL_ROOT) {
David Ahern421842e2018-04-17 17:33:18 -07001589 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
Wei Wang4512c432018-01-08 10:34:00 -08001590 return fn;
1591 }
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001594 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1595 lockdep_is_held(&table->tb6_lock));
1596 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1597 lockdep_is_held(&table->tb6_lock));
1598 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1599 lockdep_is_held(&table->tb6_lock));
1600 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1601 lockdep_is_held(&table->tb6_lock));
1602 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1603 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001604 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001605 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001606 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001607 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001608 struct fib6_info *new_fn_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1611 iter++;
1612
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001613 WARN_ON(fn->fn_flags & RTN_RTINFO);
1614 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001615 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 children = 0;
1618 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001619 if (fn_r)
1620 child = fn_r, children |= 1;
1621 if (fn_l)
1622 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001624 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625#ifdef CONFIG_IPV6_SUBTREES
1626 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001627 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628#endif
1629 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001630 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001632 if (!new_fn_leaf) {
1633 WARN_ON(!new_fn_leaf);
David Ahern421842e2018-04-17 17:33:18 -07001634 new_fn_leaf = net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636#endif
David Ahern93531c62018-04-17 17:33:25 -07001637 fib6_info_hold(new_fn_leaf);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001638 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1639 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001643 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001644 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001645 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 nstate = FWS_L;
1647 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001648 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001650 if (pn_r == fn)
1651 rcu_assign_pointer(pn->right, child);
1652 else if (pn_l == fn)
1653 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001655 else
1656 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657#endif
1658 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001659 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 nstate = FWS_R;
1661#ifdef CONFIG_IPV6_SUBTREES
1662 }
1663#endif
1664
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001665 read_lock(&net->ipv6.fib6_walker_lock);
1666 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001667 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001668 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1670 w->node = pn;
1671 w->state = nstate;
1672 }
1673 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (w->node == fn) {
1675 w->node = child;
1676 if (children&2) {
1677 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001678 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 } else {
1680 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001681 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 }
1684 }
1685 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001686 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Wei Wang81eb8442017-10-06 12:06:11 -07001688 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001689 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return pn;
1691
Wei Wang66f5d6c2017-10-06 12:06:10 -07001692 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001693 fib6_info_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 fn = pn;
1695 }
1696}
1697
Wei Wang66f5d6c2017-10-06 12:06:10 -07001698static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
David Ahern8d1c8022018-04-17 17:33:26 -07001699 struct fib6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001701 struct fib6_walker *w;
David Ahern8d1c8022018-04-17 17:33:26 -07001702 struct fib6_info *rt = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001703 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001704 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 RT6_TRACE("fib6_del_route\n");
1707
1708 /* Unlink it */
David Ahern8fb11a92018-05-04 13:54:24 -07001709 *rtp = rt->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001710 rt->fib6_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001711 net->ipv6.rt6_stats->fib_rt_entries--;
1712 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Wei Wang2b760fc2017-10-06 12:06:03 -07001714 /* Flush all cached dst in exception table */
1715 rt6_flush_exceptions(rt);
1716
David S. Millerf11e6652007-03-24 20:36:25 -07001717 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001718 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001719 fn->rr_ptr = NULL;
1720
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001721 /* Remove this entry from other siblings */
David Ahern93c2fb22018-04-18 15:38:59 -07001722 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07001723 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001724
1725 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001726 &rt->fib6_siblings, fib6_siblings)
1727 sibling->fib6_nsiblings--;
1728 rt->fib6_nsiblings = 0;
1729 list_del_init(&rt->fib6_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001730 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd3182012-10-22 03:42:09 +00001731 }
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001734 read_lock(&net->ipv6.fib6_walker_lock);
1735 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (w->state == FWS_C && w->leaf == rt) {
1737 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Ahern8fb11a92018-05-04 13:54:24 -07001738 w->leaf = rcu_dereference_protected(rt->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001739 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001740 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 w->state = FWS_U;
1742 }
1743 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001744 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Wei Wang4512c432018-01-08 10:34:00 -08001746 /* If it was last route, call fib6_repair_tree() to:
1747 * 1. For root node, put back null_entry as how the table was created.
1748 * 2. For other nodes, expunge its radix tree node.
1749 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001750 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001751 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1752 fn->fn_flags &= ~RTN_RTINFO;
1753 net->ipv6.rt6_stats->fib_route_nodes--;
1754 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001755 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001758 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
David Ahern6c31e5a2017-10-27 17:37:13 -07001760 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001761 if (!info->skip_notify)
1762 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
David Ahern93531c62018-04-17 17:33:25 -07001763 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Wei Wang66f5d6c2017-10-06 12:06:10 -07001766/* Need to own table->tb6_lock */
David Ahern8d1c8022018-04-17 17:33:26 -07001767int fib6_del(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
David Ahern93c2fb22018-04-18 15:38:59 -07001769 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1770 lockdep_is_held(&rt->fib6_table->tb6_lock));
1771 struct fib6_table *table = rt->fib6_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001772 struct net *net = info->nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07001773 struct fib6_info __rcu **rtp;
1774 struct fib6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
David Ahern421842e2018-04-17 17:33:18 -07001776 if (!fn || rt == net->ipv6.fib6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return -ENOENT;
1778
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001779 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
1782 * Walk the leaf entries looking for ourself
1783 */
1784
Wei Wang66f5d6c2017-10-06 12:06:10 -07001785 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
David Ahern8d1c8022018-04-17 17:33:26 -07001786 struct fib6_info *cur = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001787 lockdep_is_held(&table->tb6_lock));
1788 if (rt == cur) {
1789 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791 }
David Ahern8fb11a92018-05-04 13:54:24 -07001792 rtp_next = &cur->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 return -ENOENT;
1795}
1796
1797/*
1798 * Tree traversal function.
1799 *
1800 * Certainly, it is not interrupt safe.
1801 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1802 * It means, that we can modify tree during walking
1803 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001804 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *
1806 * It guarantees that every node will be traversed,
1807 * and that it will be traversed only once.
1808 *
1809 * Callback function w->func may return:
1810 * 0 -> continue walking.
1811 * positive value -> walking is suspended (used by tree dumps,
1812 * and probably by gc, if it will be split to several slices)
1813 * negative value -> terminate walking.
1814 *
1815 * The function itself returns:
1816 * 0 -> walk is complete.
1817 * >0 -> walk is incomplete (i.e. suspended)
1818 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001819 *
1820 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
1822
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001823static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001825 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Wei Wang2b760fc2017-10-06 12:06:03 -07001827 /* w->root should always be table->tb6_root */
1828 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 for (;;) {
1831 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001832 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 switch (w->state) {
1836#ifdef CONFIG_IPV6_SUBTREES
1837 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001838 if (FIB6_SUBTREE(fn)) {
1839 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 continue;
1841 }
1842 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001843#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001844 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001846 left = rcu_dereference_protected(fn->left, 1);
1847 if (left) {
1848 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 w->state = FWS_INIT;
1850 continue;
1851 }
1852 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001853 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001855 right = rcu_dereference_protected(fn->right, 1);
1856 if (right) {
1857 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 w->state = FWS_INIT;
1859 continue;
1860 }
1861 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001862 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001863 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001865 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001866 int err;
1867
Eric Dumazetfa809e22012-06-25 15:37:19 -07001868 if (w->skip) {
1869 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001870 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001871 }
1872
1873 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (err)
1875 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001876
1877 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 continue;
1879 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001880skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001882 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 case FWS_U:
1884 if (fn == w->root)
1885 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001886 pn = rcu_dereference_protected(fn->parent, 1);
1887 left = rcu_dereference_protected(pn->left, 1);
1888 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 w->node = pn;
1890#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001891 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001892 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 w->state = FWS_L;
1894 continue;
1895 }
1896#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001897 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 w->state = FWS_R;
1899 continue;
1900 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001901 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001903 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 continue;
1905 }
1906#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001907 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908#endif
1909 }
1910 }
1911}
1912
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001913static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 int res;
1916
1917 w->state = FWS_INIT;
1918 w->node = w->root;
1919
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001920 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 res = fib6_walk_continue(w);
1922 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001923 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return res;
1925}
1926
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001927static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 int res;
David Ahern8d1c8022018-04-17 17:33:26 -07001930 struct fib6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001931 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001932 struct nl_info info = {
1933 .nl_net = c->net,
1934 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001936 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1937 w->node->fn_sernum != c->sernum)
1938 w->node->fn_sernum = c->sernum;
1939
1940 if (!c->func) {
1941 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1942 w->leaf = NULL;
1943 return 0;
1944 }
1945
Wei Wang66f5d6c2017-10-06 12:06:10 -07001946 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001948 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001950 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (res) {
1952#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001953 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001954 __func__, rt,
David Ahern93c2fb22018-04-18 15:38:59 -07001955 rcu_access_pointer(rt->fib6_node),
Wei Wang4e587ea2017-08-25 15:03:10 -07001956 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957#endif
1958 continue;
1959 }
1960 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001961 } else if (res == -2) {
David Ahern93c2fb22018-04-18 15:38:59 -07001962 if (WARN_ON(!rt->fib6_nsiblings))
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001963 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07001964 rt = list_last_entry(&rt->fib6_siblings,
1965 struct fib6_info, fib6_siblings);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001966 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001968 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970 w->leaf = rt;
1971 return 0;
1972}
1973
1974/*
1975 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001976 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001978 * It may return -2 -> skip multipath route.
1979 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
1982
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001983static void fib6_clean_tree(struct net *net, struct fib6_node *root,
David Ahern8d1c8022018-04-17 17:33:26 -07001984 int (*func)(struct fib6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001985 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001987 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 c.w.root = root;
1990 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001991 c.w.count = 0;
1992 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001994 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001996 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001998 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002001static void __fib6_clean_all(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07002002 int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002003 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07002004{
Thomas Grafc71099a2006-08-04 23:20:06 -07002005 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002006 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07002007 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07002008
Patrick McHardy1b43af52006-08-10 23:11:17 -07002009 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07002010 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002011 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002012 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002013 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002014 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07002015 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002016 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07002017 }
2018 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07002019 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07002020}
2021
David Ahern8d1c8022018-04-17 17:33:26 -07002022void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002023 void *arg)
2024{
2025 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2026}
2027
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002028static void fib6_flush_trees(struct net *net)
2029{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002030 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002031
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002032 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
2036 * Garbage collection
2037 */
2038
David Ahern8d1c8022018-04-17 17:33:26 -07002039static int fib6_age(struct fib6_info *rt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Michal Kubeček3570df92016-03-08 14:44:25 +01002041 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 unsigned long now = jiffies;
2043
2044 /*
2045 * check addrconf expiration here.
2046 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 */
2048
David Ahern93c2fb22018-04-18 15:38:59 -07002049 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
David Ahern14895682018-04-17 17:33:17 -07002050 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return -1;
2053 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002054 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
Wei Wangc757faa2017-10-06 12:06:01 -07002057 /* Also age clones in the exception table.
2058 * Note, that clones are aged out
2059 * only if they are not in use now.
2060 */
2061 rt6_age_exceptions(rt, gc_args, now);
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
2064}
2065
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002066void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Michal Kubeček3570df92016-03-08 14:44:25 +01002068 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002069 unsigned long now;
2070
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002071 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002072 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2073 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002074 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002077 gc_args.timeout = expires ? (int)expires :
2078 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002079 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002080
Michal Kubeček3570df92016-03-08 14:44:25 +01002081 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002082 now = jiffies;
2083 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002086 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002087 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002088 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002089 else
2090 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002091 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Kees Cook86cb30e2017-10-17 20:21:24 -07002094static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002095{
Kees Cook86cb30e2017-10-17 20:21:24 -07002096 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2097
2098 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002099}
2100
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002101static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002102{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002103 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002104 int err;
2105
2106 err = fib6_notifier_init(net);
2107 if (err)
2108 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002109
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002110 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002111 rwlock_init(&net->ipv6.fib6_walker_lock);
2112 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002113 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002114
Benjamin Theryc5728722008-03-03 23:34:17 -08002115 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2116 if (!net->ipv6.rt6_stats)
2117 goto out_timer;
2118
Eric Dumazet10da66f2010-10-13 08:22:03 +00002119 /* Avoid false sharing : Use at least a full cache line */
2120 size = max_t(size_t, size, L1_CACHE_BYTES);
2121
2122 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002123 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002124 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002125
2126 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2127 GFP_KERNEL);
2128 if (!net->ipv6.fib6_main_tbl)
2129 goto out_fib_table_hash;
2130
2131 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002132 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002133 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002134 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2135 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002136 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002137
2138#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2139 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2140 GFP_KERNEL);
2141 if (!net->ipv6.fib6_local_tbl)
2142 goto out_fib6_main_tbl;
2143 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002144 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002145 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002146 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2147 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002148 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002149#endif
2150 fib6_tables_init(net);
2151
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002152 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002153
2154#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2155out_fib6_main_tbl:
2156 kfree(net->ipv6.fib6_main_tbl);
2157#endif
2158out_fib_table_hash:
2159 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002160out_rt6_stats:
2161 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002162out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002163 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002164 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002165}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002166
2167static void fib6_net_exit(struct net *net)
2168{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002169 unsigned int i;
2170
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002171 del_timer_sync(&net->ipv6.ip6_fib_timer);
2172
Eric Dumazet32a805b2017-09-08 15:48:47 -07002173 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002174 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2175 struct hlist_node *tmp;
2176 struct fib6_table *tb;
2177
2178 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2179 hlist_del(&tb->tb6_hlist);
2180 fib6_free_table(tb);
2181 }
2182 }
2183
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002184 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002185 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002186 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002187}
2188
2189static struct pernet_operations fib6_net_ops = {
2190 .init = fib6_net_init,
2191 .exit = fib6_net_exit,
2192};
2193
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002194int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002196 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2199 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002200 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002201 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002202 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002203 goto out;
2204
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002205 ret = register_pernet_subsys(&fib6_net_ops);
2206 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002207 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002208
Florian Westphal16feebc2017-12-02 21:44:08 +01002209 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2210 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002211 if (ret)
2212 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002213
2214 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002215out:
2216 return ret;
2217
David S. Millere8803b62012-06-16 01:12:19 -07002218out_unregister_subsys:
2219 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002220out_kmem_cache_create:
2221 kmem_cache_destroy(fib6_node_kmem);
2222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
2225void fib6_gc_cleanup(void)
2226{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002227 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 kmem_cache_destroy(fib6_node_kmem);
2229}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002230
2231#ifdef CONFIG_PROC_FS
2232
2233struct ipv6_route_iter {
2234 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002235 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002236 loff_t skip;
2237 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002238 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002239};
2240
2241static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2242{
David Ahern8d1c8022018-04-17 17:33:26 -07002243 struct fib6_info *rt = v;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002244 struct ipv6_route_iter *iter = seq->private;
David Ahern5e670d82018-04-17 17:33:14 -07002245 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002246
David Ahern93c2fb22018-04-18 15:38:59 -07002247 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002248
2249#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002250 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002251#else
2252 seq_puts(seq, "00000000000000000000000000000000 00 ");
2253#endif
David Ahern93c2fb22018-04-18 15:38:59 -07002254 if (rt->fib6_flags & RTF_GATEWAY)
David Ahern5e670d82018-04-17 17:33:14 -07002255 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002256 else
2257 seq_puts(seq, "00000000000000000000000000000000");
2258
David Ahern5e670d82018-04-17 17:33:14 -07002259 dev = rt->fib6_nh.nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002260 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
David Ahern93c2fb22018-04-18 15:38:59 -07002261 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2262 rt->fib6_flags, dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002263 iter->w.leaf = NULL;
2264 return 0;
2265}
2266
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002267static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002268{
2269 struct ipv6_route_iter *iter = w->args;
2270
2271 if (!iter->skip)
2272 return 1;
2273
2274 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002275 iter->w.leaf = rcu_dereference_protected(
David Ahern8fb11a92018-05-04 13:54:24 -07002276 iter->w.leaf->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002277 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002278 iter->skip--;
2279 if (!iter->skip && iter->w.leaf)
2280 return 1;
2281 } while (iter->w.leaf);
2282
2283 return 0;
2284}
2285
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002286static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2287 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002288{
2289 memset(&iter->w, 0, sizeof(iter->w));
2290 iter->w.func = ipv6_route_yield;
2291 iter->w.root = &iter->tbl->tb6_root;
2292 iter->w.state = FWS_INIT;
2293 iter->w.node = iter->w.root;
2294 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002295 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002296 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002297 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002298}
2299
2300static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2301 struct net *net)
2302{
2303 unsigned int h;
2304 struct hlist_node *node;
2305
2306 if (tbl) {
2307 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2308 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2309 } else {
2310 h = 0;
2311 node = NULL;
2312 }
2313
2314 while (!node && h < FIB6_TABLE_HASHSZ) {
2315 node = rcu_dereference_bh(
2316 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2317 }
2318 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2319}
2320
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002321static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2322{
2323 if (iter->sernum != iter->w.root->fn_sernum) {
2324 iter->sernum = iter->w.root->fn_sernum;
2325 iter->w.state = FWS_INIT;
2326 iter->w.node = iter->w.root;
2327 WARN_ON(iter->w.skip);
2328 iter->w.skip = iter->w.count;
2329 }
2330}
2331
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002332static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2333{
2334 int r;
David Ahern8d1c8022018-04-17 17:33:26 -07002335 struct fib6_info *n;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002336 struct net *net = seq_file_net(seq);
2337 struct ipv6_route_iter *iter = seq->private;
2338
2339 if (!v)
2340 goto iter_table;
2341
David Ahern8fb11a92018-05-04 13:54:24 -07002342 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002343 if (n) {
2344 ++*pos;
2345 return n;
2346 }
2347
2348iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002349 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002350 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002351 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002352 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002353 if (r > 0) {
2354 if (v)
2355 ++*pos;
2356 return iter->w.leaf;
2357 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002358 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002359 return NULL;
2360 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002361 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002362
2363 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2364 if (!iter->tbl)
2365 return NULL;
2366
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002367 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002368 goto iter_table;
2369}
2370
2371static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2372 __acquires(RCU_BH)
2373{
2374 struct net *net = seq_file_net(seq);
2375 struct ipv6_route_iter *iter = seq->private;
2376
2377 rcu_read_lock_bh();
2378 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2379 iter->skip = *pos;
2380
2381 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002382 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002383 return ipv6_route_seq_next(seq, NULL, pos);
2384 } else {
2385 return NULL;
2386 }
2387}
2388
2389static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2390{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002391 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002392 return w->node && !(w->state == FWS_U && w->node == w->root);
2393}
2394
2395static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2396 __releases(RCU_BH)
2397{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002398 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002399 struct ipv6_route_iter *iter = seq->private;
2400
2401 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002402 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002403
2404 rcu_read_unlock_bh();
2405}
2406
2407static const struct seq_operations ipv6_route_seq_ops = {
2408 .start = ipv6_route_seq_start,
2409 .next = ipv6_route_seq_next,
2410 .stop = ipv6_route_seq_stop,
2411 .show = ipv6_route_seq_show
2412};
2413
2414int ipv6_route_open(struct inode *inode, struct file *file)
2415{
2416 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2417 sizeof(struct ipv6_route_iter));
2418}
2419
2420#endif /* CONFIG_PROC_FS */