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. |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 12 | * |
| 13 | * Changes: |
| 14 | * Yuji SEKIYA @USAGI: Support default route on router node; |
| 15 | * remove ip6_null_entry from the top of |
| 16 | * routing table. |
| 17 | * Ville Nuorvala: Fixed routing subtrees. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 19 | |
| 20 | #define pr_fmt(fmt) "IPv6: " fmt |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/errno.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/route.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/in6.h> |
| 28 | #include <linux/init.h> |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 29 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/ipv6.h> |
| 33 | #include <net/ndisc.h> |
| 34 | #include <net/addrconf.h> |
| 35 | |
| 36 | #include <net/ip6_fib.h> |
| 37 | #include <net/ip6_route.h> |
| 38 | |
| 39 | #define RT6_DEBUG 2 |
| 40 | |
| 41 | #if RT6_DEBUG >= 3 |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 42 | #define RT6_TRACE(x...) pr_debug(x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #else |
| 44 | #define RT6_TRACE(x...) do { ; } while (0) |
| 45 | #endif |
| 46 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 47 | static struct kmem_cache * fib6_node_kmem __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | enum fib_walk_state_t |
| 50 | { |
| 51 | #ifdef CONFIG_IPV6_SUBTREES |
| 52 | FWS_S, |
| 53 | #endif |
| 54 | FWS_L, |
| 55 | FWS_R, |
| 56 | FWS_C, |
| 57 | FWS_U |
| 58 | }; |
| 59 | |
| 60 | struct fib6_cleaner_t |
| 61 | { |
| 62 | struct fib6_walker_t w; |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 63 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | int (*func)(struct rt6_info *, void *arg); |
| 65 | void *arg; |
| 66 | }; |
| 67 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 68 | static DEFINE_RWLOCK(fib6_walker_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | #ifdef CONFIG_IPV6_SUBTREES |
| 71 | #define FWS_INIT FWS_S |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #else |
| 73 | #define FWS_INIT FWS_L |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 76 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn, |
| 77 | struct rt6_info *rt); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 78 | static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn); |
| 79 | 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] | 80 | static int fib6_walk(struct fib6_walker_t *w); |
| 81 | static int fib6_walk_continue(struct fib6_walker_t *w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * A routing update causes an increase of the serial number on the |
| 85 | * affected subtree. This allows for cached routes to be asynchronously |
| 86 | * tested when modifications are made to the destination cache as a |
| 87 | * result of redirects, path MTU changes, etc. |
| 88 | */ |
| 89 | |
| 90 | static __u32 rt_sernum; |
| 91 | |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 92 | static void fib6_gc_timer_cb(unsigned long arg); |
| 93 | |
Alexey Dobriyan | bbef49d | 2010-02-18 08:13:30 +0000 | [diff] [blame] | 94 | static LIST_HEAD(fib6_walkers); |
| 95 | #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 97 | static inline void fib6_walker_link(struct fib6_walker_t *w) |
| 98 | { |
| 99 | write_lock_bh(&fib6_walker_lock); |
Alexey Dobriyan | bbef49d | 2010-02-18 08:13:30 +0000 | [diff] [blame] | 100 | list_add(&w->lh, &fib6_walkers); |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 101 | write_unlock_bh(&fib6_walker_lock); |
| 102 | } |
| 103 | |
| 104 | static inline void fib6_walker_unlink(struct fib6_walker_t *w) |
| 105 | { |
| 106 | write_lock_bh(&fib6_walker_lock); |
Alexey Dobriyan | bbef49d | 2010-02-18 08:13:30 +0000 | [diff] [blame] | 107 | list_del(&w->lh); |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 108 | write_unlock_bh(&fib6_walker_lock); |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static __inline__ u32 fib6_new_sernum(void) |
| 111 | { |
| 112 | u32 n = ++rt_sernum; |
| 113 | if ((__s32)n <= 0) |
| 114 | rt_sernum = n = 1; |
| 115 | return n; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Auxiliary address test functions for the radix tree. |
| 120 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 121 | * These assume a 32bit processor (although it will work on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * 64bit processors) |
| 123 | */ |
| 124 | |
| 125 | /* |
| 126 | * test bit |
| 127 | */ |
YOSHIFUJI Hideaki / 吉藤英明 | 02cdce5 | 2010-03-27 01:24:16 +0000 | [diff] [blame] | 128 | #if defined(__LITTLE_ENDIAN) |
| 129 | # define BITOP_BE32_SWIZZLE (0x1F & ~7) |
| 130 | #else |
| 131 | # define BITOP_BE32_SWIZZLE 0 |
| 132 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 134 | static __inline__ __be32 addr_bit_set(const void *token, int fn_bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 136 | const __be32 *addr = token; |
YOSHIFUJI Hideaki / 吉藤英明 | 02cdce5 | 2010-03-27 01:24:16 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Here, |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 139 | * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f) |
YOSHIFUJI Hideaki / 吉藤英明 | 02cdce5 | 2010-03-27 01:24:16 +0000 | [diff] [blame] | 140 | * is optimized version of |
| 141 | * htonl(1 << ((~fn_bit)&0x1F)) |
| 142 | * See include/asm-generic/bitops/le.h. |
| 143 | */ |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 144 | return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) & |
| 145 | addr[fn_bit >> 5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | static __inline__ struct fib6_node * node_alloc(void) |
| 149 | { |
| 150 | struct fib6_node *fn; |
| 151 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 152 | fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | return fn; |
| 155 | } |
| 156 | |
| 157 | static __inline__ void node_free(struct fib6_node * fn) |
| 158 | { |
| 159 | kmem_cache_free(fib6_node_kmem, fn); |
| 160 | } |
| 161 | |
| 162 | static __inline__ void rt6_release(struct rt6_info *rt) |
| 163 | { |
| 164 | if (atomic_dec_and_test(&rt->rt6i_ref)) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 165 | dst_free(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 168 | static void fib6_link_table(struct net *net, struct fib6_table *tb) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 169 | { |
| 170 | unsigned int h; |
| 171 | |
Thomas Graf | 375216a | 2006-10-21 20:20:54 -0700 | [diff] [blame] | 172 | /* |
| 173 | * Initialize table lock at a single place to give lockdep a key, |
| 174 | * tables aren't visible prior to being linked to the list. |
| 175 | */ |
| 176 | rwlock_init(&tb->tb6_lock); |
| 177 | |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 178 | h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * No protection necessary, this is the only list mutatation |
| 182 | * operation, tables never disappear once they exist. |
| 183 | */ |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 184 | 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] | 185 | } |
| 186 | |
| 187 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame] | 188 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 189 | static struct fib6_table *fib6_alloc_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 190 | { |
| 191 | struct fib6_table *table; |
| 192 | |
| 193 | table = kzalloc(sizeof(*table), GFP_ATOMIC); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 194 | if (table) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 195 | table->tb6_id = id; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 196 | table->tb6_root.leaf = net->ipv6.ip6_null_entry; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 197 | table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 198 | inet_peer_base_init(&table->tb6_peers); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return table; |
| 202 | } |
| 203 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 204 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 205 | { |
| 206 | struct fib6_table *tb; |
| 207 | |
| 208 | if (id == 0) |
| 209 | id = RT6_TABLE_MAIN; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 210 | tb = fib6_get_table(net, id); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 211 | if (tb) |
| 212 | return tb; |
| 213 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 214 | tb = fib6_alloc_table(net, id); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 215 | if (tb) |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 216 | fib6_link_table(net, tb); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 217 | |
| 218 | return tb; |
| 219 | } |
| 220 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 221 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 222 | { |
| 223 | struct fib6_table *tb; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 224 | struct hlist_head *head; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 225 | unsigned int h; |
| 226 | |
| 227 | if (id == 0) |
| 228 | id = RT6_TABLE_MAIN; |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 229 | h = id & (FIB6_TABLE_HASHSZ - 1); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 230 | rcu_read_lock(); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 231 | head = &net->ipv6.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 232 | hlist_for_each_entry_rcu(tb, head, tb6_hlist) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 233 | if (tb->tb6_id == id) { |
| 234 | rcu_read_unlock(); |
| 235 | return tb; |
| 236 | } |
| 237 | } |
| 238 | rcu_read_unlock(); |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 243 | static void __net_init fib6_tables_init(struct net *net) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 244 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 245 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
| 246 | fib6_link_table(net, net->ipv6.fib6_local_tbl); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 247 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 248 | #else |
| 249 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 250 | struct fib6_table *fib6_new_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 251 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 252 | return fib6_get_table(net, id); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 255 | struct fib6_table *fib6_get_table(struct net *net, u32 id) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 256 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 257 | return net->ipv6.fib6_main_tbl; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 258 | } |
| 259 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 260 | struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 261 | int flags, pol_lookup_t lookup) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 262 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 263 | return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 266 | static void __net_init fib6_tables_init(struct net *net) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 267 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 268 | fib6_link_table(net, net->ipv6.fib6_main_tbl); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | #endif |
| 272 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 273 | static int fib6_dump_node(struct fib6_walker_t *w) |
| 274 | { |
| 275 | int res; |
| 276 | struct rt6_info *rt; |
| 277 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 278 | for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 279 | res = rt6_dump_route(rt, w->args); |
| 280 | if (res < 0) { |
| 281 | /* Frame is full, suspend walking */ |
| 282 | w->leaf = rt; |
| 283 | return 1; |
| 284 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 285 | WARN_ON(res == 0); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 286 | } |
| 287 | w->leaf = NULL; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static void fib6_dump_end(struct netlink_callback *cb) |
| 292 | { |
| 293 | struct fib6_walker_t *w = (void*)cb->args[2]; |
| 294 | |
| 295 | if (w) { |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 296 | if (cb->args[4]) { |
| 297 | cb->args[4] = 0; |
| 298 | fib6_walker_unlink(w); |
| 299 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 300 | cb->args[2] = 0; |
| 301 | kfree(w); |
| 302 | } |
| 303 | cb->done = (void*)cb->args[3]; |
| 304 | cb->args[1] = 3; |
| 305 | } |
| 306 | |
| 307 | static int fib6_dump_done(struct netlink_callback *cb) |
| 308 | { |
| 309 | fib6_dump_end(cb); |
| 310 | return cb->done ? cb->done(cb) : 0; |
| 311 | } |
| 312 | |
| 313 | static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb, |
| 314 | struct netlink_callback *cb) |
| 315 | { |
| 316 | struct fib6_walker_t *w; |
| 317 | int res; |
| 318 | |
| 319 | w = (void *)cb->args[2]; |
| 320 | w->root = &table->tb6_root; |
| 321 | |
| 322 | if (cb->args[4] == 0) { |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 323 | w->count = 0; |
| 324 | w->skip = 0; |
| 325 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 326 | read_lock_bh(&table->tb6_lock); |
| 327 | res = fib6_walk(w); |
| 328 | read_unlock_bh(&table->tb6_lock); |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 329 | if (res > 0) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 330 | cb->args[4] = 1; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 331 | cb->args[5] = w->root->fn_sernum; |
| 332 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 333 | } else { |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 334 | if (cb->args[5] != w->root->fn_sernum) { |
| 335 | /* Begin at the root if the tree changed */ |
| 336 | cb->args[5] = w->root->fn_sernum; |
| 337 | w->state = FWS_INIT; |
| 338 | w->node = w->root; |
| 339 | w->skip = w->count; |
| 340 | } else |
| 341 | w->skip = 0; |
| 342 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 343 | read_lock_bh(&table->tb6_lock); |
| 344 | res = fib6_walk_continue(w); |
| 345 | read_unlock_bh(&table->tb6_lock); |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 346 | if (res <= 0) { |
| 347 | fib6_walker_unlink(w); |
| 348 | cb->args[4] = 0; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 349 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 350 | } |
Herbert Xu | 7891cc8 | 2009-01-13 22:17:51 -0800 | [diff] [blame] | 351 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 352 | return res; |
| 353 | } |
| 354 | |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 355 | 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] | 356 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 357 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 358 | unsigned int h, s_h; |
| 359 | unsigned int e = 0, s_e; |
| 360 | struct rt6_rtnl_dump_arg arg; |
| 361 | struct fib6_walker_t *w; |
| 362 | struct fib6_table *tb; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 363 | struct hlist_head *head; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 364 | int res = 0; |
| 365 | |
| 366 | s_h = cb->args[0]; |
| 367 | s_e = cb->args[1]; |
| 368 | |
| 369 | w = (void *)cb->args[2]; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 370 | if (!w) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 371 | /* New dump: |
| 372 | * |
| 373 | * 1. hook callback destructor. |
| 374 | */ |
| 375 | cb->args[3] = (long)cb->done; |
| 376 | cb->done = fib6_dump_done; |
| 377 | |
| 378 | /* |
| 379 | * 2. allocate and initialize walker. |
| 380 | */ |
| 381 | w = kzalloc(sizeof(*w), GFP_ATOMIC); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 382 | if (!w) |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 383 | return -ENOMEM; |
| 384 | w->func = fib6_dump_node; |
| 385 | cb->args[2] = (long)w; |
| 386 | } |
| 387 | |
| 388 | arg.skb = skb; |
| 389 | arg.cb = cb; |
Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 390 | arg.net = net; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 391 | w->args = &arg; |
| 392 | |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 393 | rcu_read_lock(); |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 394 | for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 395 | e = 0; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 396 | head = &net->ipv6.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 397 | hlist_for_each_entry_rcu(tb, head, tb6_hlist) { |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 398 | if (e < s_e) |
| 399 | goto next; |
| 400 | res = fib6_dump_table(tb, skb, cb); |
| 401 | if (res != 0) |
| 402 | goto out; |
| 403 | next: |
| 404 | e++; |
| 405 | } |
| 406 | } |
| 407 | out: |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 408 | rcu_read_unlock(); |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 409 | cb->args[1] = e; |
| 410 | cb->args[0] = h; |
| 411 | |
| 412 | res = res < 0 ? res : skb->len; |
| 413 | if (res <= 0) |
| 414 | fib6_dump_end(cb); |
| 415 | return res; |
| 416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Routing Table |
| 420 | * |
| 421 | * return the appropriate node for a routing tree "add" operation |
| 422 | * by either creating and inserting or by returning an existing |
| 423 | * node. |
| 424 | */ |
| 425 | |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 426 | static struct fib6_node *fib6_add_1(struct fib6_node *root, |
| 427 | struct in6_addr *addr, int plen, |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 428 | int offset, int allow_create, |
| 429 | int replace_required) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
| 431 | struct fib6_node *fn, *in, *ln; |
| 432 | struct fib6_node *pn = NULL; |
| 433 | struct rt6key *key; |
| 434 | int bit; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 435 | __be32 dir = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | __u32 sernum = fib6_new_sernum(); |
| 437 | |
| 438 | RT6_TRACE("fib6_add_1\n"); |
| 439 | |
| 440 | /* insert node in tree */ |
| 441 | |
| 442 | fn = root; |
| 443 | |
| 444 | do { |
| 445 | key = (struct rt6key *)((u8 *)fn->leaf + offset); |
| 446 | |
| 447 | /* |
| 448 | * Prefix match |
| 449 | */ |
| 450 | if (plen < fn->fn_bit || |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 451 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) { |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 452 | if (!allow_create) { |
| 453 | if (replace_required) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 454 | pr_warn("Can't replace route, no match found\n"); |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 455 | return ERR_PTR(-ENOENT); |
| 456 | } |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 457 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | goto insert_above; |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 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 | /* |
| 463 | * Exact match ? |
| 464 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (plen == fn->fn_bit) { |
| 467 | /* clean up an intermediate node */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 468 | if (!(fn->fn_flags & RTN_RTINFO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | rt6_release(fn->leaf); |
| 470 | fn->leaf = NULL; |
| 471 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | fn->fn_sernum = sernum; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return fn; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * We have more bits to go |
| 480 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* Try to walk down on tree. */ |
| 483 | fn->fn_sernum = sernum; |
| 484 | dir = addr_bit_set(addr, fn->fn_bit); |
| 485 | pn = fn; |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 486 | fn = dir ? fn->right : fn->left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } while (fn); |
| 488 | |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 489 | if (!allow_create) { |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 490 | /* We should not create new node because |
| 491 | * NLM_F_REPLACE was specified without NLM_F_CREATE |
| 492 | * I assume it is safe to require NLM_F_CREATE when |
| 493 | * REPLACE flag is used! Later we may want to remove the |
| 494 | * check for replace_required, because according |
| 495 | * to netlink specification, NLM_F_CREATE |
| 496 | * MUST be specified if new route is created. |
| 497 | * That would keep IPv6 consistent with IPv4 |
| 498 | */ |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 499 | if (replace_required) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 500 | pr_warn("Can't replace route, no match found\n"); |
Matti Vaittinen | 14df015 | 2011-11-16 21:18:02 +0000 | [diff] [blame] | 501 | return ERR_PTR(-ENOENT); |
| 502 | } |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 503 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | /* |
| 506 | * We walked to the bottom of tree. |
| 507 | * Create new leaf node without children. |
| 508 | */ |
| 509 | |
| 510 | ln = node_alloc(); |
| 511 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 512 | if (!ln) |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 513 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | ln->fn_bit = plen; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | ln->parent = pn; |
| 517 | ln->fn_sernum = sernum; |
| 518 | |
| 519 | if (dir) |
| 520 | pn->right = ln; |
| 521 | else |
| 522 | pn->left = ln; |
| 523 | |
| 524 | return ln; |
| 525 | |
| 526 | |
| 527 | insert_above: |
| 528 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 529 | * split since we don't have a common prefix anymore or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | * we have a less significant route. |
| 531 | * we've to insert an intermediate node on the list |
| 532 | * this new node will point to the one we need to create |
| 533 | * and the current |
| 534 | */ |
| 535 | |
| 536 | pn = fn->parent; |
| 537 | |
| 538 | /* find 1st bit in difference between the 2 addrs. |
| 539 | |
YOSHIFUJI Hideaki | 971f359 | 2005-11-08 09:37:56 -0800 | [diff] [blame] | 540 | See comment in __ipv6_addr_diff: bit may be an invalid value, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | but if it is >= plen, the value is ignored in any case. |
| 542 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 543 | |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 544 | bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 546 | /* |
| 547 | * (intermediate)[in] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | * / \ |
| 549 | * (new leaf node)[ln] (old node)[fn] |
| 550 | */ |
| 551 | if (plen > bit) { |
| 552 | in = node_alloc(); |
| 553 | ln = node_alloc(); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 554 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 555 | if (!in || !ln) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (in) |
| 557 | node_free(in); |
| 558 | if (ln) |
| 559 | node_free(ln); |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 560 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 563 | /* |
| 564 | * new intermediate node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | * RTN_RTINFO will |
| 566 | * be off since that an address that chooses one of |
| 567 | * the branches would not match less specific routes |
| 568 | * in the other branch |
| 569 | */ |
| 570 | |
| 571 | in->fn_bit = bit; |
| 572 | |
| 573 | in->parent = pn; |
| 574 | in->leaf = fn->leaf; |
| 575 | atomic_inc(&in->leaf->rt6i_ref); |
| 576 | |
| 577 | in->fn_sernum = sernum; |
| 578 | |
| 579 | /* update parent pointer */ |
| 580 | if (dir) |
| 581 | pn->right = in; |
| 582 | else |
| 583 | pn->left = in; |
| 584 | |
| 585 | ln->fn_bit = plen; |
| 586 | |
| 587 | ln->parent = in; |
| 588 | fn->parent = in; |
| 589 | |
| 590 | ln->fn_sernum = sernum; |
| 591 | |
| 592 | if (addr_bit_set(addr, bit)) { |
| 593 | in->right = ln; |
| 594 | in->left = fn; |
| 595 | } else { |
| 596 | in->left = ln; |
| 597 | in->right = fn; |
| 598 | } |
| 599 | } else { /* plen <= bit */ |
| 600 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 601 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | * (new leaf node)[ln] |
| 603 | * / \ |
| 604 | * (old node)[fn] NULL |
| 605 | */ |
| 606 | |
| 607 | ln = node_alloc(); |
| 608 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 609 | if (!ln) |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 610 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | ln->fn_bit = plen; |
| 613 | |
| 614 | ln->parent = pn; |
| 615 | |
| 616 | ln->fn_sernum = sernum; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (dir) |
| 619 | pn->right = ln; |
| 620 | else |
| 621 | pn->left = ln; |
| 622 | |
| 623 | if (addr_bit_set(&key->addr, plen)) |
| 624 | ln->right = fn; |
| 625 | else |
| 626 | ln->left = fn; |
| 627 | |
| 628 | fn->parent = ln; |
| 629 | } |
| 630 | return ln; |
| 631 | } |
| 632 | |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 633 | static inline bool rt6_qualify_for_ecmp(struct rt6_info *rt) |
| 634 | { |
| 635 | return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) == |
| 636 | RTF_GATEWAY; |
| 637 | } |
| 638 | |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 639 | static int fib6_commit_metrics(struct dst_entry *dst, |
| 640 | struct nlattr *mx, int mx_len) |
| 641 | { |
| 642 | struct nlattr *nla; |
| 643 | int remaining; |
| 644 | u32 *mp; |
| 645 | |
| 646 | if (dst->flags & DST_HOST) { |
| 647 | mp = dst_metrics_write_ptr(dst); |
| 648 | } else { |
| 649 | mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); |
| 650 | if (!mp) |
| 651 | return -ENOMEM; |
| 652 | dst_init_metrics(dst, mp, 0); |
| 653 | } |
| 654 | |
| 655 | nla_for_each_attr(nla, mx, mx_len, remaining) { |
| 656 | int type = nla_type(nla); |
| 657 | |
| 658 | if (type) { |
| 659 | if (type > RTAX_MAX) |
| 660 | return -EINVAL; |
| 661 | |
| 662 | mp[type - 1] = nla_get_u32(nla); |
| 663 | } |
| 664 | } |
| 665 | return 0; |
| 666 | } |
| 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | /* |
| 669 | * Insert routing information in a node. |
| 670 | */ |
| 671 | |
| 672 | static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt, |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 673 | struct nl_info *info, struct nlattr *mx, int mx_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
| 675 | struct rt6_info *iter = NULL; |
| 676 | struct rt6_info **ins; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 677 | int replace = (info->nlh && |
| 678 | (info->nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 679 | int add = (!info->nlh || |
| 680 | (info->nlh->nlmsg_flags & NLM_F_CREATE)); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 681 | int found = 0; |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 682 | bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 683 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
| 685 | ins = &fn->leaf; |
| 686 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 687 | for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Search for duplicates |
| 690 | */ |
| 691 | |
| 692 | if (iter->rt6i_metric == rt->rt6i_metric) { |
| 693 | /* |
| 694 | * Same priority level |
| 695 | */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 696 | if (info->nlh && |
| 697 | (info->nlh->nlmsg_flags & NLM_F_EXCL)) |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 698 | return -EEXIST; |
| 699 | if (replace) { |
| 700 | found++; |
| 701 | break; |
| 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 704 | if (iter->dst.dev == rt->dst.dev && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | iter->rt6i_idev == rt->rt6i_idev && |
| 706 | ipv6_addr_equal(&iter->rt6i_gateway, |
| 707 | &rt->rt6i_gateway)) { |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 708 | if (rt->rt6i_nsiblings) |
| 709 | rt->rt6i_nsiblings = 0; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 710 | if (!(iter->rt6i_flags & RTF_EXPIRES)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | return -EEXIST; |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 712 | if (!(rt->rt6i_flags & RTF_EXPIRES)) |
| 713 | rt6_clean_expires(iter); |
| 714 | else |
| 715 | rt6_set_expires(iter, rt->dst.expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return -EEXIST; |
| 717 | } |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 718 | /* If we have the same destination and the same metric, |
| 719 | * but not the same gateway, then the route we try to |
| 720 | * add is sibling to this route, increment our counter |
| 721 | * of siblings, and later we will add our route to the |
| 722 | * list. |
| 723 | * Only static routes (which don't have flag |
| 724 | * RTF_EXPIRES) are used for ECMPv6. |
| 725 | * |
| 726 | * To avoid long list, we only had siblings if the |
| 727 | * route have a gateway. |
| 728 | */ |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 729 | if (rt_can_ecmp && |
| 730 | rt6_qualify_for_ecmp(iter)) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 731 | rt->rt6i_nsiblings++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | if (iter->rt6i_metric > rt->rt6i_metric) |
| 735 | break; |
| 736 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 737 | ins = &iter->dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 740 | /* Reset round-robin state, if necessary */ |
| 741 | if (ins == &fn->leaf) |
| 742 | fn->rr_ptr = NULL; |
| 743 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 744 | /* Link this route to others same route. */ |
| 745 | if (rt->rt6i_nsiblings) { |
| 746 | unsigned int rt6i_nsiblings; |
| 747 | struct rt6_info *sibling, *temp_sibling; |
| 748 | |
| 749 | /* Find the first route that have the same metric */ |
| 750 | sibling = fn->leaf; |
| 751 | while (sibling) { |
Hannes Frederic Sowa | 307f2fb | 2013-07-12 23:46:33 +0200 | [diff] [blame] | 752 | if (sibling->rt6i_metric == rt->rt6i_metric && |
| 753 | rt6_qualify_for_ecmp(sibling)) { |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 754 | list_add_tail(&rt->rt6i_siblings, |
| 755 | &sibling->rt6i_siblings); |
| 756 | break; |
| 757 | } |
| 758 | sibling = sibling->dst.rt6_next; |
| 759 | } |
| 760 | /* For each sibling in the list, increment the counter of |
| 761 | * siblings. BUG() if counters does not match, list of siblings |
| 762 | * is broken! |
| 763 | */ |
| 764 | rt6i_nsiblings = 0; |
| 765 | list_for_each_entry_safe(sibling, temp_sibling, |
| 766 | &rt->rt6i_siblings, rt6i_siblings) { |
| 767 | sibling->rt6i_nsiblings++; |
| 768 | BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings); |
| 769 | rt6i_nsiblings++; |
| 770 | } |
| 771 | BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings); |
| 772 | } |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | /* |
| 775 | * insert node |
| 776 | */ |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 777 | if (!replace) { |
| 778 | if (!add) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 779 | pr_warn("NLM_F_CREATE should be set when creating new route\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 781 | add: |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 782 | if (mx) { |
| 783 | err = fib6_commit_metrics(&rt->dst, mx, mx_len); |
| 784 | if (err) |
| 785 | return err; |
| 786 | } |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 787 | rt->dst.rt6_next = iter; |
| 788 | *ins = rt; |
| 789 | rt->rt6i_node = fn; |
| 790 | atomic_inc(&rt->rt6i_ref); |
| 791 | inet6_rt_notify(RTM_NEWROUTE, rt, info); |
| 792 | info->nl_net->ipv6.rt6_stats->fib_rt_entries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 794 | if (!(fn->fn_flags & RTN_RTINFO)) { |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 795 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
| 796 | fn->fn_flags |= RTN_RTINFO; |
| 797 | } |
| 798 | |
| 799 | } else { |
| 800 | if (!found) { |
| 801 | if (add) |
| 802 | goto add; |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 803 | pr_warn("NLM_F_REPLACE set, but no existing node found!\n"); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 804 | return -ENOENT; |
| 805 | } |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 806 | if (mx) { |
| 807 | err = fib6_commit_metrics(&rt->dst, mx, mx_len); |
| 808 | if (err) |
| 809 | return err; |
| 810 | } |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 811 | *ins = rt; |
| 812 | rt->rt6i_node = fn; |
| 813 | rt->dst.rt6_next = iter->dst.rt6_next; |
| 814 | atomic_inc(&rt->rt6i_ref); |
| 815 | inet6_rt_notify(RTM_NEWROUTE, rt, info); |
| 816 | rt6_release(iter); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 817 | if (!(fn->fn_flags & RTN_RTINFO)) { |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 818 | info->nl_net->ipv6.rt6_stats->fib_route_nodes++; |
| 819 | fn->fn_flags |= RTN_RTINFO; |
| 820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 826 | 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] | 827 | { |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 828 | if (!timer_pending(&net->ipv6.ip6_fib_timer) && |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 829 | (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE))) |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 830 | mod_timer(&net->ipv6.ip6_fib_timer, |
Stephen Hemminger | 847499c | 2008-07-21 13:21:35 -0700 | [diff] [blame] | 831 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 834 | void fib6_force_start_gc(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | { |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 836 | if (!timer_pending(&net->ipv6.ip6_fib_timer)) |
| 837 | mod_timer(&net->ipv6.ip6_fib_timer, |
Stephen Hemminger | 847499c | 2008-07-21 13:21:35 -0700 | [diff] [blame] | 838 | jiffies + net->ipv6.sysctl.ip6_rt_gc_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Add routing information to the routing tree. |
| 843 | * <destination addr>/<source addr> |
| 844 | * with source addr info in sub-trees |
| 845 | */ |
| 846 | |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 847 | int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info, |
| 848 | struct nlattr *mx, int mx_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 850 | struct fib6_node *fn, *pn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | int err = -ENOMEM; |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 852 | int allow_create = 1; |
| 853 | int replace_required = 0; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 854 | |
| 855 | if (info->nlh) { |
| 856 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 857 | allow_create = 0; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 858 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 859 | replace_required = 1; |
| 860 | } |
| 861 | if (!allow_create && !replace_required) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 862 | pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 864 | fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen, |
| 865 | offsetof(struct rt6_info, rt6i_dst), allow_create, |
| 866 | replace_required); |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 867 | if (IS_ERR(fn)) { |
| 868 | err = PTR_ERR(fn); |
Daniel Borkmann | ae7b4e1 | 2013-09-07 15:13:20 +0200 | [diff] [blame] | 869 | fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | goto out; |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 873 | pn = fn; |
| 874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | #ifdef CONFIG_IPV6_SUBTREES |
| 876 | if (rt->rt6i_src.plen) { |
| 877 | struct fib6_node *sn; |
| 878 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 879 | if (!fn->subtree) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | struct fib6_node *sfn; |
| 881 | |
| 882 | /* |
| 883 | * Create subtree. |
| 884 | * |
| 885 | * fn[main tree] |
| 886 | * | |
| 887 | * sfn[subtree root] |
| 888 | * \ |
| 889 | * sn[new leaf node] |
| 890 | */ |
| 891 | |
| 892 | /* Create subtree root node */ |
| 893 | sfn = node_alloc(); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 894 | if (!sfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | goto st_failure; |
| 896 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 897 | sfn->leaf = info->nl_net->ipv6.ip6_null_entry; |
| 898 | atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | sfn->fn_flags = RTN_ROOT; |
| 900 | sfn->fn_sernum = fib6_new_sernum(); |
| 901 | |
| 902 | /* Now add the first leaf node to new subtree */ |
| 903 | |
| 904 | sn = fib6_add_1(sfn, &rt->rt6i_src.addr, |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 905 | rt->rt6i_src.plen, |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 906 | offsetof(struct rt6_info, rt6i_src), |
| 907 | allow_create, replace_required); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Wei Yongjun | f950c0e | 2012-09-20 18:29:56 +0000 | [diff] [blame] | 909 | if (IS_ERR(sn)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* If it is failed, discard just allocated |
| 911 | root, and then (in st_failure) stale node |
| 912 | in main tree. |
| 913 | */ |
| 914 | node_free(sfn); |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 915 | err = PTR_ERR(sn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | goto st_failure; |
| 917 | } |
| 918 | |
| 919 | /* Now link new subtree to main tree */ |
| 920 | sfn->parent = fn; |
| 921 | fn->subtree = sfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } else { |
| 923 | sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr, |
fan.du | 9225b23 | 2013-07-22 14:21:09 +0800 | [diff] [blame] | 924 | rt->rt6i_src.plen, |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 925 | offsetof(struct rt6_info, rt6i_src), |
| 926 | allow_create, replace_required); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Matti Vaittinen | 4a287eb | 2011-11-14 00:15:14 +0000 | [diff] [blame] | 928 | if (IS_ERR(sn)) { |
| 929 | err = PTR_ERR(sn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | goto st_failure; |
Lin Ming | 188c517 | 2012-09-25 15:17:07 +0000 | [diff] [blame] | 931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 934 | if (!fn->leaf) { |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 935 | fn->leaf = rt; |
| 936 | atomic_inc(&rt->rt6i_ref); |
| 937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | fn = sn; |
| 939 | } |
| 940 | #endif |
| 941 | |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 942 | err = fib6_add_rt2node(fn, rt, info, mx, mx_len); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 943 | if (!err) { |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 944 | fib6_start_gc(info->nl_net, rt); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 945 | if (!(rt->rt6i_flags & RTF_CACHE)) |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 946 | fib6_prune_clones(info->nl_net, pn, rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | out: |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 950 | if (err) { |
| 951 | #ifdef CONFIG_IPV6_SUBTREES |
| 952 | /* |
| 953 | * If fib6_add_1 has cleared the old leaf pointer in the |
| 954 | * super-tree leaf node we have to find a new one for it. |
| 955 | */ |
David S. Miller | 3c05123 | 2008-04-18 01:46:19 -0700 | [diff] [blame] | 956 | if (pn != fn && pn->leaf == rt) { |
| 957 | pn->leaf = NULL; |
| 958 | atomic_dec(&rt->rt6i_ref); |
| 959 | } |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 960 | if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 961 | pn->leaf = fib6_find_prefix(info->nl_net, pn); |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 962 | #if RT6_DEBUG >= 2 |
| 963 | if (!pn->leaf) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 964 | WARN_ON(pn->leaf == NULL); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 965 | pn->leaf = info->nl_net->ipv6.ip6_null_entry; |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 966 | } |
| 967 | #endif |
| 968 | atomic_inc(&pn->leaf->rt6i_ref); |
| 969 | } |
| 970 | #endif |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 971 | dst_free(&rt->dst); |
YOSHIFUJI Hideaki | 66729e1 | 2006-08-23 17:20:34 -0700 | [diff] [blame] | 972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | return err; |
| 974 | |
| 975 | #ifdef CONFIG_IPV6_SUBTREES |
| 976 | /* Subtree creation failed, probably main tree node |
| 977 | is orphan. If it is, shoot it. |
| 978 | */ |
| 979 | st_failure: |
| 980 | if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT))) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 981 | fib6_repair_tree(info->nl_net, fn); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 982 | dst_free(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return err; |
| 984 | #endif |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * Routing tree lookup |
| 989 | * |
| 990 | */ |
| 991 | |
| 992 | struct lookup_args { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 993 | int offset; /* key offset on rt6_info */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 994 | const struct in6_addr *addr; /* search key */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | }; |
| 996 | |
| 997 | static struct fib6_node * fib6_lookup_1(struct fib6_node *root, |
| 998 | struct lookup_args *args) |
| 999 | { |
| 1000 | struct fib6_node *fn; |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 1001 | __be32 dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1003 | if (unlikely(args->offset == 0)) |
| 1004 | return NULL; |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | /* |
| 1007 | * Descend on a tree |
| 1008 | */ |
| 1009 | |
| 1010 | fn = root; |
| 1011 | |
| 1012 | for (;;) { |
| 1013 | struct fib6_node *next; |
| 1014 | |
| 1015 | dir = addr_bit_set(args->addr, fn->fn_bit); |
| 1016 | |
| 1017 | next = dir ? fn->right : fn->left; |
| 1018 | |
| 1019 | if (next) { |
| 1020 | fn = next; |
| 1021 | continue; |
| 1022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | break; |
| 1024 | } |
| 1025 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1026 | while (fn) { |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1027 | if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | struct rt6key *key; |
| 1029 | |
| 1030 | key = (struct rt6key *) ((u8 *) fn->leaf + |
| 1031 | args->offset); |
| 1032 | |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1033 | if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) { |
| 1034 | #ifdef CONFIG_IPV6_SUBTREES |
Hannes Frederic Sowa | 3e3be27 | 2013-08-07 02:34:31 +0200 | [diff] [blame] | 1035 | if (fn->subtree) { |
| 1036 | struct fib6_node *sfn; |
| 1037 | sfn = fib6_lookup_1(fn->subtree, |
| 1038 | args + 1); |
| 1039 | if (!sfn) |
| 1040 | goto backtrack; |
| 1041 | fn = sfn; |
| 1042 | } |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1043 | #endif |
Hannes Frederic Sowa | 3e3be27 | 2013-08-07 02:34:31 +0200 | [diff] [blame] | 1044 | if (fn->fn_flags & RTN_RTINFO) |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1045 | return fn; |
| 1046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
Hannes Frederic Sowa | 3e3be27 | 2013-08-07 02:34:31 +0200 | [diff] [blame] | 1048 | #ifdef CONFIG_IPV6_SUBTREES |
| 1049 | backtrack: |
| 1050 | #endif |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1051 | if (fn->fn_flags & RTN_ROOT) |
| 1052 | break; |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | fn = fn->parent; |
| 1055 | } |
| 1056 | |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1060 | struct fib6_node * fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr, |
| 1061 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | struct fib6_node *fn; |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1064 | struct lookup_args args[] = { |
| 1065 | { |
| 1066 | .offset = offsetof(struct rt6_info, rt6i_dst), |
| 1067 | .addr = daddr, |
| 1068 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1070 | { |
| 1071 | .offset = offsetof(struct rt6_info, rt6i_src), |
| 1072 | .addr = saddr, |
| 1073 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | #endif |
YOSHIFUJI Hideaki | 825e288 | 2006-08-23 17:21:29 -0700 | [diff] [blame] | 1075 | { |
| 1076 | .offset = 0, /* sentinel */ |
| 1077 | } |
| 1078 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
YOSHIFUJI Hideaki | fefc2a6 | 2006-08-23 17:21:50 -0700 | [diff] [blame] | 1080 | fn = fib6_lookup_1(root, daddr ? args : args + 1); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1081 | if (!fn || fn->fn_flags & RTN_TL_ROOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | fn = root; |
| 1083 | |
| 1084 | return fn; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Get node with specified destination prefix (and source prefix, |
| 1089 | * if subtrees are used) |
| 1090 | */ |
| 1091 | |
| 1092 | |
| 1093 | static struct fib6_node * fib6_locate_1(struct fib6_node *root, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1094 | const struct in6_addr *addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | int plen, int offset) |
| 1096 | { |
| 1097 | struct fib6_node *fn; |
| 1098 | |
| 1099 | for (fn = root; fn ; ) { |
| 1100 | struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset); |
| 1101 | |
| 1102 | /* |
| 1103 | * Prefix match |
| 1104 | */ |
| 1105 | if (plen < fn->fn_bit || |
| 1106 | !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) |
| 1107 | return NULL; |
| 1108 | |
| 1109 | if (plen == fn->fn_bit) |
| 1110 | return fn; |
| 1111 | |
| 1112 | /* |
| 1113 | * We have more bits to go |
| 1114 | */ |
| 1115 | if (addr_bit_set(addr, fn->fn_bit)) |
| 1116 | fn = fn->right; |
| 1117 | else |
| 1118 | fn = fn->left; |
| 1119 | } |
| 1120 | return NULL; |
| 1121 | } |
| 1122 | |
| 1123 | struct fib6_node * fib6_locate(struct fib6_node *root, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1124 | const struct in6_addr *daddr, int dst_len, |
| 1125 | const struct in6_addr *saddr, int src_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
| 1127 | struct fib6_node *fn; |
| 1128 | |
| 1129 | fn = fib6_locate_1(root, daddr, dst_len, |
| 1130 | offsetof(struct rt6_info, rt6i_dst)); |
| 1131 | |
| 1132 | #ifdef CONFIG_IPV6_SUBTREES |
| 1133 | if (src_len) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1134 | WARN_ON(saddr == NULL); |
YOSHIFUJI Hideaki | 3fc5e04 | 2006-08-23 17:21:12 -0700 | [diff] [blame] | 1135 | if (fn && fn->subtree) |
| 1136 | fn = fib6_locate_1(fn->subtree, saddr, src_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | offsetof(struct rt6_info, rt6i_src)); |
| 1138 | } |
| 1139 | #endif |
| 1140 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1141 | if (fn && fn->fn_flags & RTN_RTINFO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | return fn; |
| 1143 | |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | /* |
| 1149 | * Deletion |
| 1150 | * |
| 1151 | */ |
| 1152 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1153 | 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] | 1154 | { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1155 | if (fn->fn_flags & RTN_ROOT) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1156 | return net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1158 | while (fn) { |
| 1159 | if (fn->left) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | return fn->left->leaf; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1161 | if (fn->right) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return fn->right->leaf; |
| 1163 | |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1164 | fn = FIB6_SUBTREE(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | } |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * Called to trim the tree of intermediate nodes when possible. "fn" |
| 1171 | * is the node we want to try and remove. |
| 1172 | */ |
| 1173 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1174 | static struct fib6_node *fib6_repair_tree(struct net *net, |
| 1175 | struct fib6_node *fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
| 1177 | int children; |
| 1178 | int nstate; |
| 1179 | struct fib6_node *child, *pn; |
| 1180 | struct fib6_walker_t *w; |
| 1181 | int iter = 0; |
| 1182 | |
| 1183 | for (;;) { |
| 1184 | RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter); |
| 1185 | iter++; |
| 1186 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1187 | WARN_ON(fn->fn_flags & RTN_RTINFO); |
| 1188 | WARN_ON(fn->fn_flags & RTN_TL_ROOT); |
| 1189 | WARN_ON(fn->leaf != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | children = 0; |
| 1192 | child = NULL; |
| 1193 | if (fn->right) child = fn->right, children |= 1; |
| 1194 | if (fn->left) child = fn->left, children |= 2; |
| 1195 | |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1196 | if (children == 3 || FIB6_SUBTREE(fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | #ifdef CONFIG_IPV6_SUBTREES |
| 1198 | /* Subtree root (i.e. fn) may have one child */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1199 | || (children && fn->fn_flags & RTN_ROOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | #endif |
| 1201 | ) { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1202 | fn->leaf = fib6_find_prefix(net, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | #if RT6_DEBUG >= 2 |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1204 | if (!fn->leaf) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1205 | WARN_ON(!fn->leaf); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1206 | fn->leaf = net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
| 1208 | #endif |
| 1209 | atomic_inc(&fn->leaf->rt6i_ref); |
| 1210 | return fn->parent; |
| 1211 | } |
| 1212 | |
| 1213 | pn = fn->parent; |
| 1214 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1215 | if (FIB6_SUBTREE(pn) == fn) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1216 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1217 | FIB6_SUBTREE(pn) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | nstate = FWS_L; |
| 1219 | } else { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1220 | WARN_ON(fn->fn_flags & RTN_ROOT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | #endif |
| 1222 | if (pn->right == fn) pn->right = child; |
| 1223 | else if (pn->left == fn) pn->left = child; |
| 1224 | #if RT6_DEBUG >= 2 |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1225 | else |
| 1226 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | #endif |
| 1228 | if (child) |
| 1229 | child->parent = pn; |
| 1230 | nstate = FWS_R; |
| 1231 | #ifdef CONFIG_IPV6_SUBTREES |
| 1232 | } |
| 1233 | #endif |
| 1234 | |
| 1235 | read_lock(&fib6_walker_lock); |
| 1236 | FOR_WALKERS(w) { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1237 | if (!child) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | if (w->root == fn) { |
| 1239 | w->root = w->node = NULL; |
| 1240 | RT6_TRACE("W %p adjusted by delroot 1\n", w); |
| 1241 | } else if (w->node == fn) { |
| 1242 | RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate); |
| 1243 | w->node = pn; |
| 1244 | w->state = nstate; |
| 1245 | } |
| 1246 | } else { |
| 1247 | if (w->root == fn) { |
| 1248 | w->root = child; |
| 1249 | RT6_TRACE("W %p adjusted by delroot 2\n", w); |
| 1250 | } |
| 1251 | if (w->node == fn) { |
| 1252 | w->node = child; |
| 1253 | if (children&2) { |
| 1254 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 1255 | w->state = w->state >= FWS_R ? FWS_U : FWS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } else { |
| 1257 | RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state); |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 1258 | w->state = w->state >= FWS_C ? FWS_U : FWS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | read_unlock(&fib6_walker_lock); |
| 1264 | |
| 1265 | node_free(fn); |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1266 | if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | return pn; |
| 1268 | |
| 1269 | rt6_release(pn->leaf); |
| 1270 | pn->leaf = NULL; |
| 1271 | fn = pn; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | 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] | 1276 | struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | { |
| 1278 | struct fib6_walker_t *w; |
| 1279 | struct rt6_info *rt = *rtp; |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1280 | struct net *net = info->nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
| 1282 | RT6_TRACE("fib6_del_route\n"); |
| 1283 | |
| 1284 | /* Unlink it */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1285 | *rtp = rt->dst.rt6_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | rt->rt6i_node = NULL; |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1287 | net->ipv6.rt6_stats->fib_rt_entries--; |
| 1288 | net->ipv6.rt6_stats->fib_discarded_routes++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 1290 | /* Reset round-robin state, if necessary */ |
| 1291 | if (fn->rr_ptr == rt) |
| 1292 | fn->rr_ptr = NULL; |
| 1293 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 1294 | /* Remove this entry from other siblings */ |
| 1295 | if (rt->rt6i_nsiblings) { |
| 1296 | struct rt6_info *sibling, *next_sibling; |
| 1297 | |
| 1298 | list_for_each_entry_safe(sibling, next_sibling, |
| 1299 | &rt->rt6i_siblings, rt6i_siblings) |
| 1300 | sibling->rt6i_nsiblings--; |
| 1301 | rt->rt6i_nsiblings = 0; |
| 1302 | list_del_init(&rt->rt6i_siblings); |
| 1303 | } |
| 1304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | /* Adjust walkers */ |
| 1306 | read_lock(&fib6_walker_lock); |
| 1307 | FOR_WALKERS(w) { |
| 1308 | if (w->state == FWS_C && w->leaf == rt) { |
| 1309 | RT6_TRACE("walker %p adjusted by delroute\n", w); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1310 | w->leaf = rt->dst.rt6_next; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1311 | if (!w->leaf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | w->state = FWS_U; |
| 1313 | } |
| 1314 | } |
| 1315 | read_unlock(&fib6_walker_lock); |
| 1316 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1317 | rt->dst.rt6_next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | /* If it was last route, expunge its radix tree node */ |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1320 | if (!fn->leaf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | fn->fn_flags &= ~RTN_RTINFO; |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1322 | net->ipv6.rt6_stats->fib_route_nodes--; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1323 | fn = fib6_repair_tree(net, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | if (atomic_read(&rt->rt6i_ref) != 1) { |
| 1327 | /* This route is used as dummy address holder in some split |
| 1328 | * nodes. It is not leaked, but it still holds other resources, |
| 1329 | * which must be released in time. So, scan ascendant nodes |
| 1330 | * and replace dummy references to this route with references |
| 1331 | * to still alive ones. |
| 1332 | */ |
| 1333 | while (fn) { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1334 | if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1335 | fn->leaf = fib6_find_prefix(net, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | atomic_inc(&fn->leaf->rt6i_ref); |
| 1337 | rt6_release(rt); |
| 1338 | } |
| 1339 | fn = fn->parent; |
| 1340 | } |
| 1341 | /* No more references are possible at this point. */ |
Pavel Emelyanov | 2df96af | 2008-02-18 20:50:42 -0800 | [diff] [blame] | 1342 | BUG_ON(atomic_read(&rt->rt6i_ref) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1345 | inet6_rt_notify(RTM_DELROUTE, rt, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | rt6_release(rt); |
| 1347 | } |
| 1348 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1349 | int fib6_del(struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1351 | struct net *net = info->nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | struct fib6_node *fn = rt->rt6i_node; |
| 1353 | struct rt6_info **rtp; |
| 1354 | |
| 1355 | #if RT6_DEBUG >= 2 |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 1356 | if (rt->dst.obsolete > 0) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1357 | WARN_ON(fn != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | return -ENOENT; |
| 1359 | } |
| 1360 | #endif |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1361 | if (!fn || rt == net->ipv6.ip6_null_entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | return -ENOENT; |
| 1363 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1364 | WARN_ON(!(fn->fn_flags & RTN_RTINFO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1366 | if (!(rt->rt6i_flags & RTF_CACHE)) { |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1367 | struct fib6_node *pn = fn; |
| 1368 | #ifdef CONFIG_IPV6_SUBTREES |
| 1369 | /* clones of this route might be in another subtree */ |
| 1370 | if (rt->rt6i_src.plen) { |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1371 | while (!(pn->fn_flags & RTN_ROOT)) |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1372 | pn = pn->parent; |
| 1373 | pn = pn->parent; |
| 1374 | } |
| 1375 | #endif |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1376 | fib6_prune_clones(info->nl_net, pn, rt); |
YOSHIFUJI Hideaki | 150730d | 2006-08-23 17:22:55 -0700 | [diff] [blame] | 1377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
| 1379 | /* |
| 1380 | * Walk the leaf entries looking for ourself |
| 1381 | */ |
| 1382 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1383 | for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | if (*rtp == rt) { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1385 | fib6_del_route(fn, rtp, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | } |
| 1389 | return -ENOENT; |
| 1390 | } |
| 1391 | |
| 1392 | /* |
| 1393 | * Tree traversal function. |
| 1394 | * |
| 1395 | * Certainly, it is not interrupt safe. |
| 1396 | * However, it is internally reenterable wrt itself and fib6_add/fib6_del. |
| 1397 | * It means, that we can modify tree during walking |
| 1398 | * and use this function for garbage collection, clone pruning, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1399 | * cleaning tree when a device goes down etc. etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | * |
| 1401 | * It guarantees that every node will be traversed, |
| 1402 | * and that it will be traversed only once. |
| 1403 | * |
| 1404 | * Callback function w->func may return: |
| 1405 | * 0 -> continue walking. |
| 1406 | * positive value -> walking is suspended (used by tree dumps, |
| 1407 | * and probably by gc, if it will be split to several slices) |
| 1408 | * negative value -> terminate walking. |
| 1409 | * |
| 1410 | * The function itself returns: |
| 1411 | * 0 -> walk is complete. |
| 1412 | * >0 -> walk is incomplete (i.e. suspended) |
| 1413 | * <0 -> walk is terminated by an error. |
| 1414 | */ |
| 1415 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 1416 | static int fib6_walk_continue(struct fib6_walker_t *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | { |
| 1418 | struct fib6_node *fn, *pn; |
| 1419 | |
| 1420 | for (;;) { |
| 1421 | fn = w->node; |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1422 | if (!fn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | return 0; |
| 1424 | |
| 1425 | if (w->prune && fn != w->root && |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1426 | fn->fn_flags & RTN_RTINFO && w->state < FWS_C) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | w->state = FWS_C; |
| 1428 | w->leaf = fn->leaf; |
| 1429 | } |
| 1430 | switch (w->state) { |
| 1431 | #ifdef CONFIG_IPV6_SUBTREES |
| 1432 | case FWS_S: |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1433 | if (FIB6_SUBTREE(fn)) { |
| 1434 | w->node = FIB6_SUBTREE(fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | continue; |
| 1436 | } |
| 1437 | w->state = FWS_L; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1438 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | case FWS_L: |
| 1440 | if (fn->left) { |
| 1441 | w->node = fn->left; |
| 1442 | w->state = FWS_INIT; |
| 1443 | continue; |
| 1444 | } |
| 1445 | w->state = FWS_R; |
| 1446 | case FWS_R: |
| 1447 | if (fn->right) { |
| 1448 | w->node = fn->right; |
| 1449 | w->state = FWS_INIT; |
| 1450 | continue; |
| 1451 | } |
| 1452 | w->state = FWS_C; |
| 1453 | w->leaf = fn->leaf; |
| 1454 | case FWS_C: |
David S. Miller | 507c9b1 | 2011-12-03 17:50:45 -0500 | [diff] [blame] | 1455 | if (w->leaf && fn->fn_flags & RTN_RTINFO) { |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1456 | int err; |
| 1457 | |
Eric Dumazet | fa809e2 | 2012-06-25 15:37:19 -0700 | [diff] [blame] | 1458 | if (w->skip) { |
| 1459 | w->skip--; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1460 | continue; |
| 1461 | } |
| 1462 | |
| 1463 | err = w->func(w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | if (err) |
| 1465 | return err; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1466 | |
| 1467 | w->count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | continue; |
| 1469 | } |
| 1470 | w->state = FWS_U; |
| 1471 | case FWS_U: |
| 1472 | if (fn == w->root) |
| 1473 | return 0; |
| 1474 | pn = fn->parent; |
| 1475 | w->node = pn; |
| 1476 | #ifdef CONFIG_IPV6_SUBTREES |
YOSHIFUJI Hideaki | 7fc3316 | 2006-08-23 17:22:24 -0700 | [diff] [blame] | 1477 | if (FIB6_SUBTREE(pn) == fn) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1478 | WARN_ON(!(fn->fn_flags & RTN_ROOT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | w->state = FWS_L; |
| 1480 | continue; |
| 1481 | } |
| 1482 | #endif |
| 1483 | if (pn->left == fn) { |
| 1484 | w->state = FWS_R; |
| 1485 | continue; |
| 1486 | } |
| 1487 | if (pn->right == fn) { |
| 1488 | w->state = FWS_C; |
| 1489 | w->leaf = w->node->leaf; |
| 1490 | continue; |
| 1491 | } |
| 1492 | #if RT6_DEBUG >= 2 |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1493 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | #endif |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
Adrian Bunk | 90d4112 | 2006-08-14 23:49:16 -0700 | [diff] [blame] | 1499 | static int fib6_walk(struct fib6_walker_t *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | { |
| 1501 | int res; |
| 1502 | |
| 1503 | w->state = FWS_INIT; |
| 1504 | w->node = w->root; |
| 1505 | |
| 1506 | fib6_walker_link(w); |
| 1507 | res = fib6_walk_continue(w); |
| 1508 | if (res <= 0) |
| 1509 | fib6_walker_unlink(w); |
| 1510 | return res; |
| 1511 | } |
| 1512 | |
| 1513 | static int fib6_clean_node(struct fib6_walker_t *w) |
| 1514 | { |
| 1515 | int res; |
| 1516 | struct rt6_info *rt; |
Benjamin Thery | 0a8891a | 2007-10-08 20:39:36 -0700 | [diff] [blame] | 1517 | 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] | 1518 | struct nl_info info = { |
| 1519 | .nl_net = c->net, |
| 1520 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1522 | for (rt = w->leaf; rt; rt = rt->dst.rt6_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | res = c->func(rt, c->arg); |
| 1524 | if (res < 0) { |
| 1525 | w->leaf = rt; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 1526 | res = fib6_del(rt, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | if (res) { |
| 1528 | #if RT6_DEBUG >= 2 |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 1529 | pr_debug("%s: del failed: rt=%p@%p err=%d\n", |
| 1530 | __func__, rt, rt->rt6i_node, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | #endif |
| 1532 | continue; |
| 1533 | } |
| 1534 | return 0; |
| 1535 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1536 | WARN_ON(res != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
| 1538 | w->leaf = rt; |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Convenient frontend to tree walker. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1544 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | * func is called on each route. |
| 1546 | * It may return -1 -> delete this route. |
| 1547 | * 0 -> continue walking |
| 1548 | * |
| 1549 | * prune==1 -> only immediate children of node (certainly, |
| 1550 | * ignoring pure split nodes) will be scanned. |
| 1551 | */ |
| 1552 | |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1553 | static void fib6_clean_tree(struct net *net, struct fib6_node *root, |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 1554 | int (*func)(struct rt6_info *, void *arg), |
| 1555 | int prune, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | { |
| 1557 | struct fib6_cleaner_t c; |
| 1558 | |
| 1559 | c.w.root = root; |
| 1560 | c.w.func = fib6_clean_node; |
| 1561 | c.w.prune = prune; |
Patrick McHardy | 2bec5a3 | 2010-02-08 05:19:03 +0000 | [diff] [blame] | 1562 | c.w.count = 0; |
| 1563 | c.w.skip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | c.func = func; |
| 1565 | c.arg = arg; |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1566 | c.net = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
| 1568 | fib6_walk(&c.w); |
| 1569 | } |
| 1570 | |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 1571 | void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 1572 | void *arg) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1573 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1574 | struct fib6_table *table; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1575 | struct hlist_head *head; |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1576 | unsigned int h; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1577 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1578 | rcu_read_lock(); |
Neil Horman | a33bc5c | 2009-07-30 18:52:15 -0700 | [diff] [blame] | 1579 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 1580 | head = &net->ipv6.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1581 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1582 | write_lock_bh(&table->tb6_lock); |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1583 | fib6_clean_tree(net, &table->tb6_root, |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 1584 | func, 0, arg); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1585 | write_unlock_bh(&table->tb6_lock); |
| 1586 | } |
| 1587 | } |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 1588 | rcu_read_unlock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | static int fib6_prune_clone(struct rt6_info *rt, void *arg) |
| 1592 | { |
| 1593 | if (rt->rt6i_flags & RTF_CACHE) { |
| 1594 | RT6_TRACE("pruning clone %p\n", rt); |
| 1595 | return -1; |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1601 | static void fib6_prune_clones(struct net *net, struct fib6_node *fn, |
| 1602 | struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | { |
Benjamin Thery | ec7d43c | 2008-03-03 23:31:57 -0800 | [diff] [blame] | 1604 | fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * Garbage collection |
| 1609 | */ |
| 1610 | |
| 1611 | static struct fib6_gc_args |
| 1612 | { |
| 1613 | int timeout; |
| 1614 | int more; |
| 1615 | } gc_args; |
| 1616 | |
| 1617 | static int fib6_age(struct rt6_info *rt, void *arg) |
| 1618 | { |
| 1619 | unsigned long now = jiffies; |
| 1620 | |
| 1621 | /* |
| 1622 | * check addrconf expiration here. |
| 1623 | * Routes are expired even if they are in use. |
| 1624 | * |
| 1625 | * Also age clones. Note, that clones are aged out |
| 1626 | * only if they are not in use now. |
| 1627 | */ |
| 1628 | |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 1629 | if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) { |
| 1630 | if (time_after(now, rt->dst.expires)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | RT6_TRACE("expiring %p\n", rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | return -1; |
| 1633 | } |
| 1634 | gc_args.more++; |
| 1635 | } else if (rt->rt6i_flags & RTF_CACHE) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1636 | if (atomic_read(&rt->dst.__refcnt) == 0 && |
| 1637 | time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | RT6_TRACE("aging clone %p\n", rt); |
| 1639 | return -1; |
David S. Miller | 5339ab8 | 2012-01-27 15:14:01 -0800 | [diff] [blame] | 1640 | } else if (rt->rt6i_flags & RTF_GATEWAY) { |
| 1641 | struct neighbour *neigh; |
| 1642 | __u8 neigh_flags = 0; |
| 1643 | |
| 1644 | neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); |
| 1645 | if (neigh) { |
| 1646 | neigh_flags = neigh->flags; |
| 1647 | neigh_release(neigh); |
| 1648 | } |
Thomas Graf | 8bd7451 | 2012-06-07 06:51:04 +0000 | [diff] [blame] | 1649 | if (!(neigh_flags & NTF_ROUTER)) { |
David S. Miller | 5339ab8 | 2012-01-27 15:14:01 -0800 | [diff] [blame] | 1650 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 1651 | rt); |
| 1652 | return -1; |
| 1653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | } |
| 1655 | gc_args.more++; |
| 1656 | } |
| 1657 | |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
| 1661 | static DEFINE_SPINLOCK(fib6_gc_lock); |
| 1662 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1663 | void fib6_run_gc(unsigned long expires, struct net *net, bool force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | { |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 1665 | unsigned long now; |
| 1666 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1667 | if (force) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | spin_lock_bh(&fib6_gc_lock); |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1669 | } else if (!spin_trylock_bh(&fib6_gc_lock)) { |
| 1670 | mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ); |
| 1671 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | } |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1673 | gc_args.timeout = expires ? (int)expires : |
| 1674 | net->ipv6.sysctl.ip6_rt_gc_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | |
Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1676 | gc_args.more = icmp6_dst_gc(); |
Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 1677 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 1678 | fib6_clean_all(net, fib6_age, NULL); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 1679 | now = jiffies; |
| 1680 | net->ipv6.ip6_rt_last_gc = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
| 1682 | if (gc_args.more) |
Stephen Hemminger | c8a4522 | 2008-07-22 14:34:09 -0700 | [diff] [blame] | 1683 | mod_timer(&net->ipv6.ip6_fib_timer, |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 1684 | round_jiffies(now |
Stephen Hemminger | c8a4522 | 2008-07-22 14:34:09 -0700 | [diff] [blame] | 1685 | + net->ipv6.sysctl.ip6_rt_gc_interval)); |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1686 | else |
| 1687 | del_timer(&net->ipv6.ip6_fib_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | spin_unlock_bh(&fib6_gc_lock); |
| 1689 | } |
| 1690 | |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 1691 | static void fib6_gc_timer_cb(unsigned long arg) |
| 1692 | { |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 1693 | fib6_run_gc(0, (struct net *)arg, true); |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1696 | static int __net_init fib6_net_init(struct net *net) |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1697 | { |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1698 | size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ; |
| 1699 | |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1700 | 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] | 1701 | |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1702 | net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL); |
| 1703 | if (!net->ipv6.rt6_stats) |
| 1704 | goto out_timer; |
| 1705 | |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1706 | /* Avoid false sharing : Use at least a full cache line */ |
| 1707 | size = max_t(size_t, size, L1_CACHE_BYTES); |
| 1708 | |
| 1709 | net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1710 | if (!net->ipv6.fib_table_hash) |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1711 | goto out_rt6_stats; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1712 | |
| 1713 | net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl), |
| 1714 | GFP_KERNEL); |
| 1715 | if (!net->ipv6.fib6_main_tbl) |
| 1716 | goto out_fib_table_hash; |
| 1717 | |
| 1718 | net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1719 | 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] | 1720 | net->ipv6.fib6_main_tbl->tb6_root.fn_flags = |
| 1721 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1722 | inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1723 | |
| 1724 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1725 | net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl), |
| 1726 | GFP_KERNEL); |
| 1727 | if (!net->ipv6.fib6_local_tbl) |
| 1728 | goto out_fib6_main_tbl; |
| 1729 | net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1730 | 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] | 1731 | net->ipv6.fib6_local_tbl->tb6_root.fn_flags = |
| 1732 | RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO; |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1733 | inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1734 | #endif |
| 1735 | fib6_tables_init(net); |
| 1736 | |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1737 | return 0; |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1738 | |
| 1739 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 1740 | out_fib6_main_tbl: |
| 1741 | kfree(net->ipv6.fib6_main_tbl); |
| 1742 | #endif |
| 1743 | out_fib_table_hash: |
| 1744 | kfree(net->ipv6.fib_table_hash); |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1745 | out_rt6_stats: |
| 1746 | kfree(net->ipv6.rt6_stats); |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1747 | out_timer: |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1748 | return -ENOMEM; |
Wang Yufen | 8db46f1 | 2014-03-28 12:07:02 +0800 | [diff] [blame^] | 1749 | } |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1750 | |
| 1751 | static void fib6_net_exit(struct net *net) |
| 1752 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1753 | rt6_ifdown(net, NULL); |
Stephen Hemminger | 417f28b | 2008-07-22 14:33:45 -0700 | [diff] [blame] | 1754 | del_timer_sync(&net->ipv6.ip6_fib_timer); |
| 1755 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1756 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1757 | inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1758 | kfree(net->ipv6.fib6_local_tbl); |
| 1759 | #endif |
David S. Miller | 8e77327 | 2012-06-11 00:01:52 -0700 | [diff] [blame] | 1760 | inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1761 | kfree(net->ipv6.fib6_main_tbl); |
| 1762 | kfree(net->ipv6.fib_table_hash); |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1763 | kfree(net->ipv6.rt6_stats); |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | static struct pernet_operations fib6_net_ops = { |
| 1767 | .init = fib6_net_init, |
| 1768 | .exit = fib6_net_exit, |
| 1769 | }; |
| 1770 | |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1771 | int __init fib6_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | { |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame] | 1773 | int ret = -ENOMEM; |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | fib6_node_kmem = kmem_cache_create("fib6_nodes", |
| 1776 | sizeof(struct fib6_node), |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1777 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1778 | NULL); |
Daniel Lezcano | f845ab6 | 2007-12-07 00:45:16 -0800 | [diff] [blame] | 1779 | if (!fib6_node_kmem) |
Daniel Lezcano | e0b85590 | 2008-03-03 23:24:31 -0800 | [diff] [blame] | 1780 | goto out; |
| 1781 | |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1782 | ret = register_pernet_subsys(&fib6_net_ops); |
| 1783 | if (ret) |
Benjamin Thery | c572872 | 2008-03-03 23:34:17 -0800 | [diff] [blame] | 1784 | goto out_kmem_cache_create; |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 1785 | |
| 1786 | ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib, |
| 1787 | NULL); |
| 1788 | if (ret) |
| 1789 | goto out_unregister_subsys; |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1790 | out: |
| 1791 | return ret; |
| 1792 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 1793 | out_unregister_subsys: |
| 1794 | unregister_pernet_subsys(&fib6_net_ops); |
Daniel Lezcano | d63bddb | 2007-12-07 00:40:34 -0800 | [diff] [blame] | 1795 | out_kmem_cache_create: |
| 1796 | kmem_cache_destroy(fib6_node_kmem); |
| 1797 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | void fib6_gc_cleanup(void) |
| 1801 | { |
Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1802 | unregister_pernet_subsys(&fib6_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | kmem_cache_destroy(fib6_node_kmem); |
| 1804 | } |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1805 | |
| 1806 | #ifdef CONFIG_PROC_FS |
| 1807 | |
| 1808 | struct ipv6_route_iter { |
| 1809 | struct seq_net_private p; |
| 1810 | struct fib6_walker_t w; |
| 1811 | loff_t skip; |
| 1812 | struct fib6_table *tbl; |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 1813 | __u32 sernum; |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1814 | }; |
| 1815 | |
| 1816 | static int ipv6_route_seq_show(struct seq_file *seq, void *v) |
| 1817 | { |
| 1818 | struct rt6_info *rt = v; |
| 1819 | struct ipv6_route_iter *iter = seq->private; |
| 1820 | |
| 1821 | seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); |
| 1822 | |
| 1823 | #ifdef CONFIG_IPV6_SUBTREES |
| 1824 | seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); |
| 1825 | #else |
| 1826 | seq_puts(seq, "00000000000000000000000000000000 00 "); |
| 1827 | #endif |
| 1828 | if (rt->rt6i_flags & RTF_GATEWAY) |
| 1829 | seq_printf(seq, "%pi6", &rt->rt6i_gateway); |
| 1830 | else |
| 1831 | seq_puts(seq, "00000000000000000000000000000000"); |
| 1832 | |
| 1833 | seq_printf(seq, " %08x %08x %08x %08x %8s\n", |
| 1834 | rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), |
| 1835 | rt->dst.__use, rt->rt6i_flags, |
| 1836 | rt->dst.dev ? rt->dst.dev->name : ""); |
| 1837 | iter->w.leaf = NULL; |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | static int ipv6_route_yield(struct fib6_walker_t *w) |
| 1842 | { |
| 1843 | struct ipv6_route_iter *iter = w->args; |
| 1844 | |
| 1845 | if (!iter->skip) |
| 1846 | return 1; |
| 1847 | |
| 1848 | do { |
| 1849 | iter->w.leaf = iter->w.leaf->dst.rt6_next; |
| 1850 | iter->skip--; |
| 1851 | if (!iter->skip && iter->w.leaf) |
| 1852 | return 1; |
| 1853 | } while (iter->w.leaf); |
| 1854 | |
| 1855 | return 0; |
| 1856 | } |
| 1857 | |
| 1858 | static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter) |
| 1859 | { |
| 1860 | memset(&iter->w, 0, sizeof(iter->w)); |
| 1861 | iter->w.func = ipv6_route_yield; |
| 1862 | iter->w.root = &iter->tbl->tb6_root; |
| 1863 | iter->w.state = FWS_INIT; |
| 1864 | iter->w.node = iter->w.root; |
| 1865 | iter->w.args = iter; |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 1866 | iter->sernum = iter->w.root->fn_sernum; |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1867 | INIT_LIST_HEAD(&iter->w.lh); |
| 1868 | fib6_walker_link(&iter->w); |
| 1869 | } |
| 1870 | |
| 1871 | static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl, |
| 1872 | struct net *net) |
| 1873 | { |
| 1874 | unsigned int h; |
| 1875 | struct hlist_node *node; |
| 1876 | |
| 1877 | if (tbl) { |
| 1878 | h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1; |
| 1879 | node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist)); |
| 1880 | } else { |
| 1881 | h = 0; |
| 1882 | node = NULL; |
| 1883 | } |
| 1884 | |
| 1885 | while (!node && h < FIB6_TABLE_HASHSZ) { |
| 1886 | node = rcu_dereference_bh( |
| 1887 | hlist_first_rcu(&net->ipv6.fib_table_hash[h++])); |
| 1888 | } |
| 1889 | return hlist_entry_safe(node, struct fib6_table, tb6_hlist); |
| 1890 | } |
| 1891 | |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 1892 | static void ipv6_route_check_sernum(struct ipv6_route_iter *iter) |
| 1893 | { |
| 1894 | if (iter->sernum != iter->w.root->fn_sernum) { |
| 1895 | iter->sernum = iter->w.root->fn_sernum; |
| 1896 | iter->w.state = FWS_INIT; |
| 1897 | iter->w.node = iter->w.root; |
| 1898 | WARN_ON(iter->w.skip); |
| 1899 | iter->w.skip = iter->w.count; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1903 | static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1904 | { |
| 1905 | int r; |
| 1906 | struct rt6_info *n; |
| 1907 | struct net *net = seq_file_net(seq); |
| 1908 | struct ipv6_route_iter *iter = seq->private; |
| 1909 | |
| 1910 | if (!v) |
| 1911 | goto iter_table; |
| 1912 | |
| 1913 | n = ((struct rt6_info *)v)->dst.rt6_next; |
| 1914 | if (n) { |
| 1915 | ++*pos; |
| 1916 | return n; |
| 1917 | } |
| 1918 | |
| 1919 | iter_table: |
Hannes Frederic Sowa | 0a67d3e | 2013-09-21 16:56:10 +0200 | [diff] [blame] | 1920 | ipv6_route_check_sernum(iter); |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 1921 | read_lock(&iter->tbl->tb6_lock); |
| 1922 | r = fib6_walk_continue(&iter->w); |
| 1923 | read_unlock(&iter->tbl->tb6_lock); |
| 1924 | if (r > 0) { |
| 1925 | if (v) |
| 1926 | ++*pos; |
| 1927 | return iter->w.leaf; |
| 1928 | } else if (r < 0) { |
| 1929 | fib6_walker_unlink(&iter->w); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | fib6_walker_unlink(&iter->w); |
| 1933 | |
| 1934 | iter->tbl = ipv6_route_seq_next_table(iter->tbl, net); |
| 1935 | if (!iter->tbl) |
| 1936 | return NULL; |
| 1937 | |
| 1938 | ipv6_route_seq_setup_walk(iter); |
| 1939 | goto iter_table; |
| 1940 | } |
| 1941 | |
| 1942 | static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos) |
| 1943 | __acquires(RCU_BH) |
| 1944 | { |
| 1945 | struct net *net = seq_file_net(seq); |
| 1946 | struct ipv6_route_iter *iter = seq->private; |
| 1947 | |
| 1948 | rcu_read_lock_bh(); |
| 1949 | iter->tbl = ipv6_route_seq_next_table(NULL, net); |
| 1950 | iter->skip = *pos; |
| 1951 | |
| 1952 | if (iter->tbl) { |
| 1953 | ipv6_route_seq_setup_walk(iter); |
| 1954 | return ipv6_route_seq_next(seq, NULL, pos); |
| 1955 | } else { |
| 1956 | return NULL; |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | static bool ipv6_route_iter_active(struct ipv6_route_iter *iter) |
| 1961 | { |
| 1962 | struct fib6_walker_t *w = &iter->w; |
| 1963 | return w->node && !(w->state == FWS_U && w->node == w->root); |
| 1964 | } |
| 1965 | |
| 1966 | static void ipv6_route_seq_stop(struct seq_file *seq, void *v) |
| 1967 | __releases(RCU_BH) |
| 1968 | { |
| 1969 | struct ipv6_route_iter *iter = seq->private; |
| 1970 | |
| 1971 | if (ipv6_route_iter_active(iter)) |
| 1972 | fib6_walker_unlink(&iter->w); |
| 1973 | |
| 1974 | rcu_read_unlock_bh(); |
| 1975 | } |
| 1976 | |
| 1977 | static const struct seq_operations ipv6_route_seq_ops = { |
| 1978 | .start = ipv6_route_seq_start, |
| 1979 | .next = ipv6_route_seq_next, |
| 1980 | .stop = ipv6_route_seq_stop, |
| 1981 | .show = ipv6_route_seq_show |
| 1982 | }; |
| 1983 | |
| 1984 | int ipv6_route_open(struct inode *inode, struct file *file) |
| 1985 | { |
| 1986 | return seq_open_net(inode, file, &ipv6_route_seq_ops, |
| 1987 | sizeof(struct ipv6_route_iter)); |
| 1988 | } |
| 1989 | |
| 1990 | #endif /* CONFIG_PROC_FS */ |