blob: 4541121426fb4c0f98963234fe33ef3418cf07e3 [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>
David Ahern717a8f52019-04-05 16:30:32 -070044#include <net/ip6_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070045#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070046#include <net/rtnh.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020047#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020048#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070049#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "fib_lookup.h"
52
Stephen Hemminger832b4c52006-08-29 16:48:09 -070053static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct hlist_head *fib_info_hash;
55static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080056static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static unsigned int fib_info_cnt;
58
59#define DEVINDEX_HASHBITS 8
60#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
61static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
62
63#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000065#define for_nexthops(fi) { \
66 int nhsel; const struct fib_nh *nh; \
67 for (nhsel = 0, nh = (fi)->fib_nh; \
68 nhsel < (fi)->fib_nhs; \
69 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000071#define change_nexthops(fi) { \
72 int nhsel; struct fib_nh *nexthop_nh; \
73 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
74 nhsel < (fi)->fib_nhs; \
75 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#else /* CONFIG_IP_ROUTE_MULTIPATH */
78
79/* Hope, that gcc will optimize it to get rid of dummy loop */
80
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000081#define for_nexthops(fi) { \
82 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
83 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000085#define change_nexthops(fi) { \
86 int nhsel; \
87 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
88 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#endif /* CONFIG_IP_ROUTE_MULTIPATH */
91
92#define endfor_nexthops(fi) }
93
94
David S. Miller3be06862011-03-07 15:01:10 -080095const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000096 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .error = 0,
98 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000099 },
100 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .error = 0,
102 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000103 },
104 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .error = 0,
106 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000107 },
108 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .error = 0,
110 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000111 },
112 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .error = 0,
114 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000115 },
116 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .error = 0,
118 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000119 },
120 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .error = -EINVAL,
122 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000123 },
124 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .error = -EHOSTUNREACH,
126 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000127 },
128 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .error = -EACCES,
130 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000131 },
132 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .error = -EAGAIN,
134 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000135 },
136 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .error = -EINVAL,
138 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000139 },
140 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .error = -EINVAL,
142 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000143 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
David S. Millerc5038a82012-07-31 15:02:02 -0700146static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000147{
148 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
149
150 if (!rt)
151 return;
152
153 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
154 * because we waited an RCU grace period before calling
155 * free_fib_info_rcu()
156 */
157
Wei Wang95c47f92017-06-17 10:42:30 -0700158 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700159 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000160}
161
David Aherna5995e72019-04-30 07:45:50 -0700162static void free_nh_exceptions(struct fib_nh_common *nhc)
David S. Millerc5038a82012-07-31 15:02:02 -0700163{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700164 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700165 int i;
166
David Aherna5995e72019-04-30 07:45:50 -0700167 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Eric Dumazetcaa41522014-09-03 22:21:56 -0700168 if (!hash)
169 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700170 for (i = 0; i < FNHE_HASH_SIZE; i++) {
171 struct fib_nh_exception *fnhe;
172
173 fnhe = rcu_dereference_protected(hash[i].chain, 1);
174 while (fnhe) {
175 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800176
David S. Millerc5038a82012-07-31 15:02:02 -0700177 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
178
Timo Teräs2ffae992013-06-27 10:27:05 +0300179 rt_fibinfo_free(&fnhe->fnhe_rth_input);
180 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700181
182 kfree(fnhe);
183
184 fnhe = next;
185 }
186 }
187 kfree(hash);
188}
189
190static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000191{
192 int cpu;
193
194 if (!rtp)
195 return;
196
197 for_each_possible_cpu(cpu) {
198 struct rtable *rt;
199
200 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700201 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700202 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700203 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700204 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000205 }
206 free_percpu(rtp);
207}
208
David Ahern979e2762019-03-27 20:53:58 -0700209void fib_nh_common_release(struct fib_nh_common *nhc)
210{
211 if (nhc->nhc_dev)
212 dev_put(nhc->nhc_dev);
213
214 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)
225 net->ipv4.fib_num_tclassid_users--;
226#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
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000235 change_nexthops(fi) {
David Ahernfaa041a2019-03-27 20:53:49 -0700236 fib_nh_release(fi->fib_net, nexthop_nh);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000237 } endfor_nexthops(fi);
238
David Aherncc5f0eb2018-10-04 20:07:52 -0700239 ip_fib_metrics_put(fi->fib_metrics);
240
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000241 kfree(fi);
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244void free_fib_info(struct fib_info *fi)
245{
246 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000247 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 fib_info_cnt--;
David Ahernfaa041a2019-03-27 20:53:49 -0700251
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000252 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100254EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256void fib_release_info(struct fib_info *fi)
257{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700258 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (fi && --fi->fib_treeref == 0) {
260 hlist_del(&fi->fib_hash);
261 if (fi->fib_prefsrc)
262 hlist_del(&fi->fib_lhash);
263 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700264 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800266 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 } endfor_nexthops(fi)
268 fi->fib_dead = 1;
269 fib_info_put(fi);
270 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700271 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000274static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 const struct fib_nh *onh = ofi->fib_nh;
277
278 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700279 if (nh->fib_nh_oif != onh->fib_nh_oif ||
David Aherna4ea5d42019-04-05 16:30:30 -0700280 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
David Ahernb75ed8b2019-03-27 20:53:55 -0700281 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700283 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100285#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 nh->nh_tclassid != onh->nh_tclassid ||
287#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700288 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
289 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -1;
David Aherna4ea5d42019-04-05 16:30:30 -0700291
292 if (nh->fib_nh_gw_family == AF_INET &&
293 nh->fib_nh_gw4 != onh->fib_nh_gw4)
294 return -1;
295
296 if (nh->fib_nh_gw_family == AF_INET6 &&
297 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
298 return -1;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 onh++;
301 } endfor_nexthops(fi);
302 return 0;
303}
304
David S. Miller88ebc722008-01-12 21:49:01 -0800305static inline unsigned int fib_devindex_hashfn(unsigned int val)
306{
307 unsigned int mask = DEVINDEX_HASHSIZE - 1;
308
309 return (val ^
310 (val >> DEVINDEX_HASHBITS) ^
311 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
315{
David S. Miller123b9732011-02-01 15:34:21 -0800316 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned int val = fi->fib_nhs;
318
David S. Miller37e826c2011-03-24 18:06:47 -0700319 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700320 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800322 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700323 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
David S. Miller88ebc722008-01-12 21:49:01 -0800324 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
327}
328
329static struct fib_info *fib_find_info(const struct fib_info *nfi)
330{
331 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct fib_info *fi;
333 unsigned int hash;
334
335 hash = fib_info_hashfn(nfi);
336 head = &fib_info_hash[hash];
337
Sasha Levinb67bfe02013-02-27 17:06:00 -0800338 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800339 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (fi->fib_nhs != nfi->fib_nhs)
342 continue;
343 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700344 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 nfi->fib_prefsrc == fi->fib_prefsrc &&
346 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000347 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000349 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400350 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
352 return fi;
353 }
354
355 return NULL;
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000359 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Al Virod878e72e2006-09-26 22:18:13 -0700361int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct fib_nh *nh;
365 unsigned int hash;
366
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700367 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 hash = fib_devindex_hashfn(dev->ifindex);
370 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800371 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700372 if (nh->fib_nh_dev == dev &&
373 nh->fib_nh_gw4 == gw &&
374 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700375 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377 }
378 }
379
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700380 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 return -1;
383}
384
Thomas Graf339bf982006-11-10 14:10:15 -0800385static inline size_t fib_nlmsg_size(struct fib_info *fi)
386{
387 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
388 + nla_total_size(4) /* RTA_TABLE */
389 + nla_total_size(4) /* RTA_DST */
390 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100391 + nla_total_size(4) /* RTA_PREFSRC */
392 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800393
394 /* space for nested metrics */
395 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
396
397 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200398 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800399 /* Also handles the special case fib_nhs == 1 */
400
401 /* each nexthop is packed in an attribute */
402 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
403
404 /* may contain flow and gateway attribute */
405 nhsize += 2 * nla_total_size(4);
406
Roopa Prabhu571e7222015-07-21 10:43:47 +0200407 /* grab encap info */
408 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700409 if (nh->fib_nh_lws) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200410 /* RTA_ENCAP_TYPE */
411 nh_encapsize += lwtunnel_get_encap_size(
David Ahernb75ed8b2019-03-27 20:53:55 -0700412 nh->fib_nh_lws);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200413 /* RTA_ENCAP */
414 nh_encapsize += nla_total_size(2);
415 }
416 } endfor_nexthops(fi);
417
Thomas Graf339bf982006-11-10 14:10:15 -0800418 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200419 payload += nla_total_size((fi->fib_nhs * nhsize) +
420 nh_encapsize);
421
Thomas Graf339bf982006-11-10 14:10:15 -0800422 }
423
424 return payload;
425}
426
Al Viro81f7bf62006-09-27 18:40:00 -0700427void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700428 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700429 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700432 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700433 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Thomas Graf339bf982006-11-10 14:10:15 -0800435 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100436 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700437 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Eric W. Biederman15e47302012-09-07 20:12:54 +0000439 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700440 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700441 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800442 if (err < 0) {
443 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
444 WARN_ON(err == -EMSGSIZE);
445 kfree_skb(skb);
446 goto errout;
447 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000448 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800449 info->nlh, GFP_KERNEL);
450 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700451errout:
452 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800453 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800456static int fib_detect_death(struct fib_info *fi, int order,
457 struct fib_info **last_resort, int *last_idx,
458 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
David Ahern619d1822019-04-05 16:30:37 -0700460 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct neighbour *n;
462 int state = NUD_NONE;
463
David Ahern619d1822019-04-05 16:30:37 -0700464 if (likely(nhc->nhc_gw_family == AF_INET))
465 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
466 else if (nhc->nhc_gw_family == AF_INET6)
467 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
468 nhc->nhc_dev);
469 else
470 n = NULL;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (n) {
473 state = n->nud_state;
474 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300475 } else {
476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800478 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000480 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000482 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300483 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *last_resort = fi;
485 *last_idx = order;
486 }
487 return 1;
488}
489
David Ahern979e2762019-03-27 20:53:58 -0700490int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
491 u16 encap_type, void *cfg, gfp_t gfp_flags,
492 struct netlink_ext_ack *extack)
493{
David Ahern0f457a32019-04-30 07:45:48 -0700494 int err;
495
496 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
497 gfp_flags);
498 if (!nhc->nhc_pcpu_rth_output)
499 return -ENOMEM;
500
David Ahern979e2762019-03-27 20:53:58 -0700501 if (encap) {
502 struct lwtunnel_state *lwtstate;
David Ahern979e2762019-03-27 20:53:58 -0700503
504 if (encap_type == LWTUNNEL_ENCAP_NONE) {
505 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
David Ahern0f457a32019-04-30 07:45:48 -0700506 err = -EINVAL;
507 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700508 }
509 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
510 cfg, &lwtstate, extack);
511 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700512 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700513
514 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
515 }
516
517 return 0;
David Ahern0f457a32019-04-30 07:45:48 -0700518
519lwt_failure:
520 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
521 nhc->nhc_pcpu_rth_output = NULL;
522 return err;
David Ahern979e2762019-03-27 20:53:58 -0700523}
524EXPORT_SYMBOL_GPL(fib_nh_common_init);
525
David Aherne4516ef2019-03-27 20:53:48 -0700526int fib_nh_init(struct net *net, struct fib_nh *nh,
527 struct fib_config *cfg, int nh_weight,
528 struct netlink_ext_ack *extack)
529{
David Ahern0f457a32019-04-30 07:45:48 -0700530 int err;
David Aherne4516ef2019-03-27 20:53:48 -0700531
David Ahernf1741732019-03-27 20:53:57 -0700532 nh->fib_nh_family = AF_INET;
533
David Ahern979e2762019-03-27 20:53:58 -0700534 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
535 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
536 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700537 return err;
David Aherne4516ef2019-03-27 20:53:48 -0700538
David Ahernb75ed8b2019-03-27 20:53:55 -0700539 nh->fib_nh_oif = cfg->fc_oif;
David Aherna4ea5d42019-04-05 16:30:30 -0700540 nh->fib_nh_gw_family = cfg->fc_gw_family;
541 if (cfg->fc_gw_family == AF_INET)
David Ahernf35b7942019-04-05 16:30:28 -0700542 nh->fib_nh_gw4 = cfg->fc_gw4;
David Aherna4ea5d42019-04-05 16:30:30 -0700543 else if (cfg->fc_gw_family == AF_INET6)
544 nh->fib_nh_gw6 = cfg->fc_gw6;
545
David Ahernb75ed8b2019-03-27 20:53:55 -0700546 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700547
548#ifdef CONFIG_IP_ROUTE_CLASSID
549 nh->nh_tclassid = cfg->fc_flow;
550 if (nh->nh_tclassid)
551 net->ipv4.fib_num_tclassid_users++;
552#endif
553#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700554 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700555#endif
556 return 0;
David Aherne4516ef2019-03-27 20:53:48 -0700557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#ifdef CONFIG_IP_ROUTE_MULTIPATH
560
David Ahern6d8422a12017-05-21 10:12:02 -0600561static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
562 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Thomas Graf4e902c52006-08-17 18:14:52 -0700566 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700568 rtnh = rtnh_next(rtnh, &remaining);
569 }
570
571 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600572 if (remaining > 0) {
573 NL_SET_ERR_MSG(extack,
574 "Invalid nexthop configuration - extra data after nexthops");
575 nhs = 0;
576 }
577
578 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Thomas Graf4e902c52006-08-17 18:14:52 -0700581static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600582 int remaining, struct fib_config *cfg,
583 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
David Aherne4516ef2019-03-27 20:53:48 -0700585 struct net *net = fi->fib_net;
586 struct fib_config fib_cfg;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200587 int ret;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700590 int attrlen;
591
David Aherne4516ef2019-03-27 20:53:48 -0700592 memset(&fib_cfg, 0, sizeof(fib_cfg));
593
David Ahernc3ab2b42017-05-21 10:12:03 -0600594 if (!rtnh_ok(rtnh, remaining)) {
595 NL_SET_ERR_MSG(extack,
596 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600598 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700599
David Ahernc3ab2b42017-05-21 10:12:03 -0600600 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
601 NL_SET_ERR_MSG(extack,
602 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300603 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600604 }
Julian Anastasov80610222016-07-10 21:11:55 +0300605
David Aherne4516ef2019-03-27 20:53:48 -0700606 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
607 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700608
609 attrlen = rtnh_attrlen(rtnh);
610 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700611 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700612
613 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700614 nlav = nla_find(attrs, attrlen, RTA_VIA);
615 if (nla && nlav) {
616 NL_SET_ERR_MSG(extack,
617 "Nexthop configuration can not contain both GATEWAY and VIA");
618 return -EINVAL;
619 }
David Ahernf35b7942019-04-05 16:30:28 -0700620 if (nla) {
David Ahernf35b7942019-04-05 16:30:28 -0700621 fib_cfg.fc_gw4 = nla_get_in_addr(nla);
David Ahernd73f80f2019-04-10 10:05:51 -0700622 if (fib_cfg.fc_gw4)
623 fib_cfg.fc_gw_family = AF_INET;
David Ahernd1566262019-04-05 16:30:40 -0700624 } else if (nlav) {
625 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
626 if (ret)
627 goto errout;
David Ahernf35b7942019-04-05 16:30:28 -0700628 }
David Aherne4516ef2019-03-27 20:53:48 -0700629
Thomas Graf4e902c52006-08-17 18:14:52 -0700630 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Aherne4516ef2019-03-27 20:53:48 -0700631 if (nla)
632 fib_cfg.fc_flow = nla_get_u32(nla);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200633
David Aherne4516ef2019-03-27 20:53:48 -0700634 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
635 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
636 if (nla)
637 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700639
David Aherne4516ef2019-03-27 20:53:48 -0700640 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
641 rtnh->rtnh_hops + 1, extack);
642 if (ret)
643 goto errout;
644
Thomas Graf4e902c52006-08-17 18:14:52 -0700645 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700647
Roopa Prabhu571e7222015-07-21 10:43:47 +0200648 ret = -EINVAL;
David Ahernb75ed8b2019-03-27 20:53:55 -0700649 if (cfg->fc_oif && fi->fib_nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700650 NL_SET_ERR_MSG(extack,
651 "Nexthop device index does not match RTA_OIF");
652 goto errout;
653 }
David Ahernf35b7942019-04-05 16:30:28 -0700654 if (cfg->fc_gw_family) {
655 if (cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family ||
656 (cfg->fc_gw_family == AF_INET &&
David Aherna4ea5d42019-04-05 16:30:30 -0700657 fi->fib_nh->fib_nh_gw4 != cfg->fc_gw4) ||
658 (cfg->fc_gw_family == AF_INET6 &&
659 ipv6_addr_cmp(&fi->fib_nh->fib_nh_gw6, &cfg->fc_gw6))) {
David Ahernf35b7942019-04-05 16:30:28 -0700660 NL_SET_ERR_MSG(extack,
David Aherna4ea5d42019-04-05 16:30:30 -0700661 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
David Ahernf35b7942019-04-05 16:30:28 -0700662 goto errout;
663 }
David Aherne4516ef2019-03-27 20:53:48 -0700664 }
665#ifdef CONFIG_IP_ROUTE_CLASSID
666 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
667 NL_SET_ERR_MSG(extack,
668 "Nexthop class id does not match RTA_FLOW");
669 goto errout;
670 }
671#endif
672 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200673errout:
674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Peter Nørlund0e884c72015-09-30 10:12:21 +0200677static void fib_rebalance(struct fib_info *fi)
678{
679 int total;
680 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200681
682 if (fi->fib_nhs < 2)
683 return;
684
685 total = 0;
686 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700687 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200688 continue;
689
David Ahernb75ed8b2019-03-27 20:53:55 -0700690 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
691 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200692 continue;
693
David Ahernb75ed8b2019-03-27 20:53:55 -0700694 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200695 } endfor_nexthops(fi);
696
697 w = 0;
698 change_nexthops(fi) {
699 int upper_bound;
700
David Ahernb75ed8b2019-03-27 20:53:55 -0700701 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200702 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700703 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
704 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200705 upper_bound = -1;
706 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700707 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200708 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
709 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200710 }
711
David Ahernb75ed8b2019-03-27 20:53:55 -0700712 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200713 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200714}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200715#else /* CONFIG_IP_ROUTE_MULTIPATH */
716
David Ahern8373c6c2019-03-27 20:53:46 -0700717static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
718 int remaining, struct fib_config *cfg,
719 struct netlink_ext_ack *extack)
720{
721 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
722
723 return -EINVAL;
724}
725
Peter Nørlund0e884c72015-09-30 10:12:21 +0200726#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200727
728#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
David Ahern30357d72017-01-30 12:07:37 -0800730static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800731 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800732 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600733 const struct fib_config *cfg,
734 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200735{
736 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200737 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200738
739 if (encap_type == LWTUNNEL_ENCAP_NONE)
740 return 0;
741
David Ahern9ae28722017-05-27 16:19:28 -0600742 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
743 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200744 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700745 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200746 lwtstate_free(lwtstate);
747 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200748
Jiri Bencdf383e62015-08-18 18:41:13 +0200749 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200750}
751
David Ahern9ae28722017-05-27 16:19:28 -0600752int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
753 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700756 struct rtnexthop *rtnh;
757 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif
759
Thomas Graf4e902c52006-08-17 18:14:52 -0700760 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
762
David Ahernf35b7942019-04-05 16:30:28 -0700763 if (cfg->fc_oif || cfg->fc_gw_family) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200764 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600765 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
766 fi->fib_nh, cfg, extack))
767 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200768 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100769#ifdef CONFIG_IP_ROUTE_CLASSID
770 if (cfg->fc_flow &&
771 cfg->fc_flow != fi->fib_nh->nh_tclassid)
772 return 1;
773#endif
David Ahernf35b7942019-04-05 16:30:28 -0700774 if ((cfg->fc_oif && cfg->fc_oif != fi->fib_nh->fib_nh_oif) ||
775 (cfg->fc_gw_family &&
776 cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family))
777 return 1;
778
779 if (cfg->fc_gw_family == AF_INET &&
780 cfg->fc_gw4 != fi->fib_nh->fib_nh_gw4)
781 return 1;
782
David Aherna4ea5d42019-04-05 16:30:30 -0700783 if (cfg->fc_gw_family == AF_INET6 &&
784 ipv6_addr_cmp(&cfg->fc_gw6, &fi->fib_nh->fib_nh_gw6))
785 return 1;
786
David Ahernf35b7942019-04-05 16:30:28 -0700787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100791 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700793
794 rtnh = cfg->fc_mp;
795 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700798 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Thomas Graf4e902c52006-08-17 18:14:52 -0700800 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700802
David Ahernb75ed8b2019-03-27 20:53:55 -0700803 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700805
806 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200807 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700808 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700809
810 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700811 nlav = nla_find(attrs, attrlen, RTA_VIA);
812 if (nla && nlav) {
813 NL_SET_ERR_MSG(extack,
814 "Nexthop configuration can not contain both GATEWAY and VIA");
815 return -EINVAL;
816 }
817
818 if (nla) {
819 if (nh->fib_nh_gw_family != AF_INET ||
820 nla_get_in_addr(nla) != nh->fib_nh_gw4)
821 return 1;
822 } else if (nlav) {
823 struct fib_config cfg2;
824 int err;
825
826 err = fib_gw_from_via(&cfg2, nlav, extack);
827 if (err)
828 return err;
829
830 switch (nh->fib_nh_gw_family) {
831 case AF_INET:
832 if (cfg2.fc_gw_family != AF_INET ||
833 cfg2.fc_gw4 != nh->fib_nh_gw4)
834 return 1;
835 break;
836 case AF_INET6:
837 if (cfg2.fc_gw_family != AF_INET6 ||
838 ipv6_addr_cmp(&cfg2.fc_gw6,
839 &nh->fib_nh_gw6))
840 return 1;
841 break;
842 }
843 }
844
Patrick McHardyc7066f72011-01-14 13:36:42 +0100845#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700846 nla = nla_find(attrs, attrlen, RTA_FLOW);
847 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 1;
849#endif
850 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700851
852 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } endfor_nexthops(fi);
854#endif
855 return 0;
856}
857
Xin Long5f9ae3d2017-08-23 10:07:26 +0800858bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
859{
860 struct nlattr *nla;
861 int remaining;
862
863 if (!cfg->fc_mx)
864 return true;
865
866 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
867 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +0100868 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800869
870 if (!type)
871 continue;
872 if (type > RTAX_MAX)
873 return false;
874
875 if (type == RTAX_CC_ALGO) {
876 char tmp[TCP_CA_NAME_MAX];
877 bool ecn_ca = false;
878
879 nla_strlcpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -0800880 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +0800881 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -0700882 if (nla_len(nla) != sizeof(u32))
883 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800884 val = nla_get_u32(nla);
885 }
886
Phil Sutterd03a4552017-12-19 15:17:13 +0100887 fi_val = fi->fib_metrics->metrics[type - 1];
888 if (type == RTAX_FEATURES)
889 fi_val &= ~DST_FEATURE_ECN_CA;
890
891 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +0800892 return false;
893 }
894
895 return true;
896}
897
David Ahern717a8f52019-04-05 16:30:32 -0700898static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
899 u32 table, struct netlink_ext_ack *extack)
900{
901 struct fib6_config cfg = {
902 .fc_table = table,
903 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
904 .fc_ifindex = nh->fib_nh_oif,
905 .fc_gateway = nh->fib_nh_gw6,
906 };
907 struct fib6_nh fib6_nh = {};
908 int err;
909
910 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
911 if (!err) {
912 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
913 dev_hold(nh->fib_nh_dev);
914 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
915 nh->fib_nh_scope = RT_SCOPE_LINK;
916
917 ipv6_stub->fib6_nh_release(&fib6_nh);
918 }
919
920 return err;
921}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000924 * Picture
925 * -------
926 *
927 * Semantics of nexthop is very messy by historical reasons.
928 * We have to take into account, that:
929 * a) gateway can be actually local interface address,
930 * so that gatewayed route is direct.
931 * b) gateway must be on-link address, possibly
932 * described not by an ifaddr, but also by a direct route.
933 * c) If both gateway and interface are specified, they should not
934 * contradict.
935 * d) If we use tunnel routes, gateway could be not on-link.
936 *
937 * Attempt to reconcile all of these (alas, self-contradictory) conditions
938 * results in pretty ugly and hairy code with obscure logic.
939 *
940 * I chose to generalized it instead, so that the size
941 * of code does not increase practically, but it becomes
942 * much more general.
943 * Every prefix is assigned a "scope" value: "host" is local address,
944 * "link" is direct route,
945 * [ ... "site" ... "interior" ... ]
946 * and "universe" is true gateway route with global meaning.
947 *
948 * Every prefix refers to a set of "nexthop"s (gw, oif),
949 * where gw must have narrower scope. This recursion stops
950 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
951 * which means that gw is forced to be on link.
952 *
953 * Code is still hairy, but now it is apparently logically
954 * consistent and very flexible. F.e. as by-product it allows
955 * to co-exists in peace independent exterior and interior
956 * routing processes.
957 *
958 * Normally it looks as following.
959 *
960 * {universe prefix} -> (gw, oif) [scope link]
961 * |
962 * |-> {link prefix} -> (gw, oif) [scope local]
963 * |
964 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
David Ahern448d7242019-04-05 16:30:31 -0700966static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
967 u8 scope, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000969 struct net_device *dev;
David Ahern448d7242019-04-05 16:30:31 -0700970 struct fib_result res;
971 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
David Ahern448d7242019-04-05 16:30:31 -0700973 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
974 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
David Ahern448d7242019-04-05 16:30:31 -0700976 if (scope >= RT_SCOPE_LINK) {
977 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
David Ahern448d7242019-04-05 16:30:31 -0700980 dev = __dev_get_by_index(net, nh->fib_nh_oif);
981 if (!dev) {
982 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
983 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
David Ahern448d7242019-04-05 16:30:31 -0700985 if (!(dev->flags & IFF_UP)) {
986 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
987 return -ENETDOWN;
988 }
989 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
990 if (addr_type != RTN_UNICAST) {
991 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
992 return -EINVAL;
993 }
994 if (!netif_carrier_ok(dev))
995 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
996 nh->fib_nh_dev = dev;
997 dev_hold(dev);
998 nh->fib_nh_scope = RT_SCOPE_LINK;
999 return 0;
1000 }
1001 rcu_read_lock();
1002 {
1003 struct fib_table *tbl = NULL;
1004 struct flowi4 fl4 = {
1005 .daddr = nh->fib_nh_gw4,
1006 .flowi4_scope = scope + 1,
1007 .flowi4_oif = nh->fib_nh_oif,
1008 .flowi4_iif = LOOPBACK_IFINDEX,
1009 };
1010
1011 /* It is not necessary, but requires a bit of thinking */
1012 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1013 fl4.flowi4_scope = RT_SCOPE_LINK;
1014
1015 if (table)
1016 tbl = fib_get_table(net, table);
1017
1018 if (tbl)
1019 err = fib_table_lookup(tbl, &fl4, &res,
1020 FIB_LOOKUP_IGNORE_LINKSTATE |
1021 FIB_LOOKUP_NOREF);
1022
1023 /* on error or if no table given do full lookup. This
1024 * is needed for example when nexthops are in the local
1025 * table rather than the given table
1026 */
1027 if (!tbl || err) {
1028 err = fib_lookup(net, &fl4, &res,
1029 FIB_LOOKUP_IGNORE_LINKSTATE);
1030 }
1031
1032 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001033 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -06001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
David Ahern448d7242019-04-05 16:30:31 -07001037
1038 err = -EINVAL;
1039 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1040 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1041 goto out;
1042 }
1043 nh->fib_nh_scope = res.scope;
1044 nh->fib_nh_oif = FIB_RES_OIF(res);
1045 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1046 if (!dev) {
1047 NL_SET_ERR_MSG(extack,
1048 "No egress device for nexthop gateway");
1049 goto out;
1050 }
1051 dev_hold(dev);
1052 if (!netif_carrier_ok(dev))
1053 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1054 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +00001055out:
1056 rcu_read_unlock();
1057 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
David Ahern448d7242019-04-05 16:30:31 -07001060static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1061 struct netlink_ext_ack *extack)
1062{
1063 struct in_device *in_dev;
1064 int err;
1065
1066 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1067 NL_SET_ERR_MSG(extack,
1068 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1069 return -EINVAL;
1070 }
1071
1072 rcu_read_lock();
1073
1074 err = -ENODEV;
1075 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1076 if (!in_dev)
1077 goto out;
1078 err = -ENETDOWN;
1079 if (!(in_dev->dev->flags & IFF_UP)) {
1080 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1081 goto out;
1082 }
1083
1084 nh->fib_nh_dev = in_dev->dev;
1085 dev_hold(nh->fib_nh_dev);
1086 nh->fib_nh_scope = RT_SCOPE_HOST;
1087 if (!netif_carrier_ok(nh->fib_nh_dev))
1088 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1089 err = 0;
1090out:
1091 rcu_read_unlock();
1092 return err;
1093}
1094
David Ahernac1fab22019-05-22 12:04:43 -07001095int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1096 struct netlink_ext_ack *extack)
David Ahern448d7242019-04-05 16:30:31 -07001097{
David Ahern448d7242019-04-05 16:30:31 -07001098 int err;
1099
1100 if (nh->fib_nh_gw_family == AF_INET)
David Ahernac1fab22019-05-22 12:04:43 -07001101 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
David Ahern717a8f52019-04-05 16:30:32 -07001102 else if (nh->fib_nh_gw_family == AF_INET6)
1103 err = fib_check_nh_v6_gw(net, nh, table, extack);
David Ahern448d7242019-04-05 16:30:31 -07001104 else
1105 err = fib_check_nh_nongw(net, nh, extack);
1106
1107 return err;
1108}
1109
Al Viro81f7bf62006-09-27 18:40:00 -07001110static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
David S. Miller123b9732011-02-01 15:34:21 -08001112 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001114 return ((__force u32)val ^
1115 ((__force u32)val >> 7) ^
1116 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
David S. Miller123b9732011-02-01 15:34:21 -08001119static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +08001122 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 else
1124 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001125 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1126 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
David S. Miller123b9732011-02-01 15:34:21 -08001129static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 if (!hash)
1132 return;
1133
1134 if (bytes <= PAGE_SIZE)
1135 kfree(hash);
1136 else
1137 free_pages((unsigned long) hash, get_order(bytes));
1138}
1139
David S. Miller123b9732011-02-01 15:34:21 -08001140static void fib_info_hash_move(struct hlist_head *new_info_hash,
1141 struct hlist_head *new_laddrhash,
1142 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
David S. Millerb7656e72005-08-05 04:12:48 -07001144 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001145 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001146 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001148 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001149 old_info_hash = fib_info_hash;
1150 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001151 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 for (i = 0; i < old_size; i++) {
1154 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001155 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct fib_info *fi;
1157
Sasha Levinb67bfe02013-02-27 17:06:00 -08001158 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct hlist_head *dest;
1160 unsigned int new_hash;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 new_hash = fib_info_hashfn(fi);
1163 dest = &new_info_hash[new_hash];
1164 hlist_add_head(&fi->fib_hash, dest);
1165 }
1166 }
1167 fib_info_hash = new_info_hash;
1168
1169 for (i = 0; i < old_size; i++) {
1170 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001171 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct fib_info *fi;
1173
Sasha Levinb67bfe02013-02-27 17:06:00 -08001174 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct hlist_head *ldest;
1176 unsigned int new_hash;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1179 ldest = &new_laddrhash[new_hash];
1180 hlist_add_head(&fi->fib_lhash, ldest);
1181 }
1182 }
1183 fib_info_laddrhash = new_laddrhash;
1184
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001185 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001186
1187 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001188 fib_info_hash_free(old_info_hash, bytes);
1189 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
David S. Miller436c3b62011-03-24 17:42:21 -07001192__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1193{
David Ahernb75ed8b2019-03-27 20:53:55 -07001194 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev,
1195 nh->fib_nh_gw4,
David S. Miller37e826c2011-03-24 18:06:47 -07001196 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001197 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1198
1199 return nh->nh_saddr;
1200}
1201
David Aherneba618a2019-04-02 14:11:55 -07001202__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1203{
1204 struct fib_nh_common *nhc = res->nhc;
1205 struct fib_nh *nh;
1206
1207 if (res->fi->fib_prefsrc)
1208 return res->fi->fib_prefsrc;
1209
1210 nh = container_of(nhc, struct fib_nh, nh_common);
1211 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1212 return nh->nh_saddr;
1213
1214 return fib_info_update_nh_saddr(net, nh);
1215}
1216
David Ahern021dd3b2015-08-13 14:59:06 -06001217static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1218{
1219 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1220 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001221 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001222 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001223
1224 if (tb_id == RT_TABLE_MAIN)
1225 tb_id = RT_TABLE_LOCAL;
1226
David Aherne1b8d902015-11-03 15:59:28 -08001227 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1228 fib_prefsrc, tb_id);
1229
1230 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1231 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1232 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001233 }
David Aherne1b8d902015-11-03 15:59:28 -08001234
1235 if (rc != RTN_LOCAL)
1236 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001237 }
1238 return true;
1239}
1240
David Ahern6d8422a12017-05-21 10:12:02 -06001241struct fib_info *fib_create_info(struct fib_config *cfg,
1242 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 int err;
1245 struct fib_info *fi = NULL;
1246 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001248 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
David S. Miller4c8237c2011-03-07 14:27:38 -08001250 if (cfg->fc_type > RTN_MAX)
1251 goto err_inval;
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001254 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1255 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
David Ahernc3ab2b42017-05-21 10:12:03 -06001259 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1260 NL_SET_ERR_MSG(extack,
1261 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001262 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001263 }
Julian Anastasov80610222016-07-10 21:11:55 +03001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001266 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001267 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (nhs == 0)
1269 goto err_inval;
1270 }
1271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001274 if (fib_info_cnt >= fib_info_hash_size) {
1275 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct hlist_head *new_info_hash;
1277 struct hlist_head *new_laddrhash;
1278 unsigned int bytes;
1279
1280 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001281 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001283 new_info_hash = fib_info_hash_alloc(bytes);
1284 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001286 fib_info_hash_free(new_info_hash, bytes);
1287 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001288 } else
David S. Miller123b9732011-02-01 15:34:21 -08001289 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
David S. Miller123b9732011-02-01 15:34:21 -08001291 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto failure;
1293 }
1294
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001295 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001296 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001298 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001299 cfg->fc_mx_len, extack);
David Ahern767a2212018-10-04 20:07:51 -07001300 if (unlikely(IS_ERR(fi->fib_metrics))) {
1301 err = PTR_ERR(fi->fib_metrics);
1302 kfree(fi);
1303 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001304 }
David Ahern767a2212018-10-04 20:07:51 -07001305
Eric Dumazet187e5b32017-08-15 05:26:17 -07001306 fib_info_cnt++;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001307 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001308 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001309 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001310 fi->fib_flags = cfg->fc_flags;
1311 fi->fib_priority = cfg->fc_priority;
1312 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001313 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001314 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 fi->fib_nhs = nhs;
1317 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001318 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 } endfor_nexthops(fi)
1320
David Aherne4516ef2019-03-27 20:53:48 -07001321 if (cfg->fc_mp)
David Ahern6d8422a12017-05-21 10:12:02 -06001322 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
David Aherne4516ef2019-03-27 20:53:48 -07001323 else
1324 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001325
David Aherne4516ef2019-03-27 20:53:48 -07001326 if (err != 0)
1327 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Thomas Graf4e902c52006-08-17 18:14:52 -07001329 if (fib_props[cfg->fc_type].error) {
David Ahernf35b7942019-04-05 16:30:28 -07001330 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001331 NL_SET_ERR_MSG(extack,
1332 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001336 } else {
1337 switch (cfg->fc_type) {
1338 case RTN_UNICAST:
1339 case RTN_LOCAL:
1340 case RTN_BROADCAST:
1341 case RTN_ANYCAST:
1342 case RTN_MULTICAST:
1343 break;
1344 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001345 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001346 goto err_inval;
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
David Ahernc3ab2b42017-05-21 10:12:03 -06001350 if (cfg->fc_scope > RT_SCOPE_HOST) {
1351 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Thomas Graf4e902c52006-08-17 18:14:52 -07001355 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct fib_nh *nh = fi->fib_nh;
1357
1358 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001359 if (nhs != 1) {
1360 NL_SET_ERR_MSG(extack,
1361 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001362 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001363 }
David Ahernbdf00462019-04-05 16:30:26 -07001364 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001365 NL_SET_ERR_MSG(extack,
1366 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001368 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001369 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1370 nh->fib_nh_dev = dev_get_by_index(net, fi->fib_nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001372 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 goto failure;
1374 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001375 int linkdown = 0;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 change_nexthops(fi) {
David Ahernac1fab22019-05-22 12:04:43 -07001378 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1379 cfg->fc_table, cfg->fc_scope,
1380 extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001381 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001383 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001384 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001386 if (linkdown == fi->fib_nhs)
1387 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
David Ahernc3ab2b42017-05-21 10:12:03 -06001390 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1391 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001392 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
David S. Miller1fc050a2011-03-07 20:54:48 -08001395 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001396 fib_info_update_nh_saddr(net, nexthop_nh);
David Ahern19a9d132019-04-05 16:30:39 -07001397 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1398 fi->fib_nh_is_v6 = true;
David S. Miller1fc050a2011-03-07 20:54:48 -08001399 } endfor_nexthops(fi)
1400
Peter Nørlund0e884c72015-09-30 10:12:21 +02001401 fib_rebalance(fi);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001404 ofi = fib_find_info(fi);
1405 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 fi->fib_dead = 1;
1407 free_fib_info(fi);
1408 ofi->fib_treeref++;
1409 return ofi;
1410 }
1411
1412 fi->fib_treeref++;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001413 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001414 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 hlist_add_head(&fi->fib_hash,
1416 &fib_info_hash[fib_info_hashfn(fi)]);
1417 if (fi->fib_prefsrc) {
1418 struct hlist_head *head;
1419
1420 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1421 hlist_add_head(&fi->fib_lhash, head);
1422 }
1423 change_nexthops(fi) {
1424 struct hlist_head *head;
1425 unsigned int hash;
1426
David Ahernb75ed8b2019-03-27 20:53:55 -07001427 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001429 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001431 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001433 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return fi;
1435
1436err_inval:
1437 err = -EINVAL;
1438
1439failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001440 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 fi->fib_dead = 1;
1442 free_fib_info(fi);
1443 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001444
1445 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
David Ahernc0a72072019-04-02 14:11:58 -07001448int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
David Ahernecc56632019-04-23 08:48:09 -07001449 unsigned char *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001450{
David Ahernc2364192019-04-02 14:11:57 -07001451 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001452 *flags |= RTNH_F_DEAD;
1453
David Ahernc2364192019-04-02 14:11:57 -07001454 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001455 *flags |= RTNH_F_LINKDOWN;
1456
1457 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001458 switch (nhc->nhc_family) {
1459 case AF_INET:
1460 if (ip_ignore_linkdown(nhc->nhc_dev))
1461 *flags |= RTNH_F_DEAD;
1462 break;
David Ahernc0a72072019-04-02 14:11:58 -07001463 case AF_INET6:
1464 if (ip6_ignore_linkdown(nhc->nhc_dev))
1465 *flags |= RTNH_F_DEAD;
1466 break;
David Ahernc2364192019-04-02 14:11:57 -07001467 }
David Ahernb0f60192019-04-02 14:11:56 -07001468 rcu_read_unlock();
1469 }
1470
David Ahernbdf00462019-04-05 16:30:26 -07001471 switch (nhc->nhc_gw_family) {
1472 case AF_INET:
1473 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1474 goto nla_put_failure;
1475 break;
1476 case AF_INET6:
David Ahernd1566262019-04-05 16:30:40 -07001477 /* if gateway family does not match nexthop family
1478 * gateway is encoded as RTA_VIA
1479 */
1480 if (nhc->nhc_gw_family != nhc->nhc_family) {
1481 int alen = sizeof(struct in6_addr);
1482 struct nlattr *nla;
1483 struct rtvia *via;
1484
1485 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1486 if (!nla)
1487 goto nla_put_failure;
1488
1489 via = nla_data(nla);
1490 via->rtvia_family = AF_INET6;
1491 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1492 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1493 &nhc->nhc_gw.ipv6) < 0) {
David Ahernbdf00462019-04-05 16:30:26 -07001494 goto nla_put_failure;
David Ahernd1566262019-04-05 16:30:40 -07001495 }
David Ahernbdf00462019-04-05 16:30:26 -07001496 break;
David Ahernc2364192019-04-02 14:11:57 -07001497 }
David Ahernb0f60192019-04-02 14:11:56 -07001498
David Ahernc2364192019-04-02 14:11:57 -07001499 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1500 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
David Ahernb0f60192019-04-02 14:11:56 -07001501 *flags |= RTNH_F_OFFLOAD;
1502
David Ahernc2364192019-04-02 14:11:57 -07001503 if (!skip_oif && nhc->nhc_dev &&
1504 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001505 goto nla_put_failure;
1506
David Ahernc2364192019-04-02 14:11:57 -07001507 if (nhc->nhc_lwtstate &&
David Ahernffa8ce52019-04-23 08:23:41 -07001508 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1509 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001510 goto nla_put_failure;
1511
1512 return 0;
1513
1514nla_put_failure:
1515 return -EMSGSIZE;
1516}
David Ahernc0a72072019-04-02 14:11:58 -07001517EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001518
David Ahernc0a72072019-04-02 14:11:58 -07001519#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1520int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1521 int nh_weight)
David Ahernb0f60192019-04-02 14:11:56 -07001522{
David Ahernc2364192019-04-02 14:11:57 -07001523 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001524 struct rtnexthop *rtnh;
David Ahernecc56632019-04-23 08:48:09 -07001525 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001526
1527 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1528 if (!rtnh)
1529 goto nla_put_failure;
1530
David Ahernc2364192019-04-02 14:11:57 -07001531 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001532 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1533
David Ahernc2364192019-04-02 14:11:57 -07001534 if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001535 goto nla_put_failure;
1536
1537 rtnh->rtnh_flags = flags;
1538
1539 /* length of rtnetlink header + attributes */
1540 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1541
1542 return 0;
1543
1544nla_put_failure:
1545 return -EMSGSIZE;
1546}
David Ahernc0a72072019-04-02 14:11:58 -07001547EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001548#endif
David Ahernb0f60192019-04-02 14:11:56 -07001549
David Ahernc2364192019-04-02 14:11:57 -07001550#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001551static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1552{
1553 struct nlattr *mp;
1554
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001555 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernb0f60192019-04-02 14:11:56 -07001556 if (!mp)
1557 goto nla_put_failure;
1558
1559 for_nexthops(fi) {
David Ahernc2364192019-04-02 14:11:57 -07001560 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001561 goto nla_put_failure;
1562#ifdef CONFIG_IP_ROUTE_CLASSID
1563 if (nh->nh_tclassid &&
1564 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1565 goto nla_put_failure;
1566#endif
1567 } endfor_nexthops(fi);
1568
1569 nla_nest_end(skb, mp);
1570
1571 return 0;
1572
1573nla_put_failure:
1574 return -EMSGSIZE;
1575}
1576#else
1577static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1578{
1579 return 0;
1580}
1581#endif
1582
Eric W. Biederman15e47302012-09-07 20:12:54 +00001583int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001584 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001585 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001587 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Eric W. Biederman15e47302012-09-07 20:12:54 +00001590 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001591 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001592 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001593
1594 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 rtm->rtm_family = AF_INET;
1596 rtm->rtm_dst_len = dst_len;
1597 rtm->rtm_src_len = 0;
1598 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001599 if (tb_id < 256)
1600 rtm->rtm_table = tb_id;
1601 else
1602 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001603 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1604 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 rtm->rtm_type = type;
1606 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001607 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001609
David S. Millerf3756b72012-04-01 20:39:02 -04001610 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001611 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001612 goto nla_put_failure;
1613 if (fi->fib_priority &&
1614 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1615 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001616 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001617 goto nla_put_failure;
1618
David S. Millerf3756b72012-04-01 20:39:02 -04001619 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001620 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001621 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (fi->fib_nhs == 1) {
David Ahernb0f60192019-04-02 14:11:56 -07001623 struct fib_nh *nh = &fi->fib_nh[0];
David Ahernecc56632019-04-23 08:48:09 -07001624 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001625
David Ahernc2364192019-04-02 14:11:57 -07001626 if (fib_nexthop_info(skb, &nh->nh_common, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001627 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001628
1629 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001630#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernb0f60192019-04-02 14:11:56 -07001631 if (nh->nh_tclassid &&
1632 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
David S. Millerf3756b72012-04-01 20:39:02 -04001633 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001634#endif
David Ahernb0f60192019-04-02 14:11:56 -07001635 } else {
1636 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001637 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001639
Johannes Berg053c0952015-01-16 22:09:00 +01001640 nlmsg_end(skb, nlh);
1641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Thomas Grafbe403ea2006-08-17 18:15:17 -07001643nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001644 nlmsg_cancel(skb, nlh);
1645 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001649 * Update FIB if:
1650 * - local address disappeared -> we must delete all the entries
1651 * referring to it.
1652 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001654int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001655{
1656 int ret = 0;
1657 unsigned int hash = fib_laddr_hashfn(local);
1658 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001659 struct net *net = dev_net(dev);
1660 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001661 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Ian Morris51456b22015-04-03 09:17:26 +01001663 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001664 return 0;
1665
Sasha Levinb67bfe02013-02-27 17:06:00 -08001666 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001667 if (!net_eq(fi->fib_net, net) ||
1668 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001669 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001670 if (fi->fib_prefsrc == local) {
1671 fi->fib_flags |= RTNH_F_DEAD;
1672 ret++;
1673 }
1674 }
1675 return ret;
1676}
1677
David Ahernb75ed8b2019-03-27 20:53:55 -07001678static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001679 enum fib_event_type event_type)
1680{
David Ahernb75ed8b2019-03-27 20:53:55 -07001681 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001682 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001683 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001684 };
1685
1686 switch (event_type) {
1687 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001688 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001689 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001690 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001691 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001692 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001693 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001694 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001695 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1696 (nh->fib_nh_flags & RTNH_F_DEAD))
1697 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001698 event_type, &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001699 default:
1700 break;
1701 }
1702
1703 return NOTIFY_DONE;
1704}
1705
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001706/* Update the PMTU of exceptions when:
1707 * - the new MTU of the first hop becomes smaller than the PMTU
1708 * - the old MTU was the same as the PMTU, and it limited discovery of
1709 * larger MTUs on the path. With that limit raised, we can now
1710 * discover larger MTUs
1711 * A special case is locked exceptions, for which the PMTU is smaller
1712 * than the minimal accepted PMTU:
1713 * - if the new MTU is greater than the PMTU, don't make any change
1714 * - otherwise, unlock and set PMTU
1715 */
David Aherna5995e72019-04-30 07:45:50 -07001716static void nh_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001717{
1718 struct fnhe_hash_bucket *bucket;
1719 int i;
1720
David Aherna5995e72019-04-30 07:45:50 -07001721 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001722 if (!bucket)
1723 return;
1724
1725 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1726 struct fib_nh_exception *fnhe;
1727
1728 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1729 fnhe;
1730 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1731 if (fnhe->fnhe_mtu_locked) {
1732 if (new <= fnhe->fnhe_pmtu) {
1733 fnhe->fnhe_pmtu = new;
1734 fnhe->fnhe_mtu_locked = false;
1735 }
1736 } else if (new < fnhe->fnhe_pmtu ||
1737 orig == fnhe->fnhe_pmtu) {
1738 fnhe->fnhe_pmtu = new;
1739 }
1740 }
1741 }
1742}
1743
1744void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1745{
1746 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1747 struct hlist_head *head = &fib_info_devhash[hash];
1748 struct fib_nh *nh;
1749
1750 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001751 if (nh->fib_nh_dev == dev)
David Aherna5995e72019-04-30 07:45:50 -07001752 nh_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001753 }
1754}
1755
Julian Anastasov4f823de2015-10-30 10:23:33 +02001756/* Event force Flags Description
1757 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1758 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1759 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1760 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1761 */
1762int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 int ret = 0;
1765 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001766 struct fib_info *prev_fi = NULL;
1767 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1768 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001769 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001770
Julian Anastasov4f823de2015-10-30 10:23:33 +02001771 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 scope = -1;
1773
Sasha Levinb67bfe02013-02-27 17:06:00 -08001774 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001775 struct fib_info *fi = nh->nh_parent;
1776 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001778 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001779 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001780 continue;
1781 prev_fi = fi;
1782 dead = 0;
1783 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001784 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001785 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001786 else if (nexthop_nh->fib_nh_dev == dev &&
1787 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001788 switch (event) {
1789 case NETDEV_DOWN:
1790 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07001791 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001792 /* fall through */
1793 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07001794 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001795 break;
1796 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001797 call_fib_nh_notifiers(nexthop_nh,
1798 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001799 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001801#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001802 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07001803 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001804 dead = fi->fib_nhs;
1805 break;
1806 }
1807#endif
1808 } endfor_nexthops(fi)
1809 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001810 switch (event) {
1811 case NETDEV_DOWN:
1812 case NETDEV_UNREGISTER:
1813 fi->fib_flags |= RTNH_F_DEAD;
1814 /* fall through */
1815 case NETDEV_CHANGE:
1816 fi->fib_flags |= RTNH_F_LINKDOWN;
1817 break;
1818 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001819 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001821
1822 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
1825 return ret;
1826}
1827
David S. Miller0c838ff2011-01-31 16:16:50 -08001828/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001829static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001830{
1831 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001832 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001833 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001834 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001835 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001836 struct fib_alias *fa, *fa1 = NULL;
1837 u32 last_prio = res->fi->fib_priority;
1838 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001839
Alexander Duyck56315f92015-02-25 15:31:31 -08001840 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001841 struct fib_info *next_fi = fa->fa_info;
1842
Julian Anastasov18a912e92015-07-22 10:43:22 +03001843 if (fa->fa_slen != slen)
1844 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001845 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1846 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001847 if (fa->tb_id != tb->tb_id)
1848 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001849 if (next_fi->fib_priority > last_prio &&
1850 fa->fa_tos == last_tos) {
1851 if (last_tos)
1852 continue;
1853 break;
1854 }
1855 if (next_fi->fib_flags & RTNH_F_DEAD)
1856 continue;
1857 last_tos = fa->fa_tos;
1858 last_prio = next_fi->fib_priority;
1859
David S. Miller37e826c2011-03-24 18:06:47 -07001860 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001861 fa->fa_type != RTN_UNICAST)
1862 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001863 if (!next_fi->fib_nh[0].fib_nh_gw4 ||
1864 next_fi->fib_nh[0].fib_nh_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08001865 continue;
1866
1867 fib_alias_accessed(fa);
1868
Ian Morris51456b22015-04-03 09:17:26 +01001869 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001870 if (next_fi != res->fi)
1871 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001872 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001873 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001874 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001875 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001876 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001877 goto out;
1878 }
1879 fi = next_fi;
1880 order++;
1881 }
1882
Ian Morris51456b22015-04-03 09:17:26 +01001883 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001884 if (fa1)
1885 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001886 goto out;
1887 }
1888
1889 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001890 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001891 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001892 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001893 goto out;
1894 }
1895
1896 if (last_idx >= 0)
1897 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001898 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001899out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001900 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001904 * Dead device goes up. We wake up dead nexthops.
1905 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
David Ahernecc56632019-04-23 08:48:09 -07001907int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct fib_info *prev_fi;
1910 unsigned int hash;
1911 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct fib_nh *nh;
1913 int ret;
1914
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001915 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 0;
1917
Julian Anastasovc9b32922015-10-30 10:23:34 +02001918 if (nh_flags & RTNH_F_DEAD) {
1919 unsigned int flags = dev_get_flags(dev);
1920
1921 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1922 nh_flags |= RTNH_F_LINKDOWN;
1923 }
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 prev_fi = NULL;
1926 hash = fib_devindex_hashfn(dev->ifindex);
1927 head = &fib_info_devhash[hash];
1928 ret = 0;
1929
Sasha Levinb67bfe02013-02-27 17:06:00 -08001930 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct fib_info *fi = nh->nh_parent;
1932 int alive;
1933
1934 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001935 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 continue;
1937
1938 prev_fi = fi;
1939 alive = 0;
1940 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001941 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 alive++;
1943 continue;
1944 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001945 if (!nexthop_nh->fib_nh_dev ||
1946 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001948 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08001949 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 continue;
1951 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001952 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001953 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 } endfor_nexthops(fi)
1955
1956 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001957 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 ret++;
1959 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001960
1961 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963
1964 return ret;
1965}
1966
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001967#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001968static bool fib_good_nh(const struct fib_nh *nh)
1969{
1970 int state = NUD_REACHABLE;
1971
David Ahernb75ed8b2019-03-27 20:53:55 -07001972 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07001973 struct neighbour *n;
1974
1975 rcu_read_lock_bh();
1976
David Ahern1a38c432019-04-05 16:30:38 -07001977 if (likely(nh->fib_nh_gw_family == AF_INET))
1978 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1979 (__force u32)nh->fib_nh_gw4);
1980 else if (nh->fib_nh_gw_family == AF_INET6)
1981 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
1982 &nh->fib_nh_gw6);
1983 else
1984 n = NULL;
David Aherna6db4492016-04-07 07:21:00 -07001985 if (n)
1986 state = n->nud_state;
1987
1988 rcu_read_unlock_bh();
1989 }
1990
1991 return !!(state & NUD_VALID);
1992}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001993
Peter Nørlund0e884c72015-09-30 10:12:21 +02001994void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001997 struct net *net = fi->fib_net;
1998 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
David Aherneba618a2019-04-02 14:11:55 -07002000 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08002001 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07002002 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08002003 continue;
2004 if (!first) {
2005 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002006 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002007 first = true;
2008 }
2009 }
2010
David Aherneba618a2019-04-02 14:11:55 -07002011 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02002012 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Xin Long6174a302018-04-01 22:40:35 +08002014 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002015 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002016 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019#endif
David Ahern3ce58d82015-10-05 08:51:25 -07002020
2021void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002022 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07002023{
David Ahern0d876f22018-02-13 08:11:34 -08002024 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2025 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08002026
David Ahern3ce58d82015-10-05 08:51:25 -07002027#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern0d876f22018-02-13 08:11:34 -08002028 if (res->fi->fib_nhs > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08002029 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01002030
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002031 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07002032 }
2033 else
2034#endif
2035 if (!res->prefixlen &&
2036 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08002037 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07002038 fib_select_default(fl4, res);
2039
David Ahern0d876f22018-02-13 08:11:34 -08002040check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07002041 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07002042 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07002043}