blob: 64b73e65f1146460b19573cf8a319c1522915dac [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int (*func)(struct rt6_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
Wei Wang66f5d6c2017-10-06 12:06:10 -070057static struct rt6_info *fib6_find_prefix(struct net *net,
58 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 Ahern7aef6852018-04-17 17:33:10 -0700108void fib6_update_sernum(struct net *net, struct rt6_info *rt)
Wei Wang180ca442017-10-06 12:05:56 -0700109{
Wei Wang180ca442017-10-06 12:05:56 -0700110 struct fib6_node *fn;
111
Wei Wang180ca442017-10-06 12:05:56 -0700112 fn = rcu_dereference_protected(rt->rt6i_node,
Ido Schimmel922c2ac2018-01-07 12:45:14 +0200113 lockdep_is_held(&rt->rt6i_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
Wei Wang81eb8442017-10-06 12:06:11 -0700148static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct fib6_node *fn;
151
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800152 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700153 if (fn)
154 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return fn;
157}
158
Wei Wang81eb8442017-10-06 12:06:11 -0700159static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700162 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Wei Wangc5cff852017-08-21 09:47:10 -0700165static void node_free_rcu(struct rcu_head *head)
166{
167 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
168
169 kmem_cache_free(fib6_node_kmem, fn);
170}
171
Wei Wang81eb8442017-10-06 12:06:11 -0700172static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700173{
174 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700175 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700176}
177
Ido Schimmela460aa82017-08-03 13:28:25 +0200178void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700179{
180 int cpu;
181
182 if (!non_pcpu_rt->rt6i_pcpu)
183 return;
184
185 for_each_possible_cpu(cpu) {
186 struct rt6_info **ppcpu_rt;
187 struct rt6_info *pcpu_rt;
188
189 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
190 pcpu_rt = *ppcpu_rt;
191 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700192 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700193 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700194 *ppcpu_rt = NULL;
195 }
196 }
197}
Ido Schimmela460aa82017-08-03 13:28:25 +0200198EXPORT_SYMBOL_GPL(rt6_free_pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200200static void fib6_free_table(struct fib6_table *table)
201{
202 inetpeer_invalidate_tree(&table->tb6_peers);
203 kfree(table);
204}
205
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800206static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700207{
208 unsigned int h;
209
Thomas Graf375216a2006-10-21 20:20:54 -0700210 /*
211 * Initialize table lock at a single place to give lockdep a key,
212 * tables aren't visible prior to being linked to the list.
213 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700214 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700215 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700216
217 /*
218 * No protection necessary, this is the only list mutatation
219 * operation, tables never disappear once they exist.
220 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800221 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700222}
223
224#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800225
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800226static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700227{
228 struct fib6_table *table;
229
230 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500231 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700232 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700233 rcu_assign_pointer(table->tb6_root.leaf,
234 net->ipv6.ip6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700235 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700236 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700237 }
238
239 return table;
240}
241
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800242struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700243{
244 struct fib6_table *tb;
245
246 if (id == 0)
247 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800248 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700249 if (tb)
250 return tb;
251
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800252 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500253 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800254 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700255
256 return tb;
257}
David Ahernb3b46632016-05-04 21:46:12 -0700258EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800260struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700261{
262 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800263 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700264 unsigned int h;
265
266 if (id == 0)
267 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700268 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700269 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800270 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800271 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700272 if (tb->tb6_id == id) {
273 rcu_read_unlock();
274 return tb;
275 }
276 }
277 rcu_read_unlock();
278
279 return NULL;
280}
David Ahernc4850682015-10-12 11:47:08 -0700281EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700282
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000283static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700284{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285 fib6_link_table(net, net->ipv6.fib6_main_tbl);
286 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700287}
Thomas Grafc71099a2006-08-04 23:20:06 -0700288#else
289
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800290struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700291{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800292 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700293}
294
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800295struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700296{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800297 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700298}
299
David S. Miller4c9483b2011-03-12 16:22:43 -0500300struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -0800301 const struct sk_buff *skb,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800302 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700303{
lucienab997ad2015-10-23 15:36:53 +0800304 struct rt6_info *rt;
305
David Ahernb75cc8f2018-03-02 08:32:17 -0800306 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300307 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800308 ip6_rt_put(rt);
309 rt = net->ipv6.ip6_null_entry;
310 dst_hold(&rt->dst);
311 }
312
313 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700314}
315
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000316static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700317{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800318 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700319}
320
321#endif
322
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200323unsigned int fib6_tables_seq_read(struct net *net)
324{
325 unsigned int h, fib_seq = 0;
326
327 rcu_read_lock();
328 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
329 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
330 struct fib6_table *tb;
331
Wei Wang66f5d6c2017-10-06 12:06:10 -0700332 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200333 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200334 }
335 rcu_read_unlock();
336
337 return fib_seq;
338}
339
340static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
341 enum fib_event_type event_type,
342 struct rt6_info *rt)
343{
344 struct fib6_entry_notifier_info info = {
345 .rt = rt,
346 };
347
348 return call_fib6_notifier(nb, net, event_type, &info.info);
349}
350
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200351static int call_fib6_entry_notifiers(struct net *net,
352 enum fib_event_type event_type,
David Ahern6c31e5a2017-10-27 17:37:13 -0700353 struct rt6_info *rt,
354 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200355{
356 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700357 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200358 .rt = rt,
359 };
360
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200361 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200362 return call_fib6_notifiers(net, event_type, &info.info);
363}
364
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200365struct fib6_dump_arg {
366 struct net *net;
367 struct notifier_block *nb;
368};
369
370static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
371{
372 if (rt == arg->net->ipv6.ip6_null_entry)
373 return;
374 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
375}
376
377static int fib6_node_dump(struct fib6_walker *w)
378{
379 struct rt6_info *rt;
380
Wei Wang66f5d6c2017-10-06 12:06:10 -0700381 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200382 fib6_rt_dump(rt, w->args);
383 w->leaf = NULL;
384 return 0;
385}
386
387static void fib6_table_dump(struct net *net, struct fib6_table *tb,
388 struct fib6_walker *w)
389{
390 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700391 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200392 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700393 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200394}
395
396/* Called with rcu_read_lock() */
397int fib6_tables_dump(struct net *net, struct notifier_block *nb)
398{
399 struct fib6_dump_arg arg;
400 struct fib6_walker *w;
401 unsigned int h;
402
403 w = kzalloc(sizeof(*w), GFP_ATOMIC);
404 if (!w)
405 return -ENOMEM;
406
407 w->func = fib6_node_dump;
408 arg.net = net;
409 arg.nb = nb;
410 w->args = &arg;
411
412 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
413 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
414 struct fib6_table *tb;
415
416 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
417 fib6_table_dump(net, tb, w);
418 }
419
420 kfree(w);
421
422 return 0;
423}
424
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200425static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700426{
427 int res;
428 struct rt6_info *rt;
429
Wei Wang66f5d6c2017-10-06 12:06:10 -0700430 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700431 res = rt6_dump_route(rt, w->args);
432 if (res < 0) {
433 /* Frame is full, suspend walking */
434 w->leaf = rt;
435 return 1;
436 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800437
438 /* Multipath routes are dumped in one route with the
439 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
440 * last sibling of this route (no need to dump the
441 * sibling routes again)
442 */
443 if (rt->rt6i_nsiblings)
444 rt = list_last_entry(&rt->rt6i_siblings,
445 struct rt6_info,
446 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700447 }
448 w->leaf = NULL;
449 return 0;
450}
451
452static void fib6_dump_end(struct netlink_callback *cb)
453{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100454 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200455 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700456
457 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800458 if (cb->args[4]) {
459 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100460 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800461 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700462 cb->args[2] = 0;
463 kfree(w);
464 }
Wang Yufen437de072014-03-28 12:07:04 +0800465 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700466 cb->args[1] = 3;
467}
468
469static int fib6_dump_done(struct netlink_callback *cb)
470{
471 fib6_dump_end(cb);
472 return cb->done ? cb->done(cb) : 0;
473}
474
475static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
476 struct netlink_callback *cb)
477{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100478 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200479 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700480 int res;
481
482 w = (void *)cb->args[2];
483 w->root = &table->tb6_root;
484
485 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000486 w->count = 0;
487 w->skip = 0;
488
Wei Wang66f5d6c2017-10-06 12:06:10 -0700489 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100490 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700491 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000492 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700493 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000494 cb->args[5] = w->root->fn_sernum;
495 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700496 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000497 if (cb->args[5] != w->root->fn_sernum) {
498 /* Begin at the root if the tree changed */
499 cb->args[5] = w->root->fn_sernum;
500 w->state = FWS_INIT;
501 w->node = w->root;
502 w->skip = w->count;
503 } else
504 w->skip = 0;
505
Wei Wang66f5d6c2017-10-06 12:06:10 -0700506 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700508 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800509 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100510 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800511 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700512 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700513 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800514
Patrick McHardy1b43af52006-08-10 23:11:17 -0700515 return res;
516}
517
Thomas Grafc127ea22007-03-22 11:58:32 -0700518static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700519{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900520 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700521 unsigned int h, s_h;
522 unsigned int e = 0, s_e;
523 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200524 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700525 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800526 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700527 int res = 0;
528
529 s_h = cb->args[0];
530 s_e = cb->args[1];
531
532 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500533 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700534 /* New dump:
535 *
536 * 1. hook callback destructor.
537 */
538 cb->args[3] = (long)cb->done;
539 cb->done = fib6_dump_done;
540
541 /*
542 * 2. allocate and initialize walker.
543 */
544 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500545 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700546 return -ENOMEM;
547 w->func = fib6_dump_node;
548 cb->args[2] = (long)w;
549 }
550
551 arg.skb = skb;
552 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700553 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700554 w->args = &arg;
555
Eric Dumazete67f88d2011-04-27 22:56:07 +0000556 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700557 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700558 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800559 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800560 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700561 if (e < s_e)
562 goto next;
563 res = fib6_dump_table(tb, skb, cb);
564 if (res != 0)
565 goto out;
566next:
567 e++;
568 }
569 }
570out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000571 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700572 cb->args[1] = e;
573 cb->args[0] = h;
574
575 res = res < 0 ? res : skb->len;
576 if (res <= 0)
577 fib6_dump_end(cb);
578 return res;
579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581/*
582 * Routing Table
583 *
584 * return the appropriate node for a routing tree "add" operation
585 * by either creating and inserting or by returning an existing
586 * node.
587 */
588
Wei Wang81eb8442017-10-06 12:06:11 -0700589static struct fib6_node *fib6_add_1(struct net *net,
590 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700591 struct fib6_node *root,
592 struct in6_addr *addr, int plen,
593 int offset, int allow_create,
594 int replace_required,
595 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct fib6_node *fn, *in, *ln;
598 struct fib6_node *pn = NULL;
599 struct rt6key *key;
600 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900601 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 RT6_TRACE("fib6_add_1\n");
604
605 /* insert node in tree */
606
607 fn = root;
608
609 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700610 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
611 lockdep_is_held(&table->tb6_lock));
612 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /*
615 * Prefix match
616 */
617 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000618 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000619 if (!allow_create) {
620 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600621 NL_SET_ERR_MSG(extack,
622 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000623 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000624 return ERR_PTR(-ENOENT);
625 }
Joe Perchesf3213832012-05-15 14:11:53 +0000626 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000629 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * Exact match ?
633 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (plen == fn->fn_bit) {
636 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500637 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700638 RCU_INIT_POINTER(fn->leaf, NULL);
639 rt6_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800640 /* remove null_entry in the root node */
641 } else if (fn->fn_flags & RTN_TL_ROOT &&
642 rcu_access_pointer(fn->leaf) ==
643 net->ipv6.ip6_null_entry) {
644 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return fn;
648 }
649
650 /*
651 * We have more bits to go
652 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 dir = addr_bit_set(addr, fn->fn_bit);
656 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700657 fn = dir ?
658 rcu_dereference_protected(fn->right,
659 lockdep_is_held(&table->tb6_lock)) :
660 rcu_dereference_protected(fn->left,
661 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } while (fn);
663
Matti Vaittinen14df0152011-11-16 21:18:02 +0000664 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000665 /* We should not create new node because
666 * NLM_F_REPLACE was specified without NLM_F_CREATE
667 * I assume it is safe to require NLM_F_CREATE when
668 * REPLACE flag is used! Later we may want to remove the
669 * check for replace_required, because according
670 * to netlink specification, NLM_F_CREATE
671 * MUST be specified if new route is created.
672 * That would keep IPv6 consistent with IPv4
673 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000674 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600675 NL_SET_ERR_MSG(extack,
676 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000677 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000678 return ERR_PTR(-ENOENT);
679 }
Joe Perchesf3213832012-05-15 14:11:53 +0000680 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * We walked to the bottom of tree.
684 * Create new leaf node without children.
685 */
686
Wei Wang81eb8442017-10-06 12:06:11 -0700687 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David S. Miller507c9b12011-12-03 17:50:45 -0500689 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000690 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700692 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700695 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700697 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return ln;
700
701
702insert_above:
703 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900704 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * we have a less significant route.
706 * we've to insert an intermediate node on the list
707 * this new node will point to the one we need to create
708 * and the current
709 */
710
Wei Wang66f5d6c2017-10-06 12:06:10 -0700711 pn = rcu_dereference_protected(fn->parent,
712 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* find 1st bit in difference between the 2 addrs.
715
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800716 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 but if it is >= plen, the value is ignored in any case.
718 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900719
fan.du9225b232013-07-22 14:21:09 +0800720 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900722 /*
723 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * / \
725 * (new leaf node)[ln] (old node)[fn]
726 */
727 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700728 in = node_alloc(net);
729 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900730
David S. Miller507c9b12011-12-03 17:50:45 -0500731 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700733 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700735 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000736 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900739 /*
740 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * RTN_RTINFO will
742 * be off since that an address that chooses one of
743 * the branches would not match less specific routes
744 * in the other branch
745 */
746
747 in->fn_bit = bit;
748
Wei Wang66f5d6c2017-10-06 12:06:10 -0700749 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700751 atomic_inc(&rcu_dereference_protected(in->leaf,
752 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* update parent pointer */
755 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700756 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700758 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 ln->fn_bit = plen;
761
Wei Wang66f5d6c2017-10-06 12:06:10 -0700762 RCU_INIT_POINTER(ln->parent, in);
763 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700766 rcu_assign_pointer(in->right, ln);
767 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700769 rcu_assign_pointer(in->left, ln);
770 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 } else { /* plen <= bit */
773
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900774 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * (new leaf node)[ln]
776 * / \
777 * (old node)[fn] NULL
778 */
779
Wei Wang81eb8442017-10-06 12:06:11 -0700780 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
David S. Miller507c9b12011-12-03 17:50:45 -0500782 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000783 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 ln->fn_bit = plen;
786
Wei Wang66f5d6c2017-10-06 12:06:10 -0700787 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700790 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700792 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Wei Wang66f5d6c2017-10-06 12:06:10 -0700794 rcu_assign_pointer(fn->parent, ln);
795
796 if (dir)
797 rcu_assign_pointer(pn->right, ln);
798 else
799 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 return ln;
802}
803
Florian Westphale715b6d2015-01-05 23:57:44 +0100804static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100805{
Florian Westphale715b6d2015-01-05 23:57:44 +0100806 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100807
Florian Westphale715b6d2015-01-05 23:57:44 +0100808 for (i = 0; i < RTAX_MAX; i++) {
809 if (test_bit(i, mxc->mx_valid))
810 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100811 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100812}
813
814static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
815{
816 if (!mxc->mx)
817 return 0;
818
819 if (dst->flags & DST_HOST) {
820 u32 *mp = dst_metrics_write_ptr(dst);
821
822 if (unlikely(!mp))
823 return -ENOMEM;
824
825 fib6_copy_metrics(mp, mxc);
826 } else {
827 dst_init_metrics(dst, mxc->mx, false);
828
829 /* We've stolen mx now. */
830 mxc->mx = NULL;
831 }
832
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100833 return 0;
834}
835
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100836static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
837 struct net *net)
838{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700839 struct fib6_table *table = rt->rt6i_table;
840
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100841 if (atomic_read(&rt->rt6i_ref) != 1) {
842 /* This route is used as dummy address holder in some split
843 * nodes. It is not leaked, but it still holds other resources,
844 * which must be released in time. So, scan ascendant nodes
845 * and replace dummy references to this route with references
846 * to still alive ones.
847 */
848 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700849 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
850 lockdep_is_held(&table->tb6_lock));
851 struct rt6_info *new_leaf;
852 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
853 new_leaf = fib6_find_prefix(net, table, fn);
854 atomic_inc(&new_leaf->rt6i_ref);
855 rcu_assign_pointer(fn->leaf, new_leaf);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100856 rt6_release(rt);
857 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700858 fn = rcu_dereference_protected(fn->parent,
859 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100860 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100861 }
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
865 * Insert routing information in a node.
866 */
867
868static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700869 struct nl_info *info, struct mx6_config *mxc,
870 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700872 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
873 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct rt6_info *iter = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700875 struct rt6_info __rcu **ins;
876 struct rt6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500877 int replace = (info->nlh &&
878 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
879 int add = (!info->nlh ||
880 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000881 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200882 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200883 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100884 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David Ahern1f5e29c2017-01-31 16:51:37 -0800886 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
887 nlflags |= NLM_F_APPEND;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 ins = &fn->leaf;
890
Wei Wang66f5d6c2017-10-06 12:06:10 -0700891 for (iter = leaf; iter;
David Miller071fb372017-11-28 15:40:15 -0500892 iter = rcu_dereference_protected(iter->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700893 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Search for duplicates
896 */
897
898 if (iter->rt6i_metric == rt->rt6i_metric) {
899 /*
900 * Same priority level
901 */
David S. Miller507c9b12011-12-03 17:50:45 -0500902 if (info->nlh &&
903 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000904 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200905
906 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000907 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200908 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
909 found++;
910 break;
911 }
912 if (rt_can_ecmp)
913 fallback_ins = fallback_ins ?: ins;
914 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
David Ahernf06b7542017-07-05 14:41:46 -0600917 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000918 if (rt->rt6i_nsiblings)
919 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500920 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000922 if (!(rt->rt6i_flags & RTF_EXPIRES))
923 rt6_clean_expires(iter);
924 else
925 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700926 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -EEXIST;
928 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000929 /* If we have the same destination and the same metric,
930 * but not the same gateway, then the route we try to
931 * add is sibling to this route, increment our counter
932 * of siblings, and later we will add our route to the
933 * list.
934 * Only static routes (which don't have flag
935 * RTF_EXPIRES) are used for ECMPv6.
936 *
937 * To avoid long list, we only had siblings if the
938 * route have a gateway.
939 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200940 if (rt_can_ecmp &&
941 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000942 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
945 if (iter->rt6i_metric > rt->rt6i_metric)
946 break;
947
Michal Kubeček27596472015-05-18 20:54:00 +0200948next_iter:
David Miller071fb372017-11-28 15:40:15 -0500949 ins = &iter->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Michal Kubeček27596472015-05-18 20:54:00 +0200952 if (fallback_ins && !found) {
953 /* No ECMP-able route found, replace first non-ECMP one */
954 ins = fallback_ins;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700955 iter = rcu_dereference_protected(*ins,
956 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +0200957 found++;
958 }
959
David S. Millerf11e6652007-03-24 20:36:25 -0700960 /* Reset round-robin state, if necessary */
961 if (ins == &fn->leaf)
962 fn->rr_ptr = NULL;
963
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000964 /* Link this route to others same route. */
965 if (rt->rt6i_nsiblings) {
966 unsigned int rt6i_nsiblings;
967 struct rt6_info *sibling, *temp_sibling;
968
969 /* Find the first route that have the same metric */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700970 sibling = leaf;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000971 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200972 if (sibling->rt6i_metric == rt->rt6i_metric &&
973 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000974 list_add_tail(&rt->rt6i_siblings,
975 &sibling->rt6i_siblings);
976 break;
977 }
David Miller071fb372017-11-28 15:40:15 -0500978 sibling = rcu_dereference_protected(sibling->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700979 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000980 }
981 /* For each sibling in the list, increment the counter of
982 * siblings. BUG() if counters does not match, list of siblings
983 * is broken!
984 */
985 rt6i_nsiblings = 0;
986 list_for_each_entry_safe(sibling, temp_sibling,
987 &rt->rt6i_siblings, rt6i_siblings) {
988 sibling->rt6i_nsiblings++;
989 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
990 rt6i_nsiblings++;
991 }
992 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +0200993 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /*
997 * insert node
998 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000999 if (!replace) {
1000 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001001 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001003add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001004 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001005 err = fib6_commit_metrics(&rt->dst, mxc);
1006 if (err)
1007 return err;
1008
David Ahern22330002018-03-27 18:21:59 -07001009 err = call_fib6_entry_notifiers(info->nl_net,
1010 FIB_EVENT_ENTRY_ADD,
1011 rt, extack);
1012 if (err)
1013 return err;
1014
David Miller071fb372017-11-28 15:40:15 -05001015 rcu_assign_pointer(rt->rt6_next, iter);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001016 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001017 rcu_assign_pointer(rt->rt6i_node, fn);
1018 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001019 if (!info->skip_notify)
1020 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001021 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
David S. Miller507c9b12011-12-03 17:50:45 -05001023 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001024 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1025 fn->fn_flags |= RTN_RTINFO;
1026 }
1027
1028 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001029 int nsiblings;
1030
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001031 if (!found) {
1032 if (add)
1033 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001034 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001035 return -ENOENT;
1036 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001037
1038 err = fib6_commit_metrics(&rt->dst, mxc);
1039 if (err)
1040 return err;
1041
David Ahern22330002018-03-27 18:21:59 -07001042 err = call_fib6_entry_notifiers(info->nl_net,
1043 FIB_EVENT_ENTRY_REPLACE,
1044 rt, extack);
1045 if (err)
1046 return err;
1047
Wei Wang66f5d6c2017-10-06 12:06:10 -07001048 atomic_inc(&rt->rt6i_ref);
Wei Wang4e587ea2017-08-25 15:03:10 -07001049 rcu_assign_pointer(rt->rt6i_node, fn);
David Miller071fb372017-11-28 15:40:15 -05001050 rt->rt6_next = iter->rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001051 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001052 if (!info->skip_notify)
1053 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001054 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001055 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1056 fn->fn_flags |= RTN_RTINFO;
1057 }
Michal Kubeček27596472015-05-18 20:54:00 +02001058 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001059 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001060 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001061 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001062 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001063 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001064
1065 if (nsiblings) {
1066 /* Replacing an ECMP route, remove all siblings */
David Miller071fb372017-11-28 15:40:15 -05001067 ins = &rt->rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001068 iter = rcu_dereference_protected(*ins,
1069 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001070 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001071 if (iter->rt6i_metric > rt->rt6i_metric)
1072 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001073 if (rt6_qualify_for_ecmp(iter)) {
David Miller071fb372017-11-28 15:40:15 -05001074 *ins = iter->rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001075 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001076 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001077 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001078 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001079 rt6_release(iter);
1080 nsiblings--;
Wei Wang81eb8442017-10-06 12:06:11 -07001081 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001082 } else {
David Miller071fb372017-11-28 15:40:15 -05001083 ins = &iter->rt6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001084 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001085 iter = rcu_dereference_protected(*ins,
1086 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001087 }
1088 WARN_ON(nsiblings != 0);
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
1092 return 0;
1093}
1094
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001095static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001097 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001098 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001099 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001100 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001103void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001105 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1106 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001107 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001110static void __fib6_update_sernum_upto_root(struct rt6_info *rt,
1111 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001112{
1113 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1114 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1115
1116 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1117 smp_wmb();
1118 while (fn) {
1119 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001120 fn = rcu_dereference_protected(fn->parent,
1121 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001122 }
1123}
1124
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001125void fib6_update_sernum_upto_root(struct net *net, struct rt6_info *rt)
1126{
1127 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * Add routing information to the routing tree.
1132 * <destination addr>/<source addr>
1133 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001134 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
1136
Florian Westphale715b6d2015-01-05 23:57:44 +01001137int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001138 struct nl_info *info, struct mx6_config *mxc,
1139 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001141 struct fib6_table *table = rt->rt6i_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001142 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001144 int allow_create = 1;
1145 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001146 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001147
Wei Wanga4c2fd72017-06-17 10:42:42 -07001148 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001149 return -EINVAL;
Wei Wang2b760fc2017-10-06 12:06:03 -07001150 if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
1151 return -EINVAL;
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001152
David S. Miller507c9b12011-12-03 17:50:45 -05001153 if (info->nlh) {
1154 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001155 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001156 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001157 replace_required = 1;
1158 }
1159 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001160 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Wei Wang81eb8442017-10-06 12:06:11 -07001162 fn = fib6_add_1(info->nl_net, table, root,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001163 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
fan.du9225b232013-07-22 14:21:09 +08001164 offsetof(struct rt6_info, rt6i_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001165 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001166 if (IS_ERR(fn)) {
1167 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001168 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001172 pn = fn;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174#ifdef CONFIG_IPV6_SUBTREES
1175 if (rt->rt6i_src.plen) {
1176 struct fib6_node *sn;
1177
Wei Wang66f5d6c2017-10-06 12:06:10 -07001178 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct fib6_node *sfn;
1180
1181 /*
1182 * Create subtree.
1183 *
1184 * fn[main tree]
1185 * |
1186 * sfn[subtree root]
1187 * \
1188 * sn[new leaf node]
1189 */
1190
1191 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001192 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001193 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001194 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001196 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001197 rcu_assign_pointer(sfn->leaf,
1198 info->nl_net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* Now add the first leaf node to new subtree */
1202
Wei Wang81eb8442017-10-06 12:06:11 -07001203 sn = fib6_add_1(info->nl_net, table, sfn,
1204 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001205 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001206 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001208 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001210 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 in main tree.
1212 */
Wei Wang81eb8442017-10-06 12:06:11 -07001213 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001214 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001215 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001219 rcu_assign_pointer(sfn->parent, fn);
1220 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001222 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1223 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001224 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001225 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001227 if (IS_ERR(sn)) {
1228 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001229 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Wei Wang66f5d6c2017-10-06 12:06:10 -07001233 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001234 if (fn->fn_flags & RTN_TL_ROOT) {
1235 /* put back null_entry for root node */
1236 rcu_assign_pointer(fn->leaf,
1237 info->nl_net->ipv6.ip6_null_entry);
1238 } else {
1239 atomic_inc(&rt->rt6i_ref);
1240 rcu_assign_pointer(fn->leaf, rt);
1241 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 fn = sn;
1244 }
1245#endif
1246
David Ahern6c31e5a2017-10-27 17:37:13 -07001247 err = fib6_add_rt2node(fn, rt, info, mxc, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001248 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001249 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001250 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001254 if (err) {
1255#ifdef CONFIG_IPV6_SUBTREES
1256 /*
1257 * If fib6_add_1 has cleared the old leaf pointer in the
1258 * super-tree leaf node we have to find a new one for it.
1259 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001260 if (pn != fn) {
1261 struct rt6_info *pn_leaf =
1262 rcu_dereference_protected(pn->leaf,
1263 lockdep_is_held(&table->tb6_lock));
1264 if (pn_leaf == rt) {
1265 pn_leaf = NULL;
1266 RCU_INIT_POINTER(pn->leaf, NULL);
1267 atomic_dec(&rt->rt6i_ref);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001268 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001269 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1270 pn_leaf = fib6_find_prefix(info->nl_net, table,
1271 pn);
1272#if RT6_DEBUG >= 2
1273 if (!pn_leaf) {
1274 WARN_ON(!pn_leaf);
1275 pn_leaf =
1276 info->nl_net->ipv6.ip6_null_entry;
1277 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001278#endif
Wei Wang7bbfe002018-01-03 14:11:59 -08001279 atomic_inc(&pn_leaf->rt6i_ref);
1280 rcu_assign_pointer(pn->leaf, pn_leaf);
1281 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001282 }
1283#endif
Wei Wang348a4002017-08-18 17:14:49 -07001284 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return err;
1287
Wei Wang348a4002017-08-18 17:14:49 -07001288failure:
Wei Wang4512c432018-01-08 10:34:00 -08001289 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1290 * 1. fn is an intermediate node and we failed to add the new
1291 * route to it in both subtree creation failure and fib6_add_rt2node()
1292 * failure case.
1293 * 2. fn is the root node in the table and we fail to add the first
1294 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Wei Wang4512c432018-01-08 10:34:00 -08001296 if (fn &&
1297 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1298 (fn->fn_flags & RTN_TL_ROOT &&
1299 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001300 fib6_repair_tree(info->nl_net, table, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001301 /* Always release dst as dst->__refcnt is guaranteed
1302 * to be taken before entering this function
1303 */
Wei Wang587fea72017-06-17 10:42:36 -07001304 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/*
1309 * Routing tree lookup
1310 *
1311 */
1312
1313struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001314 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001315 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316};
1317
Wang Yufen437de072014-03-28 12:07:04 +08001318static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1319 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001322 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001324 if (unlikely(args->offset == 0))
1325 return NULL;
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
1328 * Descend on a tree
1329 */
1330
1331 fn = root;
1332
1333 for (;;) {
1334 struct fib6_node *next;
1335
1336 dir = addr_bit_set(args->addr, fn->fn_bit);
1337
Wei Wang66f5d6c2017-10-06 12:06:10 -07001338 next = dir ? rcu_dereference(fn->right) :
1339 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (next) {
1342 fn = next;
1343 continue;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
1346 }
1347
David S. Miller507c9b12011-12-03 17:50:45 -05001348 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001349 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1350
1351 if (subtree || fn->fn_flags & RTN_RTINFO) {
1352 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct rt6key *key;
1354
Wei Wang8d1040e2017-10-06 12:06:08 -07001355 if (!leaf)
1356 goto backtrack;
1357
1358 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001360 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1361#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001362 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001363 struct fib6_node *sfn;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001364 sfn = fib6_lookup_1(subtree, args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001365 if (!sfn)
1366 goto backtrack;
1367 fn = sfn;
1368 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001369#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001370 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001371 return fn;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001374backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001375 if (fn->fn_flags & RTN_ROOT)
1376 break;
1377
Wei Wang66f5d6c2017-10-06 12:06:10 -07001378 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
1381 return NULL;
1382}
1383
Wei Wang66f5d6c2017-10-06 12:06:10 -07001384/* called with rcu_read_lock() held
1385 */
Wang Yufen437de072014-03-28 12:07:04 +08001386struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1387 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001390 struct lookup_args args[] = {
1391 {
1392 .offset = offsetof(struct rt6_info, rt6i_dst),
1393 .addr = daddr,
1394 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001396 {
1397 .offset = offsetof(struct rt6_info, rt6i_src),
1398 .addr = saddr,
1399 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001401 {
1402 .offset = 0, /* sentinel */
1403 }
1404 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001406 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001407 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 fn = root;
1409
1410 return fn;
1411}
1412
1413/*
1414 * Get node with specified destination prefix (and source prefix,
1415 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001416 * exact_match == true means we try to find fn with exact match of
1417 * the passed in prefix addr
1418 * exact_match == false means we try to find fn with longest prefix
1419 * match of the passed in prefix addr. This is useful for finding fn
1420 * for cached route as it will be stored in the exception table under
1421 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
1423
1424
Wang Yufen437de072014-03-28 12:07:04 +08001425static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1426 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001427 int plen, int offset,
1428 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Wei Wang38fbeee2017-10-06 12:06:02 -07001430 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 for (fn = root; fn ; ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001433 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001434 struct rt6key *key;
1435
1436 /* This node is being deleted */
1437 if (!leaf) {
1438 if (plen <= fn->fn_bit)
1439 goto out;
1440 else
1441 goto next;
1442 }
1443
1444 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /*
1447 * Prefix match
1448 */
1449 if (plen < fn->fn_bit ||
1450 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (plen == fn->fn_bit)
1454 return fn;
1455
Wei Wang38fbeee2017-10-06 12:06:02 -07001456 prev = fn;
1457
Wei Wang8d1040e2017-10-06 12:06:08 -07001458next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /*
1460 * We have more bits to go
1461 */
1462 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001463 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001465 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001467out:
1468 if (exact_match)
1469 return NULL;
1470 else
1471 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Wang Yufen437de072014-03-28 12:07:04 +08001474struct fib6_node *fib6_locate(struct fib6_node *root,
1475 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001476 const struct in6_addr *saddr, int src_len,
1477 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct fib6_node *fn;
1480
1481 fn = fib6_locate_1(root, daddr, dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001482 offsetof(struct rt6_info, rt6i_dst),
1483 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485#ifdef CONFIG_IPV6_SUBTREES
1486 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001487 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001488 if (fn) {
1489 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1490
1491 if (subtree) {
1492 fn = fib6_locate_1(subtree, saddr, src_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001493 offsetof(struct rt6_info, rt6i_src),
1494 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001495 }
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498#endif
1499
David S. Miller507c9b12011-12-03 17:50:45 -05001500 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return fn;
1502
1503 return NULL;
1504}
1505
1506
1507/*
1508 * Deletion
1509 *
1510 */
1511
Wei Wang66f5d6c2017-10-06 12:06:10 -07001512static struct rt6_info *fib6_find_prefix(struct net *net,
1513 struct fib6_table *table,
1514 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001516 struct fib6_node *child_left, *child_right;
1517
David S. Miller507c9b12011-12-03 17:50:45 -05001518 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001519 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
David S. Miller507c9b12011-12-03 17:50:45 -05001521 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001522 child_left = rcu_dereference_protected(fn->left,
1523 lockdep_is_held(&table->tb6_lock));
1524 child_right = rcu_dereference_protected(fn->right,
1525 lockdep_is_held(&table->tb6_lock));
1526 if (child_left)
1527 return rcu_dereference_protected(child_left->leaf,
1528 lockdep_is_held(&table->tb6_lock));
1529 if (child_right)
1530 return rcu_dereference_protected(child_right->leaf,
1531 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001533 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535 return NULL;
1536}
1537
1538/*
1539 * Called to trim the tree of intermediate nodes when possible. "fn"
1540 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001541 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
1543
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001544static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001545 struct fib6_table *table,
1546 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 int children;
1549 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001550 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001551 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int iter = 0;
1553
Wei Wang4512c432018-01-08 10:34:00 -08001554 /* Set fn->leaf to null_entry for root node. */
1555 if (fn->fn_flags & RTN_TL_ROOT) {
1556 rcu_assign_pointer(fn->leaf, net->ipv6.ip6_null_entry);
1557 return fn;
1558 }
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001561 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1562 lockdep_is_held(&table->tb6_lock));
1563 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1564 lockdep_is_held(&table->tb6_lock));
1565 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1566 lockdep_is_held(&table->tb6_lock));
1567 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1568 lockdep_is_held(&table->tb6_lock));
1569 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1570 lockdep_is_held(&table->tb6_lock));
1571 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1572 lockdep_is_held(&table->tb6_lock));
1573 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1574 lockdep_is_held(&table->tb6_lock));
1575 struct rt6_info *new_fn_leaf;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1578 iter++;
1579
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001580 WARN_ON(fn->fn_flags & RTN_RTINFO);
1581 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001582 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 children = 0;
1585 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001586 if (fn_r)
1587 child = fn_r, children |= 1;
1588 if (fn_l)
1589 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001591 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592#ifdef CONFIG_IPV6_SUBTREES
1593 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001594 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595#endif
1596 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001597 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001599 if (!new_fn_leaf) {
1600 WARN_ON(!new_fn_leaf);
1601 new_fn_leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001604 atomic_inc(&new_fn_leaf->rt6i_ref);
1605 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1606 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001610 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001611 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001612 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 nstate = FWS_L;
1614 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001615 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001617 if (pn_r == fn)
1618 rcu_assign_pointer(pn->right, child);
1619 else if (pn_l == fn)
1620 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001622 else
1623 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624#endif
1625 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001626 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 nstate = FWS_R;
1628#ifdef CONFIG_IPV6_SUBTREES
1629 }
1630#endif
1631
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001632 read_lock(&net->ipv6.fib6_walker_lock);
1633 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001634 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001635 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1637 w->node = pn;
1638 w->state = nstate;
1639 }
1640 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (w->node == fn) {
1642 w->node = child;
1643 if (children&2) {
1644 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001645 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 } else {
1647 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001648 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650 }
1651 }
1652 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001653 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Wei Wang81eb8442017-10-06 12:06:11 -07001655 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001656 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return pn;
1658
Wei Wang66f5d6c2017-10-06 12:06:10 -07001659 RCU_INIT_POINTER(pn->leaf, NULL);
1660 rt6_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 fn = pn;
1662 }
1663}
1664
Wei Wang66f5d6c2017-10-06 12:06:10 -07001665static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1666 struct rt6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001668 struct fib6_walker *w;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001669 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1670 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001671 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 RT6_TRACE("fib6_del_route\n");
1674
Wei Wang2b760fc2017-10-06 12:06:03 -07001675 WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* Unlink it */
David Miller071fb372017-11-28 15:40:15 -05001678 *rtp = rt->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001680 net->ipv6.rt6_stats->fib_rt_entries--;
1681 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Wei Wang2b760fc2017-10-06 12:06:03 -07001683 /* Flush all cached dst in exception table */
1684 rt6_flush_exceptions(rt);
1685
David S. Millerf11e6652007-03-24 20:36:25 -07001686 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001687 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001688 fn->rr_ptr = NULL;
1689
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001690 /* Remove this entry from other siblings */
1691 if (rt->rt6i_nsiblings) {
1692 struct rt6_info *sibling, *next_sibling;
1693
1694 list_for_each_entry_safe(sibling, next_sibling,
1695 &rt->rt6i_siblings, rt6i_siblings)
1696 sibling->rt6i_nsiblings--;
1697 rt->rt6i_nsiblings = 0;
1698 list_del_init(&rt->rt6i_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001699 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001700 }
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001703 read_lock(&net->ipv6.fib6_walker_lock);
1704 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (w->state == FWS_C && w->leaf == rt) {
1706 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Miller071fb372017-11-28 15:40:15 -05001707 w->leaf = rcu_dereference_protected(rt->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001708 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001709 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 w->state = FWS_U;
1711 }
1712 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001713 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Wei Wang4512c432018-01-08 10:34:00 -08001715 /* If it was last route, call fib6_repair_tree() to:
1716 * 1. For root node, put back null_entry as how the table was created.
1717 * 2. For other nodes, expunge its radix tree node.
1718 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001719 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001720 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1721 fn->fn_flags &= ~RTN_RTINFO;
1722 net->ipv6.rt6_stats->fib_route_nodes--;
1723 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001724 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001727 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
David Ahern6c31e5a2017-10-27 17:37:13 -07001729 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001730 if (!info->skip_notify)
1731 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 rt6_release(rt);
1733}
1734
Wei Wang66f5d6c2017-10-06 12:06:10 -07001735/* Need to own table->tb6_lock */
Thomas Graf86872cb2006-08-22 00:01:08 -07001736int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Wei Wang4e587ea2017-08-25 15:03:10 -07001738 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1739 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001740 struct fib6_table *table = rt->rt6i_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001741 struct net *net = info->nl_net;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001742 struct rt6_info __rcu **rtp;
1743 struct rt6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001746 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001747 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return -ENOENT;
1749 }
1750#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001751 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return -ENOENT;
1753
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001754 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Wei Wang2b760fc2017-10-06 12:06:03 -07001756 /* remove cached dst from exception table */
1757 if (rt->rt6i_flags & RTF_CACHE)
1758 return rt6_remove_exception_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 /*
1761 * Walk the leaf entries looking for ourself
1762 */
1763
Wei Wang66f5d6c2017-10-06 12:06:10 -07001764 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1765 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1766 lockdep_is_held(&table->tb6_lock));
1767 if (rt == cur) {
1768 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return 0;
1770 }
David Miller071fb372017-11-28 15:40:15 -05001771 rtp_next = &cur->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 return -ENOENT;
1774}
1775
1776/*
1777 * Tree traversal function.
1778 *
1779 * Certainly, it is not interrupt safe.
1780 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1781 * It means, that we can modify tree during walking
1782 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001783 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 *
1785 * It guarantees that every node will be traversed,
1786 * and that it will be traversed only once.
1787 *
1788 * Callback function w->func may return:
1789 * 0 -> continue walking.
1790 * positive value -> walking is suspended (used by tree dumps,
1791 * and probably by gc, if it will be split to several slices)
1792 * negative value -> terminate walking.
1793 *
1794 * The function itself returns:
1795 * 0 -> walk is complete.
1796 * >0 -> walk is incomplete (i.e. suspended)
1797 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001798 *
1799 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
1801
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001802static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001804 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Wei Wang2b760fc2017-10-06 12:06:03 -07001806 /* w->root should always be table->tb6_root */
1807 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 for (;;) {
1810 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001811 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 switch (w->state) {
1815#ifdef CONFIG_IPV6_SUBTREES
1816 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001817 if (FIB6_SUBTREE(fn)) {
1818 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 continue;
1820 }
1821 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001822#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001823 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001825 left = rcu_dereference_protected(fn->left, 1);
1826 if (left) {
1827 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 w->state = FWS_INIT;
1829 continue;
1830 }
1831 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001832 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001834 right = rcu_dereference_protected(fn->right, 1);
1835 if (right) {
1836 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 w->state = FWS_INIT;
1838 continue;
1839 }
1840 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001841 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001842 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001844 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001845 int err;
1846
Eric Dumazetfa809e22012-06-25 15:37:19 -07001847 if (w->skip) {
1848 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001849 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001850 }
1851
1852 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (err)
1854 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001855
1856 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 continue;
1858 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001859skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001861 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 case FWS_U:
1863 if (fn == w->root)
1864 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001865 pn = rcu_dereference_protected(fn->parent, 1);
1866 left = rcu_dereference_protected(pn->left, 1);
1867 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 w->node = pn;
1869#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001870 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001871 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 w->state = FWS_L;
1873 continue;
1874 }
1875#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001876 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 w->state = FWS_R;
1878 continue;
1879 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001880 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001882 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 continue;
1884 }
1885#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001886 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887#endif
1888 }
1889 }
1890}
1891
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001892static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 int res;
1895
1896 w->state = FWS_INIT;
1897 w->node = w->root;
1898
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001899 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 res = fib6_walk_continue(w);
1901 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001902 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return res;
1904}
1905
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001906static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 int res;
1909 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001910 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001911 struct nl_info info = {
1912 .nl_net = c->net,
1913 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001915 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1916 w->node->fn_sernum != c->sernum)
1917 w->node->fn_sernum = c->sernum;
1918
1919 if (!c->func) {
1920 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1921 w->leaf = NULL;
1922 return 0;
1923 }
1924
Wei Wang66f5d6c2017-10-06 12:06:10 -07001925 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001927 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001929 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (res) {
1931#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001932 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001933 __func__, rt,
1934 rcu_access_pointer(rt->rt6i_node),
1935 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936#endif
1937 continue;
1938 }
1939 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001940 } else if (res == -2) {
1941 if (WARN_ON(!rt->rt6i_nsiblings))
1942 continue;
1943 rt = list_last_entry(&rt->rt6i_siblings,
1944 struct rt6_info, rt6i_siblings);
1945 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001947 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 w->leaf = rt;
1950 return 0;
1951}
1952
1953/*
1954 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001955 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001957 * It may return -2 -> skip multipath route.
1958 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
1961
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001962static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001963 int (*func)(struct rt6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001964 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001966 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 c.w.root = root;
1969 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001970 c.w.count = 0;
1971 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001973 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001975 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001977 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001980static void __fib6_clean_all(struct net *net,
1981 int (*func)(struct rt6_info *, void *),
1982 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001983{
Thomas Grafc71099a2006-08-04 23:20:06 -07001984 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001985 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001986 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001987
Patrick McHardy1b43af52006-08-10 23:11:17 -07001988 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001989 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001990 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001991 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001992 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001993 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07001994 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001995 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001996 }
1997 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001998 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001999}
2000
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002001void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
2002 void *arg)
2003{
2004 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2005}
2006
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002007static void fib6_flush_trees(struct net *net)
2008{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02002009 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002010
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02002011 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/*
2015 * Garbage collection
2016 */
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018static int fib6_age(struct rt6_info *rt, void *arg)
2019{
Michal Kubeček3570df92016-03-08 14:44:25 +01002020 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 unsigned long now = jiffies;
2022
2023 /*
2024 * check addrconf expiration here.
2025 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
2027
David S. Millerd1918542011-12-28 20:19:20 -05002028 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
2029 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return -1;
2032 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002033 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Wei Wangc757faa2017-10-06 12:06:01 -07002036 /* Also age clones in the exception table.
2037 * Note, that clones are aged out
2038 * only if they are not in use now.
2039 */
2040 rt6_age_exceptions(rt, gc_args, now);
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
2043}
2044
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002045void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Michal Kubeček3570df92016-03-08 14:44:25 +01002047 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002048 unsigned long now;
2049
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002050 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002051 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2052 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002053 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2054 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002056 gc_args.timeout = expires ? (int)expires :
2057 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002058 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002059
Michal Kubeček3570df92016-03-08 14:44:25 +01002060 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002061 now = jiffies;
2062 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002065 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002066 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002067 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002068 else
2069 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002070 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Kees Cook86cb30e2017-10-17 20:21:24 -07002073static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002074{
Kees Cook86cb30e2017-10-17 20:21:24 -07002075 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2076
2077 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002078}
2079
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002080static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002081{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002082 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002083 int err;
2084
2085 err = fib6_notifier_init(net);
2086 if (err)
2087 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002088
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002089 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002090 rwlock_init(&net->ipv6.fib6_walker_lock);
2091 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002092 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002093
Benjamin Theryc5728722008-03-03 23:34:17 -08002094 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2095 if (!net->ipv6.rt6_stats)
2096 goto out_timer;
2097
Eric Dumazet10da66f2010-10-13 08:22:03 +00002098 /* Avoid false sharing : Use at least a full cache line */
2099 size = max_t(size_t, size, L1_CACHE_BYTES);
2100
2101 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002102 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002103 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002104
2105 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2106 GFP_KERNEL);
2107 if (!net->ipv6.fib6_main_tbl)
2108 goto out_fib_table_hash;
2109
2110 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002111 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2112 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002113 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2114 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002115 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002116
2117#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2118 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2119 GFP_KERNEL);
2120 if (!net->ipv6.fib6_local_tbl)
2121 goto out_fib6_main_tbl;
2122 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002123 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2124 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002125 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2126 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002127 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002128#endif
2129 fib6_tables_init(net);
2130
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002131 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002132
2133#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2134out_fib6_main_tbl:
2135 kfree(net->ipv6.fib6_main_tbl);
2136#endif
2137out_fib_table_hash:
2138 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002139out_rt6_stats:
2140 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002141out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002142 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002143 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002144}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002145
2146static void fib6_net_exit(struct net *net)
2147{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002148 unsigned int i;
2149
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002150 del_timer_sync(&net->ipv6.ip6_fib_timer);
2151
Eric Dumazet32a805b2017-09-08 15:48:47 -07002152 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002153 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2154 struct hlist_node *tmp;
2155 struct fib6_table *tb;
2156
2157 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2158 hlist_del(&tb->tb6_hlist);
2159 fib6_free_table(tb);
2160 }
2161 }
2162
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002163 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002164 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002165 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002166}
2167
2168static struct pernet_operations fib6_net_ops = {
2169 .init = fib6_net_init,
2170 .exit = fib6_net_exit,
2171};
2172
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002173int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002175 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2178 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002179 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002180 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002181 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002182 goto out;
2183
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002184 ret = register_pernet_subsys(&fib6_net_ops);
2185 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002186 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002187
Florian Westphal16feebc2017-12-02 21:44:08 +01002188 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2189 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002190 if (ret)
2191 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002192
2193 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002194out:
2195 return ret;
2196
David S. Millere8803b62012-06-16 01:12:19 -07002197out_unregister_subsys:
2198 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002199out_kmem_cache_create:
2200 kmem_cache_destroy(fib6_node_kmem);
2201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204void fib6_gc_cleanup(void)
2205{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002206 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 kmem_cache_destroy(fib6_node_kmem);
2208}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002209
2210#ifdef CONFIG_PROC_FS
2211
2212struct ipv6_route_iter {
2213 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002214 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002215 loff_t skip;
2216 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002217 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002218};
2219
2220static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2221{
2222 struct rt6_info *rt = v;
2223 struct ipv6_route_iter *iter = seq->private;
David Ahern5e670d82018-04-17 17:33:14 -07002224 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002225
2226 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2227
2228#ifdef CONFIG_IPV6_SUBTREES
2229 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2230#else
2231 seq_puts(seq, "00000000000000000000000000000000 00 ");
2232#endif
2233 if (rt->rt6i_flags & RTF_GATEWAY)
David Ahern5e670d82018-04-17 17:33:14 -07002234 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002235 else
2236 seq_puts(seq, "00000000000000000000000000000000");
2237
David Ahern5e670d82018-04-17 17:33:14 -07002238 dev = rt->fib6_nh.nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002239 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2240 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2241 rt->dst.__use, rt->rt6i_flags,
David Ahern5e670d82018-04-17 17:33:14 -07002242 dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002243 iter->w.leaf = NULL;
2244 return 0;
2245}
2246
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002247static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002248{
2249 struct ipv6_route_iter *iter = w->args;
2250
2251 if (!iter->skip)
2252 return 1;
2253
2254 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002255 iter->w.leaf = rcu_dereference_protected(
David Miller071fb372017-11-28 15:40:15 -05002256 iter->w.leaf->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002257 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002258 iter->skip--;
2259 if (!iter->skip && iter->w.leaf)
2260 return 1;
2261 } while (iter->w.leaf);
2262
2263 return 0;
2264}
2265
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002266static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2267 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002268{
2269 memset(&iter->w, 0, sizeof(iter->w));
2270 iter->w.func = ipv6_route_yield;
2271 iter->w.root = &iter->tbl->tb6_root;
2272 iter->w.state = FWS_INIT;
2273 iter->w.node = iter->w.root;
2274 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002275 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002276 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002277 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002278}
2279
2280static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2281 struct net *net)
2282{
2283 unsigned int h;
2284 struct hlist_node *node;
2285
2286 if (tbl) {
2287 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2288 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2289 } else {
2290 h = 0;
2291 node = NULL;
2292 }
2293
2294 while (!node && h < FIB6_TABLE_HASHSZ) {
2295 node = rcu_dereference_bh(
2296 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2297 }
2298 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2299}
2300
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002301static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2302{
2303 if (iter->sernum != iter->w.root->fn_sernum) {
2304 iter->sernum = iter->w.root->fn_sernum;
2305 iter->w.state = FWS_INIT;
2306 iter->w.node = iter->w.root;
2307 WARN_ON(iter->w.skip);
2308 iter->w.skip = iter->w.count;
2309 }
2310}
2311
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002312static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2313{
2314 int r;
2315 struct rt6_info *n;
2316 struct net *net = seq_file_net(seq);
2317 struct ipv6_route_iter *iter = seq->private;
2318
2319 if (!v)
2320 goto iter_table;
2321
David Miller071fb372017-11-28 15:40:15 -05002322 n = rcu_dereference_bh(((struct rt6_info *)v)->rt6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002323 if (n) {
2324 ++*pos;
2325 return n;
2326 }
2327
2328iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002329 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002330 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002331 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002332 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002333 if (r > 0) {
2334 if (v)
2335 ++*pos;
2336 return iter->w.leaf;
2337 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002338 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002339 return NULL;
2340 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002341 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002342
2343 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2344 if (!iter->tbl)
2345 return NULL;
2346
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002347 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002348 goto iter_table;
2349}
2350
2351static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2352 __acquires(RCU_BH)
2353{
2354 struct net *net = seq_file_net(seq);
2355 struct ipv6_route_iter *iter = seq->private;
2356
2357 rcu_read_lock_bh();
2358 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2359 iter->skip = *pos;
2360
2361 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002362 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002363 return ipv6_route_seq_next(seq, NULL, pos);
2364 } else {
2365 return NULL;
2366 }
2367}
2368
2369static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2370{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002371 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002372 return w->node && !(w->state == FWS_U && w->node == w->root);
2373}
2374
2375static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2376 __releases(RCU_BH)
2377{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002378 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002379 struct ipv6_route_iter *iter = seq->private;
2380
2381 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002382 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002383
2384 rcu_read_unlock_bh();
2385}
2386
2387static const struct seq_operations ipv6_route_seq_ops = {
2388 .start = ipv6_route_seq_start,
2389 .next = ipv6_route_seq_next,
2390 .stop = ipv6_route_seq_stop,
2391 .show = ipv6_route_seq_show
2392};
2393
2394int ipv6_route_open(struct inode *inode, struct file *file)
2395{
2396 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2397 sizeof(struct ipv6_route_iter));
2398}
2399
2400#endif /* CONFIG_PROC_FS */