]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/fib_semantics.c
net: lwtunnel: Add extack to encap attr validation
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / fib_semantics.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
8 * 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
7c0f6ba6 16#include <linux/uaccess.h>
1da177e4
LT
17#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>
14c85021 28#include <linux/inetdevice.h>
1da177e4
LT
29#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/proc_fs.h>
32#include <linux/skbuff.h>
1da177e4 33#include <linux/init.h>
5a0e3ad6 34#include <linux/slab.h>
c3ab2b4e 35#include <linux/netlink.h>
1da177e4 36
14c85021 37#include <net/arp.h>
1da177e4
LT
38#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>
f21c7bc5 44#include <net/netlink.h>
4e902c57 45#include <net/nexthop.h>
571e7226 46#include <net/lwtunnel.h>
1da177e4
LT
47
48#include "fib_lookup.h"
49
832b4c5e 50static DEFINE_SPINLOCK(fib_info_lock);
1da177e4
LT
51static struct hlist_head *fib_info_hash;
52static struct hlist_head *fib_info_laddrhash;
123b9731 53static unsigned int fib_info_hash_size;
1da177e4
LT
54static 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
1da177e4 61
6a31d2a9
ED
62#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++)
67
68#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++)
1da177e4
LT
73
74#else /* CONFIG_IP_ROUTE_MULTIPATH */
75
76/* Hope, that gcc will optimize it to get rid of dummy loop */
77
6a31d2a9
ED
78#define for_nexthops(fi) { \
79 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
80 for (nhsel = 0; nhsel < 1; nhsel++)
1da177e4 81
6a31d2a9
ED
82#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++)
1da177e4
LT
86
87#endif /* CONFIG_IP_ROUTE_MULTIPATH */
88
89#define endfor_nexthops(fi) }
90
91
3be0686b 92const struct fib_prop fib_props[RTN_MAX + 1] = {
6a31d2a9 93 [RTN_UNSPEC] = {
1da177e4
LT
94 .error = 0,
95 .scope = RT_SCOPE_NOWHERE,
6a31d2a9
ED
96 },
97 [RTN_UNICAST] = {
1da177e4
LT
98 .error = 0,
99 .scope = RT_SCOPE_UNIVERSE,
6a31d2a9
ED
100 },
101 [RTN_LOCAL] = {
1da177e4
LT
102 .error = 0,
103 .scope = RT_SCOPE_HOST,
6a31d2a9
ED
104 },
105 [RTN_BROADCAST] = {
1da177e4
LT
106 .error = 0,
107 .scope = RT_SCOPE_LINK,
6a31d2a9
ED
108 },
109 [RTN_ANYCAST] = {
1da177e4
LT
110 .error = 0,
111 .scope = RT_SCOPE_LINK,
6a31d2a9
ED
112 },
113 [RTN_MULTICAST] = {
1da177e4
LT
114 .error = 0,
115 .scope = RT_SCOPE_UNIVERSE,
6a31d2a9
ED
116 },
117 [RTN_BLACKHOLE] = {
1da177e4
LT
118 .error = -EINVAL,
119 .scope = RT_SCOPE_UNIVERSE,
6a31d2a9
ED
120 },
121 [RTN_UNREACHABLE] = {
1da177e4
LT
122 .error = -EHOSTUNREACH,
123 .scope = RT_SCOPE_UNIVERSE,
6a31d2a9
ED
124 },
125 [RTN_PROHIBIT] = {
1da177e4
LT
126 .error = -EACCES,
127 .scope = RT_SCOPE_UNIVERSE,
6a31d2a9
ED
128 },
129 [RTN_THROW] = {
1da177e4
LT
130 .error = -EAGAIN,
131 .scope = RT_SCOPE_UNIVERSE,
6a31d2a9
ED
132 },
133 [RTN_NAT] = {
1da177e4
LT
134 .error = -EINVAL,
135 .scope = RT_SCOPE_NOWHERE,
6a31d2a9
ED
136 },
137 [RTN_XRESOLVE] = {
1da177e4
LT
138 .error = -EINVAL,
139 .scope = RT_SCOPE_NOWHERE,
6a31d2a9 140 },
1da177e4
LT
141};
142
c5038a83
DM
143static void rt_fibinfo_free(struct rtable __rcu **rtp)
144{
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
4895c771
DM
158static void free_nh_exceptions(struct fib_nh *nh)
159{
caa41527 160 struct fnhe_hash_bucket *hash;
4895c771
DM
161 int i;
162
caa41527
ED
163 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
164 if (!hash)
165 return;
4895c771
DM
166 for (i = 0; i < FNHE_HASH_SIZE; i++) {
167 struct fib_nh_exception *fnhe;
168
5abf7f7e 169 fnhe = rcu_dereference_protected(hash[i].chain, 1);
4895c771
DM
170 while (fnhe) {
171 struct fib_nh_exception *next;
172
5abf7f7e 173 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
c5038a83 174
2ffae99d
TT
175 rt_fibinfo_free(&fnhe->fnhe_rth_input);
176 rt_fibinfo_free(&fnhe->fnhe_rth_output);
c5038a83 177
4895c771
DM
178 kfree(fnhe);
179
180 fnhe = next;
181 }
182 }
183 kfree(hash);
184}
185
c5038a83 186static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
d26b3a7c
ED
187{
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
1da177e4 203/* Release a nexthop info record */
19c1ea14
YZ
204static void free_fib_info_rcu(struct rcu_head *head)
205{
206 struct fib_info *fi = container_of(head, struct fib_info, rcu);
3fb07daf 207 struct dst_metrics *m;
19c1ea14 208
e49cc0da
YZ
209 change_nexthops(fi) {
210 if (nexthop_nh->nh_dev)
211 dev_put(nexthop_nh->nh_dev);
5a6228a0 212 lwtstate_put(nexthop_nh->nh_lwtstate);
caa41527 213 free_nh_exceptions(nexthop_nh);
c5038a83
DM
214 rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
215 rt_fibinfo_free(&nexthop_nh->nh_rth_input);
e49cc0da
YZ
216 } endfor_nexthops(fi);
217
3fb07daf
ED
218 m = fi->fib_metrics;
219 if (m != &dst_default_metrics && atomic_dec_and_test(&m->refcnt))
220 kfree(m);
19c1ea14
YZ
221 kfree(fi);
222}
1da177e4
LT
223
224void free_fib_info(struct fib_info *fi)
225{
226 if (fi->fib_dead == 0) {
058bd4d2 227 pr_warn("Freeing alive fib_info %p\n", fi);
1da177e4
LT
228 return;
229 }
1da177e4 230 fib_info_cnt--;
7a9bc9b8
DM
231#ifdef CONFIG_IP_ROUTE_CLASSID
232 change_nexthops(fi) {
233 if (nexthop_nh->nh_tclassid)
f4530fa5 234 fi->fib_net->ipv4.fib_num_tclassid_users--;
7a9bc9b8
DM
235 } endfor_nexthops(fi);
236#endif
19c1ea14 237 call_rcu(&fi->rcu, free_fib_info_rcu);
1da177e4 238}
b423cb10 239EXPORT_SYMBOL_GPL(free_fib_info);
1da177e4
LT
240
241void fib_release_info(struct fib_info *fi)
242{
832b4c5e 243 spin_lock_bh(&fib_info_lock);
1da177e4
LT
244 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) {
71fceff0 249 if (!nexthop_nh->nh_dev)
1da177e4 250 continue;
71fceff0 251 hlist_del(&nexthop_nh->nh_hash);
1da177e4
LT
252 } endfor_nexthops(fi)
253 fi->fib_dead = 1;
254 fib_info_put(fi);
255 }
832b4c5e 256 spin_unlock_bh(&fib_info_lock);
1da177e4
LT
257}
258
6a31d2a9 259static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
1da177e4
LT
260{
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
c7066f70 270#ifdef CONFIG_IP_ROUTE_CLASSID
1da177e4
LT
271 nh->nh_tclassid != onh->nh_tclassid ||
272#endif
571e7226 273 lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
8a3d0316 274 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
1da177e4
LT
275 return -1;
276 onh++;
277 } endfor_nexthops(fi);
278 return 0;
279}
280
88ebc72f
DM
281static 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
1da177e4
LT
290static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
291{
123b9731 292 unsigned int mask = (fib_info_hash_size - 1);
1da177e4
LT
293 unsigned int val = fi->fib_nhs;
294
37e826c5 295 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
81f7bf6c 296 val ^= (__force u32)fi->fib_prefsrc;
1da177e4 297 val ^= fi->fib_priority;
88ebc72f
DM
298 for_nexthops(fi) {
299 val ^= fib_devindex_hashfn(nh->nh_oif);
300 } endfor_nexthops(fi)
1da177e4
LT
301
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;
1da177e4
LT
308 struct fib_info *fi;
309 unsigned int hash;
310
311 hash = fib_info_hashfn(nfi);
312 head = &fib_info_hash[hash];
313
b67bfe0d 314 hlist_for_each_entry(fi, head, fib_hash) {
09ad9bc7 315 if (!net_eq(fi->fib_net, nfi->fib_net))
4814bdbd 316 continue;
1da177e4
LT
317 if (fi->fib_nhs != nfi->fib_nhs)
318 continue;
319 if (nfi->fib_protocol == fi->fib_protocol &&
37e826c5 320 nfi->fib_scope == fi->fib_scope &&
1da177e4
LT
321 nfi->fib_prefsrc == fi->fib_prefsrc &&
322 nfi->fib_priority == fi->fib_priority &&
f4ef85bb 323 nfi->fib_type == fi->fib_type &&
1da177e4 324 memcmp(nfi->fib_metrics, fi->fib_metrics,
fcd13f42 325 sizeof(u32) * RTAX_MAX) == 0 &&
8a3d0316 326 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
1da177e4
LT
327 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
328 return fi;
329 }
330
331 return NULL;
332}
333
1da177e4 334/* Check, that the gateway is already configured.
6a31d2a9 335 * Used only by redirect accept routine.
1da177e4 336 */
d878e72e 337int ip_fib_check_default(__be32 gw, struct net_device *dev)
1da177e4
LT
338{
339 struct hlist_head *head;
1da177e4
LT
340 struct fib_nh *nh;
341 unsigned int hash;
342
832b4c5e 343 spin_lock(&fib_info_lock);
1da177e4
LT
344
345 hash = fib_devindex_hashfn(dev->ifindex);
346 head = &fib_info_devhash[hash];
b67bfe0d 347 hlist_for_each_entry(nh, head, nh_hash) {
1da177e4
LT
348 if (nh->nh_dev == dev &&
349 nh->nh_gw == gw &&
6a31d2a9 350 !(nh->nh_flags & RTNH_F_DEAD)) {
832b4c5e 351 spin_unlock(&fib_info_lock);
1da177e4
LT
352 return 0;
353 }
354 }
355
832b4c5e 356 spin_unlock(&fib_info_lock);
1da177e4
LT
357
358 return -1;
359}
360
339bf98f
TG
361static 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 */
ea697639
DB
367 + nla_total_size(4) /* RTA_PREFSRC */
368 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
339bf98f
TG
369
370 /* space for nested metrics */
371 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
372
373 if (fi->fib_nhs) {
571e7226 374 size_t nh_encapsize = 0;
339bf98f
TG
375 /* 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
571e7226
RP
383 /* 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
339bf98f 394 /* all nexthops are packed in a nested attribute */
571e7226
RP
395 payload += nla_total_size((fi->fib_nhs * nhsize) +
396 nh_encapsize);
397
339bf98f
TG
398 }
399
400 return payload;
401}
402
81f7bf6c 403void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
9877b253 404 int dst_len, u32 tb_id, const struct nl_info *info,
b8f55831 405 unsigned int nlm_flags)
1da177e4
LT
406{
407 struct sk_buff *skb;
4e902c57 408 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
f21c7bc5 409 int err = -ENOBUFS;
1da177e4 410
339bf98f 411 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
51456b29 412 if (!skb)
f21c7bc5 413 goto errout;
1da177e4 414
15e47304 415 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
37e826c5 416 fa->fa_type, key, dst_len,
b8f55831 417 fa->fa_tos, fa->fa_info, nlm_flags);
26932566
PM
418 if (err < 0) {
419 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
420 WARN_ON(err == -EMSGSIZE);
421 kfree_skb(skb);
422 goto errout;
423 }
15e47304 424 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
1ce85fe4
PNA
425 info->nlh, GFP_KERNEL);
426 return;
f21c7bc5
TG
427errout:
428 if (err < 0)
4d1169c1 429 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
1da177e4
LT
430}
431
c9cb6b6e
SH
432static int fib_detect_death(struct fib_info *fi, int order,
433 struct fib_info **last_resort, int *last_idx,
434 int dflt)
1da177e4
LT
435{
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);
88f64320
JA
443 } else {
444 return 0;
1da177e4 445 }
d9319100 446 if (state == NUD_REACHABLE)
1da177e4 447 return 0;
6a31d2a9 448 if ((state & NUD_VALID) && order != dflt)
1da177e4 449 return 0;
6a31d2a9 450 if ((state & NUD_VALID) ||
88f64320 451 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
1da177e4
LT
452 *last_resort = fi;
453 *last_idx = order;
454 }
455 return 1;
456}
457
458#ifdef CONFIG_IP_ROUTE_MULTIPATH
459
6d8422a1
DA
460static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
461 struct netlink_ext_ack *extack)
1da177e4
LT
462{
463 int nhs = 0;
1da177e4 464
4e902c57 465 while (rtnh_ok(rtnh, remaining)) {
1da177e4 466 nhs++;
4e902c57
TG
467 rtnh = rtnh_next(rtnh, &remaining);
468 }
469
470 /* leftover implies invalid nexthop configuration, discard it */
c3ab2b4e
DA
471 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;
1da177e4
LT
478}
479
4e902c57 480static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
6d8422a1
DA
481 int remaining, struct fib_config *cfg,
482 struct netlink_ext_ack *extack)
1da177e4 483{
571e7226
RP
484 int ret;
485
1da177e4 486 change_nexthops(fi) {
4e902c57
TG
487 int attrlen;
488
c3ab2b4e
DA
489 if (!rtnh_ok(rtnh, remaining)) {
490 NL_SET_ERR_MSG(extack,
491 "Invalid nexthop configuration - extra data after nexthop");
1da177e4 492 return -EINVAL;
c3ab2b4e 493 }
4e902c57 494
c3ab2b4e
DA
495 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");
80610229 498 return -EINVAL;
c3ab2b4e 499 }
80610229 500
71fceff0
DM
501 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;
4e902c57
TG
505
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);
67b61f6c 511 nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
c7066f70 512#ifdef CONFIG_IP_ROUTE_CLASSID
4e902c57 513 nla = nla_find(attrs, attrlen, RTA_FLOW);
71fceff0 514 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
7a9bc9b8 515 if (nexthop_nh->nh_tclassid)
f4530fa5 516 fi->fib_net->ipv4.fib_num_tclassid_users++;
1da177e4 517#endif
571e7226
RP
518 nla = nla_find(attrs, attrlen, RTA_ENCAP);
519 if (nla) {
520 struct lwtunnel_state *lwtstate;
571e7226
RP
521 struct nlattr *nla_entype;
522
523 nla_entype = nla_find(attrs, attrlen,
524 RTA_ENCAP_TYPE);
c3ab2b4e
DA
525 if (!nla_entype) {
526 NL_SET_BAD_ATTR(extack, nla);
527 NL_SET_ERR_MSG(extack,
528 "Encap type is missing");
571e7226 529 goto err_inval;
c3ab2b4e 530 }
30357d7d
DA
531
532 ret = lwtunnel_build_state(nla_get_u16(
571e7226 533 nla_entype),
127eb7cd
TH
534 nla, AF_INET, cfg,
535 &lwtstate);
571e7226
RP
536 if (ret)
537 goto errout;
5a6228a0
ND
538 nexthop_nh->nh_lwtstate =
539 lwtstate_get(lwtstate);
571e7226 540 }
1da177e4 541 }
4e902c57
TG
542
543 rtnh = rtnh_next(rtnh, &remaining);
1da177e4 544 } endfor_nexthops(fi);
4e902c57 545
1da177e4 546 return 0;
571e7226
RP
547
548err_inval:
549 ret = -EINVAL;
550
551errout:
552 return ret;
1da177e4
LT
553}
554
0e884c78
PN
555static 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
51161aa9 569 in_dev = __in_dev_get_rtnl(nh->nh_dev);
0e884c78
PN
570
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
51161aa9 583 in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
0e884c78
PN
584
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;
0a837fe4
PN
593 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
594 total) - 1;
0e884c78
PN
595 }
596
597 atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
598 } endfor_nexthops(fi);
0e884c78
PN
599}
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 */
1da177e4 613
30357d7d 614static int fib_encap_match(u16 encap_type,
e01286ef 615 struct nlattr *encap,
30357d7d 616 const struct fib_nh *nh,
127eb7cd 617 const struct fib_config *cfg)
571e7226
RP
618{
619 struct lwtunnel_state *lwtstate;
df383e62 620 int ret, result = 0;
571e7226
RP
621
622 if (encap_type == LWTUNNEL_ENCAP_NONE)
623 return 0;
624
30357d7d 625 ret = lwtunnel_build_state(encap_type, encap,
127eb7cd 626 AF_INET, cfg, &lwtstate);
df383e62
JB
627 if (!ret) {
628 result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
629 lwtstate_free(lwtstate);
630 }
571e7226 631
df383e62 632 return result;
571e7226
RP
633}
634
4e902c57 635int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
1da177e4
LT
636{
637#ifdef CONFIG_IP_ROUTE_MULTIPATH
4e902c57
TG
638 struct rtnexthop *rtnh;
639 int remaining;
1da177e4
LT
640#endif
641
4e902c57 642 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
1da177e4
LT
643 return 1;
644
4e902c57 645 if (cfg->fc_oif || cfg->fc_gw) {
571e7226 646 if (cfg->fc_encap) {
30357d7d
DA
647 if (fib_encap_match(cfg->fc_encap_type,
648 cfg->fc_encap, fi->fib_nh, cfg))
571e7226
RP
649 return 1;
650 }
4e902c57
TG
651 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
652 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
1da177e4
LT
653 return 0;
654 return 1;
655 }
656
657#ifdef CONFIG_IP_ROUTE_MULTIPATH
51456b29 658 if (!cfg->fc_mp)
1da177e4 659 return 0;
4e902c57
TG
660
661 rtnh = cfg->fc_mp;
662 remaining = cfg->fc_mp_len;
e905a9ed 663
1da177e4 664 for_nexthops(fi) {
4e902c57 665 int attrlen;
1da177e4 666
4e902c57 667 if (!rtnh_ok(rtnh, remaining))
1da177e4 668 return -EINVAL;
4e902c57
TG
669
670 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
1da177e4 671 return 1;
4e902c57
TG
672
673 attrlen = rtnh_attrlen(rtnh);
f76936d0 674 if (attrlen > 0) {
4e902c57
TG
675 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
676
677 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
67b61f6c 678 if (nla && nla_get_in_addr(nla) != nh->nh_gw)
1da177e4 679 return 1;
c7066f70 680#ifdef CONFIG_IP_ROUTE_CLASSID
4e902c57
TG
681 nla = nla_find(attrs, attrlen, RTA_FLOW);
682 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
1da177e4
LT
683 return 1;
684#endif
685 }
4e902c57
TG
686
687 rtnh = rtnh_next(rtnh, &remaining);
1da177e4
LT
688 } endfor_nexthops(fi);
689#endif
690 return 0;
691}
692
693
694/*
6a31d2a9
ED
695 * Picture
696 * -------
697 *
698 * Semantics of nexthop is very messy by historical reasons.
699 * We have to take into account, that:
700 * a) gateway can be actually local interface address,
701 * so that gatewayed route is direct.
702 * b) gateway must be on-link address, possibly
703 * described not by an ifaddr, but also by a direct route.
704 * c) If both gateway and interface are specified, they should not
705 * contradict.
706 * d) If we use tunnel routes, gateway could be not on-link.
707 *
708 * Attempt to reconcile all of these (alas, self-contradictory) conditions
709 * results in pretty ugly and hairy code with obscure logic.
710 *
711 * I chose to generalized it instead, so that the size
712 * of code does not increase practically, but it becomes
713 * much more general.
714 * Every prefix is assigned a "scope" value: "host" is local address,
715 * "link" is direct route,
716 * [ ... "site" ... "interior" ... ]
717 * and "universe" is true gateway route with global meaning.
718 *
719 * Every prefix refers to a set of "nexthop"s (gw, oif),
720 * where gw must have narrower scope. This recursion stops
721 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
722 * which means that gw is forced to be on link.
723 *
724 * Code is still hairy, but now it is apparently logically
725 * consistent and very flexible. F.e. as by-product it allows
726 * to co-exists in peace independent exterior and interior
727 * routing processes.
728 *
729 * Normally it looks as following.
730 *
731 * {universe prefix} -> (gw, oif) [scope link]
732 * |
733 * |-> {link prefix} -> (gw, oif) [scope local]
734 * |
735 * |-> {local prefix} (terminal node)
1da177e4 736 */
4e902c57 737static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
6d8422a1 738 struct fib_nh *nh, struct netlink_ext_ack *extack)
1da177e4 739{
127eb7cd 740 int err = 0;
86167a37 741 struct net *net;
6a31d2a9 742 struct net_device *dev;
1da177e4 743
86167a37 744 net = cfg->fc_nlinfo.nl_net;
1da177e4
LT
745 if (nh->nh_gw) {
746 struct fib_result res;
747
6a31d2a9 748 if (nh->nh_flags & RTNH_F_ONLINK) {
30bbaa19 749 unsigned int addr_type;
1da177e4 750
c3ab2b4e
DA
751 if (cfg->fc_scope >= RT_SCOPE_LINK) {
752 NL_SET_ERR_MSG(extack,
753 "Nexthop has invalid scope");
1da177e4 754 return -EINVAL;
c3ab2b4e 755 }
6a31d2a9
ED
756 dev = __dev_get_by_index(net, nh->nh_oif);
757 if (!dev)
1da177e4 758 return -ENODEV;
c3ab2b4e
DA
759 if (!(dev->flags & IFF_UP)) {
760 NL_SET_ERR_MSG(extack,
761 "Nexthop device is not up");
1da177e4 762 return -ENETDOWN;
c3ab2b4e 763 }
30bbaa19 764 addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
c3ab2b4e
DA
765 if (addr_type != RTN_UNICAST) {
766 NL_SET_ERR_MSG(extack,
767 "Nexthop has invalid gateway");
30bbaa19 768 return -EINVAL;
c3ab2b4e 769 }
8a3d0316
AG
770 if (!netif_carrier_ok(dev))
771 nh->nh_flags |= RTNH_F_LINKDOWN;
1da177e4
LT
772 nh->nh_dev = dev;
773 dev_hold(dev);
774 nh->nh_scope = RT_SCOPE_LINK;
775 return 0;
776 }
ebc0ffae 777 rcu_read_lock();
1da177e4 778 {
3bfd8472 779 struct fib_table *tbl = NULL;
9ade2286
DM
780 struct flowi4 fl4 = {
781 .daddr = nh->nh_gw,
782 .flowi4_scope = cfg->fc_scope + 1,
783 .flowi4_oif = nh->nh_oif,
6a662719 784 .flowi4_iif = LOOPBACK_IFINDEX,
4e902c57 785 };
1da177e4
LT
786
787 /* It is not necessary, but requires a bit of thinking */
9ade2286
DM
788 if (fl4.flowi4_scope < RT_SCOPE_LINK)
789 fl4.flowi4_scope = RT_SCOPE_LINK;
3bfd8472
DA
790
791 if (cfg->fc_table)
792 tbl = fib_get_table(net, cfg->fc_table);
793
794 if (tbl)
795 err = fib_table_lookup(tbl, &fl4, &res,
1e313678
ED
796 FIB_LOOKUP_IGNORE_LINKSTATE |
797 FIB_LOOKUP_NOREF);
4c9bcd11
DA
798
799 /* on error or if no table given do full lookup. This
800 * is needed for example when nexthops are in the local
801 * table rather than the given table
802 */
803 if (!tbl || err) {
3bfd8472
DA
804 err = fib_lookup(net, &fl4, &res,
805 FIB_LOOKUP_IGNORE_LINKSTATE);
4c9bcd11
DA
806 }
807
ebc0ffae 808 if (err) {
c3ab2b4e
DA
809 NL_SET_ERR_MSG(extack,
810 "Nexthop has invalid gateway");
ebc0ffae 811 rcu_read_unlock();
1da177e4 812 return err;
ebc0ffae 813 }
1da177e4
LT
814 }
815 err = -EINVAL;
c3ab2b4e
DA
816 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
817 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1da177e4 818 goto out;
c3ab2b4e 819 }
1da177e4
LT
820 nh->nh_scope = res.scope;
821 nh->nh_oif = FIB_RES_OIF(res);
6a31d2a9 822 nh->nh_dev = dev = FIB_RES_DEV(res);
c3ab2b4e
DA
823 if (!dev) {
824 NL_SET_ERR_MSG(extack,
825 "No egress device for nexthop gateway");
1da177e4 826 goto out;
c3ab2b4e 827 }
6a31d2a9 828 dev_hold(dev);
8a3d0316
AG
829 if (!netif_carrier_ok(dev))
830 nh->nh_flags |= RTNH_F_LINKDOWN;
8723e1b4 831 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1da177e4
LT
832 } else {
833 struct in_device *in_dev;
834
c3ab2b4e
DA
835 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
836 NL_SET_ERR_MSG(extack,
837 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1da177e4 838 return -EINVAL;
c3ab2b4e 839 }
8723e1b4
ED
840 rcu_read_lock();
841 err = -ENODEV;
86167a37 842 in_dev = inetdev_by_index(net, nh->nh_oif);
51456b29 843 if (!in_dev)
8723e1b4
ED
844 goto out;
845 err = -ENETDOWN;
c3ab2b4e
DA
846 if (!(in_dev->dev->flags & IFF_UP)) {
847 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
8723e1b4 848 goto out;
c3ab2b4e 849 }
1da177e4
LT
850 nh->nh_dev = in_dev->dev;
851 dev_hold(nh->nh_dev);
852 nh->nh_scope = RT_SCOPE_HOST;
8a3d0316
AG
853 if (!netif_carrier_ok(nh->nh_dev))
854 nh->nh_flags |= RTNH_F_LINKDOWN;
8723e1b4 855 err = 0;
1da177e4 856 }
8723e1b4
ED
857out:
858 rcu_read_unlock();
859 return err;
1da177e4
LT
860}
861
81f7bf6c 862static inline unsigned int fib_laddr_hashfn(__be32 val)
1da177e4 863{
123b9731 864 unsigned int mask = (fib_info_hash_size - 1);
1da177e4 865
6a31d2a9
ED
866 return ((__force u32)val ^
867 ((__force u32)val >> 7) ^
868 ((__force u32)val >> 14)) & mask;
1da177e4
LT
869}
870
123b9731 871static struct hlist_head *fib_info_hash_alloc(int bytes)
1da177e4
LT
872{
873 if (bytes <= PAGE_SIZE)
88f83491 874 return kzalloc(bytes, GFP_KERNEL);
1da177e4
LT
875 else
876 return (struct hlist_head *)
6a31d2a9
ED
877 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
878 get_order(bytes));
1da177e4
LT
879}
880
123b9731 881static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1da177e4
LT
882{
883 if (!hash)
884 return;
885
886 if (bytes <= PAGE_SIZE)
887 kfree(hash);
888 else
889 free_pages((unsigned long) hash, get_order(bytes));
890}
891
123b9731
DM
892static void fib_info_hash_move(struct hlist_head *new_info_hash,
893 struct hlist_head *new_laddrhash,
894 unsigned int new_size)
1da177e4 895{
b7656e7f 896 struct hlist_head *old_info_hash, *old_laddrhash;
123b9731 897 unsigned int old_size = fib_info_hash_size;
b7656e7f 898 unsigned int i, bytes;
1da177e4 899
832b4c5e 900 spin_lock_bh(&fib_info_lock);
b7656e7f
DM
901 old_info_hash = fib_info_hash;
902 old_laddrhash = fib_info_laddrhash;
123b9731 903 fib_info_hash_size = new_size;
1da177e4
LT
904
905 for (i = 0; i < old_size; i++) {
906 struct hlist_head *head = &fib_info_hash[i];
b67bfe0d 907 struct hlist_node *n;
1da177e4
LT
908 struct fib_info *fi;
909
b67bfe0d 910 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1da177e4
LT
911 struct hlist_head *dest;
912 unsigned int new_hash;
913
1da177e4
LT
914 new_hash = fib_info_hashfn(fi);
915 dest = &new_info_hash[new_hash];
916 hlist_add_head(&fi->fib_hash, dest);
917 }
918 }
919 fib_info_hash = new_info_hash;
920
921 for (i = 0; i < old_size; i++) {
922 struct hlist_head *lhead = &fib_info_laddrhash[i];
b67bfe0d 923 struct hlist_node *n;
1da177e4
LT
924 struct fib_info *fi;
925
b67bfe0d 926 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1da177e4
LT
927 struct hlist_head *ldest;
928 unsigned int new_hash;
929
1da177e4
LT
930 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
931 ldest = &new_laddrhash[new_hash];
932 hlist_add_head(&fi->fib_lhash, ldest);
933 }
934 }
935 fib_info_laddrhash = new_laddrhash;
936
832b4c5e 937 spin_unlock_bh(&fib_info_lock);
b7656e7f
DM
938
939 bytes = old_size * sizeof(struct hlist_head *);
123b9731
DM
940 fib_info_hash_free(old_info_hash, bytes);
941 fib_info_hash_free(old_laddrhash, bytes);
1da177e4
LT
942}
943
436c3b66
DM
944__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
945{
946 nh->nh_saddr = inet_select_addr(nh->nh_dev,
947 nh->nh_gw,
37e826c5 948 nh->nh_parent->fib_scope);
436c3b66
DM
949 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
950
951 return nh->nh_saddr;
952}
953
021dd3b8
DA
954static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
955{
956 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
957 fib_prefsrc != cfg->fc_dst) {
9b8ff518 958 u32 tb_id = cfg->fc_table;
e1b8d903 959 int rc;
021dd3b8
DA
960
961 if (tb_id == RT_TABLE_MAIN)
962 tb_id = RT_TABLE_LOCAL;
963
e1b8d903
DA
964 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
965 fib_prefsrc, tb_id);
966
967 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
968 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
969 fib_prefsrc, RT_TABLE_LOCAL);
021dd3b8 970 }
e1b8d903
DA
971
972 if (rc != RTN_LOCAL)
973 return false;
021dd3b8
DA
974 }
975 return true;
976}
977
6cf9dfd3
FW
978static int
979fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
980{
c3a8d947 981 bool ecn_ca = false;
6cf9dfd3
FW
982 struct nlattr *nla;
983 int remaining;
984
985 if (!cfg->fc_mx)
986 return 0;
987
988 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
989 int type = nla_type(nla);
990 u32 val;
991
992 if (!type)
993 continue;
994 if (type > RTAX_MAX)
995 return -EINVAL;
996
997 if (type == RTAX_CC_ALGO) {
998 char tmp[TCP_CA_NAME_MAX];
999
1000 nla_strlcpy(tmp, nla, sizeof(tmp));
c3a8d947 1001 val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
6cf9dfd3
FW
1002 if (val == TCP_CA_UNSPEC)
1003 return -EINVAL;
1004 } else {
1005 val = nla_get_u32(nla);
1006 }
1007 if (type == RTAX_ADVMSS && val > 65535 - 40)
1008 val = 65535 - 40;
1009 if (type == RTAX_MTU && val > 65535 - 15)
1010 val = 65535 - 15;
626abd59
PA
1011 if (type == RTAX_HOPLIMIT && val > 255)
1012 val = 255;
b8d3e416
DB
1013 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
1014 return -EINVAL;
3fb07daf 1015 fi->fib_metrics->metrics[type - 1] = val;
6cf9dfd3
FW
1016 }
1017
c3a8d947 1018 if (ecn_ca)
3fb07daf 1019 fi->fib_metrics->metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
c3a8d947 1020
6cf9dfd3
FW
1021 return 0;
1022}
1023
6d8422a1
DA
1024struct fib_info *fib_create_info(struct fib_config *cfg,
1025 struct netlink_ext_ack *extack)
1da177e4
LT
1026{
1027 int err;
1028 struct fib_info *fi = NULL;
1029 struct fib_info *ofi;
1da177e4 1030 int nhs = 1;
7462bd74 1031 struct net *net = cfg->fc_nlinfo.nl_net;
1da177e4 1032
4c8237cd
DM
1033 if (cfg->fc_type > RTN_MAX)
1034 goto err_inval;
1035
1da177e4 1036 /* Fast check to catch the most weird cases */
c3ab2b4e
DA
1037 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1038 NL_SET_ERR_MSG(extack, "Invalid scope");
1da177e4 1039 goto err_inval;
c3ab2b4e 1040 }
1da177e4 1041
c3ab2b4e
DA
1042 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1043 NL_SET_ERR_MSG(extack,
1044 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
80610229 1045 goto err_inval;
c3ab2b4e 1046 }
80610229 1047
1da177e4 1048#ifdef CONFIG_IP_ROUTE_MULTIPATH
4e902c57 1049 if (cfg->fc_mp) {
6d8422a1 1050 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1da177e4
LT
1051 if (nhs == 0)
1052 goto err_inval;
1053 }
1054#endif
1da177e4
LT
1055
1056 err = -ENOBUFS;
123b9731
DM
1057 if (fib_info_cnt >= fib_info_hash_size) {
1058 unsigned int new_size = fib_info_hash_size << 1;
1da177e4
LT
1059 struct hlist_head *new_info_hash;
1060 struct hlist_head *new_laddrhash;
1061 unsigned int bytes;
1062
1063 if (!new_size)
d94ce9b2 1064 new_size = 16;
1da177e4 1065 bytes = new_size * sizeof(struct hlist_head *);
123b9731
DM
1066 new_info_hash = fib_info_hash_alloc(bytes);
1067 new_laddrhash = fib_info_hash_alloc(bytes);
1da177e4 1068 if (!new_info_hash || !new_laddrhash) {
123b9731
DM
1069 fib_info_hash_free(new_info_hash, bytes);
1070 fib_info_hash_free(new_laddrhash, bytes);
88f83491 1071 } else
123b9731 1072 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1da177e4 1073
123b9731 1074 if (!fib_info_hash_size)
1da177e4
LT
1075 goto failure;
1076 }
1077
0da974f4 1078 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
51456b29 1079 if (!fi)
1da177e4 1080 goto failure;
aeefa1ec 1081 fib_info_cnt++;
725d1e1b 1082 if (cfg->fc_mx) {
3fb07daf 1083 fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
725d1e1b
DM
1084 if (!fi->fib_metrics)
1085 goto failure;
3fb07daf 1086 atomic_set(&fi->fib_metrics->refcnt, 1);
725d1e1b 1087 } else
3fb07daf 1088 fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
1da177e4 1089
efd7ef1c 1090 fi->fib_net = net;
4e902c57 1091 fi->fib_protocol = cfg->fc_protocol;
37e826c5 1092 fi->fib_scope = cfg->fc_scope;
4e902c57
TG
1093 fi->fib_flags = cfg->fc_flags;
1094 fi->fib_priority = cfg->fc_priority;
1095 fi->fib_prefsrc = cfg->fc_prefsrc;
f4ef85bb 1096 fi->fib_type = cfg->fc_type;
5a56a0b3 1097 fi->fib_tb_id = cfg->fc_table;
1da177e4
LT
1098
1099 fi->fib_nhs = nhs;
1100 change_nexthops(fi) {
71fceff0 1101 nexthop_nh->nh_parent = fi;
d26b3a7c 1102 nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
f8a17175
JA
1103 if (!nexthop_nh->nh_pcpu_rth_output)
1104 goto failure;
1da177e4
LT
1105 } endfor_nexthops(fi)
1106
6cf9dfd3
FW
1107 err = fib_convert_metrics(fi, cfg);
1108 if (err)
1109 goto failure;
1da177e4 1110
4e902c57 1111 if (cfg->fc_mp) {
1da177e4 1112#ifdef CONFIG_IP_ROUTE_MULTIPATH
6d8422a1 1113 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
4e902c57 1114 if (err != 0)
1da177e4 1115 goto failure;
c3ab2b4e
DA
1116 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
1117 NL_SET_ERR_MSG(extack,
1118 "Nexthop device index does not match RTA_OIF");
1da177e4 1119 goto err_inval;
c3ab2b4e
DA
1120 }
1121 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
1122 NL_SET_ERR_MSG(extack,
1123 "Nexthop gateway does not match RTA_GATEWAY");
1da177e4 1124 goto err_inval;
c3ab2b4e 1125 }
c7066f70 1126#ifdef CONFIG_IP_ROUTE_CLASSID
c3ab2b4e
DA
1127 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
1128 NL_SET_ERR_MSG(extack,
1129 "Nexthop class id does not match RTA_FLOW");
1da177e4 1130 goto err_inval;
c3ab2b4e 1131 }
1da177e4
LT
1132#endif
1133#else
c3ab2b4e
DA
1134 NL_SET_ERR_MSG(extack,
1135 "Multipath support not enabled in kernel");
1da177e4
LT
1136 goto err_inval;
1137#endif
1138 } else {
1139 struct fib_nh *nh = fi->fib_nh;
4e902c57 1140
571e7226
RP
1141 if (cfg->fc_encap) {
1142 struct lwtunnel_state *lwtstate;
571e7226 1143
c3ab2b4e
DA
1144 if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
1145 NL_SET_ERR_MSG(extack,
1146 "LWT encap type not specified");
571e7226 1147 goto err_inval;
c3ab2b4e 1148 }
30357d7d 1149 err = lwtunnel_build_state(cfg->fc_encap_type,
127eb7cd
TH
1150 cfg->fc_encap, AF_INET, cfg,
1151 &lwtstate);
571e7226
RP
1152 if (err)
1153 goto failure;
1154
5a6228a0 1155 nh->nh_lwtstate = lwtstate_get(lwtstate);
571e7226 1156 }
4e902c57
TG
1157 nh->nh_oif = cfg->fc_oif;
1158 nh->nh_gw = cfg->fc_gw;
1159 nh->nh_flags = cfg->fc_flags;
c7066f70 1160#ifdef CONFIG_IP_ROUTE_CLASSID
4e902c57 1161 nh->nh_tclassid = cfg->fc_flow;
7a9bc9b8 1162 if (nh->nh_tclassid)
f4530fa5 1163 fi->fib_net->ipv4.fib_num_tclassid_users++;
1da177e4 1164#endif
1da177e4
LT
1165#ifdef CONFIG_IP_ROUTE_MULTIPATH
1166 nh->nh_weight = 1;
1167#endif
1168 }
1169
4e902c57 1170 if (fib_props[cfg->fc_type].error) {
c3ab2b4e
DA
1171 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1172 NL_SET_ERR_MSG(extack,
1173 "Gateway, device and multipath can not be specified for this route type");
1da177e4 1174 goto err_inval;
c3ab2b4e 1175 }
1da177e4 1176 goto link_it;
4c8237cd
DM
1177 } else {
1178 switch (cfg->fc_type) {
1179 case RTN_UNICAST:
1180 case RTN_LOCAL:
1181 case RTN_BROADCAST:
1182 case RTN_ANYCAST:
1183 case RTN_MULTICAST:
1184 break;
1185 default:
c3ab2b4e 1186 NL_SET_ERR_MSG(extack, "Invalid route type");
4c8237cd
DM
1187 goto err_inval;
1188 }
1da177e4
LT
1189 }
1190
c3ab2b4e
DA
1191 if (cfg->fc_scope > RT_SCOPE_HOST) {
1192 NL_SET_ERR_MSG(extack, "Invalid scope");
1da177e4 1193 goto err_inval;
c3ab2b4e 1194 }
1da177e4 1195
4e902c57 1196 if (cfg->fc_scope == RT_SCOPE_HOST) {
1da177e4
LT
1197 struct fib_nh *nh = fi->fib_nh;
1198
1199 /* Local address is added. */
c3ab2b4e
DA
1200 if (nhs != 1) {
1201 NL_SET_ERR_MSG(extack,
1202 "Route with host scope can not have multiple nexthops");
6d8422a1 1203 goto err_inval;
c3ab2b4e
DA
1204 }
1205 if (nh->nh_gw) {
1206 NL_SET_ERR_MSG(extack,
1207 "Route with host scope can not have a gateway");
1da177e4 1208 goto err_inval;
c3ab2b4e 1209 }
1da177e4 1210 nh->nh_scope = RT_SCOPE_NOWHERE;
7462bd74 1211 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
1da177e4 1212 err = -ENODEV;
51456b29 1213 if (!nh->nh_dev)
1da177e4
LT
1214 goto failure;
1215 } else {
8a3d0316
AG
1216 int linkdown = 0;
1217
1da177e4 1218 change_nexthops(fi) {
6d8422a1 1219 err = fib_check_nh(cfg, fi, nexthop_nh, extack);
6a31d2a9 1220 if (err != 0)
1da177e4 1221 goto failure;
8a3d0316
AG
1222 if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
1223 linkdown++;
1da177e4 1224 } endfor_nexthops(fi)
8a3d0316
AG
1225 if (linkdown == fi->fib_nhs)
1226 fi->fib_flags |= RTNH_F_LINKDOWN;
1da177e4
LT
1227 }
1228
c3ab2b4e
DA
1229 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1230 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
021dd3b8 1231 goto err_inval;
c3ab2b4e 1232 }
1da177e4 1233
1fc050a1 1234 change_nexthops(fi) {
436c3b66 1235 fib_info_update_nh_saddr(net, nexthop_nh);
0e884c78 1236 fib_add_weight(fi, nexthop_nh);
1fc050a1
DM
1237 } endfor_nexthops(fi)
1238
0e884c78
PN
1239 fib_rebalance(fi);
1240
1da177e4 1241link_it:
6a31d2a9
ED
1242 ofi = fib_find_info(fi);
1243 if (ofi) {
1da177e4
LT
1244 fi->fib_dead = 1;
1245 free_fib_info(fi);
1246 ofi->fib_treeref++;
1247 return ofi;
1248 }
1249
1250 fi->fib_treeref++;
1251 atomic_inc(&fi->fib_clntref);
832b4c5e 1252 spin_lock_bh(&fib_info_lock);
1da177e4
LT
1253 hlist_add_head(&fi->fib_hash,
1254 &fib_info_hash[fib_info_hashfn(fi)]);
1255 if (fi->fib_prefsrc) {
1256 struct hlist_head *head;
1257
1258 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1259 hlist_add_head(&fi->fib_lhash, head);
1260 }
1261 change_nexthops(fi) {
1262 struct hlist_head *head;
1263 unsigned int hash;
1264
71fceff0 1265 if (!nexthop_nh->nh_dev)
1da177e4 1266 continue;
71fceff0 1267 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
1da177e4 1268 head = &fib_info_devhash[hash];
71fceff0 1269 hlist_add_head(&nexthop_nh->nh_hash, head);
1da177e4 1270 } endfor_nexthops(fi)
832b4c5e 1271 spin_unlock_bh(&fib_info_lock);
1da177e4
LT
1272 return fi;
1273
1274err_inval:
1275 err = -EINVAL;
1276
1277failure:
e905a9ed 1278 if (fi) {
1da177e4
LT
1279 fi->fib_dead = 1;
1280 free_fib_info(fi);
1281 }
4e902c57
TG
1282
1283 return ERR_PTR(err);
1da177e4
LT
1284}
1285
15e47304 1286int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
37e826c5 1287 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
be403ea1 1288 struct fib_info *fi, unsigned int flags)
1da177e4 1289{
be403ea1 1290 struct nlmsghdr *nlh;
1da177e4 1291 struct rtmsg *rtm;
1da177e4 1292
15e47304 1293 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
51456b29 1294 if (!nlh)
26932566 1295 return -EMSGSIZE;
be403ea1
TG
1296
1297 rtm = nlmsg_data(nlh);
1da177e4
LT
1298 rtm->rtm_family = AF_INET;
1299 rtm->rtm_dst_len = dst_len;
1300 rtm->rtm_src_len = 0;
1301 rtm->rtm_tos = tos;
709772e6
KPO
1302 if (tb_id < 256)
1303 rtm->rtm_table = tb_id;
1304 else
1305 rtm->rtm_table = RT_TABLE_COMPAT;
f3756b79
DM
1306 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1307 goto nla_put_failure;
1da177e4
LT
1308 rtm->rtm_type = type;
1309 rtm->rtm_flags = fi->fib_flags;
37e826c5 1310 rtm->rtm_scope = fi->fib_scope;
1da177e4 1311 rtm->rtm_protocol = fi->fib_protocol;
be403ea1 1312
f3756b79 1313 if (rtm->rtm_dst_len &&
930345ea 1314 nla_put_in_addr(skb, RTA_DST, dst))
f3756b79
DM
1315 goto nla_put_failure;
1316 if (fi->fib_priority &&
1317 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1318 goto nla_put_failure;
3fb07daf 1319 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
be403ea1
TG
1320 goto nla_put_failure;
1321
f3756b79 1322 if (fi->fib_prefsrc &&
930345ea 1323 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
f3756b79 1324 goto nla_put_failure;
1da177e4 1325 if (fi->fib_nhs == 1) {
0eeb075f
AG
1326 struct in_device *in_dev;
1327
f3756b79 1328 if (fi->fib_nh->nh_gw &&
930345ea 1329 nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
f3756b79
DM
1330 goto nla_put_failure;
1331 if (fi->fib_nh->nh_oif &&
1332 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1333 goto nla_put_failure;
0eeb075f 1334 if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
96ac5cc9 1335 in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
0eeb075f
AG
1336 if (in_dev &&
1337 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1338 rtm->rtm_flags |= RTNH_F_DEAD;
1339 }
c7066f70 1340#ifdef CONFIG_IP_ROUTE_CLASSID
f3756b79
DM
1341 if (fi->fib_nh[0].nh_tclassid &&
1342 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1343 goto nla_put_failure;
8265abc0 1344#endif
ea7a8085
DA
1345 if (fi->fib_nh->nh_lwtstate &&
1346 lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
1347 goto nla_put_failure;
1da177e4
LT
1348 }
1349#ifdef CONFIG_IP_ROUTE_MULTIPATH
1350 if (fi->fib_nhs > 1) {
be403ea1
TG
1351 struct rtnexthop *rtnh;
1352 struct nlattr *mp;
1353
1354 mp = nla_nest_start(skb, RTA_MULTIPATH);
51456b29 1355 if (!mp)
be403ea1 1356 goto nla_put_failure;
1da177e4
LT
1357
1358 for_nexthops(fi) {
0eeb075f
AG
1359 struct in_device *in_dev;
1360
be403ea1 1361 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
51456b29 1362 if (!rtnh)
be403ea1
TG
1363 goto nla_put_failure;
1364
1365 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
0eeb075f 1366 if (nh->nh_flags & RTNH_F_LINKDOWN) {
96ac5cc9 1367 in_dev = __in_dev_get_rtnl(nh->nh_dev);
0eeb075f
AG
1368 if (in_dev &&
1369 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1370 rtnh->rtnh_flags |= RTNH_F_DEAD;
1371 }
be403ea1
TG
1372 rtnh->rtnh_hops = nh->nh_weight - 1;
1373 rtnh->rtnh_ifindex = nh->nh_oif;
1374
f3756b79 1375 if (nh->nh_gw &&
930345ea 1376 nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
f3756b79 1377 goto nla_put_failure;
c7066f70 1378#ifdef CONFIG_IP_ROUTE_CLASSID
f3756b79
DM
1379 if (nh->nh_tclassid &&
1380 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1381 goto nla_put_failure;
8265abc0 1382#endif
ea7a8085
DA
1383 if (nh->nh_lwtstate &&
1384 lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
1385 goto nla_put_failure;
1386
be403ea1
TG
1387 /* length of rtnetlink header + attributes */
1388 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
1da177e4 1389 } endfor_nexthops(fi);
be403ea1
TG
1390
1391 nla_nest_end(skb, mp);
1da177e4
LT
1392 }
1393#endif
053c095a
JB
1394 nlmsg_end(skb, nlh);
1395 return 0;
1da177e4 1396
be403ea1 1397nla_put_failure:
26932566
PM
1398 nlmsg_cancel(skb, nlh);
1399 return -EMSGSIZE;
1da177e4
LT
1400}
1401
1da177e4 1402/*
6a31d2a9
ED
1403 * Update FIB if:
1404 * - local address disappeared -> we must delete all the entries
1405 * referring to it.
1406 * - device went down -> we must shutdown all nexthops going via it.
1da177e4 1407 */
5a56a0b3 1408int fib_sync_down_addr(struct net_device *dev, __be32 local)
1da177e4
LT
1409{
1410 int ret = 0;
85326fa5
DL
1411 unsigned int hash = fib_laddr_hashfn(local);
1412 struct hlist_head *head = &fib_info_laddrhash[hash];
5a56a0b3
MT
1413 struct net *net = dev_net(dev);
1414 int tb_id = l3mdev_fib_table(dev);
85326fa5 1415 struct fib_info *fi;
1da177e4 1416
51456b29 1417 if (!fib_info_laddrhash || local == 0)
85326fa5 1418 return 0;
1da177e4 1419
b67bfe0d 1420 hlist_for_each_entry(fi, head, fib_lhash) {
5a56a0b3
MT
1421 if (!net_eq(fi->fib_net, net) ||
1422 fi->fib_tb_id != tb_id)
4814bdbd 1423 continue;
85326fa5
DL
1424 if (fi->fib_prefsrc == local) {
1425 fi->fib_flags |= RTNH_F_DEAD;
1426 ret++;
1da177e4
LT
1427 }
1428 }
85326fa5
DL
1429 return ret;
1430}
1431
982acb97
IS
1432static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
1433 enum fib_event_type event_type)
1434{
1435 struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
1436 struct fib_nh_notifier_info info = {
1437 .fib_nh = fib_nh,
1438 };
1439
1440 switch (event_type) {
1441 case FIB_EVENT_NH_ADD:
1442 if (fib_nh->nh_flags & RTNH_F_DEAD)
1443 break;
1444 if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1445 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1446 break;
1447 return call_fib_notifiers(dev_net(fib_nh->nh_dev), event_type,
1448 &info.info);
1449 case FIB_EVENT_NH_DEL:
1450 if ((IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1451 fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
1452 (fib_nh->nh_flags & RTNH_F_DEAD))
1453 return call_fib_notifiers(dev_net(fib_nh->nh_dev),
1454 event_type, &info.info);
1455 default:
1456 break;
1457 }
1458
1459 return NOTIFY_DONE;
1460}
1461
4f823def
JA
1462/* Event force Flags Description
1463 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1464 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1465 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1466 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1467 */
1468int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
85326fa5
DL
1469{
1470 int ret = 0;
1471 int scope = RT_SCOPE_NOWHERE;
1472 struct fib_info *prev_fi = NULL;
1473 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1474 struct hlist_head *head = &fib_info_devhash[hash];
85326fa5 1475 struct fib_nh *nh;
1da177e4 1476
4f823def 1477 if (force)
85326fa5 1478 scope = -1;
1da177e4 1479
b67bfe0d 1480 hlist_for_each_entry(nh, head, nh_hash) {
85326fa5
DL
1481 struct fib_info *fi = nh->nh_parent;
1482 int dead;
1da177e4 1483
85326fa5
DL
1484 BUG_ON(!fi->fib_nhs);
1485 if (nh->nh_dev != dev || fi == prev_fi)
1486 continue;
1487 prev_fi = fi;
1488 dead = 0;
1489 change_nexthops(fi) {
6a31d2a9 1490 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
85326fa5 1491 dead++;
71fceff0
DM
1492 else if (nexthop_nh->nh_dev == dev &&
1493 nexthop_nh->nh_scope != scope) {
8a3d0316
AG
1494 switch (event) {
1495 case NETDEV_DOWN:
1496 case NETDEV_UNREGISTER:
1497 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1498 /* fall through */
1499 case NETDEV_CHANGE:
1500 nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
1501 break;
1502 }
982acb97
IS
1503 call_fib_nh_notifiers(nexthop_nh,
1504 FIB_EVENT_NH_DEL);
85326fa5
DL
1505 dead++;
1506 }
1da177e4 1507#ifdef CONFIG_IP_ROUTE_MULTIPATH
8a3d0316
AG
1508 if (event == NETDEV_UNREGISTER &&
1509 nexthop_nh->nh_dev == dev) {
85326fa5
DL
1510 dead = fi->fib_nhs;
1511 break;
1da177e4 1512 }
85326fa5
DL
1513#endif
1514 } endfor_nexthops(fi)
1515 if (dead == fi->fib_nhs) {
8a3d0316
AG
1516 switch (event) {
1517 case NETDEV_DOWN:
1518 case NETDEV_UNREGISTER:
1519 fi->fib_flags |= RTNH_F_DEAD;
1520 /* fall through */
1521 case NETDEV_CHANGE:
1522 fi->fib_flags |= RTNH_F_LINKDOWN;
1523 break;
1524 }
85326fa5 1525 ret++;
1da177e4 1526 }
0e884c78
PN
1527
1528 fib_rebalance(fi);
1da177e4
LT
1529 }
1530
1531 return ret;
1532}
1533
0c838ff1 1534/* Must be invoked inside of an RCU protected region. */
c7b371e3 1535static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
0c838ff1
DM
1536{
1537 struct fib_info *fi = NULL, *last_resort = NULL;
56315f9e 1538 struct hlist_head *fa_head = res->fa_head;
0c838ff1 1539 struct fib_table *tb = res->table;
18a912e9 1540 u8 slen = 32 - res->prefixlen;
0c838ff1 1541 int order = -1, last_idx = -1;
2392debc
JA
1542 struct fib_alias *fa, *fa1 = NULL;
1543 u32 last_prio = res->fi->fib_priority;
1544 u8 last_tos = 0;
0c838ff1 1545
56315f9e 1546 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
0c838ff1
DM
1547 struct fib_info *next_fi = fa->fa_info;
1548
18a912e9
JA
1549 if (fa->fa_slen != slen)
1550 continue;
2392debc
JA
1551 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1552 continue;
18a912e9
JA
1553 if (fa->tb_id != tb->tb_id)
1554 continue;
2392debc
JA
1555 if (next_fi->fib_priority > last_prio &&
1556 fa->fa_tos == last_tos) {
1557 if (last_tos)
1558 continue;
1559 break;
1560 }
1561 if (next_fi->fib_flags & RTNH_F_DEAD)
1562 continue;
1563 last_tos = fa->fa_tos;
1564 last_prio = next_fi->fib_priority;
1565
37e826c5 1566 if (next_fi->fib_scope != res->scope ||
0c838ff1
DM
1567 fa->fa_type != RTN_UNICAST)
1568 continue;
0c838ff1
DM
1569 if (!next_fi->fib_nh[0].nh_gw ||
1570 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1571 continue;
1572
1573 fib_alias_accessed(fa);
1574
51456b29 1575 if (!fi) {
0c838ff1
DM
1576 if (next_fi != res->fi)
1577 break;
2392debc 1578 fa1 = fa;
0c838ff1 1579 } else if (!fib_detect_death(fi, order, &last_resort,
2392debc 1580 &last_idx, fa1->fa_default)) {
0c838ff1 1581 fib_result_assign(res, fi);
2392debc 1582 fa1->fa_default = order;
0c838ff1
DM
1583 goto out;
1584 }
1585 fi = next_fi;
1586 order++;
1587 }
1588
51456b29 1589 if (order <= 0 || !fi) {
2392debc
JA
1590 if (fa1)
1591 fa1->fa_default = -1;
0c838ff1
DM
1592 goto out;
1593 }
1594
1595 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2392debc 1596 fa1->fa_default)) {
0c838ff1 1597 fib_result_assign(res, fi);
2392debc 1598 fa1->fa_default = order;
0c838ff1
DM
1599 goto out;
1600 }
1601
1602 if (last_idx >= 0)
1603 fib_result_assign(res, last_resort);
2392debc 1604 fa1->fa_default = last_idx;
0c838ff1 1605out:
31d40937 1606 return;
0c838ff1
DM
1607}
1608
1da177e4 1609/*
6a31d2a9
ED
1610 * Dead device goes up. We wake up dead nexthops.
1611 * It takes sense only on multipath routes.
1da177e4 1612 */
8a3d0316 1613int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
1da177e4
LT
1614{
1615 struct fib_info *prev_fi;
1616 unsigned int hash;
1617 struct hlist_head *head;
1da177e4
LT
1618 struct fib_nh *nh;
1619 int ret;
1620
6a31d2a9 1621 if (!(dev->flags & IFF_UP))
1da177e4
LT
1622 return 0;
1623
c9b3292e
JA
1624 if (nh_flags & RTNH_F_DEAD) {
1625 unsigned int flags = dev_get_flags(dev);
1626
1627 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1628 nh_flags |= RTNH_F_LINKDOWN;
1629 }
1630
1da177e4
LT
1631 prev_fi = NULL;
1632 hash = fib_devindex_hashfn(dev->ifindex);
1633 head = &fib_info_devhash[hash];
1634 ret = 0;
1635
b67bfe0d 1636 hlist_for_each_entry(nh, head, nh_hash) {
1da177e4
LT
1637 struct fib_info *fi = nh->nh_parent;
1638 int alive;
1639
1640 BUG_ON(!fi->fib_nhs);
1641 if (nh->nh_dev != dev || fi == prev_fi)
1642 continue;
1643
1644 prev_fi = fi;
1645 alive = 0;
1646 change_nexthops(fi) {
8a3d0316 1647 if (!(nexthop_nh->nh_flags & nh_flags)) {
1da177e4
LT
1648 alive++;
1649 continue;
1650 }
51456b29 1651 if (!nexthop_nh->nh_dev ||
6a31d2a9 1652 !(nexthop_nh->nh_dev->flags & IFF_UP))
1da177e4 1653 continue;
71fceff0
DM
1654 if (nexthop_nh->nh_dev != dev ||
1655 !__in_dev_get_rtnl(dev))
1da177e4
LT
1656 continue;
1657 alive++;
8a3d0316 1658 nexthop_nh->nh_flags &= ~nh_flags;
982acb97 1659 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
1da177e4
LT
1660 } endfor_nexthops(fi)
1661
1662 if (alive > 0) {
8a3d0316 1663 fi->fib_flags &= ~nh_flags;
1da177e4
LT
1664 ret++;
1665 }
0e884c78
PN
1666
1667 fib_rebalance(fi);
1da177e4
LT
1668 }
1669
1670 return ret;
1671}
1672
8a3d0316 1673#ifdef CONFIG_IP_ROUTE_MULTIPATH
a6db4494
DA
1674static bool fib_good_nh(const struct fib_nh *nh)
1675{
1676 int state = NUD_REACHABLE;
1677
1678 if (nh->nh_scope == RT_SCOPE_LINK) {
1679 struct neighbour *n;
1680
1681 rcu_read_lock_bh();
1682
d985d151
ED
1683 n = __ipv4_neigh_lookup_noref(nh->nh_dev,
1684 (__force u32)nh->nh_gw);
a6db4494
DA
1685 if (n)
1686 state = n->nud_state;
1687
1688 rcu_read_unlock_bh();
1689 }
1690
1691 return !!(state & NUD_VALID);
1692}
8a3d0316 1693
0e884c78 1694void fib_select_multipath(struct fib_result *res, int hash)
1da177e4
LT
1695{
1696 struct fib_info *fi = res->fi;
a6db4494
DA
1697 struct net *net = fi->fib_net;
1698 bool first = false;
1da177e4 1699
0e884c78
PN
1700 for_nexthops(fi) {
1701 if (hash > atomic_read(&nh->nh_upper_bound))
1702 continue;
1da177e4 1703
a6db4494
DA
1704 if (!net->ipv4.sysctl_fib_multipath_use_neigh ||
1705 fib_good_nh(nh)) {
1706 res->nh_sel = nhsel;
1707 return;
1708 }
1709 if (!first) {
1710 res->nh_sel = nhsel;
1711 first = true;
1712 }
1da177e4 1713 } endfor_nexthops(fi);
1da177e4
LT
1714}
1715#endif
3ce58d84
DA
1716
1717void fib_select_path(struct net *net, struct fib_result *res,
bf4e0a3d 1718 struct flowi4 *fl4, const struct sk_buff *skb)
3ce58d84 1719{
7a18c5b9
DA
1720 bool oif_check;
1721
1722 oif_check = (fl4->flowi4_oif == 0 ||
1723 fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF);
1724
3ce58d84 1725#ifdef CONFIG_IP_ROUTE_MULTIPATH
7a18c5b9 1726 if (res->fi->fib_nhs > 1 && oif_check) {
bf4e0a3d 1727 int h = fib_multipath_hash(res->fi, fl4, skb);
9920e48b 1728
bf4e0a3d 1729 fib_select_multipath(res, h);
3ce58d84
DA
1730 }
1731 else
1732#endif
1733 if (!res->prefixlen &&
1734 res->table->tb_num_default > 1 &&
7a18c5b9 1735 res->type == RTN_UNICAST && oif_check)
3ce58d84
DA
1736 fib_select_default(fl4, res);
1737
1738 if (!fl4->saddr)
1739 fl4->saddr = FIB_RES_PREFSRC(net, *res);
1740}
1741EXPORT_SYMBOL_GPL(fib_select_path);