blob: fe193b48ef6126e895c3e5d1d602103aeeea186b [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
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200152static void node_free(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 kmem_cache_free(fib6_node_kmem, fn);
155}
156
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700157static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
158{
159 int cpu;
160
161 if (!non_pcpu_rt->rt6i_pcpu)
162 return;
163
164 for_each_possible_cpu(cpu) {
165 struct rt6_info **ppcpu_rt;
166 struct rt6_info *pcpu_rt;
167
168 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
169 pcpu_rt = *ppcpu_rt;
170 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700171 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700172 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700173 *ppcpu_rt = NULL;
174 }
175 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700176
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700177 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700178 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700179}
180
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200181static void rt6_release(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700183 if (atomic_dec_and_test(&rt->rt6i_ref)) {
184 rt6_free_pcpu(rt);
Wei Wang95145282017-06-17 10:42:34 -0700185 dst_dev_put(&rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700186 dst_release(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800190static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700191{
192 unsigned int h;
193
Thomas Graf375216a2006-10-21 20:20:54 -0700194 /*
195 * Initialize table lock at a single place to give lockdep a key,
196 * tables aren't visible prior to being linked to the list.
197 */
198 rwlock_init(&tb->tb6_lock);
199
Neil Hormana33bc5c2009-07-30 18:52:15 -0700200 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700201
202 /*
203 * No protection necessary, this is the only list mutatation
204 * operation, tables never disappear once they exist.
205 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800206 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700207}
208
209#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800210
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800211static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700212{
213 struct fib6_table *table;
214
215 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500216 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700217 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800218 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700219 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700220 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700221 }
222
223 return table;
224}
225
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800226struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700227{
228 struct fib6_table *tb;
229
230 if (id == 0)
231 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800232 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700233 if (tb)
234 return tb;
235
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800236 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500237 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800238 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700239
240 return tb;
241}
David Ahernb3b46632016-05-04 21:46:12 -0700242EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700243
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800244struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700245{
246 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700248 unsigned int h;
249
250 if (id == 0)
251 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700252 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700253 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800254 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800255 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700256 if (tb->tb6_id == id) {
257 rcu_read_unlock();
258 return tb;
259 }
260 }
261 rcu_read_unlock();
262
263 return NULL;
264}
David Ahernc4850682015-10-12 11:47:08 -0700265EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700266
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000267static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700268{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800269 fib6_link_table(net, net->ipv6.fib6_main_tbl);
270 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700271}
Thomas Grafc71099a2006-08-04 23:20:06 -0700272#else
273
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800274struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700275{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800276 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700277}
278
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800279struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700280{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800281 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700282}
283
David S. Miller4c9483b2011-03-12 16:22:43 -0500284struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800285 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700286{
lucienab997ad2015-10-23 15:36:53 +0800287 struct rt6_info *rt;
288
289 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300290 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800291 ip6_rt_put(rt);
292 rt = net->ipv6.ip6_null_entry;
293 dst_hold(&rt->dst);
294 }
295
296 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700297}
298
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000299static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700300{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800301 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700302}
303
304#endif
305
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200306unsigned int fib6_tables_seq_read(struct net *net)
307{
308 unsigned int h, fib_seq = 0;
309
310 rcu_read_lock();
311 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
312 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
313 struct fib6_table *tb;
314
315 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
316 read_lock_bh(&tb->tb6_lock);
317 fib_seq += tb->fib_seq;
318 read_unlock_bh(&tb->tb6_lock);
319 }
320 }
321 rcu_read_unlock();
322
323 return fib_seq;
324}
325
326static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
327 enum fib_event_type event_type,
328 struct rt6_info *rt)
329{
330 struct fib6_entry_notifier_info info = {
331 .rt = rt,
332 };
333
334 return call_fib6_notifier(nb, net, event_type, &info.info);
335}
336
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200337static int call_fib6_entry_notifiers(struct net *net,
338 enum fib_event_type event_type,
339 struct rt6_info *rt)
340{
341 struct fib6_entry_notifier_info info = {
342 .rt = rt,
343 };
344
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200345 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200346 return call_fib6_notifiers(net, event_type, &info.info);
347}
348
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200349struct fib6_dump_arg {
350 struct net *net;
351 struct notifier_block *nb;
352};
353
354static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
355{
356 if (rt == arg->net->ipv6.ip6_null_entry)
357 return;
358 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
359}
360
361static int fib6_node_dump(struct fib6_walker *w)
362{
363 struct rt6_info *rt;
364
365 for (rt = w->leaf; rt; rt = rt->dst.rt6_next)
366 fib6_rt_dump(rt, w->args);
367 w->leaf = NULL;
368 return 0;
369}
370
371static void fib6_table_dump(struct net *net, struct fib6_table *tb,
372 struct fib6_walker *w)
373{
374 w->root = &tb->tb6_root;
375 read_lock_bh(&tb->tb6_lock);
376 fib6_walk(net, w);
377 read_unlock_bh(&tb->tb6_lock);
378}
379
380/* Called with rcu_read_lock() */
381int fib6_tables_dump(struct net *net, struct notifier_block *nb)
382{
383 struct fib6_dump_arg arg;
384 struct fib6_walker *w;
385 unsigned int h;
386
387 w = kzalloc(sizeof(*w), GFP_ATOMIC);
388 if (!w)
389 return -ENOMEM;
390
391 w->func = fib6_node_dump;
392 arg.net = net;
393 arg.nb = nb;
394 w->args = &arg;
395
396 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
397 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
398 struct fib6_table *tb;
399
400 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
401 fib6_table_dump(net, tb, w);
402 }
403
404 kfree(w);
405
406 return 0;
407}
408
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200409static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700410{
411 int res;
412 struct rt6_info *rt;
413
Changli Gaod8d1f302010-06-10 23:31:35 -0700414 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700415 res = rt6_dump_route(rt, w->args);
416 if (res < 0) {
417 /* Frame is full, suspend walking */
418 w->leaf = rt;
419 return 1;
420 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800421
422 /* Multipath routes are dumped in one route with the
423 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
424 * last sibling of this route (no need to dump the
425 * sibling routes again)
426 */
427 if (rt->rt6i_nsiblings)
428 rt = list_last_entry(&rt->rt6i_siblings,
429 struct rt6_info,
430 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700431 }
432 w->leaf = NULL;
433 return 0;
434}
435
436static void fib6_dump_end(struct netlink_callback *cb)
437{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100438 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200439 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700440
441 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800442 if (cb->args[4]) {
443 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100444 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800445 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700446 cb->args[2] = 0;
447 kfree(w);
448 }
Wang Yufen437de072014-03-28 12:07:04 +0800449 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700450 cb->args[1] = 3;
451}
452
453static int fib6_dump_done(struct netlink_callback *cb)
454{
455 fib6_dump_end(cb);
456 return cb->done ? cb->done(cb) : 0;
457}
458
459static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
460 struct netlink_callback *cb)
461{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100462 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200463 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700464 int res;
465
466 w = (void *)cb->args[2];
467 w->root = &table->tb6_root;
468
469 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000470 w->count = 0;
471 w->skip = 0;
472
Patrick McHardy1b43af52006-08-10 23:11:17 -0700473 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100474 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700475 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000476 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700477 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000478 cb->args[5] = w->root->fn_sernum;
479 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700480 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000481 if (cb->args[5] != w->root->fn_sernum) {
482 /* Begin at the root if the tree changed */
483 cb->args[5] = w->root->fn_sernum;
484 w->state = FWS_INIT;
485 w->node = w->root;
486 w->skip = w->count;
487 } else
488 w->skip = 0;
489
Patrick McHardy1b43af52006-08-10 23:11:17 -0700490 read_lock_bh(&table->tb6_lock);
491 res = fib6_walk_continue(w);
492 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800493 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100494 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800495 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700496 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700497 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800498
Patrick McHardy1b43af52006-08-10 23:11:17 -0700499 return res;
500}
501
Thomas Grafc127ea22007-03-22 11:58:32 -0700502static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700503{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900504 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700505 unsigned int h, s_h;
506 unsigned int e = 0, s_e;
507 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200508 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700509 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800510 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700511 int res = 0;
512
513 s_h = cb->args[0];
514 s_e = cb->args[1];
515
516 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500517 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700518 /* New dump:
519 *
520 * 1. hook callback destructor.
521 */
522 cb->args[3] = (long)cb->done;
523 cb->done = fib6_dump_done;
524
525 /*
526 * 2. allocate and initialize walker.
527 */
528 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500529 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700530 return -ENOMEM;
531 w->func = fib6_dump_node;
532 cb->args[2] = (long)w;
533 }
534
535 arg.skb = skb;
536 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700537 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700538 w->args = &arg;
539
Eric Dumazete67f88d2011-04-27 22:56:07 +0000540 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700541 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700542 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800543 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800544 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700545 if (e < s_e)
546 goto next;
547 res = fib6_dump_table(tb, skb, cb);
548 if (res != 0)
549 goto out;
550next:
551 e++;
552 }
553 }
554out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000555 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700556 cb->args[1] = e;
557 cb->args[0] = h;
558
559 res = res < 0 ? res : skb->len;
560 if (res <= 0)
561 fib6_dump_end(cb);
562 return res;
563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565/*
566 * Routing Table
567 *
568 * return the appropriate node for a routing tree "add" operation
569 * by either creating and inserting or by returning an existing
570 * node.
571 */
572
fan.du9225b232013-07-22 14:21:09 +0800573static struct fib6_node *fib6_add_1(struct fib6_node *root,
574 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000575 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600576 int replace_required, int sernum,
577 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct fib6_node *fn, *in, *ln;
580 struct fib6_node *pn = NULL;
581 struct rt6key *key;
582 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900583 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 RT6_TRACE("fib6_add_1\n");
586
587 /* insert node in tree */
588
589 fn = root;
590
591 do {
592 key = (struct rt6key *)((u8 *)fn->leaf + offset);
593
594 /*
595 * Prefix match
596 */
597 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000598 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000599 if (!allow_create) {
600 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600601 NL_SET_ERR_MSG(extack,
602 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000603 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000604 return ERR_PTR(-ENOENT);
605 }
Joe Perchesf3213832012-05-15 14:11:53 +0000606 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000609 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Exact match ?
613 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (plen == fn->fn_bit) {
616 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500617 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 rt6_release(fn->leaf);
619 fn->leaf = NULL;
620 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return fn;
625 }
626
627 /*
628 * We have more bits to go
629 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Try to walk down on tree. */
632 fn->fn_sernum = sernum;
633 dir = addr_bit_set(addr, fn->fn_bit);
634 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800635 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } while (fn);
637
Matti Vaittinen14df0152011-11-16 21:18:02 +0000638 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000639 /* We should not create new node because
640 * NLM_F_REPLACE was specified without NLM_F_CREATE
641 * I assume it is safe to require NLM_F_CREATE when
642 * REPLACE flag is used! Later we may want to remove the
643 * check for replace_required, because according
644 * to netlink specification, NLM_F_CREATE
645 * MUST be specified if new route is created.
646 * That would keep IPv6 consistent with IPv4
647 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000648 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600649 NL_SET_ERR_MSG(extack,
650 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000651 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000652 return ERR_PTR(-ENOENT);
653 }
Joe Perchesf3213832012-05-15 14:11:53 +0000654 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * We walked to the bottom of tree.
658 * Create new leaf node without children.
659 */
660
661 ln = node_alloc();
662
David S. Miller507c9b12011-12-03 17:50:45 -0500663 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000664 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ln->parent = pn;
668 ln->fn_sernum = sernum;
669
670 if (dir)
671 pn->right = ln;
672 else
673 pn->left = ln;
674
675 return ln;
676
677
678insert_above:
679 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900680 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * we have a less significant route.
682 * we've to insert an intermediate node on the list
683 * this new node will point to the one we need to create
684 * and the current
685 */
686
687 pn = fn->parent;
688
689 /* find 1st bit in difference between the 2 addrs.
690
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800691 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 but if it is >= plen, the value is ignored in any case.
693 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900694
fan.du9225b232013-07-22 14:21:09 +0800695 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900697 /*
698 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * / \
700 * (new leaf node)[ln] (old node)[fn]
701 */
702 if (plen > bit) {
703 in = node_alloc();
704 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900705
David S. Miller507c9b12011-12-03 17:50:45 -0500706 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (in)
708 node_free(in);
709 if (ln)
710 node_free(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000711 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900714 /*
715 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * RTN_RTINFO will
717 * be off since that an address that chooses one of
718 * the branches would not match less specific routes
719 * in the other branch
720 */
721
722 in->fn_bit = bit;
723
724 in->parent = pn;
725 in->leaf = fn->leaf;
726 atomic_inc(&in->leaf->rt6i_ref);
727
728 in->fn_sernum = sernum;
729
730 /* update parent pointer */
731 if (dir)
732 pn->right = in;
733 else
734 pn->left = in;
735
736 ln->fn_bit = plen;
737
738 ln->parent = in;
739 fn->parent = in;
740
741 ln->fn_sernum = sernum;
742
743 if (addr_bit_set(addr, bit)) {
744 in->right = ln;
745 in->left = fn;
746 } else {
747 in->left = ln;
748 in->right = fn;
749 }
750 } else { /* plen <= bit */
751
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900752 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * (new leaf node)[ln]
754 * / \
755 * (old node)[fn] NULL
756 */
757
758 ln = node_alloc();
759
David S. Miller507c9b12011-12-03 17:50:45 -0500760 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000761 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 ln->fn_bit = plen;
764
765 ln->parent = pn;
766
767 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (dir)
770 pn->right = ln;
771 else
772 pn->left = ln;
773
774 if (addr_bit_set(&key->addr, plen))
775 ln->right = fn;
776 else
777 ln->left = fn;
778
779 fn->parent = ln;
780 }
781 return ln;
782}
783
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200784static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200785{
786 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
787 RTF_GATEWAY;
788}
789
Florian Westphale715b6d2015-01-05 23:57:44 +0100790static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100791{
Florian Westphale715b6d2015-01-05 23:57:44 +0100792 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100793
Florian Westphale715b6d2015-01-05 23:57:44 +0100794 for (i = 0; i < RTAX_MAX; i++) {
795 if (test_bit(i, mxc->mx_valid))
796 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100797 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100798}
799
800static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
801{
802 if (!mxc->mx)
803 return 0;
804
805 if (dst->flags & DST_HOST) {
806 u32 *mp = dst_metrics_write_ptr(dst);
807
808 if (unlikely(!mp))
809 return -ENOMEM;
810
811 fib6_copy_metrics(mp, mxc);
812 } else {
813 dst_init_metrics(dst, mxc->mx, false);
814
815 /* We've stolen mx now. */
816 mxc->mx = NULL;
817 }
818
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100819 return 0;
820}
821
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100822static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
823 struct net *net)
824{
825 if (atomic_read(&rt->rt6i_ref) != 1) {
826 /* This route is used as dummy address holder in some split
827 * nodes. It is not leaked, but it still holds other resources,
828 * which must be released in time. So, scan ascendant nodes
829 * and replace dummy references to this route with references
830 * to still alive ones.
831 */
832 while (fn) {
833 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
834 fn->leaf = fib6_find_prefix(net, fn);
835 atomic_inc(&fn->leaf->rt6i_ref);
836 rt6_release(rt);
837 }
838 fn = fn->parent;
839 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100840 }
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/*
844 * Insert routing information in a node.
845 */
846
847static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100848 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct rt6_info *iter = NULL;
851 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200852 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500853 int replace = (info->nlh &&
854 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
855 int add = (!info->nlh ||
856 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000857 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200858 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200859 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100860 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
David Ahern1f5e29c2017-01-31 16:51:37 -0800862 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
863 nlflags |= NLM_F_APPEND;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ins = &fn->leaf;
866
David S. Miller507c9b12011-12-03 17:50:45 -0500867 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * Search for duplicates
870 */
871
872 if (iter->rt6i_metric == rt->rt6i_metric) {
873 /*
874 * Same priority level
875 */
David S. Miller507c9b12011-12-03 17:50:45 -0500876 if (info->nlh &&
877 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000878 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200879
880 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000881 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200882 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
883 found++;
884 break;
885 }
886 if (rt_can_ecmp)
887 fallback_ins = fallback_ins ?: ins;
888 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
David Ahernf06b7542017-07-05 14:41:46 -0600891 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000892 if (rt->rt6i_nsiblings)
893 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500894 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000896 if (!(rt->rt6i_flags & RTF_EXPIRES))
897 rt6_clean_expires(iter);
898 else
899 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700900 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EEXIST;
902 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000903 /* If we have the same destination and the same metric,
904 * but not the same gateway, then the route we try to
905 * add is sibling to this route, increment our counter
906 * of siblings, and later we will add our route to the
907 * list.
908 * Only static routes (which don't have flag
909 * RTF_EXPIRES) are used for ECMPv6.
910 *
911 * To avoid long list, we only had siblings if the
912 * route have a gateway.
913 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200914 if (rt_can_ecmp &&
915 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000916 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919 if (iter->rt6i_metric > rt->rt6i_metric)
920 break;
921
Michal Kubeček27596472015-05-18 20:54:00 +0200922next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700923 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
Michal Kubeček27596472015-05-18 20:54:00 +0200926 if (fallback_ins && !found) {
927 /* No ECMP-able route found, replace first non-ECMP one */
928 ins = fallback_ins;
929 iter = *ins;
930 found++;
931 }
932
David S. Millerf11e6652007-03-24 20:36:25 -0700933 /* Reset round-robin state, if necessary */
934 if (ins == &fn->leaf)
935 fn->rr_ptr = NULL;
936
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000937 /* Link this route to others same route. */
938 if (rt->rt6i_nsiblings) {
939 unsigned int rt6i_nsiblings;
940 struct rt6_info *sibling, *temp_sibling;
941
942 /* Find the first route that have the same metric */
943 sibling = fn->leaf;
944 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200945 if (sibling->rt6i_metric == rt->rt6i_metric &&
946 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000947 list_add_tail(&rt->rt6i_siblings,
948 &sibling->rt6i_siblings);
949 break;
950 }
951 sibling = sibling->dst.rt6_next;
952 }
953 /* For each sibling in the list, increment the counter of
954 * siblings. BUG() if counters does not match, list of siblings
955 * is broken!
956 */
957 rt6i_nsiblings = 0;
958 list_for_each_entry_safe(sibling, temp_sibling,
959 &rt->rt6i_siblings, rt6i_siblings) {
960 sibling->rt6i_nsiblings++;
961 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
962 rt6i_nsiblings++;
963 }
964 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
965 }
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * insert node
969 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000970 if (!replace) {
971 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000972 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000974add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200975 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100976 err = fib6_commit_metrics(&rt->dst, mxc);
977 if (err)
978 return err;
979
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000980 rt->dst.rt6_next = iter;
981 *ins = rt;
982 rt->rt6i_node = fn;
983 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200984 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
985 rt);
David Ahern3b1137f2017-02-02 12:37:10 -0800986 if (!info->skip_notify)
987 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000988 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
David S. Miller507c9b12011-12-03 17:50:45 -0500990 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000991 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
992 fn->fn_flags |= RTN_RTINFO;
993 }
994
995 } else {
Michal Kubeček27596472015-05-18 20:54:00 +0200996 int nsiblings;
997
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000998 if (!found) {
999 if (add)
1000 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001001 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001002 return -ENOENT;
1003 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001004
1005 err = fib6_commit_metrics(&rt->dst, mxc);
1006 if (err)
1007 return err;
1008
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001009 *ins = rt;
1010 rt->rt6i_node = fn;
1011 rt->dst.rt6_next = iter->dst.rt6_next;
1012 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001013 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1014 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001015 if (!info->skip_notify)
1016 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001017 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001018 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1019 fn->fn_flags |= RTN_RTINFO;
1020 }
Michal Kubeček27596472015-05-18 20:54:00 +02001021 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001022 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001023 fib6_purge_rt(iter, fn, info->nl_net);
1024 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001025
1026 if (nsiblings) {
1027 /* Replacing an ECMP route, remove all siblings */
1028 ins = &rt->dst.rt6_next;
1029 iter = *ins;
1030 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001031 if (iter->rt6i_metric > rt->rt6i_metric)
1032 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001033 if (rt6_qualify_for_ecmp(iter)) {
1034 *ins = iter->dst.rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001035 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001036 fib6_purge_rt(iter, fn, info->nl_net);
1037 rt6_release(iter);
1038 nsiblings--;
1039 } else {
1040 ins = &iter->dst.rt6_next;
1041 }
1042 iter = *ins;
1043 }
1044 WARN_ON(nsiblings != 0);
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
1048 return 0;
1049}
1050
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001051static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001053 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001054 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001055 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001056 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001059void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001061 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1062 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001063 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/*
1067 * Add routing information to the routing tree.
1068 * <destination addr>/<source addr>
1069 * with source addr info in sub-trees
1070 */
1071
Florian Westphale715b6d2015-01-05 23:57:44 +01001072int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001073 struct nl_info *info, struct mx6_config *mxc,
1074 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001076 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001078 int allow_create = 1;
1079 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001080 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001081
Wei Wanga4c2fd72017-06-17 10:42:42 -07001082 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001083 return -EINVAL;
1084
David S. Miller507c9b12011-12-03 17:50:45 -05001085 if (info->nlh) {
1086 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001087 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001088 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001089 replace_required = 1;
1090 }
1091 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001092 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
fan.du9225b232013-07-22 14:21:09 +08001094 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1095 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -06001096 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001097 if (IS_ERR(fn)) {
1098 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001099 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001103 pn = fn;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#ifdef CONFIG_IPV6_SUBTREES
1106 if (rt->rt6i_src.plen) {
1107 struct fib6_node *sn;
1108
David S. Miller507c9b12011-12-03 17:50:45 -05001109 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct fib6_node *sfn;
1111
1112 /*
1113 * Create subtree.
1114 *
1115 * fn[main tree]
1116 * |
1117 * sfn[subtree root]
1118 * \
1119 * sn[new leaf node]
1120 */
1121
1122 /* Create subtree root node */
1123 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001124 if (!sfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto st_failure;
1126
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001127 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1128 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001130 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* Now add the first leaf node to new subtree */
1133
1134 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001135 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001136 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001137 allow_create, replace_required, sernum,
1138 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001140 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* If it is failed, discard just allocated
1142 root, and then (in st_failure) stale node
1143 in main tree.
1144 */
1145 node_free(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001146 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 goto st_failure;
1148 }
1149
1150 /* Now link new subtree to main tree */
1151 sfn->parent = fn;
1152 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } else {
1154 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001155 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001156 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001157 allow_create, replace_required, sernum,
1158 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001160 if (IS_ERR(sn)) {
1161 err = PTR_ERR(sn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 goto st_failure;
Lin Ming188c5172012-09-25 15:17:07 +00001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
David S. Miller507c9b12011-12-03 17:50:45 -05001166 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001167 fn->leaf = rt;
1168 atomic_inc(&rt->rt6i_ref);
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 fn = sn;
1171 }
1172#endif
1173
Florian Westphale715b6d2015-01-05 23:57:44 +01001174 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001175 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001176 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001177 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001178 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
1181out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001182 if (err) {
1183#ifdef CONFIG_IPV6_SUBTREES
1184 /*
1185 * If fib6_add_1 has cleared the old leaf pointer in the
1186 * super-tree leaf node we have to find a new one for it.
1187 */
David S. Miller3c051232008-04-18 01:46:19 -07001188 if (pn != fn && pn->leaf == rt) {
1189 pn->leaf = NULL;
1190 atomic_dec(&rt->rt6i_ref);
1191 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001192 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001193 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001194#if RT6_DEBUG >= 2
1195 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001196 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001197 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001198 }
1199#endif
1200 atomic_inc(&pn->leaf->rt6i_ref);
1201 }
1202#endif
Wei Wang1cfb71e2017-06-17 10:42:33 -07001203 /* Always release dst as dst->__refcnt is guaranteed
1204 * to be taken before entering this function
1205 */
Wei Wang587fea72017-06-17 10:42:36 -07001206 dst_release_immediate(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return err;
1209
1210#ifdef CONFIG_IPV6_SUBTREES
1211 /* Subtree creation failed, probably main tree node
1212 is orphan. If it is, shoot it.
1213 */
1214st_failure:
1215 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001216 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001217 /* Always release dst as dst->__refcnt is guaranteed
1218 * to be taken before entering this function
1219 */
Wei Wang587fea72017-06-17 10:42:36 -07001220 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return err;
1222#endif
1223}
1224
1225/*
1226 * Routing tree lookup
1227 *
1228 */
1229
1230struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001231 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001232 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233};
1234
Wang Yufen437de072014-03-28 12:07:04 +08001235static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1236 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001239 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001241 if (unlikely(args->offset == 0))
1242 return NULL;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /*
1245 * Descend on a tree
1246 */
1247
1248 fn = root;
1249
1250 for (;;) {
1251 struct fib6_node *next;
1252
1253 dir = addr_bit_set(args->addr, fn->fn_bit);
1254
1255 next = dir ? fn->right : fn->left;
1256
1257 if (next) {
1258 fn = next;
1259 continue;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 break;
1262 }
1263
David S. Miller507c9b12011-12-03 17:50:45 -05001264 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001265 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct rt6key *key;
1267
1268 key = (struct rt6key *) ((u8 *) fn->leaf +
1269 args->offset);
1270
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001271 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1272#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001273 if (fn->subtree) {
1274 struct fib6_node *sfn;
1275 sfn = fib6_lookup_1(fn->subtree,
1276 args + 1);
1277 if (!sfn)
1278 goto backtrack;
1279 fn = sfn;
1280 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001281#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001282 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001283 return fn;
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001286#ifdef CONFIG_IPV6_SUBTREES
1287backtrack:
1288#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001289 if (fn->fn_flags & RTN_ROOT)
1290 break;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 fn = fn->parent;
1293 }
1294
1295 return NULL;
1296}
1297
Wang Yufen437de072014-03-28 12:07:04 +08001298struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1299 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001302 struct lookup_args args[] = {
1303 {
1304 .offset = offsetof(struct rt6_info, rt6i_dst),
1305 .addr = daddr,
1306 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001308 {
1309 .offset = offsetof(struct rt6_info, rt6i_src),
1310 .addr = saddr,
1311 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001313 {
1314 .offset = 0, /* sentinel */
1315 }
1316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001318 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001319 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 fn = root;
1321
1322 return fn;
1323}
1324
1325/*
1326 * Get node with specified destination prefix (and source prefix,
1327 * if subtrees are used)
1328 */
1329
1330
Wang Yufen437de072014-03-28 12:07:04 +08001331static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1332 const struct in6_addr *addr,
1333 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct fib6_node *fn;
1336
1337 for (fn = root; fn ; ) {
1338 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1339
1340 /*
1341 * Prefix match
1342 */
1343 if (plen < fn->fn_bit ||
1344 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1345 return NULL;
1346
1347 if (plen == fn->fn_bit)
1348 return fn;
1349
1350 /*
1351 * We have more bits to go
1352 */
1353 if (addr_bit_set(addr, fn->fn_bit))
1354 fn = fn->right;
1355 else
1356 fn = fn->left;
1357 }
1358 return NULL;
1359}
1360
Wang Yufen437de072014-03-28 12:07:04 +08001361struct fib6_node *fib6_locate(struct fib6_node *root,
1362 const struct in6_addr *daddr, int dst_len,
1363 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct fib6_node *fn;
1366
1367 fn = fib6_locate_1(root, daddr, dst_len,
1368 offsetof(struct rt6_info, rt6i_dst));
1369
1370#ifdef CONFIG_IPV6_SUBTREES
1371 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001372 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001373 if (fn && fn->subtree)
1374 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 offsetof(struct rt6_info, rt6i_src));
1376 }
1377#endif
1378
David S. Miller507c9b12011-12-03 17:50:45 -05001379 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return fn;
1381
1382 return NULL;
1383}
1384
1385
1386/*
1387 * Deletion
1388 *
1389 */
1390
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001391static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
David S. Miller507c9b12011-12-03 17:50:45 -05001393 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001394 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
David S. Miller507c9b12011-12-03 17:50:45 -05001396 while (fn) {
1397 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001399 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return fn->right->leaf;
1401
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001402 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 return NULL;
1405}
1406
1407/*
1408 * Called to trim the tree of intermediate nodes when possible. "fn"
1409 * is the node we want to try and remove.
1410 */
1411
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001412static struct fib6_node *fib6_repair_tree(struct net *net,
1413 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 int children;
1416 int nstate;
1417 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001418 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int iter = 0;
1420
1421 for (;;) {
1422 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1423 iter++;
1424
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001425 WARN_ON(fn->fn_flags & RTN_RTINFO);
1426 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001427 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 children = 0;
1430 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001431 if (fn->right)
1432 child = fn->right, children |= 1;
1433 if (fn->left)
1434 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001436 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#ifdef CONFIG_IPV6_SUBTREES
1438 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001439 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440#endif
1441 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001442 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001444 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001445 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001446 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448#endif
1449 atomic_inc(&fn->leaf->rt6i_ref);
1450 return fn->parent;
1451 }
1452
1453 pn = fn->parent;
1454#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001455 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001456 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001457 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 nstate = FWS_L;
1459 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001460 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001462 if (pn->right == fn)
1463 pn->right = child;
1464 else if (pn->left == fn)
1465 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001467 else
1468 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#endif
1470 if (child)
1471 child->parent = pn;
1472 nstate = FWS_R;
1473#ifdef CONFIG_IPV6_SUBTREES
1474 }
1475#endif
1476
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001477 read_lock(&net->ipv6.fib6_walker_lock);
1478 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001479 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (w->root == fn) {
1481 w->root = w->node = NULL;
1482 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1483 } else if (w->node == fn) {
1484 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1485 w->node = pn;
1486 w->state = nstate;
1487 }
1488 } else {
1489 if (w->root == fn) {
1490 w->root = child;
1491 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1492 }
1493 if (w->node == fn) {
1494 w->node = child;
1495 if (children&2) {
1496 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001497 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 } else {
1499 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001500 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502 }
1503 }
1504 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001505 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001508 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return pn;
1510
1511 rt6_release(pn->leaf);
1512 pn->leaf = NULL;
1513 fn = pn;
1514 }
1515}
1516
1517static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001518 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001520 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001522 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 RT6_TRACE("fib6_del_route\n");
1525
1526 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001527 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001529 net->ipv6.rt6_stats->fib_rt_entries--;
1530 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
David S. Millerf11e6652007-03-24 20:36:25 -07001532 /* Reset round-robin state, if necessary */
1533 if (fn->rr_ptr == rt)
1534 fn->rr_ptr = NULL;
1535
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001536 /* Remove this entry from other siblings */
1537 if (rt->rt6i_nsiblings) {
1538 struct rt6_info *sibling, *next_sibling;
1539
1540 list_for_each_entry_safe(sibling, next_sibling,
1541 &rt->rt6i_siblings, rt6i_siblings)
1542 sibling->rt6i_nsiblings--;
1543 rt->rt6i_nsiblings = 0;
1544 list_del_init(&rt->rt6i_siblings);
1545 }
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001548 read_lock(&net->ipv6.fib6_walker_lock);
1549 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (w->state == FWS_C && w->leaf == rt) {
1551 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001552 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001553 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 w->state = FWS_U;
1555 }
1556 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001557 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Changli Gaod8d1f302010-06-10 23:31:35 -07001559 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001562 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001564 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001565 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001568 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001570 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
David Ahern16a16cd2017-02-02 12:37:11 -08001571 if (!info->skip_notify)
1572 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 rt6_release(rt);
1574}
1575
Thomas Graf86872cb2006-08-22 00:01:08 -07001576int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001578 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct fib6_node *fn = rt->rt6i_node;
1580 struct rt6_info **rtp;
1581
1582#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001583 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001584 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -ENOENT;
1586 }
1587#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001588 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return -ENOENT;
1590
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001591 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
David S. Miller507c9b12011-12-03 17:50:45 -05001593 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001594 struct fib6_node *pn = fn;
1595#ifdef CONFIG_IPV6_SUBTREES
1596 /* clones of this route might be in another subtree */
1597 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001598 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001599 pn = pn->parent;
1600 pn = pn->parent;
1601 }
1602#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001603 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 /*
1607 * Walk the leaf entries looking for ourself
1608 */
1609
Changli Gaod8d1f302010-06-10 23:31:35 -07001610 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001612 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
1614 }
1615 }
1616 return -ENOENT;
1617}
1618
1619/*
1620 * Tree traversal function.
1621 *
1622 * Certainly, it is not interrupt safe.
1623 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1624 * It means, that we can modify tree during walking
1625 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001626 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 *
1628 * It guarantees that every node will be traversed,
1629 * and that it will be traversed only once.
1630 *
1631 * Callback function w->func may return:
1632 * 0 -> continue walking.
1633 * positive value -> walking is suspended (used by tree dumps,
1634 * and probably by gc, if it will be split to several slices)
1635 * negative value -> terminate walking.
1636 *
1637 * The function itself returns:
1638 * 0 -> walk is complete.
1639 * >0 -> walk is incomplete (i.e. suspended)
1640 * <0 -> walk is terminated by an error.
1641 */
1642
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001643static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct fib6_node *fn, *pn;
1646
1647 for (;;) {
1648 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001649 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return 0;
1651
1652 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001653 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 w->state = FWS_C;
1655 w->leaf = fn->leaf;
1656 }
1657 switch (w->state) {
1658#ifdef CONFIG_IPV6_SUBTREES
1659 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001660 if (FIB6_SUBTREE(fn)) {
1661 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 continue;
1663 }
1664 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001665#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 case FWS_L:
1667 if (fn->left) {
1668 w->node = fn->left;
1669 w->state = FWS_INIT;
1670 continue;
1671 }
1672 w->state = FWS_R;
1673 case FWS_R:
1674 if (fn->right) {
1675 w->node = fn->right;
1676 w->state = FWS_INIT;
1677 continue;
1678 }
1679 w->state = FWS_C;
1680 w->leaf = fn->leaf;
1681 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001682 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001683 int err;
1684
Eric Dumazetfa809e22012-06-25 15:37:19 -07001685 if (w->skip) {
1686 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001687 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001688 }
1689
1690 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (err)
1692 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001693
1694 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 continue;
1696 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001697skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 w->state = FWS_U;
1699 case FWS_U:
1700 if (fn == w->root)
1701 return 0;
1702 pn = fn->parent;
1703 w->node = pn;
1704#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001705 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001706 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 w->state = FWS_L;
1708 continue;
1709 }
1710#endif
1711 if (pn->left == fn) {
1712 w->state = FWS_R;
1713 continue;
1714 }
1715 if (pn->right == fn) {
1716 w->state = FWS_C;
1717 w->leaf = w->node->leaf;
1718 continue;
1719 }
1720#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001721 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722#endif
1723 }
1724 }
1725}
1726
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001727static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 int res;
1730
1731 w->state = FWS_INIT;
1732 w->node = w->root;
1733
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001734 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 res = fib6_walk_continue(w);
1736 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001737 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return res;
1739}
1740
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001741static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 int res;
1744 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001745 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001746 struct nl_info info = {
1747 .nl_net = c->net,
1748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001750 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1751 w->node->fn_sernum != c->sernum)
1752 w->node->fn_sernum = c->sernum;
1753
1754 if (!c->func) {
1755 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1756 w->leaf = NULL;
1757 return 0;
1758 }
1759
Changli Gaod8d1f302010-06-10 23:31:35 -07001760 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 res = c->func(rt, c->arg);
1762 if (res < 0) {
1763 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001764 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (res) {
1766#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001767 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1768 __func__, rt, rt->rt6i_node, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769#endif
1770 continue;
1771 }
1772 return 0;
1773 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001774 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 w->leaf = rt;
1777 return 0;
1778}
1779
1780/*
1781 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * func is called on each route.
1784 * It may return -1 -> delete this route.
1785 * 0 -> continue walking
1786 *
1787 * prune==1 -> only immediate children of node (certainly,
1788 * ignoring pure split nodes) will be scanned.
1789 */
1790
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001791static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001792 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001793 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001795 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 c.w.root = root;
1798 c.w.func = fib6_clean_node;
1799 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001800 c.w.count = 0;
1801 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001803 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001805 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001807 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001810static void __fib6_clean_all(struct net *net,
1811 int (*func)(struct rt6_info *, void *),
1812 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001813{
Thomas Grafc71099a2006-08-04 23:20:06 -07001814 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001815 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001816 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001817
Patrick McHardy1b43af52006-08-10 23:11:17 -07001818 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001819 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001820 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001821 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001822 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001823 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001824 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001825 write_unlock_bh(&table->tb6_lock);
1826 }
1827 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001828 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001829}
1830
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001831void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1832 void *arg)
1833{
1834 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1838{
1839 if (rt->rt6i_flags & RTF_CACHE) {
1840 RT6_TRACE("pruning clone %p\n", rt);
1841 return -1;
1842 }
1843
1844 return 0;
1845}
1846
Duan Jiong163cd4e2014-05-09 13:31:43 +08001847static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001849 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1850 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001851}
1852
1853static void fib6_flush_trees(struct net *net)
1854{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001855 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001856
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001857 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001858}
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860/*
1861 * Garbage collection
1862 */
1863
Michal Kubeček3570df92016-03-08 14:44:25 +01001864struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 int timeout;
1867 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001868};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870static int fib6_age(struct rt6_info *rt, void *arg)
1871{
Michal Kubeček3570df92016-03-08 14:44:25 +01001872 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 unsigned long now = jiffies;
1874
1875 /*
1876 * check addrconf expiration here.
1877 * Routes are expired even if they are in use.
1878 *
1879 * Also age clones. Note, that clones are aged out
1880 * only if they are not in use now.
1881 */
1882
David S. Millerd1918542011-12-28 20:19:20 -05001883 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1884 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return -1;
1887 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001888 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 } else if (rt->rt6i_flags & RTF_CACHE) {
Wei Wang1cfb71e2017-06-17 10:42:33 -07001890 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Michal Kubeček3570df92016-03-08 14:44:25 +01001891 time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 RT6_TRACE("aging clone %p\n", rt);
1893 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001894 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1895 struct neighbour *neigh;
1896 __u8 neigh_flags = 0;
1897
1898 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1899 if (neigh) {
1900 neigh_flags = neigh->flags;
1901 neigh_release(neigh);
1902 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001903 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001904 RT6_TRACE("purging route %p via non-router but gateway\n",
1905 rt);
1906 return -1;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001909 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
1912 return 0;
1913}
1914
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001915void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Michal Kubeček3570df92016-03-08 14:44:25 +01001917 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001918 unsigned long now;
1919
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001920 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001921 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1922 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001923 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001926 gc_args.timeout = expires ? (int)expires :
1927 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001928 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001929
Michal Kubeček3570df92016-03-08 14:44:25 +01001930 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001931 now = jiffies;
1932 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001935 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001936 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001937 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001938 else
1939 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001940 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001943static void fib6_gc_timer_cb(unsigned long arg)
1944{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001945 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001946}
1947
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001948static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001949{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001950 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001951 int err;
1952
1953 err = fib6_notifier_init(net);
1954 if (err)
1955 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001956
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001957 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001958 rwlock_init(&net->ipv6.fib6_walker_lock);
1959 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001960 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001961
Benjamin Theryc5728722008-03-03 23:34:17 -08001962 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1963 if (!net->ipv6.rt6_stats)
1964 goto out_timer;
1965
Eric Dumazet10da66f2010-10-13 08:22:03 +00001966 /* Avoid false sharing : Use at least a full cache line */
1967 size = max_t(size_t, size, L1_CACHE_BYTES);
1968
1969 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001970 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001971 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001972
1973 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1974 GFP_KERNEL);
1975 if (!net->ipv6.fib6_main_tbl)
1976 goto out_fib_table_hash;
1977
1978 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001979 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001980 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1981 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001982 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001983
1984#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1985 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1986 GFP_KERNEL);
1987 if (!net->ipv6.fib6_local_tbl)
1988 goto out_fib6_main_tbl;
1989 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001990 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001991 net->ipv6.fib6_local_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_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001994#endif
1995 fib6_tables_init(net);
1996
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001997 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001998
1999#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2000out_fib6_main_tbl:
2001 kfree(net->ipv6.fib6_main_tbl);
2002#endif
2003out_fib_table_hash:
2004 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002005out_rt6_stats:
2006 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002007out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002008 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002009 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002010}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002011
2012static void fib6_net_exit(struct net *net)
2013{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002014 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002015 del_timer_sync(&net->ipv6.ip6_fib_timer);
2016
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002017#ifdef CONFIG_IPV6_MULTIPLE_TABLES
David S. Miller8e773272012-06-11 00:01:52 -07002018 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002019 kfree(net->ipv6.fib6_local_tbl);
2020#endif
David S. Miller8e773272012-06-11 00:01:52 -07002021 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002022 kfree(net->ipv6.fib6_main_tbl);
2023 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002024 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002025 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002026}
2027
2028static struct pernet_operations fib6_net_ops = {
2029 .init = fib6_net_init,
2030 .exit = fib6_net_exit,
2031};
2032
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002033int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002035 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2038 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002039 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002040 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002041 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002042 goto out;
2043
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002044 ret = register_pernet_subsys(&fib6_net_ops);
2045 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002046 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002047
2048 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
2049 NULL);
2050 if (ret)
2051 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002052
2053 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002054out:
2055 return ret;
2056
David S. Millere8803b62012-06-16 01:12:19 -07002057out_unregister_subsys:
2058 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002059out_kmem_cache_create:
2060 kmem_cache_destroy(fib6_node_kmem);
2061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
2064void fib6_gc_cleanup(void)
2065{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002066 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 kmem_cache_destroy(fib6_node_kmem);
2068}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002069
2070#ifdef CONFIG_PROC_FS
2071
2072struct ipv6_route_iter {
2073 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002074 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002075 loff_t skip;
2076 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002077 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002078};
2079
2080static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2081{
2082 struct rt6_info *rt = v;
2083 struct ipv6_route_iter *iter = seq->private;
2084
2085 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2086
2087#ifdef CONFIG_IPV6_SUBTREES
2088 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2089#else
2090 seq_puts(seq, "00000000000000000000000000000000 00 ");
2091#endif
2092 if (rt->rt6i_flags & RTF_GATEWAY)
2093 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2094 else
2095 seq_puts(seq, "00000000000000000000000000000000");
2096
2097 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2098 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2099 rt->dst.__use, rt->rt6i_flags,
2100 rt->dst.dev ? rt->dst.dev->name : "");
2101 iter->w.leaf = NULL;
2102 return 0;
2103}
2104
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002105static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002106{
2107 struct ipv6_route_iter *iter = w->args;
2108
2109 if (!iter->skip)
2110 return 1;
2111
2112 do {
2113 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2114 iter->skip--;
2115 if (!iter->skip && iter->w.leaf)
2116 return 1;
2117 } while (iter->w.leaf);
2118
2119 return 0;
2120}
2121
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002122static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2123 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002124{
2125 memset(&iter->w, 0, sizeof(iter->w));
2126 iter->w.func = ipv6_route_yield;
2127 iter->w.root = &iter->tbl->tb6_root;
2128 iter->w.state = FWS_INIT;
2129 iter->w.node = iter->w.root;
2130 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002131 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002132 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002133 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002134}
2135
2136static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2137 struct net *net)
2138{
2139 unsigned int h;
2140 struct hlist_node *node;
2141
2142 if (tbl) {
2143 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2144 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2145 } else {
2146 h = 0;
2147 node = NULL;
2148 }
2149
2150 while (!node && h < FIB6_TABLE_HASHSZ) {
2151 node = rcu_dereference_bh(
2152 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2153 }
2154 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2155}
2156
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002157static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2158{
2159 if (iter->sernum != iter->w.root->fn_sernum) {
2160 iter->sernum = iter->w.root->fn_sernum;
2161 iter->w.state = FWS_INIT;
2162 iter->w.node = iter->w.root;
2163 WARN_ON(iter->w.skip);
2164 iter->w.skip = iter->w.count;
2165 }
2166}
2167
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002168static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2169{
2170 int r;
2171 struct rt6_info *n;
2172 struct net *net = seq_file_net(seq);
2173 struct ipv6_route_iter *iter = seq->private;
2174
2175 if (!v)
2176 goto iter_table;
2177
2178 n = ((struct rt6_info *)v)->dst.rt6_next;
2179 if (n) {
2180 ++*pos;
2181 return n;
2182 }
2183
2184iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002185 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002186 read_lock(&iter->tbl->tb6_lock);
2187 r = fib6_walk_continue(&iter->w);
2188 read_unlock(&iter->tbl->tb6_lock);
2189 if (r > 0) {
2190 if (v)
2191 ++*pos;
2192 return iter->w.leaf;
2193 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002194 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002195 return NULL;
2196 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002197 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002198
2199 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2200 if (!iter->tbl)
2201 return NULL;
2202
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002203 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002204 goto iter_table;
2205}
2206
2207static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2208 __acquires(RCU_BH)
2209{
2210 struct net *net = seq_file_net(seq);
2211 struct ipv6_route_iter *iter = seq->private;
2212
2213 rcu_read_lock_bh();
2214 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2215 iter->skip = *pos;
2216
2217 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002218 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002219 return ipv6_route_seq_next(seq, NULL, pos);
2220 } else {
2221 return NULL;
2222 }
2223}
2224
2225static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2226{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002227 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002228 return w->node && !(w->state == FWS_U && w->node == w->root);
2229}
2230
2231static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2232 __releases(RCU_BH)
2233{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002234 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002235 struct ipv6_route_iter *iter = seq->private;
2236
2237 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002238 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002239
2240 rcu_read_unlock_bh();
2241}
2242
2243static const struct seq_operations ipv6_route_seq_ops = {
2244 .start = ipv6_route_seq_start,
2245 .next = ipv6_route_seq_next,
2246 .stop = ipv6_route_seq_stop,
2247 .show = ipv6_route_seq_show
2248};
2249
2250int ipv6_route_open(struct inode *inode, struct file *file)
2251{
2252 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2253 sizeof(struct ipv6_route_iter));
2254}
2255
2256#endif /* CONFIG_PROC_FS */