blob: a3b5c163325fa3448818c446c7de502eb4f5a9c8 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * Auxiliary address test functions for the radix tree.
115 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900116 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * 64bit processors)
118 */
119
120/*
121 * test bit
122 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000123#if defined(__LITTLE_ENDIAN)
124# define BITOP_BE32_SWIZZLE (0x1F & ~7)
125#else
126# define BITOP_BE32_SWIZZLE 0
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200129static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000131 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000132 /*
133 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
138 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700139 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
140 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200143static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct fib6_node *fn;
146
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800147 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return fn;
150}
151
Wei Wangc5cff852017-08-21 09:47:10 -0700152static void node_free_immediate(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 kmem_cache_free(fib6_node_kmem, fn);
155}
156
Wei Wangc5cff852017-08-21 09:47:10 -0700157static void node_free_rcu(struct rcu_head *head)
158{
159 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
160
161 kmem_cache_free(fib6_node_kmem, fn);
162}
163
164static void node_free(struct fib6_node *fn)
165{
166 call_rcu(&fn->rcu, node_free_rcu);
167}
168
Ido Schimmela460aa82017-08-03 13:28:25 +0200169void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700170{
171 int cpu;
172
173 if (!non_pcpu_rt->rt6i_pcpu)
174 return;
175
176 for_each_possible_cpu(cpu) {
177 struct rt6_info **ppcpu_rt;
178 struct rt6_info *pcpu_rt;
179
180 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
181 pcpu_rt = *ppcpu_rt;
182 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700183 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700184 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700185 *ppcpu_rt = NULL;
186 }
187 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700188
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700189 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700190 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700191}
Ido Schimmela460aa82017-08-03 13:28:25 +0200192EXPORT_SYMBOL_GPL(rt6_free_pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800194static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700195{
196 unsigned int h;
197
Thomas Graf375216a2006-10-21 20:20:54 -0700198 /*
199 * Initialize table lock at a single place to give lockdep a key,
200 * tables aren't visible prior to being linked to the list.
201 */
202 rwlock_init(&tb->tb6_lock);
203
Neil Hormana33bc5c2009-07-30 18:52:15 -0700204 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700205
206 /*
207 * No protection necessary, this is the only list mutatation
208 * operation, tables never disappear once they exist.
209 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800210 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700211}
212
213#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800214
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800215static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700216{
217 struct fib6_table *table;
218
219 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500220 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800222 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700223 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700224 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700225 }
226
227 return table;
228}
229
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800230struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700231{
232 struct fib6_table *tb;
233
234 if (id == 0)
235 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800236 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700237 if (tb)
238 return tb;
239
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800240 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500241 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800242 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700243
244 return tb;
245}
David Ahernb3b46632016-05-04 21:46:12 -0700246EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700247
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800248struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700249{
250 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800251 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700252 unsigned int h;
253
254 if (id == 0)
255 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700256 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700257 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800259 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700260 if (tb->tb6_id == id) {
261 rcu_read_unlock();
262 return tb;
263 }
264 }
265 rcu_read_unlock();
266
267 return NULL;
268}
David Ahernc4850682015-10-12 11:47:08 -0700269EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700270
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000271static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700272{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800273 fib6_link_table(net, net->ipv6.fib6_main_tbl);
274 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700275}
Thomas Grafc71099a2006-08-04 23:20:06 -0700276#else
277
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800278struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700279{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800280 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700281}
282
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800283struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700284{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700286}
287
David S. Miller4c9483b2011-03-12 16:22:43 -0500288struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800289 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700290{
lucienab997ad2015-10-23 15:36:53 +0800291 struct rt6_info *rt;
292
293 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300294 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800295 ip6_rt_put(rt);
296 rt = net->ipv6.ip6_null_entry;
297 dst_hold(&rt->dst);
298 }
299
300 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700301}
302
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000303static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700304{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800305 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700306}
307
308#endif
309
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200310unsigned int fib6_tables_seq_read(struct net *net)
311{
312 unsigned int h, fib_seq = 0;
313
314 rcu_read_lock();
315 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
316 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
317 struct fib6_table *tb;
318
319 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
320 read_lock_bh(&tb->tb6_lock);
321 fib_seq += tb->fib_seq;
322 read_unlock_bh(&tb->tb6_lock);
323 }
324 }
325 rcu_read_unlock();
326
327 return fib_seq;
328}
329
330static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
331 enum fib_event_type event_type,
332 struct rt6_info *rt)
333{
334 struct fib6_entry_notifier_info info = {
335 .rt = rt,
336 };
337
338 return call_fib6_notifier(nb, net, event_type, &info.info);
339}
340
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200341static int call_fib6_entry_notifiers(struct net *net,
342 enum fib_event_type event_type,
343 struct rt6_info *rt)
344{
345 struct fib6_entry_notifier_info info = {
346 .rt = rt,
347 };
348
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200349 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200350 return call_fib6_notifiers(net, event_type, &info.info);
351}
352
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200353struct fib6_dump_arg {
354 struct net *net;
355 struct notifier_block *nb;
356};
357
358static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
359{
360 if (rt == arg->net->ipv6.ip6_null_entry)
361 return;
362 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
363}
364
365static int fib6_node_dump(struct fib6_walker *w)
366{
367 struct rt6_info *rt;
368
369 for (rt = w->leaf; rt; rt = rt->dst.rt6_next)
370 fib6_rt_dump(rt, w->args);
371 w->leaf = NULL;
372 return 0;
373}
374
375static void fib6_table_dump(struct net *net, struct fib6_table *tb,
376 struct fib6_walker *w)
377{
378 w->root = &tb->tb6_root;
379 read_lock_bh(&tb->tb6_lock);
380 fib6_walk(net, w);
381 read_unlock_bh(&tb->tb6_lock);
382}
383
384/* Called with rcu_read_lock() */
385int fib6_tables_dump(struct net *net, struct notifier_block *nb)
386{
387 struct fib6_dump_arg arg;
388 struct fib6_walker *w;
389 unsigned int h;
390
391 w = kzalloc(sizeof(*w), GFP_ATOMIC);
392 if (!w)
393 return -ENOMEM;
394
395 w->func = fib6_node_dump;
396 arg.net = net;
397 arg.nb = nb;
398 w->args = &arg;
399
400 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
401 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
402 struct fib6_table *tb;
403
404 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
405 fib6_table_dump(net, tb, w);
406 }
407
408 kfree(w);
409
410 return 0;
411}
412
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200413static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700414{
415 int res;
416 struct rt6_info *rt;
417
Changli Gaod8d1f302010-06-10 23:31:35 -0700418 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700419 res = rt6_dump_route(rt, w->args);
420 if (res < 0) {
421 /* Frame is full, suspend walking */
422 w->leaf = rt;
423 return 1;
424 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800425
426 /* Multipath routes are dumped in one route with the
427 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
428 * last sibling of this route (no need to dump the
429 * sibling routes again)
430 */
431 if (rt->rt6i_nsiblings)
432 rt = list_last_entry(&rt->rt6i_siblings,
433 struct rt6_info,
434 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700435 }
436 w->leaf = NULL;
437 return 0;
438}
439
440static void fib6_dump_end(struct netlink_callback *cb)
441{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100442 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200443 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700444
445 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800446 if (cb->args[4]) {
447 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100448 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800449 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700450 cb->args[2] = 0;
451 kfree(w);
452 }
Wang Yufen437de072014-03-28 12:07:04 +0800453 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700454 cb->args[1] = 3;
455}
456
457static int fib6_dump_done(struct netlink_callback *cb)
458{
459 fib6_dump_end(cb);
460 return cb->done ? cb->done(cb) : 0;
461}
462
463static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
464 struct netlink_callback *cb)
465{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100466 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200467 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700468 int res;
469
470 w = (void *)cb->args[2];
471 w->root = &table->tb6_root;
472
473 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000474 w->count = 0;
475 w->skip = 0;
476
Patrick McHardy1b43af52006-08-10 23:11:17 -0700477 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100478 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700479 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000480 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700481 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000482 cb->args[5] = w->root->fn_sernum;
483 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700484 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000485 if (cb->args[5] != w->root->fn_sernum) {
486 /* Begin at the root if the tree changed */
487 cb->args[5] = w->root->fn_sernum;
488 w->state = FWS_INIT;
489 w->node = w->root;
490 w->skip = w->count;
491 } else
492 w->skip = 0;
493
Patrick McHardy1b43af52006-08-10 23:11:17 -0700494 read_lock_bh(&table->tb6_lock);
495 res = fib6_walk_continue(w);
496 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800497 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100498 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800499 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700500 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700501 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800502
Patrick McHardy1b43af52006-08-10 23:11:17 -0700503 return res;
504}
505
Thomas Grafc127ea22007-03-22 11:58:32 -0700506static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900508 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700509 unsigned int h, s_h;
510 unsigned int e = 0, s_e;
511 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200512 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700513 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800514 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700515 int res = 0;
516
517 s_h = cb->args[0];
518 s_e = cb->args[1];
519
520 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500521 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700522 /* New dump:
523 *
524 * 1. hook callback destructor.
525 */
526 cb->args[3] = (long)cb->done;
527 cb->done = fib6_dump_done;
528
529 /*
530 * 2. allocate and initialize walker.
531 */
532 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500533 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700534 return -ENOMEM;
535 w->func = fib6_dump_node;
536 cb->args[2] = (long)w;
537 }
538
539 arg.skb = skb;
540 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700541 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700542 w->args = &arg;
543
Eric Dumazete67f88d2011-04-27 22:56:07 +0000544 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700545 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700546 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800547 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800548 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700549 if (e < s_e)
550 goto next;
551 res = fib6_dump_table(tb, skb, cb);
552 if (res != 0)
553 goto out;
554next:
555 e++;
556 }
557 }
558out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000559 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700560 cb->args[1] = e;
561 cb->args[0] = h;
562
563 res = res < 0 ? res : skb->len;
564 if (res <= 0)
565 fib6_dump_end(cb);
566 return res;
567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/*
570 * Routing Table
571 *
572 * return the appropriate node for a routing tree "add" operation
573 * by either creating and inserting or by returning an existing
574 * node.
575 */
576
fan.du9225b232013-07-22 14:21:09 +0800577static struct fib6_node *fib6_add_1(struct fib6_node *root,
578 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000579 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600580 int replace_required, int sernum,
581 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct fib6_node *fn, *in, *ln;
584 struct fib6_node *pn = NULL;
585 struct rt6key *key;
586 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900587 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 RT6_TRACE("fib6_add_1\n");
590
591 /* insert node in tree */
592
593 fn = root;
594
595 do {
596 key = (struct rt6key *)((u8 *)fn->leaf + offset);
597
598 /*
599 * Prefix match
600 */
601 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000602 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000603 if (!allow_create) {
604 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600605 NL_SET_ERR_MSG(extack,
606 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000607 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000608 return ERR_PTR(-ENOENT);
609 }
Joe Perchesf3213832012-05-15 14:11:53 +0000610 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000613 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * Exact match ?
617 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (plen == fn->fn_bit) {
620 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500621 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 rt6_release(fn->leaf);
623 fn->leaf = NULL;
624 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return fn;
629 }
630
631 /*
632 * We have more bits to go
633 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Try to walk down on tree. */
636 fn->fn_sernum = sernum;
637 dir = addr_bit_set(addr, fn->fn_bit);
638 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800639 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 } while (fn);
641
Matti Vaittinen14df0152011-11-16 21:18:02 +0000642 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000643 /* We should not create new node because
644 * NLM_F_REPLACE was specified without NLM_F_CREATE
645 * I assume it is safe to require NLM_F_CREATE when
646 * REPLACE flag is used! Later we may want to remove the
647 * check for replace_required, because according
648 * to netlink specification, NLM_F_CREATE
649 * MUST be specified if new route is created.
650 * That would keep IPv6 consistent with IPv4
651 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000652 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600653 NL_SET_ERR_MSG(extack,
654 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000655 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000656 return ERR_PTR(-ENOENT);
657 }
Joe Perchesf3213832012-05-15 14:11:53 +0000658 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * We walked to the bottom of tree.
662 * Create new leaf node without children.
663 */
664
665 ln = node_alloc();
666
David S. Miller507c9b12011-12-03 17:50:45 -0500667 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000668 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 ln->parent = pn;
672 ln->fn_sernum = sernum;
673
674 if (dir)
675 pn->right = ln;
676 else
677 pn->left = ln;
678
679 return ln;
680
681
682insert_above:
683 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900684 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * we have a less significant route.
686 * we've to insert an intermediate node on the list
687 * this new node will point to the one we need to create
688 * and the current
689 */
690
691 pn = fn->parent;
692
693 /* find 1st bit in difference between the 2 addrs.
694
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800695 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 but if it is >= plen, the value is ignored in any case.
697 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900698
fan.du9225b232013-07-22 14:21:09 +0800699 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900701 /*
702 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * / \
704 * (new leaf node)[ln] (old node)[fn]
705 */
706 if (plen > bit) {
707 in = node_alloc();
708 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900709
David S. Miller507c9b12011-12-03 17:50:45 -0500710 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (in)
Wei Wangc5cff852017-08-21 09:47:10 -0700712 node_free_immediate(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (ln)
Wei Wangc5cff852017-08-21 09:47:10 -0700714 node_free_immediate(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000715 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900718 /*
719 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * RTN_RTINFO will
721 * be off since that an address that chooses one of
722 * the branches would not match less specific routes
723 * in the other branch
724 */
725
726 in->fn_bit = bit;
727
728 in->parent = pn;
729 in->leaf = fn->leaf;
730 atomic_inc(&in->leaf->rt6i_ref);
731
732 in->fn_sernum = sernum;
733
734 /* update parent pointer */
735 if (dir)
736 pn->right = in;
737 else
738 pn->left = in;
739
740 ln->fn_bit = plen;
741
742 ln->parent = in;
743 fn->parent = in;
744
745 ln->fn_sernum = sernum;
746
747 if (addr_bit_set(addr, bit)) {
748 in->right = ln;
749 in->left = fn;
750 } else {
751 in->left = ln;
752 in->right = fn;
753 }
754 } else { /* plen <= bit */
755
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900756 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * (new leaf node)[ln]
758 * / \
759 * (old node)[fn] NULL
760 */
761
762 ln = node_alloc();
763
David S. Miller507c9b12011-12-03 17:50:45 -0500764 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000765 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 ln->fn_bit = plen;
768
769 ln->parent = pn;
770
771 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (dir)
774 pn->right = ln;
775 else
776 pn->left = ln;
777
778 if (addr_bit_set(&key->addr, plen))
779 ln->right = fn;
780 else
781 ln->left = fn;
782
783 fn->parent = ln;
784 }
785 return ln;
786}
787
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200788static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200789{
790 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
791 RTF_GATEWAY;
792}
793
Florian Westphale715b6d2015-01-05 23:57:44 +0100794static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100795{
Florian Westphale715b6d2015-01-05 23:57:44 +0100796 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100797
Florian Westphale715b6d2015-01-05 23:57:44 +0100798 for (i = 0; i < RTAX_MAX; i++) {
799 if (test_bit(i, mxc->mx_valid))
800 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100801 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100802}
803
804static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
805{
806 if (!mxc->mx)
807 return 0;
808
809 if (dst->flags & DST_HOST) {
810 u32 *mp = dst_metrics_write_ptr(dst);
811
812 if (unlikely(!mp))
813 return -ENOMEM;
814
815 fib6_copy_metrics(mp, mxc);
816 } else {
817 dst_init_metrics(dst, mxc->mx, false);
818
819 /* We've stolen mx now. */
820 mxc->mx = NULL;
821 }
822
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100823 return 0;
824}
825
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100826static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
827 struct net *net)
828{
829 if (atomic_read(&rt->rt6i_ref) != 1) {
830 /* This route is used as dummy address holder in some split
831 * nodes. It is not leaked, but it still holds other resources,
832 * which must be released in time. So, scan ascendant nodes
833 * and replace dummy references to this route with references
834 * to still alive ones.
835 */
836 while (fn) {
837 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
838 fn->leaf = fib6_find_prefix(net, fn);
839 atomic_inc(&fn->leaf->rt6i_ref);
840 rt6_release(rt);
841 }
842 fn = fn->parent;
843 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100844 }
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*
848 * Insert routing information in a node.
849 */
850
851static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100852 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct rt6_info *iter = NULL;
855 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200856 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500857 int replace = (info->nlh &&
858 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
859 int add = (!info->nlh ||
860 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000861 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200862 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200863 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100864 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
David Ahern1f5e29c2017-01-31 16:51:37 -0800866 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
867 nlflags |= NLM_F_APPEND;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ins = &fn->leaf;
870
David S. Miller507c9b12011-12-03 17:50:45 -0500871 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Search for duplicates
874 */
875
876 if (iter->rt6i_metric == rt->rt6i_metric) {
877 /*
878 * Same priority level
879 */
David S. Miller507c9b12011-12-03 17:50:45 -0500880 if (info->nlh &&
881 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000882 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200883
884 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000885 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200886 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
887 found++;
888 break;
889 }
890 if (rt_can_ecmp)
891 fallback_ins = fallback_ins ?: ins;
892 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
David Ahernf06b7542017-07-05 14:41:46 -0600895 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000896 if (rt->rt6i_nsiblings)
897 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500898 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000900 if (!(rt->rt6i_flags & RTF_EXPIRES))
901 rt6_clean_expires(iter);
902 else
903 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700904 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EEXIST;
906 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000907 /* If we have the same destination and the same metric,
908 * but not the same gateway, then the route we try to
909 * add is sibling to this route, increment our counter
910 * of siblings, and later we will add our route to the
911 * list.
912 * Only static routes (which don't have flag
913 * RTF_EXPIRES) are used for ECMPv6.
914 *
915 * To avoid long list, we only had siblings if the
916 * route have a gateway.
917 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200918 if (rt_can_ecmp &&
919 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000920 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 if (iter->rt6i_metric > rt->rt6i_metric)
924 break;
925
Michal Kubeček27596472015-05-18 20:54:00 +0200926next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700927 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Michal Kubeček27596472015-05-18 20:54:00 +0200930 if (fallback_ins && !found) {
931 /* No ECMP-able route found, replace first non-ECMP one */
932 ins = fallback_ins;
933 iter = *ins;
934 found++;
935 }
936
David S. Millerf11e6652007-03-24 20:36:25 -0700937 /* Reset round-robin state, if necessary */
938 if (ins == &fn->leaf)
939 fn->rr_ptr = NULL;
940
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000941 /* Link this route to others same route. */
942 if (rt->rt6i_nsiblings) {
943 unsigned int rt6i_nsiblings;
944 struct rt6_info *sibling, *temp_sibling;
945
946 /* Find the first route that have the same metric */
947 sibling = fn->leaf;
948 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200949 if (sibling->rt6i_metric == rt->rt6i_metric &&
950 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000951 list_add_tail(&rt->rt6i_siblings,
952 &sibling->rt6i_siblings);
953 break;
954 }
955 sibling = sibling->dst.rt6_next;
956 }
957 /* For each sibling in the list, increment the counter of
958 * siblings. BUG() if counters does not match, list of siblings
959 * is broken!
960 */
961 rt6i_nsiblings = 0;
962 list_for_each_entry_safe(sibling, temp_sibling,
963 &rt->rt6i_siblings, rt6i_siblings) {
964 sibling->rt6i_nsiblings++;
965 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
966 rt6i_nsiblings++;
967 }
968 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
969 }
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * insert node
973 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000974 if (!replace) {
975 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000976 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000978add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200979 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100980 err = fib6_commit_metrics(&rt->dst, mxc);
981 if (err)
982 return err;
983
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000984 rt->dst.rt6_next = iter;
985 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -0700986 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000987 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200988 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
989 rt);
David Ahern3b1137f2017-02-02 12:37:10 -0800990 if (!info->skip_notify)
991 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000992 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David S. Miller507c9b12011-12-03 17:50:45 -0500994 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000995 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
996 fn->fn_flags |= RTN_RTINFO;
997 }
998
999 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001000 int nsiblings;
1001
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001002 if (!found) {
1003 if (add)
1004 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001005 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001006 return -ENOENT;
1007 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001008
1009 err = fib6_commit_metrics(&rt->dst, mxc);
1010 if (err)
1011 return err;
1012
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001013 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -07001014 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001015 rt->dst.rt6_next = iter->dst.rt6_next;
1016 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001017 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1018 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001019 if (!info->skip_notify)
1020 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001021 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001022 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1023 fn->fn_flags |= RTN_RTINFO;
1024 }
Michal Kubeček27596472015-05-18 20:54:00 +02001025 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001026 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001027 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -07001028 if (fn->rr_ptr == iter)
1029 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001030 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001031
1032 if (nsiblings) {
1033 /* Replacing an ECMP route, remove all siblings */
1034 ins = &rt->dst.rt6_next;
1035 iter = *ins;
1036 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001037 if (iter->rt6i_metric > rt->rt6i_metric)
1038 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001039 if (rt6_qualify_for_ecmp(iter)) {
1040 *ins = iter->dst.rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001041 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001042 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -07001043 if (fn->rr_ptr == iter)
1044 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001045 rt6_release(iter);
1046 nsiblings--;
1047 } else {
1048 ins = &iter->dst.rt6_next;
1049 }
1050 iter = *ins;
1051 }
1052 WARN_ON(nsiblings != 0);
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
1056 return 0;
1057}
1058
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001059static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001061 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001062 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001063 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001064 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001067void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001069 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1070 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001071 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074/*
1075 * Add routing information to the routing tree.
1076 * <destination addr>/<source addr>
1077 * with source addr info in sub-trees
1078 */
1079
Florian Westphale715b6d2015-01-05 23:57:44 +01001080int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001081 struct nl_info *info, struct mx6_config *mxc,
1082 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001084 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001086 int allow_create = 1;
1087 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001088 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001089
Wei Wanga4c2fd72017-06-17 10:42:42 -07001090 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001091 return -EINVAL;
1092
David S. Miller507c9b12011-12-03 17:50:45 -05001093 if (info->nlh) {
1094 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001095 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001096 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001097 replace_required = 1;
1098 }
1099 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001100 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
fan.du9225b232013-07-22 14:21:09 +08001102 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1103 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -06001104 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001105 if (IS_ERR(fn)) {
1106 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001107 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001111 pn = fn;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#ifdef CONFIG_IPV6_SUBTREES
1114 if (rt->rt6i_src.plen) {
1115 struct fib6_node *sn;
1116
David S. Miller507c9b12011-12-03 17:50:45 -05001117 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct fib6_node *sfn;
1119
1120 /*
1121 * Create subtree.
1122 *
1123 * fn[main tree]
1124 * |
1125 * sfn[subtree root]
1126 * \
1127 * sn[new leaf node]
1128 */
1129
1130 /* Create subtree root node */
1131 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001132 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001133 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001135 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1136 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001138 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* Now add the first leaf node to new subtree */
1141
1142 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001143 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001144 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001145 allow_create, replace_required, sernum,
1146 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001148 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001150 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 in main tree.
1152 */
Wei Wangc5cff852017-08-21 09:47:10 -07001153 node_free_immediate(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001154 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001155 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
1158 /* Now link new subtree to main tree */
1159 sfn->parent = fn;
1160 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 } else {
1162 sn = fib6_add_1(fn->subtree, &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
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001168 if (IS_ERR(sn)) {
1169 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001170 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
David S. Miller507c9b12011-12-03 17:50:45 -05001174 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001175 fn->leaf = rt;
1176 atomic_inc(&rt->rt6i_ref);
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 fn = sn;
1179 }
1180#endif
1181
Florian Westphale715b6d2015-01-05 23:57:44 +01001182 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001183 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001184 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001185 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001186 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
1189out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001190 if (err) {
1191#ifdef CONFIG_IPV6_SUBTREES
1192 /*
1193 * If fib6_add_1 has cleared the old leaf pointer in the
1194 * super-tree leaf node we have to find a new one for it.
1195 */
David S. Miller3c051232008-04-18 01:46:19 -07001196 if (pn != fn && pn->leaf == rt) {
1197 pn->leaf = NULL;
1198 atomic_dec(&rt->rt6i_ref);
1199 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001200 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001201 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001202#if RT6_DEBUG >= 2
1203 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001204 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001205 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001206 }
1207#endif
1208 atomic_inc(&pn->leaf->rt6i_ref);
1209 }
1210#endif
Wei Wang348a4002017-08-18 17:14:49 -07001211 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return err;
1214
Wei Wang348a4002017-08-18 17:14:49 -07001215failure:
1216 /* fn->leaf could be NULL if fn is an intermediate node and we
1217 * failed to add the new route to it in both subtree creation
1218 * failure and fib6_add_rt2node() failure case.
1219 * In both cases, fib6_repair_tree() should be called to fix
1220 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001223 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001224 /* Always release dst as dst->__refcnt is guaranteed
1225 * to be taken before entering this function
1226 */
Wei Wang587fea72017-06-17 10:42:36 -07001227 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/*
1232 * Routing tree lookup
1233 *
1234 */
1235
1236struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001237 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001238 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Wang Yufen437de072014-03-28 12:07:04 +08001241static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1242 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001245 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001247 if (unlikely(args->offset == 0))
1248 return NULL;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Descend on a tree
1252 */
1253
1254 fn = root;
1255
1256 for (;;) {
1257 struct fib6_node *next;
1258
1259 dir = addr_bit_set(args->addr, fn->fn_bit);
1260
1261 next = dir ? fn->right : fn->left;
1262
1263 if (next) {
1264 fn = next;
1265 continue;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268 }
1269
David S. Miller507c9b12011-12-03 17:50:45 -05001270 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001271 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct rt6key *key;
1273
1274 key = (struct rt6key *) ((u8 *) fn->leaf +
1275 args->offset);
1276
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001277 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1278#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001279 if (fn->subtree) {
1280 struct fib6_node *sfn;
1281 sfn = fib6_lookup_1(fn->subtree,
1282 args + 1);
1283 if (!sfn)
1284 goto backtrack;
1285 fn = sfn;
1286 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001287#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001288 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001289 return fn;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001292#ifdef CONFIG_IPV6_SUBTREES
1293backtrack:
1294#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001295 if (fn->fn_flags & RTN_ROOT)
1296 break;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 fn = fn->parent;
1299 }
1300
1301 return NULL;
1302}
1303
Wang Yufen437de072014-03-28 12:07:04 +08001304struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1305 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001308 struct lookup_args args[] = {
1309 {
1310 .offset = offsetof(struct rt6_info, rt6i_dst),
1311 .addr = daddr,
1312 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001314 {
1315 .offset = offsetof(struct rt6_info, rt6i_src),
1316 .addr = saddr,
1317 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001319 {
1320 .offset = 0, /* sentinel */
1321 }
1322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001324 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001325 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 fn = root;
1327
1328 return fn;
1329}
1330
1331/*
1332 * Get node with specified destination prefix (and source prefix,
1333 * if subtrees are used)
1334 */
1335
1336
Wang Yufen437de072014-03-28 12:07:04 +08001337static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1338 const struct in6_addr *addr,
1339 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct fib6_node *fn;
1342
1343 for (fn = root; fn ; ) {
1344 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1345
1346 /*
1347 * Prefix match
1348 */
1349 if (plen < fn->fn_bit ||
1350 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1351 return NULL;
1352
1353 if (plen == fn->fn_bit)
1354 return fn;
1355
1356 /*
1357 * We have more bits to go
1358 */
1359 if (addr_bit_set(addr, fn->fn_bit))
1360 fn = fn->right;
1361 else
1362 fn = fn->left;
1363 }
1364 return NULL;
1365}
1366
Wang Yufen437de072014-03-28 12:07:04 +08001367struct fib6_node *fib6_locate(struct fib6_node *root,
1368 const struct in6_addr *daddr, int dst_len,
1369 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct fib6_node *fn;
1372
1373 fn = fib6_locate_1(root, daddr, dst_len,
1374 offsetof(struct rt6_info, rt6i_dst));
1375
1376#ifdef CONFIG_IPV6_SUBTREES
1377 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001378 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001379 if (fn && fn->subtree)
1380 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 offsetof(struct rt6_info, rt6i_src));
1382 }
1383#endif
1384
David S. Miller507c9b12011-12-03 17:50:45 -05001385 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return fn;
1387
1388 return NULL;
1389}
1390
1391
1392/*
1393 * Deletion
1394 *
1395 */
1396
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001397static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
David S. Miller507c9b12011-12-03 17:50:45 -05001399 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001400 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
David S. Miller507c9b12011-12-03 17:50:45 -05001402 while (fn) {
1403 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001405 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return fn->right->leaf;
1407
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001408 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 return NULL;
1411}
1412
1413/*
1414 * Called to trim the tree of intermediate nodes when possible. "fn"
1415 * is the node we want to try and remove.
1416 */
1417
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001418static struct fib6_node *fib6_repair_tree(struct net *net,
1419 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 int children;
1422 int nstate;
1423 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001424 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int iter = 0;
1426
1427 for (;;) {
1428 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1429 iter++;
1430
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001431 WARN_ON(fn->fn_flags & RTN_RTINFO);
1432 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001433 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 children = 0;
1436 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001437 if (fn->right)
1438 child = fn->right, children |= 1;
1439 if (fn->left)
1440 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001442 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#ifdef CONFIG_IPV6_SUBTREES
1444 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001445 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#endif
1447 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001448 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001450 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001451 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001452 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454#endif
1455 atomic_inc(&fn->leaf->rt6i_ref);
1456 return fn->parent;
1457 }
1458
1459 pn = fn->parent;
1460#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001461 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001462 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001463 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 nstate = FWS_L;
1465 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001466 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001468 if (pn->right == fn)
1469 pn->right = child;
1470 else if (pn->left == fn)
1471 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001473 else
1474 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#endif
1476 if (child)
1477 child->parent = pn;
1478 nstate = FWS_R;
1479#ifdef CONFIG_IPV6_SUBTREES
1480 }
1481#endif
1482
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001483 read_lock(&net->ipv6.fib6_walker_lock);
1484 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001485 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (w->root == fn) {
1487 w->root = w->node = NULL;
1488 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1489 } else if (w->node == fn) {
1490 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1491 w->node = pn;
1492 w->state = nstate;
1493 }
1494 } else {
1495 if (w->root == fn) {
1496 w->root = child;
1497 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1498 }
1499 if (w->node == fn) {
1500 w->node = child;
1501 if (children&2) {
1502 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001503 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 } else {
1505 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001506 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508 }
1509 }
1510 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001511 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001514 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return pn;
1516
1517 rt6_release(pn->leaf);
1518 pn->leaf = NULL;
1519 fn = pn;
1520 }
1521}
1522
1523static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001524 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001526 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001528 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 RT6_TRACE("fib6_del_route\n");
1531
1532 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001533 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001535 net->ipv6.rt6_stats->fib_rt_entries--;
1536 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
David S. Millerf11e6652007-03-24 20:36:25 -07001538 /* Reset round-robin state, if necessary */
1539 if (fn->rr_ptr == rt)
1540 fn->rr_ptr = NULL;
1541
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001542 /* Remove this entry from other siblings */
1543 if (rt->rt6i_nsiblings) {
1544 struct rt6_info *sibling, *next_sibling;
1545
1546 list_for_each_entry_safe(sibling, next_sibling,
1547 &rt->rt6i_siblings, rt6i_siblings)
1548 sibling->rt6i_nsiblings--;
1549 rt->rt6i_nsiblings = 0;
1550 list_del_init(&rt->rt6i_siblings);
1551 }
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001554 read_lock(&net->ipv6.fib6_walker_lock);
1555 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (w->state == FWS_C && w->leaf == rt) {
1557 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001558 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001559 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 w->state = FWS_U;
1561 }
1562 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001563 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Changli Gaod8d1f302010-06-10 23:31:35 -07001565 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001568 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001570 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001571 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001574 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001576 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
David Ahern16a16cd2017-02-02 12:37:11 -08001577 if (!info->skip_notify)
1578 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 rt6_release(rt);
1580}
1581
Thomas Graf86872cb2006-08-22 00:01:08 -07001582int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Wei Wang4e587ea2017-08-25 15:03:10 -07001584 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1585 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001586 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct rt6_info **rtp;
1588
1589#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001590 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001591 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return -ENOENT;
1593 }
1594#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001595 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return -ENOENT;
1597
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001598 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David S. Miller507c9b12011-12-03 17:50:45 -05001600 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001601 struct fib6_node *pn = fn;
1602#ifdef CONFIG_IPV6_SUBTREES
1603 /* clones of this route might be in another subtree */
1604 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001605 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001606 pn = pn->parent;
1607 pn = pn->parent;
1608 }
1609#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001610 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 /*
1614 * Walk the leaf entries looking for ourself
1615 */
1616
Changli Gaod8d1f302010-06-10 23:31:35 -07001617 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001619 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return 0;
1621 }
1622 }
1623 return -ENOENT;
1624}
1625
1626/*
1627 * Tree traversal function.
1628 *
1629 * Certainly, it is not interrupt safe.
1630 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1631 * It means, that we can modify tree during walking
1632 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001633 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 *
1635 * It guarantees that every node will be traversed,
1636 * and that it will be traversed only once.
1637 *
1638 * Callback function w->func may return:
1639 * 0 -> continue walking.
1640 * positive value -> walking is suspended (used by tree dumps,
1641 * and probably by gc, if it will be split to several slices)
1642 * negative value -> terminate walking.
1643 *
1644 * The function itself returns:
1645 * 0 -> walk is complete.
1646 * >0 -> walk is incomplete (i.e. suspended)
1647 * <0 -> walk is terminated by an error.
1648 */
1649
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001650static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct fib6_node *fn, *pn;
1653
1654 for (;;) {
1655 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001656 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return 0;
1658
1659 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001660 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 w->state = FWS_C;
1662 w->leaf = fn->leaf;
1663 }
1664 switch (w->state) {
1665#ifdef CONFIG_IPV6_SUBTREES
1666 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001667 if (FIB6_SUBTREE(fn)) {
1668 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 continue;
1670 }
1671 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 case FWS_L:
1674 if (fn->left) {
1675 w->node = fn->left;
1676 w->state = FWS_INIT;
1677 continue;
1678 }
1679 w->state = FWS_R;
1680 case FWS_R:
1681 if (fn->right) {
1682 w->node = fn->right;
1683 w->state = FWS_INIT;
1684 continue;
1685 }
1686 w->state = FWS_C;
1687 w->leaf = fn->leaf;
1688 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001689 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001690 int err;
1691
Eric Dumazetfa809e22012-06-25 15:37:19 -07001692 if (w->skip) {
1693 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001694 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001695 }
1696
1697 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (err)
1699 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001700
1701 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 continue;
1703 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001704skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 w->state = FWS_U;
1706 case FWS_U:
1707 if (fn == w->root)
1708 return 0;
1709 pn = fn->parent;
1710 w->node = pn;
1711#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001712 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001713 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 w->state = FWS_L;
1715 continue;
1716 }
1717#endif
1718 if (pn->left == fn) {
1719 w->state = FWS_R;
1720 continue;
1721 }
1722 if (pn->right == fn) {
1723 w->state = FWS_C;
1724 w->leaf = w->node->leaf;
1725 continue;
1726 }
1727#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001728 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729#endif
1730 }
1731 }
1732}
1733
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001734static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 int res;
1737
1738 w->state = FWS_INIT;
1739 w->node = w->root;
1740
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001741 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 res = fib6_walk_continue(w);
1743 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001744 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return res;
1746}
1747
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001748static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int res;
1751 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001752 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001753 struct nl_info info = {
1754 .nl_net = c->net,
1755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001757 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1758 w->node->fn_sernum != c->sernum)
1759 w->node->fn_sernum = c->sernum;
1760
1761 if (!c->func) {
1762 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1763 w->leaf = NULL;
1764 return 0;
1765 }
1766
Changli Gaod8d1f302010-06-10 23:31:35 -07001767 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 res = c->func(rt, c->arg);
1769 if (res < 0) {
1770 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001771 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (res) {
1773#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001774 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001775 __func__, rt,
1776 rcu_access_pointer(rt->rt6i_node),
1777 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778#endif
1779 continue;
1780 }
1781 return 0;
1782 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001783 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 w->leaf = rt;
1786 return 0;
1787}
1788
1789/*
1790 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001791 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 * func is called on each route.
1793 * It may return -1 -> delete this route.
1794 * 0 -> continue walking
1795 *
1796 * prune==1 -> only immediate children of node (certainly,
1797 * ignoring pure split nodes) will be scanned.
1798 */
1799
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001800static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001801 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001802 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001804 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 c.w.root = root;
1807 c.w.func = fib6_clean_node;
1808 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001809 c.w.count = 0;
1810 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001812 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001814 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001816 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001819static void __fib6_clean_all(struct net *net,
1820 int (*func)(struct rt6_info *, void *),
1821 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001822{
Thomas Grafc71099a2006-08-04 23:20:06 -07001823 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001824 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001825 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001826
Patrick McHardy1b43af52006-08-10 23:11:17 -07001827 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001828 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001829 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001830 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001831 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001832 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001833 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001834 write_unlock_bh(&table->tb6_lock);
1835 }
1836 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001837 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001838}
1839
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001840void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1841 void *arg)
1842{
1843 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1844}
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1847{
1848 if (rt->rt6i_flags & RTF_CACHE) {
1849 RT6_TRACE("pruning clone %p\n", rt);
1850 return -1;
1851 }
1852
1853 return 0;
1854}
1855
Duan Jiong163cd4e2014-05-09 13:31:43 +08001856static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001858 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1859 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001860}
1861
1862static void fib6_flush_trees(struct net *net)
1863{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001864 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001865
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001866 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/*
1870 * Garbage collection
1871 */
1872
Michal Kubeček3570df92016-03-08 14:44:25 +01001873struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 int timeout;
1876 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001877};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879static int fib6_age(struct rt6_info *rt, void *arg)
1880{
Michal Kubeček3570df92016-03-08 14:44:25 +01001881 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 unsigned long now = jiffies;
1883
1884 /*
1885 * check addrconf expiration here.
1886 * Routes are expired even if they are in use.
1887 *
1888 * Also age clones. Note, that clones are aged out
1889 * only if they are not in use now.
1890 */
1891
David S. Millerd1918542011-12-28 20:19:20 -05001892 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1893 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return -1;
1896 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001897 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 } else if (rt->rt6i_flags & RTF_CACHE) {
Xin Long1e2ea8a2017-08-26 20:10:10 +08001899 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout))
1900 rt->dst.obsolete = DST_OBSOLETE_KILL;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001901 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Xin Long1e2ea8a2017-08-26 20:10:10 +08001902 rt->dst.obsolete == DST_OBSOLETE_KILL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 RT6_TRACE("aging clone %p\n", rt);
1904 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001905 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1906 struct neighbour *neigh;
1907 __u8 neigh_flags = 0;
1908
1909 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1910 if (neigh) {
1911 neigh_flags = neigh->flags;
1912 neigh_release(neigh);
1913 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001914 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001915 RT6_TRACE("purging route %p via non-router but gateway\n",
1916 rt);
1917 return -1;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001920 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
1923 return 0;
1924}
1925
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001926void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Michal Kubeček3570df92016-03-08 14:44:25 +01001928 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001929 unsigned long now;
1930
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001931 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001932 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1933 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001934 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1935 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001937 gc_args.timeout = expires ? (int)expires :
1938 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001939 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001940
Michal Kubeček3570df92016-03-08 14:44:25 +01001941 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001942 now = jiffies;
1943 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001946 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001947 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001948 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001949 else
1950 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001951 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001954static void fib6_gc_timer_cb(unsigned long arg)
1955{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001956 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001957}
1958
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001959static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001960{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001961 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001962 int err;
1963
1964 err = fib6_notifier_init(net);
1965 if (err)
1966 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001967
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001968 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001969 rwlock_init(&net->ipv6.fib6_walker_lock);
1970 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001971 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001972
Benjamin Theryc5728722008-03-03 23:34:17 -08001973 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1974 if (!net->ipv6.rt6_stats)
1975 goto out_timer;
1976
Eric Dumazet10da66f2010-10-13 08:22:03 +00001977 /* Avoid false sharing : Use at least a full cache line */
1978 size = max_t(size_t, size, L1_CACHE_BYTES);
1979
1980 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001981 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001982 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001983
1984 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1985 GFP_KERNEL);
1986 if (!net->ipv6.fib6_main_tbl)
1987 goto out_fib_table_hash;
1988
1989 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001990 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001991 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1992 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001993 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001994
1995#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1996 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1997 GFP_KERNEL);
1998 if (!net->ipv6.fib6_local_tbl)
1999 goto out_fib6_main_tbl;
2000 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002001 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002002 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2003 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002004 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002005#endif
2006 fib6_tables_init(net);
2007
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002008 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002009
2010#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2011out_fib6_main_tbl:
2012 kfree(net->ipv6.fib6_main_tbl);
2013#endif
2014out_fib_table_hash:
2015 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002016out_rt6_stats:
2017 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002018out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002019 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002020 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002021}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002022
2023static void fib6_net_exit(struct net *net)
2024{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002025 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002026 del_timer_sync(&net->ipv6.ip6_fib_timer);
2027
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002028#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07002029 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002030 kfree(net->ipv6.fib6_local_tbl);
2031#endif
David S. Miller8e773272012-06-11 00:01:52 -07002032 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002033 kfree(net->ipv6.fib6_main_tbl);
2034 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002035 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002036 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002037}
2038
2039static struct pernet_operations fib6_net_ops = {
2040 .init = fib6_net_init,
2041 .exit = fib6_net_exit,
2042};
2043
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002044int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002046 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2049 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002050 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002051 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002052 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002053 goto out;
2054
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002055 ret = register_pernet_subsys(&fib6_net_ops);
2056 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002057 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002058
2059 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
Florian Westphalb97bac62017-08-09 20:41:48 +02002060 0);
David S. Millere8803b62012-06-16 01:12:19 -07002061 if (ret)
2062 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002063
2064 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002065out:
2066 return ret;
2067
David S. Millere8803b62012-06-16 01:12:19 -07002068out_unregister_subsys:
2069 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002070out_kmem_cache_create:
2071 kmem_cache_destroy(fib6_node_kmem);
2072 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075void fib6_gc_cleanup(void)
2076{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002077 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 kmem_cache_destroy(fib6_node_kmem);
2079}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002080
2081#ifdef CONFIG_PROC_FS
2082
2083struct ipv6_route_iter {
2084 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002085 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002086 loff_t skip;
2087 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002088 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002089};
2090
2091static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2092{
2093 struct rt6_info *rt = v;
2094 struct ipv6_route_iter *iter = seq->private;
2095
2096 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2097
2098#ifdef CONFIG_IPV6_SUBTREES
2099 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2100#else
2101 seq_puts(seq, "00000000000000000000000000000000 00 ");
2102#endif
2103 if (rt->rt6i_flags & RTF_GATEWAY)
2104 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2105 else
2106 seq_puts(seq, "00000000000000000000000000000000");
2107
2108 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2109 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2110 rt->dst.__use, rt->rt6i_flags,
2111 rt->dst.dev ? rt->dst.dev->name : "");
2112 iter->w.leaf = NULL;
2113 return 0;
2114}
2115
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002116static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002117{
2118 struct ipv6_route_iter *iter = w->args;
2119
2120 if (!iter->skip)
2121 return 1;
2122
2123 do {
2124 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2125 iter->skip--;
2126 if (!iter->skip && iter->w.leaf)
2127 return 1;
2128 } while (iter->w.leaf);
2129
2130 return 0;
2131}
2132
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002133static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2134 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002135{
2136 memset(&iter->w, 0, sizeof(iter->w));
2137 iter->w.func = ipv6_route_yield;
2138 iter->w.root = &iter->tbl->tb6_root;
2139 iter->w.state = FWS_INIT;
2140 iter->w.node = iter->w.root;
2141 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002142 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002143 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002144 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002145}
2146
2147static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2148 struct net *net)
2149{
2150 unsigned int h;
2151 struct hlist_node *node;
2152
2153 if (tbl) {
2154 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2155 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2156 } else {
2157 h = 0;
2158 node = NULL;
2159 }
2160
2161 while (!node && h < FIB6_TABLE_HASHSZ) {
2162 node = rcu_dereference_bh(
2163 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2164 }
2165 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2166}
2167
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002168static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2169{
2170 if (iter->sernum != iter->w.root->fn_sernum) {
2171 iter->sernum = iter->w.root->fn_sernum;
2172 iter->w.state = FWS_INIT;
2173 iter->w.node = iter->w.root;
2174 WARN_ON(iter->w.skip);
2175 iter->w.skip = iter->w.count;
2176 }
2177}
2178
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002179static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2180{
2181 int r;
2182 struct rt6_info *n;
2183 struct net *net = seq_file_net(seq);
2184 struct ipv6_route_iter *iter = seq->private;
2185
2186 if (!v)
2187 goto iter_table;
2188
2189 n = ((struct rt6_info *)v)->dst.rt6_next;
2190 if (n) {
2191 ++*pos;
2192 return n;
2193 }
2194
2195iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002196 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002197 read_lock(&iter->tbl->tb6_lock);
2198 r = fib6_walk_continue(&iter->w);
2199 read_unlock(&iter->tbl->tb6_lock);
2200 if (r > 0) {
2201 if (v)
2202 ++*pos;
2203 return iter->w.leaf;
2204 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002205 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002206 return NULL;
2207 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002208 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002209
2210 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2211 if (!iter->tbl)
2212 return NULL;
2213
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002214 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002215 goto iter_table;
2216}
2217
2218static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2219 __acquires(RCU_BH)
2220{
2221 struct net *net = seq_file_net(seq);
2222 struct ipv6_route_iter *iter = seq->private;
2223
2224 rcu_read_lock_bh();
2225 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2226 iter->skip = *pos;
2227
2228 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002229 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002230 return ipv6_route_seq_next(seq, NULL, pos);
2231 } else {
2232 return NULL;
2233 }
2234}
2235
2236static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2237{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002238 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002239 return w->node && !(w->state == FWS_U && w->node == w->root);
2240}
2241
2242static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2243 __releases(RCU_BH)
2244{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002245 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002246 struct ipv6_route_iter *iter = seq->private;
2247
2248 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002249 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002250
2251 rcu_read_unlock_bh();
2252}
2253
2254static const struct seq_operations ipv6_route_seq_ops = {
2255 .start = ipv6_route_seq_start,
2256 .next = ipv6_route_seq_next,
2257 .stop = ipv6_route_seq_stop,
2258 .show = ipv6_route_seq_show
2259};
2260
2261int ipv6_route_open(struct inode *inode, struct file *file)
2262{
2263 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2264 sizeof(struct ipv6_route_iter));
2265}
2266
2267#endif /* CONFIG_PROC_FS */