blob: 548af48212fcb575e8e57a8047ffa06d4ca07851 [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
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080073static void fib6_gc_timer_cb(unsigned long arg);
74
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,
356 struct rt6_info *rt)
357{
358 struct fib6_entry_notifier_info info = {
359 .rt = rt,
360 };
361
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200362 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200363 return call_fib6_notifiers(net, event_type, &info.info);
364}
365
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200366struct fib6_dump_arg {
367 struct net *net;
368 struct notifier_block *nb;
369};
370
371static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
372{
373 if (rt == arg->net->ipv6.ip6_null_entry)
374 return;
375 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
376}
377
378static int fib6_node_dump(struct fib6_walker *w)
379{
380 struct rt6_info *rt;
381
Wei Wang66f5d6c2017-10-06 12:06:10 -0700382 for_each_fib6_walker_rt(w)
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200383 fib6_rt_dump(rt, w->args);
384 w->leaf = NULL;
385 return 0;
386}
387
388static void fib6_table_dump(struct net *net, struct fib6_table *tb,
389 struct fib6_walker *w)
390{
391 w->root = &tb->tb6_root;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700392 spin_lock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200393 fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700394 spin_unlock_bh(&tb->tb6_lock);
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200395}
396
397/* Called with rcu_read_lock() */
398int fib6_tables_dump(struct net *net, struct notifier_block *nb)
399{
400 struct fib6_dump_arg arg;
401 struct fib6_walker *w;
402 unsigned int h;
403
404 w = kzalloc(sizeof(*w), GFP_ATOMIC);
405 if (!w)
406 return -ENOMEM;
407
408 w->func = fib6_node_dump;
409 arg.net = net;
410 arg.nb = nb;
411 w->args = &arg;
412
413 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
414 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
415 struct fib6_table *tb;
416
417 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
418 fib6_table_dump(net, tb, w);
419 }
420
421 kfree(w);
422
423 return 0;
424}
425
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200426static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700427{
428 int res;
429 struct rt6_info *rt;
430
Wei Wang66f5d6c2017-10-06 12:06:10 -0700431 for_each_fib6_walker_rt(w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700432 res = rt6_dump_route(rt, w->args);
433 if (res < 0) {
434 /* Frame is full, suspend walking */
435 w->leaf = rt;
436 return 1;
437 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800438
439 /* Multipath routes are dumped in one route with the
440 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
441 * last sibling of this route (no need to dump the
442 * sibling routes again)
443 */
444 if (rt->rt6i_nsiblings)
445 rt = list_last_entry(&rt->rt6i_siblings,
446 struct rt6_info,
447 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700448 }
449 w->leaf = NULL;
450 return 0;
451}
452
453static void fib6_dump_end(struct netlink_callback *cb)
454{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100455 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200456 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700457
458 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800459 if (cb->args[4]) {
460 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100461 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800462 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700463 cb->args[2] = 0;
464 kfree(w);
465 }
Wang Yufen437de072014-03-28 12:07:04 +0800466 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700467 cb->args[1] = 3;
468}
469
470static int fib6_dump_done(struct netlink_callback *cb)
471{
472 fib6_dump_end(cb);
473 return cb->done ? cb->done(cb) : 0;
474}
475
476static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
477 struct netlink_callback *cb)
478{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100479 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200480 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700481 int res;
482
483 w = (void *)cb->args[2];
484 w->root = &table->tb6_root;
485
486 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000487 w->count = 0;
488 w->skip = 0;
489
Wei Wang66f5d6c2017-10-06 12:06:10 -0700490 spin_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100491 res = fib6_walk(net, w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700492 spin_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000493 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700494 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000495 cb->args[5] = w->root->fn_sernum;
496 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700497 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000498 if (cb->args[5] != w->root->fn_sernum) {
499 /* Begin at the root if the tree changed */
500 cb->args[5] = w->root->fn_sernum;
501 w->state = FWS_INIT;
502 w->node = w->root;
503 w->skip = w->count;
504 } else
505 w->skip = 0;
506
Wei Wang66f5d6c2017-10-06 12:06:10 -0700507 spin_lock_bh(&table->tb6_lock);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700508 res = fib6_walk_continue(w);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700509 spin_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800510 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100511 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800512 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700513 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700514 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800515
Patrick McHardy1b43af52006-08-10 23:11:17 -0700516 return res;
517}
518
Thomas Grafc127ea22007-03-22 11:58:32 -0700519static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700520{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900521 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700522 unsigned int h, s_h;
523 unsigned int e = 0, s_e;
524 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200525 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700526 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800527 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700528 int res = 0;
529
530 s_h = cb->args[0];
531 s_e = cb->args[1];
532
533 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500534 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700535 /* New dump:
536 *
537 * 1. hook callback destructor.
538 */
539 cb->args[3] = (long)cb->done;
540 cb->done = fib6_dump_done;
541
542 /*
543 * 2. allocate and initialize walker.
544 */
545 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500546 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700547 return -ENOMEM;
548 w->func = fib6_dump_node;
549 cb->args[2] = (long)w;
550 }
551
552 arg.skb = skb;
553 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700554 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700555 w->args = &arg;
556
Eric Dumazete67f88d2011-04-27 22:56:07 +0000557 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700558 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700559 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800560 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800561 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700562 if (e < s_e)
563 goto next;
564 res = fib6_dump_table(tb, skb, cb);
565 if (res != 0)
566 goto out;
567next:
568 e++;
569 }
570 }
571out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000572 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700573 cb->args[1] = e;
574 cb->args[0] = h;
575
576 res = res < 0 ? res : skb->len;
577 if (res <= 0)
578 fib6_dump_end(cb);
579 return res;
580}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582/*
583 * Routing Table
584 *
585 * return the appropriate node for a routing tree "add" operation
586 * by either creating and inserting or by returning an existing
587 * node.
588 */
589
Wei Wang81eb8442017-10-06 12:06:11 -0700590static struct fib6_node *fib6_add_1(struct net *net,
591 struct fib6_table *table,
Wei Wang66f5d6c2017-10-06 12:06:10 -0700592 struct fib6_node *root,
593 struct in6_addr *addr, int plen,
594 int offset, int allow_create,
595 int replace_required,
596 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct fib6_node *fn, *in, *ln;
599 struct fib6_node *pn = NULL;
600 struct rt6key *key;
601 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900602 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 RT6_TRACE("fib6_add_1\n");
605
606 /* insert node in tree */
607
608 fn = root;
609
610 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700611 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
612 lockdep_is_held(&table->tb6_lock));
613 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /*
616 * Prefix match
617 */
618 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000619 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000620 if (!allow_create) {
621 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600622 NL_SET_ERR_MSG(extack,
623 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000624 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000625 return ERR_PTR(-ENOENT);
626 }
Joe Perchesf3213832012-05-15 14:11:53 +0000627 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000630 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Exact match ?
634 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (plen == fn->fn_bit) {
637 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500638 if (!(fn->fn_flags & RTN_RTINFO)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700639 RCU_INIT_POINTER(fn->leaf, NULL);
640 rt6_release(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return fn;
644 }
645
646 /*
647 * We have more bits to go
648 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* Try to walk down on tree. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 dir = addr_bit_set(addr, fn->fn_bit);
652 pn = fn;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700653 fn = dir ?
654 rcu_dereference_protected(fn->right,
655 lockdep_is_held(&table->tb6_lock)) :
656 rcu_dereference_protected(fn->left,
657 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 } while (fn);
659
Matti Vaittinen14df0152011-11-16 21:18:02 +0000660 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000661 /* We should not create new node because
662 * NLM_F_REPLACE was specified without NLM_F_CREATE
663 * I assume it is safe to require NLM_F_CREATE when
664 * REPLACE flag is used! Later we may want to remove the
665 * check for replace_required, because according
666 * to netlink specification, NLM_F_CREATE
667 * MUST be specified if new route is created.
668 * That would keep IPv6 consistent with IPv4
669 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000670 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600671 NL_SET_ERR_MSG(extack,
672 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000673 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000674 return ERR_PTR(-ENOENT);
675 }
Joe Perchesf3213832012-05-15 14:11:53 +0000676 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /*
679 * We walked to the bottom of tree.
680 * Create new leaf node without children.
681 */
682
Wei Wang81eb8442017-10-06 12:06:11 -0700683 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
David S. Miller507c9b12011-12-03 17:50:45 -0500685 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000686 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ln->fn_bit = plen;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700688 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700691 rcu_assign_pointer(pn->right, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700693 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return ln;
696
697
698insert_above:
699 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900700 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * we have a less significant route.
702 * we've to insert an intermediate node on the list
703 * this new node will point to the one we need to create
704 * and the current
705 */
706
Wei Wang66f5d6c2017-10-06 12:06:10 -0700707 pn = rcu_dereference_protected(fn->parent,
708 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /* find 1st bit in difference between the 2 addrs.
711
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800712 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 but if it is >= plen, the value is ignored in any case.
714 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900715
fan.du9225b232013-07-22 14:21:09 +0800716 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900718 /*
719 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * / \
721 * (new leaf node)[ln] (old node)[fn]
722 */
723 if (plen > bit) {
Wei Wang81eb8442017-10-06 12:06:11 -0700724 in = node_alloc(net);
725 ln = node_alloc(net);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900726
David S. Miller507c9b12011-12-03 17:50:45 -0500727 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (in)
Wei Wang81eb8442017-10-06 12:06:11 -0700729 node_free_immediate(net, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (ln)
Wei Wang81eb8442017-10-06 12:06:11 -0700731 node_free_immediate(net, ln);
Lin Ming188c5172012-09-25 15:17:07 +0000732 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900735 /*
736 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * RTN_RTINFO will
738 * be off since that an address that chooses one of
739 * the branches would not match less specific routes
740 * in the other branch
741 */
742
743 in->fn_bit = bit;
744
Wei Wang66f5d6c2017-10-06 12:06:10 -0700745 RCU_INIT_POINTER(in->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 in->leaf = fn->leaf;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700747 atomic_inc(&rcu_dereference_protected(in->leaf,
748 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* update parent pointer */
751 if (dir)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700752 rcu_assign_pointer(pn->right, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700754 rcu_assign_pointer(pn->left, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 ln->fn_bit = plen;
757
Wei Wang66f5d6c2017-10-06 12:06:10 -0700758 RCU_INIT_POINTER(ln->parent, in);
759 rcu_assign_pointer(fn->parent, in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (addr_bit_set(addr, bit)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700762 rcu_assign_pointer(in->right, ln);
763 rcu_assign_pointer(in->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700765 rcu_assign_pointer(in->left, ln);
766 rcu_assign_pointer(in->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 } else { /* plen <= bit */
769
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900770 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * (new leaf node)[ln]
772 * / \
773 * (old node)[fn] NULL
774 */
775
Wei Wang81eb8442017-10-06 12:06:11 -0700776 ln = node_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David S. Miller507c9b12011-12-03 17:50:45 -0500778 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000779 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 ln->fn_bit = plen;
782
Wei Wang66f5d6c2017-10-06 12:06:10 -0700783 RCU_INIT_POINTER(ln->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (addr_bit_set(&key->addr, plen))
Wei Wang66f5d6c2017-10-06 12:06:10 -0700786 RCU_INIT_POINTER(ln->right, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 else
Wei Wang66f5d6c2017-10-06 12:06:10 -0700788 RCU_INIT_POINTER(ln->left, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Wei Wang66f5d6c2017-10-06 12:06:10 -0700790 rcu_assign_pointer(fn->parent, ln);
791
792 if (dir)
793 rcu_assign_pointer(pn->right, ln);
794 else
795 rcu_assign_pointer(pn->left, ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 return ln;
798}
799
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200800static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200801{
802 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
803 RTF_GATEWAY;
804}
805
Florian Westphale715b6d2015-01-05 23:57:44 +0100806static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100807{
Florian Westphale715b6d2015-01-05 23:57:44 +0100808 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100809
Florian Westphale715b6d2015-01-05 23:57:44 +0100810 for (i = 0; i < RTAX_MAX; i++) {
811 if (test_bit(i, mxc->mx_valid))
812 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100813 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100814}
815
816static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
817{
818 if (!mxc->mx)
819 return 0;
820
821 if (dst->flags & DST_HOST) {
822 u32 *mp = dst_metrics_write_ptr(dst);
823
824 if (unlikely(!mp))
825 return -ENOMEM;
826
827 fib6_copy_metrics(mp, mxc);
828 } else {
829 dst_init_metrics(dst, mxc->mx, false);
830
831 /* We've stolen mx now. */
832 mxc->mx = NULL;
833 }
834
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100835 return 0;
836}
837
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100838static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
839 struct net *net)
840{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700841 struct fib6_table *table = rt->rt6i_table;
842
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100843 if (atomic_read(&rt->rt6i_ref) != 1) {
844 /* This route is used as dummy address holder in some split
845 * nodes. It is not leaked, but it still holds other resources,
846 * which must be released in time. So, scan ascendant nodes
847 * and replace dummy references to this route with references
848 * to still alive ones.
849 */
850 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -0700851 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
852 lockdep_is_held(&table->tb6_lock));
853 struct rt6_info *new_leaf;
854 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
855 new_leaf = fib6_find_prefix(net, table, fn);
856 atomic_inc(&new_leaf->rt6i_ref);
857 rcu_assign_pointer(fn->leaf, new_leaf);
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100858 rt6_release(rt);
859 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700860 fn = rcu_dereference_protected(fn->parent,
861 lockdep_is_held(&table->tb6_lock));
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100862 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100863 }
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * Insert routing information in a node.
868 */
869
870static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100871 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700873 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
874 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct rt6_info *iter = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700876 struct rt6_info __rcu **ins;
877 struct rt6_info __rcu **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500878 int replace = (info->nlh &&
879 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
880 int add = (!info->nlh ||
881 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000882 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200883 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200884 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100885 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
David Ahern1f5e29c2017-01-31 16:51:37 -0800887 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
888 nlflags |= NLM_F_APPEND;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ins = &fn->leaf;
891
Wei Wang66f5d6c2017-10-06 12:06:10 -0700892 for (iter = leaf; iter;
893 iter = rcu_dereference_protected(iter->dst.rt6_next,
894 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * Search for duplicates
897 */
898
899 if (iter->rt6i_metric == rt->rt6i_metric) {
900 /*
901 * Same priority level
902 */
David S. Miller507c9b12011-12-03 17:50:45 -0500903 if (info->nlh &&
904 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000905 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200906
907 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000908 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200909 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
910 found++;
911 break;
912 }
913 if (rt_can_ecmp)
914 fallback_ins = fallback_ins ?: ins;
915 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
David Ahernf06b7542017-07-05 14:41:46 -0600918 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000919 if (rt->rt6i_nsiblings)
920 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500921 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000923 if (!(rt->rt6i_flags & RTF_EXPIRES))
924 rt6_clean_expires(iter);
925 else
926 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700927 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -EEXIST;
929 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000930 /* If we have the same destination and the same metric,
931 * but not the same gateway, then the route we try to
932 * add is sibling to this route, increment our counter
933 * of siblings, and later we will add our route to the
934 * list.
935 * Only static routes (which don't have flag
936 * RTF_EXPIRES) are used for ECMPv6.
937 *
938 * To avoid long list, we only had siblings if the
939 * route have a gateway.
940 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200941 if (rt_can_ecmp &&
942 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000943 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946 if (iter->rt6i_metric > rt->rt6i_metric)
947 break;
948
Michal Kubeček27596472015-05-18 20:54:00 +0200949next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700950 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
Michal Kubeček27596472015-05-18 20:54:00 +0200953 if (fallback_ins && !found) {
954 /* No ECMP-able route found, replace first non-ECMP one */
955 ins = fallback_ins;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700956 iter = rcu_dereference_protected(*ins,
957 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +0200958 found++;
959 }
960
David S. Millerf11e6652007-03-24 20:36:25 -0700961 /* Reset round-robin state, if necessary */
962 if (ins == &fn->leaf)
963 fn->rr_ptr = NULL;
964
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000965 /* Link this route to others same route. */
966 if (rt->rt6i_nsiblings) {
967 unsigned int rt6i_nsiblings;
968 struct rt6_info *sibling, *temp_sibling;
969
970 /* Find the first route that have the same metric */
Wei Wang66f5d6c2017-10-06 12:06:10 -0700971 sibling = leaf;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000972 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200973 if (sibling->rt6i_metric == rt->rt6i_metric &&
974 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000975 list_add_tail(&rt->rt6i_siblings,
976 &sibling->rt6i_siblings);
977 break;
978 }
Wei Wang66f5d6c2017-10-06 12:06:10 -0700979 sibling = rcu_dereference_protected(sibling->dst.rt6_next,
980 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000981 }
982 /* For each sibling in the list, increment the counter of
983 * siblings. BUG() if counters does not match, list of siblings
984 * is broken!
985 */
986 rt6i_nsiblings = 0;
987 list_for_each_entry_safe(sibling, temp_sibling,
988 &rt->rt6i_siblings, rt6i_siblings) {
989 sibling->rt6i_nsiblings++;
990 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
991 rt6i_nsiblings++;
992 }
993 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /*
997 * insert node
998 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000999 if (!replace) {
1000 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +00001001 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001003add:
Guillaume Nault73483c12016-09-07 17:21:40 +02001004 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001005 err = fib6_commit_metrics(&rt->dst, mxc);
1006 if (err)
1007 return err;
1008
Wei Wang66f5d6c2017-10-06 12:06:10 -07001009 rcu_assign_pointer(rt->dst.rt6_next, iter);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001010 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001011 rcu_assign_pointer(rt->rt6i_node, fn);
1012 rcu_assign_pointer(*ins, rt);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001013 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
1014 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001015 if (!info->skip_notify)
1016 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001017 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
David S. Miller507c9b12011-12-03 17:50:45 -05001019 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001020 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1021 fn->fn_flags |= RTN_RTINFO;
1022 }
1023
1024 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001025 int nsiblings;
1026
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001027 if (!found) {
1028 if (add)
1029 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001030 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001031 return -ENOENT;
1032 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001033
1034 err = fib6_commit_metrics(&rt->dst, mxc);
1035 if (err)
1036 return err;
1037
Wei Wang66f5d6c2017-10-06 12:06:10 -07001038 atomic_inc(&rt->rt6i_ref);
Wei Wang4e587ea2017-08-25 15:03:10 -07001039 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001040 rt->dst.rt6_next = iter->dst.rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001041 rcu_assign_pointer(*ins, rt);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001042 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1043 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001044 if (!info->skip_notify)
1045 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001046 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001047 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1048 fn->fn_flags |= RTN_RTINFO;
1049 }
Michal Kubeček27596472015-05-18 20:54:00 +02001050 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001051 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001052 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001053 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001054 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001055 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001056
1057 if (nsiblings) {
1058 /* Replacing an ECMP route, remove all siblings */
1059 ins = &rt->dst.rt6_next;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001060 iter = rcu_dereference_protected(*ins,
1061 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001062 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001063 if (iter->rt6i_metric > rt->rt6i_metric)
1064 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001065 if (rt6_qualify_for_ecmp(iter)) {
1066 *ins = iter->dst.rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001067 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001068 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001069 if (rcu_access_pointer(fn->rr_ptr) == iter)
Wei Wang383143f2017-08-16 11:18:09 -07001070 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001071 rt6_release(iter);
1072 nsiblings--;
Wei Wang81eb8442017-10-06 12:06:11 -07001073 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
Michal Kubeček27596472015-05-18 20:54:00 +02001074 } else {
1075 ins = &iter->dst.rt6_next;
1076 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001077 iter = rcu_dereference_protected(*ins,
1078 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Michal Kubeček27596472015-05-18 20:54:00 +02001079 }
1080 WARN_ON(nsiblings != 0);
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
1084 return 0;
1085}
1086
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001087static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001089 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001090 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001091 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001092 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001095void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001097 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1098 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001099 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Wei Wangbbd63f02017-10-06 12:06:07 -07001102static void fib6_update_sernum_upto_root(struct rt6_info *rt,
1103 int sernum)
1104{
1105 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1106 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1107
1108 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1109 smp_wmb();
1110 while (fn) {
1111 fn->fn_sernum = sernum;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001112 fn = rcu_dereference_protected(fn->parent,
1113 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wangbbd63f02017-10-06 12:06:07 -07001114 }
1115}
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117/*
1118 * Add routing information to the routing tree.
1119 * <destination addr>/<source addr>
1120 * with source addr info in sub-trees
Wei Wang66f5d6c2017-10-06 12:06:10 -07001121 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
1123
Florian Westphale715b6d2015-01-05 23:57:44 +01001124int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001125 struct nl_info *info, struct mx6_config *mxc,
1126 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001128 struct fib6_table *table = rt->rt6i_table;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001129 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001131 int allow_create = 1;
1132 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001133 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001134
Wei Wanga4c2fd72017-06-17 10:42:42 -07001135 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001136 return -EINVAL;
Wei Wang2b760fc2017-10-06 12:06:03 -07001137 if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
1138 return -EINVAL;
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001139
David S. Miller507c9b12011-12-03 17:50:45 -05001140 if (info->nlh) {
1141 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001142 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001143 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001144 replace_required = 1;
1145 }
1146 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001147 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Wei Wang81eb8442017-10-06 12:06:11 -07001149 fn = fib6_add_1(info->nl_net, table, root,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001150 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
fan.du9225b232013-07-22 14:21:09 +08001151 offsetof(struct rt6_info, rt6i_dst), allow_create,
Wei Wangbbd63f02017-10-06 12:06:07 -07001152 replace_required, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001153 if (IS_ERR(fn)) {
1154 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001155 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001159 pn = fn;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161#ifdef CONFIG_IPV6_SUBTREES
1162 if (rt->rt6i_src.plen) {
1163 struct fib6_node *sn;
1164
Wei Wang66f5d6c2017-10-06 12:06:10 -07001165 if (!rcu_access_pointer(fn->subtree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct fib6_node *sfn;
1167
1168 /*
1169 * Create subtree.
1170 *
1171 * fn[main tree]
1172 * |
1173 * sfn[subtree root]
1174 * \
1175 * sn[new leaf node]
1176 */
1177
1178 /* Create subtree root node */
Wei Wang81eb8442017-10-06 12:06:11 -07001179 sfn = node_alloc(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001180 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001181 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001183 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001184 rcu_assign_pointer(sfn->leaf,
1185 info->nl_net->ipv6.ip6_null_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sfn->fn_flags = RTN_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* Now add the first leaf node to new subtree */
1189
Wei Wang81eb8442017-10-06 12:06:11 -07001190 sn = fib6_add_1(info->nl_net, table, sfn,
1191 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001192 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001193 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001195 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001197 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 in main tree.
1199 */
Wei Wang81eb8442017-10-06 12:06:11 -07001200 node_free_immediate(info->nl_net, sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001201 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001202 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205 /* Now link new subtree to main tree */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001206 rcu_assign_pointer(sfn->parent, fn);
1207 rcu_assign_pointer(fn->subtree, sfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 } else {
Wei Wang81eb8442017-10-06 12:06:11 -07001209 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1210 &rt->rt6i_src.addr, rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001211 offsetof(struct rt6_info, rt6i_src),
Wei Wangbbd63f02017-10-06 12:06:07 -07001212 allow_create, replace_required, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001214 if (IS_ERR(sn)) {
1215 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001216 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
Wei Wang66f5d6c2017-10-06 12:06:10 -07001220 if (!rcu_access_pointer(fn->leaf)) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001221 atomic_inc(&rt->rt6i_ref);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001222 rcu_assign_pointer(fn->leaf, rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 fn = sn;
1225 }
1226#endif
1227
Florian Westphale715b6d2015-01-05 23:57:44 +01001228 err = fib6_add_rt2node(fn, rt, info, mxc);
Wei Wangbbd63f02017-10-06 12:06:07 -07001229 if (!err) {
1230 fib6_update_sernum_upto_root(rt, sernum);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001231 fib6_start_gc(info->nl_net, rt);
Wei Wangbbd63f02017-10-06 12:06:07 -07001232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001235 if (err) {
1236#ifdef CONFIG_IPV6_SUBTREES
1237 /*
1238 * If fib6_add_1 has cleared the old leaf pointer in the
1239 * super-tree leaf node we have to find a new one for it.
1240 */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001241 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1242 lockdep_is_held(&table->tb6_lock));
1243 if (pn != fn && pn_leaf == rt) {
1244 pn_leaf = NULL;
1245 RCU_INIT_POINTER(pn->leaf, NULL);
David S. Miller3c051232008-04-18 01:46:19 -07001246 atomic_dec(&rt->rt6i_ref);
1247 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001248 if (pn != fn && !pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1249 pn_leaf = fib6_find_prefix(info->nl_net, table, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001250#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001251 if (!pn_leaf) {
1252 WARN_ON(!pn_leaf);
1253 pn_leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001254 }
1255#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001256 atomic_inc(&pn_leaf->rt6i_ref);
1257 rcu_assign_pointer(pn->leaf, pn_leaf);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001258 }
1259#endif
Wei Wang348a4002017-08-18 17:14:49 -07001260 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return err;
1263
Wei Wang348a4002017-08-18 17:14:49 -07001264failure:
1265 /* fn->leaf could be NULL if fn is an intermediate node and we
1266 * failed to add the new route to it in both subtree creation
1267 * failure and fib6_add_rt2node() failure case.
1268 * In both cases, fib6_repair_tree() should be called to fix
1269 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001272 fib6_repair_tree(info->nl_net, table, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001273 /* Always release dst as dst->__refcnt is guaranteed
1274 * to be taken before entering this function
1275 */
Wei Wang587fea72017-06-17 10:42:36 -07001276 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280/*
1281 * Routing tree lookup
1282 *
1283 */
1284
1285struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001286 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001287 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288};
1289
Wang Yufen437de072014-03-28 12:07:04 +08001290static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1291 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001294 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001296 if (unlikely(args->offset == 0))
1297 return NULL;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
1300 * Descend on a tree
1301 */
1302
1303 fn = root;
1304
1305 for (;;) {
1306 struct fib6_node *next;
1307
1308 dir = addr_bit_set(args->addr, fn->fn_bit);
1309
Wei Wang66f5d6c2017-10-06 12:06:10 -07001310 next = dir ? rcu_dereference(fn->right) :
1311 rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 if (next) {
1314 fn = next;
1315 continue;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
1318 }
1319
David S. Miller507c9b12011-12-03 17:50:45 -05001320 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001321 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1322
1323 if (subtree || fn->fn_flags & RTN_RTINFO) {
1324 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct rt6key *key;
1326
Wei Wang8d1040e2017-10-06 12:06:08 -07001327 if (!leaf)
1328 goto backtrack;
1329
1330 key = (struct rt6key *) ((u8 *)leaf + args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001332 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1333#ifdef CONFIG_IPV6_SUBTREES
Wei Wang66f5d6c2017-10-06 12:06:10 -07001334 if (subtree) {
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001335 struct fib6_node *sfn;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001336 sfn = fib6_lookup_1(subtree, args + 1);
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001337 if (!sfn)
1338 goto backtrack;
1339 fn = sfn;
1340 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001341#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001342 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001343 return fn;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001346backtrack:
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001347 if (fn->fn_flags & RTN_ROOT)
1348 break;
1349
Wei Wang66f5d6c2017-10-06 12:06:10 -07001350 fn = rcu_dereference(fn->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 return NULL;
1354}
1355
Wei Wang66f5d6c2017-10-06 12:06:10 -07001356/* called with rcu_read_lock() held
1357 */
Wang Yufen437de072014-03-28 12:07:04 +08001358struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1359 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001362 struct lookup_args args[] = {
1363 {
1364 .offset = offsetof(struct rt6_info, rt6i_dst),
1365 .addr = daddr,
1366 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001368 {
1369 .offset = offsetof(struct rt6_info, rt6i_src),
1370 .addr = saddr,
1371 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001373 {
1374 .offset = 0, /* sentinel */
1375 }
1376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001378 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001379 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 fn = root;
1381
1382 return fn;
1383}
1384
1385/*
1386 * Get node with specified destination prefix (and source prefix,
1387 * if subtrees are used)
Wei Wang38fbeee2017-10-06 12:06:02 -07001388 * exact_match == true means we try to find fn with exact match of
1389 * the passed in prefix addr
1390 * exact_match == false means we try to find fn with longest prefix
1391 * match of the passed in prefix addr. This is useful for finding fn
1392 * for cached route as it will be stored in the exception table under
1393 * the node with longest prefix length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
1395
1396
Wang Yufen437de072014-03-28 12:07:04 +08001397static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1398 const struct in6_addr *addr,
Wei Wang38fbeee2017-10-06 12:06:02 -07001399 int plen, int offset,
1400 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Wei Wang38fbeee2017-10-06 12:06:02 -07001402 struct fib6_node *fn, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 for (fn = root; fn ; ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001405 struct rt6_info *leaf = rcu_dereference(fn->leaf);
Wei Wang8d1040e2017-10-06 12:06:08 -07001406 struct rt6key *key;
1407
1408 /* This node is being deleted */
1409 if (!leaf) {
1410 if (plen <= fn->fn_bit)
1411 goto out;
1412 else
1413 goto next;
1414 }
1415
1416 key = (struct rt6key *)((u8 *)leaf + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
1419 * Prefix match
1420 */
1421 if (plen < fn->fn_bit ||
1422 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
Wei Wang38fbeee2017-10-06 12:06:02 -07001423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (plen == fn->fn_bit)
1426 return fn;
1427
Wei Wang38fbeee2017-10-06 12:06:02 -07001428 prev = fn;
1429
Wei Wang8d1040e2017-10-06 12:06:08 -07001430next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /*
1432 * We have more bits to go
1433 */
1434 if (addr_bit_set(addr, fn->fn_bit))
Wei Wang66f5d6c2017-10-06 12:06:10 -07001435 fn = rcu_dereference(fn->right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 else
Wei Wang66f5d6c2017-10-06 12:06:10 -07001437 fn = rcu_dereference(fn->left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Wei Wang38fbeee2017-10-06 12:06:02 -07001439out:
1440 if (exact_match)
1441 return NULL;
1442 else
1443 return prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Wang Yufen437de072014-03-28 12:07:04 +08001446struct fib6_node *fib6_locate(struct fib6_node *root,
1447 const struct in6_addr *daddr, int dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001448 const struct in6_addr *saddr, int src_len,
1449 bool exact_match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct fib6_node *fn;
1452
1453 fn = fib6_locate_1(root, daddr, dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001454 offsetof(struct rt6_info, rt6i_dst),
1455 exact_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457#ifdef CONFIG_IPV6_SUBTREES
1458 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001459 WARN_ON(saddr == NULL);
Wei Wang0e801932017-10-13 15:01:08 -07001460 if (fn) {
1461 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1462
1463 if (subtree) {
1464 fn = fib6_locate_1(subtree, saddr, src_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07001465 offsetof(struct rt6_info, rt6i_src),
1466 exact_match);
Wei Wang0e801932017-10-13 15:01:08 -07001467 }
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470#endif
1471
David S. Miller507c9b12011-12-03 17:50:45 -05001472 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return fn;
1474
1475 return NULL;
1476}
1477
1478
1479/*
1480 * Deletion
1481 *
1482 */
1483
Wei Wang66f5d6c2017-10-06 12:06:10 -07001484static struct rt6_info *fib6_find_prefix(struct net *net,
1485 struct fib6_table *table,
1486 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001488 struct fib6_node *child_left, *child_right;
1489
David S. Miller507c9b12011-12-03 17:50:45 -05001490 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001491 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
David S. Miller507c9b12011-12-03 17:50:45 -05001493 while (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001494 child_left = rcu_dereference_protected(fn->left,
1495 lockdep_is_held(&table->tb6_lock));
1496 child_right = rcu_dereference_protected(fn->right,
1497 lockdep_is_held(&table->tb6_lock));
1498 if (child_left)
1499 return rcu_dereference_protected(child_left->leaf,
1500 lockdep_is_held(&table->tb6_lock));
1501 if (child_right)
1502 return rcu_dereference_protected(child_right->leaf,
1503 lockdep_is_held(&table->tb6_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001505 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 return NULL;
1508}
1509
1510/*
1511 * Called to trim the tree of intermediate nodes when possible. "fn"
1512 * is the node we want to try and remove.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001513 * Need to own table->tb6_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
1515
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001516static struct fib6_node *fib6_repair_tree(struct net *net,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001517 struct fib6_table *table,
1518 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 int children;
1521 int nstate;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001522 struct fib6_node *child;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001523 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int iter = 0;
1525
1526 for (;;) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001527 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1528 lockdep_is_held(&table->tb6_lock));
1529 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1530 lockdep_is_held(&table->tb6_lock));
1531 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1532 lockdep_is_held(&table->tb6_lock));
1533 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1534 lockdep_is_held(&table->tb6_lock));
1535 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1536 lockdep_is_held(&table->tb6_lock));
1537 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1538 lockdep_is_held(&table->tb6_lock));
1539 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1540 lockdep_is_held(&table->tb6_lock));
1541 struct rt6_info *new_fn_leaf;
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1544 iter++;
1545
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001546 WARN_ON(fn->fn_flags & RTN_RTINFO);
1547 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001548 WARN_ON(fn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 children = 0;
1551 child = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001552 if (fn_r)
1553 child = fn_r, children |= 1;
1554 if (fn_l)
1555 child = fn_l, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001557 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558#ifdef CONFIG_IPV6_SUBTREES
1559 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001560 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561#endif
1562 ) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001563 new_fn_leaf = fib6_find_prefix(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564#if RT6_DEBUG >= 2
Wei Wang66f5d6c2017-10-06 12:06:10 -07001565 if (!new_fn_leaf) {
1566 WARN_ON(!new_fn_leaf);
1567 new_fn_leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001570 atomic_inc(&new_fn_leaf->rt6i_ref);
1571 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1572 return pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001576 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001577 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001578 RCU_INIT_POINTER(pn->subtree, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 nstate = FWS_L;
1580 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001581 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001583 if (pn_r == fn)
1584 rcu_assign_pointer(pn->right, child);
1585 else if (pn_l == fn)
1586 rcu_assign_pointer(pn->left, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001588 else
1589 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590#endif
1591 if (child)
Wei Wang66f5d6c2017-10-06 12:06:10 -07001592 rcu_assign_pointer(child->parent, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 nstate = FWS_R;
1594#ifdef CONFIG_IPV6_SUBTREES
1595 }
1596#endif
1597
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001598 read_lock(&net->ipv6.fib6_walker_lock);
1599 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001600 if (!child) {
Wei Wang2b760fc2017-10-06 12:06:03 -07001601 if (w->node == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1603 w->node = pn;
1604 w->state = nstate;
1605 }
1606 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (w->node == fn) {
1608 w->node = child;
1609 if (children&2) {
1610 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001611 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 } else {
1613 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001614 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 }
1617 }
1618 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001619 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Wei Wang81eb8442017-10-06 12:06:11 -07001621 node_free(net, fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001622 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return pn;
1624
Wei Wang66f5d6c2017-10-06 12:06:10 -07001625 RCU_INIT_POINTER(pn->leaf, NULL);
1626 rt6_release(pn_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 fn = pn;
1628 }
1629}
1630
Wei Wang66f5d6c2017-10-06 12:06:10 -07001631static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1632 struct rt6_info __rcu **rtp, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001634 struct fib6_walker *w;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001635 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1636 lockdep_is_held(&table->tb6_lock));
Benjamin Theryc5728722008-03-03 23:34:17 -08001637 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 RT6_TRACE("fib6_del_route\n");
1640
Wei Wang2b760fc2017-10-06 12:06:03 -07001641 WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001644 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001646 net->ipv6.rt6_stats->fib_rt_entries--;
1647 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Wei Wang2b760fc2017-10-06 12:06:03 -07001649 /* Flush all cached dst in exception table */
1650 rt6_flush_exceptions(rt);
1651
David S. Millerf11e6652007-03-24 20:36:25 -07001652 /* Reset round-robin state, if necessary */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001653 if (rcu_access_pointer(fn->rr_ptr) == rt)
David S. Millerf11e6652007-03-24 20:36:25 -07001654 fn->rr_ptr = NULL;
1655
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001656 /* Remove this entry from other siblings */
1657 if (rt->rt6i_nsiblings) {
1658 struct rt6_info *sibling, *next_sibling;
1659
1660 list_for_each_entry_safe(sibling, next_sibling,
1661 &rt->rt6i_siblings, rt6i_siblings)
1662 sibling->rt6i_nsiblings--;
1663 rt->rt6i_nsiblings = 0;
1664 list_del_init(&rt->rt6i_siblings);
1665 }
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001668 read_lock(&net->ipv6.fib6_walker_lock);
1669 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (w->state == FWS_C && w->leaf == rt) {
1671 RT6_TRACE("walker %p adjusted by delroute\n", w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001672 w->leaf = rcu_dereference_protected(rt->dst.rt6_next,
1673 lockdep_is_held(&table->tb6_lock));
David S. Miller507c9b12011-12-03 17:50:45 -05001674 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 w->state = FWS_U;
1676 }
1677 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001678 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* If it was last route, expunge its radix tree node */
Wei Wang66f5d6c2017-10-06 12:06:10 -07001681 if (!rcu_access_pointer(fn->leaf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001683 net->ipv6.rt6_stats->fib_route_nodes--;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001684 fn = fib6_repair_tree(net, table, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001687 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001689 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
David Ahern16a16cd2017-02-02 12:37:11 -08001690 if (!info->skip_notify)
1691 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 rt6_release(rt);
1693}
1694
Wei Wang66f5d6c2017-10-06 12:06:10 -07001695/* Need to own table->tb6_lock */
Thomas Graf86872cb2006-08-22 00:01:08 -07001696int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Wei Wang4e587ea2017-08-25 15:03:10 -07001698 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1699 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Wei Wang66f5d6c2017-10-06 12:06:10 -07001700 struct fib6_table *table = rt->rt6i_table;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001701 struct net *net = info->nl_net;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001702 struct rt6_info __rcu **rtp;
1703 struct rt6_info __rcu **rtp_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001706 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001707 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return -ENOENT;
1709 }
1710#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001711 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return -ENOENT;
1713
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001714 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Wei Wang2b760fc2017-10-06 12:06:03 -07001716 /* remove cached dst from exception table */
1717 if (rt->rt6i_flags & RTF_CACHE)
1718 return rt6_remove_exception_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /*
1721 * Walk the leaf entries looking for ourself
1722 */
1723
Wei Wang66f5d6c2017-10-06 12:06:10 -07001724 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1725 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1726 lockdep_is_held(&table->tb6_lock));
1727 if (rt == cur) {
1728 fib6_del_route(table, fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 0;
1730 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001731 rtp_next = &cur->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 return -ENOENT;
1734}
1735
1736/*
1737 * Tree traversal function.
1738 *
1739 * Certainly, it is not interrupt safe.
1740 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1741 * It means, that we can modify tree during walking
1742 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001743 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 *
1745 * It guarantees that every node will be traversed,
1746 * and that it will be traversed only once.
1747 *
1748 * Callback function w->func may return:
1749 * 0 -> continue walking.
1750 * positive value -> walking is suspended (used by tree dumps,
1751 * and probably by gc, if it will be split to several slices)
1752 * negative value -> terminate walking.
1753 *
1754 * The function itself returns:
1755 * 0 -> walk is complete.
1756 * >0 -> walk is incomplete (i.e. suspended)
1757 * <0 -> walk is terminated by an error.
Wei Wang66f5d6c2017-10-06 12:06:10 -07001758 *
1759 * This function is called with tb6_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
1761
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001762static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001764 struct fib6_node *fn, *pn, *left, *right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Wei Wang2b760fc2017-10-06 12:06:03 -07001766 /* w->root should always be table->tb6_root */
1767 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 for (;;) {
1770 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001771 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 switch (w->state) {
1775#ifdef CONFIG_IPV6_SUBTREES
1776 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001777 if (FIB6_SUBTREE(fn)) {
1778 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 continue;
1780 }
1781 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001782#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 case FWS_L:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001784 left = rcu_dereference_protected(fn->left, 1);
1785 if (left) {
1786 w->node = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 w->state = FWS_INIT;
1788 continue;
1789 }
1790 w->state = FWS_R;
1791 case FWS_R:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001792 right = rcu_dereference_protected(fn->right, 1);
1793 if (right) {
1794 w->node = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 w->state = FWS_INIT;
1796 continue;
1797 }
1798 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001799 w->leaf = rcu_dereference_protected(fn->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001801 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001802 int err;
1803
Eric Dumazetfa809e22012-06-25 15:37:19 -07001804 if (w->skip) {
1805 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001806 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001807 }
1808
1809 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (err)
1811 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001812
1813 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 continue;
1815 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001816skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 w->state = FWS_U;
1818 case FWS_U:
1819 if (fn == w->root)
1820 return 0;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001821 pn = rcu_dereference_protected(fn->parent, 1);
1822 left = rcu_dereference_protected(pn->left, 1);
1823 right = rcu_dereference_protected(pn->right, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 w->node = pn;
1825#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001826 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001827 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 w->state = FWS_L;
1829 continue;
1830 }
1831#endif
Wei Wang66f5d6c2017-10-06 12:06:10 -07001832 if (left == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 w->state = FWS_R;
1834 continue;
1835 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001836 if (right == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 w->state = FWS_C;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001838 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 continue;
1840 }
1841#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001842 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843#endif
1844 }
1845 }
1846}
1847
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001848static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 int res;
1851
1852 w->state = FWS_INIT;
1853 w->node = w->root;
1854
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001855 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 res = fib6_walk_continue(w);
1857 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001858 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return res;
1860}
1861
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001862static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 int res;
1865 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001866 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001867 struct nl_info info = {
1868 .nl_net = c->net,
1869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001871 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1872 w->node->fn_sernum != c->sernum)
1873 w->node->fn_sernum = c->sernum;
1874
1875 if (!c->func) {
1876 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1877 w->leaf = NULL;
1878 return 0;
1879 }
1880
Wei Wang66f5d6c2017-10-06 12:06:10 -07001881 for_each_fib6_walker_rt(w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 res = c->func(rt, c->arg);
1883 if (res < 0) {
1884 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001885 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (res) {
1887#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001888 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001889 __func__, rt,
1890 rcu_access_pointer(rt->rt6i_node),
1891 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#endif
1893 continue;
1894 }
1895 return 0;
1896 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001897 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 w->leaf = rt;
1900 return 0;
1901}
1902
1903/*
1904 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001905 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 * func is called on each route.
1907 * It may return -1 -> delete this route.
1908 * 0 -> continue walking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
1910
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001911static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001912 int (*func)(struct rt6_info *, void *arg),
Wei Wang2b760fc2017-10-06 12:06:03 -07001913 int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001915 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 c.w.root = root;
1918 c.w.func = fib6_clean_node;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001919 c.w.count = 0;
1920 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001922 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001924 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001926 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001929static void __fib6_clean_all(struct net *net,
1930 int (*func)(struct rt6_info *, void *),
1931 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001932{
Thomas Grafc71099a2006-08-04 23:20:06 -07001933 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001934 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001935 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001936
Patrick McHardy1b43af52006-08-10 23:11:17 -07001937 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001938 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001939 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001940 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001941 spin_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001942 fib6_clean_tree(net, &table->tb6_root,
Wei Wang2b760fc2017-10-06 12:06:03 -07001943 func, sernum, arg);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001944 spin_unlock_bh(&table->tb6_lock);
Thomas Grafc71099a2006-08-04 23:20:06 -07001945 }
1946 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001947 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001948}
1949
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001950void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1951 void *arg)
1952{
1953 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1954}
1955
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001956static void fib6_flush_trees(struct net *net)
1957{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001958 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001959
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001960 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001961}
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963/*
1964 * Garbage collection
1965 */
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967static int fib6_age(struct rt6_info *rt, void *arg)
1968{
Michal Kubeček3570df92016-03-08 14:44:25 +01001969 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 unsigned long now = jiffies;
1971
1972 /*
1973 * check addrconf expiration here.
1974 * Routes are expired even if they are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 */
1976
David S. Millerd1918542011-12-28 20:19:20 -05001977 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1978 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -1;
1981 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001982 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
Wei Wangc757faa2017-10-06 12:06:01 -07001985 /* Also age clones in the exception table.
1986 * Note, that clones are aged out
1987 * only if they are not in use now.
1988 */
1989 rt6_age_exceptions(rt, gc_args, now);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 0;
1992}
1993
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001994void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Michal Kubeček3570df92016-03-08 14:44:25 +01001996 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001997 unsigned long now;
1998
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001999 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002000 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2001 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002002 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2003 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002005 gc_args.timeout = expires ? (int)expires :
2006 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07002007 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08002008
Michal Kubeček3570df92016-03-08 14:44:25 +01002009 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002010 now = jiffies;
2011 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002014 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02002015 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07002016 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002017 else
2018 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002019 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002022static void fib6_gc_timer_cb(unsigned long arg)
2023{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02002024 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08002025}
2026
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002027static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002028{
Eric Dumazet10da66f2010-10-13 08:22:03 +00002029 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002030 int err;
2031
2032 err = fib6_notifier_init(net);
2033 if (err)
2034 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00002035
Michal Kubeček3dc94f92016-03-08 14:44:45 +01002036 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002037 rwlock_init(&net->ipv6.fib6_walker_lock);
2038 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002039 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002040
Benjamin Theryc5728722008-03-03 23:34:17 -08002041 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2042 if (!net->ipv6.rt6_stats)
2043 goto out_timer;
2044
Eric Dumazet10da66f2010-10-13 08:22:03 +00002045 /* Avoid false sharing : Use at least a full cache line */
2046 size = max_t(size_t, size, L1_CACHE_BYTES);
2047
2048 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002049 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002050 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002051
2052 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2053 GFP_KERNEL);
2054 if (!net->ipv6.fib6_main_tbl)
2055 goto out_fib_table_hash;
2056
2057 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002058 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2059 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002060 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2061 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002062 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002063
2064#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2065 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2066 GFP_KERNEL);
2067 if (!net->ipv6.fib6_local_tbl)
2068 goto out_fib6_main_tbl;
2069 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002070 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2071 net->ipv6.ip6_null_entry);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002072 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2073 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002074 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002075#endif
2076 fib6_tables_init(net);
2077
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002078 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002079
2080#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2081out_fib6_main_tbl:
2082 kfree(net->ipv6.fib6_main_tbl);
2083#endif
2084out_fib_table_hash:
2085 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002086out_rt6_stats:
2087 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002088out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002089 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002090 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002091}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002092
2093static void fib6_net_exit(struct net *net)
2094{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002095 unsigned int i;
2096
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002097 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002098 del_timer_sync(&net->ipv6.ip6_fib_timer);
2099
Eric Dumazet32a805b2017-09-08 15:48:47 -07002100 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002101 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2102 struct hlist_node *tmp;
2103 struct fib6_table *tb;
2104
2105 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2106 hlist_del(&tb->tb6_hlist);
2107 fib6_free_table(tb);
2108 }
2109 }
2110
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002111 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002112 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002113 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002114}
2115
2116static struct pernet_operations fib6_net_ops = {
2117 .init = fib6_net_init,
2118 .exit = fib6_net_exit,
2119};
2120
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002121int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002123 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2126 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002127 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002128 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002129 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002130 goto out;
2131
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002132 ret = register_pernet_subsys(&fib6_net_ops);
2133 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002134 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002135
2136 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
Florian Westphalb97bac62017-08-09 20:41:48 +02002137 0);
David S. Millere8803b62012-06-16 01:12:19 -07002138 if (ret)
2139 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002140
2141 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002142out:
2143 return ret;
2144
David S. Millere8803b62012-06-16 01:12:19 -07002145out_unregister_subsys:
2146 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002147out_kmem_cache_create:
2148 kmem_cache_destroy(fib6_node_kmem);
2149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152void fib6_gc_cleanup(void)
2153{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002154 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 kmem_cache_destroy(fib6_node_kmem);
2156}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002157
2158#ifdef CONFIG_PROC_FS
2159
2160struct ipv6_route_iter {
2161 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002162 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002163 loff_t skip;
2164 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002165 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002166};
2167
2168static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2169{
2170 struct rt6_info *rt = v;
2171 struct ipv6_route_iter *iter = seq->private;
2172
2173 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2174
2175#ifdef CONFIG_IPV6_SUBTREES
2176 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2177#else
2178 seq_puts(seq, "00000000000000000000000000000000 00 ");
2179#endif
2180 if (rt->rt6i_flags & RTF_GATEWAY)
2181 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2182 else
2183 seq_puts(seq, "00000000000000000000000000000000");
2184
2185 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2186 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2187 rt->dst.__use, rt->rt6i_flags,
2188 rt->dst.dev ? rt->dst.dev->name : "");
2189 iter->w.leaf = NULL;
2190 return 0;
2191}
2192
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002193static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002194{
2195 struct ipv6_route_iter *iter = w->args;
2196
2197 if (!iter->skip)
2198 return 1;
2199
2200 do {
Wei Wang66f5d6c2017-10-06 12:06:10 -07002201 iter->w.leaf = rcu_dereference_protected(
2202 iter->w.leaf->dst.rt6_next,
2203 lockdep_is_held(&iter->tbl->tb6_lock));
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002204 iter->skip--;
2205 if (!iter->skip && iter->w.leaf)
2206 return 1;
2207 } while (iter->w.leaf);
2208
2209 return 0;
2210}
2211
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002212static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2213 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002214{
2215 memset(&iter->w, 0, sizeof(iter->w));
2216 iter->w.func = ipv6_route_yield;
2217 iter->w.root = &iter->tbl->tb6_root;
2218 iter->w.state = FWS_INIT;
2219 iter->w.node = iter->w.root;
2220 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002221 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002222 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002223 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002224}
2225
2226static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2227 struct net *net)
2228{
2229 unsigned int h;
2230 struct hlist_node *node;
2231
2232 if (tbl) {
2233 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2234 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2235 } else {
2236 h = 0;
2237 node = NULL;
2238 }
2239
2240 while (!node && h < FIB6_TABLE_HASHSZ) {
2241 node = rcu_dereference_bh(
2242 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2243 }
2244 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2245}
2246
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002247static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2248{
2249 if (iter->sernum != iter->w.root->fn_sernum) {
2250 iter->sernum = iter->w.root->fn_sernum;
2251 iter->w.state = FWS_INIT;
2252 iter->w.node = iter->w.root;
2253 WARN_ON(iter->w.skip);
2254 iter->w.skip = iter->w.count;
2255 }
2256}
2257
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002258static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2259{
2260 int r;
2261 struct rt6_info *n;
2262 struct net *net = seq_file_net(seq);
2263 struct ipv6_route_iter *iter = seq->private;
2264
2265 if (!v)
2266 goto iter_table;
2267
Wei Wangd0e60202017-10-09 17:17:26 -07002268 n = rcu_dereference_bh(((struct rt6_info *)v)->dst.rt6_next);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002269 if (n) {
2270 ++*pos;
2271 return n;
2272 }
2273
2274iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002275 ipv6_route_check_sernum(iter);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002276 spin_lock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002277 r = fib6_walk_continue(&iter->w);
Wei Wang66f5d6c2017-10-06 12:06:10 -07002278 spin_unlock_bh(&iter->tbl->tb6_lock);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002279 if (r > 0) {
2280 if (v)
2281 ++*pos;
2282 return iter->w.leaf;
2283 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002284 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002285 return NULL;
2286 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002287 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002288
2289 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2290 if (!iter->tbl)
2291 return NULL;
2292
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002293 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002294 goto iter_table;
2295}
2296
2297static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2298 __acquires(RCU_BH)
2299{
2300 struct net *net = seq_file_net(seq);
2301 struct ipv6_route_iter *iter = seq->private;
2302
2303 rcu_read_lock_bh();
2304 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2305 iter->skip = *pos;
2306
2307 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002308 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002309 return ipv6_route_seq_next(seq, NULL, pos);
2310 } else {
2311 return NULL;
2312 }
2313}
2314
2315static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2316{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002317 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002318 return w->node && !(w->state == FWS_U && w->node == w->root);
2319}
2320
2321static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2322 __releases(RCU_BH)
2323{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002324 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002325 struct ipv6_route_iter *iter = seq->private;
2326
2327 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002328 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002329
2330 rcu_read_unlock_bh();
2331}
2332
2333static const struct seq_operations ipv6_route_seq_ops = {
2334 .start = ipv6_route_seq_start,
2335 .next = ipv6_route_seq_next,
2336 .stop = ipv6_route_seq_stop,
2337 .show = ipv6_route_seq_show
2338};
2339
2340int ipv6_route_open(struct inode *inode, struct file *file)
2341{
2342 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2343 sizeof(struct ipv6_route_iter));
2344}
2345
2346#endif /* CONFIG_PROC_FS */