blob: c861a6d4671d3f0f82f68752d16a8192ac649f8a [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
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700170void fib6_info_destroy_rcu(struct rcu_head *head)
David Aherna64efe142018-04-17 17:33:24 -0700171{
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700172 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
David Aherna64efe142018-04-17 17:33:24 -0700173 struct rt6_exception_bucket *bucket;
David S. Millere6aed042018-08-01 21:32:30 -0700174 struct dst_metrics *m;
David Aherna64efe142018-04-17 17:33:24 -0700175
David Ahern93c2fb22018-04-18 15:38:59 -0700176 WARN_ON(f6i->fib6_node);
David Aherna64efe142018-04-17 17:33:24 -0700177
178 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
179 if (bucket) {
180 f6i->rt6i_exception_bucket = NULL;
181 kfree(bucket);
182 }
183
184 if (f6i->rt6i_pcpu) {
185 int cpu;
186
187 for_each_possible_cpu(cpu) {
188 struct rt6_info **ppcpu_rt;
189 struct rt6_info *pcpu_rt;
190
191 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
192 pcpu_rt = *ppcpu_rt;
193 if (pcpu_rt) {
194 dst_dev_put(&pcpu_rt->dst);
195 dst_release(&pcpu_rt->dst);
196 *ppcpu_rt = NULL;
197 }
198 }
199 }
200
David Ahern80f1a0f2018-08-20 13:02:41 -0700201 lwtstate_put(f6i->fib6_nh.nh_lwtstate);
202
David Aherna64efe142018-04-17 17:33:24 -0700203 if (f6i->fib6_nh.nh_dev)
204 dev_put(f6i->fib6_nh.nh_dev);
205
David S. Millere6aed042018-08-01 21:32:30 -0700206 m = f6i->fib6_metrics;
207 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
208 kfree(m);
David Ahern93531c62018-04-17 17:33:25 -0700209
David Aherna64efe142018-04-17 17:33:24 -0700210 kfree(f6i);
211}
Eric Dumazet9b0a8da2018-06-18 05:24:31 -0700212EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
David Aherna64efe142018-04-17 17:33:24 -0700213
Wei Wang81eb8442017-10-06 12:06:11 -0700214static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct fib6_node *fn;
217
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800218 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700219 if (fn)
220 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 return fn;
223}
224
Wei Wang81eb8442017-10-06 12:06:11 -0700225static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700228 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Wei Wangc5cff852017-08-21 09:47:10 -0700231static void node_free_rcu(struct rcu_head *head)
232{
233 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
234
235 kmem_cache_free(fib6_node_kmem, fn);
236}
237
Wei Wang81eb8442017-10-06 12:06:11 -0700238static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700239{
240 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700241 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700242}
243
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200244static void fib6_free_table(struct fib6_table *table)
245{
246 inetpeer_invalidate_tree(&table->tb6_peers);
247 kfree(table);
248}
249
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700251{
252 unsigned int h;
253
Thomas Graf375216a2006-10-21 20:20:54 -0700254 /*
255 * Initialize table lock at a single place to give lockdep a key,
256 * tables aren't visible prior to being linked to the list.
257 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700258 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700259 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700260
261 /*
262 * No protection necessary, this is the only list mutatation
263 * operation, tables never disappear once they exist.
264 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800265 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700266}
267
268#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800269
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800270static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700271{
272 struct fib6_table *table;
273
274 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500275 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700276 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700277 rcu_assign_pointer(table->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -0700278 net->ipv6.fib6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700279 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700280 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700281 }
282
283 return table;
284}
285
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800286struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700287{
288 struct fib6_table *tb;
289
290 if (id == 0)
291 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800292 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700293 if (tb)
294 return tb;
295
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800296 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500297 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800298 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700299
300 return tb;
301}
David Ahernb3b46632016-05-04 21:46:12 -0700302EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700303
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800304struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700305{
306 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800307 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700308 unsigned int h;
309
310 if (id == 0)
311 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700312 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700313 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800314 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800315 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700316 if (tb->tb6_id == id) {
317 rcu_read_unlock();
318 return tb;
319 }
320 }
321 rcu_read_unlock();
322
323 return NULL;
324}
David Ahernc4850682015-10-12 11:47:08 -0700325EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700326
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000327static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700328{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800329 fib6_link_table(net, net->ipv6.fib6_main_tbl);
330 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700331}
Thomas Grafc71099a2006-08-04 23:20:06 -0700332#else
333
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800334struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700335{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800336 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700337}
338
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800339struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700340{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800341 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700342}
343
David S. Miller4c9483b2011-03-12 16:22:43 -0500344struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -0800345 const struct sk_buff *skb,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800346 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700347{
lucienab997ad2015-10-23 15:36:53 +0800348 struct rt6_info *rt;
349
David Ahernb75cc8f2018-03-02 08:32:17 -0800350 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300351 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800352 ip6_rt_put(rt);
353 rt = net->ipv6.ip6_null_entry;
354 dst_hold(&rt->dst);
355 }
356
357 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700358}
359
David Ahern138118e2018-05-09 20:34:23 -0700360/* called with rcu lock held; no reference taken on fib6_info */
361struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
362 int flags)
363{
364 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, flags);
365}
366
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000367static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700368{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800369 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700370}
371
372#endif
373
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200374unsigned int fib6_tables_seq_read(struct net *net)
375{
376 unsigned int h, fib_seq = 0;
377
378 rcu_read_lock();
379 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
380 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
381 struct fib6_table *tb;
382
Wei Wang66f5d6c2017-10-06 12:06:10 -0700383 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200384 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200385 }
386 rcu_read_unlock();
387
388 return fib_seq;
389}
390
391static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
392 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700393 struct fib6_info *rt)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200394{
395 struct fib6_entry_notifier_info info = {
396 .rt = rt,
397 };
398
399 return call_fib6_notifier(nb, net, event_type, &info.info);
400}
401
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200402static int call_fib6_entry_notifiers(struct net *net,
403 enum fib_event_type event_type,
David Ahern8d1c8022018-04-17 17:33:26 -0700404 struct fib6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700405 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200406{
407 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700408 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200409 .rt = rt,
410 };
411
David Ahern93c2fb22018-04-18 15:38:59 -0700412 rt->fib6_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200413 return call_fib6_notifiers(net, event_type, &info.info);
414}
415
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200416struct fib6_dump_arg {
417 struct net *net;
418 struct notifier_block *nb;
419};
420
David Ahern8d1c8022018-04-17 17:33:26 -0700421static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200422{
David Ahern421842e2018-04-17 17:33:18 -0700423 if (rt == arg->net->ipv6.fib6_null_entry)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200424 return;
425 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
426}
427
428static int fib6_node_dump(struct fib6_walker *w)
429{
David Ahern8d1c8022018-04-17 17:33:26 -0700430 struct fib6_info *rt;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200431
Wei Wang66f5d6c2017-10-06 12:06:10 -0700432 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200433 fib6_rt_dump(rt, w->args);
434 w->leaf = NULL;
435 return 0;
436}
437
438static void fib6_table_dump(struct net *net, struct fib6_table *tb,
439 struct fib6_walker *w)
440{
441 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700442 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200443 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700444 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200445}
446
447/* Called with rcu_read_lock() */
448int fib6_tables_dump(struct net *net, struct notifier_block *nb)
449{
450 struct fib6_dump_arg arg;
451 struct fib6_walker *w;
452 unsigned int h;
453
454 w = kzalloc(sizeof(*w), GFP_ATOMIC);
455 if (!w)
456 return -ENOMEM;
457
458 w->func = fib6_node_dump;
459 arg.net = net;
460 arg.nb = nb;
461 w->args = &arg;
462
463 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
464 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
465 struct fib6_table *tb;
466
467 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
468 fib6_table_dump(net, tb, w);
469 }
470
471 kfree(w);
472
473 return 0;
474}
475
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200476static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700477{
478 int res;
David Ahern8d1c8022018-04-17 17:33:26 -0700479 struct fib6_info *rt;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700480
Wei Wang66f5d6c2017-10-06 12:06:10 -0700481 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700482 res = rt6_dump_route(rt, w->args);
483 if (res < 0) {
484 /* Frame is full, suspend walking */
485 w->leaf = rt;
486 return 1;
487 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800488
489 /* Multipath routes are dumped in one route with the
490 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
491 * last sibling of this route (no need to dump the
492 * sibling routes again)
493 */
David Ahern93c2fb22018-04-18 15:38:59 -0700494 if (rt->fib6_nsiblings)
495 rt = list_last_entry(&rt->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -0700496 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -0700497 fib6_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700498 }
499 w->leaf = NULL;
500 return 0;
501}
502
503static void fib6_dump_end(struct netlink_callback *cb)
504{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100505 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200506 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507
508 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800509 if (cb->args[4]) {
510 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100511 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800512 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700513 cb->args[2] = 0;
514 kfree(w);
515 }
Wang Yufen437de072014-03-28 12:07:04 +0800516 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700517 cb->args[1] = 3;
518}
519
520static int fib6_dump_done(struct netlink_callback *cb)
521{
522 fib6_dump_end(cb);
523 return cb->done ? cb->done(cb) : 0;
524}
525
526static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
527 struct netlink_callback *cb)
528{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100529 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200530 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700531 int res;
532
533 w = (void *)cb->args[2];
534 w->root = &table->tb6_root;
535
536 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000537 w->count = 0;
538 w->skip = 0;
539
Wei Wang66f5d6c2017-10-06 12:06:10 -0700540 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100541 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700542 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000543 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700544 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000545 cb->args[5] = w->root->fn_sernum;
546 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700547 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000548 if (cb->args[5] != w->root->fn_sernum) {
549 /* Begin at the root if the tree changed */
550 cb->args[5] = w->root->fn_sernum;
551 w->state = FWS_INIT;
552 w->node = w->root;
553 w->skip = w->count;
554 } else
555 w->skip = 0;
556
Wei Wang66f5d6c2017-10-06 12:06:10 -0700557 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700558 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700559 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800560 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100561 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800562 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700563 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700564 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800565
Patrick McHardy1b43af52006-08-10 23:11:17 -0700566 return res;
567}
568
Thomas Grafc127ea22007-03-22 11:58:32 -0700569static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700570{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900571 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700572 unsigned int h, s_h;
573 unsigned int e = 0, s_e;
574 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200575 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700576 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800577 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700578 int res = 0;
579
580 s_h = cb->args[0];
581 s_e = cb->args[1];
582
583 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500584 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700585 /* New dump:
586 *
587 * 1. hook callback destructor.
588 */
589 cb->args[3] = (long)cb->done;
590 cb->done = fib6_dump_done;
591
592 /*
593 * 2. allocate and initialize walker.
594 */
595 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500596 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700597 return -ENOMEM;
598 w->func = fib6_dump_node;
599 cb->args[2] = (long)w;
600 }
601
602 arg.skb = skb;
603 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700604 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700605 w->args = &arg;
606
Eric Dumazete67f88d2011-04-27 22:56:07 +0000607 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700608 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700609 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800610 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800611 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700612 if (e < s_e)
613 goto next;
614 res = fib6_dump_table(tb, skb, cb);
615 if (res != 0)
616 goto out;
617next:
618 e++;
619 }
620 }
621out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000622 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700623 cb->args[1] = e;
624 cb->args[0] = h;
625
626 res = res < 0 ? res : skb->len;
627 if (res <= 0)
628 fib6_dump_end(cb);
629 return res;
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
David Ahern8d1c8022018-04-17 17:33:26 -0700632void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
David Ahernd4ead6b2018-04-17 17:33:16 -0700633{
634 if (!f6i)
635 return;
636
637 if (f6i->fib6_metrics == &dst_default_metrics) {
638 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
639
640 if (!p)
641 return;
642
643 refcount_set(&p->refcnt, 1);
644 f6i->fib6_metrics = p;
645 }
646
647 f6i->fib6_metrics->metrics[metric - 1] = val;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
651 * Routing Table
652 *
653 * return the appropriate node for a routing tree "add" operation
654 * by either creating and inserting or by returning an existing
655 * node.
656 */
657
Wei Wang81eb8442017-10-06 12:06:11 -0700658static struct fib6_node *fib6_add_1(struct net *net,
659 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700660 struct fib6_node *root,
661 struct in6_addr *addr, int plen,
662 int offset, int allow_create,
663 int replace_required,
664 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct fib6_node *fn, *in, *ln;
667 struct fib6_node *pn = NULL;
668 struct rt6key *key;
669 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900670 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 RT6_TRACE("fib6_add_1\n");
673
674 /* insert node in tree */
675
676 fn = root;
677
678 do {
David Ahern8d1c8022018-04-17 17:33:26 -0700679 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700680 lockdep_is_held(&table->tb6_lock));
681 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /*
684 * Prefix match
685 */
686 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000687 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000688 if (!allow_create) {
689 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600690 NL_SET_ERR_MSG(extack,
691 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000692 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000693 return ERR_PTR(-ENOENT);
694 }
Joe Perchesf3213832012-05-15 14:11:53 +0000695 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000698 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * Exact match ?
702 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (plen == fn->fn_bit) {
705 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500706 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700707 RCU_INIT_POINTER(fn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700708 fib6_info_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800709 /* remove null_entry in the root node */
710 } else if (fn->fn_flags & RTN_TL_ROOT &&
711 rcu_access_pointer(fn->leaf) ==
David Ahern421842e2018-04-17 17:33:18 -0700712 net->ipv6.fib6_null_entry) {
Wei Wang4512c432018-01-08 10:34:00 -0800713 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return fn;
717 }
718
719 /*
720 * We have more bits to go
721 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 dir = addr_bit_set(addr, fn->fn_bit);
725 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700726 fn = dir ?
727 rcu_dereference_protected(fn->right,
728 lockdep_is_held(&table->tb6_lock)) :
729 rcu_dereference_protected(fn->left,
730 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } while (fn);
732
Matti Vaittinen14df0152011-11-16 21:18:02 +0000733 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000734 /* We should not create new node because
735 * NLM_F_REPLACE was specified without NLM_F_CREATE
736 * I assume it is safe to require NLM_F_CREATE when
737 * REPLACE flag is used! Later we may want to remove the
738 * check for replace_required, because according
739 * to netlink specification, NLM_F_CREATE
740 * MUST be specified if new route is created.
741 * That would keep IPv6 consistent with IPv4
742 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000743 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600744 NL_SET_ERR_MSG(extack,
745 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000746 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000747 return ERR_PTR(-ENOENT);
748 }
Joe Perchesf3213832012-05-15 14:11:53 +0000749 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
752 * We walked to the bottom of tree.
753 * Create new leaf node without children.
754 */
755
Wei Wang81eb8442017-10-06 12:06:11 -0700756 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
David S. Miller507c9b12011-12-03 17:50:45 -0500758 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000759 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700761 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700764 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700766 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return ln;
769
770
771insert_above:
772 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900773 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * we have a less significant route.
775 * we've to insert an intermediate node on the list
776 * this new node will point to the one we need to create
777 * and the current
778 */
779
Wei Wang66f5d6c2017-10-06 12:06:10 -0700780 pn = rcu_dereference_protected(fn->parent,
781 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* find 1st bit in difference between the 2 addrs.
784
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800785 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 but if it is >= plen, the value is ignored in any case.
787 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900788
fan.du9225b232013-07-22 14:21:09 +0800789 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900791 /*
792 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * / \
794 * (new leaf node)[ln] (old node)[fn]
795 */
796 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700797 in = node_alloc(net);
798 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900799
David S. Miller507c9b12011-12-03 17:50:45 -0500800 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700802 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700804 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000805 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900808 /*
809 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * RTN_RTINFO will
811 * be off since that an address that chooses one of
812 * the branches would not match less specific routes
813 * in the other branch
814 */
815
816 in->fn_bit = bit;
817
Wei Wang66f5d6c2017-10-06 12:06:10 -0700818 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700820 atomic_inc(&rcu_dereference_protected(in->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700821 lockdep_is_held(&table->tb6_lock))->fib6_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* update parent pointer */
824 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700825 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700827 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 ln->fn_bit = plen;
830
Wei Wang66f5d6c2017-10-06 12:06:10 -0700831 RCU_INIT_POINTER(ln->parent, in);
832 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700835 rcu_assign_pointer(in->right, ln);
836 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700838 rcu_assign_pointer(in->left, ln);
839 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 } else { /* plen <= bit */
842
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900843 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * (new leaf node)[ln]
845 * / \
846 * (old node)[fn] NULL
847 */
848
Wei Wang81eb8442017-10-06 12:06:11 -0700849 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David S. Miller507c9b12011-12-03 17:50:45 -0500851 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000852 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 ln->fn_bit = plen;
855
Wei Wang66f5d6c2017-10-06 12:06:10 -0700856 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700859 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700861 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Wei Wang66f5d6c2017-10-06 12:06:10 -0700863 rcu_assign_pointer(fn->parent, ln);
864
865 if (dir)
866 rcu_assign_pointer(pn->right, ln);
867 else
868 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 return ln;
871}
872
David Ahern5bcaa412018-04-20 15:38:01 -0700873static void fib6_drop_pcpu_from(struct fib6_info *f6i,
874 const struct fib6_table *table)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100875{
David Ahern5bcaa412018-04-20 15:38:01 -0700876 int cpu;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100877
David Ahern5bcaa412018-04-20 15:38:01 -0700878 /* release the reference to this fib entry from
879 * all of its cached pcpu routes
880 */
881 for_each_possible_cpu(cpu) {
882 struct rt6_info **ppcpu_rt;
883 struct rt6_info *pcpu_rt;
884
885 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
886 pcpu_rt = *ppcpu_rt;
887 if (pcpu_rt) {
David Aherna68886a2018-04-20 15:38:02 -0700888 struct fib6_info *from;
889
890 from = rcu_dereference_protected(pcpu_rt->from,
891 lockdep_is_held(&table->tb6_lock));
892 rcu_assign_pointer(pcpu_rt->from, NULL);
893 fib6_info_release(from);
David Ahern5bcaa412018-04-20 15:38:01 -0700894 }
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100895 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100896}
897
David Ahern8d1c8022018-04-17 17:33:26 -0700898static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100899 struct net *net)
900{
David Ahern93c2fb22018-04-18 15:38:59 -0700901 struct fib6_table *table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700902
David Ahern93c2fb22018-04-18 15:38:59 -0700903 if (atomic_read(&rt->fib6_ref) != 1) {
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100904 /* This route is used as dummy address holder in some split
905 * nodes. It is not leaked, but it still holds other resources,
906 * which must be released in time. So, scan ascendant nodes
907 * and replace dummy references to this route with references
908 * to still alive ones.
909 */
910 while (fn) {
David Ahern8d1c8022018-04-17 17:33:26 -0700911 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700912 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -0700913 struct fib6_info *new_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700914 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
915 new_leaf = fib6_find_prefix(net, table, fn);
David Ahern93c2fb22018-04-18 15:38:59 -0700916 atomic_inc(&new_leaf->fib6_ref);
David Ahern93531c62018-04-17 17:33:25 -0700917
Wei Wang66f5d6c2017-10-06 12:06:10 -0700918 rcu_assign_pointer(fn->leaf, new_leaf);
David Ahern93531c62018-04-17 17:33:25 -0700919 fib6_info_release(rt);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100920 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700921 fn = rcu_dereference_protected(fn->parent,
922 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100923 }
David Ahern93531c62018-04-17 17:33:25 -0700924
David Ahern5bcaa412018-04-20 15:38:01 -0700925 if (rt->rt6i_pcpu)
926 fib6_drop_pcpu_from(rt, table);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100927 }
928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
931 * Insert routing information in a node.
932 */
933
David Ahern8d1c8022018-04-17 17:33:26 -0700934static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700935 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700936 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
David Ahern8d1c8022018-04-17 17:33:26 -0700938 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -0700939 lockdep_is_held(&rt->fib6_table->tb6_lock));
David Ahern33bd5ac2018-07-03 14:36:21 -0700940 struct fib6_info *iter = NULL;
David Ahern8d1c8022018-04-17 17:33:26 -0700941 struct fib6_info __rcu **ins;
David Ahern33bd5ac2018-07-03 14:36:21 -0700942 struct fib6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500943 int replace = (info->nlh &&
944 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
945 int add = (!info->nlh ||
946 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000947 int found = 0;
David Ahern33bd5ac2018-07-03 14:36:21 -0700948 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200949 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100950 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
David Ahern33bd5ac2018-07-03 14:36:21 -0700952 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
David Ahern1f5e29c2017-01-31 16:51:37 -0800953 nlflags |= NLM_F_APPEND;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ins = &fn->leaf;
956
Wei Wang66f5d6c2017-10-06 12:06:10 -0700957 for (iter = leaf; iter;
David Ahern8fb11a92018-05-04 13:54:24 -0700958 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -0700959 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /*
961 * Search for duplicates
962 */
963
David Ahern93c2fb22018-04-18 15:38:59 -0700964 if (iter->fib6_metric == rt->fib6_metric) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * Same priority level
967 */
David S. Miller507c9b12011-12-03 17:50:45 -0500968 if (info->nlh &&
969 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000970 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200971
972 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000973 if (replace) {
David Ahern33bd5ac2018-07-03 14:36:21 -0700974 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
975 found++;
976 break;
977 }
978 if (rt_can_ecmp)
979 fallback_ins = fallback_ins ?: ins;
980 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
David Ahernf06b7542017-07-05 14:41:46 -0600983 if (rt6_duplicate_nexthop(iter, rt)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700984 if (rt->fib6_nsiblings)
985 rt->fib6_nsiblings = 0;
986 if (!(iter->fib6_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return -EEXIST;
David Ahern93c2fb22018-04-18 15:38:59 -0700988 if (!(rt->fib6_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -0700989 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +0000990 else
David Ahern14895682018-04-17 17:33:17 -0700991 fib6_set_expires(iter, rt->expires);
David Ahernd4ead6b2018-04-17 17:33:16 -0700992 fib6_metric_set(iter, RTAX_MTU, rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return -EEXIST;
994 }
David Ahern33bd5ac2018-07-03 14:36:21 -0700995 /* If we have the same destination and the same metric,
996 * but not the same gateway, then the route we try to
997 * add is sibling to this route, increment our counter
998 * of siblings, and later we will add our route to the
999 * list.
1000 * Only static routes (which don't have flag
1001 * RTF_EXPIRES) are used for ECMPv6.
1002 *
1003 * To avoid long list, we only had siblings if the
1004 * route have a gateway.
1005 */
1006 if (rt_can_ecmp &&
1007 rt6_qualify_for_ecmp(iter))
1008 rt->fib6_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
David Ahern93c2fb22018-04-18 15:38:59 -07001011 if (iter->fib6_metric > rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013
David Ahern33bd5ac2018-07-03 14:36:21 -07001014next_iter:
David Ahern8fb11a92018-05-04 13:54:24 -07001015 ins = &iter->fib6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001016 }
1017
David Ahern33bd5ac2018-07-03 14:36:21 -07001018 if (fallback_ins && !found) {
1019 /* No ECMP-able route found, replace first non-ECMP one */
1020 ins = fallback_ins;
1021 iter = rcu_dereference_protected(*ins,
1022 lockdep_is_held(&rt->fib6_table->tb6_lock));
1023 found++;
1024 }
1025
David S. Millerf11e6652007-03-24 20:36:25 -07001026 /* Reset round-robin state, if necessary */
1027 if (ins == &fn->leaf)
1028 fn->rr_ptr = NULL;
1029
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001030 /* Link this route to others same route. */
David Ahern33bd5ac2018-07-03 14:36:21 -07001031 if (rt->fib6_nsiblings) {
1032 unsigned int fib6_nsiblings;
David Ahern8d1c8022018-04-17 17:33:26 -07001033 struct fib6_info *sibling, *temp_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001034
David Ahern33bd5ac2018-07-03 14:36:21 -07001035 /* Find the first route that have the same metric */
1036 sibling = leaf;
1037 while (sibling) {
1038 if (sibling->fib6_metric == rt->fib6_metric &&
1039 rt6_qualify_for_ecmp(sibling)) {
1040 list_add_tail(&rt->fib6_siblings,
1041 &sibling->fib6_siblings);
1042 break;
1043 }
1044 sibling = rcu_dereference_protected(sibling->fib6_next,
1045 lockdep_is_held(&rt->fib6_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001046 }
1047 /* For each sibling in the list, increment the counter of
1048 * siblings. BUG() if counters does not match, list of siblings
1049 * is broken!
1050 */
David Ahern33bd5ac2018-07-03 14:36:21 -07001051 fib6_nsiblings = 0;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001052 list_for_each_entry_safe(sibling, temp_sibling,
David Ahern33bd5ac2018-07-03 14:36:21 -07001053 &rt->fib6_siblings, fib6_siblings) {
David Ahern93c2fb22018-04-18 15:38:59 -07001054 sibling->fib6_nsiblings++;
David Ahern33bd5ac2018-07-03 14:36:21 -07001055 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1056 fib6_nsiblings++;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001057 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001058 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1059 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001060 }
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * insert node
1064 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001065 if (!replace) {
1066 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001067 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001069add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001070 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001071
David Ahern33bd5ac2018-07-03 14:36:21 -07001072 err = call_fib6_entry_notifiers(info->nl_net,
1073 FIB_EVENT_ENTRY_ADD,
1074 rt, extack);
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001075 if (err)
1076 return err;
1077
David Ahern8fb11a92018-05-04 13:54:24 -07001078 rcu_assign_pointer(rt->fib6_next, iter);
David Ahern93c2fb22018-04-18 15:38:59 -07001079 atomic_inc(&rt->fib6_ref);
1080 rcu_assign_pointer(rt->fib6_node, fn);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001081 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001082 if (!info->skip_notify)
1083 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001084 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
David S. Miller507c9b12011-12-03 17:50:45 -05001086 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001087 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1088 fn->fn_flags |= RTN_RTINFO;
1089 }
1090
1091 } else {
David Ahern33bd5ac2018-07-03 14:36:21 -07001092 int nsiblings;
Michal Kubeček27596472015-05-18 20:54:00 +02001093
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001094 if (!found) {
1095 if (add)
1096 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001097 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001098 return -ENOENT;
1099 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001100
David Ahern22330002018-03-27 18:21:59 -07001101 err = call_fib6_entry_notifiers(info->nl_net,
1102 FIB_EVENT_ENTRY_REPLACE,
1103 rt, extack);
1104 if (err)
1105 return err;
1106
David Ahern93c2fb22018-04-18 15:38:59 -07001107 atomic_inc(&rt->fib6_ref);
1108 rcu_assign_pointer(rt->fib6_node, fn);
David Ahern33bd5ac2018-07-03 14:36:21 -07001109 rt->fib6_next = iter->fib6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001110 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001111 if (!info->skip_notify)
1112 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001113 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001114 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1115 fn->fn_flags |= RTN_RTINFO;
1116 }
David Ahern33bd5ac2018-07-03 14:36:21 -07001117 nsiblings = iter->fib6_nsiblings;
1118 iter->fib6_node = NULL;
1119 fib6_purge_rt(iter, fn, info->nl_net);
1120 if (rcu_access_pointer(fn->rr_ptr) == iter)
David Ahernf34436a2018-05-21 10:26:53 -07001121 fn->rr_ptr = NULL;
David Ahern33bd5ac2018-07-03 14:36:21 -07001122 fib6_info_release(iter);
1123
1124 if (nsiblings) {
1125 /* Replacing an ECMP route, remove all siblings */
1126 ins = &rt->fib6_next;
1127 iter = rcu_dereference_protected(*ins,
1128 lockdep_is_held(&rt->fib6_table->tb6_lock));
1129 while (iter) {
1130 if (iter->fib6_metric > rt->fib6_metric)
1131 break;
1132 if (rt6_qualify_for_ecmp(iter)) {
1133 *ins = iter->fib6_next;
1134 iter->fib6_node = NULL;
1135 fib6_purge_rt(iter, fn, info->nl_net);
1136 if (rcu_access_pointer(fn->rr_ptr) == iter)
1137 fn->rr_ptr = NULL;
1138 fib6_info_release(iter);
1139 nsiblings--;
1140 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1141 } else {
1142 ins = &iter->fib6_next;
1143 }
1144 iter = rcu_dereference_protected(*ins,
1145 lockdep_is_held(&rt->fib6_table->tb6_lock));
1146 }
1147 WARN_ON(nsiblings != 0);
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 return 0;
1152}
1153
David Ahern8d1c8022018-04-17 17:33:26 -07001154static void fib6_start_gc(struct net *net, struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001156 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001157 (rt->fib6_flags & RTF_EXPIRES))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001158 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001159 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001162void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001164 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1165 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001166 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
David Ahern8d1c8022018-04-17 17:33:26 -07001169static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001170 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001171{
David Ahern93c2fb22018-04-18 15:38:59 -07001172 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1173 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001174
1175 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1176 smp_wmb();
1177 while (fn) {
1178 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001179 fn = rcu_dereference_protected(fn->parent,
David Ahern93c2fb22018-04-18 15:38:59 -07001180 lockdep_is_held(&rt->fib6_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001181 }
1182}
1183
David Ahern8d1c8022018-04-17 17:33:26 -07001184void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001185{
1186 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189/*
1190 * Add routing information to the routing tree.
1191 * <destination addr>/<source addr>
1192 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001193 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
1195
David Ahern8d1c8022018-04-17 17:33:26 -07001196int fib6_add(struct fib6_node *root, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001197 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
David Ahern93c2fb22018-04-18 15:38:59 -07001199 struct fib6_table *table = rt->fib6_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001200 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001202 int allow_create = 1;
1203 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001204 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001205
1206 if (info->nlh) {
1207 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001208 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001209 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001210 replace_required = 1;
1211 }
1212 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001213 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Wei Wang81eb8442017-10-06 12:06:11 -07001215 fn = fib6_add_1(info->nl_net, table, root,
David Ahern93c2fb22018-04-18 15:38:59 -07001216 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1217 offsetof(struct fib6_info, fib6_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001218 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001219 if (IS_ERR(fn)) {
1220 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001221 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001225 pn = fn;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07001228 if (rt->fib6_src.plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct fib6_node *sn;
1230
Wei Wang66f5d6c2017-10-06 12:06:10 -07001231 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 struct fib6_node *sfn;
1233
1234 /*
1235 * Create subtree.
1236 *
1237 * fn[main tree]
1238 * |
1239 * sfn[subtree root]
1240 * \
1241 * sn[new leaf node]
1242 */
1243
1244 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001245 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001246 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001247 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
David Ahern93c2fb22018-04-18 15:38:59 -07001249 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001250 rcu_assign_pointer(sfn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001251 info->nl_net->ipv6.fib6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* Now add the first leaf node to new subtree */
1255
Wei Wang81eb8442017-10-06 12:06:11 -07001256 sn = fib6_add_1(info->nl_net, table, sfn,
David Ahern93c2fb22018-04-18 15:38:59 -07001257 &rt->fib6_src.addr, rt->fib6_src.plen,
1258 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001259 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001261 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001263 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 in main tree.
1265 */
Wei Wang81eb8442017-10-06 12:06:11 -07001266 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001267 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001268 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
1271 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001272 rcu_assign_pointer(sfn->parent, fn);
1273 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001275 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
David Ahern93c2fb22018-04-18 15:38:59 -07001276 &rt->fib6_src.addr, rt->fib6_src.plen,
1277 offsetof(struct fib6_info, fib6_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001278 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001280 if (IS_ERR(sn)) {
1281 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001282 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285
Wei Wang66f5d6c2017-10-06 12:06:10 -07001286 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001287 if (fn->fn_flags & RTN_TL_ROOT) {
1288 /* put back null_entry for root node */
1289 rcu_assign_pointer(fn->leaf,
David Ahern421842e2018-04-17 17:33:18 -07001290 info->nl_net->ipv6.fib6_null_entry);
Wei Wang591ff9e2018-01-18 10:40:03 -08001291 } else {
David Ahern93c2fb22018-04-18 15:38:59 -07001292 atomic_inc(&rt->fib6_ref);
Wei Wang591ff9e2018-01-18 10:40:03 -08001293 rcu_assign_pointer(fn->leaf, rt);
1294 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 fn = sn;
1297 }
1298#endif
1299
David Ahernd4ead6b2018-04-17 17:33:16 -07001300 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001301 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001302 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001303 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001307 if (err) {
1308#ifdef CONFIG_IPV6_SUBTREES
1309 /*
1310 * If fib6_add_1 has cleared the old leaf pointer in the
1311 * super-tree leaf node we have to find a new one for it.
1312 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001313 if (pn != fn) {
David Ahern8d1c8022018-04-17 17:33:26 -07001314 struct fib6_info *pn_leaf =
Wei Wang7bbfe002018-01-03 14:11:59 -08001315 rcu_dereference_protected(pn->leaf,
1316 lockdep_is_held(&table->tb6_lock));
1317 if (pn_leaf == rt) {
1318 pn_leaf = NULL;
1319 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001320 fib6_info_release(rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001321 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001322 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1323 pn_leaf = fib6_find_prefix(info->nl_net, table,
1324 pn);
1325#if RT6_DEBUG >= 2
1326 if (!pn_leaf) {
1327 WARN_ON(!pn_leaf);
1328 pn_leaf =
David Ahern421842e2018-04-17 17:33:18 -07001329 info->nl_net->ipv6.fib6_null_entry;
Wei Wang7bbfe002018-01-03 14:11:59 -08001330 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001331#endif
David Ahern93531c62018-04-17 17:33:25 -07001332 fib6_info_hold(pn_leaf);
Wei Wang7bbfe002018-01-03 14:11:59 -08001333 rcu_assign_pointer(pn->leaf, pn_leaf);
1334 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001335 }
1336#endif
Wei Wang348a4002017-08-18 17:14:49 -07001337 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return err;
1340
Wei Wang348a4002017-08-18 17:14:49 -07001341failure:
Wei Wang4512c432018-01-08 10:34:00 -08001342 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1343 * 1. fn is an intermediate node and we failed to add the new
1344 * route to it in both subtree creation failure and fib6_add_rt2node()
1345 * failure case.
1346 * 2. fn is the root node in the table and we fail to add the first
1347 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
Wei Wang4512c432018-01-08 10:34:00 -08001349 if (fn &&
1350 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1351 (fn->fn_flags & RTN_TL_ROOT &&
1352 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001353 fib6_repair_tree(info->nl_net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357/*
1358 * Routing tree lookup
1359 *
1360 */
1361
1362struct lookup_args {
David Ahern8d1c8022018-04-17 17:33:26 -07001363 int offset; /* key offset on fib6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001364 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365};
1366
David Ahern64547432018-05-09 20:34:19 -07001367static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1368 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001371 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001373 if (unlikely(args->offset == 0))
1374 return NULL;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /*
1377 * Descend on a tree
1378 */
1379
1380 fn = root;
1381
1382 for (;;) {
1383 struct fib6_node *next;
1384
1385 dir = addr_bit_set(args->addr, fn->fn_bit);
1386
Wei Wang66f5d6c2017-10-06 12:06:10 -07001387 next = dir ? rcu_dereference(fn->right) :
1388 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if (next) {
1391 fn = next;
1392 continue;
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
1395 }
1396
David S. Miller507c9b12011-12-03 17:50:45 -05001397 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001398 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1399
1400 if (subtree || fn->fn_flags & RTN_RTINFO) {
David Ahern8d1c8022018-04-17 17:33:26 -07001401 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct rt6key *key;
1403
Wei Wang8d1040e2017-10-06 12:06:08 -07001404 if (!leaf)
1405 goto backtrack;
1406
1407 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001409 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1410#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001411 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001412 struct fib6_node *sfn;
David Ahern64547432018-05-09 20:34:19 -07001413 sfn = fib6_node_lookup_1(subtree,
1414 args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001415 if (!sfn)
1416 goto backtrack;
1417 fn = sfn;
1418 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001419#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001420 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001421 return fn;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001424backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001425 if (fn->fn_flags & RTN_ROOT)
1426 break;
1427
Wei Wang66f5d6c2017-10-06 12:06:10 -07001428 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 return NULL;
1432}
1433
Wei Wang66f5d6c2017-10-06 12:06:10 -07001434/* called with rcu_read_lock() held
1435 */
David Ahern64547432018-05-09 20:34:19 -07001436struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1437 const struct in6_addr *daddr,
1438 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001441 struct lookup_args args[] = {
1442 {
David Ahern93c2fb22018-04-18 15:38:59 -07001443 .offset = offsetof(struct fib6_info, fib6_dst),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001444 .addr = daddr,
1445 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001447 {
David Ahern93c2fb22018-04-18 15:38:59 -07001448 .offset = offsetof(struct fib6_info, fib6_src),
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001449 .addr = saddr,
1450 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001452 {
1453 .offset = 0, /* sentinel */
1454 }
1455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
David Ahern64547432018-05-09 20:34:19 -07001457 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001458 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 fn = root;
1460
1461 return fn;
1462}
1463
1464/*
1465 * Get node with specified destination prefix (and source prefix,
1466 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001467 * exact_match == true means we try to find fn with exact match of
1468 * the passed in prefix addr
1469 * exact_match == false means we try to find fn with longest prefix
1470 * match of the passed in prefix addr. This is useful for finding fn
1471 * for cached route as it will be stored in the exception table under
1472 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474
1475
Wang Yufen437de072014-03-28 12:07:04 +08001476static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1477 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001478 int plen, int offset,
1479 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Wei Wang38fbeee2017-10-06 12:06:02 -07001481 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 for (fn = root; fn ; ) {
David Ahern8d1c8022018-04-17 17:33:26 -07001484 struct fib6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001485 struct rt6key *key;
1486
1487 /* This node is being deleted */
1488 if (!leaf) {
1489 if (plen <= fn->fn_bit)
1490 goto out;
1491 else
1492 goto next;
1493 }
1494
1495 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * Prefix match
1499 */
1500 if (plen < fn->fn_bit ||
1501 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (plen == fn->fn_bit)
1505 return fn;
1506
Wei Wang38fbeee2017-10-06 12:06:02 -07001507 prev = fn;
1508
Wei Wang8d1040e2017-10-06 12:06:08 -07001509next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * We have more bits to go
1512 */
1513 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001514 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001516 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001518out:
1519 if (exact_match)
1520 return NULL;
1521 else
1522 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Wang Yufen437de072014-03-28 12:07:04 +08001525struct fib6_node *fib6_locate(struct fib6_node *root,
1526 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001527 const struct in6_addr *saddr, int src_len,
1528 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct fib6_node *fn;
1531
1532 fn = fib6_locate_1(root, daddr, dst_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001533 offsetof(struct fib6_info, fib6_dst),
Wei Wang38fbeee2017-10-06 12:06:02 -07001534 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536#ifdef CONFIG_IPV6_SUBTREES
1537 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001538 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001539 if (fn) {
1540 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1541
1542 if (subtree) {
1543 fn = fib6_locate_1(subtree, saddr, src_len,
David Ahern93c2fb22018-04-18 15:38:59 -07001544 offsetof(struct fib6_info, fib6_src),
Wei Wang38fbeee2017-10-06 12:06:02 -07001545 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001546 }
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549#endif
1550
David S. Miller507c9b12011-12-03 17:50:45 -05001551 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return fn;
1553
1554 return NULL;
1555}
1556
1557
1558/*
1559 * Deletion
1560 *
1561 */
1562
David Ahern8d1c8022018-04-17 17:33:26 -07001563static struct fib6_info *fib6_find_prefix(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001564 struct fib6_table *table,
1565 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001567 struct fib6_node *child_left, *child_right;
1568
David S. Miller507c9b12011-12-03 17:50:45 -05001569 if (fn->fn_flags & RTN_ROOT)
David Ahern421842e2018-04-17 17:33:18 -07001570 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
David S. Miller507c9b12011-12-03 17:50:45 -05001572 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001573 child_left = rcu_dereference_protected(fn->left,
1574 lockdep_is_held(&table->tb6_lock));
1575 child_right = rcu_dereference_protected(fn->right,
1576 lockdep_is_held(&table->tb6_lock));
1577 if (child_left)
1578 return rcu_dereference_protected(child_left->leaf,
1579 lockdep_is_held(&table->tb6_lock));
1580 if (child_right)
1581 return rcu_dereference_protected(child_right->leaf,
1582 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001584 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586 return NULL;
1587}
1588
1589/*
1590 * Called to trim the tree of intermediate nodes when possible. "fn"
1591 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001592 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 */
1594
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001595static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001596 struct fib6_table *table,
1597 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 int children;
1600 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001601 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001602 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int iter = 0;
1604
Wei Wang4512c432018-01-08 10:34:00 -08001605 /* Set fn->leaf to null_entry for root node. */
1606 if (fn->fn_flags & RTN_TL_ROOT) {
David Ahern421842e2018-04-17 17:33:18 -07001607 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
Wei Wang4512c432018-01-08 10:34:00 -08001608 return fn;
1609 }
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001612 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1613 lockdep_is_held(&table->tb6_lock));
1614 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1615 lockdep_is_held(&table->tb6_lock));
1616 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1617 lockdep_is_held(&table->tb6_lock));
1618 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1619 lockdep_is_held(&table->tb6_lock));
1620 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1621 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001622 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001623 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001624 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001625 lockdep_is_held(&table->tb6_lock));
David Ahern8d1c8022018-04-17 17:33:26 -07001626 struct fib6_info *new_fn_leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1629 iter++;
1630
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001631 WARN_ON(fn->fn_flags & RTN_RTINFO);
1632 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001633 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 children = 0;
1636 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001637 if (fn_r)
1638 child = fn_r, children |= 1;
1639 if (fn_l)
1640 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001642 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643#ifdef CONFIG_IPV6_SUBTREES
1644 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001645 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646#endif
1647 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001648 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001650 if (!new_fn_leaf) {
1651 WARN_ON(!new_fn_leaf);
David Ahern421842e2018-04-17 17:33:18 -07001652 new_fn_leaf = net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654#endif
David Ahern93531c62018-04-17 17:33:25 -07001655 fib6_info_hold(new_fn_leaf);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001656 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1657 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001661 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001662 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001663 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 nstate = FWS_L;
1665 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001666 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001668 if (pn_r == fn)
1669 rcu_assign_pointer(pn->right, child);
1670 else if (pn_l == fn)
1671 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001673 else
1674 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675#endif
1676 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001677 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 nstate = FWS_R;
1679#ifdef CONFIG_IPV6_SUBTREES
1680 }
1681#endif
1682
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001683 read_lock(&net->ipv6.fib6_walker_lock);
1684 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001685 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001686 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1688 w->node = pn;
1689 w->state = nstate;
1690 }
1691 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (w->node == fn) {
1693 w->node = child;
1694 if (children&2) {
1695 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001696 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 } else {
1698 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001699 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
1702 }
1703 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001704 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Wei Wang81eb8442017-10-06 12:06:11 -07001706 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001707 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return pn;
1709
Wei Wang66f5d6c2017-10-06 12:06:10 -07001710 RCU_INIT_POINTER(pn->leaf, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001711 fib6_info_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 fn = pn;
1713 }
1714}
1715
Wei Wang66f5d6c2017-10-06 12:06:10 -07001716static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
David Ahern8d1c8022018-04-17 17:33:26 -07001717 struct fib6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001719 struct fib6_walker *w;
David Ahern8d1c8022018-04-17 17:33:26 -07001720 struct fib6_info *rt = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001721 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001722 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 RT6_TRACE("fib6_del_route\n");
1725
1726 /* Unlink it */
David Ahern8fb11a92018-05-04 13:54:24 -07001727 *rtp = rt->fib6_next;
David Ahern93c2fb22018-04-18 15:38:59 -07001728 rt->fib6_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001729 net->ipv6.rt6_stats->fib_rt_entries--;
1730 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Wei Wang2b760fc2017-10-06 12:06:03 -07001732 /* Flush all cached dst in exception table */
1733 rt6_flush_exceptions(rt);
1734
David S. Millerf11e6652007-03-24 20:36:25 -07001735 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001736 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001737 fn->rr_ptr = NULL;
1738
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001739 /* Remove this entry from other siblings */
David Ahern93c2fb22018-04-18 15:38:59 -07001740 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07001741 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001742
1743 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07001744 &rt->fib6_siblings, fib6_siblings)
1745 sibling->fib6_nsiblings--;
1746 rt->fib6_nsiblings = 0;
1747 list_del_init(&rt->fib6_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001748 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001749 }
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001752 read_lock(&net->ipv6.fib6_walker_lock);
1753 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (w->state == FWS_C && w->leaf == rt) {
1755 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Ahern8fb11a92018-05-04 13:54:24 -07001756 w->leaf = rcu_dereference_protected(rt->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001757 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001758 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 w->state = FWS_U;
1760 }
1761 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001762 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Wei Wang4512c432018-01-08 10:34:00 -08001764 /* If it was last route, call fib6_repair_tree() to:
1765 * 1. For root node, put back null_entry as how the table was created.
1766 * 2. For other nodes, expunge its radix tree node.
1767 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001768 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001769 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1770 fn->fn_flags &= ~RTN_RTINFO;
1771 net->ipv6.rt6_stats->fib_route_nodes--;
1772 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001773 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001776 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
David Ahern6c31e5a2017-10-27 17:37:13 -07001778 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001779 if (!info->skip_notify)
1780 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
David Ahern93531c62018-04-17 17:33:25 -07001781 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Wei Wang66f5d6c2017-10-06 12:06:10 -07001784/* Need to own table->tb6_lock */
David Ahern8d1c8022018-04-17 17:33:26 -07001785int fib6_del(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
David Ahern93c2fb22018-04-18 15:38:59 -07001787 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1788 lockdep_is_held(&rt->fib6_table->tb6_lock));
1789 struct fib6_table *table = rt->fib6_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001790 struct net *net = info->nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07001791 struct fib6_info __rcu **rtp;
1792 struct fib6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
David Ahern421842e2018-04-17 17:33:18 -07001794 if (!fn || rt == net->ipv6.fib6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return -ENOENT;
1796
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001797 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /*
1800 * Walk the leaf entries looking for ourself
1801 */
1802
Wei Wang66f5d6c2017-10-06 12:06:10 -07001803 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
David Ahern8d1c8022018-04-17 17:33:26 -07001804 struct fib6_info *cur = rcu_dereference_protected(*rtp,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001805 lockdep_is_held(&table->tb6_lock));
1806 if (rt == cur) {
1807 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
1809 }
David Ahern8fb11a92018-05-04 13:54:24 -07001810 rtp_next = &cur->fib6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 return -ENOENT;
1813}
1814
1815/*
1816 * Tree traversal function.
1817 *
1818 * Certainly, it is not interrupt safe.
1819 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1820 * It means, that we can modify tree during walking
1821 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001822 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 *
1824 * It guarantees that every node will be traversed,
1825 * and that it will be traversed only once.
1826 *
1827 * Callback function w->func may return:
1828 * 0 -> continue walking.
1829 * positive value -> walking is suspended (used by tree dumps,
1830 * and probably by gc, if it will be split to several slices)
1831 * negative value -> terminate walking.
1832 *
1833 * The function itself returns:
1834 * 0 -> walk is complete.
1835 * >0 -> walk is incomplete (i.e. suspended)
1836 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001837 *
1838 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
1840
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001841static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001843 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Wei Wang2b760fc2017-10-06 12:06:03 -07001845 /* w->root should always be table->tb6_root */
1846 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 for (;;) {
1849 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001850 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return 0;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 switch (w->state) {
1854#ifdef CONFIG_IPV6_SUBTREES
1855 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001856 if (FIB6_SUBTREE(fn)) {
1857 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 continue;
1859 }
1860 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001861#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001862 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001864 left = rcu_dereference_protected(fn->left, 1);
1865 if (left) {
1866 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 w->state = FWS_INIT;
1868 continue;
1869 }
1870 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001871 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001873 right = rcu_dereference_protected(fn->right, 1);
1874 if (right) {
1875 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 w->state = FWS_INIT;
1877 continue;
1878 }
1879 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001880 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001881 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001883 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001884 int err;
1885
Eric Dumazetfa809e22012-06-25 15:37:19 -07001886 if (w->skip) {
1887 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001888 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001889 }
1890
1891 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (err)
1893 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001894
1895 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 continue;
1897 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001898skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001900 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 case FWS_U:
1902 if (fn == w->root)
1903 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001904 pn = rcu_dereference_protected(fn->parent, 1);
1905 left = rcu_dereference_protected(pn->left, 1);
1906 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 w->node = pn;
1908#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001909 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001910 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 w->state = FWS_L;
1912 continue;
1913 }
1914#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001915 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 w->state = FWS_R;
1917 continue;
1918 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001919 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001921 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 continue;
1923 }
1924#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001925 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926#endif
1927 }
1928 }
1929}
1930
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001931static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 int res;
1934
1935 w->state = FWS_INIT;
1936 w->node = w->root;
1937
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001938 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 res = fib6_walk_continue(w);
1940 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001941 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return res;
1943}
1944
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001945static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 int res;
David Ahern8d1c8022018-04-17 17:33:26 -07001948 struct fib6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001949 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001950 struct nl_info info = {
1951 .nl_net = c->net,
1952 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001954 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1955 w->node->fn_sernum != c->sernum)
1956 w->node->fn_sernum = c->sernum;
1957
1958 if (!c->func) {
1959 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1960 w->leaf = NULL;
1961 return 0;
1962 }
1963
Wei Wang66f5d6c2017-10-06 12:06:10 -07001964 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001966 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001968 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (res) {
1970#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001971 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001972 __func__, rt,
David Ahern93c2fb22018-04-18 15:38:59 -07001973 rcu_access_pointer(rt->fib6_node),
Wei Wang4e587ea2017-08-25 15:03:10 -07001974 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975#endif
1976 continue;
1977 }
1978 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001979 } else if (res == -2) {
David Ahern93c2fb22018-04-18 15:38:59 -07001980 if (WARN_ON(!rt->fib6_nsiblings))
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001981 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07001982 rt = list_last_entry(&rt->fib6_siblings,
1983 struct fib6_info, fib6_siblings);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001984 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001986 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 w->leaf = rt;
1989 return 0;
1990}
1991
1992/*
1993 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001994 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001996 * It may return -2 -> skip multipath route.
1997 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
2000
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002001static void fib6_clean_tree(struct net *net, struct fib6_node *root,
David Ahern8d1c8022018-04-17 17:33:26 -07002002 int (*func)(struct fib6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07002003 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002005 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 c.w.root = root;
2008 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00002009 c.w.count = 0;
2010 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002012 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002014 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002016 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002019static void __fib6_clean_all(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07002020 int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002021 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07002022{
Thomas Grafc71099a2006-08-04 23:20:06 -07002023 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002024 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07002025 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07002026
Patrick McHardy1b43af52006-08-10 23:11:17 -07002027 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07002028 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002029 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002030 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002031 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08002032 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07002033 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002034 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07002035 }
2036 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07002037 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07002038}
2039
David Ahern8d1c8022018-04-17 17:33:26 -07002040void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002041 void *arg)
2042{
2043 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2044}
2045
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002046static void fib6_flush_trees(struct net *net)
2047{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002048 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002049
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002050 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002051}
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053/*
2054 * Garbage collection
2055 */
2056
David Ahern8d1c8022018-04-17 17:33:26 -07002057static int fib6_age(struct fib6_info *rt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Michal Kubeček3570df92016-03-08 14:44:25 +01002059 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 unsigned long now = jiffies;
2061
2062 /*
2063 * check addrconf expiration here.
2064 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
2066
David Ahern93c2fb22018-04-18 15:38:59 -07002067 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
David Ahern14895682018-04-17 17:33:17 -07002068 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return -1;
2071 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002072 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074
Wei Wangc757faa2017-10-06 12:06:01 -07002075 /* Also age clones in the exception table.
2076 * Note, that clones are aged out
2077 * only if they are not in use now.
2078 */
2079 rt6_age_exceptions(rt, gc_args, now);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002084void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Michal Kubeček3570df92016-03-08 14:44:25 +01002086 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002087 unsigned long now;
2088
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002089 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002090 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2091 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002092 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2093 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002095 gc_args.timeout = expires ? (int)expires :
2096 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002097 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002098
Michal Kubeček3570df92016-03-08 14:44:25 +01002099 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002100 now = jiffies;
2101 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002104 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002105 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002106 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002107 else
2108 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002109 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Kees Cook86cb30e2017-10-17 20:21:24 -07002112static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002113{
Kees Cook86cb30e2017-10-17 20:21:24 -07002114 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2115
2116 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002117}
2118
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002119static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002120{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002121 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002122 int err;
2123
2124 err = fib6_notifier_init(net);
2125 if (err)
2126 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002127
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002128 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002129 rwlock_init(&net->ipv6.fib6_walker_lock);
2130 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002131 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002132
Benjamin Theryc5728722008-03-03 23:34:17 -08002133 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2134 if (!net->ipv6.rt6_stats)
2135 goto out_timer;
2136
Eric Dumazet10da66f2010-10-13 08:22:03 +00002137 /* Avoid false sharing : Use at least a full cache line */
2138 size = max_t(size_t, size, L1_CACHE_BYTES);
2139
2140 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002141 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002142 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002143
2144 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2145 GFP_KERNEL);
2146 if (!net->ipv6.fib6_main_tbl)
2147 goto out_fib_table_hash;
2148
2149 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002150 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002151 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002152 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2153 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002154 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002155
2156#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2157 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2158 GFP_KERNEL);
2159 if (!net->ipv6.fib6_local_tbl)
2160 goto out_fib6_main_tbl;
2161 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002162 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
David Ahern421842e2018-04-17 17:33:18 -07002163 net->ipv6.fib6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002164 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2165 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002166 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002167#endif
2168 fib6_tables_init(net);
2169
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002170 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002171
2172#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2173out_fib6_main_tbl:
2174 kfree(net->ipv6.fib6_main_tbl);
2175#endif
2176out_fib_table_hash:
2177 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002178out_rt6_stats:
2179 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002180out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002181 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002182 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002183}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002184
2185static void fib6_net_exit(struct net *net)
2186{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002187 unsigned int i;
2188
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002189 del_timer_sync(&net->ipv6.ip6_fib_timer);
2190
Eric Dumazet32a805b2017-09-08 15:48:47 -07002191 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002192 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2193 struct hlist_node *tmp;
2194 struct fib6_table *tb;
2195
2196 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2197 hlist_del(&tb->tb6_hlist);
2198 fib6_free_table(tb);
2199 }
2200 }
2201
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002202 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002203 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002204 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002205}
2206
2207static struct pernet_operations fib6_net_ops = {
2208 .init = fib6_net_init,
2209 .exit = fib6_net_exit,
2210};
2211
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002212int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002214 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2217 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002218 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002219 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002220 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002221 goto out;
2222
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002223 ret = register_pernet_subsys(&fib6_net_ops);
2224 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002225 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002226
Florian Westphal16feebc2017-12-02 21:44:08 +01002227 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2228 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002229 if (ret)
2230 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002231
2232 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002233out:
2234 return ret;
2235
David S. Millere8803b62012-06-16 01:12:19 -07002236out_unregister_subsys:
2237 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002238out_kmem_cache_create:
2239 kmem_cache_destroy(fib6_node_kmem);
2240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243void fib6_gc_cleanup(void)
2244{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002245 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 kmem_cache_destroy(fib6_node_kmem);
2247}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002248
2249#ifdef CONFIG_PROC_FS
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002250static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2251{
David Ahern8d1c8022018-04-17 17:33:26 -07002252 struct fib6_info *rt = v;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002253 struct ipv6_route_iter *iter = seq->private;
David Ahern5e670d82018-04-17 17:33:14 -07002254 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002255
David Ahern93c2fb22018-04-18 15:38:59 -07002256 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002257
2258#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002259 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002260#else
2261 seq_puts(seq, "00000000000000000000000000000000 00 ");
2262#endif
David Ahern93c2fb22018-04-18 15:38:59 -07002263 if (rt->fib6_flags & RTF_GATEWAY)
David Ahern5e670d82018-04-17 17:33:14 -07002264 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002265 else
2266 seq_puts(seq, "00000000000000000000000000000000");
2267
David Ahern5e670d82018-04-17 17:33:14 -07002268 dev = rt->fib6_nh.nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002269 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
David Ahern93c2fb22018-04-18 15:38:59 -07002270 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2271 rt->fib6_flags, dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002272 iter->w.leaf = NULL;
2273 return 0;
2274}
2275
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002276static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002277{
2278 struct ipv6_route_iter *iter = w->args;
2279
2280 if (!iter->skip)
2281 return 1;
2282
2283 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002284 iter->w.leaf = rcu_dereference_protected(
David Ahern8fb11a92018-05-04 13:54:24 -07002285 iter->w.leaf->fib6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002286 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002287 iter->skip--;
2288 if (!iter->skip && iter->w.leaf)
2289 return 1;
2290 } while (iter->w.leaf);
2291
2292 return 0;
2293}
2294
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002295static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2296 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002297{
2298 memset(&iter->w, 0, sizeof(iter->w));
2299 iter->w.func = ipv6_route_yield;
2300 iter->w.root = &iter->tbl->tb6_root;
2301 iter->w.state = FWS_INIT;
2302 iter->w.node = iter->w.root;
2303 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002304 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002305 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002306 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002307}
2308
2309static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2310 struct net *net)
2311{
2312 unsigned int h;
2313 struct hlist_node *node;
2314
2315 if (tbl) {
2316 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2317 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2318 } else {
2319 h = 0;
2320 node = NULL;
2321 }
2322
2323 while (!node && h < FIB6_TABLE_HASHSZ) {
2324 node = rcu_dereference_bh(
2325 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2326 }
2327 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2328}
2329
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002330static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2331{
2332 if (iter->sernum != iter->w.root->fn_sernum) {
2333 iter->sernum = iter->w.root->fn_sernum;
2334 iter->w.state = FWS_INIT;
2335 iter->w.node = iter->w.root;
2336 WARN_ON(iter->w.skip);
2337 iter->w.skip = iter->w.count;
2338 }
2339}
2340
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002341static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2342{
2343 int r;
David Ahern8d1c8022018-04-17 17:33:26 -07002344 struct fib6_info *n;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002345 struct net *net = seq_file_net(seq);
2346 struct ipv6_route_iter *iter = seq->private;
2347
2348 if (!v)
2349 goto iter_table;
2350
David Ahern8fb11a92018-05-04 13:54:24 -07002351 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002352 if (n) {
2353 ++*pos;
2354 return n;
2355 }
2356
2357iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002358 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002359 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002360 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002361 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002362 if (r > 0) {
2363 if (v)
2364 ++*pos;
2365 return iter->w.leaf;
2366 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002367 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002368 return NULL;
2369 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002370 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002371
2372 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2373 if (!iter->tbl)
2374 return NULL;
2375
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002376 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002377 goto iter_table;
2378}
2379
2380static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2381 __acquires(RCU_BH)
2382{
2383 struct net *net = seq_file_net(seq);
2384 struct ipv6_route_iter *iter = seq->private;
2385
2386 rcu_read_lock_bh();
2387 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2388 iter->skip = *pos;
2389
2390 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002391 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002392 return ipv6_route_seq_next(seq, NULL, pos);
2393 } else {
2394 return NULL;
2395 }
2396}
2397
2398static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2399{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002400 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002401 return w->node && !(w->state == FWS_U && w->node == w->root);
2402}
2403
2404static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2405 __releases(RCU_BH)
2406{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002407 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002408 struct ipv6_route_iter *iter = seq->private;
2409
2410 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002411 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002412
2413 rcu_read_unlock_bh();
2414}
2415
Christoph Hellwigc3506372018-04-10 19:42:55 +02002416const struct seq_operations ipv6_route_seq_ops = {
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002417 .start = ipv6_route_seq_start,
2418 .next = ipv6_route_seq_next,
2419 .stop = ipv6_route_seq_stop,
2420 .show = ipv6_route_seq_show
2421};
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002422#endif /* CONFIG_PROC_FS */