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