blob: e11f78c6373fea7a9ca4b28812f8950c51e5f938 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IPv4 Forwarding Information Base: semantics.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/bitops.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/jiffies.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/socket.h>
24#include <linux/sockios.h>
25#include <linux/errno.h>
26#include <linux/in.h>
27#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/proc_fs.h>
32#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
David Ahernc3ab2b42017-05-21 10:12:03 -060035#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/ip.h>
39#include <net/protocol.h>
40#include <net/route.h>
41#include <net/tcp.h>
42#include <net/sock.h>
43#include <net/ip_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070044#include <net/netlink.h>
Thomas Graf4e902c52006-08-17 18:14:52 -070045#include <net/nexthop.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020046#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020047#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070048#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include "fib_lookup.h"
51
Stephen Hemminger832b4c52006-08-29 16:48:09 -070052static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct hlist_head *fib_info_hash;
54static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080055static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static unsigned int fib_info_cnt;
57
58#define DEVINDEX_HASHBITS 8
59#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
60static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
61
62#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000064#define for_nexthops(fi) { \
65 int nhsel; const struct fib_nh *nh; \
66 for (nhsel = 0, nh = (fi)->fib_nh; \
67 nhsel < (fi)->fib_nhs; \
68 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000070#define change_nexthops(fi) { \
71 int nhsel; struct fib_nh *nexthop_nh; \
72 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
73 nhsel < (fi)->fib_nhs; \
74 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#else /* CONFIG_IP_ROUTE_MULTIPATH */
77
78/* Hope, that gcc will optimize it to get rid of dummy loop */
79
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000080#define for_nexthops(fi) { \
81 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
82 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000084#define change_nexthops(fi) { \
85 int nhsel; \
86 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
87 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#endif /* CONFIG_IP_ROUTE_MULTIPATH */
90
91#define endfor_nexthops(fi) }
92
93
David S. Miller3be06862011-03-07 15:01:10 -080094const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000095 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .error = 0,
97 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000098 },
99 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .error = 0,
101 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000102 },
103 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .error = 0,
105 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000106 },
107 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .error = 0,
109 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000110 },
111 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .error = 0,
113 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000114 },
115 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .error = 0,
117 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000118 },
119 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .error = -EINVAL,
121 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000122 },
123 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .error = -EHOSTUNREACH,
125 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000126 },
127 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .error = -EACCES,
129 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000130 },
131 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .error = -EAGAIN,
133 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000134 },
135 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .error = -EINVAL,
137 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000138 },
139 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .error = -EINVAL,
141 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000142 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
David S. Millerc5038a82012-07-31 15:02:02 -0700145static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000146{
147 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
148
149 if (!rt)
150 return;
151
152 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
153 * because we waited an RCU grace period before calling
154 * free_fib_info_rcu()
155 */
156
Wei Wang95c47f92017-06-17 10:42:30 -0700157 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700158 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000159}
160
David S. Millerc5038a82012-07-31 15:02:02 -0700161static void free_nh_exceptions(struct fib_nh *nh)
162{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700163 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700164 int i;
165
Eric Dumazetcaa41522014-09-03 22:21:56 -0700166 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
167 if (!hash)
168 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700169 for (i = 0; i < FNHE_HASH_SIZE; i++) {
170 struct fib_nh_exception *fnhe;
171
172 fnhe = rcu_dereference_protected(hash[i].chain, 1);
173 while (fnhe) {
174 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800175
David S. Millerc5038a82012-07-31 15:02:02 -0700176 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
177
Timo Teräs2ffae992013-06-27 10:27:05 +0300178 rt_fibinfo_free(&fnhe->fnhe_rth_input);
179 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700180
181 kfree(fnhe);
182
183 fnhe = next;
184 }
185 }
186 kfree(hash);
187}
188
189static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000190{
191 int cpu;
192
193 if (!rtp)
194 return;
195
196 for_each_possible_cpu(cpu) {
197 struct rtable *rt;
198
199 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700200 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700201 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700202 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700203 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000204 }
205 free_percpu(rtp);
206}
207
David Ahern979e2762019-03-27 20:53:58 -0700208void fib_nh_common_release(struct fib_nh_common *nhc)
209{
210 if (nhc->nhc_dev)
211 dev_put(nhc->nhc_dev);
212
213 lwtstate_put(nhc->nhc_lwtstate);
214}
215EXPORT_SYMBOL_GPL(fib_nh_common_release);
216
David Ahernfaa041a2019-03-27 20:53:49 -0700217void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
218{
219#ifdef CONFIG_IP_ROUTE_CLASSID
220 if (fib_nh->nh_tclassid)
221 net->ipv4.fib_num_tclassid_users--;
222#endif
David Ahern979e2762019-03-27 20:53:58 -0700223 fib_nh_common_release(&fib_nh->nh_common);
David Ahernfaa041a2019-03-27 20:53:49 -0700224 free_nh_exceptions(fib_nh);
225 rt_fibinfo_free_cpus(fib_nh->nh_pcpu_rth_output);
226 rt_fibinfo_free(&fib_nh->nh_rth_input);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000230static void free_fib_info_rcu(struct rcu_head *head)
231{
232 struct fib_info *fi = container_of(head, struct fib_info, rcu);
233
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000234 change_nexthops(fi) {
David Ahernfaa041a2019-03-27 20:53:49 -0700235 fib_nh_release(fi->fib_net, nexthop_nh);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000236 } endfor_nexthops(fi);
237
David Aherncc5f0eb2018-10-04 20:07:52 -0700238 ip_fib_metrics_put(fi->fib_metrics);
239
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000240 kfree(fi);
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243void free_fib_info(struct fib_info *fi)
244{
245 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000246 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 fib_info_cnt--;
David Ahernfaa041a2019-03-27 20:53:49 -0700250
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000251 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100253EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255void fib_release_info(struct fib_info *fi)
256{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700257 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (fi && --fi->fib_treeref == 0) {
259 hlist_del(&fi->fib_hash);
260 if (fi->fib_prefsrc)
261 hlist_del(&fi->fib_lhash);
262 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700263 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800265 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 } endfor_nexthops(fi)
267 fi->fib_dead = 1;
268 fib_info_put(fi);
269 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700270 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000273static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 const struct fib_nh *onh = ofi->fib_nh;
276
277 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700278 if (nh->fib_nh_oif != onh->fib_nh_oif ||
279 nh->fib_nh_gw4 != onh->fib_nh_gw4 ||
280 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700282 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100284#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 nh->nh_tclassid != onh->nh_tclassid ||
286#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700287 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
288 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -1;
290 onh++;
291 } endfor_nexthops(fi);
292 return 0;
293}
294
David S. Miller88ebc722008-01-12 21:49:01 -0800295static inline unsigned int fib_devindex_hashfn(unsigned int val)
296{
297 unsigned int mask = DEVINDEX_HASHSIZE - 1;
298
299 return (val ^
300 (val >> DEVINDEX_HASHBITS) ^
301 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
305{
David S. Miller123b9732011-02-01 15:34:21 -0800306 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned int val = fi->fib_nhs;
308
David S. Miller37e826c2011-03-24 18:06:47 -0700309 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700310 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800312 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700313 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
David S. Miller88ebc722008-01-12 21:49:01 -0800314 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
317}
318
319static struct fib_info *fib_find_info(const struct fib_info *nfi)
320{
321 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct fib_info *fi;
323 unsigned int hash;
324
325 hash = fib_info_hashfn(nfi);
326 head = &fib_info_hash[hash];
327
Sasha Levinb67bfe02013-02-27 17:06:00 -0800328 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800329 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800330 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (fi->fib_nhs != nfi->fib_nhs)
332 continue;
333 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700334 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 nfi->fib_prefsrc == fi->fib_prefsrc &&
336 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000337 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000339 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400340 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
342 return fi;
343 }
344
345 return NULL;
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000349 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Al Virod878e72e2006-09-26 22:18:13 -0700351int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct fib_nh *nh;
355 unsigned int hash;
356
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700357 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 hash = fib_devindex_hashfn(dev->ifindex);
360 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800361 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700362 if (nh->fib_nh_dev == dev &&
363 nh->fib_nh_gw4 == gw &&
364 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700365 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367 }
368 }
369
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700370 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return -1;
373}
374
Thomas Graf339bf982006-11-10 14:10:15 -0800375static inline size_t fib_nlmsg_size(struct fib_info *fi)
376{
377 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
378 + nla_total_size(4) /* RTA_TABLE */
379 + nla_total_size(4) /* RTA_DST */
380 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100381 + nla_total_size(4) /* RTA_PREFSRC */
382 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800383
384 /* space for nested metrics */
385 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
386
387 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200388 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800389 /* Also handles the special case fib_nhs == 1 */
390
391 /* each nexthop is packed in an attribute */
392 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
393
394 /* may contain flow and gateway attribute */
395 nhsize += 2 * nla_total_size(4);
396
Roopa Prabhu571e7222015-07-21 10:43:47 +0200397 /* grab encap info */
398 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700399 if (nh->fib_nh_lws) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200400 /* RTA_ENCAP_TYPE */
401 nh_encapsize += lwtunnel_get_encap_size(
David Ahernb75ed8b2019-03-27 20:53:55 -0700402 nh->fib_nh_lws);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200403 /* RTA_ENCAP */
404 nh_encapsize += nla_total_size(2);
405 }
406 } endfor_nexthops(fi);
407
Thomas Graf339bf982006-11-10 14:10:15 -0800408 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200409 payload += nla_total_size((fi->fib_nhs * nhsize) +
410 nh_encapsize);
411
Thomas Graf339bf982006-11-10 14:10:15 -0800412 }
413
414 return payload;
415}
416
Al Viro81f7bf62006-09-27 18:40:00 -0700417void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700418 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700419 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700422 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700423 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Thomas Graf339bf982006-11-10 14:10:15 -0800425 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100426 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700427 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric W. Biederman15e47302012-09-07 20:12:54 +0000429 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700430 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700431 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800432 if (err < 0) {
433 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
434 WARN_ON(err == -EMSGSIZE);
435 kfree_skb(skb);
436 goto errout;
437 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000438 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800439 info->nlh, GFP_KERNEL);
440 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700441errout:
442 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800443 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800446static int fib_detect_death(struct fib_info *fi, int order,
447 struct fib_info **last_resort, int *last_idx,
448 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct neighbour *n;
451 int state = NUD_NONE;
452
David Ahernb75ed8b2019-03-27 20:53:55 -0700453 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].fib_nh_gw4, fi->fib_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (n) {
455 state = n->nud_state;
456 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300457 } else {
458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800460 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000462 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000464 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300465 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *last_resort = fi;
467 *last_idx = order;
468 }
469 return 1;
470}
471
David Ahern979e2762019-03-27 20:53:58 -0700472int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
473 u16 encap_type, void *cfg, gfp_t gfp_flags,
474 struct netlink_ext_ack *extack)
475{
476 if (encap) {
477 struct lwtunnel_state *lwtstate;
478 int err;
479
480 if (encap_type == LWTUNNEL_ENCAP_NONE) {
481 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
482 return -EINVAL;
483 }
484 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
485 cfg, &lwtstate, extack);
486 if (err)
487 return err;
488
489 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
490 }
491
492 return 0;
493}
494EXPORT_SYMBOL_GPL(fib_nh_common_init);
495
David Aherne4516ef2019-03-27 20:53:48 -0700496int fib_nh_init(struct net *net, struct fib_nh *nh,
497 struct fib_config *cfg, int nh_weight,
498 struct netlink_ext_ack *extack)
499{
500 int err = -ENOMEM;
501
David Ahernf1741732019-03-27 20:53:57 -0700502 nh->fib_nh_family = AF_INET;
503
David Aherne4516ef2019-03-27 20:53:48 -0700504 nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
505 if (!nh->nh_pcpu_rth_output)
506 goto err_out;
507
David Ahern979e2762019-03-27 20:53:58 -0700508 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
509 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
510 if (err)
511 goto init_failure;
David Aherne4516ef2019-03-27 20:53:48 -0700512
David Ahernb75ed8b2019-03-27 20:53:55 -0700513 nh->fib_nh_oif = cfg->fc_oif;
David Ahernf1741732019-03-27 20:53:57 -0700514 if (cfg->fc_gw) {
515 nh->fib_nh_gw4 = cfg->fc_gw;
David Ahernbdf00462019-04-05 16:30:26 -0700516 nh->fib_nh_gw_family = AF_INET;
David Ahernf1741732019-03-27 20:53:57 -0700517 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700518 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700519
520#ifdef CONFIG_IP_ROUTE_CLASSID
521 nh->nh_tclassid = cfg->fc_flow;
522 if (nh->nh_tclassid)
523 net->ipv4.fib_num_tclassid_users++;
524#endif
525#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700526 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700527#endif
528 return 0;
529
David Ahern979e2762019-03-27 20:53:58 -0700530init_failure:
David Aherne4516ef2019-03-27 20:53:48 -0700531 rt_fibinfo_free_cpus(nh->nh_pcpu_rth_output);
532 nh->nh_pcpu_rth_output = NULL;
533err_out:
534 return err;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#ifdef CONFIG_IP_ROUTE_MULTIPATH
538
David Ahern6d8422a12017-05-21 10:12:02 -0600539static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
540 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Thomas Graf4e902c52006-08-17 18:14:52 -0700544 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700546 rtnh = rtnh_next(rtnh, &remaining);
547 }
548
549 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600550 if (remaining > 0) {
551 NL_SET_ERR_MSG(extack,
552 "Invalid nexthop configuration - extra data after nexthops");
553 nhs = 0;
554 }
555
556 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Thomas Graf4e902c52006-08-17 18:14:52 -0700559static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600560 int remaining, struct fib_config *cfg,
561 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
David Aherne4516ef2019-03-27 20:53:48 -0700563 struct net *net = fi->fib_net;
564 struct fib_config fib_cfg;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200565 int ret;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700568 int attrlen;
569
David Aherne4516ef2019-03-27 20:53:48 -0700570 memset(&fib_cfg, 0, sizeof(fib_cfg));
571
David Ahernc3ab2b42017-05-21 10:12:03 -0600572 if (!rtnh_ok(rtnh, remaining)) {
573 NL_SET_ERR_MSG(extack,
574 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600576 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700577
David Ahernc3ab2b42017-05-21 10:12:03 -0600578 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
579 NL_SET_ERR_MSG(extack,
580 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300581 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600582 }
Julian Anastasov80610222016-07-10 21:11:55 +0300583
David Aherne4516ef2019-03-27 20:53:48 -0700584 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
585 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700586
587 attrlen = rtnh_attrlen(rtnh);
588 if (attrlen > 0) {
589 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
590
591 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Aherne4516ef2019-03-27 20:53:48 -0700592 if (nla)
593 fib_cfg.fc_gw = nla_get_in_addr(nla);
594
Thomas Graf4e902c52006-08-17 18:14:52 -0700595 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Aherne4516ef2019-03-27 20:53:48 -0700596 if (nla)
597 fib_cfg.fc_flow = nla_get_u32(nla);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200598
David Aherne4516ef2019-03-27 20:53:48 -0700599 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
600 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
601 if (nla)
602 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700604
David Aherne4516ef2019-03-27 20:53:48 -0700605 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
606 rtnh->rtnh_hops + 1, extack);
607 if (ret)
608 goto errout;
609
Thomas Graf4e902c52006-08-17 18:14:52 -0700610 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700612
Roopa Prabhu571e7222015-07-21 10:43:47 +0200613 ret = -EINVAL;
David Ahernb75ed8b2019-03-27 20:53:55 -0700614 if (cfg->fc_oif && fi->fib_nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700615 NL_SET_ERR_MSG(extack,
616 "Nexthop device index does not match RTA_OIF");
617 goto errout;
618 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700619 if (cfg->fc_gw && fi->fib_nh->fib_nh_gw4 != cfg->fc_gw) {
David Aherne4516ef2019-03-27 20:53:48 -0700620 NL_SET_ERR_MSG(extack,
621 "Nexthop gateway does not match RTA_GATEWAY");
622 goto errout;
623 }
624#ifdef CONFIG_IP_ROUTE_CLASSID
625 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
626 NL_SET_ERR_MSG(extack,
627 "Nexthop class id does not match RTA_FLOW");
628 goto errout;
629 }
630#endif
631 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200632errout:
633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Peter Nørlund0e884c72015-09-30 10:12:21 +0200636static void fib_rebalance(struct fib_info *fi)
637{
638 int total;
639 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200640
641 if (fi->fib_nhs < 2)
642 return;
643
644 total = 0;
645 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700646 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200647 continue;
648
David Ahernb75ed8b2019-03-27 20:53:55 -0700649 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
650 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200651 continue;
652
David Ahernb75ed8b2019-03-27 20:53:55 -0700653 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200654 } endfor_nexthops(fi);
655
656 w = 0;
657 change_nexthops(fi) {
658 int upper_bound;
659
David Ahernb75ed8b2019-03-27 20:53:55 -0700660 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200661 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700662 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
663 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200664 upper_bound = -1;
665 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700666 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200667 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
668 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200669 }
670
David Ahernb75ed8b2019-03-27 20:53:55 -0700671 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200672 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200673}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200674#else /* CONFIG_IP_ROUTE_MULTIPATH */
675
David Ahern8373c6c2019-03-27 20:53:46 -0700676static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
677 int remaining, struct fib_config *cfg,
678 struct netlink_ext_ack *extack)
679{
680 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
681
682 return -EINVAL;
683}
684
Peter Nørlund0e884c72015-09-30 10:12:21 +0200685#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200686
687#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David Ahern30357d72017-01-30 12:07:37 -0800689static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800690 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800691 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600692 const struct fib_config *cfg,
693 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200694{
695 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200696 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200697
698 if (encap_type == LWTUNNEL_ENCAP_NONE)
699 return 0;
700
David Ahern9ae28722017-05-27 16:19:28 -0600701 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
702 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200703 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700704 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200705 lwtstate_free(lwtstate);
706 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200707
Jiri Bencdf383e62015-08-18 18:41:13 +0200708 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200709}
710
David Ahern9ae28722017-05-27 16:19:28 -0600711int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
712 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700715 struct rtnexthop *rtnh;
716 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717#endif
718
Thomas Graf4e902c52006-08-17 18:14:52 -0700719 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 1;
721
Thomas Graf4e902c52006-08-17 18:14:52 -0700722 if (cfg->fc_oif || cfg->fc_gw) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200723 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600724 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
725 fi->fib_nh, cfg, extack))
726 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200727 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100728#ifdef CONFIG_IP_ROUTE_CLASSID
729 if (cfg->fc_flow &&
730 cfg->fc_flow != fi->fib_nh->nh_tclassid)
731 return 1;
732#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700733 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->fib_nh_oif) &&
734 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->fib_nh_gw4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736 return 1;
737 }
738
739#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100740 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700742
743 rtnh = cfg->fc_mp;
744 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700747 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Thomas Graf4e902c52006-08-17 18:14:52 -0700749 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700751
David Ahernb75ed8b2019-03-27 20:53:55 -0700752 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700754
755 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200756 if (attrlen > 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700757 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
758
759 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernb75ed8b2019-03-27 20:53:55 -0700760 if (nla && nla_get_in_addr(nla) != nh->fib_nh_gw4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100762#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700763 nla = nla_find(attrs, attrlen, RTA_FLOW);
764 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 1;
766#endif
767 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700768
769 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 } endfor_nexthops(fi);
771#endif
772 return 0;
773}
774
Xin Long5f9ae3d2017-08-23 10:07:26 +0800775bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
776{
777 struct nlattr *nla;
778 int remaining;
779
780 if (!cfg->fc_mx)
781 return true;
782
783 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
784 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +0100785 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800786
787 if (!type)
788 continue;
789 if (type > RTAX_MAX)
790 return false;
791
792 if (type == RTAX_CC_ALGO) {
793 char tmp[TCP_CA_NAME_MAX];
794 bool ecn_ca = false;
795
796 nla_strlcpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -0800797 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +0800798 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -0700799 if (nla_len(nla) != sizeof(u32))
800 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800801 val = nla_get_u32(nla);
802 }
803
Phil Sutterd03a4552017-12-19 15:17:13 +0100804 fi_val = fi->fib_metrics->metrics[type - 1];
805 if (type == RTAX_FEATURES)
806 fi_val &= ~DST_FEATURE_ECN_CA;
807
808 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +0800809 return false;
810 }
811
812 return true;
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000817 * Picture
818 * -------
819 *
820 * Semantics of nexthop is very messy by historical reasons.
821 * We have to take into account, that:
822 * a) gateway can be actually local interface address,
823 * so that gatewayed route is direct.
824 * b) gateway must be on-link address, possibly
825 * described not by an ifaddr, but also by a direct route.
826 * c) If both gateway and interface are specified, they should not
827 * contradict.
828 * d) If we use tunnel routes, gateway could be not on-link.
829 *
830 * Attempt to reconcile all of these (alas, self-contradictory) conditions
831 * results in pretty ugly and hairy code with obscure logic.
832 *
833 * I chose to generalized it instead, so that the size
834 * of code does not increase practically, but it becomes
835 * much more general.
836 * Every prefix is assigned a "scope" value: "host" is local address,
837 * "link" is direct route,
838 * [ ... "site" ... "interior" ... ]
839 * and "universe" is true gateway route with global meaning.
840 *
841 * Every prefix refers to a set of "nexthop"s (gw, oif),
842 * where gw must have narrower scope. This recursion stops
843 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
844 * which means that gw is forced to be on link.
845 *
846 * Code is still hairy, but now it is apparently logically
847 * consistent and very flexible. F.e. as by-product it allows
848 * to co-exists in peace independent exterior and interior
849 * routing processes.
850 *
851 * Normally it looks as following.
852 *
853 * {universe prefix} -> (gw, oif) [scope link]
854 * |
855 * |-> {link prefix} -> (gw, oif) [scope local]
856 * |
857 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 */
David Ahernfa8fefa2017-09-27 20:41:59 -0700859static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
860 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Tom Herbert127eb7c2015-08-24 09:45:41 -0700862 int err = 0;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800863 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000864 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Denis V. Lunev86167a32008-01-21 17:34:00 -0800866 net = cfg->fc_nlinfo.nl_net;
David Ahernb75ed8b2019-03-27 20:53:55 -0700867 if (nh->fib_nh_gw4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct fib_result res;
869
David Ahernb75ed8b2019-03-27 20:53:55 -0700870 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
David Ahern30bbaa12015-08-13 14:59:05 -0600871 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
David Ahernc3ab2b42017-05-21 10:12:03 -0600873 if (cfg->fc_scope >= RT_SCOPE_LINK) {
874 NL_SET_ERR_MSG(extack,
875 "Nexthop has invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600877 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700878 dev = __dev_get_by_index(net, nh->fib_nh_oif);
David Ahern066b1032018-08-29 16:53:27 -0700879 if (!dev) {
880 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -ENODEV;
David Ahern066b1032018-08-29 16:53:27 -0700882 }
David Ahernc3ab2b42017-05-21 10:12:03 -0600883 if (!(dev->flags & IFF_UP)) {
884 NL_SET_ERR_MSG(extack,
885 "Nexthop device is not up");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600887 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700888 addr_type = inet_addr_type_dev_table(net, dev,
889 nh->fib_nh_gw4);
David Ahernc3ab2b42017-05-21 10:12:03 -0600890 if (addr_type != RTN_UNICAST) {
891 NL_SET_ERR_MSG(extack,
892 "Nexthop has invalid gateway");
David Ahern30bbaa12015-08-13 14:59:05 -0600893 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600894 }
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400895 if (!netif_carrier_ok(dev))
David Ahernb75ed8b2019-03-27 20:53:55 -0700896 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
897 nh->fib_nh_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dev_hold(dev);
David Ahernb75ed8b2019-03-27 20:53:55 -0700899 nh->fib_nh_scope = RT_SCOPE_LINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return 0;
901 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000902 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 {
David Ahern3bfd8472015-08-13 14:59:07 -0600904 struct fib_table *tbl = NULL;
David S. Miller9ade2282011-03-12 02:02:42 -0500905 struct flowi4 fl4 = {
David Ahernb75ed8b2019-03-27 20:53:55 -0700906 .daddr = nh->fib_nh_gw4,
David S. Miller9ade2282011-03-12 02:02:42 -0500907 .flowi4_scope = cfg->fc_scope + 1,
David Ahernb75ed8b2019-03-27 20:53:55 -0700908 .flowi4_oif = nh->fib_nh_oif,
Cong Wang6a662712014-04-15 16:25:34 -0700909 .flowi4_iif = LOOPBACK_IFINDEX,
Thomas Graf4e902c52006-08-17 18:14:52 -0700910 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500913 if (fl4.flowi4_scope < RT_SCOPE_LINK)
914 fl4.flowi4_scope = RT_SCOPE_LINK;
David Ahern3bfd8472015-08-13 14:59:07 -0600915
916 if (cfg->fc_table)
917 tbl = fib_get_table(net, cfg->fc_table);
918
919 if (tbl)
920 err = fib_table_lookup(tbl, &fl4, &res,
Eric Dumazet1e313672015-08-15 10:54:07 -0700921 FIB_LOOKUP_IGNORE_LINKSTATE |
922 FIB_LOOKUP_NOREF);
David Ahern4c9bcd12015-08-19 11:40:31 -0700923
924 /* on error or if no table given do full lookup. This
925 * is needed for example when nexthops are in the local
926 * table rather than the given table
927 */
928 if (!tbl || err) {
David Ahern3bfd8472015-08-13 14:59:07 -0600929 err = fib_lookup(net, &fl4, &res,
930 FIB_LOOKUP_IGNORE_LINKSTATE);
David Ahern4c9bcd12015-08-19 11:40:31 -0700931 }
932
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000933 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600934 NL_SET_ERR_MSG(extack,
935 "Nexthop has invalid gateway");
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000936 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 err = -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600941 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
942 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600944 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700945 nh->fib_nh_scope = res.scope;
946 nh->fib_nh_oif = FIB_RES_OIF(res);
947 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
David Ahernc3ab2b42017-05-21 10:12:03 -0600948 if (!dev) {
949 NL_SET_ERR_MSG(extack,
950 "No egress device for nexthop gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600952 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000953 dev_hold(dev);
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400954 if (!netif_carrier_ok(dev))
David Ahernb75ed8b2019-03-27 20:53:55 -0700955 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000956 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 } else {
958 struct in_device *in_dev;
959
David Ahernb75ed8b2019-03-27 20:53:55 -0700960 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600961 NL_SET_ERR_MSG(extack,
962 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600964 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000965 rcu_read_lock();
966 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -0700967 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
Ian Morris51456b22015-04-03 09:17:26 +0100968 if (!in_dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000969 goto out;
970 err = -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600971 if (!(in_dev->dev->flags & IFF_UP)) {
972 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000973 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600974 }
David Ahernb75ed8b2019-03-27 20:53:55 -0700975 nh->fib_nh_dev = in_dev->dev;
976 dev_hold(nh->fib_nh_dev);
977 nh->fib_nh_scope = RT_SCOPE_HOST;
978 if (!netif_carrier_ok(nh->fib_nh_dev))
979 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000980 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000982out:
983 rcu_read_unlock();
984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Al Viro81f7bf62006-09-27 18:40:00 -0700987static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
David S. Miller123b9732011-02-01 15:34:21 -0800989 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000991 return ((__force u32)val ^
992 ((__force u32)val >> 7) ^
993 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
David S. Miller123b9732011-02-01 15:34:21 -0800996static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800999 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 else
1001 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001002 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1003 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
David S. Miller123b9732011-02-01 15:34:21 -08001006static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 if (!hash)
1009 return;
1010
1011 if (bytes <= PAGE_SIZE)
1012 kfree(hash);
1013 else
1014 free_pages((unsigned long) hash, get_order(bytes));
1015}
1016
David S. Miller123b9732011-02-01 15:34:21 -08001017static void fib_info_hash_move(struct hlist_head *new_info_hash,
1018 struct hlist_head *new_laddrhash,
1019 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
David S. Millerb7656e72005-08-05 04:12:48 -07001021 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001022 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001023 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001025 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001026 old_info_hash = fib_info_hash;
1027 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001028 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 for (i = 0; i < old_size; i++) {
1031 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001032 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct fib_info *fi;
1034
Sasha Levinb67bfe02013-02-27 17:06:00 -08001035 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct hlist_head *dest;
1037 unsigned int new_hash;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 new_hash = fib_info_hashfn(fi);
1040 dest = &new_info_hash[new_hash];
1041 hlist_add_head(&fi->fib_hash, dest);
1042 }
1043 }
1044 fib_info_hash = new_info_hash;
1045
1046 for (i = 0; i < old_size; i++) {
1047 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001048 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct fib_info *fi;
1050
Sasha Levinb67bfe02013-02-27 17:06:00 -08001051 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct hlist_head *ldest;
1053 unsigned int new_hash;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1056 ldest = &new_laddrhash[new_hash];
1057 hlist_add_head(&fi->fib_lhash, ldest);
1058 }
1059 }
1060 fib_info_laddrhash = new_laddrhash;
1061
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001062 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001063
1064 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001065 fib_info_hash_free(old_info_hash, bytes);
1066 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
David S. Miller436c3b62011-03-24 17:42:21 -07001069__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1070{
David Ahernb75ed8b2019-03-27 20:53:55 -07001071 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev,
1072 nh->fib_nh_gw4,
David S. Miller37e826c2011-03-24 18:06:47 -07001073 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001074 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1075
1076 return nh->nh_saddr;
1077}
1078
David Aherneba618a2019-04-02 14:11:55 -07001079__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1080{
1081 struct fib_nh_common *nhc = res->nhc;
1082 struct fib_nh *nh;
1083
1084 if (res->fi->fib_prefsrc)
1085 return res->fi->fib_prefsrc;
1086
1087 nh = container_of(nhc, struct fib_nh, nh_common);
1088 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1089 return nh->nh_saddr;
1090
1091 return fib_info_update_nh_saddr(net, nh);
1092}
1093
David Ahern021dd3b2015-08-13 14:59:06 -06001094static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1095{
1096 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1097 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001098 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001099 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001100
1101 if (tb_id == RT_TABLE_MAIN)
1102 tb_id = RT_TABLE_LOCAL;
1103
David Aherne1b8d902015-11-03 15:59:28 -08001104 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1105 fib_prefsrc, tb_id);
1106
1107 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1108 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1109 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001110 }
David Aherne1b8d902015-11-03 15:59:28 -08001111
1112 if (rc != RTN_LOCAL)
1113 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001114 }
1115 return true;
1116}
1117
David Ahern6d8422a12017-05-21 10:12:02 -06001118struct fib_info *fib_create_info(struct fib_config *cfg,
1119 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 int err;
1122 struct fib_info *fi = NULL;
1123 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001125 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
David S. Miller4c8237c2011-03-07 14:27:38 -08001127 if (cfg->fc_type > RTN_MAX)
1128 goto err_inval;
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001131 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1132 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
David Ahernc3ab2b42017-05-21 10:12:03 -06001136 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1137 NL_SET_ERR_MSG(extack,
1138 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001139 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001140 }
Julian Anastasov80610222016-07-10 21:11:55 +03001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001143 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001144 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (nhs == 0)
1146 goto err_inval;
1147 }
1148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001151 if (fib_info_cnt >= fib_info_hash_size) {
1152 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct hlist_head *new_info_hash;
1154 struct hlist_head *new_laddrhash;
1155 unsigned int bytes;
1156
1157 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001158 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001160 new_info_hash = fib_info_hash_alloc(bytes);
1161 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001163 fib_info_hash_free(new_info_hash, bytes);
1164 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001165 } else
David S. Miller123b9732011-02-01 15:34:21 -08001166 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
David S. Miller123b9732011-02-01 15:34:21 -08001168 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto failure;
1170 }
1171
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001172 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001173 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001175 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001176 cfg->fc_mx_len, extack);
David Ahern767a2212018-10-04 20:07:51 -07001177 if (unlikely(IS_ERR(fi->fib_metrics))) {
1178 err = PTR_ERR(fi->fib_metrics);
1179 kfree(fi);
1180 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001181 }
David Ahern767a2212018-10-04 20:07:51 -07001182
Eric Dumazet187e5b32017-08-15 05:26:17 -07001183 fib_info_cnt++;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001184 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001185 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001186 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001187 fi->fib_flags = cfg->fc_flags;
1188 fi->fib_priority = cfg->fc_priority;
1189 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001190 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001191 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 fi->fib_nhs = nhs;
1194 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001195 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } endfor_nexthops(fi)
1197
David Aherne4516ef2019-03-27 20:53:48 -07001198 if (cfg->fc_mp)
David Ahern6d8422a12017-05-21 10:12:02 -06001199 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
David Aherne4516ef2019-03-27 20:53:48 -07001200 else
1201 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001202
David Aherne4516ef2019-03-27 20:53:48 -07001203 if (err != 0)
1204 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Thomas Graf4e902c52006-08-17 18:14:52 -07001206 if (fib_props[cfg->fc_type].error) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001207 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1208 NL_SET_ERR_MSG(extack,
1209 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001213 } else {
1214 switch (cfg->fc_type) {
1215 case RTN_UNICAST:
1216 case RTN_LOCAL:
1217 case RTN_BROADCAST:
1218 case RTN_ANYCAST:
1219 case RTN_MULTICAST:
1220 break;
1221 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001222 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001223 goto err_inval;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
David Ahernc3ab2b42017-05-21 10:12:03 -06001227 if (cfg->fc_scope > RT_SCOPE_HOST) {
1228 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Thomas Graf4e902c52006-08-17 18:14:52 -07001232 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct fib_nh *nh = fi->fib_nh;
1234
1235 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001236 if (nhs != 1) {
1237 NL_SET_ERR_MSG(extack,
1238 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001239 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001240 }
David Ahernbdf00462019-04-05 16:30:26 -07001241 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001242 NL_SET_ERR_MSG(extack,
1243 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001245 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001246 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1247 nh->fib_nh_dev = dev_get_by_index(net, fi->fib_nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001249 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 goto failure;
1251 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001252 int linkdown = 0;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 change_nexthops(fi) {
David Ahernfa8fefa2017-09-27 20:41:59 -07001255 err = fib_check_nh(cfg, nexthop_nh, extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001256 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001258 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001259 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001261 if (linkdown == fi->fib_nhs)
1262 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
David Ahernc3ab2b42017-05-21 10:12:03 -06001265 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1266 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001267 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
David S. Miller1fc050a2011-03-07 20:54:48 -08001270 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001271 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -08001272 } endfor_nexthops(fi)
1273
Peter Nørlund0e884c72015-09-30 10:12:21 +02001274 fib_rebalance(fi);
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001277 ofi = fib_find_info(fi);
1278 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 fi->fib_dead = 1;
1280 free_fib_info(fi);
1281 ofi->fib_treeref++;
1282 return ofi;
1283 }
1284
1285 fi->fib_treeref++;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001286 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001287 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 hlist_add_head(&fi->fib_hash,
1289 &fib_info_hash[fib_info_hashfn(fi)]);
1290 if (fi->fib_prefsrc) {
1291 struct hlist_head *head;
1292
1293 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1294 hlist_add_head(&fi->fib_lhash, head);
1295 }
1296 change_nexthops(fi) {
1297 struct hlist_head *head;
1298 unsigned int hash;
1299
David Ahernb75ed8b2019-03-27 20:53:55 -07001300 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001302 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001304 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001306 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return fi;
1308
1309err_inval:
1310 err = -EINVAL;
1311
1312failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001313 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 fi->fib_dead = 1;
1315 free_fib_info(fi);
1316 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001317
1318 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
David Ahernc0a72072019-04-02 14:11:58 -07001321int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1322 unsigned int *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001323{
David Ahernc2364192019-04-02 14:11:57 -07001324 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001325 *flags |= RTNH_F_DEAD;
1326
David Ahernc2364192019-04-02 14:11:57 -07001327 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001328 *flags |= RTNH_F_LINKDOWN;
1329
1330 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001331 switch (nhc->nhc_family) {
1332 case AF_INET:
1333 if (ip_ignore_linkdown(nhc->nhc_dev))
1334 *flags |= RTNH_F_DEAD;
1335 break;
David Ahernc0a72072019-04-02 14:11:58 -07001336 case AF_INET6:
1337 if (ip6_ignore_linkdown(nhc->nhc_dev))
1338 *flags |= RTNH_F_DEAD;
1339 break;
David Ahernc2364192019-04-02 14:11:57 -07001340 }
David Ahernb0f60192019-04-02 14:11:56 -07001341 rcu_read_unlock();
1342 }
1343
David Ahernbdf00462019-04-05 16:30:26 -07001344 switch (nhc->nhc_gw_family) {
1345 case AF_INET:
1346 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1347 goto nla_put_failure;
1348 break;
1349 case AF_INET6:
1350 if (nla_put_in6_addr(skb, RTA_GATEWAY, &nhc->nhc_gw.ipv6) < 0)
1351 goto nla_put_failure;
1352 break;
David Ahernc2364192019-04-02 14:11:57 -07001353 }
David Ahernb0f60192019-04-02 14:11:56 -07001354
David Ahernc2364192019-04-02 14:11:57 -07001355 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1356 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
David Ahernb0f60192019-04-02 14:11:56 -07001357 *flags |= RTNH_F_OFFLOAD;
1358
David Ahernc2364192019-04-02 14:11:57 -07001359 if (!skip_oif && nhc->nhc_dev &&
1360 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001361 goto nla_put_failure;
1362
David Ahernc2364192019-04-02 14:11:57 -07001363 if (nhc->nhc_lwtstate &&
1364 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001365 goto nla_put_failure;
1366
1367 return 0;
1368
1369nla_put_failure:
1370 return -EMSGSIZE;
1371}
David Ahernc0a72072019-04-02 14:11:58 -07001372EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001373
David Ahernc0a72072019-04-02 14:11:58 -07001374#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1375int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1376 int nh_weight)
David Ahernb0f60192019-04-02 14:11:56 -07001377{
David Ahernc2364192019-04-02 14:11:57 -07001378 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001379 struct rtnexthop *rtnh;
1380 unsigned int flags = 0;
1381
1382 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1383 if (!rtnh)
1384 goto nla_put_failure;
1385
David Ahernc2364192019-04-02 14:11:57 -07001386 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001387 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1388
David Ahernc2364192019-04-02 14:11:57 -07001389 if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001390 goto nla_put_failure;
1391
1392 rtnh->rtnh_flags = flags;
1393
1394 /* length of rtnetlink header + attributes */
1395 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1396
1397 return 0;
1398
1399nla_put_failure:
1400 return -EMSGSIZE;
1401}
David Ahernc0a72072019-04-02 14:11:58 -07001402EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001403#endif
David Ahernb0f60192019-04-02 14:11:56 -07001404
David Ahernc2364192019-04-02 14:11:57 -07001405#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001406static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1407{
1408 struct nlattr *mp;
1409
1410 mp = nla_nest_start(skb, RTA_MULTIPATH);
1411 if (!mp)
1412 goto nla_put_failure;
1413
1414 for_nexthops(fi) {
David Ahernc2364192019-04-02 14:11:57 -07001415 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001416 goto nla_put_failure;
1417#ifdef CONFIG_IP_ROUTE_CLASSID
1418 if (nh->nh_tclassid &&
1419 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1420 goto nla_put_failure;
1421#endif
1422 } endfor_nexthops(fi);
1423
1424 nla_nest_end(skb, mp);
1425
1426 return 0;
1427
1428nla_put_failure:
1429 return -EMSGSIZE;
1430}
1431#else
1432static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1433{
1434 return 0;
1435}
1436#endif
1437
Eric W. Biederman15e47302012-09-07 20:12:54 +00001438int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001439 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001440 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001442 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Eric W. Biederman15e47302012-09-07 20:12:54 +00001445 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001446 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001447 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001448
1449 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 rtm->rtm_family = AF_INET;
1451 rtm->rtm_dst_len = dst_len;
1452 rtm->rtm_src_len = 0;
1453 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001454 if (tb_id < 256)
1455 rtm->rtm_table = tb_id;
1456 else
1457 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001458 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1459 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 rtm->rtm_type = type;
1461 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001462 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001464
David S. Millerf3756b72012-04-01 20:39:02 -04001465 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001466 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001467 goto nla_put_failure;
1468 if (fi->fib_priority &&
1469 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1470 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001471 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001472 goto nla_put_failure;
1473
David S. Millerf3756b72012-04-01 20:39:02 -04001474 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001475 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001476 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (fi->fib_nhs == 1) {
David Ahernb0f60192019-04-02 14:11:56 -07001478 struct fib_nh *nh = &fi->fib_nh[0];
1479 unsigned int flags = 0;
1480
David Ahernc2364192019-04-02 14:11:57 -07001481 if (fib_nexthop_info(skb, &nh->nh_common, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001482 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001483
1484 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001485#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernb0f60192019-04-02 14:11:56 -07001486 if (nh->nh_tclassid &&
1487 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
David S. Millerf3756b72012-04-01 20:39:02 -04001488 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001489#endif
David Ahernb0f60192019-04-02 14:11:56 -07001490 } else {
1491 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001492 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001494
Johannes Berg053c0952015-01-16 22:09:00 +01001495 nlmsg_end(skb, nlh);
1496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Thomas Grafbe403ea2006-08-17 18:15:17 -07001498nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001499 nlmsg_cancel(skb, nlh);
1500 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001504 * Update FIB if:
1505 * - local address disappeared -> we must delete all the entries
1506 * referring to it.
1507 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001509int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001510{
1511 int ret = 0;
1512 unsigned int hash = fib_laddr_hashfn(local);
1513 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001514 struct net *net = dev_net(dev);
1515 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001516 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Ian Morris51456b22015-04-03 09:17:26 +01001518 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001519 return 0;
1520
Sasha Levinb67bfe02013-02-27 17:06:00 -08001521 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001522 if (!net_eq(fi->fib_net, net) ||
1523 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001524 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001525 if (fi->fib_prefsrc == local) {
1526 fi->fib_flags |= RTNH_F_DEAD;
1527 ret++;
1528 }
1529 }
1530 return ret;
1531}
1532
David Ahernb75ed8b2019-03-27 20:53:55 -07001533static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001534 enum fib_event_type event_type)
1535{
David Ahernb75ed8b2019-03-27 20:53:55 -07001536 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001537 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001538 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001539 };
1540
1541 switch (event_type) {
1542 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001543 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001544 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001545 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001546 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001547 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001548 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001549 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001550 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1551 (nh->fib_nh_flags & RTNH_F_DEAD))
1552 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001553 event_type, &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001554 default:
1555 break;
1556 }
1557
1558 return NOTIFY_DONE;
1559}
1560
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001561/* Update the PMTU of exceptions when:
1562 * - the new MTU of the first hop becomes smaller than the PMTU
1563 * - the old MTU was the same as the PMTU, and it limited discovery of
1564 * larger MTUs on the path. With that limit raised, we can now
1565 * discover larger MTUs
1566 * A special case is locked exceptions, for which the PMTU is smaller
1567 * than the minimal accepted PMTU:
1568 * - if the new MTU is greater than the PMTU, don't make any change
1569 * - otherwise, unlock and set PMTU
1570 */
1571static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1572{
1573 struct fnhe_hash_bucket *bucket;
1574 int i;
1575
1576 bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1577 if (!bucket)
1578 return;
1579
1580 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1581 struct fib_nh_exception *fnhe;
1582
1583 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1584 fnhe;
1585 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1586 if (fnhe->fnhe_mtu_locked) {
1587 if (new <= fnhe->fnhe_pmtu) {
1588 fnhe->fnhe_pmtu = new;
1589 fnhe->fnhe_mtu_locked = false;
1590 }
1591 } else if (new < fnhe->fnhe_pmtu ||
1592 orig == fnhe->fnhe_pmtu) {
1593 fnhe->fnhe_pmtu = new;
1594 }
1595 }
1596 }
1597}
1598
1599void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1600{
1601 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1602 struct hlist_head *head = &fib_info_devhash[hash];
1603 struct fib_nh *nh;
1604
1605 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001606 if (nh->fib_nh_dev == dev)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001607 nh_update_mtu(nh, dev->mtu, orig_mtu);
1608 }
1609}
1610
Julian Anastasov4f823de2015-10-30 10:23:33 +02001611/* Event force Flags Description
1612 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1613 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1614 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1615 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1616 */
1617int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 int ret = 0;
1620 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001621 struct fib_info *prev_fi = NULL;
1622 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1623 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001624 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001625
Julian Anastasov4f823de2015-10-30 10:23:33 +02001626 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 scope = -1;
1628
Sasha Levinb67bfe02013-02-27 17:06:00 -08001629 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001630 struct fib_info *fi = nh->nh_parent;
1631 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001633 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001634 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001635 continue;
1636 prev_fi = fi;
1637 dead = 0;
1638 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001639 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001640 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001641 else if (nexthop_nh->fib_nh_dev == dev &&
1642 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001643 switch (event) {
1644 case NETDEV_DOWN:
1645 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07001646 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001647 /* fall through */
1648 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07001649 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001650 break;
1651 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001652 call_fib_nh_notifiers(nexthop_nh,
1653 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001654 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001656#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001657 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07001658 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001659 dead = fi->fib_nhs;
1660 break;
1661 }
1662#endif
1663 } endfor_nexthops(fi)
1664 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001665 switch (event) {
1666 case NETDEV_DOWN:
1667 case NETDEV_UNREGISTER:
1668 fi->fib_flags |= RTNH_F_DEAD;
1669 /* fall through */
1670 case NETDEV_CHANGE:
1671 fi->fib_flags |= RTNH_F_LINKDOWN;
1672 break;
1673 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001674 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001676
1677 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
1680 return ret;
1681}
1682
David S. Miller0c838ff2011-01-31 16:16:50 -08001683/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001684static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001685{
1686 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001687 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001688 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001689 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001690 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001691 struct fib_alias *fa, *fa1 = NULL;
1692 u32 last_prio = res->fi->fib_priority;
1693 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001694
Alexander Duyck56315f92015-02-25 15:31:31 -08001695 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001696 struct fib_info *next_fi = fa->fa_info;
1697
Julian Anastasov18a912e92015-07-22 10:43:22 +03001698 if (fa->fa_slen != slen)
1699 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001700 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1701 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001702 if (fa->tb_id != tb->tb_id)
1703 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001704 if (next_fi->fib_priority > last_prio &&
1705 fa->fa_tos == last_tos) {
1706 if (last_tos)
1707 continue;
1708 break;
1709 }
1710 if (next_fi->fib_flags & RTNH_F_DEAD)
1711 continue;
1712 last_tos = fa->fa_tos;
1713 last_prio = next_fi->fib_priority;
1714
David S. Miller37e826c2011-03-24 18:06:47 -07001715 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001716 fa->fa_type != RTN_UNICAST)
1717 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001718 if (!next_fi->fib_nh[0].fib_nh_gw4 ||
1719 next_fi->fib_nh[0].fib_nh_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08001720 continue;
1721
1722 fib_alias_accessed(fa);
1723
Ian Morris51456b22015-04-03 09:17:26 +01001724 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001725 if (next_fi != res->fi)
1726 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001727 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001728 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001729 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001730 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001731 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001732 goto out;
1733 }
1734 fi = next_fi;
1735 order++;
1736 }
1737
Ian Morris51456b22015-04-03 09:17:26 +01001738 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001739 if (fa1)
1740 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001741 goto out;
1742 }
1743
1744 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001745 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001746 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001747 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001748 goto out;
1749 }
1750
1751 if (last_idx >= 0)
1752 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001753 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001754out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001755 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001759 * Dead device goes up. We wake up dead nexthops.
1760 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001762int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 struct fib_info *prev_fi;
1765 unsigned int hash;
1766 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct fib_nh *nh;
1768 int ret;
1769
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001770 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
1772
Julian Anastasovc9b32922015-10-30 10:23:34 +02001773 if (nh_flags & RTNH_F_DEAD) {
1774 unsigned int flags = dev_get_flags(dev);
1775
1776 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1777 nh_flags |= RTNH_F_LINKDOWN;
1778 }
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 prev_fi = NULL;
1781 hash = fib_devindex_hashfn(dev->ifindex);
1782 head = &fib_info_devhash[hash];
1783 ret = 0;
1784
Sasha Levinb67bfe02013-02-27 17:06:00 -08001785 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct fib_info *fi = nh->nh_parent;
1787 int alive;
1788
1789 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001790 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 continue;
1792
1793 prev_fi = fi;
1794 alive = 0;
1795 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001796 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 alive++;
1798 continue;
1799 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001800 if (!nexthop_nh->fib_nh_dev ||
1801 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001803 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08001804 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 continue;
1806 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001807 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001808 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 } endfor_nexthops(fi)
1810
1811 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001812 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ret++;
1814 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001815
1816 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818
1819 return ret;
1820}
1821
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001822#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001823static bool fib_good_nh(const struct fib_nh *nh)
1824{
1825 int state = NUD_REACHABLE;
1826
David Ahernb75ed8b2019-03-27 20:53:55 -07001827 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07001828 struct neighbour *n;
1829
1830 rcu_read_lock_bh();
1831
David Ahernb75ed8b2019-03-27 20:53:55 -07001832 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1833 (__force u32)nh->fib_nh_gw4);
David Aherna6db4492016-04-07 07:21:00 -07001834 if (n)
1835 state = n->nud_state;
1836
1837 rcu_read_unlock_bh();
1838 }
1839
1840 return !!(state & NUD_VALID);
1841}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001842
Peter Nørlund0e884c72015-09-30 10:12:21 +02001843void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001846 struct net *net = fi->fib_net;
1847 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
David Aherneba618a2019-04-02 14:11:55 -07001849 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08001850 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07001851 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08001852 continue;
1853 if (!first) {
1854 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001855 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08001856 first = true;
1857 }
1858 }
1859
David Aherneba618a2019-04-02 14:11:55 -07001860 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02001861 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Xin Long6174a302018-04-01 22:40:35 +08001863 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001864 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08001865 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868#endif
David Ahern3ce58d82015-10-05 08:51:25 -07001869
1870void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001871 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07001872{
David Ahern0d876f22018-02-13 08:11:34 -08001873 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
1874 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08001875
David Ahern3ce58d82015-10-05 08:51:25 -07001876#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern0d876f22018-02-13 08:11:34 -08001877 if (res->fi->fib_nhs > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08001878 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01001879
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001880 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07001881 }
1882 else
1883#endif
1884 if (!res->prefixlen &&
1885 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08001886 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07001887 fib_select_default(fl4, res);
1888
David Ahern0d876f22018-02-13 08:11:34 -08001889check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07001890 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07001891 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07001892}