blob: 0ba4fbb2f85559203857a7ac781c5a2686ac685c [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
41#define RT6_DEBUG 2
42
43#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000044#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
46#define RT6_TRACE(x...) do { ; } while (0)
47#endif
48
Wang Yufen437de072014-03-28 12:07:04 +080049static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020051struct fib6_cleaner {
52 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080053 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020055 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 void *arg;
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_IPV6_SUBTREES
60#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
62#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Duan Jiong163cd4e2014-05-09 13:31:43 +080065static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080066static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
67static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010068static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020069static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
76 */
77
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080078static void fib6_gc_timer_cb(unsigned long arg);
79
Michal Kubeček9a03cd82016-03-08 14:44:35 +010080#define FOR_WALKERS(net, w) \
81 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Michal Kubeček9a03cd82016-03-08 14:44:35 +010083static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070084{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010085 write_lock_bh(&net->ipv6.fib6_walker_lock);
86 list_add(&w->lh, &net->ipv6.fib6_walkers);
87 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070088}
89
Michal Kubeček9a03cd82016-03-08 14:44:35 +010090static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070091{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010092 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000093 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010094 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070095}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020096
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020099 int new, old;
100
101 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200102 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200103 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200104 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
105 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200106 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200109enum {
110 FIB6_NO_SERNUM_CHANGE = 0,
111};
112
Wei Wang180ca442017-10-06 12:05:56 -0700113void fib6_update_sernum(struct rt6_info *rt)
114{
115 struct fib6_table *table = rt->rt6i_table;
116 struct net *net = dev_net(rt->dst.dev);
117 struct fib6_node *fn;
118
119 write_lock_bh(&table->tb6_lock);
120 fn = rcu_dereference_protected(rt->rt6i_node,
121 lockdep_is_held(&table->tb6_lock));
122 if (fn)
123 fn->fn_sernum = fib6_new_sernum(net);
124 write_unlock_bh(&table->tb6_lock);
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * Auxiliary address test functions for the radix tree.
129 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900130 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * 64bit processors)
132 */
133
134/*
135 * test bit
136 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000137#if defined(__LITTLE_ENDIAN)
138# define BITOP_BE32_SWIZZLE (0x1F & ~7)
139#else
140# define BITOP_BE32_SWIZZLE 0
141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200143static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000145 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000146 /*
147 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800148 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000149 * is optimized version of
150 * htonl(1 << ((~fn_bit)&0x1F))
151 * See include/asm-generic/bitops/le.h.
152 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700153 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
154 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200157static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct fib6_node *fn;
160
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800161 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return fn;
164}
165
Wei Wangc5cff852017-08-21 09:47:10 -0700166static void node_free_immediate(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 kmem_cache_free(fib6_node_kmem, fn);
169}
170
Wei Wangc5cff852017-08-21 09:47:10 -0700171static void node_free_rcu(struct rcu_head *head)
172{
173 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
174
175 kmem_cache_free(fib6_node_kmem, fn);
176}
177
178static void node_free(struct fib6_node *fn)
179{
180 call_rcu(&fn->rcu, node_free_rcu);
181}
182
Ido Schimmela460aa82017-08-03 13:28:25 +0200183void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700184{
185 int cpu;
186
187 if (!non_pcpu_rt->rt6i_pcpu)
188 return;
189
190 for_each_possible_cpu(cpu) {
191 struct rt6_info **ppcpu_rt;
192 struct rt6_info *pcpu_rt;
193
194 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
195 pcpu_rt = *ppcpu_rt;
196 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700197 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700198 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700199 *ppcpu_rt = NULL;
200 }
201 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700202
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700203 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700204 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700205}
Ido Schimmela460aa82017-08-03 13:28:25 +0200206EXPORT_SYMBOL_GPL(rt6_free_pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200208static void fib6_free_table(struct fib6_table *table)
209{
210 inetpeer_invalidate_tree(&table->tb6_peers);
211 kfree(table);
212}
213
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800214static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700215{
216 unsigned int h;
217
Thomas Graf375216a2006-10-21 20:20:54 -0700218 /*
219 * Initialize table lock at a single place to give lockdep a key,
220 * tables aren't visible prior to being linked to the list.
221 */
222 rwlock_init(&tb->tb6_lock);
223
Neil Hormana33bc5c2009-07-30 18:52:15 -0700224 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700225
226 /*
227 * No protection necessary, this is the only list mutatation
228 * operation, tables never disappear once they exist.
229 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800230 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700231}
232
233#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800234
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800235static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700236{
237 struct fib6_table *table;
238
239 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500240 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700241 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800242 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700243 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700244 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700245 }
246
247 return table;
248}
249
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700251{
252 struct fib6_table *tb;
253
254 if (id == 0)
255 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700257 if (tb)
258 return tb;
259
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800260 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500261 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800262 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700263
264 return tb;
265}
David Ahernb3b46632016-05-04 21:46:12 -0700266EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700267
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800268struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700269{
270 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800271 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700272 unsigned int h;
273
274 if (id == 0)
275 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700276 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700277 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800278 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800279 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700280 if (tb->tb6_id == id) {
281 rcu_read_unlock();
282 return tb;
283 }
284 }
285 rcu_read_unlock();
286
287 return NULL;
288}
David Ahernc4850682015-10-12 11:47:08 -0700289EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700290
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000291static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700292{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800293 fib6_link_table(net, net->ipv6.fib6_main_tbl);
294 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700295}
Thomas Grafc71099a2006-08-04 23:20:06 -0700296#else
297
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800298struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700299{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800300 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700301}
302
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800303struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700304{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800305 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700306}
307
David S. Miller4c9483b2011-03-12 16:22:43 -0500308struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800309 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700310{
lucienab997ad2015-10-23 15:36:53 +0800311 struct rt6_info *rt;
312
313 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300314 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800315 ip6_rt_put(rt);
316 rt = net->ipv6.ip6_null_entry;
317 dst_hold(&rt->dst);
318 }
319
320 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700321}
322
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000323static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700324{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800325 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700326}
327
328#endif
329
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200330unsigned int fib6_tables_seq_read(struct net *net)
331{
332 unsigned int h, fib_seq = 0;
333
334 rcu_read_lock();
335 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
336 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
337 struct fib6_table *tb;
338
339 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
340 read_lock_bh(&tb->tb6_lock);
341 fib_seq += tb->fib_seq;
342 read_unlock_bh(&tb->tb6_lock);
343 }
344 }
345 rcu_read_unlock();
346
347 return fib_seq;
348}
349
350static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
351 enum fib_event_type event_type,
352 struct rt6_info *rt)
353{
354 struct fib6_entry_notifier_info info = {
355 .rt = rt,
356 };
357
358 return call_fib6_notifier(nb, net, event_type, &info.info);
359}
360
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200361static int call_fib6_entry_notifiers(struct net *net,
362 enum fib_event_type event_type,
363 struct rt6_info *rt)
364{
365 struct fib6_entry_notifier_info info = {
366 .rt = rt,
367 };
368
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200369 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200370 return call_fib6_notifiers(net, event_type, &info.info);
371}
372
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200373struct fib6_dump_arg {
374 struct net *net;
375 struct notifier_block *nb;
376};
377
378static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
379{
380 if (rt == arg->net->ipv6.ip6_null_entry)
381 return;
382 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
383}
384
385static int fib6_node_dump(struct fib6_walker *w)
386{
387 struct rt6_info *rt;
388
389 for (rt = w->leaf; rt; rt = rt->dst.rt6_next)
390 fib6_rt_dump(rt, w->args);
391 w->leaf = NULL;
392 return 0;
393}
394
395static void fib6_table_dump(struct net *net, struct fib6_table *tb,
396 struct fib6_walker *w)
397{
398 w->root = &tb->tb6_root;
399 read_lock_bh(&tb->tb6_lock);
400 fib6_walk(net, w);
401 read_unlock_bh(&tb->tb6_lock);
402}
403
404/* Called with rcu_read_lock() */
405int fib6_tables_dump(struct net *net, struct notifier_block *nb)
406{
407 struct fib6_dump_arg arg;
408 struct fib6_walker *w;
409 unsigned int h;
410
411 w = kzalloc(sizeof(*w), GFP_ATOMIC);
412 if (!w)
413 return -ENOMEM;
414
415 w->func = fib6_node_dump;
416 arg.net = net;
417 arg.nb = nb;
418 w->args = &arg;
419
420 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
421 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
422 struct fib6_table *tb;
423
424 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
425 fib6_table_dump(net, tb, w);
426 }
427
428 kfree(w);
429
430 return 0;
431}
432
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200433static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700434{
435 int res;
436 struct rt6_info *rt;
437
Changli Gaod8d1f302010-06-10 23:31:35 -0700438 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700439 res = rt6_dump_route(rt, w->args);
440 if (res < 0) {
441 /* Frame is full, suspend walking */
442 w->leaf = rt;
443 return 1;
444 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800445
446 /* Multipath routes are dumped in one route with the
447 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
448 * last sibling of this route (no need to dump the
449 * sibling routes again)
450 */
451 if (rt->rt6i_nsiblings)
452 rt = list_last_entry(&rt->rt6i_siblings,
453 struct rt6_info,
454 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700455 }
456 w->leaf = NULL;
457 return 0;
458}
459
460static void fib6_dump_end(struct netlink_callback *cb)
461{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100462 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200463 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700464
465 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800466 if (cb->args[4]) {
467 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100468 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800469 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700470 cb->args[2] = 0;
471 kfree(w);
472 }
Wang Yufen437de072014-03-28 12:07:04 +0800473 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700474 cb->args[1] = 3;
475}
476
477static int fib6_dump_done(struct netlink_callback *cb)
478{
479 fib6_dump_end(cb);
480 return cb->done ? cb->done(cb) : 0;
481}
482
483static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
484 struct netlink_callback *cb)
485{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100486 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200487 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700488 int res;
489
490 w = (void *)cb->args[2];
491 w->root = &table->tb6_root;
492
493 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000494 w->count = 0;
495 w->skip = 0;
496
Patrick McHardy1b43af52006-08-10 23:11:17 -0700497 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100498 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700499 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000500 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700501 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000502 cb->args[5] = w->root->fn_sernum;
503 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700504 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000505 if (cb->args[5] != w->root->fn_sernum) {
506 /* Begin at the root if the tree changed */
507 cb->args[5] = w->root->fn_sernum;
508 w->state = FWS_INIT;
509 w->node = w->root;
510 w->skip = w->count;
511 } else
512 w->skip = 0;
513
Patrick McHardy1b43af52006-08-10 23:11:17 -0700514 read_lock_bh(&table->tb6_lock);
515 res = fib6_walk_continue(w);
516 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800517 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100518 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800519 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700520 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700521 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800522
Patrick McHardy1b43af52006-08-10 23:11:17 -0700523 return res;
524}
525
Thomas Grafc127ea22007-03-22 11:58:32 -0700526static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700527{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900528 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700529 unsigned int h, s_h;
530 unsigned int e = 0, s_e;
531 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200532 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700533 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800534 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700535 int res = 0;
536
537 s_h = cb->args[0];
538 s_e = cb->args[1];
539
540 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500541 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700542 /* New dump:
543 *
544 * 1. hook callback destructor.
545 */
546 cb->args[3] = (long)cb->done;
547 cb->done = fib6_dump_done;
548
549 /*
550 * 2. allocate and initialize walker.
551 */
552 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500553 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700554 return -ENOMEM;
555 w->func = fib6_dump_node;
556 cb->args[2] = (long)w;
557 }
558
559 arg.skb = skb;
560 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700561 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700562 w->args = &arg;
563
Eric Dumazete67f88d2011-04-27 22:56:07 +0000564 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700565 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700566 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800567 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800568 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700569 if (e < s_e)
570 goto next;
571 res = fib6_dump_table(tb, skb, cb);
572 if (res != 0)
573 goto out;
574next:
575 e++;
576 }
577 }
578out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000579 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700580 cb->args[1] = e;
581 cb->args[0] = h;
582
583 res = res < 0 ? res : skb->len;
584 if (res <= 0)
585 fib6_dump_end(cb);
586 return res;
587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/*
590 * Routing Table
591 *
592 * return the appropriate node for a routing tree "add" operation
593 * by either creating and inserting or by returning an existing
594 * node.
595 */
596
fan.du9225b232013-07-22 14:21:09 +0800597static struct fib6_node *fib6_add_1(struct fib6_node *root,
598 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000599 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600600 int replace_required, int sernum,
601 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct fib6_node *fn, *in, *ln;
604 struct fib6_node *pn = NULL;
605 struct rt6key *key;
606 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900607 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 RT6_TRACE("fib6_add_1\n");
610
611 /* insert node in tree */
612
613 fn = root;
614
615 do {
616 key = (struct rt6key *)((u8 *)fn->leaf + offset);
617
618 /*
619 * Prefix match
620 */
621 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000622 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000623 if (!allow_create) {
624 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600625 NL_SET_ERR_MSG(extack,
626 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000627 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000628 return ERR_PTR(-ENOENT);
629 }
Joe Perchesf3213832012-05-15 14:11:53 +0000630 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000633 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * Exact match ?
637 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (plen == fn->fn_bit) {
640 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500641 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 rt6_release(fn->leaf);
643 fn->leaf = NULL;
644 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return fn;
649 }
650
651 /*
652 * We have more bits to go
653 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* Try to walk down on tree. */
656 fn->fn_sernum = sernum;
657 dir = addr_bit_set(addr, fn->fn_bit);
658 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800659 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } while (fn);
661
Matti Vaittinen14df0152011-11-16 21:18:02 +0000662 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000663 /* We should not create new node because
664 * NLM_F_REPLACE was specified without NLM_F_CREATE
665 * I assume it is safe to require NLM_F_CREATE when
666 * REPLACE flag is used! Later we may want to remove the
667 * check for replace_required, because according
668 * to netlink specification, NLM_F_CREATE
669 * MUST be specified if new route is created.
670 * That would keep IPv6 consistent with IPv4
671 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000672 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600673 NL_SET_ERR_MSG(extack,
674 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000675 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000676 return ERR_PTR(-ENOENT);
677 }
Joe Perchesf3213832012-05-15 14:11:53 +0000678 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * We walked to the bottom of tree.
682 * Create new leaf node without children.
683 */
684
685 ln = node_alloc();
686
David S. Miller507c9b12011-12-03 17:50:45 -0500687 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000688 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 ln->parent = pn;
692 ln->fn_sernum = sernum;
693
694 if (dir)
695 pn->right = ln;
696 else
697 pn->left = ln;
698
699 return ln;
700
701
702insert_above:
703 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900704 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * we have a less significant route.
706 * we've to insert an intermediate node on the list
707 * this new node will point to the one we need to create
708 * and the current
709 */
710
711 pn = fn->parent;
712
713 /* find 1st bit in difference between the 2 addrs.
714
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800715 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 but if it is >= plen, the value is ignored in any case.
717 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900718
fan.du9225b232013-07-22 14:21:09 +0800719 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900721 /*
722 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * / \
724 * (new leaf node)[ln] (old node)[fn]
725 */
726 if (plen > bit) {
727 in = node_alloc();
728 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900729
David S. Miller507c9b12011-12-03 17:50:45 -0500730 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (in)
Wei Wangc5cff852017-08-21 09:47:10 -0700732 node_free_immediate(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (ln)
Wei Wangc5cff852017-08-21 09:47:10 -0700734 node_free_immediate(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000735 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900738 /*
739 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * RTN_RTINFO will
741 * be off since that an address that chooses one of
742 * the branches would not match less specific routes
743 * in the other branch
744 */
745
746 in->fn_bit = bit;
747
748 in->parent = pn;
749 in->leaf = fn->leaf;
750 atomic_inc(&in->leaf->rt6i_ref);
751
752 in->fn_sernum = sernum;
753
754 /* update parent pointer */
755 if (dir)
756 pn->right = in;
757 else
758 pn->left = in;
759
760 ln->fn_bit = plen;
761
762 ln->parent = in;
763 fn->parent = in;
764
765 ln->fn_sernum = sernum;
766
767 if (addr_bit_set(addr, bit)) {
768 in->right = ln;
769 in->left = fn;
770 } else {
771 in->left = ln;
772 in->right = fn;
773 }
774 } else { /* plen <= bit */
775
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900776 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * (new leaf node)[ln]
778 * / \
779 * (old node)[fn] NULL
780 */
781
782 ln = node_alloc();
783
David S. Miller507c9b12011-12-03 17:50:45 -0500784 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000785 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 ln->fn_bit = plen;
788
789 ln->parent = pn;
790
791 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (dir)
794 pn->right = ln;
795 else
796 pn->left = ln;
797
798 if (addr_bit_set(&key->addr, plen))
799 ln->right = fn;
800 else
801 ln->left = fn;
802
803 fn->parent = ln;
804 }
805 return ln;
806}
807
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200808static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200809{
810 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
811 RTF_GATEWAY;
812}
813
Florian Westphale715b6d2015-01-05 23:57:44 +0100814static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100815{
Florian Westphale715b6d2015-01-05 23:57:44 +0100816 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100817
Florian Westphale715b6d2015-01-05 23:57:44 +0100818 for (i = 0; i < RTAX_MAX; i++) {
819 if (test_bit(i, mxc->mx_valid))
820 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100821 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100822}
823
824static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
825{
826 if (!mxc->mx)
827 return 0;
828
829 if (dst->flags & DST_HOST) {
830 u32 *mp = dst_metrics_write_ptr(dst);
831
832 if (unlikely(!mp))
833 return -ENOMEM;
834
835 fib6_copy_metrics(mp, mxc);
836 } else {
837 dst_init_metrics(dst, mxc->mx, false);
838
839 /* We've stolen mx now. */
840 mxc->mx = NULL;
841 }
842
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100843 return 0;
844}
845
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100846static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
847 struct net *net)
848{
849 if (atomic_read(&rt->rt6i_ref) != 1) {
850 /* This route is used as dummy address holder in some split
851 * nodes. It is not leaked, but it still holds other resources,
852 * which must be released in time. So, scan ascendant nodes
853 * and replace dummy references to this route with references
854 * to still alive ones.
855 */
856 while (fn) {
857 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
858 fn->leaf = fib6_find_prefix(net, fn);
859 atomic_inc(&fn->leaf->rt6i_ref);
860 rt6_release(rt);
861 }
862 fn = fn->parent;
863 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100864 }
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
868 * Insert routing information in a node.
869 */
870
871static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100872 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct rt6_info *iter = NULL;
875 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200876 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500877 int replace = (info->nlh &&
878 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
879 int add = (!info->nlh ||
880 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000881 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200882 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200883 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100884 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David Ahern1f5e29c2017-01-31 16:51:37 -0800886 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
887 nlflags |= NLM_F_APPEND;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 ins = &fn->leaf;
890
David S. Miller507c9b12011-12-03 17:50:45 -0500891 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
893 * Search for duplicates
894 */
895
896 if (iter->rt6i_metric == rt->rt6i_metric) {
897 /*
898 * Same priority level
899 */
David S. Miller507c9b12011-12-03 17:50:45 -0500900 if (info->nlh &&
901 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000902 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200903
904 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000905 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200906 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
907 found++;
908 break;
909 }
910 if (rt_can_ecmp)
911 fallback_ins = fallback_ins ?: ins;
912 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
David Ahernf06b7542017-07-05 14:41:46 -0600915 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000916 if (rt->rt6i_nsiblings)
917 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500918 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000920 if (!(rt->rt6i_flags & RTF_EXPIRES))
921 rt6_clean_expires(iter);
922 else
923 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700924 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EEXIST;
926 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000927 /* If we have the same destination and the same metric,
928 * but not the same gateway, then the route we try to
929 * add is sibling to this route, increment our counter
930 * of siblings, and later we will add our route to the
931 * list.
932 * Only static routes (which don't have flag
933 * RTF_EXPIRES) are used for ECMPv6.
934 *
935 * To avoid long list, we only had siblings if the
936 * route have a gateway.
937 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200938 if (rt_can_ecmp &&
939 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000940 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 if (iter->rt6i_metric > rt->rt6i_metric)
944 break;
945
Michal Kubeček27596472015-05-18 20:54:00 +0200946next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700947 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Michal Kubeček27596472015-05-18 20:54:00 +0200950 if (fallback_ins && !found) {
951 /* No ECMP-able route found, replace first non-ECMP one */
952 ins = fallback_ins;
953 iter = *ins;
954 found++;
955 }
956
David S. Millerf11e6652007-03-24 20:36:25 -0700957 /* Reset round-robin state, if necessary */
958 if (ins == &fn->leaf)
959 fn->rr_ptr = NULL;
960
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000961 /* Link this route to others same route. */
962 if (rt->rt6i_nsiblings) {
963 unsigned int rt6i_nsiblings;
964 struct rt6_info *sibling, *temp_sibling;
965
966 /* Find the first route that have the same metric */
967 sibling = fn->leaf;
968 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200969 if (sibling->rt6i_metric == rt->rt6i_metric &&
970 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000971 list_add_tail(&rt->rt6i_siblings,
972 &sibling->rt6i_siblings);
973 break;
974 }
975 sibling = sibling->dst.rt6_next;
976 }
977 /* For each sibling in the list, increment the counter of
978 * siblings. BUG() if counters does not match, list of siblings
979 * is broken!
980 */
981 rt6i_nsiblings = 0;
982 list_for_each_entry_safe(sibling, temp_sibling,
983 &rt->rt6i_siblings, rt6i_siblings) {
984 sibling->rt6i_nsiblings++;
985 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
986 rt6i_nsiblings++;
987 }
988 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
989 }
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /*
992 * insert node
993 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000994 if (!replace) {
995 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000996 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000998add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200999 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +01001000 err = fib6_commit_metrics(&rt->dst, mxc);
1001 if (err)
1002 return err;
1003
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001004 rt->dst.rt6_next = iter;
1005 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -07001006 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001007 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001008 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
1009 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001010 if (!info->skip_notify)
1011 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001012 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David S. Miller507c9b12011-12-03 17:50:45 -05001014 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001015 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1016 fn->fn_flags |= RTN_RTINFO;
1017 }
1018
1019 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001020 int nsiblings;
1021
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001022 if (!found) {
1023 if (add)
1024 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001025 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001026 return -ENOENT;
1027 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001028
1029 err = fib6_commit_metrics(&rt->dst, mxc);
1030 if (err)
1031 return err;
1032
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001033 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -07001034 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001035 rt->dst.rt6_next = iter->dst.rt6_next;
1036 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001037 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1038 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001039 if (!info->skip_notify)
1040 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001041 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001042 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1043 fn->fn_flags |= RTN_RTINFO;
1044 }
Michal Kubeček27596472015-05-18 20:54:00 +02001045 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001046 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001047 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -07001048 if (fn->rr_ptr == iter)
1049 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001050 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001051
1052 if (nsiblings) {
1053 /* Replacing an ECMP route, remove all siblings */
1054 ins = &rt->dst.rt6_next;
1055 iter = *ins;
1056 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001057 if (iter->rt6i_metric > rt->rt6i_metric)
1058 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001059 if (rt6_qualify_for_ecmp(iter)) {
1060 *ins = iter->dst.rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001061 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001062 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -07001063 if (fn->rr_ptr == iter)
1064 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001065 rt6_release(iter);
1066 nsiblings--;
1067 } else {
1068 ins = &iter->dst.rt6_next;
1069 }
1070 iter = *ins;
1071 }
1072 WARN_ON(nsiblings != 0);
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 return 0;
1077}
1078
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001079static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001081 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001082 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001083 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001084 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001087void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001089 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1090 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001091 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094/*
1095 * Add routing information to the routing tree.
1096 * <destination addr>/<source addr>
1097 * with source addr info in sub-trees
1098 */
1099
Florian Westphale715b6d2015-01-05 23:57:44 +01001100int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001101 struct nl_info *info, struct mx6_config *mxc,
1102 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001104 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001106 int allow_create = 1;
1107 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001108 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001109
Wei Wanga4c2fd72017-06-17 10:42:42 -07001110 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001111 return -EINVAL;
1112
David S. Miller507c9b12011-12-03 17:50:45 -05001113 if (info->nlh) {
1114 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001115 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001116 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001117 replace_required = 1;
1118 }
1119 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001120 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
fan.du9225b232013-07-22 14:21:09 +08001122 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1123 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -06001124 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001125 if (IS_ERR(fn)) {
1126 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001127 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001131 pn = fn;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#ifdef CONFIG_IPV6_SUBTREES
1134 if (rt->rt6i_src.plen) {
1135 struct fib6_node *sn;
1136
David S. Miller507c9b12011-12-03 17:50:45 -05001137 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct fib6_node *sfn;
1139
1140 /*
1141 * Create subtree.
1142 *
1143 * fn[main tree]
1144 * |
1145 * sfn[subtree root]
1146 * \
1147 * sn[new leaf node]
1148 */
1149
1150 /* Create subtree root node */
1151 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001152 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001153 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001155 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1156 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001158 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /* Now add the first leaf node to new subtree */
1161
1162 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001163 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001164 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001165 allow_create, replace_required, sernum,
1166 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001168 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001170 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 in main tree.
1172 */
Wei Wangc5cff852017-08-21 09:47:10 -07001173 node_free_immediate(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001174 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001175 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
1178 /* Now link new subtree to main tree */
1179 sfn->parent = fn;
1180 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 } else {
1182 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001183 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001184 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001185 allow_create, replace_required, sernum,
1186 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001188 if (IS_ERR(sn)) {
1189 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001190 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
David S. Miller507c9b12011-12-03 17:50:45 -05001194 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001195 fn->leaf = rt;
1196 atomic_inc(&rt->rt6i_ref);
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 fn = sn;
1199 }
1200#endif
1201
Florian Westphale715b6d2015-01-05 23:57:44 +01001202 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001203 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001204 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001205 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001206 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
1209out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001210 if (err) {
1211#ifdef CONFIG_IPV6_SUBTREES
1212 /*
1213 * If fib6_add_1 has cleared the old leaf pointer in the
1214 * super-tree leaf node we have to find a new one for it.
1215 */
David S. Miller3c051232008-04-18 01:46:19 -07001216 if (pn != fn && pn->leaf == rt) {
1217 pn->leaf = NULL;
1218 atomic_dec(&rt->rt6i_ref);
1219 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001220 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001221 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001222#if RT6_DEBUG >= 2
1223 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001224 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001225 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001226 }
1227#endif
1228 atomic_inc(&pn->leaf->rt6i_ref);
1229 }
1230#endif
Wei Wang348a4002017-08-18 17:14:49 -07001231 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return err;
1234
Wei Wang348a4002017-08-18 17:14:49 -07001235failure:
1236 /* fn->leaf could be NULL if fn is an intermediate node and we
1237 * failed to add the new route to it in both subtree creation
1238 * failure and fib6_add_rt2node() failure case.
1239 * In both cases, fib6_repair_tree() should be called to fix
1240 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001243 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001244 /* Always release dst as dst->__refcnt is guaranteed
1245 * to be taken before entering this function
1246 */
Wei Wang587fea72017-06-17 10:42:36 -07001247 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * Routing tree lookup
1253 *
1254 */
1255
1256struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001257 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001258 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
1260
Wang Yufen437de072014-03-28 12:07:04 +08001261static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1262 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001265 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001267 if (unlikely(args->offset == 0))
1268 return NULL;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Descend on a tree
1272 */
1273
1274 fn = root;
1275
1276 for (;;) {
1277 struct fib6_node *next;
1278
1279 dir = addr_bit_set(args->addr, fn->fn_bit);
1280
1281 next = dir ? fn->right : fn->left;
1282
1283 if (next) {
1284 fn = next;
1285 continue;
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 }
1289
David S. Miller507c9b12011-12-03 17:50:45 -05001290 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001291 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct rt6key *key;
1293
1294 key = (struct rt6key *) ((u8 *) fn->leaf +
1295 args->offset);
1296
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001297 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1298#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001299 if (fn->subtree) {
1300 struct fib6_node *sfn;
1301 sfn = fib6_lookup_1(fn->subtree,
1302 args + 1);
1303 if (!sfn)
1304 goto backtrack;
1305 fn = sfn;
1306 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001307#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001308 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001309 return fn;
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001312#ifdef CONFIG_IPV6_SUBTREES
1313backtrack:
1314#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001315 if (fn->fn_flags & RTN_ROOT)
1316 break;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 fn = fn->parent;
1319 }
1320
1321 return NULL;
1322}
1323
Wang Yufen437de072014-03-28 12:07:04 +08001324struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1325 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001328 struct lookup_args args[] = {
1329 {
1330 .offset = offsetof(struct rt6_info, rt6i_dst),
1331 .addr = daddr,
1332 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001334 {
1335 .offset = offsetof(struct rt6_info, rt6i_src),
1336 .addr = saddr,
1337 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001339 {
1340 .offset = 0, /* sentinel */
1341 }
1342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001344 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001345 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 fn = root;
1347
1348 return fn;
1349}
1350
1351/*
1352 * Get node with specified destination prefix (and source prefix,
1353 * if subtrees are used)
1354 */
1355
1356
Wang Yufen437de072014-03-28 12:07:04 +08001357static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1358 const struct in6_addr *addr,
1359 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct fib6_node *fn;
1362
1363 for (fn = root; fn ; ) {
1364 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1365
1366 /*
1367 * Prefix match
1368 */
1369 if (plen < fn->fn_bit ||
1370 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1371 return NULL;
1372
1373 if (plen == fn->fn_bit)
1374 return fn;
1375
1376 /*
1377 * We have more bits to go
1378 */
1379 if (addr_bit_set(addr, fn->fn_bit))
1380 fn = fn->right;
1381 else
1382 fn = fn->left;
1383 }
1384 return NULL;
1385}
1386
Wang Yufen437de072014-03-28 12:07:04 +08001387struct fib6_node *fib6_locate(struct fib6_node *root,
1388 const struct in6_addr *daddr, int dst_len,
1389 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct fib6_node *fn;
1392
1393 fn = fib6_locate_1(root, daddr, dst_len,
1394 offsetof(struct rt6_info, rt6i_dst));
1395
1396#ifdef CONFIG_IPV6_SUBTREES
1397 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001398 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001399 if (fn && fn->subtree)
1400 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 offsetof(struct rt6_info, rt6i_src));
1402 }
1403#endif
1404
David S. Miller507c9b12011-12-03 17:50:45 -05001405 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return fn;
1407
1408 return NULL;
1409}
1410
1411
1412/*
1413 * Deletion
1414 *
1415 */
1416
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001417static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
David S. Miller507c9b12011-12-03 17:50:45 -05001419 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001420 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
David S. Miller507c9b12011-12-03 17:50:45 -05001422 while (fn) {
1423 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001425 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return fn->right->leaf;
1427
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001428 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 return NULL;
1431}
1432
1433/*
1434 * Called to trim the tree of intermediate nodes when possible. "fn"
1435 * is the node we want to try and remove.
1436 */
1437
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001438static struct fib6_node *fib6_repair_tree(struct net *net,
1439 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 int children;
1442 int nstate;
1443 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001444 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int iter = 0;
1446
1447 for (;;) {
1448 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1449 iter++;
1450
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001451 WARN_ON(fn->fn_flags & RTN_RTINFO);
1452 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001453 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 children = 0;
1456 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001457 if (fn->right)
1458 child = fn->right, children |= 1;
1459 if (fn->left)
1460 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001462 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463#ifdef CONFIG_IPV6_SUBTREES
1464 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001465 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466#endif
1467 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001468 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001470 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001471 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001472 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474#endif
1475 atomic_inc(&fn->leaf->rt6i_ref);
1476 return fn->parent;
1477 }
1478
1479 pn = fn->parent;
1480#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001481 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001482 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001483 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 nstate = FWS_L;
1485 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001486 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001488 if (pn->right == fn)
1489 pn->right = child;
1490 else if (pn->left == fn)
1491 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001493 else
1494 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495#endif
1496 if (child)
1497 child->parent = pn;
1498 nstate = FWS_R;
1499#ifdef CONFIG_IPV6_SUBTREES
1500 }
1501#endif
1502
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001503 read_lock(&net->ipv6.fib6_walker_lock);
1504 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001505 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (w->root == fn) {
1507 w->root = w->node = NULL;
1508 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1509 } else if (w->node == fn) {
1510 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1511 w->node = pn;
1512 w->state = nstate;
1513 }
1514 } else {
1515 if (w->root == fn) {
1516 w->root = child;
1517 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1518 }
1519 if (w->node == fn) {
1520 w->node = child;
1521 if (children&2) {
1522 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001523 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 } else {
1525 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001526 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 }
1529 }
1530 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001531 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001534 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return pn;
1536
1537 rt6_release(pn->leaf);
1538 pn->leaf = NULL;
1539 fn = pn;
1540 }
1541}
1542
1543static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001544 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001546 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001548 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 RT6_TRACE("fib6_del_route\n");
1551
1552 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001553 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001555 net->ipv6.rt6_stats->fib_rt_entries--;
1556 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
David S. Millerf11e6652007-03-24 20:36:25 -07001558 /* Reset round-robin state, if necessary */
1559 if (fn->rr_ptr == rt)
1560 fn->rr_ptr = NULL;
1561
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001562 /* Remove this entry from other siblings */
1563 if (rt->rt6i_nsiblings) {
1564 struct rt6_info *sibling, *next_sibling;
1565
1566 list_for_each_entry_safe(sibling, next_sibling,
1567 &rt->rt6i_siblings, rt6i_siblings)
1568 sibling->rt6i_nsiblings--;
1569 rt->rt6i_nsiblings = 0;
1570 list_del_init(&rt->rt6i_siblings);
1571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001574 read_lock(&net->ipv6.fib6_walker_lock);
1575 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (w->state == FWS_C && w->leaf == rt) {
1577 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001578 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001579 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 w->state = FWS_U;
1581 }
1582 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001583 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Changli Gaod8d1f302010-06-10 23:31:35 -07001585 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001588 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001590 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001591 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001594 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001596 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
David Ahern16a16cd2017-02-02 12:37:11 -08001597 if (!info->skip_notify)
1598 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 rt6_release(rt);
1600}
1601
Thomas Graf86872cb2006-08-22 00:01:08 -07001602int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Wei Wang4e587ea2017-08-25 15:03:10 -07001604 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1605 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001606 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 struct rt6_info **rtp;
1608
1609#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001610 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001611 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return -ENOENT;
1613 }
1614#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001615 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return -ENOENT;
1617
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001618 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
David S. Miller507c9b12011-12-03 17:50:45 -05001620 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001621 struct fib6_node *pn = fn;
1622#ifdef CONFIG_IPV6_SUBTREES
1623 /* clones of this route might be in another subtree */
1624 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001625 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001626 pn = pn->parent;
1627 pn = pn->parent;
1628 }
1629#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001630 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * Walk the leaf entries looking for ourself
1635 */
1636
Changli Gaod8d1f302010-06-10 23:31:35 -07001637 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001639 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return 0;
1641 }
1642 }
1643 return -ENOENT;
1644}
1645
1646/*
1647 * Tree traversal function.
1648 *
1649 * Certainly, it is not interrupt safe.
1650 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1651 * It means, that we can modify tree during walking
1652 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001653 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *
1655 * It guarantees that every node will be traversed,
1656 * and that it will be traversed only once.
1657 *
1658 * Callback function w->func may return:
1659 * 0 -> continue walking.
1660 * positive value -> walking is suspended (used by tree dumps,
1661 * and probably by gc, if it will be split to several slices)
1662 * negative value -> terminate walking.
1663 *
1664 * The function itself returns:
1665 * 0 -> walk is complete.
1666 * >0 -> walk is incomplete (i.e. suspended)
1667 * <0 -> walk is terminated by an error.
1668 */
1669
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001670static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct fib6_node *fn, *pn;
1673
1674 for (;;) {
1675 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001676 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678
1679 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001680 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 w->state = FWS_C;
1682 w->leaf = fn->leaf;
1683 }
1684 switch (w->state) {
1685#ifdef CONFIG_IPV6_SUBTREES
1686 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001687 if (FIB6_SUBTREE(fn)) {
1688 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 continue;
1690 }
1691 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 case FWS_L:
1694 if (fn->left) {
1695 w->node = fn->left;
1696 w->state = FWS_INIT;
1697 continue;
1698 }
1699 w->state = FWS_R;
1700 case FWS_R:
1701 if (fn->right) {
1702 w->node = fn->right;
1703 w->state = FWS_INIT;
1704 continue;
1705 }
1706 w->state = FWS_C;
1707 w->leaf = fn->leaf;
1708 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001709 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001710 int err;
1711
Eric Dumazetfa809e22012-06-25 15:37:19 -07001712 if (w->skip) {
1713 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001714 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001715 }
1716
1717 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (err)
1719 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001720
1721 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 continue;
1723 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001724skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 w->state = FWS_U;
1726 case FWS_U:
1727 if (fn == w->root)
1728 return 0;
1729 pn = fn->parent;
1730 w->node = pn;
1731#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001732 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001733 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 w->state = FWS_L;
1735 continue;
1736 }
1737#endif
1738 if (pn->left == fn) {
1739 w->state = FWS_R;
1740 continue;
1741 }
1742 if (pn->right == fn) {
1743 w->state = FWS_C;
1744 w->leaf = w->node->leaf;
1745 continue;
1746 }
1747#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001748 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749#endif
1750 }
1751 }
1752}
1753
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001754static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 int res;
1757
1758 w->state = FWS_INIT;
1759 w->node = w->root;
1760
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001761 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 res = fib6_walk_continue(w);
1763 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001764 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return res;
1766}
1767
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001768static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 int res;
1771 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001772 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001773 struct nl_info info = {
1774 .nl_net = c->net,
1775 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001777 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1778 w->node->fn_sernum != c->sernum)
1779 w->node->fn_sernum = c->sernum;
1780
1781 if (!c->func) {
1782 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1783 w->leaf = NULL;
1784 return 0;
1785 }
1786
Changli Gaod8d1f302010-06-10 23:31:35 -07001787 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 res = c->func(rt, c->arg);
1789 if (res < 0) {
1790 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001791 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (res) {
1793#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001794 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001795 __func__, rt,
1796 rcu_access_pointer(rt->rt6i_node),
1797 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798#endif
1799 continue;
1800 }
1801 return 0;
1802 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001803 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 w->leaf = rt;
1806 return 0;
1807}
1808
1809/*
1810 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001811 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * func is called on each route.
1813 * It may return -1 -> delete this route.
1814 * 0 -> continue walking
1815 *
1816 * prune==1 -> only immediate children of node (certainly,
1817 * ignoring pure split nodes) will be scanned.
1818 */
1819
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001820static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001821 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001822 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001824 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 c.w.root = root;
1827 c.w.func = fib6_clean_node;
1828 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001829 c.w.count = 0;
1830 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001832 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001834 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001836 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001839static void __fib6_clean_all(struct net *net,
1840 int (*func)(struct rt6_info *, void *),
1841 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001842{
Thomas Grafc71099a2006-08-04 23:20:06 -07001843 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001844 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001845 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001846
Patrick McHardy1b43af52006-08-10 23:11:17 -07001847 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001848 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001849 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001850 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001851 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001852 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001853 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001854 write_unlock_bh(&table->tb6_lock);
1855 }
1856 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001857 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001858}
1859
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001860void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1861 void *arg)
1862{
1863 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1864}
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1867{
1868 if (rt->rt6i_flags & RTF_CACHE) {
1869 RT6_TRACE("pruning clone %p\n", rt);
1870 return -1;
1871 }
1872
1873 return 0;
1874}
1875
Duan Jiong163cd4e2014-05-09 13:31:43 +08001876static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001878 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1879 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001880}
1881
1882static void fib6_flush_trees(struct net *net)
1883{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001884 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001885
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001886 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001887}
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889/*
1890 * Garbage collection
1891 */
1892
Michal Kubeček3570df92016-03-08 14:44:25 +01001893struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 int timeout;
1896 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001897};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899static int fib6_age(struct rt6_info *rt, void *arg)
1900{
Michal Kubeček3570df92016-03-08 14:44:25 +01001901 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 unsigned long now = jiffies;
1903
1904 /*
1905 * check addrconf expiration here.
1906 * Routes are expired even if they are in use.
1907 *
1908 * Also age clones. Note, that clones are aged out
1909 * only if they are not in use now.
1910 */
1911
David S. Millerd1918542011-12-28 20:19:20 -05001912 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1913 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return -1;
1916 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001917 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 } else if (rt->rt6i_flags & RTF_CACHE) {
Xin Long1e2ea8a2017-08-26 20:10:10 +08001919 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout))
1920 rt->dst.obsolete = DST_OBSOLETE_KILL;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001921 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Xin Long1e2ea8a2017-08-26 20:10:10 +08001922 rt->dst.obsolete == DST_OBSOLETE_KILL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 RT6_TRACE("aging clone %p\n", rt);
1924 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001925 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1926 struct neighbour *neigh;
1927 __u8 neigh_flags = 0;
1928
1929 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1930 if (neigh) {
1931 neigh_flags = neigh->flags;
1932 neigh_release(neigh);
1933 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001934 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001935 RT6_TRACE("purging route %p via non-router but gateway\n",
1936 rt);
1937 return -1;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001940 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
1943 return 0;
1944}
1945
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001946void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Michal Kubeček3570df92016-03-08 14:44:25 +01001948 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001949 unsigned long now;
1950
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001951 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001952 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1953 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001954 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001957 gc_args.timeout = expires ? (int)expires :
1958 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001959 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001960
Michal Kubeček3570df92016-03-08 14:44:25 +01001961 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001962 now = jiffies;
1963 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001966 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001967 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001968 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001969 else
1970 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001971 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001974static void fib6_gc_timer_cb(unsigned long arg)
1975{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001976 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001977}
1978
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001979static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001980{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001981 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001982 int err;
1983
1984 err = fib6_notifier_init(net);
1985 if (err)
1986 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001987
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001988 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001989 rwlock_init(&net->ipv6.fib6_walker_lock);
1990 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001991 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001992
Benjamin Theryc5728722008-03-03 23:34:17 -08001993 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1994 if (!net->ipv6.rt6_stats)
1995 goto out_timer;
1996
Eric Dumazet10da66f2010-10-13 08:22:03 +00001997 /* Avoid false sharing : Use at least a full cache line */
1998 size = max_t(size_t, size, L1_CACHE_BYTES);
1999
2000 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002001 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08002002 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002003
2004 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2005 GFP_KERNEL);
2006 if (!net->ipv6.fib6_main_tbl)
2007 goto out_fib_table_hash;
2008
2009 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002010 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002011 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2012 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002013 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002014
2015#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2016 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2017 GFP_KERNEL);
2018 if (!net->ipv6.fib6_local_tbl)
2019 goto out_fib6_main_tbl;
2020 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002021 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002022 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2023 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002024 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002025#endif
2026 fib6_tables_init(net);
2027
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002028 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002029
2030#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2031out_fib6_main_tbl:
2032 kfree(net->ipv6.fib6_main_tbl);
2033#endif
2034out_fib_table_hash:
2035 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002036out_rt6_stats:
2037 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002038out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002039 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002040 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002041}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002042
2043static void fib6_net_exit(struct net *net)
2044{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002045 unsigned int i;
2046
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002047 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002048 del_timer_sync(&net->ipv6.ip6_fib_timer);
2049
Eric Dumazet32a805b2017-09-08 15:48:47 -07002050 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002051 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2052 struct hlist_node *tmp;
2053 struct fib6_table *tb;
2054
2055 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2056 hlist_del(&tb->tb6_hlist);
2057 fib6_free_table(tb);
2058 }
2059 }
2060
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002061 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002062 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002063 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002064}
2065
2066static struct pernet_operations fib6_net_ops = {
2067 .init = fib6_net_init,
2068 .exit = fib6_net_exit,
2069};
2070
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002071int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002073 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2076 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002077 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002078 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002079 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002080 goto out;
2081
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002082 ret = register_pernet_subsys(&fib6_net_ops);
2083 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002084 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002085
2086 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
Florian Westphalb97bac62017-08-09 20:41:48 +02002087 0);
David S. Millere8803b62012-06-16 01:12:19 -07002088 if (ret)
2089 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002090
2091 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002092out:
2093 return ret;
2094
David S. Millere8803b62012-06-16 01:12:19 -07002095out_unregister_subsys:
2096 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002097out_kmem_cache_create:
2098 kmem_cache_destroy(fib6_node_kmem);
2099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102void fib6_gc_cleanup(void)
2103{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002104 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 kmem_cache_destroy(fib6_node_kmem);
2106}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002107
2108#ifdef CONFIG_PROC_FS
2109
2110struct ipv6_route_iter {
2111 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002112 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002113 loff_t skip;
2114 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002115 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002116};
2117
2118static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2119{
2120 struct rt6_info *rt = v;
2121 struct ipv6_route_iter *iter = seq->private;
2122
2123 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2124
2125#ifdef CONFIG_IPV6_SUBTREES
2126 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2127#else
2128 seq_puts(seq, "00000000000000000000000000000000 00 ");
2129#endif
2130 if (rt->rt6i_flags & RTF_GATEWAY)
2131 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2132 else
2133 seq_puts(seq, "00000000000000000000000000000000");
2134
2135 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2136 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2137 rt->dst.__use, rt->rt6i_flags,
2138 rt->dst.dev ? rt->dst.dev->name : "");
2139 iter->w.leaf = NULL;
2140 return 0;
2141}
2142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002143static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002144{
2145 struct ipv6_route_iter *iter = w->args;
2146
2147 if (!iter->skip)
2148 return 1;
2149
2150 do {
2151 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2152 iter->skip--;
2153 if (!iter->skip && iter->w.leaf)
2154 return 1;
2155 } while (iter->w.leaf);
2156
2157 return 0;
2158}
2159
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002160static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2161 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002162{
2163 memset(&iter->w, 0, sizeof(iter->w));
2164 iter->w.func = ipv6_route_yield;
2165 iter->w.root = &iter->tbl->tb6_root;
2166 iter->w.state = FWS_INIT;
2167 iter->w.node = iter->w.root;
2168 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002169 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002170 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002171 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002172}
2173
2174static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2175 struct net *net)
2176{
2177 unsigned int h;
2178 struct hlist_node *node;
2179
2180 if (tbl) {
2181 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2182 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2183 } else {
2184 h = 0;
2185 node = NULL;
2186 }
2187
2188 while (!node && h < FIB6_TABLE_HASHSZ) {
2189 node = rcu_dereference_bh(
2190 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2191 }
2192 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2193}
2194
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002195static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2196{
2197 if (iter->sernum != iter->w.root->fn_sernum) {
2198 iter->sernum = iter->w.root->fn_sernum;
2199 iter->w.state = FWS_INIT;
2200 iter->w.node = iter->w.root;
2201 WARN_ON(iter->w.skip);
2202 iter->w.skip = iter->w.count;
2203 }
2204}
2205
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002206static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2207{
2208 int r;
2209 struct rt6_info *n;
2210 struct net *net = seq_file_net(seq);
2211 struct ipv6_route_iter *iter = seq->private;
2212
2213 if (!v)
2214 goto iter_table;
2215
2216 n = ((struct rt6_info *)v)->dst.rt6_next;
2217 if (n) {
2218 ++*pos;
2219 return n;
2220 }
2221
2222iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002223 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002224 read_lock(&iter->tbl->tb6_lock);
2225 r = fib6_walk_continue(&iter->w);
2226 read_unlock(&iter->tbl->tb6_lock);
2227 if (r > 0) {
2228 if (v)
2229 ++*pos;
2230 return iter->w.leaf;
2231 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002232 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002233 return NULL;
2234 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002235 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002236
2237 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2238 if (!iter->tbl)
2239 return NULL;
2240
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002241 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002242 goto iter_table;
2243}
2244
2245static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2246 __acquires(RCU_BH)
2247{
2248 struct net *net = seq_file_net(seq);
2249 struct ipv6_route_iter *iter = seq->private;
2250
2251 rcu_read_lock_bh();
2252 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2253 iter->skip = *pos;
2254
2255 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002256 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002257 return ipv6_route_seq_next(seq, NULL, pos);
2258 } else {
2259 return NULL;
2260 }
2261}
2262
2263static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2264{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002265 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002266 return w->node && !(w->state == FWS_U && w->node == w->root);
2267}
2268
2269static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2270 __releases(RCU_BH)
2271{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002272 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002273 struct ipv6_route_iter *iter = seq->private;
2274
2275 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002276 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002277
2278 rcu_read_unlock_bh();
2279}
2280
2281static const struct seq_operations ipv6_route_seq_ops = {
2282 .start = ipv6_route_seq_start,
2283 .next = ipv6_route_seq_next,
2284 .stop = ipv6_route_seq_stop,
2285 .show = ipv6_route_seq_show
2286};
2287
2288int ipv6_route_open(struct inode *inode, struct file *file)
2289{
2290 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2291 sizeof(struct ipv6_route_iter));
2292}
2293
2294#endif /* CONFIG_PROC_FS */