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