blob: 9dcc3924a97561d689a1fd8862742d9543339dd2 [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);
Wei Wang4512c432018-01-08 10:34:00 -0800643 /* remove null_entry in the root node */
644 } else if (fn->fn_flags & RTN_TL_ROOT &&
645 rcu_access_pointer(fn->leaf) ==
646 net->ipv6.ip6_null_entry) {
647 RCU_INIT_POINTER(fn->leaf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return fn;
651 }
652
653 /*
654 * We have more bits to go
655 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dir = addr_bit_set(addr, fn->fn_bit);
659 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700660 fn = dir ?
661 rcu_dereference_protected(fn->right,
662 lockdep_is_held(&table->tb6_lock)) :
663 rcu_dereference_protected(fn->left,
664 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 } while (fn);
666
Matti Vaittinen14df0152011-11-16 21:18:02 +0000667 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000668 /* We should not create new node because
669 * NLM_F_REPLACE was specified without NLM_F_CREATE
670 * I assume it is safe to require NLM_F_CREATE when
671 * REPLACE flag is used! Later we may want to remove the
672 * check for replace_required, because according
673 * to netlink specification, NLM_F_CREATE
674 * MUST be specified if new route is created.
675 * That would keep IPv6 consistent with IPv4
676 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000677 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600678 NL_SET_ERR_MSG(extack,
679 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000680 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000681 return ERR_PTR(-ENOENT);
682 }
Joe Perchesf3213832012-05-15 14:11:53 +0000683 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * We walked to the bottom of tree.
687 * Create new leaf node without children.
688 */
689
Wei Wang81eb8442017-10-06 12:06:11 -0700690 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
David S. Miller507c9b12011-12-03 17:50:45 -0500692 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000693 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700695 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700698 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700700 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 return ln;
703
704
705insert_above:
706 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900707 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * we have a less significant route.
709 * we've to insert an intermediate node on the list
710 * this new node will point to the one we need to create
711 * and the current
712 */
713
Wei Wang66f5d6c2017-10-06 12:06:10 -0700714 pn = rcu_dereference_protected(fn->parent,
715 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* find 1st bit in difference between the 2 addrs.
718
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800719 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 but if it is >= plen, the value is ignored in any case.
721 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900722
fan.du9225b232013-07-22 14:21:09 +0800723 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900725 /*
726 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * / \
728 * (new leaf node)[ln] (old node)[fn]
729 */
730 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700731 in = node_alloc(net);
732 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900733
David S. Miller507c9b12011-12-03 17:50:45 -0500734 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700736 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700738 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000739 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900742 /*
743 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * RTN_RTINFO will
745 * be off since that an address that chooses one of
746 * the branches would not match less specific routes
747 * in the other branch
748 */
749
750 in->fn_bit = bit;
751
Wei Wang66f5d6c2017-10-06 12:06:10 -0700752 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700754 atomic_inc(&rcu_dereference_protected(in->leaf,
755 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* update parent pointer */
758 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700759 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700761 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 ln->fn_bit = plen;
764
Wei Wang66f5d6c2017-10-06 12:06:10 -0700765 RCU_INIT_POINTER(ln->parent, in);
766 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700769 rcu_assign_pointer(in->right, ln);
770 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700772 rcu_assign_pointer(in->left, ln);
773 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 } else { /* plen <= bit */
776
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900777 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * (new leaf node)[ln]
779 * / \
780 * (old node)[fn] NULL
781 */
782
Wei Wang81eb8442017-10-06 12:06:11 -0700783 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
David S. Miller507c9b12011-12-03 17:50:45 -0500785 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000786 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 ln->fn_bit = plen;
789
Wei Wang66f5d6c2017-10-06 12:06:10 -0700790 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700793 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700795 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Wei Wang66f5d6c2017-10-06 12:06:10 -0700797 rcu_assign_pointer(fn->parent, ln);
798
799 if (dir)
800 rcu_assign_pointer(pn->right, ln);
801 else
802 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 return ln;
805}
806
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200807static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200808{
809 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
810 RTF_GATEWAY;
811}
812
Florian Westphale715b6d2015-01-05 23:57:44 +0100813static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100814{
Florian Westphale715b6d2015-01-05 23:57:44 +0100815 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100816
Florian Westphale715b6d2015-01-05 23:57:44 +0100817 for (i = 0; i < RTAX_MAX; i++) {
818 if (test_bit(i, mxc->mx_valid))
819 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100820 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100821}
822
823static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
824{
825 if (!mxc->mx)
826 return 0;
827
828 if (dst->flags & DST_HOST) {
829 u32 *mp = dst_metrics_write_ptr(dst);
830
831 if (unlikely(!mp))
832 return -ENOMEM;
833
834 fib6_copy_metrics(mp, mxc);
835 } else {
836 dst_init_metrics(dst, mxc->mx, false);
837
838 /* We've stolen mx now. */
839 mxc->mx = NULL;
840 }
841
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100842 return 0;
843}
844
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100845static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
846 struct net *net)
847{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700848 struct fib6_table *table = rt->rt6i_table;
849
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100850 if (atomic_read(&rt->rt6i_ref) != 1) {
851 /* This route is used as dummy address holder in some split
852 * nodes. It is not leaked, but it still holds other resources,
853 * which must be released in time. So, scan ascendant nodes
854 * and replace dummy references to this route with references
855 * to still alive ones.
856 */
857 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700858 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
859 lockdep_is_held(&table->tb6_lock));
860 struct rt6_info *new_leaf;
861 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
862 new_leaf = fib6_find_prefix(net, table, fn);
863 atomic_inc(&new_leaf->rt6i_ref);
864 rcu_assign_pointer(fn->leaf, new_leaf);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100865 rt6_release(rt);
866 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700867 fn = rcu_dereference_protected(fn->parent,
868 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100869 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100870 }
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * Insert routing information in a node.
875 */
876
877static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
David Ahern6c31e5a2017-10-27 17:37:13 -0700878 struct nl_info *info, struct mx6_config *mxc,
879 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700881 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
882 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct rt6_info *iter = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700884 struct rt6_info __rcu **ins;
885 struct rt6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500886 int replace = (info->nlh &&
887 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
888 int add = (!info->nlh ||
889 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000890 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200891 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200892 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100893 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
David Ahern1f5e29c2017-01-31 16:51:37 -0800895 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
896 nlflags |= NLM_F_APPEND;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 ins = &fn->leaf;
899
Wei Wang66f5d6c2017-10-06 12:06:10 -0700900 for (iter = leaf; iter;
901 iter = rcu_dereference_protected(iter->dst.rt6_next,
902 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * Search for duplicates
905 */
906
907 if (iter->rt6i_metric == rt->rt6i_metric) {
908 /*
909 * Same priority level
910 */
David S. Miller507c9b12011-12-03 17:50:45 -0500911 if (info->nlh &&
912 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000913 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200914
915 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000916 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200917 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
918 found++;
919 break;
920 }
921 if (rt_can_ecmp)
922 fallback_ins = fallback_ins ?: ins;
923 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
David Ahernf06b7542017-07-05 14:41:46 -0600926 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000927 if (rt->rt6i_nsiblings)
928 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500929 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000931 if (!(rt->rt6i_flags & RTF_EXPIRES))
932 rt6_clean_expires(iter);
933 else
934 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700935 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -EEXIST;
937 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000938 /* If we have the same destination and the same metric,
939 * but not the same gateway, then the route we try to
940 * add is sibling to this route, increment our counter
941 * of siblings, and later we will add our route to the
942 * list.
943 * Only static routes (which don't have flag
944 * RTF_EXPIRES) are used for ECMPv6.
945 *
946 * To avoid long list, we only had siblings if the
947 * route have a gateway.
948 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200949 if (rt_can_ecmp &&
950 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000951 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
954 if (iter->rt6i_metric > rt->rt6i_metric)
955 break;
956
Michal Kubeček27596472015-05-18 20:54:00 +0200957next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700958 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Michal Kubeček27596472015-05-18 20:54:00 +0200961 if (fallback_ins && !found) {
962 /* No ECMP-able route found, replace first non-ECMP one */
963 ins = fallback_ins;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700964 iter = rcu_dereference_protected(*ins,
965 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +0200966 found++;
967 }
968
David S. Millerf11e6652007-03-24 20:36:25 -0700969 /* Reset round-robin state, if necessary */
970 if (ins == &fn->leaf)
971 fn->rr_ptr = NULL;
972
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000973 /* Link this route to others same route. */
974 if (rt->rt6i_nsiblings) {
975 unsigned int rt6i_nsiblings;
976 struct rt6_info *sibling, *temp_sibling;
977
978 /* Find the first route that have the same metric */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700979 sibling = leaf;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000980 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200981 if (sibling->rt6i_metric == rt->rt6i_metric &&
982 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000983 list_add_tail(&rt->rt6i_siblings,
984 &sibling->rt6i_siblings);
985 break;
986 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700987 sibling = rcu_dereference_protected(sibling->dst.rt6_next,
988 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000989 }
990 /* For each sibling in the list, increment the counter of
991 * siblings. BUG() if counters does not match, list of siblings
992 * is broken!
993 */
994 rt6i_nsiblings = 0;
995 list_for_each_entry_safe(sibling, temp_sibling,
996 &rt->rt6i_siblings, rt6i_siblings) {
997 sibling->rt6i_nsiblings++;
998 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
999 rt6i_nsiblings++;
1000 }
1001 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
1002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * insert node
1006 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001007 if (!replace) {
1008 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001009 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001011add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001012 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001013 err = fib6_commit_metrics(&rt->dst, mxc);
1014 if (err)
1015 return err;
1016
Wei Wang66f5d6c2017-10-06 12:06:10 -07001017 rcu_assign_pointer(rt->dst.rt6_next, iter);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001018 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001019 rcu_assign_pointer(rt->rt6i_node, fn);
1020 rcu_assign_pointer(*ins, rt);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001021 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
David Ahern6c31e5a2017-10-27 17:37:13 -07001022 rt, extack);
David Ahern3b1137f2017-02-02 12:37:10 -08001023 if (!info->skip_notify)
1024 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001025 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
David S. Miller507c9b12011-12-03 17:50:45 -05001027 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001028 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1029 fn->fn_flags |= RTN_RTINFO;
1030 }
1031
1032 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001033 int nsiblings;
1034
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001035 if (!found) {
1036 if (add)
1037 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001038 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001039 return -ENOENT;
1040 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001041
1042 err = fib6_commit_metrics(&rt->dst, mxc);
1043 if (err)
1044 return err;
1045
Wei Wang66f5d6c2017-10-06 12:06:10 -07001046 atomic_inc(&rt->rt6i_ref);
Wei Wang4e587ea2017-08-25 15:03:10 -07001047 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001048 rt->dst.rt6_next = iter->dst.rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001049 rcu_assign_pointer(*ins, rt);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001050 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
David Ahern6c31e5a2017-10-27 17:37:13 -07001051 rt, extack);
David Ahern3b1137f2017-02-02 12:37:10 -08001052 if (!info->skip_notify)
1053 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001054 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001055 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1056 fn->fn_flags |= RTN_RTINFO;
1057 }
Michal Kubeček27596472015-05-18 20:54:00 +02001058 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001059 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001060 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001061 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001062 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001063 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001064
1065 if (nsiblings) {
1066 /* Replacing an ECMP route, remove all siblings */
1067 ins = &rt->dst.rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001068 iter = rcu_dereference_protected(*ins,
1069 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001070 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001071 if (iter->rt6i_metric > rt->rt6i_metric)
1072 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001073 if (rt6_qualify_for_ecmp(iter)) {
1074 *ins = iter->dst.rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001075 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001076 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001077 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001078 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001079 rt6_release(iter);
1080 nsiblings--;
Wei Wang81eb8442017-10-06 12:06:11 -07001081 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001082 } else {
1083 ins = &iter->dst.rt6_next;
1084 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001085 iter = rcu_dereference_protected(*ins,
1086 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001087 }
1088 WARN_ON(nsiblings != 0);
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
1092 return 0;
1093}
1094
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001095static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001097 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001098 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001099 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001100 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001103void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001105 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1106 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001107 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Wei Wangbbd63f02017-10-06 12:06:07 -07001110static void fib6_update_sernum_upto_root(struct rt6_info *rt,
1111 int sernum)
1112{
1113 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1114 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1115
1116 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1117 smp_wmb();
1118 while (fn) {
1119 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001120 fn = rcu_dereference_protected(fn->parent,
1121 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001122 }
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
1126 * Add routing information to the routing tree.
1127 * <destination addr>/<source addr>
1128 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001129 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
1131
Florian Westphale715b6d2015-01-05 23:57:44 +01001132int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001133 struct nl_info *info, struct mx6_config *mxc,
1134 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001136 struct fib6_table *table = rt->rt6i_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001137 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001139 int allow_create = 1;
1140 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001141 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001142
Wei Wanga4c2fd72017-06-17 10:42:42 -07001143 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001144 return -EINVAL;
Wei Wang2b760fc2017-10-06 12:06:03 -07001145 if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
1146 return -EINVAL;
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001147
David S. Miller507c9b12011-12-03 17:50:45 -05001148 if (info->nlh) {
1149 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001150 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001151 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001152 replace_required = 1;
1153 }
1154 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001155 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Wei Wang81eb8442017-10-06 12:06:11 -07001157 fn = fib6_add_1(info->nl_net, table, root,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001158 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
fan.du9225b232013-07-22 14:21:09 +08001159 offsetof(struct rt6_info, rt6i_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001160 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001161 if (IS_ERR(fn)) {
1162 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001163 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001167 pn = fn;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#ifdef CONFIG_IPV6_SUBTREES
1170 if (rt->rt6i_src.plen) {
1171 struct fib6_node *sn;
1172
Wei Wang66f5d6c2017-10-06 12:06:10 -07001173 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct fib6_node *sfn;
1175
1176 /*
1177 * Create subtree.
1178 *
1179 * fn[main tree]
1180 * |
1181 * sfn[subtree root]
1182 * \
1183 * sn[new leaf node]
1184 */
1185
1186 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001187 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001188 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001189 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001191 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001192 rcu_assign_pointer(sfn->leaf,
1193 info->nl_net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 /* Now add the first leaf node to new subtree */
1197
Wei Wang81eb8442017-10-06 12:06:11 -07001198 sn = fib6_add_1(info->nl_net, table, sfn,
1199 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001200 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001201 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001203 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001205 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 in main tree.
1207 */
Wei Wang81eb8442017-10-06 12:06:11 -07001208 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001209 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001210 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
1213 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001214 rcu_assign_pointer(sfn->parent, fn);
1215 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001217 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1218 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001219 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001220 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001222 if (IS_ERR(sn)) {
1223 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001224 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Wei Wang66f5d6c2017-10-06 12:06:10 -07001228 if (!rcu_access_pointer(fn->leaf)) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001229 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001230 rcu_assign_pointer(fn->leaf, rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 fn = sn;
1233 }
1234#endif
1235
David Ahern6c31e5a2017-10-27 17:37:13 -07001236 err = fib6_add_rt2node(fn, rt, info, mxc, extack);
Wei Wangbbd63f02017-10-06 12:06:07 -07001237 if (!err) {
1238 fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001239 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001243 if (err) {
1244#ifdef CONFIG_IPV6_SUBTREES
1245 /*
1246 * If fib6_add_1 has cleared the old leaf pointer in the
1247 * super-tree leaf node we have to find a new one for it.
1248 */
Wei Wang7bbfe002018-01-03 14:11:59 -08001249 if (pn != fn) {
1250 struct rt6_info *pn_leaf =
1251 rcu_dereference_protected(pn->leaf,
1252 lockdep_is_held(&table->tb6_lock));
1253 if (pn_leaf == rt) {
1254 pn_leaf = NULL;
1255 RCU_INIT_POINTER(pn->leaf, NULL);
1256 atomic_dec(&rt->rt6i_ref);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001257 }
Wei Wang7bbfe002018-01-03 14:11:59 -08001258 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1259 pn_leaf = fib6_find_prefix(info->nl_net, table,
1260 pn);
1261#if RT6_DEBUG >= 2
1262 if (!pn_leaf) {
1263 WARN_ON(!pn_leaf);
1264 pn_leaf =
1265 info->nl_net->ipv6.ip6_null_entry;
1266 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001267#endif
Wei Wang7bbfe002018-01-03 14:11:59 -08001268 atomic_inc(&pn_leaf->rt6i_ref);
1269 rcu_assign_pointer(pn->leaf, pn_leaf);
1270 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001271 }
1272#endif
Wei Wang348a4002017-08-18 17:14:49 -07001273 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return err;
1276
Wei Wang348a4002017-08-18 17:14:49 -07001277failure:
Wei Wang4512c432018-01-08 10:34:00 -08001278 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1279 * 1. fn is an intermediate node and we failed to add the new
1280 * route to it in both subtree creation failure and fib6_add_rt2node()
1281 * failure case.
1282 * 2. fn is the root node in the table and we fail to add the first
1283 * default route to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
Wei Wang4512c432018-01-08 10:34:00 -08001285 if (fn &&
1286 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1287 (fn->fn_flags & RTN_TL_ROOT &&
1288 !rcu_access_pointer(fn->leaf))))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001289 fib6_repair_tree(info->nl_net, table, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001290 /* Always release dst as dst->__refcnt is guaranteed
1291 * to be taken before entering this function
1292 */
Wei Wang587fea72017-06-17 10:42:36 -07001293 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297/*
1298 * Routing tree lookup
1299 *
1300 */
1301
1302struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001303 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001304 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305};
1306
Wang Yufen437de072014-03-28 12:07:04 +08001307static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1308 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001311 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001313 if (unlikely(args->offset == 0))
1314 return NULL;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /*
1317 * Descend on a tree
1318 */
1319
1320 fn = root;
1321
1322 for (;;) {
1323 struct fib6_node *next;
1324
1325 dir = addr_bit_set(args->addr, fn->fn_bit);
1326
Wei Wang66f5d6c2017-10-06 12:06:10 -07001327 next = dir ? rcu_dereference(fn->right) :
1328 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (next) {
1331 fn = next;
1332 continue;
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335 }
1336
David S. Miller507c9b12011-12-03 17:50:45 -05001337 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001338 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1339
1340 if (subtree || fn->fn_flags & RTN_RTINFO) {
1341 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct rt6key *key;
1343
Wei Wang8d1040e2017-10-06 12:06:08 -07001344 if (!leaf)
1345 goto backtrack;
1346
1347 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001349 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1350#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001351 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001352 struct fib6_node *sfn;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001353 sfn = fib6_lookup_1(subtree, args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001354 if (!sfn)
1355 goto backtrack;
1356 fn = sfn;
1357 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001358#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001359 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001360 return fn;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001363backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001364 if (fn->fn_flags & RTN_ROOT)
1365 break;
1366
Wei Wang66f5d6c2017-10-06 12:06:10 -07001367 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
1370 return NULL;
1371}
1372
Wei Wang66f5d6c2017-10-06 12:06:10 -07001373/* called with rcu_read_lock() held
1374 */
Wang Yufen437de072014-03-28 12:07:04 +08001375struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1376 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001379 struct lookup_args args[] = {
1380 {
1381 .offset = offsetof(struct rt6_info, rt6i_dst),
1382 .addr = daddr,
1383 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001385 {
1386 .offset = offsetof(struct rt6_info, rt6i_src),
1387 .addr = saddr,
1388 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001390 {
1391 .offset = 0, /* sentinel */
1392 }
1393 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001395 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001396 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 fn = root;
1398
1399 return fn;
1400}
1401
1402/*
1403 * Get node with specified destination prefix (and source prefix,
1404 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001405 * exact_match == true means we try to find fn with exact match of
1406 * the passed in prefix addr
1407 * exact_match == false means we try to find fn with longest prefix
1408 * match of the passed in prefix addr. This is useful for finding fn
1409 * for cached route as it will be stored in the exception table under
1410 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
1412
1413
Wang Yufen437de072014-03-28 12:07:04 +08001414static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1415 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001416 int plen, int offset,
1417 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Wei Wang38fbeee2017-10-06 12:06:02 -07001419 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 for (fn = root; fn ; ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001422 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001423 struct rt6key *key;
1424
1425 /* This node is being deleted */
1426 if (!leaf) {
1427 if (plen <= fn->fn_bit)
1428 goto out;
1429 else
1430 goto next;
1431 }
1432
1433 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
1436 * Prefix match
1437 */
1438 if (plen < fn->fn_bit ||
1439 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (plen == fn->fn_bit)
1443 return fn;
1444
Wei Wang38fbeee2017-10-06 12:06:02 -07001445 prev = fn;
1446
Wei Wang8d1040e2017-10-06 12:06:08 -07001447next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /*
1449 * We have more bits to go
1450 */
1451 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001452 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001454 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001456out:
1457 if (exact_match)
1458 return NULL;
1459 else
1460 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Wang Yufen437de072014-03-28 12:07:04 +08001463struct fib6_node *fib6_locate(struct fib6_node *root,
1464 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001465 const struct in6_addr *saddr, int src_len,
1466 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct fib6_node *fn;
1469
1470 fn = fib6_locate_1(root, daddr, dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001471 offsetof(struct rt6_info, rt6i_dst),
1472 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474#ifdef CONFIG_IPV6_SUBTREES
1475 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001476 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001477 if (fn) {
1478 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1479
1480 if (subtree) {
1481 fn = fib6_locate_1(subtree, saddr, src_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001482 offsetof(struct rt6_info, rt6i_src),
1483 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001484 }
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487#endif
1488
David S. Miller507c9b12011-12-03 17:50:45 -05001489 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return fn;
1491
1492 return NULL;
1493}
1494
1495
1496/*
1497 * Deletion
1498 *
1499 */
1500
Wei Wang66f5d6c2017-10-06 12:06:10 -07001501static struct rt6_info *fib6_find_prefix(struct net *net,
1502 struct fib6_table *table,
1503 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001505 struct fib6_node *child_left, *child_right;
1506
David S. Miller507c9b12011-12-03 17:50:45 -05001507 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001508 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
David S. Miller507c9b12011-12-03 17:50:45 -05001510 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001511 child_left = rcu_dereference_protected(fn->left,
1512 lockdep_is_held(&table->tb6_lock));
1513 child_right = rcu_dereference_protected(fn->right,
1514 lockdep_is_held(&table->tb6_lock));
1515 if (child_left)
1516 return rcu_dereference_protected(child_left->leaf,
1517 lockdep_is_held(&table->tb6_lock));
1518 if (child_right)
1519 return rcu_dereference_protected(child_right->leaf,
1520 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001522 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 return NULL;
1525}
1526
1527/*
1528 * Called to trim the tree of intermediate nodes when possible. "fn"
1529 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001530 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
1532
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001533static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001534 struct fib6_table *table,
1535 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 int children;
1538 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001539 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001540 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int iter = 0;
1542
Wei Wang4512c432018-01-08 10:34:00 -08001543 /* Set fn->leaf to null_entry for root node. */
1544 if (fn->fn_flags & RTN_TL_ROOT) {
1545 rcu_assign_pointer(fn->leaf, net->ipv6.ip6_null_entry);
1546 return fn;
1547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001550 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1551 lockdep_is_held(&table->tb6_lock));
1552 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1553 lockdep_is_held(&table->tb6_lock));
1554 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1555 lockdep_is_held(&table->tb6_lock));
1556 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1557 lockdep_is_held(&table->tb6_lock));
1558 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1559 lockdep_is_held(&table->tb6_lock));
1560 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1561 lockdep_is_held(&table->tb6_lock));
1562 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1563 lockdep_is_held(&table->tb6_lock));
1564 struct rt6_info *new_fn_leaf;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1567 iter++;
1568
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001569 WARN_ON(fn->fn_flags & RTN_RTINFO);
1570 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001571 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 children = 0;
1574 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001575 if (fn_r)
1576 child = fn_r, children |= 1;
1577 if (fn_l)
1578 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001580 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581#ifdef CONFIG_IPV6_SUBTREES
1582 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001583 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584#endif
1585 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001586 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001588 if (!new_fn_leaf) {
1589 WARN_ON(!new_fn_leaf);
1590 new_fn_leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001593 atomic_inc(&new_fn_leaf->rt6i_ref);
1594 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1595 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001599 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001600 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001601 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 nstate = FWS_L;
1603 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001604 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001606 if (pn_r == fn)
1607 rcu_assign_pointer(pn->right, child);
1608 else if (pn_l == fn)
1609 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001611 else
1612 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#endif
1614 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001615 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 nstate = FWS_R;
1617#ifdef CONFIG_IPV6_SUBTREES
1618 }
1619#endif
1620
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001621 read_lock(&net->ipv6.fib6_walker_lock);
1622 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001623 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001624 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1626 w->node = pn;
1627 w->state = nstate;
1628 }
1629 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (w->node == fn) {
1631 w->node = child;
1632 if (children&2) {
1633 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001634 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 } else {
1636 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001637 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 }
1640 }
1641 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001642 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Wei Wang81eb8442017-10-06 12:06:11 -07001644 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001645 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return pn;
1647
Wei Wang66f5d6c2017-10-06 12:06:10 -07001648 RCU_INIT_POINTER(pn->leaf, NULL);
1649 rt6_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 fn = pn;
1651 }
1652}
1653
Wei Wang66f5d6c2017-10-06 12:06:10 -07001654static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1655 struct rt6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001657 struct fib6_walker *w;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001658 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1659 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001660 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 RT6_TRACE("fib6_del_route\n");
1663
Wei Wang2b760fc2017-10-06 12:06:03 -07001664 WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001667 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001669 net->ipv6.rt6_stats->fib_rt_entries--;
1670 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Wei Wang2b760fc2017-10-06 12:06:03 -07001672 /* Flush all cached dst in exception table */
1673 rt6_flush_exceptions(rt);
1674
David S. Millerf11e6652007-03-24 20:36:25 -07001675 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001676 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001677 fn->rr_ptr = NULL;
1678
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001679 /* Remove this entry from other siblings */
1680 if (rt->rt6i_nsiblings) {
1681 struct rt6_info *sibling, *next_sibling;
1682
1683 list_for_each_entry_safe(sibling, next_sibling,
1684 &rt->rt6i_siblings, rt6i_siblings)
1685 sibling->rt6i_nsiblings--;
1686 rt->rt6i_nsiblings = 0;
1687 list_del_init(&rt->rt6i_siblings);
1688 }
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001691 read_lock(&net->ipv6.fib6_walker_lock);
1692 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (w->state == FWS_C && w->leaf == rt) {
1694 RT6_TRACE("walker %p adjusted by delroute\n", w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001695 w->leaf = rcu_dereference_protected(rt->dst.rt6_next,
1696 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001697 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 w->state = FWS_U;
1699 }
1700 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001701 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Wei Wang4512c432018-01-08 10:34:00 -08001703 /* If it was last route, call fib6_repair_tree() to:
1704 * 1. For root node, put back null_entry as how the table was created.
1705 * 2. For other nodes, expunge its radix tree node.
1706 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001707 if (!rcu_access_pointer(fn->leaf)) {
Wei Wang4512c432018-01-08 10:34:00 -08001708 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1709 fn->fn_flags &= ~RTN_RTINFO;
1710 net->ipv6.rt6_stats->fib_route_nodes--;
1711 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001712 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001715 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
David Ahern6c31e5a2017-10-27 17:37:13 -07001717 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
David Ahern16a16cd2017-02-02 12:37:11 -08001718 if (!info->skip_notify)
1719 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rt6_release(rt);
1721}
1722
Wei Wang66f5d6c2017-10-06 12:06:10 -07001723/* Need to own table->tb6_lock */
Thomas Graf86872cb2006-08-22 00:01:08 -07001724int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Wei Wang4e587ea2017-08-25 15:03:10 -07001726 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1727 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001728 struct fib6_table *table = rt->rt6i_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001729 struct net *net = info->nl_net;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001730 struct rt6_info __rcu **rtp;
1731 struct rt6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001734 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001735 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return -ENOENT;
1737 }
1738#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001739 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -ENOENT;
1741
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001742 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Wei Wang2b760fc2017-10-06 12:06:03 -07001744 /* remove cached dst from exception table */
1745 if (rt->rt6i_flags & RTF_CACHE)
1746 return rt6_remove_exception_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /*
1749 * Walk the leaf entries looking for ourself
1750 */
1751
Wei Wang66f5d6c2017-10-06 12:06:10 -07001752 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1753 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1754 lockdep_is_held(&table->tb6_lock));
1755 if (rt == cur) {
1756 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
1758 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001759 rtp_next = &cur->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 return -ENOENT;
1762}
1763
1764/*
1765 * Tree traversal function.
1766 *
1767 * Certainly, it is not interrupt safe.
1768 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1769 * It means, that we can modify tree during walking
1770 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001771 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *
1773 * It guarantees that every node will be traversed,
1774 * and that it will be traversed only once.
1775 *
1776 * Callback function w->func may return:
1777 * 0 -> continue walking.
1778 * positive value -> walking is suspended (used by tree dumps,
1779 * and probably by gc, if it will be split to several slices)
1780 * negative value -> terminate walking.
1781 *
1782 * The function itself returns:
1783 * 0 -> walk is complete.
1784 * >0 -> walk is incomplete (i.e. suspended)
1785 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001786 *
1787 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 */
1789
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001790static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001792 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Wei Wang2b760fc2017-10-06 12:06:03 -07001794 /* w->root should always be table->tb6_root */
1795 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 for (;;) {
1798 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001799 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return 0;
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 switch (w->state) {
1803#ifdef CONFIG_IPV6_SUBTREES
1804 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001805 if (FIB6_SUBTREE(fn)) {
1806 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 continue;
1808 }
1809 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001810#endif
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001811 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001813 left = rcu_dereference_protected(fn->left, 1);
1814 if (left) {
1815 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 w->state = FWS_INIT;
1817 continue;
1818 }
1819 w->state = FWS_R;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001820 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001822 right = rcu_dereference_protected(fn->right, 1);
1823 if (right) {
1824 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 w->state = FWS_INIT;
1826 continue;
1827 }
1828 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001829 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001830 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001832 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001833 int err;
1834
Eric Dumazetfa809e22012-06-25 15:37:19 -07001835 if (w->skip) {
1836 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001837 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001838 }
1839
1840 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (err)
1842 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001843
1844 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 continue;
1846 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001847skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 w->state = FWS_U;
Gustavo A. R. Silva275757e62017-10-16 16:36:52 -05001849 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 case FWS_U:
1851 if (fn == w->root)
1852 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001853 pn = rcu_dereference_protected(fn->parent, 1);
1854 left = rcu_dereference_protected(pn->left, 1);
1855 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 w->node = pn;
1857#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001858 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001859 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 w->state = FWS_L;
1861 continue;
1862 }
1863#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001864 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 w->state = FWS_R;
1866 continue;
1867 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001868 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001870 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 continue;
1872 }
1873#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001874 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875#endif
1876 }
1877 }
1878}
1879
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001880static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 int res;
1883
1884 w->state = FWS_INIT;
1885 w->node = w->root;
1886
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001887 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 res = fib6_walk_continue(w);
1889 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001890 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return res;
1892}
1893
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001894static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 int res;
1897 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001898 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001899 struct nl_info info = {
1900 .nl_net = c->net,
1901 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001903 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1904 w->node->fn_sernum != c->sernum)
1905 w->node->fn_sernum = c->sernum;
1906
1907 if (!c->func) {
1908 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1909 w->leaf = NULL;
1910 return 0;
1911 }
1912
Wei Wang66f5d6c2017-10-06 12:06:10 -07001913 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 res = c->func(rt, c->arg);
1915 if (res < 0) {
1916 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001917 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (res) {
1919#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001920 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001921 __func__, rt,
1922 rcu_access_pointer(rt->rt6i_node),
1923 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924#endif
1925 continue;
1926 }
1927 return 0;
1928 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001929 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931 w->leaf = rt;
1932 return 0;
1933}
1934
1935/*
1936 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001937 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 * func is called on each route.
1939 * It may return -1 -> delete this route.
1940 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
1942
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001943static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001944 int (*func)(struct rt6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001945 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001947 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 c.w.root = root;
1950 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001951 c.w.count = 0;
1952 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001954 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001956 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001958 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001961static void __fib6_clean_all(struct net *net,
1962 int (*func)(struct rt6_info *, void *),
1963 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001964{
Thomas Grafc71099a2006-08-04 23:20:06 -07001965 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001966 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001967 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001968
Patrick McHardy1b43af52006-08-10 23:11:17 -07001969 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001970 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001971 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001972 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001973 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001974 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07001975 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001976 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001977 }
1978 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001979 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001980}
1981
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001982void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1983 void *arg)
1984{
1985 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1986}
1987
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001988static void fib6_flush_trees(struct net *net)
1989{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001990 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001991
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001992 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001993}
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995/*
1996 * Garbage collection
1997 */
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999static int fib6_age(struct rt6_info *rt, void *arg)
2000{
Michal Kubeček3570df92016-03-08 14:44:25 +01002001 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 unsigned long now = jiffies;
2003
2004 /*
2005 * check addrconf expiration here.
2006 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
2008
David S. Millerd1918542011-12-28 20:19:20 -05002009 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
2010 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return -1;
2013 }
Michal Kubeček3570df92016-03-08 14:44:25 +01002014 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
Wei Wangc757faa2017-10-06 12:06:01 -07002017 /* Also age clones in the exception table.
2018 * Note, that clones are aged out
2019 * only if they are not in use now.
2020 */
2021 rt6_age_exceptions(rt, gc_args, now);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return 0;
2024}
2025
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002026void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Michal Kubeček3570df92016-03-08 14:44:25 +01002028 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02002029 unsigned long now;
2030
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002031 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002032 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2033 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002034 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002037 gc_args.timeout = expires ? (int)expires :
2038 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002039 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002040
Michal Kubeček3570df92016-03-08 14:44:25 +01002041 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002042 now = jiffies;
2043 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002046 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002047 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002048 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002049 else
2050 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002051 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Kees Cook86cb30e2017-10-17 20:21:24 -07002054static void fib6_gc_timer_cb(struct timer_list *t)
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002055{
Kees Cook86cb30e2017-10-17 20:21:24 -07002056 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2057
2058 fib6_run_gc(0, arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002059}
2060
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002061static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002062{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002063 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002064 int err;
2065
2066 err = fib6_notifier_init(net);
2067 if (err)
2068 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002069
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002070 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002071 rwlock_init(&net->ipv6.fib6_walker_lock);
2072 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Kees Cook86cb30e2017-10-17 20:21:24 -07002073 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002074
Benjamin Theryc5728722008-03-03 23:34:17 -08002075 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2076 if (!net->ipv6.rt6_stats)
2077 goto out_timer;
2078
Eric Dumazet10da66f2010-10-13 08:22:03 +00002079 /* Avoid false sharing : Use at least a full cache line */
2080 size = max_t(size_t, size, L1_CACHE_BYTES);
2081
2082 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002083 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002084 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002085
2086 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2087 GFP_KERNEL);
2088 if (!net->ipv6.fib6_main_tbl)
2089 goto out_fib_table_hash;
2090
2091 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002092 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2093 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002094 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2095 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002096 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002097
2098#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2099 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2100 GFP_KERNEL);
2101 if (!net->ipv6.fib6_local_tbl)
2102 goto out_fib6_main_tbl;
2103 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002104 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2105 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002106 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2107 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002108 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002109#endif
2110 fib6_tables_init(net);
2111
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002112 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002113
2114#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2115out_fib6_main_tbl:
2116 kfree(net->ipv6.fib6_main_tbl);
2117#endif
2118out_fib_table_hash:
2119 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002120out_rt6_stats:
2121 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002122out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002123 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002124 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002125}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002126
2127static void fib6_net_exit(struct net *net)
2128{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002129 unsigned int i;
2130
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002131 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002132 del_timer_sync(&net->ipv6.ip6_fib_timer);
2133
Eric Dumazet32a805b2017-09-08 15:48:47 -07002134 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002135 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2136 struct hlist_node *tmp;
2137 struct fib6_table *tb;
2138
2139 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2140 hlist_del(&tb->tb6_hlist);
2141 fib6_free_table(tb);
2142 }
2143 }
2144
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002145 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002146 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002147 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002148}
2149
2150static struct pernet_operations fib6_net_ops = {
2151 .init = fib6_net_init,
2152 .exit = fib6_net_exit,
2153};
2154
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002155int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002157 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2160 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002161 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002162 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002163 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002164 goto out;
2165
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002166 ret = register_pernet_subsys(&fib6_net_ops);
2167 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002168 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002169
2170 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
Florian Westphalb97bac62017-08-09 20:41:48 +02002171 0);
David S. Millere8803b62012-06-16 01:12:19 -07002172 if (ret)
2173 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002174
2175 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002176out:
2177 return ret;
2178
David S. Millere8803b62012-06-16 01:12:19 -07002179out_unregister_subsys:
2180 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002181out_kmem_cache_create:
2182 kmem_cache_destroy(fib6_node_kmem);
2183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
2186void fib6_gc_cleanup(void)
2187{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002188 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 kmem_cache_destroy(fib6_node_kmem);
2190}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002191
2192#ifdef CONFIG_PROC_FS
2193
2194struct ipv6_route_iter {
2195 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002196 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002197 loff_t skip;
2198 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002199 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002200};
2201
2202static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2203{
2204 struct rt6_info *rt = v;
2205 struct ipv6_route_iter *iter = seq->private;
2206
2207 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2208
2209#ifdef CONFIG_IPV6_SUBTREES
2210 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2211#else
2212 seq_puts(seq, "00000000000000000000000000000000 00 ");
2213#endif
2214 if (rt->rt6i_flags & RTF_GATEWAY)
2215 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2216 else
2217 seq_puts(seq, "00000000000000000000000000000000");
2218
2219 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2220 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2221 rt->dst.__use, rt->rt6i_flags,
2222 rt->dst.dev ? rt->dst.dev->name : "");
2223 iter->w.leaf = NULL;
2224 return 0;
2225}
2226
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002227static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002228{
2229 struct ipv6_route_iter *iter = w->args;
2230
2231 if (!iter->skip)
2232 return 1;
2233
2234 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002235 iter->w.leaf = rcu_dereference_protected(
2236 iter->w.leaf->dst.rt6_next,
2237 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002238 iter->skip--;
2239 if (!iter->skip && iter->w.leaf)
2240 return 1;
2241 } while (iter->w.leaf);
2242
2243 return 0;
2244}
2245
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002246static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2247 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002248{
2249 memset(&iter->w, 0, sizeof(iter->w));
2250 iter->w.func = ipv6_route_yield;
2251 iter->w.root = &iter->tbl->tb6_root;
2252 iter->w.state = FWS_INIT;
2253 iter->w.node = iter->w.root;
2254 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002255 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002256 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002257 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002258}
2259
2260static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2261 struct net *net)
2262{
2263 unsigned int h;
2264 struct hlist_node *node;
2265
2266 if (tbl) {
2267 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2268 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2269 } else {
2270 h = 0;
2271 node = NULL;
2272 }
2273
2274 while (!node && h < FIB6_TABLE_HASHSZ) {
2275 node = rcu_dereference_bh(
2276 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2277 }
2278 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2279}
2280
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002281static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2282{
2283 if (iter->sernum != iter->w.root->fn_sernum) {
2284 iter->sernum = iter->w.root->fn_sernum;
2285 iter->w.state = FWS_INIT;
2286 iter->w.node = iter->w.root;
2287 WARN_ON(iter->w.skip);
2288 iter->w.skip = iter->w.count;
2289 }
2290}
2291
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002292static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2293{
2294 int r;
2295 struct rt6_info *n;
2296 struct net *net = seq_file_net(seq);
2297 struct ipv6_route_iter *iter = seq->private;
2298
2299 if (!v)
2300 goto iter_table;
2301
Wei Wangd0e60202017-10-09 17:17:26 -07002302 n = rcu_dereference_bh(((struct rt6_info *)v)->dst.rt6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002303 if (n) {
2304 ++*pos;
2305 return n;
2306 }
2307
2308iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002309 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002310 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002311 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002312 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002313 if (r > 0) {
2314 if (v)
2315 ++*pos;
2316 return iter->w.leaf;
2317 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002318 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002319 return NULL;
2320 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002321 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002322
2323 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2324 if (!iter->tbl)
2325 return NULL;
2326
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002327 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002328 goto iter_table;
2329}
2330
2331static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2332 __acquires(RCU_BH)
2333{
2334 struct net *net = seq_file_net(seq);
2335 struct ipv6_route_iter *iter = seq->private;
2336
2337 rcu_read_lock_bh();
2338 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2339 iter->skip = *pos;
2340
2341 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002342 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002343 return ipv6_route_seq_next(seq, NULL, pos);
2344 } else {
2345 return NULL;
2346 }
2347}
2348
2349static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2350{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002351 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002352 return w->node && !(w->state == FWS_U && w->node == w->root);
2353}
2354
2355static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2356 __releases(RCU_BH)
2357{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002358 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002359 struct ipv6_route_iter *iter = seq->private;
2360
2361 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002362 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002363
2364 rcu_read_unlock_bh();
2365}
2366
2367static const struct seq_operations ipv6_route_seq_ops = {
2368 .start = ipv6_route_seq_start,
2369 .next = ipv6_route_seq_next,
2370 .stop = ipv6_route_seq_stop,
2371 .show = ipv6_route_seq_show
2372};
2373
2374int ipv6_route_open(struct inode *inode, struct file *file)
2375{
2376 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2377 sizeof(struct ipv6_route_iter));
2378}
2379
2380#endif /* CONFIG_PROC_FS */