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