blob: 446204ca7406634253cf8a9fdee0629d2cefbaa6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "fib_lookup.h"
50
Stephen Hemminger832b4c52006-08-29 16:48:09 -070051static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static struct hlist_head *fib_info_hash;
53static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080054static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static unsigned int fib_info_cnt;
56
57#define DEVINDEX_HASHBITS 8
58#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
59static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
60
61#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000063#define for_nexthops(fi) { \
64 int nhsel; const struct fib_nh *nh; \
65 for (nhsel = 0, nh = (fi)->fib_nh; \
66 nhsel < (fi)->fib_nhs; \
67 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000069#define change_nexthops(fi) { \
70 int nhsel; struct fib_nh *nexthop_nh; \
71 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72 nhsel < (fi)->fib_nhs; \
73 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#else /* CONFIG_IP_ROUTE_MULTIPATH */
76
77/* Hope, that gcc will optimize it to get rid of dummy loop */
78
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000079#define for_nexthops(fi) { \
80 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
81 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000083#define change_nexthops(fi) { \
84 int nhsel; \
85 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
86 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#endif /* CONFIG_IP_ROUTE_MULTIPATH */
89
90#define endfor_nexthops(fi) }
91
92
David S. Miller3be06862011-03-07 15:01:10 -080093const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000094 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .error = 0,
96 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000097 },
98 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .error = 0,
100 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000101 },
102 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .error = 0,
104 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000105 },
106 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .error = 0,
108 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000109 },
110 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .error = 0,
112 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000113 },
114 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .error = 0,
116 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000117 },
118 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .error = -EINVAL,
120 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000121 },
122 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .error = -EHOSTUNREACH,
124 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000125 },
126 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .error = -EACCES,
128 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000129 },
130 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .error = -EAGAIN,
132 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000133 },
134 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .error = -EINVAL,
136 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000137 },
138 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .error = -EINVAL,
140 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000141 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
David S. Millerc5038a82012-07-31 15:02:02 -0700144static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000145{
146 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
147
148 if (!rt)
149 return;
150
151 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
152 * because we waited an RCU grace period before calling
153 * free_fib_info_rcu()
154 */
155
Wei Wang95c47f92017-06-17 10:42:30 -0700156 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700157 dst_release_immediate(&rt->dst);
Eric Dumazet54764bb2012-07-31 01:08:23 +0000158}
159
David S. Millerc5038a82012-07-31 15:02:02 -0700160static void free_nh_exceptions(struct fib_nh *nh)
161{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700162 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700163 int i;
164
Eric Dumazetcaa41522014-09-03 22:21:56 -0700165 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
166 if (!hash)
167 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700168 for (i = 0; i < FNHE_HASH_SIZE; i++) {
169 struct fib_nh_exception *fnhe;
170
171 fnhe = rcu_dereference_protected(hash[i].chain, 1);
172 while (fnhe) {
173 struct fib_nh_exception *next;
Stephen Hemminger82695b32018-02-27 15:48:21 -0800174
David S. Millerc5038a82012-07-31 15:02:02 -0700175 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
176
Timo Teräs2ffae992013-06-27 10:27:05 +0300177 rt_fibinfo_free(&fnhe->fnhe_rth_input);
178 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700179
180 kfree(fnhe);
181
182 fnhe = next;
183 }
184 }
185 kfree(hash);
186}
187
188static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000189{
190 int cpu;
191
192 if (!rtp)
193 return;
194
195 for_each_possible_cpu(cpu) {
196 struct rtable *rt;
197
198 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
Wei Wang08301062017-06-17 10:42:29 -0700199 if (rt) {
Wei Wang95c47f92017-06-17 10:42:30 -0700200 dst_dev_put(&rt->dst);
Wei Wangb838d5e2017-06-17 10:42:32 -0700201 dst_release_immediate(&rt->dst);
Wei Wang08301062017-06-17 10:42:29 -0700202 }
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000203 }
204 free_percpu(rtp);
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000208static void free_fib_info_rcu(struct rcu_head *head)
209{
210 struct fib_info *fi = container_of(head, struct fib_info, rcu);
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700211 struct dst_metrics *m;
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000212
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000213 change_nexthops(fi) {
214 if (nexthop_nh->nh_dev)
215 dev_put(nexthop_nh->nh_dev);
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200216 lwtstate_put(nexthop_nh->nh_lwtstate);
Eric Dumazetcaa41522014-09-03 22:21:56 -0700217 free_nh_exceptions(nexthop_nh);
David S. Millerc5038a82012-07-31 15:02:02 -0700218 rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
219 rt_fibinfo_free(&nexthop_nh->nh_rth_input);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000220 } endfor_nexthops(fi);
221
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700222 m = fi->fib_metrics;
Eric Dumazet9620fef2017-08-18 12:08:07 -0700223 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700224 kfree(m);
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000225 kfree(fi);
226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228void free_fib_info(struct fib_info *fi)
229{
230 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000231 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 fib_info_cnt--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700235#ifdef CONFIG_IP_ROUTE_CLASSID
236 change_nexthops(fi) {
237 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700238 fi->fib_net->ipv4.fib_num_tclassid_users--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700239 } endfor_nexthops(fi);
240#endif
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000241 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100243EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245void fib_release_info(struct fib_info *fi)
246{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700247 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (fi && --fi->fib_treeref == 0) {
249 hlist_del(&fi->fib_hash);
250 if (fi->fib_prefsrc)
251 hlist_del(&fi->fib_lhash);
252 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800253 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800255 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } endfor_nexthops(fi)
257 fi->fib_dead = 1;
258 fib_info_put(fi);
259 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700260 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000263static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 const struct fib_nh *onh = ofi->fib_nh;
266
267 for_nexthops(fi) {
268 if (nh->nh_oif != onh->nh_oif ||
269 nh->nh_gw != onh->nh_gw ||
270 nh->nh_scope != onh->nh_scope ||
271#ifdef CONFIG_IP_ROUTE_MULTIPATH
272 nh->nh_weight != onh->nh_weight ||
273#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100274#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 nh->nh_tclassid != onh->nh_tclassid ||
276#endif
Roopa Prabhu571e7222015-07-21 10:43:47 +0200277 lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400278 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -1;
280 onh++;
281 } endfor_nexthops(fi);
282 return 0;
283}
284
David S. Miller88ebc722008-01-12 21:49:01 -0800285static inline unsigned int fib_devindex_hashfn(unsigned int val)
286{
287 unsigned int mask = DEVINDEX_HASHSIZE - 1;
288
289 return (val ^
290 (val >> DEVINDEX_HASHBITS) ^
291 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
295{
David S. Miller123b9732011-02-01 15:34:21 -0800296 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned int val = fi->fib_nhs;
298
David S. Miller37e826c2011-03-24 18:06:47 -0700299 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700300 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800302 for_nexthops(fi) {
303 val ^= fib_devindex_hashfn(nh->nh_oif);
304 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
307}
308
309static struct fib_info *fib_find_info(const struct fib_info *nfi)
310{
311 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct fib_info *fi;
313 unsigned int hash;
314
315 hash = fib_info_hashfn(nfi);
316 head = &fib_info_hash[hash];
317
Sasha Levinb67bfe02013-02-27 17:06:00 -0800318 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800319 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800320 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (fi->fib_nhs != nfi->fib_nhs)
322 continue;
323 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700324 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 nfi->fib_prefsrc == fi->fib_prefsrc &&
326 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000327 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000329 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400330 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
332 return fi;
333 }
334
335 return NULL;
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000339 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Al Virod878e72e2006-09-26 22:18:13 -0700341int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct fib_nh *nh;
345 unsigned int hash;
346
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700347 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 hash = fib_devindex_hashfn(dev->ifindex);
350 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800351 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (nh->nh_dev == dev &&
353 nh->nh_gw == gw &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000354 !(nh->nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700355 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357 }
358 }
359
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700360 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 return -1;
363}
364
Thomas Graf339bf982006-11-10 14:10:15 -0800365static inline size_t fib_nlmsg_size(struct fib_info *fi)
366{
367 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
368 + nla_total_size(4) /* RTA_TABLE */
369 + nla_total_size(4) /* RTA_DST */
370 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100371 + nla_total_size(4) /* RTA_PREFSRC */
372 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800373
374 /* space for nested metrics */
375 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
376
377 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200378 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800379 /* Also handles the special case fib_nhs == 1 */
380
381 /* each nexthop is packed in an attribute */
382 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
383
384 /* may contain flow and gateway attribute */
385 nhsize += 2 * nla_total_size(4);
386
Roopa Prabhu571e7222015-07-21 10:43:47 +0200387 /* grab encap info */
388 for_nexthops(fi) {
389 if (nh->nh_lwtstate) {
390 /* RTA_ENCAP_TYPE */
391 nh_encapsize += lwtunnel_get_encap_size(
392 nh->nh_lwtstate);
393 /* RTA_ENCAP */
394 nh_encapsize += nla_total_size(2);
395 }
396 } endfor_nexthops(fi);
397
Thomas Graf339bf982006-11-10 14:10:15 -0800398 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200399 payload += nla_total_size((fi->fib_nhs * nhsize) +
400 nh_encapsize);
401
Thomas Graf339bf982006-11-10 14:10:15 -0800402 }
403
404 return payload;
405}
406
Al Viro81f7bf62006-09-27 18:40:00 -0700407void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700408 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700409 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700412 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700413 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Graf339bf982006-11-10 14:10:15 -0800415 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100416 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700417 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Eric W. Biederman15e47302012-09-07 20:12:54 +0000419 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700420 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700421 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800422 if (err < 0) {
423 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
424 WARN_ON(err == -EMSGSIZE);
425 kfree_skb(skb);
426 goto errout;
427 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000428 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800429 info->nlh, GFP_KERNEL);
430 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700431errout:
432 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800433 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800436static int fib_detect_death(struct fib_info *fi, int order,
437 struct fib_info **last_resort, int *last_idx,
438 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct neighbour *n;
441 int state = NUD_NONE;
442
443 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
444 if (n) {
445 state = n->nud_state;
446 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300447 } else {
448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800450 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000452 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000454 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300455 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *last_resort = fi;
457 *last_idx = order;
458 }
459 return 1;
460}
461
462#ifdef CONFIG_IP_ROUTE_MULTIPATH
463
David Ahern6d8422a12017-05-21 10:12:02 -0600464static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
465 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Thomas Graf4e902c52006-08-17 18:14:52 -0700469 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700471 rtnh = rtnh_next(rtnh, &remaining);
472 }
473
474 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600475 if (remaining > 0) {
476 NL_SET_ERR_MSG(extack,
477 "Invalid nexthop configuration - extra data after nexthops");
478 nhs = 0;
479 }
480
481 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Thomas Graf4e902c52006-08-17 18:14:52 -0700484static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600485 int remaining, struct fib_config *cfg,
486 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Roopa Prabhu571e7222015-07-21 10:43:47 +0200488 int ret;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700491 int attrlen;
492
David Ahernc3ab2b42017-05-21 10:12:03 -0600493 if (!rtnh_ok(rtnh, remaining)) {
494 NL_SET_ERR_MSG(extack,
495 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600497 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700498
David Ahernc3ab2b42017-05-21 10:12:03 -0600499 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
500 NL_SET_ERR_MSG(extack,
501 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300502 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600503 }
Julian Anastasov80610222016-07-10 21:11:55 +0300504
David S. Miller71fceff2010-01-15 01:16:40 -0800505 nexthop_nh->nh_flags =
506 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
507 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
508 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700509
510 attrlen = rtnh_attrlen(rtnh);
511 if (attrlen > 0) {
512 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
513
514 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Jiri Benc67b61f62015-03-29 16:59:26 +0200515 nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100516#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700517 nla = nla_find(attrs, attrlen, RTA_FLOW);
David S. Miller71fceff2010-01-15 01:16:40 -0800518 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700519 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700520 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#endif
Roopa Prabhu571e7222015-07-21 10:43:47 +0200522 nla = nla_find(attrs, attrlen, RTA_ENCAP);
523 if (nla) {
524 struct lwtunnel_state *lwtstate;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200525 struct nlattr *nla_entype;
526
527 nla_entype = nla_find(attrs, attrlen,
528 RTA_ENCAP_TYPE);
David Ahernc3ab2b42017-05-21 10:12:03 -0600529 if (!nla_entype) {
530 NL_SET_BAD_ATTR(extack, nla);
531 NL_SET_ERR_MSG(extack,
532 "Encap type is missing");
Roopa Prabhu571e7222015-07-21 10:43:47 +0200533 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -0600534 }
David Ahern30357d72017-01-30 12:07:37 -0800535
536 ret = lwtunnel_build_state(nla_get_u16(
Roopa Prabhu571e7222015-07-21 10:43:47 +0200537 nla_entype),
Tom Herbert127eb7c2015-08-24 09:45:41 -0700538 nla, AF_INET, cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600539 &lwtstate, extack);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200540 if (ret)
541 goto errout;
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200542 nexthop_nh->nh_lwtstate =
543 lwtstate_get(lwtstate);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700546
547 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200551
552err_inval:
553 ret = -EINVAL;
554
555errout:
556 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Peter Nørlund0e884c72015-09-30 10:12:21 +0200559static void fib_rebalance(struct fib_info *fi)
560{
561 int total;
562 int w;
563 struct in_device *in_dev;
564
565 if (fi->fib_nhs < 2)
566 return;
567
568 total = 0;
569 for_nexthops(fi) {
570 if (nh->nh_flags & RTNH_F_DEAD)
571 continue;
572
David Ahern51161aa2015-10-14 16:44:00 -0700573 in_dev = __in_dev_get_rtnl(nh->nh_dev);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200574
575 if (in_dev &&
576 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
577 nh->nh_flags & RTNH_F_LINKDOWN)
578 continue;
579
580 total += nh->nh_weight;
581 } endfor_nexthops(fi);
582
583 w = 0;
584 change_nexthops(fi) {
585 int upper_bound;
586
David Ahern51161aa2015-10-14 16:44:00 -0700587 in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200588
589 if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
590 upper_bound = -1;
591 } else if (in_dev &&
592 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
593 nexthop_nh->nh_flags & RTNH_F_LINKDOWN) {
594 upper_bound = -1;
595 } else {
596 w += nexthop_nh->nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200597 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
598 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200599 }
600
601 atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
602 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200603}
Peter Nørlund0e884c72015-09-30 10:12:21 +0200604#else /* CONFIG_IP_ROUTE_MULTIPATH */
605
606#define fib_rebalance(fi) do { } while (0)
Peter Nørlund0e884c72015-09-30 10:12:21 +0200607
608#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
David Ahern30357d72017-01-30 12:07:37 -0800610static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800611 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800612 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600613 const struct fib_config *cfg,
614 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200615{
616 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200617 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200618
619 if (encap_type == LWTUNNEL_ENCAP_NONE)
620 return 0;
621
David Ahern9ae28722017-05-27 16:19:28 -0600622 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
623 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200624 if (!ret) {
625 result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
626 lwtstate_free(lwtstate);
627 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200628
Jiri Bencdf383e62015-08-18 18:41:13 +0200629 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200630}
631
David Ahern9ae28722017-05-27 16:19:28 -0600632int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
633 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700636 struct rtnexthop *rtnh;
637 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#endif
639
Thomas Graf4e902c52006-08-17 18:14:52 -0700640 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 1;
642
Thomas Graf4e902c52006-08-17 18:14:52 -0700643 if (cfg->fc_oif || cfg->fc_gw) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200644 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600645 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
646 fi->fib_nh, cfg, extack))
647 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200648 }
Stefano Brivioa8c6db12018-02-15 09:46:03 +0100649#ifdef CONFIG_IP_ROUTE_CLASSID
650 if (cfg->fc_flow &&
651 cfg->fc_flow != fi->fib_nh->nh_tclassid)
652 return 1;
653#endif
Thomas Graf4e902c52006-08-17 18:14:52 -0700654 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
655 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657 return 1;
658 }
659
660#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100661 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700663
664 rtnh = cfg->fc_mp;
665 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700668 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thomas Graf4e902c52006-08-17 18:14:52 -0700670 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700672
673 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700675
676 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200677 if (attrlen > 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700678 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
679
680 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Jiri Benc67b61f62015-03-29 16:59:26 +0200681 if (nla && nla_get_in_addr(nla) != nh->nh_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100683#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700684 nla = nla_find(attrs, attrlen, RTA_FLOW);
685 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 1;
687#endif
688 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700689
690 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 } endfor_nexthops(fi);
692#endif
693 return 0;
694}
695
Xin Long5f9ae3d2017-08-23 10:07:26 +0800696bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
697{
698 struct nlattr *nla;
699 int remaining;
700
701 if (!cfg->fc_mx)
702 return true;
703
704 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
705 int type = nla_type(nla);
Phil Sutterd03a4552017-12-19 15:17:13 +0100706 u32 fi_val, val;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800707
708 if (!type)
709 continue;
710 if (type > RTAX_MAX)
711 return false;
712
713 if (type == RTAX_CC_ALGO) {
714 char tmp[TCP_CA_NAME_MAX];
715 bool ecn_ca = false;
716
717 nla_strlcpy(tmp, nla, sizeof(tmp));
Stephen Hemminger6670e152017-11-14 08:25:49 -0800718 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
Xin Long5f9ae3d2017-08-23 10:07:26 +0800719 } else {
Eric Dumazet5b5e7a02018-06-05 06:06:19 -0700720 if (nla_len(nla) != sizeof(u32))
721 return false;
Xin Long5f9ae3d2017-08-23 10:07:26 +0800722 val = nla_get_u32(nla);
723 }
724
Phil Sutterd03a4552017-12-19 15:17:13 +0100725 fi_val = fi->fib_metrics->metrics[type - 1];
726 if (type == RTAX_FEATURES)
727 fi_val &= ~DST_FEATURE_ECN_CA;
728
729 if (fi_val != val)
Xin Long5f9ae3d2017-08-23 10:07:26 +0800730 return false;
731 }
732
733 return true;
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000738 * Picture
739 * -------
740 *
741 * Semantics of nexthop is very messy by historical reasons.
742 * We have to take into account, that:
743 * a) gateway can be actually local interface address,
744 * so that gatewayed route is direct.
745 * b) gateway must be on-link address, possibly
746 * described not by an ifaddr, but also by a direct route.
747 * c) If both gateway and interface are specified, they should not
748 * contradict.
749 * d) If we use tunnel routes, gateway could be not on-link.
750 *
751 * Attempt to reconcile all of these (alas, self-contradictory) conditions
752 * results in pretty ugly and hairy code with obscure logic.
753 *
754 * I chose to generalized it instead, so that the size
755 * of code does not increase practically, but it becomes
756 * much more general.
757 * Every prefix is assigned a "scope" value: "host" is local address,
758 * "link" is direct route,
759 * [ ... "site" ... "interior" ... ]
760 * and "universe" is true gateway route with global meaning.
761 *
762 * Every prefix refers to a set of "nexthop"s (gw, oif),
763 * where gw must have narrower scope. This recursion stops
764 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
765 * which means that gw is forced to be on link.
766 *
767 * Code is still hairy, but now it is apparently logically
768 * consistent and very flexible. F.e. as by-product it allows
769 * to co-exists in peace independent exterior and interior
770 * routing processes.
771 *
772 * Normally it looks as following.
773 *
774 * {universe prefix} -> (gw, oif) [scope link]
775 * |
776 * |-> {link prefix} -> (gw, oif) [scope local]
777 * |
778 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
David Ahernfa8fefa2017-09-27 20:41:59 -0700780static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
781 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Tom Herbert127eb7c2015-08-24 09:45:41 -0700783 int err = 0;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800784 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000785 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Denis V. Lunev86167a32008-01-21 17:34:00 -0800787 net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (nh->nh_gw) {
789 struct fib_result res;
790
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000791 if (nh->nh_flags & RTNH_F_ONLINK) {
David Ahern30bbaa12015-08-13 14:59:05 -0600792 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
David Ahernc3ab2b42017-05-21 10:12:03 -0600794 if (cfg->fc_scope >= RT_SCOPE_LINK) {
795 NL_SET_ERR_MSG(extack,
796 "Nexthop has invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600798 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000799 dev = __dev_get_by_index(net, nh->nh_oif);
800 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -ENODEV;
David Ahernc3ab2b42017-05-21 10:12:03 -0600802 if (!(dev->flags & IFF_UP)) {
803 NL_SET_ERR_MSG(extack,
804 "Nexthop device is not up");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600806 }
David Ahern30bbaa12015-08-13 14:59:05 -0600807 addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
David Ahernc3ab2b42017-05-21 10:12:03 -0600808 if (addr_type != RTN_UNICAST) {
809 NL_SET_ERR_MSG(extack,
810 "Nexthop has invalid gateway");
David Ahern30bbaa12015-08-13 14:59:05 -0600811 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600812 }
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400813 if (!netif_carrier_ok(dev))
814 nh->nh_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 nh->nh_dev = dev;
816 dev_hold(dev);
817 nh->nh_scope = RT_SCOPE_LINK;
818 return 0;
819 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000820 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 {
David Ahern3bfd8472015-08-13 14:59:07 -0600822 struct fib_table *tbl = NULL;
David S. Miller9ade2282011-03-12 02:02:42 -0500823 struct flowi4 fl4 = {
824 .daddr = nh->nh_gw,
825 .flowi4_scope = cfg->fc_scope + 1,
826 .flowi4_oif = nh->nh_oif,
Cong Wang6a662712014-04-15 16:25:34 -0700827 .flowi4_iif = LOOPBACK_IFINDEX,
Thomas Graf4e902c52006-08-17 18:14:52 -0700828 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500831 if (fl4.flowi4_scope < RT_SCOPE_LINK)
832 fl4.flowi4_scope = RT_SCOPE_LINK;
David Ahern3bfd8472015-08-13 14:59:07 -0600833
834 if (cfg->fc_table)
835 tbl = fib_get_table(net, cfg->fc_table);
836
837 if (tbl)
838 err = fib_table_lookup(tbl, &fl4, &res,
Eric Dumazet1e313672015-08-15 10:54:07 -0700839 FIB_LOOKUP_IGNORE_LINKSTATE |
840 FIB_LOOKUP_NOREF);
David Ahern4c9bcd12015-08-19 11:40:31 -0700841
842 /* on error or if no table given do full lookup. This
843 * is needed for example when nexthops are in the local
844 * table rather than the given table
845 */
846 if (!tbl || err) {
David Ahern3bfd8472015-08-13 14:59:07 -0600847 err = fib_lookup(net, &fl4, &res,
848 FIB_LOOKUP_IGNORE_LINKSTATE);
David Ahern4c9bcd12015-08-19 11:40:31 -0700849 }
850
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000851 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600852 NL_SET_ERR_MSG(extack,
853 "Nexthop has invalid gateway");
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000854 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 err = -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600859 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
860 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 nh->nh_scope = res.scope;
864 nh->nh_oif = FIB_RES_OIF(res);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000865 nh->nh_dev = dev = FIB_RES_DEV(res);
David Ahernc3ab2b42017-05-21 10:12:03 -0600866 if (!dev) {
867 NL_SET_ERR_MSG(extack,
868 "No egress device for nexthop gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600870 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000871 dev_hold(dev);
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400872 if (!netif_carrier_ok(dev))
873 nh->nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000874 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 } else {
876 struct in_device *in_dev;
877
David Ahernc3ab2b42017-05-21 10:12:03 -0600878 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
879 NL_SET_ERR_MSG(extack,
880 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600882 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000883 rcu_read_lock();
884 err = -ENODEV;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800885 in_dev = inetdev_by_index(net, nh->nh_oif);
Ian Morris51456b22015-04-03 09:17:26 +0100886 if (!in_dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000887 goto out;
888 err = -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600889 if (!(in_dev->dev->flags & IFF_UP)) {
890 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000891 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 nh->nh_dev = in_dev->dev;
894 dev_hold(nh->nh_dev);
895 nh->nh_scope = RT_SCOPE_HOST;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400896 if (!netif_carrier_ok(nh->nh_dev))
897 nh->nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000898 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000900out:
901 rcu_read_unlock();
902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Al Viro81f7bf62006-09-27 18:40:00 -0700905static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
David S. Miller123b9732011-02-01 15:34:21 -0800907 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000909 return ((__force u32)val ^
910 ((__force u32)val >> 7) ^
911 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
David S. Miller123b9732011-02-01 15:34:21 -0800914static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800917 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 else
919 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000920 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
921 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
David S. Miller123b9732011-02-01 15:34:21 -0800924static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 if (!hash)
927 return;
928
929 if (bytes <= PAGE_SIZE)
930 kfree(hash);
931 else
932 free_pages((unsigned long) hash, get_order(bytes));
933}
934
David S. Miller123b9732011-02-01 15:34:21 -0800935static void fib_info_hash_move(struct hlist_head *new_info_hash,
936 struct hlist_head *new_laddrhash,
937 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
David S. Millerb7656e72005-08-05 04:12:48 -0700939 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800940 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -0700941 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700943 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700944 old_info_hash = fib_info_hash;
945 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800946 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 for (i = 0; i < old_size; i++) {
949 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800950 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct fib_info *fi;
952
Sasha Levinb67bfe02013-02-27 17:06:00 -0800953 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct hlist_head *dest;
955 unsigned int new_hash;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 new_hash = fib_info_hashfn(fi);
958 dest = &new_info_hash[new_hash];
959 hlist_add_head(&fi->fib_hash, dest);
960 }
961 }
962 fib_info_hash = new_info_hash;
963
964 for (i = 0; i < old_size; i++) {
965 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800966 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct fib_info *fi;
968
Sasha Levinb67bfe02013-02-27 17:06:00 -0800969 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct hlist_head *ldest;
971 unsigned int new_hash;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
974 ldest = &new_laddrhash[new_hash];
975 hlist_add_head(&fi->fib_lhash, ldest);
976 }
977 }
978 fib_info_laddrhash = new_laddrhash;
979
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700980 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700981
982 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800983 fib_info_hash_free(old_info_hash, bytes);
984 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
David S. Miller436c3b62011-03-24 17:42:21 -0700987__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
988{
989 nh->nh_saddr = inet_select_addr(nh->nh_dev,
990 nh->nh_gw,
David S. Miller37e826c2011-03-24 18:06:47 -0700991 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -0700992 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
993
994 return nh->nh_saddr;
995}
996
David Ahern021dd3b2015-08-13 14:59:06 -0600997static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
998{
999 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1000 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -06001001 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -08001002 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -06001003
1004 if (tb_id == RT_TABLE_MAIN)
1005 tb_id = RT_TABLE_LOCAL;
1006
David Aherne1b8d902015-11-03 15:59:28 -08001007 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1008 fib_prefsrc, tb_id);
1009
1010 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1011 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1012 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -06001013 }
David Aherne1b8d902015-11-03 15:59:28 -08001014
1015 if (rc != RTN_LOCAL)
1016 return false;
David Ahern021dd3b2015-08-13 14:59:06 -06001017 }
1018 return true;
1019}
1020
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001021static int
1022fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
1023{
David Aherna9195252018-04-17 17:33:07 -07001024 return ip_metrics_convert(fi->fib_net, cfg->fc_mx, cfg->fc_mx_len,
1025 fi->fib_metrics->metrics);
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001026}
1027
David Ahern6d8422a12017-05-21 10:12:02 -06001028struct fib_info *fib_create_info(struct fib_config *cfg,
1029 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 int err;
1032 struct fib_info *fi = NULL;
1033 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001035 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
David S. Miller4c8237c2011-03-07 14:27:38 -08001037 if (cfg->fc_type > RTN_MAX)
1038 goto err_inval;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001041 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1042 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
David Ahernc3ab2b42017-05-21 10:12:03 -06001046 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1047 NL_SET_ERR_MSG(extack,
1048 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001049 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001050 }
Julian Anastasov80610222016-07-10 21:11:55 +03001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001053 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001054 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (nhs == 0)
1056 goto err_inval;
1057 }
1058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001061 if (fib_info_cnt >= fib_info_hash_size) {
1062 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct hlist_head *new_info_hash;
1064 struct hlist_head *new_laddrhash;
1065 unsigned int bytes;
1066
1067 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001068 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001070 new_info_hash = fib_info_hash_alloc(bytes);
1071 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001073 fib_info_hash_free(new_info_hash, bytes);
1074 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001075 } else
David S. Miller123b9732011-02-01 15:34:21 -08001076 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
David S. Miller123b9732011-02-01 15:34:21 -08001078 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto failure;
1080 }
1081
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001082 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001083 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto failure;
David S. Miller725d1e12011-01-28 14:05:05 -08001085 if (cfg->fc_mx) {
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001086 fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001087 if (unlikely(!fi->fib_metrics)) {
1088 kfree(fi);
1089 return ERR_PTR(err);
1090 }
Eric Dumazet9620fef2017-08-18 12:08:07 -07001091 refcount_set(&fi->fib_metrics->refcnt, 1);
Eric Dumazet187e5b32017-08-15 05:26:17 -07001092 } else {
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001093 fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
Eric Dumazet187e5b32017-08-15 05:26:17 -07001094 }
1095 fib_info_cnt++;
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001096 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001097 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001098 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001099 fi->fib_flags = cfg->fc_flags;
1100 fi->fib_priority = cfg->fc_priority;
1101 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001102 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001103 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 fi->fib_nhs = nhs;
1106 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001107 nexthop_nh->nh_parent = fi;
Eric Dumazetd26b3a72012-07-31 05:45:30 +00001108 nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
Julian Anastasovf8a17172012-10-08 11:41:17 +00001109 if (!nexthop_nh->nh_pcpu_rth_output)
1110 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 } endfor_nexthops(fi)
1112
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001113 err = fib_convert_metrics(fi, cfg);
1114 if (err)
1115 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Thomas Graf4e902c52006-08-17 18:14:52 -07001117 if (cfg->fc_mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern6d8422a12017-05-21 10:12:02 -06001119 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001120 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 goto failure;
David Ahernc3ab2b42017-05-21 10:12:03 -06001122 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
1123 NL_SET_ERR_MSG(extack,
1124 "Nexthop device index does not match RTA_OIF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001126 }
1127 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
1128 NL_SET_ERR_MSG(extack,
1129 "Nexthop gateway does not match RTA_GATEWAY");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001131 }
Patrick McHardyc7066f72011-01-14 13:36:42 +01001132#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernc3ab2b42017-05-21 10:12:03 -06001133 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
1134 NL_SET_ERR_MSG(extack,
1135 "Nexthop class id does not match RTA_FLOW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138#endif
1139#else
David Ahernc3ab2b42017-05-21 10:12:03 -06001140 NL_SET_ERR_MSG(extack,
1141 "Multipath support not enabled in kernel");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto err_inval;
1143#endif
1144 } else {
1145 struct fib_nh *nh = fi->fib_nh;
Thomas Graf4e902c52006-08-17 18:14:52 -07001146
Roopa Prabhu571e7222015-07-21 10:43:47 +02001147 if (cfg->fc_encap) {
1148 struct lwtunnel_state *lwtstate;
Roopa Prabhu571e7222015-07-21 10:43:47 +02001149
David Ahernc3ab2b42017-05-21 10:12:03 -06001150 if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
1151 NL_SET_ERR_MSG(extack,
1152 "LWT encap type not specified");
Roopa Prabhu571e7222015-07-21 10:43:47 +02001153 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001154 }
David Ahern30357d72017-01-30 12:07:37 -08001155 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07001156 cfg->fc_encap, AF_INET, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06001157 &lwtstate, extack);
Roopa Prabhu571e7222015-07-21 10:43:47 +02001158 if (err)
1159 goto failure;
1160
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +02001161 nh->nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu571e7222015-07-21 10:43:47 +02001162 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001163 nh->nh_oif = cfg->fc_oif;
1164 nh->nh_gw = cfg->fc_gw;
1165 nh->nh_flags = cfg->fc_flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001166#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -07001167 nh->nh_tclassid = cfg->fc_flow;
David S. Miller7a9bc9b2012-06-29 01:32:45 -07001168 if (nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -07001169 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#ifdef CONFIG_IP_ROUTE_MULTIPATH
1172 nh->nh_weight = 1;
1173#endif
1174 }
1175
Thomas Graf4e902c52006-08-17 18:14:52 -07001176 if (fib_props[cfg->fc_type].error) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001177 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1178 NL_SET_ERR_MSG(extack,
1179 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001183 } else {
1184 switch (cfg->fc_type) {
1185 case RTN_UNICAST:
1186 case RTN_LOCAL:
1187 case RTN_BROADCAST:
1188 case RTN_ANYCAST:
1189 case RTN_MULTICAST:
1190 break;
1191 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001192 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001193 goto err_inval;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
David Ahernc3ab2b42017-05-21 10:12:03 -06001197 if (cfg->fc_scope > RT_SCOPE_HOST) {
1198 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Thomas Graf4e902c52006-08-17 18:14:52 -07001202 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct fib_nh *nh = fi->fib_nh;
1204
1205 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001206 if (nhs != 1) {
1207 NL_SET_ERR_MSG(extack,
1208 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001209 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001210 }
1211 if (nh->nh_gw) {
1212 NL_SET_ERR_MSG(extack,
1213 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 nh->nh_scope = RT_SCOPE_NOWHERE;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001217 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 err = -ENODEV;
Ian Morris51456b22015-04-03 09:17:26 +01001219 if (!nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto failure;
1221 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001222 int linkdown = 0;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 change_nexthops(fi) {
David Ahernfa8fefa2017-09-27 20:41:59 -07001225 err = fib_check_nh(cfg, nexthop_nh, extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001226 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto failure;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001228 if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
1229 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001231 if (linkdown == fi->fib_nhs)
1232 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
David Ahernc3ab2b42017-05-21 10:12:03 -06001235 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1236 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001237 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
David S. Miller1fc050a2011-03-07 20:54:48 -08001240 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001241 fib_info_update_nh_saddr(net, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -08001242 } endfor_nexthops(fi)
1243
Peter Nørlund0e884c72015-09-30 10:12:21 +02001244 fib_rebalance(fi);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001247 ofi = fib_find_info(fi);
1248 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 fi->fib_dead = 1;
1250 free_fib_info(fi);
1251 ofi->fib_treeref++;
1252 return ofi;
1253 }
1254
1255 fi->fib_treeref++;
Reshetova, Elena0029c0d2017-07-04 09:35:02 +03001256 refcount_set(&fi->fib_clntref, 1);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001257 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 hlist_add_head(&fi->fib_hash,
1259 &fib_info_hash[fib_info_hashfn(fi)]);
1260 if (fi->fib_prefsrc) {
1261 struct hlist_head *head;
1262
1263 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1264 hlist_add_head(&fi->fib_lhash, head);
1265 }
1266 change_nexthops(fi) {
1267 struct hlist_head *head;
1268 unsigned int hash;
1269
David S. Miller71fceff2010-01-15 01:16:40 -08001270 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001272 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001274 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001276 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return fi;
1278
1279err_inval:
1280 err = -EINVAL;
1281
1282failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001283 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 fi->fib_dead = 1;
1285 free_fib_info(fi);
1286 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001287
1288 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Eric W. Biederman15e47302012-09-07 20:12:54 +00001291int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001292 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001293 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001295 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Eric W. Biederman15e47302012-09-07 20:12:54 +00001298 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001299 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001300 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001301
1302 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 rtm->rtm_family = AF_INET;
1304 rtm->rtm_dst_len = dst_len;
1305 rtm->rtm_src_len = 0;
1306 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001307 if (tb_id < 256)
1308 rtm->rtm_table = tb_id;
1309 else
1310 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001311 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1312 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 rtm->rtm_type = type;
1314 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001315 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001317
David S. Millerf3756b72012-04-01 20:39:02 -04001318 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001319 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001320 goto nla_put_failure;
1321 if (fi->fib_priority &&
1322 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1323 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001324 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001325 goto nla_put_failure;
1326
David S. Millerf3756b72012-04-01 20:39:02 -04001327 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001328 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001329 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (fi->fib_nhs == 1) {
David S. Millerf3756b72012-04-01 20:39:02 -04001331 if (fi->fib_nh->nh_gw &&
Jiri Benc930345e2015-03-29 16:59:25 +02001332 nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
David S. Millerf3756b72012-04-01 20:39:02 -04001333 goto nla_put_failure;
1334 if (fi->fib_nh->nh_oif &&
1335 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1336 goto nla_put_failure;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001337 if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
Florian Westphal25dd1692017-11-02 16:02:20 +01001338 struct in_device *in_dev;
1339
1340 rcu_read_lock();
1341 in_dev = __in_dev_get_rcu(fi->fib_nh->nh_dev);
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001342 if (in_dev &&
1343 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1344 rtm->rtm_flags |= RTNH_F_DEAD;
Florian Westphal25dd1692017-11-02 16:02:20 +01001345 rcu_read_unlock();
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001346 }
Ido Schimmel475abbf2017-08-02 09:56:01 +02001347 if (fi->fib_nh->nh_flags & RTNH_F_OFFLOAD)
1348 rtm->rtm_flags |= RTNH_F_OFFLOAD;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001349#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001350 if (fi->fib_nh[0].nh_tclassid &&
1351 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1352 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001353#endif
David Ahernea7a8082017-01-11 14:29:54 -08001354 if (fi->fib_nh->nh_lwtstate &&
1355 lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
1356 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358#ifdef CONFIG_IP_ROUTE_MULTIPATH
1359 if (fi->fib_nhs > 1) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001360 struct rtnexthop *rtnh;
1361 struct nlattr *mp;
1362
1363 mp = nla_nest_start(skb, RTA_MULTIPATH);
Ian Morris51456b22015-04-03 09:17:26 +01001364 if (!mp)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001365 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 for_nexthops(fi) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001368 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
Ian Morris51456b22015-04-03 09:17:26 +01001369 if (!rtnh)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001370 goto nla_put_failure;
1371
1372 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001373 if (nh->nh_flags & RTNH_F_LINKDOWN) {
Florian Westphal25dd1692017-11-02 16:02:20 +01001374 struct in_device *in_dev;
1375
1376 rcu_read_lock();
1377 in_dev = __in_dev_get_rcu(nh->nh_dev);
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001378 if (in_dev &&
1379 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1380 rtnh->rtnh_flags |= RTNH_F_DEAD;
Florian Westphal25dd1692017-11-02 16:02:20 +01001381 rcu_read_unlock();
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001382 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001383 rtnh->rtnh_hops = nh->nh_weight - 1;
1384 rtnh->rtnh_ifindex = nh->nh_oif;
1385
David S. Millerf3756b72012-04-01 20:39:02 -04001386 if (nh->nh_gw &&
Jiri Benc930345e2015-03-29 16:59:25 +02001387 nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
David S. Millerf3756b72012-04-01 20:39:02 -04001388 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001389#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001390 if (nh->nh_tclassid &&
1391 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1392 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001393#endif
David Ahernea7a8082017-01-11 14:29:54 -08001394 if (nh->nh_lwtstate &&
1395 lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
1396 goto nla_put_failure;
1397
Thomas Grafbe403ea2006-08-17 18:15:17 -07001398 /* length of rtnetlink header + attributes */
1399 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 } endfor_nexthops(fi);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001401
1402 nla_nest_end(skb, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404#endif
Johannes Berg053c0952015-01-16 22:09:00 +01001405 nlmsg_end(skb, nlh);
1406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Thomas Grafbe403ea2006-08-17 18:15:17 -07001408nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001409 nlmsg_cancel(skb, nlh);
1410 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001414 * Update FIB if:
1415 * - local address disappeared -> we must delete all the entries
1416 * referring to it.
1417 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001419int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001420{
1421 int ret = 0;
1422 unsigned int hash = fib_laddr_hashfn(local);
1423 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001424 struct net *net = dev_net(dev);
1425 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001426 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ian Morris51456b22015-04-03 09:17:26 +01001428 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001429 return 0;
1430
Sasha Levinb67bfe02013-02-27 17:06:00 -08001431 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001432 if (!net_eq(fi->fib_net, net) ||
1433 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001434 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001435 if (fi->fib_prefsrc == local) {
1436 fi->fib_flags |= RTNH_F_DEAD;
1437 ret++;
1438 }
1439 }
1440 return ret;
1441}
1442
Ido Schimmel982acb92017-02-08 11:16:39 +01001443static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
1444 enum fib_event_type event_type)
1445{
1446 struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
1447 struct fib_nh_notifier_info info = {
1448 .fib_nh = fib_nh,
1449 };
1450
1451 switch (event_type) {
1452 case FIB_EVENT_NH_ADD:
1453 if (fib_nh->nh_flags & RTNH_F_DEAD)
1454 break;
1455 if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1456 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1457 break;
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001458 return call_fib4_notifiers(dev_net(fib_nh->nh_dev), event_type,
1459 &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001460 case FIB_EVENT_NH_DEL:
Ido Schimmel71ed7ee2017-07-28 23:27:44 +03001461 if ((in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
Ido Schimmel982acb92017-02-08 11:16:39 +01001462 fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
1463 (fib_nh->nh_flags & RTNH_F_DEAD))
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001464 return call_fib4_notifiers(dev_net(fib_nh->nh_dev),
1465 event_type, &info.info);
Ido Schimmel982acb92017-02-08 11:16:39 +01001466 default:
1467 break;
1468 }
1469
1470 return NOTIFY_DONE;
1471}
1472
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001473/* Update the PMTU of exceptions when:
1474 * - the new MTU of the first hop becomes smaller than the PMTU
1475 * - the old MTU was the same as the PMTU, and it limited discovery of
1476 * larger MTUs on the path. With that limit raised, we can now
1477 * discover larger MTUs
1478 * A special case is locked exceptions, for which the PMTU is smaller
1479 * than the minimal accepted PMTU:
1480 * - if the new MTU is greater than the PMTU, don't make any change
1481 * - otherwise, unlock and set PMTU
1482 */
1483static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1484{
1485 struct fnhe_hash_bucket *bucket;
1486 int i;
1487
1488 bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1489 if (!bucket)
1490 return;
1491
1492 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1493 struct fib_nh_exception *fnhe;
1494
1495 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1496 fnhe;
1497 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1498 if (fnhe->fnhe_mtu_locked) {
1499 if (new <= fnhe->fnhe_pmtu) {
1500 fnhe->fnhe_pmtu = new;
1501 fnhe->fnhe_mtu_locked = false;
1502 }
1503 } else if (new < fnhe->fnhe_pmtu ||
1504 orig == fnhe->fnhe_pmtu) {
1505 fnhe->fnhe_pmtu = new;
1506 }
1507 }
1508 }
1509}
1510
1511void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1512{
1513 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1514 struct hlist_head *head = &fib_info_devhash[hash];
1515 struct fib_nh *nh;
1516
1517 hlist_for_each_entry(nh, head, nh_hash) {
1518 if (nh->nh_dev == dev)
1519 nh_update_mtu(nh, dev->mtu, orig_mtu);
1520 }
1521}
1522
Julian Anastasov4f823de2015-10-30 10:23:33 +02001523/* Event force Flags Description
1524 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1525 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1526 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1527 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1528 */
1529int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 int ret = 0;
1532 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001533 struct fib_info *prev_fi = NULL;
1534 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1535 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001536 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001537
Julian Anastasov4f823de2015-10-30 10:23:33 +02001538 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 scope = -1;
1540
Sasha Levinb67bfe02013-02-27 17:06:00 -08001541 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001542 struct fib_info *fi = nh->nh_parent;
1543 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001545 BUG_ON(!fi->fib_nhs);
1546 if (nh->nh_dev != dev || fi == prev_fi)
1547 continue;
1548 prev_fi = fi;
1549 dead = 0;
1550 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001551 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001552 dead++;
David S. Miller71fceff2010-01-15 01:16:40 -08001553 else if (nexthop_nh->nh_dev == dev &&
1554 nexthop_nh->nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001555 switch (event) {
1556 case NETDEV_DOWN:
1557 case NETDEV_UNREGISTER:
1558 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1559 /* fall through */
1560 case NETDEV_CHANGE:
1561 nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
1562 break;
1563 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001564 call_fib_nh_notifiers(nexthop_nh,
1565 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001566 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001568#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001569 if (event == NETDEV_UNREGISTER &&
1570 nexthop_nh->nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001571 dead = fi->fib_nhs;
1572 break;
1573 }
1574#endif
1575 } endfor_nexthops(fi)
1576 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001577 switch (event) {
1578 case NETDEV_DOWN:
1579 case NETDEV_UNREGISTER:
1580 fi->fib_flags |= RTNH_F_DEAD;
1581 /* fall through */
1582 case NETDEV_CHANGE:
1583 fi->fib_flags |= RTNH_F_LINKDOWN;
1584 break;
1585 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001586 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001588
1589 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
1592 return ret;
1593}
1594
David S. Miller0c838ff2011-01-31 16:16:50 -08001595/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001596static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001597{
1598 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001599 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001600 struct fib_table *tb = res->table;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001601 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001602 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001603 struct fib_alias *fa, *fa1 = NULL;
1604 u32 last_prio = res->fi->fib_priority;
1605 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001606
Alexander Duyck56315f92015-02-25 15:31:31 -08001607 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001608 struct fib_info *next_fi = fa->fa_info;
1609
Julian Anastasov18a912e92015-07-22 10:43:22 +03001610 if (fa->fa_slen != slen)
1611 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001612 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1613 continue;
Julian Anastasov18a912e92015-07-22 10:43:22 +03001614 if (fa->tb_id != tb->tb_id)
1615 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001616 if (next_fi->fib_priority > last_prio &&
1617 fa->fa_tos == last_tos) {
1618 if (last_tos)
1619 continue;
1620 break;
1621 }
1622 if (next_fi->fib_flags & RTNH_F_DEAD)
1623 continue;
1624 last_tos = fa->fa_tos;
1625 last_prio = next_fi->fib_priority;
1626
David S. Miller37e826c2011-03-24 18:06:47 -07001627 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001628 fa->fa_type != RTN_UNICAST)
1629 continue;
David S. Miller0c838ff2011-01-31 16:16:50 -08001630 if (!next_fi->fib_nh[0].nh_gw ||
1631 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1632 continue;
1633
1634 fib_alias_accessed(fa);
1635
Ian Morris51456b22015-04-03 09:17:26 +01001636 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001637 if (next_fi != res->fi)
1638 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001639 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001640 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001641 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001642 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001643 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001644 goto out;
1645 }
1646 fi = next_fi;
1647 order++;
1648 }
1649
Ian Morris51456b22015-04-03 09:17:26 +01001650 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001651 if (fa1)
1652 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001653 goto out;
1654 }
1655
1656 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001657 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001658 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001659 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001660 goto out;
1661 }
1662
1663 if (last_idx >= 0)
1664 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001665 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001666out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001667 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001671 * Dead device goes up. We wake up dead nexthops.
1672 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001674int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 struct fib_info *prev_fi;
1677 unsigned int hash;
1678 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct fib_nh *nh;
1680 int ret;
1681
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001682 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684
Julian Anastasovc9b32922015-10-30 10:23:34 +02001685 if (nh_flags & RTNH_F_DEAD) {
1686 unsigned int flags = dev_get_flags(dev);
1687
1688 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1689 nh_flags |= RTNH_F_LINKDOWN;
1690 }
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 prev_fi = NULL;
1693 hash = fib_devindex_hashfn(dev->ifindex);
1694 head = &fib_info_devhash[hash];
1695 ret = 0;
1696
Sasha Levinb67bfe02013-02-27 17:06:00 -08001697 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct fib_info *fi = nh->nh_parent;
1699 int alive;
1700
1701 BUG_ON(!fi->fib_nhs);
1702 if (nh->nh_dev != dev || fi == prev_fi)
1703 continue;
1704
1705 prev_fi = fi;
1706 alive = 0;
1707 change_nexthops(fi) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001708 if (!(nexthop_nh->nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 alive++;
1710 continue;
1711 }
Ian Morris51456b22015-04-03 09:17:26 +01001712 if (!nexthop_nh->nh_dev ||
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001713 !(nexthop_nh->nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001715 if (nexthop_nh->nh_dev != dev ||
1716 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 continue;
1718 alive++;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001719 nexthop_nh->nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001720 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 } endfor_nexthops(fi)
1722
1723 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001724 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ret++;
1726 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001727
1728 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730
1731 return ret;
1732}
1733
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001734#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001735static bool fib_good_nh(const struct fib_nh *nh)
1736{
1737 int state = NUD_REACHABLE;
1738
1739 if (nh->nh_scope == RT_SCOPE_LINK) {
1740 struct neighbour *n;
1741
1742 rcu_read_lock_bh();
1743
Eric Dumazetd985d152016-08-18 10:19:34 -07001744 n = __ipv4_neigh_lookup_noref(nh->nh_dev,
1745 (__force u32)nh->nh_gw);
David Aherna6db4492016-04-07 07:21:00 -07001746 if (n)
1747 state = n->nud_state;
1748
1749 rcu_read_unlock_bh();
1750 }
1751
1752 return !!(state & NUD_VALID);
1753}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001754
Peter Nørlund0e884c72015-09-30 10:12:21 +02001755void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
1757 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001758 struct net *net = fi->fib_net;
1759 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Peter Nørlund0e884c72015-09-30 10:12:21 +02001761 for_nexthops(fi) {
Xin Long6174a302018-04-01 22:40:35 +08001762 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
1763 if (!fib_good_nh(nh))
1764 continue;
1765 if (!first) {
1766 res->nh_sel = nhsel;
1767 first = true;
1768 }
1769 }
1770
Peter Nørlund0e884c72015-09-30 10:12:21 +02001771 if (hash > atomic_read(&nh->nh_upper_bound))
1772 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Xin Long6174a302018-04-01 22:40:35 +08001774 res->nh_sel = nhsel;
1775 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778#endif
David Ahern3ce58d82015-10-05 08:51:25 -07001779
1780void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001781 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07001782{
David Ahern0d876f22018-02-13 08:11:34 -08001783 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
1784 goto check_saddr;
David Ahern7a18c5b2017-01-10 14:37:35 -08001785
David Ahern3ce58d82015-10-05 08:51:25 -07001786#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern0d876f22018-02-13 08:11:34 -08001787 if (res->fi->fib_nhs > 1) {
David Ahern7efc0b62018-03-02 08:32:12 -08001788 int h = fib_multipath_hash(net, fl4, skb, NULL);
Paolo Abeni9920e482015-10-29 22:20:40 +01001789
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001790 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07001791 }
1792 else
1793#endif
1794 if (!res->prefixlen &&
1795 res->table->tb_num_default > 1 &&
David Ahern0d876f22018-02-13 08:11:34 -08001796 res->type == RTN_UNICAST)
David Ahern3ce58d82015-10-05 08:51:25 -07001797 fib_select_default(fl4, res);
1798
David Ahern0d876f22018-02-13 08:11:34 -08001799check_saddr:
David Ahern3ce58d82015-10-05 08:51:25 -07001800 if (!fl4->saddr)
1801 fl4->saddr = FIB_RES_PREFSRC(net, *res);
1802}