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