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