blob: f25f4d9831e89de6bcd8a8669608bbef4f86dbab [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
David Ahernd4ead6b2018-04-17 17:33:16 -0700581void fib6_metric_set(struct rt6_info *f6i, int metric, u32 val)
582{
583 if (!f6i)
584 return;
585
586 if (f6i->fib6_metrics == &dst_default_metrics) {
587 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
588
589 if (!p)
590 return;
591
592 refcount_set(&p->refcnt, 1);
593 f6i->fib6_metrics = p;
594 }
595
596 f6i->fib6_metrics->metrics[metric - 1] = val;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * Routing Table
601 *
602 * return the appropriate node for a routing tree "add" operation
603 * by either creating and inserting or by returning an existing
604 * node.
605 */
606
Wei Wang81eb8442017-10-06 12:06:11 -0700607static struct fib6_node *fib6_add_1(struct net *net,
608 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700609 struct fib6_node *root,
610 struct in6_addr *addr, int plen,
611 int offset, int allow_create,
612 int replace_required,
613 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct fib6_node *fn, *in, *ln;
616 struct fib6_node *pn = NULL;
617 struct rt6key *key;
618 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900619 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 RT6_TRACE("fib6_add_1\n");
622
623 /* insert node in tree */
624
625 fn = root;
626
627 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700628 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
629 lockdep_is_held(&table->tb6_lock));
630 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /*
633 * Prefix match
634 */
635 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000636 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000637 if (!allow_create) {
638 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600639 NL_SET_ERR_MSG(extack,
640 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000641 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000642 return ERR_PTR(-ENOENT);
643 }
Joe Perchesf3213832012-05-15 14:11:53 +0000644 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000647 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * Exact match ?
651 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (plen == fn->fn_bit) {
654 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500655 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700656 RCU_INIT_POINTER(fn->leaf, NULL);
657 rt6_release(leaf);
Wei Wang4512c432018-01-08 10:34:00 -0800658 /* remove null_entry in the root node */
659 } else if (fn->fn_flags & RTN_TL_ROOT &&
660 rcu_access_pointer(fn->leaf) ==
661 net->ipv6.ip6_null_entry) {
662 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return fn;
666 }
667
668 /*
669 * We have more bits to go
670 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 dir = addr_bit_set(addr, fn->fn_bit);
674 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700675 fn = dir ?
676 rcu_dereference_protected(fn->right,
677 lockdep_is_held(&table->tb6_lock)) :
678 rcu_dereference_protected(fn->left,
679 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 } while (fn);
681
Matti Vaittinen14df0152011-11-16 21:18:02 +0000682 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000683 /* We should not create new node because
684 * NLM_F_REPLACE was specified without NLM_F_CREATE
685 * I assume it is safe to require NLM_F_CREATE when
686 * REPLACE flag is used! Later we may want to remove the
687 * check for replace_required, because according
688 * to netlink specification, NLM_F_CREATE
689 * MUST be specified if new route is created.
690 * That would keep IPv6 consistent with IPv4
691 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000692 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600693 NL_SET_ERR_MSG(extack,
694 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000695 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000696 return ERR_PTR(-ENOENT);
697 }
Joe Perchesf3213832012-05-15 14:11:53 +0000698 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * We walked to the bottom of tree.
702 * Create new leaf node without children.
703 */
704
Wei Wang81eb8442017-10-06 12:06:11 -0700705 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller507c9b12011-12-03 17:50:45 -0500707 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000708 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700710 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700713 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700715 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return ln;
718
719
720insert_above:
721 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900722 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * we have a less significant route.
724 * we've to insert an intermediate node on the list
725 * this new node will point to the one we need to create
726 * and the current
727 */
728
Wei Wang66f5d6c2017-10-06 12:06:10 -0700729 pn = rcu_dereference_protected(fn->parent,
730 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* find 1st bit in difference between the 2 addrs.
733
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800734 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 but if it is >= plen, the value is ignored in any case.
736 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900737
fan.du9225b232013-07-22 14:21:09 +0800738 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900740 /*
741 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * / \
743 * (new leaf node)[ln] (old node)[fn]
744 */
745 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700746 in = node_alloc(net);
747 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900748
David S. Miller507c9b12011-12-03 17:50:45 -0500749 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700751 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700753 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000754 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900757 /*
758 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * RTN_RTINFO will
760 * be off since that an address that chooses one of
761 * the branches would not match less specific routes
762 * in the other branch
763 */
764
765 in->fn_bit = bit;
766
Wei Wang66f5d6c2017-10-06 12:06:10 -0700767 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700769 atomic_inc(&rcu_dereference_protected(in->leaf,
770 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* update parent pointer */
773 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700774 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700776 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 ln->fn_bit = plen;
779
Wei Wang66f5d6c2017-10-06 12:06:10 -0700780 RCU_INIT_POINTER(ln->parent, in);
781 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700784 rcu_assign_pointer(in->right, ln);
785 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700787 rcu_assign_pointer(in->left, ln);
788 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 } else { /* plen <= bit */
791
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900792 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * (new leaf node)[ln]
794 * / \
795 * (old node)[fn] NULL
796 */
797
Wei Wang81eb8442017-10-06 12:06:11 -0700798 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
David S. Miller507c9b12011-12-03 17:50:45 -0500800 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000801 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 ln->fn_bit = plen;
804
Wei Wang66f5d6c2017-10-06 12:06:10 -0700805 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700808 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700810 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Wei Wang66f5d6c2017-10-06 12:06:10 -0700812 rcu_assign_pointer(fn->parent, ln);
813
814 if (dir)
815 rcu_assign_pointer(pn->right, ln);
816 else
817 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 return ln;
820}
821
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100822static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
823 struct net *net)
824{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700825 struct fib6_table *table = rt->rt6i_table;
826
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100827 if (atomic_read(&rt->rt6i_ref) != 1) {
828 /* This route is used as dummy address holder in some split
829 * nodes. It is not leaked, but it still holds other resources,
830 * which must be released in time. So, scan ascendant nodes
831 * and replace dummy references to this route with references
832 * to still alive ones.
833 */
834 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700835 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
836 lockdep_is_held(&table->tb6_lock));
837 struct rt6_info *new_leaf;
838 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
839 new_leaf = fib6_find_prefix(net, table, fn);
840 atomic_inc(&new_leaf->rt6i_ref);
841 rcu_assign_pointer(fn->leaf, new_leaf);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100842 rt6_release(rt);
843 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700844 fn = rcu_dereference_protected(fn->parent,
845 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100846 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100847 }
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
851 * Insert routing information in a node.
852 */
853
854static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -0700855 struct nl_info *info,
David Ahern6c31e5a2017-10-27 17:37:13 -0700856 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700858 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
859 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct rt6_info *iter = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700861 struct rt6_info __rcu **ins;
862 struct rt6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500863 int replace = (info->nlh &&
864 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
865 int add = (!info->nlh ||
866 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000867 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200868 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200869 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100870 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Ahern1f5e29c2017-01-31 16:51:37 -0800872 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
873 nlflags |= NLM_F_APPEND;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ins = &fn->leaf;
876
Wei Wang66f5d6c2017-10-06 12:06:10 -0700877 for (iter = leaf; iter;
David Miller071fb372017-11-28 15:40:15 -0500878 iter = rcu_dereference_protected(iter->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700879 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /*
881 * Search for duplicates
882 */
883
884 if (iter->rt6i_metric == rt->rt6i_metric) {
885 /*
886 * Same priority level
887 */
David S. Miller507c9b12011-12-03 17:50:45 -0500888 if (info->nlh &&
889 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000890 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200891
892 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000893 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200894 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
895 found++;
896 break;
897 }
898 if (rt_can_ecmp)
899 fallback_ins = fallback_ins ?: ins;
900 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
David Ahernf06b7542017-07-05 14:41:46 -0600903 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000904 if (rt->rt6i_nsiblings)
905 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500906 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000908 if (!(rt->rt6i_flags & RTF_EXPIRES))
David Ahern14895682018-04-17 17:33:17 -0700909 fib6_clean_expires(iter);
Gao feng1716a962012-04-06 00:13:10 +0000910 else
David Ahern14895682018-04-17 17:33:17 -0700911 fib6_set_expires(iter, rt->expires);
David Ahernd4ead6b2018-04-17 17:33:16 -0700912 fib6_metric_set(iter, RTAX_MTU, rt->fib6_pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EEXIST;
914 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000915 /* If we have the same destination and the same metric,
916 * but not the same gateway, then the route we try to
917 * add is sibling to this route, increment our counter
918 * of siblings, and later we will add our route to the
919 * list.
920 * Only static routes (which don't have flag
921 * RTF_EXPIRES) are used for ECMPv6.
922 *
923 * To avoid long list, we only had siblings if the
924 * route have a gateway.
925 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200926 if (rt_can_ecmp &&
927 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000928 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
931 if (iter->rt6i_metric > rt->rt6i_metric)
932 break;
933
Michal Kubeček27596472015-05-18 20:54:00 +0200934next_iter:
David Miller071fb372017-11-28 15:40:15 -0500935 ins = &iter->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Michal Kubeček27596472015-05-18 20:54:00 +0200938 if (fallback_ins && !found) {
939 /* No ECMP-able route found, replace first non-ECMP one */
940 ins = fallback_ins;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700941 iter = rcu_dereference_protected(*ins,
942 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +0200943 found++;
944 }
945
David S. Millerf11e6652007-03-24 20:36:25 -0700946 /* Reset round-robin state, if necessary */
947 if (ins == &fn->leaf)
948 fn->rr_ptr = NULL;
949
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000950 /* Link this route to others same route. */
951 if (rt->rt6i_nsiblings) {
952 unsigned int rt6i_nsiblings;
953 struct rt6_info *sibling, *temp_sibling;
954
955 /* Find the first route that have the same metric */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700956 sibling = leaf;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000957 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200958 if (sibling->rt6i_metric == rt->rt6i_metric &&
959 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000960 list_add_tail(&rt->rt6i_siblings,
961 &sibling->rt6i_siblings);
962 break;
963 }
David Miller071fb372017-11-28 15:40:15 -0500964 sibling = rcu_dereference_protected(sibling->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700965 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000966 }
967 /* For each sibling in the list, increment the counter of
968 * siblings. BUG() if counters does not match, list of siblings
969 * is broken!
970 */
971 rt6i_nsiblings = 0;
972 list_for_each_entry_safe(sibling, temp_sibling,
973 &rt->rt6i_siblings, rt6i_siblings) {
974 sibling->rt6i_nsiblings++;
975 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
976 rt6i_nsiblings++;
977 }
978 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +0200979 rt6_multipath_rebalance(temp_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000980 }
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * insert node
984 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000985 if (!replace) {
986 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000987 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000989add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200990 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100991
David Ahern22330002018-03-27 18:21:59 -0700992 err = call_fib6_entry_notifiers(info->nl_net,
993 FIB_EVENT_ENTRY_ADD,
994 rt, extack);
995 if (err)
996 return err;
997
David Miller071fb372017-11-28 15:40:15 -0500998 rcu_assign_pointer(rt->rt6_next, iter);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000999 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001000 rcu_assign_pointer(rt->rt6i_node, fn);
1001 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001002 if (!info->skip_notify)
1003 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001004 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
David S. Miller507c9b12011-12-03 17:50:45 -05001006 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001007 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1008 fn->fn_flags |= RTN_RTINFO;
1009 }
1010
1011 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001012 int nsiblings;
1013
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001014 if (!found) {
1015 if (add)
1016 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001017 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001018 return -ENOENT;
1019 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001020
David Ahern22330002018-03-27 18:21:59 -07001021 err = call_fib6_entry_notifiers(info->nl_net,
1022 FIB_EVENT_ENTRY_REPLACE,
1023 rt, extack);
1024 if (err)
1025 return err;
1026
Wei Wang66f5d6c2017-10-06 12:06:10 -07001027 atomic_inc(&rt->rt6i_ref);
Wei Wang4e587ea2017-08-25 15:03:10 -07001028 rcu_assign_pointer(rt->rt6i_node, fn);
David Miller071fb372017-11-28 15:40:15 -05001029 rt->rt6_next = iter->rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001030 rcu_assign_pointer(*ins, rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001031 if (!info->skip_notify)
1032 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001033 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001034 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1035 fn->fn_flags |= RTN_RTINFO;
1036 }
Michal Kubeček27596472015-05-18 20:54:00 +02001037 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001038 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001039 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001040 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001041 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001042 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001043
1044 if (nsiblings) {
1045 /* Replacing an ECMP route, remove all siblings */
David Miller071fb372017-11-28 15:40:15 -05001046 ins = &rt->rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001047 iter = rcu_dereference_protected(*ins,
1048 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001049 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001050 if (iter->rt6i_metric > rt->rt6i_metric)
1051 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001052 if (rt6_qualify_for_ecmp(iter)) {
David Miller071fb372017-11-28 15:40:15 -05001053 *ins = iter->rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001054 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001055 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001056 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001057 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001058 rt6_release(iter);
1059 nsiblings--;
Wei Wang81eb8442017-10-06 12:06:11 -07001060 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001061 } else {
David Miller071fb372017-11-28 15:40:15 -05001062 ins = &iter->rt6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001063 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001064 iter = rcu_dereference_protected(*ins,
1065 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001066 }
1067 WARN_ON(nsiblings != 0);
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
1071 return 0;
1072}
1073
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001074static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001076 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001077 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001078 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001079 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001082void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001084 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1085 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001086 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001089static void __fib6_update_sernum_upto_root(struct rt6_info *rt,
1090 int sernum)
Wei Wangbbd63f02017-10-06 12:06:07 -07001091{
1092 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1093 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1094
1095 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1096 smp_wmb();
1097 while (fn) {
1098 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001099 fn = rcu_dereference_protected(fn->parent,
1100 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001101 }
1102}
1103
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001104void fib6_update_sernum_upto_root(struct net *net, struct rt6_info *rt)
1105{
1106 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/*
1110 * Add routing information to the routing tree.
1111 * <destination addr>/<source addr>
1112 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001113 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
1115
Florian Westphale715b6d2015-01-05 23:57:44 +01001116int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07001117 struct nl_info *info, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001119 struct fib6_table *table = rt->rt6i_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001120 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001122 int allow_create = 1;
1123 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001124 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001125
Wei Wanga4c2fd72017-06-17 10:42:42 -07001126 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001127 return -EINVAL;
Wei Wang2b760fc2017-10-06 12:06:03 -07001128 if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
1129 return -EINVAL;
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001130
David S. Miller507c9b12011-12-03 17:50:45 -05001131 if (info->nlh) {
1132 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001133 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001134 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001135 replace_required = 1;
1136 }
1137 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001138 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Wei Wang81eb8442017-10-06 12:06:11 -07001140 fn = fib6_add_1(info->nl_net, table, root,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001141 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
fan.du9225b232013-07-22 14:21:09 +08001142 offsetof(struct rt6_info, rt6i_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001143 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001144 if (IS_ERR(fn)) {
1145 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001146 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001150 pn = fn;
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152#ifdef CONFIG_IPV6_SUBTREES
1153 if (rt->rt6i_src.plen) {
1154 struct fib6_node *sn;
1155
Wei Wang66f5d6c2017-10-06 12:06:10 -07001156 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct fib6_node *sfn;
1158
1159 /*
1160 * Create subtree.
1161 *
1162 * fn[main tree]
1163 * |
1164 * sfn[subtree root]
1165 * \
1166 * sn[new leaf node]
1167 */
1168
1169 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001170 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001171 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001172 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001174 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001175 rcu_assign_pointer(sfn->leaf,
1176 info->nl_net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* Now add the first leaf node to new subtree */
1180
Wei Wang81eb8442017-10-06 12:06:11 -07001181 sn = fib6_add_1(info->nl_net, table, sfn,
1182 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001183 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001184 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001186 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001188 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 in main tree.
1190 */
Wei Wang81eb8442017-10-06 12:06:11 -07001191 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001192 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001193 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001197 rcu_assign_pointer(sfn->parent, fn);
1198 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001200 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1201 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001202 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001203 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001205 if (IS_ERR(sn)) {
1206 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001207 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
Wei Wang66f5d6c2017-10-06 12:06:10 -07001211 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang591ff9e2018-01-18 10:40:03 -08001212 if (fn->fn_flags & RTN_TL_ROOT) {
1213 /* put back null_entry for root node */
1214 rcu_assign_pointer(fn->leaf,
1215 info->nl_net->ipv6.ip6_null_entry);
1216 } else {
1217 atomic_inc(&rt->rt6i_ref);
1218 rcu_assign_pointer(fn->leaf, rt);
1219 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 fn = sn;
1222 }
1223#endif
1224
David Ahernd4ead6b2018-04-17 17:33:16 -07001225 err = fib6_add_rt2node(fn, rt, info, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001226 if (!err) {
Ido Schimmel4a8e56e2018-01-07 12:45:13 +02001227 __fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001228 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001232 if (err) {
1233#ifdef CONFIG_IPV6_SUBTREES
1234 /*
1235 * If fib6_add_1 has cleared the old leaf pointer in the
1236 * super-tree leaf node we have to find a new one for it.
1237 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001238 if (pn != fn) {
1239 struct rt6_info *pn_leaf =
1240 rcu_dereference_protected(pn->leaf,
1241 lockdep_is_held(&table->tb6_lock));
1242 if (pn_leaf == rt) {
1243 pn_leaf = NULL;
1244 RCU_INIT_POINTER(pn->leaf, NULL);
1245 atomic_dec(&rt->rt6i_ref);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001246 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001247 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1248 pn_leaf = fib6_find_prefix(info->nl_net, table,
1249 pn);
1250#if RT6_DEBUG >= 2
1251 if (!pn_leaf) {
1252 WARN_ON(!pn_leaf);
1253 pn_leaf =
1254 info->nl_net->ipv6.ip6_null_entry;
1255 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001256#endif
Wei Wang7bbfe002018-01-03 14:11:59 -08001257 atomic_inc(&pn_leaf->rt6i_ref);
1258 rcu_assign_pointer(pn->leaf, pn_leaf);
1259 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001260 }
1261#endif
Wei Wang348a4002017-08-18 17:14:49 -07001262 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return err;
1265
Wei Wang348a4002017-08-18 17:14:49 -07001266failure:
Wei Wang4512c432018-01-08 10:34:00 -08001267 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1268 * 1. fn is an intermediate node and we failed to add the new
1269 * route to it in both subtree creation failure and fib6_add_rt2node()
1270 * failure case.
1271 * 2. fn is the root node in the table and we fail to add the first
1272 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Wei Wang4512c432018-01-08 10:34:00 -08001274 if (fn &&
1275 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1276 (fn->fn_flags & RTN_TL_ROOT &&
1277 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001278 fib6_repair_tree(info->nl_net, table, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001279 /* Always release dst as dst->__refcnt is guaranteed
1280 * to be taken before entering this function
1281 */
Wei Wang587fea72017-06-17 10:42:36 -07001282 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286/*
1287 * Routing tree lookup
1288 *
1289 */
1290
1291struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001292 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001293 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294};
1295
Wang Yufen437de072014-03-28 12:07:04 +08001296static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1297 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001300 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001302 if (unlikely(args->offset == 0))
1303 return NULL;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /*
1306 * Descend on a tree
1307 */
1308
1309 fn = root;
1310
1311 for (;;) {
1312 struct fib6_node *next;
1313
1314 dir = addr_bit_set(args->addr, fn->fn_bit);
1315
Wei Wang66f5d6c2017-10-06 12:06:10 -07001316 next = dir ? rcu_dereference(fn->right) :
1317 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (next) {
1320 fn = next;
1321 continue;
1322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
1324 }
1325
David S. Miller507c9b12011-12-03 17:50:45 -05001326 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001327 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1328
1329 if (subtree || fn->fn_flags & RTN_RTINFO) {
1330 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 struct rt6key *key;
1332
Wei Wang8d1040e2017-10-06 12:06:08 -07001333 if (!leaf)
1334 goto backtrack;
1335
1336 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001338 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1339#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001340 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001341 struct fib6_node *sfn;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001342 sfn = fib6_lookup_1(subtree, args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001343 if (!sfn)
1344 goto backtrack;
1345 fn = sfn;
1346 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001347#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001348 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001349 return fn;
1350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001352backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001353 if (fn->fn_flags & RTN_ROOT)
1354 break;
1355
Wei Wang66f5d6c2017-10-06 12:06:10 -07001356 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358
1359 return NULL;
1360}
1361
Wei Wang66f5d6c2017-10-06 12:06:10 -07001362/* called with rcu_read_lock() held
1363 */
Wang Yufen437de072014-03-28 12:07:04 +08001364struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1365 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001368 struct lookup_args args[] = {
1369 {
1370 .offset = offsetof(struct rt6_info, rt6i_dst),
1371 .addr = daddr,
1372 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001374 {
1375 .offset = offsetof(struct rt6_info, rt6i_src),
1376 .addr = saddr,
1377 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001379 {
1380 .offset = 0, /* sentinel */
1381 }
1382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001384 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001385 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 fn = root;
1387
1388 return fn;
1389}
1390
1391/*
1392 * Get node with specified destination prefix (and source prefix,
1393 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001394 * exact_match == true means we try to find fn with exact match of
1395 * the passed in prefix addr
1396 * exact_match == false means we try to find fn with longest prefix
1397 * match of the passed in prefix addr. This is useful for finding fn
1398 * for cached route as it will be stored in the exception table under
1399 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401
1402
Wang Yufen437de072014-03-28 12:07:04 +08001403static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1404 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001405 int plen, int offset,
1406 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Wei Wang38fbeee2017-10-06 12:06:02 -07001408 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 for (fn = root; fn ; ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001411 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001412 struct rt6key *key;
1413
1414 /* This node is being deleted */
1415 if (!leaf) {
1416 if (plen <= fn->fn_bit)
1417 goto out;
1418 else
1419 goto next;
1420 }
1421
1422 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /*
1425 * Prefix match
1426 */
1427 if (plen < fn->fn_bit ||
1428 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (plen == fn->fn_bit)
1432 return fn;
1433
Wei Wang38fbeee2017-10-06 12:06:02 -07001434 prev = fn;
1435
Wei Wang8d1040e2017-10-06 12:06:08 -07001436next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /*
1438 * We have more bits to go
1439 */
1440 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001441 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001443 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001445out:
1446 if (exact_match)
1447 return NULL;
1448 else
1449 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Wang Yufen437de072014-03-28 12:07:04 +08001452struct fib6_node *fib6_locate(struct fib6_node *root,
1453 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001454 const struct in6_addr *saddr, int src_len,
1455 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct fib6_node *fn;
1458
1459 fn = fib6_locate_1(root, daddr, dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001460 offsetof(struct rt6_info, rt6i_dst),
1461 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463#ifdef CONFIG_IPV6_SUBTREES
1464 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001465 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001466 if (fn) {
1467 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1468
1469 if (subtree) {
1470 fn = fib6_locate_1(subtree, saddr, src_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001471 offsetof(struct rt6_info, rt6i_src),
1472 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001473 }
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476#endif
1477
David S. Miller507c9b12011-12-03 17:50:45 -05001478 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return fn;
1480
1481 return NULL;
1482}
1483
1484
1485/*
1486 * Deletion
1487 *
1488 */
1489
Wei Wang66f5d6c2017-10-06 12:06:10 -07001490static struct rt6_info *fib6_find_prefix(struct net *net,
1491 struct fib6_table *table,
1492 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001494 struct fib6_node *child_left, *child_right;
1495
David S. Miller507c9b12011-12-03 17:50:45 -05001496 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001497 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
David S. Miller507c9b12011-12-03 17:50:45 -05001499 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001500 child_left = rcu_dereference_protected(fn->left,
1501 lockdep_is_held(&table->tb6_lock));
1502 child_right = rcu_dereference_protected(fn->right,
1503 lockdep_is_held(&table->tb6_lock));
1504 if (child_left)
1505 return rcu_dereference_protected(child_left->leaf,
1506 lockdep_is_held(&table->tb6_lock));
1507 if (child_right)
1508 return rcu_dereference_protected(child_right->leaf,
1509 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001511 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 return NULL;
1514}
1515
1516/*
1517 * Called to trim the tree of intermediate nodes when possible. "fn"
1518 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001519 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
1521
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001522static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001523 struct fib6_table *table,
1524 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 int children;
1527 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001528 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001529 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 int iter = 0;
1531
Wei Wang4512c432018-01-08 10:34:00 -08001532 /* Set fn->leaf to null_entry for root node. */
1533 if (fn->fn_flags & RTN_TL_ROOT) {
1534 rcu_assign_pointer(fn->leaf, net->ipv6.ip6_null_entry);
1535 return fn;
1536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001539 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1540 lockdep_is_held(&table->tb6_lock));
1541 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1542 lockdep_is_held(&table->tb6_lock));
1543 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1544 lockdep_is_held(&table->tb6_lock));
1545 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1546 lockdep_is_held(&table->tb6_lock));
1547 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1548 lockdep_is_held(&table->tb6_lock));
1549 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1550 lockdep_is_held(&table->tb6_lock));
1551 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1552 lockdep_is_held(&table->tb6_lock));
1553 struct rt6_info *new_fn_leaf;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1556 iter++;
1557
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001558 WARN_ON(fn->fn_flags & RTN_RTINFO);
1559 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001560 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 children = 0;
1563 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001564 if (fn_r)
1565 child = fn_r, children |= 1;
1566 if (fn_l)
1567 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001569 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570#ifdef CONFIG_IPV6_SUBTREES
1571 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001572 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573#endif
1574 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001575 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001577 if (!new_fn_leaf) {
1578 WARN_ON(!new_fn_leaf);
1579 new_fn_leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001582 atomic_inc(&new_fn_leaf->rt6i_ref);
1583 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1584 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001588 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001589 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001590 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 nstate = FWS_L;
1592 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001593 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001595 if (pn_r == fn)
1596 rcu_assign_pointer(pn->right, child);
1597 else if (pn_l == fn)
1598 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001600 else
1601 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602#endif
1603 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001604 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 nstate = FWS_R;
1606#ifdef CONFIG_IPV6_SUBTREES
1607 }
1608#endif
1609
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001610 read_lock(&net->ipv6.fib6_walker_lock);
1611 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001612 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001613 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1615 w->node = pn;
1616 w->state = nstate;
1617 }
1618 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (w->node == fn) {
1620 w->node = child;
1621 if (children&2) {
1622 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001623 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 } else {
1625 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001626 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628 }
1629 }
1630 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001631 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Wei Wang81eb8442017-10-06 12:06:11 -07001633 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001634 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return pn;
1636
Wei Wang66f5d6c2017-10-06 12:06:10 -07001637 RCU_INIT_POINTER(pn->leaf, NULL);
1638 rt6_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 fn = pn;
1640 }
1641}
1642
Wei Wang66f5d6c2017-10-06 12:06:10 -07001643static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1644 struct rt6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001646 struct fib6_walker *w;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001647 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1648 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001649 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 RT6_TRACE("fib6_del_route\n");
1652
Wei Wang2b760fc2017-10-06 12:06:03 -07001653 WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /* Unlink it */
David Miller071fb372017-11-28 15:40:15 -05001656 *rtp = rt->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001658 net->ipv6.rt6_stats->fib_rt_entries--;
1659 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Wei Wang2b760fc2017-10-06 12:06:03 -07001661 /* Flush all cached dst in exception table */
1662 rt6_flush_exceptions(rt);
1663
David S. Millerf11e6652007-03-24 20:36:25 -07001664 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001665 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001666 fn->rr_ptr = NULL;
1667
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001668 /* Remove this entry from other siblings */
1669 if (rt->rt6i_nsiblings) {
1670 struct rt6_info *sibling, *next_sibling;
1671
1672 list_for_each_entry_safe(sibling, next_sibling,
1673 &rt->rt6i_siblings, rt6i_siblings)
1674 sibling->rt6i_nsiblings--;
1675 rt->rt6i_nsiblings = 0;
1676 list_del_init(&rt->rt6i_siblings);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02001677 rt6_multipath_rebalance(next_sibling);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001678 }
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001681 read_lock(&net->ipv6.fib6_walker_lock);
1682 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (w->state == FWS_C && w->leaf == rt) {
1684 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Miller071fb372017-11-28 15:40:15 -05001685 w->leaf = rcu_dereference_protected(rt->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001686 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001687 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 w->state = FWS_U;
1689 }
1690 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001691 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Wei Wang4512c432018-01-08 10:34:00 -08001693 /* If it was last route, call fib6_repair_tree() to:
1694 * 1. For root node, put back null_entry as how the table was created.
1695 * 2. For other nodes, expunge its radix tree node.
1696 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001697 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001698 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1699 fn->fn_flags &= ~RTN_RTINFO;
1700 net->ipv6.rt6_stats->fib_route_nodes--;
1701 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001702 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001705 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
David Ahern6c31e5a2017-10-27 17:37:13 -07001707 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001708 if (!info->skip_notify)
1709 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 rt6_release(rt);
1711}
1712
Wei Wang66f5d6c2017-10-06 12:06:10 -07001713/* Need to own table->tb6_lock */
Thomas Graf86872cb2006-08-22 00:01:08 -07001714int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Wei Wang4e587ea2017-08-25 15:03:10 -07001716 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1717 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001718 struct fib6_table *table = rt->rt6i_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001719 struct net *net = info->nl_net;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001720 struct rt6_info __rcu **rtp;
1721 struct rt6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001724 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001725 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return -ENOENT;
1727 }
1728#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001729 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return -ENOENT;
1731
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001732 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Wei Wang2b760fc2017-10-06 12:06:03 -07001734 /* remove cached dst from exception table */
1735 if (rt->rt6i_flags & RTF_CACHE)
1736 return rt6_remove_exception_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /*
1739 * Walk the leaf entries looking for ourself
1740 */
1741
Wei Wang66f5d6c2017-10-06 12:06:10 -07001742 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1743 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1744 lockdep_is_held(&table->tb6_lock));
1745 if (rt == cur) {
1746 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return 0;
1748 }
David Miller071fb372017-11-28 15:40:15 -05001749 rtp_next = &cur->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751 return -ENOENT;
1752}
1753
1754/*
1755 * Tree traversal function.
1756 *
1757 * Certainly, it is not interrupt safe.
1758 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1759 * It means, that we can modify tree during walking
1760 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001761 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *
1763 * It guarantees that every node will be traversed,
1764 * and that it will be traversed only once.
1765 *
1766 * Callback function w->func may return:
1767 * 0 -> continue walking.
1768 * positive value -> walking is suspended (used by tree dumps,
1769 * and probably by gc, if it will be split to several slices)
1770 * negative value -> terminate walking.
1771 *
1772 * The function itself returns:
1773 * 0 -> walk is complete.
1774 * >0 -> walk is incomplete (i.e. suspended)
1775 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001776 *
1777 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
1779
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001780static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001782 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Wei Wang2b760fc2017-10-06 12:06:03 -07001784 /* w->root should always be table->tb6_root */
1785 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 for (;;) {
1788 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001789 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 switch (w->state) {
1793#ifdef CONFIG_IPV6_SUBTREES
1794 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001795 if (FIB6_SUBTREE(fn)) {
1796 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 continue;
1798 }
1799 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001800#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001801 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001803 left = rcu_dereference_protected(fn->left, 1);
1804 if (left) {
1805 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 w->state = FWS_INIT;
1807 continue;
1808 }
1809 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001810 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001812 right = rcu_dereference_protected(fn->right, 1);
1813 if (right) {
1814 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 w->state = FWS_INIT;
1816 continue;
1817 }
1818 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001819 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001820 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001822 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001823 int err;
1824
Eric Dumazetfa809e22012-06-25 15:37:19 -07001825 if (w->skip) {
1826 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001827 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001828 }
1829
1830 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (err)
1832 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001833
1834 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 continue;
1836 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001837skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001839 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 case FWS_U:
1841 if (fn == w->root)
1842 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001843 pn = rcu_dereference_protected(fn->parent, 1);
1844 left = rcu_dereference_protected(pn->left, 1);
1845 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 w->node = pn;
1847#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001848 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001849 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 w->state = FWS_L;
1851 continue;
1852 }
1853#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001854 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 w->state = FWS_R;
1856 continue;
1857 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001858 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001860 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 continue;
1862 }
1863#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001864 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865#endif
1866 }
1867 }
1868}
1869
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001870static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 int res;
1873
1874 w->state = FWS_INIT;
1875 w->node = w->root;
1876
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001877 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 res = fib6_walk_continue(w);
1879 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001880 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return res;
1882}
1883
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001884static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 int res;
1887 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001888 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001889 struct nl_info info = {
1890 .nl_net = c->net,
1891 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001893 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1894 w->node->fn_sernum != c->sernum)
1895 w->node->fn_sernum = c->sernum;
1896
1897 if (!c->func) {
1898 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1899 w->leaf = NULL;
1900 return 0;
1901 }
1902
Wei Wang66f5d6c2017-10-06 12:06:10 -07001903 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001905 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001907 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (res) {
1909#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001910 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001911 __func__, rt,
1912 rcu_access_pointer(rt->rt6i_node),
1913 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914#endif
1915 continue;
1916 }
1917 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001918 } else if (res == -2) {
1919 if (WARN_ON(!rt->rt6i_nsiblings))
1920 continue;
1921 rt = list_last_entry(&rt->rt6i_siblings,
1922 struct rt6_info, rt6i_siblings);
1923 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001925 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927 w->leaf = rt;
1928 return 0;
1929}
1930
1931/*
1932 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001935 * It may return -2 -> skip multipath route.
1936 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 */
1939
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001940static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001941 int (*func)(struct rt6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001942 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001944 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 c.w.root = root;
1947 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001948 c.w.count = 0;
1949 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001951 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001953 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001955 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001958static void __fib6_clean_all(struct net *net,
1959 int (*func)(struct rt6_info *, void *),
1960 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001961{
Thomas Grafc71099a2006-08-04 23:20:06 -07001962 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001963 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001964 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001965
Patrick McHardy1b43af52006-08-10 23:11:17 -07001966 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001967 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001968 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001969 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001970 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001971 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07001972 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001973 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001974 }
1975 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001976 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001977}
1978
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001979void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1980 void *arg)
1981{
1982 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1983}
1984
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001985static void fib6_flush_trees(struct net *net)
1986{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001987 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001988
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001989 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/*
1993 * Garbage collection
1994 */
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996static int fib6_age(struct rt6_info *rt, void *arg)
1997{
Michal Kubeček3570df92016-03-08 14:44:25 +01001998 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 unsigned long now = jiffies;
2000
2001 /*
2002 * check addrconf expiration here.
2003 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
2005
David Ahern14895682018-04-17 17:33:17 -07002006 if (rt->rt6i_flags & RTF_EXPIRES && rt->expires) {
2007 if (time_after(now, rt->expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return -1;
2010 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002011 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Wei Wangc757faa2017-10-06 12:06:01 -07002014 /* Also age clones in the exception table.
2015 * Note, that clones are aged out
2016 * only if they are not in use now.
2017 */
2018 rt6_age_exceptions(rt, gc_args, now);
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return 0;
2021}
2022
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002023void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Michal Kubeček3570df92016-03-08 14:44:25 +01002025 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002026 unsigned long now;
2027
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002028 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002029 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2030 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002031 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002034 gc_args.timeout = expires ? (int)expires :
2035 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002036 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002037
Michal Kubeček3570df92016-03-08 14:44:25 +01002038 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002039 now = jiffies;
2040 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002043 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002044 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002045 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002046 else
2047 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002048 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Kees Cook86cb30e2017-10-17 20:21:24 -07002051static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002052{
Kees Cook86cb30e2017-10-17 20:21:24 -07002053 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2054
2055 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002056}
2057
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002058static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002059{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002060 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002061 int err;
2062
2063 err = fib6_notifier_init(net);
2064 if (err)
2065 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002066
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002067 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002068 rwlock_init(&net->ipv6.fib6_walker_lock);
2069 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002070 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002071
Benjamin Theryc5728722008-03-03 23:34:17 -08002072 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2073 if (!net->ipv6.rt6_stats)
2074 goto out_timer;
2075
Eric Dumazet10da66f2010-10-13 08:22:03 +00002076 /* Avoid false sharing : Use at least a full cache line */
2077 size = max_t(size_t, size, L1_CACHE_BYTES);
2078
2079 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002080 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002081 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002082
2083 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2084 GFP_KERNEL);
2085 if (!net->ipv6.fib6_main_tbl)
2086 goto out_fib_table_hash;
2087
2088 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002089 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2090 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002091 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2092 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002093 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002094
2095#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2096 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2097 GFP_KERNEL);
2098 if (!net->ipv6.fib6_local_tbl)
2099 goto out_fib6_main_tbl;
2100 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002101 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2102 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002103 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2104 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002105 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002106#endif
2107 fib6_tables_init(net);
2108
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002109 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002110
2111#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2112out_fib6_main_tbl:
2113 kfree(net->ipv6.fib6_main_tbl);
2114#endif
2115out_fib_table_hash:
2116 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002117out_rt6_stats:
2118 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002119out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002120 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002121 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002122}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002123
2124static void fib6_net_exit(struct net *net)
2125{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002126 unsigned int i;
2127
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002128 del_timer_sync(&net->ipv6.ip6_fib_timer);
2129
Eric Dumazet32a805b2017-09-08 15:48:47 -07002130 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002131 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2132 struct hlist_node *tmp;
2133 struct fib6_table *tb;
2134
2135 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2136 hlist_del(&tb->tb6_hlist);
2137 fib6_free_table(tb);
2138 }
2139 }
2140
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002141 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002142 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002143 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002144}
2145
2146static struct pernet_operations fib6_net_ops = {
2147 .init = fib6_net_init,
2148 .exit = fib6_net_exit,
2149};
2150
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002151int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002153 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2156 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002157 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002158 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002159 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002160 goto out;
2161
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002162 ret = register_pernet_subsys(&fib6_net_ops);
2163 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002164 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002165
Florian Westphal16feebc2017-12-02 21:44:08 +01002166 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2167 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002168 if (ret)
2169 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002170
2171 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002172out:
2173 return ret;
2174
David S. Millere8803b62012-06-16 01:12:19 -07002175out_unregister_subsys:
2176 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002177out_kmem_cache_create:
2178 kmem_cache_destroy(fib6_node_kmem);
2179 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182void fib6_gc_cleanup(void)
2183{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002184 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 kmem_cache_destroy(fib6_node_kmem);
2186}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002187
2188#ifdef CONFIG_PROC_FS
2189
2190struct ipv6_route_iter {
2191 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002192 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002193 loff_t skip;
2194 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002195 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002196};
2197
2198static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2199{
2200 struct rt6_info *rt = v;
2201 struct ipv6_route_iter *iter = seq->private;
David Ahern5e670d82018-04-17 17:33:14 -07002202 const struct net_device *dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002203
2204 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2205
2206#ifdef CONFIG_IPV6_SUBTREES
2207 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2208#else
2209 seq_puts(seq, "00000000000000000000000000000000 00 ");
2210#endif
2211 if (rt->rt6i_flags & RTF_GATEWAY)
David Ahern5e670d82018-04-17 17:33:14 -07002212 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002213 else
2214 seq_puts(seq, "00000000000000000000000000000000");
2215
David Ahern5e670d82018-04-17 17:33:14 -07002216 dev = rt->fib6_nh.nh_dev;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002217 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2218 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2219 rt->dst.__use, rt->rt6i_flags,
David Ahern5e670d82018-04-17 17:33:14 -07002220 dev ? dev->name : "");
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002221 iter->w.leaf = NULL;
2222 return 0;
2223}
2224
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002225static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002226{
2227 struct ipv6_route_iter *iter = w->args;
2228
2229 if (!iter->skip)
2230 return 1;
2231
2232 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002233 iter->w.leaf = rcu_dereference_protected(
David Miller071fb372017-11-28 15:40:15 -05002234 iter->w.leaf->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002235 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002236 iter->skip--;
2237 if (!iter->skip && iter->w.leaf)
2238 return 1;
2239 } while (iter->w.leaf);
2240
2241 return 0;
2242}
2243
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002244static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2245 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002246{
2247 memset(&iter->w, 0, sizeof(iter->w));
2248 iter->w.func = ipv6_route_yield;
2249 iter->w.root = &iter->tbl->tb6_root;
2250 iter->w.state = FWS_INIT;
2251 iter->w.node = iter->w.root;
2252 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002253 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002254 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002255 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002256}
2257
2258static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2259 struct net *net)
2260{
2261 unsigned int h;
2262 struct hlist_node *node;
2263
2264 if (tbl) {
2265 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2266 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2267 } else {
2268 h = 0;
2269 node = NULL;
2270 }
2271
2272 while (!node && h < FIB6_TABLE_HASHSZ) {
2273 node = rcu_dereference_bh(
2274 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2275 }
2276 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2277}
2278
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002279static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2280{
2281 if (iter->sernum != iter->w.root->fn_sernum) {
2282 iter->sernum = iter->w.root->fn_sernum;
2283 iter->w.state = FWS_INIT;
2284 iter->w.node = iter->w.root;
2285 WARN_ON(iter->w.skip);
2286 iter->w.skip = iter->w.count;
2287 }
2288}
2289
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002290static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2291{
2292 int r;
2293 struct rt6_info *n;
2294 struct net *net = seq_file_net(seq);
2295 struct ipv6_route_iter *iter = seq->private;
2296
2297 if (!v)
2298 goto iter_table;
2299
David Miller071fb372017-11-28 15:40:15 -05002300 n = rcu_dereference_bh(((struct rt6_info *)v)->rt6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002301 if (n) {
2302 ++*pos;
2303 return n;
2304 }
2305
2306iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002307 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002308 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002309 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002310 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002311 if (r > 0) {
2312 if (v)
2313 ++*pos;
2314 return iter->w.leaf;
2315 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002316 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002317 return NULL;
2318 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002319 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002320
2321 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2322 if (!iter->tbl)
2323 return NULL;
2324
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002325 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002326 goto iter_table;
2327}
2328
2329static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2330 __acquires(RCU_BH)
2331{
2332 struct net *net = seq_file_net(seq);
2333 struct ipv6_route_iter *iter = seq->private;
2334
2335 rcu_read_lock_bh();
2336 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2337 iter->skip = *pos;
2338
2339 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002340 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002341 return ipv6_route_seq_next(seq, NULL, pos);
2342 } else {
2343 return NULL;
2344 }
2345}
2346
2347static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2348{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002349 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002350 return w->node && !(w->state == FWS_U && w->node == w->root);
2351}
2352
2353static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2354 __releases(RCU_BH)
2355{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002356 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002357 struct ipv6_route_iter *iter = seq->private;
2358
2359 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002360 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002361
2362 rcu_read_unlock_bh();
2363}
2364
2365static const struct seq_operations ipv6_route_seq_ops = {
2366 .start = ipv6_route_seq_start,
2367 .next = ipv6_route_seq_next,
2368 .stop = ipv6_route_seq_stop,
2369 .show = ipv6_route_seq_show
2370};
2371
2372int ipv6_route_open(struct inode *inode, struct file *file)
2373{
2374 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2375 sizeof(struct ipv6_route_iter));
2376}
2377
2378#endif /* CONFIG_PROC_FS */