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