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