blob: e8a0fcf8850abf663ef75b60968cd44dfc69ebfe [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.
12 */
13
14/*
15 * Changes:
16 * Yuji SEKIYA @USAGI: Support default route on router node;
17 * remove ip6_null_entry from the top of
18 * routing table.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070019 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/net.h>
24#include <linux/route.h>
25#include <linux/netdevice.h>
26#include <linux/in6.h>
27#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070028#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/ipv6.h>
32#include <net/ndisc.h>
33#include <net/addrconf.h>
34
35#include <net/ip6_fib.h>
36#include <net/ip6_route.h>
37
38#define RT6_DEBUG 2
39
40#if RT6_DEBUG >= 3
41#define RT6_TRACE(x...) printk(KERN_DEBUG x)
42#else
43#define RT6_TRACE(x...) do { ; } while (0)
44#endif
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache * fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48enum fib_walk_state_t
49{
50#ifdef CONFIG_IPV6_SUBTREES
51 FWS_S,
52#endif
53 FWS_L,
54 FWS_R,
55 FWS_C,
56 FWS_U
57};
58
59struct fib6_cleaner_t
60{
61 struct fib6_walker_t w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080062 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int (*func)(struct rt6_info *, void *arg);
64 void *arg;
65};
66
Adrian Bunk90d41122006-08-14 23:49:16 -070067static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#ifdef CONFIG_IPV6_SUBTREES
70#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#else
72#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
Benjamin Theryec7d43c2008-03-03 23:31:57 -080075static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
76 struct rt6_info *rt);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080077static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
78static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Adrian Bunk90d41122006-08-14 23:49:16 -070079static int fib6_walk(struct fib6_walker_t *w);
80static int fib6_walk_continue(struct fib6_walker_t *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * A routing update causes an increase of the serial number on the
84 * affected subtree. This allows for cached routes to be asynchronously
85 * tested when modifications are made to the destination cache as a
86 * result of redirects, path MTU changes, etc.
87 */
88
89static __u32 rt_sernum;
90
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080091static void fib6_gc_timer_cb(unsigned long arg);
92
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000093static LIST_HEAD(fib6_walkers);
94#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Adrian Bunk90d41122006-08-14 23:49:16 -070096static inline void fib6_walker_link(struct fib6_walker_t *w)
97{
98 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000099 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -0700100 write_unlock_bh(&fib6_walker_lock);
101}
102
103static inline void fib6_walker_unlink(struct fib6_walker_t *w)
104{
105 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000106 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -0700107 write_unlock_bh(&fib6_walker_lock);
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static __inline__ u32 fib6_new_sernum(void)
110{
111 u32 n = ++rt_sernum;
112 if ((__s32)n <= 0)
113 rt_sernum = n = 1;
114 return n;
115}
116
117/*
118 * Auxiliary address test functions for the radix tree.
119 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900120 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * 64bit processors)
122 */
123
124/*
125 * test bit
126 */
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000127#if defined(__LITTLE_ENDIAN)
128# define BITOP_BE32_SWIZZLE (0x1F & ~7)
129#else
130# define BITOP_BE32_SWIZZLE 0
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000133static __inline__ __be32 addr_bit_set(const void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000135 const __be32 *addr = token;
YOSHIFUJI Hideaki / 吉藤英明02cdce52010-03-27 01:24:16 +0000136 /*
137 * Here,
138 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
139 * is optimized version of
140 * htonl(1 << ((~fn_bit)&0x1F))
141 * See include/asm-generic/bitops/le.h.
142 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700143 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
144 addr[fn_bit >> 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static __inline__ struct fib6_node * node_alloc(void)
148{
149 struct fib6_node *fn;
150
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800151 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return fn;
154}
155
156static __inline__ void node_free(struct fib6_node * fn)
157{
158 kmem_cache_free(fib6_node_kmem, fn);
159}
160
161static __inline__ void rt6_release(struct rt6_info *rt)
162{
163 if (atomic_dec_and_test(&rt->rt6i_ref))
Changli Gaod8d1f302010-06-10 23:31:35 -0700164 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800167static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700168{
169 unsigned int h;
170
Thomas Graf375216a2006-10-21 20:20:54 -0700171 /*
172 * Initialize table lock at a single place to give lockdep a key,
173 * tables aren't visible prior to being linked to the list.
174 */
175 rwlock_init(&tb->tb6_lock);
176
Neil Hormana33bc5c2009-07-30 18:52:15 -0700177 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700178
179 /*
180 * No protection necessary, this is the only list mutatation
181 * operation, tables never disappear once they exist.
182 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800183 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700184}
185
186#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800187
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800188static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700189{
190 struct fib6_table *table;
191
192 table = kzalloc(sizeof(*table), GFP_ATOMIC);
193 if (table != NULL) {
194 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800195 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700196 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
197 }
198
199 return table;
200}
201
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800202struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700203{
204 struct fib6_table *tb;
205
206 if (id == 0)
207 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800208 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700209 if (tb)
210 return tb;
211
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800212 tb = fib6_alloc_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700213 if (tb != NULL)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800214 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700215
216 return tb;
217}
218
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800219struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700220{
221 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800222 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700223 struct hlist_node *node;
224 unsigned int h;
225
226 if (id == 0)
227 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700228 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700229 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800230 head = &net->ipv6.fib_table_hash[h];
231 hlist_for_each_entry_rcu(tb, node, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700232 if (tb->tb6_id == id) {
233 rcu_read_unlock();
234 return tb;
235 }
236 }
237 rcu_read_unlock();
238
239 return NULL;
240}
241
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000242static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700243{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800244 fib6_link_table(net, net->ipv6.fib6_main_tbl);
245 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700246}
Thomas Grafc71099a2006-08-04 23:20:06 -0700247#else
248
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800249struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700250{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800251 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700252}
253
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{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800256 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700257}
258
David S. Miller4c9483b2011-03-12 16:22:43 -0500259struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800260 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700261{
David S. Miller4c9483b2011-03-12 16:22:43 -0500262 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700263}
264
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000265static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700266{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800267 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700268}
269
270#endif
271
Patrick McHardy1b43af52006-08-10 23:11:17 -0700272static int fib6_dump_node(struct fib6_walker_t *w)
273{
274 int res;
275 struct rt6_info *rt;
276
Changli Gaod8d1f302010-06-10 23:31:35 -0700277 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700278 res = rt6_dump_route(rt, w->args);
279 if (res < 0) {
280 /* Frame is full, suspend walking */
281 w->leaf = rt;
282 return 1;
283 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700284 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700285 }
286 w->leaf = NULL;
287 return 0;
288}
289
290static void fib6_dump_end(struct netlink_callback *cb)
291{
292 struct fib6_walker_t *w = (void*)cb->args[2];
293
294 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800295 if (cb->args[4]) {
296 cb->args[4] = 0;
297 fib6_walker_unlink(w);
298 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700299 cb->args[2] = 0;
300 kfree(w);
301 }
302 cb->done = (void*)cb->args[3];
303 cb->args[1] = 3;
304}
305
306static int fib6_dump_done(struct netlink_callback *cb)
307{
308 fib6_dump_end(cb);
309 return cb->done ? cb->done(cb) : 0;
310}
311
312static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
313 struct netlink_callback *cb)
314{
315 struct fib6_walker_t *w;
316 int res;
317
318 w = (void *)cb->args[2];
319 w->root = &table->tb6_root;
320
321 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000322 w->count = 0;
323 w->skip = 0;
324
Patrick McHardy1b43af52006-08-10 23:11:17 -0700325 read_lock_bh(&table->tb6_lock);
326 res = fib6_walk(w);
327 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000328 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700329 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000330 cb->args[5] = w->root->fn_sernum;
331 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700332 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000333 if (cb->args[5] != w->root->fn_sernum) {
334 /* Begin at the root if the tree changed */
335 cb->args[5] = w->root->fn_sernum;
336 w->state = FWS_INIT;
337 w->node = w->root;
338 w->skip = w->count;
339 } else
340 w->skip = 0;
341
Patrick McHardy1b43af52006-08-10 23:11:17 -0700342 read_lock_bh(&table->tb6_lock);
343 res = fib6_walk_continue(w);
344 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800345 if (res <= 0) {
346 fib6_walker_unlink(w);
347 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700348 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700349 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800350
Patrick McHardy1b43af52006-08-10 23:11:17 -0700351 return res;
352}
353
Thomas Grafc127ea22007-03-22 11:58:32 -0700354static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700355{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900356 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700357 unsigned int h, s_h;
358 unsigned int e = 0, s_e;
359 struct rt6_rtnl_dump_arg arg;
360 struct fib6_walker_t *w;
361 struct fib6_table *tb;
362 struct hlist_node *node;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800363 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700364 int res = 0;
365
366 s_h = cb->args[0];
367 s_e = cb->args[1];
368
369 w = (void *)cb->args[2];
370 if (w == NULL) {
371 /* New dump:
372 *
373 * 1. hook callback destructor.
374 */
375 cb->args[3] = (long)cb->done;
376 cb->done = fib6_dump_done;
377
378 /*
379 * 2. allocate and initialize walker.
380 */
381 w = kzalloc(sizeof(*w), GFP_ATOMIC);
382 if (w == NULL)
383 return -ENOMEM;
384 w->func = fib6_dump_node;
385 cb->args[2] = (long)w;
386 }
387
388 arg.skb = skb;
389 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700390 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700391 w->args = &arg;
392
Eric Dumazete67f88d2011-04-27 22:56:07 +0000393 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -0700394 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700395 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800396 head = &net->ipv6.fib_table_hash[h];
Eric Dumazete67f88d2011-04-27 22:56:07 +0000397 hlist_for_each_entry_rcu(tb, node, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700398 if (e < s_e)
399 goto next;
400 res = fib6_dump_table(tb, skb, cb);
401 if (res != 0)
402 goto out;
403next:
404 e++;
405 }
406 }
407out:
Eric Dumazete67f88d2011-04-27 22:56:07 +0000408 rcu_read_unlock();
Patrick McHardy1b43af52006-08-10 23:11:17 -0700409 cb->args[1] = e;
410 cb->args[0] = h;
411
412 res = res < 0 ? res : skb->len;
413 if (res <= 0)
414 fib6_dump_end(cb);
415 return res;
416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418/*
419 * Routing Table
420 *
421 * return the appropriate node for a routing tree "add" operation
422 * by either creating and inserting or by returning an existing
423 * node.
424 */
425
426static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
427 int addrlen, int plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000428 int offset, int allow_create,
429 int replace_required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct fib6_node *fn, *in, *ln;
432 struct fib6_node *pn = NULL;
433 struct rt6key *key;
434 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900435 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 __u32 sernum = fib6_new_sernum();
437
438 RT6_TRACE("fib6_add_1\n");
439
440 /* insert node in tree */
441
442 fn = root;
443
444 do {
445 key = (struct rt6key *)((u8 *)fn->leaf + offset);
446
447 /*
448 * Prefix match
449 */
450 if (plen < fn->fn_bit ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000451 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
452 if (!allow_create)
453 printk(KERN_WARNING
454 "IPv6: NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto insert_above;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000456 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
459 * Exact match ?
460 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (plen == fn->fn_bit) {
463 /* clean up an intermediate node */
464 if ((fn->fn_flags & RTN_RTINFO) == 0) {
465 rt6_release(fn->leaf);
466 fn->leaf = NULL;
467 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return fn;
472 }
473
474 /*
475 * We have more bits to go
476 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* Try to walk down on tree. */
479 fn->fn_sernum = sernum;
480 dir = addr_bit_set(addr, fn->fn_bit);
481 pn = fn;
482 fn = dir ? fn->right: fn->left;
483 } while (fn);
484
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000485 if (replace_required && !allow_create) {
486 /* We should not create new node because
487 * NLM_F_REPLACE was specified without NLM_F_CREATE
488 * I assume it is safe to require NLM_F_CREATE when
489 * REPLACE flag is used! Later we may want to remove the
490 * check for replace_required, because according
491 * to netlink specification, NLM_F_CREATE
492 * MUST be specified if new route is created.
493 * That would keep IPv6 consistent with IPv4
494 */
495 printk(KERN_WARNING
496 "IPv6: NLM_F_CREATE should be set when creating new route - ignoring request\n");
497 return ERR_PTR(-ENOENT);
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * We walked to the bottom of tree.
501 * Create new leaf node without children.
502 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000503 if (!allow_create)
504 printk(KERN_WARNING "IPv6: NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 ln = node_alloc();
507
508 if (ln == NULL)
509 return NULL;
510 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ln->parent = pn;
513 ln->fn_sernum = sernum;
514
515 if (dir)
516 pn->right = ln;
517 else
518 pn->left = ln;
519
520 return ln;
521
522
523insert_above:
524 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900525 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * we have a less significant route.
527 * we've to insert an intermediate node on the list
528 * this new node will point to the one we need to create
529 * and the current
530 */
531
532 pn = fn->parent;
533
534 /* find 1st bit in difference between the 2 addrs.
535
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800536 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 but if it is >= plen, the value is ignored in any case.
538 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900539
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800540 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900542 /*
543 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * / \
545 * (new leaf node)[ln] (old node)[fn]
546 */
547 if (plen > bit) {
548 in = node_alloc();
549 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (in == NULL || ln == NULL) {
552 if (in)
553 node_free(in);
554 if (ln)
555 node_free(ln);
556 return NULL;
557 }
558
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900559 /*
560 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * RTN_RTINFO will
562 * be off since that an address that chooses one of
563 * the branches would not match less specific routes
564 * in the other branch
565 */
566
567 in->fn_bit = bit;
568
569 in->parent = pn;
570 in->leaf = fn->leaf;
571 atomic_inc(&in->leaf->rt6i_ref);
572
573 in->fn_sernum = sernum;
574
575 /* update parent pointer */
576 if (dir)
577 pn->right = in;
578 else
579 pn->left = in;
580
581 ln->fn_bit = plen;
582
583 ln->parent = in;
584 fn->parent = in;
585
586 ln->fn_sernum = sernum;
587
588 if (addr_bit_set(addr, bit)) {
589 in->right = ln;
590 in->left = fn;
591 } else {
592 in->left = ln;
593 in->right = fn;
594 }
595 } else { /* plen <= bit */
596
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900597 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * (new leaf node)[ln]
599 * / \
600 * (old node)[fn] NULL
601 */
602
603 ln = node_alloc();
604
605 if (ln == NULL)
606 return NULL;
607
608 ln->fn_bit = plen;
609
610 ln->parent = pn;
611
612 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (dir)
615 pn->right = ln;
616 else
617 pn->left = ln;
618
619 if (addr_bit_set(&key->addr, plen))
620 ln->right = fn;
621 else
622 ln->left = fn;
623
624 fn->parent = ln;
625 }
626 return ln;
627}
628
629/*
630 * Insert routing information in a node.
631 */
632
633static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Thomas Graf86872cb2006-08-22 00:01:08 -0700634 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct rt6_info *iter = NULL;
637 struct rt6_info **ins;
Matti Vaittinen229a66e2011-11-15 00:58:59 +0000638 int replace = (NULL != info->nlh &&
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000639 (info->nlh->nlmsg_flags&NLM_F_REPLACE));
Matti Vaittinen229a66e2011-11-15 00:58:59 +0000640 int add = (NULL == info->nlh ||
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000641 (info->nlh->nlmsg_flags&NLM_F_CREATE));
642 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 ins = &fn->leaf;
645
Changli Gaod8d1f302010-06-10 23:31:35 -0700646 for (iter = fn->leaf; iter; iter=iter->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Search for duplicates
649 */
650
651 if (iter->rt6i_metric == rt->rt6i_metric) {
652 /*
653 * Same priority level
654 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000655 if (NULL != info->nlh &&
656 (info->nlh->nlmsg_flags&NLM_F_EXCL))
657 return -EEXIST;
658 if (replace) {
659 found++;
660 break;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (iter->rt6i_dev == rt->rt6i_dev &&
664 iter->rt6i_idev == rt->rt6i_idev &&
665 ipv6_addr_equal(&iter->rt6i_gateway,
666 &rt->rt6i_gateway)) {
667 if (!(iter->rt6i_flags&RTF_EXPIRES))
668 return -EEXIST;
669 iter->rt6i_expires = rt->rt6i_expires;
670 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
671 iter->rt6i_flags &= ~RTF_EXPIRES;
672 iter->rt6i_expires = 0;
673 }
674 return -EEXIST;
675 }
676 }
677
678 if (iter->rt6i_metric > rt->rt6i_metric)
679 break;
680
Changli Gaod8d1f302010-06-10 23:31:35 -0700681 ins = &iter->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
David S. Millerf11e6652007-03-24 20:36:25 -0700684 /* Reset round-robin state, if necessary */
685 if (ins == &fn->leaf)
686 fn->rr_ptr = NULL;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * insert node
690 */
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000691 if (!replace) {
692 if (!add)
693 printk(KERN_WARNING "IPv6: NLM_F_CREATE should be set when creating new route\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000695add:
696 rt->dst.rt6_next = iter;
697 *ins = rt;
698 rt->rt6i_node = fn;
699 atomic_inc(&rt->rt6i_ref);
700 inet6_rt_notify(RTM_NEWROUTE, rt, info);
701 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000703 if ((fn->fn_flags & RTN_RTINFO) == 0) {
704 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
705 fn->fn_flags |= RTN_RTINFO;
706 }
707
708 } else {
709 if (!found) {
710 if (add)
711 goto add;
712 printk(KERN_WARNING "IPv6: NLM_F_REPLACE set, but no existing node found!\n");
713 return -ENOENT;
714 }
715 *ins = rt;
716 rt->rt6i_node = fn;
717 rt->dst.rt6_next = iter->dst.rt6_next;
718 atomic_inc(&rt->rt6i_ref);
719 inet6_rt_notify(RTM_NEWROUTE, rt, info);
720 rt6_release(iter);
721 if ((fn->fn_flags & RTN_RTINFO) == 0) {
722 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
723 fn->fn_flags |= RTN_RTINFO;
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 return 0;
728}
729
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800730static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700732 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700734 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700735 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800738void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700740 if (!timer_pending(&net->ipv6.ip6_fib_timer))
741 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700742 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
746 * Add routing information to the routing tree.
747 * <destination addr>/<source addr>
748 * with source addr info in sub-trees
749 */
750
Thomas Graf86872cb2006-08-22 00:01:08 -0700751int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700753 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int err = -ENOMEM;
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000755 int allow_create = 1;
756 int replace_required = 0;
Matti Vaittinen229a66e2011-11-15 00:58:59 +0000757 if (NULL != info->nlh) {
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000758 if (!(info->nlh->nlmsg_flags&NLM_F_CREATE))
759 allow_create = 0;
760 if ((info->nlh->nlmsg_flags&NLM_F_REPLACE))
761 replace_required = 1;
762 }
763 if (!allow_create && !replace_required)
764 printk(KERN_WARNING "IPv6: RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000767 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst),
768 allow_create, replace_required);
769
770 if (IS_ERR(fn)) {
771 err = PTR_ERR(fn);
772 fn = NULL;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (fn == NULL)
776 goto out;
777
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700778 pn = fn;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#ifdef CONFIG_IPV6_SUBTREES
781 if (rt->rt6i_src.plen) {
782 struct fib6_node *sn;
783
784 if (fn->subtree == NULL) {
785 struct fib6_node *sfn;
786
787 /*
788 * Create subtree.
789 *
790 * fn[main tree]
791 * |
792 * sfn[subtree root]
793 * \
794 * sn[new leaf node]
795 */
796
797 /* Create subtree root node */
798 sfn = node_alloc();
799 if (sfn == NULL)
800 goto st_failure;
801
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800802 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
803 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 sfn->fn_flags = RTN_ROOT;
805 sfn->fn_sernum = fib6_new_sernum();
806
807 /* Now add the first leaf node to new subtree */
808
809 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
810 sizeof(struct in6_addr), rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000811 offsetof(struct rt6_info, rt6i_src),
812 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (sn == NULL) {
815 /* If it is failed, discard just allocated
816 root, and then (in st_failure) stale node
817 in main tree.
818 */
819 node_free(sfn);
820 goto st_failure;
821 }
822
823 /* Now link new subtree to main tree */
824 sfn->parent = fn;
825 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 } else {
827 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
828 sizeof(struct in6_addr), rt->rt6i_src.plen,
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000829 offsetof(struct rt6_info, rt6i_src),
830 allow_create, replace_required);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Matti Vaittinen4a287eb2011-11-14 00:15:14 +0000832 if (IS_ERR(sn)) {
833 err = PTR_ERR(sn);
834 sn = NULL;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (sn == NULL)
837 goto st_failure;
838 }
839
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700840 if (fn->leaf == NULL) {
841 fn->leaf = rt;
842 atomic_inc(&rt->rt6i_ref);
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 fn = sn;
845 }
846#endif
847
Thomas Graf86872cb2006-08-22 00:01:08 -0700848 err = fib6_add_rt2node(fn, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (err == 0) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800851 fib6_start_gc(info->nl_net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!(rt->rt6i_flags&RTF_CACHE))
Benjamin Theryec7d43c2008-03-03 23:31:57 -0800853 fib6_prune_clones(info->nl_net, pn, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700857 if (err) {
858#ifdef CONFIG_IPV6_SUBTREES
859 /*
860 * If fib6_add_1 has cleared the old leaf pointer in the
861 * super-tree leaf node we have to find a new one for it.
862 */
David S. Miller3c051232008-04-18 01:46:19 -0700863 if (pn != fn && pn->leaf == rt) {
864 pn->leaf = NULL;
865 atomic_dec(&rt->rt6i_ref);
866 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700867 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800868 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700869#if RT6_DEBUG >= 2
870 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700871 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800872 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700873 }
874#endif
875 atomic_inc(&pn->leaf->rt6i_ref);
876 }
877#endif
Changli Gaod8d1f302010-06-10 23:31:35 -0700878 dst_free(&rt->dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return err;
881
882#ifdef CONFIG_IPV6_SUBTREES
883 /* Subtree creation failed, probably main tree node
884 is orphan. If it is, shoot it.
885 */
886st_failure:
887 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800888 fib6_repair_tree(info->nl_net, fn);
Changli Gaod8d1f302010-06-10 23:31:35 -0700889 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return err;
891#endif
892}
893
894/*
895 * Routing tree lookup
896 *
897 */
898
899struct lookup_args {
900 int offset; /* key offset on rt6_info */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000901 const struct in6_addr *addr; /* search key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902};
903
904static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
905 struct lookup_args *args)
906{
907 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -0800908 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700910 if (unlikely(args->offset == 0))
911 return NULL;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * Descend on a tree
915 */
916
917 fn = root;
918
919 for (;;) {
920 struct fib6_node *next;
921
922 dir = addr_bit_set(args->addr, fn->fn_bit);
923
924 next = dir ? fn->right : fn->left;
925
926 if (next) {
927 fn = next;
928 continue;
929 }
930
931 break;
932 }
933
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700934 while(fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -0700935 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct rt6key *key;
937
938 key = (struct rt6key *) ((u8 *) fn->leaf +
939 args->offset);
940
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700941 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
942#ifdef CONFIG_IPV6_SUBTREES
943 if (fn->subtree)
944 fn = fib6_lookup_1(fn->subtree, args + 1);
945#endif
946 if (!fn || fn->fn_flags & RTN_RTINFO)
947 return fn;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700951 if (fn->fn_flags & RTN_ROOT)
952 break;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 fn = fn->parent;
955 }
956
957 return NULL;
958}
959
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000960struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
961 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700964 struct lookup_args args[] = {
965 {
966 .offset = offsetof(struct rt6_info, rt6i_dst),
967 .addr = daddr,
968 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700970 {
971 .offset = offsetof(struct rt6_info, rt6i_src),
972 .addr = saddr,
973 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700975 {
976 .offset = 0, /* sentinel */
977 }
978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -0700980 fn = fib6_lookup_1(root, daddr ? args : args + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
983 fn = root;
984
985 return fn;
986}
987
988/*
989 * Get node with specified destination prefix (and source prefix,
990 * if subtrees are used)
991 */
992
993
994static struct fib6_node * fib6_locate_1(struct fib6_node *root,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000995 const struct in6_addr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 int plen, int offset)
997{
998 struct fib6_node *fn;
999
1000 for (fn = root; fn ; ) {
1001 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1002
1003 /*
1004 * Prefix match
1005 */
1006 if (plen < fn->fn_bit ||
1007 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1008 return NULL;
1009
1010 if (plen == fn->fn_bit)
1011 return fn;
1012
1013 /*
1014 * We have more bits to go
1015 */
1016 if (addr_bit_set(addr, fn->fn_bit))
1017 fn = fn->right;
1018 else
1019 fn = fn->left;
1020 }
1021 return NULL;
1022}
1023
1024struct fib6_node * fib6_locate(struct fib6_node *root,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001025 const struct in6_addr *daddr, int dst_len,
1026 const struct in6_addr *saddr, int src_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct fib6_node *fn;
1029
1030 fn = fib6_locate_1(root, daddr, dst_len,
1031 offsetof(struct rt6_info, rt6i_dst));
1032
1033#ifdef CONFIG_IPV6_SUBTREES
1034 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001035 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -07001036 if (fn && fn->subtree)
1037 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 offsetof(struct rt6_info, rt6i_src));
1039 }
1040#endif
1041
1042 if (fn && fn->fn_flags&RTN_RTINFO)
1043 return fn;
1044
1045 return NULL;
1046}
1047
1048
1049/*
1050 * Deletion
1051 *
1052 */
1053
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001054static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 if (fn->fn_flags&RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001057 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 while(fn) {
1060 if(fn->left)
1061 return fn->left->leaf;
1062
1063 if(fn->right)
1064 return fn->right->leaf;
1065
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001066 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 return NULL;
1069}
1070
1071/*
1072 * Called to trim the tree of intermediate nodes when possible. "fn"
1073 * is the node we want to try and remove.
1074 */
1075
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001076static struct fib6_node *fib6_repair_tree(struct net *net,
1077 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 int children;
1080 int nstate;
1081 struct fib6_node *child, *pn;
1082 struct fib6_walker_t *w;
1083 int iter = 0;
1084
1085 for (;;) {
1086 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1087 iter++;
1088
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001089 WARN_ON(fn->fn_flags & RTN_RTINFO);
1090 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1091 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 children = 0;
1094 child = NULL;
1095 if (fn->right) child = fn->right, children |= 1;
1096 if (fn->left) child = fn->left, children |= 2;
1097
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001098 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#ifdef CONFIG_IPV6_SUBTREES
1100 /* Subtree root (i.e. fn) may have one child */
1101 || (children && fn->fn_flags&RTN_ROOT)
1102#endif
1103 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001104 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#if RT6_DEBUG >= 2
1106 if (fn->leaf==NULL) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001107 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001108 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110#endif
1111 atomic_inc(&fn->leaf->rt6i_ref);
1112 return fn->parent;
1113 }
1114
1115 pn = fn->parent;
1116#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001117 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001118 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001119 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 nstate = FWS_L;
1121 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001122 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123#endif
1124 if (pn->right == fn) pn->right = child;
1125 else if (pn->left == fn) pn->left = child;
1126#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001127 else
1128 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#endif
1130 if (child)
1131 child->parent = pn;
1132 nstate = FWS_R;
1133#ifdef CONFIG_IPV6_SUBTREES
1134 }
1135#endif
1136
1137 read_lock(&fib6_walker_lock);
1138 FOR_WALKERS(w) {
1139 if (child == NULL) {
1140 if (w->root == fn) {
1141 w->root = w->node = NULL;
1142 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1143 } else if (w->node == fn) {
1144 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1145 w->node = pn;
1146 w->state = nstate;
1147 }
1148 } else {
1149 if (w->root == fn) {
1150 w->root = child;
1151 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1152 }
1153 if (w->node == fn) {
1154 w->node = child;
1155 if (children&2) {
1156 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1157 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
1158 } else {
1159 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1160 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
1161 }
1162 }
1163 }
1164 }
1165 read_unlock(&fib6_walker_lock);
1166
1167 node_free(fn);
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001168 if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return pn;
1170
1171 rt6_release(pn->leaf);
1172 pn->leaf = NULL;
1173 fn = pn;
1174 }
1175}
1176
1177static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001178 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct fib6_walker_t *w;
1181 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001182 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 RT6_TRACE("fib6_del_route\n");
1185
1186 /* Unlink it */
Changli Gaod8d1f302010-06-10 23:31:35 -07001187 *rtp = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001189 net->ipv6.rt6_stats->fib_rt_entries--;
1190 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
David S. Millerf11e6652007-03-24 20:36:25 -07001192 /* Reset round-robin state, if necessary */
1193 if (fn->rr_ptr == rt)
1194 fn->rr_ptr = NULL;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* Adjust walkers */
1197 read_lock(&fib6_walker_lock);
1198 FOR_WALKERS(w) {
1199 if (w->state == FWS_C && w->leaf == rt) {
1200 RT6_TRACE("walker %p adjusted by delroute\n", w);
Changli Gaod8d1f302010-06-10 23:31:35 -07001201 w->leaf = rt->dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (w->leaf == NULL)
1203 w->state = FWS_U;
1204 }
1205 }
1206 read_unlock(&fib6_walker_lock);
1207
Changli Gaod8d1f302010-06-10 23:31:35 -07001208 rt->dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* If it was last route, expunge its radix tree node */
1211 if (fn->leaf == NULL) {
1212 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001213 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001214 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
1217 if (atomic_read(&rt->rt6i_ref) != 1) {
1218 /* This route is used as dummy address holder in some split
1219 * nodes. It is not leaked, but it still holds other resources,
1220 * which must be released in time. So, scan ascendant nodes
1221 * and replace dummy references to this route with references
1222 * to still alive ones.
1223 */
1224 while (fn) {
1225 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001226 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 atomic_inc(&fn->leaf->rt6i_ref);
1228 rt6_release(rt);
1229 }
1230 fn = fn->parent;
1231 }
1232 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001233 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Thomas Graf86872cb2006-08-22 00:01:08 -07001236 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 rt6_release(rt);
1238}
1239
Thomas Graf86872cb2006-08-22 00:01:08 -07001240int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001242 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct fib6_node *fn = rt->rt6i_node;
1244 struct rt6_info **rtp;
1245
1246#if RT6_DEBUG >= 2
Changli Gaod8d1f302010-06-10 23:31:35 -07001247 if (rt->dst.obsolete>0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001248 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -ENOENT;
1250 }
1251#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001252 if (fn == NULL || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -ENOENT;
1254
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001255 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001257 if (!(rt->rt6i_flags&RTF_CACHE)) {
1258 struct fib6_node *pn = fn;
1259#ifdef CONFIG_IPV6_SUBTREES
1260 /* clones of this route might be in another subtree */
1261 if (rt->rt6i_src.plen) {
1262 while (!(pn->fn_flags&RTN_ROOT))
1263 pn = pn->parent;
1264 pn = pn->parent;
1265 }
1266#endif
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001267 fib6_prune_clones(info->nl_net, pn, rt);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /*
1271 * Walk the leaf entries looking for ourself
1272 */
1273
Changli Gaod8d1f302010-06-10 23:31:35 -07001274 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001276 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278 }
1279 }
1280 return -ENOENT;
1281}
1282
1283/*
1284 * Tree traversal function.
1285 *
1286 * Certainly, it is not interrupt safe.
1287 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1288 * It means, that we can modify tree during walking
1289 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001290 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 *
1292 * It guarantees that every node will be traversed,
1293 * and that it will be traversed only once.
1294 *
1295 * Callback function w->func may return:
1296 * 0 -> continue walking.
1297 * positive value -> walking is suspended (used by tree dumps,
1298 * and probably by gc, if it will be split to several slices)
1299 * negative value -> terminate walking.
1300 *
1301 * The function itself returns:
1302 * 0 -> walk is complete.
1303 * >0 -> walk is incomplete (i.e. suspended)
1304 * <0 -> walk is terminated by an error.
1305 */
1306
Adrian Bunk90d41122006-08-14 23:49:16 -07001307static int fib6_walk_continue(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct fib6_node *fn, *pn;
1310
1311 for (;;) {
1312 fn = w->node;
1313 if (fn == NULL)
1314 return 0;
1315
1316 if (w->prune && fn != w->root &&
1317 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1318 w->state = FWS_C;
1319 w->leaf = fn->leaf;
1320 }
1321 switch (w->state) {
1322#ifdef CONFIG_IPV6_SUBTREES
1323 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001324 if (FIB6_SUBTREE(fn)) {
1325 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 continue;
1327 }
1328 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001329#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case FWS_L:
1331 if (fn->left) {
1332 w->node = fn->left;
1333 w->state = FWS_INIT;
1334 continue;
1335 }
1336 w->state = FWS_R;
1337 case FWS_R:
1338 if (fn->right) {
1339 w->node = fn->right;
1340 w->state = FWS_INIT;
1341 continue;
1342 }
1343 w->state = FWS_C;
1344 w->leaf = fn->leaf;
1345 case FWS_C:
1346 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001347 int err;
1348
1349 if (w->count < w->skip) {
1350 w->count++;
1351 continue;
1352 }
1353
1354 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (err)
1356 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001357
1358 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 continue;
1360 }
1361 w->state = FWS_U;
1362 case FWS_U:
1363 if (fn == w->root)
1364 return 0;
1365 pn = fn->parent;
1366 w->node = pn;
1367#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001368 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001369 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 w->state = FWS_L;
1371 continue;
1372 }
1373#endif
1374 if (pn->left == fn) {
1375 w->state = FWS_R;
1376 continue;
1377 }
1378 if (pn->right == fn) {
1379 w->state = FWS_C;
1380 w->leaf = w->node->leaf;
1381 continue;
1382 }
1383#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001384 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385#endif
1386 }
1387 }
1388}
1389
Adrian Bunk90d41122006-08-14 23:49:16 -07001390static int fib6_walk(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 int res;
1393
1394 w->state = FWS_INIT;
1395 w->node = w->root;
1396
1397 fib6_walker_link(w);
1398 res = fib6_walk_continue(w);
1399 if (res <= 0)
1400 fib6_walker_unlink(w);
1401 return res;
1402}
1403
1404static int fib6_clean_node(struct fib6_walker_t *w)
1405{
1406 int res;
1407 struct rt6_info *rt;
Benjamin Thery0a8891a2007-10-08 20:39:36 -07001408 struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001409 struct nl_info info = {
1410 .nl_net = c->net,
1411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Changli Gaod8d1f302010-06-10 23:31:35 -07001413 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 res = c->func(rt, c->arg);
1415 if (res < 0) {
1416 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001417 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (res) {
1419#if RT6_DEBUG >= 2
1420 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1421#endif
1422 continue;
1423 }
1424 return 0;
1425 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001426 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 w->leaf = rt;
1429 return 0;
1430}
1431
1432/*
1433 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001434 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * func is called on each route.
1436 * It may return -1 -> delete this route.
1437 * 0 -> continue walking
1438 *
1439 * prune==1 -> only immediate children of node (certainly,
1440 * ignoring pure split nodes) will be scanned.
1441 */
1442
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001443static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001444 int (*func)(struct rt6_info *, void *arg),
1445 int prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct fib6_cleaner_t c;
1448
1449 c.w.root = root;
1450 c.w.func = fib6_clean_node;
1451 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001452 c.w.count = 0;
1453 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 c.func = func;
1455 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001456 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 fib6_walk(&c.w);
1459}
1460
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001461void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Thomas Grafc71099a2006-08-04 23:20:06 -07001462 int prune, void *arg)
1463{
Thomas Grafc71099a2006-08-04 23:20:06 -07001464 struct fib6_table *table;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001465 struct hlist_node *node;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001466 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001467 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001468
Patrick McHardy1b43af52006-08-10 23:11:17 -07001469 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001470 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001471 head = &net->ipv6.fib_table_hash[h];
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001472 hlist_for_each_entry_rcu(table, node, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001473 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001474 fib6_clean_tree(net, &table->tb6_root,
1475 func, prune, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001476 write_unlock_bh(&table->tb6_lock);
1477 }
1478 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001479 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001480}
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1483{
1484 if (rt->rt6i_flags & RTF_CACHE) {
1485 RT6_TRACE("pruning clone %p\n", rt);
1486 return -1;
1487 }
1488
1489 return 0;
1490}
1491
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001492static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
1493 struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001495 fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498/*
1499 * Garbage collection
1500 */
1501
1502static struct fib6_gc_args
1503{
1504 int timeout;
1505 int more;
1506} gc_args;
1507
1508static int fib6_age(struct rt6_info *rt, void *arg)
1509{
1510 unsigned long now = jiffies;
1511
1512 /*
1513 * check addrconf expiration here.
1514 * Routes are expired even if they are in use.
1515 *
1516 * Also age clones. Note, that clones are aged out
1517 * only if they are not in use now.
1518 */
1519
1520 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1521 if (time_after(now, rt->rt6i_expires)) {
1522 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -1;
1524 }
1525 gc_args.more++;
1526 } else if (rt->rt6i_flags & RTF_CACHE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001527 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1528 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 RT6_TRACE("aging clone %p\n", rt);
1530 return -1;
1531 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
Eric Dumazetf2c31e32011-07-29 19:00:53 +00001532 (!(dst_get_neighbour_raw(&rt->dst)->flags & NTF_ROUTER))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 RT6_TRACE("purging route %p via non-router but gateway\n",
1534 rt);
1535 return -1;
1536 }
1537 gc_args.more++;
1538 }
1539
1540 return 0;
1541}
1542
1543static DEFINE_SPINLOCK(fib6_gc_lock);
1544
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001545void fib6_run_gc(unsigned long expires, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001547 if (expires != ~0UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_lock_bh(&fib6_gc_lock);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001549 gc_args.timeout = expires ? (int)expires :
1550 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 } else {
Stephen Hemmingera76d7342008-07-22 14:34:35 -07001552 if (!spin_trylock_bh(&fib6_gc_lock)) {
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001553 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return;
1555 }
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001556 gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001559 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001560
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001561 fib6_clean_all(net, fib6_age, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001564 mod_timer(&net->ipv6.ip6_fib_timer,
1565 round_jiffies(jiffies
1566 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001567 else
1568 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 spin_unlock_bh(&fib6_gc_lock);
1570}
1571
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001572static void fib6_gc_timer_cb(unsigned long arg)
1573{
1574 fib6_run_gc(0, (struct net *)arg);
1575}
1576
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001577static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001578{
Eric Dumazet10da66f2010-10-13 08:22:03 +00001579 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1580
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001581 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001582
Benjamin Theryc5728722008-03-03 23:34:17 -08001583 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1584 if (!net->ipv6.rt6_stats)
1585 goto out_timer;
1586
Eric Dumazet10da66f2010-10-13 08:22:03 +00001587 /* Avoid false sharing : Use at least a full cache line */
1588 size = max_t(size_t, size, L1_CACHE_BYTES);
1589
1590 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001591 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001592 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001593
1594 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1595 GFP_KERNEL);
1596 if (!net->ipv6.fib6_main_tbl)
1597 goto out_fib_table_hash;
1598
1599 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001600 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001601 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1602 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1603
1604#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1605 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1606 GFP_KERNEL);
1607 if (!net->ipv6.fib6_local_tbl)
1608 goto out_fib6_main_tbl;
1609 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001610 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001611 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1612 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1613#endif
1614 fib6_tables_init(net);
1615
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001616 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001617
1618#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1619out_fib6_main_tbl:
1620 kfree(net->ipv6.fib6_main_tbl);
1621#endif
1622out_fib_table_hash:
1623 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001624out_rt6_stats:
1625 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001626out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001627 return -ENOMEM;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001628 }
1629
1630static void fib6_net_exit(struct net *net)
1631{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001632 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001633 del_timer_sync(&net->ipv6.ip6_fib_timer);
1634
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001635#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1636 kfree(net->ipv6.fib6_local_tbl);
1637#endif
1638 kfree(net->ipv6.fib6_main_tbl);
1639 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001640 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001641}
1642
1643static struct pernet_operations fib6_net_ops = {
1644 .init = fib6_net_init,
1645 .exit = fib6_net_exit,
1646};
1647
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001648int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001650 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1653 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001654 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001655 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001656 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001657 goto out;
1658
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001659 ret = register_pernet_subsys(&fib6_net_ops);
1660 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001661 goto out_kmem_cache_create;
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001662
Greg Rosec7ac8672011-06-10 01:27:09 +00001663 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1664 NULL);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001665 if (ret)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001666 goto out_unregister_subsys;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001667out:
1668 return ret;
1669
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001670out_unregister_subsys:
1671 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001672out_kmem_cache_create:
1673 kmem_cache_destroy(fib6_node_kmem);
1674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677void fib6_gc_cleanup(void)
1678{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001679 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 kmem_cache_destroy(fib6_node_kmem);
1681}