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