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