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