blob: 32ce6e6202d2fe5cff05249093cee52a9135300c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IPv4 Forwarding Information Base: semantics.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/bitops.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/jiffies.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/socket.h>
24#include <linux/sockios.h>
25#include <linux/errno.h>
26#include <linux/in.h>
27#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/proc_fs.h>
32#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
David Ahernc3ab2b42017-05-21 10:12:03 -060035#include <linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/ip.h>
39#include <net/protocol.h>
40#include <net/route.h>
41#include <net/tcp.h>
42#include <net/sock.h>
43#include <net/ip_fib.h>
Thomas Graff21c7bc2006-08-15 00:34:17 -070044#include <net/netlink.h>
Thomas Graf4e902c52006-08-17 18:14:52 -070045#include <net/nexthop.h>
Roopa Prabhu571e7222015-07-21 10:43:47 +020046#include <net/lwtunnel.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020047#include <net/fib_notifier.h>
David Ahernc0a72072019-04-02 14:11:58 -070048#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include "fib_lookup.h"
51
Stephen Hemminger832b4c52006-08-29 16:48:09 -070052static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct hlist_head *fib_info_hash;
54static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080055static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static unsigned int fib_info_cnt;
57
58#define DEVINDEX_HASHBITS 8
59#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
60static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
61
62#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000064#define for_nexthops(fi) { \
65 int nhsel; const struct fib_nh *nh; \
66 for (nhsel = 0, nh = (fi)->fib_nh; \
67 nhsel < (fi)->fib_nhs; \
68 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000070#define change_nexthops(fi) { \
71 int nhsel; struct fib_nh *nexthop_nh; \
72 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
73 nhsel < (fi)->fib_nhs; \
74 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#else /* CONFIG_IP_ROUTE_MULTIPATH */
77
78/* Hope, that gcc will optimize it to get rid of dummy loop */
79
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000080#define for_nexthops(fi) { \
81 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
82 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000084#define change_nexthops(fi) { \
85 int nhsel; \
86 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
87 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#endif /* CONFIG_IP_ROUTE_MULTIPATH */
90
91#define endfor_nexthops(fi) }
92
93
David S. Miller3be06862011-03-07 15:01:10 -080094const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000095 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .error = 0,
97 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000098 },
99 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .error = 0,
101 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000102 },
103 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .error = 0,
105 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000106 },
107 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .error = 0,
109 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000110 },
111 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .error = 0,
113 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000114 },
115 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .error = 0,
117 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000118 },
119 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .error = -EINVAL,
121 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000122 },
123 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .error = -EHOSTUNREACH,
125 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000126 },
127 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .error = -EACCES,
129 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000130 },
131 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .error = -EAGAIN,
133 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000134 },
135 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .error = -EINVAL,
137 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000138 },
139 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .error = -EINVAL,
141 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000142 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
David S. Millerc5038a82012-07-31 15:02:02 -0700145static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000146{
147 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
148
149 if (!rt)
150 return;
151
152 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
153 * because we waited an RCU grace period before calling
154 * free_fib_info_rcu()
155 */
156
Wei Wang95c47f92017-06-17 10:42:30 -0700157 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700158 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000159}
160
David S. Millerc5038a82012-07-31 15:02:02 -0700161static void free_nh_exceptions(struct fib_nh *nh)
162{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700163 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700164 int i;
165
Eric Dumazetcaa41522014-09-03 22:21:56 -0700166 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
167 if (!hash)
168 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700169 for (i = 0; i < FNHE_HASH_SIZE; i++) {
170 struct fib_nh_exception *fnhe;
171
172 fnhe = rcu_dereference_protected(hash[i].chain, 1);
173 while (fnhe) {
174 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800175
David S. Millerc5038a82012-07-31 15:02:02 -0700176 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
177
Timo Teräs2ffae992013-06-27 10:27:05 +0300178 rt_fibinfo_free(&fnhe->fnhe_rth_input);
179 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700180
181 kfree(fnhe);
182
183 fnhe = next;
184 }
185 }
186 kfree(hash);
187}
188
189static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000190{
191 int cpu;
192
193 if (!rtp)
194 return;
195
196 for_each_possible_cpu(cpu) {
197 struct rtable *rt;
198
199 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700200 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700201 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700202 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700203 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000204 }
205 free_percpu(rtp);
206}
207
David Ahern979e2762019-03-27 20:53:58 -0700208void fib_nh_common_release(struct fib_nh_common *nhc)
209{
210 if (nhc->nhc_dev)
211 dev_put(nhc->nhc_dev);
212
213 lwtstate_put(nhc->nhc_lwtstate);
214}
215EXPORT_SYMBOL_GPL(fib_nh_common_release);
216
David Ahernfaa041a2019-03-27 20:53:49 -0700217void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
218{
219#ifdef CONFIG_IP_ROUTE_CLASSID
220 if (fib_nh->nh_tclassid)
221 net->ipv4.fib_num_tclassid_users--;
222#endif
David Ahern979e2762019-03-27 20:53:58 -0700223 fib_nh_common_release(&fib_nh->nh_common);
David Ahernfaa041a2019-03-27 20:53:49 -0700224 free_nh_exceptions(fib_nh);
225 rt_fibinfo_free_cpus(fib_nh->nh_pcpu_rth_output);
226 rt_fibinfo_free(&fib_nh->nh_rth_input);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000230static void free_fib_info_rcu(struct rcu_head *head)
231{
232 struct fib_info *fi = container_of(head, struct fib_info, rcu);
233
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000234 change_nexthops(fi) {
David Ahernfaa041a2019-03-27 20:53:49 -0700235 fib_nh_release(fi->fib_net, nexthop_nh);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000236 } endfor_nexthops(fi);
237
David Aherncc5f0eb2018-10-04 20:07:52 -0700238 ip_fib_metrics_put(fi->fib_metrics);
239
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000240 kfree(fi);
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243void free_fib_info(struct fib_info *fi)
244{
245 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000246 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 fib_info_cnt--;
David Ahernfaa041a2019-03-27 20:53:49 -0700250
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000251 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100253EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255void fib_release_info(struct fib_info *fi)
256{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700257 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (fi && --fi->fib_treeref == 0) {
259 hlist_del(&fi->fib_hash);
260 if (fi->fib_prefsrc)
261 hlist_del(&fi->fib_lhash);
262 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700263 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800265 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 } endfor_nexthops(fi)
267 fi->fib_dead = 1;
268 fib_info_put(fi);
269 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700270 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000273static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 const struct fib_nh *onh = ofi->fib_nh;
276
277 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700278 if (nh->fib_nh_oif != onh->fib_nh_oif ||
David Aherna4ea5d42019-04-05 16:30:30 -0700279 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
David Ahernb75ed8b2019-03-27 20:53:55 -0700280 nh->fib_nh_scope != onh->fib_nh_scope ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700282 nh->fib_nh_weight != onh->fib_nh_weight ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100284#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 nh->nh_tclassid != onh->nh_tclassid ||
286#endif
David Ahernb75ed8b2019-03-27 20:53:55 -0700287 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
288 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -1;
David Aherna4ea5d42019-04-05 16:30:30 -0700290
291 if (nh->fib_nh_gw_family == AF_INET &&
292 nh->fib_nh_gw4 != onh->fib_nh_gw4)
293 return -1;
294
295 if (nh->fib_nh_gw_family == AF_INET6 &&
296 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
297 return -1;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 onh++;
300 } endfor_nexthops(fi);
301 return 0;
302}
303
David S. Miller88ebc722008-01-12 21:49:01 -0800304static inline unsigned int fib_devindex_hashfn(unsigned int val)
305{
306 unsigned int mask = DEVINDEX_HASHSIZE - 1;
307
308 return (val ^
309 (val >> DEVINDEX_HASHBITS) ^
310 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
314{
David S. Miller123b9732011-02-01 15:34:21 -0800315 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned int val = fi->fib_nhs;
317
David S. Miller37e826c2011-03-24 18:06:47 -0700318 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700319 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800321 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700322 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
David S. Miller88ebc722008-01-12 21:49:01 -0800323 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
326}
327
328static struct fib_info *fib_find_info(const struct fib_info *nfi)
329{
330 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct fib_info *fi;
332 unsigned int hash;
333
334 hash = fib_info_hashfn(nfi);
335 head = &fib_info_hash[hash];
336
Sasha Levinb67bfe02013-02-27 17:06:00 -0800337 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800338 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800339 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (fi->fib_nhs != nfi->fib_nhs)
341 continue;
342 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700343 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 nfi->fib_prefsrc == fi->fib_prefsrc &&
345 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000346 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000348 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400349 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
351 return fi;
352 }
353
354 return NULL;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000358 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Al Virod878e72e2006-09-26 22:18:13 -0700360int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct fib_nh *nh;
364 unsigned int hash;
365
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700366 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 hash = fib_devindex_hashfn(dev->ifindex);
369 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800370 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700371 if (nh->fib_nh_dev == dev &&
372 nh->fib_nh_gw4 == gw &&
373 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700374 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376 }
377 }
378
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700379 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return -1;
382}
383
Thomas Graf339bf982006-11-10 14:10:15 -0800384static inline size_t fib_nlmsg_size(struct fib_info *fi)
385{
386 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
387 + nla_total_size(4) /* RTA_TABLE */
388 + nla_total_size(4) /* RTA_DST */
389 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100390 + nla_total_size(4) /* RTA_PREFSRC */
391 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800392
393 /* space for nested metrics */
394 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
395
396 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200397 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800398 /* Also handles the special case fib_nhs == 1 */
399
400 /* each nexthop is packed in an attribute */
401 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
402
403 /* may contain flow and gateway attribute */
404 nhsize += 2 * nla_total_size(4);
405
Roopa Prabhu571e7222015-07-21 10:43:47 +0200406 /* grab encap info */
407 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700408 if (nh->fib_nh_lws) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200409 /* RTA_ENCAP_TYPE */
410 nh_encapsize += lwtunnel_get_encap_size(
David Ahernb75ed8b2019-03-27 20:53:55 -0700411 nh->fib_nh_lws);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200412 /* RTA_ENCAP */
413 nh_encapsize += nla_total_size(2);
414 }
415 } endfor_nexthops(fi);
416
Thomas Graf339bf982006-11-10 14:10:15 -0800417 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200418 payload += nla_total_size((fi->fib_nhs * nhsize) +
419 nh_encapsize);
420
Thomas Graf339bf982006-11-10 14:10:15 -0800421 }
422
423 return payload;
424}
425
Al Viro81f7bf62006-09-27 18:40:00 -0700426void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700427 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700428 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700431 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700432 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Thomas Graf339bf982006-11-10 14:10:15 -0800434 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100435 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700436 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Eric W. Biederman15e47302012-09-07 20:12:54 +0000438 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700439 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700440 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800441 if (err < 0) {
442 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
443 WARN_ON(err == -EMSGSIZE);
444 kfree_skb(skb);
445 goto errout;
446 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000447 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800448 info->nlh, GFP_KERNEL);
449 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700450errout:
451 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800452 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800455static int fib_detect_death(struct fib_info *fi, int order,
456 struct fib_info **last_resort, int *last_idx,
457 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct neighbour *n;
460 int state = NUD_NONE;
461
David Ahernb75ed8b2019-03-27 20:53:55 -0700462 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].fib_nh_gw4, fi->fib_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (n) {
464 state = n->nud_state;
465 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300466 } else {
467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800469 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000471 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000473 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300474 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *last_resort = fi;
476 *last_idx = order;
477 }
478 return 1;
479}
480
David Ahern979e2762019-03-27 20:53:58 -0700481int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
482 u16 encap_type, void *cfg, gfp_t gfp_flags,
483 struct netlink_ext_ack *extack)
484{
485 if (encap) {
486 struct lwtunnel_state *lwtstate;
487 int err;
488
489 if (encap_type == LWTUNNEL_ENCAP_NONE) {
490 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
491 return -EINVAL;
492 }
493 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
494 cfg, &lwtstate, extack);
495 if (err)
496 return err;
497
498 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
499 }
500
501 return 0;
502}
503EXPORT_SYMBOL_GPL(fib_nh_common_init);
504
David Aherne4516ef2019-03-27 20:53:48 -0700505int fib_nh_init(struct net *net, struct fib_nh *nh,
506 struct fib_config *cfg, int nh_weight,
507 struct netlink_ext_ack *extack)
508{
509 int err = -ENOMEM;
510
David Ahernf1741732019-03-27 20:53:57 -0700511 nh->fib_nh_family = AF_INET;
512
David Aherne4516ef2019-03-27 20:53:48 -0700513 nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
514 if (!nh->nh_pcpu_rth_output)
515 goto err_out;
516
David Ahern979e2762019-03-27 20:53:58 -0700517 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
518 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
519 if (err)
520 goto init_failure;
David Aherne4516ef2019-03-27 20:53:48 -0700521
David Ahernb75ed8b2019-03-27 20:53:55 -0700522 nh->fib_nh_oif = cfg->fc_oif;
David Aherna4ea5d42019-04-05 16:30:30 -0700523 nh->fib_nh_gw_family = cfg->fc_gw_family;
524 if (cfg->fc_gw_family == AF_INET)
David Ahernf35b7942019-04-05 16:30:28 -0700525 nh->fib_nh_gw4 = cfg->fc_gw4;
David Aherna4ea5d42019-04-05 16:30:30 -0700526 else if (cfg->fc_gw_family == AF_INET6)
527 nh->fib_nh_gw6 = cfg->fc_gw6;
528
David Ahernb75ed8b2019-03-27 20:53:55 -0700529 nh->fib_nh_flags = cfg->fc_flags;
David Aherne4516ef2019-03-27 20:53:48 -0700530
531#ifdef CONFIG_IP_ROUTE_CLASSID
532 nh->nh_tclassid = cfg->fc_flow;
533 if (nh->nh_tclassid)
534 net->ipv4.fib_num_tclassid_users++;
535#endif
536#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb75ed8b2019-03-27 20:53:55 -0700537 nh->fib_nh_weight = nh_weight;
David Aherne4516ef2019-03-27 20:53:48 -0700538#endif
539 return 0;
540
David Ahern979e2762019-03-27 20:53:58 -0700541init_failure:
David Aherne4516ef2019-03-27 20:53:48 -0700542 rt_fibinfo_free_cpus(nh->nh_pcpu_rth_output);
543 nh->nh_pcpu_rth_output = NULL;
544err_out:
545 return err;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef CONFIG_IP_ROUTE_MULTIPATH
549
David Ahern6d8422a12017-05-21 10:12:02 -0600550static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
551 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Thomas Graf4e902c52006-08-17 18:14:52 -0700555 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700557 rtnh = rtnh_next(rtnh, &remaining);
558 }
559
560 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600561 if (remaining > 0) {
562 NL_SET_ERR_MSG(extack,
563 "Invalid nexthop configuration - extra data after nexthops");
564 nhs = 0;
565 }
566
567 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Thomas Graf4e902c52006-08-17 18:14:52 -0700570static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600571 int remaining, struct fib_config *cfg,
572 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
David Aherne4516ef2019-03-27 20:53:48 -0700574 struct net *net = fi->fib_net;
575 struct fib_config fib_cfg;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200576 int ret;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700579 int attrlen;
580
David Aherne4516ef2019-03-27 20:53:48 -0700581 memset(&fib_cfg, 0, sizeof(fib_cfg));
582
David Ahernc3ab2b42017-05-21 10:12:03 -0600583 if (!rtnh_ok(rtnh, remaining)) {
584 NL_SET_ERR_MSG(extack,
585 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600587 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700588
David Ahernc3ab2b42017-05-21 10:12:03 -0600589 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
590 NL_SET_ERR_MSG(extack,
591 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300592 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600593 }
Julian Anastasov80610222016-07-10 21:11:55 +0300594
David Aherne4516ef2019-03-27 20:53:48 -0700595 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
596 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Thomas Graf4e902c52006-08-17 18:14:52 -0700597
598 attrlen = rtnh_attrlen(rtnh);
599 if (attrlen > 0) {
600 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
601
602 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernf35b7942019-04-05 16:30:28 -0700603 if (nla) {
604 fib_cfg.fc_gw_family = AF_INET;
605 fib_cfg.fc_gw4 = nla_get_in_addr(nla);
606 }
David Aherne4516ef2019-03-27 20:53:48 -0700607
Thomas Graf4e902c52006-08-17 18:14:52 -0700608 nla = nla_find(attrs, attrlen, RTA_FLOW);
David Aherne4516ef2019-03-27 20:53:48 -0700609 if (nla)
610 fib_cfg.fc_flow = nla_get_u32(nla);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200611
David Aherne4516ef2019-03-27 20:53:48 -0700612 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
613 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
614 if (nla)
615 fib_cfg.fc_encap_type = nla_get_u16(nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700617
David Aherne4516ef2019-03-27 20:53:48 -0700618 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
619 rtnh->rtnh_hops + 1, extack);
620 if (ret)
621 goto errout;
622
Thomas Graf4e902c52006-08-17 18:14:52 -0700623 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700625
Roopa Prabhu571e7222015-07-21 10:43:47 +0200626 ret = -EINVAL;
David Ahernb75ed8b2019-03-27 20:53:55 -0700627 if (cfg->fc_oif && fi->fib_nh->fib_nh_oif != cfg->fc_oif) {
David Aherne4516ef2019-03-27 20:53:48 -0700628 NL_SET_ERR_MSG(extack,
629 "Nexthop device index does not match RTA_OIF");
630 goto errout;
631 }
David Ahernf35b7942019-04-05 16:30:28 -0700632 if (cfg->fc_gw_family) {
633 if (cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family ||
634 (cfg->fc_gw_family == AF_INET &&
David Aherna4ea5d42019-04-05 16:30:30 -0700635 fi->fib_nh->fib_nh_gw4 != cfg->fc_gw4) ||
636 (cfg->fc_gw_family == AF_INET6 &&
637 ipv6_addr_cmp(&fi->fib_nh->fib_nh_gw6, &cfg->fc_gw6))) {
David Ahernf35b7942019-04-05 16:30:28 -0700638 NL_SET_ERR_MSG(extack,
David Aherna4ea5d42019-04-05 16:30:30 -0700639 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
David Ahernf35b7942019-04-05 16:30:28 -0700640 goto errout;
641 }
David Aherne4516ef2019-03-27 20:53:48 -0700642 }
643#ifdef CONFIG_IP_ROUTE_CLASSID
644 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
645 NL_SET_ERR_MSG(extack,
646 "Nexthop class id does not match RTA_FLOW");
647 goto errout;
648 }
649#endif
650 ret = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200651errout:
652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Peter Nørlund0e884c72015-09-30 10:12:21 +0200655static void fib_rebalance(struct fib_info *fi)
656{
657 int total;
658 int w;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200659
660 if (fi->fib_nhs < 2)
661 return;
662
663 total = 0;
664 for_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700665 if (nh->fib_nh_flags & RTNH_F_DEAD)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200666 continue;
667
David Ahernb75ed8b2019-03-27 20:53:55 -0700668 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
669 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200670 continue;
671
David Ahernb75ed8b2019-03-27 20:53:55 -0700672 total += nh->fib_nh_weight;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200673 } endfor_nexthops(fi);
674
675 w = 0;
676 change_nexthops(fi) {
677 int upper_bound;
678
David Ahernb75ed8b2019-03-27 20:53:55 -0700679 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200680 upper_bound = -1;
David Ahernb75ed8b2019-03-27 20:53:55 -0700681 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
682 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Peter Nørlund0e884c72015-09-30 10:12:21 +0200683 upper_bound = -1;
684 } else {
David Ahernb75ed8b2019-03-27 20:53:55 -0700685 w += nexthop_nh->fib_nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200686 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
687 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200688 }
689
David Ahernb75ed8b2019-03-27 20:53:55 -0700690 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200691 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200692}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200693#else /* CONFIG_IP_ROUTE_MULTIPATH */
694
David Ahern8373c6c2019-03-27 20:53:46 -0700695static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
696 int remaining, struct fib_config *cfg,
697 struct netlink_ext_ack *extack)
698{
699 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
700
701 return -EINVAL;
702}
703
Peter Nørlund0e884c72015-09-30 10:12:21 +0200704#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200705
706#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
David Ahern30357d72017-01-30 12:07:37 -0800708static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800709 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800710 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600711 const struct fib_config *cfg,
712 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200713{
714 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200715 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200716
717 if (encap_type == LWTUNNEL_ENCAP_NONE)
718 return 0;
719
David Ahern9ae28722017-05-27 16:19:28 -0600720 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
721 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200722 if (!ret) {
David Ahernb75ed8b2019-03-27 20:53:55 -0700723 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Jiri Bencdf383e62015-08-18 18:41:13 +0200724 lwtstate_free(lwtstate);
725 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200726
Jiri Bencdf383e62015-08-18 18:41:13 +0200727 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200728}
729
David Ahern9ae28722017-05-27 16:19:28 -0600730int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
731 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700734 struct rtnexthop *rtnh;
735 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#endif
737
Thomas Graf4e902c52006-08-17 18:14:52 -0700738 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 1;
740
David Ahernf35b7942019-04-05 16:30:28 -0700741 if (cfg->fc_oif || cfg->fc_gw_family) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200742 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600743 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
744 fi->fib_nh, cfg, extack))
745 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200746 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100747#ifdef CONFIG_IP_ROUTE_CLASSID
748 if (cfg->fc_flow &&
749 cfg->fc_flow != fi->fib_nh->nh_tclassid)
750 return 1;
751#endif
David Ahernf35b7942019-04-05 16:30:28 -0700752 if ((cfg->fc_oif && cfg->fc_oif != fi->fib_nh->fib_nh_oif) ||
753 (cfg->fc_gw_family &&
754 cfg->fc_gw_family != fi->fib_nh->fib_nh_gw_family))
755 return 1;
756
757 if (cfg->fc_gw_family == AF_INET &&
758 cfg->fc_gw4 != fi->fib_nh->fib_nh_gw4)
759 return 1;
760
David Aherna4ea5d42019-04-05 16:30:30 -0700761 if (cfg->fc_gw_family == AF_INET6 &&
762 ipv6_addr_cmp(&cfg->fc_gw6, &fi->fib_nh->fib_nh_gw6))
763 return 1;
764
David Ahernf35b7942019-04-05 16:30:28 -0700765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100769 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700771
772 rtnh = cfg->fc_mp;
773 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700776 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Thomas Graf4e902c52006-08-17 18:14:52 -0700778 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700780
David Ahernb75ed8b2019-03-27 20:53:55 -0700781 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700783
784 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200785 if (attrlen > 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700786 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
787
788 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Ahernb75ed8b2019-03-27 20:53:55 -0700789 if (nla && nla_get_in_addr(nla) != nh->fib_nh_gw4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100791#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700792 nla = nla_find(attrs, attrlen, RTA_FLOW);
793 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 1;
795#endif
796 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700797
798 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 } endfor_nexthops(fi);
800#endif
801 return 0;
802}
803
Xin Long5f9ae3d2017-08-23 10:07:26 +0800804bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
805{
806 struct nlattr *nla;
807 int remaining;
808
809 if (!cfg->fc_mx)
810 return true;
811
812 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
813 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +0100814 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800815
816 if (!type)
817 continue;
818 if (type > RTAX_MAX)
819 return false;
820
821 if (type == RTAX_CC_ALGO) {
822 char tmp[TCP_CA_NAME_MAX];
823 bool ecn_ca = false;
824
825 nla_strlcpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -0800826 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +0800827 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -0700828 if (nla_len(nla) != sizeof(u32))
829 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800830 val = nla_get_u32(nla);
831 }
832
Phil Sutterd03a4552017-12-19 15:17:13 +0100833 fi_val = fi->fib_metrics->metrics[type - 1];
834 if (type == RTAX_FEATURES)
835 fi_val &= ~DST_FEATURE_ECN_CA;
836
837 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +0800838 return false;
839 }
840
841 return true;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000846 * Picture
847 * -------
848 *
849 * Semantics of nexthop is very messy by historical reasons.
850 * We have to take into account, that:
851 * a) gateway can be actually local interface address,
852 * so that gatewayed route is direct.
853 * b) gateway must be on-link address, possibly
854 * described not by an ifaddr, but also by a direct route.
855 * c) If both gateway and interface are specified, they should not
856 * contradict.
857 * d) If we use tunnel routes, gateway could be not on-link.
858 *
859 * Attempt to reconcile all of these (alas, self-contradictory) conditions
860 * results in pretty ugly and hairy code with obscure logic.
861 *
862 * I chose to generalized it instead, so that the size
863 * of code does not increase practically, but it becomes
864 * much more general.
865 * Every prefix is assigned a "scope" value: "host" is local address,
866 * "link" is direct route,
867 * [ ... "site" ... "interior" ... ]
868 * and "universe" is true gateway route with global meaning.
869 *
870 * Every prefix refers to a set of "nexthop"s (gw, oif),
871 * where gw must have narrower scope. This recursion stops
872 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
873 * which means that gw is forced to be on link.
874 *
875 * Code is still hairy, but now it is apparently logically
876 * consistent and very flexible. F.e. as by-product it allows
877 * to co-exists in peace independent exterior and interior
878 * routing processes.
879 *
880 * Normally it looks as following.
881 *
882 * {universe prefix} -> (gw, oif) [scope link]
883 * |
884 * |-> {link prefix} -> (gw, oif) [scope local]
885 * |
886 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
David Ahern448d7242019-04-05 16:30:31 -0700888static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
889 u8 scope, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000891 struct net_device *dev;
David Ahern448d7242019-04-05 16:30:31 -0700892 struct fib_result res;
893 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
David Ahern448d7242019-04-05 16:30:31 -0700895 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
896 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
David Ahern448d7242019-04-05 16:30:31 -0700898 if (scope >= RT_SCOPE_LINK) {
899 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
900 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
David Ahern448d7242019-04-05 16:30:31 -0700902 dev = __dev_get_by_index(net, nh->fib_nh_oif);
903 if (!dev) {
904 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
905 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
David Ahern448d7242019-04-05 16:30:31 -0700907 if (!(dev->flags & IFF_UP)) {
908 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
909 return -ENETDOWN;
910 }
911 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
912 if (addr_type != RTN_UNICAST) {
913 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
914 return -EINVAL;
915 }
916 if (!netif_carrier_ok(dev))
917 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
918 nh->fib_nh_dev = dev;
919 dev_hold(dev);
920 nh->fib_nh_scope = RT_SCOPE_LINK;
921 return 0;
922 }
923 rcu_read_lock();
924 {
925 struct fib_table *tbl = NULL;
926 struct flowi4 fl4 = {
927 .daddr = nh->fib_nh_gw4,
928 .flowi4_scope = scope + 1,
929 .flowi4_oif = nh->fib_nh_oif,
930 .flowi4_iif = LOOPBACK_IFINDEX,
931 };
932
933 /* It is not necessary, but requires a bit of thinking */
934 if (fl4.flowi4_scope < RT_SCOPE_LINK)
935 fl4.flowi4_scope = RT_SCOPE_LINK;
936
937 if (table)
938 tbl = fib_get_table(net, table);
939
940 if (tbl)
941 err = fib_table_lookup(tbl, &fl4, &res,
942 FIB_LOOKUP_IGNORE_LINKSTATE |
943 FIB_LOOKUP_NOREF);
944
945 /* on error or if no table given do full lookup. This
946 * is needed for example when nexthops are in the local
947 * table rather than the given table
948 */
949 if (!tbl || err) {
950 err = fib_lookup(net, &fl4, &res,
951 FIB_LOOKUP_IGNORE_LINKSTATE);
952 }
953
954 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600955 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
David Ahern448d7242019-04-05 16:30:31 -0700959
960 err = -EINVAL;
961 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
962 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
963 goto out;
964 }
965 nh->fib_nh_scope = res.scope;
966 nh->fib_nh_oif = FIB_RES_OIF(res);
967 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
968 if (!dev) {
969 NL_SET_ERR_MSG(extack,
970 "No egress device for nexthop gateway");
971 goto out;
972 }
973 dev_hold(dev);
974 if (!netif_carrier_ok(dev))
975 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
976 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000977out:
978 rcu_read_unlock();
979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
David Ahern448d7242019-04-05 16:30:31 -0700982static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
983 struct netlink_ext_ack *extack)
984{
985 struct in_device *in_dev;
986 int err;
987
988 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
989 NL_SET_ERR_MSG(extack,
990 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
991 return -EINVAL;
992 }
993
994 rcu_read_lock();
995
996 err = -ENODEV;
997 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
998 if (!in_dev)
999 goto out;
1000 err = -ENETDOWN;
1001 if (!(in_dev->dev->flags & IFF_UP)) {
1002 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1003 goto out;
1004 }
1005
1006 nh->fib_nh_dev = in_dev->dev;
1007 dev_hold(nh->fib_nh_dev);
1008 nh->fib_nh_scope = RT_SCOPE_HOST;
1009 if (!netif_carrier_ok(nh->fib_nh_dev))
1010 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1011 err = 0;
1012out:
1013 rcu_read_unlock();
1014 return err;
1015}
1016
1017static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
1018 struct netlink_ext_ack *extack)
1019{
1020 struct net *net = cfg->fc_nlinfo.nl_net;
1021 u32 table = cfg->fc_table;
1022 int err;
1023
1024 if (nh->fib_nh_gw_family == AF_INET)
1025 err = fib_check_nh_v4_gw(net, nh, table, cfg->fc_scope, extack);
1026 else
1027 err = fib_check_nh_nongw(net, nh, extack);
1028
1029 return err;
1030}
1031
Al Viro81f7bf62006-09-27 18:40:00 -07001032static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
David S. Miller123b9732011-02-01 15:34:21 -08001034 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001036 return ((__force u32)val ^
1037 ((__force u32)val >> 7) ^
1038 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
David S. Miller123b9732011-02-01 15:34:21 -08001041static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +08001044 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 else
1046 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001047 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1048 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
David S. Miller123b9732011-02-01 15:34:21 -08001051static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 if (!hash)
1054 return;
1055
1056 if (bytes <= PAGE_SIZE)
1057 kfree(hash);
1058 else
1059 free_pages((unsigned long) hash, get_order(bytes));
1060}
1061
David S. Miller123b9732011-02-01 15:34:21 -08001062static void fib_info_hash_move(struct hlist_head *new_info_hash,
1063 struct hlist_head *new_laddrhash,
1064 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
David S. Millerb7656e72005-08-05 04:12:48 -07001066 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001067 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -07001068 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001070 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001071 old_info_hash = fib_info_hash;
1072 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -08001073 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 for (i = 0; i < old_size; i++) {
1076 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001077 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct fib_info *fi;
1079
Sasha Levinb67bfe02013-02-27 17:06:00 -08001080 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct hlist_head *dest;
1082 unsigned int new_hash;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 new_hash = fib_info_hashfn(fi);
1085 dest = &new_info_hash[new_hash];
1086 hlist_add_head(&fi->fib_hash, dest);
1087 }
1088 }
1089 fib_info_hash = new_info_hash;
1090
1091 for (i = 0; i < old_size; i++) {
1092 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001093 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct fib_info *fi;
1095
Sasha Levinb67bfe02013-02-27 17:06:00 -08001096 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct hlist_head *ldest;
1098 unsigned int new_hash;
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1101 ldest = &new_laddrhash[new_hash];
1102 hlist_add_head(&fi->fib_lhash, ldest);
1103 }
1104 }
1105 fib_info_laddrhash = new_laddrhash;
1106
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001107 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -07001108
1109 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001110 fib_info_hash_free(old_info_hash, bytes);
1111 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
David S. Miller436c3b62011-03-24 17:42:21 -07001114__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1115{
David Ahernb75ed8b2019-03-27 20:53:55 -07001116 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev,
1117 nh->fib_nh_gw4,
David S. Miller37e826c2011-03-24 18:06:47 -07001118 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -07001119 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1120
1121 return nh->nh_saddr;
1122}
1123
David Aherneba618a2019-04-02 14:11:55 -07001124__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1125{
1126 struct fib_nh_common *nhc = res->nhc;
1127 struct fib_nh *nh;
1128
1129 if (res->fi->fib_prefsrc)
1130 return res->fi->fib_prefsrc;
1131
1132 nh = container_of(nhc, struct fib_nh, nh_common);
1133 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1134 return nh->nh_saddr;
1135
1136 return fib_info_update_nh_saddr(net, nh);
1137}
1138
David Ahern021dd3b2015-08-13 14:59:06 -06001139static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1140{
1141 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1142 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001143 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001144 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001145
1146 if (tb_id == RT_TABLE_MAIN)
1147 tb_id = RT_TABLE_LOCAL;
1148
David Aherne1b8d902015-11-03 15:59:28 -08001149 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1150 fib_prefsrc, tb_id);
1151
1152 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1153 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1154 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001155 }
David Aherne1b8d902015-11-03 15:59:28 -08001156
1157 if (rc != RTN_LOCAL)
1158 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001159 }
1160 return true;
1161}
1162
David Ahern6d8422a12017-05-21 10:12:02 -06001163struct fib_info *fib_create_info(struct fib_config *cfg,
1164 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 int err;
1167 struct fib_info *fi = NULL;
1168 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001170 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
David S. Miller4c8237c2011-03-07 14:27:38 -08001172 if (cfg->fc_type > RTN_MAX)
1173 goto err_inval;
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001176 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1177 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
David Ahernc3ab2b42017-05-21 10:12:03 -06001181 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1182 NL_SET_ERR_MSG(extack,
1183 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001184 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001185 }
Julian Anastasov80610222016-07-10 21:11:55 +03001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001188 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001189 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (nhs == 0)
1191 goto err_inval;
1192 }
1193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001196 if (fib_info_cnt >= fib_info_hash_size) {
1197 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct hlist_head *new_info_hash;
1199 struct hlist_head *new_laddrhash;
1200 unsigned int bytes;
1201
1202 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001203 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001205 new_info_hash = fib_info_hash_alloc(bytes);
1206 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001208 fib_info_hash_free(new_info_hash, bytes);
1209 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001210 } else
David S. Miller123b9732011-02-01 15:34:21 -08001211 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David S. Miller123b9732011-02-01 15:34:21 -08001213 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto failure;
1215 }
1216
Gustavo A. R. Silva1f533ba2019-01-30 18:51:48 -06001217 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001218 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto failure;
David Ahern767a2212018-10-04 20:07:51 -07001220 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
David Ahernd7e774f2018-11-06 12:51:15 -08001221 cfg->fc_mx_len, extack);
David Ahern767a2212018-10-04 20:07:51 -07001222 if (unlikely(IS_ERR(fi->fib_metrics))) {
1223 err = PTR_ERR(fi->fib_metrics);
1224 kfree(fi);
1225 return ERR_PTR(err);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001226 }
David Ahern767a2212018-10-04 20:07:51 -07001227
Eric Dumazet187e5b32017-08-15 05:26:17 -07001228 fib_info_cnt++;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001229 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001230 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001231 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001232 fi->fib_flags = cfg->fc_flags;
1233 fi->fib_priority = cfg->fc_priority;
1234 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001235 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001236 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 fi->fib_nhs = nhs;
1239 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001240 nexthop_nh->nh_parent = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 } endfor_nexthops(fi)
1242
David Aherne4516ef2019-03-27 20:53:48 -07001243 if (cfg->fc_mp)
David Ahern6d8422a12017-05-21 10:12:02 -06001244 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
David Aherne4516ef2019-03-27 20:53:48 -07001245 else
1246 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001247
David Aherne4516ef2019-03-27 20:53:48 -07001248 if (err != 0)
1249 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Thomas Graf4e902c52006-08-17 18:14:52 -07001251 if (fib_props[cfg->fc_type].error) {
David Ahernf35b7942019-04-05 16:30:28 -07001252 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001253 NL_SET_ERR_MSG(extack,
1254 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001258 } else {
1259 switch (cfg->fc_type) {
1260 case RTN_UNICAST:
1261 case RTN_LOCAL:
1262 case RTN_BROADCAST:
1263 case RTN_ANYCAST:
1264 case RTN_MULTICAST:
1265 break;
1266 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001267 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001268 goto err_inval;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
David Ahernc3ab2b42017-05-21 10:12:03 -06001272 if (cfg->fc_scope > RT_SCOPE_HOST) {
1273 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Thomas Graf4e902c52006-08-17 18:14:52 -07001277 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct fib_nh *nh = fi->fib_nh;
1279
1280 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001281 if (nhs != 1) {
1282 NL_SET_ERR_MSG(extack,
1283 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001284 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001285 }
David Ahernbdf00462019-04-05 16:30:26 -07001286 if (nh->fib_nh_gw_family) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001287 NL_SET_ERR_MSG(extack,
1288 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001290 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001291 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1292 nh->fib_nh_dev = dev_get_by_index(net, fi->fib_nh->fib_nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 err = -ENODEV;
David Ahernb75ed8b2019-03-27 20:53:55 -07001294 if (!nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto failure;
1296 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001297 int linkdown = 0;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 change_nexthops(fi) {
David Ahernfa8fefa2017-09-27 20:41:59 -07001300 err = fib_check_nh(cfg, nexthop_nh, extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001301 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 goto failure;
David Ahernb75ed8b2019-03-27 20:53:55 -07001303 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001304 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001306 if (linkdown == fi->fib_nhs)
1307 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
David Ahernc3ab2b42017-05-21 10:12:03 -06001310 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1311 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001312 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
David S. Miller1fc050a2011-03-07 20:54:48 -08001315 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001316 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -08001317 } endfor_nexthops(fi)
1318
Peter Nørlund0e884c72015-09-30 10:12:21 +02001319 fib_rebalance(fi);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001322 ofi = fib_find_info(fi);
1323 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 fi->fib_dead = 1;
1325 free_fib_info(fi);
1326 ofi->fib_treeref++;
1327 return ofi;
1328 }
1329
1330 fi->fib_treeref++;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001331 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001332 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 hlist_add_head(&fi->fib_hash,
1334 &fib_info_hash[fib_info_hashfn(fi)]);
1335 if (fi->fib_prefsrc) {
1336 struct hlist_head *head;
1337
1338 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1339 hlist_add_head(&fi->fib_lhash, head);
1340 }
1341 change_nexthops(fi) {
1342 struct hlist_head *head;
1343 unsigned int hash;
1344
David Ahernb75ed8b2019-03-27 20:53:55 -07001345 if (!nexthop_nh->fib_nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001347 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001349 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001351 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return fi;
1353
1354err_inval:
1355 err = -EINVAL;
1356
1357failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001358 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 fi->fib_dead = 1;
1360 free_fib_info(fi);
1361 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001362
1363 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
David Ahernc0a72072019-04-02 14:11:58 -07001366int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1367 unsigned int *flags, bool skip_oif)
David Ahernb0f60192019-04-02 14:11:56 -07001368{
David Ahernc2364192019-04-02 14:11:57 -07001369 if (nhc->nhc_flags & RTNH_F_DEAD)
David Ahernb0f60192019-04-02 14:11:56 -07001370 *flags |= RTNH_F_DEAD;
1371
David Ahernc2364192019-04-02 14:11:57 -07001372 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
David Ahernb0f60192019-04-02 14:11:56 -07001373 *flags |= RTNH_F_LINKDOWN;
1374
1375 rcu_read_lock();
David Ahernc2364192019-04-02 14:11:57 -07001376 switch (nhc->nhc_family) {
1377 case AF_INET:
1378 if (ip_ignore_linkdown(nhc->nhc_dev))
1379 *flags |= RTNH_F_DEAD;
1380 break;
David Ahernc0a72072019-04-02 14:11:58 -07001381 case AF_INET6:
1382 if (ip6_ignore_linkdown(nhc->nhc_dev))
1383 *flags |= RTNH_F_DEAD;
1384 break;
David Ahernc2364192019-04-02 14:11:57 -07001385 }
David Ahernb0f60192019-04-02 14:11:56 -07001386 rcu_read_unlock();
1387 }
1388
David Ahernbdf00462019-04-05 16:30:26 -07001389 switch (nhc->nhc_gw_family) {
1390 case AF_INET:
1391 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1392 goto nla_put_failure;
1393 break;
1394 case AF_INET6:
1395 if (nla_put_in6_addr(skb, RTA_GATEWAY, &nhc->nhc_gw.ipv6) < 0)
1396 goto nla_put_failure;
1397 break;
David Ahernc2364192019-04-02 14:11:57 -07001398 }
David Ahernb0f60192019-04-02 14:11:56 -07001399
David Ahernc2364192019-04-02 14:11:57 -07001400 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1401 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
David Ahernb0f60192019-04-02 14:11:56 -07001402 *flags |= RTNH_F_OFFLOAD;
1403
David Ahernc2364192019-04-02 14:11:57 -07001404 if (!skip_oif && nhc->nhc_dev &&
1405 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
David Ahernb0f60192019-04-02 14:11:56 -07001406 goto nla_put_failure;
1407
David Ahernc2364192019-04-02 14:11:57 -07001408 if (nhc->nhc_lwtstate &&
1409 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001410 goto nla_put_failure;
1411
1412 return 0;
1413
1414nla_put_failure:
1415 return -EMSGSIZE;
1416}
David Ahernc0a72072019-04-02 14:11:58 -07001417EXPORT_SYMBOL_GPL(fib_nexthop_info);
David Ahernb0f60192019-04-02 14:11:56 -07001418
David Ahernc0a72072019-04-02 14:11:58 -07001419#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1420int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1421 int nh_weight)
David Ahernb0f60192019-04-02 14:11:56 -07001422{
David Ahernc2364192019-04-02 14:11:57 -07001423 const struct net_device *dev = nhc->nhc_dev;
David Ahernb0f60192019-04-02 14:11:56 -07001424 struct rtnexthop *rtnh;
1425 unsigned int flags = 0;
1426
1427 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1428 if (!rtnh)
1429 goto nla_put_failure;
1430
David Ahernc2364192019-04-02 14:11:57 -07001431 rtnh->rtnh_hops = nh_weight - 1;
David Ahernb0f60192019-04-02 14:11:56 -07001432 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1433
David Ahernc2364192019-04-02 14:11:57 -07001434 if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001435 goto nla_put_failure;
1436
1437 rtnh->rtnh_flags = flags;
1438
1439 /* length of rtnetlink header + attributes */
1440 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1441
1442 return 0;
1443
1444nla_put_failure:
1445 return -EMSGSIZE;
1446}
David Ahernc0a72072019-04-02 14:11:58 -07001447EXPORT_SYMBOL_GPL(fib_add_nexthop);
David Ahernc2364192019-04-02 14:11:57 -07001448#endif
David Ahernb0f60192019-04-02 14:11:56 -07001449
David Ahernc2364192019-04-02 14:11:57 -07001450#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahernb0f60192019-04-02 14:11:56 -07001451static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1452{
1453 struct nlattr *mp;
1454
1455 mp = nla_nest_start(skb, RTA_MULTIPATH);
1456 if (!mp)
1457 goto nla_put_failure;
1458
1459 for_nexthops(fi) {
David Ahernc2364192019-04-02 14:11:57 -07001460 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
David Ahernb0f60192019-04-02 14:11:56 -07001461 goto nla_put_failure;
1462#ifdef CONFIG_IP_ROUTE_CLASSID
1463 if (nh->nh_tclassid &&
1464 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1465 goto nla_put_failure;
1466#endif
1467 } endfor_nexthops(fi);
1468
1469 nla_nest_end(skb, mp);
1470
1471 return 0;
1472
1473nla_put_failure:
1474 return -EMSGSIZE;
1475}
1476#else
1477static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1478{
1479 return 0;
1480}
1481#endif
1482
Eric W. Biederman15e47302012-09-07 20:12:54 +00001483int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001484 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001485 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001487 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Eric W. Biederman15e47302012-09-07 20:12:54 +00001490 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001491 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001492 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001493
1494 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 rtm->rtm_family = AF_INET;
1496 rtm->rtm_dst_len = dst_len;
1497 rtm->rtm_src_len = 0;
1498 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001499 if (tb_id < 256)
1500 rtm->rtm_table = tb_id;
1501 else
1502 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001503 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1504 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 rtm->rtm_type = type;
1506 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001507 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001509
David S. Millerf3756b72012-04-01 20:39:02 -04001510 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001511 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001512 goto nla_put_failure;
1513 if (fi->fib_priority &&
1514 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1515 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001516 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001517 goto nla_put_failure;
1518
David S. Millerf3756b72012-04-01 20:39:02 -04001519 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001520 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001521 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (fi->fib_nhs == 1) {
David Ahernb0f60192019-04-02 14:11:56 -07001523 struct fib_nh *nh = &fi->fib_nh[0];
1524 unsigned int flags = 0;
1525
David Ahernc2364192019-04-02 14:11:57 -07001526 if (fib_nexthop_info(skb, &nh->nh_common, &flags, false) < 0)
David S. Millerf3756b72012-04-01 20:39:02 -04001527 goto nla_put_failure;
David Ahernb0f60192019-04-02 14:11:56 -07001528
1529 rtm->rtm_flags = flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001530#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernb0f60192019-04-02 14:11:56 -07001531 if (nh->nh_tclassid &&
1532 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
David S. Millerf3756b72012-04-01 20:39:02 -04001533 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001534#endif
David Ahernb0f60192019-04-02 14:11:56 -07001535 } else {
1536 if (fib_add_multipath(skb, fi) < 0)
David Ahernea7a8082017-01-11 14:29:54 -08001537 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001539
Johannes Berg053c0952015-01-16 22:09:00 +01001540 nlmsg_end(skb, nlh);
1541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Thomas Grafbe403ea2006-08-17 18:15:17 -07001543nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001544 nlmsg_cancel(skb, nlh);
1545 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001549 * Update FIB if:
1550 * - local address disappeared -> we must delete all the entries
1551 * referring to it.
1552 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001554int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001555{
1556 int ret = 0;
1557 unsigned int hash = fib_laddr_hashfn(local);
1558 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001559 struct net *net = dev_net(dev);
1560 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001561 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Ian Morris51456b22015-04-03 09:17:26 +01001563 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001564 return 0;
1565
Sasha Levinb67bfe02013-02-27 17:06:00 -08001566 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001567 if (!net_eq(fi->fib_net, net) ||
1568 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001569 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001570 if (fi->fib_prefsrc == local) {
1571 fi->fib_flags |= RTNH_F_DEAD;
1572 ret++;
1573 }
1574 }
1575 return ret;
1576}
1577
David Ahernb75ed8b2019-03-27 20:53:55 -07001578static int call_fib_nh_notifiers(struct fib_nh *nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001579 enum fib_event_type event_type)
1580{
David Ahernb75ed8b2019-03-27 20:53:55 -07001581 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Ido Schimmel982acb92017-02-08 11:16:39 +01001582 struct fib_nh_notifier_info info = {
David Ahernb75ed8b2019-03-27 20:53:55 -07001583 .fib_nh = nh,
Ido Schimmel982acb92017-02-08 11:16:39 +01001584 };
1585
1586 switch (event_type) {
1587 case FIB_EVENT_NH_ADD:
David Ahernb75ed8b2019-03-27 20:53:55 -07001588 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel982acb92017-02-08 11:16:39 +01001589 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001590 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Ido Schimmel982acb92017-02-08 11:16:39 +01001591 break;
David Ahernb75ed8b2019-03-27 20:53:55 -07001592 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001593 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001594 case FIB_EVENT_NH_DEL:
David Ahernb75ed8b2019-03-27 20:53:55 -07001595 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1596 (nh->fib_nh_flags & RTNH_F_DEAD))
1597 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001598 event_type, &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001599 default:
1600 break;
1601 }
1602
1603 return NOTIFY_DONE;
1604}
1605
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001606/* Update the PMTU of exceptions when:
1607 * - the new MTU of the first hop becomes smaller than the PMTU
1608 * - the old MTU was the same as the PMTU, and it limited discovery of
1609 * larger MTUs on the path. With that limit raised, we can now
1610 * discover larger MTUs
1611 * A special case is locked exceptions, for which the PMTU is smaller
1612 * than the minimal accepted PMTU:
1613 * - if the new MTU is greater than the PMTU, don't make any change
1614 * - otherwise, unlock and set PMTU
1615 */
1616static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1617{
1618 struct fnhe_hash_bucket *bucket;
1619 int i;
1620
1621 bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1622 if (!bucket)
1623 return;
1624
1625 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1626 struct fib_nh_exception *fnhe;
1627
1628 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1629 fnhe;
1630 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1631 if (fnhe->fnhe_mtu_locked) {
1632 if (new <= fnhe->fnhe_pmtu) {
1633 fnhe->fnhe_pmtu = new;
1634 fnhe->fnhe_mtu_locked = false;
1635 }
1636 } else if (new < fnhe->fnhe_pmtu ||
1637 orig == fnhe->fnhe_pmtu) {
1638 fnhe->fnhe_pmtu = new;
1639 }
1640 }
1641 }
1642}
1643
1644void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1645{
1646 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1647 struct hlist_head *head = &fib_info_devhash[hash];
1648 struct fib_nh *nh;
1649
1650 hlist_for_each_entry(nh, head, nh_hash) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001651 if (nh->fib_nh_dev == dev)
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001652 nh_update_mtu(nh, dev->mtu, orig_mtu);
1653 }
1654}
1655
Julian Anastasov4f823de2015-10-30 10:23:33 +02001656/* Event force Flags Description
1657 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1658 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1659 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1660 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1661 */
1662int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 int ret = 0;
1665 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001666 struct fib_info *prev_fi = NULL;
1667 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1668 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001669 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001670
Julian Anastasov4f823de2015-10-30 10:23:33 +02001671 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 scope = -1;
1673
Sasha Levinb67bfe02013-02-27 17:06:00 -08001674 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001675 struct fib_info *fi = nh->nh_parent;
1676 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001678 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001679 if (nh->fib_nh_dev != dev || fi == prev_fi)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001680 continue;
1681 prev_fi = fi;
1682 dead = 0;
1683 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001684 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001685 dead++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001686 else if (nexthop_nh->fib_nh_dev == dev &&
1687 nexthop_nh->fib_nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001688 switch (event) {
1689 case NETDEV_DOWN:
1690 case NETDEV_UNREGISTER:
David Ahernb75ed8b2019-03-27 20:53:55 -07001691 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001692 /* fall through */
1693 case NETDEV_CHANGE:
David Ahernb75ed8b2019-03-27 20:53:55 -07001694 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001695 break;
1696 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001697 call_fib_nh_notifiers(nexthop_nh,
1698 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001699 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001701#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001702 if (event == NETDEV_UNREGISTER &&
David Ahernb75ed8b2019-03-27 20:53:55 -07001703 nexthop_nh->fib_nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001704 dead = fi->fib_nhs;
1705 break;
1706 }
1707#endif
1708 } endfor_nexthops(fi)
1709 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001710 switch (event) {
1711 case NETDEV_DOWN:
1712 case NETDEV_UNREGISTER:
1713 fi->fib_flags |= RTNH_F_DEAD;
1714 /* fall through */
1715 case NETDEV_CHANGE:
1716 fi->fib_flags |= RTNH_F_LINKDOWN;
1717 break;
1718 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001719 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001721
1722 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
1725 return ret;
1726}
1727
David S. Miller0c838ff2011-01-31 16:16:50 -08001728/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001729static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001730{
1731 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001732 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001733 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001734 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001735 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001736 struct fib_alias *fa, *fa1 = NULL;
1737 u32 last_prio = res->fi->fib_priority;
1738 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001739
Alexander Duyck56315f92015-02-25 15:31:31 -08001740 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001741 struct fib_info *next_fi = fa->fa_info;
1742
Julian Anastasov18a912e92015-07-22 10:43:22 +03001743 if (fa->fa_slen != slen)
1744 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001745 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1746 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001747 if (fa->tb_id != tb->tb_id)
1748 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001749 if (next_fi->fib_priority > last_prio &&
1750 fa->fa_tos == last_tos) {
1751 if (last_tos)
1752 continue;
1753 break;
1754 }
1755 if (next_fi->fib_flags & RTNH_F_DEAD)
1756 continue;
1757 last_tos = fa->fa_tos;
1758 last_prio = next_fi->fib_priority;
1759
David S. Miller37e826c2011-03-24 18:06:47 -07001760 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001761 fa->fa_type != RTN_UNICAST)
1762 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001763 if (!next_fi->fib_nh[0].fib_nh_gw4 ||
1764 next_fi->fib_nh[0].fib_nh_scope != RT_SCOPE_LINK)
David S. Miller0c838ff2011-01-31 16:16:50 -08001765 continue;
1766
1767 fib_alias_accessed(fa);
1768
Ian Morris51456b22015-04-03 09:17:26 +01001769 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001770 if (next_fi != res->fi)
1771 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001772 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001773 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001774 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001775 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001776 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001777 goto out;
1778 }
1779 fi = next_fi;
1780 order++;
1781 }
1782
Ian Morris51456b22015-04-03 09:17:26 +01001783 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001784 if (fa1)
1785 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001786 goto out;
1787 }
1788
1789 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001790 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001791 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001792 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001793 goto out;
1794 }
1795
1796 if (last_idx >= 0)
1797 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001798 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001799out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001800 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001804 * Dead device goes up. We wake up dead nexthops.
1805 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001807int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct fib_info *prev_fi;
1810 unsigned int hash;
1811 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct fib_nh *nh;
1813 int ret;
1814
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001815 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817
Julian Anastasovc9b32922015-10-30 10:23:34 +02001818 if (nh_flags & RTNH_F_DEAD) {
1819 unsigned int flags = dev_get_flags(dev);
1820
1821 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1822 nh_flags |= RTNH_F_LINKDOWN;
1823 }
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 prev_fi = NULL;
1826 hash = fib_devindex_hashfn(dev->ifindex);
1827 head = &fib_info_devhash[hash];
1828 ret = 0;
1829
Sasha Levinb67bfe02013-02-27 17:06:00 -08001830 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct fib_info *fi = nh->nh_parent;
1832 int alive;
1833
1834 BUG_ON(!fi->fib_nhs);
David Ahernb75ed8b2019-03-27 20:53:55 -07001835 if (nh->fib_nh_dev != dev || fi == prev_fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 continue;
1837
1838 prev_fi = fi;
1839 alive = 0;
1840 change_nexthops(fi) {
David Ahernb75ed8b2019-03-27 20:53:55 -07001841 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 alive++;
1843 continue;
1844 }
David Ahernb75ed8b2019-03-27 20:53:55 -07001845 if (!nexthop_nh->fib_nh_dev ||
1846 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 continue;
David Ahernb75ed8b2019-03-27 20:53:55 -07001848 if (nexthop_nh->fib_nh_dev != dev ||
David S. Miller71fceff2010-01-15 01:16:40 -08001849 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 continue;
1851 alive++;
David Ahernb75ed8b2019-03-27 20:53:55 -07001852 nexthop_nh->fib_nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001853 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 } endfor_nexthops(fi)
1855
1856 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001857 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 ret++;
1859 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001860
1861 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863
1864 return ret;
1865}
1866
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001867#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001868static bool fib_good_nh(const struct fib_nh *nh)
1869{
1870 int state = NUD_REACHABLE;
1871
David Ahernb75ed8b2019-03-27 20:53:55 -07001872 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
David Aherna6db4492016-04-07 07:21:00 -07001873 struct neighbour *n;
1874
1875 rcu_read_lock_bh();
1876
David Ahernb75ed8b2019-03-27 20:53:55 -07001877 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1878 (__force u32)nh->fib_nh_gw4);
David Aherna6db4492016-04-07 07:21:00 -07001879 if (n)
1880 state = n->nud_state;
1881
1882 rcu_read_unlock_bh();
1883 }
1884
1885 return !!(state & NUD_VALID);
1886}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001887
Peter Nørlund0e884c72015-09-30 10:12:21 +02001888void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001891 struct net *net = fi->fib_net;
1892 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
David Aherneba618a2019-04-02 14:11:55 -07001894 change_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08001895 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Aherneba618a2019-04-02 14:11:55 -07001896 if (!fib_good_nh(nexthop_nh))
Xin Long6174a302018-04-01 22:40:35 +08001897 continue;
1898 if (!first) {
1899 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001900 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08001901 first = true;
1902 }
1903 }
1904
David Aherneba618a2019-04-02 14:11:55 -07001905 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Peter Nørlund0e884c72015-09-30 10:12:21 +02001906 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Xin Long6174a302018-04-01 22:40:35 +08001908 res->nh_sel = nhsel;
David Aherneba618a2019-04-02 14:11:55 -07001909 res->nhc = &nexthop_nh->nh_common;
Xin Long6174a302018-04-01 22:40:35 +08001910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913#endif
David Ahern3ce58d82015-10-05 08:51:25 -07001914
1915void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001916 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07001917{
David Ahern0d876f22018-02-13 08:11:34 -08001918 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
1919 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08001920
David Ahern3ce58d82015-10-05 08:51:25 -07001921#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern0d876f22018-02-13 08:11:34 -08001922 if (res->fi->fib_nhs > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08001923 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01001924
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001925 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07001926 }
1927 else
1928#endif
1929 if (!res->prefixlen &&
1930 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08001931 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07001932 fib_select_default(fl4, res);
1933
David Ahern0d876f22018-02-13 08:11:34 -08001934check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07001935 if (!fl4->saddr)
David Aherneba618a2019-04-02 14:11:55 -07001936 fl4->saddr = fib_result_prefsrc(net, res);
David Ahern3ce58d82015-10-05 08:51:25 -07001937}