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