1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
7 * IPv4 Forwarding Information Base: semantics.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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>
18 #include <linux/string.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/errno.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>
36 #include <net/protocol.h>
37 #include <net/route.h>
40 #include <net/ip_fib.h>
41 #include <net/ip6_fib.h>
42 #include <net/nexthop.h>
43 #include <net/netlink.h>
45 #include <net/lwtunnel.h>
46 #include <net/fib_notifier.h>
47 #include <net/addrconf.h>
49 #include "fib_lookup.h"
51 static DEFINE_SPINLOCK(fib_info_lock);
52 static struct hlist_head *fib_info_hash;
53 static struct hlist_head *fib_info_laddrhash;
54 static unsigned int fib_info_hash_size;
55 static unsigned int fib_info_cnt;
57 #define DEVINDEX_HASHBITS 8
58 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
59 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
61 /* for_nexthops and change_nexthops only used when nexthop object
62 * is not set in a fib_info. The logic within can reference fib_nh.
64 #ifdef CONFIG_IP_ROUTE_MULTIPATH
66 #define for_nexthops(fi) { \
67 int nhsel; const struct fib_nh *nh; \
68 for (nhsel = 0, nh = (fi)->fib_nh; \
69 nhsel < fib_info_num_path((fi)); \
72 #define change_nexthops(fi) { \
73 int nhsel; struct fib_nh *nexthop_nh; \
74 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
75 nhsel < fib_info_num_path((fi)); \
76 nexthop_nh++, nhsel++)
78 #else /* CONFIG_IP_ROUTE_MULTIPATH */
80 /* Hope, that gcc will optimize it to get rid of dummy loop */
82 #define for_nexthops(fi) { \
83 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
84 for (nhsel = 0; nhsel < 1; nhsel++)
86 #define change_nexthops(fi) { \
88 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
89 for (nhsel = 0; nhsel < 1; nhsel++)
91 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
93 #define endfor_nexthops(fi) }
96 const struct fib_prop fib_props[RTN_MAX + 1] = {
99 .scope = RT_SCOPE_NOWHERE,
103 .scope = RT_SCOPE_UNIVERSE,
107 .scope = RT_SCOPE_HOST,
111 .scope = RT_SCOPE_LINK,
115 .scope = RT_SCOPE_LINK,
119 .scope = RT_SCOPE_UNIVERSE,
123 .scope = RT_SCOPE_UNIVERSE,
125 [RTN_UNREACHABLE] = {
126 .error = -EHOSTUNREACH,
127 .scope = RT_SCOPE_UNIVERSE,
131 .scope = RT_SCOPE_UNIVERSE,
135 .scope = RT_SCOPE_UNIVERSE,
139 .scope = RT_SCOPE_NOWHERE,
143 .scope = RT_SCOPE_NOWHERE,
147 static void rt_fibinfo_free(struct rtable __rcu **rtp)
149 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
154 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
155 * because we waited an RCU grace period before calling
156 * free_fib_info_rcu()
159 dst_dev_put(&rt->dst);
160 dst_release_immediate(&rt->dst);
163 static void free_nh_exceptions(struct fib_nh_common *nhc)
165 struct fnhe_hash_bucket *hash;
168 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
171 for (i = 0; i < FNHE_HASH_SIZE; i++) {
172 struct fib_nh_exception *fnhe;
174 fnhe = rcu_dereference_protected(hash[i].chain, 1);
176 struct fib_nh_exception *next;
178 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
180 rt_fibinfo_free(&fnhe->fnhe_rth_input);
181 rt_fibinfo_free(&fnhe->fnhe_rth_output);
191 static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
198 for_each_possible_cpu(cpu) {
201 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
203 dst_dev_put(&rt->dst);
204 dst_release_immediate(&rt->dst);
210 void fib_nh_common_release(struct fib_nh_common *nhc)
212 dev_put(nhc->nhc_dev);
213 lwtstate_put(nhc->nhc_lwtstate);
214 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
215 rt_fibinfo_free(&nhc->nhc_rth_input);
216 free_nh_exceptions(nhc);
218 EXPORT_SYMBOL_GPL(fib_nh_common_release);
220 void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
222 #ifdef CONFIG_IP_ROUTE_CLASSID
223 if (fib_nh->nh_tclassid)
224 atomic_dec(&net->ipv4.fib_num_tclassid_users);
226 fib_nh_common_release(&fib_nh->nh_common);
229 /* Release a nexthop info record */
230 static void free_fib_info_rcu(struct rcu_head *head)
232 struct fib_info *fi = container_of(head, struct fib_info, rcu);
237 change_nexthops(fi) {
238 fib_nh_release(fi->fib_net, nexthop_nh);
239 } endfor_nexthops(fi);
242 ip_fib_metrics_put(fi->fib_metrics);
247 void free_fib_info(struct fib_info *fi)
249 if (fi->fib_dead == 0) {
250 pr_warn("Freeing alive fib_info %p\n", fi);
254 call_rcu(&fi->rcu, free_fib_info_rcu);
256 EXPORT_SYMBOL_GPL(free_fib_info);
258 void fib_release_info(struct fib_info *fi)
260 spin_lock_bh(&fib_info_lock);
261 if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
262 hlist_del(&fi->fib_hash);
264 /* Paired with READ_ONCE() in fib_create_info(). */
265 WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
268 hlist_del(&fi->fib_lhash);
270 list_del(&fi->nh_list);
272 change_nexthops(fi) {
273 if (!nexthop_nh->fib_nh_dev)
275 hlist_del(&nexthop_nh->nh_hash);
276 } endfor_nexthops(fi)
281 spin_unlock_bh(&fib_info_lock);
284 static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
286 const struct fib_nh *onh;
288 if (fi->nh || ofi->nh)
289 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
291 if (ofi->fib_nhs == 0)
295 onh = fib_info_nh(ofi, nhsel);
297 if (nh->fib_nh_oif != onh->fib_nh_oif ||
298 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
299 nh->fib_nh_scope != onh->fib_nh_scope ||
300 #ifdef CONFIG_IP_ROUTE_MULTIPATH
301 nh->fib_nh_weight != onh->fib_nh_weight ||
303 #ifdef CONFIG_IP_ROUTE_CLASSID
304 nh->nh_tclassid != onh->nh_tclassid ||
306 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
307 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
310 if (nh->fib_nh_gw_family == AF_INET &&
311 nh->fib_nh_gw4 != onh->fib_nh_gw4)
314 if (nh->fib_nh_gw_family == AF_INET6 &&
315 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
317 } endfor_nexthops(fi);
321 static inline unsigned int fib_devindex_hashfn(unsigned int val)
323 return hash_32(val, DEVINDEX_HASHBITS);
326 static struct hlist_head *
327 fib_info_devhash_bucket(const struct net_device *dev)
329 u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
331 return &fib_info_devhash[fib_devindex_hashfn(val)];
334 static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
335 u32 prefsrc, u32 priority)
337 unsigned int val = init_val;
339 val ^= (protocol << 8) | scope;
346 static unsigned int fib_info_hashfn_result(unsigned int val)
348 unsigned int mask = (fib_info_hash_size - 1);
350 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
353 static inline unsigned int fib_info_hashfn(struct fib_info *fi)
357 val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
358 fi->fib_scope, (__force u32)fi->fib_prefsrc,
362 val ^= fib_devindex_hashfn(fi->nh->id);
365 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
366 } endfor_nexthops(fi)
369 return fib_info_hashfn_result(val);
372 /* no metrics, only nexthop id */
373 static struct fib_info *fib_find_info_nh(struct net *net,
374 const struct fib_config *cfg)
376 struct hlist_head *head;
380 hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
381 cfg->fc_protocol, cfg->fc_scope,
382 (__force u32)cfg->fc_prefsrc,
384 hash = fib_info_hashfn_result(hash);
385 head = &fib_info_hash[hash];
387 hlist_for_each_entry(fi, head, fib_hash) {
388 if (!net_eq(fi->fib_net, net))
390 if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
392 if (cfg->fc_protocol == fi->fib_protocol &&
393 cfg->fc_scope == fi->fib_scope &&
394 cfg->fc_prefsrc == fi->fib_prefsrc &&
395 cfg->fc_priority == fi->fib_priority &&
396 cfg->fc_type == fi->fib_type &&
397 cfg->fc_table == fi->fib_tb_id &&
398 !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
405 static struct fib_info *fib_find_info(struct fib_info *nfi)
407 struct hlist_head *head;
411 hash = fib_info_hashfn(nfi);
412 head = &fib_info_hash[hash];
414 hlist_for_each_entry(fi, head, fib_hash) {
415 if (!net_eq(fi->fib_net, nfi->fib_net))
417 if (fi->fib_nhs != nfi->fib_nhs)
419 if (nfi->fib_protocol == fi->fib_protocol &&
420 nfi->fib_scope == fi->fib_scope &&
421 nfi->fib_prefsrc == fi->fib_prefsrc &&
422 nfi->fib_priority == fi->fib_priority &&
423 nfi->fib_type == fi->fib_type &&
424 nfi->fib_tb_id == fi->fib_tb_id &&
425 memcmp(nfi->fib_metrics, fi->fib_metrics,
426 sizeof(u32) * RTAX_MAX) == 0 &&
427 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
428 nh_comp(fi, nfi) == 0)
435 /* Check, that the gateway is already configured.
436 * Used only by redirect accept routine.
438 int ip_fib_check_default(__be32 gw, struct net_device *dev)
440 struct hlist_head *head;
443 spin_lock(&fib_info_lock);
445 head = fib_info_devhash_bucket(dev);
447 hlist_for_each_entry(nh, head, nh_hash) {
448 if (nh->fib_nh_dev == dev &&
449 nh->fib_nh_gw4 == gw &&
450 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
451 spin_unlock(&fib_info_lock);
456 spin_unlock(&fib_info_lock);
461 size_t fib_nlmsg_size(struct fib_info *fi)
463 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
464 + nla_total_size(4) /* RTA_TABLE */
465 + nla_total_size(4) /* RTA_DST */
466 + nla_total_size(4) /* RTA_PRIORITY */
467 + nla_total_size(4) /* RTA_PREFSRC */
468 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
469 unsigned int nhs = fib_info_num_path(fi);
471 /* space for nested metrics */
472 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
475 payload += nla_total_size(4); /* RTA_NH_ID */
478 size_t nh_encapsize = 0;
479 /* Also handles the special case nhs == 1 */
481 /* each nexthop is packed in an attribute */
482 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
485 /* may contain flow and gateway attribute */
486 nhsize += 2 * nla_total_size(4);
488 /* grab encap info */
489 for (i = 0; i < fib_info_num_path(fi); i++) {
490 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
492 if (nhc->nhc_lwtstate) {
494 nh_encapsize += lwtunnel_get_encap_size(
497 nh_encapsize += nla_total_size(2);
501 /* all nexthops are packed in a nested attribute */
502 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
509 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
510 int dst_len, u32 tb_id, const struct nl_info *info,
511 unsigned int nlm_flags)
513 struct fib_rt_info fri;
515 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
518 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
522 fri.fi = fa->fa_info;
525 fri.dst_len = dst_len;
526 fri.tos = fa->fa_tos;
527 fri.type = fa->fa_type;
528 fri.offload = READ_ONCE(fa->offload);
529 fri.trap = READ_ONCE(fa->trap);
530 fri.offload_failed = READ_ONCE(fa->offload_failed);
531 err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
533 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
534 WARN_ON(err == -EMSGSIZE);
538 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
539 info->nlh, GFP_KERNEL);
543 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
546 static int fib_detect_death(struct fib_info *fi, int order,
547 struct fib_info **last_resort, int *last_idx,
550 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
552 int state = NUD_NONE;
554 if (likely(nhc->nhc_gw_family == AF_INET))
555 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
556 else if (nhc->nhc_gw_family == AF_INET6)
557 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
563 state = n->nud_state;
568 if (state == NUD_REACHABLE)
570 if ((state & NUD_VALID) && order != dflt)
572 if ((state & NUD_VALID) ||
573 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
580 int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
581 struct nlattr *encap, u16 encap_type,
582 void *cfg, gfp_t gfp_flags,
583 struct netlink_ext_ack *extack)
587 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
589 if (!nhc->nhc_pcpu_rth_output)
593 struct lwtunnel_state *lwtstate;
595 if (encap_type == LWTUNNEL_ENCAP_NONE) {
596 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
600 err = lwtunnel_build_state(net, encap_type, encap,
601 nhc->nhc_family, cfg, &lwtstate,
606 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
612 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
613 nhc->nhc_pcpu_rth_output = NULL;
616 EXPORT_SYMBOL_GPL(fib_nh_common_init);
618 int fib_nh_init(struct net *net, struct fib_nh *nh,
619 struct fib_config *cfg, int nh_weight,
620 struct netlink_ext_ack *extack)
624 nh->fib_nh_family = AF_INET;
626 err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
627 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
631 nh->fib_nh_oif = cfg->fc_oif;
632 nh->fib_nh_gw_family = cfg->fc_gw_family;
633 if (cfg->fc_gw_family == AF_INET)
634 nh->fib_nh_gw4 = cfg->fc_gw4;
635 else if (cfg->fc_gw_family == AF_INET6)
636 nh->fib_nh_gw6 = cfg->fc_gw6;
638 nh->fib_nh_flags = cfg->fc_flags;
640 #ifdef CONFIG_IP_ROUTE_CLASSID
641 nh->nh_tclassid = cfg->fc_flow;
643 atomic_inc(&net->ipv4.fib_num_tclassid_users);
645 #ifdef CONFIG_IP_ROUTE_MULTIPATH
646 nh->fib_nh_weight = nh_weight;
651 #ifdef CONFIG_IP_ROUTE_MULTIPATH
653 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
654 struct netlink_ext_ack *extack)
658 while (rtnh_ok(rtnh, remaining)) {
660 rtnh = rtnh_next(rtnh, &remaining);
663 /* leftover implies invalid nexthop configuration, discard it */
665 NL_SET_ERR_MSG(extack,
666 "Invalid nexthop configuration - extra data after nexthops");
673 static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
674 struct netlink_ext_ack *extack)
676 if (nla_len(nla) < sizeof(*gw)) {
677 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
681 *gw = nla_get_in_addr(nla);
686 /* only called when fib_nh is integrated into fib_info */
687 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
688 int remaining, struct fib_config *cfg,
689 struct netlink_ext_ack *extack)
691 struct net *net = fi->fib_net;
692 struct fib_config fib_cfg;
696 change_nexthops(fi) {
699 memset(&fib_cfg, 0, sizeof(fib_cfg));
701 if (!rtnh_ok(rtnh, remaining)) {
702 NL_SET_ERR_MSG(extack,
703 "Invalid nexthop configuration - extra data after nexthop");
707 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
708 NL_SET_ERR_MSG(extack,
709 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
713 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
714 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
716 attrlen = rtnh_attrlen(rtnh);
718 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
720 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
721 nlav = nla_find(attrs, attrlen, RTA_VIA);
723 NL_SET_ERR_MSG(extack,
724 "Nexthop configuration can not contain both GATEWAY and VIA");
728 ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
734 fib_cfg.fc_gw_family = AF_INET;
736 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
741 nla = nla_find(attrs, attrlen, RTA_FLOW);
743 if (nla_len(nla) < sizeof(u32)) {
744 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
747 fib_cfg.fc_flow = nla_get_u32(nla);
750 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
751 /* RTA_ENCAP_TYPE length checked in
752 * lwtunnel_valid_encap_type_attr
754 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
756 fib_cfg.fc_encap_type = nla_get_u16(nla);
759 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
760 rtnh->rtnh_hops + 1, extack);
764 rtnh = rtnh_next(rtnh, &remaining);
765 } endfor_nexthops(fi);
768 nh = fib_info_nh(fi, 0);
769 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
770 NL_SET_ERR_MSG(extack,
771 "Nexthop device index does not match RTA_OIF");
774 if (cfg->fc_gw_family) {
775 if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
776 (cfg->fc_gw_family == AF_INET &&
777 nh->fib_nh_gw4 != cfg->fc_gw4) ||
778 (cfg->fc_gw_family == AF_INET6 &&
779 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
780 NL_SET_ERR_MSG(extack,
781 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
785 #ifdef CONFIG_IP_ROUTE_CLASSID
786 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
787 NL_SET_ERR_MSG(extack,
788 "Nexthop class id does not match RTA_FLOW");
797 /* only called when fib_nh is integrated into fib_info */
798 static void fib_rebalance(struct fib_info *fi)
803 if (fib_info_num_path(fi) < 2)
808 if (nh->fib_nh_flags & RTNH_F_DEAD)
811 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
812 nh->fib_nh_flags & RTNH_F_LINKDOWN)
815 total += nh->fib_nh_weight;
816 } endfor_nexthops(fi);
819 change_nexthops(fi) {
822 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
824 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
825 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
828 w += nexthop_nh->fib_nh_weight;
829 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
833 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
834 } endfor_nexthops(fi);
836 #else /* CONFIG_IP_ROUTE_MULTIPATH */
838 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
839 int remaining, struct fib_config *cfg,
840 struct netlink_ext_ack *extack)
842 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
847 #define fib_rebalance(fi) do { } while (0)
849 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
851 static int fib_encap_match(struct net *net, u16 encap_type,
852 struct nlattr *encap,
853 const struct fib_nh *nh,
854 const struct fib_config *cfg,
855 struct netlink_ext_ack *extack)
857 struct lwtunnel_state *lwtstate;
860 if (encap_type == LWTUNNEL_ENCAP_NONE)
863 ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
864 cfg, &lwtstate, extack);
866 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
867 lwtstate_free(lwtstate);
873 int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
874 struct netlink_ext_ack *extack)
876 #ifdef CONFIG_IP_ROUTE_MULTIPATH
877 struct rtnexthop *rtnh;
881 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
885 if (fi->nh && cfg->fc_nh_id == fi->nh->id)
891 if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
896 if (cfg->fc_oif || cfg->fc_gw_family) {
899 nh = fib_info_nh(fi, 0);
901 if (fib_encap_match(net, cfg->fc_encap_type,
902 cfg->fc_encap, nh, cfg, extack))
905 #ifdef CONFIG_IP_ROUTE_CLASSID
907 cfg->fc_flow != nh->nh_tclassid)
910 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
911 (cfg->fc_gw_family &&
912 cfg->fc_gw_family != nh->fib_nh_gw_family))
915 if (cfg->fc_gw_family == AF_INET &&
916 cfg->fc_gw4 != nh->fib_nh_gw4)
919 if (cfg->fc_gw_family == AF_INET6 &&
920 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
926 #ifdef CONFIG_IP_ROUTE_MULTIPATH
931 remaining = cfg->fc_mp_len;
936 if (!rtnh_ok(rtnh, remaining))
939 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
942 attrlen = rtnh_attrlen(rtnh);
944 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
947 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
948 nlav = nla_find(attrs, attrlen, RTA_VIA);
950 NL_SET_ERR_MSG(extack,
951 "Nexthop configuration can not contain both GATEWAY and VIA");
958 err = fib_gw_from_attr(&gw, nla, extack);
962 if (nh->fib_nh_gw_family != AF_INET ||
963 gw != nh->fib_nh_gw4)
966 struct fib_config cfg2;
968 err = fib_gw_from_via(&cfg2, nlav, extack);
972 switch (nh->fib_nh_gw_family) {
974 if (cfg2.fc_gw_family != AF_INET ||
975 cfg2.fc_gw4 != nh->fib_nh_gw4)
979 if (cfg2.fc_gw_family != AF_INET6 ||
980 ipv6_addr_cmp(&cfg2.fc_gw6,
987 #ifdef CONFIG_IP_ROUTE_CLASSID
988 nla = nla_find(attrs, attrlen, RTA_FLOW);
990 if (nla_len(nla) < sizeof(u32)) {
991 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
994 if (nla_get_u32(nla) != nh->nh_tclassid)
1000 rtnh = rtnh_next(rtnh, &remaining);
1001 } endfor_nexthops(fi);
1006 bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
1014 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1015 int type = nla_type(nla);
1020 if (type > RTAX_MAX)
1023 if (type == RTAX_CC_ALGO) {
1024 char tmp[TCP_CA_NAME_MAX];
1025 bool ecn_ca = false;
1027 nla_strscpy(tmp, nla, sizeof(tmp));
1028 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
1030 if (nla_len(nla) != sizeof(u32))
1032 val = nla_get_u32(nla);
1035 fi_val = fi->fib_metrics->metrics[type - 1];
1036 if (type == RTAX_FEATURES)
1037 fi_val &= ~DST_FEATURE_ECN_CA;
1046 static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1047 u32 table, struct netlink_ext_ack *extack)
1049 struct fib6_config cfg = {
1051 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1052 .fc_ifindex = nh->fib_nh_oif,
1053 .fc_gateway = nh->fib_nh_gw6,
1055 struct fib6_nh fib6_nh = {};
1058 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1060 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1061 dev_hold(nh->fib_nh_dev);
1062 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1063 nh->fib_nh_scope = RT_SCOPE_LINK;
1065 ipv6_stub->fib6_nh_release(&fib6_nh);
1075 * Semantics of nexthop is very messy by historical reasons.
1076 * We have to take into account, that:
1077 * a) gateway can be actually local interface address,
1078 * so that gatewayed route is direct.
1079 * b) gateway must be on-link address, possibly
1080 * described not by an ifaddr, but also by a direct route.
1081 * c) If both gateway and interface are specified, they should not
1083 * d) If we use tunnel routes, gateway could be not on-link.
1085 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1086 * results in pretty ugly and hairy code with obscure logic.
1088 * I chose to generalized it instead, so that the size
1089 * of code does not increase practically, but it becomes
1090 * much more general.
1091 * Every prefix is assigned a "scope" value: "host" is local address,
1092 * "link" is direct route,
1093 * [ ... "site" ... "interior" ... ]
1094 * and "universe" is true gateway route with global meaning.
1096 * Every prefix refers to a set of "nexthop"s (gw, oif),
1097 * where gw must have narrower scope. This recursion stops
1098 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1099 * which means that gw is forced to be on link.
1101 * Code is still hairy, but now it is apparently logically
1102 * consistent and very flexible. F.e. as by-product it allows
1103 * to co-exists in peace independent exterior and interior
1104 * routing processes.
1106 * Normally it looks as following.
1108 * {universe prefix} -> (gw, oif) [scope link]
1110 * |-> {link prefix} -> (gw, oif) [scope local]
1112 * |-> {local prefix} (terminal node)
1114 static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1115 u8 scope, struct netlink_ext_ack *extack)
1117 struct net_device *dev;
1118 struct fib_result res;
1121 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1122 unsigned int addr_type;
1124 if (scope >= RT_SCOPE_LINK) {
1125 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1128 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1130 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1133 if (!(dev->flags & IFF_UP)) {
1134 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1137 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1138 if (addr_type != RTN_UNICAST) {
1139 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1142 if (!netif_carrier_ok(dev))
1143 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1144 nh->fib_nh_dev = dev;
1146 nh->fib_nh_scope = RT_SCOPE_LINK;
1151 struct fib_table *tbl = NULL;
1152 struct flowi4 fl4 = {
1153 .daddr = nh->fib_nh_gw4,
1154 .flowi4_scope = scope + 1,
1155 .flowi4_oif = nh->fib_nh_oif,
1156 .flowi4_iif = LOOPBACK_IFINDEX,
1159 /* It is not necessary, but requires a bit of thinking */
1160 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1161 fl4.flowi4_scope = RT_SCOPE_LINK;
1163 if (table && table != RT_TABLE_MAIN)
1164 tbl = fib_get_table(net, table);
1167 err = fib_table_lookup(tbl, &fl4, &res,
1168 FIB_LOOKUP_IGNORE_LINKSTATE |
1171 /* on error or if no table given do full lookup. This
1172 * is needed for example when nexthops are in the local
1173 * table rather than the given table
1176 err = fib_lookup(net, &fl4, &res,
1177 FIB_LOOKUP_IGNORE_LINKSTATE);
1181 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1187 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1188 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1191 nh->fib_nh_scope = res.scope;
1192 nh->fib_nh_oif = FIB_RES_OIF(res);
1193 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1195 NL_SET_ERR_MSG(extack,
1196 "No egress device for nexthop gateway");
1200 if (!netif_carrier_ok(dev))
1201 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1202 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1208 static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1209 struct netlink_ext_ack *extack)
1211 struct in_device *in_dev;
1214 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1215 NL_SET_ERR_MSG(extack,
1216 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1223 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1227 if (!(in_dev->dev->flags & IFF_UP)) {
1228 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1232 nh->fib_nh_dev = in_dev->dev;
1233 dev_hold(nh->fib_nh_dev);
1234 nh->fib_nh_scope = RT_SCOPE_LINK;
1235 if (!netif_carrier_ok(nh->fib_nh_dev))
1236 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1243 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1244 struct netlink_ext_ack *extack)
1248 if (nh->fib_nh_gw_family == AF_INET)
1249 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1250 else if (nh->fib_nh_gw_family == AF_INET6)
1251 err = fib_check_nh_v6_gw(net, nh, table, extack);
1253 err = fib_check_nh_nongw(net, nh, extack);
1258 static inline unsigned int fib_laddr_hashfn(__be32 val)
1260 unsigned int mask = (fib_info_hash_size - 1);
1262 return ((__force u32)val ^
1263 ((__force u32)val >> 7) ^
1264 ((__force u32)val >> 14)) & mask;
1267 static struct hlist_head *fib_info_hash_alloc(int bytes)
1269 if (bytes <= PAGE_SIZE)
1270 return kzalloc(bytes, GFP_KERNEL);
1272 return (struct hlist_head *)
1273 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1277 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1282 if (bytes <= PAGE_SIZE)
1285 free_pages((unsigned long) hash, get_order(bytes));
1288 static void fib_info_hash_move(struct hlist_head *new_info_hash,
1289 struct hlist_head *new_laddrhash,
1290 unsigned int new_size)
1292 struct hlist_head *old_info_hash, *old_laddrhash;
1293 unsigned int old_size = fib_info_hash_size;
1294 unsigned int i, bytes;
1296 spin_lock_bh(&fib_info_lock);
1297 old_info_hash = fib_info_hash;
1298 old_laddrhash = fib_info_laddrhash;
1299 fib_info_hash_size = new_size;
1301 for (i = 0; i < old_size; i++) {
1302 struct hlist_head *head = &fib_info_hash[i];
1303 struct hlist_node *n;
1304 struct fib_info *fi;
1306 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1307 struct hlist_head *dest;
1308 unsigned int new_hash;
1310 new_hash = fib_info_hashfn(fi);
1311 dest = &new_info_hash[new_hash];
1312 hlist_add_head(&fi->fib_hash, dest);
1315 fib_info_hash = new_info_hash;
1317 for (i = 0; i < old_size; i++) {
1318 struct hlist_head *lhead = &fib_info_laddrhash[i];
1319 struct hlist_node *n;
1320 struct fib_info *fi;
1322 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1323 struct hlist_head *ldest;
1324 unsigned int new_hash;
1326 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1327 ldest = &new_laddrhash[new_hash];
1328 hlist_add_head(&fi->fib_lhash, ldest);
1331 fib_info_laddrhash = new_laddrhash;
1333 spin_unlock_bh(&fib_info_lock);
1335 bytes = old_size * sizeof(struct hlist_head *);
1336 fib_info_hash_free(old_info_hash, bytes);
1337 fib_info_hash_free(old_laddrhash, bytes);
1340 __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1341 unsigned char scope)
1345 if (nhc->nhc_family != AF_INET)
1346 return inet_select_addr(nhc->nhc_dev, 0, scope);
1348 nh = container_of(nhc, struct fib_nh, nh_common);
1349 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1350 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1352 return nh->nh_saddr;
1355 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1357 struct fib_nh_common *nhc = res->nhc;
1359 if (res->fi->fib_prefsrc)
1360 return res->fi->fib_prefsrc;
1362 if (nhc->nhc_family == AF_INET) {
1365 nh = container_of(nhc, struct fib_nh, nh_common);
1366 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1367 return nh->nh_saddr;
1370 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1373 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1375 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1376 fib_prefsrc != cfg->fc_dst) {
1377 u32 tb_id = cfg->fc_table;
1380 if (tb_id == RT_TABLE_MAIN)
1381 tb_id = RT_TABLE_LOCAL;
1383 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1384 fib_prefsrc, tb_id);
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);
1391 if (rc != RTN_LOCAL)
1397 struct fib_info *fib_create_info(struct fib_config *cfg,
1398 struct netlink_ext_ack *extack)
1401 struct fib_info *fi = NULL;
1402 struct nexthop *nh = NULL;
1403 struct fib_info *ofi;
1405 struct net *net = cfg->fc_nlinfo.nl_net;
1407 if (cfg->fc_type > RTN_MAX)
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");
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");
1422 if (cfg->fc_nh_id) {
1424 fi = fib_find_info_nh(net, cfg);
1426 refcount_inc(&fi->fib_treeref);
1431 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1433 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1439 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1441 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1449 /* Paired with WRITE_ONCE() in fib_release_info() */
1450 if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
1451 unsigned int new_size = fib_info_hash_size << 1;
1452 struct hlist_head *new_info_hash;
1453 struct hlist_head *new_laddrhash;
1458 bytes = new_size * sizeof(struct hlist_head *);
1459 new_info_hash = fib_info_hash_alloc(bytes);
1460 new_laddrhash = fib_info_hash_alloc(bytes);
1461 if (!new_info_hash || !new_laddrhash) {
1462 fib_info_hash_free(new_info_hash, bytes);
1463 fib_info_hash_free(new_laddrhash, bytes);
1465 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1467 if (!fib_info_hash_size)
1471 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1474 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1475 cfg->fc_mx_len, extack);
1476 if (IS_ERR(fi->fib_metrics)) {
1477 err = PTR_ERR(fi->fib_metrics);
1479 return ERR_PTR(err);
1483 fi->fib_protocol = cfg->fc_protocol;
1484 fi->fib_scope = cfg->fc_scope;
1485 fi->fib_flags = cfg->fc_flags;
1486 fi->fib_priority = cfg->fc_priority;
1487 fi->fib_prefsrc = cfg->fc_prefsrc;
1488 fi->fib_type = cfg->fc_type;
1489 fi->fib_tb_id = cfg->fc_table;
1493 if (!nexthop_get(nh)) {
1494 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1501 change_nexthops(fi) {
1502 nexthop_nh->nh_parent = fi;
1503 } endfor_nexthops(fi)
1506 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1509 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1515 if (fib_props[cfg->fc_type].error) {
1516 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1517 NL_SET_ERR_MSG(extack,
1518 "Gateway, device and multipath can not be specified for this route type");
1523 switch (cfg->fc_type) {
1531 NL_SET_ERR_MSG(extack, "Invalid route type");
1536 if (cfg->fc_scope > RT_SCOPE_HOST) {
1537 NL_SET_ERR_MSG(extack, "Invalid scope");
1542 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1545 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
1546 struct fib_nh *nh = fi->fib_nh;
1548 /* Local address is added. */
1550 NL_SET_ERR_MSG(extack,
1551 "Route with host scope can not have multiple nexthops");
1554 if (nh->fib_nh_gw_family) {
1555 NL_SET_ERR_MSG(extack,
1556 "Route with host scope can not have a gateway");
1559 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1560 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
1562 if (!nh->fib_nh_dev)
1567 change_nexthops(fi) {
1568 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1569 cfg->fc_table, cfg->fc_scope,
1573 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1575 } endfor_nexthops(fi)
1576 if (linkdown == fi->fib_nhs)
1577 fi->fib_flags |= RTNH_F_LINKDOWN;
1580 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1581 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1586 change_nexthops(fi) {
1587 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1589 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1590 fi->fib_nh_is_v6 = true;
1591 } endfor_nexthops(fi)
1597 ofi = fib_find_info(fi);
1601 refcount_inc(&ofi->fib_treeref);
1605 refcount_set(&fi->fib_treeref, 1);
1606 refcount_set(&fi->fib_clntref, 1);
1607 spin_lock_bh(&fib_info_lock);
1609 hlist_add_head(&fi->fib_hash,
1610 &fib_info_hash[fib_info_hashfn(fi)]);
1611 if (fi->fib_prefsrc) {
1612 struct hlist_head *head;
1614 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1615 hlist_add_head(&fi->fib_lhash, head);
1618 list_add(&fi->nh_list, &nh->fi_list);
1620 change_nexthops(fi) {
1621 struct hlist_head *head;
1623 if (!nexthop_nh->fib_nh_dev)
1625 head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
1626 hlist_add_head(&nexthop_nh->nh_hash, head);
1627 } endfor_nexthops(fi)
1629 spin_unlock_bh(&fib_info_lock);
1641 return ERR_PTR(err);
1644 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1645 u8 rt_family, unsigned char *flags, bool skip_oif)
1647 if (nhc->nhc_flags & RTNH_F_DEAD)
1648 *flags |= RTNH_F_DEAD;
1650 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1651 *flags |= RTNH_F_LINKDOWN;
1654 switch (nhc->nhc_family) {
1656 if (ip_ignore_linkdown(nhc->nhc_dev))
1657 *flags |= RTNH_F_DEAD;
1660 if (ip6_ignore_linkdown(nhc->nhc_dev))
1661 *flags |= RTNH_F_DEAD;
1667 switch (nhc->nhc_gw_family) {
1669 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1670 goto nla_put_failure;
1673 /* if gateway family does not match nexthop family
1674 * gateway is encoded as RTA_VIA
1676 if (rt_family != nhc->nhc_gw_family) {
1677 int alen = sizeof(struct in6_addr);
1681 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1683 goto nla_put_failure;
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;
1695 *flags |= (nhc->nhc_flags &
1696 (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
1698 if (!skip_oif && nhc->nhc_dev &&
1699 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1700 goto nla_put_failure;
1702 if (nhc->nhc_lwtstate &&
1703 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1704 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1705 goto nla_put_failure;
1712 EXPORT_SYMBOL_GPL(fib_nexthop_info);
1714 #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1715 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1716 int nh_weight, u8 rt_family, u32 nh_tclassid)
1718 const struct net_device *dev = nhc->nhc_dev;
1719 struct rtnexthop *rtnh;
1720 unsigned char flags = 0;
1722 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1724 goto nla_put_failure;
1726 rtnh->rtnh_hops = nh_weight - 1;
1727 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1729 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1730 goto nla_put_failure;
1732 rtnh->rtnh_flags = flags;
1734 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1735 goto nla_put_failure;
1737 /* length of rtnetlink header + attributes */
1738 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1745 EXPORT_SYMBOL_GPL(fib_add_nexthop);
1748 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1749 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1753 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1755 goto nla_put_failure;
1757 if (unlikely(fi->nh)) {
1758 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1759 goto nla_put_failure;
1764 u32 nh_tclassid = 0;
1765 #ifdef CONFIG_IP_ROUTE_CLASSID
1766 nh_tclassid = nh->nh_tclassid;
1768 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1769 AF_INET, nh_tclassid) < 0)
1770 goto nla_put_failure;
1771 } endfor_nexthops(fi);
1774 nla_nest_end(skb, mp);
1782 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1788 int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1789 const struct fib_rt_info *fri, unsigned int flags)
1791 unsigned int nhs = fib_info_num_path(fri->fi);
1792 struct fib_info *fi = fri->fi;
1793 u32 tb_id = fri->tb_id;
1794 struct nlmsghdr *nlh;
1797 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1801 rtm = nlmsg_data(nlh);
1802 rtm->rtm_family = AF_INET;
1803 rtm->rtm_dst_len = fri->dst_len;
1804 rtm->rtm_src_len = 0;
1805 rtm->rtm_tos = fri->tos;
1807 rtm->rtm_table = tb_id;
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 = fri->type;
1813 rtm->rtm_flags = fi->fib_flags;
1814 rtm->rtm_scope = fi->fib_scope;
1815 rtm->rtm_protocol = fi->fib_protocol;
1817 if (rtm->rtm_dst_len &&
1818 nla_put_in_addr(skb, RTA_DST, fri->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;
1826 if (fi->fib_prefsrc &&
1827 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1828 goto nla_put_failure;
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 if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
1840 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1841 unsigned char flags = 0;
1843 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1844 goto nla_put_failure;
1846 rtm->rtm_flags = flags;
1847 #ifdef CONFIG_IP_ROUTE_CLASSID
1848 if (nhc->nhc_family == AF_INET) {
1851 nh = container_of(nhc, struct fib_nh, nh_common);
1852 if (nh->nh_tclassid &&
1853 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1854 goto nla_put_failure;
1858 if (fib_add_multipath(skb, fi) < 0)
1859 goto nla_put_failure;
1864 rtm->rtm_flags |= RTM_F_OFFLOAD;
1866 rtm->rtm_flags |= RTM_F_TRAP;
1867 if (fri->offload_failed)
1868 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
1870 nlmsg_end(skb, nlh);
1874 nlmsg_cancel(skb, nlh);
1880 * - local address disappeared -> we must delete all the entries
1882 * - device went down -> we must shutdown all nexthops going via it.
1884 int fib_sync_down_addr(struct net_device *dev, __be32 local)
1887 unsigned int hash = fib_laddr_hashfn(local);
1888 struct hlist_head *head = &fib_info_laddrhash[hash];
1889 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
1890 struct net *net = dev_net(dev);
1891 struct fib_info *fi;
1893 if (!fib_info_laddrhash || local == 0)
1896 hlist_for_each_entry(fi, head, fib_lhash) {
1897 if (!net_eq(fi->fib_net, net) ||
1898 fi->fib_tb_id != tb_id)
1900 if (fi->fib_prefsrc == local) {
1901 fi->fib_flags |= RTNH_F_DEAD;
1908 static int call_fib_nh_notifiers(struct fib_nh *nh,
1909 enum fib_event_type event_type)
1911 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1912 struct fib_nh_notifier_info info = {
1916 switch (event_type) {
1917 case FIB_EVENT_NH_ADD:
1918 if (nh->fib_nh_flags & RTNH_F_DEAD)
1920 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1922 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1924 case FIB_EVENT_NH_DEL:
1925 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1926 (nh->fib_nh_flags & RTNH_F_DEAD))
1927 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1928 event_type, &info.info);
1937 /* Update the PMTU of exceptions when:
1938 * - the new MTU of the first hop becomes smaller than the PMTU
1939 * - the old MTU was the same as the PMTU, and it limited discovery of
1940 * larger MTUs on the path. With that limit raised, we can now
1941 * discover larger MTUs
1942 * A special case is locked exceptions, for which the PMTU is smaller
1943 * than the minimal accepted PMTU:
1944 * - if the new MTU is greater than the PMTU, don't make any change
1945 * - otherwise, unlock and set PMTU
1947 void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1949 struct fnhe_hash_bucket *bucket;
1952 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1956 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1957 struct fib_nh_exception *fnhe;
1959 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1961 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1962 if (fnhe->fnhe_mtu_locked) {
1963 if (new <= fnhe->fnhe_pmtu) {
1964 fnhe->fnhe_pmtu = new;
1965 fnhe->fnhe_mtu_locked = false;
1967 } else if (new < fnhe->fnhe_pmtu ||
1968 orig == fnhe->fnhe_pmtu) {
1969 fnhe->fnhe_pmtu = new;
1975 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1977 struct hlist_head *head = fib_info_devhash_bucket(dev);
1980 hlist_for_each_entry(nh, head, nh_hash) {
1981 if (nh->fib_nh_dev == dev)
1982 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1986 /* Event force Flags Description
1987 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1988 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1989 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1990 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1992 * only used when fib_nh is built into fib_info
1994 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1996 struct hlist_head *head = fib_info_devhash_bucket(dev);
1997 struct fib_info *prev_fi = NULL;
1998 int scope = RT_SCOPE_NOWHERE;
2005 hlist_for_each_entry(nh, head, nh_hash) {
2006 struct fib_info *fi = nh->nh_parent;
2009 BUG_ON(!fi->fib_nhs);
2010 if (nh->fib_nh_dev != dev || fi == prev_fi)
2014 change_nexthops(fi) {
2015 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
2017 else if (nexthop_nh->fib_nh_dev == dev &&
2018 nexthop_nh->fib_nh_scope != scope) {
2021 case NETDEV_UNREGISTER:
2022 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
2025 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
2028 call_fib_nh_notifiers(nexthop_nh,
2032 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2033 if (event == NETDEV_UNREGISTER &&
2034 nexthop_nh->fib_nh_dev == dev) {
2039 } endfor_nexthops(fi)
2040 if (dead == fi->fib_nhs) {
2043 case NETDEV_UNREGISTER:
2044 fi->fib_flags |= RTNH_F_DEAD;
2047 fi->fib_flags |= RTNH_F_LINKDOWN;
2059 /* Must be invoked inside of an RCU protected region. */
2060 static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
2062 struct fib_info *fi = NULL, *last_resort = NULL;
2063 struct hlist_head *fa_head = res->fa_head;
2064 struct fib_table *tb = res->table;
2065 u8 slen = 32 - res->prefixlen;
2066 int order = -1, last_idx = -1;
2067 struct fib_alias *fa, *fa1 = NULL;
2068 u32 last_prio = res->fi->fib_priority;
2071 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
2072 struct fib_info *next_fi = fa->fa_info;
2073 struct fib_nh_common *nhc;
2075 if (fa->fa_slen != slen)
2077 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2079 if (fa->tb_id != tb->tb_id)
2081 if (next_fi->fib_priority > last_prio &&
2082 fa->fa_tos == last_tos) {
2087 if (next_fi->fib_flags & RTNH_F_DEAD)
2089 last_tos = fa->fa_tos;
2090 last_prio = next_fi->fib_priority;
2092 if (next_fi->fib_scope != res->scope ||
2093 fa->fa_type != RTN_UNICAST)
2096 nhc = fib_info_nhc(next_fi, 0);
2097 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
2100 fib_alias_accessed(fa);
2103 if (next_fi != res->fi)
2106 } else if (!fib_detect_death(fi, order, &last_resort,
2107 &last_idx, fa1->fa_default)) {
2108 fib_result_assign(res, fi);
2109 fa1->fa_default = order;
2116 if (order <= 0 || !fi) {
2118 fa1->fa_default = -1;
2122 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2124 fib_result_assign(res, fi);
2125 fa1->fa_default = order;
2130 fib_result_assign(res, last_resort);
2131 fa1->fa_default = last_idx;
2137 * Dead device goes up. We wake up dead nexthops.
2138 * It takes sense only on multipath routes.
2140 * only used when fib_nh is built into fib_info
2142 int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
2144 struct fib_info *prev_fi;
2145 struct hlist_head *head;
2149 if (!(dev->flags & IFF_UP))
2152 if (nh_flags & RTNH_F_DEAD) {
2153 unsigned int flags = dev_get_flags(dev);
2155 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2156 nh_flags |= RTNH_F_LINKDOWN;
2160 head = fib_info_devhash_bucket(dev);
2163 hlist_for_each_entry(nh, head, nh_hash) {
2164 struct fib_info *fi = nh->nh_parent;
2167 BUG_ON(!fi->fib_nhs);
2168 if (nh->fib_nh_dev != dev || fi == prev_fi)
2173 change_nexthops(fi) {
2174 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
2178 if (!nexthop_nh->fib_nh_dev ||
2179 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
2181 if (nexthop_nh->fib_nh_dev != dev ||
2182 !__in_dev_get_rtnl(dev))
2185 nexthop_nh->fib_nh_flags &= ~nh_flags;
2186 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
2187 } endfor_nexthops(fi)
2190 fi->fib_flags &= ~nh_flags;
2200 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2201 static bool fib_good_nh(const struct fib_nh *nh)
2203 int state = NUD_REACHABLE;
2205 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2206 struct neighbour *n;
2210 if (likely(nh->fib_nh_gw_family == AF_INET))
2211 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2212 (__force u32)nh->fib_nh_gw4);
2213 else if (nh->fib_nh_gw_family == AF_INET6)
2214 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2219 state = n->nud_state;
2221 rcu_read_unlock_bh();
2224 return !!(state & NUD_VALID);
2227 void fib_select_multipath(struct fib_result *res, int hash)
2229 struct fib_info *fi = res->fi;
2230 struct net *net = fi->fib_net;
2233 if (unlikely(res->fi->nh)) {
2234 nexthop_path_fib_result(res, hash);
2238 change_nexthops(fi) {
2239 if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
2240 if (!fib_good_nh(nexthop_nh))
2243 res->nh_sel = nhsel;
2244 res->nhc = &nexthop_nh->nh_common;
2249 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
2252 res->nh_sel = nhsel;
2253 res->nhc = &nexthop_nh->nh_common;
2255 } endfor_nexthops(fi);
2259 void fib_select_path(struct net *net, struct fib_result *res,
2260 struct flowi4 *fl4, const struct sk_buff *skb)
2262 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2265 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2266 if (fib_info_num_path(res->fi) > 1) {
2267 int h = fib_multipath_hash(net, fl4, skb, NULL);
2269 fib_select_multipath(res, h);
2273 if (!res->prefixlen &&
2274 res->table->tb_num_default > 1 &&
2275 res->type == RTN_UNICAST)
2276 fib_select_default(fl4, res);
2280 fl4->saddr = fib_result_prefsrc(net, res);