Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Forwarding Information Database |
| 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * Changes: |
| 18 | * Yuji SEKIYA @USAGI: Support default route on router node; |
| 19 | * remove ip6_null_entry from the top of |
| 20 | * routing table. |
YOSHIFUJI Hideaki | c0bece9 | 2006-08-23 17:23:25 -0700 | [diff] [blame] | 21 | * Ville Nuorvala: Fixed routing subtrees. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/errno.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/route.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/in6.h> |
| 29 | #include <linux/init.h> |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 30 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #ifdef CONFIG_PROC_FS |
| 33 | #include <linux/proc_fs.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <net/ipv6.h> |
| 37 | #include <net/ndisc.h> |
| 38 | #include <net/addrconf.h> |
| 39 | |
| 40 | #include <net/ip6_fib.h> |
| 41 | #include <net/ip6_route.h> |
| 42 | |
| 43 | #define RT6_DEBUG 2 |
| 44 | |
| 45 | #if RT6_DEBUG >= 3 |
| 46 | #define RT6_TRACE(x...) printk(KERN_DEBUG x) |
| 47 | #else |
| 48 | #define RT6_TRACE(x...) do { ; } while (0) |
| 49 | #endif |
| 50 | |
| 51 | struct rt6_statistics rt6_stats; |
| 52 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 53 | static struct kmem_cache * fib6_node_kmem __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | enum fib_walk_state_t |
| 56 | { |
| 57 | #ifdef CONFIG_IPV6_SUBTREES |
| 58 | FWS_S, |
| 59 | #endif |
| 60 | FWS_L, |
| 61 | FWS_R, |
| 62 | FWS_C, |
| 63 | FWS_U |
| 64 | }; |
| 65 | |
| 66 | struct fib6_cleaner_t |
| 67 | { |
| 68 | struct fib6_walker_t w; |
| 69 | int (*func)(struct rt6_info *, void *arg); |
| 70 | void *arg; |
| 71 | }; |
| 72 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 73 | static DEFINE_RWLOCK(fib6_walker_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | #ifdef CONFIG_IPV6_SUBTREES |
| 76 | #define FWS_INIT FWS_S |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #else |
| 78 | #define FWS_INIT FWS_L |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt); |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 82 | static struct rt6_info * fib6_find_prefix(struct fib6_node *fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | static struct fib6_node * fib6_repair_tree(struct fib6_node *fn); |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 84 | static int fib6_walk(struct fib6_walker_t *w); |
| 85 | static int fib6_walk_continue(struct fib6_walker_t *w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * A routing update causes an increase of the serial number on the |
| 89 | * affected subtree. This allows for cached routes to be asynchronously |
| 90 | * tested when modifications are made to the destination cache as a |
| 91 | * result of redirects, path MTU changes, etc. |
| 92 | */ |
| 93 | |
| 94 | static __u32 rt_sernum; |
| 95 | |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 96 | static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 98 | static struct fib6_walker_t fib6_walker_list = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | .prev = &fib6_walker_list, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 100 | .next = &fib6_walker_list, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next) |
| 104 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 105 | static inline void fib6_walker_link(struct fib6_walker_t *w) |
| 106 | { |
| 107 | write_lock_bh(&fib6_walker_lock); |
| 108 | w->next = fib6_walker_list.next; |
| 109 | w->prev = &fib6_walker_list; |
| 110 | w->next->prev = w; |
| 111 | w->prev->next = w; |
| 112 | write_unlock_bh(&fib6_walker_lock); |
| 113 | } |
| 114 | |
| 115 | static inline void fib6_walker_unlink(struct fib6_walker_t *w) |
| 116 | { |
| 117 | write_lock_bh(&fib6_walker_lock); |
| 118 | w->next->prev = w->prev; |
| 119 | w->prev->next = w->next; |
| 120 | w->prev = w->next = w; |
| 121 | write_unlock_bh(&fib6_walker_lock); |
| 122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | static __inline__ u32 fib6_new_sernum(void) |
| 124 | { |
| 125 | u32 n = ++rt_sernum; |
| 126 | if ((__s32)n <= 0) |
| 127 | rt_sernum = n = 1; |
| 128 | return n; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Auxiliary address test functions for the radix tree. |
| 133 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 134 | * These assume a 32bit processor (although it will work on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * 64bit processors) |
| 136 | */ |
| 137 | |
| 138 | /* |
| 139 | * test bit |
| 140 | */ |
| 141 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 142 | static __inline__ __be32 addr_bit_set(void *token, int fn_bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 144 | __be32 *addr = token; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5]; |
| 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static __inline__ struct fib6_node * node_alloc(void) |
| 150 | { |
| 151 | struct fib6_node *fn; |
| 152 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 153 | fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | return fn; |
| 156 | } |
| 157 | |
| 158 | static __inline__ void node_free(struct fib6_node * fn) |
| 159 | { |
| 160 | kmem_cache_free(fib6_node_kmem, fn); |
| 161 | } |
| 162 | |
| 163 | static __inline__ void rt6_release(struct rt6_info *rt) |
| 164 | { |
| 165 | if (atomic_dec_and_test(&rt->rt6i_ref)) |
| 166 | dst_free(&rt->u.dst); |
| 167 | } |
| 168 | |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 169 | static struct fib6_table *fib6_main_tbl; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 170 | |
| 171 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 172 | #define FIB_TABLE_HASHSZ 256 |
| 173 | #else |
| 174 | #define FIB_TABLE_HASHSZ 1 |
| 175 | #endif |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 176 | static struct hlist_head *fib_table_hash; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 177 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 178 | static void fib6_link_table(struct fib6_table *tb) |
| 179 | { |
| 180 | unsigned int h; |
| 181 | |
Thomas Graf | 375216a | 2006-10-21 20:20:54 -0700 | [diff] [blame] | 182 | /* |
| 183 | * Initialize table lock at a single place to give lockdep a key, |
| 184 | * tables aren't visible prior to being linked to the list. |
| 185 | */ |
| 186 | rwlock_init(&tb->tb6_lock); |
| 187 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 188 | h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1); |
| 189 | |
| 190 | /* |
| 191 | * No protection necessary, this is the only list mutatation |
| 192 | * operation, tables never disappear once they exist. |
| 193 | */ |
| 194 | hlist_add_head_rcu(&tb->tb6_hlist, &fib_table_hash[h]); |
| 195 | } |
| 196 | |
| 197 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 198 | |
| 199 | static struct fib6_table *fib6_local_tbl; |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 200 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 201 | static struct fib6_table *fib6_alloc_table(u32 id) |
| 202 | { |
| 203 | struct fib6_table *table; |
| 204 | |
| 205 | table = kzalloc(sizeof(*table), GFP_ATOMIC); |
| 206 | if (table != NULL) { |
| 207 | table->tb6_id = id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 208 | table->tb6_root.leaf = &ip6_null_entry; |
| 209 | table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
| 210 | } |
| 211 | |
| 212 | return table; |
| 213 | } |
| 214 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 215 | struct fib6_table *fib6_new_table(u32 id) |
| 216 | { |
| 217 | struct fib6_table *tb; |
| 218 | |
| 219 | if (id == 0) |
| 220 | id = RT6_TABLE_MAIN; |
| 221 | tb = fib6_get_table(id); |
| 222 | if (tb) |
| 223 | return tb; |
| 224 | |
| 225 | tb = fib6_alloc_table(id); |
| 226 | if (tb != NULL) |
| 227 | fib6_link_table(tb); |
| 228 | |
| 229 | return tb; |
| 230 | } |
| 231 | |
| 232 | struct fib6_table *fib6_get_table(u32 id) |
| 233 | { |
| 234 | struct fib6_table *tb; |
| 235 | struct hlist_node *node; |
| 236 | unsigned int h; |
| 237 | |
| 238 | if (id == 0) |
| 239 | id = RT6_TABLE_MAIN; |
| 240 | h = id & (FIB_TABLE_HASHSZ - 1); |
| 241 | rcu_read_lock(); |
| 242 | hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb6_hlist) { |
| 243 | if (tb->tb6_id == id) { |
| 244 | rcu_read_unlock(); |
| 245 | return tb; |
| 246 | } |
| 247 | } |
| 248 | rcu_read_unlock(); |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 253 | static void __init fib6_tables_init(void) |
| 254 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 255 | fib6_link_table(fib6_main_tbl); |
| 256 | fib6_link_table(fib6_local_tbl); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | #else |
| 260 | |
| 261 | struct fib6_table *fib6_new_table(u32 id) |
| 262 | { |
| 263 | return fib6_get_table(id); |
| 264 | } |
| 265 | |
| 266 | struct fib6_table *fib6_get_table(u32 id) |
| 267 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 268 | return fib6_main_tbl; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags, |
| 272 | pol_lookup_t lookup) |
| 273 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 274 | return (struct dst_entry *) lookup(fib6_main_tbl, fl, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static void __init fib6_tables_init(void) |
| 278 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 279 | fib6_link_table(fib6_main_tbl); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | #endif |
| 283 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 284 | static int fib6_dump_node(struct fib6_walker_t *w) |
| 285 | { |
| 286 | int res; |
| 287 | struct rt6_info *rt; |
| 288 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 289 | for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 290 | res = rt6_dump_route(rt, w->args); |
| 291 | if (res < 0) { |
| 292 | /* Frame is full, suspend walking */ |
| 293 | w->leaf = rt; |
| 294 | return 1; |
| 295 | } |
| 296 | BUG_TRAP(res!=0); |
| 297 | } |
| 298 | w->leaf = NULL; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static void fib6_dump_end(struct netlink_callback *cb) |
| 303 | { |
| 304 | struct fib6_walker_t *w = (void*)cb->args[2]; |
| 305 | |
| 306 | if (w) { |
| 307 | cb->args[2] = 0; |
| 308 | kfree(w); |
| 309 | } |
| 310 | cb->done = (void*)cb->args[3]; |
| 311 | cb->args[1] = 3; |
| 312 | } |
| 313 | |
| 314 | static int fib6_dump_done(struct netlink_callback *cb) |
| 315 | { |
| 316 | fib6_dump_end(cb); |
| 317 | return cb->done ? cb->done(cb) : 0; |
| 318 | } |
| 319 | |
| 320 | static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, |
| 321 | struct netlink_callback *cb) |
| 322 | { |
| 323 | struct fib6_walker_t *w; |
| 324 | int res; |
| 325 | |
| 326 | w = (void *)cb->args[2]; |
| 327 | w->root = &table->tb6_root; |
| 328 | |
| 329 | if (cb->args[4] == 0) { |
| 330 | read_lock_bh(&table->tb6_lock); |
| 331 | res = fib6_walk(w); |
| 332 | read_unlock_bh(&table->tb6_lock); |
| 333 | if (res > 0) |
| 334 | cb->args[4] = 1; |
| 335 | } else { |
| 336 | read_lock_bh(&table->tb6_lock); |
| 337 | res = fib6_walk_continue(w); |
| 338 | read_unlock_bh(&table->tb6_lock); |
| 339 | if (res != 0) { |
| 340 | if (res < 0) |
| 341 | fib6_walker_unlink(w); |
| 342 | goto end; |
| 343 | } |
| 344 | fib6_walker_unlink(w); |
| 345 | cb->args[4] = 0; |
| 346 | } |
| 347 | end: |
| 348 | return res; |
| 349 | } |
| 350 | |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 351 | static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 352 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 353 | struct net *net = skb->sk->sk_net; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 354 | unsigned int h, s_h; |
| 355 | unsigned int e = 0, s_e; |
| 356 | struct rt6_rtnl_dump_arg arg; |
| 357 | struct fib6_walker_t *w; |
| 358 | struct fib6_table *tb; |
| 359 | struct hlist_node *node; |
| 360 | int res = 0; |
| 361 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 362 | if (net != &init_net) |
| 363 | return 0; |
| 364 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 365 | s_h = cb->args[0]; |
| 366 | s_e = cb->args[1]; |
| 367 | |
| 368 | w = (void *)cb->args[2]; |
| 369 | if (w == NULL) { |
| 370 | /* New dump: |
| 371 | * |
| 372 | * 1. hook callback destructor. |
| 373 | */ |
| 374 | cb->args[3] = (long)cb->done; |
| 375 | cb->done = fib6_dump_done; |
| 376 | |
| 377 | /* |
| 378 | * 2. allocate and initialize walker. |
| 379 | */ |
| 380 | w = kzalloc(sizeof(*w), GFP_ATOMIC); |
| 381 | if (w == NULL) |
| 382 | return -ENOMEM; |
| 383 | w->func = fib6_dump_node; |
| 384 | cb->args[2] = (long)w; |
| 385 | } |
| 386 | |
| 387 | arg.skb = skb; |
| 388 | arg.cb = cb; |
| 389 | w->args = &arg; |
| 390 | |
| 391 | for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) { |
| 392 | e = 0; |
| 393 | hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) { |
| 394 | if (e < s_e) |
| 395 | goto next; |
| 396 | res = fib6_dump_table(tb, skb, cb); |
| 397 | if (res != 0) |
| 398 | goto out; |
| 399 | next: |
| 400 | e++; |
| 401 | } |
| 402 | } |
| 403 | out: |
| 404 | cb->args[1] = e; |
| 405 | cb->args[0] = h; |
| 406 | |
| 407 | res = res < 0 ? res : skb->len; |
| 408 | if (res <= 0) |
| 409 | fib6_dump_end(cb); |
| 410 | return res; |
| 411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * Routing Table |
| 415 | * |
| 416 | * return the appropriate node for a routing tree "add" operation |
| 417 | * by either creating and inserting or by returning an existing |
| 418 | * node. |
| 419 | */ |
| 420 | |
| 421 | static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr, |
| 422 | int addrlen, int plen, |
| 423 | int offset) |
| 424 | { |
| 425 | struct fib6_node *fn, *in, *ln; |
| 426 | struct fib6_node *pn = NULL; |
| 427 | struct rt6key *key; |
| 428 | int bit; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 429 | __be32 dir = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | __u32 sernum = fib6_new_sernum(); |
| 431 | |
| 432 | RT6_TRACE("fib6_add_1\n"); |
| 433 | |
| 434 | /* insert node in tree */ |
| 435 | |
| 436 | fn = root; |
| 437 | |
| 438 | do { |
| 439 | key = (struct rt6key *)((u8 *)fn->leaf + offset); |
| 440 | |
| 441 | /* |
| 442 | * Prefix match |
| 443 | */ |
| 444 | if (plen < fn->fn_bit || |
| 445 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) |
| 446 | goto insert_above; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* |
| 449 | * Exact match ? |
| 450 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (plen == fn->fn_bit) { |
| 453 | /* clean up an intermediate node */ |
| 454 | if ((fn->fn_flags & RTN_RTINFO) == 0) { |
| 455 | rt6_release(fn->leaf); |
| 456 | fn->leaf = NULL; |
| 457 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | fn->fn_sernum = sernum; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return fn; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * We have more bits to go |
| 466 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* Try to walk down on tree. */ |
| 469 | fn->fn_sernum = sernum; |
| 470 | dir = addr_bit_set(addr, fn->fn_bit); |
| 471 | pn = fn; |
| 472 | fn = dir ? fn->right: fn->left; |
| 473 | } while (fn); |
| 474 | |
| 475 | /* |
| 476 | * We walked to the bottom of tree. |
| 477 | * Create new leaf node without children. |
| 478 | */ |
| 479 | |
| 480 | ln = node_alloc(); |
| 481 | |
| 482 | if (ln == NULL) |
| 483 | return NULL; |
| 484 | ln->fn_bit = plen; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | ln->parent = pn; |
| 487 | ln->fn_sernum = sernum; |
| 488 | |
| 489 | if (dir) |
| 490 | pn->right = ln; |
| 491 | else |
| 492 | pn->left = ln; |
| 493 | |
| 494 | return ln; |
| 495 | |
| 496 | |
| 497 | insert_above: |
| 498 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 499 | * split since we don't have a common prefix anymore or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | * we have a less significant route. |
| 501 | * we've to insert an intermediate node on the list |
| 502 | * this new node will point to the one we need to create |
| 503 | * and the current |
| 504 | */ |
| 505 | |
| 506 | pn = fn->parent; |
| 507 | |
| 508 | /* find 1st bit in difference between the 2 addrs. |
| 509 | |
YOSHIFUJI Hideaki | 971f359 | 2005-11-08 09:37:56 -0800 | [diff] [blame] | 510 | See comment in __ipv6_addr_diff: bit may be an invalid value, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | but if it is >= plen, the value is ignored in any case. |
| 512 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 513 | |
YOSHIFUJI Hideaki | 971f359 | 2005-11-08 09:37:56 -0800 | [diff] [blame] | 514 | bit = __ipv6_addr_diff(addr, &key->addr, addrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 516 | /* |
| 517 | * (intermediate)[in] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | * / \ |
| 519 | * (new leaf node)[ln] (old node)[fn] |
| 520 | */ |
| 521 | if (plen > bit) { |
| 522 | in = node_alloc(); |
| 523 | ln = node_alloc(); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (in == NULL || ln == NULL) { |
| 526 | if (in) |
| 527 | node_free(in); |
| 528 | if (ln) |
| 529 | node_free(ln); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 533 | /* |
| 534 | * new intermediate node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | * RTN_RTINFO will |
| 536 | * be off since that an address that chooses one of |
| 537 | * the branches would not match less specific routes |
| 538 | * in the other branch |
| 539 | */ |
| 540 | |
| 541 | in->fn_bit = bit; |
| 542 | |
| 543 | in->parent = pn; |
| 544 | in->leaf = fn->leaf; |
| 545 | atomic_inc(&in->leaf->rt6i_ref); |
| 546 | |
| 547 | in->fn_sernum = sernum; |
| 548 | |
| 549 | /* update parent pointer */ |
| 550 | if (dir) |
| 551 | pn->right = in; |
| 552 | else |
| 553 | pn->left = in; |
| 554 | |
| 555 | ln->fn_bit = plen; |
| 556 | |
| 557 | ln->parent = in; |
| 558 | fn->parent = in; |
| 559 | |
| 560 | ln->fn_sernum = sernum; |
| 561 | |
| 562 | if (addr_bit_set(addr, bit)) { |
| 563 | in->right = ln; |
| 564 | in->left = fn; |
| 565 | } else { |
| 566 | in->left = ln; |
| 567 | in->right = fn; |
| 568 | } |
| 569 | } else { /* plen <= bit */ |
| 570 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 571 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | * (new leaf node)[ln] |
| 573 | * / \ |
| 574 | * (old node)[fn] NULL |
| 575 | */ |
| 576 | |
| 577 | ln = node_alloc(); |
| 578 | |
| 579 | if (ln == NULL) |
| 580 | return NULL; |
| 581 | |
| 582 | ln->fn_bit = plen; |
| 583 | |
| 584 | ln->parent = pn; |
| 585 | |
| 586 | ln->fn_sernum = sernum; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (dir) |
| 589 | pn->right = ln; |
| 590 | else |
| 591 | pn->left = ln; |
| 592 | |
| 593 | if (addr_bit_set(&key->addr, plen)) |
| 594 | ln->right = fn; |
| 595 | else |
| 596 | ln->left = fn; |
| 597 | |
| 598 | fn->parent = ln; |
| 599 | } |
| 600 | return ln; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Insert routing information in a node. |
| 605 | */ |
| 606 | |
| 607 | static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 608 | struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
| 610 | struct rt6_info *iter = NULL; |
| 611 | struct rt6_info **ins; |
| 612 | |
| 613 | ins = &fn->leaf; |
| 614 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 615 | for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /* |
| 617 | * Search for duplicates |
| 618 | */ |
| 619 | |
| 620 | if (iter->rt6i_metric == rt->rt6i_metric) { |
| 621 | /* |
| 622 | * Same priority level |
| 623 | */ |
| 624 | |
| 625 | if (iter->rt6i_dev == rt->rt6i_dev && |
| 626 | iter->rt6i_idev == rt->rt6i_idev && |
| 627 | ipv6_addr_equal(&iter->rt6i_gateway, |
| 628 | &rt->rt6i_gateway)) { |
| 629 | if (!(iter->rt6i_flags&RTF_EXPIRES)) |
| 630 | return -EEXIST; |
| 631 | iter->rt6i_expires = rt->rt6i_expires; |
| 632 | if (!(rt->rt6i_flags&RTF_EXPIRES)) { |
| 633 | iter->rt6i_flags &= ~RTF_EXPIRES; |
| 634 | iter->rt6i_expires = 0; |
| 635 | } |
| 636 | return -EEXIST; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (iter->rt6i_metric > rt->rt6i_metric) |
| 641 | break; |
| 642 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 643 | ins = &iter->u.dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 646 | /* Reset round-robin state, if necessary */ |
| 647 | if (ins == &fn->leaf) |
| 648 | fn->rr_ptr = NULL; |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | /* |
| 651 | * insert node |
| 652 | */ |
| 653 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 654 | rt->u.dst.rt6_next = iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | *ins = rt; |
| 656 | rt->rt6i_node = fn; |
| 657 | atomic_inc(&rt->rt6i_ref); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 658 | inet6_rt_notify(RTM_NEWROUTE, rt, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | rt6_stats.fib_rt_entries++; |
| 660 | |
| 661 | if ((fn->fn_flags & RTN_RTINFO) == 0) { |
| 662 | rt6_stats.fib_route_nodes++; |
| 663 | fn->fn_flags |= RTN_RTINFO; |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static __inline__ void fib6_start_gc(struct rt6_info *rt) |
| 670 | { |
| 671 | if (ip6_fib_timer.expires == 0 && |
| 672 | (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE))) |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 673 | mod_timer(&ip6_fib_timer, jiffies + |
| 674 | init_net.ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void fib6_force_start_gc(void) |
| 678 | { |
| 679 | if (ip6_fib_timer.expires == 0) |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 680 | mod_timer(&ip6_fib_timer, jiffies + |
| 681 | init_net.ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Add routing information to the routing tree. |
| 686 | * <destination addr>/<source addr> |
| 687 | * with source addr info in sub-trees |
| 688 | */ |
| 689 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 690 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 692 | struct fib6_node *fn, *pn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | int err = -ENOMEM; |
| 694 | |
| 695 | fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr), |
| 696 | rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst)); |
| 697 | |
| 698 | if (fn == NULL) |
| 699 | goto out; |
| 700 | |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 701 | pn = fn; |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | #ifdef CONFIG_IPV6_SUBTREES |
| 704 | if (rt->rt6i_src.plen) { |
| 705 | struct fib6_node *sn; |
| 706 | |
| 707 | if (fn->subtree == NULL) { |
| 708 | struct fib6_node *sfn; |
| 709 | |
| 710 | /* |
| 711 | * Create subtree. |
| 712 | * |
| 713 | * fn[main tree] |
| 714 | * | |
| 715 | * sfn[subtree root] |
| 716 | * \ |
| 717 | * sn[new leaf node] |
| 718 | */ |
| 719 | |
| 720 | /* Create subtree root node */ |
| 721 | sfn = node_alloc(); |
| 722 | if (sfn == NULL) |
| 723 | goto st_failure; |
| 724 | |
| 725 | sfn->leaf = &ip6_null_entry; |
| 726 | atomic_inc(&ip6_null_entry.rt6i_ref); |
| 727 | sfn->fn_flags = RTN_ROOT; |
| 728 | sfn->fn_sernum = fib6_new_sernum(); |
| 729 | |
| 730 | /* Now add the first leaf node to new subtree */ |
| 731 | |
| 732 | sn = fib6_add_1(sfn, &rt->rt6i_src.addr, |
| 733 | sizeof(struct in6_addr), rt->rt6i_src.plen, |
| 734 | offsetof(struct rt6_info, rt6i_src)); |
| 735 | |
| 736 | if (sn == NULL) { |
| 737 | /* If it is failed, discard just allocated |
| 738 | root, and then (in st_failure) stale node |
| 739 | in main tree. |
| 740 | */ |
| 741 | node_free(sfn); |
| 742 | goto st_failure; |
| 743 | } |
| 744 | |
| 745 | /* Now link new subtree to main tree */ |
| 746 | sfn->parent = fn; |
| 747 | fn->subtree = sfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } else { |
| 749 | sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr, |
| 750 | sizeof(struct in6_addr), rt->rt6i_src.plen, |
| 751 | offsetof(struct rt6_info, rt6i_src)); |
| 752 | |
| 753 | if (sn == NULL) |
| 754 | goto st_failure; |
| 755 | } |
| 756 | |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 757 | if (fn->leaf == NULL) { |
| 758 | fn->leaf = rt; |
| 759 | atomic_inc(&rt->rt6i_ref); |
| 760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | fn = sn; |
| 762 | } |
| 763 | #endif |
| 764 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 765 | err = fib6_add_rt2node(fn, rt, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | if (err == 0) { |
| 768 | fib6_start_gc(rt); |
| 769 | if (!(rt->rt6i_flags&RTF_CACHE)) |
YOSHIFUJI Hideaki | 2285adc | 2006-08-23 17:20:54 -0700 | [diff] [blame] | 770 | fib6_prune_clones(pn, rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | out: |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 774 | if (err) { |
| 775 | #ifdef CONFIG_IPV6_SUBTREES |
| 776 | /* |
| 777 | * If fib6_add_1 has cleared the old leaf pointer in the |
| 778 | * super-tree leaf node we have to find a new one for it. |
| 779 | */ |
| 780 | if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) { |
| 781 | pn->leaf = fib6_find_prefix(pn); |
| 782 | #if RT6_DEBUG >= 2 |
| 783 | if (!pn->leaf) { |
| 784 | BUG_TRAP(pn->leaf != NULL); |
| 785 | pn->leaf = &ip6_null_entry; |
| 786 | } |
| 787 | #endif |
| 788 | atomic_inc(&pn->leaf->rt6i_ref); |
| 789 | } |
| 790 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | dst_free(&rt->u.dst); |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return err; |
| 794 | |
| 795 | #ifdef CONFIG_IPV6_SUBTREES |
| 796 | /* Subtree creation failed, probably main tree node |
| 797 | is orphan. If it is, shoot it. |
| 798 | */ |
| 799 | st_failure: |
| 800 | if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT))) |
| 801 | fib6_repair_tree(fn); |
| 802 | dst_free(&rt->u.dst); |
| 803 | return err; |
| 804 | #endif |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Routing tree lookup |
| 809 | * |
| 810 | */ |
| 811 | |
| 812 | struct lookup_args { |
| 813 | int offset; /* key offset on rt6_info */ |
| 814 | struct in6_addr *addr; /* search key */ |
| 815 | }; |
| 816 | |
| 817 | static struct fib6_node * fib6_lookup_1(struct fib6_node *root, |
| 818 | struct lookup_args *args) |
| 819 | { |
| 820 | struct fib6_node *fn; |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 821 | __be32 dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 823 | if (unlikely(args->offset == 0)) |
| 824 | return NULL; |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | /* |
| 827 | * Descend on a tree |
| 828 | */ |
| 829 | |
| 830 | fn = root; |
| 831 | |
| 832 | for (;;) { |
| 833 | struct fib6_node *next; |
| 834 | |
| 835 | dir = addr_bit_set(args->addr, fn->fn_bit); |
| 836 | |
| 837 | next = dir ? fn->right : fn->left; |
| 838 | |
| 839 | if (next) { |
| 840 | fn = next; |
| 841 | continue; |
| 842 | } |
| 843 | |
| 844 | break; |
| 845 | } |
| 846 | |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 847 | while(fn) { |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 848 | if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | struct rt6key *key; |
| 850 | |
| 851 | key = (struct rt6key *) ((u8 *) fn->leaf + |
| 852 | args->offset); |
| 853 | |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 854 | if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) { |
| 855 | #ifdef CONFIG_IPV6_SUBTREES |
| 856 | if (fn->subtree) |
| 857 | fn = fib6_lookup_1(fn->subtree, args + 1); |
| 858 | #endif |
| 859 | if (!fn || fn->fn_flags & RTN_RTINFO) |
| 860 | return fn; |
| 861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 864 | if (fn->fn_flags & RTN_ROOT) |
| 865 | break; |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | fn = fn->parent; |
| 868 | } |
| 869 | |
| 870 | return NULL; |
| 871 | } |
| 872 | |
| 873 | struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr, |
| 874 | struct in6_addr *saddr) |
| 875 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | struct fib6_node *fn; |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 877 | struct lookup_args args[] = { |
| 878 | { |
| 879 | .offset = offsetof(struct rt6_info, rt6i_dst), |
| 880 | .addr = daddr, |
| 881 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 883 | { |
| 884 | .offset = offsetof(struct rt6_info, rt6i_src), |
| 885 | .addr = saddr, |
| 886 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | #endif |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 888 | { |
| 889 | .offset = 0, /* sentinel */ |
| 890 | } |
| 891 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
YOSHIFUJI Hideaki | fefc2a6 | 2006-08-23 17:21:50 -0700 | [diff] [blame] | 893 | fn = fib6_lookup_1(root, daddr ? args : args + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | if (fn == NULL || fn->fn_flags & RTN_TL_ROOT) |
| 896 | fn = root; |
| 897 | |
| 898 | return fn; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Get node with specified destination prefix (and source prefix, |
| 903 | * if subtrees are used) |
| 904 | */ |
| 905 | |
| 906 | |
| 907 | static struct fib6_node * fib6_locate_1(struct fib6_node *root, |
| 908 | struct in6_addr *addr, |
| 909 | int plen, int offset) |
| 910 | { |
| 911 | struct fib6_node *fn; |
| 912 | |
| 913 | for (fn = root; fn ; ) { |
| 914 | struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset); |
| 915 | |
| 916 | /* |
| 917 | * Prefix match |
| 918 | */ |
| 919 | if (plen < fn->fn_bit || |
| 920 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) |
| 921 | return NULL; |
| 922 | |
| 923 | if (plen == fn->fn_bit) |
| 924 | return fn; |
| 925 | |
| 926 | /* |
| 927 | * We have more bits to go |
| 928 | */ |
| 929 | if (addr_bit_set(addr, fn->fn_bit)) |
| 930 | fn = fn->right; |
| 931 | else |
| 932 | fn = fn->left; |
| 933 | } |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | struct fib6_node * fib6_locate(struct fib6_node *root, |
| 938 | struct in6_addr *daddr, int dst_len, |
| 939 | struct in6_addr *saddr, int src_len) |
| 940 | { |
| 941 | struct fib6_node *fn; |
| 942 | |
| 943 | fn = fib6_locate_1(root, daddr, dst_len, |
| 944 | offsetof(struct rt6_info, rt6i_dst)); |
| 945 | |
| 946 | #ifdef CONFIG_IPV6_SUBTREES |
| 947 | if (src_len) { |
| 948 | BUG_TRAP(saddr!=NULL); |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 949 | if (fn && fn->subtree) |
| 950 | fn = fib6_locate_1(fn->subtree, saddr, src_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | offsetof(struct rt6_info, rt6i_src)); |
| 952 | } |
| 953 | #endif |
| 954 | |
| 955 | if (fn && fn->fn_flags&RTN_RTINFO) |
| 956 | return fn; |
| 957 | |
| 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | |
| 962 | /* |
| 963 | * Deletion |
| 964 | * |
| 965 | */ |
| 966 | |
| 967 | static struct rt6_info * fib6_find_prefix(struct fib6_node *fn) |
| 968 | { |
| 969 | if (fn->fn_flags&RTN_ROOT) |
| 970 | return &ip6_null_entry; |
| 971 | |
| 972 | while(fn) { |
| 973 | if(fn->left) |
| 974 | return fn->left->leaf; |
| 975 | |
| 976 | if(fn->right) |
| 977 | return fn->right->leaf; |
| 978 | |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 979 | fn = FIB6_SUBTREE(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | return NULL; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Called to trim the tree of intermediate nodes when possible. "fn" |
| 986 | * is the node we want to try and remove. |
| 987 | */ |
| 988 | |
| 989 | static struct fib6_node * fib6_repair_tree(struct fib6_node *fn) |
| 990 | { |
| 991 | int children; |
| 992 | int nstate; |
| 993 | struct fib6_node *child, *pn; |
| 994 | struct fib6_walker_t *w; |
| 995 | int iter = 0; |
| 996 | |
| 997 | for (;;) { |
| 998 | RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter); |
| 999 | iter++; |
| 1000 | |
| 1001 | BUG_TRAP(!(fn->fn_flags&RTN_RTINFO)); |
| 1002 | BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT)); |
| 1003 | BUG_TRAP(fn->leaf==NULL); |
| 1004 | |
| 1005 | children = 0; |
| 1006 | child = NULL; |
| 1007 | if (fn->right) child = fn->right, children |= 1; |
| 1008 | if (fn->left) child = fn->left, children |= 2; |
| 1009 | |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1010 | if (children == 3 || FIB6_SUBTREE(fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | #ifdef CONFIG_IPV6_SUBTREES |
| 1012 | /* Subtree root (i.e. fn) may have one child */ |
| 1013 | || (children && fn->fn_flags&RTN_ROOT) |
| 1014 | #endif |
| 1015 | ) { |
| 1016 | fn->leaf = fib6_find_prefix(fn); |
| 1017 | #if RT6_DEBUG >= 2 |
| 1018 | if (fn->leaf==NULL) { |
| 1019 | BUG_TRAP(fn->leaf); |
| 1020 | fn->leaf = &ip6_null_entry; |
| 1021 | } |
| 1022 | #endif |
| 1023 | atomic_inc(&fn->leaf->rt6i_ref); |
| 1024 | return fn->parent; |
| 1025 | } |
| 1026 | |
| 1027 | pn = fn->parent; |
| 1028 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1029 | if (FIB6_SUBTREE(pn) == fn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | BUG_TRAP(fn->fn_flags&RTN_ROOT); |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1031 | FIB6_SUBTREE(pn) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | nstate = FWS_L; |
| 1033 | } else { |
| 1034 | BUG_TRAP(!(fn->fn_flags&RTN_ROOT)); |
| 1035 | #endif |
| 1036 | if (pn->right == fn) pn->right = child; |
| 1037 | else if (pn->left == fn) pn->left = child; |
| 1038 | #if RT6_DEBUG >= 2 |
| 1039 | else BUG_TRAP(0); |
| 1040 | #endif |
| 1041 | if (child) |
| 1042 | child->parent = pn; |
| 1043 | nstate = FWS_R; |
| 1044 | #ifdef CONFIG_IPV6_SUBTREES |
| 1045 | } |
| 1046 | #endif |
| 1047 | |
| 1048 | read_lock(&fib6_walker_lock); |
| 1049 | FOR_WALKERS(w) { |
| 1050 | if (child == NULL) { |
| 1051 | if (w->root == fn) { |
| 1052 | w->root = w->node = NULL; |
| 1053 | RT6_TRACE("W %p adjusted by delroot 1\n", w); |
| 1054 | } else if (w->node == fn) { |
| 1055 | RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); |
| 1056 | w->node = pn; |
| 1057 | w->state = nstate; |
| 1058 | } |
| 1059 | } else { |
| 1060 | if (w->root == fn) { |
| 1061 | w->root = child; |
| 1062 | RT6_TRACE("W %p adjusted by delroot 2\n", w); |
| 1063 | } |
| 1064 | if (w->node == fn) { |
| 1065 | w->node = child; |
| 1066 | if (children&2) { |
| 1067 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); |
| 1068 | w->state = w->state>=FWS_R ? FWS_U : FWS_INIT; |
| 1069 | } else { |
| 1070 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); |
| 1071 | w->state = w->state>=FWS_C ? FWS_U : FWS_INIT; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | read_unlock(&fib6_walker_lock); |
| 1077 | |
| 1078 | node_free(fn); |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1079 | if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | return pn; |
| 1081 | |
| 1082 | rt6_release(pn->leaf); |
| 1083 | pn->leaf = NULL; |
| 1084 | fn = pn; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1089 | struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
| 1091 | struct fib6_walker_t *w; |
| 1092 | struct rt6_info *rt = *rtp; |
| 1093 | |
| 1094 | RT6_TRACE("fib6_del_route\n"); |
| 1095 | |
| 1096 | /* Unlink it */ |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 1097 | *rtp = rt->u.dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | rt->rt6i_node = NULL; |
| 1099 | rt6_stats.fib_rt_entries--; |
| 1100 | rt6_stats.fib_discarded_routes++; |
| 1101 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 1102 | /* Reset round-robin state, if necessary */ |
| 1103 | if (fn->rr_ptr == rt) |
| 1104 | fn->rr_ptr = NULL; |
| 1105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | /* Adjust walkers */ |
| 1107 | read_lock(&fib6_walker_lock); |
| 1108 | FOR_WALKERS(w) { |
| 1109 | if (w->state == FWS_C && w->leaf == rt) { |
| 1110 | RT6_TRACE("walker %p adjusted by delroute\n", w); |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 1111 | w->leaf = rt->u.dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | if (w->leaf == NULL) |
| 1113 | w->state = FWS_U; |
| 1114 | } |
| 1115 | } |
| 1116 | read_unlock(&fib6_walker_lock); |
| 1117 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 1118 | rt->u.dst.rt6_next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | /* If it was last route, expunge its radix tree node */ |
| 1121 | if (fn->leaf == NULL) { |
| 1122 | fn->fn_flags &= ~RTN_RTINFO; |
| 1123 | rt6_stats.fib_route_nodes--; |
| 1124 | fn = fib6_repair_tree(fn); |
| 1125 | } |
| 1126 | |
| 1127 | if (atomic_read(&rt->rt6i_ref) != 1) { |
| 1128 | /* This route is used as dummy address holder in some split |
| 1129 | * nodes. It is not leaked, but it still holds other resources, |
| 1130 | * which must be released in time. So, scan ascendant nodes |
| 1131 | * and replace dummy references to this route with references |
| 1132 | * to still alive ones. |
| 1133 | */ |
| 1134 | while (fn) { |
| 1135 | if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) { |
| 1136 | fn->leaf = fib6_find_prefix(fn); |
| 1137 | atomic_inc(&fn->leaf->rt6i_ref); |
| 1138 | rt6_release(rt); |
| 1139 | } |
| 1140 | fn = fn->parent; |
| 1141 | } |
| 1142 | /* No more references are possible at this point. */ |
Pavel Emelyanov | 2df96af | 2008-02-18 20:50:42 -0800 | [diff] [blame] | 1143 | BUG_ON(atomic_read(&rt->rt6i_ref) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1146 | inet6_rt_notify(RTM_DELROUTE, rt, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | rt6_release(rt); |
| 1148 | } |
| 1149 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1150 | int fib6_del(struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
| 1152 | struct fib6_node *fn = rt->rt6i_node; |
| 1153 | struct rt6_info **rtp; |
| 1154 | |
| 1155 | #if RT6_DEBUG >= 2 |
| 1156 | if (rt->u.dst.obsolete>0) { |
| 1157 | BUG_TRAP(fn==NULL); |
| 1158 | return -ENOENT; |
| 1159 | } |
| 1160 | #endif |
| 1161 | if (fn == NULL || rt == &ip6_null_entry) |
| 1162 | return -ENOENT; |
| 1163 | |
| 1164 | BUG_TRAP(fn->fn_flags&RTN_RTINFO); |
| 1165 | |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1166 | if (!(rt->rt6i_flags&RTF_CACHE)) { |
| 1167 | struct fib6_node *pn = fn; |
| 1168 | #ifdef CONFIG_IPV6_SUBTREES |
| 1169 | /* clones of this route might be in another subtree */ |
| 1170 | if (rt->rt6i_src.plen) { |
| 1171 | while (!(pn->fn_flags&RTN_ROOT)) |
| 1172 | pn = pn->parent; |
| 1173 | pn = pn->parent; |
| 1174 | } |
| 1175 | #endif |
| 1176 | fib6_prune_clones(pn, rt); |
| 1177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
| 1179 | /* |
| 1180 | * Walk the leaf entries looking for ourself |
| 1181 | */ |
| 1182 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 1183 | for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | if (*rtp == rt) { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1185 | fib6_del_route(fn, rtp, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | } |
| 1189 | return -ENOENT; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Tree traversal function. |
| 1194 | * |
| 1195 | * Certainly, it is not interrupt safe. |
| 1196 | * However, it is internally reenterable wrt itself and fib6_add/fib6_del. |
| 1197 | * It means, that we can modify tree during walking |
| 1198 | * and use this function for garbage collection, clone pruning, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1199 | * cleaning tree when a device goes down etc. etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | * |
| 1201 | * It guarantees that every node will be traversed, |
| 1202 | * and that it will be traversed only once. |
| 1203 | * |
| 1204 | * Callback function w->func may return: |
| 1205 | * 0 -> continue walking. |
| 1206 | * positive value -> walking is suspended (used by tree dumps, |
| 1207 | * and probably by gc, if it will be split to several slices) |
| 1208 | * negative value -> terminate walking. |
| 1209 | * |
| 1210 | * The function itself returns: |
| 1211 | * 0 -> walk is complete. |
| 1212 | * >0 -> walk is incomplete (i.e. suspended) |
| 1213 | * <0 -> walk is terminated by an error. |
| 1214 | */ |
| 1215 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 1216 | static int fib6_walk_continue(struct fib6_walker_t *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | { |
| 1218 | struct fib6_node *fn, *pn; |
| 1219 | |
| 1220 | for (;;) { |
| 1221 | fn = w->node; |
| 1222 | if (fn == NULL) |
| 1223 | return 0; |
| 1224 | |
| 1225 | if (w->prune && fn != w->root && |
| 1226 | fn->fn_flags&RTN_RTINFO && w->state < FWS_C) { |
| 1227 | w->state = FWS_C; |
| 1228 | w->leaf = fn->leaf; |
| 1229 | } |
| 1230 | switch (w->state) { |
| 1231 | #ifdef CONFIG_IPV6_SUBTREES |
| 1232 | case FWS_S: |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1233 | if (FIB6_SUBTREE(fn)) { |
| 1234 | w->node = FIB6_SUBTREE(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | continue; |
| 1236 | } |
| 1237 | w->state = FWS_L; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1238 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | case FWS_L: |
| 1240 | if (fn->left) { |
| 1241 | w->node = fn->left; |
| 1242 | w->state = FWS_INIT; |
| 1243 | continue; |
| 1244 | } |
| 1245 | w->state = FWS_R; |
| 1246 | case FWS_R: |
| 1247 | if (fn->right) { |
| 1248 | w->node = fn->right; |
| 1249 | w->state = FWS_INIT; |
| 1250 | continue; |
| 1251 | } |
| 1252 | w->state = FWS_C; |
| 1253 | w->leaf = fn->leaf; |
| 1254 | case FWS_C: |
| 1255 | if (w->leaf && fn->fn_flags&RTN_RTINFO) { |
| 1256 | int err = w->func(w); |
| 1257 | if (err) |
| 1258 | return err; |
| 1259 | continue; |
| 1260 | } |
| 1261 | w->state = FWS_U; |
| 1262 | case FWS_U: |
| 1263 | if (fn == w->root) |
| 1264 | return 0; |
| 1265 | pn = fn->parent; |
| 1266 | w->node = pn; |
| 1267 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1268 | if (FIB6_SUBTREE(pn) == fn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | BUG_TRAP(fn->fn_flags&RTN_ROOT); |
| 1270 | w->state = FWS_L; |
| 1271 | continue; |
| 1272 | } |
| 1273 | #endif |
| 1274 | if (pn->left == fn) { |
| 1275 | w->state = FWS_R; |
| 1276 | continue; |
| 1277 | } |
| 1278 | if (pn->right == fn) { |
| 1279 | w->state = FWS_C; |
| 1280 | w->leaf = w->node->leaf; |
| 1281 | continue; |
| 1282 | } |
| 1283 | #if RT6_DEBUG >= 2 |
| 1284 | BUG_TRAP(0); |
| 1285 | #endif |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 1290 | static int fib6_walk(struct fib6_walker_t *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | { |
| 1292 | int res; |
| 1293 | |
| 1294 | w->state = FWS_INIT; |
| 1295 | w->node = w->root; |
| 1296 | |
| 1297 | fib6_walker_link(w); |
| 1298 | res = fib6_walk_continue(w); |
| 1299 | if (res <= 0) |
| 1300 | fib6_walker_unlink(w); |
| 1301 | return res; |
| 1302 | } |
| 1303 | |
| 1304 | static int fib6_clean_node(struct fib6_walker_t *w) |
| 1305 | { |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1306 | struct nl_info info = { |
| 1307 | .nl_net = &init_net, |
| 1308 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | int res; |
| 1310 | struct rt6_info *rt; |
Benjamin Thery | 0a8891a | 2007-10-08 20:39:36 -0700 | [diff] [blame] | 1311 | struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
Eric Dumazet | 7cc4826 | 2007-02-09 16:22:57 -0800 | [diff] [blame] | 1313 | for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | res = c->func(rt, c->arg); |
| 1315 | if (res < 0) { |
| 1316 | w->leaf = rt; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 1317 | res = fib6_del(rt, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | if (res) { |
| 1319 | #if RT6_DEBUG >= 2 |
| 1320 | printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res); |
| 1321 | #endif |
| 1322 | continue; |
| 1323 | } |
| 1324 | return 0; |
| 1325 | } |
| 1326 | BUG_TRAP(res==0); |
| 1327 | } |
| 1328 | w->leaf = rt; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * Convenient frontend to tree walker. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1334 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | * func is called on each route. |
| 1336 | * It may return -1 -> delete this route. |
| 1337 | * 0 -> continue walking |
| 1338 | * |
| 1339 | * prune==1 -> only immediate children of node (certainly, |
| 1340 | * ignoring pure split nodes) will be scanned. |
| 1341 | */ |
| 1342 | |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 1343 | static void fib6_clean_tree(struct fib6_node *root, |
| 1344 | int (*func)(struct rt6_info *, void *arg), |
| 1345 | int prune, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | { |
| 1347 | struct fib6_cleaner_t c; |
| 1348 | |
| 1349 | c.w.root = root; |
| 1350 | c.w.func = fib6_clean_node; |
| 1351 | c.w.prune = prune; |
| 1352 | c.func = func; |
| 1353 | c.arg = arg; |
| 1354 | |
| 1355 | fib6_walk(&c.w); |
| 1356 | } |
| 1357 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1358 | void fib6_clean_all(int (*func)(struct rt6_info *, void *arg), |
| 1359 | int prune, void *arg) |
| 1360 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1361 | struct fib6_table *table; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1362 | struct hlist_node *node; |
| 1363 | unsigned int h; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1364 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1365 | rcu_read_lock(); |
| 1366 | for (h = 0; h < FIB_TABLE_HASHSZ; h++) { |
| 1367 | hlist_for_each_entry_rcu(table, node, &fib_table_hash[h], |
| 1368 | tb6_hlist) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1369 | write_lock_bh(&table->tb6_lock); |
| 1370 | fib6_clean_tree(&table->tb6_root, func, prune, arg); |
| 1371 | write_unlock_bh(&table->tb6_lock); |
| 1372 | } |
| 1373 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1374 | rcu_read_unlock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | static int fib6_prune_clone(struct rt6_info *rt, void *arg) |
| 1378 | { |
| 1379 | if (rt->rt6i_flags & RTF_CACHE) { |
| 1380 | RT6_TRACE("pruning clone %p\n", rt); |
| 1381 | return -1; |
| 1382 | } |
| 1383 | |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt) |
| 1388 | { |
| 1389 | fib6_clean_tree(fn, fib6_prune_clone, 1, rt); |
| 1390 | } |
| 1391 | |
| 1392 | /* |
| 1393 | * Garbage collection |
| 1394 | */ |
| 1395 | |
| 1396 | static struct fib6_gc_args |
| 1397 | { |
| 1398 | int timeout; |
| 1399 | int more; |
| 1400 | } gc_args; |
| 1401 | |
| 1402 | static int fib6_age(struct rt6_info *rt, void *arg) |
| 1403 | { |
| 1404 | unsigned long now = jiffies; |
| 1405 | |
| 1406 | /* |
| 1407 | * check addrconf expiration here. |
| 1408 | * Routes are expired even if they are in use. |
| 1409 | * |
| 1410 | * Also age clones. Note, that clones are aged out |
| 1411 | * only if they are not in use now. |
| 1412 | */ |
| 1413 | |
| 1414 | if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) { |
| 1415 | if (time_after(now, rt->rt6i_expires)) { |
| 1416 | RT6_TRACE("expiring %p\n", rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return -1; |
| 1418 | } |
| 1419 | gc_args.more++; |
| 1420 | } else if (rt->rt6i_flags & RTF_CACHE) { |
| 1421 | if (atomic_read(&rt->u.dst.__refcnt) == 0 && |
| 1422 | time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) { |
| 1423 | RT6_TRACE("aging clone %p\n", rt); |
| 1424 | return -1; |
| 1425 | } else if ((rt->rt6i_flags & RTF_GATEWAY) && |
| 1426 | (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) { |
| 1427 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 1428 | rt); |
| 1429 | return -1; |
| 1430 | } |
| 1431 | gc_args.more++; |
| 1432 | } |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | static DEFINE_SPINLOCK(fib6_gc_lock); |
| 1438 | |
| 1439 | void fib6_run_gc(unsigned long dummy) |
| 1440 | { |
| 1441 | if (dummy != ~0UL) { |
| 1442 | spin_lock_bh(&fib6_gc_lock); |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 1443 | gc_args.timeout = dummy ? (int)dummy : |
| 1444 | init_net.ipv6.sysctl.ip6_rt_gc_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | } else { |
| 1446 | local_bh_disable(); |
| 1447 | if (!spin_trylock(&fib6_gc_lock)) { |
| 1448 | mod_timer(&ip6_fib_timer, jiffies + HZ); |
| 1449 | local_bh_enable(); |
| 1450 | return; |
| 1451 | } |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 1452 | gc_args.timeout = init_net.ipv6.sysctl.ip6_rt_gc_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | } |
| 1454 | gc_args.more = 0; |
| 1455 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1456 | icmp6_dst_gc(&gc_args.more); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1457 | fib6_clean_all(fib6_age, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | if (gc_args.more) |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 1460 | mod_timer(&ip6_fib_timer, jiffies + |
| 1461 | init_net.ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | else { |
| 1463 | del_timer(&ip6_fib_timer); |
| 1464 | ip6_fib_timer.expires = 0; |
| 1465 | } |
| 1466 | spin_unlock_bh(&fib6_gc_lock); |
| 1467 | } |
| 1468 | |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1469 | int __init fib6_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 1471 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | fib6_node_kmem = kmem_cache_create("fib6_nodes", |
| 1473 | sizeof(struct fib6_node), |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1474 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1475 | NULL); |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1476 | if (!fib6_node_kmem) |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 1477 | goto out; |
| 1478 | |
| 1479 | fib_table_hash = kzalloc(sizeof(*fib_table_hash)*FIB_TABLE_HASHSZ, |
| 1480 | GFP_KERNEL); |
| 1481 | if (!fib_table_hash) |
| 1482 | goto out_kmem_cache_create; |
| 1483 | |
| 1484 | fib6_main_tbl = kzalloc(sizeof(*fib6_main_tbl), GFP_KERNEL); |
| 1485 | if (!fib6_main_tbl) |
| 1486 | goto out_fib_table_hash; |
| 1487 | |
| 1488 | fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; |
| 1489 | fib6_main_tbl->tb6_root.leaf = &ip6_null_entry; |
| 1490 | fib6_main_tbl->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
| 1491 | |
| 1492 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1493 | fib6_local_tbl = kzalloc(sizeof(*fib6_local_tbl), GFP_KERNEL); |
| 1494 | if (!fib6_local_tbl) |
| 1495 | goto out_fib6_main_tbl; |
| 1496 | |
| 1497 | fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; |
| 1498 | fib6_local_tbl->tb6_root.leaf = &ip6_null_entry; |
| 1499 | fib6_local_tbl->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
| 1500 | #endif |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1501 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1502 | fib6_tables_init(); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 1503 | |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1504 | ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib); |
| 1505 | if (ret) |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 1506 | goto out_fib6_local_tbl; |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1507 | out: |
| 1508 | return ret; |
| 1509 | |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 1510 | out_fib6_local_tbl: |
| 1511 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1512 | kfree(fib6_local_tbl); |
| 1513 | out_fib6_main_tbl: |
| 1514 | #endif |
| 1515 | kfree(fib6_main_tbl); |
| 1516 | out_fib_table_hash: |
| 1517 | kfree(fib_table_hash); |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1518 | out_kmem_cache_create: |
| 1519 | kmem_cache_destroy(fib6_node_kmem); |
| 1520 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | void fib6_gc_cleanup(void) |
| 1524 | { |
| 1525 | del_timer(&ip6_fib_timer); |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame^] | 1526 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1527 | kfree(fib6_local_tbl); |
| 1528 | #endif |
| 1529 | kfree(fib6_main_tbl); |
| 1530 | kfree(fib_table_hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | kmem_cache_destroy(fib6_node_kmem); |
| 1532 | } |