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