blob: e5308d7cbd75c4fb67861082382122d445cf5b74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
Wang Yufen8db46f12014-03-28 12:07:02 +080012 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joe Perchesf3213832012-05-15 14:11:53 +000019
20#define pr_fmt(fmt) "IPv6: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ipv6.h>
33#include <net/ndisc.h>
34#include <net/addrconf.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020035#include <net/lwtunnel.h>
Ido Schimmeldf77fe42017-08-03 13:28:17 +020036#include <net/fib_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <net/ip6_fib.h>
39#include <net/ip6_route.h>
40
41#define RT6_DEBUG 2
42
43#if RT6_DEBUG >= 3
Joe Perches91df42b2012-05-15 14:11:54 +000044#define RT6_TRACE(x...) pr_debug(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
46#define RT6_TRACE(x...) do { ; } while (0)
47#endif
48
Wang Yufen437de072014-03-28 12:07:04 +080049static struct kmem_cache *fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020051struct fib6_cleaner {
52 struct fib6_walker w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080053 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int (*func)(struct rt6_info *, void *arg);
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +020055 int sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 void *arg;
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_IPV6_SUBTREES
60#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#else
62#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Duan Jiong163cd4e2014-05-09 13:31:43 +080065static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080066static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
67static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010068static int fib6_walk(struct net *net, struct fib6_walker *w);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020069static int fib6_walk_continue(struct fib6_walker *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
76 */
77
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080078static void fib6_gc_timer_cb(unsigned long arg);
79
Michal Kubeček9a03cd82016-03-08 14:44:35 +010080#define FOR_WALKERS(net, w) \
81 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Michal Kubeček9a03cd82016-03-08 14:44:35 +010083static void fib6_walker_link(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070084{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010085 write_lock_bh(&net->ipv6.fib6_walker_lock);
86 list_add(&w->lh, &net->ipv6.fib6_walkers);
87 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070088}
89
Michal Kubeček9a03cd82016-03-08 14:44:35 +010090static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
Adrian Bunk90d41122006-08-14 23:49:16 -070091{
Michal Kubeček9a03cd82016-03-08 14:44:35 +010092 write_lock_bh(&net->ipv6.fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000093 list_del(&w->lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +010094 write_unlock_bh(&net->ipv6.fib6_walker_lock);
Adrian Bunk90d41122006-08-14 23:49:16 -070095}
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +020096
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +020097static int fib6_new_sernum(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +020099 int new, old;
100
101 do {
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200102 old = atomic_read(&net->ipv6.fib6_sernum);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200103 new = old < INT_MAX ? old + 1 : 1;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200104 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
105 old, new) != old);
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +0200106 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +0200109enum {
110 FIB6_NO_SERNUM_CHANGE = 0,
111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * Auxiliary address test functions for the radix tree.
115 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900116 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * 64bit processors)
118 */
119
120/*
121 * test bit
122 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000123#if defined(__LITTLE_ENDIAN)
124# define BITOP_BE32_SWIZZLE (0x1F & ~7)
125#else
126# define BITOP_BE32_SWIZZLE 0
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200129static __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000131 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000132 /*
133 * Here,
Wang Yufen8db46f12014-03-28 12:07:02 +0800134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
138 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700139 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
140 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200143static struct fib6_node *node_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct fib6_node *fn;
146
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800147 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return fn;
150}
151
Wei Wangc5cff852017-08-21 09:47:10 -0700152static void node_free_immediate(struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 kmem_cache_free(fib6_node_kmem, fn);
155}
156
Wei Wangc5cff852017-08-21 09:47:10 -0700157static void node_free_rcu(struct rcu_head *head)
158{
159 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
160
161 kmem_cache_free(fib6_node_kmem, fn);
162}
163
164static void node_free(struct fib6_node *fn)
165{
166 call_rcu(&fn->rcu, node_free_rcu);
167}
168
Ido Schimmela460aa82017-08-03 13:28:25 +0200169void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700170{
171 int cpu;
172
173 if (!non_pcpu_rt->rt6i_pcpu)
174 return;
175
176 for_each_possible_cpu(cpu) {
177 struct rt6_info **ppcpu_rt;
178 struct rt6_info *pcpu_rt;
179
180 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
181 pcpu_rt = *ppcpu_rt;
182 if (pcpu_rt) {
Wei Wang95145282017-06-17 10:42:34 -0700183 dst_dev_put(&pcpu_rt->dst);
Wei Wang1cfb71e2017-06-17 10:42:33 -0700184 dst_release(&pcpu_rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700185 *ppcpu_rt = NULL;
186 }
187 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700188
Martin KaFai Lau903ce4a2016-07-05 12:10:23 -0700189 free_percpu(non_pcpu_rt->rt6i_pcpu);
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -0700190 non_pcpu_rt->rt6i_pcpu = NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700191}
Ido Schimmela460aa82017-08-03 13:28:25 +0200192EXPORT_SYMBOL_GPL(rt6_free_pcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +0200194static void fib6_free_table(struct fib6_table *table)
195{
196 inetpeer_invalidate_tree(&table->tb6_peers);
197 kfree(table);
198}
199
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800200static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700201{
202 unsigned int h;
203
Thomas Graf375216a2006-10-21 20:20:54 -0700204 /*
205 * Initialize table lock at a single place to give lockdep a key,
206 * tables aren't visible prior to being linked to the list.
207 */
208 rwlock_init(&tb->tb6_lock);
209
Neil Hormana33bc5c2009-07-30 18:52:15 -0700210 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700211
212 /*
213 * No protection necessary, this is the only list mutatation
214 * operation, tables never disappear once they exist.
215 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800216 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700217}
218
219#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800220
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800221static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700222{
223 struct fib6_table *table;
224
225 table = kzalloc(sizeof(*table), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500226 if (table) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700227 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800228 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700229 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -0700230 inet_peer_base_init(&table->tb6_peers);
Thomas Grafc71099a2006-08-04 23:20:06 -0700231 }
232
233 return table;
234}
235
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800236struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700237{
238 struct fib6_table *tb;
239
240 if (id == 0)
241 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800242 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700243 if (tb)
244 return tb;
245
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800246 tb = fib6_alloc_table(net, id);
David S. Miller507c9b12011-12-03 17:50:45 -0500247 if (tb)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800248 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700249
250 return tb;
251}
David Ahernb3b46632016-05-04 21:46:12 -0700252EXPORT_SYMBOL_GPL(fib6_new_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700253
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800254struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700255{
256 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800257 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700258 unsigned int h;
259
260 if (id == 0)
261 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700262 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700263 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800264 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800265 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700266 if (tb->tb6_id == id) {
267 rcu_read_unlock();
268 return tb;
269 }
270 }
271 rcu_read_unlock();
272
273 return NULL;
274}
David Ahernc4850682015-10-12 11:47:08 -0700275EXPORT_SYMBOL_GPL(fib6_get_table);
Thomas Grafc71099a2006-08-04 23:20:06 -0700276
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000277static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700278{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800279 fib6_link_table(net, net->ipv6.fib6_main_tbl);
280 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700281}
Thomas Grafc71099a2006-08-04 23:20:06 -0700282#else
283
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800284struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700285{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800286 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700287}
288
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800289struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700290{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800291 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700292}
293
David S. Miller4c9483b2011-03-12 16:22:43 -0500294struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800295 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700296{
lucienab997ad2015-10-23 15:36:53 +0800297 struct rt6_info *rt;
298
299 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Serhey Popovych07f61552017-06-20 13:29:25 +0300300 if (rt->dst.error == -EAGAIN) {
lucienab997ad2015-10-23 15:36:53 +0800301 ip6_rt_put(rt);
302 rt = net->ipv6.ip6_null_entry;
303 dst_hold(&rt->dst);
304 }
305
306 return &rt->dst;
Thomas Grafc71099a2006-08-04 23:20:06 -0700307}
308
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000309static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700310{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800311 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700312}
313
314#endif
315
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200316unsigned int fib6_tables_seq_read(struct net *net)
317{
318 unsigned int h, fib_seq = 0;
319
320 rcu_read_lock();
321 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
322 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
323 struct fib6_table *tb;
324
325 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
326 read_lock_bh(&tb->tb6_lock);
327 fib_seq += tb->fib_seq;
328 read_unlock_bh(&tb->tb6_lock);
329 }
330 }
331 rcu_read_unlock();
332
333 return fib_seq;
334}
335
336static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
337 enum fib_event_type event_type,
338 struct rt6_info *rt)
339{
340 struct fib6_entry_notifier_info info = {
341 .rt = rt,
342 };
343
344 return call_fib6_notifier(nb, net, event_type, &info.info);
345}
346
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200347static int call_fib6_entry_notifiers(struct net *net,
348 enum fib_event_type event_type,
349 struct rt6_info *rt)
350{
351 struct fib6_entry_notifier_info info = {
352 .rt = rt,
353 };
354
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200355 rt->rt6i_table->fib_seq++;
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200356 return call_fib6_notifiers(net, event_type, &info.info);
357}
358
Ido Schimmele1ee0a52017-08-03 13:28:19 +0200359struct fib6_dump_arg {
360 struct net *net;
361 struct notifier_block *nb;
362};
363
364static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
365{
366 if (rt == arg->net->ipv6.ip6_null_entry)
367 return;
368 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
369}
370
371static int fib6_node_dump(struct fib6_walker *w)
372{
373 struct rt6_info *rt;
374
375 for (rt = w->leaf; rt; rt = rt->dst.rt6_next)
376 fib6_rt_dump(rt, w->args);
377 w->leaf = NULL;
378 return 0;
379}
380
381static void fib6_table_dump(struct net *net, struct fib6_table *tb,
382 struct fib6_walker *w)
383{
384 w->root = &tb->tb6_root;
385 read_lock_bh(&tb->tb6_lock);
386 fib6_walk(net, w);
387 read_unlock_bh(&tb->tb6_lock);
388}
389
390/* Called with rcu_read_lock() */
391int fib6_tables_dump(struct net *net, struct notifier_block *nb)
392{
393 struct fib6_dump_arg arg;
394 struct fib6_walker *w;
395 unsigned int h;
396
397 w = kzalloc(sizeof(*w), GFP_ATOMIC);
398 if (!w)
399 return -ENOMEM;
400
401 w->func = fib6_node_dump;
402 arg.net = net;
403 arg.nb = nb;
404 w->args = &arg;
405
406 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
407 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
408 struct fib6_table *tb;
409
410 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
411 fib6_table_dump(net, tb, w);
412 }
413
414 kfree(w);
415
416 return 0;
417}
418
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200419static int fib6_dump_node(struct fib6_walker *w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700420{
421 int res;
422 struct rt6_info *rt;
423
Changli Gaod8d1f302010-06-10 23:31:35 -0700424 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700425 res = rt6_dump_route(rt, w->args);
426 if (res < 0) {
427 /* Frame is full, suspend walking */
428 w->leaf = rt;
429 return 1;
430 }
David Ahernbeb1afac52017-02-02 12:37:09 -0800431
432 /* Multipath routes are dumped in one route with the
433 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
434 * last sibling of this route (no need to dump the
435 * sibling routes again)
436 */
437 if (rt->rt6i_nsiblings)
438 rt = list_last_entry(&rt->rt6i_siblings,
439 struct rt6_info,
440 rt6i_siblings);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700441 }
442 w->leaf = NULL;
443 return 0;
444}
445
446static void fib6_dump_end(struct netlink_callback *cb)
447{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100448 struct net *net = sock_net(cb->skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200449 struct fib6_walker *w = (void *)cb->args[2];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700450
451 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800452 if (cb->args[4]) {
453 cb->args[4] = 0;
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100454 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800455 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700456 cb->args[2] = 0;
457 kfree(w);
458 }
Wang Yufen437de072014-03-28 12:07:04 +0800459 cb->done = (void *)cb->args[3];
Patrick McHardy1b43af52006-08-10 23:11:17 -0700460 cb->args[1] = 3;
461}
462
463static int fib6_dump_done(struct netlink_callback *cb)
464{
465 fib6_dump_end(cb);
466 return cb->done ? cb->done(cb) : 0;
467}
468
469static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
470 struct netlink_callback *cb)
471{
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100472 struct net *net = sock_net(skb->sk);
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200473 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700474 int res;
475
476 w = (void *)cb->args[2];
477 w->root = &table->tb6_root;
478
479 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000480 w->count = 0;
481 w->skip = 0;
482
Patrick McHardy1b43af52006-08-10 23:11:17 -0700483 read_lock_bh(&table->tb6_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100484 res = fib6_walk(net, w);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700485 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000486 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700487 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000488 cb->args[5] = w->root->fn_sernum;
489 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700490 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000491 if (cb->args[5] != w->root->fn_sernum) {
492 /* Begin at the root if the tree changed */
493 cb->args[5] = w->root->fn_sernum;
494 w->state = FWS_INIT;
495 w->node = w->root;
496 w->skip = w->count;
497 } else
498 w->skip = 0;
499
Patrick McHardy1b43af52006-08-10 23:11:17 -0700500 read_lock_bh(&table->tb6_lock);
501 res = fib6_walk_continue(w);
502 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800503 if (res <= 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +0100504 fib6_walker_unlink(net, w);
Herbert Xu7891cc82009-01-13 22:17:51 -0800505 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700506 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700507 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800508
Patrick McHardy1b43af52006-08-10 23:11:17 -0700509 return res;
510}
511
Thomas Grafc127ea22007-03-22 11:58:32 -0700512static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700513{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900514 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700515 unsigned int h, s_h;
516 unsigned int e = 0, s_e;
517 struct rt6_rtnl_dump_arg arg;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200518 struct fib6_walker *w;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700519 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800520 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700521 int res = 0;
522
523 s_h = cb->args[0];
524 s_e = cb->args[1];
525
526 w = (void *)cb->args[2];
David S. Miller507c9b12011-12-03 17:50:45 -0500527 if (!w) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700528 /* New dump:
529 *
530 * 1. hook callback destructor.
531 */
532 cb->args[3] = (long)cb->done;
533 cb->done = fib6_dump_done;
534
535 /*
536 * 2. allocate and initialize walker.
537 */
538 w = kzalloc(sizeof(*w), GFP_ATOMIC);
David S. Miller507c9b12011-12-03 17:50:45 -0500539 if (!w)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700540 return -ENOMEM;
541 w->func = fib6_dump_node;
542 cb->args[2] = (long)w;
543 }
544
545 arg.skb = skb;
546 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700547 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700548 w->args = &arg;
549
Eric Dumazete67f88d2011-04-27 22:56:07 +0000550 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700551 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700552 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800553 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800554 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700555 if (e < s_e)
556 goto next;
557 res = fib6_dump_table(tb, skb, cb);
558 if (res != 0)
559 goto out;
560next:
561 e++;
562 }
563 }
564out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000565 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700566 cb->args[1] = e;
567 cb->args[0] = h;
568
569 res = res < 0 ? res : skb->len;
570 if (res <= 0)
571 fib6_dump_end(cb);
572 return res;
573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575/*
576 * Routing Table
577 *
578 * return the appropriate node for a routing tree "add" operation
579 * by either creating and inserting or by returning an existing
580 * node.
581 */
582
fan.du9225b232013-07-22 14:21:09 +0800583static struct fib6_node *fib6_add_1(struct fib6_node *root,
584 struct in6_addr *addr, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000585 int offset, int allow_create,
David Ahern333c4302017-05-21 10:12:04 -0600586 int replace_required, int sernum,
587 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct fib6_node *fn, *in, *ln;
590 struct fib6_node *pn = NULL;
591 struct rt6key *key;
592 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900593 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 RT6_TRACE("fib6_add_1\n");
596
597 /* insert node in tree */
598
599 fn = root;
600
601 do {
602 key = (struct rt6key *)((u8 *)fn->leaf + offset);
603
604 /*
605 * Prefix match
606 */
607 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000608 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
Matti Vaittinen14df0152011-11-16 21:18:02 +0000609 if (!allow_create) {
610 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600611 NL_SET_ERR_MSG(extack,
612 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000613 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000614 return ERR_PTR(-ENOENT);
615 }
Joe Perchesf3213832012-05-15 14:11:53 +0000616 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000619 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * Exact match ?
623 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (plen == fn->fn_bit) {
626 /* clean up an intermediate node */
David S. Miller507c9b12011-12-03 17:50:45 -0500627 if (!(fn->fn_flags & RTN_RTINFO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 rt6_release(fn->leaf);
629 fn->leaf = NULL;
630 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return fn;
635 }
636
637 /*
638 * We have more bits to go
639 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* Try to walk down on tree. */
642 fn->fn_sernum = sernum;
643 dir = addr_bit_set(addr, fn->fn_bit);
644 pn = fn;
Wang Yufen8db46f12014-03-28 12:07:02 +0800645 fn = dir ? fn->right : fn->left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 } while (fn);
647
Matti Vaittinen14df0152011-11-16 21:18:02 +0000648 if (!allow_create) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000649 /* We should not create new node because
650 * NLM_F_REPLACE was specified without NLM_F_CREATE
651 * I assume it is safe to require NLM_F_CREATE when
652 * REPLACE flag is used! Later we may want to remove the
653 * check for replace_required, because according
654 * to netlink specification, NLM_F_CREATE
655 * MUST be specified if new route is created.
656 * That would keep IPv6 consistent with IPv4
657 */
Matti Vaittinen14df0152011-11-16 21:18:02 +0000658 if (replace_required) {
David Ahernd5d531c2017-05-21 10:12:05 -0600659 NL_SET_ERR_MSG(extack,
660 "Can not replace route - no match found");
Joe Perchesf3213832012-05-15 14:11:53 +0000661 pr_warn("Can't replace route, no match found\n");
Matti Vaittinen14df0152011-11-16 21:18:02 +0000662 return ERR_PTR(-ENOENT);
663 }
Joe Perchesf3213832012-05-15 14:11:53 +0000664 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
667 * We walked to the bottom of tree.
668 * Create new leaf node without children.
669 */
670
671 ln = node_alloc();
672
David S. Miller507c9b12011-12-03 17:50:45 -0500673 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000674 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ln->parent = pn;
678 ln->fn_sernum = sernum;
679
680 if (dir)
681 pn->right = ln;
682 else
683 pn->left = ln;
684
685 return ln;
686
687
688insert_above:
689 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900690 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * we have a less significant route.
692 * we've to insert an intermediate node on the list
693 * this new node will point to the one we need to create
694 * and the current
695 */
696
697 pn = fn->parent;
698
699 /* find 1st bit in difference between the 2 addrs.
700
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800701 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 but if it is >= plen, the value is ignored in any case.
703 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900704
fan.du9225b232013-07-22 14:21:09 +0800705 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900707 /*
708 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * / \
710 * (new leaf node)[ln] (old node)[fn]
711 */
712 if (plen > bit) {
713 in = node_alloc();
714 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900715
David S. Miller507c9b12011-12-03 17:50:45 -0500716 if (!in || !ln) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (in)
Wei Wangc5cff852017-08-21 09:47:10 -0700718 node_free_immediate(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (ln)
Wei Wangc5cff852017-08-21 09:47:10 -0700720 node_free_immediate(ln);
Lin Ming188c5172012-09-25 15:17:07 +0000721 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900724 /*
725 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * RTN_RTINFO will
727 * be off since that an address that chooses one of
728 * the branches would not match less specific routes
729 * in the other branch
730 */
731
732 in->fn_bit = bit;
733
734 in->parent = pn;
735 in->leaf = fn->leaf;
736 atomic_inc(&in->leaf->rt6i_ref);
737
738 in->fn_sernum = sernum;
739
740 /* update parent pointer */
741 if (dir)
742 pn->right = in;
743 else
744 pn->left = in;
745
746 ln->fn_bit = plen;
747
748 ln->parent = in;
749 fn->parent = in;
750
751 ln->fn_sernum = sernum;
752
753 if (addr_bit_set(addr, bit)) {
754 in->right = ln;
755 in->left = fn;
756 } else {
757 in->left = ln;
758 in->right = fn;
759 }
760 } else { /* plen <= bit */
761
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900762 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * (new leaf node)[ln]
764 * / \
765 * (old node)[fn] NULL
766 */
767
768 ln = node_alloc();
769
David S. Miller507c9b12011-12-03 17:50:45 -0500770 if (!ln)
Lin Ming188c5172012-09-25 15:17:07 +0000771 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 ln->fn_bit = plen;
774
775 ln->parent = pn;
776
777 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (dir)
780 pn->right = ln;
781 else
782 pn->left = ln;
783
784 if (addr_bit_set(&key->addr, plen))
785 ln->right = fn;
786 else
787 ln->left = fn;
788
789 fn->parent = ln;
790 }
791 return ln;
792}
793
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +0200794static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200795{
796 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
797 RTF_GATEWAY;
798}
799
Florian Westphale715b6d2015-01-05 23:57:44 +0100800static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100801{
Florian Westphale715b6d2015-01-05 23:57:44 +0100802 int i;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100803
Florian Westphale715b6d2015-01-05 23:57:44 +0100804 for (i = 0; i < RTAX_MAX; i++) {
805 if (test_bit(i, mxc->mx_valid))
806 mp[i] = mxc->mx[i];
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100807 }
Florian Westphale715b6d2015-01-05 23:57:44 +0100808}
809
810static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
811{
812 if (!mxc->mx)
813 return 0;
814
815 if (dst->flags & DST_HOST) {
816 u32 *mp = dst_metrics_write_ptr(dst);
817
818 if (unlikely(!mp))
819 return -ENOMEM;
820
821 fib6_copy_metrics(mp, mxc);
822 } else {
823 dst_init_metrics(dst, mxc->mx, false);
824
825 /* We've stolen mx now. */
826 mxc->mx = NULL;
827 }
828
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100829 return 0;
830}
831
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100832static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
833 struct net *net)
834{
835 if (atomic_read(&rt->rt6i_ref) != 1) {
836 /* This route is used as dummy address holder in some split
837 * nodes. It is not leaked, but it still holds other resources,
838 * which must be released in time. So, scan ascendant nodes
839 * and replace dummy references to this route with references
840 * to still alive ones.
841 */
842 while (fn) {
843 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
844 fn->leaf = fib6_find_prefix(net, fn);
845 atomic_inc(&fn->leaf->rt6i_ref);
846 rt6_release(rt);
847 }
848 fn = fn->parent;
849 }
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +0100850 }
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853/*
854 * Insert routing information in a node.
855 */
856
857static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Florian Westphale715b6d2015-01-05 23:57:44 +0100858 struct nl_info *info, struct mx6_config *mxc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct rt6_info *iter = NULL;
861 struct rt6_info **ins;
Michal Kubeček27596472015-05-18 20:54:00 +0200862 struct rt6_info **fallback_ins = NULL;
David S. Miller507c9b12011-12-03 17:50:45 -0500863 int replace = (info->nlh &&
864 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
865 int add = (!info->nlh ||
866 (info->nlh->nlmsg_flags & NLM_F_CREATE));
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000867 int found = 0;
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200868 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
Guillaume Nault73483c12016-09-07 17:21:40 +0200869 u16 nlflags = NLM_F_EXCL;
Michal Kubečeke5fd3872014-03-27 13:04:08 +0100870 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Ahern1f5e29c2017-01-31 16:51:37 -0800872 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
873 nlflags |= NLM_F_APPEND;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ins = &fn->leaf;
876
David S. Miller507c9b12011-12-03 17:50:45 -0500877 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /*
879 * Search for duplicates
880 */
881
882 if (iter->rt6i_metric == rt->rt6i_metric) {
883 /*
884 * Same priority level
885 */
David S. Miller507c9b12011-12-03 17:50:45 -0500886 if (info->nlh &&
887 (info->nlh->nlmsg_flags & NLM_F_EXCL))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000888 return -EEXIST;
Guillaume Nault73483c12016-09-07 17:21:40 +0200889
890 nlflags &= ~NLM_F_EXCL;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000891 if (replace) {
Michal Kubeček27596472015-05-18 20:54:00 +0200892 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
893 found++;
894 break;
895 }
896 if (rt_can_ecmp)
897 fallback_ins = fallback_ins ?: ins;
898 goto next_iter;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
David Ahernf06b7542017-07-05 14:41:46 -0600901 if (rt6_duplicate_nexthop(iter, rt)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000902 if (rt->rt6i_nsiblings)
903 rt->rt6i_nsiblings = 0;
David S. Miller507c9b12011-12-03 17:50:45 -0500904 if (!(iter->rt6i_flags & RTF_EXPIRES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EEXIST;
Gao feng1716a962012-04-06 00:13:10 +0000906 if (!(rt->rt6i_flags & RTF_EXPIRES))
907 rt6_clean_expires(iter);
908 else
909 rt6_set_expires(iter, rt->dst.expires);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -0700910 iter->rt6i_pmtu = rt->rt6i_pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -EEXIST;
912 }
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000913 /* If we have the same destination and the same metric,
914 * but not the same gateway, then the route we try to
915 * add is sibling to this route, increment our counter
916 * of siblings, and later we will add our route to the
917 * list.
918 * Only static routes (which don't have flag
919 * RTF_EXPIRES) are used for ECMPv6.
920 *
921 * To avoid long list, we only had siblings if the
922 * route have a gateway.
923 */
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200924 if (rt_can_ecmp &&
925 rt6_qualify_for_ecmp(iter))
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000926 rt->rt6i_nsiblings++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 if (iter->rt6i_metric > rt->rt6i_metric)
930 break;
931
Michal Kubeček27596472015-05-18 20:54:00 +0200932next_iter:
Changli Gaod8d1f302010-06-10 23:31:35 -0700933 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
Michal Kubeček27596472015-05-18 20:54:00 +0200936 if (fallback_ins && !found) {
937 /* No ECMP-able route found, replace first non-ECMP one */
938 ins = fallback_ins;
939 iter = *ins;
940 found++;
941 }
942
David S. Millerf11e6652007-03-24 20:36:25 -0700943 /* Reset round-robin state, if necessary */
944 if (ins == &fn->leaf)
945 fn->rr_ptr = NULL;
946
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000947 /* Link this route to others same route. */
948 if (rt->rt6i_nsiblings) {
949 unsigned int rt6i_nsiblings;
950 struct rt6_info *sibling, *temp_sibling;
951
952 /* Find the first route that have the same metric */
953 sibling = fn->leaf;
954 while (sibling) {
Hannes Frederic Sowa307f2fb2013-07-12 23:46:33 +0200955 if (sibling->rt6i_metric == rt->rt6i_metric &&
956 rt6_qualify_for_ecmp(sibling)) {
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000957 list_add_tail(&rt->rt6i_siblings,
958 &sibling->rt6i_siblings);
959 break;
960 }
961 sibling = sibling->dst.rt6_next;
962 }
963 /* For each sibling in the list, increment the counter of
964 * siblings. BUG() if counters does not match, list of siblings
965 * is broken!
966 */
967 rt6i_nsiblings = 0;
968 list_for_each_entry_safe(sibling, temp_sibling,
969 &rt->rt6i_siblings, rt6i_siblings) {
970 sibling->rt6i_nsiblings++;
971 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
972 rt6i_nsiblings++;
973 }
974 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
975 }
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /*
978 * insert node
979 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000980 if (!replace) {
981 if (!add)
Joe Perchesf3213832012-05-15 14:11:53 +0000982 pr_warn("NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000984add:
Guillaume Nault73483c12016-09-07 17:21:40 +0200985 nlflags |= NLM_F_CREATE;
Florian Westphale715b6d2015-01-05 23:57:44 +0100986 err = fib6_commit_metrics(&rt->dst, mxc);
987 if (err)
988 return err;
989
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000990 rt->dst.rt6_next = iter;
991 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -0700992 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000993 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +0200994 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
995 rt);
David Ahern3b1137f2017-02-02 12:37:10 -0800996 if (!info->skip_notify)
997 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000998 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
David S. Miller507c9b12011-12-03 17:50:45 -05001000 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001001 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1002 fn->fn_flags |= RTN_RTINFO;
1003 }
1004
1005 } else {
Michal Kubeček27596472015-05-18 20:54:00 +02001006 int nsiblings;
1007
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001008 if (!found) {
1009 if (add)
1010 goto add;
Joe Perchesf3213832012-05-15 14:11:53 +00001011 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001012 return -ENOENT;
1013 }
Florian Westphale715b6d2015-01-05 23:57:44 +01001014
1015 err = fib6_commit_metrics(&rt->dst, mxc);
1016 if (err)
1017 return err;
1018
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001019 *ins = rt;
Wei Wang4e587ea2017-08-25 15:03:10 -07001020 rcu_assign_pointer(rt->rt6i_node, fn);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001021 rt->dst.rt6_next = iter->dst.rt6_next;
1022 atomic_inc(&rt->rt6i_ref);
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001023 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1024 rt);
David Ahern3b1137f2017-02-02 12:37:10 -08001025 if (!info->skip_notify)
1026 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
David S. Miller507c9b12011-12-03 17:50:45 -05001027 if (!(fn->fn_flags & RTN_RTINFO)) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001028 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1029 fn->fn_flags |= RTN_RTINFO;
1030 }
Michal Kubeček27596472015-05-18 20:54:00 +02001031 nsiblings = iter->rt6i_nsiblings;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001032 iter->rt6i_node = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001033 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -07001034 if (fn->rr_ptr == iter)
1035 fn->rr_ptr = NULL;
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001036 rt6_release(iter);
Michal Kubeček27596472015-05-18 20:54:00 +02001037
1038 if (nsiblings) {
1039 /* Replacing an ECMP route, remove all siblings */
1040 ins = &rt->dst.rt6_next;
1041 iter = *ins;
1042 while (iter) {
Sabrina Dubroca67e19402017-03-13 13:28:09 +01001043 if (iter->rt6i_metric > rt->rt6i_metric)
1044 break;
Michal Kubeček27596472015-05-18 20:54:00 +02001045 if (rt6_qualify_for_ecmp(iter)) {
1046 *ins = iter->dst.rt6_next;
Ido Schimmel7483cea2017-08-03 13:28:22 +02001047 iter->rt6i_node = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001048 fib6_purge_rt(iter, fn, info->nl_net);
Wei Wang383143f2017-08-16 11:18:09 -07001049 if (fn->rr_ptr == iter)
1050 fn->rr_ptr = NULL;
Michal Kubeček27596472015-05-18 20:54:00 +02001051 rt6_release(iter);
1052 nsiblings--;
1053 } else {
1054 ins = &iter->dst.rt6_next;
1055 }
1056 iter = *ins;
1057 }
1058 WARN_ON(nsiblings != 0);
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061
1062 return 0;
1063}
1064
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001065static void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001067 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
David S. Miller507c9b12011-12-03 17:50:45 -05001068 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001069 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001070 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001073void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001075 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1076 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -07001077 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080/*
1081 * Add routing information to the routing tree.
1082 * <destination addr>/<source addr>
1083 * with source addr info in sub-trees
1084 */
1085
Florian Westphale715b6d2015-01-05 23:57:44 +01001086int fib6_add(struct fib6_node *root, struct rt6_info *rt,
David Ahern333c4302017-05-21 10:12:04 -06001087 struct nl_info *info, struct mx6_config *mxc,
1088 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001090 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001092 int allow_create = 1;
1093 int replace_required = 0;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001094 int sernum = fib6_new_sernum(info->nl_net);
David S. Miller507c9b12011-12-03 17:50:45 -05001095
Wei Wanga4c2fd72017-06-17 10:42:42 -07001096 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07001097 return -EINVAL;
1098
David S. Miller507c9b12011-12-03 17:50:45 -05001099 if (info->nlh) {
1100 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001101 allow_create = 0;
David S. Miller507c9b12011-12-03 17:50:45 -05001102 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001103 replace_required = 1;
1104 }
1105 if (!allow_create && !replace_required)
Joe Perchesf3213832012-05-15 14:11:53 +00001106 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
fan.du9225b232013-07-22 14:21:09 +08001108 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1109 offsetof(struct rt6_info, rt6i_dst), allow_create,
David Ahern333c4302017-05-21 10:12:04 -06001110 replace_required, sernum, extack);
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001111 if (IS_ERR(fn)) {
1112 err = PTR_ERR(fn);
Daniel Borkmannae7b4e12013-09-07 15:13:20 +02001113 fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto out;
Lin Ming188c5172012-09-25 15:17:07 +00001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001117 pn = fn;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#ifdef CONFIG_IPV6_SUBTREES
1120 if (rt->rt6i_src.plen) {
1121 struct fib6_node *sn;
1122
David S. Miller507c9b12011-12-03 17:50:45 -05001123 if (!fn->subtree) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct fib6_node *sfn;
1125
1126 /*
1127 * Create subtree.
1128 *
1129 * fn[main tree]
1130 * |
1131 * sfn[subtree root]
1132 * \
1133 * sn[new leaf node]
1134 */
1135
1136 /* Create subtree root node */
1137 sfn = node_alloc();
David S. Miller507c9b12011-12-03 17:50:45 -05001138 if (!sfn)
Wei Wang348a4002017-08-18 17:14:49 -07001139 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001141 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1142 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sfn->fn_flags = RTN_ROOT;
Hannes Frederic Sowac8c4d422014-10-06 19:58:36 +02001144 sfn->fn_sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* Now add the first leaf node to new subtree */
1147
1148 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001149 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001150 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001151 allow_create, replace_required, sernum,
1152 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Wei Yongjunf950c0e2012-09-20 18:29:56 +00001154 if (IS_ERR(sn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* If it is failed, discard just allocated
Wei Wang348a4002017-08-18 17:14:49 -07001156 root, and then (in failure) stale node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 in main tree.
1158 */
Wei Wangc5cff852017-08-21 09:47:10 -07001159 node_free_immediate(sfn);
Lin Ming188c5172012-09-25 15:17:07 +00001160 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001161 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 /* Now link new subtree to main tree */
1165 sfn->parent = fn;
1166 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else {
1168 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
fan.du9225b232013-07-22 14:21:09 +08001169 rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001170 offsetof(struct rt6_info, rt6i_src),
David Ahern333c4302017-05-21 10:12:04 -06001171 allow_create, replace_required, sernum,
1172 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Matti Vaittinen4a287eb2011-11-14 00:15:14 +00001174 if (IS_ERR(sn)) {
1175 err = PTR_ERR(sn);
Wei Wang348a4002017-08-18 17:14:49 -07001176 goto failure;
Lin Ming188c5172012-09-25 15:17:07 +00001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
David S. Miller507c9b12011-12-03 17:50:45 -05001180 if (!fn->leaf) {
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001181 fn->leaf = rt;
1182 atomic_inc(&rt->rt6i_ref);
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 fn = sn;
1185 }
1186#endif
1187
Florian Westphale715b6d2015-01-05 23:57:44 +01001188 err = fib6_add_rt2node(fn, rt, info, mxc);
David S. Miller507c9b12011-12-03 17:50:45 -05001189 if (!err) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001190 fib6_start_gc(info->nl_net, rt);
David S. Miller507c9b12011-12-03 17:50:45 -05001191 if (!(rt->rt6i_flags & RTF_CACHE))
Duan Jiong163cd4e2014-05-09 13:31:43 +08001192 fib6_prune_clones(info->nl_net, pn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
1195out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001196 if (err) {
1197#ifdef CONFIG_IPV6_SUBTREES
1198 /*
1199 * If fib6_add_1 has cleared the old leaf pointer in the
1200 * super-tree leaf node we have to find a new one for it.
1201 */
David S. Miller3c051232008-04-18 01:46:19 -07001202 if (pn != fn && pn->leaf == rt) {
1203 pn->leaf = NULL;
1204 atomic_dec(&rt->rt6i_ref);
1205 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001206 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001207 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001208#if RT6_DEBUG >= 2
1209 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001210 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001211 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001212 }
1213#endif
1214 atomic_inc(&pn->leaf->rt6i_ref);
1215 }
1216#endif
Wei Wang348a4002017-08-18 17:14:49 -07001217 goto failure;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -07001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return err;
1220
Wei Wang348a4002017-08-18 17:14:49 -07001221failure:
1222 /* fn->leaf could be NULL if fn is an intermediate node and we
1223 * failed to add the new route to it in both subtree creation
1224 * failure and fib6_add_rt2node() failure case.
1225 * In both cases, fib6_repair_tree() should be called to fix
1226 * fn->leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001229 fib6_repair_tree(info->nl_net, fn);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001230 /* Always release dst as dst->__refcnt is guaranteed
1231 * to be taken before entering this function
1232 */
Wei Wang587fea72017-06-17 10:42:36 -07001233 dst_release_immediate(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237/*
1238 * Routing tree lookup
1239 *
1240 */
1241
1242struct lookup_args {
David S. Miller507c9b12011-12-03 17:50:45 -05001243 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001244 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245};
1246
Wang Yufen437de072014-03-28 12:07:04 +08001247static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1248 struct lookup_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -08001251 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001253 if (unlikely(args->offset == 0))
1254 return NULL;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * Descend on a tree
1258 */
1259
1260 fn = root;
1261
1262 for (;;) {
1263 struct fib6_node *next;
1264
1265 dir = addr_bit_set(args->addr, fn->fn_bit);
1266
1267 next = dir ? fn->right : fn->left;
1268
1269 if (next) {
1270 fn = next;
1271 continue;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
1274 }
1275
David S. Miller507c9b12011-12-03 17:50:45 -05001276 while (fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001277 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct rt6key *key;
1279
1280 key = (struct rt6key *) ((u8 *) fn->leaf +
1281 args->offset);
1282
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001283 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1284#ifdef CONFIG_IPV6_SUBTREES
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001285 if (fn->subtree) {
1286 struct fib6_node *sfn;
1287 sfn = fib6_lookup_1(fn->subtree,
1288 args + 1);
1289 if (!sfn)
1290 goto backtrack;
1291 fn = sfn;
1292 }
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001293#endif
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001294 if (fn->fn_flags & RTN_RTINFO)
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001295 return fn;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Hannes Frederic Sowa3e3be272013-08-07 02:34:31 +02001298#ifdef CONFIG_IPV6_SUBTREES
1299backtrack:
1300#endif
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001301 if (fn->fn_flags & RTN_ROOT)
1302 break;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 fn = fn->parent;
1305 }
1306
1307 return NULL;
1308}
1309
Wang Yufen437de072014-03-28 12:07:04 +08001310struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1311 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001314 struct lookup_args args[] = {
1315 {
1316 .offset = offsetof(struct rt6_info, rt6i_dst),
1317 .addr = daddr,
1318 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001320 {
1321 .offset = offsetof(struct rt6_info, rt6i_src),
1322 .addr = saddr,
1323 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -07001325 {
1326 .offset = 0, /* sentinel */
1327 }
1328 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -07001330 fn = fib6_lookup_1(root, daddr ? args : args + 1);
David S. Miller507c9b12011-12-03 17:50:45 -05001331 if (!fn || fn->fn_flags & RTN_TL_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 fn = root;
1333
1334 return fn;
1335}
1336
1337/*
1338 * Get node with specified destination prefix (and source prefix,
1339 * if subtrees are used)
1340 */
1341
1342
Wang Yufen437de072014-03-28 12:07:04 +08001343static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1344 const struct in6_addr *addr,
1345 int plen, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct fib6_node *fn;
1348
1349 for (fn = root; fn ; ) {
1350 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1351
1352 /*
1353 * Prefix match
1354 */
1355 if (plen < fn->fn_bit ||
1356 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1357 return NULL;
1358
1359 if (plen == fn->fn_bit)
1360 return fn;
1361
1362 /*
1363 * We have more bits to go
1364 */
1365 if (addr_bit_set(addr, fn->fn_bit))
1366 fn = fn->right;
1367 else
1368 fn = fn->left;
1369 }
1370 return NULL;
1371}
1372
Wang Yufen437de072014-03-28 12:07:04 +08001373struct fib6_node *fib6_locate(struct fib6_node *root,
1374 const struct in6_addr *daddr, int dst_len,
1375 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct fib6_node *fn;
1378
1379 fn = fib6_locate_1(root, daddr, dst_len,
1380 offsetof(struct rt6_info, rt6i_dst));
1381
1382#ifdef CONFIG_IPV6_SUBTREES
1383 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001384 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001385 if (fn && fn->subtree)
1386 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 offsetof(struct rt6_info, rt6i_src));
1388 }
1389#endif
1390
David S. Miller507c9b12011-12-03 17:50:45 -05001391 if (fn && fn->fn_flags & RTN_RTINFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return fn;
1393
1394 return NULL;
1395}
1396
1397
1398/*
1399 * Deletion
1400 *
1401 */
1402
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001403static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
David S. Miller507c9b12011-12-03 17:50:45 -05001405 if (fn->fn_flags & RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001406 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
David S. Miller507c9b12011-12-03 17:50:45 -05001408 while (fn) {
1409 if (fn->left)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return fn->left->leaf;
David S. Miller507c9b12011-12-03 17:50:45 -05001411 if (fn->right)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return fn->right->leaf;
1413
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001414 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 return NULL;
1417}
1418
1419/*
1420 * Called to trim the tree of intermediate nodes when possible. "fn"
1421 * is the node we want to try and remove.
1422 */
1423
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001424static struct fib6_node *fib6_repair_tree(struct net *net,
1425 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 int children;
1428 int nstate;
1429 struct fib6_node *child, *pn;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001430 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int iter = 0;
1432
1433 for (;;) {
1434 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1435 iter++;
1436
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001437 WARN_ON(fn->fn_flags & RTN_RTINFO);
1438 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
Ian Morris53b24b82015-03-29 14:00:05 +01001439 WARN_ON(fn->leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 children = 0;
1442 child = NULL;
Wang Yufen49e253e2014-03-28 12:07:03 +08001443 if (fn->right)
1444 child = fn->right, children |= 1;
1445 if (fn->left)
1446 child = fn->left, children |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001448 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#ifdef CONFIG_IPV6_SUBTREES
1450 /* Subtree root (i.e. fn) may have one child */
David S. Miller507c9b12011-12-03 17:50:45 -05001451 || (children && fn->fn_flags & RTN_ROOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#endif
1453 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001454 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455#if RT6_DEBUG >= 2
David S. Miller507c9b12011-12-03 17:50:45 -05001456 if (!fn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001457 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001458 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460#endif
1461 atomic_inc(&fn->leaf->rt6i_ref);
1462 return fn->parent;
1463 }
1464
1465 pn = fn->parent;
1466#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001467 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001468 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001469 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 nstate = FWS_L;
1471 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001472 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473#endif
Wang Yufen49e253e2014-03-28 12:07:03 +08001474 if (pn->right == fn)
1475 pn->right = child;
1476 else if (pn->left == fn)
1477 pn->left = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001479 else
1480 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481#endif
1482 if (child)
1483 child->parent = pn;
1484 nstate = FWS_R;
1485#ifdef CONFIG_IPV6_SUBTREES
1486 }
1487#endif
1488
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001489 read_lock(&net->ipv6.fib6_walker_lock);
1490 FOR_WALKERS(net, w) {
David S. Miller507c9b12011-12-03 17:50:45 -05001491 if (!child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (w->root == fn) {
1493 w->root = w->node = NULL;
1494 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1495 } else if (w->node == fn) {
1496 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1497 w->node = pn;
1498 w->state = nstate;
1499 }
1500 } else {
1501 if (w->root == fn) {
1502 w->root = child;
1503 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1504 }
1505 if (w->node == fn) {
1506 w->node = child;
1507 if (children&2) {
1508 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001509 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 } else {
1511 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
Wang Yufen8db46f12014-03-28 12:07:02 +08001512 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 }
1515 }
1516 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001517 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 node_free(fn);
David S. Miller507c9b12011-12-03 17:50:45 -05001520 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return pn;
1522
1523 rt6_release(pn->leaf);
1524 pn->leaf = NULL;
1525 fn = pn;
1526 }
1527}
1528
1529static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001530 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001532 struct fib6_walker *w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001534 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 RT6_TRACE("fib6_del_route\n");
1537
1538 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001539 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001541 net->ipv6.rt6_stats->fib_rt_entries--;
1542 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
David S. Millerf11e6652007-03-24 20:36:25 -07001544 /* Reset round-robin state, if necessary */
1545 if (fn->rr_ptr == rt)
1546 fn->rr_ptr = NULL;
1547
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00001548 /* Remove this entry from other siblings */
1549 if (rt->rt6i_nsiblings) {
1550 struct rt6_info *sibling, *next_sibling;
1551
1552 list_for_each_entry_safe(sibling, next_sibling,
1553 &rt->rt6i_siblings, rt6i_siblings)
1554 sibling->rt6i_nsiblings--;
1555 rt->rt6i_nsiblings = 0;
1556 list_del_init(&rt->rt6i_siblings);
1557 }
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* Adjust walkers */
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001560 read_lock(&net->ipv6.fib6_walker_lock);
1561 FOR_WALKERS(net, w) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (w->state == FWS_C && w->leaf == rt) {
1563 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001564 w->leaf = rt->dst.rt6_next;
David S. Miller507c9b12011-12-03 17:50:45 -05001565 if (!w->leaf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 w->state = FWS_U;
1567 }
1568 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001569 read_unlock(&net->ipv6.fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Changli Gaod8d1f302010-06-10 23:31:35 -07001571 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* If it was last route, expunge its radix tree node */
David S. Miller507c9b12011-12-03 17:50:45 -05001574 if (!fn->leaf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001576 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001577 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579
Hannes Frederic Sowa6e9e16e62015-01-26 15:11:17 +01001580 fib6_purge_rt(rt, fn, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Ido Schimmeldf77fe42017-08-03 13:28:17 +02001582 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
David Ahern16a16cd2017-02-02 12:37:11 -08001583 if (!info->skip_notify)
1584 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 rt6_release(rt);
1586}
1587
Thomas Graf86872cb2006-08-22 00:01:08 -07001588int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Wei Wang4e587ea2017-08-25 15:03:10 -07001590 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1591 lockdep_is_held(&rt->rt6i_table->tb6_lock));
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001592 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct rt6_info **rtp;
1594
1595#if RT6_DEBUG >= 2
Wang Yufen8db46f12014-03-28 12:07:02 +08001596 if (rt->dst.obsolete > 0) {
Ian Morris53b24b82015-03-29 14:00:05 +01001597 WARN_ON(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return -ENOENT;
1599 }
1600#endif
David S. Miller507c9b12011-12-03 17:50:45 -05001601 if (!fn || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return -ENOENT;
1603
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001604 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
David S. Miller507c9b12011-12-03 17:50:45 -05001606 if (!(rt->rt6i_flags & RTF_CACHE)) {
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001607 struct fib6_node *pn = fn;
1608#ifdef CONFIG_IPV6_SUBTREES
1609 /* clones of this route might be in another subtree */
1610 if (rt->rt6i_src.plen) {
David S. Miller507c9b12011-12-03 17:50:45 -05001611 while (!(pn->fn_flags & RTN_ROOT))
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001612 pn = pn->parent;
1613 pn = pn->parent;
1614 }
1615#endif
Duan Jiong163cd4e2014-05-09 13:31:43 +08001616 fib6_prune_clones(info->nl_net, pn);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 /*
1620 * Walk the leaf entries looking for ourself
1621 */
1622
Changli Gaod8d1f302010-06-10 23:31:35 -07001623 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001625 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627 }
1628 }
1629 return -ENOENT;
1630}
1631
1632/*
1633 * Tree traversal function.
1634 *
1635 * Certainly, it is not interrupt safe.
1636 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1637 * It means, that we can modify tree during walking
1638 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001639 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 *
1641 * It guarantees that every node will be traversed,
1642 * and that it will be traversed only once.
1643 *
1644 * Callback function w->func may return:
1645 * 0 -> continue walking.
1646 * positive value -> walking is suspended (used by tree dumps,
1647 * and probably by gc, if it will be split to several slices)
1648 * negative value -> terminate walking.
1649 *
1650 * The function itself returns:
1651 * 0 -> walk is complete.
1652 * >0 -> walk is incomplete (i.e. suspended)
1653 * <0 -> walk is terminated by an error.
1654 */
1655
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001656static int fib6_walk_continue(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct fib6_node *fn, *pn;
1659
1660 for (;;) {
1661 fn = w->node;
David S. Miller507c9b12011-12-03 17:50:45 -05001662 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 0;
1664
1665 if (w->prune && fn != w->root &&
David S. Miller507c9b12011-12-03 17:50:45 -05001666 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 w->state = FWS_C;
1668 w->leaf = fn->leaf;
1669 }
1670 switch (w->state) {
1671#ifdef CONFIG_IPV6_SUBTREES
1672 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001673 if (FIB6_SUBTREE(fn)) {
1674 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 continue;
1676 }
1677 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001678#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 case FWS_L:
1680 if (fn->left) {
1681 w->node = fn->left;
1682 w->state = FWS_INIT;
1683 continue;
1684 }
1685 w->state = FWS_R;
1686 case FWS_R:
1687 if (fn->right) {
1688 w->node = fn->right;
1689 w->state = FWS_INIT;
1690 continue;
1691 }
1692 w->state = FWS_C;
1693 w->leaf = fn->leaf;
1694 case FWS_C:
David S. Miller507c9b12011-12-03 17:50:45 -05001695 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001696 int err;
1697
Eric Dumazetfa809e22012-06-25 15:37:19 -07001698 if (w->skip) {
1699 w->skip--;
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001700 goto skip;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001701 }
1702
1703 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (err)
1705 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001706
1707 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 continue;
1709 }
Kumar Sundararajan1c265852014-04-24 09:48:53 -04001710skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 w->state = FWS_U;
1712 case FWS_U:
1713 if (fn == w->root)
1714 return 0;
1715 pn = fn->parent;
1716 w->node = pn;
1717#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001718 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001719 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 w->state = FWS_L;
1721 continue;
1722 }
1723#endif
1724 if (pn->left == fn) {
1725 w->state = FWS_R;
1726 continue;
1727 }
1728 if (pn->right == fn) {
1729 w->state = FWS_C;
1730 w->leaf = w->node->leaf;
1731 continue;
1732 }
1733#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001734 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735#endif
1736 }
1737 }
1738}
1739
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001740static int fib6_walk(struct net *net, struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 int res;
1743
1744 w->state = FWS_INIT;
1745 w->node = w->root;
1746
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001747 fib6_walker_link(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 res = fib6_walk_continue(w);
1749 if (res <= 0)
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001750 fib6_walker_unlink(net, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return res;
1752}
1753
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001754static int fib6_clean_node(struct fib6_walker *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 int res;
1757 struct rt6_info *rt;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001758 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001759 struct nl_info info = {
1760 .nl_net = c->net,
1761 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001763 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1764 w->node->fn_sernum != c->sernum)
1765 w->node->fn_sernum = c->sernum;
1766
1767 if (!c->func) {
1768 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1769 w->leaf = NULL;
1770 return 0;
1771 }
1772
Changli Gaod8d1f302010-06-10 23:31:35 -07001773 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 res = c->func(rt, c->arg);
1775 if (res < 0) {
1776 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001777 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (res) {
1779#if RT6_DEBUG >= 2
Joe Perches91df42b2012-05-15 14:11:54 +00001780 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
Wei Wang4e587ea2017-08-25 15:03:10 -07001781 __func__, rt,
1782 rcu_access_pointer(rt->rt6i_node),
1783 res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784#endif
1785 continue;
1786 }
1787 return 0;
1788 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001789 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791 w->leaf = rt;
1792 return 0;
1793}
1794
1795/*
1796 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001797 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * func is called on each route.
1799 * It may return -1 -> delete this route.
1800 * 0 -> continue walking
1801 *
1802 * prune==1 -> only immediate children of node (certainly,
1803 * ignoring pure split nodes) will be scanned.
1804 */
1805
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001806static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001807 int (*func)(struct rt6_info *, void *arg),
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001808 bool prune, int sernum, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02001810 struct fib6_cleaner c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 c.w.root = root;
1813 c.w.func = fib6_clean_node;
1814 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001815 c.w.count = 0;
1816 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 c.func = func;
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001818 c.sernum = sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001820 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001822 fib6_walk(net, &c.w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001825static void __fib6_clean_all(struct net *net,
1826 int (*func)(struct rt6_info *, void *),
1827 int sernum, void *arg)
Thomas Grafc71099a2006-08-04 23:20:06 -07001828{
Thomas Grafc71099a2006-08-04 23:20:06 -07001829 struct fib6_table *table;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001830 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001831 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001832
Patrick McHardy1b43af52006-08-10 23:11:17 -07001833 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001834 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001835 head = &net->ipv6.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001836 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001837 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001838 fib6_clean_tree(net, &table->tb6_root,
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001839 func, false, sernum, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001840 write_unlock_bh(&table->tb6_lock);
1841 }
1842 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001843 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001844}
1845
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001846void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1847 void *arg)
1848{
1849 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1850}
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1853{
1854 if (rt->rt6i_flags & RTF_CACHE) {
1855 RT6_TRACE("pruning clone %p\n", rt);
1856 return -1;
1857 }
1858
1859 return 0;
1860}
1861
Duan Jiong163cd4e2014-05-09 13:31:43 +08001862static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001864 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1865 FIB6_NO_SERNUM_CHANGE, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001866}
1867
1868static void fib6_flush_trees(struct net *net)
1869{
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +02001870 int new_sernum = fib6_new_sernum(net);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001871
Hannes Frederic Sowa327571c2014-10-06 19:58:38 +02001872 __fib6_clean_all(net, NULL, new_sernum, NULL);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02001873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875/*
1876 * Garbage collection
1877 */
1878
Michal Kubeček3570df92016-03-08 14:44:25 +01001879struct fib6_gc_args
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 int timeout;
1882 int more;
Michal Kubeček3570df92016-03-08 14:44:25 +01001883};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885static int fib6_age(struct rt6_info *rt, void *arg)
1886{
Michal Kubeček3570df92016-03-08 14:44:25 +01001887 struct fib6_gc_args *gc_args = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 unsigned long now = jiffies;
1889
1890 /*
1891 * check addrconf expiration here.
1892 * Routes are expired even if they are in use.
1893 *
1894 * Also age clones. Note, that clones are aged out
1895 * only if they are not in use now.
1896 */
1897
David S. Millerd1918542011-12-28 20:19:20 -05001898 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1899 if (time_after(now, rt->dst.expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return -1;
1902 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001903 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } else if (rt->rt6i_flags & RTF_CACHE) {
Xin Long1e2ea8a2017-08-26 20:10:10 +08001905 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout))
1906 rt->dst.obsolete = DST_OBSOLETE_KILL;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001907 if (atomic_read(&rt->dst.__refcnt) == 1 &&
Xin Long1e2ea8a2017-08-26 20:10:10 +08001908 rt->dst.obsolete == DST_OBSOLETE_KILL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 RT6_TRACE("aging clone %p\n", rt);
1910 return -1;
David S. Miller5339ab82012-01-27 15:14:01 -08001911 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1912 struct neighbour *neigh;
1913 __u8 neigh_flags = 0;
1914
1915 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1916 if (neigh) {
1917 neigh_flags = neigh->flags;
1918 neigh_release(neigh);
1919 }
Thomas Graf8bd74512012-06-07 06:51:04 +00001920 if (!(neigh_flags & NTF_ROUTER)) {
David S. Miller5339ab82012-01-27 15:14:01 -08001921 RT6_TRACE("purging route %p via non-router but gateway\n",
1922 rt);
1923 return -1;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Michal Kubeček3570df92016-03-08 14:44:25 +01001926 gc_args->more++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
1929 return 0;
1930}
1931
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001932void fib6_run_gc(unsigned long expires, struct net *net, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Michal Kubeček3570df92016-03-08 14:44:25 +01001934 struct fib6_gc_args gc_args;
Michal Kubeček49a18d82013-08-01 10:04:24 +02001935 unsigned long now;
1936
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001937 if (force) {
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001938 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1939 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001940 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001943 gc_args.timeout = expires ? (int)expires :
1944 net->ipv6.sysctl.ip6_rt_gc_interval;
Wei Wangdb916642017-06-17 10:42:37 -07001945 gc_args.more = 0;
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001946
Michal Kubeček3570df92016-03-08 14:44:25 +01001947 fib6_clean_all(net, fib6_age, &gc_args);
Michal Kubeček49a18d82013-08-01 10:04:24 +02001948 now = jiffies;
1949 net->ipv6.ip6_rt_last_gc = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001952 mod_timer(&net->ipv6.ip6_fib_timer,
Michal Kubeček49a18d82013-08-01 10:04:24 +02001953 round_jiffies(now
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001954 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001955 else
1956 del_timer(&net->ipv6.ip6_fib_timer);
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001957 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001960static void fib6_gc_timer_cb(unsigned long arg)
1961{
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001962 fib6_run_gc(0, (struct net *)arg, true);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001963}
1964
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001965static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001966{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001967 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
Ido Schimmel16ab6d72017-08-03 13:28:16 +02001968 int err;
1969
1970 err = fib6_notifier_init(net);
1971 if (err)
1972 return err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001973
Michal Kubeček3dc94f92016-03-08 14:44:45 +01001974 spin_lock_init(&net->ipv6.fib6_gc_lock);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01001975 rwlock_init(&net->ipv6.fib6_walker_lock);
1976 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001977 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001978
Benjamin Theryc5728722008-03-03 23:34:17 -08001979 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1980 if (!net->ipv6.rt6_stats)
1981 goto out_timer;
1982
Eric Dumazet10da66f2010-10-13 08:22:03 +00001983 /* Avoid false sharing : Use at least a full cache line */
1984 size = max_t(size_t, size, L1_CACHE_BYTES);
1985
1986 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001987 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001988 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001989
1990 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1991 GFP_KERNEL);
1992 if (!net->ipv6.fib6_main_tbl)
1993 goto out_fib_table_hash;
1994
1995 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001996 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001997 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1998 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07001999 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002000
2001#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2002 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2003 GFP_KERNEL);
2004 if (!net->ipv6.fib6_local_tbl)
2005 goto out_fib6_main_tbl;
2006 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002007 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002008 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2009 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
David S. Miller8e773272012-06-11 00:01:52 -07002010 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002011#endif
2012 fib6_tables_init(net);
2013
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002014 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002015
2016#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2017out_fib6_main_tbl:
2018 kfree(net->ipv6.fib6_main_tbl);
2019#endif
2020out_fib_table_hash:
2021 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002022out_rt6_stats:
2023 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002024out_timer:
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002025 fib6_notifier_exit(net);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002026 return -ENOMEM;
Wang Yufen8db46f12014-03-28 12:07:02 +08002027}
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002028
2029static void fib6_net_exit(struct net *net)
2030{
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002031 unsigned int i;
2032
Daniel Lezcano8ed67782008-03-04 13:48:30 -08002033 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07002034 del_timer_sync(&net->ipv6.ip6_fib_timer);
2035
Eric Dumazet32a805b2017-09-08 15:48:47 -07002036 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
Sabrina Dubrocaba1cc082017-09-08 10:26:19 +02002037 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2038 struct hlist_node *tmp;
2039 struct fib6_table *tb;
2040
2041 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2042 hlist_del(&tb->tb6_hlist);
2043 fib6_free_table(tb);
2044 }
2045 }
2046
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002047 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08002048 kfree(net->ipv6.rt6_stats);
Ido Schimmel16ab6d72017-08-03 13:28:16 +02002049 fib6_notifier_exit(net);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002050}
2051
2052static struct pernet_operations fib6_net_ops = {
2053 .init = fib6_net_init,
2054 .exit = fib6_net_exit,
2055};
2056
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002057int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002059 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2062 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002063 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002064 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08002065 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08002066 goto out;
2067
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002068 ret = register_pernet_subsys(&fib6_net_ops);
2069 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08002070 goto out_kmem_cache_create;
David S. Millere8803b62012-06-16 01:12:19 -07002071
2072 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
Florian Westphalb97bac62017-08-09 20:41:48 +02002073 0);
David S. Millere8803b62012-06-16 01:12:19 -07002074 if (ret)
2075 goto out_unregister_subsys;
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02002076
2077 __fib6_flush_trees = fib6_flush_trees;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002078out:
2079 return ret;
2080
David S. Millere8803b62012-06-16 01:12:19 -07002081out_unregister_subsys:
2082 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08002083out_kmem_cache_create:
2084 kmem_cache_destroy(fib6_node_kmem);
2085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088void fib6_gc_cleanup(void)
2089{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08002090 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 kmem_cache_destroy(fib6_node_kmem);
2092}
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002093
2094#ifdef CONFIG_PROC_FS
2095
2096struct ipv6_route_iter {
2097 struct seq_net_private p;
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002098 struct fib6_walker w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002099 loff_t skip;
2100 struct fib6_table *tbl;
Hannes Frederic Sowa42b18702014-10-06 19:58:35 +02002101 int sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002102};
2103
2104static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2105{
2106 struct rt6_info *rt = v;
2107 struct ipv6_route_iter *iter = seq->private;
2108
2109 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2110
2111#ifdef CONFIG_IPV6_SUBTREES
2112 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2113#else
2114 seq_puts(seq, "00000000000000000000000000000000 00 ");
2115#endif
2116 if (rt->rt6i_flags & RTF_GATEWAY)
2117 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2118 else
2119 seq_puts(seq, "00000000000000000000000000000000");
2120
2121 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2122 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2123 rt->dst.__use, rt->rt6i_flags,
2124 rt->dst.dev ? rt->dst.dev->name : "");
2125 iter->w.leaf = NULL;
2126 return 0;
2127}
2128
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002129static int ipv6_route_yield(struct fib6_walker *w)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002130{
2131 struct ipv6_route_iter *iter = w->args;
2132
2133 if (!iter->skip)
2134 return 1;
2135
2136 do {
2137 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2138 iter->skip--;
2139 if (!iter->skip && iter->w.leaf)
2140 return 1;
2141 } while (iter->w.leaf);
2142
2143 return 0;
2144}
2145
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002146static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2147 struct net *net)
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002148{
2149 memset(&iter->w, 0, sizeof(iter->w));
2150 iter->w.func = ipv6_route_yield;
2151 iter->w.root = &iter->tbl->tb6_root;
2152 iter->w.state = FWS_INIT;
2153 iter->w.node = iter->w.root;
2154 iter->w.args = iter;
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002155 iter->sernum = iter->w.root->fn_sernum;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002156 INIT_LIST_HEAD(&iter->w.lh);
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002157 fib6_walker_link(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002158}
2159
2160static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2161 struct net *net)
2162{
2163 unsigned int h;
2164 struct hlist_node *node;
2165
2166 if (tbl) {
2167 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2168 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2169 } else {
2170 h = 0;
2171 node = NULL;
2172 }
2173
2174 while (!node && h < FIB6_TABLE_HASHSZ) {
2175 node = rcu_dereference_bh(
2176 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2177 }
2178 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2179}
2180
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002181static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2182{
2183 if (iter->sernum != iter->w.root->fn_sernum) {
2184 iter->sernum = iter->w.root->fn_sernum;
2185 iter->w.state = FWS_INIT;
2186 iter->w.node = iter->w.root;
2187 WARN_ON(iter->w.skip);
2188 iter->w.skip = iter->w.count;
2189 }
2190}
2191
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002192static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2193{
2194 int r;
2195 struct rt6_info *n;
2196 struct net *net = seq_file_net(seq);
2197 struct ipv6_route_iter *iter = seq->private;
2198
2199 if (!v)
2200 goto iter_table;
2201
2202 n = ((struct rt6_info *)v)->dst.rt6_next;
2203 if (n) {
2204 ++*pos;
2205 return n;
2206 }
2207
2208iter_table:
Hannes Frederic Sowa0a67d3e2013-09-21 16:56:10 +02002209 ipv6_route_check_sernum(iter);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002210 read_lock(&iter->tbl->tb6_lock);
2211 r = fib6_walk_continue(&iter->w);
2212 read_unlock(&iter->tbl->tb6_lock);
2213 if (r > 0) {
2214 if (v)
2215 ++*pos;
2216 return iter->w.leaf;
2217 } else if (r < 0) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002218 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002219 return NULL;
2220 }
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002221 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002222
2223 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2224 if (!iter->tbl)
2225 return NULL;
2226
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002227 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002228 goto iter_table;
2229}
2230
2231static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2232 __acquires(RCU_BH)
2233{
2234 struct net *net = seq_file_net(seq);
2235 struct ipv6_route_iter *iter = seq->private;
2236
2237 rcu_read_lock_bh();
2238 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2239 iter->skip = *pos;
2240
2241 if (iter->tbl) {
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002242 ipv6_route_seq_setup_walk(iter, net);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002243 return ipv6_route_seq_next(seq, NULL, pos);
2244 } else {
2245 return NULL;
2246 }
2247}
2248
2249static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2250{
Hannes Frederic Sowa94b2cfe2014-10-06 19:58:34 +02002251 struct fib6_walker *w = &iter->w;
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002252 return w->node && !(w->state == FWS_U && w->node == w->root);
2253}
2254
2255static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2256 __releases(RCU_BH)
2257{
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002258 struct net *net = seq_file_net(seq);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002259 struct ipv6_route_iter *iter = seq->private;
2260
2261 if (ipv6_route_iter_active(iter))
Michal Kubeček9a03cd82016-03-08 14:44:35 +01002262 fib6_walker_unlink(net, &iter->w);
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02002263
2264 rcu_read_unlock_bh();
2265}
2266
2267static const struct seq_operations ipv6_route_seq_ops = {
2268 .start = ipv6_route_seq_start,
2269 .next = ipv6_route_seq_next,
2270 .stop = ipv6_route_seq_stop,
2271 .show = ipv6_route_seq_show
2272};
2273
2274int ipv6_route_open(struct inode *inode, struct file *file)
2275{
2276 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2277 sizeof(struct ipv6_route_iter));
2278}
2279
2280#endif /* CONFIG_PROC_FS */