blob: d3e26e55f2e1c047661a88661ea59af55f40935e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Melo14c85022005-12-27 02:43:12 -020028#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/proc_fs.h>
32#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
David Ahernc3ab2b42017-05-21 10:12:03 -060035#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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 Graff21c7bc2006-08-15 00:34:17 -070044#include <net/netlink.h>
Thomas Graf4e902c52006-08-17 18:14:52 -070045#include <net/nexthop.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020046#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020047#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070048#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include "fib_lookup.h"
51
Stephen Hemminger832b4c52006-08-29 16:48:09 -070052static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct hlist_head *fib_info_hash;
54static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080055static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static unsigned int fib_info_cnt;
57
58#define DEVINDEX_HASHBITS 8
59#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
60static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
61
62#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000064#define for_nexthops(fi) { \
65 int nhsel; const struct fib_nh *nh; \
66 for (nhsel = 0, nh = (fi)->fib_nh; \
67 nhsel < (fi)->fib_nhs; \
68 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000070#define change_nexthops(fi) { \
71 int nhsel; struct fib_nh *nexthop_nh; \
72 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
73 nhsel < (fi)->fib_nhs; \
74 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#else /* CONFIG_IP_ROUTE_MULTIPATH */
77
78/* Hope, that gcc will optimize it to get rid of dummy loop */
79
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000080#define for_nexthops(fi) { \
81 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
82 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000084#define change_nexthops(fi) { \
85 int nhsel; \
86 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
87 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#endif /* CONFIG_IP_ROUTE_MULTIPATH */
90
91#define endfor_nexthops(fi) }
92
93
David S. Miller3be06862011-03-07 15:01:10 -080094const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000095 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .error = 0,
97 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000098 },
99 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .error = 0,
101 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000102 },
103 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .error = 0,
105 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000106 },
107 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .error = 0,
109 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000110 },
111 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .error = 0,
113 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000114 },
115 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .error = 0,
117 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000118 },
119 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .error = -EINVAL,
121 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000122 },
123 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .error = -EHOSTUNREACH,
125 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000126 },
127 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .error = -EACCES,
129 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000130 },
131 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .error = -EAGAIN,
133 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000134 },
135 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .error = -EINVAL,
137 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000138 },
139 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .error = -EINVAL,
141 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000142 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
David S. Millerc5038a82012-07-31 15:02:02 -0700145static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000146{
147 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
148
149 if (!rt)
150 return;
151
152 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
153 * because we waited an RCU grace period before calling
154 * free_fib_info_rcu()
155 */
156
Wei Wang95c47f92017-06-17 10:42:30 -0700157 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700158 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000159}
160
David S. Millerc5038a82012-07-31 15:02:02 -0700161static void free_nh_exceptions(struct fib_nh *nh)
162{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700163 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700164 int i;
165
Eric Dumazetcaa41522014-09-03 22:21:56 -0700166 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
167 if (!hash)
168 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700169 for (i = 0; i < FNHE_HASH_SIZE; i++) {
170 struct fib_nh_exception *fnhe;
171
172 fnhe = rcu_dereference_protected(hash[i].chain, 1);
173 while (fnhe) {
174 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800175
David S. Millerc5038a82012-07-31 15:02:02 -0700176 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
177
Timo Teräs2ffae992013-06-27 10:27:05 +0300178 rt_fibinfo_free(&fnhe->fnhe_rth_input);
179 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700180
181 kfree(fnhe);
182
183 fnhe = next;
184 }
185 }
186 kfree(hash);
187}
188
189static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000190{
191 int cpu;
192
193 if (!rtp)
194 return;
195
196 for_each_possible_cpu(cpu) {
197 struct rtable *rt;
198
199 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700200 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700201 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700202 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700203 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000204 }
205 free_percpu(rtp);
206}
207
David Ahern979e2762019-03-27 20:53:58 -0700208void fib_nh_common_release(struct fib_nh_common *nhc)
209{
210 if (nhc->nhc_dev)
211 dev_put(nhc->nhc_dev);
212
213 lwtstate_put(nhc->nhc_lwtstate);
214}
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 free_nh_exceptions(fib_nh);
225 rt_fibinfo_free_cpus(fib_nh->nh_pcpu_rth_output);
226 rt_fibinfo_free(&fib_nh->nh_rth_input);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000230static void free_fib_info_rcu(struct rcu_head *head)
231{
232 struct fib_info *fi = container_of(head, struct fib_info, rcu);
233
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000234 change_nexthops(fi) {
David Ahernfaa041a2019-03-27 20:53:49 -0700235 fib_nh_release(fi->fib_net, nexthop_nh);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000236 } endfor_nexthops(fi);
237
David Aherncc5f0eb2018-10-04 20:07:52 -0700238 ip_fib_metrics_put(fi->fib_metrics);
239
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000240 kfree(fi);
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243void free_fib_info(struct fib_info *fi)
244{
245 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000246 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 fib_info_cnt--;
David Ahernfaa041a2019-03-27 20:53:49 -0700250
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000251 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100253EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255void fib_release_info(struct fib_info *fi)
256{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700257 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (fi && --fi->fib_treeref == 0) {
259 hlist_del(&fi->fib_hash);
260 if (fi->fib_prefsrc)
261 hlist_del(&fi->fib_lhash);
262 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700263 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800265 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 } endfor_nexthops(fi)
267 fi->fib_dead = 1;
268 fib_info_put(fi);
269 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700270 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000273static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 const struct fib_nh *onh = ofi->fib_nh;
276
277 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700278 if (nh->fib_nh_oif != onh->fib_nh_oif ||
279 nh->fib_nh_gw4 != onh->fib_nh_gw4 ||
280 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700282 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100284#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 nh->nh_tclassid != onh->nh_tclassid ||
286#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700287 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
288 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -1;
290 onh++;
291 } endfor_nexthops(fi);
292 return 0;
293}
294
David S. Miller88ebc722008-01-12 21:49:01 -0800295static inline unsigned int fib_devindex_hashfn(unsigned int val)
296{
297 unsigned int mask = DEVINDEX_HASHSIZE - 1;
298
299 return (val ^
300 (val >> DEVINDEX_HASHBITS) ^
301 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
305{
David S. Miller123b9732011-02-01 15:34:21 -0800306 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned int val = fi->fib_nhs;
308
David S. Miller37e826c2011-03-24 18:06:47 -0700309 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700310 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800312 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700313 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
David S. Miller88ebc722008-01-12 21:49:01 -0800314 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
317}
318
319static struct fib_info *fib_find_info(const struct fib_info *nfi)
320{
321 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct fib_info *fi;
323 unsigned int hash;
324
325 hash = fib_info_hashfn(nfi);
326 head = &fib_info_hash[hash];
327
Sasha Levinb67bfe02013-02-27 17:06:00 -0800328 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800329 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800330 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (fi->fib_nhs != nfi->fib_nhs)
332 continue;
333 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700334 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 nfi->fib_prefsrc == fi->fib_prefsrc &&
336 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000337 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000339 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400340 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
342 return fi;
343 }
344
345 return NULL;
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000349 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Al Virod878e72e2006-09-26 22:18:13 -0700351int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct fib_nh *nh;
355 unsigned int hash;
356
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700357 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 hash = fib_devindex_hashfn(dev->ifindex);
360 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800361 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700362 if (nh->fib_nh_dev == dev &&
363 nh->fib_nh_gw4 == gw &&
364 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700365 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367 }
368 }
369
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700370 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return -1;
373}
374
Thomas Graf339bf982006-11-10 14:10:15 -0800375static inline size_t fib_nlmsg_size(struct fib_info *fi)
376{
377 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
378 + nla_total_size(4) /* RTA_TABLE */
379 + nla_total_size(4) /* RTA_DST */
380 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100381 + nla_total_size(4) /* RTA_PREFSRC */
382 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800383
384 /* space for nested metrics */
385 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
386
387 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200388 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800389 /* Also handles the special case fib_nhs == 1 */
390
391 /* each nexthop is packed in an attribute */
392 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
393
394 /* may contain flow and gateway attribute */
395 nhsize += 2 * nla_total_size(4);
396
Roopa Prabhu571e7222015-07-21 10:43:47 +0200397 /* grab encap info */
398 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700399 if (nh->fib_nh_lws) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200400 /* RTA_ENCAP_TYPE */
401 nh_encapsize += lwtunnel_get_encap_size(
David Ahernb75ed8b2019-03-27 20:53:55 -0700402 nh->fib_nh_lws);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200403 /* RTA_ENCAP */
404 nh_encapsize += nla_total_size(2);
405 }
406 } endfor_nexthops(fi);
407
Thomas Graf339bf982006-11-10 14:10:15 -0800408 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200409 payload += nla_total_size((fi->fib_nhs * nhsize) +
410 nh_encapsize);
411
Thomas Graf339bf982006-11-10 14:10:15 -0800412 }
413
414 return payload;
415}
416
Al Viro81f7bf62006-09-27 18:40:00 -0700417void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700418 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700419 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700422 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700423 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Thomas Graf339bf982006-11-10 14:10:15 -0800425 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100426 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700427 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric W. Biederman15e47302012-09-07 20:12:54 +0000429 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700430 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700431 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800432 if (err < 0) {
433 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
434 WARN_ON(err == -EMSGSIZE);
435 kfree_skb(skb);
436 goto errout;
437 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000438 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800439 info->nlh, GFP_KERNEL);
440 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700441errout:
442 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800443 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800446static int fib_detect_death(struct fib_info *fi, int order,
447 struct fib_info **last_resort, int *last_idx,
448 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct neighbour *n;
451 int state = NUD_NONE;
452
David Ahernb75ed8b2019-03-27 20:53:55 -0700453 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].fib_nh_gw4, fi->fib_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (n) {
455 state = n->nud_state;
456 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300457 } else {
458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800460 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000462 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000464 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300465 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *last_resort = fi;
467 *last_idx = order;
468 }
469 return 1;
470}
471
David Ahern979e2762019-03-27 20:53:58 -0700472int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
473 u16 encap_type, void *cfg, gfp_t gfp_flags,
474 struct netlink_ext_ack *extack)
475{
476 if (encap) {
477 struct lwtunnel_state *lwtstate;
478 int err;
479
480 if (encap_type == LWTUNNEL_ENCAP_NONE) {
481 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
482 return -EINVAL;
483 }
484 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
485 cfg, &lwtstate, extack);
486 if (err)
487 return err;
488
489 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
490 }
491
492 return 0;
493}
494EXPORT_SYMBOL_GPL(fib_nh_common_init);
495
David Aherne4516ef2019-03-27 20:53:48 -0700496int fib_nh_init(struct net *net, struct fib_nh *nh,
497 struct fib_config *cfg, int nh_weight,
498 struct netlink_ext_ack *extack)
499{
500 int err = -ENOMEM;
501
David Ahernf1741732019-03-27 20:53:57 -0700502 nh->fib_nh_family = AF_INET;
503
David Aherne4516ef2019-03-27 20:53:48 -0700504 nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
505 if (!nh->nh_pcpu_rth_output)
506 goto err_out;
507
David Ahern979e2762019-03-27 20:53:58 -0700508 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
509 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
510 if (err)
511 goto init_failure;
David Aherne4516ef2019-03-27 20:53:48 -0700512
David Ahernb75ed8b2019-03-27 20:53:55 -0700513 nh->fib_nh_oif = cfg->fc_oif;
David Ahernf35b7942019-04-05 16:30:28 -0700514 if (cfg->fc_gw_family == AF_INET) {
515 nh->fib_nh_gw4 = cfg->fc_gw4;
David Ahernbdf00462019-04-05 16:30:26 -0700516 nh->fib_nh_gw_family = AF_INET;
David Ahernf1741732019-03-27 20:53:57 -0700517 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700518 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700519
520#ifdef CONFIG_IP_ROUTE_CLASSID
521 nh->nh_tclassid = cfg->fc_flow;
522 if (nh->nh_tclassid)
523 net->ipv4.fib_num_tclassid_users++;
524#endif
525#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700526 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700527#endif
528 return 0;
529
David Ahern979e2762019-03-27 20:53:58 -0700530init_failure:
David Aherne4516ef2019-03-27 20:53:48 -0700531 rt_fibinfo_free_cpus(nh->nh_pcpu_rth_output);
532 nh->nh_pcpu_rth_output = NULL;
533err_out:
534 return err;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#ifdef CONFIG_IP_ROUTE_MULTIPATH
538
David Ahern6d8422a12017-05-21 10:12:02 -0600539static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
540 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Thomas Graf4e902c52006-08-17 18:14:52 -0700544 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700546 rtnh = rtnh_next(rtnh, &remaining);
547 }
548
549 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600550 if (remaining > 0) {
551 NL_SET_ERR_MSG(extack,
552 "Invalid nexthop configuration - extra data after nexthops");
553 nhs = 0;
554 }
555
556 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Thomas Graf4e902c52006-08-17 18:14:52 -0700559static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600560 int remaining, struct fib_config *cfg,
561 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
David Aherne4516ef2019-03-27 20:53:48 -0700563 struct net *net = fi->fib_net;
564 struct fib_config fib_cfg;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200565 int ret;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700568 int attrlen;
569
David Aherne4516ef2019-03-27 20:53:48 -0700570 memset(&fib_cfg, 0, sizeof(fib_cfg));
571
David Ahernc3ab2b42017-05-21 10:12:03 -0600572 if (!rtnh_ok(rtnh, remaining)) {
573 NL_SET_ERR_MSG(extack,
574 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600576 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700577
David Ahernc3ab2b42017-05-21 10:12:03 -0600578 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
579 NL_SET_ERR_MSG(extack,
580 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300581 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600582 }
Julian Anastasov80610222016-07-10 21:11:55 +0300583
David Aherne4516ef2019-03-27 20:53:48 -0700584 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
585 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700586
587 attrlen = rtnh_attrlen(rtnh);
588 if (attrlen > 0) {
589 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
590
591 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernf35b7942019-04-05 16:30:28 -0700592 if (nla) {
593 fib_cfg.fc_gw_family = AF_INET;
594 fib_cfg.fc_gw4 = nla_get_in_addr(nla);
595 }
David Aherne4516ef2019-03-27 20:53:48 -0700596
Thomas Graf4e902c52006-08-17 18:14:52 -0700597 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Aherne4516ef2019-03-27 20:53:48 -0700598 if (nla)
599 fib_cfg.fc_flow = nla_get_u32(nla);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200600
David Aherne4516ef2019-03-27 20:53:48 -0700601 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
602 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
603 if (nla)
604 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700606
David Aherne4516ef2019-03-27 20:53:48 -0700607 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
608 rtnh->rtnh_hops + 1, extack);
609 if (ret)
610 goto errout;
611
Thomas Graf4e902c52006-08-17 18:14:52 -0700612 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700614
Roopa Prabhu571e7222015-07-21 10:43:47 +0200615 ret = -EINVAL;
David Ahernb75ed8b2019-03-27 20:53:55 -0700616 if (cfg->fc_oif && fi->fib_nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700617 NL_SET_ERR_MSG(extack,
618 "Nexthop device index does not match RTA_OIF");
619 goto errout;
620 }
David Ahernf35b7942019-04-05 16:30:28 -0700621 if (cfg->fc_gw_family) {
622 if (cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family ||
623 (cfg->fc_gw_family == AF_INET &&
624 fi->fib_nh->fib_nh_gw4 != cfg->fc_gw4)) {
625 NL_SET_ERR_MSG(extack,
626 "Nexthop gateway does not match RTA_GATEWAY");
627 goto errout;
628 }
David Aherne4516ef2019-03-27 20:53:48 -0700629 }
630#ifdef CONFIG_IP_ROUTE_CLASSID
631 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
632 NL_SET_ERR_MSG(extack,
633 "Nexthop class id does not match RTA_FLOW");
634 goto errout;
635 }
636#endif
637 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200638errout:
639 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Peter Nørlund0e884c72015-09-30 10:12:21 +0200642static void fib_rebalance(struct fib_info *fi)
643{
644 int total;
645 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200646
647 if (fi->fib_nhs < 2)
648 return;
649
650 total = 0;
651 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700652 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200653 continue;
654
David Ahernb75ed8b2019-03-27 20:53:55 -0700655 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
656 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200657 continue;
658
David Ahernb75ed8b2019-03-27 20:53:55 -0700659 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200660 } endfor_nexthops(fi);
661
662 w = 0;
663 change_nexthops(fi) {
664 int upper_bound;
665
David Ahernb75ed8b2019-03-27 20:53:55 -0700666 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200667 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700668 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
669 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200670 upper_bound = -1;
671 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700672 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200673 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
674 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200675 }
676
David Ahernb75ed8b2019-03-27 20:53:55 -0700677 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200678 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200679}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200680#else /* CONFIG_IP_ROUTE_MULTIPATH */
681
David Ahern8373c6c2019-03-27 20:53:46 -0700682static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
683 int remaining, struct fib_config *cfg,
684 struct netlink_ext_ack *extack)
685{
686 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
687
688 return -EINVAL;
689}
690
Peter Nørlund0e884c72015-09-30 10:12:21 +0200691#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200692
693#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Ahern30357d72017-01-30 12:07:37 -0800695static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800696 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800697 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600698 const struct fib_config *cfg,
699 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200700{
701 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200702 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200703
704 if (encap_type == LWTUNNEL_ENCAP_NONE)
705 return 0;
706
David Ahern9ae28722017-05-27 16:19:28 -0600707 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
708 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200709 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700710 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200711 lwtstate_free(lwtstate);
712 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200713
Jiri Bencdf383e62015-08-18 18:41:13 +0200714 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200715}
716
David Ahern9ae28722017-05-27 16:19:28 -0600717int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
718 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700721 struct rtnexthop *rtnh;
722 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723#endif
724
Thomas Graf4e902c52006-08-17 18:14:52 -0700725 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 1;
727
David Ahernf35b7942019-04-05 16:30:28 -0700728 if (cfg->fc_oif || cfg->fc_gw_family) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200729 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600730 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
731 fi->fib_nh, cfg, extack))
732 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200733 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100734#ifdef CONFIG_IP_ROUTE_CLASSID
735 if (cfg->fc_flow &&
736 cfg->fc_flow != fi->fib_nh->nh_tclassid)
737 return 1;
738#endif
David Ahernf35b7942019-04-05 16:30:28 -0700739 if ((cfg->fc_oif && cfg->fc_oif != fi->fib_nh->fib_nh_oif) ||
740 (cfg->fc_gw_family &&
741 cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family))
742 return 1;
743
744 if (cfg->fc_gw_family == AF_INET &&
745 cfg->fc_gw4 != fi->fib_nh->fib_nh_gw4)
746 return 1;
747
748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
751#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100752 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700754
755 rtnh = cfg->fc_mp;
756 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700759 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Thomas Graf4e902c52006-08-17 18:14:52 -0700761 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700763
David Ahernb75ed8b2019-03-27 20:53:55 -0700764 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700766
767 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200768 if (attrlen > 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700769 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
770
771 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernb75ed8b2019-03-27 20:53:55 -0700772 if (nla && nla_get_in_addr(nla) != nh->fib_nh_gw4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100774#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700775 nla = nla_find(attrs, attrlen, RTA_FLOW);
776 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 1;
778#endif
779 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700780
781 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 } endfor_nexthops(fi);
783#endif
784 return 0;
785}
786
Xin Long5f9ae3d2017-08-23 10:07:26 +0800787bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
788{
789 struct nlattr *nla;
790 int remaining;
791
792 if (!cfg->fc_mx)
793 return true;
794
795 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
796 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +0100797 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800798
799 if (!type)
800 continue;
801 if (type > RTAX_MAX)
802 return false;
803
804 if (type == RTAX_CC_ALGO) {
805 char tmp[TCP_CA_NAME_MAX];
806 bool ecn_ca = false;
807
808 nla_strlcpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -0800809 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +0800810 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -0700811 if (nla_len(nla) != sizeof(u32))
812 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800813 val = nla_get_u32(nla);
814 }
815
Phil Sutterd03a4552017-12-19 15:17:13 +0100816 fi_val = fi->fib_metrics->metrics[type - 1];
817 if (type == RTAX_FEATURES)
818 fi_val &= ~DST_FEATURE_ECN_CA;
819
820 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +0800821 return false;
822 }
823
824 return true;
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000829 * Picture
830 * -------
831 *
832 * Semantics of nexthop is very messy by historical reasons.
833 * We have to take into account, that:
834 * a) gateway can be actually local interface address,
835 * so that gatewayed route is direct.
836 * b) gateway must be on-link address, possibly
837 * described not by an ifaddr, but also by a direct route.
838 * c) If both gateway and interface are specified, they should not
839 * contradict.
840 * d) If we use tunnel routes, gateway could be not on-link.
841 *
842 * Attempt to reconcile all of these (alas, self-contradictory) conditions
843 * results in pretty ugly and hairy code with obscure logic.
844 *
845 * I chose to generalized it instead, so that the size
846 * of code does not increase practically, but it becomes
847 * much more general.
848 * Every prefix is assigned a "scope" value: "host" is local address,
849 * "link" is direct route,
850 * [ ... "site" ... "interior" ... ]
851 * and "universe" is true gateway route with global meaning.
852 *
853 * Every prefix refers to a set of "nexthop"s (gw, oif),
854 * where gw must have narrower scope. This recursion stops
855 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
856 * which means that gw is forced to be on link.
857 *
858 * Code is still hairy, but now it is apparently logically
859 * consistent and very flexible. F.e. as by-product it allows
860 * to co-exists in peace independent exterior and interior
861 * routing processes.
862 *
863 * Normally it looks as following.
864 *
865 * {universe prefix} -> (gw, oif) [scope link]
866 * |
867 * |-> {link prefix} -> (gw, oif) [scope local]
868 * |
869 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
David Ahernfa8fefa2017-09-27 20:41:59 -0700871static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
872 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Tom Herbert127eb7c2015-08-24 09:45:41 -0700874 int err = 0;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800875 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000876 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Denis V. Lunev86167a32008-01-21 17:34:00 -0800878 net = cfg->fc_nlinfo.nl_net;
David Ahernb75ed8b2019-03-27 20:53:55 -0700879 if (nh->fib_nh_gw4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct fib_result res;
881
David Ahernb75ed8b2019-03-27 20:53:55 -0700882 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
David Ahern30bbaa12015-08-13 14:59:05 -0600883 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
David Ahernc3ab2b42017-05-21 10:12:03 -0600885 if (cfg->fc_scope >= RT_SCOPE_LINK) {
886 NL_SET_ERR_MSG(extack,
887 "Nexthop has invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600889 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700890 dev = __dev_get_by_index(net, nh->fib_nh_oif);
David Ahern066b1032018-08-29 16:53:27 -0700891 if (!dev) {
892 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -ENODEV;
David Ahern066b1032018-08-29 16:53:27 -0700894 }
David Ahernc3ab2b42017-05-21 10:12:03 -0600895 if (!(dev->flags & IFF_UP)) {
896 NL_SET_ERR_MSG(extack,
897 "Nexthop device is not up");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600899 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700900 addr_type = inet_addr_type_dev_table(net, dev,
901 nh->fib_nh_gw4);
David Ahernc3ab2b42017-05-21 10:12:03 -0600902 if (addr_type != RTN_UNICAST) {
903 NL_SET_ERR_MSG(extack,
904 "Nexthop has invalid gateway");
David Ahern30bbaa12015-08-13 14:59:05 -0600905 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600906 }
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400907 if (!netif_carrier_ok(dev))
David Ahernb75ed8b2019-03-27 20:53:55 -0700908 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
909 nh->fib_nh_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 dev_hold(dev);
David Ahernb75ed8b2019-03-27 20:53:55 -0700911 nh->fib_nh_scope = RT_SCOPE_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000914 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 {
David Ahern3bfd8472015-08-13 14:59:07 -0600916 struct fib_table *tbl = NULL;
David S. Miller9ade2282011-03-12 02:02:42 -0500917 struct flowi4 fl4 = {
David Ahernb75ed8b2019-03-27 20:53:55 -0700918 .daddr = nh->fib_nh_gw4,
David S. Miller9ade2282011-03-12 02:02:42 -0500919 .flowi4_scope = cfg->fc_scope + 1,
David Ahernb75ed8b2019-03-27 20:53:55 -0700920 .flowi4_oif = nh->fib_nh_oif,
Cong Wang6a662712014-04-15 16:25:34 -0700921 .flowi4_iif = LOOPBACK_IFINDEX,
Thomas Graf4e902c52006-08-17 18:14:52 -0700922 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500925 if (fl4.flowi4_scope < RT_SCOPE_LINK)
926 fl4.flowi4_scope = RT_SCOPE_LINK;
David Ahern3bfd8472015-08-13 14:59:07 -0600927
928 if (cfg->fc_table)
929 tbl = fib_get_table(net, cfg->fc_table);
930
931 if (tbl)
932 err = fib_table_lookup(tbl, &fl4, &res,
Eric Dumazet1e313672015-08-15 10:54:07 -0700933 FIB_LOOKUP_IGNORE_LINKSTATE |
934 FIB_LOOKUP_NOREF);
David Ahern4c9bcd12015-08-19 11:40:31 -0700935
936 /* on error or if no table given do full lookup. This
937 * is needed for example when nexthops are in the local
938 * table rather than the given table
939 */
940 if (!tbl || err) {
David Ahern3bfd8472015-08-13 14:59:07 -0600941 err = fib_lookup(net, &fl4, &res,
942 FIB_LOOKUP_IGNORE_LINKSTATE);
David Ahern4c9bcd12015-08-19 11:40:31 -0700943 }
944
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000945 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600946 NL_SET_ERR_MSG(extack,
947 "Nexthop has invalid gateway");
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000948 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952 err = -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600953 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
954 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600956 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700957 nh->fib_nh_scope = res.scope;
958 nh->fib_nh_oif = FIB_RES_OIF(res);
959 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
David Ahernc3ab2b42017-05-21 10:12:03 -0600960 if (!dev) {
961 NL_SET_ERR_MSG(extack,
962 "No egress device for nexthop gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600964 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000965 dev_hold(dev);
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400966 if (!netif_carrier_ok(dev))
David Ahernb75ed8b2019-03-27 20:53:55 -0700967 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000968 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 } else {
970 struct in_device *in_dev;
971
David Ahernb75ed8b2019-03-27 20:53:55 -0700972 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600973 NL_SET_ERR_MSG(extack,
974 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600976 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000977 rcu_read_lock();
978 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -0700979 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
Ian Morris51456b22015-04-03 09:17:26 +0100980 if (!in_dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000981 goto out;
982 err = -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600983 if (!(in_dev->dev->flags & IFF_UP)) {
984 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000985 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600986 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700987 nh->fib_nh_dev = in_dev->dev;
988 dev_hold(nh->fib_nh_dev);
989 nh->fib_nh_scope = RT_SCOPE_HOST;
990 if (!netif_carrier_ok(nh->fib_nh_dev))
991 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000992 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000994out:
995 rcu_read_unlock();
996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Al Viro81f7bf62006-09-27 18:40:00 -0700999static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
David S. Miller123b9732011-02-01 15:34:21 -08001001 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001003 return ((__force u32)val ^
1004 ((__force u32)val >> 7) ^
1005 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
David S. Miller123b9732011-02-01 15:34:21 -08001008static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +08001011 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 else
1013 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001014 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1015 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
David S. Miller123b9732011-02-01 15:34:21 -08001018static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 if (!hash)
1021 return;
1022
1023 if (bytes <= PAGE_SIZE)
1024 kfree(hash);
1025 else
1026 free_pages((unsigned long) hash, get_order(bytes));
1027}
1028
David S. Miller123b9732011-02-01 15:34:21 -08001029static void fib_info_hash_move(struct hlist_head *new_info_hash,
1030 struct hlist_head *new_laddrhash,
1031 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
David S. Millerb7656e72005-08-05 04:12:48 -07001033 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001034 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001035 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001037 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001038 old_info_hash = fib_info_hash;
1039 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001040 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 for (i = 0; i < old_size; i++) {
1043 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001044 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 struct fib_info *fi;
1046
Sasha Levinb67bfe02013-02-27 17:06:00 -08001047 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct hlist_head *dest;
1049 unsigned int new_hash;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 new_hash = fib_info_hashfn(fi);
1052 dest = &new_info_hash[new_hash];
1053 hlist_add_head(&fi->fib_hash, dest);
1054 }
1055 }
1056 fib_info_hash = new_info_hash;
1057
1058 for (i = 0; i < old_size; i++) {
1059 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001060 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct fib_info *fi;
1062
Sasha Levinb67bfe02013-02-27 17:06:00 -08001063 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct hlist_head *ldest;
1065 unsigned int new_hash;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1068 ldest = &new_laddrhash[new_hash];
1069 hlist_add_head(&fi->fib_lhash, ldest);
1070 }
1071 }
1072 fib_info_laddrhash = new_laddrhash;
1073
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001074 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001075
1076 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001077 fib_info_hash_free(old_info_hash, bytes);
1078 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
David S. Miller436c3b62011-03-24 17:42:21 -07001081__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1082{
David Ahernb75ed8b2019-03-27 20:53:55 -07001083 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev,
1084 nh->fib_nh_gw4,
David S. Miller37e826c2011-03-24 18:06:47 -07001085 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001086 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1087
1088 return nh->nh_saddr;
1089}
1090
David Aherneba618a2019-04-02 14:11:55 -07001091__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1092{
1093 struct fib_nh_common *nhc = res->nhc;
1094 struct fib_nh *nh;
1095
1096 if (res->fi->fib_prefsrc)
1097 return res->fi->fib_prefsrc;
1098
1099 nh = container_of(nhc, struct fib_nh, nh_common);
1100 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1101 return nh->nh_saddr;
1102
1103 return fib_info_update_nh_saddr(net, nh);
1104}
1105
David Ahern021dd3b2015-08-13 14:59:06 -06001106static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1107{
1108 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1109 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001110 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001111 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001112
1113 if (tb_id == RT_TABLE_MAIN)
1114 tb_id = RT_TABLE_LOCAL;
1115
David Aherne1b8d902015-11-03 15:59:28 -08001116 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1117 fib_prefsrc, tb_id);
1118
1119 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1120 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1121 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001122 }
David Aherne1b8d902015-11-03 15:59:28 -08001123
1124 if (rc != RTN_LOCAL)
1125 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001126 }
1127 return true;
1128}
1129
David Ahern6d8422a12017-05-21 10:12:02 -06001130struct fib_info *fib_create_info(struct fib_config *cfg,
1131 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 int err;
1134 struct fib_info *fi = NULL;
1135 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001137 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
David S. Miller4c8237c2011-03-07 14:27:38 -08001139 if (cfg->fc_type > RTN_MAX)
1140 goto err_inval;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001143 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1144 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
David Ahernc3ab2b42017-05-21 10:12:03 -06001148 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1149 NL_SET_ERR_MSG(extack,
1150 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001151 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001152 }
Julian Anastasov80610222016-07-10 21:11:55 +03001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001155 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001156 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (nhs == 0)
1158 goto err_inval;
1159 }
1160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001163 if (fib_info_cnt >= fib_info_hash_size) {
1164 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct hlist_head *new_info_hash;
1166 struct hlist_head *new_laddrhash;
1167 unsigned int bytes;
1168
1169 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001170 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001172 new_info_hash = fib_info_hash_alloc(bytes);
1173 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001175 fib_info_hash_free(new_info_hash, bytes);
1176 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001177 } else
David S. Miller123b9732011-02-01 15:34:21 -08001178 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
David S. Miller123b9732011-02-01 15:34:21 -08001180 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 goto failure;
1182 }
1183
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001184 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001185 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001187 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001188 cfg->fc_mx_len, extack);
David Ahern767a2212018-10-04 20:07:51 -07001189 if (unlikely(IS_ERR(fi->fib_metrics))) {
1190 err = PTR_ERR(fi->fib_metrics);
1191 kfree(fi);
1192 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001193 }
David Ahern767a2212018-10-04 20:07:51 -07001194
Eric Dumazet187e5b32017-08-15 05:26:17 -07001195 fib_info_cnt++;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001196 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001197 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001198 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001199 fi->fib_flags = cfg->fc_flags;
1200 fi->fib_priority = cfg->fc_priority;
1201 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001202 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001203 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 fi->fib_nhs = nhs;
1206 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001207 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 } endfor_nexthops(fi)
1209
David Aherne4516ef2019-03-27 20:53:48 -07001210 if (cfg->fc_mp)
David Ahern6d8422a12017-05-21 10:12:02 -06001211 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
David Aherne4516ef2019-03-27 20:53:48 -07001212 else
1213 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001214
David Aherne4516ef2019-03-27 20:53:48 -07001215 if (err != 0)
1216 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Thomas Graf4e902c52006-08-17 18:14:52 -07001218 if (fib_props[cfg->fc_type].error) {
David Ahernf35b7942019-04-05 16:30:28 -07001219 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001220 NL_SET_ERR_MSG(extack,
1221 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001225 } else {
1226 switch (cfg->fc_type) {
1227 case RTN_UNICAST:
1228 case RTN_LOCAL:
1229 case RTN_BROADCAST:
1230 case RTN_ANYCAST:
1231 case RTN_MULTICAST:
1232 break;
1233 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001234 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001235 goto err_inval;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
David Ahernc3ab2b42017-05-21 10:12:03 -06001239 if (cfg->fc_scope > RT_SCOPE_HOST) {
1240 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Thomas Graf4e902c52006-08-17 18:14:52 -07001244 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct fib_nh *nh = fi->fib_nh;
1246
1247 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001248 if (nhs != 1) {
1249 NL_SET_ERR_MSG(extack,
1250 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001251 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001252 }
David Ahernbdf00462019-04-05 16:30:26 -07001253 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001254 NL_SET_ERR_MSG(extack,
1255 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001257 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001258 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1259 nh->fib_nh_dev = dev_get_by_index(net, fi->fib_nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001261 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto failure;
1263 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001264 int linkdown = 0;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 change_nexthops(fi) {
David Ahernfa8fefa2017-09-27 20:41:59 -07001267 err = fib_check_nh(cfg, nexthop_nh, extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001268 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001270 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001271 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001273 if (linkdown == fi->fib_nhs)
1274 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
David Ahernc3ab2b42017-05-21 10:12:03 -06001277 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1278 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001279 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
David S. Miller1fc050a2011-03-07 20:54:48 -08001282 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001283 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -08001284 } endfor_nexthops(fi)
1285
Peter Nørlund0e884c72015-09-30 10:12:21 +02001286 fib_rebalance(fi);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001289 ofi = fib_find_info(fi);
1290 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 fi->fib_dead = 1;
1292 free_fib_info(fi);
1293 ofi->fib_treeref++;
1294 return ofi;
1295 }
1296
1297 fi->fib_treeref++;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001298 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001299 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 hlist_add_head(&fi->fib_hash,
1301 &fib_info_hash[fib_info_hashfn(fi)]);
1302 if (fi->fib_prefsrc) {
1303 struct hlist_head *head;
1304
1305 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1306 hlist_add_head(&fi->fib_lhash, head);
1307 }
1308 change_nexthops(fi) {
1309 struct hlist_head *head;
1310 unsigned int hash;
1311
David Ahernb75ed8b2019-03-27 20:53:55 -07001312 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001314 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001316 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001318 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return fi;
1320
1321err_inval:
1322 err = -EINVAL;
1323
1324failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001325 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 fi->fib_dead = 1;
1327 free_fib_info(fi);
1328 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001329
1330 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
David Ahernc0a72072019-04-02 14:11:58 -07001333int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1334 unsigned int *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001335{
David Ahernc2364192019-04-02 14:11:57 -07001336 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001337 *flags |= RTNH_F_DEAD;
1338
David Ahernc2364192019-04-02 14:11:57 -07001339 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001340 *flags |= RTNH_F_LINKDOWN;
1341
1342 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001343 switch (nhc->nhc_family) {
1344 case AF_INET:
1345 if (ip_ignore_linkdown(nhc->nhc_dev))
1346 *flags |= RTNH_F_DEAD;
1347 break;
David Ahernc0a72072019-04-02 14:11:58 -07001348 case AF_INET6:
1349 if (ip6_ignore_linkdown(nhc->nhc_dev))
1350 *flags |= RTNH_F_DEAD;
1351 break;
David Ahernc2364192019-04-02 14:11:57 -07001352 }
David Ahernb0f60192019-04-02 14:11:56 -07001353 rcu_read_unlock();
1354 }
1355
David Ahernbdf00462019-04-05 16:30:26 -07001356 switch (nhc->nhc_gw_family) {
1357 case AF_INET:
1358 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1359 goto nla_put_failure;
1360 break;
1361 case AF_INET6:
1362 if (nla_put_in6_addr(skb, RTA_GATEWAY, &nhc->nhc_gw.ipv6) < 0)
1363 goto nla_put_failure;
1364 break;
David Ahernc2364192019-04-02 14:11:57 -07001365 }
David Ahernb0f60192019-04-02 14:11:56 -07001366
David Ahernc2364192019-04-02 14:11:57 -07001367 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1368 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
David Ahernb0f60192019-04-02 14:11:56 -07001369 *flags |= RTNH_F_OFFLOAD;
1370
David Ahernc2364192019-04-02 14:11:57 -07001371 if (!skip_oif && nhc->nhc_dev &&
1372 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001373 goto nla_put_failure;
1374
David Ahernc2364192019-04-02 14:11:57 -07001375 if (nhc->nhc_lwtstate &&
1376 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001377 goto nla_put_failure;
1378
1379 return 0;
1380
1381nla_put_failure:
1382 return -EMSGSIZE;
1383}
David Ahernc0a72072019-04-02 14:11:58 -07001384EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001385
David Ahernc0a72072019-04-02 14:11:58 -07001386#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1387int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1388 int nh_weight)
David Ahernb0f60192019-04-02 14:11:56 -07001389{
David Ahernc2364192019-04-02 14:11:57 -07001390 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001391 struct rtnexthop *rtnh;
1392 unsigned int flags = 0;
1393
1394 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1395 if (!rtnh)
1396 goto nla_put_failure;
1397
David Ahernc2364192019-04-02 14:11:57 -07001398 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001399 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1400
David Ahernc2364192019-04-02 14:11:57 -07001401 if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001402 goto nla_put_failure;
1403
1404 rtnh->rtnh_flags = flags;
1405
1406 /* length of rtnetlink header + attributes */
1407 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1408
1409 return 0;
1410
1411nla_put_failure:
1412 return -EMSGSIZE;
1413}
David Ahernc0a72072019-04-02 14:11:58 -07001414EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001415#endif
David Ahernb0f60192019-04-02 14:11:56 -07001416
David Ahernc2364192019-04-02 14:11:57 -07001417#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001418static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1419{
1420 struct nlattr *mp;
1421
1422 mp = nla_nest_start(skb, RTA_MULTIPATH);
1423 if (!mp)
1424 goto nla_put_failure;
1425
1426 for_nexthops(fi) {
David Ahernc2364192019-04-02 14:11:57 -07001427 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001428 goto nla_put_failure;
1429#ifdef CONFIG_IP_ROUTE_CLASSID
1430 if (nh->nh_tclassid &&
1431 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1432 goto nla_put_failure;
1433#endif
1434 } endfor_nexthops(fi);
1435
1436 nla_nest_end(skb, mp);
1437
1438 return 0;
1439
1440nla_put_failure:
1441 return -EMSGSIZE;
1442}
1443#else
1444static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1445{
1446 return 0;
1447}
1448#endif
1449
Eric W. Biederman15e47302012-09-07 20:12:54 +00001450int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001451 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001452 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001454 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Eric W. Biederman15e47302012-09-07 20:12:54 +00001457 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001458 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001459 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001460
1461 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rtm->rtm_family = AF_INET;
1463 rtm->rtm_dst_len = dst_len;
1464 rtm->rtm_src_len = 0;
1465 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001466 if (tb_id < 256)
1467 rtm->rtm_table = tb_id;
1468 else
1469 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001470 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1471 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 rtm->rtm_type = type;
1473 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001474 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001476
David S. Millerf3756b72012-04-01 20:39:02 -04001477 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001478 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001479 goto nla_put_failure;
1480 if (fi->fib_priority &&
1481 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1482 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001483 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001484 goto nla_put_failure;
1485
David S. Millerf3756b72012-04-01 20:39:02 -04001486 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001487 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001488 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (fi->fib_nhs == 1) {
David Ahernb0f60192019-04-02 14:11:56 -07001490 struct fib_nh *nh = &fi->fib_nh[0];
1491 unsigned int flags = 0;
1492
David Ahernc2364192019-04-02 14:11:57 -07001493 if (fib_nexthop_info(skb, &nh->nh_common, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001494 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001495
1496 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001497#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernb0f60192019-04-02 14:11:56 -07001498 if (nh->nh_tclassid &&
1499 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
David S. Millerf3756b72012-04-01 20:39:02 -04001500 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001501#endif
David Ahernb0f60192019-04-02 14:11:56 -07001502 } else {
1503 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001504 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001506
Johannes Berg053c0952015-01-16 22:09:00 +01001507 nlmsg_end(skb, nlh);
1508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Thomas Grafbe403ea2006-08-17 18:15:17 -07001510nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001511 nlmsg_cancel(skb, nlh);
1512 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001516 * Update FIB if:
1517 * - local address disappeared -> we must delete all the entries
1518 * referring to it.
1519 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001521int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001522{
1523 int ret = 0;
1524 unsigned int hash = fib_laddr_hashfn(local);
1525 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001526 struct net *net = dev_net(dev);
1527 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001528 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Ian Morris51456b22015-04-03 09:17:26 +01001530 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001531 return 0;
1532
Sasha Levinb67bfe02013-02-27 17:06:00 -08001533 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001534 if (!net_eq(fi->fib_net, net) ||
1535 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001536 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001537 if (fi->fib_prefsrc == local) {
1538 fi->fib_flags |= RTNH_F_DEAD;
1539 ret++;
1540 }
1541 }
1542 return ret;
1543}
1544
David Ahernb75ed8b2019-03-27 20:53:55 -07001545static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001546 enum fib_event_type event_type)
1547{
David Ahernb75ed8b2019-03-27 20:53:55 -07001548 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001549 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001550 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001551 };
1552
1553 switch (event_type) {
1554 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001555 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001556 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001557 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001558 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001559 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001560 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001561 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001562 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1563 (nh->fib_nh_flags & RTNH_F_DEAD))
1564 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001565 event_type, &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001566 default:
1567 break;
1568 }
1569
1570 return NOTIFY_DONE;
1571}
1572
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001573/* Update the PMTU of exceptions when:
1574 * - the new MTU of the first hop becomes smaller than the PMTU
1575 * - the old MTU was the same as the PMTU, and it limited discovery of
1576 * larger MTUs on the path. With that limit raised, we can now
1577 * discover larger MTUs
1578 * A special case is locked exceptions, for which the PMTU is smaller
1579 * than the minimal accepted PMTU:
1580 * - if the new MTU is greater than the PMTU, don't make any change
1581 * - otherwise, unlock and set PMTU
1582 */
1583static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1584{
1585 struct fnhe_hash_bucket *bucket;
1586 int i;
1587
1588 bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1589 if (!bucket)
1590 return;
1591
1592 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1593 struct fib_nh_exception *fnhe;
1594
1595 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1596 fnhe;
1597 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1598 if (fnhe->fnhe_mtu_locked) {
1599 if (new <= fnhe->fnhe_pmtu) {
1600 fnhe->fnhe_pmtu = new;
1601 fnhe->fnhe_mtu_locked = false;
1602 }
1603 } else if (new < fnhe->fnhe_pmtu ||
1604 orig == fnhe->fnhe_pmtu) {
1605 fnhe->fnhe_pmtu = new;
1606 }
1607 }
1608 }
1609}
1610
1611void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1612{
1613 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1614 struct hlist_head *head = &fib_info_devhash[hash];
1615 struct fib_nh *nh;
1616
1617 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001618 if (nh->fib_nh_dev == dev)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001619 nh_update_mtu(nh, dev->mtu, orig_mtu);
1620 }
1621}
1622
Julian Anastasov4f823de2015-10-30 10:23:33 +02001623/* Event force Flags Description
1624 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1625 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1626 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1627 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1628 */
1629int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 int ret = 0;
1632 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001633 struct fib_info *prev_fi = NULL;
1634 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1635 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001636 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001637
Julian Anastasov4f823de2015-10-30 10:23:33 +02001638 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 scope = -1;
1640
Sasha Levinb67bfe02013-02-27 17:06:00 -08001641 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001642 struct fib_info *fi = nh->nh_parent;
1643 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001645 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001646 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001647 continue;
1648 prev_fi = fi;
1649 dead = 0;
1650 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001651 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001652 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001653 else if (nexthop_nh->fib_nh_dev == dev &&
1654 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001655 switch (event) {
1656 case NETDEV_DOWN:
1657 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07001658 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001659 /* fall through */
1660 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07001661 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001662 break;
1663 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001664 call_fib_nh_notifiers(nexthop_nh,
1665 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001666 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001668#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001669 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07001670 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001671 dead = fi->fib_nhs;
1672 break;
1673 }
1674#endif
1675 } endfor_nexthops(fi)
1676 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001677 switch (event) {
1678 case NETDEV_DOWN:
1679 case NETDEV_UNREGISTER:
1680 fi->fib_flags |= RTNH_F_DEAD;
1681 /* fall through */
1682 case NETDEV_CHANGE:
1683 fi->fib_flags |= RTNH_F_LINKDOWN;
1684 break;
1685 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001686 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001688
1689 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
1692 return ret;
1693}
1694
David S. Miller0c838ff2011-01-31 16:16:50 -08001695/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001696static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001697{
1698 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001699 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001700 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001701 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001702 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001703 struct fib_alias *fa, *fa1 = NULL;
1704 u32 last_prio = res->fi->fib_priority;
1705 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001706
Alexander Duyck56315f92015-02-25 15:31:31 -08001707 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001708 struct fib_info *next_fi = fa->fa_info;
1709
Julian Anastasov18a912e92015-07-22 10:43:22 +03001710 if (fa->fa_slen != slen)
1711 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001712 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1713 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001714 if (fa->tb_id != tb->tb_id)
1715 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001716 if (next_fi->fib_priority > last_prio &&
1717 fa->fa_tos == last_tos) {
1718 if (last_tos)
1719 continue;
1720 break;
1721 }
1722 if (next_fi->fib_flags & RTNH_F_DEAD)
1723 continue;
1724 last_tos = fa->fa_tos;
1725 last_prio = next_fi->fib_priority;
1726
David S. Miller37e826c2011-03-24 18:06:47 -07001727 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001728 fa->fa_type != RTN_UNICAST)
1729 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001730 if (!next_fi->fib_nh[0].fib_nh_gw4 ||
1731 next_fi->fib_nh[0].fib_nh_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08001732 continue;
1733
1734 fib_alias_accessed(fa);
1735
Ian Morris51456b22015-04-03 09:17:26 +01001736 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001737 if (next_fi != res->fi)
1738 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001739 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001740 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001741 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001742 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001743 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001744 goto out;
1745 }
1746 fi = next_fi;
1747 order++;
1748 }
1749
Ian Morris51456b22015-04-03 09:17:26 +01001750 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001751 if (fa1)
1752 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001753 goto out;
1754 }
1755
1756 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001757 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001758 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001759 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001760 goto out;
1761 }
1762
1763 if (last_idx >= 0)
1764 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001765 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001766out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001767 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001768}
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001771 * Dead device goes up. We wake up dead nexthops.
1772 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001774int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct fib_info *prev_fi;
1777 unsigned int hash;
1778 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct fib_nh *nh;
1780 int ret;
1781
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001782 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
1784
Julian Anastasovc9b32922015-10-30 10:23:34 +02001785 if (nh_flags & RTNH_F_DEAD) {
1786 unsigned int flags = dev_get_flags(dev);
1787
1788 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1789 nh_flags |= RTNH_F_LINKDOWN;
1790 }
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 prev_fi = NULL;
1793 hash = fib_devindex_hashfn(dev->ifindex);
1794 head = &fib_info_devhash[hash];
1795 ret = 0;
1796
Sasha Levinb67bfe02013-02-27 17:06:00 -08001797 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct fib_info *fi = nh->nh_parent;
1799 int alive;
1800
1801 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001802 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 continue;
1804
1805 prev_fi = fi;
1806 alive = 0;
1807 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001808 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 alive++;
1810 continue;
1811 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001812 if (!nexthop_nh->fib_nh_dev ||
1813 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001815 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08001816 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 continue;
1818 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001819 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001820 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 } endfor_nexthops(fi)
1822
1823 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001824 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 ret++;
1826 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001827
1828 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830
1831 return ret;
1832}
1833
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001834#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001835static bool fib_good_nh(const struct fib_nh *nh)
1836{
1837 int state = NUD_REACHABLE;
1838
David Ahernb75ed8b2019-03-27 20:53:55 -07001839 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07001840 struct neighbour *n;
1841
1842 rcu_read_lock_bh();
1843
David Ahernb75ed8b2019-03-27 20:53:55 -07001844 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1845 (__force u32)nh->fib_nh_gw4);
David Aherna6db4492016-04-07 07:21:00 -07001846 if (n)
1847 state = n->nud_state;
1848
1849 rcu_read_unlock_bh();
1850 }
1851
1852 return !!(state & NUD_VALID);
1853}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001854
Peter Nørlund0e884c72015-09-30 10:12:21 +02001855void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001858 struct net *net = fi->fib_net;
1859 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
David Aherneba618a2019-04-02 14:11:55 -07001861 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08001862 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07001863 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08001864 continue;
1865 if (!first) {
1866 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001867 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08001868 first = true;
1869 }
1870 }
1871
David Aherneba618a2019-04-02 14:11:55 -07001872 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02001873 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Xin Long6174a302018-04-01 22:40:35 +08001875 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001876 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08001877 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880#endif
David Ahern3ce58d82015-10-05 08:51:25 -07001881
1882void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001883 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07001884{
David Ahern0d876f22018-02-13 08:11:34 -08001885 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
1886 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08001887
David Ahern3ce58d82015-10-05 08:51:25 -07001888#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern0d876f22018-02-13 08:11:34 -08001889 if (res->fi->fib_nhs > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08001890 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01001891
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001892 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07001893 }
1894 else
1895#endif
1896 if (!res->prefixlen &&
1897 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08001898 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07001899 fib_select_default(fl4, res);
1900
David Ahern0d876f22018-02-13 08:11:34 -08001901check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07001902 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07001903 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07001904}