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