blob: b4589861b84c6bc4daa7149e078ad63749c7622f [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>
Eric Dumazetd07418a2022-01-19 02:04:12 -080032#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020034#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/ip.h>
36#include <net/protocol.h>
37#include <net/route.h>
38#include <net/tcp.h>
39#include <net/sock.h>
40#include <net/ip_fib.h>
David Ahern717a8f52019-04-05 16:30:32 -070041#include <net/ip6_fib.h>
David Ahern5481d732019-06-03 20:19:49 -070042#include <net/nexthop.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070043#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070044#include <net/rtnh.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020045#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020046#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070047#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "fib_lookup.h"
50
Stephen Hemminger832b4c52006-08-29 16:48:09 -070051static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static struct hlist_head *fib_info_hash;
53static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080054static unsigned int fib_info_hash_size;
Eric Dumazet79eb15d2022-01-19 02:04:13 -080055static unsigned int fib_info_hash_bits;
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
David Aherndcb1ecb2019-06-03 20:19:50 -070062/* for_nexthops and change_nexthops only used when nexthop object
63 * is not set in a fib_info. The logic within can reference fib_nh.
64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000067#define for_nexthops(fi) { \
68 int nhsel; const struct fib_nh *nh; \
69 for (nhsel = 0, nh = (fi)->fib_nh; \
David Ahern5481d732019-06-03 20:19:49 -070070 nhsel < fib_info_num_path((fi)); \
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000071 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000073#define change_nexthops(fi) { \
74 int nhsel; struct fib_nh *nexthop_nh; \
75 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
David Ahern5481d732019-06-03 20:19:49 -070076 nhsel < fib_info_num_path((fi)); \
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000077 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#else /* CONFIG_IP_ROUTE_MULTIPATH */
80
81/* Hope, that gcc will optimize it to get rid of dummy loop */
82
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000083#define for_nexthops(fi) { \
84 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
85 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000087#define change_nexthops(fi) { \
88 int nhsel; \
89 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
90 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#endif /* CONFIG_IP_ROUTE_MULTIPATH */
93
94#define endfor_nexthops(fi) }
95
96
David S. Miller3be06862011-03-07 15:01:10 -080097const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000098 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .error = 0,
100 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000101 },
102 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .error = 0,
104 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000105 },
106 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .error = 0,
108 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000109 },
110 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .error = 0,
112 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000113 },
114 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .error = 0,
116 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000117 },
118 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .error = 0,
120 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000121 },
122 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .error = -EINVAL,
124 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000125 },
126 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .error = -EHOSTUNREACH,
128 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000129 },
130 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .error = -EACCES,
132 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000133 },
134 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .error = -EAGAIN,
136 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000137 },
138 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .error = -EINVAL,
140 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000141 },
142 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .error = -EINVAL,
144 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000145 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
David S. Millerc5038a82012-07-31 15:02:02 -0700148static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000149{
150 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
151
152 if (!rt)
153 return;
154
155 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
156 * because we waited an RCU grace period before calling
157 * free_fib_info_rcu()
158 */
159
Wei Wang95c47f92017-06-17 10:42:30 -0700160 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700161 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000162}
163
David Aherna5995e72019-04-30 07:45:50 -0700164static void free_nh_exceptions(struct fib_nh_common *nhc)
David S. Millerc5038a82012-07-31 15:02:02 -0700165{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700166 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700167 int i;
168
David Aherna5995e72019-04-30 07:45:50 -0700169 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Eric Dumazetcaa41522014-09-03 22:21:56 -0700170 if (!hash)
171 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700172 for (i = 0; i < FNHE_HASH_SIZE; i++) {
173 struct fib_nh_exception *fnhe;
174
175 fnhe = rcu_dereference_protected(hash[i].chain, 1);
176 while (fnhe) {
177 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800178
David S. Millerc5038a82012-07-31 15:02:02 -0700179 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
180
Timo Teräs2ffae992013-06-27 10:27:05 +0300181 rt_fibinfo_free(&fnhe->fnhe_rth_input);
182 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700183
184 kfree(fnhe);
185
186 fnhe = next;
187 }
188 }
189 kfree(hash);
190}
191
192static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000193{
194 int cpu;
195
196 if (!rtp)
197 return;
198
199 for_each_possible_cpu(cpu) {
200 struct rtable *rt;
201
202 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700203 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700204 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700205 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700206 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000207 }
208 free_percpu(rtp);
209}
210
David Ahern979e2762019-03-27 20:53:58 -0700211void fib_nh_common_release(struct fib_nh_common *nhc)
212{
Eric Dumazete44b14e2021-12-06 17:30:32 -0800213 dev_put_track(nhc->nhc_dev, &nhc->nhc_dev_tracker);
David Ahern979e2762019-03-27 20:53:58 -0700214 lwtstate_put(nhc->nhc_lwtstate);
David Ahern0f457a32019-04-30 07:45:48 -0700215 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
216 rt_fibinfo_free(&nhc->nhc_rth_input);
David Aherna5995e72019-04-30 07:45:50 -0700217 free_nh_exceptions(nhc);
David Ahern979e2762019-03-27 20:53:58 -0700218}
219EXPORT_SYMBOL_GPL(fib_nh_common_release);
220
David Ahernfaa041a2019-03-27 20:53:49 -0700221void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
222{
223#ifdef CONFIG_IP_ROUTE_CLASSID
224 if (fib_nh->nh_tclassid)
Eric Dumazet213f5f82021-12-01 18:26:35 -0800225 atomic_dec(&net->ipv4.fib_num_tclassid_users);
David Ahernfaa041a2019-03-27 20:53:49 -0700226#endif
David Ahern979e2762019-03-27 20:53:58 -0700227 fib_nh_common_release(&fib_nh->nh_common);
David Ahernfaa041a2019-03-27 20:53:49 -0700228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000231static void free_fib_info_rcu(struct rcu_head *head)
232{
233 struct fib_info *fi = container_of(head, struct fib_info, rcu);
234
David Ahern4c7e8082019-06-03 20:19:51 -0700235 if (fi->nh) {
236 nexthop_put(fi->nh);
237 } else {
238 change_nexthops(fi) {
239 fib_nh_release(fi->fib_net, nexthop_nh);
240 } endfor_nexthops(fi);
241 }
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000242
David Aherncc5f0eb2018-10-04 20:07:52 -0700243 ip_fib_metrics_put(fi->fib_metrics);
244
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000245 kfree(fi);
246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248void free_fib_info(struct fib_info *fi)
249{
250 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000251 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return;
253 }
David Ahernfaa041a2019-03-27 20:53:49 -0700254
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000255 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100257EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259void fib_release_info(struct fib_info *fi)
260{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700261 spin_lock_bh(&fib_info_lock);
Yajun Deng79976892021-07-29 15:13:50 +0800262 if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 hlist_del(&fi->fib_hash);
Eric Dumazet0a6e6b32022-01-16 01:02:20 -0800264
265 /* Paired with READ_ONCE() in fib_create_info(). */
266 WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (fi->fib_prefsrc)
269 hlist_del(&fi->fib_lhash);
David Ahern4c7e8082019-06-03 20:19:51 -0700270 if (fi->nh) {
271 list_del(&fi->nh_list);
272 } else {
273 change_nexthops(fi) {
274 if (!nexthop_nh->fib_nh_dev)
275 continue;
276 hlist_del(&nexthop_nh->nh_hash);
277 } endfor_nexthops(fi)
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 fi->fib_dead = 1;
280 fib_info_put(fi);
281 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700282 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
David Ahern5481d732019-06-03 20:19:49 -0700285static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
David Ahern5481d732019-06-03 20:19:49 -0700287 const struct fib_nh *onh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
David Ahern4c7e8082019-06-03 20:19:51 -0700289 if (fi->nh || ofi->nh)
290 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
291
292 if (ofi->fib_nhs == 0)
293 return 0;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 for_nexthops(fi) {
David Ahern5481d732019-06-03 20:19:49 -0700296 onh = fib_info_nh(ofi, nhsel);
297
David Ahernb75ed8b2019-03-27 20:53:55 -0700298 if (nh->fib_nh_oif != onh->fib_nh_oif ||
David Aherna4ea5d42019-04-05 16:30:30 -0700299 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
David Ahernb75ed8b2019-03-27 20:53:55 -0700300 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700302 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100304#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 nh->nh_tclassid != onh->nh_tclassid ||
306#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700307 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
308 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -1;
David Aherna4ea5d42019-04-05 16:30:30 -0700310
311 if (nh->fib_nh_gw_family == AF_INET &&
312 nh->fib_nh_gw4 != onh->fib_nh_gw4)
313 return -1;
314
315 if (nh->fib_nh_gw_family == AF_INET6 &&
316 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
317 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } endfor_nexthops(fi);
319 return 0;
320}
321
David S. Miller88ebc722008-01-12 21:49:01 -0800322static inline unsigned int fib_devindex_hashfn(unsigned int val)
323{
Eric Dumazetd07418a2022-01-19 02:04:12 -0800324 return hash_32(val, DEVINDEX_HASHBITS);
325}
David S. Miller88ebc722008-01-12 21:49:01 -0800326
Eric Dumazetd07418a2022-01-19 02:04:12 -0800327static struct hlist_head *
328fib_info_devhash_bucket(const struct net_device *dev)
329{
330 u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
331
332 return &fib_info_devhash[fib_devindex_hashfn(val)];
David S. Miller88ebc722008-01-12 21:49:01 -0800333}
334
David Ahern6c48ea52019-06-08 14:53:33 -0700335static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
336 u32 prefsrc, u32 priority)
337{
338 unsigned int val = init_val;
339
340 val ^= (protocol << 8) | scope;
341 val ^= prefsrc;
342 val ^= priority;
343
344 return val;
345}
346
347static unsigned int fib_info_hashfn_result(unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
David S. Miller123b9732011-02-01 15:34:21 -0800349 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David Ahern6c48ea52019-06-08 14:53:33 -0700351 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
352}
353
354static inline unsigned int fib_info_hashfn(struct fib_info *fi)
355{
356 unsigned int val;
357
358 val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
359 fi->fib_scope, (__force u32)fi->fib_prefsrc,
360 fi->fib_priority);
David Ahern4c7e8082019-06-03 20:19:51 -0700361
362 if (fi->nh) {
363 val ^= fib_devindex_hashfn(fi->nh->id);
364 } else {
365 for_nexthops(fi) {
366 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
367 } endfor_nexthops(fi)
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David Ahern6c48ea52019-06-08 14:53:33 -0700370 return fib_info_hashfn_result(val);
371}
372
373/* no metrics, only nexthop id */
374static struct fib_info *fib_find_info_nh(struct net *net,
375 const struct fib_config *cfg)
376{
377 struct hlist_head *head;
378 struct fib_info *fi;
379 unsigned int hash;
380
381 hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
382 cfg->fc_protocol, cfg->fc_scope,
383 (__force u32)cfg->fc_prefsrc,
384 cfg->fc_priority);
385 hash = fib_info_hashfn_result(hash);
386 head = &fib_info_hash[hash];
387
388 hlist_for_each_entry(fi, head, fib_hash) {
389 if (!net_eq(fi->fib_net, net))
390 continue;
391 if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
392 continue;
393 if (cfg->fc_protocol == fi->fib_protocol &&
394 cfg->fc_scope == fi->fib_scope &&
395 cfg->fc_prefsrc == fi->fib_prefsrc &&
396 cfg->fc_priority == fi->fib_priority &&
397 cfg->fc_type == fi->fib_type &&
398 cfg->fc_table == fi->fib_tb_id &&
399 !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
400 return fi;
401 }
402
403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
David Ahern5481d732019-06-03 20:19:49 -0700406static struct fib_info *fib_find_info(struct fib_info *nfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct fib_info *fi;
410 unsigned int hash;
411
412 hash = fib_info_hashfn(nfi);
413 head = &fib_info_hash[hash];
414
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800416 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800417 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (fi->fib_nhs != nfi->fib_nhs)
419 continue;
420 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700421 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 nfi->fib_prefsrc == fi->fib_prefsrc &&
423 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000424 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000426 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400427 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
David Ahern4c7e8082019-06-03 20:19:51 -0700428 nh_comp(fi, nfi) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return fi;
430 }
431
432 return NULL;
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000436 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
Al Virod878e72e2006-09-26 22:18:13 -0700438int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct fib_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700443 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Eric Dumazetd07418a2022-01-19 02:04:12 -0800445 head = fib_info_devhash_bucket(dev);
446
Sasha Levinb67bfe02013-02-27 17:06:00 -0800447 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700448 if (nh->fib_nh_dev == dev &&
449 nh->fib_nh_gw4 == gw &&
450 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700451 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453 }
454 }
455
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700456 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return -1;
459}
460
Amit Cohen1e7bdec2021-02-01 21:47:51 +0200461size_t fib_nlmsg_size(struct fib_info *fi)
Thomas Graf339bf982006-11-10 14:10:15 -0800462{
463 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
464 + nla_total_size(4) /* RTA_TABLE */
465 + nla_total_size(4) /* RTA_DST */
466 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100467 + nla_total_size(4) /* RTA_PREFSRC */
468 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
David Ahern5481d732019-06-03 20:19:49 -0700469 unsigned int nhs = fib_info_num_path(fi);
Thomas Graf339bf982006-11-10 14:10:15 -0800470
471 /* space for nested metrics */
472 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
473
David Ahern4c7e8082019-06-03 20:19:51 -0700474 if (fi->nh)
475 payload += nla_total_size(4); /* RTA_NH_ID */
476
David Ahern5481d732019-06-03 20:19:49 -0700477 if (nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200478 size_t nh_encapsize = 0;
David Ahern5481d732019-06-03 20:19:49 -0700479 /* Also handles the special case nhs == 1 */
Thomas Graf339bf982006-11-10 14:10:15 -0800480
481 /* each nexthop is packed in an attribute */
482 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
David Aherndcb1ecb2019-06-03 20:19:50 -0700483 unsigned int i;
Thomas Graf339bf982006-11-10 14:10:15 -0800484
485 /* may contain flow and gateway attribute */
486 nhsize += 2 * nla_total_size(4);
487
Roopa Prabhu571e7222015-07-21 10:43:47 +0200488 /* grab encap info */
David Aherndcb1ecb2019-06-03 20:19:50 -0700489 for (i = 0; i < fib_info_num_path(fi); i++) {
490 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
491
492 if (nhc->nhc_lwtstate) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200493 /* RTA_ENCAP_TYPE */
494 nh_encapsize += lwtunnel_get_encap_size(
David Aherndcb1ecb2019-06-03 20:19:50 -0700495 nhc->nhc_lwtstate);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200496 /* RTA_ENCAP */
497 nh_encapsize += nla_total_size(2);
498 }
David Aherndcb1ecb2019-06-03 20:19:50 -0700499 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200500
Thomas Graf339bf982006-11-10 14:10:15 -0800501 /* all nexthops are packed in a nested attribute */
David Ahern5481d732019-06-03 20:19:49 -0700502 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200503
Thomas Graf339bf982006-11-10 14:10:15 -0800504 }
505
506 return payload;
507}
508
Al Viro81f7bf62006-09-27 18:40:00 -0700509void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700510 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700511 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200513 struct fib_rt_info fri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700515 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700516 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Thomas Graf339bf982006-11-10 14:10:15 -0800518 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100519 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700520 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200522 fri.fi = fa->fa_info;
523 fri.tb_id = tb_id;
524 fri.dst = key;
525 fri.dst_len = dst_len;
526 fri.tos = fa->fa_tos;
527 fri.type = fa->fa_type;
Ido Schimmel90b93f12020-01-14 13:23:11 +0200528 fri.offload = fa->offload;
529 fri.trap = fa->trap;
Amit Cohen36c51002021-02-07 10:22:50 +0200530 fri.offload_failed = fa->offload_failed;
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200531 err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800532 if (err < 0) {
533 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
534 WARN_ON(err == -EMSGSIZE);
535 kfree_skb(skb);
536 goto errout;
537 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800539 info->nlh, GFP_KERNEL);
540 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700541errout:
542 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800543 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800546static int fib_detect_death(struct fib_info *fi, int order,
547 struct fib_info **last_resort, int *last_idx,
548 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
David Ahern619d1822019-04-05 16:30:37 -0700550 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct neighbour *n;
552 int state = NUD_NONE;
553
David Ahern619d1822019-04-05 16:30:37 -0700554 if (likely(nhc->nhc_gw_family == AF_INET))
555 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
556 else if (nhc->nhc_gw_family == AF_INET6)
557 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
558 nhc->nhc_dev);
559 else
560 n = NULL;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (n) {
563 state = n->nud_state;
564 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300565 } else {
566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800568 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000570 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000572 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300573 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *last_resort = fi;
575 *last_idx = order;
576 }
577 return 1;
578}
579
Alexander Aringfaee6762020-03-27 18:00:21 -0400580int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
581 struct nlattr *encap, u16 encap_type,
582 void *cfg, gfp_t gfp_flags,
David Ahern979e2762019-03-27 20:53:58 -0700583 struct netlink_ext_ack *extack)
584{
David Ahern0f457a32019-04-30 07:45:48 -0700585 int err;
586
587 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
588 gfp_flags);
589 if (!nhc->nhc_pcpu_rth_output)
590 return -ENOMEM;
591
David Ahern979e2762019-03-27 20:53:58 -0700592 if (encap) {
593 struct lwtunnel_state *lwtstate;
David Ahern979e2762019-03-27 20:53:58 -0700594
595 if (encap_type == LWTUNNEL_ENCAP_NONE) {
596 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
David Ahern0f457a32019-04-30 07:45:48 -0700597 err = -EINVAL;
598 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700599 }
Alexander Aringfaee6762020-03-27 18:00:21 -0400600 err = lwtunnel_build_state(net, encap_type, encap,
601 nhc->nhc_family, cfg, &lwtstate,
602 extack);
David Ahern979e2762019-03-27 20:53:58 -0700603 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700604 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700605
606 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
607 }
608
609 return 0;
David Ahern0f457a32019-04-30 07:45:48 -0700610
611lwt_failure:
612 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
613 nhc->nhc_pcpu_rth_output = NULL;
614 return err;
David Ahern979e2762019-03-27 20:53:58 -0700615}
616EXPORT_SYMBOL_GPL(fib_nh_common_init);
617
David Aherne4516ef2019-03-27 20:53:48 -0700618int fib_nh_init(struct net *net, struct fib_nh *nh,
619 struct fib_config *cfg, int nh_weight,
620 struct netlink_ext_ack *extack)
621{
David Ahern0f457a32019-04-30 07:45:48 -0700622 int err;
David Aherne4516ef2019-03-27 20:53:48 -0700623
David Ahernf1741732019-03-27 20:53:57 -0700624 nh->fib_nh_family = AF_INET;
625
Alexander Aringfaee6762020-03-27 18:00:21 -0400626 err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
David Ahern979e2762019-03-27 20:53:58 -0700627 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
628 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700629 return err;
David Aherne4516ef2019-03-27 20:53:48 -0700630
David Ahernb75ed8b2019-03-27 20:53:55 -0700631 nh->fib_nh_oif = cfg->fc_oif;
David Aherna4ea5d42019-04-05 16:30:30 -0700632 nh->fib_nh_gw_family = cfg->fc_gw_family;
633 if (cfg->fc_gw_family == AF_INET)
David Ahernf35b7942019-04-05 16:30:28 -0700634 nh->fib_nh_gw4 = cfg->fc_gw4;
David Aherna4ea5d42019-04-05 16:30:30 -0700635 else if (cfg->fc_gw_family == AF_INET6)
636 nh->fib_nh_gw6 = cfg->fc_gw6;
637
David Ahernb75ed8b2019-03-27 20:53:55 -0700638 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700639
640#ifdef CONFIG_IP_ROUTE_CLASSID
641 nh->nh_tclassid = cfg->fc_flow;
642 if (nh->nh_tclassid)
Eric Dumazet213f5f82021-12-01 18:26:35 -0800643 atomic_inc(&net->ipv4.fib_num_tclassid_users);
David Aherne4516ef2019-03-27 20:53:48 -0700644#endif
645#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700646 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700647#endif
648 return 0;
David Aherne4516ef2019-03-27 20:53:48 -0700649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#ifdef CONFIG_IP_ROUTE_MULTIPATH
652
David Ahern6d8422a12017-05-21 10:12:02 -0600653static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
654 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Thomas Graf4e902c52006-08-17 18:14:52 -0700658 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700660 rtnh = rtnh_next(rtnh, &remaining);
661 }
662
663 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600664 if (remaining > 0) {
665 NL_SET_ERR_MSG(extack,
666 "Invalid nexthop configuration - extra data after nexthops");
667 nhs = 0;
668 }
669
670 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
David Ahern7a3429b2021-12-30 17:36:31 -0700673static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
674 struct netlink_ext_ack *extack)
675{
676 if (nla_len(nla) < sizeof(*gw)) {
677 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
678 return -EINVAL;
679 }
680
681 *gw = nla_get_in_addr(nla);
682
683 return 0;
684}
685
David Ahern4c7e8082019-06-03 20:19:51 -0700686/* only called when fib_nh is integrated into fib_info */
Thomas Graf4e902c52006-08-17 18:14:52 -0700687static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600688 int remaining, struct fib_config *cfg,
689 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
David Aherne4516ef2019-03-27 20:53:48 -0700691 struct net *net = fi->fib_net;
692 struct fib_config fib_cfg;
David Ahern5481d732019-06-03 20:19:49 -0700693 struct fib_nh *nh;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200694 int ret;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700697 int attrlen;
698
David Aherne4516ef2019-03-27 20:53:48 -0700699 memset(&fib_cfg, 0, sizeof(fib_cfg));
700
David Ahernc3ab2b42017-05-21 10:12:03 -0600701 if (!rtnh_ok(rtnh, remaining)) {
702 NL_SET_ERR_MSG(extack,
703 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600705 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700706
David Ahernc3ab2b42017-05-21 10:12:03 -0600707 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
708 NL_SET_ERR_MSG(extack,
709 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300710 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600711 }
Julian Anastasov80610222016-07-10 21:11:55 +0300712
David Aherne4516ef2019-03-27 20:53:48 -0700713 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
714 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700715
716 attrlen = rtnh_attrlen(rtnh);
717 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700718 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700719
720 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700721 nlav = nla_find(attrs, attrlen, RTA_VIA);
722 if (nla && nlav) {
723 NL_SET_ERR_MSG(extack,
724 "Nexthop configuration can not contain both GATEWAY and VIA");
725 return -EINVAL;
726 }
David Ahernf35b7942019-04-05 16:30:28 -0700727 if (nla) {
David Ahern7a3429b2021-12-30 17:36:31 -0700728 ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
729 extack);
730 if (ret)
731 goto errout;
732
David Ahernd73f80f2019-04-10 10:05:51 -0700733 if (fib_cfg.fc_gw4)
734 fib_cfg.fc_gw_family = AF_INET;
David Ahernd1566262019-04-05 16:30:40 -0700735 } else if (nlav) {
736 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
737 if (ret)
738 goto errout;
David Ahernf35b7942019-04-05 16:30:28 -0700739 }
David Aherne4516ef2019-03-27 20:53:48 -0700740
Thomas Graf4e902c52006-08-17 18:14:52 -0700741 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Ahern664b9c42021-12-30 17:36:32 -0700742 if (nla) {
743 if (nla_len(nla) < sizeof(u32)) {
744 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
745 return -EINVAL;
746 }
David Aherne4516ef2019-03-27 20:53:48 -0700747 fib_cfg.fc_flow = nla_get_u32(nla);
David Ahern664b9c42021-12-30 17:36:32 -0700748 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200749
David Aherne4516ef2019-03-27 20:53:48 -0700750 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
David Ahern8bda81a2021-12-30 17:36:35 -0700751 /* RTA_ENCAP_TYPE length checked in
752 * lwtunnel_valid_encap_type_attr
753 */
David Aherne4516ef2019-03-27 20:53:48 -0700754 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
755 if (nla)
756 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700758
David Aherne4516ef2019-03-27 20:53:48 -0700759 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
760 rtnh->rtnh_hops + 1, extack);
761 if (ret)
762 goto errout;
763
Thomas Graf4e902c52006-08-17 18:14:52 -0700764 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700766
Roopa Prabhu571e7222015-07-21 10:43:47 +0200767 ret = -EINVAL;
David Ahern5481d732019-06-03 20:19:49 -0700768 nh = fib_info_nh(fi, 0);
769 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700770 NL_SET_ERR_MSG(extack,
771 "Nexthop device index does not match RTA_OIF");
772 goto errout;
773 }
David Ahernf35b7942019-04-05 16:30:28 -0700774 if (cfg->fc_gw_family) {
David Ahern5481d732019-06-03 20:19:49 -0700775 if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
David Ahernf35b7942019-04-05 16:30:28 -0700776 (cfg->fc_gw_family == AF_INET &&
David Ahern5481d732019-06-03 20:19:49 -0700777 nh->fib_nh_gw4 != cfg->fc_gw4) ||
David Aherna4ea5d42019-04-05 16:30:30 -0700778 (cfg->fc_gw_family == AF_INET6 &&
David Ahern5481d732019-06-03 20:19:49 -0700779 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
David Ahernf35b7942019-04-05 16:30:28 -0700780 NL_SET_ERR_MSG(extack,
David Aherna4ea5d42019-04-05 16:30:30 -0700781 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
David Ahernf35b7942019-04-05 16:30:28 -0700782 goto errout;
783 }
David Aherne4516ef2019-03-27 20:53:48 -0700784 }
785#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahern5481d732019-06-03 20:19:49 -0700786 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
David Aherne4516ef2019-03-27 20:53:48 -0700787 NL_SET_ERR_MSG(extack,
788 "Nexthop class id does not match RTA_FLOW");
789 goto errout;
790 }
791#endif
792 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200793errout:
794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
David Ahern4c7e8082019-06-03 20:19:51 -0700797/* only called when fib_nh is integrated into fib_info */
Peter Nørlund0e884c72015-09-30 10:12:21 +0200798static void fib_rebalance(struct fib_info *fi)
799{
800 int total;
801 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200802
David Ahern5481d732019-06-03 20:19:49 -0700803 if (fib_info_num_path(fi) < 2)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200804 return;
805
806 total = 0;
807 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700808 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200809 continue;
810
David Ahernb75ed8b2019-03-27 20:53:55 -0700811 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
812 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200813 continue;
814
David Ahernb75ed8b2019-03-27 20:53:55 -0700815 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200816 } endfor_nexthops(fi);
817
818 w = 0;
819 change_nexthops(fi) {
820 int upper_bound;
821
David Ahernb75ed8b2019-03-27 20:53:55 -0700822 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200823 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700824 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
825 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200826 upper_bound = -1;
827 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700828 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200829 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
830 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200831 }
832
David Ahernb75ed8b2019-03-27 20:53:55 -0700833 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200834 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200835}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200836#else /* CONFIG_IP_ROUTE_MULTIPATH */
837
David Ahern8373c6c2019-03-27 20:53:46 -0700838static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
839 int remaining, struct fib_config *cfg,
840 struct netlink_ext_ack *extack)
841{
842 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
843
844 return -EINVAL;
845}
846
Peter Nørlund0e884c72015-09-30 10:12:21 +0200847#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200848
849#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Alexander Aringfaee6762020-03-27 18:00:21 -0400851static int fib_encap_match(struct net *net, u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800852 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800853 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600854 const struct fib_config *cfg,
855 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200856{
857 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200858 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200859
860 if (encap_type == LWTUNNEL_ENCAP_NONE)
861 return 0;
862
Alexander Aringfaee6762020-03-27 18:00:21 -0400863 ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
David Ahern9ae28722017-05-27 16:19:28 -0600864 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200865 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700866 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200867 lwtstate_free(lwtstate);
868 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200869
Jiri Bencdf383e62015-08-18 18:41:13 +0200870 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200871}
872
Alexander Aringfaee6762020-03-27 18:00:21 -0400873int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
David Ahern9ae28722017-05-27 16:19:28 -0600874 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700877 struct rtnexthop *rtnh;
878 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#endif
880
Thomas Graf4e902c52006-08-17 18:14:52 -0700881 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 1;
883
David Ahern493ced12019-06-08 14:53:32 -0700884 if (cfg->fc_nh_id) {
885 if (fi->nh && cfg->fc_nh_id == fi->nh->id)
886 return 0;
887 return 1;
888 }
889
David Ahernf35b7942019-04-05 16:30:28 -0700890 if (cfg->fc_oif || cfg->fc_gw_family) {
David Ahern5481d732019-06-03 20:19:49 -0700891 struct fib_nh *nh = fib_info_nh(fi, 0);
892
Roopa Prabhu571e7222015-07-21 10:43:47 +0200893 if (cfg->fc_encap) {
Alexander Aringfaee6762020-03-27 18:00:21 -0400894 if (fib_encap_match(net, cfg->fc_encap_type,
895 cfg->fc_encap, nh, cfg, extack))
David Ahern9ae28722017-05-27 16:19:28 -0600896 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200897 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100898#ifdef CONFIG_IP_ROUTE_CLASSID
899 if (cfg->fc_flow &&
David Ahern5481d732019-06-03 20:19:49 -0700900 cfg->fc_flow != nh->nh_tclassid)
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100901 return 1;
902#endif
David Ahern5481d732019-06-03 20:19:49 -0700903 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
David Ahernf35b7942019-04-05 16:30:28 -0700904 (cfg->fc_gw_family &&
David Ahern5481d732019-06-03 20:19:49 -0700905 cfg->fc_gw_family != nh->fib_nh_gw_family))
David Ahernf35b7942019-04-05 16:30:28 -0700906 return 1;
907
908 if (cfg->fc_gw_family == AF_INET &&
David Ahern5481d732019-06-03 20:19:49 -0700909 cfg->fc_gw4 != nh->fib_nh_gw4)
David Ahernf35b7942019-04-05 16:30:28 -0700910 return 1;
911
David Aherna4ea5d42019-04-05 16:30:30 -0700912 if (cfg->fc_gw_family == AF_INET6 &&
David Ahern5481d732019-06-03 20:19:49 -0700913 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
David Aherna4ea5d42019-04-05 16:30:30 -0700914 return 1;
915
David Ahernf35b7942019-04-05 16:30:28 -0700916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100920 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700922
923 rtnh = cfg->fc_mp;
924 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700927 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Thomas Graf4e902c52006-08-17 18:14:52 -0700929 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700931
David Ahernb75ed8b2019-03-27 20:53:55 -0700932 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700934
935 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200936 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700937 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
David Ahern7a3429b2021-12-30 17:36:31 -0700938 int err;
Thomas Graf4e902c52006-08-17 18:14:52 -0700939
940 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700941 nlav = nla_find(attrs, attrlen, RTA_VIA);
942 if (nla && nlav) {
943 NL_SET_ERR_MSG(extack,
944 "Nexthop configuration can not contain both GATEWAY and VIA");
945 return -EINVAL;
946 }
947
948 if (nla) {
David Ahern7a3429b2021-12-30 17:36:31 -0700949 __be32 gw;
950
951 err = fib_gw_from_attr(&gw, nla, extack);
952 if (err)
953 return err;
954
David Ahernd1566262019-04-05 16:30:40 -0700955 if (nh->fib_nh_gw_family != AF_INET ||
David Ahern7a3429b2021-12-30 17:36:31 -0700956 gw != nh->fib_nh_gw4)
David Ahernd1566262019-04-05 16:30:40 -0700957 return 1;
958 } else if (nlav) {
959 struct fib_config cfg2;
David Ahernd1566262019-04-05 16:30:40 -0700960
961 err = fib_gw_from_via(&cfg2, nlav, extack);
962 if (err)
963 return err;
964
965 switch (nh->fib_nh_gw_family) {
966 case AF_INET:
967 if (cfg2.fc_gw_family != AF_INET ||
968 cfg2.fc_gw4 != nh->fib_nh_gw4)
969 return 1;
970 break;
971 case AF_INET6:
972 if (cfg2.fc_gw_family != AF_INET6 ||
973 ipv6_addr_cmp(&cfg2.fc_gw6,
974 &nh->fib_nh_gw6))
975 return 1;
976 break;
977 }
978 }
979
Patrick McHardyc7066f72011-01-14 13:36:42 +0100980#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700981 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Ahern664b9c42021-12-30 17:36:32 -0700982 if (nla) {
983 if (nla_len(nla) < sizeof(u32)) {
984 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
985 return -EINVAL;
986 }
987 if (nla_get_u32(nla) != nh->nh_tclassid)
988 return 1;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#endif
991 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700992
993 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 } endfor_nexthops(fi);
995#endif
996 return 0;
997}
998
Xin Long5f9ae3d2017-08-23 10:07:26 +0800999bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
1000{
1001 struct nlattr *nla;
1002 int remaining;
1003
1004 if (!cfg->fc_mx)
1005 return true;
1006
1007 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1008 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +01001009 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +08001010
1011 if (!type)
1012 continue;
1013 if (type > RTAX_MAX)
1014 return false;
1015
1016 if (type == RTAX_CC_ALGO) {
1017 char tmp[TCP_CA_NAME_MAX];
1018 bool ecn_ca = false;
1019
Francis Laniel872f6902020-11-15 18:08:06 +01001020 nla_strscpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -08001021 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +08001022 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -07001023 if (nla_len(nla) != sizeof(u32))
1024 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +08001025 val = nla_get_u32(nla);
1026 }
1027
Phil Sutterd03a4552017-12-19 15:17:13 +01001028 fi_val = fi->fib_metrics->metrics[type - 1];
1029 if (type == RTAX_FEATURES)
1030 fi_val &= ~DST_FEATURE_ECN_CA;
1031
1032 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +08001033 return false;
1034 }
1035
1036 return true;
1037}
1038
David Ahern717a8f52019-04-05 16:30:32 -07001039static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1040 u32 table, struct netlink_ext_ack *extack)
1041{
1042 struct fib6_config cfg = {
1043 .fc_table = table,
1044 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1045 .fc_ifindex = nh->fib_nh_oif,
1046 .fc_gateway = nh->fib_nh_gw6,
1047 };
1048 struct fib6_nh fib6_nh = {};
1049 int err;
1050
1051 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1052 if (!err) {
1053 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001054 dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_KERNEL);
David Ahern717a8f52019-04-05 16:30:32 -07001055 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1056 nh->fib_nh_scope = RT_SCOPE_LINK;
1057
1058 ipv6_stub->fib6_nh_release(&fib6_nh);
1059 }
1060
1061 return err;
1062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001065 * Picture
1066 * -------
1067 *
1068 * Semantics of nexthop is very messy by historical reasons.
1069 * We have to take into account, that:
1070 * a) gateway can be actually local interface address,
1071 * so that gatewayed route is direct.
1072 * b) gateway must be on-link address, possibly
1073 * described not by an ifaddr, but also by a direct route.
1074 * c) If both gateway and interface are specified, they should not
1075 * contradict.
1076 * d) If we use tunnel routes, gateway could be not on-link.
1077 *
1078 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1079 * results in pretty ugly and hairy code with obscure logic.
1080 *
1081 * I chose to generalized it instead, so that the size
1082 * of code does not increase practically, but it becomes
1083 * much more general.
1084 * Every prefix is assigned a "scope" value: "host" is local address,
1085 * "link" is direct route,
1086 * [ ... "site" ... "interior" ... ]
1087 * and "universe" is true gateway route with global meaning.
1088 *
1089 * Every prefix refers to a set of "nexthop"s (gw, oif),
1090 * where gw must have narrower scope. This recursion stops
1091 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1092 * which means that gw is forced to be on link.
1093 *
1094 * Code is still hairy, but now it is apparently logically
1095 * consistent and very flexible. F.e. as by-product it allows
1096 * to co-exists in peace independent exterior and interior
1097 * routing processes.
1098 *
1099 * Normally it looks as following.
1100 *
1101 * {universe prefix} -> (gw, oif) [scope link]
1102 * |
1103 * |-> {link prefix} -> (gw, oif) [scope local]
1104 * |
1105 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
David Ahern448d7242019-04-05 16:30:31 -07001107static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1108 u8 scope, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001110 struct net_device *dev;
David Ahern448d7242019-04-05 16:30:31 -07001111 struct fib_result res;
Enrico Weigeltc3fee642019-06-06 16:43:17 +02001112 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
David Ahern448d7242019-04-05 16:30:31 -07001114 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1115 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
David Ahern448d7242019-04-05 16:30:31 -07001117 if (scope >= RT_SCOPE_LINK) {
1118 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
David Ahern448d7242019-04-05 16:30:31 -07001121 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1122 if (!dev) {
1123 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1124 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
David Ahern448d7242019-04-05 16:30:31 -07001126 if (!(dev->flags & IFF_UP)) {
1127 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1128 return -ENETDOWN;
1129 }
1130 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1131 if (addr_type != RTN_UNICAST) {
1132 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1133 return -EINVAL;
1134 }
1135 if (!netif_carrier_ok(dev))
1136 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1137 nh->fib_nh_dev = dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001138 dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
David Ahern448d7242019-04-05 16:30:31 -07001139 nh->fib_nh_scope = RT_SCOPE_LINK;
1140 return 0;
1141 }
1142 rcu_read_lock();
1143 {
1144 struct fib_table *tbl = NULL;
1145 struct flowi4 fl4 = {
1146 .daddr = nh->fib_nh_gw4,
1147 .flowi4_scope = scope + 1,
1148 .flowi4_oif = nh->fib_nh_oif,
1149 .flowi4_iif = LOOPBACK_IFINDEX,
1150 };
1151
1152 /* It is not necessary, but requires a bit of thinking */
1153 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1154 fl4.flowi4_scope = RT_SCOPE_LINK;
1155
guodeqing5eea3a62020-06-17 10:07:16 +08001156 if (table && table != RT_TABLE_MAIN)
David Ahern448d7242019-04-05 16:30:31 -07001157 tbl = fib_get_table(net, table);
1158
1159 if (tbl)
1160 err = fib_table_lookup(tbl, &fl4, &res,
1161 FIB_LOOKUP_IGNORE_LINKSTATE |
1162 FIB_LOOKUP_NOREF);
1163
1164 /* on error or if no table given do full lookup. This
1165 * is needed for example when nexthops are in the local
1166 * table rather than the given table
1167 */
1168 if (!tbl || err) {
1169 err = fib_lookup(net, &fl4, &res,
1170 FIB_LOOKUP_IGNORE_LINKSTATE);
1171 }
1172
1173 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001174 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -06001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
David Ahern448d7242019-04-05 16:30:31 -07001178
1179 err = -EINVAL;
1180 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1181 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1182 goto out;
1183 }
1184 nh->fib_nh_scope = res.scope;
1185 nh->fib_nh_oif = FIB_RES_OIF(res);
1186 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1187 if (!dev) {
1188 NL_SET_ERR_MSG(extack,
1189 "No egress device for nexthop gateway");
1190 goto out;
1191 }
Eric Dumazete44b14e2021-12-06 17:30:32 -08001192 dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
David Ahern448d7242019-04-05 16:30:31 -07001193 if (!netif_carrier_ok(dev))
1194 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1195 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +00001196out:
1197 rcu_read_unlock();
1198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
David Ahern448d7242019-04-05 16:30:31 -07001201static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1202 struct netlink_ext_ack *extack)
1203{
1204 struct in_device *in_dev;
1205 int err;
1206
1207 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1208 NL_SET_ERR_MSG(extack,
1209 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1210 return -EINVAL;
1211 }
1212
1213 rcu_read_lock();
1214
1215 err = -ENODEV;
1216 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1217 if (!in_dev)
1218 goto out;
1219 err = -ENETDOWN;
1220 if (!(in_dev->dev->flags & IFF_UP)) {
1221 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1222 goto out;
1223 }
1224
1225 nh->fib_nh_dev = in_dev->dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001226 dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
David Ahern448d7242019-04-05 16:30:31 -07001227 nh->fib_nh_scope = RT_SCOPE_HOST;
1228 if (!netif_carrier_ok(nh->fib_nh_dev))
1229 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1230 err = 0;
1231out:
1232 rcu_read_unlock();
1233 return err;
1234}
1235
David Ahernac1fab22019-05-22 12:04:43 -07001236int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1237 struct netlink_ext_ack *extack)
David Ahern448d7242019-04-05 16:30:31 -07001238{
David Ahern448d7242019-04-05 16:30:31 -07001239 int err;
1240
1241 if (nh->fib_nh_gw_family == AF_INET)
David Ahernac1fab22019-05-22 12:04:43 -07001242 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
David Ahern717a8f52019-04-05 16:30:32 -07001243 else if (nh->fib_nh_gw_family == AF_INET6)
1244 err = fib_check_nh_v6_gw(net, nh, table, extack);
David Ahern448d7242019-04-05 16:30:31 -07001245 else
1246 err = fib_check_nh_nongw(net, nh, extack);
1247
1248 return err;
1249}
1250
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001251static struct hlist_head *
1252fib_info_laddrhash_bucket(const struct net *net, __be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001254 u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val,
1255 fib_info_hash_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001257 return &fib_info_laddrhash[slot];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
David S. Miller123b9732011-02-01 15:34:21 -08001260static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +08001263 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 else
1265 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001266 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1267 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
David S. Miller123b9732011-02-01 15:34:21 -08001270static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 if (!hash)
1273 return;
1274
1275 if (bytes <= PAGE_SIZE)
1276 kfree(hash);
1277 else
1278 free_pages((unsigned long) hash, get_order(bytes));
1279}
1280
David S. Miller123b9732011-02-01 15:34:21 -08001281static void fib_info_hash_move(struct hlist_head *new_info_hash,
1282 struct hlist_head *new_laddrhash,
1283 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
David S. Millerb7656e72005-08-05 04:12:48 -07001285 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001286 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001287 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001289 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001290 old_info_hash = fib_info_hash;
1291 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001292 fib_info_hash_size = new_size;
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001293 fib_info_hash_bits = ilog2(new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 for (i = 0; i < old_size; i++) {
1296 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001297 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct fib_info *fi;
1299
Sasha Levinb67bfe02013-02-27 17:06:00 -08001300 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct hlist_head *dest;
1302 unsigned int new_hash;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 new_hash = fib_info_hashfn(fi);
1305 dest = &new_info_hash[new_hash];
1306 hlist_add_head(&fi->fib_hash, dest);
1307 }
1308 }
1309 fib_info_hash = new_info_hash;
1310
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001311 fib_info_laddrhash = new_laddrhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 for (i = 0; i < old_size; i++) {
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001313 struct hlist_head *lhead = &old_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001314 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct fib_info *fi;
1316
Sasha Levinb67bfe02013-02-27 17:06:00 -08001317 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct hlist_head *ldest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001320 ldest = fib_info_laddrhash_bucket(fi->fib_net,
1321 fi->fib_prefsrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 hlist_add_head(&fi->fib_lhash, ldest);
1323 }
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001326 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001327
1328 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001329 fib_info_hash_free(old_info_hash, bytes);
1330 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
David Aherndcb1ecb2019-06-03 20:19:50 -07001333__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1334 unsigned char scope)
David S. Miller436c3b62011-03-24 17:42:21 -07001335{
David Aherndcb1ecb2019-06-03 20:19:50 -07001336 struct fib_nh *nh;
1337
1338 if (nhc->nhc_family != AF_INET)
1339 return inet_select_addr(nhc->nhc_dev, 0, scope);
1340
1341 nh = container_of(nhc, struct fib_nh, nh_common);
David Ahernc3669482019-05-22 12:04:45 -07001342 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001343 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1344
1345 return nh->nh_saddr;
1346}
1347
David Aherneba618a2019-04-02 14:11:55 -07001348__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1349{
1350 struct fib_nh_common *nhc = res->nhc;
David Aherneba618a2019-04-02 14:11:55 -07001351
1352 if (res->fi->fib_prefsrc)
1353 return res->fi->fib_prefsrc;
1354
David Aherndcb1ecb2019-06-03 20:19:50 -07001355 if (nhc->nhc_family == AF_INET) {
1356 struct fib_nh *nh;
David Aherneba618a2019-04-02 14:11:55 -07001357
David Aherndcb1ecb2019-06-03 20:19:50 -07001358 nh = container_of(nhc, struct fib_nh, nh_common);
1359 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1360 return nh->nh_saddr;
1361 }
1362
1363 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
David Aherneba618a2019-04-02 14:11:55 -07001364}
1365
David Ahern021dd3b2015-08-13 14:59:06 -06001366static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1367{
1368 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1369 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001370 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001371 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001372
1373 if (tb_id == RT_TABLE_MAIN)
1374 tb_id = RT_TABLE_LOCAL;
1375
David Aherne1b8d902015-11-03 15:59:28 -08001376 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1377 fib_prefsrc, tb_id);
1378
1379 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1380 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1381 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001382 }
David Aherne1b8d902015-11-03 15:59:28 -08001383
1384 if (rc != RTN_LOCAL)
1385 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001386 }
1387 return true;
1388}
1389
David Ahern6d8422a12017-05-21 10:12:02 -06001390struct fib_info *fib_create_info(struct fib_config *cfg,
1391 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 int err;
1394 struct fib_info *fi = NULL;
David Ahern4c7e8082019-06-03 20:19:51 -07001395 struct nexthop *nh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001398 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
David S. Miller4c8237c2011-03-07 14:27:38 -08001400 if (cfg->fc_type > RTN_MAX)
1401 goto err_inval;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001404 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1405 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
David Ahernc3ab2b42017-05-21 10:12:03 -06001409 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1410 NL_SET_ERR_MSG(extack,
1411 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001412 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001413 }
Julian Anastasov80610222016-07-10 21:11:55 +03001414
David Ahern493ced12019-06-08 14:53:32 -07001415 if (cfg->fc_nh_id) {
David Ahern6c48ea52019-06-08 14:53:33 -07001416 if (!cfg->fc_mx) {
1417 fi = fib_find_info_nh(net, cfg);
1418 if (fi) {
Yajun Deng79976892021-07-29 15:13:50 +08001419 refcount_inc(&fi->fib_treeref);
David Ahern6c48ea52019-06-08 14:53:33 -07001420 return fi;
1421 }
1422 }
1423
David Ahern493ced12019-06-08 14:53:32 -07001424 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1425 if (!nh) {
1426 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1427 goto err_inval;
1428 }
1429 nhs = 0;
1430 }
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001433 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001434 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (nhs == 0)
1436 goto err_inval;
1437 }
1438#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 err = -ENOBUFS;
Eric Dumazet0a6e6b32022-01-16 01:02:20 -08001441
1442 /* Paired with WRITE_ONCE() in fib_release_info() */
1443 if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
David S. Miller123b9732011-02-01 15:34:21 -08001444 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 struct hlist_head *new_info_hash;
1446 struct hlist_head *new_laddrhash;
1447 unsigned int bytes;
1448
1449 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001450 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001452 new_info_hash = fib_info_hash_alloc(bytes);
1453 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001455 fib_info_hash_free(new_info_hash, bytes);
1456 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001457 } else
David S. Miller123b9732011-02-01 15:34:21 -08001458 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
David S. Miller123b9732011-02-01 15:34:21 -08001460 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto failure;
1462 }
1463
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001464 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001465 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001467 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001468 cfg->fc_mx_len, extack);
Enrico Weigelt88e235b2019-06-05 23:09:05 +02001469 if (IS_ERR(fi->fib_metrics)) {
David Ahern767a2212018-10-04 20:07:51 -07001470 err = PTR_ERR(fi->fib_metrics);
1471 kfree(fi);
1472 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001473 }
David Ahern767a2212018-10-04 20:07:51 -07001474
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001475 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001476 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001477 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001478 fi->fib_flags = cfg->fc_flags;
1479 fi->fib_priority = cfg->fc_priority;
1480 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001481 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001482 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 fi->fib_nhs = nhs;
David Ahern4c7e8082019-06-03 20:19:51 -07001485 if (nh) {
1486 if (!nexthop_get(nh)) {
1487 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1488 err = -EINVAL;
1489 } else {
1490 err = 0;
1491 fi->nh = nh;
1492 }
1493 } else {
1494 change_nexthops(fi) {
1495 nexthop_nh->nh_parent = fi;
1496 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
David Ahern4c7e8082019-06-03 20:19:51 -07001498 if (cfg->fc_mp)
1499 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1500 extack);
1501 else
1502 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1503 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001504
David Aherne4516ef2019-03-27 20:53:48 -07001505 if (err != 0)
1506 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Thomas Graf4e902c52006-08-17 18:14:52 -07001508 if (fib_props[cfg->fc_type].error) {
David Ahernf35b7942019-04-05 16:30:28 -07001509 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001510 NL_SET_ERR_MSG(extack,
1511 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001515 } else {
1516 switch (cfg->fc_type) {
1517 case RTN_UNICAST:
1518 case RTN_LOCAL:
1519 case RTN_BROADCAST:
1520 case RTN_ANYCAST:
1521 case RTN_MULTICAST:
1522 break;
1523 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001524 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001525 goto err_inval;
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
David Ahernc3ab2b42017-05-21 10:12:03 -06001529 if (cfg->fc_scope > RT_SCOPE_HOST) {
1530 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
David Ahern4c7e8082019-06-03 20:19:51 -07001534 if (fi->nh) {
1535 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1536 if (err)
1537 goto failure;
1538 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 struct fib_nh *nh = fi->fib_nh;
1540
1541 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001542 if (nhs != 1) {
1543 NL_SET_ERR_MSG(extack,
1544 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001545 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001546 }
David Ahernbdf00462019-04-05 16:30:26 -07001547 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001548 NL_SET_ERR_MSG(extack,
1549 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001551 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001552 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
David Ahern5481d732019-06-03 20:19:49 -07001553 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001555 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto failure;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001557 netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1558 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001560 int linkdown = 0;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 change_nexthops(fi) {
David Ahernac1fab22019-05-22 12:04:43 -07001563 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1564 cfg->fc_table, cfg->fc_scope,
1565 extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001566 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001568 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001569 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001571 if (linkdown == fi->fib_nhs)
1572 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
David Ahernc3ab2b42017-05-21 10:12:03 -06001575 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1576 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001577 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
David Ahern4c7e8082019-06-03 20:19:51 -07001580 if (!fi->nh) {
1581 change_nexthops(fi) {
1582 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1583 fi->fib_scope);
1584 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1585 fi->fib_nh_is_v6 = true;
1586 } endfor_nexthops(fi)
David S. Miller1fc050a2011-03-07 20:54:48 -08001587
David Ahern4c7e8082019-06-03 20:19:51 -07001588 fib_rebalance(fi);
1589 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001592 ofi = fib_find_info(fi);
1593 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 fi->fib_dead = 1;
1595 free_fib_info(fi);
Yajun Deng79976892021-07-29 15:13:50 +08001596 refcount_inc(&ofi->fib_treeref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return ofi;
1598 }
1599
David Ahern28814cd2021-08-02 10:02:21 -06001600 refcount_set(&fi->fib_treeref, 1);
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001601 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001602 spin_lock_bh(&fib_info_lock);
Eric Dumazet0a6e6b32022-01-16 01:02:20 -08001603 fib_info_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 hlist_add_head(&fi->fib_hash,
1605 &fib_info_hash[fib_info_hashfn(fi)]);
1606 if (fi->fib_prefsrc) {
1607 struct hlist_head *head;
1608
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001609 head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 hlist_add_head(&fi->fib_lhash, head);
1611 }
David Ahern4c7e8082019-06-03 20:19:51 -07001612 if (fi->nh) {
1613 list_add(&fi->nh_list, &nh->fi_list);
1614 } else {
1615 change_nexthops(fi) {
1616 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
David Ahern4c7e8082019-06-03 20:19:51 -07001618 if (!nexthop_nh->fib_nh_dev)
1619 continue;
Eric Dumazetd07418a2022-01-19 02:04:12 -08001620 head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
David Ahern4c7e8082019-06-03 20:19:51 -07001621 hlist_add_head(&nexthop_nh->nh_hash, head);
1622 } endfor_nexthops(fi)
1623 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001624 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return fi;
1626
1627err_inval:
1628 err = -EINVAL;
1629
1630failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001631 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 fi->fib_dead = 1;
1633 free_fib_info(fi);
1634 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001635
1636 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
David Ahernc0a72072019-04-02 14:11:58 -07001639int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001640 u8 rt_family, unsigned char *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001641{
David Ahernc2364192019-04-02 14:11:57 -07001642 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001643 *flags |= RTNH_F_DEAD;
1644
David Ahernc2364192019-04-02 14:11:57 -07001645 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001646 *flags |= RTNH_F_LINKDOWN;
1647
1648 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001649 switch (nhc->nhc_family) {
1650 case AF_INET:
1651 if (ip_ignore_linkdown(nhc->nhc_dev))
1652 *flags |= RTNH_F_DEAD;
1653 break;
David Ahernc0a72072019-04-02 14:11:58 -07001654 case AF_INET6:
1655 if (ip6_ignore_linkdown(nhc->nhc_dev))
1656 *flags |= RTNH_F_DEAD;
1657 break;
David Ahernc2364192019-04-02 14:11:57 -07001658 }
David Ahernb0f60192019-04-02 14:11:56 -07001659 rcu_read_unlock();
1660 }
1661
David Ahernbdf00462019-04-05 16:30:26 -07001662 switch (nhc->nhc_gw_family) {
1663 case AF_INET:
1664 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1665 goto nla_put_failure;
1666 break;
1667 case AF_INET6:
David Ahernd1566262019-04-05 16:30:40 -07001668 /* if gateway family does not match nexthop family
1669 * gateway is encoded as RTA_VIA
1670 */
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001671 if (rt_family != nhc->nhc_gw_family) {
David Ahernd1566262019-04-05 16:30:40 -07001672 int alen = sizeof(struct in6_addr);
1673 struct nlattr *nla;
1674 struct rtvia *via;
1675
1676 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1677 if (!nla)
1678 goto nla_put_failure;
1679
1680 via = nla_data(nla);
1681 via->rtvia_family = AF_INET6;
1682 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1683 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1684 &nhc->nhc_gw.ipv6) < 0) {
David Ahernbdf00462019-04-05 16:30:26 -07001685 goto nla_put_failure;
David Ahernd1566262019-04-05 16:30:40 -07001686 }
David Ahernbdf00462019-04-05 16:30:26 -07001687 break;
David Ahernc2364192019-04-02 14:11:57 -07001688 }
David Ahernb0f60192019-04-02 14:11:56 -07001689
Ido Schimmelca787e02020-11-10 12:25:53 +02001690 *flags |= (nhc->nhc_flags &
1691 (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
David Ahernb0f60192019-04-02 14:11:56 -07001692
David Ahernc2364192019-04-02 14:11:57 -07001693 if (!skip_oif && nhc->nhc_dev &&
1694 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001695 goto nla_put_failure;
1696
David Ahernc2364192019-04-02 14:11:57 -07001697 if (nhc->nhc_lwtstate &&
David Ahernffa8ce52019-04-23 08:23:41 -07001698 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1699 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001700 goto nla_put_failure;
1701
1702 return 0;
1703
1704nla_put_failure:
1705 return -EMSGSIZE;
1706}
David Ahernc0a72072019-04-02 14:11:58 -07001707EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001708
David Ahernc0a72072019-04-02 14:11:58 -07001709#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1710int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
Xiao Liang597aa162021-09-23 23:03:19 +08001711 int nh_weight, u8 rt_family, u32 nh_tclassid)
David Ahernb0f60192019-04-02 14:11:56 -07001712{
David Ahernc2364192019-04-02 14:11:57 -07001713 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001714 struct rtnexthop *rtnh;
David Ahernecc56632019-04-23 08:48:09 -07001715 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001716
1717 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1718 if (!rtnh)
1719 goto nla_put_failure;
1720
David Ahernc2364192019-04-02 14:11:57 -07001721 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001722 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1723
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001724 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001725 goto nla_put_failure;
1726
1727 rtnh->rtnh_flags = flags;
1728
Xiao Liang597aa162021-09-23 23:03:19 +08001729 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1730 goto nla_put_failure;
1731
David Ahernb0f60192019-04-02 14:11:56 -07001732 /* length of rtnetlink header + attributes */
1733 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1734
1735 return 0;
1736
1737nla_put_failure:
1738 return -EMSGSIZE;
1739}
David Ahernc0a72072019-04-02 14:11:58 -07001740EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001741#endif
David Ahernb0f60192019-04-02 14:11:56 -07001742
David Ahernc2364192019-04-02 14:11:57 -07001743#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001744static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1745{
1746 struct nlattr *mp;
1747
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001748 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernb0f60192019-04-02 14:11:56 -07001749 if (!mp)
1750 goto nla_put_failure;
1751
David Ahern4c7e8082019-06-03 20:19:51 -07001752 if (unlikely(fi->nh)) {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001753 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
David Ahern4c7e8082019-06-03 20:19:51 -07001754 goto nla_put_failure;
1755 goto mp_end;
1756 }
1757
David Ahernb0f60192019-04-02 14:11:56 -07001758 for_nexthops(fi) {
Xiao Liang597aa162021-09-23 23:03:19 +08001759 u32 nh_tclassid = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001760#ifdef CONFIG_IP_ROUTE_CLASSID
Xiao Liang597aa162021-09-23 23:03:19 +08001761 nh_tclassid = nh->nh_tclassid;
David Ahernb0f60192019-04-02 14:11:56 -07001762#endif
Xiao Liang597aa162021-09-23 23:03:19 +08001763 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1764 AF_INET, nh_tclassid) < 0)
1765 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001766 } endfor_nexthops(fi);
1767
David Ahern4c7e8082019-06-03 20:19:51 -07001768mp_end:
David Ahernb0f60192019-04-02 14:11:56 -07001769 nla_nest_end(skb, mp);
1770
1771 return 0;
1772
1773nla_put_failure:
1774 return -EMSGSIZE;
1775}
1776#else
1777static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1778{
1779 return 0;
1780}
1781#endif
1782
Eric W. Biederman15e47302012-09-07 20:12:54 +00001783int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
Amit Cohen08554782021-02-01 21:47:50 +02001784 const struct fib_rt_info *fri, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001786 unsigned int nhs = fib_info_num_path(fri->fi);
1787 struct fib_info *fi = fri->fi;
1788 u32 tb_id = fri->tb_id;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001789 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Eric W. Biederman15e47302012-09-07 20:12:54 +00001792 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001793 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001794 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001795
1796 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rtm->rtm_family = AF_INET;
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001798 rtm->rtm_dst_len = fri->dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 rtm->rtm_src_len = 0;
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001800 rtm->rtm_tos = fri->tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001801 if (tb_id < 256)
1802 rtm->rtm_table = tb_id;
1803 else
1804 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001805 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1806 goto nla_put_failure;
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001807 rtm->rtm_type = fri->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001809 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001811
David S. Millerf3756b72012-04-01 20:39:02 -04001812 if (rtm->rtm_dst_len &&
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001813 nla_put_in_addr(skb, RTA_DST, fri->dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001814 goto nla_put_failure;
1815 if (fi->fib_priority &&
1816 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1817 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001818 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001819 goto nla_put_failure;
1820
David S. Millerf3756b72012-04-01 20:39:02 -04001821 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001822 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001823 goto nla_put_failure;
David Ahern4c7e8082019-06-03 20:19:51 -07001824
1825 if (fi->nh) {
1826 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
1827 goto nla_put_failure;
1828 if (nexthop_is_blackhole(fi->nh))
1829 rtm->rtm_type = RTN_BLACKHOLE;
Roopa Prabhu4f801162020-04-27 13:56:46 -07001830 if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode)
1831 goto offload;
David Ahern4c7e8082019-06-03 20:19:51 -07001832 }
1833
David Ahern5481d732019-06-03 20:19:49 -07001834 if (nhs == 1) {
David Aherndcb1ecb2019-06-03 20:19:50 -07001835 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
David Ahernecc56632019-04-23 08:48:09 -07001836 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001837
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001838 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001839 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001840
1841 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001842#ifdef CONFIG_IP_ROUTE_CLASSID
David Aherndcb1ecb2019-06-03 20:19:50 -07001843 if (nhc->nhc_family == AF_INET) {
1844 struct fib_nh *nh;
1845
1846 nh = container_of(nhc, struct fib_nh, nh_common);
1847 if (nh->nh_tclassid &&
1848 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1849 goto nla_put_failure;
1850 }
Patrick McHardy8265abc2006-07-21 15:09:55 -07001851#endif
David Ahernb0f60192019-04-02 14:11:56 -07001852 } else {
1853 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001854 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001856
Roopa Prabhu4f801162020-04-27 13:56:46 -07001857offload:
Ido Schimmel90b93f12020-01-14 13:23:11 +02001858 if (fri->offload)
1859 rtm->rtm_flags |= RTM_F_OFFLOAD;
1860 if (fri->trap)
1861 rtm->rtm_flags |= RTM_F_TRAP;
Amit Cohen36c51002021-02-07 10:22:50 +02001862 if (fri->offload_failed)
1863 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001864
Johannes Berg053c0952015-01-16 22:09:00 +01001865 nlmsg_end(skb, nlh);
1866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Thomas Grafbe403ea2006-08-17 18:15:17 -07001868nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001869 nlmsg_cancel(skb, nlh);
1870 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001874 * Update FIB if:
1875 * - local address disappeared -> we must delete all the entries
1876 * referring to it.
1877 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001879int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001880{
David Aherne0a31262019-11-07 18:29:52 +00001881 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001882 struct net *net = dev_net(dev);
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001883 struct hlist_head *head;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001884 struct fib_info *fi;
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001885 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Ian Morris51456b22015-04-03 09:17:26 +01001887 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001888 return 0;
1889
Eric Dumazet79eb15d2022-01-19 02:04:13 -08001890 head = fib_info_laddrhash_bucket(net, local);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001891 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001892 if (!net_eq(fi->fib_net, net) ||
1893 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001894 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001895 if (fi->fib_prefsrc == local) {
1896 fi->fib_flags |= RTNH_F_DEAD;
1897 ret++;
1898 }
1899 }
1900 return ret;
1901}
1902
David Ahernb75ed8b2019-03-27 20:53:55 -07001903static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001904 enum fib_event_type event_type)
1905{
David Ahernb75ed8b2019-03-27 20:53:55 -07001906 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001907 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001908 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001909 };
1910
1911 switch (event_type) {
1912 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001913 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001914 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001915 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001916 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001917 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001918 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001919 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001920 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1921 (nh->fib_nh_flags & RTNH_F_DEAD))
1922 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001923 event_type, &info.info);
Gustavo A. R. Silva79121182020-11-20 12:25:57 -06001924 break;
Ido Schimmel982acb92017-02-08 11:16:39 +01001925 default:
1926 break;
1927 }
1928
1929 return NOTIFY_DONE;
1930}
1931
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001932/* Update the PMTU of exceptions when:
1933 * - the new MTU of the first hop becomes smaller than the PMTU
1934 * - the old MTU was the same as the PMTU, and it limited discovery of
1935 * larger MTUs on the path. With that limit raised, we can now
1936 * discover larger MTUs
1937 * A special case is locked exceptions, for which the PMTU is smaller
1938 * than the minimal accepted PMTU:
1939 * - if the new MTU is greater than the PMTU, don't make any change
1940 * - otherwise, unlock and set PMTU
1941 */
David Ahern06c77c32019-05-22 12:04:46 -07001942void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001943{
1944 struct fnhe_hash_bucket *bucket;
1945 int i;
1946
David Aherna5995e72019-04-30 07:45:50 -07001947 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001948 if (!bucket)
1949 return;
1950
1951 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1952 struct fib_nh_exception *fnhe;
1953
1954 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1955 fnhe;
1956 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1957 if (fnhe->fnhe_mtu_locked) {
1958 if (new <= fnhe->fnhe_pmtu) {
1959 fnhe->fnhe_pmtu = new;
1960 fnhe->fnhe_mtu_locked = false;
1961 }
1962 } else if (new < fnhe->fnhe_pmtu ||
1963 orig == fnhe->fnhe_pmtu) {
1964 fnhe->fnhe_pmtu = new;
1965 }
1966 }
1967 }
1968}
1969
1970void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1971{
Eric Dumazetd07418a2022-01-19 02:04:12 -08001972 struct hlist_head *head = fib_info_devhash_bucket(dev);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001973 struct fib_nh *nh;
1974
1975 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001976 if (nh->fib_nh_dev == dev)
David Ahern06c77c32019-05-22 12:04:46 -07001977 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001978 }
1979}
1980
Julian Anastasov4f823de2015-10-30 10:23:33 +02001981/* Event force Flags Description
1982 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1983 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1984 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1985 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
David Ahern4c7e8082019-06-03 20:19:51 -07001986 *
1987 * only used when fib_nh is built into fib_info
Julian Anastasov4f823de2015-10-30 10:23:33 +02001988 */
1989int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Eric Dumazetd07418a2022-01-19 02:04:12 -08001991 struct hlist_head *head = fib_info_devhash_bucket(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001992 struct fib_info *prev_fi = NULL;
Eric Dumazetd07418a2022-01-19 02:04:12 -08001993 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001994 struct fib_nh *nh;
Eric Dumazetd07418a2022-01-19 02:04:12 -08001995 int ret = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001996
Julian Anastasov4f823de2015-10-30 10:23:33 +02001997 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 scope = -1;
1999
Sasha Levinb67bfe02013-02-27 17:06:00 -08002000 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002001 struct fib_info *fi = nh->nh_parent;
2002 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002004 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07002005 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002006 continue;
2007 prev_fi = fi;
2008 dead = 0;
2009 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07002010 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002011 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07002012 else if (nexthop_nh->fib_nh_dev == dev &&
2013 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002014 switch (event) {
2015 case NETDEV_DOWN:
2016 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07002017 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Joe Perchesa8eceea2020-03-12 15:50:22 -07002018 fallthrough;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002019 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07002020 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002021 break;
2022 }
Ido Schimmel982acb92017-02-08 11:16:39 +01002023 call_fib_nh_notifiers(nexthop_nh,
2024 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002025 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002027#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002028 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07002029 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002030 dead = fi->fib_nhs;
2031 break;
2032 }
2033#endif
2034 } endfor_nexthops(fi)
2035 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002036 switch (event) {
2037 case NETDEV_DOWN:
2038 case NETDEV_UNREGISTER:
2039 fi->fib_flags |= RTNH_F_DEAD;
Joe Perchesa8eceea2020-03-12 15:50:22 -07002040 fallthrough;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002041 case NETDEV_CHANGE:
2042 fi->fib_flags |= RTNH_F_LINKDOWN;
2043 break;
2044 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002045 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02002047
2048 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
2051 return ret;
2052}
2053
David S. Miller0c838ff2011-01-31 16:16:50 -08002054/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08002055static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08002056{
2057 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08002058 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08002059 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03002060 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08002061 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002062 struct fib_alias *fa, *fa1 = NULL;
2063 u32 last_prio = res->fi->fib_priority;
2064 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08002065
Alexander Duyck56315f92015-02-25 15:31:31 -08002066 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002067 struct fib_info *next_fi = fa->fa_info;
David Ahern7c74b0b2020-04-22 15:40:20 -06002068 struct fib_nh_common *nhc;
David S. Miller0c838ff2011-01-31 16:16:50 -08002069
Julian Anastasov18a912e92015-07-22 10:43:22 +03002070 if (fa->fa_slen != slen)
2071 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002072 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2073 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03002074 if (fa->tb_id != tb->tb_id)
2075 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002076 if (next_fi->fib_priority > last_prio &&
2077 fa->fa_tos == last_tos) {
2078 if (last_tos)
2079 continue;
2080 break;
2081 }
2082 if (next_fi->fib_flags & RTNH_F_DEAD)
2083 continue;
2084 last_tos = fa->fa_tos;
2085 last_prio = next_fi->fib_priority;
2086
David S. Miller37e826c2011-03-24 18:06:47 -07002087 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08002088 fa->fa_type != RTN_UNICAST)
2089 continue;
David Ahern5481d732019-06-03 20:19:49 -07002090
David Ahern7c74b0b2020-04-22 15:40:20 -06002091 nhc = fib_info_nhc(next_fi, 0);
2092 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08002093 continue;
2094
2095 fib_alias_accessed(fa);
2096
Ian Morris51456b22015-04-03 09:17:26 +01002097 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002098 if (next_fi != res->fi)
2099 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002100 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08002101 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03002102 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002103 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03002104 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08002105 goto out;
2106 }
2107 fi = next_fi;
2108 order++;
2109 }
2110
Ian Morris51456b22015-04-03 09:17:26 +01002111 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03002112 if (fa1)
2113 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08002114 goto out;
2115 }
2116
2117 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03002118 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002119 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03002120 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08002121 goto out;
2122 }
2123
2124 if (last_idx >= 0)
2125 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03002126 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08002127out:
Eric Dumazet31d40932011-02-14 11:23:04 -08002128 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08002129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00002132 * Dead device goes up. We wake up dead nexthops.
2133 * It takes sense only on multipath routes.
David Ahern4c7e8082019-06-03 20:19:51 -07002134 *
2135 * only used when fib_nh is built into fib_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 */
David Ahernecc56632019-04-23 08:48:09 -07002137int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct fib_info *prev_fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct fib_nh *nh;
2142 int ret;
2143
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00002144 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 0;
2146
Julian Anastasovc9b32922015-10-30 10:23:34 +02002147 if (nh_flags & RTNH_F_DEAD) {
2148 unsigned int flags = dev_get_flags(dev);
2149
2150 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2151 nh_flags |= RTNH_F_LINKDOWN;
2152 }
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 prev_fi = NULL;
Eric Dumazetd07418a2022-01-19 02:04:12 -08002155 head = fib_info_devhash_bucket(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 ret = 0;
2157
Sasha Levinb67bfe02013-02-27 17:06:00 -08002158 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct fib_info *fi = nh->nh_parent;
2160 int alive;
2161
2162 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07002163 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 continue;
2165
2166 prev_fi = fi;
2167 alive = 0;
2168 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07002169 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 alive++;
2171 continue;
2172 }
David Ahernb75ed8b2019-03-27 20:53:55 -07002173 if (!nexthop_nh->fib_nh_dev ||
2174 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07002176 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08002177 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 continue;
2179 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07002180 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01002181 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } endfor_nexthops(fi)
2183
2184 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002185 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 ret++;
2187 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02002188
2189 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191
2192 return ret;
2193}
2194
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002195#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07002196static bool fib_good_nh(const struct fib_nh *nh)
2197{
2198 int state = NUD_REACHABLE;
2199
David Ahernb75ed8b2019-03-27 20:53:55 -07002200 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07002201 struct neighbour *n;
2202
2203 rcu_read_lock_bh();
2204
David Ahern1a38c432019-04-05 16:30:38 -07002205 if (likely(nh->fib_nh_gw_family == AF_INET))
2206 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2207 (__force u32)nh->fib_nh_gw4);
2208 else if (nh->fib_nh_gw_family == AF_INET6)
2209 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2210 &nh->fib_nh_gw6);
2211 else
2212 n = NULL;
David Aherna6db4492016-04-07 07:21:00 -07002213 if (n)
2214 state = n->nud_state;
2215
2216 rcu_read_unlock_bh();
2217 }
2218
2219 return !!(state & NUD_VALID);
2220}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002221
Peter Nørlund0e884c72015-09-30 10:12:21 +02002222void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07002225 struct net *net = fi->fib_net;
2226 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
David Ahern4c7e8082019-06-03 20:19:51 -07002228 if (unlikely(res->fi->nh)) {
2229 nexthop_path_fib_result(res, hash);
2230 return;
2231 }
2232
David Aherneba618a2019-04-02 14:11:55 -07002233 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08002234 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07002235 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08002236 continue;
2237 if (!first) {
2238 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002239 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002240 first = true;
2241 }
2242 }
2243
David Aherneba618a2019-04-02 14:11:55 -07002244 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02002245 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Xin Long6174a302018-04-01 22:40:35 +08002247 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002248 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252#endif
David Ahern3ce58d82015-10-05 08:51:25 -07002253
2254void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002255 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07002256{
David Ahern0d876f22018-02-13 08:11:34 -08002257 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2258 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08002259
David Ahern3ce58d82015-10-05 08:51:25 -07002260#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern5481d732019-06-03 20:19:49 -07002261 if (fib_info_num_path(res->fi) > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08002262 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01002263
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002264 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07002265 }
2266 else
2267#endif
2268 if (!res->prefixlen &&
2269 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08002270 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07002271 fib_select_default(fl4, res);
2272
David Ahern0d876f22018-02-13 08:11:34 -08002273check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07002274 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07002275 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07002276}