blob: 45619c005b8dddd7ccd5c7029efa4ed69b6ce1de [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * IPv4 Forwarding Information Base: semantics.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/bitops.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/jiffies.h>
17#include <linux/mm.h>
18#include <linux/string.h>
19#include <linux/socket.h>
20#include <linux/sockios.h>
21#include <linux/errno.h>
22#include <linux/in.h>
23#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
26#include <linux/if_arp.h>
27#include <linux/proc_fs.h>
28#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
David Ahernc3ab2b42017-05-21 10:12:03 -060031#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020033#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/ip.h>
35#include <net/protocol.h>
36#include <net/route.h>
37#include <net/tcp.h>
38#include <net/sock.h>
39#include <net/ip_fib.h>
David Ahern717a8f52019-04-05 16:30:32 -070040#include <net/ip6_fib.h>
David Ahern5481d732019-06-03 20:19:49 -070041#include <net/nexthop.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070042#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070043#include <net/rtnh.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020044#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020045#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070046#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include "fib_lookup.h"
49
Stephen Hemminger832b4c52006-08-29 16:48:09 -070050static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct hlist_head *fib_info_hash;
52static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080053static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned int fib_info_cnt;
55
56#define DEVINDEX_HASHBITS 8
57#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
58static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
59
David Aherndcb1ecb2019-06-03 20:19:50 -070060/* for_nexthops and change_nexthops only used when nexthop object
61 * is not set in a fib_info. The logic within can reference fib_nh.
62 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#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; \
David Ahern5481d732019-06-03 20:19:49 -070068 nhsel < fib_info_num_path((fi)); \
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000069 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); \
David Ahern5481d732019-06-03 20:19:49 -070074 nhsel < fib_info_num_path((fi)); \
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000075 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{
Eric Dumazete44b14e2021-12-06 17:30:32 -0800211 dev_put_track(nhc->nhc_dev, &nhc->nhc_dev_tracker);
David Ahern979e2762019-03-27 20:53:58 -0700212 lwtstate_put(nhc->nhc_lwtstate);
David Ahern0f457a32019-04-30 07:45:48 -0700213 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
214 rt_fibinfo_free(&nhc->nhc_rth_input);
David Aherna5995e72019-04-30 07:45:50 -0700215 free_nh_exceptions(nhc);
David Ahern979e2762019-03-27 20:53:58 -0700216}
217EXPORT_SYMBOL_GPL(fib_nh_common_release);
218
David Ahernfaa041a2019-03-27 20:53:49 -0700219void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
220{
221#ifdef CONFIG_IP_ROUTE_CLASSID
222 if (fib_nh->nh_tclassid)
Eric Dumazet213f5f82021-12-01 18:26:35 -0800223 atomic_dec(&net->ipv4.fib_num_tclassid_users);
David Ahernfaa041a2019-03-27 20:53:49 -0700224#endif
David Ahern979e2762019-03-27 20:53:58 -0700225 fib_nh_common_release(&fib_nh->nh_common);
David Ahernfaa041a2019-03-27 20:53:49 -0700226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000229static void free_fib_info_rcu(struct rcu_head *head)
230{
231 struct fib_info *fi = container_of(head, struct fib_info, rcu);
232
David Ahern4c7e8082019-06-03 20:19:51 -0700233 if (fi->nh) {
234 nexthop_put(fi->nh);
235 } else {
236 change_nexthops(fi) {
237 fib_nh_release(fi->fib_net, nexthop_nh);
238 } endfor_nexthops(fi);
239 }
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000240
David Aherncc5f0eb2018-10-04 20:07:52 -0700241 ip_fib_metrics_put(fi->fib_metrics);
242
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000243 kfree(fi);
244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246void free_fib_info(struct fib_info *fi)
247{
248 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000249 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return;
251 }
David Ahernfaa041a2019-03-27 20:53:49 -0700252
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000253 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100255EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257void fib_release_info(struct fib_info *fi)
258{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700259 spin_lock_bh(&fib_info_lock);
Yajun Deng79976892021-07-29 15:13:50 +0800260 if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 hlist_del(&fi->fib_hash);
Eric Dumazet0a6e6b32022-01-16 01:02:20 -0800262
263 /* Paired with READ_ONCE() in fib_create_info(). */
264 WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (fi->fib_prefsrc)
267 hlist_del(&fi->fib_lhash);
David Ahern4c7e8082019-06-03 20:19:51 -0700268 if (fi->nh) {
269 list_del(&fi->nh_list);
270 } else {
271 change_nexthops(fi) {
272 if (!nexthop_nh->fib_nh_dev)
273 continue;
274 hlist_del(&nexthop_nh->nh_hash);
275 } endfor_nexthops(fi)
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 fi->fib_dead = 1;
278 fib_info_put(fi);
279 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700280 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
David Ahern5481d732019-06-03 20:19:49 -0700283static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
David Ahern5481d732019-06-03 20:19:49 -0700285 const struct fib_nh *onh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
David Ahern4c7e8082019-06-03 20:19:51 -0700287 if (fi->nh || ofi->nh)
288 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
289
290 if (ofi->fib_nhs == 0)
291 return 0;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 for_nexthops(fi) {
David Ahern5481d732019-06-03 20:19:49 -0700294 onh = fib_info_nh(ofi, nhsel);
295
David Ahernb75ed8b2019-03-27 20:53:55 -0700296 if (nh->fib_nh_oif != onh->fib_nh_oif ||
David Aherna4ea5d42019-04-05 16:30:30 -0700297 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
David Ahernb75ed8b2019-03-27 20:53:55 -0700298 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700300 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100302#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 nh->nh_tclassid != onh->nh_tclassid ||
304#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700305 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
306 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -1;
David Aherna4ea5d42019-04-05 16:30:30 -0700308
309 if (nh->fib_nh_gw_family == AF_INET &&
310 nh->fib_nh_gw4 != onh->fib_nh_gw4)
311 return -1;
312
313 if (nh->fib_nh_gw_family == AF_INET6 &&
314 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
315 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 } endfor_nexthops(fi);
317 return 0;
318}
319
David S. Miller88ebc722008-01-12 21:49:01 -0800320static inline unsigned int fib_devindex_hashfn(unsigned int val)
321{
322 unsigned int mask = DEVINDEX_HASHSIZE - 1;
323
324 return (val ^
325 (val >> DEVINDEX_HASHBITS) ^
326 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
327}
328
David Ahern6c48ea52019-06-08 14:53:33 -0700329static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
330 u32 prefsrc, u32 priority)
331{
332 unsigned int val = init_val;
333
334 val ^= (protocol << 8) | scope;
335 val ^= prefsrc;
336 val ^= priority;
337
338 return val;
339}
340
341static unsigned int fib_info_hashfn_result(unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
David S. Miller123b9732011-02-01 15:34:21 -0800343 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
David Ahern6c48ea52019-06-08 14:53:33 -0700345 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
346}
347
348static inline unsigned int fib_info_hashfn(struct fib_info *fi)
349{
350 unsigned int val;
351
352 val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
353 fi->fib_scope, (__force u32)fi->fib_prefsrc,
354 fi->fib_priority);
David Ahern4c7e8082019-06-03 20:19:51 -0700355
356 if (fi->nh) {
357 val ^= fib_devindex_hashfn(fi->nh->id);
358 } else {
359 for_nexthops(fi) {
360 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
361 } endfor_nexthops(fi)
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David Ahern6c48ea52019-06-08 14:53:33 -0700364 return fib_info_hashfn_result(val);
365}
366
367/* no metrics, only nexthop id */
368static struct fib_info *fib_find_info_nh(struct net *net,
369 const struct fib_config *cfg)
370{
371 struct hlist_head *head;
372 struct fib_info *fi;
373 unsigned int hash;
374
375 hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
376 cfg->fc_protocol, cfg->fc_scope,
377 (__force u32)cfg->fc_prefsrc,
378 cfg->fc_priority);
379 hash = fib_info_hashfn_result(hash);
380 head = &fib_info_hash[hash];
381
382 hlist_for_each_entry(fi, head, fib_hash) {
383 if (!net_eq(fi->fib_net, net))
384 continue;
385 if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
386 continue;
387 if (cfg->fc_protocol == fi->fib_protocol &&
388 cfg->fc_scope == fi->fib_scope &&
389 cfg->fc_prefsrc == fi->fib_prefsrc &&
390 cfg->fc_priority == fi->fib_priority &&
391 cfg->fc_type == fi->fib_type &&
392 cfg->fc_table == fi->fib_tb_id &&
393 !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
394 return fi;
395 }
396
397 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
David Ahern5481d732019-06-03 20:19:49 -0700400static struct fib_info *fib_find_info(struct fib_info *nfi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct fib_info *fi;
404 unsigned int hash;
405
406 hash = fib_info_hashfn(nfi);
407 head = &fib_info_hash[hash];
408
Sasha Levinb67bfe02013-02-27 17:06:00 -0800409 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800410 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800411 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (fi->fib_nhs != nfi->fib_nhs)
413 continue;
414 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700415 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 nfi->fib_prefsrc == fi->fib_prefsrc &&
417 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000418 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000420 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400421 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
David Ahern4c7e8082019-06-03 20:19:51 -0700422 nh_comp(fi, nfi) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return fi;
424 }
425
426 return NULL;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000430 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
Al Virod878e72e2006-09-26 22:18:13 -0700432int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct fib_nh *nh;
436 unsigned int hash;
437
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700438 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 hash = fib_devindex_hashfn(dev->ifindex);
441 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800442 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700443 if (nh->fib_nh_dev == dev &&
444 nh->fib_nh_gw4 == gw &&
445 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700446 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448 }
449 }
450
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700451 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return -1;
454}
455
Amit Cohen1e7bdec2021-02-01 21:47:51 +0200456size_t fib_nlmsg_size(struct fib_info *fi)
Thomas Graf339bf982006-11-10 14:10:15 -0800457{
458 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
459 + nla_total_size(4) /* RTA_TABLE */
460 + nla_total_size(4) /* RTA_DST */
461 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100462 + nla_total_size(4) /* RTA_PREFSRC */
463 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
David Ahern5481d732019-06-03 20:19:49 -0700464 unsigned int nhs = fib_info_num_path(fi);
Thomas Graf339bf982006-11-10 14:10:15 -0800465
466 /* space for nested metrics */
467 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
468
David Ahern4c7e8082019-06-03 20:19:51 -0700469 if (fi->nh)
470 payload += nla_total_size(4); /* RTA_NH_ID */
471
David Ahern5481d732019-06-03 20:19:49 -0700472 if (nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200473 size_t nh_encapsize = 0;
David Ahern5481d732019-06-03 20:19:49 -0700474 /* Also handles the special case nhs == 1 */
Thomas Graf339bf982006-11-10 14:10:15 -0800475
476 /* each nexthop is packed in an attribute */
477 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
David Aherndcb1ecb2019-06-03 20:19:50 -0700478 unsigned int i;
Thomas Graf339bf982006-11-10 14:10:15 -0800479
480 /* may contain flow and gateway attribute */
481 nhsize += 2 * nla_total_size(4);
482
Roopa Prabhu571e7222015-07-21 10:43:47 +0200483 /* grab encap info */
David Aherndcb1ecb2019-06-03 20:19:50 -0700484 for (i = 0; i < fib_info_num_path(fi); i++) {
485 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
486
487 if (nhc->nhc_lwtstate) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200488 /* RTA_ENCAP_TYPE */
489 nh_encapsize += lwtunnel_get_encap_size(
David Aherndcb1ecb2019-06-03 20:19:50 -0700490 nhc->nhc_lwtstate);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200491 /* RTA_ENCAP */
492 nh_encapsize += nla_total_size(2);
493 }
David Aherndcb1ecb2019-06-03 20:19:50 -0700494 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200495
Thomas Graf339bf982006-11-10 14:10:15 -0800496 /* all nexthops are packed in a nested attribute */
David Ahern5481d732019-06-03 20:19:49 -0700497 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200498
Thomas Graf339bf982006-11-10 14:10:15 -0800499 }
500
501 return payload;
502}
503
Al Viro81f7bf62006-09-27 18:40:00 -0700504void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700505 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700506 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200508 struct fib_rt_info fri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700510 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700511 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Thomas Graf339bf982006-11-10 14:10:15 -0800513 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100514 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700515 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200517 fri.fi = fa->fa_info;
518 fri.tb_id = tb_id;
519 fri.dst = key;
520 fri.dst_len = dst_len;
521 fri.tos = fa->fa_tos;
522 fri.type = fa->fa_type;
Ido Schimmel90b93f12020-01-14 13:23:11 +0200523 fri.offload = fa->offload;
524 fri.trap = fa->trap;
Amit Cohen36c51002021-02-07 10:22:50 +0200525 fri.offload_failed = fa->offload_failed;
Ido Schimmel1e301fd2020-01-14 13:23:10 +0200526 err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800527 if (err < 0) {
528 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
529 WARN_ON(err == -EMSGSIZE);
530 kfree_skb(skb);
531 goto errout;
532 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000533 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800534 info->nlh, GFP_KERNEL);
535 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700536errout:
537 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800538 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800541static int fib_detect_death(struct fib_info *fi, int order,
542 struct fib_info **last_resort, int *last_idx,
543 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
David Ahern619d1822019-04-05 16:30:37 -0700545 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct neighbour *n;
547 int state = NUD_NONE;
548
David Ahern619d1822019-04-05 16:30:37 -0700549 if (likely(nhc->nhc_gw_family == AF_INET))
550 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
551 else if (nhc->nhc_gw_family == AF_INET6)
552 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
553 nhc->nhc_dev);
554 else
555 n = NULL;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (n) {
558 state = n->nud_state;
559 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300560 } else {
561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800563 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000565 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000567 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300568 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 *last_resort = fi;
570 *last_idx = order;
571 }
572 return 1;
573}
574
Alexander Aringfaee6762020-03-27 18:00:21 -0400575int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
576 struct nlattr *encap, u16 encap_type,
577 void *cfg, gfp_t gfp_flags,
David Ahern979e2762019-03-27 20:53:58 -0700578 struct netlink_ext_ack *extack)
579{
David Ahern0f457a32019-04-30 07:45:48 -0700580 int err;
581
582 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
583 gfp_flags);
584 if (!nhc->nhc_pcpu_rth_output)
585 return -ENOMEM;
586
David Ahern979e2762019-03-27 20:53:58 -0700587 if (encap) {
588 struct lwtunnel_state *lwtstate;
David Ahern979e2762019-03-27 20:53:58 -0700589
590 if (encap_type == LWTUNNEL_ENCAP_NONE) {
591 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
David Ahern0f457a32019-04-30 07:45:48 -0700592 err = -EINVAL;
593 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700594 }
Alexander Aringfaee6762020-03-27 18:00:21 -0400595 err = lwtunnel_build_state(net, encap_type, encap,
596 nhc->nhc_family, cfg, &lwtstate,
597 extack);
David Ahern979e2762019-03-27 20:53:58 -0700598 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700599 goto lwt_failure;
David Ahern979e2762019-03-27 20:53:58 -0700600
601 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
602 }
603
604 return 0;
David Ahern0f457a32019-04-30 07:45:48 -0700605
606lwt_failure:
607 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
608 nhc->nhc_pcpu_rth_output = NULL;
609 return err;
David Ahern979e2762019-03-27 20:53:58 -0700610}
611EXPORT_SYMBOL_GPL(fib_nh_common_init);
612
David Aherne4516ef2019-03-27 20:53:48 -0700613int fib_nh_init(struct net *net, struct fib_nh *nh,
614 struct fib_config *cfg, int nh_weight,
615 struct netlink_ext_ack *extack)
616{
David Ahern0f457a32019-04-30 07:45:48 -0700617 int err;
David Aherne4516ef2019-03-27 20:53:48 -0700618
David Ahernf1741732019-03-27 20:53:57 -0700619 nh->fib_nh_family = AF_INET;
620
Alexander Aringfaee6762020-03-27 18:00:21 -0400621 err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
David Ahern979e2762019-03-27 20:53:58 -0700622 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
623 if (err)
David Ahern0f457a32019-04-30 07:45:48 -0700624 return err;
David Aherne4516ef2019-03-27 20:53:48 -0700625
David Ahernb75ed8b2019-03-27 20:53:55 -0700626 nh->fib_nh_oif = cfg->fc_oif;
David Aherna4ea5d42019-04-05 16:30:30 -0700627 nh->fib_nh_gw_family = cfg->fc_gw_family;
628 if (cfg->fc_gw_family == AF_INET)
David Ahernf35b7942019-04-05 16:30:28 -0700629 nh->fib_nh_gw4 = cfg->fc_gw4;
David Aherna4ea5d42019-04-05 16:30:30 -0700630 else if (cfg->fc_gw_family == AF_INET6)
631 nh->fib_nh_gw6 = cfg->fc_gw6;
632
David Ahernb75ed8b2019-03-27 20:53:55 -0700633 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700634
635#ifdef CONFIG_IP_ROUTE_CLASSID
636 nh->nh_tclassid = cfg->fc_flow;
637 if (nh->nh_tclassid)
Eric Dumazet213f5f82021-12-01 18:26:35 -0800638 atomic_inc(&net->ipv4.fib_num_tclassid_users);
David Aherne4516ef2019-03-27 20:53:48 -0700639#endif
640#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700641 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700642#endif
643 return 0;
David Aherne4516ef2019-03-27 20:53:48 -0700644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#ifdef CONFIG_IP_ROUTE_MULTIPATH
647
David Ahern6d8422a12017-05-21 10:12:02 -0600648static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
649 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Thomas Graf4e902c52006-08-17 18:14:52 -0700653 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700655 rtnh = rtnh_next(rtnh, &remaining);
656 }
657
658 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600659 if (remaining > 0) {
660 NL_SET_ERR_MSG(extack,
661 "Invalid nexthop configuration - extra data after nexthops");
662 nhs = 0;
663 }
664
665 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
David Ahern7a3429b2021-12-30 17:36:31 -0700668static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
669 struct netlink_ext_ack *extack)
670{
671 if (nla_len(nla) < sizeof(*gw)) {
672 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
673 return -EINVAL;
674 }
675
676 *gw = nla_get_in_addr(nla);
677
678 return 0;
679}
680
David Ahern4c7e8082019-06-03 20:19:51 -0700681/* only called when fib_nh is integrated into fib_info */
Thomas Graf4e902c52006-08-17 18:14:52 -0700682static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600683 int remaining, struct fib_config *cfg,
684 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
David Aherne4516ef2019-03-27 20:53:48 -0700686 struct net *net = fi->fib_net;
687 struct fib_config fib_cfg;
David Ahern5481d732019-06-03 20:19:49 -0700688 struct fib_nh *nh;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200689 int ret;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700692 int attrlen;
693
David Aherne4516ef2019-03-27 20:53:48 -0700694 memset(&fib_cfg, 0, sizeof(fib_cfg));
695
David Ahernc3ab2b42017-05-21 10:12:03 -0600696 if (!rtnh_ok(rtnh, remaining)) {
697 NL_SET_ERR_MSG(extack,
698 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600700 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700701
David Ahernc3ab2b42017-05-21 10:12:03 -0600702 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
703 NL_SET_ERR_MSG(extack,
704 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300705 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600706 }
Julian Anastasov80610222016-07-10 21:11:55 +0300707
David Aherne4516ef2019-03-27 20:53:48 -0700708 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
709 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700710
711 attrlen = rtnh_attrlen(rtnh);
712 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700713 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700714
715 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700716 nlav = nla_find(attrs, attrlen, RTA_VIA);
717 if (nla && nlav) {
718 NL_SET_ERR_MSG(extack,
719 "Nexthop configuration can not contain both GATEWAY and VIA");
720 return -EINVAL;
721 }
David Ahernf35b7942019-04-05 16:30:28 -0700722 if (nla) {
David Ahern7a3429b2021-12-30 17:36:31 -0700723 ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
724 extack);
725 if (ret)
726 goto errout;
727
David Ahernd73f80f2019-04-10 10:05:51 -0700728 if (fib_cfg.fc_gw4)
729 fib_cfg.fc_gw_family = AF_INET;
David Ahernd1566262019-04-05 16:30:40 -0700730 } else if (nlav) {
731 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
732 if (ret)
733 goto errout;
David Ahernf35b7942019-04-05 16:30:28 -0700734 }
David Aherne4516ef2019-03-27 20:53:48 -0700735
Thomas Graf4e902c52006-08-17 18:14:52 -0700736 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Ahern664b9c42021-12-30 17:36:32 -0700737 if (nla) {
738 if (nla_len(nla) < sizeof(u32)) {
739 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
740 return -EINVAL;
741 }
David Aherne4516ef2019-03-27 20:53:48 -0700742 fib_cfg.fc_flow = nla_get_u32(nla);
David Ahern664b9c42021-12-30 17:36:32 -0700743 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200744
David Aherne4516ef2019-03-27 20:53:48 -0700745 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
David Ahern8bda81a2021-12-30 17:36:35 -0700746 /* RTA_ENCAP_TYPE length checked in
747 * lwtunnel_valid_encap_type_attr
748 */
David Aherne4516ef2019-03-27 20:53:48 -0700749 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
750 if (nla)
751 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700753
David Aherne4516ef2019-03-27 20:53:48 -0700754 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
755 rtnh->rtnh_hops + 1, extack);
756 if (ret)
757 goto errout;
758
Thomas Graf4e902c52006-08-17 18:14:52 -0700759 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700761
Roopa Prabhu571e7222015-07-21 10:43:47 +0200762 ret = -EINVAL;
David Ahern5481d732019-06-03 20:19:49 -0700763 nh = fib_info_nh(fi, 0);
764 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700765 NL_SET_ERR_MSG(extack,
766 "Nexthop device index does not match RTA_OIF");
767 goto errout;
768 }
David Ahernf35b7942019-04-05 16:30:28 -0700769 if (cfg->fc_gw_family) {
David Ahern5481d732019-06-03 20:19:49 -0700770 if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
David Ahernf35b7942019-04-05 16:30:28 -0700771 (cfg->fc_gw_family == AF_INET &&
David Ahern5481d732019-06-03 20:19:49 -0700772 nh->fib_nh_gw4 != cfg->fc_gw4) ||
David Aherna4ea5d42019-04-05 16:30:30 -0700773 (cfg->fc_gw_family == AF_INET6 &&
David Ahern5481d732019-06-03 20:19:49 -0700774 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
David Ahernf35b7942019-04-05 16:30:28 -0700775 NL_SET_ERR_MSG(extack,
David Aherna4ea5d42019-04-05 16:30:30 -0700776 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
David Ahernf35b7942019-04-05 16:30:28 -0700777 goto errout;
778 }
David Aherne4516ef2019-03-27 20:53:48 -0700779 }
780#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahern5481d732019-06-03 20:19:49 -0700781 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
David Aherne4516ef2019-03-27 20:53:48 -0700782 NL_SET_ERR_MSG(extack,
783 "Nexthop class id does not match RTA_FLOW");
784 goto errout;
785 }
786#endif
787 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200788errout:
789 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
David Ahern4c7e8082019-06-03 20:19:51 -0700792/* only called when fib_nh is integrated into fib_info */
Peter Nørlund0e884c72015-09-30 10:12:21 +0200793static void fib_rebalance(struct fib_info *fi)
794{
795 int total;
796 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200797
David Ahern5481d732019-06-03 20:19:49 -0700798 if (fib_info_num_path(fi) < 2)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200799 return;
800
801 total = 0;
802 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700803 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200804 continue;
805
David Ahernb75ed8b2019-03-27 20:53:55 -0700806 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
807 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200808 continue;
809
David Ahernb75ed8b2019-03-27 20:53:55 -0700810 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200811 } endfor_nexthops(fi);
812
813 w = 0;
814 change_nexthops(fi) {
815 int upper_bound;
816
David Ahernb75ed8b2019-03-27 20:53:55 -0700817 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200818 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700819 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
820 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200821 upper_bound = -1;
822 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700823 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200824 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
825 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200826 }
827
David Ahernb75ed8b2019-03-27 20:53:55 -0700828 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200829 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200830}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200831#else /* CONFIG_IP_ROUTE_MULTIPATH */
832
David Ahern8373c6c2019-03-27 20:53:46 -0700833static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
834 int remaining, struct fib_config *cfg,
835 struct netlink_ext_ack *extack)
836{
837 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
838
839 return -EINVAL;
840}
841
Peter Nørlund0e884c72015-09-30 10:12:21 +0200842#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200843
844#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Alexander Aringfaee6762020-03-27 18:00:21 -0400846static int fib_encap_match(struct net *net, u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800847 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800848 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600849 const struct fib_config *cfg,
850 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200851{
852 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200853 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200854
855 if (encap_type == LWTUNNEL_ENCAP_NONE)
856 return 0;
857
Alexander Aringfaee6762020-03-27 18:00:21 -0400858 ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
David Ahern9ae28722017-05-27 16:19:28 -0600859 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200860 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700861 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200862 lwtstate_free(lwtstate);
863 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200864
Jiri Bencdf383e62015-08-18 18:41:13 +0200865 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200866}
867
Alexander Aringfaee6762020-03-27 18:00:21 -0400868int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
David Ahern9ae28722017-05-27 16:19:28 -0600869 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700872 struct rtnexthop *rtnh;
873 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#endif
875
Thomas Graf4e902c52006-08-17 18:14:52 -0700876 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 1;
878
David Ahern493ced12019-06-08 14:53:32 -0700879 if (cfg->fc_nh_id) {
880 if (fi->nh && cfg->fc_nh_id == fi->nh->id)
881 return 0;
882 return 1;
883 }
884
David Ahernf35b7942019-04-05 16:30:28 -0700885 if (cfg->fc_oif || cfg->fc_gw_family) {
David Ahern5481d732019-06-03 20:19:49 -0700886 struct fib_nh *nh = fib_info_nh(fi, 0);
887
Roopa Prabhu571e7222015-07-21 10:43:47 +0200888 if (cfg->fc_encap) {
Alexander Aringfaee6762020-03-27 18:00:21 -0400889 if (fib_encap_match(net, cfg->fc_encap_type,
890 cfg->fc_encap, nh, cfg, extack))
David Ahern9ae28722017-05-27 16:19:28 -0600891 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200892 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100893#ifdef CONFIG_IP_ROUTE_CLASSID
894 if (cfg->fc_flow &&
David Ahern5481d732019-06-03 20:19:49 -0700895 cfg->fc_flow != nh->nh_tclassid)
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100896 return 1;
897#endif
David Ahern5481d732019-06-03 20:19:49 -0700898 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
David Ahernf35b7942019-04-05 16:30:28 -0700899 (cfg->fc_gw_family &&
David Ahern5481d732019-06-03 20:19:49 -0700900 cfg->fc_gw_family != nh->fib_nh_gw_family))
David Ahernf35b7942019-04-05 16:30:28 -0700901 return 1;
902
903 if (cfg->fc_gw_family == AF_INET &&
David Ahern5481d732019-06-03 20:19:49 -0700904 cfg->fc_gw4 != nh->fib_nh_gw4)
David Ahernf35b7942019-04-05 16:30:28 -0700905 return 1;
906
David Aherna4ea5d42019-04-05 16:30:30 -0700907 if (cfg->fc_gw_family == AF_INET6 &&
David Ahern5481d732019-06-03 20:19:49 -0700908 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
David Aherna4ea5d42019-04-05 16:30:30 -0700909 return 1;
910
David Ahernf35b7942019-04-05 16:30:28 -0700911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913
914#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100915 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700917
918 rtnh = cfg->fc_mp;
919 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700922 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Thomas Graf4e902c52006-08-17 18:14:52 -0700924 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700926
David Ahernb75ed8b2019-03-27 20:53:55 -0700927 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700929
930 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200931 if (attrlen > 0) {
David Ahernd1566262019-04-05 16:30:40 -0700932 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
David Ahern7a3429b2021-12-30 17:36:31 -0700933 int err;
Thomas Graf4e902c52006-08-17 18:14:52 -0700934
935 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernd1566262019-04-05 16:30:40 -0700936 nlav = nla_find(attrs, attrlen, RTA_VIA);
937 if (nla && nlav) {
938 NL_SET_ERR_MSG(extack,
939 "Nexthop configuration can not contain both GATEWAY and VIA");
940 return -EINVAL;
941 }
942
943 if (nla) {
David Ahern7a3429b2021-12-30 17:36:31 -0700944 __be32 gw;
945
946 err = fib_gw_from_attr(&gw, nla, extack);
947 if (err)
948 return err;
949
David Ahernd1566262019-04-05 16:30:40 -0700950 if (nh->fib_nh_gw_family != AF_INET ||
David Ahern7a3429b2021-12-30 17:36:31 -0700951 gw != nh->fib_nh_gw4)
David Ahernd1566262019-04-05 16:30:40 -0700952 return 1;
953 } else if (nlav) {
954 struct fib_config cfg2;
David Ahernd1566262019-04-05 16:30:40 -0700955
956 err = fib_gw_from_via(&cfg2, nlav, extack);
957 if (err)
958 return err;
959
960 switch (nh->fib_nh_gw_family) {
961 case AF_INET:
962 if (cfg2.fc_gw_family != AF_INET ||
963 cfg2.fc_gw4 != nh->fib_nh_gw4)
964 return 1;
965 break;
966 case AF_INET6:
967 if (cfg2.fc_gw_family != AF_INET6 ||
968 ipv6_addr_cmp(&cfg2.fc_gw6,
969 &nh->fib_nh_gw6))
970 return 1;
971 break;
972 }
973 }
974
Patrick McHardyc7066f72011-01-14 13:36:42 +0100975#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700976 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Ahern664b9c42021-12-30 17:36:32 -0700977 if (nla) {
978 if (nla_len(nla) < sizeof(u32)) {
979 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
980 return -EINVAL;
981 }
982 if (nla_get_u32(nla) != nh->nh_tclassid)
983 return 1;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985#endif
986 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700987
988 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 } endfor_nexthops(fi);
990#endif
991 return 0;
992}
993
Xin Long5f9ae3d2017-08-23 10:07:26 +0800994bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
995{
996 struct nlattr *nla;
997 int remaining;
998
999 if (!cfg->fc_mx)
1000 return true;
1001
1002 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1003 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +01001004 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +08001005
1006 if (!type)
1007 continue;
1008 if (type > RTAX_MAX)
1009 return false;
1010
1011 if (type == RTAX_CC_ALGO) {
1012 char tmp[TCP_CA_NAME_MAX];
1013 bool ecn_ca = false;
1014
Francis Laniel872f6902020-11-15 18:08:06 +01001015 nla_strscpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -08001016 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +08001017 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -07001018 if (nla_len(nla) != sizeof(u32))
1019 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +08001020 val = nla_get_u32(nla);
1021 }
1022
Phil Sutterd03a4552017-12-19 15:17:13 +01001023 fi_val = fi->fib_metrics->metrics[type - 1];
1024 if (type == RTAX_FEATURES)
1025 fi_val &= ~DST_FEATURE_ECN_CA;
1026
1027 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +08001028 return false;
1029 }
1030
1031 return true;
1032}
1033
David Ahern717a8f52019-04-05 16:30:32 -07001034static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1035 u32 table, struct netlink_ext_ack *extack)
1036{
1037 struct fib6_config cfg = {
1038 .fc_table = table,
1039 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1040 .fc_ifindex = nh->fib_nh_oif,
1041 .fc_gateway = nh->fib_nh_gw6,
1042 };
1043 struct fib6_nh fib6_nh = {};
1044 int err;
1045
1046 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1047 if (!err) {
1048 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001049 dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_KERNEL);
David Ahern717a8f52019-04-05 16:30:32 -07001050 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1051 nh->fib_nh_scope = RT_SCOPE_LINK;
1052
1053 ipv6_stub->fib6_nh_release(&fib6_nh);
1054 }
1055
1056 return err;
1057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001060 * Picture
1061 * -------
1062 *
1063 * Semantics of nexthop is very messy by historical reasons.
1064 * We have to take into account, that:
1065 * a) gateway can be actually local interface address,
1066 * so that gatewayed route is direct.
1067 * b) gateway must be on-link address, possibly
1068 * described not by an ifaddr, but also by a direct route.
1069 * c) If both gateway and interface are specified, they should not
1070 * contradict.
1071 * d) If we use tunnel routes, gateway could be not on-link.
1072 *
1073 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1074 * results in pretty ugly and hairy code with obscure logic.
1075 *
1076 * I chose to generalized it instead, so that the size
1077 * of code does not increase practically, but it becomes
1078 * much more general.
1079 * Every prefix is assigned a "scope" value: "host" is local address,
1080 * "link" is direct route,
1081 * [ ... "site" ... "interior" ... ]
1082 * and "universe" is true gateway route with global meaning.
1083 *
1084 * Every prefix refers to a set of "nexthop"s (gw, oif),
1085 * where gw must have narrower scope. This recursion stops
1086 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1087 * which means that gw is forced to be on link.
1088 *
1089 * Code is still hairy, but now it is apparently logically
1090 * consistent and very flexible. F.e. as by-product it allows
1091 * to co-exists in peace independent exterior and interior
1092 * routing processes.
1093 *
1094 * Normally it looks as following.
1095 *
1096 * {universe prefix} -> (gw, oif) [scope link]
1097 * |
1098 * |-> {link prefix} -> (gw, oif) [scope local]
1099 * |
1100 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
David Ahern448d7242019-04-05 16:30:31 -07001102static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1103 u8 scope, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001105 struct net_device *dev;
David Ahern448d7242019-04-05 16:30:31 -07001106 struct fib_result res;
Enrico Weigeltc3fee642019-06-06 16:43:17 +02001107 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
David Ahern448d7242019-04-05 16:30:31 -07001109 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1110 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
David Ahern448d7242019-04-05 16:30:31 -07001112 if (scope >= RT_SCOPE_LINK) {
1113 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1114 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
David Ahern448d7242019-04-05 16:30:31 -07001116 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1117 if (!dev) {
1118 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1119 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
David Ahern448d7242019-04-05 16:30:31 -07001121 if (!(dev->flags & IFF_UP)) {
1122 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1123 return -ENETDOWN;
1124 }
1125 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1126 if (addr_type != RTN_UNICAST) {
1127 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1128 return -EINVAL;
1129 }
1130 if (!netif_carrier_ok(dev))
1131 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1132 nh->fib_nh_dev = dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001133 dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
David Ahern448d7242019-04-05 16:30:31 -07001134 nh->fib_nh_scope = RT_SCOPE_LINK;
1135 return 0;
1136 }
1137 rcu_read_lock();
1138 {
1139 struct fib_table *tbl = NULL;
1140 struct flowi4 fl4 = {
1141 .daddr = nh->fib_nh_gw4,
1142 .flowi4_scope = scope + 1,
1143 .flowi4_oif = nh->fib_nh_oif,
1144 .flowi4_iif = LOOPBACK_IFINDEX,
1145 };
1146
1147 /* It is not necessary, but requires a bit of thinking */
1148 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1149 fl4.flowi4_scope = RT_SCOPE_LINK;
1150
guodeqing5eea3a62020-06-17 10:07:16 +08001151 if (table && table != RT_TABLE_MAIN)
David Ahern448d7242019-04-05 16:30:31 -07001152 tbl = fib_get_table(net, table);
1153
1154 if (tbl)
1155 err = fib_table_lookup(tbl, &fl4, &res,
1156 FIB_LOOKUP_IGNORE_LINKSTATE |
1157 FIB_LOOKUP_NOREF);
1158
1159 /* on error or if no table given do full lookup. This
1160 * is needed for example when nexthops are in the local
1161 * table rather than the given table
1162 */
1163 if (!tbl || err) {
1164 err = fib_lookup(net, &fl4, &res,
1165 FIB_LOOKUP_IGNORE_LINKSTATE);
1166 }
1167
1168 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001169 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -06001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
David Ahern448d7242019-04-05 16:30:31 -07001173
1174 err = -EINVAL;
1175 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1176 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1177 goto out;
1178 }
1179 nh->fib_nh_scope = res.scope;
1180 nh->fib_nh_oif = FIB_RES_OIF(res);
1181 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1182 if (!dev) {
1183 NL_SET_ERR_MSG(extack,
1184 "No egress device for nexthop gateway");
1185 goto out;
1186 }
Eric Dumazete44b14e2021-12-06 17:30:32 -08001187 dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
David Ahern448d7242019-04-05 16:30:31 -07001188 if (!netif_carrier_ok(dev))
1189 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1190 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +00001191out:
1192 rcu_read_unlock();
1193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
David Ahern448d7242019-04-05 16:30:31 -07001196static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1197 struct netlink_ext_ack *extack)
1198{
1199 struct in_device *in_dev;
1200 int err;
1201
1202 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1203 NL_SET_ERR_MSG(extack,
1204 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1205 return -EINVAL;
1206 }
1207
1208 rcu_read_lock();
1209
1210 err = -ENODEV;
1211 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1212 if (!in_dev)
1213 goto out;
1214 err = -ENETDOWN;
1215 if (!(in_dev->dev->flags & IFF_UP)) {
1216 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1217 goto out;
1218 }
1219
1220 nh->fib_nh_dev = in_dev->dev;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001221 dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
David Ahern448d7242019-04-05 16:30:31 -07001222 nh->fib_nh_scope = RT_SCOPE_HOST;
1223 if (!netif_carrier_ok(nh->fib_nh_dev))
1224 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1225 err = 0;
1226out:
1227 rcu_read_unlock();
1228 return err;
1229}
1230
David Ahernac1fab22019-05-22 12:04:43 -07001231int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1232 struct netlink_ext_ack *extack)
David Ahern448d7242019-04-05 16:30:31 -07001233{
David Ahern448d7242019-04-05 16:30:31 -07001234 int err;
1235
1236 if (nh->fib_nh_gw_family == AF_INET)
David Ahernac1fab22019-05-22 12:04:43 -07001237 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
David Ahern717a8f52019-04-05 16:30:32 -07001238 else if (nh->fib_nh_gw_family == AF_INET6)
1239 err = fib_check_nh_v6_gw(net, nh, table, extack);
David Ahern448d7242019-04-05 16:30:31 -07001240 else
1241 err = fib_check_nh_nongw(net, nh, extack);
1242
1243 return err;
1244}
1245
Al Viro81f7bf62006-09-27 18:40:00 -07001246static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
David S. Miller123b9732011-02-01 15:34:21 -08001248 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001250 return ((__force u32)val ^
1251 ((__force u32)val >> 7) ^
1252 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
David S. Miller123b9732011-02-01 15:34:21 -08001255static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +08001258 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 else
1260 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001261 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1262 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
David S. Miller123b9732011-02-01 15:34:21 -08001265static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 if (!hash)
1268 return;
1269
1270 if (bytes <= PAGE_SIZE)
1271 kfree(hash);
1272 else
1273 free_pages((unsigned long) hash, get_order(bytes));
1274}
1275
David S. Miller123b9732011-02-01 15:34:21 -08001276static void fib_info_hash_move(struct hlist_head *new_info_hash,
1277 struct hlist_head *new_laddrhash,
1278 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
David S. Millerb7656e72005-08-05 04:12:48 -07001280 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001281 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001282 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001284 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001285 old_info_hash = fib_info_hash;
1286 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001287 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 for (i = 0; i < old_size; i++) {
1290 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001291 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct fib_info *fi;
1293
Sasha Levinb67bfe02013-02-27 17:06:00 -08001294 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct hlist_head *dest;
1296 unsigned int new_hash;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 new_hash = fib_info_hashfn(fi);
1299 dest = &new_info_hash[new_hash];
1300 hlist_add_head(&fi->fib_hash, dest);
1301 }
1302 }
1303 fib_info_hash = new_info_hash;
1304
1305 for (i = 0; i < old_size; i++) {
1306 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001307 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct fib_info *fi;
1309
Sasha Levinb67bfe02013-02-27 17:06:00 -08001310 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 struct hlist_head *ldest;
1312 unsigned int new_hash;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1315 ldest = &new_laddrhash[new_hash];
1316 hlist_add_head(&fi->fib_lhash, ldest);
1317 }
1318 }
1319 fib_info_laddrhash = new_laddrhash;
1320
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001321 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001322
1323 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001324 fib_info_hash_free(old_info_hash, bytes);
1325 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
David Aherndcb1ecb2019-06-03 20:19:50 -07001328__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1329 unsigned char scope)
David S. Miller436c3b62011-03-24 17:42:21 -07001330{
David Aherndcb1ecb2019-06-03 20:19:50 -07001331 struct fib_nh *nh;
1332
1333 if (nhc->nhc_family != AF_INET)
1334 return inet_select_addr(nhc->nhc_dev, 0, scope);
1335
1336 nh = container_of(nhc, struct fib_nh, nh_common);
David Ahernc3669482019-05-22 12:04:45 -07001337 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001338 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1339
1340 return nh->nh_saddr;
1341}
1342
David Aherneba618a2019-04-02 14:11:55 -07001343__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1344{
1345 struct fib_nh_common *nhc = res->nhc;
David Aherneba618a2019-04-02 14:11:55 -07001346
1347 if (res->fi->fib_prefsrc)
1348 return res->fi->fib_prefsrc;
1349
David Aherndcb1ecb2019-06-03 20:19:50 -07001350 if (nhc->nhc_family == AF_INET) {
1351 struct fib_nh *nh;
David Aherneba618a2019-04-02 14:11:55 -07001352
David Aherndcb1ecb2019-06-03 20:19:50 -07001353 nh = container_of(nhc, struct fib_nh, nh_common);
1354 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1355 return nh->nh_saddr;
1356 }
1357
1358 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
David Aherneba618a2019-04-02 14:11:55 -07001359}
1360
David Ahern021dd3b2015-08-13 14:59:06 -06001361static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1362{
1363 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1364 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001365 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001366 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001367
1368 if (tb_id == RT_TABLE_MAIN)
1369 tb_id = RT_TABLE_LOCAL;
1370
David Aherne1b8d902015-11-03 15:59:28 -08001371 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1372 fib_prefsrc, tb_id);
1373
1374 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1375 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1376 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001377 }
David Aherne1b8d902015-11-03 15:59:28 -08001378
1379 if (rc != RTN_LOCAL)
1380 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001381 }
1382 return true;
1383}
1384
David Ahern6d8422a12017-05-21 10:12:02 -06001385struct fib_info *fib_create_info(struct fib_config *cfg,
1386 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int err;
1389 struct fib_info *fi = NULL;
David Ahern4c7e8082019-06-03 20:19:51 -07001390 struct nexthop *nh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001393 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
David S. Miller4c8237c2011-03-07 14:27:38 -08001395 if (cfg->fc_type > RTN_MAX)
1396 goto err_inval;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001399 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1400 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
David Ahernc3ab2b42017-05-21 10:12:03 -06001404 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1405 NL_SET_ERR_MSG(extack,
1406 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001407 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001408 }
Julian Anastasov80610222016-07-10 21:11:55 +03001409
David Ahern493ced12019-06-08 14:53:32 -07001410 if (cfg->fc_nh_id) {
David Ahern6c48ea52019-06-08 14:53:33 -07001411 if (!cfg->fc_mx) {
1412 fi = fib_find_info_nh(net, cfg);
1413 if (fi) {
Yajun Deng79976892021-07-29 15:13:50 +08001414 refcount_inc(&fi->fib_treeref);
David Ahern6c48ea52019-06-08 14:53:33 -07001415 return fi;
1416 }
1417 }
1418
David Ahern493ced12019-06-08 14:53:32 -07001419 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1420 if (!nh) {
1421 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1422 goto err_inval;
1423 }
1424 nhs = 0;
1425 }
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001428 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001429 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (nhs == 0)
1431 goto err_inval;
1432 }
1433#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 err = -ENOBUFS;
Eric Dumazet0a6e6b32022-01-16 01:02:20 -08001436
1437 /* Paired with WRITE_ONCE() in fib_release_info() */
1438 if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
David S. Miller123b9732011-02-01 15:34:21 -08001439 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 struct hlist_head *new_info_hash;
1441 struct hlist_head *new_laddrhash;
1442 unsigned int bytes;
1443
1444 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001445 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001447 new_info_hash = fib_info_hash_alloc(bytes);
1448 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001450 fib_info_hash_free(new_info_hash, bytes);
1451 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001452 } else
David S. Miller123b9732011-02-01 15:34:21 -08001453 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
David S. Miller123b9732011-02-01 15:34:21 -08001455 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto failure;
1457 }
1458
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001459 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001460 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001462 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001463 cfg->fc_mx_len, extack);
Enrico Weigelt88e235b2019-06-05 23:09:05 +02001464 if (IS_ERR(fi->fib_metrics)) {
David Ahern767a2212018-10-04 20:07:51 -07001465 err = PTR_ERR(fi->fib_metrics);
1466 kfree(fi);
1467 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001468 }
David Ahern767a2212018-10-04 20:07:51 -07001469
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001470 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001471 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001472 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001473 fi->fib_flags = cfg->fc_flags;
1474 fi->fib_priority = cfg->fc_priority;
1475 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001476 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001477 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 fi->fib_nhs = nhs;
David Ahern4c7e8082019-06-03 20:19:51 -07001480 if (nh) {
1481 if (!nexthop_get(nh)) {
1482 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1483 err = -EINVAL;
1484 } else {
1485 err = 0;
1486 fi->nh = nh;
1487 }
1488 } else {
1489 change_nexthops(fi) {
1490 nexthop_nh->nh_parent = fi;
1491 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
David Ahern4c7e8082019-06-03 20:19:51 -07001493 if (cfg->fc_mp)
1494 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1495 extack);
1496 else
1497 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1498 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001499
David Aherne4516ef2019-03-27 20:53:48 -07001500 if (err != 0)
1501 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Thomas Graf4e902c52006-08-17 18:14:52 -07001503 if (fib_props[cfg->fc_type].error) {
David Ahernf35b7942019-04-05 16:30:28 -07001504 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001505 NL_SET_ERR_MSG(extack,
1506 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001510 } else {
1511 switch (cfg->fc_type) {
1512 case RTN_UNICAST:
1513 case RTN_LOCAL:
1514 case RTN_BROADCAST:
1515 case RTN_ANYCAST:
1516 case RTN_MULTICAST:
1517 break;
1518 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001519 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001520 goto err_inval;
1521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
David Ahernc3ab2b42017-05-21 10:12:03 -06001524 if (cfg->fc_scope > RT_SCOPE_HOST) {
1525 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
David Ahern4c7e8082019-06-03 20:19:51 -07001529 if (fi->nh) {
1530 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1531 if (err)
1532 goto failure;
1533 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct fib_nh *nh = fi->fib_nh;
1535
1536 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001537 if (nhs != 1) {
1538 NL_SET_ERR_MSG(extack,
1539 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001540 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001541 }
David Ahernbdf00462019-04-05 16:30:26 -07001542 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001543 NL_SET_ERR_MSG(extack,
1544 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001546 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001547 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
David Ahern5481d732019-06-03 20:19:49 -07001548 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001550 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto failure;
Eric Dumazete44b14e2021-12-06 17:30:32 -08001552 netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1553 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001555 int linkdown = 0;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 change_nexthops(fi) {
David Ahernac1fab22019-05-22 12:04:43 -07001558 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1559 cfg->fc_table, cfg->fc_scope,
1560 extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001561 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001563 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001564 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001566 if (linkdown == fi->fib_nhs)
1567 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
David Ahernc3ab2b42017-05-21 10:12:03 -06001570 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1571 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001572 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
David Ahern4c7e8082019-06-03 20:19:51 -07001575 if (!fi->nh) {
1576 change_nexthops(fi) {
1577 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1578 fi->fib_scope);
1579 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1580 fi->fib_nh_is_v6 = true;
1581 } endfor_nexthops(fi)
David S. Miller1fc050a2011-03-07 20:54:48 -08001582
David Ahern4c7e8082019-06-03 20:19:51 -07001583 fib_rebalance(fi);
1584 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001587 ofi = fib_find_info(fi);
1588 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 fi->fib_dead = 1;
1590 free_fib_info(fi);
Yajun Deng79976892021-07-29 15:13:50 +08001591 refcount_inc(&ofi->fib_treeref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return ofi;
1593 }
1594
David Ahern28814cd2021-08-02 10:02:21 -06001595 refcount_set(&fi->fib_treeref, 1);
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001596 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001597 spin_lock_bh(&fib_info_lock);
Eric Dumazet0a6e6b32022-01-16 01:02:20 -08001598 fib_info_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 hlist_add_head(&fi->fib_hash,
1600 &fib_info_hash[fib_info_hashfn(fi)]);
1601 if (fi->fib_prefsrc) {
1602 struct hlist_head *head;
1603
1604 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1605 hlist_add_head(&fi->fib_lhash, head);
1606 }
David Ahern4c7e8082019-06-03 20:19:51 -07001607 if (fi->nh) {
1608 list_add(&fi->nh_list, &nh->fi_list);
1609 } else {
1610 change_nexthops(fi) {
1611 struct hlist_head *head;
1612 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
David Ahern4c7e8082019-06-03 20:19:51 -07001614 if (!nexthop_nh->fib_nh_dev)
1615 continue;
1616 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
1617 head = &fib_info_devhash[hash];
1618 hlist_add_head(&nexthop_nh->nh_hash, head);
1619 } endfor_nexthops(fi)
1620 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001621 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return fi;
1623
1624err_inval:
1625 err = -EINVAL;
1626
1627failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001628 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 fi->fib_dead = 1;
1630 free_fib_info(fi);
1631 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001632
1633 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
David Ahernc0a72072019-04-02 14:11:58 -07001636int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001637 u8 rt_family, unsigned char *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001638{
David Ahernc2364192019-04-02 14:11:57 -07001639 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001640 *flags |= RTNH_F_DEAD;
1641
David Ahernc2364192019-04-02 14:11:57 -07001642 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001643 *flags |= RTNH_F_LINKDOWN;
1644
1645 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001646 switch (nhc->nhc_family) {
1647 case AF_INET:
1648 if (ip_ignore_linkdown(nhc->nhc_dev))
1649 *flags |= RTNH_F_DEAD;
1650 break;
David Ahernc0a72072019-04-02 14:11:58 -07001651 case AF_INET6:
1652 if (ip6_ignore_linkdown(nhc->nhc_dev))
1653 *flags |= RTNH_F_DEAD;
1654 break;
David Ahernc2364192019-04-02 14:11:57 -07001655 }
David Ahernb0f60192019-04-02 14:11:56 -07001656 rcu_read_unlock();
1657 }
1658
David Ahernbdf00462019-04-05 16:30:26 -07001659 switch (nhc->nhc_gw_family) {
1660 case AF_INET:
1661 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1662 goto nla_put_failure;
1663 break;
1664 case AF_INET6:
David Ahernd1566262019-04-05 16:30:40 -07001665 /* if gateway family does not match nexthop family
1666 * gateway is encoded as RTA_VIA
1667 */
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001668 if (rt_family != nhc->nhc_gw_family) {
David Ahernd1566262019-04-05 16:30:40 -07001669 int alen = sizeof(struct in6_addr);
1670 struct nlattr *nla;
1671 struct rtvia *via;
1672
1673 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1674 if (!nla)
1675 goto nla_put_failure;
1676
1677 via = nla_data(nla);
1678 via->rtvia_family = AF_INET6;
1679 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1680 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1681 &nhc->nhc_gw.ipv6) < 0) {
David Ahernbdf00462019-04-05 16:30:26 -07001682 goto nla_put_failure;
David Ahernd1566262019-04-05 16:30:40 -07001683 }
David Ahernbdf00462019-04-05 16:30:26 -07001684 break;
David Ahernc2364192019-04-02 14:11:57 -07001685 }
David Ahernb0f60192019-04-02 14:11:56 -07001686
Ido Schimmelca787e02020-11-10 12:25:53 +02001687 *flags |= (nhc->nhc_flags &
1688 (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
David Ahernb0f60192019-04-02 14:11:56 -07001689
David Ahernc2364192019-04-02 14:11:57 -07001690 if (!skip_oif && nhc->nhc_dev &&
1691 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001692 goto nla_put_failure;
1693
David Ahernc2364192019-04-02 14:11:57 -07001694 if (nhc->nhc_lwtstate &&
David Ahernffa8ce52019-04-23 08:23:41 -07001695 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1696 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001697 goto nla_put_failure;
1698
1699 return 0;
1700
1701nla_put_failure:
1702 return -EMSGSIZE;
1703}
David Ahernc0a72072019-04-02 14:11:58 -07001704EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001705
David Ahernc0a72072019-04-02 14:11:58 -07001706#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1707int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
Xiao Liang597aa162021-09-23 23:03:19 +08001708 int nh_weight, u8 rt_family, u32 nh_tclassid)
David Ahernb0f60192019-04-02 14:11:56 -07001709{
David Ahernc2364192019-04-02 14:11:57 -07001710 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001711 struct rtnexthop *rtnh;
David Ahernecc56632019-04-23 08:48:09 -07001712 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001713
1714 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1715 if (!rtnh)
1716 goto nla_put_failure;
1717
David Ahernc2364192019-04-02 14:11:57 -07001718 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001719 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1720
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001721 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001722 goto nla_put_failure;
1723
1724 rtnh->rtnh_flags = flags;
1725
Xiao Liang597aa162021-09-23 23:03:19 +08001726 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1727 goto nla_put_failure;
1728
David Ahernb0f60192019-04-02 14:11:56 -07001729 /* length of rtnetlink header + attributes */
1730 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1731
1732 return 0;
1733
1734nla_put_failure:
1735 return -EMSGSIZE;
1736}
David Ahernc0a72072019-04-02 14:11:58 -07001737EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001738#endif
David Ahernb0f60192019-04-02 14:11:56 -07001739
David Ahernc2364192019-04-02 14:11:57 -07001740#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001741static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1742{
1743 struct nlattr *mp;
1744
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001745 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernb0f60192019-04-02 14:11:56 -07001746 if (!mp)
1747 goto nla_put_failure;
1748
David Ahern4c7e8082019-06-03 20:19:51 -07001749 if (unlikely(fi->nh)) {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001750 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
David Ahern4c7e8082019-06-03 20:19:51 -07001751 goto nla_put_failure;
1752 goto mp_end;
1753 }
1754
David Ahernb0f60192019-04-02 14:11:56 -07001755 for_nexthops(fi) {
Xiao Liang597aa162021-09-23 23:03:19 +08001756 u32 nh_tclassid = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001757#ifdef CONFIG_IP_ROUTE_CLASSID
Xiao Liang597aa162021-09-23 23:03:19 +08001758 nh_tclassid = nh->nh_tclassid;
David Ahernb0f60192019-04-02 14:11:56 -07001759#endif
Xiao Liang597aa162021-09-23 23:03:19 +08001760 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1761 AF_INET, nh_tclassid) < 0)
1762 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001763 } endfor_nexthops(fi);
1764
David Ahern4c7e8082019-06-03 20:19:51 -07001765mp_end:
David Ahernb0f60192019-04-02 14:11:56 -07001766 nla_nest_end(skb, mp);
1767
1768 return 0;
1769
1770nla_put_failure:
1771 return -EMSGSIZE;
1772}
1773#else
1774static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1775{
1776 return 0;
1777}
1778#endif
1779
Eric W. Biederman15e47302012-09-07 20:12:54 +00001780int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
Amit Cohen08554782021-02-01 21:47:50 +02001781 const struct fib_rt_info *fri, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001783 unsigned int nhs = fib_info_num_path(fri->fi);
1784 struct fib_info *fi = fri->fi;
1785 u32 tb_id = fri->tb_id;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001786 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Eric W. Biederman15e47302012-09-07 20:12:54 +00001789 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001790 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001791 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001792
1793 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 rtm->rtm_family = AF_INET;
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001795 rtm->rtm_dst_len = fri->dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 rtm->rtm_src_len = 0;
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001797 rtm->rtm_tos = fri->tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001798 if (tb_id < 256)
1799 rtm->rtm_table = tb_id;
1800 else
1801 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001802 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1803 goto nla_put_failure;
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001804 rtm->rtm_type = fri->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001806 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001808
David S. Millerf3756b72012-04-01 20:39:02 -04001809 if (rtm->rtm_dst_len &&
Ido Schimmel1e301fd2020-01-14 13:23:10 +02001810 nla_put_in_addr(skb, RTA_DST, fri->dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001811 goto nla_put_failure;
1812 if (fi->fib_priority &&
1813 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1814 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001815 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001816 goto nla_put_failure;
1817
David S. Millerf3756b72012-04-01 20:39:02 -04001818 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001819 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001820 goto nla_put_failure;
David Ahern4c7e8082019-06-03 20:19:51 -07001821
1822 if (fi->nh) {
1823 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
1824 goto nla_put_failure;
1825 if (nexthop_is_blackhole(fi->nh))
1826 rtm->rtm_type = RTN_BLACKHOLE;
Roopa Prabhu4f801162020-04-27 13:56:46 -07001827 if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode)
1828 goto offload;
David Ahern4c7e8082019-06-03 20:19:51 -07001829 }
1830
David Ahern5481d732019-06-03 20:19:49 -07001831 if (nhs == 1) {
David Aherndcb1ecb2019-06-03 20:19:50 -07001832 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
David Ahernecc56632019-04-23 08:48:09 -07001833 unsigned char flags = 0;
David Ahernb0f60192019-04-02 14:11:56 -07001834
Donald Sharp7bdf4de2019-09-04 10:11:58 -04001835 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001836 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001837
1838 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001839#ifdef CONFIG_IP_ROUTE_CLASSID
David Aherndcb1ecb2019-06-03 20:19:50 -07001840 if (nhc->nhc_family == AF_INET) {
1841 struct fib_nh *nh;
1842
1843 nh = container_of(nhc, struct fib_nh, nh_common);
1844 if (nh->nh_tclassid &&
1845 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1846 goto nla_put_failure;
1847 }
Patrick McHardy8265abc2006-07-21 15:09:55 -07001848#endif
David Ahernb0f60192019-04-02 14:11:56 -07001849 } else {
1850 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001851 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001853
Roopa Prabhu4f801162020-04-27 13:56:46 -07001854offload:
Ido Schimmel90b93f12020-01-14 13:23:11 +02001855 if (fri->offload)
1856 rtm->rtm_flags |= RTM_F_OFFLOAD;
1857 if (fri->trap)
1858 rtm->rtm_flags |= RTM_F_TRAP;
Amit Cohen36c51002021-02-07 10:22:50 +02001859 if (fri->offload_failed)
1860 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
Ido Schimmel90b93f12020-01-14 13:23:11 +02001861
Johannes Berg053c0952015-01-16 22:09:00 +01001862 nlmsg_end(skb, nlh);
1863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Thomas Grafbe403ea2006-08-17 18:15:17 -07001865nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001866 nlmsg_cancel(skb, nlh);
1867 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001871 * Update FIB if:
1872 * - local address disappeared -> we must delete all the entries
1873 * referring to it.
1874 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001876int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001877{
1878 int ret = 0;
1879 unsigned int hash = fib_laddr_hashfn(local);
1880 struct hlist_head *head = &fib_info_laddrhash[hash];
David Aherne0a31262019-11-07 18:29:52 +00001881 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001882 struct net *net = dev_net(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001883 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Ian Morris51456b22015-04-03 09:17:26 +01001885 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001886 return 0;
1887
Sasha Levinb67bfe02013-02-27 17:06:00 -08001888 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001889 if (!net_eq(fi->fib_net, net) ||
1890 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001891 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001892 if (fi->fib_prefsrc == local) {
1893 fi->fib_flags |= RTNH_F_DEAD;
1894 ret++;
1895 }
1896 }
1897 return ret;
1898}
1899
David Ahernb75ed8b2019-03-27 20:53:55 -07001900static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001901 enum fib_event_type event_type)
1902{
David Ahernb75ed8b2019-03-27 20:53:55 -07001903 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001904 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001905 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001906 };
1907
1908 switch (event_type) {
1909 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001910 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001911 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001912 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001913 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001914 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001915 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001916 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001917 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1918 (nh->fib_nh_flags & RTNH_F_DEAD))
1919 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001920 event_type, &info.info);
Gustavo A. R. Silva79121182020-11-20 12:25:57 -06001921 break;
Ido Schimmel982acb92017-02-08 11:16:39 +01001922 default:
1923 break;
1924 }
1925
1926 return NOTIFY_DONE;
1927}
1928
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001929/* Update the PMTU of exceptions when:
1930 * - the new MTU of the first hop becomes smaller than the PMTU
1931 * - the old MTU was the same as the PMTU, and it limited discovery of
1932 * larger MTUs on the path. With that limit raised, we can now
1933 * discover larger MTUs
1934 * A special case is locked exceptions, for which the PMTU is smaller
1935 * than the minimal accepted PMTU:
1936 * - if the new MTU is greater than the PMTU, don't make any change
1937 * - otherwise, unlock and set PMTU
1938 */
David Ahern06c77c32019-05-22 12:04:46 -07001939void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001940{
1941 struct fnhe_hash_bucket *bucket;
1942 int i;
1943
David Aherna5995e72019-04-30 07:45:50 -07001944 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001945 if (!bucket)
1946 return;
1947
1948 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1949 struct fib_nh_exception *fnhe;
1950
1951 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1952 fnhe;
1953 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1954 if (fnhe->fnhe_mtu_locked) {
1955 if (new <= fnhe->fnhe_pmtu) {
1956 fnhe->fnhe_pmtu = new;
1957 fnhe->fnhe_mtu_locked = false;
1958 }
1959 } else if (new < fnhe->fnhe_pmtu ||
1960 orig == fnhe->fnhe_pmtu) {
1961 fnhe->fnhe_pmtu = new;
1962 }
1963 }
1964 }
1965}
1966
1967void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1968{
1969 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1970 struct hlist_head *head = &fib_info_devhash[hash];
1971 struct fib_nh *nh;
1972
1973 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001974 if (nh->fib_nh_dev == dev)
David Ahern06c77c32019-05-22 12:04:46 -07001975 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001976 }
1977}
1978
Julian Anastasov4f823de2015-10-30 10:23:33 +02001979/* Event force Flags Description
1980 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1981 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1982 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1983 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
David Ahern4c7e8082019-06-03 20:19:51 -07001984 *
1985 * only used when fib_nh is built into fib_info
Julian Anastasov4f823de2015-10-30 10:23:33 +02001986 */
1987int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int ret = 0;
1990 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001991 struct fib_info *prev_fi = NULL;
1992 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1993 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001994 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001995
Julian Anastasov4f823de2015-10-30 10:23:33 +02001996 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 scope = -1;
1998
Sasha Levinb67bfe02013-02-27 17:06:00 -08001999 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002000 struct fib_info *fi = nh->nh_parent;
2001 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002003 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07002004 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002005 continue;
2006 prev_fi = fi;
2007 dead = 0;
2008 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07002009 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002010 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07002011 else if (nexthop_nh->fib_nh_dev == dev &&
2012 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002013 switch (event) {
2014 case NETDEV_DOWN:
2015 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07002016 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Joe Perchesa8eceea2020-03-12 15:50:22 -07002017 fallthrough;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002018 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07002019 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002020 break;
2021 }
Ido Schimmel982acb92017-02-08 11:16:39 +01002022 call_fib_nh_notifiers(nexthop_nh,
2023 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002024 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002026#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002027 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07002028 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002029 dead = fi->fib_nhs;
2030 break;
2031 }
2032#endif
2033 } endfor_nexthops(fi)
2034 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002035 switch (event) {
2036 case NETDEV_DOWN:
2037 case NETDEV_UNREGISTER:
2038 fi->fib_flags |= RTNH_F_DEAD;
Joe Perchesa8eceea2020-03-12 15:50:22 -07002039 fallthrough;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002040 case NETDEV_CHANGE:
2041 fi->fib_flags |= RTNH_F_LINKDOWN;
2042 break;
2043 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08002044 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02002046
2047 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049
2050 return ret;
2051}
2052
David S. Miller0c838ff2011-01-31 16:16:50 -08002053/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08002054static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08002055{
2056 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08002057 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08002058 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03002059 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08002060 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002061 struct fib_alias *fa, *fa1 = NULL;
2062 u32 last_prio = res->fi->fib_priority;
2063 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08002064
Alexander Duyck56315f92015-02-25 15:31:31 -08002065 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002066 struct fib_info *next_fi = fa->fa_info;
David Ahern7c74b0b2020-04-22 15:40:20 -06002067 struct fib_nh_common *nhc;
David S. Miller0c838ff2011-01-31 16:16:50 -08002068
Julian Anastasov18a912e92015-07-22 10:43:22 +03002069 if (fa->fa_slen != slen)
2070 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002071 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2072 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03002073 if (fa->tb_id != tb->tb_id)
2074 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002075 if (next_fi->fib_priority > last_prio &&
2076 fa->fa_tos == last_tos) {
2077 if (last_tos)
2078 continue;
2079 break;
2080 }
2081 if (next_fi->fib_flags & RTNH_F_DEAD)
2082 continue;
2083 last_tos = fa->fa_tos;
2084 last_prio = next_fi->fib_priority;
2085
David S. Miller37e826c2011-03-24 18:06:47 -07002086 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08002087 fa->fa_type != RTN_UNICAST)
2088 continue;
David Ahern5481d732019-06-03 20:19:49 -07002089
David Ahern7c74b0b2020-04-22 15:40:20 -06002090 nhc = fib_info_nhc(next_fi, 0);
2091 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08002092 continue;
2093
2094 fib_alias_accessed(fa);
2095
Ian Morris51456b22015-04-03 09:17:26 +01002096 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002097 if (next_fi != res->fi)
2098 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03002099 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08002100 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03002101 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002102 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03002103 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08002104 goto out;
2105 }
2106 fi = next_fi;
2107 order++;
2108 }
2109
Ian Morris51456b22015-04-03 09:17:26 +01002110 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03002111 if (fa1)
2112 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08002113 goto out;
2114 }
2115
2116 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03002117 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08002118 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03002119 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08002120 goto out;
2121 }
2122
2123 if (last_idx >= 0)
2124 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03002125 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08002126out:
Eric Dumazet31d40932011-02-14 11:23:04 -08002127 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08002128}
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00002131 * Dead device goes up. We wake up dead nexthops.
2132 * It takes sense only on multipath routes.
David Ahern4c7e8082019-06-03 20:19:51 -07002133 *
2134 * only used when fib_nh is built into fib_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 */
David Ahernecc56632019-04-23 08:48:09 -07002136int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 struct fib_info *prev_fi;
2139 unsigned int hash;
2140 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct fib_nh *nh;
2142 int ret;
2143
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00002144 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 0;
2146
Julian Anastasovc9b32922015-10-30 10:23:34 +02002147 if (nh_flags & RTNH_F_DEAD) {
2148 unsigned int flags = dev_get_flags(dev);
2149
2150 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2151 nh_flags |= RTNH_F_LINKDOWN;
2152 }
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 prev_fi = NULL;
2155 hash = fib_devindex_hashfn(dev->ifindex);
2156 head = &fib_info_devhash[hash];
2157 ret = 0;
2158
Sasha Levinb67bfe02013-02-27 17:06:00 -08002159 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct fib_info *fi = nh->nh_parent;
2161 int alive;
2162
2163 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07002164 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 continue;
2166
2167 prev_fi = fi;
2168 alive = 0;
2169 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07002170 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 alive++;
2172 continue;
2173 }
David Ahernb75ed8b2019-03-27 20:53:55 -07002174 if (!nexthop_nh->fib_nh_dev ||
2175 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07002177 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08002178 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 continue;
2180 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07002181 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01002182 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 } endfor_nexthops(fi)
2184
2185 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002186 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 ret++;
2188 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02002189
2190 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
2193 return ret;
2194}
2195
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002196#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07002197static bool fib_good_nh(const struct fib_nh *nh)
2198{
2199 int state = NUD_REACHABLE;
2200
David Ahernb75ed8b2019-03-27 20:53:55 -07002201 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07002202 struct neighbour *n;
2203
2204 rcu_read_lock_bh();
2205
David Ahern1a38c432019-04-05 16:30:38 -07002206 if (likely(nh->fib_nh_gw_family == AF_INET))
2207 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2208 (__force u32)nh->fib_nh_gw4);
2209 else if (nh->fib_nh_gw_family == AF_INET6)
2210 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2211 &nh->fib_nh_gw6);
2212 else
2213 n = NULL;
David Aherna6db4492016-04-07 07:21:00 -07002214 if (n)
2215 state = n->nud_state;
2216
2217 rcu_read_unlock_bh();
2218 }
2219
2220 return !!(state & NUD_VALID);
2221}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04002222
Peter Nørlund0e884c72015-09-30 10:12:21 +02002223void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07002226 struct net *net = fi->fib_net;
2227 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
David Ahern4c7e8082019-06-03 20:19:51 -07002229 if (unlikely(res->fi->nh)) {
2230 nexthop_path_fib_result(res, hash);
2231 return;
2232 }
2233
David Aherneba618a2019-04-02 14:11:55 -07002234 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08002235 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07002236 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08002237 continue;
2238 if (!first) {
2239 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002240 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002241 first = true;
2242 }
2243 }
2244
David Aherneba618a2019-04-02 14:11:55 -07002245 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02002246 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Xin Long6174a302018-04-01 22:40:35 +08002248 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07002249 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08002250 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253#endif
David Ahern3ce58d82015-10-05 08:51:25 -07002254
2255void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002256 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07002257{
David Ahern0d876f22018-02-13 08:11:34 -08002258 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2259 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08002260
David Ahern3ce58d82015-10-05 08:51:25 -07002261#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern5481d732019-06-03 20:19:49 -07002262 if (fib_info_num_path(res->fi) > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08002263 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01002264
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02002265 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07002266 }
2267 else
2268#endif
2269 if (!res->prefixlen &&
2270 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08002271 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07002272 fib_select_default(fl4, res);
2273
David Ahern0d876f22018-02-13 08:11:34 -08002274check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07002275 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07002276 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07002277}