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