Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * IPv4 Forwarding Information Base: semantics. |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 12 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/bitops.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/jiffies.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/socket.h> |
| 20 | #include <linux/sockios.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/inet.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 24 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/if_arp.h> |
| 27 | #include <linux/proc_fs.h> |
| 28 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 31 | #include <linux/netlink.h> |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 32 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 34 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <net/ip.h> |
| 36 | #include <net/protocol.h> |
| 37 | #include <net/route.h> |
| 38 | #include <net/tcp.h> |
| 39 | #include <net/sock.h> |
| 40 | #include <net/ip_fib.h> |
David Ahern | 717a8f5 | 2019-04-05 16:30:32 -0700 | [diff] [blame] | 41 | #include <net/ip6_fib.h> |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 42 | #include <net/nexthop.h> |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 43 | #include <net/netlink.h> |
David Ahern | 3c618c1 | 2019-04-20 09:28:20 -0700 | [diff] [blame] | 44 | #include <net/rtnh.h> |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 45 | #include <net/lwtunnel.h> |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 46 | #include <net/fib_notifier.h> |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 47 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #include "fib_lookup.h" |
| 50 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 51 | static DEFINE_SPINLOCK(fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static struct hlist_head *fib_info_hash; |
| 53 | static struct hlist_head *fib_info_laddrhash; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 54 | static unsigned int fib_info_hash_size; |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 55 | static unsigned int fib_info_hash_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static unsigned int fib_info_cnt; |
| 57 | |
| 58 | #define DEVINDEX_HASHBITS 8 |
| 59 | #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) |
| 60 | static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; |
| 61 | |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 62 | /* for_nexthops and change_nexthops only used when nexthop object |
| 63 | * is not set in a fib_info. The logic within can reference fib_nh. |
| 64 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 67 | #define for_nexthops(fi) { \ |
| 68 | int nhsel; const struct fib_nh *nh; \ |
| 69 | for (nhsel = 0, nh = (fi)->fib_nh; \ |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 70 | nhsel < fib_info_num_path((fi)); \ |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 71 | nh++, nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 73 | #define change_nexthops(fi) { \ |
| 74 | int nhsel; struct fib_nh *nexthop_nh; \ |
| 75 | for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 76 | nhsel < fib_info_num_path((fi)); \ |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 77 | nexthop_nh++, nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | #else /* CONFIG_IP_ROUTE_MULTIPATH */ |
| 80 | |
| 81 | /* Hope, that gcc will optimize it to get rid of dummy loop */ |
| 82 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 83 | #define for_nexthops(fi) { \ |
| 84 | int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ |
| 85 | for (nhsel = 0; nhsel < 1; nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 87 | #define change_nexthops(fi) { \ |
| 88 | int nhsel; \ |
| 89 | struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ |
| 90 | for (nhsel = 0; nhsel < 1; nhsel++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | #endif /* CONFIG_IP_ROUTE_MULTIPATH */ |
| 93 | |
| 94 | #define endfor_nexthops(fi) } |
| 95 | |
| 96 | |
David S. Miller | 3be0686 | 2011-03-07 15:01:10 -0800 | [diff] [blame] | 97 | const struct fib_prop fib_props[RTN_MAX + 1] = { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 98 | [RTN_UNSPEC] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | .error = 0, |
| 100 | .scope = RT_SCOPE_NOWHERE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 101 | }, |
| 102 | [RTN_UNICAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .error = 0, |
| 104 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 105 | }, |
| 106 | [RTN_LOCAL] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .error = 0, |
| 108 | .scope = RT_SCOPE_HOST, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 109 | }, |
| 110 | [RTN_BROADCAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .error = 0, |
| 112 | .scope = RT_SCOPE_LINK, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 113 | }, |
| 114 | [RTN_ANYCAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .error = 0, |
| 116 | .scope = RT_SCOPE_LINK, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 117 | }, |
| 118 | [RTN_MULTICAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .error = 0, |
| 120 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 121 | }, |
| 122 | [RTN_BLACKHOLE] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | .error = -EINVAL, |
| 124 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 125 | }, |
| 126 | [RTN_UNREACHABLE] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .error = -EHOSTUNREACH, |
| 128 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 129 | }, |
| 130 | [RTN_PROHIBIT] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .error = -EACCES, |
| 132 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 133 | }, |
| 134 | [RTN_THROW] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .error = -EAGAIN, |
| 136 | .scope = RT_SCOPE_UNIVERSE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 137 | }, |
| 138 | [RTN_NAT] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .error = -EINVAL, |
| 140 | .scope = RT_SCOPE_NOWHERE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 141 | }, |
| 142 | [RTN_XRESOLVE] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | .error = -EINVAL, |
| 144 | .scope = RT_SCOPE_NOWHERE, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 145 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
David S. Miller | c5038a8 | 2012-07-31 15:02:02 -0700 | [diff] [blame] | 148 | static void rt_fibinfo_free(struct rtable __rcu **rtp) |
Eric Dumazet | 54764bb | 2012-07-31 01:08:23 +0000 | [diff] [blame] | 149 | { |
| 150 | struct rtable *rt = rcu_dereference_protected(*rtp, 1); |
| 151 | |
| 152 | if (!rt) |
| 153 | return; |
| 154 | |
| 155 | /* Not even needed : RCU_INIT_POINTER(*rtp, NULL); |
| 156 | * because we waited an RCU grace period before calling |
| 157 | * free_fib_info_rcu() |
| 158 | */ |
| 159 | |
Wei Wang | 95c47f9 | 2017-06-17 10:42:30 -0700 | [diff] [blame] | 160 | dst_dev_put(&rt->dst); |
Wei Wang | b838d5e | 2017-06-17 10:42:32 -0700 | [diff] [blame] | 161 | dst_release_immediate(&rt->dst); |
Eric Dumazet | 54764bb | 2012-07-31 01:08:23 +0000 | [diff] [blame] | 162 | } |
| 163 | |
David Ahern | a5995e7 | 2019-04-30 07:45:50 -0700 | [diff] [blame] | 164 | static void free_nh_exceptions(struct fib_nh_common *nhc) |
David S. Miller | c5038a8 | 2012-07-31 15:02:02 -0700 | [diff] [blame] | 165 | { |
Eric Dumazet | caa4152 | 2014-09-03 22:21:56 -0700 | [diff] [blame] | 166 | struct fnhe_hash_bucket *hash; |
David S. Miller | c5038a8 | 2012-07-31 15:02:02 -0700 | [diff] [blame] | 167 | int i; |
| 168 | |
David Ahern | a5995e7 | 2019-04-30 07:45:50 -0700 | [diff] [blame] | 169 | hash = rcu_dereference_protected(nhc->nhc_exceptions, 1); |
Eric Dumazet | caa4152 | 2014-09-03 22:21:56 -0700 | [diff] [blame] | 170 | if (!hash) |
| 171 | return; |
David S. Miller | c5038a8 | 2012-07-31 15:02:02 -0700 | [diff] [blame] | 172 | for (i = 0; i < FNHE_HASH_SIZE; i++) { |
| 173 | struct fib_nh_exception *fnhe; |
| 174 | |
| 175 | fnhe = rcu_dereference_protected(hash[i].chain, 1); |
| 176 | while (fnhe) { |
| 177 | struct fib_nh_exception *next; |
Stephen Hemminger | 82695b3 | 2018-02-27 15:48:21 -0800 | [diff] [blame] | 178 | |
David S. Miller | c5038a8 | 2012-07-31 15:02:02 -0700 | [diff] [blame] | 179 | next = rcu_dereference_protected(fnhe->fnhe_next, 1); |
| 180 | |
Timo Teräs | 2ffae99 | 2013-06-27 10:27:05 +0300 | [diff] [blame] | 181 | rt_fibinfo_free(&fnhe->fnhe_rth_input); |
| 182 | rt_fibinfo_free(&fnhe->fnhe_rth_output); |
David S. Miller | c5038a8 | 2012-07-31 15:02:02 -0700 | [diff] [blame] | 183 | |
| 184 | kfree(fnhe); |
| 185 | |
| 186 | fnhe = next; |
| 187 | } |
| 188 | } |
| 189 | kfree(hash); |
| 190 | } |
| 191 | |
| 192 | static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp) |
Eric Dumazet | d26b3a7 | 2012-07-31 05:45:30 +0000 | [diff] [blame] | 193 | { |
| 194 | int cpu; |
| 195 | |
| 196 | if (!rtp) |
| 197 | return; |
| 198 | |
| 199 | for_each_possible_cpu(cpu) { |
| 200 | struct rtable *rt; |
| 201 | |
| 202 | rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1); |
Wei Wang | 0830106 | 2017-06-17 10:42:29 -0700 | [diff] [blame] | 203 | if (rt) { |
Wei Wang | 95c47f9 | 2017-06-17 10:42:30 -0700 | [diff] [blame] | 204 | dst_dev_put(&rt->dst); |
Wei Wang | b838d5e | 2017-06-17 10:42:32 -0700 | [diff] [blame] | 205 | dst_release_immediate(&rt->dst); |
Wei Wang | 0830106 | 2017-06-17 10:42:29 -0700 | [diff] [blame] | 206 | } |
Eric Dumazet | d26b3a7 | 2012-07-31 05:45:30 +0000 | [diff] [blame] | 207 | } |
| 208 | free_percpu(rtp); |
| 209 | } |
| 210 | |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 211 | void fib_nh_common_release(struct fib_nh_common *nhc) |
| 212 | { |
Eric Dumazet | e44b14e | 2021-12-06 17:30:32 -0800 | [diff] [blame] | 213 | dev_put_track(nhc->nhc_dev, &nhc->nhc_dev_tracker); |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 214 | lwtstate_put(nhc->nhc_lwtstate); |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 215 | rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output); |
| 216 | rt_fibinfo_free(&nhc->nhc_rth_input); |
David Ahern | a5995e7 | 2019-04-30 07:45:50 -0700 | [diff] [blame] | 217 | free_nh_exceptions(nhc); |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 218 | } |
| 219 | EXPORT_SYMBOL_GPL(fib_nh_common_release); |
| 220 | |
David Ahern | faa041a | 2019-03-27 20:53:49 -0700 | [diff] [blame] | 221 | void fib_nh_release(struct net *net, struct fib_nh *fib_nh) |
| 222 | { |
| 223 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 224 | if (fib_nh->nh_tclassid) |
Eric Dumazet | 213f5f8 | 2021-12-01 18:26:35 -0800 | [diff] [blame] | 225 | atomic_dec(&net->ipv4.fib_num_tclassid_users); |
David Ahern | faa041a | 2019-03-27 20:53:49 -0700 | [diff] [blame] | 226 | #endif |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 227 | fib_nh_common_release(&fib_nh->nh_common); |
David Ahern | faa041a | 2019-03-27 20:53:49 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | /* Release a nexthop info record */ |
Yan, Zheng | 19c1ea1 | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 231 | static void free_fib_info_rcu(struct rcu_head *head) |
| 232 | { |
| 233 | struct fib_info *fi = container_of(head, struct fib_info, rcu); |
| 234 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 235 | if (fi->nh) { |
| 236 | nexthop_put(fi->nh); |
| 237 | } else { |
| 238 | change_nexthops(fi) { |
| 239 | fib_nh_release(fi->fib_net, nexthop_nh); |
| 240 | } endfor_nexthops(fi); |
| 241 | } |
Yanmin Zhang | e49cc0d | 2012-05-23 15:39:45 +0000 | [diff] [blame] | 242 | |
David Ahern | cc5f0eb | 2018-10-04 20:07:52 -0700 | [diff] [blame] | 243 | ip_fib_metrics_put(fi->fib_metrics); |
| 244 | |
Yan, Zheng | 19c1ea1 | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 245 | kfree(fi); |
| 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | void free_fib_info(struct fib_info *fi) |
| 249 | { |
| 250 | if (fi->fib_dead == 0) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 251 | pr_warn("Freeing alive fib_info %p\n", fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return; |
| 253 | } |
David Ahern | faa041a | 2019-03-27 20:53:49 -0700 | [diff] [blame] | 254 | |
Yan, Zheng | 19c1ea1 | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 255 | call_rcu(&fi->rcu, free_fib_info_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Ido Schimmel | b423cb1 | 2016-12-03 16:44:58 +0100 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(free_fib_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | void fib_release_info(struct fib_info *fi) |
| 260 | { |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 261 | spin_lock_bh(&fib_info_lock); |
Yajun Deng | 7997689 | 2021-07-29 15:13:50 +0800 | [diff] [blame] | 262 | if (fi && refcount_dec_and_test(&fi->fib_treeref)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | hlist_del(&fi->fib_hash); |
Eric Dumazet | 0a6e6b3 | 2022-01-16 01:02:20 -0800 | [diff] [blame] | 264 | |
| 265 | /* Paired with READ_ONCE() in fib_create_info(). */ |
| 266 | WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1); |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (fi->fib_prefsrc) |
| 269 | hlist_del(&fi->fib_lhash); |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 270 | if (fi->nh) { |
| 271 | list_del(&fi->nh_list); |
| 272 | } else { |
| 273 | change_nexthops(fi) { |
| 274 | if (!nexthop_nh->fib_nh_dev) |
| 275 | continue; |
| 276 | hlist_del(&nexthop_nh->nh_hash); |
| 277 | } endfor_nexthops(fi) |
| 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | fi->fib_dead = 1; |
| 280 | fib_info_put(fi); |
| 281 | } |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 282 | spin_unlock_bh(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 285 | static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 287 | const struct fib_nh *onh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 289 | if (fi->nh || ofi->nh) |
| 290 | return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1; |
| 291 | |
| 292 | if (ofi->fib_nhs == 0) |
| 293 | return 0; |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | for_nexthops(fi) { |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 296 | onh = fib_info_nh(ofi, nhsel); |
| 297 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 298 | if (nh->fib_nh_oif != onh->fib_nh_oif || |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 299 | nh->fib_nh_gw_family != onh->fib_nh_gw_family || |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 300 | nh->fib_nh_scope != onh->fib_nh_scope || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 302 | nh->fib_nh_weight != onh->fib_nh_weight || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | #endif |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 304 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | nh->nh_tclassid != onh->nh_tclassid || |
| 306 | #endif |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 307 | lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) || |
| 308 | ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return -1; |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 310 | |
| 311 | if (nh->fib_nh_gw_family == AF_INET && |
| 312 | nh->fib_nh_gw4 != onh->fib_nh_gw4) |
| 313 | return -1; |
| 314 | |
| 315 | if (nh->fib_nh_gw_family == AF_INET6 && |
| 316 | ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6)) |
| 317 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } endfor_nexthops(fi); |
| 319 | return 0; |
| 320 | } |
| 321 | |
David S. Miller | 88ebc72 | 2008-01-12 21:49:01 -0800 | [diff] [blame] | 322 | static inline unsigned int fib_devindex_hashfn(unsigned int val) |
| 323 | { |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 324 | return hash_32(val, DEVINDEX_HASHBITS); |
| 325 | } |
David S. Miller | 88ebc72 | 2008-01-12 21:49:01 -0800 | [diff] [blame] | 326 | |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 327 | static struct hlist_head * |
| 328 | fib_info_devhash_bucket(const struct net_device *dev) |
| 329 | { |
| 330 | u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex; |
| 331 | |
| 332 | return &fib_info_devhash[fib_devindex_hashfn(val)]; |
David S. Miller | 88ebc72 | 2008-01-12 21:49:01 -0800 | [diff] [blame] | 333 | } |
| 334 | |
David Ahern | 6c48ea5 | 2019-06-08 14:53:33 -0700 | [diff] [blame] | 335 | static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope, |
| 336 | u32 prefsrc, u32 priority) |
| 337 | { |
| 338 | unsigned int val = init_val; |
| 339 | |
| 340 | val ^= (protocol << 8) | scope; |
| 341 | val ^= prefsrc; |
| 342 | val ^= priority; |
| 343 | |
| 344 | return val; |
| 345 | } |
| 346 | |
| 347 | static unsigned int fib_info_hashfn_result(unsigned int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 349 | unsigned int mask = (fib_info_hash_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
David Ahern | 6c48ea5 | 2019-06-08 14:53:33 -0700 | [diff] [blame] | 351 | return (val ^ (val >> 7) ^ (val >> 12)) & mask; |
| 352 | } |
| 353 | |
| 354 | static inline unsigned int fib_info_hashfn(struct fib_info *fi) |
| 355 | { |
| 356 | unsigned int val; |
| 357 | |
| 358 | val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol, |
| 359 | fi->fib_scope, (__force u32)fi->fib_prefsrc, |
| 360 | fi->fib_priority); |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 361 | |
| 362 | if (fi->nh) { |
| 363 | val ^= fib_devindex_hashfn(fi->nh->id); |
| 364 | } else { |
| 365 | for_nexthops(fi) { |
| 366 | val ^= fib_devindex_hashfn(nh->fib_nh_oif); |
| 367 | } endfor_nexthops(fi) |
| 368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
David Ahern | 6c48ea5 | 2019-06-08 14:53:33 -0700 | [diff] [blame] | 370 | return fib_info_hashfn_result(val); |
| 371 | } |
| 372 | |
| 373 | /* no metrics, only nexthop id */ |
| 374 | static struct fib_info *fib_find_info_nh(struct net *net, |
| 375 | const struct fib_config *cfg) |
| 376 | { |
| 377 | struct hlist_head *head; |
| 378 | struct fib_info *fi; |
| 379 | unsigned int hash; |
| 380 | |
| 381 | hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id), |
| 382 | cfg->fc_protocol, cfg->fc_scope, |
| 383 | (__force u32)cfg->fc_prefsrc, |
| 384 | cfg->fc_priority); |
| 385 | hash = fib_info_hashfn_result(hash); |
| 386 | head = &fib_info_hash[hash]; |
| 387 | |
| 388 | hlist_for_each_entry(fi, head, fib_hash) { |
| 389 | if (!net_eq(fi->fib_net, net)) |
| 390 | continue; |
| 391 | if (!fi->nh || fi->nh->id != cfg->fc_nh_id) |
| 392 | continue; |
| 393 | if (cfg->fc_protocol == fi->fib_protocol && |
| 394 | cfg->fc_scope == fi->fib_scope && |
| 395 | cfg->fc_prefsrc == fi->fib_prefsrc && |
| 396 | cfg->fc_priority == fi->fib_priority && |
| 397 | cfg->fc_type == fi->fib_type && |
| 398 | cfg->fc_table == fi->fib_tb_id && |
| 399 | !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK)) |
| 400 | return fi; |
| 401 | } |
| 402 | |
| 403 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 406 | static struct fib_info *fib_find_info(struct fib_info *nfi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | struct fib_info *fi; |
| 410 | unsigned int hash; |
| 411 | |
| 412 | hash = fib_info_hashfn(nfi); |
| 413 | head = &fib_info_hash[hash]; |
| 414 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 415 | hlist_for_each_entry(fi, head, fib_hash) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 416 | if (!net_eq(fi->fib_net, nfi->fib_net)) |
Denis V. Lunev | 4814bdb | 2008-01-31 18:50:07 -0800 | [diff] [blame] | 417 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | if (fi->fib_nhs != nfi->fib_nhs) |
| 419 | continue; |
| 420 | if (nfi->fib_protocol == fi->fib_protocol && |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 421 | nfi->fib_scope == fi->fib_scope && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | nfi->fib_prefsrc == fi->fib_prefsrc && |
| 423 | nfi->fib_priority == fi->fib_priority && |
Eric Dumazet | f4ef85b | 2012-10-04 01:25:26 +0000 | [diff] [blame] | 424 | nfi->fib_type == fi->fib_type && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | memcmp(nfi->fib_metrics, fi->fib_metrics, |
Eric Dumazet | fcd13f4 | 2011-03-24 07:01:24 +0000 | [diff] [blame] | 426 | sizeof(u32) * RTAX_MAX) == 0 && |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 427 | !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) && |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 428 | nh_comp(fi, nfi) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | return fi; |
| 430 | } |
| 431 | |
| 432 | return NULL; |
| 433 | } |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* Check, that the gateway is already configured. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 436 | * Used only by redirect accept routine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | */ |
Al Viro | d878e72e | 2006-09-26 22:18:13 -0700 | [diff] [blame] | 438 | int ip_fib_check_default(__be32 gw, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | struct fib_nh *nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 443 | spin_lock(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 445 | head = fib_info_devhash_bucket(dev); |
| 446 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 447 | hlist_for_each_entry(nh, head, nh_hash) { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 448 | if (nh->fib_nh_dev == dev && |
| 449 | nh->fib_nh_gw4 == gw && |
| 450 | !(nh->fib_nh_flags & RTNH_F_DEAD)) { |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 451 | spin_unlock(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | } |
| 455 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 456 | spin_unlock(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | return -1; |
| 459 | } |
| 460 | |
Amit Cohen | 1e7bdec | 2021-02-01 21:47:51 +0200 | [diff] [blame] | 461 | size_t fib_nlmsg_size(struct fib_info *fi) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 462 | { |
| 463 | size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 464 | + nla_total_size(4) /* RTA_TABLE */ |
| 465 | + nla_total_size(4) /* RTA_DST */ |
| 466 | + nla_total_size(4) /* RTA_PRIORITY */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 467 | + nla_total_size(4) /* RTA_PREFSRC */ |
| 468 | + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */ |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 469 | unsigned int nhs = fib_info_num_path(fi); |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 470 | |
| 471 | /* space for nested metrics */ |
| 472 | payload += nla_total_size((RTAX_MAX * nla_total_size(4))); |
| 473 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 474 | if (fi->nh) |
| 475 | payload += nla_total_size(4); /* RTA_NH_ID */ |
| 476 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 477 | if (nhs) { |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 478 | size_t nh_encapsize = 0; |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 479 | /* Also handles the special case nhs == 1 */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 480 | |
| 481 | /* each nexthop is packed in an attribute */ |
| 482 | size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 483 | unsigned int i; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 484 | |
| 485 | /* may contain flow and gateway attribute */ |
| 486 | nhsize += 2 * nla_total_size(4); |
| 487 | |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 488 | /* grab encap info */ |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 489 | for (i = 0; i < fib_info_num_path(fi); i++) { |
| 490 | struct fib_nh_common *nhc = fib_info_nhc(fi, i); |
| 491 | |
| 492 | if (nhc->nhc_lwtstate) { |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 493 | /* RTA_ENCAP_TYPE */ |
| 494 | nh_encapsize += lwtunnel_get_encap_size( |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 495 | nhc->nhc_lwtstate); |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 496 | /* RTA_ENCAP */ |
| 497 | nh_encapsize += nla_total_size(2); |
| 498 | } |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 499 | } |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 500 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 501 | /* all nexthops are packed in a nested attribute */ |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 502 | payload += nla_total_size((nhs * nhsize) + nh_encapsize); |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 503 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | return payload; |
| 507 | } |
| 508 | |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 509 | void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, |
Joe Perches | 9877b25 | 2013-10-17 13:34:11 -0700 | [diff] [blame] | 510 | int dst_len, u32 tb_id, const struct nl_info *info, |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 511 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 513 | struct fib_rt_info fri; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | struct sk_buff *skb; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 515 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 516 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 518 | skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 519 | if (!skb) |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 520 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 522 | fri.fi = fa->fa_info; |
| 523 | fri.tb_id = tb_id; |
| 524 | fri.dst = key; |
| 525 | fri.dst_len = dst_len; |
| 526 | fri.tos = fa->fa_tos; |
| 527 | fri.type = fa->fa_type; |
Ido Schimmel | 90b93f1 | 2020-01-14 13:23:11 +0200 | [diff] [blame] | 528 | fri.offload = fa->offload; |
| 529 | fri.trap = fa->trap; |
Amit Cohen | 36c5100 | 2021-02-07 10:22:50 +0200 | [diff] [blame] | 530 | fri.offload_failed = fa->offload_failed; |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 531 | err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 532 | if (err < 0) { |
| 533 | /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ |
| 534 | WARN_ON(err == -EMSGSIZE); |
| 535 | kfree_skb(skb); |
| 536 | goto errout; |
| 537 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 538 | rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 539 | info->nlh, GFP_KERNEL); |
| 540 | return; |
Thomas Graf | f21c7bc | 2006-08-15 00:34:17 -0700 | [diff] [blame] | 541 | errout: |
| 542 | if (err < 0) |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 543 | rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Stephen Hemminger | c9cb6b6 | 2013-12-28 11:05:36 -0800 | [diff] [blame] | 546 | static int fib_detect_death(struct fib_info *fi, int order, |
| 547 | struct fib_info **last_resort, int *last_idx, |
| 548 | int dflt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | { |
David Ahern | 619d182 | 2019-04-05 16:30:37 -0700 | [diff] [blame] | 550 | const struct fib_nh_common *nhc = fib_info_nhc(fi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | struct neighbour *n; |
| 552 | int state = NUD_NONE; |
| 553 | |
David Ahern | 619d182 | 2019-04-05 16:30:37 -0700 | [diff] [blame] | 554 | if (likely(nhc->nhc_gw_family == AF_INET)) |
| 555 | n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev); |
| 556 | else if (nhc->nhc_gw_family == AF_INET6) |
| 557 | n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6, |
| 558 | nhc->nhc_dev); |
| 559 | else |
| 560 | n = NULL; |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | if (n) { |
| 563 | state = n->nud_state; |
| 564 | neigh_release(n); |
Julian Anastasov | 88f6432 | 2015-07-23 10:39:35 +0300 | [diff] [blame] | 565 | } else { |
| 566 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Jianjun Kong | d9319100 | 2008-11-03 00:23:42 -0800 | [diff] [blame] | 568 | if (state == NUD_REACHABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return 0; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 570 | if ((state & NUD_VALID) && order != dflt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return 0; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 572 | if ((state & NUD_VALID) || |
Julian Anastasov | 88f6432 | 2015-07-23 10:39:35 +0300 | [diff] [blame] | 573 | (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | *last_resort = fi; |
| 575 | *last_idx = order; |
| 576 | } |
| 577 | return 1; |
| 578 | } |
| 579 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 580 | int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc, |
| 581 | struct nlattr *encap, u16 encap_type, |
| 582 | void *cfg, gfp_t gfp_flags, |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 583 | struct netlink_ext_ack *extack) |
| 584 | { |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 585 | int err; |
| 586 | |
| 587 | nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *, |
| 588 | gfp_flags); |
| 589 | if (!nhc->nhc_pcpu_rth_output) |
| 590 | return -ENOMEM; |
| 591 | |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 592 | if (encap) { |
| 593 | struct lwtunnel_state *lwtstate; |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 594 | |
| 595 | if (encap_type == LWTUNNEL_ENCAP_NONE) { |
| 596 | NL_SET_ERR_MSG(extack, "LWT encap type not specified"); |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 597 | err = -EINVAL; |
| 598 | goto lwt_failure; |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 599 | } |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 600 | err = lwtunnel_build_state(net, encap_type, encap, |
| 601 | nhc->nhc_family, cfg, &lwtstate, |
| 602 | extack); |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 603 | if (err) |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 604 | goto lwt_failure; |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 605 | |
| 606 | nhc->nhc_lwtstate = lwtstate_get(lwtstate); |
| 607 | } |
| 608 | |
| 609 | return 0; |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 610 | |
| 611 | lwt_failure: |
| 612 | rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output); |
| 613 | nhc->nhc_pcpu_rth_output = NULL; |
| 614 | return err; |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 615 | } |
| 616 | EXPORT_SYMBOL_GPL(fib_nh_common_init); |
| 617 | |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 618 | int fib_nh_init(struct net *net, struct fib_nh *nh, |
| 619 | struct fib_config *cfg, int nh_weight, |
| 620 | struct netlink_ext_ack *extack) |
| 621 | { |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 622 | int err; |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 623 | |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 624 | nh->fib_nh_family = AF_INET; |
| 625 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 626 | err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap, |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 627 | cfg->fc_encap_type, cfg, GFP_KERNEL, extack); |
| 628 | if (err) |
David Ahern | 0f457a3 | 2019-04-30 07:45:48 -0700 | [diff] [blame] | 629 | return err; |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 630 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 631 | nh->fib_nh_oif = cfg->fc_oif; |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 632 | nh->fib_nh_gw_family = cfg->fc_gw_family; |
| 633 | if (cfg->fc_gw_family == AF_INET) |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 634 | nh->fib_nh_gw4 = cfg->fc_gw4; |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 635 | else if (cfg->fc_gw_family == AF_INET6) |
| 636 | nh->fib_nh_gw6 = cfg->fc_gw6; |
| 637 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 638 | nh->fib_nh_flags = cfg->fc_flags; |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 639 | |
| 640 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 641 | nh->nh_tclassid = cfg->fc_flow; |
| 642 | if (nh->nh_tclassid) |
Eric Dumazet | 213f5f8 | 2021-12-01 18:26:35 -0800 | [diff] [blame] | 643 | atomic_inc(&net->ipv4.fib_num_tclassid_users); |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 644 | #endif |
| 645 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 646 | nh->fib_nh_weight = nh_weight; |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 647 | #endif |
| 648 | return 0; |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
| 652 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 653 | static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining, |
| 654 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
| 656 | int nhs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 658 | while (rtnh_ok(rtnh, remaining)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | nhs++; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 660 | rtnh = rtnh_next(rtnh, &remaining); |
| 661 | } |
| 662 | |
| 663 | /* leftover implies invalid nexthop configuration, discard it */ |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 664 | if (remaining > 0) { |
| 665 | NL_SET_ERR_MSG(extack, |
| 666 | "Invalid nexthop configuration - extra data after nexthops"); |
| 667 | nhs = 0; |
| 668 | } |
| 669 | |
| 670 | return nhs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
David Ahern | 7a3429b | 2021-12-30 17:36:31 -0700 | [diff] [blame] | 673 | static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla, |
| 674 | struct netlink_ext_ack *extack) |
| 675 | { |
| 676 | if (nla_len(nla) < sizeof(*gw)) { |
| 677 | NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY"); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | |
| 681 | *gw = nla_get_in_addr(nla); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 686 | /* only called when fib_nh is integrated into fib_info */ |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 687 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 688 | int remaining, struct fib_config *cfg, |
| 689 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 691 | struct net *net = fi->fib_net; |
| 692 | struct fib_config fib_cfg; |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 693 | struct fib_nh *nh; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 694 | int ret; |
| 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | change_nexthops(fi) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 697 | int attrlen; |
| 698 | |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 699 | memset(&fib_cfg, 0, sizeof(fib_cfg)); |
| 700 | |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 701 | if (!rtnh_ok(rtnh, remaining)) { |
| 702 | NL_SET_ERR_MSG(extack, |
| 703 | "Invalid nexthop configuration - extra data after nexthop"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return -EINVAL; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 705 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 706 | |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 707 | if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) { |
| 708 | NL_SET_ERR_MSG(extack, |
| 709 | "Invalid flags for nexthop - can not contain DEAD or LINKDOWN"); |
Julian Anastasov | 8061022 | 2016-07-10 21:11:55 +0300 | [diff] [blame] | 710 | return -EINVAL; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 711 | } |
Julian Anastasov | 8061022 | 2016-07-10 21:11:55 +0300 | [diff] [blame] | 712 | |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 713 | fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; |
| 714 | fib_cfg.fc_oif = rtnh->rtnh_ifindex; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 715 | |
| 716 | attrlen = rtnh_attrlen(rtnh); |
| 717 | if (attrlen > 0) { |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 718 | struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 719 | |
| 720 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 721 | nlav = nla_find(attrs, attrlen, RTA_VIA); |
| 722 | if (nla && nlav) { |
| 723 | NL_SET_ERR_MSG(extack, |
| 724 | "Nexthop configuration can not contain both GATEWAY and VIA"); |
| 725 | return -EINVAL; |
| 726 | } |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 727 | if (nla) { |
David Ahern | 7a3429b | 2021-12-30 17:36:31 -0700 | [diff] [blame] | 728 | ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla, |
| 729 | extack); |
| 730 | if (ret) |
| 731 | goto errout; |
| 732 | |
David Ahern | d73f80f | 2019-04-10 10:05:51 -0700 | [diff] [blame] | 733 | if (fib_cfg.fc_gw4) |
| 734 | fib_cfg.fc_gw_family = AF_INET; |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 735 | } else if (nlav) { |
| 736 | ret = fib_gw_from_via(&fib_cfg, nlav, extack); |
| 737 | if (ret) |
| 738 | goto errout; |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 739 | } |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 740 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 741 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
David Ahern | 664b9c4 | 2021-12-30 17:36:32 -0700 | [diff] [blame] | 742 | if (nla) { |
| 743 | if (nla_len(nla) < sizeof(u32)) { |
| 744 | NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW"); |
| 745 | return -EINVAL; |
| 746 | } |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 747 | fib_cfg.fc_flow = nla_get_u32(nla); |
David Ahern | 664b9c4 | 2021-12-30 17:36:32 -0700 | [diff] [blame] | 748 | } |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 749 | |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 750 | fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
David Ahern | 8bda81a | 2021-12-30 17:36:35 -0700 | [diff] [blame] | 751 | /* RTA_ENCAP_TYPE length checked in |
| 752 | * lwtunnel_valid_encap_type_attr |
| 753 | */ |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 754 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 755 | if (nla) |
| 756 | fib_cfg.fc_encap_type = nla_get_u16(nla); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 758 | |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 759 | ret = fib_nh_init(net, nexthop_nh, &fib_cfg, |
| 760 | rtnh->rtnh_hops + 1, extack); |
| 761 | if (ret) |
| 762 | goto errout; |
| 763 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 764 | rtnh = rtnh_next(rtnh, &remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } endfor_nexthops(fi); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 766 | |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 767 | ret = -EINVAL; |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 768 | nh = fib_info_nh(fi, 0); |
| 769 | if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) { |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 770 | NL_SET_ERR_MSG(extack, |
| 771 | "Nexthop device index does not match RTA_OIF"); |
| 772 | goto errout; |
| 773 | } |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 774 | if (cfg->fc_gw_family) { |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 775 | if (cfg->fc_gw_family != nh->fib_nh_gw_family || |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 776 | (cfg->fc_gw_family == AF_INET && |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 777 | nh->fib_nh_gw4 != cfg->fc_gw4) || |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 778 | (cfg->fc_gw_family == AF_INET6 && |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 779 | ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) { |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 780 | NL_SET_ERR_MSG(extack, |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 781 | "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA"); |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 782 | goto errout; |
| 783 | } |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 784 | } |
| 785 | #ifdef CONFIG_IP_ROUTE_CLASSID |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 786 | if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) { |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 787 | NL_SET_ERR_MSG(extack, |
| 788 | "Nexthop class id does not match RTA_FLOW"); |
| 789 | goto errout; |
| 790 | } |
| 791 | #endif |
| 792 | ret = 0; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 793 | errout: |
| 794 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
| 796 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 797 | /* only called when fib_nh is integrated into fib_info */ |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 798 | static void fib_rebalance(struct fib_info *fi) |
| 799 | { |
| 800 | int total; |
| 801 | int w; |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 802 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 803 | if (fib_info_num_path(fi) < 2) |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 804 | return; |
| 805 | |
| 806 | total = 0; |
| 807 | for_nexthops(fi) { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 808 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 809 | continue; |
| 810 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 811 | if (ip_ignore_linkdown(nh->fib_nh_dev) && |
| 812 | nh->fib_nh_flags & RTNH_F_LINKDOWN) |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 813 | continue; |
| 814 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 815 | total += nh->fib_nh_weight; |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 816 | } endfor_nexthops(fi); |
| 817 | |
| 818 | w = 0; |
| 819 | change_nexthops(fi) { |
| 820 | int upper_bound; |
| 821 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 822 | if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) { |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 823 | upper_bound = -1; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 824 | } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) && |
| 825 | nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) { |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 826 | upper_bound = -1; |
| 827 | } else { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 828 | w += nexthop_nh->fib_nh_weight; |
Peter Nørlund | 0a837fe | 2015-10-06 07:24:47 +0200 | [diff] [blame] | 829 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, |
| 830 | total) - 1; |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 831 | } |
| 832 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 833 | atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound); |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 834 | } endfor_nexthops(fi); |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 835 | } |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 836 | #else /* CONFIG_IP_ROUTE_MULTIPATH */ |
| 837 | |
David Ahern | 8373c6c | 2019-03-27 20:53:46 -0700 | [diff] [blame] | 838 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
| 839 | int remaining, struct fib_config *cfg, |
| 840 | struct netlink_ext_ack *extack) |
| 841 | { |
| 842 | NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel"); |
| 843 | |
| 844 | return -EINVAL; |
| 845 | } |
| 846 | |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 847 | #define fib_rebalance(fi) do { } while (0) |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 848 | |
| 849 | #endif /* CONFIG_IP_ROUTE_MULTIPATH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 851 | static int fib_encap_match(struct net *net, u16 encap_type, |
Ying Xue | e01286e | 2015-08-19 16:04:51 +0800 | [diff] [blame] | 852 | struct nlattr *encap, |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 853 | const struct fib_nh *nh, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 854 | const struct fib_config *cfg, |
| 855 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 856 | { |
| 857 | struct lwtunnel_state *lwtstate; |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 858 | int ret, result = 0; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 859 | |
| 860 | if (encap_type == LWTUNNEL_ENCAP_NONE) |
| 861 | return 0; |
| 862 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 863 | ret = lwtunnel_build_state(net, encap_type, encap, AF_INET, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 864 | cfg, &lwtstate, extack); |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 865 | if (!ret) { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 866 | result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws); |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 867 | lwtstate_free(lwtstate); |
| 868 | } |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 869 | |
Jiri Benc | df383e6 | 2015-08-18 18:41:13 +0200 | [diff] [blame] | 870 | return result; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 873 | int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 874 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
| 876 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 877 | struct rtnexthop *rtnh; |
| 878 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | #endif |
| 880 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 881 | if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | return 1; |
| 883 | |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 884 | if (cfg->fc_nh_id) { |
| 885 | if (fi->nh && cfg->fc_nh_id == fi->nh->id) |
| 886 | return 0; |
| 887 | return 1; |
| 888 | } |
| 889 | |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 890 | if (cfg->fc_oif || cfg->fc_gw_family) { |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 891 | struct fib_nh *nh = fib_info_nh(fi, 0); |
| 892 | |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 893 | if (cfg->fc_encap) { |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 894 | if (fib_encap_match(net, cfg->fc_encap_type, |
| 895 | cfg->fc_encap, nh, cfg, extack)) |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 896 | return 1; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 897 | } |
Stefano Brivio | a8c6db1 | 2018-02-15 09:46:03 +0100 | [diff] [blame] | 898 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 899 | if (cfg->fc_flow && |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 900 | cfg->fc_flow != nh->nh_tclassid) |
Stefano Brivio | a8c6db1 | 2018-02-15 09:46:03 +0100 | [diff] [blame] | 901 | return 1; |
| 902 | #endif |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 903 | if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) || |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 904 | (cfg->fc_gw_family && |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 905 | cfg->fc_gw_family != nh->fib_nh_gw_family)) |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 906 | return 1; |
| 907 | |
| 908 | if (cfg->fc_gw_family == AF_INET && |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 909 | cfg->fc_gw4 != nh->fib_nh_gw4) |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 910 | return 1; |
| 911 | |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 912 | if (cfg->fc_gw_family == AF_INET6 && |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 913 | ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6)) |
David Ahern | a4ea5d4 | 2019-04-05 16:30:30 -0700 | [diff] [blame] | 914 | return 1; |
| 915 | |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 916 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 920 | if (!cfg->fc_mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return 0; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 922 | |
| 923 | rtnh = cfg->fc_mp; |
| 924 | remaining = cfg->fc_mp_len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | for_nexthops(fi) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 927 | int attrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 929 | if (!rtnh_ok(rtnh, remaining)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | return -EINVAL; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 931 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 932 | if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return 1; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 934 | |
| 935 | attrlen = rtnh_attrlen(rtnh); |
Jiri Pirko | f76936d | 2014-10-13 16:34:10 +0200 | [diff] [blame] | 936 | if (attrlen > 0) { |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 937 | struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh); |
David Ahern | 7a3429b | 2021-12-30 17:36:31 -0700 | [diff] [blame] | 938 | int err; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 939 | |
| 940 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 941 | nlav = nla_find(attrs, attrlen, RTA_VIA); |
| 942 | if (nla && nlav) { |
| 943 | NL_SET_ERR_MSG(extack, |
| 944 | "Nexthop configuration can not contain both GATEWAY and VIA"); |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | |
| 948 | if (nla) { |
David Ahern | 7a3429b | 2021-12-30 17:36:31 -0700 | [diff] [blame] | 949 | __be32 gw; |
| 950 | |
| 951 | err = fib_gw_from_attr(&gw, nla, extack); |
| 952 | if (err) |
| 953 | return err; |
| 954 | |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 955 | if (nh->fib_nh_gw_family != AF_INET || |
David Ahern | 7a3429b | 2021-12-30 17:36:31 -0700 | [diff] [blame] | 956 | gw != nh->fib_nh_gw4) |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 957 | return 1; |
| 958 | } else if (nlav) { |
| 959 | struct fib_config cfg2; |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 960 | |
| 961 | err = fib_gw_from_via(&cfg2, nlav, extack); |
| 962 | if (err) |
| 963 | return err; |
| 964 | |
| 965 | switch (nh->fib_nh_gw_family) { |
| 966 | case AF_INET: |
| 967 | if (cfg2.fc_gw_family != AF_INET || |
| 968 | cfg2.fc_gw4 != nh->fib_nh_gw4) |
| 969 | return 1; |
| 970 | break; |
| 971 | case AF_INET6: |
| 972 | if (cfg2.fc_gw_family != AF_INET6 || |
| 973 | ipv6_addr_cmp(&cfg2.fc_gw6, |
| 974 | &nh->fib_nh_gw6)) |
| 975 | return 1; |
| 976 | break; |
| 977 | } |
| 978 | } |
| 979 | |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 980 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 981 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
David Ahern | 664b9c4 | 2021-12-30 17:36:32 -0700 | [diff] [blame] | 982 | if (nla) { |
| 983 | if (nla_len(nla) < sizeof(u32)) { |
| 984 | NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW"); |
| 985 | return -EINVAL; |
| 986 | } |
| 987 | if (nla_get_u32(nla) != nh->nh_tclassid) |
| 988 | return 1; |
| 989 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | #endif |
| 991 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 992 | |
| 993 | rtnh = rtnh_next(rtnh, &remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } endfor_nexthops(fi); |
| 995 | #endif |
| 996 | return 0; |
| 997 | } |
| 998 | |
Xin Long | 5f9ae3d | 2017-08-23 10:07:26 +0800 | [diff] [blame] | 999 | bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi) |
| 1000 | { |
| 1001 | struct nlattr *nla; |
| 1002 | int remaining; |
| 1003 | |
| 1004 | if (!cfg->fc_mx) |
| 1005 | return true; |
| 1006 | |
| 1007 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { |
| 1008 | int type = nla_type(nla); |
Phil Sutter | d03a455 | 2017-12-19 15:17:13 +0100 | [diff] [blame] | 1009 | u32 fi_val, val; |
Xin Long | 5f9ae3d | 2017-08-23 10:07:26 +0800 | [diff] [blame] | 1010 | |
| 1011 | if (!type) |
| 1012 | continue; |
| 1013 | if (type > RTAX_MAX) |
| 1014 | return false; |
| 1015 | |
| 1016 | if (type == RTAX_CC_ALGO) { |
| 1017 | char tmp[TCP_CA_NAME_MAX]; |
| 1018 | bool ecn_ca = false; |
| 1019 | |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 1020 | nla_strscpy(tmp, nla, sizeof(tmp)); |
Stephen Hemminger | 6670e15 | 2017-11-14 08:25:49 -0800 | [diff] [blame] | 1021 | val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca); |
Xin Long | 5f9ae3d | 2017-08-23 10:07:26 +0800 | [diff] [blame] | 1022 | } else { |
Eric Dumazet | 5b5e7a0 | 2018-06-05 06:06:19 -0700 | [diff] [blame] | 1023 | if (nla_len(nla) != sizeof(u32)) |
| 1024 | return false; |
Xin Long | 5f9ae3d | 2017-08-23 10:07:26 +0800 | [diff] [blame] | 1025 | val = nla_get_u32(nla); |
| 1026 | } |
| 1027 | |
Phil Sutter | d03a455 | 2017-12-19 15:17:13 +0100 | [diff] [blame] | 1028 | fi_val = fi->fib_metrics->metrics[type - 1]; |
| 1029 | if (type == RTAX_FEATURES) |
| 1030 | fi_val &= ~DST_FEATURE_ECN_CA; |
| 1031 | |
| 1032 | if (fi_val != val) |
Xin Long | 5f9ae3d | 2017-08-23 10:07:26 +0800 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
David Ahern | 717a8f5 | 2019-04-05 16:30:32 -0700 | [diff] [blame] | 1039 | static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh, |
| 1040 | u32 table, struct netlink_ext_ack *extack) |
| 1041 | { |
| 1042 | struct fib6_config cfg = { |
| 1043 | .fc_table = table, |
| 1044 | .fc_flags = nh->fib_nh_flags | RTF_GATEWAY, |
| 1045 | .fc_ifindex = nh->fib_nh_oif, |
| 1046 | .fc_gateway = nh->fib_nh_gw6, |
| 1047 | }; |
| 1048 | struct fib6_nh fib6_nh = {}; |
| 1049 | int err; |
| 1050 | |
| 1051 | err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack); |
| 1052 | if (!err) { |
| 1053 | nh->fib_nh_dev = fib6_nh.fib_nh_dev; |
Eric Dumazet | e44b14e | 2021-12-06 17:30:32 -0800 | [diff] [blame] | 1054 | dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_KERNEL); |
David Ahern | 717a8f5 | 2019-04-05 16:30:32 -0700 | [diff] [blame] | 1055 | nh->fib_nh_oif = nh->fib_nh_dev->ifindex; |
| 1056 | nh->fib_nh_scope = RT_SCOPE_LINK; |
| 1057 | |
| 1058 | ipv6_stub->fib6_nh_release(&fib6_nh); |
| 1059 | } |
| 1060 | |
| 1061 | return err; |
| 1062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1065 | * Picture |
| 1066 | * ------- |
| 1067 | * |
| 1068 | * Semantics of nexthop is very messy by historical reasons. |
| 1069 | * We have to take into account, that: |
| 1070 | * a) gateway can be actually local interface address, |
| 1071 | * so that gatewayed route is direct. |
| 1072 | * b) gateway must be on-link address, possibly |
| 1073 | * described not by an ifaddr, but also by a direct route. |
| 1074 | * c) If both gateway and interface are specified, they should not |
| 1075 | * contradict. |
| 1076 | * d) If we use tunnel routes, gateway could be not on-link. |
| 1077 | * |
| 1078 | * Attempt to reconcile all of these (alas, self-contradictory) conditions |
| 1079 | * results in pretty ugly and hairy code with obscure logic. |
| 1080 | * |
| 1081 | * I chose to generalized it instead, so that the size |
| 1082 | * of code does not increase practically, but it becomes |
| 1083 | * much more general. |
| 1084 | * Every prefix is assigned a "scope" value: "host" is local address, |
| 1085 | * "link" is direct route, |
| 1086 | * [ ... "site" ... "interior" ... ] |
| 1087 | * and "universe" is true gateway route with global meaning. |
| 1088 | * |
| 1089 | * Every prefix refers to a set of "nexthop"s (gw, oif), |
| 1090 | * where gw must have narrower scope. This recursion stops |
| 1091 | * when gw has LOCAL scope or if "nexthop" is declared ONLINK, |
| 1092 | * which means that gw is forced to be on link. |
| 1093 | * |
| 1094 | * Code is still hairy, but now it is apparently logically |
| 1095 | * consistent and very flexible. F.e. as by-product it allows |
| 1096 | * to co-exists in peace independent exterior and interior |
| 1097 | * routing processes. |
| 1098 | * |
| 1099 | * Normally it looks as following. |
| 1100 | * |
| 1101 | * {universe prefix} -> (gw, oif) [scope link] |
| 1102 | * | |
| 1103 | * |-> {link prefix} -> (gw, oif) [scope local] |
| 1104 | * | |
| 1105 | * |-> {local prefix} (terminal node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | */ |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1107 | static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table, |
| 1108 | u8 scope, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | { |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1110 | struct net_device *dev; |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1111 | struct fib_result res; |
Enrico Weigelt | c3fee64 | 2019-06-06 16:43:17 +0200 | [diff] [blame] | 1112 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1114 | if (nh->fib_nh_flags & RTNH_F_ONLINK) { |
| 1115 | unsigned int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1117 | if (scope >= RT_SCOPE_LINK) { |
| 1118 | NL_SET_ERR_MSG(extack, "Nexthop has invalid scope"); |
| 1119 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1121 | dev = __dev_get_by_index(net, nh->fib_nh_oif); |
| 1122 | if (!dev) { |
| 1123 | NL_SET_ERR_MSG(extack, "Nexthop device required for onlink"); |
| 1124 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1126 | if (!(dev->flags & IFF_UP)) { |
| 1127 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 1128 | return -ENETDOWN; |
| 1129 | } |
| 1130 | addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4); |
| 1131 | if (addr_type != RTN_UNICAST) { |
| 1132 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); |
| 1133 | return -EINVAL; |
| 1134 | } |
| 1135 | if (!netif_carrier_ok(dev)) |
| 1136 | nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
| 1137 | nh->fib_nh_dev = dev; |
Eric Dumazet | e44b14e | 2021-12-06 17:30:32 -0800 | [diff] [blame] | 1138 | dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC); |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1139 | nh->fib_nh_scope = RT_SCOPE_LINK; |
| 1140 | return 0; |
| 1141 | } |
| 1142 | rcu_read_lock(); |
| 1143 | { |
| 1144 | struct fib_table *tbl = NULL; |
| 1145 | struct flowi4 fl4 = { |
| 1146 | .daddr = nh->fib_nh_gw4, |
| 1147 | .flowi4_scope = scope + 1, |
| 1148 | .flowi4_oif = nh->fib_nh_oif, |
| 1149 | .flowi4_iif = LOOPBACK_IFINDEX, |
| 1150 | }; |
| 1151 | |
| 1152 | /* It is not necessary, but requires a bit of thinking */ |
| 1153 | if (fl4.flowi4_scope < RT_SCOPE_LINK) |
| 1154 | fl4.flowi4_scope = RT_SCOPE_LINK; |
| 1155 | |
guodeqing | 5eea3a6 | 2020-06-17 10:07:16 +0800 | [diff] [blame] | 1156 | if (table && table != RT_TABLE_MAIN) |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1157 | tbl = fib_get_table(net, table); |
| 1158 | |
| 1159 | if (tbl) |
| 1160 | err = fib_table_lookup(tbl, &fl4, &res, |
| 1161 | FIB_LOOKUP_IGNORE_LINKSTATE | |
| 1162 | FIB_LOOKUP_NOREF); |
| 1163 | |
| 1164 | /* on error or if no table given do full lookup. This |
| 1165 | * is needed for example when nexthops are in the local |
| 1166 | * table rather than the given table |
| 1167 | */ |
| 1168 | if (!tbl || err) { |
| 1169 | err = fib_lookup(net, &fl4, &res, |
| 1170 | FIB_LOOKUP_IGNORE_LINKSTATE); |
| 1171 | } |
| 1172 | |
| 1173 | if (err) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1174 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | goto out; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1178 | |
| 1179 | err = -EINVAL; |
| 1180 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) { |
| 1181 | NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway"); |
| 1182 | goto out; |
| 1183 | } |
| 1184 | nh->fib_nh_scope = res.scope; |
| 1185 | nh->fib_nh_oif = FIB_RES_OIF(res); |
| 1186 | nh->fib_nh_dev = dev = FIB_RES_DEV(res); |
| 1187 | if (!dev) { |
| 1188 | NL_SET_ERR_MSG(extack, |
| 1189 | "No egress device for nexthop gateway"); |
| 1190 | goto out; |
| 1191 | } |
Eric Dumazet | e44b14e | 2021-12-06 17:30:32 -0800 | [diff] [blame] | 1192 | dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC); |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1193 | if (!netif_carrier_ok(dev)) |
| 1194 | nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
| 1195 | err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 1196 | out: |
| 1197 | rcu_read_unlock(); |
| 1198 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1201 | static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh, |
| 1202 | struct netlink_ext_ack *extack) |
| 1203 | { |
| 1204 | struct in_device *in_dev; |
| 1205 | int err; |
| 1206 | |
| 1207 | if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) { |
| 1208 | NL_SET_ERR_MSG(extack, |
| 1209 | "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set"); |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | |
| 1213 | rcu_read_lock(); |
| 1214 | |
| 1215 | err = -ENODEV; |
| 1216 | in_dev = inetdev_by_index(net, nh->fib_nh_oif); |
| 1217 | if (!in_dev) |
| 1218 | goto out; |
| 1219 | err = -ENETDOWN; |
| 1220 | if (!(in_dev->dev->flags & IFF_UP)) { |
| 1221 | NL_SET_ERR_MSG(extack, "Device for nexthop is not up"); |
| 1222 | goto out; |
| 1223 | } |
| 1224 | |
| 1225 | nh->fib_nh_dev = in_dev->dev; |
Eric Dumazet | e44b14e | 2021-12-06 17:30:32 -0800 | [diff] [blame] | 1226 | dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC); |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1227 | nh->fib_nh_scope = RT_SCOPE_HOST; |
| 1228 | if (!netif_carrier_ok(nh->fib_nh_dev)) |
| 1229 | nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
| 1230 | err = 0; |
| 1231 | out: |
| 1232 | rcu_read_unlock(); |
| 1233 | return err; |
| 1234 | } |
| 1235 | |
David Ahern | ac1fab2 | 2019-05-22 12:04:43 -0700 | [diff] [blame] | 1236 | int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope, |
| 1237 | struct netlink_ext_ack *extack) |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1238 | { |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1239 | int err; |
| 1240 | |
| 1241 | if (nh->fib_nh_gw_family == AF_INET) |
David Ahern | ac1fab2 | 2019-05-22 12:04:43 -0700 | [diff] [blame] | 1242 | err = fib_check_nh_v4_gw(net, nh, table, scope, extack); |
David Ahern | 717a8f5 | 2019-04-05 16:30:32 -0700 | [diff] [blame] | 1243 | else if (nh->fib_nh_gw_family == AF_INET6) |
| 1244 | err = fib_check_nh_v6_gw(net, nh, table, extack); |
David Ahern | 448d724 | 2019-04-05 16:30:31 -0700 | [diff] [blame] | 1245 | else |
| 1246 | err = fib_check_nh_nongw(net, nh, extack); |
| 1247 | |
| 1248 | return err; |
| 1249 | } |
| 1250 | |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1251 | static struct hlist_head * |
| 1252 | fib_info_laddrhash_bucket(const struct net *net, __be32 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1254 | u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val, |
| 1255 | fib_info_hash_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1257 | return &fib_info_laddrhash[slot]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1260 | static struct hlist_head *fib_info_hash_alloc(int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
| 1262 | if (bytes <= PAGE_SIZE) |
Joonwoo Park | 88f8349 | 2007-11-26 23:29:32 +0800 | [diff] [blame] | 1263 | return kzalloc(bytes, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | else |
| 1265 | return (struct hlist_head *) |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1266 | __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 1267 | get_order(bytes)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1270 | static void fib_info_hash_free(struct hlist_head *hash, int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | { |
| 1272 | if (!hash) |
| 1273 | return; |
| 1274 | |
| 1275 | if (bytes <= PAGE_SIZE) |
| 1276 | kfree(hash); |
| 1277 | else |
| 1278 | free_pages((unsigned long) hash, get_order(bytes)); |
| 1279 | } |
| 1280 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1281 | static void fib_info_hash_move(struct hlist_head *new_info_hash, |
| 1282 | struct hlist_head *new_laddrhash, |
| 1283 | unsigned int new_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | { |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 1285 | struct hlist_head *old_info_hash, *old_laddrhash; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1286 | unsigned int old_size = fib_info_hash_size; |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 1287 | unsigned int i, bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 1289 | spin_lock_bh(&fib_info_lock); |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 1290 | old_info_hash = fib_info_hash; |
| 1291 | old_laddrhash = fib_info_laddrhash; |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1292 | fib_info_hash_size = new_size; |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1293 | fib_info_hash_bits = ilog2(new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | for (i = 0; i < old_size; i++) { |
| 1296 | struct hlist_head *head = &fib_info_hash[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1297 | struct hlist_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | struct fib_info *fi; |
| 1299 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1300 | hlist_for_each_entry_safe(fi, n, head, fib_hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | struct hlist_head *dest; |
| 1302 | unsigned int new_hash; |
| 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | new_hash = fib_info_hashfn(fi); |
| 1305 | dest = &new_info_hash[new_hash]; |
| 1306 | hlist_add_head(&fi->fib_hash, dest); |
| 1307 | } |
| 1308 | } |
| 1309 | fib_info_hash = new_info_hash; |
| 1310 | |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1311 | fib_info_laddrhash = new_laddrhash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | for (i = 0; i < old_size; i++) { |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1313 | struct hlist_head *lhead = &old_laddrhash[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1314 | struct hlist_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | struct fib_info *fi; |
| 1316 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1317 | hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | struct hlist_head *ldest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1320 | ldest = fib_info_laddrhash_bucket(fi->fib_net, |
| 1321 | fi->fib_prefsrc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | hlist_add_head(&fi->fib_lhash, ldest); |
| 1323 | } |
| 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 1326 | spin_unlock_bh(&fib_info_lock); |
David S. Miller | b7656e7 | 2005-08-05 04:12:48 -0700 | [diff] [blame] | 1327 | |
| 1328 | bytes = old_size * sizeof(struct hlist_head *); |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1329 | fib_info_hash_free(old_info_hash, bytes); |
| 1330 | fib_info_hash_free(old_laddrhash, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1333 | __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc, |
| 1334 | unsigned char scope) |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1335 | { |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1336 | struct fib_nh *nh; |
| 1337 | |
| 1338 | if (nhc->nhc_family != AF_INET) |
| 1339 | return inet_select_addr(nhc->nhc_dev, 0, scope); |
| 1340 | |
| 1341 | nh = container_of(nhc, struct fib_nh, nh_common); |
David Ahern | c366948 | 2019-05-22 12:04:45 -0700 | [diff] [blame] | 1342 | nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope); |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1343 | nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); |
| 1344 | |
| 1345 | return nh->nh_saddr; |
| 1346 | } |
| 1347 | |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 1348 | __be32 fib_result_prefsrc(struct net *net, struct fib_result *res) |
| 1349 | { |
| 1350 | struct fib_nh_common *nhc = res->nhc; |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (res->fi->fib_prefsrc) |
| 1353 | return res->fi->fib_prefsrc; |
| 1354 | |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1355 | if (nhc->nhc_family == AF_INET) { |
| 1356 | struct fib_nh *nh; |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 1357 | |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1358 | nh = container_of(nhc, struct fib_nh, nh_common); |
| 1359 | if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid)) |
| 1360 | return nh->nh_saddr; |
| 1361 | } |
| 1362 | |
| 1363 | return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope); |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1366 | static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc) |
| 1367 | { |
| 1368 | if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || |
| 1369 | fib_prefsrc != cfg->fc_dst) { |
David Ahern | 9b8ff51 | 2015-09-01 14:26:35 -0600 | [diff] [blame] | 1370 | u32 tb_id = cfg->fc_table; |
David Ahern | e1b8d90 | 2015-11-03 15:59:28 -0800 | [diff] [blame] | 1371 | int rc; |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1372 | |
| 1373 | if (tb_id == RT_TABLE_MAIN) |
| 1374 | tb_id = RT_TABLE_LOCAL; |
| 1375 | |
David Ahern | e1b8d90 | 2015-11-03 15:59:28 -0800 | [diff] [blame] | 1376 | rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net, |
| 1377 | fib_prefsrc, tb_id); |
| 1378 | |
| 1379 | if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) { |
| 1380 | rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net, |
| 1381 | fib_prefsrc, RT_TABLE_LOCAL); |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1382 | } |
David Ahern | e1b8d90 | 2015-11-03 15:59:28 -0800 | [diff] [blame] | 1383 | |
| 1384 | if (rc != RTN_LOCAL) |
| 1385 | return false; |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1386 | } |
| 1387 | return true; |
| 1388 | } |
| 1389 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 1390 | struct fib_info *fib_create_info(struct fib_config *cfg, |
| 1391 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | { |
| 1393 | int err; |
| 1394 | struct fib_info *fi = NULL; |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1395 | struct nexthop *nh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | struct fib_info *ofi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | int nhs = 1; |
Denis V. Lunev | 7462bd74 | 2008-01-31 18:49:32 -0800 | [diff] [blame] | 1398 | struct net *net = cfg->fc_nlinfo.nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
David S. Miller | 4c8237c | 2011-03-07 14:27:38 -0800 | [diff] [blame] | 1400 | if (cfg->fc_type > RTN_MAX) |
| 1401 | goto err_inval; |
| 1402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | /* Fast check to catch the most weird cases */ |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1404 | if (fib_props[cfg->fc_type].scope > cfg->fc_scope) { |
| 1405 | NL_SET_ERR_MSG(extack, "Invalid scope"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1409 | if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) { |
| 1410 | NL_SET_ERR_MSG(extack, |
| 1411 | "Invalid rtm_flags - can not contain DEAD or LINKDOWN"); |
Julian Anastasov | 8061022 | 2016-07-10 21:11:55 +0300 | [diff] [blame] | 1412 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1413 | } |
Julian Anastasov | 8061022 | 2016-07-10 21:11:55 +0300 | [diff] [blame] | 1414 | |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 1415 | if (cfg->fc_nh_id) { |
David Ahern | 6c48ea5 | 2019-06-08 14:53:33 -0700 | [diff] [blame] | 1416 | if (!cfg->fc_mx) { |
| 1417 | fi = fib_find_info_nh(net, cfg); |
| 1418 | if (fi) { |
Yajun Deng | 7997689 | 2021-07-29 15:13:50 +0800 | [diff] [blame] | 1419 | refcount_inc(&fi->fib_treeref); |
David Ahern | 6c48ea5 | 2019-06-08 14:53:33 -0700 | [diff] [blame] | 1420 | return fi; |
| 1421 | } |
| 1422 | } |
| 1423 | |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 1424 | nh = nexthop_find_by_id(net, cfg->fc_nh_id); |
| 1425 | if (!nh) { |
| 1426 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 1427 | goto err_inval; |
| 1428 | } |
| 1429 | nhs = 0; |
| 1430 | } |
| 1431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1433 | if (cfg->fc_mp) { |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 1434 | nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | if (nhs == 0) |
| 1436 | goto err_inval; |
| 1437 | } |
| 1438 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
| 1440 | err = -ENOBUFS; |
Eric Dumazet | 0a6e6b3 | 2022-01-16 01:02:20 -0800 | [diff] [blame] | 1441 | |
| 1442 | /* Paired with WRITE_ONCE() in fib_release_info() */ |
| 1443 | if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) { |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1444 | unsigned int new_size = fib_info_hash_size << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | struct hlist_head *new_info_hash; |
| 1446 | struct hlist_head *new_laddrhash; |
| 1447 | unsigned int bytes; |
| 1448 | |
| 1449 | if (!new_size) |
Eric Dumazet | d94ce9b | 2012-10-21 20:12:09 +0000 | [diff] [blame] | 1450 | new_size = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | bytes = new_size * sizeof(struct hlist_head *); |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1452 | new_info_hash = fib_info_hash_alloc(bytes); |
| 1453 | new_laddrhash = fib_info_hash_alloc(bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | if (!new_info_hash || !new_laddrhash) { |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1455 | fib_info_hash_free(new_info_hash, bytes); |
| 1456 | fib_info_hash_free(new_laddrhash, bytes); |
Joonwoo Park | 88f8349 | 2007-11-26 23:29:32 +0800 | [diff] [blame] | 1457 | } else |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1458 | fib_info_hash_move(new_info_hash, new_laddrhash, new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | |
David S. Miller | 123b973 | 2011-02-01 15:34:21 -0800 | [diff] [blame] | 1460 | if (!fib_info_hash_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | goto failure; |
| 1462 | } |
| 1463 | |
Gustavo A. R. Silva | 1f533ba | 2019-01-30 18:51:48 -0600 | [diff] [blame] | 1464 | fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1465 | if (!fi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | goto failure; |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 1467 | fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx, |
David Ahern | d7e774f | 2018-11-06 12:51:15 -0800 | [diff] [blame] | 1468 | cfg->fc_mx_len, extack); |
Enrico Weigelt | 88e235b | 2019-06-05 23:09:05 +0200 | [diff] [blame] | 1469 | if (IS_ERR(fi->fib_metrics)) { |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 1470 | err = PTR_ERR(fi->fib_metrics); |
| 1471 | kfree(fi); |
| 1472 | return ERR_PTR(err); |
Eric Dumazet | 187e5b3 | 2017-08-15 05:26:17 -0700 | [diff] [blame] | 1473 | } |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 1474 | |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 1475 | fi->fib_net = net; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1476 | fi->fib_protocol = cfg->fc_protocol; |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 1477 | fi->fib_scope = cfg->fc_scope; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1478 | fi->fib_flags = cfg->fc_flags; |
| 1479 | fi->fib_priority = cfg->fc_priority; |
| 1480 | fi->fib_prefsrc = cfg->fc_prefsrc; |
Eric Dumazet | f4ef85b | 2012-10-04 01:25:26 +0000 | [diff] [blame] | 1481 | fi->fib_type = cfg->fc_type; |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 1482 | fi->fib_tb_id = cfg->fc_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
| 1484 | fi->fib_nhs = nhs; |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1485 | if (nh) { |
| 1486 | if (!nexthop_get(nh)) { |
| 1487 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); |
| 1488 | err = -EINVAL; |
| 1489 | } else { |
| 1490 | err = 0; |
| 1491 | fi->nh = nh; |
| 1492 | } |
| 1493 | } else { |
| 1494 | change_nexthops(fi) { |
| 1495 | nexthop_nh->nh_parent = fi; |
| 1496 | } endfor_nexthops(fi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1498 | if (cfg->fc_mp) |
| 1499 | err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, |
| 1500 | extack); |
| 1501 | else |
| 1502 | err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack); |
| 1503 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1504 | |
David Ahern | e4516ef | 2019-03-27 20:53:48 -0700 | [diff] [blame] | 1505 | if (err != 0) |
| 1506 | goto failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1508 | if (fib_props[cfg->fc_type].error) { |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 1509 | if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1510 | NL_SET_ERR_MSG(extack, |
| 1511 | "Gateway, device and multipath can not be specified for this route type"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | goto link_it; |
David S. Miller | 4c8237c | 2011-03-07 14:27:38 -0800 | [diff] [blame] | 1515 | } else { |
| 1516 | switch (cfg->fc_type) { |
| 1517 | case RTN_UNICAST: |
| 1518 | case RTN_LOCAL: |
| 1519 | case RTN_BROADCAST: |
| 1520 | case RTN_ANYCAST: |
| 1521 | case RTN_MULTICAST: |
| 1522 | break; |
| 1523 | default: |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1524 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
David S. Miller | 4c8237c | 2011-03-07 14:27:38 -0800 | [diff] [blame] | 1525 | goto err_inval; |
| 1526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1529 | if (cfg->fc_scope > RT_SCOPE_HOST) { |
| 1530 | NL_SET_ERR_MSG(extack, "Invalid scope"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1534 | if (fi->nh) { |
| 1535 | err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack); |
| 1536 | if (err) |
| 1537 | goto failure; |
| 1538 | } else if (cfg->fc_scope == RT_SCOPE_HOST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | struct fib_nh *nh = fi->fib_nh; |
| 1540 | |
| 1541 | /* Local address is added. */ |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1542 | if (nhs != 1) { |
| 1543 | NL_SET_ERR_MSG(extack, |
| 1544 | "Route with host scope can not have multiple nexthops"); |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 1545 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1546 | } |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 1547 | if (nh->fib_nh_gw_family) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1548 | NL_SET_ERR_MSG(extack, |
| 1549 | "Route with host scope can not have a gateway"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1551 | } |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1552 | nh->fib_nh_scope = RT_SCOPE_NOWHERE; |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 1553 | nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | err = -ENODEV; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1555 | if (!nh->fib_nh_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | goto failure; |
Eric Dumazet | e44b14e | 2021-12-06 17:30:32 -0800 | [diff] [blame] | 1557 | netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, |
| 1558 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } else { |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1560 | int linkdown = 0; |
| 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | change_nexthops(fi) { |
David Ahern | ac1fab2 | 2019-05-22 12:04:43 -0700 | [diff] [blame] | 1563 | err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh, |
| 1564 | cfg->fc_table, cfg->fc_scope, |
| 1565 | extack); |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1566 | if (err != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | goto failure; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1568 | if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1569 | linkdown++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | } endfor_nexthops(fi) |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1571 | if (linkdown == fi->fib_nhs) |
| 1572 | fi->fib_flags |= RTNH_F_LINKDOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1575 | if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) { |
| 1576 | NL_SET_ERR_MSG(extack, "Invalid prefsrc address"); |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1577 | goto err_inval; |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 1578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1580 | if (!fi->nh) { |
| 1581 | change_nexthops(fi) { |
| 1582 | fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common, |
| 1583 | fi->fib_scope); |
| 1584 | if (nexthop_nh->fib_nh_gw_family == AF_INET6) |
| 1585 | fi->fib_nh_is_v6 = true; |
| 1586 | } endfor_nexthops(fi) |
David S. Miller | 1fc050a | 2011-03-07 20:54:48 -0800 | [diff] [blame] | 1587 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1588 | fib_rebalance(fi); |
| 1589 | } |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | link_it: |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1592 | ofi = fib_find_info(fi); |
| 1593 | if (ofi) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | fi->fib_dead = 1; |
| 1595 | free_fib_info(fi); |
Yajun Deng | 7997689 | 2021-07-29 15:13:50 +0800 | [diff] [blame] | 1596 | refcount_inc(&ofi->fib_treeref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | return ofi; |
| 1598 | } |
| 1599 | |
David Ahern | 28814cd | 2021-08-02 10:02:21 -0600 | [diff] [blame] | 1600 | refcount_set(&fi->fib_treeref, 1); |
Reshetova, Elena | 0029c0d | 2017-07-04 09:35:02 +0300 | [diff] [blame] | 1601 | refcount_set(&fi->fib_clntref, 1); |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 1602 | spin_lock_bh(&fib_info_lock); |
Eric Dumazet | 0a6e6b3 | 2022-01-16 01:02:20 -0800 | [diff] [blame] | 1603 | fib_info_cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | hlist_add_head(&fi->fib_hash, |
| 1605 | &fib_info_hash[fib_info_hashfn(fi)]); |
| 1606 | if (fi->fib_prefsrc) { |
| 1607 | struct hlist_head *head; |
| 1608 | |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1609 | head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | hlist_add_head(&fi->fib_lhash, head); |
| 1611 | } |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1612 | if (fi->nh) { |
| 1613 | list_add(&fi->nh_list, &nh->fi_list); |
| 1614 | } else { |
| 1615 | change_nexthops(fi) { |
| 1616 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1618 | if (!nexthop_nh->fib_nh_dev) |
| 1619 | continue; |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 1620 | head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev); |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1621 | hlist_add_head(&nexthop_nh->nh_hash, head); |
| 1622 | } endfor_nexthops(fi) |
| 1623 | } |
Stephen Hemminger | 832b4c5 | 2006-08-29 16:48:09 -0700 | [diff] [blame] | 1624 | spin_unlock_bh(&fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | return fi; |
| 1626 | |
| 1627 | err_inval: |
| 1628 | err = -EINVAL; |
| 1629 | |
| 1630 | failure: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1631 | if (fi) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | fi->fib_dead = 1; |
| 1633 | free_fib_info(fi); |
| 1634 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1635 | |
| 1636 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 1639 | int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc, |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 1640 | u8 rt_family, unsigned char *flags, bool skip_oif) |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1641 | { |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1642 | if (nhc->nhc_flags & RTNH_F_DEAD) |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1643 | *flags |= RTNH_F_DEAD; |
| 1644 | |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1645 | if (nhc->nhc_flags & RTNH_F_LINKDOWN) { |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1646 | *flags |= RTNH_F_LINKDOWN; |
| 1647 | |
| 1648 | rcu_read_lock(); |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1649 | switch (nhc->nhc_family) { |
| 1650 | case AF_INET: |
| 1651 | if (ip_ignore_linkdown(nhc->nhc_dev)) |
| 1652 | *flags |= RTNH_F_DEAD; |
| 1653 | break; |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 1654 | case AF_INET6: |
| 1655 | if (ip6_ignore_linkdown(nhc->nhc_dev)) |
| 1656 | *flags |= RTNH_F_DEAD; |
| 1657 | break; |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1658 | } |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1659 | rcu_read_unlock(); |
| 1660 | } |
| 1661 | |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 1662 | switch (nhc->nhc_gw_family) { |
| 1663 | case AF_INET: |
| 1664 | if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4)) |
| 1665 | goto nla_put_failure; |
| 1666 | break; |
| 1667 | case AF_INET6: |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 1668 | /* if gateway family does not match nexthop family |
| 1669 | * gateway is encoded as RTA_VIA |
| 1670 | */ |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 1671 | if (rt_family != nhc->nhc_gw_family) { |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 1672 | int alen = sizeof(struct in6_addr); |
| 1673 | struct nlattr *nla; |
| 1674 | struct rtvia *via; |
| 1675 | |
| 1676 | nla = nla_reserve(skb, RTA_VIA, alen + 2); |
| 1677 | if (!nla) |
| 1678 | goto nla_put_failure; |
| 1679 | |
| 1680 | via = nla_data(nla); |
| 1681 | via->rtvia_family = AF_INET6; |
| 1682 | memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen); |
| 1683 | } else if (nla_put_in6_addr(skb, RTA_GATEWAY, |
| 1684 | &nhc->nhc_gw.ipv6) < 0) { |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 1685 | goto nla_put_failure; |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 1686 | } |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 1687 | break; |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1688 | } |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1689 | |
Ido Schimmel | ca787e0 | 2020-11-10 12:25:53 +0200 | [diff] [blame] | 1690 | *flags |= (nhc->nhc_flags & |
| 1691 | (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP)); |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1692 | |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1693 | if (!skip_oif && nhc->nhc_dev && |
| 1694 | nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex)) |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1695 | goto nla_put_failure; |
| 1696 | |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1697 | if (nhc->nhc_lwtstate && |
David Ahern | ffa8ce5 | 2019-04-23 08:23:41 -0700 | [diff] [blame] | 1698 | lwtunnel_fill_encap(skb, nhc->nhc_lwtstate, |
| 1699 | RTA_ENCAP, RTA_ENCAP_TYPE) < 0) |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1700 | goto nla_put_failure; |
| 1701 | |
| 1702 | return 0; |
| 1703 | |
| 1704 | nla_put_failure: |
| 1705 | return -EMSGSIZE; |
| 1706 | } |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 1707 | EXPORT_SYMBOL_GPL(fib_nexthop_info); |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1708 | |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 1709 | #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6) |
| 1710 | int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc, |
Xiao Liang | 597aa16 | 2021-09-23 23:03:19 +0800 | [diff] [blame] | 1711 | int nh_weight, u8 rt_family, u32 nh_tclassid) |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1712 | { |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1713 | const struct net_device *dev = nhc->nhc_dev; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1714 | struct rtnexthop *rtnh; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 1715 | unsigned char flags = 0; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1716 | |
| 1717 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
| 1718 | if (!rtnh) |
| 1719 | goto nla_put_failure; |
| 1720 | |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1721 | rtnh->rtnh_hops = nh_weight - 1; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1722 | rtnh->rtnh_ifindex = dev ? dev->ifindex : 0; |
| 1723 | |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 1724 | if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0) |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1725 | goto nla_put_failure; |
| 1726 | |
| 1727 | rtnh->rtnh_flags = flags; |
| 1728 | |
Xiao Liang | 597aa16 | 2021-09-23 23:03:19 +0800 | [diff] [blame] | 1729 | if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid)) |
| 1730 | goto nla_put_failure; |
| 1731 | |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1732 | /* length of rtnetlink header + attributes */ |
| 1733 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; |
| 1734 | |
| 1735 | return 0; |
| 1736 | |
| 1737 | nla_put_failure: |
| 1738 | return -EMSGSIZE; |
| 1739 | } |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 1740 | EXPORT_SYMBOL_GPL(fib_add_nexthop); |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1741 | #endif |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1742 | |
David Ahern | c236419 | 2019-04-02 14:11:57 -0700 | [diff] [blame] | 1743 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1744 | static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi) |
| 1745 | { |
| 1746 | struct nlattr *mp; |
| 1747 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1748 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1749 | if (!mp) |
| 1750 | goto nla_put_failure; |
| 1751 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1752 | if (unlikely(fi->nh)) { |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 1753 | if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0) |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1754 | goto nla_put_failure; |
| 1755 | goto mp_end; |
| 1756 | } |
| 1757 | |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1758 | for_nexthops(fi) { |
Xiao Liang | 597aa16 | 2021-09-23 23:03:19 +0800 | [diff] [blame] | 1759 | u32 nh_tclassid = 0; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1760 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Xiao Liang | 597aa16 | 2021-09-23 23:03:19 +0800 | [diff] [blame] | 1761 | nh_tclassid = nh->nh_tclassid; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1762 | #endif |
Xiao Liang | 597aa16 | 2021-09-23 23:03:19 +0800 | [diff] [blame] | 1763 | if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight, |
| 1764 | AF_INET, nh_tclassid) < 0) |
| 1765 | goto nla_put_failure; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1766 | } endfor_nexthops(fi); |
| 1767 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1768 | mp_end: |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1769 | nla_nest_end(skb, mp); |
| 1770 | |
| 1771 | return 0; |
| 1772 | |
| 1773 | nla_put_failure: |
| 1774 | return -EMSGSIZE; |
| 1775 | } |
| 1776 | #else |
| 1777 | static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi) |
| 1778 | { |
| 1779 | return 0; |
| 1780 | } |
| 1781 | #endif |
| 1782 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1783 | int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, |
Amit Cohen | 0855478 | 2021-02-01 21:47:50 +0200 | [diff] [blame] | 1784 | const struct fib_rt_info *fri, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | { |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 1786 | unsigned int nhs = fib_info_num_path(fri->fi); |
| 1787 | struct fib_info *fi = fri->fi; |
| 1788 | u32 tb_id = fri->tb_id; |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1789 | struct nlmsghdr *nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | struct rtmsg *rtm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1792 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1793 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1794 | return -EMSGSIZE; |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1795 | |
| 1796 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | rtm->rtm_family = AF_INET; |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 1798 | rtm->rtm_dst_len = fri->dst_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | rtm->rtm_src_len = 0; |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 1800 | rtm->rtm_tos = fri->tos; |
Krzysztof Piotr Oledzki | 709772e | 2008-06-10 15:44:49 -0700 | [diff] [blame] | 1801 | if (tb_id < 256) |
| 1802 | rtm->rtm_table = tb_id; |
| 1803 | else |
| 1804 | rtm->rtm_table = RT_TABLE_COMPAT; |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1805 | if (nla_put_u32(skb, RTA_TABLE, tb_id)) |
| 1806 | goto nla_put_failure; |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 1807 | rtm->rtm_type = fri->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | rtm->rtm_flags = fi->fib_flags; |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 1809 | rtm->rtm_scope = fi->fib_scope; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | rtm->rtm_protocol = fi->fib_protocol; |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1811 | |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1812 | if (rtm->rtm_dst_len && |
Ido Schimmel | 1e301fd | 2020-01-14 13:23:10 +0200 | [diff] [blame] | 1813 | nla_put_in_addr(skb, RTA_DST, fri->dst)) |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1814 | goto nla_put_failure; |
| 1815 | if (fi->fib_priority && |
| 1816 | nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) |
| 1817 | goto nla_put_failure; |
Eric Dumazet | 3fb07da | 2017-05-25 14:27:35 -0700 | [diff] [blame] | 1818 | if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0) |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1819 | goto nla_put_failure; |
| 1820 | |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1821 | if (fi->fib_prefsrc && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1822 | nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc)) |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1823 | goto nla_put_failure; |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1824 | |
| 1825 | if (fi->nh) { |
| 1826 | if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id)) |
| 1827 | goto nla_put_failure; |
| 1828 | if (nexthop_is_blackhole(fi->nh)) |
| 1829 | rtm->rtm_type = RTN_BLACKHOLE; |
Roopa Prabhu | 4f80116 | 2020-04-27 13:56:46 -0700 | [diff] [blame] | 1830 | if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode) |
| 1831 | goto offload; |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 1834 | if (nhs == 1) { |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1835 | const struct fib_nh_common *nhc = fib_info_nhc(fi, 0); |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 1836 | unsigned char flags = 0; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1837 | |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 1838 | if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0) |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1839 | goto nla_put_failure; |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1840 | |
| 1841 | rtm->rtm_flags = flags; |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 1842 | #ifdef CONFIG_IP_ROUTE_CLASSID |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 1843 | if (nhc->nhc_family == AF_INET) { |
| 1844 | struct fib_nh *nh; |
| 1845 | |
| 1846 | nh = container_of(nhc, struct fib_nh, nh_common); |
| 1847 | if (nh->nh_tclassid && |
| 1848 | nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) |
| 1849 | goto nla_put_failure; |
| 1850 | } |
Patrick McHardy | 8265abc | 2006-07-21 15:09:55 -0700 | [diff] [blame] | 1851 | #endif |
David Ahern | b0f6019 | 2019-04-02 14:11:56 -0700 | [diff] [blame] | 1852 | } else { |
| 1853 | if (fib_add_multipath(skb, fi) < 0) |
David Ahern | ea7a808 | 2017-01-11 14:29:54 -0800 | [diff] [blame] | 1854 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | } |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1856 | |
Roopa Prabhu | 4f80116 | 2020-04-27 13:56:46 -0700 | [diff] [blame] | 1857 | offload: |
Ido Schimmel | 90b93f1 | 2020-01-14 13:23:11 +0200 | [diff] [blame] | 1858 | if (fri->offload) |
| 1859 | rtm->rtm_flags |= RTM_F_OFFLOAD; |
| 1860 | if (fri->trap) |
| 1861 | rtm->rtm_flags |= RTM_F_TRAP; |
Amit Cohen | 36c5100 | 2021-02-07 10:22:50 +0200 | [diff] [blame] | 1862 | if (fri->offload_failed) |
| 1863 | rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED; |
Ido Schimmel | 90b93f1 | 2020-01-14 13:23:11 +0200 | [diff] [blame] | 1864 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1865 | nlmsg_end(skb, nlh); |
| 1866 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1868 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1869 | nlmsg_cancel(skb, nlh); |
| 1870 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1874 | * Update FIB if: |
| 1875 | * - local address disappeared -> we must delete all the entries |
| 1876 | * referring to it. |
| 1877 | * - device went down -> we must shutdown all nexthops going via it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | */ |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 1879 | int fib_sync_down_addr(struct net_device *dev, __be32 local) |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1880 | { |
David Ahern | e0a3126 | 2019-11-07 18:29:52 +0000 | [diff] [blame] | 1881 | int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 1882 | struct net *net = dev_net(dev); |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1883 | struct hlist_head *head; |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1884 | struct fib_info *fi; |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1885 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1887 | if (!fib_info_laddrhash || local == 0) |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1888 | return 0; |
| 1889 | |
Eric Dumazet | 79eb15d | 2022-01-19 02:04:13 -0800 | [diff] [blame] | 1890 | head = fib_info_laddrhash_bucket(net, local); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1891 | hlist_for_each_entry(fi, head, fib_lhash) { |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 1892 | if (!net_eq(fi->fib_net, net) || |
| 1893 | fi->fib_tb_id != tb_id) |
Denis V. Lunev | 4814bdb | 2008-01-31 18:50:07 -0800 | [diff] [blame] | 1894 | continue; |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1895 | if (fi->fib_prefsrc == local) { |
| 1896 | fi->fib_flags |= RTNH_F_DEAD; |
| 1897 | ret++; |
| 1898 | } |
| 1899 | } |
| 1900 | return ret; |
| 1901 | } |
| 1902 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1903 | static int call_fib_nh_notifiers(struct fib_nh *nh, |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1904 | enum fib_event_type event_type) |
| 1905 | { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1906 | bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev); |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1907 | struct fib_nh_notifier_info info = { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1908 | .fib_nh = nh, |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1909 | }; |
| 1910 | |
| 1911 | switch (event_type) { |
| 1912 | case FIB_EVENT_NH_ADD: |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1913 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1914 | break; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1915 | if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1916 | break; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1917 | return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type, |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1918 | &info.info); |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1919 | case FIB_EVENT_NH_DEL: |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1920 | if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) || |
| 1921 | (nh->fib_nh_flags & RTNH_F_DEAD)) |
| 1922 | return call_fib4_notifiers(dev_net(nh->fib_nh_dev), |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1923 | event_type, &info.info); |
Gustavo A. R. Silva | 7912118 | 2020-11-20 12:25:57 -0600 | [diff] [blame] | 1924 | break; |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 1925 | default: |
| 1926 | break; |
| 1927 | } |
| 1928 | |
| 1929 | return NOTIFY_DONE; |
| 1930 | } |
| 1931 | |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1932 | /* Update the PMTU of exceptions when: |
| 1933 | * - the new MTU of the first hop becomes smaller than the PMTU |
| 1934 | * - the old MTU was the same as the PMTU, and it limited discovery of |
| 1935 | * larger MTUs on the path. With that limit raised, we can now |
| 1936 | * discover larger MTUs |
| 1937 | * A special case is locked exceptions, for which the PMTU is smaller |
| 1938 | * than the minimal accepted PMTU: |
| 1939 | * - if the new MTU is greater than the PMTU, don't make any change |
| 1940 | * - otherwise, unlock and set PMTU |
| 1941 | */ |
David Ahern | 06c77c3 | 2019-05-22 12:04:46 -0700 | [diff] [blame] | 1942 | void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig) |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1943 | { |
| 1944 | struct fnhe_hash_bucket *bucket; |
| 1945 | int i; |
| 1946 | |
David Ahern | a5995e7 | 2019-04-30 07:45:50 -0700 | [diff] [blame] | 1947 | bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1948 | if (!bucket) |
| 1949 | return; |
| 1950 | |
| 1951 | for (i = 0; i < FNHE_HASH_SIZE; i++) { |
| 1952 | struct fib_nh_exception *fnhe; |
| 1953 | |
| 1954 | for (fnhe = rcu_dereference_protected(bucket[i].chain, 1); |
| 1955 | fnhe; |
| 1956 | fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) { |
| 1957 | if (fnhe->fnhe_mtu_locked) { |
| 1958 | if (new <= fnhe->fnhe_pmtu) { |
| 1959 | fnhe->fnhe_pmtu = new; |
| 1960 | fnhe->fnhe_mtu_locked = false; |
| 1961 | } |
| 1962 | } else if (new < fnhe->fnhe_pmtu || |
| 1963 | orig == fnhe->fnhe_pmtu) { |
| 1964 | fnhe->fnhe_pmtu = new; |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | void fib_sync_mtu(struct net_device *dev, u32 orig_mtu) |
| 1971 | { |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 1972 | struct hlist_head *head = fib_info_devhash_bucket(dev); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1973 | struct fib_nh *nh; |
| 1974 | |
| 1975 | hlist_for_each_entry(nh, head, nh_hash) { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 1976 | if (nh->fib_nh_dev == dev) |
David Ahern | 06c77c3 | 2019-05-22 12:04:46 -0700 | [diff] [blame] | 1977 | fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1981 | /* Event force Flags Description |
| 1982 | * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host |
| 1983 | * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host |
| 1984 | * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed |
| 1985 | * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 1986 | * |
| 1987 | * only used when fib_nh is built into fib_info |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1988 | */ |
| 1989 | int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | { |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 1991 | struct hlist_head *head = fib_info_devhash_bucket(dev); |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1992 | struct fib_info *prev_fi = NULL; |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 1993 | int scope = RT_SCOPE_NOWHERE; |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 1994 | struct fib_nh *nh; |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 1995 | int ret = 0; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1996 | |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1997 | if (force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | scope = -1; |
| 1999 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2000 | hlist_for_each_entry(nh, head, nh_hash) { |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2001 | struct fib_info *fi = nh->nh_parent; |
| 2002 | int dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2004 | BUG_ON(!fi->fib_nhs); |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2005 | if (nh->fib_nh_dev != dev || fi == prev_fi) |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2006 | continue; |
| 2007 | prev_fi = fi; |
| 2008 | dead = 0; |
| 2009 | change_nexthops(fi) { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2010 | if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2011 | dead++; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2012 | else if (nexthop_nh->fib_nh_dev == dev && |
| 2013 | nexthop_nh->fib_nh_scope != scope) { |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2014 | switch (event) { |
| 2015 | case NETDEV_DOWN: |
| 2016 | case NETDEV_UNREGISTER: |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2017 | nexthop_nh->fib_nh_flags |= RTNH_F_DEAD; |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 2018 | fallthrough; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2019 | case NETDEV_CHANGE: |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2020 | nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2021 | break; |
| 2022 | } |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 2023 | call_fib_nh_notifiers(nexthop_nh, |
| 2024 | FIB_EVENT_NH_DEL); |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2025 | dead++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2027 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2028 | if (event == NETDEV_UNREGISTER && |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2029 | nexthop_nh->fib_nh_dev == dev) { |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2030 | dead = fi->fib_nhs; |
| 2031 | break; |
| 2032 | } |
| 2033 | #endif |
| 2034 | } endfor_nexthops(fi) |
| 2035 | if (dead == fi->fib_nhs) { |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2036 | switch (event) { |
| 2037 | case NETDEV_DOWN: |
| 2038 | case NETDEV_UNREGISTER: |
| 2039 | fi->fib_flags |= RTNH_F_DEAD; |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 2040 | fallthrough; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2041 | case NETDEV_CHANGE: |
| 2042 | fi->fib_flags |= RTNH_F_LINKDOWN; |
| 2043 | break; |
| 2044 | } |
Denis V. Lunev | 85326fa | 2008-01-31 18:48:47 -0800 | [diff] [blame] | 2045 | ret++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | } |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 2047 | |
| 2048 | fib_rebalance(fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | return ret; |
| 2052 | } |
| 2053 | |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2054 | /* Must be invoked inside of an RCU protected region. */ |
David Ahern | c7b371e | 2017-01-05 19:33:59 -0800 | [diff] [blame] | 2055 | static void fib_select_default(const struct flowi4 *flp, struct fib_result *res) |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2056 | { |
| 2057 | struct fib_info *fi = NULL, *last_resort = NULL; |
Alexander Duyck | 56315f9 | 2015-02-25 15:31:31 -0800 | [diff] [blame] | 2058 | struct hlist_head *fa_head = res->fa_head; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2059 | struct fib_table *tb = res->table; |
Julian Anastasov | 18a912e9 | 2015-07-22 10:43:22 +0300 | [diff] [blame] | 2060 | u8 slen = 32 - res->prefixlen; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2061 | int order = -1, last_idx = -1; |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2062 | struct fib_alias *fa, *fa1 = NULL; |
| 2063 | u32 last_prio = res->fi->fib_priority; |
| 2064 | u8 last_tos = 0; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2065 | |
Alexander Duyck | 56315f9 | 2015-02-25 15:31:31 -0800 | [diff] [blame] | 2066 | hlist_for_each_entry_rcu(fa, fa_head, fa_list) { |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2067 | struct fib_info *next_fi = fa->fa_info; |
David Ahern | 7c74b0b | 2020-04-22 15:40:20 -0600 | [diff] [blame] | 2068 | struct fib_nh_common *nhc; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2069 | |
Julian Anastasov | 18a912e9 | 2015-07-22 10:43:22 +0300 | [diff] [blame] | 2070 | if (fa->fa_slen != slen) |
| 2071 | continue; |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2072 | if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos) |
| 2073 | continue; |
Julian Anastasov | 18a912e9 | 2015-07-22 10:43:22 +0300 | [diff] [blame] | 2074 | if (fa->tb_id != tb->tb_id) |
| 2075 | continue; |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2076 | if (next_fi->fib_priority > last_prio && |
| 2077 | fa->fa_tos == last_tos) { |
| 2078 | if (last_tos) |
| 2079 | continue; |
| 2080 | break; |
| 2081 | } |
| 2082 | if (next_fi->fib_flags & RTNH_F_DEAD) |
| 2083 | continue; |
| 2084 | last_tos = fa->fa_tos; |
| 2085 | last_prio = next_fi->fib_priority; |
| 2086 | |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 2087 | if (next_fi->fib_scope != res->scope || |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2088 | fa->fa_type != RTN_UNICAST) |
| 2089 | continue; |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 2090 | |
David Ahern | 7c74b0b | 2020-04-22 15:40:20 -0600 | [diff] [blame] | 2091 | nhc = fib_info_nhc(next_fi, 0); |
| 2092 | if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK) |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2093 | continue; |
| 2094 | |
| 2095 | fib_alias_accessed(fa); |
| 2096 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2097 | if (!fi) { |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2098 | if (next_fi != res->fi) |
| 2099 | break; |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2100 | fa1 = fa; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2101 | } else if (!fib_detect_death(fi, order, &last_resort, |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2102 | &last_idx, fa1->fa_default)) { |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2103 | fib_result_assign(res, fi); |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2104 | fa1->fa_default = order; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2105 | goto out; |
| 2106 | } |
| 2107 | fi = next_fi; |
| 2108 | order++; |
| 2109 | } |
| 2110 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2111 | if (order <= 0 || !fi) { |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2112 | if (fa1) |
| 2113 | fa1->fa_default = -1; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2114 | goto out; |
| 2115 | } |
| 2116 | |
| 2117 | if (!fib_detect_death(fi, order, &last_resort, &last_idx, |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2118 | fa1->fa_default)) { |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2119 | fib_result_assign(res, fi); |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2120 | fa1->fa_default = order; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2121 | goto out; |
| 2122 | } |
| 2123 | |
| 2124 | if (last_idx >= 0) |
| 2125 | fib_result_assign(res, last_resort); |
Julian Anastasov | 2392deb | 2015-07-22 10:43:23 +0300 | [diff] [blame] | 2126 | fa1->fa_default = last_idx; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2127 | out: |
Eric Dumazet | 31d4093 | 2011-02-14 11:23:04 -0800 | [diff] [blame] | 2128 | return; |
David S. Miller | 0c838ff | 2011-01-31 16:16:50 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 2132 | * Dead device goes up. We wake up dead nexthops. |
| 2133 | * It takes sense only on multipath routes. |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 2134 | * |
| 2135 | * only used when fib_nh is built into fib_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | */ |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 2137 | int fib_sync_up(struct net_device *dev, unsigned char nh_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | { |
| 2139 | struct fib_info *prev_fi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | struct fib_nh *nh; |
| 2142 | int ret; |
| 2143 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 2144 | if (!(dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | return 0; |
| 2146 | |
Julian Anastasov | c9b3292 | 2015-10-30 10:23:34 +0200 | [diff] [blame] | 2147 | if (nh_flags & RTNH_F_DEAD) { |
| 2148 | unsigned int flags = dev_get_flags(dev); |
| 2149 | |
| 2150 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) |
| 2151 | nh_flags |= RTNH_F_LINKDOWN; |
| 2152 | } |
| 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | prev_fi = NULL; |
Eric Dumazet | d07418a | 2022-01-19 02:04:12 -0800 | [diff] [blame] | 2155 | head = fib_info_devhash_bucket(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | ret = 0; |
| 2157 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2158 | hlist_for_each_entry(nh, head, nh_hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | struct fib_info *fi = nh->nh_parent; |
| 2160 | int alive; |
| 2161 | |
| 2162 | BUG_ON(!fi->fib_nhs); |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2163 | if (nh->fib_nh_dev != dev || fi == prev_fi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | continue; |
| 2165 | |
| 2166 | prev_fi = fi; |
| 2167 | alive = 0; |
| 2168 | change_nexthops(fi) { |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2169 | if (!(nexthop_nh->fib_nh_flags & nh_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | alive++; |
| 2171 | continue; |
| 2172 | } |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2173 | if (!nexthop_nh->fib_nh_dev || |
| 2174 | !(nexthop_nh->fib_nh_dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | continue; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2176 | if (nexthop_nh->fib_nh_dev != dev || |
David S. Miller | 71fceff | 2010-01-15 01:16:40 -0800 | [diff] [blame] | 2177 | !__in_dev_get_rtnl(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | continue; |
| 2179 | alive++; |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2180 | nexthop_nh->fib_nh_flags &= ~nh_flags; |
Ido Schimmel | 982acb9 | 2017-02-08 11:16:39 +0100 | [diff] [blame] | 2181 | call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } endfor_nexthops(fi) |
| 2183 | |
| 2184 | if (alive > 0) { |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2185 | fi->fib_flags &= ~nh_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | ret++; |
| 2187 | } |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 2188 | |
| 2189 | fib_rebalance(fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2195 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David Ahern | a6db449 | 2016-04-07 07:21:00 -0700 | [diff] [blame] | 2196 | static bool fib_good_nh(const struct fib_nh *nh) |
| 2197 | { |
| 2198 | int state = NUD_REACHABLE; |
| 2199 | |
David Ahern | b75ed8b | 2019-03-27 20:53:55 -0700 | [diff] [blame] | 2200 | if (nh->fib_nh_scope == RT_SCOPE_LINK) { |
David Ahern | a6db449 | 2016-04-07 07:21:00 -0700 | [diff] [blame] | 2201 | struct neighbour *n; |
| 2202 | |
| 2203 | rcu_read_lock_bh(); |
| 2204 | |
David Ahern | 1a38c43 | 2019-04-05 16:30:38 -0700 | [diff] [blame] | 2205 | if (likely(nh->fib_nh_gw_family == AF_INET)) |
| 2206 | n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, |
| 2207 | (__force u32)nh->fib_nh_gw4); |
| 2208 | else if (nh->fib_nh_gw_family == AF_INET6) |
| 2209 | n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, |
| 2210 | &nh->fib_nh_gw6); |
| 2211 | else |
| 2212 | n = NULL; |
David Ahern | a6db449 | 2016-04-07 07:21:00 -0700 | [diff] [blame] | 2213 | if (n) |
| 2214 | state = n->nud_state; |
| 2215 | |
| 2216 | rcu_read_unlock_bh(); |
| 2217 | } |
| 2218 | |
| 2219 | return !!(state & NUD_VALID); |
| 2220 | } |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 2221 | |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 2222 | void fib_select_multipath(struct fib_result *res, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | { |
| 2224 | struct fib_info *fi = res->fi; |
David Ahern | a6db449 | 2016-04-07 07:21:00 -0700 | [diff] [blame] | 2225 | struct net *net = fi->fib_net; |
| 2226 | bool first = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | |
David Ahern | 4c7e808 | 2019-06-03 20:19:51 -0700 | [diff] [blame] | 2228 | if (unlikely(res->fi->nh)) { |
| 2229 | nexthop_path_fib_result(res, hash); |
| 2230 | return; |
| 2231 | } |
| 2232 | |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 2233 | change_nexthops(fi) { |
Xin Long | 6174a30 | 2018-04-01 22:40:35 +0800 | [diff] [blame] | 2234 | if (net->ipv4.sysctl_fib_multipath_use_neigh) { |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 2235 | if (!fib_good_nh(nexthop_nh)) |
Xin Long | 6174a30 | 2018-04-01 22:40:35 +0800 | [diff] [blame] | 2236 | continue; |
| 2237 | if (!first) { |
| 2238 | res->nh_sel = nhsel; |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 2239 | res->nhc = &nexthop_nh->nh_common; |
Xin Long | 6174a30 | 2018-04-01 22:40:35 +0800 | [diff] [blame] | 2240 | first = true; |
| 2241 | } |
| 2242 | } |
| 2243 | |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 2244 | if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound)) |
Peter Nørlund | 0e884c7 | 2015-09-30 10:12:21 +0200 | [diff] [blame] | 2245 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Xin Long | 6174a30 | 2018-04-01 22:40:35 +0800 | [diff] [blame] | 2247 | res->nh_sel = nhsel; |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 2248 | res->nhc = &nexthop_nh->nh_common; |
Xin Long | 6174a30 | 2018-04-01 22:40:35 +0800 | [diff] [blame] | 2249 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | } endfor_nexthops(fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | } |
| 2252 | #endif |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2253 | |
| 2254 | void fib_select_path(struct net *net, struct fib_result *res, |
Nikolay Aleksandrov | bf4e0a3 | 2017-03-16 15:28:00 +0200 | [diff] [blame] | 2255 | struct flowi4 *fl4, const struct sk_buff *skb) |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2256 | { |
David Ahern | 0d876f2 | 2018-02-13 08:11:34 -0800 | [diff] [blame] | 2257 | if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) |
| 2258 | goto check_saddr; |
David Ahern | 7a18c5b | 2017-01-10 14:37:35 -0800 | [diff] [blame] | 2259 | |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2260 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 2261 | if (fib_info_num_path(res->fi) > 1) { |
David Ahern | 7efc0b6 | 2018-03-02 08:32:12 -0800 | [diff] [blame] | 2262 | int h = fib_multipath_hash(net, fl4, skb, NULL); |
Paolo Abeni | 9920e48 | 2015-10-29 22:20:40 +0100 | [diff] [blame] | 2263 | |
Nikolay Aleksandrov | bf4e0a3 | 2017-03-16 15:28:00 +0200 | [diff] [blame] | 2264 | fib_select_multipath(res, h); |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2265 | } |
| 2266 | else |
| 2267 | #endif |
| 2268 | if (!res->prefixlen && |
| 2269 | res->table->tb_num_default > 1 && |
David Ahern | 0d876f2 | 2018-02-13 08:11:34 -0800 | [diff] [blame] | 2270 | res->type == RTN_UNICAST) |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2271 | fib_select_default(fl4, res); |
| 2272 | |
David Ahern | 0d876f2 | 2018-02-13 08:11:34 -0800 | [diff] [blame] | 2273 | check_saddr: |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2274 | if (!fl4->saddr) |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 2275 | fl4->saddr = fib_result_prefsrc(net, res); |
David Ahern | 3ce58d8 | 2015-10-05 08:51:25 -0700 | [diff] [blame] | 2276 | } |