blob: b80410673915094255824d4e4b5fbdad292fd4c8 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * IPv4 Forwarding Information Base: semantics.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/bitops.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/jiffies.h>
17#include <linux/mm.h>
18#include <linux/string.h>
19#include <linux/socket.h>
20#include <linux/sockios.h>
21#include <linux/errno.h>
22#include <linux/in.h>
23#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
26#include <linux/if_arp.h>
27#include <linux/proc_fs.h>
28#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
David Ahernc3ab2b42017-05-21 10:12:03 -060031#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020033#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/ip.h>
35#include <net/protocol.h>
36#include <net/route.h>
37#include <net/tcp.h>
38#include <net/sock.h>
39#include <net/ip_fib.h>
David Ahern717a8f52019-04-05 16:30:32 -070040#include <net/ip6_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070041#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070042#include <net/rtnh.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020043#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020044#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070045#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "fib_lookup.h"
48
Stephen Hemminger832b4c52006-08-29 16:48:09 -070049static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static struct hlist_head *fib_info_hash;
51static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080052static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static unsigned int fib_info_cnt;
54
55#define DEVINDEX_HASHBITS 8
56#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
57static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
58
59#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000061#define for_nexthops(fi) { \
62 int nhsel; const struct fib_nh *nh; \
63 for (nhsel = 0, nh = (fi)->fib_nh; \
64 nhsel < (fi)->fib_nhs; \
65 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000067#define change_nexthops(fi) { \
68 int nhsel; struct fib_nh *nexthop_nh; \
69 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
70 nhsel < (fi)->fib_nhs; \
71 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#else /* CONFIG_IP_ROUTE_MULTIPATH */
74
75/* Hope, that gcc will optimize it to get rid of dummy loop */
76
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000077#define for_nexthops(fi) { \
78 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
79 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000081#define change_nexthops(fi) { \
82 int nhsel; \
83 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
84 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#endif /* CONFIG_IP_ROUTE_MULTIPATH */
87
88#define endfor_nexthops(fi) }
89
90
David S. Miller3be06862011-03-07 15:01:10 -080091const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000092 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .error = 0,
94 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000095 },
96 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .error = 0,
98 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000099 },
100 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .error = 0,
102 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000103 },
104 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .error = 0,
106 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000107 },
108 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .error = 0,
110 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000111 },
112 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .error = 0,
114 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000115 },
116 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .error = -EINVAL,
118 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000119 },
120 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .error = -EHOSTUNREACH,
122 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000123 },
124 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .error = -EACCES,
126 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000127 },
128 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .error = -EAGAIN,
130 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000131 },
132 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .error = -EINVAL,
134 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000135 },
136 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .error = -EINVAL,
138 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000139 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
David S. Millerc5038a82012-07-31 15:02:02 -0700142static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000143{
144 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
145
146 if (!rt)
147 return;
148
149 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
150 * because we waited an RCU grace period before calling
151 * free_fib_info_rcu()
152 */
153
Wei Wang95c47f92017-06-17 10:42:30 -0700154 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700155 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000156}
157
David Aherna5995e72019-04-30 07:45:50 -0700158static void free_nh_exceptions(struct fib_nh_common *nhc)
David S. Millerc5038a82012-07-31 15:02:02 -0700159{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700160 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700161 int i;
162
David Aherna5995e72019-04-30 07:45:50 -0700163 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Eric Dumazetcaa41522014-09-03 22:21:56 -0700164 if (!hash)
165 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700166 for (i = 0; i < FNHE_HASH_SIZE; i++) {
167 struct fib_nh_exception *fnhe;
168
169 fnhe = rcu_dereference_protected(hash[i].chain, 1);
170 while (fnhe) {
171 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800172
David S. Millerc5038a82012-07-31 15:02:02 -0700173 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
174
Timo Teräs2ffae992013-06-27 10:27:05 +0300175 rt_fibinfo_free(&fnhe->fnhe_rth_input);
176 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700177
178 kfree(fnhe);
179
180 fnhe = next;
181 }
182 }
183 kfree(hash);
184}
185
186static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000187{
188 int cpu;
189
190 if (!rtp)
191 return;
192
193 for_each_possible_cpu(cpu) {
194 struct rtable *rt;
195
196 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700197 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700198 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700199 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700200 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000201 }
202 free_percpu(rtp);
203}
204
David Ahern979e2762019-03-27 20:53:58 -0700205void fib_nh_common_release(struct fib_nh_common *nhc)
206{
207 if (nhc->nhc_dev)
208 dev_put(nhc->nhc_dev);
209
210 lwtstate_put(nhc->nhc_lwtstate);
David Ahern0f457a32019-04-30 07:45:48 -0700211 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
212 rt_fibinfo_free(&nhc->nhc_rth_input);
David Aherna5995e72019-04-30 07:45:50 -0700213 free_nh_exceptions(nhc);
David Ahern979e2762019-03-27 20:53:58 -0700214}
215EXPORT_SYMBOL_GPL(fib_nh_common_release);
216
David Ahernfaa041a2019-03-27 20:53:49 -0700217void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
218{
219#ifdef CONFIG_IP_ROUTE_CLASSID
220 if (fib_nh->nh_tclassid)
221 net->ipv4.fib_num_tclassid_users--;
222#endif
David Ahern979e2762019-03-27 20:53:58 -0700223 fib_nh_common_release(&fib_nh->nh_common);
David Ahernfaa041a2019-03-27 20:53:49 -0700224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000227static void free_fib_info_rcu(struct rcu_head *head)
228{
229 struct fib_info *fi = container_of(head, struct fib_info, rcu);
230
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000231 change_nexthops(fi) {
David Ahernfaa041a2019-03-27 20:53:49 -0700232 fib_nh_release(fi->fib_net, nexthop_nh);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000233 } endfor_nexthops(fi);
234
David Aherncc5f0eb2018-10-04 20:07:52 -0700235 ip_fib_metrics_put(fi->fib_metrics);
236
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000237 kfree(fi);
238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240void free_fib_info(struct fib_info *fi)
241{
242 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000243 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 fib_info_cnt--;
David Ahernfaa041a2019-03-27 20:53:49 -0700247
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000248 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100250EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252void fib_release_info(struct fib_info *fi)
253{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700254 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (fi && --fi->fib_treeref == 0) {
256 hlist_del(&fi->fib_hash);
257 if (fi->fib_prefsrc)
258 hlist_del(&fi->fib_lhash);
259 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700260 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800262 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 } endfor_nexthops(fi)
264 fi->fib_dead = 1;
265 fib_info_put(fi);
266 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700267 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000270static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 const struct fib_nh *onh = ofi->fib_nh;
273
274 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700275 if (nh->fib_nh_oif != onh->fib_nh_oif ||
David Aherna4ea5d42019-04-05 16:30:30 -0700276 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
David Ahernb75ed8b2019-03-27 20:53:55 -0700277 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700279 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100281#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 nh->nh_tclassid != onh->nh_tclassid ||
283#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700284 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
285 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -1;
David Aherna4ea5d42019-04-05 16:30:30 -0700287
288 if (nh->fib_nh_gw_family == AF_INET &&
289 nh->fib_nh_gw4 != onh->fib_nh_gw4)
290 return -1;
291
292 if (nh->fib_nh_gw_family == AF_INET6 &&
293 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
294 return -1;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 onh++;
297 } endfor_nexthops(fi);
298 return 0;
299}
300
David S. Miller88ebc722008-01-12 21:49:01 -0800301static inline unsigned int fib_devindex_hashfn(unsigned int val)
302{
303 unsigned int mask = DEVINDEX_HASHSIZE - 1;
304
305 return (val ^
306 (val >> DEVINDEX_HASHBITS) ^
307 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
311{
David S. Miller123b9732011-02-01 15:34:21 -0800312 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned int val = fi->fib_nhs;
314
David S. Miller37e826c2011-03-24 18:06:47 -0700315 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700316 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800318 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700319 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
David S. Miller88ebc722008-01-12 21:49:01 -0800320 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
323}
324
325static struct fib_info *fib_find_info(const struct fib_info *nfi)
326{
327 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct fib_info *fi;
329 unsigned int hash;
330
331 hash = fib_info_hashfn(nfi);
332 head = &fib_info_hash[hash];
333
Sasha Levinb67bfe02013-02-27 17:06:00 -0800334 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800335 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800336 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (fi->fib_nhs != nfi->fib_nhs)
338 continue;
339 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700340 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 nfi->fib_prefsrc == fi->fib_prefsrc &&
342 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000343 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000345 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400346 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
348 return fi;
349 }
350
351 return NULL;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000355 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Al Virod878e72e2006-09-26 22:18:13 -0700357int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct fib_nh *nh;
361 unsigned int hash;
362
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700363 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 hash = fib_devindex_hashfn(dev->ifindex);
366 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800367 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700368 if (nh->fib_nh_dev == dev &&
369 nh->fib_nh_gw4 == gw &&
370 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700371 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373 }
374 }
375
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700376 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 return -1;
379}
380
Thomas Graf339bf982006-11-10 14:10:15 -0800381static inline size_t fib_nlmsg_size(struct fib_info *fi)
382{
383 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
384 + nla_total_size(4) /* RTA_TABLE */
385 + nla_total_size(4) /* RTA_DST */
386 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100387 + nla_total_size(4) /* RTA_PREFSRC */
388 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800389
390 /* space for nested metrics */
391 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
392
393 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200394 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800395 /* Also handles the special case fib_nhs == 1 */
396
397 /* each nexthop is packed in an attribute */
398 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
399
400 /* may contain flow and gateway attribute */
401 nhsize += 2 * nla_total_size(4);
402
Roopa Prabhu571e7222015-07-21 10:43:47 +0200403 /* grab encap info */
404 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700405 if (nh->fib_nh_lws) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200406 /* RTA_ENCAP_TYPE */
407 nh_encapsize += lwtunnel_get_encap_size(
David Ahernb75ed8b2019-03-27 20:53:55 -0700408 nh->fib_nh_lws);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200409 /* RTA_ENCAP */
410 nh_encapsize += nla_total_size(2);
411 }
412 } endfor_nexthops(fi);
413
Thomas Graf339bf982006-11-10 14:10:15 -0800414 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200415 payload += nla_total_size((fi->fib_nhs * nhsize) +
416 nh_encapsize);
417
Thomas Graf339bf982006-11-10 14:10:15 -0800418 }
419
420 return payload;
421}
422
Al Viro81f7bf62006-09-27 18:40:00 -0700423void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700424 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700425 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700428 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700429 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Thomas Graf339bf982006-11-10 14:10:15 -0800431 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100432 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700433 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Eric W. Biederman15e47302012-09-07 20:12:54 +0000435 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700436 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700437 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800438 if (err < 0) {
439 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
440 WARN_ON(err == -EMSGSIZE);
441 kfree_skb(skb);
442 goto errout;
443 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000444 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800445 info->nlh, GFP_KERNEL);
446 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700447errout:
448 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800449 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800452static int fib_detect_death(struct fib_info *fi, int order,
453 struct fib_info **last_resort, int *last_idx,
454 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
David Ahern619d1822019-04-05 16:30:37 -0700456 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct neighbour *n;
458 int state = NUD_NONE;
459
David Ahern619d1822019-04-05 16:30:37 -0700460 if (likely(nhc->nhc_gw_family == AF_INET))
461 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
462 else if (nhc->nhc_gw_family == AF_INET6)
463 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
464 nhc->nhc_dev);
465 else
466 n = NULL;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (n) {
469 state = n->nud_state;
470 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300471 } else {
472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800474 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000476 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000478 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300479 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *last_resort = fi;
481 *last_idx = order;
482 }
483 return 1;
484}
485
David Ahern979e2762019-03-27 20:53:58 -0700486int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
487 u16 encap_type, void *cfg, gfp_t gfp_flags,
488 struct netlink_ext_ack *extack)
489{
David Ahern0f457a32019-04-30 07:45:48 -0700490 int err;
491
492 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
493 gfp_flags);
494 if (!nhc->nhc_pcpu_rth_output)
495 return -ENOMEM;
496
David Ahern979e2762019-03-27 20:53:58 -0700497 if (encap) {
498 struct lwtunnel_state *lwtstate;
David Ahern979e2762019-03-27 20:53:58 -0700499
500 if (encap_type == LWTUNNEL_ENCAP_NONE) {
501 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
David Ahern0f457a32019-04-30 07:45:48 -0700502 err = -EINVAL;
503 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700504 }
505 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
506 cfg, &lwtstate, extack);
507 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700508 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700509
510 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
511 }
512
513 return 0;
David Ahern0f457a32019-04-30 07:45:48 -0700514
515lwt_failure:
516 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
517 nhc->nhc_pcpu_rth_output = NULL;
518 return err;
David Ahern979e2762019-03-27 20:53:58 -0700519}
520EXPORT_SYMBOL_GPL(fib_nh_common_init);
521
David Aherne4516ef2019-03-27 20:53:48 -0700522int fib_nh_init(struct net *net, struct fib_nh *nh,
523 struct fib_config *cfg, int nh_weight,
524 struct netlink_ext_ack *extack)
525{
David Ahern0f457a32019-04-30 07:45:48 -0700526 int err;
David Aherne4516ef2019-03-27 20:53:48 -0700527
David Ahernf1741732019-03-27 20:53:57 -0700528 nh->fib_nh_family = AF_INET;
529
David Ahern979e2762019-03-27 20:53:58 -0700530 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
531 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
532 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700533 return err;
David Aherne4516ef2019-03-27 20:53:48 -0700534
David Ahernb75ed8b2019-03-27 20:53:55 -0700535 nh->fib_nh_oif = cfg->fc_oif;
David Aherna4ea5d42019-04-05 16:30:30 -0700536 nh->fib_nh_gw_family = cfg->fc_gw_family;
537 if (cfg->fc_gw_family == AF_INET)
David Ahernf35b7942019-04-05 16:30:28 -0700538 nh->fib_nh_gw4 = cfg->fc_gw4;
David Aherna4ea5d42019-04-05 16:30:30 -0700539 else if (cfg->fc_gw_family == AF_INET6)
540 nh->fib_nh_gw6 = cfg->fc_gw6;
541
David Ahernb75ed8b2019-03-27 20:53:55 -0700542 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700543
544#ifdef CONFIG_IP_ROUTE_CLASSID
545 nh->nh_tclassid = cfg->fc_flow;
546 if (nh->nh_tclassid)
547 net->ipv4.fib_num_tclassid_users++;
548#endif
549#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700550 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700551#endif
552 return 0;
David Aherne4516ef2019-03-27 20:53:48 -0700553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#ifdef CONFIG_IP_ROUTE_MULTIPATH
556
David Ahern6d8422a12017-05-21 10:12:02 -0600557static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
558 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Thomas Graf4e902c52006-08-17 18:14:52 -0700562 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700564 rtnh = rtnh_next(rtnh, &remaining);
565 }
566
567 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600568 if (remaining > 0) {
569 NL_SET_ERR_MSG(extack,
570 "Invalid nexthop configuration - extra data after nexthops");
571 nhs = 0;
572 }
573
574 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Thomas Graf4e902c52006-08-17 18:14:52 -0700577static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600578 int remaining, struct fib_config *cfg,
579 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
David Aherne4516ef2019-03-27 20:53:48 -0700581 struct net *net = fi->fib_net;
582 struct fib_config fib_cfg;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200583 int ret;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700586 int attrlen;
587
David Aherne4516ef2019-03-27 20:53:48 -0700588 memset(&fib_cfg, 0, sizeof(fib_cfg));
589
David Ahernc3ab2b42017-05-21 10:12:03 -0600590 if (!rtnh_ok(rtnh, remaining)) {
591 NL_SET_ERR_MSG(extack,
592 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600594 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700595
David Ahernc3ab2b42017-05-21 10:12:03 -0600596 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
597 NL_SET_ERR_MSG(extack,
598 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300599 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600600 }
Julian Anastasov80610222016-07-10 21:11:55 +0300601
David Aherne4516ef2019-03-27 20:53:48 -0700602 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
603 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700604
605 attrlen = rtnh_attrlen(rtnh);
606 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700607 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700608
609 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700610 nlav = nla_find(attrs, attrlen, RTA_VIA);
611 if (nla && nlav) {
612 NL_SET_ERR_MSG(extack,
613 "Nexthop configuration can not contain both GATEWAY and VIA");
614 return -EINVAL;
615 }
David Ahernf35b7942019-04-05 16:30:28 -0700616 if (nla) {
David Ahernf35b7942019-04-05 16:30:28 -0700617 fib_cfg.fc_gw4 = nla_get_in_addr(nla);
David Ahernd73f80f2019-04-10 10:05:51 -0700618 if (fib_cfg.fc_gw4)
619 fib_cfg.fc_gw_family = AF_INET;
David Ahernd1566262019-04-05 16:30:40 -0700620 } else if (nlav) {
621 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
622 if (ret)
623 goto errout;
David Ahernf35b7942019-04-05 16:30:28 -0700624 }
David Aherne4516ef2019-03-27 20:53:48 -0700625
Thomas Graf4e902c52006-08-17 18:14:52 -0700626 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Aherne4516ef2019-03-27 20:53:48 -0700627 if (nla)
628 fib_cfg.fc_flow = nla_get_u32(nla);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200629
David Aherne4516ef2019-03-27 20:53:48 -0700630 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
631 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
632 if (nla)
633 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700635
David Aherne4516ef2019-03-27 20:53:48 -0700636 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
637 rtnh->rtnh_hops + 1, extack);
638 if (ret)
639 goto errout;
640
Thomas Graf4e902c52006-08-17 18:14:52 -0700641 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700643
Roopa Prabhu571e7222015-07-21 10:43:47 +0200644 ret = -EINVAL;
David Ahernb75ed8b2019-03-27 20:53:55 -0700645 if (cfg->fc_oif && fi->fib_nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700646 NL_SET_ERR_MSG(extack,
647 "Nexthop device index does not match RTA_OIF");
648 goto errout;
649 }
David Ahernf35b7942019-04-05 16:30:28 -0700650 if (cfg->fc_gw_family) {
651 if (cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family ||
652 (cfg->fc_gw_family == AF_INET &&
David Aherna4ea5d42019-04-05 16:30:30 -0700653 fi->fib_nh->fib_nh_gw4 != cfg->fc_gw4) ||
654 (cfg->fc_gw_family == AF_INET6 &&
655 ipv6_addr_cmp(&fi->fib_nh->fib_nh_gw6, &cfg->fc_gw6))) {
David Ahernf35b7942019-04-05 16:30:28 -0700656 NL_SET_ERR_MSG(extack,
David Aherna4ea5d42019-04-05 16:30:30 -0700657 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
David Ahernf35b7942019-04-05 16:30:28 -0700658 goto errout;
659 }
David Aherne4516ef2019-03-27 20:53:48 -0700660 }
661#ifdef CONFIG_IP_ROUTE_CLASSID
662 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
663 NL_SET_ERR_MSG(extack,
664 "Nexthop class id does not match RTA_FLOW");
665 goto errout;
666 }
667#endif
668 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200669errout:
670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Peter Nørlund0e884c72015-09-30 10:12:21 +0200673static void fib_rebalance(struct fib_info *fi)
674{
675 int total;
676 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200677
678 if (fi->fib_nhs < 2)
679 return;
680
681 total = 0;
682 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700683 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200684 continue;
685
David Ahernb75ed8b2019-03-27 20:53:55 -0700686 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
687 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200688 continue;
689
David Ahernb75ed8b2019-03-27 20:53:55 -0700690 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200691 } endfor_nexthops(fi);
692
693 w = 0;
694 change_nexthops(fi) {
695 int upper_bound;
696
David Ahernb75ed8b2019-03-27 20:53:55 -0700697 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200698 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700699 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
700 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200701 upper_bound = -1;
702 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700703 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200704 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
705 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200706 }
707
David Ahernb75ed8b2019-03-27 20:53:55 -0700708 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200709 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200710}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200711#else /* CONFIG_IP_ROUTE_MULTIPATH */
712
David Ahern8373c6c2019-03-27 20:53:46 -0700713static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
714 int remaining, struct fib_config *cfg,
715 struct netlink_ext_ack *extack)
716{
717 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
718
719 return -EINVAL;
720}
721
Peter Nørlund0e884c72015-09-30 10:12:21 +0200722#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200723
724#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David Ahern30357d72017-01-30 12:07:37 -0800726static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800727 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800728 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600729 const struct fib_config *cfg,
730 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200731{
732 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200733 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200734
735 if (encap_type == LWTUNNEL_ENCAP_NONE)
736 return 0;
737
David Ahern9ae28722017-05-27 16:19:28 -0600738 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
739 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200740 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700741 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200742 lwtstate_free(lwtstate);
743 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200744
Jiri Bencdf383e62015-08-18 18:41:13 +0200745 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200746}
747
David Ahern9ae28722017-05-27 16:19:28 -0600748int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
749 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700752 struct rtnexthop *rtnh;
753 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif
755
Thomas Graf4e902c52006-08-17 18:14:52 -0700756 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 1;
758
David Ahernf35b7942019-04-05 16:30:28 -0700759 if (cfg->fc_oif || cfg->fc_gw_family) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200760 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600761 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
762 fi->fib_nh, cfg, extack))
763 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200764 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100765#ifdef CONFIG_IP_ROUTE_CLASSID
766 if (cfg->fc_flow &&
767 cfg->fc_flow != fi->fib_nh->nh_tclassid)
768 return 1;
769#endif
David Ahernf35b7942019-04-05 16:30:28 -0700770 if ((cfg->fc_oif && cfg->fc_oif != fi->fib_nh->fib_nh_oif) ||
771 (cfg->fc_gw_family &&
772 cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family))
773 return 1;
774
775 if (cfg->fc_gw_family == AF_INET &&
776 cfg->fc_gw4 != fi->fib_nh->fib_nh_gw4)
777 return 1;
778
David Aherna4ea5d42019-04-05 16:30:30 -0700779 if (cfg->fc_gw_family == AF_INET6 &&
780 ipv6_addr_cmp(&cfg->fc_gw6, &fi->fib_nh->fib_nh_gw6))
781 return 1;
782
David Ahernf35b7942019-04-05 16:30:28 -0700783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100787 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700789
790 rtnh = cfg->fc_mp;
791 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700794 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Graf4e902c52006-08-17 18:14:52 -0700796 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700798
David Ahernb75ed8b2019-03-27 20:53:55 -0700799 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700801
802 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200803 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700804 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700805
806 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700807 nlav = nla_find(attrs, attrlen, RTA_VIA);
808 if (nla && nlav) {
809 NL_SET_ERR_MSG(extack,
810 "Nexthop configuration can not contain both GATEWAY and VIA");
811 return -EINVAL;
812 }
813
814 if (nla) {
815 if (nh->fib_nh_gw_family != AF_INET ||
816 nla_get_in_addr(nla) != nh->fib_nh_gw4)
817 return 1;
818 } else if (nlav) {
819 struct fib_config cfg2;
820 int err;
821
822 err = fib_gw_from_via(&cfg2, nlav, extack);
823 if (err)
824 return err;
825
826 switch (nh->fib_nh_gw_family) {
827 case AF_INET:
828 if (cfg2.fc_gw_family != AF_INET ||
829 cfg2.fc_gw4 != nh->fib_nh_gw4)
830 return 1;
831 break;
832 case AF_INET6:
833 if (cfg2.fc_gw_family != AF_INET6 ||
834 ipv6_addr_cmp(&cfg2.fc_gw6,
835 &nh->fib_nh_gw6))
836 return 1;
837 break;
838 }
839 }
840
Patrick McHardyc7066f72011-01-14 13:36:42 +0100841#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700842 nla = nla_find(attrs, attrlen, RTA_FLOW);
843 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 1;
845#endif
846 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700847
848 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } endfor_nexthops(fi);
850#endif
851 return 0;
852}
853
Xin Long5f9ae3d2017-08-23 10:07:26 +0800854bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
855{
856 struct nlattr *nla;
857 int remaining;
858
859 if (!cfg->fc_mx)
860 return true;
861
862 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
863 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +0100864 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800865
866 if (!type)
867 continue;
868 if (type > RTAX_MAX)
869 return false;
870
871 if (type == RTAX_CC_ALGO) {
872 char tmp[TCP_CA_NAME_MAX];
873 bool ecn_ca = false;
874
875 nla_strlcpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -0800876 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +0800877 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -0700878 if (nla_len(nla) != sizeof(u32))
879 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800880 val = nla_get_u32(nla);
881 }
882
Phil Sutterd03a4552017-12-19 15:17:13 +0100883 fi_val = fi->fib_metrics->metrics[type - 1];
884 if (type == RTAX_FEATURES)
885 fi_val &= ~DST_FEATURE_ECN_CA;
886
887 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +0800888 return false;
889 }
890
891 return true;
892}
893
David Ahern717a8f52019-04-05 16:30:32 -0700894static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
895 u32 table, struct netlink_ext_ack *extack)
896{
897 struct fib6_config cfg = {
898 .fc_table = table,
899 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
900 .fc_ifindex = nh->fib_nh_oif,
901 .fc_gateway = nh->fib_nh_gw6,
902 };
903 struct fib6_nh fib6_nh = {};
904 int err;
905
906 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
907 if (!err) {
908 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
909 dev_hold(nh->fib_nh_dev);
910 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
911 nh->fib_nh_scope = RT_SCOPE_LINK;
912
913 ipv6_stub->fib6_nh_release(&fib6_nh);
914 }
915
916 return err;
917}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000920 * Picture
921 * -------
922 *
923 * Semantics of nexthop is very messy by historical reasons.
924 * We have to take into account, that:
925 * a) gateway can be actually local interface address,
926 * so that gatewayed route is direct.
927 * b) gateway must be on-link address, possibly
928 * described not by an ifaddr, but also by a direct route.
929 * c) If both gateway and interface are specified, they should not
930 * contradict.
931 * d) If we use tunnel routes, gateway could be not on-link.
932 *
933 * Attempt to reconcile all of these (alas, self-contradictory) conditions
934 * results in pretty ugly and hairy code with obscure logic.
935 *
936 * I chose to generalized it instead, so that the size
937 * of code does not increase practically, but it becomes
938 * much more general.
939 * Every prefix is assigned a "scope" value: "host" is local address,
940 * "link" is direct route,
941 * [ ... "site" ... "interior" ... ]
942 * and "universe" is true gateway route with global meaning.
943 *
944 * Every prefix refers to a set of "nexthop"s (gw, oif),
945 * where gw must have narrower scope. This recursion stops
946 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
947 * which means that gw is forced to be on link.
948 *
949 * Code is still hairy, but now it is apparently logically
950 * consistent and very flexible. F.e. as by-product it allows
951 * to co-exists in peace independent exterior and interior
952 * routing processes.
953 *
954 * Normally it looks as following.
955 *
956 * {universe prefix} -> (gw, oif) [scope link]
957 * |
958 * |-> {link prefix} -> (gw, oif) [scope local]
959 * |
960 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
David Ahern448d7242019-04-05 16:30:31 -0700962static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
963 u8 scope, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000965 struct net_device *dev;
David Ahern448d7242019-04-05 16:30:31 -0700966 struct fib_result res;
967 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
David Ahern448d7242019-04-05 16:30:31 -0700969 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
970 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
David Ahern448d7242019-04-05 16:30:31 -0700972 if (scope >= RT_SCOPE_LINK) {
973 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
David Ahern448d7242019-04-05 16:30:31 -0700976 dev = __dev_get_by_index(net, nh->fib_nh_oif);
977 if (!dev) {
978 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
979 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
David Ahern448d7242019-04-05 16:30:31 -0700981 if (!(dev->flags & IFF_UP)) {
982 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
983 return -ENETDOWN;
984 }
985 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
986 if (addr_type != RTN_UNICAST) {
987 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
988 return -EINVAL;
989 }
990 if (!netif_carrier_ok(dev))
991 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
992 nh->fib_nh_dev = dev;
993 dev_hold(dev);
994 nh->fib_nh_scope = RT_SCOPE_LINK;
995 return 0;
996 }
997 rcu_read_lock();
998 {
999 struct fib_table *tbl = NULL;
1000 struct flowi4 fl4 = {
1001 .daddr = nh->fib_nh_gw4,
1002 .flowi4_scope = scope + 1,
1003 .flowi4_oif = nh->fib_nh_oif,
1004 .flowi4_iif = LOOPBACK_IFINDEX,
1005 };
1006
1007 /* It is not necessary, but requires a bit of thinking */
1008 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1009 fl4.flowi4_scope = RT_SCOPE_LINK;
1010
1011 if (table)
1012 tbl = fib_get_table(net, table);
1013
1014 if (tbl)
1015 err = fib_table_lookup(tbl, &fl4, &res,
1016 FIB_LOOKUP_IGNORE_LINKSTATE |
1017 FIB_LOOKUP_NOREF);
1018
1019 /* on error or if no table given do full lookup. This
1020 * is needed for example when nexthops are in the local
1021 * table rather than the given table
1022 */
1023 if (!tbl || err) {
1024 err = fib_lookup(net, &fl4, &res,
1025 FIB_LOOKUP_IGNORE_LINKSTATE);
1026 }
1027
1028 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001029 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -06001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
David Ahern448d7242019-04-05 16:30:31 -07001033
1034 err = -EINVAL;
1035 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1036 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1037 goto out;
1038 }
1039 nh->fib_nh_scope = res.scope;
1040 nh->fib_nh_oif = FIB_RES_OIF(res);
1041 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1042 if (!dev) {
1043 NL_SET_ERR_MSG(extack,
1044 "No egress device for nexthop gateway");
1045 goto out;
1046 }
1047 dev_hold(dev);
1048 if (!netif_carrier_ok(dev))
1049 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1050 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +00001051out:
1052 rcu_read_unlock();
1053 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
David Ahern448d7242019-04-05 16:30:31 -07001056static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1057 struct netlink_ext_ack *extack)
1058{
1059 struct in_device *in_dev;
1060 int err;
1061
1062 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1063 NL_SET_ERR_MSG(extack,
1064 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1065 return -EINVAL;
1066 }
1067
1068 rcu_read_lock();
1069
1070 err = -ENODEV;
1071 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1072 if (!in_dev)
1073 goto out;
1074 err = -ENETDOWN;
1075 if (!(in_dev->dev->flags & IFF_UP)) {
1076 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1077 goto out;
1078 }
1079
1080 nh->fib_nh_dev = in_dev->dev;
1081 dev_hold(nh->fib_nh_dev);
1082 nh->fib_nh_scope = RT_SCOPE_HOST;
1083 if (!netif_carrier_ok(nh->fib_nh_dev))
1084 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1085 err = 0;
1086out:
1087 rcu_read_unlock();
1088 return err;
1089}
1090
1091static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
1092 struct netlink_ext_ack *extack)
1093{
1094 struct net *net = cfg->fc_nlinfo.nl_net;
1095 u32 table = cfg->fc_table;
1096 int err;
1097
1098 if (nh->fib_nh_gw_family == AF_INET)
1099 err = fib_check_nh_v4_gw(net, nh, table, cfg->fc_scope, extack);
David Ahern717a8f52019-04-05 16:30:32 -07001100 else if (nh->fib_nh_gw_family == AF_INET6)
1101 err = fib_check_nh_v6_gw(net, nh, table, extack);
David Ahern448d7242019-04-05 16:30:31 -07001102 else
1103 err = fib_check_nh_nongw(net, nh, extack);
1104
1105 return err;
1106}
1107
Al Viro81f7bf62006-09-27 18:40:00 -07001108static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
David S. Miller123b9732011-02-01 15:34:21 -08001110 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001112 return ((__force u32)val ^
1113 ((__force u32)val >> 7) ^
1114 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
David S. Miller123b9732011-02-01 15:34:21 -08001117static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +08001120 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 else
1122 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001123 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1124 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
David S. Miller123b9732011-02-01 15:34:21 -08001127static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 if (!hash)
1130 return;
1131
1132 if (bytes <= PAGE_SIZE)
1133 kfree(hash);
1134 else
1135 free_pages((unsigned long) hash, get_order(bytes));
1136}
1137
David S. Miller123b9732011-02-01 15:34:21 -08001138static void fib_info_hash_move(struct hlist_head *new_info_hash,
1139 struct hlist_head *new_laddrhash,
1140 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
David S. Millerb7656e72005-08-05 04:12:48 -07001142 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001143 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001144 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001146 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001147 old_info_hash = fib_info_hash;
1148 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001149 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 for (i = 0; i < old_size; i++) {
1152 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001153 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct fib_info *fi;
1155
Sasha Levinb67bfe02013-02-27 17:06:00 -08001156 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct hlist_head *dest;
1158 unsigned int new_hash;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 new_hash = fib_info_hashfn(fi);
1161 dest = &new_info_hash[new_hash];
1162 hlist_add_head(&fi->fib_hash, dest);
1163 }
1164 }
1165 fib_info_hash = new_info_hash;
1166
1167 for (i = 0; i < old_size; i++) {
1168 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001169 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct fib_info *fi;
1171
Sasha Levinb67bfe02013-02-27 17:06:00 -08001172 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct hlist_head *ldest;
1174 unsigned int new_hash;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1177 ldest = &new_laddrhash[new_hash];
1178 hlist_add_head(&fi->fib_lhash, ldest);
1179 }
1180 }
1181 fib_info_laddrhash = new_laddrhash;
1182
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001183 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001184
1185 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001186 fib_info_hash_free(old_info_hash, bytes);
1187 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
David S. Miller436c3b62011-03-24 17:42:21 -07001190__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1191{
David Ahernb75ed8b2019-03-27 20:53:55 -07001192 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev,
1193 nh->fib_nh_gw4,
David S. Miller37e826c2011-03-24 18:06:47 -07001194 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001195 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1196
1197 return nh->nh_saddr;
1198}
1199
David Aherneba618a2019-04-02 14:11:55 -07001200__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1201{
1202 struct fib_nh_common *nhc = res->nhc;
1203 struct fib_nh *nh;
1204
1205 if (res->fi->fib_prefsrc)
1206 return res->fi->fib_prefsrc;
1207
1208 nh = container_of(nhc, struct fib_nh, nh_common);
1209 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1210 return nh->nh_saddr;
1211
1212 return fib_info_update_nh_saddr(net, nh);
1213}
1214
David Ahern021dd3b2015-08-13 14:59:06 -06001215static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1216{
1217 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1218 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001219 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001220 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001221
1222 if (tb_id == RT_TABLE_MAIN)
1223 tb_id = RT_TABLE_LOCAL;
1224
David Aherne1b8d902015-11-03 15:59:28 -08001225 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1226 fib_prefsrc, tb_id);
1227
1228 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1229 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1230 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001231 }
David Aherne1b8d902015-11-03 15:59:28 -08001232
1233 if (rc != RTN_LOCAL)
1234 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001235 }
1236 return true;
1237}
1238
David Ahern6d8422a12017-05-21 10:12:02 -06001239struct fib_info *fib_create_info(struct fib_config *cfg,
1240 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 int err;
1243 struct fib_info *fi = NULL;
1244 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001246 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
David S. Miller4c8237c2011-03-07 14:27:38 -08001248 if (cfg->fc_type > RTN_MAX)
1249 goto err_inval;
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001252 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1253 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
David Ahernc3ab2b42017-05-21 10:12:03 -06001257 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1258 NL_SET_ERR_MSG(extack,
1259 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001260 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001261 }
Julian Anastasov80610222016-07-10 21:11:55 +03001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001264 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001265 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (nhs == 0)
1267 goto err_inval;
1268 }
1269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001272 if (fib_info_cnt >= fib_info_hash_size) {
1273 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct hlist_head *new_info_hash;
1275 struct hlist_head *new_laddrhash;
1276 unsigned int bytes;
1277
1278 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001279 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001281 new_info_hash = fib_info_hash_alloc(bytes);
1282 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001284 fib_info_hash_free(new_info_hash, bytes);
1285 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001286 } else
David S. Miller123b9732011-02-01 15:34:21 -08001287 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
David S. Miller123b9732011-02-01 15:34:21 -08001289 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto failure;
1291 }
1292
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001293 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001294 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001296 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001297 cfg->fc_mx_len, extack);
David Ahern767a2212018-10-04 20:07:51 -07001298 if (unlikely(IS_ERR(fi->fib_metrics))) {
1299 err = PTR_ERR(fi->fib_metrics);
1300 kfree(fi);
1301 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001302 }
David Ahern767a2212018-10-04 20:07:51 -07001303
Eric Dumazet187e5b32017-08-15 05:26:17 -07001304 fib_info_cnt++;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001305 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001306 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001307 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001308 fi->fib_flags = cfg->fc_flags;
1309 fi->fib_priority = cfg->fc_priority;
1310 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001311 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001312 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 fi->fib_nhs = nhs;
1315 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001316 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 } endfor_nexthops(fi)
1318
David Aherne4516ef2019-03-27 20:53:48 -07001319 if (cfg->fc_mp)
David Ahern6d8422a12017-05-21 10:12:02 -06001320 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
David Aherne4516ef2019-03-27 20:53:48 -07001321 else
1322 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001323
David Aherne4516ef2019-03-27 20:53:48 -07001324 if (err != 0)
1325 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Thomas Graf4e902c52006-08-17 18:14:52 -07001327 if (fib_props[cfg->fc_type].error) {
David Ahernf35b7942019-04-05 16:30:28 -07001328 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001329 NL_SET_ERR_MSG(extack,
1330 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001334 } else {
1335 switch (cfg->fc_type) {
1336 case RTN_UNICAST:
1337 case RTN_LOCAL:
1338 case RTN_BROADCAST:
1339 case RTN_ANYCAST:
1340 case RTN_MULTICAST:
1341 break;
1342 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001343 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001344 goto err_inval;
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
David Ahernc3ab2b42017-05-21 10:12:03 -06001348 if (cfg->fc_scope > RT_SCOPE_HOST) {
1349 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Thomas Graf4e902c52006-08-17 18:14:52 -07001353 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 struct fib_nh *nh = fi->fib_nh;
1355
1356 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001357 if (nhs != 1) {
1358 NL_SET_ERR_MSG(extack,
1359 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001360 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001361 }
David Ahernbdf00462019-04-05 16:30:26 -07001362 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001363 NL_SET_ERR_MSG(extack,
1364 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001366 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001367 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1368 nh->fib_nh_dev = dev_get_by_index(net, fi->fib_nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001370 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto failure;
1372 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001373 int linkdown = 0;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 change_nexthops(fi) {
David Ahernfa8fefa2017-09-27 20:41:59 -07001376 err = fib_check_nh(cfg, nexthop_nh, extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001377 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001379 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001380 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001382 if (linkdown == fi->fib_nhs)
1383 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
David Ahernc3ab2b42017-05-21 10:12:03 -06001386 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1387 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001388 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David S. Miller1fc050a2011-03-07 20:54:48 -08001391 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001392 fib_info_update_nh_saddr(net, nexthop_nh);
David Ahern19a9d132019-04-05 16:30:39 -07001393 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1394 fi->fib_nh_is_v6 = true;
David S. Miller1fc050a2011-03-07 20:54:48 -08001395 } endfor_nexthops(fi)
1396
Peter Nørlund0e884c72015-09-30 10:12:21 +02001397 fib_rebalance(fi);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001400 ofi = fib_find_info(fi);
1401 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 fi->fib_dead = 1;
1403 free_fib_info(fi);
1404 ofi->fib_treeref++;
1405 return ofi;
1406 }
1407
1408 fi->fib_treeref++;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001409 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001410 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 hlist_add_head(&fi->fib_hash,
1412 &fib_info_hash[fib_info_hashfn(fi)]);
1413 if (fi->fib_prefsrc) {
1414 struct hlist_head *head;
1415
1416 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1417 hlist_add_head(&fi->fib_lhash, head);
1418 }
1419 change_nexthops(fi) {
1420 struct hlist_head *head;
1421 unsigned int hash;
1422
David Ahernb75ed8b2019-03-27 20:53:55 -07001423 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001425 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001427 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001429 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return fi;
1431
1432err_inval:
1433 err = -EINVAL;
1434
1435failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001436 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 fi->fib_dead = 1;
1438 free_fib_info(fi);
1439 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001440
1441 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
David Ahernc0a72072019-04-02 14:11:58 -07001444int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
David Ahernecc56632019-04-23 08:48:09 -07001445 unsigned char *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001446{
David Ahernc2364192019-04-02 14:11:57 -07001447 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001448 *flags |= RTNH_F_DEAD;
1449
David Ahernc2364192019-04-02 14:11:57 -07001450 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001451 *flags |= RTNH_F_LINKDOWN;
1452
1453 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001454 switch (nhc->nhc_family) {
1455 case AF_INET:
1456 if (ip_ignore_linkdown(nhc->nhc_dev))
1457 *flags |= RTNH_F_DEAD;
1458 break;
David Ahernc0a72072019-04-02 14:11:58 -07001459 case AF_INET6:
1460 if (ip6_ignore_linkdown(nhc->nhc_dev))
1461 *flags |= RTNH_F_DEAD;
1462 break;
David Ahernc2364192019-04-02 14:11:57 -07001463 }
David Ahernb0f60192019-04-02 14:11:56 -07001464 rcu_read_unlock();
1465 }
1466
David Ahernbdf00462019-04-05 16:30:26 -07001467 switch (nhc->nhc_gw_family) {
1468 case AF_INET:
1469 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1470 goto nla_put_failure;
1471 break;
1472 case AF_INET6:
David Ahernd1566262019-04-05 16:30:40 -07001473 /* if gateway family does not match nexthop family
1474 * gateway is encoded as RTA_VIA
1475 */
1476 if (nhc->nhc_gw_family != nhc->nhc_family) {
1477 int alen = sizeof(struct in6_addr);
1478 struct nlattr *nla;
1479 struct rtvia *via;
1480
1481 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1482 if (!nla)
1483 goto nla_put_failure;
1484
1485 via = nla_data(nla);
1486 via->rtvia_family = AF_INET6;
1487 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1488 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1489 &nhc->nhc_gw.ipv6) < 0) {
David Ahernbdf00462019-04-05 16:30:26 -07001490 goto nla_put_failure;
David Ahernd1566262019-04-05 16:30:40 -07001491 }
David Ahernbdf00462019-04-05 16:30:26 -07001492 break;
David Ahernc2364192019-04-02 14:11:57 -07001493 }
David Ahernb0f60192019-04-02 14:11:56 -07001494
David Ahernc2364192019-04-02 14:11:57 -07001495 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1496 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
David Ahernb0f60192019-04-02 14:11:56 -07001497 *flags |= RTNH_F_OFFLOAD;
1498
David Ahernc2364192019-04-02 14:11:57 -07001499 if (!skip_oif && nhc->nhc_dev &&
1500 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001501 goto nla_put_failure;
1502
David Ahernc2364192019-04-02 14:11:57 -07001503 if (nhc->nhc_lwtstate &&
David Ahernffa8ce52019-04-23 08:23:41 -07001504 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1505 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001506 goto nla_put_failure;
1507
1508 return 0;
1509
1510nla_put_failure:
1511 return -EMSGSIZE;
1512}
David Ahernc0a72072019-04-02 14:11:58 -07001513EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001514
David Ahernc0a72072019-04-02 14:11:58 -07001515#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1516int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1517 int nh_weight)
David Ahernb0f60192019-04-02 14:11:56 -07001518{
David Ahernc2364192019-04-02 14:11:57 -07001519 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001520 struct rtnexthop *rtnh;
David Ahernecc56632019-04-23 08:48:09 -07001521 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001522
1523 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1524 if (!rtnh)
1525 goto nla_put_failure;
1526
David Ahernc2364192019-04-02 14:11:57 -07001527 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001528 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1529
David Ahernc2364192019-04-02 14:11:57 -07001530 if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001531 goto nla_put_failure;
1532
1533 rtnh->rtnh_flags = flags;
1534
1535 /* length of rtnetlink header + attributes */
1536 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1537
1538 return 0;
1539
1540nla_put_failure:
1541 return -EMSGSIZE;
1542}
David Ahernc0a72072019-04-02 14:11:58 -07001543EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001544#endif
David Ahernb0f60192019-04-02 14:11:56 -07001545
David Ahernc2364192019-04-02 14:11:57 -07001546#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001547static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1548{
1549 struct nlattr *mp;
1550
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001551 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernb0f60192019-04-02 14:11:56 -07001552 if (!mp)
1553 goto nla_put_failure;
1554
1555 for_nexthops(fi) {
David Ahernc2364192019-04-02 14:11:57 -07001556 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001557 goto nla_put_failure;
1558#ifdef CONFIG_IP_ROUTE_CLASSID
1559 if (nh->nh_tclassid &&
1560 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1561 goto nla_put_failure;
1562#endif
1563 } endfor_nexthops(fi);
1564
1565 nla_nest_end(skb, mp);
1566
1567 return 0;
1568
1569nla_put_failure:
1570 return -EMSGSIZE;
1571}
1572#else
1573static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1574{
1575 return 0;
1576}
1577#endif
1578
Eric W. Biederman15e47302012-09-07 20:12:54 +00001579int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001580 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001581 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001583 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Eric W. Biederman15e47302012-09-07 20:12:54 +00001586 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001587 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001588 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001589
1590 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 rtm->rtm_family = AF_INET;
1592 rtm->rtm_dst_len = dst_len;
1593 rtm->rtm_src_len = 0;
1594 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001595 if (tb_id < 256)
1596 rtm->rtm_table = tb_id;
1597 else
1598 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001599 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1600 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 rtm->rtm_type = type;
1602 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001603 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001605
David S. Millerf3756b72012-04-01 20:39:02 -04001606 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001607 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001608 goto nla_put_failure;
1609 if (fi->fib_priority &&
1610 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1611 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001612 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001613 goto nla_put_failure;
1614
David S. Millerf3756b72012-04-01 20:39:02 -04001615 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001616 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001617 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (fi->fib_nhs == 1) {
David Ahernb0f60192019-04-02 14:11:56 -07001619 struct fib_nh *nh = &fi->fib_nh[0];
David Ahernecc56632019-04-23 08:48:09 -07001620 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001621
David Ahernc2364192019-04-02 14:11:57 -07001622 if (fib_nexthop_info(skb, &nh->nh_common, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001623 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001624
1625 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001626#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernb0f60192019-04-02 14:11:56 -07001627 if (nh->nh_tclassid &&
1628 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
David S. Millerf3756b72012-04-01 20:39:02 -04001629 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001630#endif
David Ahernb0f60192019-04-02 14:11:56 -07001631 } else {
1632 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001633 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001635
Johannes Berg053c0952015-01-16 22:09:00 +01001636 nlmsg_end(skb, nlh);
1637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Thomas Grafbe403ea2006-08-17 18:15:17 -07001639nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001640 nlmsg_cancel(skb, nlh);
1641 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001645 * Update FIB if:
1646 * - local address disappeared -> we must delete all the entries
1647 * referring to it.
1648 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001650int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001651{
1652 int ret = 0;
1653 unsigned int hash = fib_laddr_hashfn(local);
1654 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001655 struct net *net = dev_net(dev);
1656 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001657 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Ian Morris51456b22015-04-03 09:17:26 +01001659 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001660 return 0;
1661
Sasha Levinb67bfe02013-02-27 17:06:00 -08001662 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001663 if (!net_eq(fi->fib_net, net) ||
1664 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001665 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001666 if (fi->fib_prefsrc == local) {
1667 fi->fib_flags |= RTNH_F_DEAD;
1668 ret++;
1669 }
1670 }
1671 return ret;
1672}
1673
David Ahernb75ed8b2019-03-27 20:53:55 -07001674static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001675 enum fib_event_type event_type)
1676{
David Ahernb75ed8b2019-03-27 20:53:55 -07001677 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001678 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001679 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001680 };
1681
1682 switch (event_type) {
1683 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001684 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001685 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001686 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001687 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001688 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001689 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001690 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001691 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1692 (nh->fib_nh_flags & RTNH_F_DEAD))
1693 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001694 event_type, &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001695 default:
1696 break;
1697 }
1698
1699 return NOTIFY_DONE;
1700}
1701
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001702/* Update the PMTU of exceptions when:
1703 * - the new MTU of the first hop becomes smaller than the PMTU
1704 * - the old MTU was the same as the PMTU, and it limited discovery of
1705 * larger MTUs on the path. With that limit raised, we can now
1706 * discover larger MTUs
1707 * A special case is locked exceptions, for which the PMTU is smaller
1708 * than the minimal accepted PMTU:
1709 * - if the new MTU is greater than the PMTU, don't make any change
1710 * - otherwise, unlock and set PMTU
1711 */
David Aherna5995e72019-04-30 07:45:50 -07001712static void nh_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001713{
1714 struct fnhe_hash_bucket *bucket;
1715 int i;
1716
David Aherna5995e72019-04-30 07:45:50 -07001717 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001718 if (!bucket)
1719 return;
1720
1721 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1722 struct fib_nh_exception *fnhe;
1723
1724 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1725 fnhe;
1726 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1727 if (fnhe->fnhe_mtu_locked) {
1728 if (new <= fnhe->fnhe_pmtu) {
1729 fnhe->fnhe_pmtu = new;
1730 fnhe->fnhe_mtu_locked = false;
1731 }
1732 } else if (new < fnhe->fnhe_pmtu ||
1733 orig == fnhe->fnhe_pmtu) {
1734 fnhe->fnhe_pmtu = new;
1735 }
1736 }
1737 }
1738}
1739
1740void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1741{
1742 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1743 struct hlist_head *head = &fib_info_devhash[hash];
1744 struct fib_nh *nh;
1745
1746 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001747 if (nh->fib_nh_dev == dev)
David Aherna5995e72019-04-30 07:45:50 -07001748 nh_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001749 }
1750}
1751
Julian Anastasov4f823de2015-10-30 10:23:33 +02001752/* Event force Flags Description
1753 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1754 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1755 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1756 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1757 */
1758int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 int ret = 0;
1761 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001762 struct fib_info *prev_fi = NULL;
1763 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1764 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001765 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001766
Julian Anastasov4f823de2015-10-30 10:23:33 +02001767 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 scope = -1;
1769
Sasha Levinb67bfe02013-02-27 17:06:00 -08001770 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001771 struct fib_info *fi = nh->nh_parent;
1772 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001774 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001775 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001776 continue;
1777 prev_fi = fi;
1778 dead = 0;
1779 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001780 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001781 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001782 else if (nexthop_nh->fib_nh_dev == dev &&
1783 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001784 switch (event) {
1785 case NETDEV_DOWN:
1786 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07001787 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001788 /* fall through */
1789 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07001790 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001791 break;
1792 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001793 call_fib_nh_notifiers(nexthop_nh,
1794 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001795 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001797#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001798 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07001799 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001800 dead = fi->fib_nhs;
1801 break;
1802 }
1803#endif
1804 } endfor_nexthops(fi)
1805 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001806 switch (event) {
1807 case NETDEV_DOWN:
1808 case NETDEV_UNREGISTER:
1809 fi->fib_flags |= RTNH_F_DEAD;
1810 /* fall through */
1811 case NETDEV_CHANGE:
1812 fi->fib_flags |= RTNH_F_LINKDOWN;
1813 break;
1814 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001815 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001817
1818 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
1821 return ret;
1822}
1823
David S. Miller0c838ff2011-01-31 16:16:50 -08001824/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001825static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001826{
1827 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001828 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001829 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001830 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001831 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001832 struct fib_alias *fa, *fa1 = NULL;
1833 u32 last_prio = res->fi->fib_priority;
1834 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001835
Alexander Duyck56315f92015-02-25 15:31:31 -08001836 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001837 struct fib_info *next_fi = fa->fa_info;
1838
Julian Anastasov18a912e92015-07-22 10:43:22 +03001839 if (fa->fa_slen != slen)
1840 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001841 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1842 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001843 if (fa->tb_id != tb->tb_id)
1844 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001845 if (next_fi->fib_priority > last_prio &&
1846 fa->fa_tos == last_tos) {
1847 if (last_tos)
1848 continue;
1849 break;
1850 }
1851 if (next_fi->fib_flags & RTNH_F_DEAD)
1852 continue;
1853 last_tos = fa->fa_tos;
1854 last_prio = next_fi->fib_priority;
1855
David S. Miller37e826c2011-03-24 18:06:47 -07001856 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001857 fa->fa_type != RTN_UNICAST)
1858 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001859 if (!next_fi->fib_nh[0].fib_nh_gw4 ||
1860 next_fi->fib_nh[0].fib_nh_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08001861 continue;
1862
1863 fib_alias_accessed(fa);
1864
Ian Morris51456b22015-04-03 09:17:26 +01001865 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001866 if (next_fi != res->fi)
1867 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001868 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001869 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001870 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001871 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001872 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001873 goto out;
1874 }
1875 fi = next_fi;
1876 order++;
1877 }
1878
Ian Morris51456b22015-04-03 09:17:26 +01001879 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001880 if (fa1)
1881 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001882 goto out;
1883 }
1884
1885 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001886 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001887 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001888 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001889 goto out;
1890 }
1891
1892 if (last_idx >= 0)
1893 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001894 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001895out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001896 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001897}
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001900 * Dead device goes up. We wake up dead nexthops.
1901 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
David Ahernecc56632019-04-23 08:48:09 -07001903int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct fib_info *prev_fi;
1906 unsigned int hash;
1907 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct fib_nh *nh;
1909 int ret;
1910
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001911 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
1913
Julian Anastasovc9b32922015-10-30 10:23:34 +02001914 if (nh_flags & RTNH_F_DEAD) {
1915 unsigned int flags = dev_get_flags(dev);
1916
1917 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1918 nh_flags |= RTNH_F_LINKDOWN;
1919 }
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 prev_fi = NULL;
1922 hash = fib_devindex_hashfn(dev->ifindex);
1923 head = &fib_info_devhash[hash];
1924 ret = 0;
1925
Sasha Levinb67bfe02013-02-27 17:06:00 -08001926 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct fib_info *fi = nh->nh_parent;
1928 int alive;
1929
1930 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001931 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 continue;
1933
1934 prev_fi = fi;
1935 alive = 0;
1936 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001937 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 alive++;
1939 continue;
1940 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001941 if (!nexthop_nh->fib_nh_dev ||
1942 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001944 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08001945 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 continue;
1947 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001948 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001949 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 } endfor_nexthops(fi)
1951
1952 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001953 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 ret++;
1955 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001956
1957 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
1960 return ret;
1961}
1962
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001963#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001964static bool fib_good_nh(const struct fib_nh *nh)
1965{
1966 int state = NUD_REACHABLE;
1967
David Ahernb75ed8b2019-03-27 20:53:55 -07001968 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07001969 struct neighbour *n;
1970
1971 rcu_read_lock_bh();
1972
David Ahern1a38c432019-04-05 16:30:38 -07001973 if (likely(nh->fib_nh_gw_family == AF_INET))
1974 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1975 (__force u32)nh->fib_nh_gw4);
1976 else if (nh->fib_nh_gw_family == AF_INET6)
1977 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
1978 &nh->fib_nh_gw6);
1979 else
1980 n = NULL;
David Aherna6db4492016-04-07 07:21:00 -07001981 if (n)
1982 state = n->nud_state;
1983
1984 rcu_read_unlock_bh();
1985 }
1986
1987 return !!(state & NUD_VALID);
1988}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001989
Peter Nørlund0e884c72015-09-30 10:12:21 +02001990void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001993 struct net *net = fi->fib_net;
1994 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
David Aherneba618a2019-04-02 14:11:55 -07001996 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08001997 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07001998 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08001999 continue;
2000 if (!first) {
2001 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002002 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002003 first = true;
2004 }
2005 }
2006
David Aherneba618a2019-04-02 14:11:55 -07002007 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02002008 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Xin Long6174a302018-04-01 22:40:35 +08002010 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002011 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015#endif
David Ahern3ce58d82015-10-05 08:51:25 -07002016
2017void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002018 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07002019{
David Ahern0d876f22018-02-13 08:11:34 -08002020 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2021 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08002022
David Ahern3ce58d82015-10-05 08:51:25 -07002023#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern0d876f22018-02-13 08:11:34 -08002024 if (res->fi->fib_nhs > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08002025 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01002026
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002027 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07002028 }
2029 else
2030#endif
2031 if (!res->prefixlen &&
2032 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08002033 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07002034 fib_select_default(fl4, res);
2035
David Ahern0d876f22018-02-13 08:11:34 -08002036check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07002037 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07002038 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07002039}