blob: 2157dc08c407aca7dddeab2d8da7b0703ec3acac [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include "fib_lookup.h"
49
Stephen Hemminger832b4c52006-08-29 16:48:09 -070050static DEFINE_SPINLOCK(fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct hlist_head *fib_info_hash;
52static struct hlist_head *fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -080053static unsigned int fib_info_hash_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned int fib_info_cnt;
55
56#define DEVINDEX_HASHBITS 8
57#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
58static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
59
60#ifdef CONFIG_IP_ROUTE_MULTIPATH
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000062#define for_nexthops(fi) { \
63 int nhsel; const struct fib_nh *nh; \
64 for (nhsel = 0, nh = (fi)->fib_nh; \
65 nhsel < (fi)->fib_nhs; \
66 nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000068#define change_nexthops(fi) { \
69 int nhsel; struct fib_nh *nexthop_nh; \
70 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
71 nhsel < (fi)->fib_nhs; \
72 nexthop_nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#else /* CONFIG_IP_ROUTE_MULTIPATH */
75
76/* Hope, that gcc will optimize it to get rid of dummy loop */
77
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000078#define for_nexthops(fi) { \
79 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
80 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000082#define change_nexthops(fi) { \
83 int nhsel; \
84 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
85 for (nhsel = 0; nhsel < 1; nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#endif /* CONFIG_IP_ROUTE_MULTIPATH */
88
89#define endfor_nexthops(fi) }
90
91
David S. Miller3be06862011-03-07 15:01:10 -080092const struct fib_prop fib_props[RTN_MAX + 1] = {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000093 [RTN_UNSPEC] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .error = 0,
95 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000096 },
97 [RTN_UNICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .error = 0,
99 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000100 },
101 [RTN_LOCAL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .error = 0,
103 .scope = RT_SCOPE_HOST,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000104 },
105 [RTN_BROADCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .error = 0,
107 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000108 },
109 [RTN_ANYCAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .error = 0,
111 .scope = RT_SCOPE_LINK,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000112 },
113 [RTN_MULTICAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .error = 0,
115 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000116 },
117 [RTN_BLACKHOLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .error = -EINVAL,
119 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000120 },
121 [RTN_UNREACHABLE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .error = -EHOSTUNREACH,
123 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000124 },
125 [RTN_PROHIBIT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .error = -EACCES,
127 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000128 },
129 [RTN_THROW] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .error = -EAGAIN,
131 .scope = RT_SCOPE_UNIVERSE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000132 },
133 [RTN_NAT] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .error = -EINVAL,
135 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000136 },
137 [RTN_XRESOLVE] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .error = -EINVAL,
139 .scope = RT_SCOPE_NOWHERE,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000140 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
David S. Millerc5038a82012-07-31 15:02:02 -0700143static void rt_fibinfo_free(struct rtable __rcu **rtp)
Eric Dumazet54764bb2012-07-31 01:08:23 +0000144{
145 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
146
147 if (!rt)
148 return;
149
150 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
151 * because we waited an RCU grace period before calling
152 * free_fib_info_rcu()
153 */
154
155 dst_free(&rt->dst);
156}
157
David S. Millerc5038a82012-07-31 15:02:02 -0700158static void free_nh_exceptions(struct fib_nh *nh)
159{
Eric Dumazetcaa41522014-09-03 22:21:56 -0700160 struct fnhe_hash_bucket *hash;
David S. Millerc5038a82012-07-31 15:02:02 -0700161 int i;
162
Eric Dumazetcaa41522014-09-03 22:21:56 -0700163 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
164 if (!hash)
165 return;
David S. Millerc5038a82012-07-31 15:02:02 -0700166 for (i = 0; i < FNHE_HASH_SIZE; i++) {
167 struct fib_nh_exception *fnhe;
168
169 fnhe = rcu_dereference_protected(hash[i].chain, 1);
170 while (fnhe) {
171 struct fib_nh_exception *next;
172
173 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
174
Timo Teräs2ffae992013-06-27 10:27:05 +0300175 rt_fibinfo_free(&fnhe->fnhe_rth_input);
176 rt_fibinfo_free(&fnhe->fnhe_rth_output);
David S. Millerc5038a82012-07-31 15:02:02 -0700177
178 kfree(fnhe);
179
180 fnhe = next;
181 }
182 }
183 kfree(hash);
184}
185
186static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
Eric Dumazetd26b3a72012-07-31 05:45:30 +0000187{
188 int cpu;
189
190 if (!rtp)
191 return;
192
193 for_each_possible_cpu(cpu) {
194 struct rtable *rt;
195
196 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
197 if (rt)
198 dst_free(&rt->dst);
199 }
200 free_percpu(rtp);
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* Release a nexthop info record */
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000204static void free_fib_info_rcu(struct rcu_head *head)
205{
206 struct fib_info *fi = container_of(head, struct fib_info, rcu);
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700207 struct dst_metrics *m;
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000208
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000209 change_nexthops(fi) {
210 if (nexthop_nh->nh_dev)
211 dev_put(nexthop_nh->nh_dev);
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200212 lwtstate_put(nexthop_nh->nh_lwtstate);
Eric Dumazetcaa41522014-09-03 22:21:56 -0700213 free_nh_exceptions(nexthop_nh);
David S. Millerc5038a82012-07-31 15:02:02 -0700214 rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
215 rt_fibinfo_free(&nexthop_nh->nh_rth_input);
Yanmin Zhange49cc0d2012-05-23 15:39:45 +0000216 } endfor_nexthops(fi);
217
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700218 m = fi->fib_metrics;
219 if (m != &dst_default_metrics && atomic_dec_and_test(&m->refcnt))
220 kfree(m);
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000221 kfree(fi);
222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224void free_fib_info(struct fib_info *fi)
225{
226 if (fi->fib_dead == 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000227 pr_warn("Freeing alive fib_info %p\n", fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 fib_info_cnt--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700231#ifdef CONFIG_IP_ROUTE_CLASSID
232 change_nexthops(fi) {
233 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700234 fi->fib_net->ipv4.fib_num_tclassid_users--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700235 } endfor_nexthops(fi);
236#endif
Yan, Zheng19c1ea12011-09-04 20:24:20 +0000237 call_rcu(&fi->rcu, free_fib_info_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Ido Schimmelb423cb12016-12-03 16:44:58 +0100239EXPORT_SYMBOL_GPL(free_fib_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241void fib_release_info(struct fib_info *fi)
242{
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700243 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (fi && --fi->fib_treeref == 0) {
245 hlist_del(&fi->fib_hash);
246 if (fi->fib_prefsrc)
247 hlist_del(&fi->fib_lhash);
248 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -0800249 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 continue;
David S. Miller71fceff2010-01-15 01:16:40 -0800251 hlist_del(&nexthop_nh->nh_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 } endfor_nexthops(fi)
253 fi->fib_dead = 1;
254 fib_info_put(fi);
255 }
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700256 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000259static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 const struct fib_nh *onh = ofi->fib_nh;
262
263 for_nexthops(fi) {
264 if (nh->nh_oif != onh->nh_oif ||
265 nh->nh_gw != onh->nh_gw ||
266 nh->nh_scope != onh->nh_scope ||
267#ifdef CONFIG_IP_ROUTE_MULTIPATH
268 nh->nh_weight != onh->nh_weight ||
269#endif
Patrick McHardyc7066f72011-01-14 13:36:42 +0100270#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 nh->nh_tclassid != onh->nh_tclassid ||
272#endif
Roopa Prabhu571e7222015-07-21 10:43:47 +0200273 lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400274 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -1;
276 onh++;
277 } endfor_nexthops(fi);
278 return 0;
279}
280
David S. Miller88ebc722008-01-12 21:49:01 -0800281static inline unsigned int fib_devindex_hashfn(unsigned int val)
282{
283 unsigned int mask = DEVINDEX_HASHSIZE - 1;
284
285 return (val ^
286 (val >> DEVINDEX_HASHBITS) ^
287 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
291{
David S. Miller123b9732011-02-01 15:34:21 -0800292 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned int val = fi->fib_nhs;
294
David S. Miller37e826c2011-03-24 18:06:47 -0700295 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
Al Viro81f7bf62006-09-27 18:40:00 -0700296 val ^= (__force u32)fi->fib_prefsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 val ^= fi->fib_priority;
David S. Miller88ebc722008-01-12 21:49:01 -0800298 for_nexthops(fi) {
299 val ^= fib_devindex_hashfn(nh->nh_oif);
300 } endfor_nexthops(fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
303}
304
305static struct fib_info *fib_find_info(const struct fib_info *nfi)
306{
307 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct fib_info *fi;
309 unsigned int hash;
310
311 hash = fib_info_hashfn(nfi);
312 head = &fib_info_hash[hash];
313
Sasha Levinb67bfe02013-02-27 17:06:00 -0800314 hlist_for_each_entry(fi, head, fib_hash) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800315 if (!net_eq(fi->fib_net, nfi->fib_net))
Denis V. Lunev4814bdb2008-01-31 18:50:07 -0800316 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (fi->fib_nhs != nfi->fib_nhs)
318 continue;
319 if (nfi->fib_protocol == fi->fib_protocol &&
David S. Miller37e826c2011-03-24 18:06:47 -0700320 nfi->fib_scope == fi->fib_scope &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 nfi->fib_prefsrc == fi->fib_prefsrc &&
322 nfi->fib_priority == fi->fib_priority &&
Eric Dumazetf4ef85b2012-10-04 01:25:26 +0000323 nfi->fib_type == fi->fib_type &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 memcmp(nfi->fib_metrics, fi->fib_metrics,
Eric Dumazetfcd13f42011-03-24 07:01:24 +0000325 sizeof(u32) * RTAX_MAX) == 0 &&
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400326 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
328 return fi;
329 }
330
331 return NULL;
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/* Check, that the gateway is already configured.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000335 * Used only by redirect accept routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Al Virod878e72e2006-09-26 22:18:13 -0700337int ip_fib_check_default(__be32 gw, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct fib_nh *nh;
341 unsigned int hash;
342
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700343 spin_lock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 hash = fib_devindex_hashfn(dev->ifindex);
346 head = &fib_info_devhash[hash];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800347 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (nh->nh_dev == dev &&
349 nh->nh_gw == gw &&
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000350 !(nh->nh_flags & RTNH_F_DEAD)) {
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700351 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353 }
354 }
355
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700356 spin_unlock(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 return -1;
359}
360
Thomas Graf339bf982006-11-10 14:10:15 -0800361static inline size_t fib_nlmsg_size(struct fib_info *fi)
362{
363 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
364 + nla_total_size(4) /* RTA_TABLE */
365 + nla_total_size(4) /* RTA_DST */
366 + nla_total_size(4) /* RTA_PRIORITY */
Daniel Borkmannea697632015-01-05 23:57:47 +0100367 + nla_total_size(4) /* RTA_PREFSRC */
368 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
Thomas Graf339bf982006-11-10 14:10:15 -0800369
370 /* space for nested metrics */
371 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
372
373 if (fi->fib_nhs) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200374 size_t nh_encapsize = 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800375 /* Also handles the special case fib_nhs == 1 */
376
377 /* each nexthop is packed in an attribute */
378 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
379
380 /* may contain flow and gateway attribute */
381 nhsize += 2 * nla_total_size(4);
382
Roopa Prabhu571e7222015-07-21 10:43:47 +0200383 /* grab encap info */
384 for_nexthops(fi) {
385 if (nh->nh_lwtstate) {
386 /* RTA_ENCAP_TYPE */
387 nh_encapsize += lwtunnel_get_encap_size(
388 nh->nh_lwtstate);
389 /* RTA_ENCAP */
390 nh_encapsize += nla_total_size(2);
391 }
392 } endfor_nexthops(fi);
393
Thomas Graf339bf982006-11-10 14:10:15 -0800394 /* all nexthops are packed in a nested attribute */
Roopa Prabhu571e7222015-07-21 10:43:47 +0200395 payload += nla_total_size((fi->fib_nhs * nhsize) +
396 nh_encapsize);
397
Thomas Graf339bf982006-11-10 14:10:15 -0800398 }
399
400 return payload;
401}
402
Al Viro81f7bf62006-09-27 18:40:00 -0700403void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
Joe Perches9877b252013-10-17 13:34:11 -0700404 int dst_len, u32 tb_id, const struct nl_info *info,
Milan Kocianb8f55832007-05-23 14:55:06 -0700405 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct sk_buff *skb;
Thomas Graf4e902c52006-08-17 18:14:52 -0700408 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700409 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Thomas Graf339bf982006-11-10 14:10:15 -0800411 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100412 if (!skb)
Thomas Graff21c7bc2006-08-15 00:34:17 -0700413 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Eric W. Biederman15e47302012-09-07 20:12:54 +0000415 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
David S. Miller37e826c2011-03-24 18:06:47 -0700416 fa->fa_type, key, dst_len,
Milan Kocianb8f55832007-05-23 14:55:06 -0700417 fa->fa_tos, fa->fa_info, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -0800418 if (err < 0) {
419 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
420 WARN_ON(err == -EMSGSIZE);
421 kfree_skb(skb);
422 goto errout;
423 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000424 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800425 info->nlh, GFP_KERNEL);
426 return;
Thomas Graff21c7bc2006-08-15 00:34:17 -0700427errout:
428 if (err < 0)
Denis V. Lunev4d1169c2008-01-10 03:26:13 -0800429 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Stephen Hemmingerc9cb6b62013-12-28 11:05:36 -0800432static int fib_detect_death(struct fib_info *fi, int order,
433 struct fib_info **last_resort, int *last_idx,
434 int dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct neighbour *n;
437 int state = NUD_NONE;
438
439 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
440 if (n) {
441 state = n->nud_state;
442 neigh_release(n);
Julian Anastasov88f64322015-07-23 10:39:35 +0300443 } else {
444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Jianjun Kongd93191002008-11-03 00:23:42 -0800446 if (state == NUD_REACHABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000448 if ((state & NUD_VALID) && order != dflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000450 if ((state & NUD_VALID) ||
Julian Anastasov88f64322015-07-23 10:39:35 +0300451 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *last_resort = fi;
453 *last_idx = order;
454 }
455 return 1;
456}
457
458#ifdef CONFIG_IP_ROUTE_MULTIPATH
459
David Ahern6d8422a12017-05-21 10:12:02 -0600460static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
461 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 int nhs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Thomas Graf4e902c52006-08-17 18:14:52 -0700465 while (rtnh_ok(rtnh, remaining)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 nhs++;
Thomas Graf4e902c52006-08-17 18:14:52 -0700467 rtnh = rtnh_next(rtnh, &remaining);
468 }
469
470 /* leftover implies invalid nexthop configuration, discard it */
David Ahernc3ab2b42017-05-21 10:12:03 -0600471 if (remaining > 0) {
472 NL_SET_ERR_MSG(extack,
473 "Invalid nexthop configuration - extra data after nexthops");
474 nhs = 0;
475 }
476
477 return nhs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Thomas Graf4e902c52006-08-17 18:14:52 -0700480static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
David Ahern6d8422a12017-05-21 10:12:02 -0600481 int remaining, struct fib_config *cfg,
482 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Roopa Prabhu571e7222015-07-21 10:43:47 +0200484 int ret;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 change_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700487 int attrlen;
488
David Ahernc3ab2b42017-05-21 10:12:03 -0600489 if (!rtnh_ok(rtnh, remaining)) {
490 NL_SET_ERR_MSG(extack,
491 "Invalid nexthop configuration - extra data after nexthop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600493 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700494
David Ahernc3ab2b42017-05-21 10:12:03 -0600495 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
496 NL_SET_ERR_MSG(extack,
497 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +0300498 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600499 }
Julian Anastasov80610222016-07-10 21:11:55 +0300500
David S. Miller71fceff2010-01-15 01:16:40 -0800501 nexthop_nh->nh_flags =
502 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
503 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
504 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700505
506 attrlen = rtnh_attrlen(rtnh);
507 if (attrlen > 0) {
508 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
509
510 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Jiri Benc67b61f62015-03-29 16:59:26 +0200511 nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100512#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700513 nla = nla_find(attrs, attrlen, RTA_FLOW);
David S. Miller71fceff2010-01-15 01:16:40 -0800514 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700515 if (nexthop_nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700516 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#endif
Roopa Prabhu571e7222015-07-21 10:43:47 +0200518 nla = nla_find(attrs, attrlen, RTA_ENCAP);
519 if (nla) {
520 struct lwtunnel_state *lwtstate;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200521 struct nlattr *nla_entype;
522
523 nla_entype = nla_find(attrs, attrlen,
524 RTA_ENCAP_TYPE);
David Ahernc3ab2b42017-05-21 10:12:03 -0600525 if (!nla_entype) {
526 NL_SET_BAD_ATTR(extack, nla);
527 NL_SET_ERR_MSG(extack,
528 "Encap type is missing");
Roopa Prabhu571e7222015-07-21 10:43:47 +0200529 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -0600530 }
David Ahern30357d72017-01-30 12:07:37 -0800531
532 ret = lwtunnel_build_state(nla_get_u16(
Roopa Prabhu571e7222015-07-21 10:43:47 +0200533 nla_entype),
Tom Herbert127eb7c2015-08-24 09:45:41 -0700534 nla, AF_INET, cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600535 &lwtstate, extack);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200536 if (ret)
537 goto errout;
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200538 nexthop_nh->nh_lwtstate =
539 lwtstate_get(lwtstate);
Roopa Prabhu571e7222015-07-21 10:43:47 +0200540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700542
543 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } endfor_nexthops(fi);
Thomas Graf4e902c52006-08-17 18:14:52 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200547
548err_inval:
549 ret = -EINVAL;
550
551errout:
552 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Peter Nørlund0e884c72015-09-30 10:12:21 +0200555static void fib_rebalance(struct fib_info *fi)
556{
557 int total;
558 int w;
559 struct in_device *in_dev;
560
561 if (fi->fib_nhs < 2)
562 return;
563
564 total = 0;
565 for_nexthops(fi) {
566 if (nh->nh_flags & RTNH_F_DEAD)
567 continue;
568
David Ahern51161aa2015-10-14 16:44:00 -0700569 in_dev = __in_dev_get_rtnl(nh->nh_dev);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200570
571 if (in_dev &&
572 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
573 nh->nh_flags & RTNH_F_LINKDOWN)
574 continue;
575
576 total += nh->nh_weight;
577 } endfor_nexthops(fi);
578
579 w = 0;
580 change_nexthops(fi) {
581 int upper_bound;
582
David Ahern51161aa2015-10-14 16:44:00 -0700583 in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200584
585 if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
586 upper_bound = -1;
587 } else if (in_dev &&
588 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
589 nexthop_nh->nh_flags & RTNH_F_LINKDOWN) {
590 upper_bound = -1;
591 } else {
592 w += nexthop_nh->nh_weight;
Peter Nørlund0a837fe2015-10-06 07:24:47 +0200593 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
594 total) - 1;
Peter Nørlund0e884c72015-09-30 10:12:21 +0200595 }
596
597 atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
598 } endfor_nexthops(fi);
Peter Nørlund0e884c72015-09-30 10:12:21 +0200599}
600
601static inline void fib_add_weight(struct fib_info *fi,
602 const struct fib_nh *nh)
603{
604 fi->fib_weight += nh->nh_weight;
605}
606
607#else /* CONFIG_IP_ROUTE_MULTIPATH */
608
609#define fib_rebalance(fi) do { } while (0)
610#define fib_add_weight(fi, nh) do { } while (0)
611
612#endif /* CONFIG_IP_ROUTE_MULTIPATH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
David Ahern30357d72017-01-30 12:07:37 -0800614static int fib_encap_match(u16 encap_type,
Ying Xuee01286e2015-08-19 16:04:51 +0800615 struct nlattr *encap,
David Ahern30357d72017-01-30 12:07:37 -0800616 const struct fib_nh *nh,
David Ahern9ae28722017-05-27 16:19:28 -0600617 const struct fib_config *cfg,
618 struct netlink_ext_ack *extack)
Roopa Prabhu571e7222015-07-21 10:43:47 +0200619{
620 struct lwtunnel_state *lwtstate;
Jiri Bencdf383e62015-08-18 18:41:13 +0200621 int ret, result = 0;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200622
623 if (encap_type == LWTUNNEL_ENCAP_NONE)
624 return 0;
625
David Ahern9ae28722017-05-27 16:19:28 -0600626 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
627 cfg, &lwtstate, extack);
Jiri Bencdf383e62015-08-18 18:41:13 +0200628 if (!ret) {
629 result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
630 lwtstate_free(lwtstate);
631 }
Roopa Prabhu571e7222015-07-21 10:43:47 +0200632
Jiri Bencdf383e62015-08-18 18:41:13 +0200633 return result;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200634}
635
David Ahern9ae28722017-05-27 16:19:28 -0600636int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
637 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -0700640 struct rtnexthop *rtnh;
641 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#endif
643
Thomas Graf4e902c52006-08-17 18:14:52 -0700644 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 1;
646
Thomas Graf4e902c52006-08-17 18:14:52 -0700647 if (cfg->fc_oif || cfg->fc_gw) {
Roopa Prabhu571e7222015-07-21 10:43:47 +0200648 if (cfg->fc_encap) {
David Ahern9ae28722017-05-27 16:19:28 -0600649 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
650 fi->fib_nh, cfg, extack))
651 return 1;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200652 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700653 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
654 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656 return 1;
657 }
658
659#ifdef CONFIG_IP_ROUTE_MULTIPATH
Ian Morris51456b22015-04-03 09:17:26 +0100660 if (!cfg->fc_mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700662
663 rtnh = cfg->fc_mp;
664 remaining = cfg->fc_mp_len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 for_nexthops(fi) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700667 int attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Thomas Graf4e902c52006-08-17 18:14:52 -0700669 if (!rtnh_ok(rtnh, remaining))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EINVAL;
Thomas Graf4e902c52006-08-17 18:14:52 -0700671
672 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 1;
Thomas Graf4e902c52006-08-17 18:14:52 -0700674
675 attrlen = rtnh_attrlen(rtnh);
Jiri Pirkof76936d2014-10-13 16:34:10 +0200676 if (attrlen > 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700677 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
678
679 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
Jiri Benc67b61f62015-03-29 16:59:26 +0200680 if (nla && nla_get_in_addr(nla) != nh->nh_gw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 1;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100682#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -0700683 nla = nla_find(attrs, attrlen, RTA_FLOW);
684 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 1;
686#endif
687 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700688
689 rtnh = rtnh_next(rtnh, &remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } endfor_nexthops(fi);
691#endif
692 return 0;
693}
694
695
696/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000697 * Picture
698 * -------
699 *
700 * Semantics of nexthop is very messy by historical reasons.
701 * We have to take into account, that:
702 * a) gateway can be actually local interface address,
703 * so that gatewayed route is direct.
704 * b) gateway must be on-link address, possibly
705 * described not by an ifaddr, but also by a direct route.
706 * c) If both gateway and interface are specified, they should not
707 * contradict.
708 * d) If we use tunnel routes, gateway could be not on-link.
709 *
710 * Attempt to reconcile all of these (alas, self-contradictory) conditions
711 * results in pretty ugly and hairy code with obscure logic.
712 *
713 * I chose to generalized it instead, so that the size
714 * of code does not increase practically, but it becomes
715 * much more general.
716 * Every prefix is assigned a "scope" value: "host" is local address,
717 * "link" is direct route,
718 * [ ... "site" ... "interior" ... ]
719 * and "universe" is true gateway route with global meaning.
720 *
721 * Every prefix refers to a set of "nexthop"s (gw, oif),
722 * where gw must have narrower scope. This recursion stops
723 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
724 * which means that gw is forced to be on link.
725 *
726 * Code is still hairy, but now it is apparently logically
727 * consistent and very flexible. F.e. as by-product it allows
728 * to co-exists in peace independent exterior and interior
729 * routing processes.
730 *
731 * Normally it looks as following.
732 *
733 * {universe prefix} -> (gw, oif) [scope link]
734 * |
735 * |-> {link prefix} -> (gw, oif) [scope local]
736 * |
737 * |-> {local prefix} (terminal node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Thomas Graf4e902c52006-08-17 18:14:52 -0700739static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
David Ahern6d8422a12017-05-21 10:12:02 -0600740 struct fib_nh *nh, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Tom Herbert127eb7c2015-08-24 09:45:41 -0700742 int err = 0;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800743 struct net *net;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000744 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Denis V. Lunev86167a32008-01-21 17:34:00 -0800746 net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (nh->nh_gw) {
748 struct fib_result res;
749
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000750 if (nh->nh_flags & RTNH_F_ONLINK) {
David Ahern30bbaa12015-08-13 14:59:05 -0600751 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David Ahernc3ab2b42017-05-21 10:12:03 -0600753 if (cfg->fc_scope >= RT_SCOPE_LINK) {
754 NL_SET_ERR_MSG(extack,
755 "Nexthop has invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600757 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000758 dev = __dev_get_by_index(net, nh->nh_oif);
759 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -ENODEV;
David Ahernc3ab2b42017-05-21 10:12:03 -0600761 if (!(dev->flags & IFF_UP)) {
762 NL_SET_ERR_MSG(extack,
763 "Nexthop device is not up");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600765 }
David Ahern30bbaa12015-08-13 14:59:05 -0600766 addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
David Ahernc3ab2b42017-05-21 10:12:03 -0600767 if (addr_type != RTN_UNICAST) {
768 NL_SET_ERR_MSG(extack,
769 "Nexthop has invalid gateway");
David Ahern30bbaa12015-08-13 14:59:05 -0600770 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600771 }
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400772 if (!netif_carrier_ok(dev))
773 nh->nh_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 nh->nh_dev = dev;
775 dev_hold(dev);
776 nh->nh_scope = RT_SCOPE_LINK;
777 return 0;
778 }
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000779 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 {
David Ahern3bfd8472015-08-13 14:59:07 -0600781 struct fib_table *tbl = NULL;
David S. Miller9ade2282011-03-12 02:02:42 -0500782 struct flowi4 fl4 = {
783 .daddr = nh->nh_gw,
784 .flowi4_scope = cfg->fc_scope + 1,
785 .flowi4_oif = nh->nh_oif,
Cong Wang6a662712014-04-15 16:25:34 -0700786 .flowi4_iif = LOOPBACK_IFINDEX,
Thomas Graf4e902c52006-08-17 18:14:52 -0700787 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* It is not necessary, but requires a bit of thinking */
David S. Miller9ade2282011-03-12 02:02:42 -0500790 if (fl4.flowi4_scope < RT_SCOPE_LINK)
791 fl4.flowi4_scope = RT_SCOPE_LINK;
David Ahern3bfd8472015-08-13 14:59:07 -0600792
793 if (cfg->fc_table)
794 tbl = fib_get_table(net, cfg->fc_table);
795
796 if (tbl)
797 err = fib_table_lookup(tbl, &fl4, &res,
Eric Dumazet1e313672015-08-15 10:54:07 -0700798 FIB_LOOKUP_IGNORE_LINKSTATE |
799 FIB_LOOKUP_NOREF);
David Ahern4c9bcd12015-08-19 11:40:31 -0700800
801 /* on error or if no table given do full lookup. This
802 * is needed for example when nexthops are in the local
803 * table rather than the given table
804 */
805 if (!tbl || err) {
David Ahern3bfd8472015-08-13 14:59:07 -0600806 err = fib_lookup(net, &fl4, &res,
807 FIB_LOOKUP_IGNORE_LINKSTATE);
David Ahern4c9bcd12015-08-19 11:40:31 -0700808 }
809
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000810 if (err) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600811 NL_SET_ERR_MSG(extack,
812 "Nexthop has invalid gateway");
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000813 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return err;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 err = -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600818 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
819 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 nh->nh_scope = res.scope;
823 nh->nh_oif = FIB_RES_OIF(res);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000824 nh->nh_dev = dev = FIB_RES_DEV(res);
David Ahernc3ab2b42017-05-21 10:12:03 -0600825 if (!dev) {
826 NL_SET_ERR_MSG(extack,
827 "No egress device for nexthop gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600829 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000830 dev_hold(dev);
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400831 if (!netif_carrier_ok(dev))
832 nh->nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000833 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
835 struct in_device *in_dev;
836
David Ahernc3ab2b42017-05-21 10:12:03 -0600837 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
838 NL_SET_ERR_MSG(extack,
839 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EINVAL;
David Ahernc3ab2b42017-05-21 10:12:03 -0600841 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000842 rcu_read_lock();
843 err = -ENODEV;
Denis V. Lunev86167a32008-01-21 17:34:00 -0800844 in_dev = inetdev_by_index(net, nh->nh_oif);
Ian Morris51456b22015-04-03 09:17:26 +0100845 if (!in_dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000846 goto out;
847 err = -ENETDOWN;
David Ahernc3ab2b42017-05-21 10:12:03 -0600848 if (!(in_dev->dev->flags & IFF_UP)) {
849 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000850 goto out;
David Ahernc3ab2b42017-05-21 10:12:03 -0600851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 nh->nh_dev = in_dev->dev;
853 dev_hold(nh->nh_dev);
854 nh->nh_scope = RT_SCOPE_HOST;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -0400855 if (!netif_carrier_ok(nh->nh_dev))
856 nh->nh_flags |= RTNH_F_LINKDOWN;
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000857 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000859out:
860 rcu_read_unlock();
861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Al Viro81f7bf62006-09-27 18:40:00 -0700864static inline unsigned int fib_laddr_hashfn(__be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
David S. Miller123b9732011-02-01 15:34:21 -0800866 unsigned int mask = (fib_info_hash_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000868 return ((__force u32)val ^
869 ((__force u32)val >> 7) ^
870 ((__force u32)val >> 14)) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
David S. Miller123b9732011-02-01 15:34:21 -0800873static struct hlist_head *fib_info_hash_alloc(int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 if (bytes <= PAGE_SIZE)
Joonwoo Park88f83492007-11-26 23:29:32 +0800876 return kzalloc(bytes, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 else
878 return (struct hlist_head *)
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000879 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
880 get_order(bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
David S. Miller123b9732011-02-01 15:34:21 -0800883static void fib_info_hash_free(struct hlist_head *hash, int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 if (!hash)
886 return;
887
888 if (bytes <= PAGE_SIZE)
889 kfree(hash);
890 else
891 free_pages((unsigned long) hash, get_order(bytes));
892}
893
David S. Miller123b9732011-02-01 15:34:21 -0800894static void fib_info_hash_move(struct hlist_head *new_info_hash,
895 struct hlist_head *new_laddrhash,
896 unsigned int new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
David S. Millerb7656e72005-08-05 04:12:48 -0700898 struct hlist_head *old_info_hash, *old_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800899 unsigned int old_size = fib_info_hash_size;
David S. Millerb7656e72005-08-05 04:12:48 -0700900 unsigned int i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700902 spin_lock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700903 old_info_hash = fib_info_hash;
904 old_laddrhash = fib_info_laddrhash;
David S. Miller123b9732011-02-01 15:34:21 -0800905 fib_info_hash_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 for (i = 0; i < old_size; i++) {
908 struct hlist_head *head = &fib_info_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800909 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct fib_info *fi;
911
Sasha Levinb67bfe02013-02-27 17:06:00 -0800912 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct hlist_head *dest;
914 unsigned int new_hash;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 new_hash = fib_info_hashfn(fi);
917 dest = &new_info_hash[new_hash];
918 hlist_add_head(&fi->fib_hash, dest);
919 }
920 }
921 fib_info_hash = new_info_hash;
922
923 for (i = 0; i < old_size; i++) {
924 struct hlist_head *lhead = &fib_info_laddrhash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800925 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct fib_info *fi;
927
Sasha Levinb67bfe02013-02-27 17:06:00 -0800928 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct hlist_head *ldest;
930 unsigned int new_hash;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
933 ldest = &new_laddrhash[new_hash];
934 hlist_add_head(&fi->fib_lhash, ldest);
935 }
936 }
937 fib_info_laddrhash = new_laddrhash;
938
Stephen Hemminger832b4c52006-08-29 16:48:09 -0700939 spin_unlock_bh(&fib_info_lock);
David S. Millerb7656e72005-08-05 04:12:48 -0700940
941 bytes = old_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -0800942 fib_info_hash_free(old_info_hash, bytes);
943 fib_info_hash_free(old_laddrhash, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
David S. Miller436c3b62011-03-24 17:42:21 -0700946__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
947{
948 nh->nh_saddr = inet_select_addr(nh->nh_dev,
949 nh->nh_gw,
David S. Miller37e826c2011-03-24 18:06:47 -0700950 nh->nh_parent->fib_scope);
David S. Miller436c3b62011-03-24 17:42:21 -0700951 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
952
953 return nh->nh_saddr;
954}
955
David Ahern021dd3b2015-08-13 14:59:06 -0600956static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
957{
958 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
959 fib_prefsrc != cfg->fc_dst) {
David Ahern9b8ff512015-09-01 14:26:35 -0600960 u32 tb_id = cfg->fc_table;
David Aherne1b8d902015-11-03 15:59:28 -0800961 int rc;
David Ahern021dd3b2015-08-13 14:59:06 -0600962
963 if (tb_id == RT_TABLE_MAIN)
964 tb_id = RT_TABLE_LOCAL;
965
David Aherne1b8d902015-11-03 15:59:28 -0800966 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
967 fib_prefsrc, tb_id);
968
969 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
970 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
971 fib_prefsrc, RT_TABLE_LOCAL);
David Ahern021dd3b2015-08-13 14:59:06 -0600972 }
David Aherne1b8d902015-11-03 15:59:28 -0800973
974 if (rc != RTN_LOCAL)
975 return false;
David Ahern021dd3b2015-08-13 14:59:06 -0600976 }
977 return true;
978}
979
Florian Westphal6cf9dfd2015-08-31 15:58:44 +0200980static int
981fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
982{
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200983 bool ecn_ca = false;
Florian Westphal6cf9dfd2015-08-31 15:58:44 +0200984 struct nlattr *nla;
985 int remaining;
986
987 if (!cfg->fc_mx)
988 return 0;
989
990 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
991 int type = nla_type(nla);
992 u32 val;
993
994 if (!type)
995 continue;
996 if (type > RTAX_MAX)
997 return -EINVAL;
998
999 if (type == RTAX_CC_ALGO) {
1000 char tmp[TCP_CA_NAME_MAX];
1001
1002 nla_strlcpy(tmp, nla, sizeof(tmp));
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001003 val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001004 if (val == TCP_CA_UNSPEC)
1005 return -EINVAL;
1006 } else {
1007 val = nla_get_u32(nla);
1008 }
1009 if (type == RTAX_ADVMSS && val > 65535 - 40)
1010 val = 65535 - 40;
1011 if (type == RTAX_MTU && val > 65535 - 15)
1012 val = 65535 - 15;
Paolo Abeni626abd52016-05-13 18:33:41 +02001013 if (type == RTAX_HOPLIMIT && val > 255)
1014 val = 255;
Daniel Borkmannb8d3e412015-08-31 15:58:46 +02001015 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
1016 return -EINVAL;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001017 fi->fib_metrics->metrics[type - 1] = val;
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001018 }
1019
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001020 if (ecn_ca)
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001021 fi->fib_metrics->metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02001022
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001023 return 0;
1024}
1025
David Ahern6d8422a12017-05-21 10:12:02 -06001026struct fib_info *fib_create_info(struct fib_config *cfg,
1027 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 int err;
1030 struct fib_info *fi = NULL;
1031 struct fib_info *ofi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int nhs = 1;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001033 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
David S. Miller4c8237c2011-03-07 14:27:38 -08001035 if (cfg->fc_type > RTN_MAX)
1036 goto err_inval;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Fast check to catch the most weird cases */
David Ahernc3ab2b42017-05-21 10:12:03 -06001039 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1040 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
David Ahernc3ab2b42017-05-21 10:12:03 -06001044 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1045 NL_SET_ERR_MSG(extack,
1046 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
Julian Anastasov80610222016-07-10 21:11:55 +03001047 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001048 }
Julian Anastasov80610222016-07-10 21:11:55 +03001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#ifdef CONFIG_IP_ROUTE_MULTIPATH
Thomas Graf4e902c52006-08-17 18:14:52 -07001051 if (cfg->fc_mp) {
David Ahern6d8422a12017-05-21 10:12:02 -06001052 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (nhs == 0)
1054 goto err_inval;
1055 }
1056#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 err = -ENOBUFS;
David S. Miller123b9732011-02-01 15:34:21 -08001059 if (fib_info_cnt >= fib_info_hash_size) {
1060 unsigned int new_size = fib_info_hash_size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct hlist_head *new_info_hash;
1062 struct hlist_head *new_laddrhash;
1063 unsigned int bytes;
1064
1065 if (!new_size)
Eric Dumazetd94ce9b2012-10-21 20:12:09 +00001066 new_size = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 bytes = new_size * sizeof(struct hlist_head *);
David S. Miller123b9732011-02-01 15:34:21 -08001068 new_info_hash = fib_info_hash_alloc(bytes);
1069 new_laddrhash = fib_info_hash_alloc(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (!new_info_hash || !new_laddrhash) {
David S. Miller123b9732011-02-01 15:34:21 -08001071 fib_info_hash_free(new_info_hash, bytes);
1072 fib_info_hash_free(new_laddrhash, bytes);
Joonwoo Park88f83492007-11-26 23:29:32 +08001073 } else
David S. Miller123b9732011-02-01 15:34:21 -08001074 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
David S. Miller123b9732011-02-01 15:34:21 -08001076 if (!fib_info_hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto failure;
1078 }
1079
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001080 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001081 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto failure;
Sergey Popovichaeefa1e2014-05-06 18:23:08 +03001083 fib_info_cnt++;
David S. Miller725d1e12011-01-28 14:05:05 -08001084 if (cfg->fc_mx) {
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001085 fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
David S. Miller725d1e12011-01-28 14:05:05 -08001086 if (!fi->fib_metrics)
1087 goto failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001088 atomic_set(&fi->fib_metrics->refcnt, 1);
David S. Miller725d1e12011-01-28 14:05:05 -08001089 } else
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001090 fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001092 fi->fib_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -07001093 fi->fib_protocol = cfg->fc_protocol;
David S. Miller37e826c2011-03-24 18:06:47 -07001094 fi->fib_scope = cfg->fc_scope;
Thomas Graf4e902c52006-08-17 18:14:52 -07001095 fi->fib_flags = cfg->fc_flags;
1096 fi->fib_priority = cfg->fc_priority;
1097 fi->fib_prefsrc = cfg->fc_prefsrc;
Eric Dumazetf4ef85b2012-10-04 01:25:26 +00001098 fi->fib_type = cfg->fc_type;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001099 fi->fib_tb_id = cfg->fc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 fi->fib_nhs = nhs;
1102 change_nexthops(fi) {
David S. Miller71fceff2010-01-15 01:16:40 -08001103 nexthop_nh->nh_parent = fi;
Eric Dumazetd26b3a72012-07-31 05:45:30 +00001104 nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
Julian Anastasovf8a17172012-10-08 11:41:17 +00001105 if (!nexthop_nh->nh_pcpu_rth_output)
1106 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 } endfor_nexthops(fi)
1108
Florian Westphal6cf9dfd2015-08-31 15:58:44 +02001109 err = fib_convert_metrics(fi, cfg);
1110 if (err)
1111 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Thomas Graf4e902c52006-08-17 18:14:52 -07001113 if (cfg->fc_mp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern6d8422a12017-05-21 10:12:02 -06001115 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -07001116 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 goto failure;
David Ahernc3ab2b42017-05-21 10:12:03 -06001118 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
1119 NL_SET_ERR_MSG(extack,
1120 "Nexthop device index does not match RTA_OIF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001122 }
1123 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
1124 NL_SET_ERR_MSG(extack,
1125 "Nexthop gateway does not match RTA_GATEWAY");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001127 }
Patrick McHardyc7066f72011-01-14 13:36:42 +01001128#ifdef CONFIG_IP_ROUTE_CLASSID
David Ahernc3ab2b42017-05-21 10:12:03 -06001129 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
1130 NL_SET_ERR_MSG(extack,
1131 "Nexthop class id does not match RTA_FLOW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134#endif
1135#else
David Ahernc3ab2b42017-05-21 10:12:03 -06001136 NL_SET_ERR_MSG(extack,
1137 "Multipath support not enabled in kernel");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto err_inval;
1139#endif
1140 } else {
1141 struct fib_nh *nh = fi->fib_nh;
Thomas Graf4e902c52006-08-17 18:14:52 -07001142
Roopa Prabhu571e7222015-07-21 10:43:47 +02001143 if (cfg->fc_encap) {
1144 struct lwtunnel_state *lwtstate;
Roopa Prabhu571e7222015-07-21 10:43:47 +02001145
David Ahernc3ab2b42017-05-21 10:12:03 -06001146 if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
1147 NL_SET_ERR_MSG(extack,
1148 "LWT encap type not specified");
Roopa Prabhu571e7222015-07-21 10:43:47 +02001149 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001150 }
David Ahern30357d72017-01-30 12:07:37 -08001151 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07001152 cfg->fc_encap, AF_INET, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06001153 &lwtstate, extack);
Roopa Prabhu571e7222015-07-21 10:43:47 +02001154 if (err)
1155 goto failure;
1156
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +02001157 nh->nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu571e7222015-07-21 10:43:47 +02001158 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001159 nh->nh_oif = cfg->fc_oif;
1160 nh->nh_gw = cfg->fc_gw;
1161 nh->nh_flags = cfg->fc_flags;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001162#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Graf4e902c52006-08-17 18:14:52 -07001163 nh->nh_tclassid = cfg->fc_flow;
David S. Miller7a9bc9b2012-06-29 01:32:45 -07001164 if (nh->nh_tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -07001165 fi->fib_net->ipv4.fib_num_tclassid_users++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167#ifdef CONFIG_IP_ROUTE_MULTIPATH
1168 nh->nh_weight = 1;
1169#endif
1170 }
1171
Thomas Graf4e902c52006-08-17 18:14:52 -07001172 if (fib_props[cfg->fc_type].error) {
David Ahernc3ab2b42017-05-21 10:12:03 -06001173 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1174 NL_SET_ERR_MSG(extack,
1175 "Gateway, device and multipath can not be specified for this route type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto link_it;
David S. Miller4c8237c2011-03-07 14:27:38 -08001179 } else {
1180 switch (cfg->fc_type) {
1181 case RTN_UNICAST:
1182 case RTN_LOCAL:
1183 case RTN_BROADCAST:
1184 case RTN_ANYCAST:
1185 case RTN_MULTICAST:
1186 break;
1187 default:
David Ahernc3ab2b42017-05-21 10:12:03 -06001188 NL_SET_ERR_MSG(extack, "Invalid route type");
David S. Miller4c8237c2011-03-07 14:27:38 -08001189 goto err_inval;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
David Ahernc3ab2b42017-05-21 10:12:03 -06001193 if (cfg->fc_scope > RT_SCOPE_HOST) {
1194 NL_SET_ERR_MSG(extack, "Invalid scope");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Thomas Graf4e902c52006-08-17 18:14:52 -07001198 if (cfg->fc_scope == RT_SCOPE_HOST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 struct fib_nh *nh = fi->fib_nh;
1200
1201 /* Local address is added. */
David Ahernc3ab2b42017-05-21 10:12:03 -06001202 if (nhs != 1) {
1203 NL_SET_ERR_MSG(extack,
1204 "Route with host scope can not have multiple nexthops");
David Ahern6d8422a12017-05-21 10:12:02 -06001205 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001206 }
1207 if (nh->nh_gw) {
1208 NL_SET_ERR_MSG(extack,
1209 "Route with host scope can not have a gateway");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 nh->nh_scope = RT_SCOPE_NOWHERE;
Denis V. Lunev7462bd742008-01-31 18:49:32 -08001213 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 err = -ENODEV;
Ian Morris51456b22015-04-03 09:17:26 +01001215 if (!nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto failure;
1217 } else {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001218 int linkdown = 0;
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 change_nexthops(fi) {
David Ahern6d8422a12017-05-21 10:12:02 -06001221 err = fib_check_nh(cfg, fi, nexthop_nh, extack);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001222 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto failure;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001224 if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
1225 linkdown++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 } endfor_nexthops(fi)
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001227 if (linkdown == fi->fib_nhs)
1228 fi->fib_flags |= RTNH_F_LINKDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
David Ahernc3ab2b42017-05-21 10:12:03 -06001231 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1232 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
David Ahern021dd3b2015-08-13 14:59:06 -06001233 goto err_inval;
David Ahernc3ab2b42017-05-21 10:12:03 -06001234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
David S. Miller1fc050a2011-03-07 20:54:48 -08001236 change_nexthops(fi) {
David S. Miller436c3b62011-03-24 17:42:21 -07001237 fib_info_update_nh_saddr(net, nexthop_nh);
Peter Nørlund0e884c72015-09-30 10:12:21 +02001238 fib_add_weight(fi, nexthop_nh);
David S. Miller1fc050a2011-03-07 20:54:48 -08001239 } endfor_nexthops(fi)
1240
Peter Nørlund0e884c72015-09-30 10:12:21 +02001241 fib_rebalance(fi);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243link_it:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001244 ofi = fib_find_info(fi);
1245 if (ofi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 fi->fib_dead = 1;
1247 free_fib_info(fi);
1248 ofi->fib_treeref++;
1249 return ofi;
1250 }
1251
1252 fi->fib_treeref++;
1253 atomic_inc(&fi->fib_clntref);
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001254 spin_lock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 hlist_add_head(&fi->fib_hash,
1256 &fib_info_hash[fib_info_hashfn(fi)]);
1257 if (fi->fib_prefsrc) {
1258 struct hlist_head *head;
1259
1260 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1261 hlist_add_head(&fi->fib_lhash, head);
1262 }
1263 change_nexthops(fi) {
1264 struct hlist_head *head;
1265 unsigned int hash;
1266
David S. Miller71fceff2010-01-15 01:16:40 -08001267 if (!nexthop_nh->nh_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001269 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 head = &fib_info_devhash[hash];
David S. Miller71fceff2010-01-15 01:16:40 -08001271 hlist_add_head(&nexthop_nh->nh_hash, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 } endfor_nexthops(fi)
Stephen Hemminger832b4c52006-08-29 16:48:09 -07001273 spin_unlock_bh(&fib_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return fi;
1275
1276err_inval:
1277 err = -EINVAL;
1278
1279failure:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001280 if (fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 fi->fib_dead = 1;
1282 free_fib_info(fi);
1283 }
Thomas Graf4e902c52006-08-17 18:14:52 -07001284
1285 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Eric W. Biederman15e47302012-09-07 20:12:54 +00001288int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
David S. Miller37e826c2011-03-24 18:06:47 -07001289 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001290 struct fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Thomas Grafbe403ea2006-08-17 18:15:17 -07001292 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Eric W. Biederman15e47302012-09-07 20:12:54 +00001295 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01001296 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001297 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001298
1299 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 rtm->rtm_family = AF_INET;
1301 rtm->rtm_dst_len = dst_len;
1302 rtm->rtm_src_len = 0;
1303 rtm->rtm_tos = tos;
Krzysztof Piotr Oledzki709772e2008-06-10 15:44:49 -07001304 if (tb_id < 256)
1305 rtm->rtm_table = tb_id;
1306 else
1307 rtm->rtm_table = RT_TABLE_COMPAT;
David S. Millerf3756b72012-04-01 20:39:02 -04001308 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1309 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 rtm->rtm_type = type;
1311 rtm->rtm_flags = fi->fib_flags;
David S. Miller37e826c2011-03-24 18:06:47 -07001312 rtm->rtm_scope = fi->fib_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 rtm->rtm_protocol = fi->fib_protocol;
Thomas Grafbe403ea2006-08-17 18:15:17 -07001314
David S. Millerf3756b72012-04-01 20:39:02 -04001315 if (rtm->rtm_dst_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02001316 nla_put_in_addr(skb, RTA_DST, dst))
David S. Millerf3756b72012-04-01 20:39:02 -04001317 goto nla_put_failure;
1318 if (fi->fib_priority &&
1319 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1320 goto nla_put_failure;
Eric Dumazet3fb07da2017-05-25 14:27:35 -07001321 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001322 goto nla_put_failure;
1323
David S. Millerf3756b72012-04-01 20:39:02 -04001324 if (fi->fib_prefsrc &&
Jiri Benc930345e2015-03-29 16:59:25 +02001325 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
David S. Millerf3756b72012-04-01 20:39:02 -04001326 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (fi->fib_nhs == 1) {
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001328 struct in_device *in_dev;
1329
David S. Millerf3756b72012-04-01 20:39:02 -04001330 if (fi->fib_nh->nh_gw &&
Jiri Benc930345e2015-03-29 16:59:25 +02001331 nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
David S. Millerf3756b72012-04-01 20:39:02 -04001332 goto nla_put_failure;
1333 if (fi->fib_nh->nh_oif &&
1334 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1335 goto nla_put_failure;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001336 if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
Andy Gospodarek96ac5cc2015-06-26 19:37:11 -04001337 in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001338 if (in_dev &&
1339 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1340 rtm->rtm_flags |= RTNH_F_DEAD;
1341 }
Patrick McHardyc7066f72011-01-14 13:36:42 +01001342#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001343 if (fi->fib_nh[0].nh_tclassid &&
1344 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1345 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001346#endif
David Ahernea7a8082017-01-11 14:29:54 -08001347 if (fi->fib_nh->nh_lwtstate &&
1348 lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
1349 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351#ifdef CONFIG_IP_ROUTE_MULTIPATH
1352 if (fi->fib_nhs > 1) {
Thomas Grafbe403ea2006-08-17 18:15:17 -07001353 struct rtnexthop *rtnh;
1354 struct nlattr *mp;
1355
1356 mp = nla_nest_start(skb, RTA_MULTIPATH);
Ian Morris51456b22015-04-03 09:17:26 +01001357 if (!mp)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001358 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 for_nexthops(fi) {
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001361 struct in_device *in_dev;
1362
Thomas Grafbe403ea2006-08-17 18:15:17 -07001363 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
Ian Morris51456b22015-04-03 09:17:26 +01001364 if (!rtnh)
Thomas Grafbe403ea2006-08-17 18:15:17 -07001365 goto nla_put_failure;
1366
1367 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001368 if (nh->nh_flags & RTNH_F_LINKDOWN) {
Andy Gospodarek96ac5cc2015-06-26 19:37:11 -04001369 in_dev = __in_dev_get_rtnl(nh->nh_dev);
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001370 if (in_dev &&
1371 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1372 rtnh->rtnh_flags |= RTNH_F_DEAD;
1373 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07001374 rtnh->rtnh_hops = nh->nh_weight - 1;
1375 rtnh->rtnh_ifindex = nh->nh_oif;
1376
David S. Millerf3756b72012-04-01 20:39:02 -04001377 if (nh->nh_gw &&
Jiri Benc930345e2015-03-29 16:59:25 +02001378 nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
David S. Millerf3756b72012-04-01 20:39:02 -04001379 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +01001380#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -04001381 if (nh->nh_tclassid &&
1382 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1383 goto nla_put_failure;
Patrick McHardy8265abc2006-07-21 15:09:55 -07001384#endif
David Ahernea7a8082017-01-11 14:29:54 -08001385 if (nh->nh_lwtstate &&
1386 lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
1387 goto nla_put_failure;
1388
Thomas Grafbe403ea2006-08-17 18:15:17 -07001389 /* length of rtnetlink header + attributes */
1390 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } endfor_nexthops(fi);
Thomas Grafbe403ea2006-08-17 18:15:17 -07001392
1393 nla_nest_end(skb, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395#endif
Johannes Berg053c0952015-01-16 22:09:00 +01001396 nlmsg_end(skb, nlh);
1397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Thomas Grafbe403ea2006-08-17 18:15:17 -07001399nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001400 nlmsg_cancel(skb, nlh);
1401 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001405 * Update FIB if:
1406 * - local address disappeared -> we must delete all the entries
1407 * referring to it.
1408 * - device went down -> we must shutdown all nexthops going via it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001410int fib_sync_down_addr(struct net_device *dev, __be32 local)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001411{
1412 int ret = 0;
1413 unsigned int hash = fib_laddr_hashfn(local);
1414 struct hlist_head *head = &fib_info_laddrhash[hash];
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001415 struct net *net = dev_net(dev);
1416 int tb_id = l3mdev_fib_table(dev);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001417 struct fib_info *fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Ian Morris51456b22015-04-03 09:17:26 +01001419 if (!fib_info_laddrhash || local == 0)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001420 return 0;
1421
Sasha Levinb67bfe02013-02-27 17:06:00 -08001422 hlist_for_each_entry(fi, head, fib_lhash) {
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001423 if (!net_eq(fi->fib_net, net) ||
1424 fi->fib_tb_id != tb_id)
Denis V. Lunev4814bdb2008-01-31 18:50:07 -08001425 continue;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001426 if (fi->fib_prefsrc == local) {
1427 fi->fib_flags |= RTNH_F_DEAD;
1428 ret++;
1429 }
1430 }
1431 return ret;
1432}
1433
Ido Schimmel982acb92017-02-08 11:16:39 +01001434static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
1435 enum fib_event_type event_type)
1436{
1437 struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
1438 struct fib_nh_notifier_info info = {
1439 .fib_nh = fib_nh,
1440 };
1441
1442 switch (event_type) {
1443 case FIB_EVENT_NH_ADD:
1444 if (fib_nh->nh_flags & RTNH_F_DEAD)
1445 break;
1446 if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1447 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1448 break;
1449 return call_fib_notifiers(dev_net(fib_nh->nh_dev), event_type,
1450 &info.info);
1451 case FIB_EVENT_NH_DEL:
1452 if ((IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1453 fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
1454 (fib_nh->nh_flags & RTNH_F_DEAD))
1455 return call_fib_notifiers(dev_net(fib_nh->nh_dev),
1456 event_type, &info.info);
1457 default:
1458 break;
1459 }
1460
1461 return NOTIFY_DONE;
1462}
1463
Julian Anastasov4f823de2015-10-30 10:23:33 +02001464/* Event force Flags Description
1465 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1466 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1467 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1468 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1469 */
1470int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 int ret = 0;
1473 int scope = RT_SCOPE_NOWHERE;
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001474 struct fib_info *prev_fi = NULL;
1475 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1476 struct hlist_head *head = &fib_info_devhash[hash];
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001477 struct fib_nh *nh;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001478
Julian Anastasov4f823de2015-10-30 10:23:33 +02001479 if (force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 scope = -1;
1481
Sasha Levinb67bfe02013-02-27 17:06:00 -08001482 hlist_for_each_entry(nh, head, nh_hash) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001483 struct fib_info *fi = nh->nh_parent;
1484 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001486 BUG_ON(!fi->fib_nhs);
1487 if (nh->nh_dev != dev || fi == prev_fi)
1488 continue;
1489 prev_fi = fi;
1490 dead = 0;
1491 change_nexthops(fi) {
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001492 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001493 dead++;
David S. Miller71fceff2010-01-15 01:16:40 -08001494 else if (nexthop_nh->nh_dev == dev &&
1495 nexthop_nh->nh_scope != scope) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001496 switch (event) {
1497 case NETDEV_DOWN:
1498 case NETDEV_UNREGISTER:
1499 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1500 /* fall through */
1501 case NETDEV_CHANGE:
1502 nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
1503 break;
1504 }
Ido Schimmel982acb92017-02-08 11:16:39 +01001505 call_fib_nh_notifiers(nexthop_nh,
1506 FIB_EVENT_NH_DEL);
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001507 dead++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001509#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001510 if (event == NETDEV_UNREGISTER &&
1511 nexthop_nh->nh_dev == dev) {
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001512 dead = fi->fib_nhs;
1513 break;
1514 }
1515#endif
1516 } endfor_nexthops(fi)
1517 if (dead == fi->fib_nhs) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001518 switch (event) {
1519 case NETDEV_DOWN:
1520 case NETDEV_UNREGISTER:
1521 fi->fib_flags |= RTNH_F_DEAD;
1522 /* fall through */
1523 case NETDEV_CHANGE:
1524 fi->fib_flags |= RTNH_F_LINKDOWN;
1525 break;
1526 }
Denis V. Lunev85326fa2008-01-31 18:48:47 -08001527 ret++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001529
1530 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
1533 return ret;
1534}
1535
David S. Miller0c838ff2011-01-31 16:16:50 -08001536/* Must be invoked inside of an RCU protected region. */
David Ahernc7b371e2017-01-05 19:33:59 -08001537static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
David S. Miller0c838ff2011-01-31 16:16:50 -08001538{
1539 struct fib_info *fi = NULL, *last_resort = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001540 struct hlist_head *fa_head = res->fa_head;
David S. Miller0c838ff2011-01-31 16:16:50 -08001541 struct fib_table *tb = res->table;
Julian Anastasov18a912e2015-07-22 10:43:22 +03001542 u8 slen = 32 - res->prefixlen;
David S. Miller0c838ff2011-01-31 16:16:50 -08001543 int order = -1, last_idx = -1;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001544 struct fib_alias *fa, *fa1 = NULL;
1545 u32 last_prio = res->fi->fib_priority;
1546 u8 last_tos = 0;
David S. Miller0c838ff2011-01-31 16:16:50 -08001547
Alexander Duyck56315f92015-02-25 15:31:31 -08001548 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001549 struct fib_info *next_fi = fa->fa_info;
1550
Julian Anastasov18a912e2015-07-22 10:43:22 +03001551 if (fa->fa_slen != slen)
1552 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001553 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1554 continue;
Julian Anastasov18a912e2015-07-22 10:43:22 +03001555 if (fa->tb_id != tb->tb_id)
1556 continue;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001557 if (next_fi->fib_priority > last_prio &&
1558 fa->fa_tos == last_tos) {
1559 if (last_tos)
1560 continue;
1561 break;
1562 }
1563 if (next_fi->fib_flags & RTNH_F_DEAD)
1564 continue;
1565 last_tos = fa->fa_tos;
1566 last_prio = next_fi->fib_priority;
1567
David S. Miller37e826c2011-03-24 18:06:47 -07001568 if (next_fi->fib_scope != res->scope ||
David S. Miller0c838ff2011-01-31 16:16:50 -08001569 fa->fa_type != RTN_UNICAST)
1570 continue;
David S. Miller0c838ff2011-01-31 16:16:50 -08001571 if (!next_fi->fib_nh[0].nh_gw ||
1572 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1573 continue;
1574
1575 fib_alias_accessed(fa);
1576
Ian Morris51456b22015-04-03 09:17:26 +01001577 if (!fi) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001578 if (next_fi != res->fi)
1579 break;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001580 fa1 = fa;
David S. Miller0c838ff2011-01-31 16:16:50 -08001581 } else if (!fib_detect_death(fi, order, &last_resort,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001582 &last_idx, fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001583 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001584 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001585 goto out;
1586 }
1587 fi = next_fi;
1588 order++;
1589 }
1590
Ian Morris51456b22015-04-03 09:17:26 +01001591 if (order <= 0 || !fi) {
Julian Anastasov2392deb2015-07-22 10:43:23 +03001592 if (fa1)
1593 fa1->fa_default = -1;
David S. Miller0c838ff2011-01-31 16:16:50 -08001594 goto out;
1595 }
1596
1597 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
Julian Anastasov2392deb2015-07-22 10:43:23 +03001598 fa1->fa_default)) {
David S. Miller0c838ff2011-01-31 16:16:50 -08001599 fib_result_assign(res, fi);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001600 fa1->fa_default = order;
David S. Miller0c838ff2011-01-31 16:16:50 -08001601 goto out;
1602 }
1603
1604 if (last_idx >= 0)
1605 fib_result_assign(res, last_resort);
Julian Anastasov2392deb2015-07-22 10:43:23 +03001606 fa1->fa_default = last_idx;
David S. Miller0c838ff2011-01-31 16:16:50 -08001607out:
Eric Dumazet31d40932011-02-14 11:23:04 -08001608 return;
David S. Miller0c838ff2011-01-31 16:16:50 -08001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001612 * Dead device goes up. We wake up dead nexthops.
1613 * It takes sense only on multipath routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001615int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct fib_info *prev_fi;
1618 unsigned int hash;
1619 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct fib_nh *nh;
1621 int ret;
1622
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001623 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return 0;
1625
Julian Anastasovc9b32922015-10-30 10:23:34 +02001626 if (nh_flags & RTNH_F_DEAD) {
1627 unsigned int flags = dev_get_flags(dev);
1628
1629 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1630 nh_flags |= RTNH_F_LINKDOWN;
1631 }
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 prev_fi = NULL;
1634 hash = fib_devindex_hashfn(dev->ifindex);
1635 head = &fib_info_devhash[hash];
1636 ret = 0;
1637
Sasha Levinb67bfe02013-02-27 17:06:00 -08001638 hlist_for_each_entry(nh, head, nh_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct fib_info *fi = nh->nh_parent;
1640 int alive;
1641
1642 BUG_ON(!fi->fib_nhs);
1643 if (nh->nh_dev != dev || fi == prev_fi)
1644 continue;
1645
1646 prev_fi = fi;
1647 alive = 0;
1648 change_nexthops(fi) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001649 if (!(nexthop_nh->nh_flags & nh_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 alive++;
1651 continue;
1652 }
Ian Morris51456b22015-04-03 09:17:26 +01001653 if (!nexthop_nh->nh_dev ||
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001654 !(nexthop_nh->nh_dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 continue;
David S. Miller71fceff2010-01-15 01:16:40 -08001656 if (nexthop_nh->nh_dev != dev ||
1657 !__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 continue;
1659 alive++;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001660 nexthop_nh->nh_flags &= ~nh_flags;
Ido Schimmel982acb92017-02-08 11:16:39 +01001661 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 } endfor_nexthops(fi)
1663
1664 if (alive > 0) {
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001665 fi->fib_flags &= ~nh_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 ret++;
1667 }
Peter Nørlund0e884c72015-09-30 10:12:21 +02001668
1669 fib_rebalance(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672 return ret;
1673}
1674
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001675#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Aherna6db4492016-04-07 07:21:00 -07001676static bool fib_good_nh(const struct fib_nh *nh)
1677{
1678 int state = NUD_REACHABLE;
1679
1680 if (nh->nh_scope == RT_SCOPE_LINK) {
1681 struct neighbour *n;
1682
1683 rcu_read_lock_bh();
1684
Eric Dumazetd985d152016-08-18 10:19:34 -07001685 n = __ipv4_neigh_lookup_noref(nh->nh_dev,
1686 (__force u32)nh->nh_gw);
David Aherna6db4492016-04-07 07:21:00 -07001687 if (n)
1688 state = n->nud_state;
1689
1690 rcu_read_unlock_bh();
1691 }
1692
1693 return !!(state & NUD_VALID);
1694}
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001695
Peter Nørlund0e884c72015-09-30 10:12:21 +02001696void fib_select_multipath(struct fib_result *res, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct fib_info *fi = res->fi;
David Aherna6db4492016-04-07 07:21:00 -07001699 struct net *net = fi->fib_net;
1700 bool first = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Peter Nørlund0e884c72015-09-30 10:12:21 +02001702 for_nexthops(fi) {
1703 if (hash > atomic_read(&nh->nh_upper_bound))
1704 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
David Aherna6db4492016-04-07 07:21:00 -07001706 if (!net->ipv4.sysctl_fib_multipath_use_neigh ||
1707 fib_good_nh(nh)) {
1708 res->nh_sel = nhsel;
1709 return;
1710 }
1711 if (!first) {
1712 res->nh_sel = nhsel;
1713 first = true;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 } endfor_nexthops(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717#endif
David Ahern3ce58d82015-10-05 08:51:25 -07001718
1719void fib_select_path(struct net *net, struct fib_result *res,
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001720 struct flowi4 *fl4, const struct sk_buff *skb)
David Ahern3ce58d82015-10-05 08:51:25 -07001721{
David Ahern7a18c5b2017-01-10 14:37:35 -08001722 bool oif_check;
1723
1724 oif_check = (fl4->flowi4_oif == 0 ||
1725 fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF);
1726
David Ahern3ce58d82015-10-05 08:51:25 -07001727#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern7a18c5b2017-01-10 14:37:35 -08001728 if (res->fi->fib_nhs > 1 && oif_check) {
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001729 int h = fib_multipath_hash(res->fi, fl4, skb);
Paolo Abeni9920e482015-10-29 22:20:40 +01001730
Nikolay Aleksandrovbf4e0a32017-03-16 15:28:00 +02001731 fib_select_multipath(res, h);
David Ahern3ce58d82015-10-05 08:51:25 -07001732 }
1733 else
1734#endif
1735 if (!res->prefixlen &&
1736 res->table->tb_num_default > 1 &&
David Ahern7a18c5b2017-01-10 14:37:35 -08001737 res->type == RTN_UNICAST && oif_check)
David Ahern3ce58d82015-10-05 08:51:25 -07001738 fib_select_default(fl4, res);
1739
1740 if (!fl4->saddr)
1741 fl4->saddr = FIB_RES_PREFSRC(net, *res);
1742}
1743EXPORT_SYMBOL_GPL(fib_select_path);