blob: fc7e8c4fc8eea75761936536cc1586b6800d9477 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * IPv4 Forwarding Information Base: semantics.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12#include <linux/uaccess.h>
13#include <linux/bitops.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/jiffies.h>
17#include <linux/mm.h>
18#include <linux/string.h>
19#include <linux/socket.h>
20#include <linux/sockios.h>
21#include <linux/errno.h>
22#include <linux/in.h>
23#include <linux/inet.h>
24#include <linux/inetdevice.h>
25#include <linux/netdevice.h>
26#include <linux/if_arp.h>
27#include <linux/proc_fs.h>
28#include <linux/skbuff.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/netlink.h>
32#include <linux/hash.h>
33#include <linux/nospec.h>
34
35#include <net/arp.h>
36#include <net/ip.h>
37#include <net/protocol.h>
38#include <net/route.h>
39#include <net/tcp.h>
40#include <net/sock.h>
41#include <net/ip_fib.h>
42#include <net/ip6_fib.h>
43#include <net/nexthop.h>
44#include <net/netlink.h>
45#include <net/rtnh.h>
46#include <net/lwtunnel.h>
47#include <net/fib_notifier.h>
48#include <net/addrconf.h>
49
50#include "fib_lookup.h"
51
52static DEFINE_SPINLOCK(fib_info_lock);
53static struct hlist_head *fib_info_hash;
54static struct hlist_head *fib_info_laddrhash;
55static unsigned int fib_info_hash_size;
56static unsigned int fib_info_cnt;
57
58#define DEVINDEX_HASHBITS 8
59#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
60static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
61
62/* for_nexthops and change_nexthops only used when nexthop object
63 * is not set in a fib_info. The logic within can reference fib_nh.
64 */
65#ifdef CONFIG_IP_ROUTE_MULTIPATH
66
67#define for_nexthops(fi) { \
68 int nhsel; const struct fib_nh *nh; \
69 for (nhsel = 0, nh = (fi)->fib_nh; \
70 nhsel < fib_info_num_path((fi)); \
71 nh++, nhsel++)
72
73#define change_nexthops(fi) { \
74 int nhsel; struct fib_nh *nexthop_nh; \
75 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
76 nhsel < fib_info_num_path((fi)); \
77 nexthop_nh++, nhsel++)
78
79#else /* CONFIG_IP_ROUTE_MULTIPATH */
80
81/* Hope, that gcc will optimize it to get rid of dummy loop */
82
83#define for_nexthops(fi) { \
84 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
85 for (nhsel = 0; nhsel < 1; nhsel++)
86
87#define change_nexthops(fi) { \
88 int nhsel; \
89 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
90 for (nhsel = 0; nhsel < 1; nhsel++)
91
92#endif /* CONFIG_IP_ROUTE_MULTIPATH */
93
94#define endfor_nexthops(fi) }
95
96
97const struct fib_prop fib_props[RTN_MAX + 1] = {
98 [RTN_UNSPEC] = {
99 .error = 0,
100 .scope = RT_SCOPE_NOWHERE,
101 },
102 [RTN_UNICAST] = {
103 .error = 0,
104 .scope = RT_SCOPE_UNIVERSE,
105 },
106 [RTN_LOCAL] = {
107 .error = 0,
108 .scope = RT_SCOPE_HOST,
109 },
110 [RTN_BROADCAST] = {
111 .error = 0,
112 .scope = RT_SCOPE_LINK,
113 },
114 [RTN_ANYCAST] = {
115 .error = 0,
116 .scope = RT_SCOPE_LINK,
117 },
118 [RTN_MULTICAST] = {
119 .error = 0,
120 .scope = RT_SCOPE_UNIVERSE,
121 },
122 [RTN_BLACKHOLE] = {
123 .error = -EINVAL,
124 .scope = RT_SCOPE_UNIVERSE,
125 },
126 [RTN_UNREACHABLE] = {
127 .error = -EHOSTUNREACH,
128 .scope = RT_SCOPE_UNIVERSE,
129 },
130 [RTN_PROHIBIT] = {
131 .error = -EACCES,
132 .scope = RT_SCOPE_UNIVERSE,
133 },
134 [RTN_THROW] = {
135 .error = -EAGAIN,
136 .scope = RT_SCOPE_UNIVERSE,
137 },
138 [RTN_NAT] = {
139 .error = -EINVAL,
140 .scope = RT_SCOPE_NOWHERE,
141 },
142 [RTN_XRESOLVE] = {
143 .error = -EINVAL,
144 .scope = RT_SCOPE_NOWHERE,
145 },
146 [RTN_POLICY_FAILED] = {
147 .error = -EACCES,
148 .scope = RT_SCOPE_UNIVERSE,
149 },
150};
151
152static void rt_fibinfo_free(struct rtable __rcu **rtp)
153{
154 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
155
156 if (!rt)
157 return;
158
159 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
160 * because we waited an RCU grace period before calling
161 * free_fib_info_rcu()
162 */
163
164 dst_dev_put(&rt->dst);
165 dst_release_immediate(&rt->dst);
166}
167
168static void free_nh_exceptions(struct fib_nh_common *nhc)
169{
170 struct fnhe_hash_bucket *hash;
171 int i;
172
173 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
174 if (!hash)
175 return;
176 for (i = 0; i < FNHE_HASH_SIZE; i++) {
177 struct fib_nh_exception *fnhe;
178
179 fnhe = rcu_dereference_protected(hash[i].chain, 1);
180 while (fnhe) {
181 struct fib_nh_exception *next;
182
183 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
184
185 rt_fibinfo_free(&fnhe->fnhe_rth_input);
186 rt_fibinfo_free(&fnhe->fnhe_rth_output);
187
188 kfree(fnhe);
189
190 fnhe = next;
191 }
192 }
193 kfree(hash);
194}
195
196static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
197{
198 int cpu;
199
200 if (!rtp)
201 return;
202
203 for_each_possible_cpu(cpu) {
204 struct rtable *rt;
205
206 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
207 if (rt) {
208 dst_dev_put(&rt->dst);
209 dst_release_immediate(&rt->dst);
210 }
211 }
212 free_percpu(rtp);
213}
214
215void fib_nh_common_release(struct fib_nh_common *nhc)
216{
217 if (nhc->nhc_dev)
218 dev_put(nhc->nhc_dev);
219
220 lwtstate_put(nhc->nhc_lwtstate);
221 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
222 rt_fibinfo_free(&nhc->nhc_rth_input);
223 free_nh_exceptions(nhc);
224}
225EXPORT_SYMBOL_GPL(fib_nh_common_release);
226
227void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
228{
229#ifdef CONFIG_IP_ROUTE_CLASSID
230 if (fib_nh->nh_tclassid)
231 atomic_dec(&net->ipv4.fib_num_tclassid_users);
232#endif
233 fib_nh_common_release(&fib_nh->nh_common);
234}
235
236/* Release a nexthop info record */
237static void free_fib_info_rcu(struct rcu_head *head)
238{
239 struct fib_info *fi = container_of(head, struct fib_info, rcu);
240
241 if (fi->nh) {
242 nexthop_put(fi->nh);
243 } else {
244 change_nexthops(fi) {
245 fib_nh_release(fi->fib_net, nexthop_nh);
246 } endfor_nexthops(fi);
247 }
248
249 ip_fib_metrics_put(fi->fib_metrics);
250
251 kfree(fi);
252}
253
254void free_fib_info(struct fib_info *fi)
255{
256 if (fi->fib_dead == 0) {
257 pr_warn("Freeing alive fib_info %p\n", fi);
258 return;
259 }
260 fib_info_cnt--;
261
262 call_rcu(&fi->rcu, free_fib_info_rcu);
263}
264EXPORT_SYMBOL_GPL(free_fib_info);
265
266void fib_release_info(struct fib_info *fi)
267{
268 spin_lock_bh(&fib_info_lock);
269 if (fi && --fi->fib_treeref == 0) {
270 hlist_del(&fi->fib_hash);
271 if (fi->fib_prefsrc)
272 hlist_del(&fi->fib_lhash);
273 if (fi->nh) {
274 list_del(&fi->nh_list);
275 } else {
276 change_nexthops(fi) {
277 if (!nexthop_nh->fib_nh_dev)
278 continue;
279 hlist_del(&nexthop_nh->nh_hash);
280 } endfor_nexthops(fi)
281 }
282 /* Paired with READ_ONCE() from fib_table_lookup() */
283 WRITE_ONCE(fi->fib_dead, 1);
284 fib_info_put(fi);
285 }
286 spin_unlock_bh(&fib_info_lock);
287}
288
289static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
290{
291 const struct fib_nh *onh;
292
293 if (fi->nh || ofi->nh)
294 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
295
296 if (ofi->fib_nhs == 0)
297 return 0;
298
299 for_nexthops(fi) {
300 onh = fib_info_nh(ofi, nhsel);
301
302 if (nh->fib_nh_oif != onh->fib_nh_oif ||
303 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
304 nh->fib_nh_scope != onh->fib_nh_scope ||
305#ifdef CONFIG_IP_ROUTE_MULTIPATH
306 nh->fib_nh_weight != onh->fib_nh_weight ||
307#endif
308#ifdef CONFIG_IP_ROUTE_CLASSID
309 nh->nh_tclassid != onh->nh_tclassid ||
310#endif
311 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
312 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
313 return -1;
314
315 if (nh->fib_nh_gw_family == AF_INET &&
316 nh->fib_nh_gw4 != onh->fib_nh_gw4)
317 return -1;
318
319 if (nh->fib_nh_gw_family == AF_INET6 &&
320 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
321 return -1;
322 } endfor_nexthops(fi);
323 return 0;
324}
325
326static inline unsigned int fib_devindex_hashfn(unsigned int val)
327{
328 return hash_32(val, DEVINDEX_HASHBITS);
329}
330
331static struct hlist_head *
332fib_info_devhash_bucket(const struct net_device *dev)
333{
334 u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
335
336 return &fib_info_devhash[fib_devindex_hashfn(val)];
337}
338
339static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
340 u32 prefsrc, u32 priority)
341{
342 unsigned int val = init_val;
343
344 val ^= (protocol << 8) | scope;
345 val ^= prefsrc;
346 val ^= priority;
347
348 return val;
349}
350
351static unsigned int fib_info_hashfn_result(unsigned int val)
352{
353 unsigned int mask = (fib_info_hash_size - 1);
354
355 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
356}
357
358static inline unsigned int fib_info_hashfn(struct fib_info *fi)
359{
360 unsigned int val;
361
362 val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
363 fi->fib_scope, (__force u32)fi->fib_prefsrc,
364 fi->fib_priority);
365
366 if (fi->nh) {
367 val ^= fib_devindex_hashfn(fi->nh->id);
368 } else {
369 for_nexthops(fi) {
370 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
371 } endfor_nexthops(fi)
372 }
373
374 return fib_info_hashfn_result(val);
375}
376
377/* no metrics, only nexthop id */
378static struct fib_info *fib_find_info_nh(struct net *net,
379 const struct fib_config *cfg)
380{
381 struct hlist_head *head;
382 struct fib_info *fi;
383 unsigned int hash;
384
385 hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
386 cfg->fc_protocol, cfg->fc_scope,
387 (__force u32)cfg->fc_prefsrc,
388 cfg->fc_priority);
389 hash = fib_info_hashfn_result(hash);
390 head = &fib_info_hash[hash];
391
392 hlist_for_each_entry(fi, head, fib_hash) {
393 if (!net_eq(fi->fib_net, net))
394 continue;
395 if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
396 continue;
397 if (cfg->fc_protocol == fi->fib_protocol &&
398 cfg->fc_scope == fi->fib_scope &&
399 cfg->fc_prefsrc == fi->fib_prefsrc &&
400 cfg->fc_priority == fi->fib_priority &&
401 cfg->fc_type == fi->fib_type &&
402 cfg->fc_table == fi->fib_tb_id &&
403 !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
404 return fi;
405 }
406
407 return NULL;
408}
409
410static struct fib_info *fib_find_info(struct fib_info *nfi)
411{
412 struct hlist_head *head;
413 struct fib_info *fi;
414 unsigned int hash;
415
416 hash = fib_info_hashfn(nfi);
417 head = &fib_info_hash[hash];
418
419 hlist_for_each_entry(fi, head, fib_hash) {
420 if (!net_eq(fi->fib_net, nfi->fib_net))
421 continue;
422 if (fi->fib_nhs != nfi->fib_nhs)
423 continue;
424 if (nfi->fib_protocol == fi->fib_protocol &&
425 nfi->fib_scope == fi->fib_scope &&
426 nfi->fib_prefsrc == fi->fib_prefsrc &&
427 nfi->fib_priority == fi->fib_priority &&
428 nfi->fib_type == fi->fib_type &&
429 nfi->fib_tb_id == fi->fib_tb_id &&
430 memcmp(nfi->fib_metrics, fi->fib_metrics,
431 sizeof(u32) * RTAX_MAX) == 0 &&
432 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
433 nh_comp(fi, nfi) == 0)
434 return fi;
435 }
436
437 return NULL;
438}
439
440/* Check, that the gateway is already configured.
441 * Used only by redirect accept routine.
442 */
443int ip_fib_check_default(__be32 gw, struct net_device *dev)
444{
445 struct hlist_head *head;
446 struct fib_nh *nh;
447
448 spin_lock(&fib_info_lock);
449
450 head = fib_info_devhash_bucket(dev);
451
452 hlist_for_each_entry(nh, head, nh_hash) {
453 if (nh->fib_nh_dev == dev &&
454 nh->fib_nh_gw4 == gw &&
455 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
456 spin_unlock(&fib_info_lock);
457 return 0;
458 }
459 }
460
461 spin_unlock(&fib_info_lock);
462
463 return -1;
464}
465
466static inline size_t fib_nlmsg_size(struct fib_info *fi)
467{
468 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
469 + nla_total_size(4) /* RTA_TABLE */
470 + nla_total_size(4) /* RTA_DST */
471 + nla_total_size(4) /* RTA_PRIORITY */
472 + nla_total_size(4) /* RTA_PREFSRC */
473 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
474 unsigned int nhs = fib_info_num_path(fi);
475
476 /* space for nested metrics */
477 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
478
479 if (fi->nh)
480 payload += nla_total_size(4); /* RTA_NH_ID */
481
482 if (nhs) {
483 size_t nh_encapsize = 0;
484 /* Also handles the special case nhs == 1 */
485
486 /* each nexthop is packed in an attribute */
487 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
488 unsigned int i;
489
490 /* may contain flow and gateway attribute */
491 nhsize += 2 * nla_total_size(4);
492
493 /* grab encap info */
494 for (i = 0; i < fib_info_num_path(fi); i++) {
495 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
496
497 if (nhc->nhc_lwtstate) {
498 /* RTA_ENCAP_TYPE */
499 nh_encapsize += lwtunnel_get_encap_size(
500 nhc->nhc_lwtstate);
501 /* RTA_ENCAP */
502 nh_encapsize += nla_total_size(2);
503 }
504 }
505
506 /* all nexthops are packed in a nested attribute */
507 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
508
509 }
510
511 return payload;
512}
513
514void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
515 int dst_len, u32 tb_id, const struct nl_info *info,
516 unsigned int nlm_flags)
517{
518 struct sk_buff *skb;
519 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
520 int err = -ENOBUFS;
521
522 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
523 if (!skb)
524 goto errout;
525
526 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
527 fa->fa_type, key, dst_len,
528 fa->fa_tos, fa->fa_info, nlm_flags);
529 if (err < 0) {
530 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
531 WARN_ON(err == -EMSGSIZE);
532 kfree_skb(skb);
533 goto errout;
534 }
535 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
536 info->nlh, GFP_KERNEL);
537 return;
538errout:
539 if (err < 0)
540 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
541}
542
543static int fib_detect_death(struct fib_info *fi, int order,
544 struct fib_info **last_resort, int *last_idx,
545 int dflt)
546{
547 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
548 struct neighbour *n;
549 int state = NUD_NONE;
550
551 if (likely(nhc->nhc_gw_family == AF_INET))
552 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
553 else if (nhc->nhc_gw_family == AF_INET6)
554 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
555 nhc->nhc_dev);
556 else
557 n = NULL;
558
559 if (n) {
560 state = n->nud_state;
561 neigh_release(n);
562 } else {
563 return 0;
564 }
565 if (state == NUD_REACHABLE)
566 return 0;
567 if ((state & NUD_VALID) && order != dflt)
568 return 0;
569 if ((state & NUD_VALID) ||
570 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
571 *last_resort = fi;
572 *last_idx = order;
573 }
574 return 1;
575}
576
577int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
578 u16 encap_type, void *cfg, gfp_t gfp_flags,
579 struct netlink_ext_ack *extack)
580{
581 int err;
582
583 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
584 gfp_flags);
585 if (!nhc->nhc_pcpu_rth_output)
586 return -ENOMEM;
587
588 if (encap) {
589 struct lwtunnel_state *lwtstate;
590
591 if (encap_type == LWTUNNEL_ENCAP_NONE) {
592 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
593 err = -EINVAL;
594 goto lwt_failure;
595 }
596 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
597 cfg, &lwtstate, extack);
598 if (err)
599 goto lwt_failure;
600
601 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
602 }
603
604 return 0;
605
606lwt_failure:
607 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
608 nhc->nhc_pcpu_rth_output = NULL;
609 return err;
610}
611EXPORT_SYMBOL_GPL(fib_nh_common_init);
612
613int fib_nh_init(struct net *net, struct fib_nh *nh,
614 struct fib_config *cfg, int nh_weight,
615 struct netlink_ext_ack *extack)
616{
617 int err;
618
619 nh->fib_nh_family = AF_INET;
620
621 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
622 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
623 if (err)
624 return err;
625
626 nh->fib_nh_oif = cfg->fc_oif;
627 nh->fib_nh_gw_family = cfg->fc_gw_family;
628 if (cfg->fc_gw_family == AF_INET)
629 nh->fib_nh_gw4 = cfg->fc_gw4;
630 else if (cfg->fc_gw_family == AF_INET6)
631 nh->fib_nh_gw6 = cfg->fc_gw6;
632
633 nh->fib_nh_flags = cfg->fc_flags;
634
635#ifdef CONFIG_IP_ROUTE_CLASSID
636 nh->nh_tclassid = cfg->fc_flow;
637 if (nh->nh_tclassid)
638 atomic_inc(&net->ipv4.fib_num_tclassid_users);
639#endif
640#ifdef CONFIG_IP_ROUTE_MULTIPATH
641 nh->fib_nh_weight = nh_weight;
642#endif
643 return 0;
644}
645
646#ifdef CONFIG_IP_ROUTE_MULTIPATH
647
648static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
649 struct netlink_ext_ack *extack)
650{
651 int nhs = 0;
652
653 while (rtnh_ok(rtnh, remaining)) {
654 nhs++;
655 rtnh = rtnh_next(rtnh, &remaining);
656 }
657
658 /* leftover implies invalid nexthop configuration, discard it */
659 if (remaining > 0) {
660 NL_SET_ERR_MSG(extack,
661 "Invalid nexthop configuration - extra data after nexthops");
662 nhs = 0;
663 }
664
665 return nhs;
666}
667
668static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
669 struct netlink_ext_ack *extack)
670{
671 if (nla_len(nla) < sizeof(*gw)) {
672 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
673 return -EINVAL;
674 }
675
676 *gw = nla_get_in_addr(nla);
677
678 return 0;
679}
680
681/* only called when fib_nh is integrated into fib_info */
682static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
683 int remaining, struct fib_config *cfg,
684 struct netlink_ext_ack *extack)
685{
686 struct net *net = fi->fib_net;
687 struct fib_config fib_cfg;
688 struct fib_nh *nh;
689 int ret;
690
691 change_nexthops(fi) {
692 int attrlen;
693
694 memset(&fib_cfg, 0, sizeof(fib_cfg));
695
696 if (!rtnh_ok(rtnh, remaining)) {
697 NL_SET_ERR_MSG(extack,
698 "Invalid nexthop configuration - extra data after nexthop");
699 return -EINVAL;
700 }
701
702 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
703 NL_SET_ERR_MSG(extack,
704 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
705 return -EINVAL;
706 }
707
708 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
709 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
710
711 attrlen = rtnh_attrlen(rtnh);
712 if (attrlen > 0) {
713 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
714
715 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
716 nlav = nla_find(attrs, attrlen, RTA_VIA);
717 if (nla && nlav) {
718 NL_SET_ERR_MSG(extack,
719 "Nexthop configuration can not contain both GATEWAY and VIA");
720 return -EINVAL;
721 }
722 if (nla) {
723 ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
724 extack);
725 if (ret)
726 goto errout;
727
728 if (fib_cfg.fc_gw4)
729 fib_cfg.fc_gw_family = AF_INET;
730 } else if (nlav) {
731 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
732 if (ret)
733 goto errout;
734 }
735
736 nla = nla_find(attrs, attrlen, RTA_FLOW);
737 if (nla) {
738 if (nla_len(nla) < sizeof(u32)) {
739 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
740 return -EINVAL;
741 }
742 fib_cfg.fc_flow = nla_get_u32(nla);
743 }
744
745 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
746 /* RTA_ENCAP_TYPE length checked in
747 * lwtunnel_valid_encap_type_attr
748 */
749 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
750 if (nla)
751 fib_cfg.fc_encap_type = nla_get_u16(nla);
752 }
753
754 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
755 rtnh->rtnh_hops + 1, extack);
756 if (ret)
757 goto errout;
758
759 rtnh = rtnh_next(rtnh, &remaining);
760 } endfor_nexthops(fi);
761
762 ret = -EINVAL;
763 nh = fib_info_nh(fi, 0);
764 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
765 NL_SET_ERR_MSG(extack,
766 "Nexthop device index does not match RTA_OIF");
767 goto errout;
768 }
769 if (cfg->fc_gw_family) {
770 if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
771 (cfg->fc_gw_family == AF_INET &&
772 nh->fib_nh_gw4 != cfg->fc_gw4) ||
773 (cfg->fc_gw_family == AF_INET6 &&
774 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
775 NL_SET_ERR_MSG(extack,
776 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
777 goto errout;
778 }
779 }
780#ifdef CONFIG_IP_ROUTE_CLASSID
781 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
782 NL_SET_ERR_MSG(extack,
783 "Nexthop class id does not match RTA_FLOW");
784 goto errout;
785 }
786#endif
787 ret = 0;
788errout:
789 return ret;
790}
791
792/* only called when fib_nh is integrated into fib_info */
793static void fib_rebalance(struct fib_info *fi)
794{
795 int total;
796 int w;
797
798 if (fib_info_num_path(fi) < 2)
799 return;
800
801 total = 0;
802 for_nexthops(fi) {
803 if (nh->fib_nh_flags & RTNH_F_DEAD)
804 continue;
805
806 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
807 nh->fib_nh_flags & RTNH_F_LINKDOWN)
808 continue;
809
810 total += nh->fib_nh_weight;
811 } endfor_nexthops(fi);
812
813 w = 0;
814 change_nexthops(fi) {
815 int upper_bound;
816
817 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
818 upper_bound = -1;
819 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
820 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
821 upper_bound = -1;
822 } else {
823 w += nexthop_nh->fib_nh_weight;
824 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
825 total) - 1;
826 }
827
828 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
829 } endfor_nexthops(fi);
830}
831#else /* CONFIG_IP_ROUTE_MULTIPATH */
832
833static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
834 int remaining, struct fib_config *cfg,
835 struct netlink_ext_ack *extack)
836{
837 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
838
839 return -EINVAL;
840}
841
842#define fib_rebalance(fi) do { } while (0)
843
844#endif /* CONFIG_IP_ROUTE_MULTIPATH */
845
846static int fib_encap_match(u16 encap_type,
847 struct nlattr *encap,
848 const struct fib_nh *nh,
849 const struct fib_config *cfg,
850 struct netlink_ext_ack *extack)
851{
852 struct lwtunnel_state *lwtstate;
853 int ret, result = 0;
854
855 if (encap_type == LWTUNNEL_ENCAP_NONE)
856 return 0;
857
858 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
859 cfg, &lwtstate, extack);
860 if (!ret) {
861 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
862 lwtstate_free(lwtstate);
863 }
864
865 return result;
866}
867
868int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
869 struct netlink_ext_ack *extack)
870{
871#ifdef CONFIG_IP_ROUTE_MULTIPATH
872 struct rtnexthop *rtnh;
873 int remaining;
874#endif
875
876 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
877 return 1;
878
879 if (cfg->fc_nh_id) {
880 if (fi->nh && cfg->fc_nh_id == fi->nh->id)
881 return 0;
882 return 1;
883 }
884
885 if (fi->nh) {
886 if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
887 return 1;
888 return 0;
889 }
890
891 if (cfg->fc_oif || cfg->fc_gw_family) {
892 struct fib_nh *nh;
893
894 nh = fib_info_nh(fi, 0);
895 if (cfg->fc_encap) {
896 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
897 nh, cfg, extack))
898 return 1;
899 }
900#ifdef CONFIG_IP_ROUTE_CLASSID
901 if (cfg->fc_flow &&
902 cfg->fc_flow != nh->nh_tclassid)
903 return 1;
904#endif
905 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
906 (cfg->fc_gw_family &&
907 cfg->fc_gw_family != nh->fib_nh_gw_family))
908 return 1;
909
910 if (cfg->fc_gw_family == AF_INET &&
911 cfg->fc_gw4 != nh->fib_nh_gw4)
912 return 1;
913
914 if (cfg->fc_gw_family == AF_INET6 &&
915 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
916 return 1;
917
918 return 0;
919 }
920
921#ifdef CONFIG_IP_ROUTE_MULTIPATH
922 if (!cfg->fc_mp)
923 return 0;
924
925 rtnh = cfg->fc_mp;
926 remaining = cfg->fc_mp_len;
927
928 for_nexthops(fi) {
929 int attrlen;
930
931 if (!rtnh_ok(rtnh, remaining))
932 return -EINVAL;
933
934 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
935 return 1;
936
937 attrlen = rtnh_attrlen(rtnh);
938 if (attrlen > 0) {
939 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
940 int err;
941
942 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
943 nlav = nla_find(attrs, attrlen, RTA_VIA);
944 if (nla && nlav) {
945 NL_SET_ERR_MSG(extack,
946 "Nexthop configuration can not contain both GATEWAY and VIA");
947 return -EINVAL;
948 }
949
950 if (nla) {
951 __be32 gw;
952
953 err = fib_gw_from_attr(&gw, nla, extack);
954 if (err)
955 return err;
956
957 if (nh->fib_nh_gw_family != AF_INET ||
958 gw != nh->fib_nh_gw4)
959 return 1;
960 } else if (nlav) {
961 struct fib_config cfg2;
962
963 err = fib_gw_from_via(&cfg2, nlav, extack);
964 if (err)
965 return err;
966
967 switch (nh->fib_nh_gw_family) {
968 case AF_INET:
969 if (cfg2.fc_gw_family != AF_INET ||
970 cfg2.fc_gw4 != nh->fib_nh_gw4)
971 return 1;
972 break;
973 case AF_INET6:
974 if (cfg2.fc_gw_family != AF_INET6 ||
975 ipv6_addr_cmp(&cfg2.fc_gw6,
976 &nh->fib_nh_gw6))
977 return 1;
978 break;
979 }
980 }
981
982#ifdef CONFIG_IP_ROUTE_CLASSID
983 nla = nla_find(attrs, attrlen, RTA_FLOW);
984 if (nla) {
985 if (nla_len(nla) < sizeof(u32)) {
986 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
987 return -EINVAL;
988 }
989 if (nla_get_u32(nla) != nh->nh_tclassid)
990 return 1;
991 }
992#endif
993 }
994
995 rtnh = rtnh_next(rtnh, &remaining);
996 } endfor_nexthops(fi);
997#endif
998 return 0;
999}
1000
1001bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
1002{
1003 struct nlattr *nla;
1004 int remaining;
1005
1006 if (!cfg->fc_mx)
1007 return true;
1008
1009 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1010 int type = nla_type(nla);
1011 u32 fi_val, val;
1012
1013 if (!type)
1014 continue;
1015 if (type > RTAX_MAX)
1016 return false;
1017
1018 type = array_index_nospec(type, RTAX_MAX + 1);
1019 if (type == RTAX_CC_ALGO) {
1020 char tmp[TCP_CA_NAME_MAX];
1021 bool ecn_ca = false;
1022
1023 nla_strlcpy(tmp, nla, sizeof(tmp));
1024 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
1025 } else {
1026 if (nla_len(nla) != sizeof(u32))
1027 return false;
1028 val = nla_get_u32(nla);
1029 }
1030
1031 fi_val = fi->fib_metrics->metrics[type - 1];
1032 if (type == RTAX_FEATURES)
1033 fi_val &= ~DST_FEATURE_ECN_CA;
1034
1035 if (fi_val != val)
1036 return false;
1037 }
1038
1039 return true;
1040}
1041
1042static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1043 u32 table, struct netlink_ext_ack *extack)
1044{
1045 struct fib6_config cfg = {
1046 .fc_table = table,
1047 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1048 .fc_ifindex = nh->fib_nh_oif,
1049 .fc_gateway = nh->fib_nh_gw6,
1050 };
1051 struct fib6_nh fib6_nh = {};
1052 int err;
1053
1054 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1055 if (!err) {
1056 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1057 dev_hold(nh->fib_nh_dev);
1058 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1059 nh->fib_nh_scope = RT_SCOPE_LINK;
1060
1061 ipv6_stub->fib6_nh_release(&fib6_nh);
1062 }
1063
1064 return err;
1065}
1066
1067/*
1068 * Picture
1069 * -------
1070 *
1071 * Semantics of nexthop is very messy by historical reasons.
1072 * We have to take into account, that:
1073 * a) gateway can be actually local interface address,
1074 * so that gatewayed route is direct.
1075 * b) gateway must be on-link address, possibly
1076 * described not by an ifaddr, but also by a direct route.
1077 * c) If both gateway and interface are specified, they should not
1078 * contradict.
1079 * d) If we use tunnel routes, gateway could be not on-link.
1080 *
1081 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1082 * results in pretty ugly and hairy code with obscure logic.
1083 *
1084 * I chose to generalized it instead, so that the size
1085 * of code does not increase practically, but it becomes
1086 * much more general.
1087 * Every prefix is assigned a "scope" value: "host" is local address,
1088 * "link" is direct route,
1089 * [ ... "site" ... "interior" ... ]
1090 * and "universe" is true gateway route with global meaning.
1091 *
1092 * Every prefix refers to a set of "nexthop"s (gw, oif),
1093 * where gw must have narrower scope. This recursion stops
1094 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1095 * which means that gw is forced to be on link.
1096 *
1097 * Code is still hairy, but now it is apparently logically
1098 * consistent and very flexible. F.e. as by-product it allows
1099 * to co-exists in peace independent exterior and interior
1100 * routing processes.
1101 *
1102 * Normally it looks as following.
1103 *
1104 * {universe prefix} -> (gw, oif) [scope link]
1105 * |
1106 * |-> {link prefix} -> (gw, oif) [scope local]
1107 * |
1108 * |-> {local prefix} (terminal node)
1109 */
1110static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1111 u8 scope, struct netlink_ext_ack *extack)
1112{
1113 struct net_device *dev;
1114 struct fib_result res;
1115 int err = 0;
1116
1117 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1118 unsigned int addr_type;
1119
1120 if (scope >= RT_SCOPE_LINK) {
1121 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1122 return -EINVAL;
1123 }
1124 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1125 if (!dev) {
1126 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1127 return -ENODEV;
1128 }
1129 if (!(dev->flags & IFF_UP)) {
1130 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1131 return -ENETDOWN;
1132 }
1133 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1134 if (addr_type != RTN_UNICAST) {
1135 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1136 return -EINVAL;
1137 }
1138 if (!netif_carrier_ok(dev))
1139 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1140 nh->fib_nh_dev = dev;
1141 dev_hold(dev);
1142 nh->fib_nh_scope = RT_SCOPE_LINK;
1143 return 0;
1144 }
1145 rcu_read_lock();
1146 {
1147 struct fib_table *tbl = NULL;
1148 struct flowi4 fl4 = {
1149 .daddr = nh->fib_nh_gw4,
1150 .flowi4_scope = scope + 1,
1151 .flowi4_oif = nh->fib_nh_oif,
1152 .flowi4_iif = LOOPBACK_IFINDEX,
1153 };
1154
1155 /* It is not necessary, but requires a bit of thinking */
1156 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1157 fl4.flowi4_scope = RT_SCOPE_LINK;
1158
1159 if (table && table != RT_TABLE_MAIN)
1160 tbl = fib_get_table(net, table);
1161
1162 if (tbl)
1163 err = fib_table_lookup(tbl, &fl4, &res,
1164 FIB_LOOKUP_IGNORE_LINKSTATE |
1165 FIB_LOOKUP_NOREF);
1166
1167 /* on error or if no table given do full lookup. This
1168 * is needed for example when nexthops are in the local
1169 * table rather than the given table
1170 */
1171 if (!tbl || err) {
1172 err = fib_lookup(net, &fl4, &res,
1173 FIB_LOOKUP_IGNORE_LINKSTATE);
1174 }
1175
1176 if (err) {
1177 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1178 goto out;
1179 }
1180 }
1181
1182 err = -EINVAL;
1183 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1184 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1185 goto out;
1186 }
1187 nh->fib_nh_scope = res.scope;
1188 nh->fib_nh_oif = FIB_RES_OIF(res);
1189 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1190 if (!dev) {
1191 NL_SET_ERR_MSG(extack,
1192 "No egress device for nexthop gateway");
1193 goto out;
1194 }
1195 dev_hold(dev);
1196 if (!netif_carrier_ok(dev))
1197 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1198 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1199out:
1200 rcu_read_unlock();
1201 return err;
1202}
1203
1204static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1205 struct netlink_ext_ack *extack)
1206{
1207 struct in_device *in_dev;
1208 int err;
1209
1210 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1211 NL_SET_ERR_MSG(extack,
1212 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1213 return -EINVAL;
1214 }
1215
1216 rcu_read_lock();
1217
1218 err = -ENODEV;
1219 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1220 if (!in_dev)
1221 goto out;
1222 err = -ENETDOWN;
1223 if (!(in_dev->dev->flags & IFF_UP)) {
1224 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1225 goto out;
1226 }
1227
1228 nh->fib_nh_dev = in_dev->dev;
1229 dev_hold(nh->fib_nh_dev);
1230 nh->fib_nh_scope = RT_SCOPE_LINK;
1231 if (!netif_carrier_ok(nh->fib_nh_dev))
1232 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1233 err = 0;
1234out:
1235 rcu_read_unlock();
1236 return err;
1237}
1238
1239int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1240 struct netlink_ext_ack *extack)
1241{
1242 int err;
1243
1244 if (nh->fib_nh_gw_family == AF_INET)
1245 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1246 else if (nh->fib_nh_gw_family == AF_INET6)
1247 err = fib_check_nh_v6_gw(net, nh, table, extack);
1248 else
1249 err = fib_check_nh_nongw(net, nh, extack);
1250
1251 return err;
1252}
1253
1254static inline unsigned int fib_laddr_hashfn(__be32 val)
1255{
1256 unsigned int mask = (fib_info_hash_size - 1);
1257
1258 return ((__force u32)val ^
1259 ((__force u32)val >> 7) ^
1260 ((__force u32)val >> 14)) & mask;
1261}
1262
1263static struct hlist_head *fib_info_hash_alloc(int bytes)
1264{
1265 if (bytes <= PAGE_SIZE)
1266 return kzalloc(bytes, GFP_KERNEL);
1267 else
1268 return (struct hlist_head *)
1269 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1270 get_order(bytes));
1271}
1272
1273static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1274{
1275 if (!hash)
1276 return;
1277
1278 if (bytes <= PAGE_SIZE)
1279 kfree(hash);
1280 else
1281 free_pages((unsigned long) hash, get_order(bytes));
1282}
1283
1284static void fib_info_hash_move(struct hlist_head *new_info_hash,
1285 struct hlist_head *new_laddrhash,
1286 unsigned int new_size)
1287{
1288 struct hlist_head *old_info_hash, *old_laddrhash;
1289 unsigned int old_size = fib_info_hash_size;
1290 unsigned int i, bytes;
1291
1292 spin_lock_bh(&fib_info_lock);
1293 old_info_hash = fib_info_hash;
1294 old_laddrhash = fib_info_laddrhash;
1295 fib_info_hash_size = new_size;
1296
1297 for (i = 0; i < old_size; i++) {
1298 struct hlist_head *head = &fib_info_hash[i];
1299 struct hlist_node *n;
1300 struct fib_info *fi;
1301
1302 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1303 struct hlist_head *dest;
1304 unsigned int new_hash;
1305
1306 new_hash = fib_info_hashfn(fi);
1307 dest = &new_info_hash[new_hash];
1308 hlist_add_head(&fi->fib_hash, dest);
1309 }
1310 }
1311 fib_info_hash = new_info_hash;
1312
1313 for (i = 0; i < old_size; i++) {
1314 struct hlist_head *lhead = &fib_info_laddrhash[i];
1315 struct hlist_node *n;
1316 struct fib_info *fi;
1317
1318 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1319 struct hlist_head *ldest;
1320 unsigned int new_hash;
1321
1322 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1323 ldest = &new_laddrhash[new_hash];
1324 hlist_add_head(&fi->fib_lhash, ldest);
1325 }
1326 }
1327 fib_info_laddrhash = new_laddrhash;
1328
1329 spin_unlock_bh(&fib_info_lock);
1330
1331 bytes = old_size * sizeof(struct hlist_head *);
1332 fib_info_hash_free(old_info_hash, bytes);
1333 fib_info_hash_free(old_laddrhash, bytes);
1334}
1335
1336__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1337 unsigned char scope)
1338{
1339 struct fib_nh *nh;
1340 __be32 saddr;
1341
1342 if (nhc->nhc_family != AF_INET)
1343 return inet_select_addr(nhc->nhc_dev, 0, scope);
1344
1345 nh = container_of(nhc, struct fib_nh, nh_common);
1346 saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1347
1348 WRITE_ONCE(nh->nh_saddr, saddr);
1349 WRITE_ONCE(nh->nh_saddr_genid, atomic_read(&net->ipv4.dev_addr_genid));
1350
1351 return saddr;
1352}
1353
1354__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1355{
1356 struct fib_nh_common *nhc = res->nhc;
1357
1358 if (res->fi->fib_prefsrc)
1359 return res->fi->fib_prefsrc;
1360
1361 if (nhc->nhc_family == AF_INET) {
1362 struct fib_nh *nh;
1363
1364 nh = container_of(nhc, struct fib_nh, nh_common);
1365 if (READ_ONCE(nh->nh_saddr_genid) ==
1366 atomic_read(&net->ipv4.dev_addr_genid))
1367 return READ_ONCE(nh->nh_saddr);
1368 }
1369
1370 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1371}
1372
1373static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1374{
1375 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1376 fib_prefsrc != cfg->fc_dst) {
1377 u32 tb_id = cfg->fc_table;
1378 int rc;
1379
1380 if (tb_id == RT_TABLE_MAIN)
1381 tb_id = RT_TABLE_LOCAL;
1382
1383 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1384 fib_prefsrc, tb_id);
1385
1386 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1387 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1388 fib_prefsrc, RT_TABLE_LOCAL);
1389 }
1390
1391 if (rc != RTN_LOCAL)
1392 return false;
1393 }
1394 return true;
1395}
1396
1397struct fib_info *fib_create_info(struct fib_config *cfg,
1398 struct netlink_ext_ack *extack)
1399{
1400 int err;
1401 struct fib_info *fi = NULL;
1402 struct nexthop *nh = NULL;
1403 struct fib_info *ofi;
1404 int nhs = 1;
1405 struct net *net = cfg->fc_nlinfo.nl_net;
1406
1407 if (cfg->fc_type > RTN_MAX)
1408 goto err_inval;
1409
1410 /* Fast check to catch the most weird cases */
1411 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1412 NL_SET_ERR_MSG(extack, "Invalid scope");
1413 goto err_inval;
1414 }
1415
1416 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1417 NL_SET_ERR_MSG(extack,
1418 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1419 goto err_inval;
1420 }
1421
1422 if (cfg->fc_nh_id) {
1423 if (!cfg->fc_mx) {
1424 fi = fib_find_info_nh(net, cfg);
1425 if (fi) {
1426 fi->fib_treeref++;
1427 return fi;
1428 }
1429 }
1430
1431 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1432 if (!nh) {
1433 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1434 goto err_inval;
1435 }
1436 nhs = 0;
1437 }
1438
1439#ifdef CONFIG_IP_ROUTE_MULTIPATH
1440 if (cfg->fc_mp) {
1441 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1442 if (nhs == 0)
1443 goto err_inval;
1444 }
1445#endif
1446
1447 err = -ENOBUFS;
1448 if (fib_info_cnt >= fib_info_hash_size) {
1449 unsigned int new_size = fib_info_hash_size << 1;
1450 struct hlist_head *new_info_hash;
1451 struct hlist_head *new_laddrhash;
1452 unsigned int bytes;
1453
1454 if (!new_size)
1455 new_size = 16;
1456 bytes = new_size * sizeof(struct hlist_head *);
1457 new_info_hash = fib_info_hash_alloc(bytes);
1458 new_laddrhash = fib_info_hash_alloc(bytes);
1459 if (!new_info_hash || !new_laddrhash) {
1460 fib_info_hash_free(new_info_hash, bytes);
1461 fib_info_hash_free(new_laddrhash, bytes);
1462 } else
1463 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1464
1465 if (!fib_info_hash_size)
1466 goto failure;
1467 }
1468
1469 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1470 if (!fi)
1471 goto failure;
1472 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1473 cfg->fc_mx_len, extack);
1474 if (IS_ERR(fi->fib_metrics)) {
1475 err = PTR_ERR(fi->fib_metrics);
1476 kfree(fi);
1477 return ERR_PTR(err);
1478 }
1479
1480 fib_info_cnt++;
1481 fi->fib_net = net;
1482 fi->fib_protocol = cfg->fc_protocol;
1483 fi->fib_scope = cfg->fc_scope;
1484 fi->fib_flags = cfg->fc_flags;
1485 fi->fib_priority = cfg->fc_priority;
1486 fi->fib_prefsrc = cfg->fc_prefsrc;
1487 fi->fib_type = cfg->fc_type;
1488 fi->fib_tb_id = cfg->fc_table;
1489
1490 fi->fib_nhs = nhs;
1491 if (nh) {
1492 if (!nexthop_get(nh)) {
1493 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1494 err = -EINVAL;
1495 } else {
1496 err = 0;
1497 fi->nh = nh;
1498 }
1499 } else {
1500 change_nexthops(fi) {
1501 nexthop_nh->nh_parent = fi;
1502 } endfor_nexthops(fi)
1503
1504 if (cfg->fc_mp)
1505 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1506 extack);
1507 else
1508 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1509 }
1510
1511 if (err != 0)
1512 goto failure;
1513
1514 if (fib_props[cfg->fc_type].error) {
1515 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1516 NL_SET_ERR_MSG(extack,
1517 "Gateway, device and multipath can not be specified for this route type");
1518 goto err_inval;
1519 }
1520 goto link_it;
1521 } else {
1522 switch (cfg->fc_type) {
1523 case RTN_UNICAST:
1524 case RTN_LOCAL:
1525 case RTN_BROADCAST:
1526 case RTN_ANYCAST:
1527 case RTN_MULTICAST:
1528 break;
1529 default:
1530 NL_SET_ERR_MSG(extack, "Invalid route type");
1531 goto err_inval;
1532 }
1533 }
1534
1535 if (cfg->fc_scope > RT_SCOPE_HOST) {
1536 NL_SET_ERR_MSG(extack, "Invalid scope");
1537 goto err_inval;
1538 }
1539
1540 if (fi->nh) {
1541 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1542 if (err)
1543 goto failure;
1544 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
1545 struct fib_nh *nh = fi->fib_nh;
1546
1547 /* Local address is added. */
1548 if (nhs != 1) {
1549 NL_SET_ERR_MSG(extack,
1550 "Route with host scope can not have multiple nexthops");
1551 goto err_inval;
1552 }
1553 if (nh->fib_nh_gw_family) {
1554 NL_SET_ERR_MSG(extack,
1555 "Route with host scope can not have a gateway");
1556 goto err_inval;
1557 }
1558 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1559 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
1560 err = -ENODEV;
1561 if (!nh->fib_nh_dev)
1562 goto failure;
1563 } else {
1564 int linkdown = 0;
1565
1566 change_nexthops(fi) {
1567 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1568 cfg->fc_table, cfg->fc_scope,
1569 extack);
1570 if (err != 0)
1571 goto failure;
1572 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1573 linkdown++;
1574 } endfor_nexthops(fi)
1575 if (linkdown == fi->fib_nhs)
1576 fi->fib_flags |= RTNH_F_LINKDOWN;
1577 }
1578
1579 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1580 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1581 goto err_inval;
1582 }
1583
1584 if (!fi->nh) {
1585 change_nexthops(fi) {
1586 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1587 fi->fib_scope);
1588 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1589 fi->fib_nh_is_v6 = true;
1590 } endfor_nexthops(fi)
1591
1592 fib_rebalance(fi);
1593 }
1594
1595link_it:
1596 ofi = fib_find_info(fi);
1597 if (ofi) {
1598 /* fib_table_lookup() should not see @fi yet. */
1599 fi->fib_dead = 1;
1600 free_fib_info(fi);
1601 ofi->fib_treeref++;
1602 return ofi;
1603 }
1604
1605 fi->fib_treeref++;
1606 refcount_set(&fi->fib_clntref, 1);
1607 spin_lock_bh(&fib_info_lock);
1608 hlist_add_head(&fi->fib_hash,
1609 &fib_info_hash[fib_info_hashfn(fi)]);
1610 if (fi->fib_prefsrc) {
1611 struct hlist_head *head;
1612
1613 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1614 hlist_add_head(&fi->fib_lhash, head);
1615 }
1616 if (fi->nh) {
1617 list_add(&fi->nh_list, &nh->fi_list);
1618 } else {
1619 change_nexthops(fi) {
1620 struct hlist_head *head;
1621
1622 if (!nexthop_nh->fib_nh_dev)
1623 continue;
1624 head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
1625 hlist_add_head(&nexthop_nh->nh_hash, head);
1626 } endfor_nexthops(fi)
1627 }
1628 spin_unlock_bh(&fib_info_lock);
1629 return fi;
1630
1631err_inval:
1632 err = -EINVAL;
1633
1634failure:
1635 if (fi) {
1636 /* fib_table_lookup() should not see @fi yet. */
1637 fi->fib_dead = 1;
1638 free_fib_info(fi);
1639 }
1640
1641 return ERR_PTR(err);
1642}
1643
1644int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1645 u8 rt_family, unsigned char *flags, bool skip_oif)
1646{
1647 if (nhc->nhc_flags & RTNH_F_DEAD)
1648 *flags |= RTNH_F_DEAD;
1649
1650 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1651 *flags |= RTNH_F_LINKDOWN;
1652
1653 rcu_read_lock();
1654 switch (nhc->nhc_family) {
1655 case AF_INET:
1656 if (ip_ignore_linkdown(nhc->nhc_dev))
1657 *flags |= RTNH_F_DEAD;
1658 break;
1659 case AF_INET6:
1660 if (ip6_ignore_linkdown(nhc->nhc_dev))
1661 *flags |= RTNH_F_DEAD;
1662 break;
1663 }
1664 rcu_read_unlock();
1665 }
1666
1667 switch (nhc->nhc_gw_family) {
1668 case AF_INET:
1669 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1670 goto nla_put_failure;
1671 break;
1672 case AF_INET6:
1673 /* if gateway family does not match nexthop family
1674 * gateway is encoded as RTA_VIA
1675 */
1676 if (rt_family != nhc->nhc_gw_family) {
1677 int alen = sizeof(struct in6_addr);
1678 struct nlattr *nla;
1679 struct rtvia *via;
1680
1681 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1682 if (!nla)
1683 goto nla_put_failure;
1684
1685 via = nla_data(nla);
1686 via->rtvia_family = AF_INET6;
1687 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1688 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1689 &nhc->nhc_gw.ipv6) < 0) {
1690 goto nla_put_failure;
1691 }
1692 break;
1693 }
1694
1695 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1696 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
1697 *flags |= RTNH_F_OFFLOAD;
1698
1699 if (!skip_oif && nhc->nhc_dev &&
1700 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1701 goto nla_put_failure;
1702
1703 if (nhc->nhc_lwtstate &&
1704 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1705 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1706 goto nla_put_failure;
1707
1708 return 0;
1709
1710nla_put_failure:
1711 return -EMSGSIZE;
1712}
1713EXPORT_SYMBOL_GPL(fib_nexthop_info);
1714
1715#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1716int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1717 int nh_weight, u8 rt_family, u32 nh_tclassid)
1718{
1719 const struct net_device *dev = nhc->nhc_dev;
1720 struct rtnexthop *rtnh;
1721 unsigned char flags = 0;
1722
1723 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1724 if (!rtnh)
1725 goto nla_put_failure;
1726
1727 rtnh->rtnh_hops = nh_weight - 1;
1728 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1729
1730 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1731 goto nla_put_failure;
1732
1733 rtnh->rtnh_flags = flags;
1734
1735 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1736 goto nla_put_failure;
1737
1738 /* length of rtnetlink header + attributes */
1739 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1740
1741 return 0;
1742
1743nla_put_failure:
1744 return -EMSGSIZE;
1745}
1746EXPORT_SYMBOL_GPL(fib_add_nexthop);
1747#endif
1748
1749#ifdef CONFIG_IP_ROUTE_MULTIPATH
1750static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1751{
1752 struct nlattr *mp;
1753
1754 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1755 if (!mp)
1756 goto nla_put_failure;
1757
1758 if (unlikely(fi->nh)) {
1759 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1760 goto nla_put_failure;
1761 goto mp_end;
1762 }
1763
1764 for_nexthops(fi) {
1765 u32 nh_tclassid = 0;
1766#ifdef CONFIG_IP_ROUTE_CLASSID
1767 nh_tclassid = nh->nh_tclassid;
1768#endif
1769 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1770 AF_INET, nh_tclassid) < 0)
1771 goto nla_put_failure;
1772 } endfor_nexthops(fi);
1773
1774mp_end:
1775 nla_nest_end(skb, mp);
1776
1777 return 0;
1778
1779nla_put_failure:
1780 return -EMSGSIZE;
1781}
1782#else
1783static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1784{
1785 return 0;
1786}
1787#endif
1788
1789int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1790 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
1791 struct fib_info *fi, unsigned int flags)
1792{
1793 unsigned int nhs = fib_info_num_path(fi);
1794 struct nlmsghdr *nlh;
1795 struct rtmsg *rtm;
1796
1797 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1798 if (!nlh)
1799 return -EMSGSIZE;
1800
1801 rtm = nlmsg_data(nlh);
1802 rtm->rtm_family = AF_INET;
1803 rtm->rtm_dst_len = dst_len;
1804 rtm->rtm_src_len = 0;
1805 rtm->rtm_tos = tos;
1806 if (tb_id < 256)
1807 rtm->rtm_table = tb_id;
1808 else
1809 rtm->rtm_table = RT_TABLE_COMPAT;
1810 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1811 goto nla_put_failure;
1812 rtm->rtm_type = type;
1813 rtm->rtm_flags = fi->fib_flags;
1814 rtm->rtm_scope = fi->fib_scope;
1815 rtm->rtm_protocol = fi->fib_protocol;
1816
1817 if (rtm->rtm_dst_len &&
1818 nla_put_in_addr(skb, RTA_DST, dst))
1819 goto nla_put_failure;
1820 if (fi->fib_priority &&
1821 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1822 goto nla_put_failure;
1823 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1824 goto nla_put_failure;
1825
1826 if (fi->fib_prefsrc &&
1827 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1828 goto nla_put_failure;
1829
1830 if (fi->nh) {
1831 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
1832 goto nla_put_failure;
1833 if (nexthop_is_blackhole(fi->nh))
1834 rtm->rtm_type = RTN_BLACKHOLE;
1835 }
1836
1837 if (nhs == 1) {
1838 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1839 unsigned char flags = 0;
1840
1841 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1842 goto nla_put_failure;
1843
1844 rtm->rtm_flags = flags;
1845#ifdef CONFIG_IP_ROUTE_CLASSID
1846 if (nhc->nhc_family == AF_INET) {
1847 struct fib_nh *nh;
1848
1849 nh = container_of(nhc, struct fib_nh, nh_common);
1850 if (nh->nh_tclassid &&
1851 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1852 goto nla_put_failure;
1853 }
1854#endif
1855 } else {
1856 if (fib_add_multipath(skb, fi) < 0)
1857 goto nla_put_failure;
1858 }
1859
1860 nlmsg_end(skb, nlh);
1861 return 0;
1862
1863nla_put_failure:
1864 nlmsg_cancel(skb, nlh);
1865 return -EMSGSIZE;
1866}
1867
1868/*
1869 * Update FIB if:
1870 * - local address disappeared -> we must delete all the entries
1871 * referring to it.
1872 * - device went down -> we must shutdown all nexthops going via it.
1873 */
1874int fib_sync_down_addr(struct net_device *dev, __be32 local)
1875{
1876 int ret = 0;
1877 unsigned int hash = fib_laddr_hashfn(local);
1878 struct hlist_head *head = &fib_info_laddrhash[hash];
1879 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
1880 struct net *net = dev_net(dev);
1881 struct fib_info *fi;
1882
1883 if (!fib_info_laddrhash || local == 0)
1884 return 0;
1885
1886 hlist_for_each_entry(fi, head, fib_lhash) {
1887 if (!net_eq(fi->fib_net, net) ||
1888 fi->fib_tb_id != tb_id)
1889 continue;
1890 if (fi->fib_prefsrc == local) {
1891 fi->fib_flags |= RTNH_F_DEAD;
1892 ret++;
1893 }
1894 }
1895 return ret;
1896}
1897
1898static int call_fib_nh_notifiers(struct fib_nh *nh,
1899 enum fib_event_type event_type)
1900{
1901 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1902 struct fib_nh_notifier_info info = {
1903 .fib_nh = nh,
1904 };
1905
1906 switch (event_type) {
1907 case FIB_EVENT_NH_ADD:
1908 if (nh->fib_nh_flags & RTNH_F_DEAD)
1909 break;
1910 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1911 break;
1912 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1913 &info.info);
1914 case FIB_EVENT_NH_DEL:
1915 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1916 (nh->fib_nh_flags & RTNH_F_DEAD))
1917 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1918 event_type, &info.info);
1919 default:
1920 break;
1921 }
1922
1923 return NOTIFY_DONE;
1924}
1925
1926/* Update the PMTU of exceptions when:
1927 * - the new MTU of the first hop becomes smaller than the PMTU
1928 * - the old MTU was the same as the PMTU, and it limited discovery of
1929 * larger MTUs on the path. With that limit raised, we can now
1930 * discover larger MTUs
1931 * A special case is locked exceptions, for which the PMTU is smaller
1932 * than the minimal accepted PMTU:
1933 * - if the new MTU is greater than the PMTU, don't make any change
1934 * - otherwise, unlock and set PMTU
1935 */
1936void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1937{
1938 struct fnhe_hash_bucket *bucket;
1939 int i;
1940
1941 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1942 if (!bucket)
1943 return;
1944
1945 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1946 struct fib_nh_exception *fnhe;
1947
1948 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1949 fnhe;
1950 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1951 if (fnhe->fnhe_mtu_locked) {
1952 if (new <= fnhe->fnhe_pmtu) {
1953 fnhe->fnhe_pmtu = new;
1954 fnhe->fnhe_mtu_locked = false;
1955 }
1956 } else if (new < fnhe->fnhe_pmtu ||
1957 orig == fnhe->fnhe_pmtu) {
1958 fnhe->fnhe_pmtu = new;
1959 }
1960 }
1961 }
1962}
1963
1964void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1965{
1966 struct hlist_head *head = fib_info_devhash_bucket(dev);
1967 struct fib_nh *nh;
1968
1969 hlist_for_each_entry(nh, head, nh_hash) {
1970 if (nh->fib_nh_dev == dev)
1971 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1972 }
1973}
1974
1975/* Event force Flags Description
1976 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1977 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1978 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1979 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1980 *
1981 * only used when fib_nh is built into fib_info
1982 */
1983int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1984{
1985 struct hlist_head *head = fib_info_devhash_bucket(dev);
1986 struct fib_info *prev_fi = NULL;
1987 int scope = RT_SCOPE_NOWHERE;
1988 struct fib_nh *nh;
1989 int ret = 0;
1990
1991 if (force)
1992 scope = -1;
1993
1994 hlist_for_each_entry(nh, head, nh_hash) {
1995 struct fib_info *fi = nh->nh_parent;
1996 int dead;
1997
1998 BUG_ON(!fi->fib_nhs);
1999 if (nh->fib_nh_dev != dev || fi == prev_fi)
2000 continue;
2001 prev_fi = fi;
2002 dead = 0;
2003 change_nexthops(fi) {
2004 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
2005 dead++;
2006 else if (nexthop_nh->fib_nh_dev == dev &&
2007 nexthop_nh->fib_nh_scope != scope) {
2008 switch (event) {
2009 case NETDEV_DOWN:
2010 case NETDEV_UNREGISTER:
2011 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
2012 /* fall through */
2013 case NETDEV_CHANGE:
2014 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
2015 break;
2016 }
2017 call_fib_nh_notifiers(nexthop_nh,
2018 FIB_EVENT_NH_DEL);
2019 dead++;
2020 }
2021#ifdef CONFIG_IP_ROUTE_MULTIPATH
2022 if (event == NETDEV_UNREGISTER &&
2023 nexthop_nh->fib_nh_dev == dev) {
2024 dead = fi->fib_nhs;
2025 break;
2026 }
2027#endif
2028 } endfor_nexthops(fi)
2029 if (dead == fi->fib_nhs) {
2030 switch (event) {
2031 case NETDEV_DOWN:
2032 case NETDEV_UNREGISTER:
2033 fi->fib_flags |= RTNH_F_DEAD;
2034 /* fall through */
2035 case NETDEV_CHANGE:
2036 fi->fib_flags |= RTNH_F_LINKDOWN;
2037 break;
2038 }
2039 ret++;
2040 }
2041
2042 fib_rebalance(fi);
2043 }
2044
2045 return ret;
2046}
2047
2048/* Must be invoked inside of an RCU protected region. */
2049static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
2050{
2051 struct fib_info *fi = NULL, *last_resort = NULL;
2052 struct hlist_head *fa_head = res->fa_head;
2053 struct fib_table *tb = res->table;
2054 u8 slen = 32 - res->prefixlen;
2055 int order = -1, last_idx = -1;
2056 struct fib_alias *fa, *fa1 = NULL;
2057 u32 last_prio = res->fi->fib_priority;
2058 u8 last_tos = 0;
2059
2060 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
2061 struct fib_info *next_fi = fa->fa_info;
2062 struct fib_nh_common *nhc;
2063
2064 if (fa->fa_slen != slen)
2065 continue;
2066 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2067 continue;
2068 if (fa->tb_id != tb->tb_id)
2069 continue;
2070 if (next_fi->fib_priority > last_prio &&
2071 fa->fa_tos == last_tos) {
2072 if (last_tos)
2073 continue;
2074 break;
2075 }
2076 if (next_fi->fib_flags & RTNH_F_DEAD)
2077 continue;
2078 last_tos = fa->fa_tos;
2079 last_prio = next_fi->fib_priority;
2080
2081 if (next_fi->fib_scope != res->scope ||
2082 fa->fa_type != RTN_UNICAST)
2083 continue;
2084
2085 nhc = fib_info_nhc(next_fi, 0);
2086 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
2087 continue;
2088
2089 fib_alias_accessed(fa);
2090
2091 if (!fi) {
2092 if (next_fi != res->fi)
2093 break;
2094 fa1 = fa;
2095 } else if (!fib_detect_death(fi, order, &last_resort,
2096 &last_idx, fa1->fa_default)) {
2097 fib_result_assign(res, fi);
2098 fa1->fa_default = order;
2099 goto out;
2100 }
2101 fi = next_fi;
2102 order++;
2103 }
2104
2105 if (order <= 0 || !fi) {
2106 if (fa1)
2107 fa1->fa_default = -1;
2108 goto out;
2109 }
2110
2111 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2112 fa1->fa_default)) {
2113 fib_result_assign(res, fi);
2114 fa1->fa_default = order;
2115 goto out;
2116 }
2117
2118 if (last_idx >= 0)
2119 fib_result_assign(res, last_resort);
2120 fa1->fa_default = last_idx;
2121out:
2122 return;
2123}
2124
2125/*
2126 * Dead device goes up. We wake up dead nexthops.
2127 * It takes sense only on multipath routes.
2128 *
2129 * only used when fib_nh is built into fib_info
2130 */
2131int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
2132{
2133 struct fib_info *prev_fi;
2134 struct hlist_head *head;
2135 struct fib_nh *nh;
2136 int ret;
2137
2138 if (!(dev->flags & IFF_UP))
2139 return 0;
2140
2141 if (nh_flags & RTNH_F_DEAD) {
2142 unsigned int flags = dev_get_flags(dev);
2143
2144 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2145 nh_flags |= RTNH_F_LINKDOWN;
2146 }
2147
2148 prev_fi = NULL;
2149 head = fib_info_devhash_bucket(dev);
2150 ret = 0;
2151
2152 hlist_for_each_entry(nh, head, nh_hash) {
2153 struct fib_info *fi = nh->nh_parent;
2154 int alive;
2155
2156 BUG_ON(!fi->fib_nhs);
2157 if (nh->fib_nh_dev != dev || fi == prev_fi)
2158 continue;
2159
2160 prev_fi = fi;
2161 alive = 0;
2162 change_nexthops(fi) {
2163 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
2164 alive++;
2165 continue;
2166 }
2167 if (!nexthop_nh->fib_nh_dev ||
2168 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
2169 continue;
2170 if (nexthop_nh->fib_nh_dev != dev ||
2171 !__in_dev_get_rtnl(dev))
2172 continue;
2173 alive++;
2174 nexthop_nh->fib_nh_flags &= ~nh_flags;
2175 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
2176 } endfor_nexthops(fi)
2177
2178 if (alive > 0) {
2179 fi->fib_flags &= ~nh_flags;
2180 ret++;
2181 }
2182
2183 fib_rebalance(fi);
2184 }
2185
2186 return ret;
2187}
2188
2189#ifdef CONFIG_IP_ROUTE_MULTIPATH
2190static bool fib_good_nh(const struct fib_nh *nh)
2191{
2192 int state = NUD_REACHABLE;
2193
2194 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2195 struct neighbour *n;
2196
2197 rcu_read_lock_bh();
2198
2199 if (likely(nh->fib_nh_gw_family == AF_INET))
2200 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2201 (__force u32)nh->fib_nh_gw4);
2202 else if (nh->fib_nh_gw_family == AF_INET6)
2203 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2204 &nh->fib_nh_gw6);
2205 else
2206 n = NULL;
2207 if (n)
2208 state = n->nud_state;
2209
2210 rcu_read_unlock_bh();
2211 }
2212
2213 return !!(state & NUD_VALID);
2214}
2215
2216void fib_select_multipath(struct fib_result *res, int hash)
2217{
2218 struct fib_info *fi = res->fi;
2219 struct net *net = fi->fib_net;
2220 bool first = false;
2221
2222 if (unlikely(res->fi->nh)) {
2223 nexthop_path_fib_result(res, hash);
2224 return;
2225 }
2226
2227 change_nexthops(fi) {
2228 if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
2229 if (!fib_good_nh(nexthop_nh))
2230 continue;
2231 if (!first) {
2232 res->nh_sel = nhsel;
2233 res->nhc = &nexthop_nh->nh_common;
2234 first = true;
2235 }
2236 }
2237
2238 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
2239 continue;
2240
2241 res->nh_sel = nhsel;
2242 res->nhc = &nexthop_nh->nh_common;
2243 return;
2244 } endfor_nexthops(fi);
2245}
2246#endif
2247
2248void fib_select_path(struct net *net, struct fib_result *res,
2249 struct flowi4 *fl4, const struct sk_buff *skb)
2250{
2251 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2252 goto check_saddr;
2253
2254#ifdef CONFIG_IP_ROUTE_MULTIPATH
2255 if (fib_info_num_path(res->fi) > 1) {
2256 int h = fib_multipath_hash(net, fl4, skb, NULL);
2257
2258 fib_select_multipath(res, h);
2259 }
2260 else
2261#endif
2262 if (!res->prefixlen &&
2263 res->table->tb_num_default > 1 &&
2264 res->type == RTN_UNICAST)
2265 fib_select_default(fl4, res);
2266
2267check_saddr:
2268 if (!fl4->saddr)
2269 fl4->saddr = fib_result_prefsrc(net, res);
2270}