blob: 5e4b5eef0ddd345d926cbf3a84e2d1970c0ba938 [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
Wei Wang180ca442017-10-06 12:05:56 -0700108void fib6_update_sernum(struct rt6_info *rt)
109{
110 struct fib6_table *table = rt->rt6i_table;
111 struct net *net = dev_net(rt->dst.dev);
112 struct fib6_node *fn;
113
Wei Wang66f5d6c2017-10-06 12:06:10 -0700114 spin_lock_bh(&table->tb6_lock);
Wei Wang180ca442017-10-06 12:05:56 -0700115 fn = rcu_dereference_protected(rt->rt6i_node,
116 lockdep_is_held(&table->tb6_lock));
117 if (fn)
118 fn->fn_sernum = fib6_new_sernum(net);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700119 spin_unlock_bh(&table->tb6_lock);
Wei Wang180ca442017-10-06 12:05:56 -0700120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
123 * Auxiliary address test functions for the radix tree.
124 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900125 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * 64bit processors)
127 */
128
129/*
130 * test bit
131 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000132#if defined(__LITTLE_ENDIAN)
133# define BITOP_BE32_SWIZZLE (0x1F & ~7)
134#else
135# define BITOP_BE32_SWIZZLE 0
136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200138static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000140 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000141 /*
142 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800143 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000144 * is optimized version of
145 * htonl(1 << ((~fn_bit)&0x1F))
146 * See include/asm-generic/bitops/le.h.
147 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700148 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
149 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Wei Wang81eb8442017-10-06 12:06:11 -0700152static struct fib6_node *node_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct fib6_node *fn;
155
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800156 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Wei Wang81eb8442017-10-06 12:06:11 -0700157 if (fn)
158 net->ipv6.rt6_stats->fib_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return fn;
161}
162
Wei Wang81eb8442017-10-06 12:06:11 -0700163static void node_free_immediate(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 kmem_cache_free(fib6_node_kmem, fn);
Wei Wang81eb8442017-10-06 12:06:11 -0700166 net->ipv6.rt6_stats->fib_nodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Wei Wangc5cff852017-08-21 09:47:10 -0700169static void node_free_rcu(struct rcu_head *head)
170{
171 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
172
173 kmem_cache_free(fib6_node_kmem, fn);
174}
175
Wei Wang81eb8442017-10-06 12:06:11 -0700176static void node_free(struct net *net, struct fib6_node *fn)
Wei Wangc5cff852017-08-21 09:47:10 -0700177{
178 call_rcu(&fn->rcu, node_free_rcu);
Wei Wang81eb8442017-10-06 12:06:11 -0700179 net->ipv6.rt6_stats->fib_nodes--;
Wei Wangc5cff852017-08-21 09:47:10 -0700180}
181
Ido Schimmela460aa82017-08-03 13:28:25 +0200182void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700183{
184 int cpu;
185
186 if (!non_pcpu_rt->rt6i_pcpu)
187 return;
188
189 for_each_possible_cpu(cpu) {
190 struct rt6_info **ppcpu_rt;
191 struct rt6_info *pcpu_rt;
192
193 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
194 pcpu_rt = *ppcpu_rt;
195 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700196 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700197 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700198 *ppcpu_rt = NULL;
199 }
200 }
201}
Ido Schimmela460aa82017-08-03 13:28:25 +0200202EXPORT_SYMBOL_GPL(rt6_free_pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200204static void fib6_free_table(struct fib6_table *table)
205{
206 inetpeer_invalidate_tree(&table->tb6_peers);
207 kfree(table);
208}
209
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800210static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700211{
212 unsigned int h;
213
Thomas Graf375216a2006-10-21 20:20:54 -0700214 /*
215 * Initialize table lock at a single place to give lockdep a key,
216 * tables aren't visible prior to being linked to the list.
217 */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700218 spin_lock_init(&tb->tb6_lock);
Neil Hormana33bc5c2009-07-30 18:52:15 -0700219 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700220
221 /*
222 * No protection necessary, this is the only list mutatation
223 * operation, tables never disappear once they exist.
224 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800225 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700226}
227
228#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800229
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800230static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700231{
232 struct fib6_table *table;
233
234 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500235 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700236 table->tb6_id = id;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700237 rcu_assign_pointer(table->tb6_root.leaf,
238 net->ipv6.ip6_null_entry);
Thomas Grafc71099a2006-08-04 23:20:06 -0700239 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700240 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700241 }
242
243 return table;
244}
245
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800246struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700247{
248 struct fib6_table *tb;
249
250 if (id == 0)
251 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800252 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700253 if (tb)
254 return tb;
255
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800256 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500257 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259
260 return tb;
261}
David Ahernb3b46632016-05-04 21:46:12 -0700262EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700263
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800264struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700265{
266 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800267 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700268 unsigned int h;
269
270 if (id == 0)
271 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700272 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700273 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800274 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800275 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700276 if (tb->tb6_id == id) {
277 rcu_read_unlock();
278 return tb;
279 }
280 }
281 rcu_read_unlock();
282
283 return NULL;
284}
David Ahernc4850682015-10-12 11:47:08 -0700285EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700286
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000287static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700288{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800289 fib6_link_table(net, net->ipv6.fib6_main_tbl);
290 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700291}
Thomas Grafc71099a2006-08-04 23:20:06 -0700292#else
293
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800294struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700295{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800296 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700297}
298
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800299struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700300{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800301 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700302}
303
David S. Miller4c9483b2011-03-12 16:22:43 -0500304struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800305 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700306{
lucienab997ad2015-10-23 15:36:53 +0800307 struct rt6_info *rt;
308
309 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300310 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800311 ip6_rt_put(rt);
312 rt = net->ipv6.ip6_null_entry;
313 dst_hold(&rt->dst);
314 }
315
316 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700317}
318
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000319static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700320{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800321 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700322}
323
324#endif
325
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200326unsigned int fib6_tables_seq_read(struct net *net)
327{
328 unsigned int h, fib_seq = 0;
329
330 rcu_read_lock();
331 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
332 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
333 struct fib6_table *tb;
334
Wei Wang66f5d6c2017-10-06 12:06:10 -0700335 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200336 fib_seq += tb->fib_seq;
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200337 }
338 rcu_read_unlock();
339
340 return fib_seq;
341}
342
343static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
344 enum fib_event_type event_type,
345 struct rt6_info *rt)
346{
347 struct fib6_entry_notifier_info info = {
348 .rt = rt,
349 };
350
351 return call_fib6_notifier(nb, net, event_type, &info.info);
352}
353
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200354static int call_fib6_entry_notifiers(struct net *net,
355 enum fib_event_type event_type,
David Ahern6c31e5a2017-10-27 17:37:13 -0700356 struct rt6_info *rt,
357 struct netlink_ext_ack *extack)
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200358{
359 struct fib6_entry_notifier_info info = {
David Ahern6c31e5a2017-10-27 17:37:13 -0700360 .info.extack = extack,
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200361 .rt = rt,
362 };
363
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200364 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200365 return call_fib6_notifiers(net, event_type, &info.info);
366}
367
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200368struct fib6_dump_arg {
369 struct net *net;
370 struct notifier_block *nb;
371};
372
373static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
374{
375 if (rt == arg->net->ipv6.ip6_null_entry)
376 return;
377 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
378}
379
380static int fib6_node_dump(struct fib6_walker *w)
381{
382 struct rt6_info *rt;
383
Wei Wang66f5d6c2017-10-06 12:06:10 -0700384 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200385 fib6_rt_dump(rt, w->args);
386 w->leaf = NULL;
387 return 0;
388}
389
390static void fib6_table_dump(struct net *net, struct fib6_table *tb,
391 struct fib6_walker *w)
392{
393 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700394 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200395 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700396 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200397}
398
399/* Called with rcu_read_lock() */
400int fib6_tables_dump(struct net *net, struct notifier_block *nb)
401{
402 struct fib6_dump_arg arg;
403 struct fib6_walker *w;
404 unsigned int h;
405
406 w = kzalloc(sizeof(*w), GFP_ATOMIC);
407 if (!w)
408 return -ENOMEM;
409
410 w->func = fib6_node_dump;
411 arg.net = net;
412 arg.nb = nb;
413 w->args = &arg;
414
415 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
416 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
417 struct fib6_table *tb;
418
419 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
420 fib6_table_dump(net, tb, w);
421 }
422
423 kfree(w);
424
425 return 0;
426}
427
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200428static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700429{
430 int res;
431 struct rt6_info *rt;
432
Wei Wang66f5d6c2017-10-06 12:06:10 -0700433 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700434 res = rt6_dump_route(rt, w->args);
435 if (res < 0) {
436 /* Frame is full, suspend walking */
437 w->leaf = rt;
438 return 1;
439 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800440
441 /* Multipath routes are dumped in one route with the
442 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
443 * last sibling of this route (no need to dump the
444 * sibling routes again)
445 */
446 if (rt->rt6i_nsiblings)
447 rt = list_last_entry(&rt->rt6i_siblings,
448 struct rt6_info,
449 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700450 }
451 w->leaf = NULL;
452 return 0;
453}
454
455static void fib6_dump_end(struct netlink_callback *cb)
456{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100457 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200458 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700459
460 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800461 if (cb->args[4]) {
462 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100463 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800464 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700465 cb->args[2] = 0;
466 kfree(w);
467 }
Wang Yufen437de072014-03-28 12:07:04 +0800468 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700469 cb->args[1] = 3;
470}
471
472static int fib6_dump_done(struct netlink_callback *cb)
473{
474 fib6_dump_end(cb);
475 return cb->done ? cb->done(cb) : 0;
476}
477
478static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
479 struct netlink_callback *cb)
480{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100481 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200482 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700483 int res;
484
485 w = (void *)cb->args[2];
486 w->root = &table->tb6_root;
487
488 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000489 w->count = 0;
490 w->skip = 0;
491
Wei Wang66f5d6c2017-10-06 12:06:10 -0700492 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100493 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700494 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000495 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700496 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000497 cb->args[5] = w->root->fn_sernum;
498 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700499 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000500 if (cb->args[5] != w->root->fn_sernum) {
501 /* Begin at the root if the tree changed */
502 cb->args[5] = w->root->fn_sernum;
503 w->state = FWS_INIT;
504 w->node = w->root;
505 w->skip = w->count;
506 } else
507 w->skip = 0;
508
Wei Wang66f5d6c2017-10-06 12:06:10 -0700509 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700510 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700511 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800512 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100513 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800514 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700515 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700516 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800517
Patrick McHardy1b43af52006-08-10 23:11:17 -0700518 return res;
519}
520
Thomas Grafc127ea22007-03-22 11:58:32 -0700521static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700522{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900523 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700524 unsigned int h, s_h;
525 unsigned int e = 0, s_e;
526 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200527 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700528 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800529 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700530 int res = 0;
531
532 s_h = cb->args[0];
533 s_e = cb->args[1];
534
535 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500536 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700537 /* New dump:
538 *
539 * 1. hook callback destructor.
540 */
541 cb->args[3] = (long)cb->done;
542 cb->done = fib6_dump_done;
543
544 /*
545 * 2. allocate and initialize walker.
546 */
547 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500548 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700549 return -ENOMEM;
550 w->func = fib6_dump_node;
551 cb->args[2] = (long)w;
552 }
553
554 arg.skb = skb;
555 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700556 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700557 w->args = &arg;
558
Eric Dumazete67f88d2011-04-27 22:56:07 +0000559 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700560 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700561 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800562 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800563 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700564 if (e < s_e)
565 goto next;
566 res = fib6_dump_table(tb, skb, cb);
567 if (res != 0)
568 goto out;
569next:
570 e++;
571 }
572 }
573out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000574 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700575 cb->args[1] = e;
576 cb->args[0] = h;
577
578 res = res < 0 ? res : skb->len;
579 if (res <= 0)
580 fib6_dump_end(cb);
581 return res;
582}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584/*
585 * Routing Table
586 *
587 * return the appropriate node for a routing tree "add" operation
588 * by either creating and inserting or by returning an existing
589 * node.
590 */
591
Wei Wang81eb8442017-10-06 12:06:11 -0700592static struct fib6_node *fib6_add_1(struct net *net,
593 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700594 struct fib6_node *root,
595 struct in6_addr *addr, int plen,
596 int offset, int allow_create,
597 int replace_required,
598 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct fib6_node *fn, *in, *ln;
601 struct fib6_node *pn = NULL;
602 struct rt6key *key;
603 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900604 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 RT6_TRACE("fib6_add_1\n");
607
608 /* insert node in tree */
609
610 fn = root;
611
612 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700613 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
614 lockdep_is_held(&table->tb6_lock));
615 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /*
618 * Prefix match
619 */
620 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000621 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000622 if (!allow_create) {
623 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600624 NL_SET_ERR_MSG(extack,
625 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000626 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000627 return ERR_PTR(-ENOENT);
628 }
Joe Perchesf3213832012-05-15 14:11:53 +0000629 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000632 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
635 * Exact match ?
636 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (plen == fn->fn_bit) {
639 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500640 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700641 RCU_INIT_POINTER(fn->leaf, NULL);
642 rt6_release(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return fn;
646 }
647
648 /*
649 * We have more bits to go
650 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dir = addr_bit_set(addr, fn->fn_bit);
654 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700655 fn = dir ?
656 rcu_dereference_protected(fn->right,
657 lockdep_is_held(&table->tb6_lock)) :
658 rcu_dereference_protected(fn->left,
659 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } while (fn);
661
Matti Vaittinen14df0152011-11-16 21:18:02 +0000662 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000663 /* We should not create new node because
664 * NLM_F_REPLACE was specified without NLM_F_CREATE
665 * I assume it is safe to require NLM_F_CREATE when
666 * REPLACE flag is used! Later we may want to remove the
667 * check for replace_required, because according
668 * to netlink specification, NLM_F_CREATE
669 * MUST be specified if new route is created.
670 * That would keep IPv6 consistent with IPv4
671 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000672 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600673 NL_SET_ERR_MSG(extack,
674 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000675 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000676 return ERR_PTR(-ENOENT);
677 }
Joe Perchesf3213832012-05-15 14:11:53 +0000678 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * We walked to the bottom of tree.
682 * Create new leaf node without children.
683 */
684
Wei Wang81eb8442017-10-06 12:06:11 -0700685 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
David S. Miller507c9b12011-12-03 17:50:45 -0500687 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000688 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700690 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700693 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700695 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 return ln;
698
699
700insert_above:
701 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900702 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * we have a less significant route.
704 * we've to insert an intermediate node on the list
705 * this new node will point to the one we need to create
706 * and the current
707 */
708
Wei Wang66f5d6c2017-10-06 12:06:10 -0700709 pn = rcu_dereference_protected(fn->parent,
710 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* find 1st bit in difference between the 2 addrs.
713
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800714 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 but if it is >= plen, the value is ignored in any case.
716 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900717
fan.du9225b232013-07-22 14:21:09 +0800718 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900720 /*
721 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * / \
723 * (new leaf node)[ln] (old node)[fn]
724 */
725 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700726 in = node_alloc(net);
727 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900728
David S. Miller507c9b12011-12-03 17:50:45 -0500729 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700731 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700733 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000734 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900737 /*
738 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * RTN_RTINFO will
740 * be off since that an address that chooses one of
741 * the branches would not match less specific routes
742 * in the other branch
743 */
744
745 in->fn_bit = bit;
746
Wei Wang66f5d6c2017-10-06 12:06:10 -0700747 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700749 atomic_inc(&rcu_dereference_protected(in->leaf,
750 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* update parent pointer */
753 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700754 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700756 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 ln->fn_bit = plen;
759
Wei Wang66f5d6c2017-10-06 12:06:10 -0700760 RCU_INIT_POINTER(ln->parent, in);
761 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700764 rcu_assign_pointer(in->right, ln);
765 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700767 rcu_assign_pointer(in->left, ln);
768 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 } else { /* plen <= bit */
771
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900772 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * (new leaf node)[ln]
774 * / \
775 * (old node)[fn] NULL
776 */
777
Wei Wang81eb8442017-10-06 12:06:11 -0700778 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
David S. Miller507c9b12011-12-03 17:50:45 -0500780 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000781 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 ln->fn_bit = plen;
784
Wei Wang66f5d6c2017-10-06 12:06:10 -0700785 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700788 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700790 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Wei Wang66f5d6c2017-10-06 12:06:10 -0700792 rcu_assign_pointer(fn->parent, ln);
793
794 if (dir)
795 rcu_assign_pointer(pn->right, ln);
796 else
797 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 return ln;
800}
801
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200802static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200803{
804 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
805 RTF_GATEWAY;
806}
807
Florian Westphale715b6d2015-01-05 23:57:44 +0100808static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100809{
Florian Westphale715b6d2015-01-05 23:57:44 +0100810 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100811
Florian Westphale715b6d2015-01-05 23:57:44 +0100812 for (i = 0; i < RTAX_MAX; i++) {
813 if (test_bit(i, mxc->mx_valid))
814 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100815 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100816}
817
818static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
819{
820 if (!mxc->mx)
821 return 0;
822
823 if (dst->flags & DST_HOST) {
824 u32 *mp = dst_metrics_write_ptr(dst);
825
826 if (unlikely(!mp))
827 return -ENOMEM;
828
829 fib6_copy_metrics(mp, mxc);
830 } else {
831 dst_init_metrics(dst, mxc->mx, false);
832
833 /* We've stolen mx now. */
834 mxc->mx = NULL;
835 }
836
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100837 return 0;
838}
839
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100840static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
841 struct net *net)
842{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700843 struct fib6_table *table = rt->rt6i_table;
844
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100845 if (atomic_read(&rt->rt6i_ref) != 1) {
846 /* This route is used as dummy address holder in some split
847 * nodes. It is not leaked, but it still holds other resources,
848 * which must be released in time. So, scan ascendant nodes
849 * and replace dummy references to this route with references
850 * to still alive ones.
851 */
852 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700853 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
854 lockdep_is_held(&table->tb6_lock));
855 struct rt6_info *new_leaf;
856 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
857 new_leaf = fib6_find_prefix(net, table, fn);
858 atomic_inc(&new_leaf->rt6i_ref);
859 rcu_assign_pointer(fn->leaf, new_leaf);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100860 rt6_release(rt);
861 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700862 fn = rcu_dereference_protected(fn->parent,
863 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100864 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100865 }
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
869 * Insert routing information in a node.
870 */
871
872static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700873 struct nl_info *info, struct mx6_config *mxc,
874 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700876 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
877 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct rt6_info *iter = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700879 struct rt6_info __rcu **ins;
880 struct rt6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500881 int replace = (info->nlh &&
882 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
883 int add = (!info->nlh ||
884 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000885 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200886 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200887 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100888 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
David Ahern1f5e29c2017-01-31 16:51:37 -0800890 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
891 nlflags |= NLM_F_APPEND;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 ins = &fn->leaf;
894
Wei Wang66f5d6c2017-10-06 12:06:10 -0700895 for (iter = leaf; iter;
David Miller071fb372017-11-28 15:40:15 -0500896 iter = rcu_dereference_protected(iter->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700897 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Search for duplicates
900 */
901
902 if (iter->rt6i_metric == rt->rt6i_metric) {
903 /*
904 * Same priority level
905 */
David S. Miller507c9b12011-12-03 17:50:45 -0500906 if (info->nlh &&
907 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000908 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200909
910 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000911 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200912 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
913 found++;
914 break;
915 }
916 if (rt_can_ecmp)
917 fallback_ins = fallback_ins ?: ins;
918 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
David Ahernf06b7542017-07-05 14:41:46 -0600921 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000922 if (rt->rt6i_nsiblings)
923 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500924 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000926 if (!(rt->rt6i_flags & RTF_EXPIRES))
927 rt6_clean_expires(iter);
928 else
929 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700930 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -EEXIST;
932 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000933 /* If we have the same destination and the same metric,
934 * but not the same gateway, then the route we try to
935 * add is sibling to this route, increment our counter
936 * of siblings, and later we will add our route to the
937 * list.
938 * Only static routes (which don't have flag
939 * RTF_EXPIRES) are used for ECMPv6.
940 *
941 * To avoid long list, we only had siblings if the
942 * route have a gateway.
943 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200944 if (rt_can_ecmp &&
945 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000946 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949 if (iter->rt6i_metric > rt->rt6i_metric)
950 break;
951
Michal Kubeček27596472015-05-18 20:54:00 +0200952next_iter:
David Miller071fb372017-11-28 15:40:15 -0500953 ins = &iter->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Michal Kubeček27596472015-05-18 20:54:00 +0200956 if (fallback_ins && !found) {
957 /* No ECMP-able route found, replace first non-ECMP one */
958 ins = fallback_ins;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700959 iter = rcu_dereference_protected(*ins,
960 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +0200961 found++;
962 }
963
David S. Millerf11e6652007-03-24 20:36:25 -0700964 /* Reset round-robin state, if necessary */
965 if (ins == &fn->leaf)
966 fn->rr_ptr = NULL;
967
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000968 /* Link this route to others same route. */
969 if (rt->rt6i_nsiblings) {
970 unsigned int rt6i_nsiblings;
971 struct rt6_info *sibling, *temp_sibling;
972
973 /* Find the first route that have the same metric */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700974 sibling = leaf;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000975 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200976 if (sibling->rt6i_metric == rt->rt6i_metric &&
977 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000978 list_add_tail(&rt->rt6i_siblings,
979 &sibling->rt6i_siblings);
980 break;
981 }
David Miller071fb372017-11-28 15:40:15 -0500982 sibling = rcu_dereference_protected(sibling->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700983 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000984 }
985 /* For each sibling in the list, increment the counter of
986 * siblings. BUG() if counters does not match, list of siblings
987 * is broken!
988 */
989 rt6i_nsiblings = 0;
990 list_for_each_entry_safe(sibling, temp_sibling,
991 &rt->rt6i_siblings, rt6i_siblings) {
992 sibling->rt6i_nsiblings++;
993 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
994 rt6i_nsiblings++;
995 }
996 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
997 }
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
1000 * insert node
1001 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001002 if (!replace) {
1003 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001004 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001006add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001007 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001008 err = fib6_commit_metrics(&rt->dst, mxc);
1009 if (err)
1010 return err;
1011
David Miller071fb372017-11-28 15:40:15 -05001012 rcu_assign_pointer(rt->rt6_next, iter);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001013 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001014 rcu_assign_pointer(rt->rt6i_node, fn);
1015 rcu_assign_pointer(*ins, rt);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001016 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
David Ahern6c31e5a2017-10-27 17:37:13 -07001017 rt, extack);
David Ahern3b1137f2017-02-02 12:37:10 -08001018 if (!info->skip_notify)
1019 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001020 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
David S. Miller507c9b12011-12-03 17:50:45 -05001022 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001023 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1024 fn->fn_flags |= RTN_RTINFO;
1025 }
1026
1027 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001028 int nsiblings;
1029
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001030 if (!found) {
1031 if (add)
1032 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001033 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001034 return -ENOENT;
1035 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001036
1037 err = fib6_commit_metrics(&rt->dst, mxc);
1038 if (err)
1039 return err;
1040
Wei Wang66f5d6c2017-10-06 12:06:10 -07001041 atomic_inc(&rt->rt6i_ref);
Wei Wang4e587ea2017-08-25 15:03:10 -07001042 rcu_assign_pointer(rt->rt6i_node, fn);
David Miller071fb372017-11-28 15:40:15 -05001043 rt->rt6_next = iter->rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001044 rcu_assign_pointer(*ins, rt);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001045 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
David Ahern6c31e5a2017-10-27 17:37:13 -07001046 rt, extack);
David Ahern3b1137f2017-02-02 12:37:10 -08001047 if (!info->skip_notify)
1048 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001049 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001050 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1051 fn->fn_flags |= RTN_RTINFO;
1052 }
Michal Kubeček27596472015-05-18 20:54:00 +02001053 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001054 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001055 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;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001058 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001059
1060 if (nsiblings) {
1061 /* Replacing an ECMP route, remove all siblings */
David Miller071fb372017-11-28 15:40:15 -05001062 ins = &rt->rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001063 iter = rcu_dereference_protected(*ins,
1064 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001065 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001066 if (iter->rt6i_metric > rt->rt6i_metric)
1067 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001068 if (rt6_qualify_for_ecmp(iter)) {
David Miller071fb372017-11-28 15:40:15 -05001069 *ins = iter->rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001070 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001071 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001072 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001073 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001074 rt6_release(iter);
1075 nsiblings--;
Wei Wang81eb8442017-10-06 12:06:11 -07001076 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001077 } else {
David Miller071fb372017-11-28 15:40:15 -05001078 ins = &iter->rt6_next;
Michal Kubeček27596472015-05-18 20:54:00 +02001079 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001080 iter = rcu_dereference_protected(*ins,
1081 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001082 }
1083 WARN_ON(nsiblings != 0);
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
1087 return 0;
1088}
1089
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001090static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001092 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001093 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001094 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001095 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001098void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001100 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1101 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001102 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Wei Wangbbd63f02017-10-06 12:06:07 -07001105static void fib6_update_sernum_upto_root(struct rt6_info *rt,
1106 int sernum)
1107{
1108 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1109 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1110
1111 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1112 smp_wmb();
1113 while (fn) {
1114 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001115 fn = rcu_dereference_protected(fn->parent,
1116 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001117 }
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/*
1121 * Add routing information to the routing tree.
1122 * <destination addr>/<source addr>
1123 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001124 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
1126
Florian Westphale715b6d2015-01-05 23:57:44 +01001127int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001128 struct nl_info *info, struct mx6_config *mxc,
1129 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001131 struct fib6_table *table = rt->rt6i_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001132 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001134 int allow_create = 1;
1135 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001136 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001137
Wei Wanga4c2fd72017-06-17 10:42:42 -07001138 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001139 return -EINVAL;
Wei Wang2b760fc2017-10-06 12:06:03 -07001140 if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
1141 return -EINVAL;
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001142
David S. Miller507c9b12011-12-03 17:50:45 -05001143 if (info->nlh) {
1144 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001145 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001146 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001147 replace_required = 1;
1148 }
1149 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001150 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Wei Wang81eb8442017-10-06 12:06:11 -07001152 fn = fib6_add_1(info->nl_net, table, root,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001153 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
fan.du9225b232013-07-22 14:21:09 +08001154 offsetof(struct rt6_info, rt6i_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001155 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001156 if (IS_ERR(fn)) {
1157 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001158 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001162 pn = fn;
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164#ifdef CONFIG_IPV6_SUBTREES
1165 if (rt->rt6i_src.plen) {
1166 struct fib6_node *sn;
1167
Wei Wang66f5d6c2017-10-06 12:06:10 -07001168 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct fib6_node *sfn;
1170
1171 /*
1172 * Create subtree.
1173 *
1174 * fn[main tree]
1175 * |
1176 * sfn[subtree root]
1177 * \
1178 * sn[new leaf node]
1179 */
1180
1181 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001182 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001183 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001184 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001186 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001187 rcu_assign_pointer(sfn->leaf,
1188 info->nl_net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* Now add the first leaf node to new subtree */
1192
Wei Wang81eb8442017-10-06 12:06:11 -07001193 sn = fib6_add_1(info->nl_net, table, sfn,
1194 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001195 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001196 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001198 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001200 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 in main tree.
1202 */
Wei Wang81eb8442017-10-06 12:06:11 -07001203 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001204 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001205 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001209 rcu_assign_pointer(sfn->parent, fn);
1210 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001212 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1213 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001214 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001215 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001217 if (IS_ERR(sn)) {
1218 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001219 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
Wei Wang66f5d6c2017-10-06 12:06:10 -07001223 if (!rcu_access_pointer(fn->leaf)) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001224 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001225 rcu_assign_pointer(fn->leaf, rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 fn = sn;
1228 }
1229#endif
1230
David Ahern6c31e5a2017-10-27 17:37:13 -07001231 err = fib6_add_rt2node(fn, rt, info, mxc, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001232 if (!err) {
1233 fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001234 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001238 if (err) {
1239#ifdef CONFIG_IPV6_SUBTREES
1240 /*
1241 * If fib6_add_1 has cleared the old leaf pointer in the
1242 * super-tree leaf node we have to find a new one for it.
1243 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001244 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1245 lockdep_is_held(&table->tb6_lock));
1246 if (pn != fn && pn_leaf == rt) {
1247 pn_leaf = NULL;
1248 RCU_INIT_POINTER(pn->leaf, NULL);
David S. Miller3c051232008-04-18 01:46:19 -07001249 atomic_dec(&rt->rt6i_ref);
1250 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001251 if (pn != fn && !pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1252 pn_leaf = fib6_find_prefix(info->nl_net, table, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001253#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001254 if (!pn_leaf) {
1255 WARN_ON(!pn_leaf);
1256 pn_leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001257 }
1258#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001259 atomic_inc(&pn_leaf->rt6i_ref);
1260 rcu_assign_pointer(pn->leaf, pn_leaf);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001261 }
1262#endif
Wei Wang348a4002017-08-18 17:14:49 -07001263 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return err;
1266
Wei Wang348a4002017-08-18 17:14:49 -07001267failure:
1268 /* fn->leaf could be NULL if fn is an intermediate node and we
1269 * failed to add the new route to it in both subtree creation
1270 * failure and fib6_add_rt2node() failure case.
1271 * In both cases, fib6_repair_tree() should be called to fix
1272 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001275 fib6_repair_tree(info->nl_net, table, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001276 /* Always release dst as dst->__refcnt is guaranteed
1277 * to be taken before entering this function
1278 */
Wei Wang587fea72017-06-17 10:42:36 -07001279 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283/*
1284 * Routing tree lookup
1285 *
1286 */
1287
1288struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001289 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001290 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291};
1292
Wang Yufen437de072014-03-28 12:07:04 +08001293static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1294 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001297 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001299 if (unlikely(args->offset == 0))
1300 return NULL;
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Descend on a tree
1304 */
1305
1306 fn = root;
1307
1308 for (;;) {
1309 struct fib6_node *next;
1310
1311 dir = addr_bit_set(args->addr, fn->fn_bit);
1312
Wei Wang66f5d6c2017-10-06 12:06:10 -07001313 next = dir ? rcu_dereference(fn->right) :
1314 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (next) {
1317 fn = next;
1318 continue;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321 }
1322
David S. Miller507c9b12011-12-03 17:50:45 -05001323 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001324 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1325
1326 if (subtree || fn->fn_flags & RTN_RTINFO) {
1327 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct rt6key *key;
1329
Wei Wang8d1040e2017-10-06 12:06:08 -07001330 if (!leaf)
1331 goto backtrack;
1332
1333 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001335 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1336#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001337 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001338 struct fib6_node *sfn;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001339 sfn = fib6_lookup_1(subtree, args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001340 if (!sfn)
1341 goto backtrack;
1342 fn = sfn;
1343 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001344#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001345 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001346 return fn;
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001349backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001350 if (fn->fn_flags & RTN_ROOT)
1351 break;
1352
Wei Wang66f5d6c2017-10-06 12:06:10 -07001353 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
1356 return NULL;
1357}
1358
Wei Wang66f5d6c2017-10-06 12:06:10 -07001359/* called with rcu_read_lock() held
1360 */
Wang Yufen437de072014-03-28 12:07:04 +08001361struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1362 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001365 struct lookup_args args[] = {
1366 {
1367 .offset = offsetof(struct rt6_info, rt6i_dst),
1368 .addr = daddr,
1369 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001371 {
1372 .offset = offsetof(struct rt6_info, rt6i_src),
1373 .addr = saddr,
1374 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001376 {
1377 .offset = 0, /* sentinel */
1378 }
1379 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001381 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001382 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 fn = root;
1384
1385 return fn;
1386}
1387
1388/*
1389 * Get node with specified destination prefix (and source prefix,
1390 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001391 * exact_match == true means we try to find fn with exact match of
1392 * the passed in prefix addr
1393 * exact_match == false means we try to find fn with longest prefix
1394 * match of the passed in prefix addr. This is useful for finding fn
1395 * for cached route as it will be stored in the exception table under
1396 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
1398
1399
Wang Yufen437de072014-03-28 12:07:04 +08001400static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1401 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001402 int plen, int offset,
1403 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Wei Wang38fbeee2017-10-06 12:06:02 -07001405 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 for (fn = root; fn ; ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001408 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001409 struct rt6key *key;
1410
1411 /* This node is being deleted */
1412 if (!leaf) {
1413 if (plen <= fn->fn_bit)
1414 goto out;
1415 else
1416 goto next;
1417 }
1418
1419 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * Prefix match
1423 */
1424 if (plen < fn->fn_bit ||
1425 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001426 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 if (plen == fn->fn_bit)
1429 return fn;
1430
Wei Wang38fbeee2017-10-06 12:06:02 -07001431 prev = fn;
1432
Wei Wang8d1040e2017-10-06 12:06:08 -07001433next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /*
1435 * We have more bits to go
1436 */
1437 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001438 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001440 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001442out:
1443 if (exact_match)
1444 return NULL;
1445 else
1446 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Wang Yufen437de072014-03-28 12:07:04 +08001449struct fib6_node *fib6_locate(struct fib6_node *root,
1450 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001451 const struct in6_addr *saddr, int src_len,
1452 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 struct fib6_node *fn;
1455
1456 fn = fib6_locate_1(root, daddr, dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001457 offsetof(struct rt6_info, rt6i_dst),
1458 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460#ifdef CONFIG_IPV6_SUBTREES
1461 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001462 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001463 if (fn) {
1464 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1465
1466 if (subtree) {
1467 fn = fib6_locate_1(subtree, saddr, src_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001468 offsetof(struct rt6_info, rt6i_src),
1469 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001470 }
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473#endif
1474
David S. Miller507c9b12011-12-03 17:50:45 -05001475 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return fn;
1477
1478 return NULL;
1479}
1480
1481
1482/*
1483 * Deletion
1484 *
1485 */
1486
Wei Wang66f5d6c2017-10-06 12:06:10 -07001487static struct rt6_info *fib6_find_prefix(struct net *net,
1488 struct fib6_table *table,
1489 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001491 struct fib6_node *child_left, *child_right;
1492
David S. Miller507c9b12011-12-03 17:50:45 -05001493 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001494 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
David S. Miller507c9b12011-12-03 17:50:45 -05001496 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001497 child_left = rcu_dereference_protected(fn->left,
1498 lockdep_is_held(&table->tb6_lock));
1499 child_right = rcu_dereference_protected(fn->right,
1500 lockdep_is_held(&table->tb6_lock));
1501 if (child_left)
1502 return rcu_dereference_protected(child_left->leaf,
1503 lockdep_is_held(&table->tb6_lock));
1504 if (child_right)
1505 return rcu_dereference_protected(child_right->leaf,
1506 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001508 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 return NULL;
1511}
1512
1513/*
1514 * Called to trim the tree of intermediate nodes when possible. "fn"
1515 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001516 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
1518
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001519static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001520 struct fib6_table *table,
1521 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 int children;
1524 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001525 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001526 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int iter = 0;
1528
1529 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001530 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1531 lockdep_is_held(&table->tb6_lock));
1532 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1533 lockdep_is_held(&table->tb6_lock));
1534 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1535 lockdep_is_held(&table->tb6_lock));
1536 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1537 lockdep_is_held(&table->tb6_lock));
1538 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1539 lockdep_is_held(&table->tb6_lock));
1540 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1541 lockdep_is_held(&table->tb6_lock));
1542 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1543 lockdep_is_held(&table->tb6_lock));
1544 struct rt6_info *new_fn_leaf;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1547 iter++;
1548
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001549 WARN_ON(fn->fn_flags & RTN_RTINFO);
1550 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001551 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 children = 0;
1554 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001555 if (fn_r)
1556 child = fn_r, children |= 1;
1557 if (fn_l)
1558 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001560 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561#ifdef CONFIG_IPV6_SUBTREES
1562 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001563 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564#endif
1565 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001566 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001568 if (!new_fn_leaf) {
1569 WARN_ON(!new_fn_leaf);
1570 new_fn_leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001573 atomic_inc(&new_fn_leaf->rt6i_ref);
1574 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1575 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001579 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001580 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001581 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 nstate = FWS_L;
1583 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001584 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001586 if (pn_r == fn)
1587 rcu_assign_pointer(pn->right, child);
1588 else if (pn_l == fn)
1589 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001591 else
1592 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593#endif
1594 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001595 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 nstate = FWS_R;
1597#ifdef CONFIG_IPV6_SUBTREES
1598 }
1599#endif
1600
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001601 read_lock(&net->ipv6.fib6_walker_lock);
1602 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001603 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001604 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1606 w->node = pn;
1607 w->state = nstate;
1608 }
1609 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (w->node == fn) {
1611 w->node = child;
1612 if (children&2) {
1613 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001614 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 } else {
1616 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001617 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 }
1620 }
1621 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001622 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Wei Wang81eb8442017-10-06 12:06:11 -07001624 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001625 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return pn;
1627
Wei Wang66f5d6c2017-10-06 12:06:10 -07001628 RCU_INIT_POINTER(pn->leaf, NULL);
1629 rt6_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 fn = pn;
1631 }
1632}
1633
Wei Wang66f5d6c2017-10-06 12:06:10 -07001634static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1635 struct rt6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001637 struct fib6_walker *w;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001638 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1639 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001640 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 RT6_TRACE("fib6_del_route\n");
1643
Wei Wang2b760fc2017-10-06 12:06:03 -07001644 WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* Unlink it */
David Miller071fb372017-11-28 15:40:15 -05001647 *rtp = rt->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001649 net->ipv6.rt6_stats->fib_rt_entries--;
1650 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Wei Wang2b760fc2017-10-06 12:06:03 -07001652 /* Flush all cached dst in exception table */
1653 rt6_flush_exceptions(rt);
1654
David S. Millerf11e6652007-03-24 20:36:25 -07001655 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001656 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001657 fn->rr_ptr = NULL;
1658
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001659 /* Remove this entry from other siblings */
1660 if (rt->rt6i_nsiblings) {
1661 struct rt6_info *sibling, *next_sibling;
1662
1663 list_for_each_entry_safe(sibling, next_sibling,
1664 &rt->rt6i_siblings, rt6i_siblings)
1665 sibling->rt6i_nsiblings--;
1666 rt->rt6i_nsiblings = 0;
1667 list_del_init(&rt->rt6i_siblings);
1668 }
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001671 read_lock(&net->ipv6.fib6_walker_lock);
1672 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (w->state == FWS_C && w->leaf == rt) {
1674 RT6_TRACE("walker %p adjusted by delroute\n", w);
David Miller071fb372017-11-28 15:40:15 -05001675 w->leaf = rcu_dereference_protected(rt->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001676 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001677 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 w->state = FWS_U;
1679 }
1680 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001681 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* If it was last route, expunge its radix tree node */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001684 if (!rcu_access_pointer(fn->leaf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001686 net->ipv6.rt6_stats->fib_route_nodes--;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001687 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001690 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
David Ahern6c31e5a2017-10-27 17:37:13 -07001692 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001693 if (!info->skip_notify)
1694 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 rt6_release(rt);
1696}
1697
Wei Wang66f5d6c2017-10-06 12:06:10 -07001698/* Need to own table->tb6_lock */
Thomas Graf86872cb2006-08-22 00:01:08 -07001699int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Wei Wang4e587ea2017-08-25 15:03:10 -07001701 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1702 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001703 struct fib6_table *table = rt->rt6i_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001704 struct net *net = info->nl_net;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001705 struct rt6_info __rcu **rtp;
1706 struct rt6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001709 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001710 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return -ENOENT;
1712 }
1713#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001714 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return -ENOENT;
1716
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001717 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Wei Wang2b760fc2017-10-06 12:06:03 -07001719 /* remove cached dst from exception table */
1720 if (rt->rt6i_flags & RTF_CACHE)
1721 return rt6_remove_exception_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 /*
1724 * Walk the leaf entries looking for ourself
1725 */
1726
Wei Wang66f5d6c2017-10-06 12:06:10 -07001727 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1728 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1729 lockdep_is_held(&table->tb6_lock));
1730 if (rt == cur) {
1731 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 0;
1733 }
David Miller071fb372017-11-28 15:40:15 -05001734 rtp_next = &cur->rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 return -ENOENT;
1737}
1738
1739/*
1740 * Tree traversal function.
1741 *
1742 * Certainly, it is not interrupt safe.
1743 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1744 * It means, that we can modify tree during walking
1745 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001746 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *
1748 * It guarantees that every node will be traversed,
1749 * and that it will be traversed only once.
1750 *
1751 * Callback function w->func may return:
1752 * 0 -> continue walking.
1753 * positive value -> walking is suspended (used by tree dumps,
1754 * and probably by gc, if it will be split to several slices)
1755 * negative value -> terminate walking.
1756 *
1757 * The function itself returns:
1758 * 0 -> walk is complete.
1759 * >0 -> walk is incomplete (i.e. suspended)
1760 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001761 *
1762 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
1764
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001765static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001767 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Wei Wang2b760fc2017-10-06 12:06:03 -07001769 /* w->root should always be table->tb6_root */
1770 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 for (;;) {
1773 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001774 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return 0;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 switch (w->state) {
1778#ifdef CONFIG_IPV6_SUBTREES
1779 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001780 if (FIB6_SUBTREE(fn)) {
1781 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 continue;
1783 }
1784 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001785#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001786 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001788 left = rcu_dereference_protected(fn->left, 1);
1789 if (left) {
1790 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 w->state = FWS_INIT;
1792 continue;
1793 }
1794 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001795 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001797 right = rcu_dereference_protected(fn->right, 1);
1798 if (right) {
1799 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 w->state = FWS_INIT;
1801 continue;
1802 }
1803 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001804 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001805 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001807 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001808 int err;
1809
Eric Dumazetfa809e22012-06-25 15:37:19 -07001810 if (w->skip) {
1811 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001812 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001813 }
1814
1815 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (err)
1817 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001818
1819 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 continue;
1821 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001822skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001824 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 case FWS_U:
1826 if (fn == w->root)
1827 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001828 pn = rcu_dereference_protected(fn->parent, 1);
1829 left = rcu_dereference_protected(pn->left, 1);
1830 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 w->node = pn;
1832#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001833 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001834 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 w->state = FWS_L;
1836 continue;
1837 }
1838#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001839 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 w->state = FWS_R;
1841 continue;
1842 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001843 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001845 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 continue;
1847 }
1848#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001849 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850#endif
1851 }
1852 }
1853}
1854
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001855static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 int res;
1858
1859 w->state = FWS_INIT;
1860 w->node = w->root;
1861
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001862 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 res = fib6_walk_continue(w);
1864 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001865 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return res;
1867}
1868
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001869static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 int res;
1872 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001873 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001874 struct nl_info info = {
1875 .nl_net = c->net,
1876 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001878 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1879 w->node->fn_sernum != c->sernum)
1880 w->node->fn_sernum = c->sernum;
1881
1882 if (!c->func) {
1883 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1884 w->leaf = NULL;
1885 return 0;
1886 }
1887
Wei Wang66f5d6c2017-10-06 12:06:10 -07001888 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 res = c->func(rt, c->arg);
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001890 if (res == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001892 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (res) {
1894#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001895 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001896 __func__, rt,
1897 rcu_access_pointer(rt->rt6i_node),
1898 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899#endif
1900 continue;
1901 }
1902 return 0;
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001903 } else if (res == -2) {
1904 if (WARN_ON(!rt->rt6i_nsiblings))
1905 continue;
1906 rt = list_last_entry(&rt->rt6i_siblings,
1907 struct rt6_info, rt6i_siblings);
1908 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001910 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 w->leaf = rt;
1913 return 0;
1914}
1915
1916/*
1917 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001918 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * func is called on each route.
Ido Schimmelb5cb5a72018-01-07 12:45:12 +02001920 * It may return -2 -> skip multipath route.
1921 * -1 -> delete this route.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 */
1924
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001925static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001926 int (*func)(struct rt6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001927 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001929 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 c.w.root = root;
1932 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001933 c.w.count = 0;
1934 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001936 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001938 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001940 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001943static void __fib6_clean_all(struct net *net,
1944 int (*func)(struct rt6_info *, void *),
1945 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001946{
Thomas Grafc71099a2006-08-04 23:20:06 -07001947 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001948 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001949 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001950
Patrick McHardy1b43af52006-08-10 23:11:17 -07001951 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001952 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001953 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001954 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001955 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001956 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07001957 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001958 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001959 }
1960 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001961 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001962}
1963
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001964void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1965 void *arg)
1966{
1967 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1968}
1969
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001970static void fib6_flush_trees(struct net *net)
1971{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001972 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001973
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001974 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/*
1978 * Garbage collection
1979 */
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981static int fib6_age(struct rt6_info *rt, void *arg)
1982{
Michal Kubeček3570df92016-03-08 14:44:25 +01001983 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 unsigned long now = jiffies;
1985
1986 /*
1987 * check addrconf expiration here.
1988 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
1990
David S. Millerd1918542011-12-28 20:19:20 -05001991 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1992 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return -1;
1995 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001996 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998
Wei Wangc757faa2017-10-06 12:06:01 -07001999 /* Also age clones in the exception table.
2000 * Note, that clones are aged out
2001 * only if they are not in use now.
2002 */
2003 rt6_age_exceptions(rt, gc_args, now);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
2006}
2007
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002008void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Michal Kubeček3570df92016-03-08 14:44:25 +01002010 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002011 unsigned long now;
2012
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002013 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002014 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2015 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002016 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2017 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002019 gc_args.timeout = expires ? (int)expires :
2020 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002021 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002022
Michal Kubeček3570df92016-03-08 14:44:25 +01002023 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002024 now = jiffies;
2025 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002028 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002029 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002030 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002031 else
2032 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002033 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Kees Cook86cb30e2017-10-17 20:21:24 -07002036static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002037{
Kees Cook86cb30e2017-10-17 20:21:24 -07002038 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2039
2040 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002041}
2042
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002043static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002044{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002045 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002046 int err;
2047
2048 err = fib6_notifier_init(net);
2049 if (err)
2050 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002051
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002052 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002053 rwlock_init(&net->ipv6.fib6_walker_lock);
2054 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002055 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002056
Benjamin Theryc5728722008-03-03 23:34:17 -08002057 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2058 if (!net->ipv6.rt6_stats)
2059 goto out_timer;
2060
Eric Dumazet10da66f2010-10-13 08:22:03 +00002061 /* Avoid false sharing : Use at least a full cache line */
2062 size = max_t(size_t, size, L1_CACHE_BYTES);
2063
2064 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002065 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002066 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002067
2068 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2069 GFP_KERNEL);
2070 if (!net->ipv6.fib6_main_tbl)
2071 goto out_fib_table_hash;
2072
2073 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002074 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2075 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002076 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2077 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002078 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002079
2080#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2081 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2082 GFP_KERNEL);
2083 if (!net->ipv6.fib6_local_tbl)
2084 goto out_fib6_main_tbl;
2085 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002086 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2087 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002088 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2089 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002090 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002091#endif
2092 fib6_tables_init(net);
2093
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002094 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002095
2096#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2097out_fib6_main_tbl:
2098 kfree(net->ipv6.fib6_main_tbl);
2099#endif
2100out_fib_table_hash:
2101 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002102out_rt6_stats:
2103 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002104out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002105 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002106 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002107}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002108
2109static void fib6_net_exit(struct net *net)
2110{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002111 unsigned int i;
2112
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002113 del_timer_sync(&net->ipv6.ip6_fib_timer);
2114
Eric Dumazet32a805b2017-09-08 15:48:47 -07002115 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002116 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2117 struct hlist_node *tmp;
2118 struct fib6_table *tb;
2119
2120 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2121 hlist_del(&tb->tb6_hlist);
2122 fib6_free_table(tb);
2123 }
2124 }
2125
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002126 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002127 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002128 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002129}
2130
2131static struct pernet_operations fib6_net_ops = {
2132 .init = fib6_net_init,
2133 .exit = fib6_net_exit,
2134};
2135
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002136int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002138 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2141 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002142 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002143 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002144 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002145 goto out;
2146
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002147 ret = register_pernet_subsys(&fib6_net_ops);
2148 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002149 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002150
Florian Westphal16feebc2017-12-02 21:44:08 +01002151 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2152 inet6_dump_fib, 0);
David S. Millere8803b62012-06-16 01:12:19 -07002153 if (ret)
2154 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002155
2156 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002157out:
2158 return ret;
2159
David S. Millere8803b62012-06-16 01:12:19 -07002160out_unregister_subsys:
2161 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002162out_kmem_cache_create:
2163 kmem_cache_destroy(fib6_node_kmem);
2164 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167void fib6_gc_cleanup(void)
2168{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002169 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 kmem_cache_destroy(fib6_node_kmem);
2171}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002172
2173#ifdef CONFIG_PROC_FS
2174
2175struct ipv6_route_iter {
2176 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002177 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002178 loff_t skip;
2179 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002180 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002181};
2182
2183static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2184{
2185 struct rt6_info *rt = v;
2186 struct ipv6_route_iter *iter = seq->private;
2187
2188 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2189
2190#ifdef CONFIG_IPV6_SUBTREES
2191 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2192#else
2193 seq_puts(seq, "00000000000000000000000000000000 00 ");
2194#endif
2195 if (rt->rt6i_flags & RTF_GATEWAY)
2196 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2197 else
2198 seq_puts(seq, "00000000000000000000000000000000");
2199
2200 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2201 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2202 rt->dst.__use, rt->rt6i_flags,
2203 rt->dst.dev ? rt->dst.dev->name : "");
2204 iter->w.leaf = NULL;
2205 return 0;
2206}
2207
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002208static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002209{
2210 struct ipv6_route_iter *iter = w->args;
2211
2212 if (!iter->skip)
2213 return 1;
2214
2215 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002216 iter->w.leaf = rcu_dereference_protected(
David Miller071fb372017-11-28 15:40:15 -05002217 iter->w.leaf->rt6_next,
Wei Wang66f5d6c2017-10-06 12:06:10 -07002218 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002219 iter->skip--;
2220 if (!iter->skip && iter->w.leaf)
2221 return 1;
2222 } while (iter->w.leaf);
2223
2224 return 0;
2225}
2226
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002227static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2228 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002229{
2230 memset(&iter->w, 0, sizeof(iter->w));
2231 iter->w.func = ipv6_route_yield;
2232 iter->w.root = &iter->tbl->tb6_root;
2233 iter->w.state = FWS_INIT;
2234 iter->w.node = iter->w.root;
2235 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002236 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002237 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002238 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002239}
2240
2241static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2242 struct net *net)
2243{
2244 unsigned int h;
2245 struct hlist_node *node;
2246
2247 if (tbl) {
2248 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2249 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2250 } else {
2251 h = 0;
2252 node = NULL;
2253 }
2254
2255 while (!node && h < FIB6_TABLE_HASHSZ) {
2256 node = rcu_dereference_bh(
2257 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2258 }
2259 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2260}
2261
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002262static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2263{
2264 if (iter->sernum != iter->w.root->fn_sernum) {
2265 iter->sernum = iter->w.root->fn_sernum;
2266 iter->w.state = FWS_INIT;
2267 iter->w.node = iter->w.root;
2268 WARN_ON(iter->w.skip);
2269 iter->w.skip = iter->w.count;
2270 }
2271}
2272
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002273static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2274{
2275 int r;
2276 struct rt6_info *n;
2277 struct net *net = seq_file_net(seq);
2278 struct ipv6_route_iter *iter = seq->private;
2279
2280 if (!v)
2281 goto iter_table;
2282
David Miller071fb372017-11-28 15:40:15 -05002283 n = rcu_dereference_bh(((struct rt6_info *)v)->rt6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002284 if (n) {
2285 ++*pos;
2286 return n;
2287 }
2288
2289iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002290 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002291 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002292 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002293 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002294 if (r > 0) {
2295 if (v)
2296 ++*pos;
2297 return iter->w.leaf;
2298 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002299 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002300 return NULL;
2301 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002302 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002303
2304 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2305 if (!iter->tbl)
2306 return NULL;
2307
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002308 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002309 goto iter_table;
2310}
2311
2312static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2313 __acquires(RCU_BH)
2314{
2315 struct net *net = seq_file_net(seq);
2316 struct ipv6_route_iter *iter = seq->private;
2317
2318 rcu_read_lock_bh();
2319 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2320 iter->skip = *pos;
2321
2322 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002323 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002324 return ipv6_route_seq_next(seq, NULL, pos);
2325 } else {
2326 return NULL;
2327 }
2328}
2329
2330static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2331{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002332 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002333 return w->node && !(w->state == FWS_U && w->node == w->root);
2334}
2335
2336static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2337 __releases(RCU_BH)
2338{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002339 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002340 struct ipv6_route_iter *iter = seq->private;
2341
2342 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002343 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002344
2345 rcu_read_unlock_bh();
2346}
2347
2348static const struct seq_operations ipv6_route_seq_ops = {
2349 .start = ipv6_route_seq_start,
2350 .next = ipv6_route_seq_next,
2351 .stop = ipv6_route_seq_stop,
2352 .show = ipv6_route_seq_show
2353};
2354
2355int ipv6_route_open(struct inode *inode, struct file *file)
2356{
2357 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2358 sizeof(struct ipv6_route_iter));
2359}
2360
2361#endif /* CONFIG_PROC_FS */