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