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_hash_bits;
56 static unsigned int fib_info_cnt;
58 #define DEVINDEX_HASHBITS 8
59 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
60 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
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.
65 #ifdef CONFIG_IP_ROUTE_MULTIPATH
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)); \
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++)
79 #else /* CONFIG_IP_ROUTE_MULTIPATH */
81 /* Hope, that gcc will optimize it to get rid of dummy loop */
83 #define for_nexthops(fi) { \
84 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
85 for (nhsel = 0; nhsel < 1; nhsel++)
87 #define change_nexthops(fi) { \
89 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
90 for (nhsel = 0; nhsel < 1; nhsel++)
92 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
94 #define endfor_nexthops(fi) }
97 const struct fib_prop fib_props[RTN_MAX + 1] = {
100 .scope = RT_SCOPE_NOWHERE,
104 .scope = RT_SCOPE_UNIVERSE,
108 .scope = RT_SCOPE_HOST,
112 .scope = RT_SCOPE_LINK,
116 .scope = RT_SCOPE_LINK,
120 .scope = RT_SCOPE_UNIVERSE,
124 .scope = RT_SCOPE_UNIVERSE,
126 [RTN_UNREACHABLE] = {
127 .error = -EHOSTUNREACH,
128 .scope = RT_SCOPE_UNIVERSE,
132 .scope = RT_SCOPE_UNIVERSE,
136 .scope = RT_SCOPE_UNIVERSE,
140 .scope = RT_SCOPE_NOWHERE,
144 .scope = RT_SCOPE_NOWHERE,
148 static void rt_fibinfo_free(struct rtable __rcu **rtp)
150 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
155 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
156 * because we waited an RCU grace period before calling
157 * free_fib_info_rcu()
160 dst_dev_put(&rt->dst);
161 dst_release_immediate(&rt->dst);
164 static void free_nh_exceptions(struct fib_nh_common *nhc)
166 struct fnhe_hash_bucket *hash;
169 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
172 for (i = 0; i < FNHE_HASH_SIZE; i++) {
173 struct fib_nh_exception *fnhe;
175 fnhe = rcu_dereference_protected(hash[i].chain, 1);
177 struct fib_nh_exception *next;
179 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
181 rt_fibinfo_free(&fnhe->fnhe_rth_input);
182 rt_fibinfo_free(&fnhe->fnhe_rth_output);
192 static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
199 for_each_possible_cpu(cpu) {
202 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
204 dst_dev_put(&rt->dst);
205 dst_release_immediate(&rt->dst);
211 void fib_nh_common_release(struct fib_nh_common *nhc)
213 dev_put_track(nhc->nhc_dev, &nhc->nhc_dev_tracker);
214 lwtstate_put(nhc->nhc_lwtstate);
215 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
216 rt_fibinfo_free(&nhc->nhc_rth_input);
217 free_nh_exceptions(nhc);
219 EXPORT_SYMBOL_GPL(fib_nh_common_release);
221 void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
223 #ifdef CONFIG_IP_ROUTE_CLASSID
224 if (fib_nh->nh_tclassid)
225 atomic_dec(&net->ipv4.fib_num_tclassid_users);
227 fib_nh_common_release(&fib_nh->nh_common);
230 /* Release a nexthop info record */
231 static void free_fib_info_rcu(struct rcu_head *head)
233 struct fib_info *fi = container_of(head, struct fib_info, rcu);
238 change_nexthops(fi) {
239 fib_nh_release(fi->fib_net, nexthop_nh);
240 } endfor_nexthops(fi);
243 ip_fib_metrics_put(fi->fib_metrics);
248 void free_fib_info(struct fib_info *fi)
250 if (fi->fib_dead == 0) {
251 pr_warn("Freeing alive fib_info %p\n", fi);
255 call_rcu(&fi->rcu, free_fib_info_rcu);
257 EXPORT_SYMBOL_GPL(free_fib_info);
259 void fib_release_info(struct fib_info *fi)
261 spin_lock_bh(&fib_info_lock);
262 if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
263 hlist_del(&fi->fib_hash);
265 /* Paired with READ_ONCE() in fib_create_info(). */
266 WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
269 hlist_del(&fi->fib_lhash);
271 list_del(&fi->nh_list);
273 change_nexthops(fi) {
274 if (!nexthop_nh->fib_nh_dev)
276 hlist_del(&nexthop_nh->nh_hash);
277 } endfor_nexthops(fi)
282 spin_unlock_bh(&fib_info_lock);
285 static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
287 const struct fib_nh *onh;
289 if (fi->nh || ofi->nh)
290 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
292 if (ofi->fib_nhs == 0)
296 onh = fib_info_nh(ofi, nhsel);
298 if (nh->fib_nh_oif != onh->fib_nh_oif ||
299 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
300 nh->fib_nh_scope != onh->fib_nh_scope ||
301 #ifdef CONFIG_IP_ROUTE_MULTIPATH
302 nh->fib_nh_weight != onh->fib_nh_weight ||
304 #ifdef CONFIG_IP_ROUTE_CLASSID
305 nh->nh_tclassid != onh->nh_tclassid ||
307 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
308 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
311 if (nh->fib_nh_gw_family == AF_INET &&
312 nh->fib_nh_gw4 != onh->fib_nh_gw4)
315 if (nh->fib_nh_gw_family == AF_INET6 &&
316 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
318 } endfor_nexthops(fi);
322 static inline unsigned int fib_devindex_hashfn(unsigned int val)
324 return hash_32(val, DEVINDEX_HASHBITS);
327 static struct hlist_head *
328 fib_info_devhash_bucket(const struct net_device *dev)
330 u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
332 return &fib_info_devhash[fib_devindex_hashfn(val)];
335 static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
336 u32 prefsrc, u32 priority)
338 unsigned int val = init_val;
340 val ^= (protocol << 8) | scope;
347 static unsigned int fib_info_hashfn_result(unsigned int val)
349 unsigned int mask = (fib_info_hash_size - 1);
351 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
354 static inline unsigned int fib_info_hashfn(struct fib_info *fi)
358 val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
359 fi->fib_scope, (__force u32)fi->fib_prefsrc,
363 val ^= fib_devindex_hashfn(fi->nh->id);
366 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
367 } endfor_nexthops(fi)
370 return fib_info_hashfn_result(val);
373 /* no metrics, only nexthop id */
374 static struct fib_info *fib_find_info_nh(struct net *net,
375 const struct fib_config *cfg)
377 struct hlist_head *head;
381 hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
382 cfg->fc_protocol, cfg->fc_scope,
383 (__force u32)cfg->fc_prefsrc,
385 hash = fib_info_hashfn_result(hash);
386 head = &fib_info_hash[hash];
388 hlist_for_each_entry(fi, head, fib_hash) {
389 if (!net_eq(fi->fib_net, net))
391 if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
393 if (cfg->fc_protocol == fi->fib_protocol &&
394 cfg->fc_scope == fi->fib_scope &&
395 cfg->fc_prefsrc == fi->fib_prefsrc &&
396 cfg->fc_priority == fi->fib_priority &&
397 cfg->fc_type == fi->fib_type &&
398 cfg->fc_table == fi->fib_tb_id &&
399 !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
406 static struct fib_info *fib_find_info(struct fib_info *nfi)
408 struct hlist_head *head;
412 hash = fib_info_hashfn(nfi);
413 head = &fib_info_hash[hash];
415 hlist_for_each_entry(fi, head, fib_hash) {
416 if (!net_eq(fi->fib_net, nfi->fib_net))
418 if (fi->fib_nhs != nfi->fib_nhs)
420 if (nfi->fib_protocol == fi->fib_protocol &&
421 nfi->fib_scope == fi->fib_scope &&
422 nfi->fib_prefsrc == fi->fib_prefsrc &&
423 nfi->fib_priority == fi->fib_priority &&
424 nfi->fib_type == fi->fib_type &&
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)
890 if (cfg->fc_oif || cfg->fc_gw_family) {
891 struct fib_nh *nh = fib_info_nh(fi, 0);
894 if (fib_encap_match(net, cfg->fc_encap_type,
895 cfg->fc_encap, nh, cfg, extack))
898 #ifdef CONFIG_IP_ROUTE_CLASSID
900 cfg->fc_flow != nh->nh_tclassid)
903 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
904 (cfg->fc_gw_family &&
905 cfg->fc_gw_family != nh->fib_nh_gw_family))
908 if (cfg->fc_gw_family == AF_INET &&
909 cfg->fc_gw4 != nh->fib_nh_gw4)
912 if (cfg->fc_gw_family == AF_INET6 &&
913 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
919 #ifdef CONFIG_IP_ROUTE_MULTIPATH
924 remaining = cfg->fc_mp_len;
929 if (!rtnh_ok(rtnh, remaining))
932 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
935 attrlen = rtnh_attrlen(rtnh);
937 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
940 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
941 nlav = nla_find(attrs, attrlen, RTA_VIA);
943 NL_SET_ERR_MSG(extack,
944 "Nexthop configuration can not contain both GATEWAY and VIA");
951 err = fib_gw_from_attr(&gw, nla, extack);
955 if (nh->fib_nh_gw_family != AF_INET ||
956 gw != nh->fib_nh_gw4)
959 struct fib_config cfg2;
961 err = fib_gw_from_via(&cfg2, nlav, extack);
965 switch (nh->fib_nh_gw_family) {
967 if (cfg2.fc_gw_family != AF_INET ||
968 cfg2.fc_gw4 != nh->fib_nh_gw4)
972 if (cfg2.fc_gw_family != AF_INET6 ||
973 ipv6_addr_cmp(&cfg2.fc_gw6,
980 #ifdef CONFIG_IP_ROUTE_CLASSID
981 nla = nla_find(attrs, attrlen, RTA_FLOW);
983 if (nla_len(nla) < sizeof(u32)) {
984 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
987 if (nla_get_u32(nla) != nh->nh_tclassid)
993 rtnh = rtnh_next(rtnh, &remaining);
994 } endfor_nexthops(fi);
999 bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
1007 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1008 int type = nla_type(nla);
1013 if (type > RTAX_MAX)
1016 if (type == RTAX_CC_ALGO) {
1017 char tmp[TCP_CA_NAME_MAX];
1018 bool ecn_ca = false;
1020 nla_strscpy(tmp, nla, sizeof(tmp));
1021 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
1023 if (nla_len(nla) != sizeof(u32))
1025 val = nla_get_u32(nla);
1028 fi_val = fi->fib_metrics->metrics[type - 1];
1029 if (type == RTAX_FEATURES)
1030 fi_val &= ~DST_FEATURE_ECN_CA;
1039 static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1040 u32 table, struct netlink_ext_ack *extack)
1042 struct fib6_config cfg = {
1044 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1045 .fc_ifindex = nh->fib_nh_oif,
1046 .fc_gateway = nh->fib_nh_gw6,
1048 struct fib6_nh fib6_nh = {};
1051 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1053 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1054 dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_KERNEL);
1055 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1056 nh->fib_nh_scope = RT_SCOPE_LINK;
1058 ipv6_stub->fib6_nh_release(&fib6_nh);
1068 * Semantics of nexthop is very messy by historical reasons.
1069 * We have to take into account, that:
1070 * a) gateway can be actually local interface address,
1071 * so that gatewayed route is direct.
1072 * b) gateway must be on-link address, possibly
1073 * described not by an ifaddr, but also by a direct route.
1074 * c) If both gateway and interface are specified, they should not
1076 * d) If we use tunnel routes, gateway could be not on-link.
1078 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1079 * results in pretty ugly and hairy code with obscure logic.
1081 * I chose to generalized it instead, so that the size
1082 * of code does not increase practically, but it becomes
1083 * much more general.
1084 * Every prefix is assigned a "scope" value: "host" is local address,
1085 * "link" is direct route,
1086 * [ ... "site" ... "interior" ... ]
1087 * and "universe" is true gateway route with global meaning.
1089 * Every prefix refers to a set of "nexthop"s (gw, oif),
1090 * where gw must have narrower scope. This recursion stops
1091 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1092 * which means that gw is forced to be on link.
1094 * Code is still hairy, but now it is apparently logically
1095 * consistent and very flexible. F.e. as by-product it allows
1096 * to co-exists in peace independent exterior and interior
1097 * routing processes.
1099 * Normally it looks as following.
1101 * {universe prefix} -> (gw, oif) [scope link]
1103 * |-> {link prefix} -> (gw, oif) [scope local]
1105 * |-> {local prefix} (terminal node)
1107 static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1108 u8 scope, struct netlink_ext_ack *extack)
1110 struct net_device *dev;
1111 struct fib_result res;
1114 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1115 unsigned int addr_type;
1117 if (scope >= RT_SCOPE_LINK) {
1118 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1121 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1123 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1126 if (!(dev->flags & IFF_UP)) {
1127 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1130 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1131 if (addr_type != RTN_UNICAST) {
1132 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1135 if (!netif_carrier_ok(dev))
1136 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1137 nh->fib_nh_dev = dev;
1138 dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1139 nh->fib_nh_scope = RT_SCOPE_LINK;
1144 struct fib_table *tbl = NULL;
1145 struct flowi4 fl4 = {
1146 .daddr = nh->fib_nh_gw4,
1147 .flowi4_scope = scope + 1,
1148 .flowi4_oif = nh->fib_nh_oif,
1149 .flowi4_iif = LOOPBACK_IFINDEX,
1152 /* It is not necessary, but requires a bit of thinking */
1153 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1154 fl4.flowi4_scope = RT_SCOPE_LINK;
1156 if (table && table != RT_TABLE_MAIN)
1157 tbl = fib_get_table(net, table);
1160 err = fib_table_lookup(tbl, &fl4, &res,
1161 FIB_LOOKUP_IGNORE_LINKSTATE |
1164 /* on error or if no table given do full lookup. This
1165 * is needed for example when nexthops are in the local
1166 * table rather than the given table
1169 err = fib_lookup(net, &fl4, &res,
1170 FIB_LOOKUP_IGNORE_LINKSTATE);
1174 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1180 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1181 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1184 nh->fib_nh_scope = res.scope;
1185 nh->fib_nh_oif = FIB_RES_OIF(res);
1186 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1188 NL_SET_ERR_MSG(extack,
1189 "No egress device for nexthop gateway");
1192 dev_hold_track(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1193 if (!netif_carrier_ok(dev))
1194 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1195 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1201 static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1202 struct netlink_ext_ack *extack)
1204 struct in_device *in_dev;
1207 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1208 NL_SET_ERR_MSG(extack,
1209 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1216 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1220 if (!(in_dev->dev->flags & IFF_UP)) {
1221 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1225 nh->fib_nh_dev = in_dev->dev;
1226 dev_hold_track(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1227 nh->fib_nh_scope = RT_SCOPE_HOST;
1228 if (!netif_carrier_ok(nh->fib_nh_dev))
1229 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1236 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1237 struct netlink_ext_ack *extack)
1241 if (nh->fib_nh_gw_family == AF_INET)
1242 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1243 else if (nh->fib_nh_gw_family == AF_INET6)
1244 err = fib_check_nh_v6_gw(net, nh, table, extack);
1246 err = fib_check_nh_nongw(net, nh, extack);
1251 static struct hlist_head *
1252 fib_info_laddrhash_bucket(const struct net *net, __be32 val)
1254 u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val,
1255 fib_info_hash_bits);
1257 return &fib_info_laddrhash[slot];
1260 static struct hlist_head *fib_info_hash_alloc(int bytes)
1262 if (bytes <= PAGE_SIZE)
1263 return kzalloc(bytes, GFP_KERNEL);
1265 return (struct hlist_head *)
1266 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1270 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1275 if (bytes <= PAGE_SIZE)
1278 free_pages((unsigned long) hash, get_order(bytes));
1281 static void fib_info_hash_move(struct hlist_head *new_info_hash,
1282 struct hlist_head *new_laddrhash,
1283 unsigned int new_size)
1285 struct hlist_head *old_info_hash, *old_laddrhash;
1286 unsigned int old_size = fib_info_hash_size;
1287 unsigned int i, bytes;
1289 spin_lock_bh(&fib_info_lock);
1290 old_info_hash = fib_info_hash;
1291 old_laddrhash = fib_info_laddrhash;
1292 fib_info_hash_size = new_size;
1293 fib_info_hash_bits = ilog2(new_size);
1295 for (i = 0; i < old_size; i++) {
1296 struct hlist_head *head = &fib_info_hash[i];
1297 struct hlist_node *n;
1298 struct fib_info *fi;
1300 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1301 struct hlist_head *dest;
1302 unsigned int new_hash;
1304 new_hash = fib_info_hashfn(fi);
1305 dest = &new_info_hash[new_hash];
1306 hlist_add_head(&fi->fib_hash, dest);
1309 fib_info_hash = new_info_hash;
1311 fib_info_laddrhash = new_laddrhash;
1312 for (i = 0; i < old_size; i++) {
1313 struct hlist_head *lhead = &old_laddrhash[i];
1314 struct hlist_node *n;
1315 struct fib_info *fi;
1317 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1318 struct hlist_head *ldest;
1320 ldest = fib_info_laddrhash_bucket(fi->fib_net,
1322 hlist_add_head(&fi->fib_lhash, ldest);
1326 spin_unlock_bh(&fib_info_lock);
1328 bytes = old_size * sizeof(struct hlist_head *);
1329 fib_info_hash_free(old_info_hash, bytes);
1330 fib_info_hash_free(old_laddrhash, bytes);
1333 __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1334 unsigned char scope)
1338 if (nhc->nhc_family != AF_INET)
1339 return inet_select_addr(nhc->nhc_dev, 0, scope);
1341 nh = container_of(nhc, struct fib_nh, nh_common);
1342 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1343 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1345 return nh->nh_saddr;
1348 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1350 struct fib_nh_common *nhc = res->nhc;
1352 if (res->fi->fib_prefsrc)
1353 return res->fi->fib_prefsrc;
1355 if (nhc->nhc_family == AF_INET) {
1358 nh = container_of(nhc, struct fib_nh, nh_common);
1359 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1360 return nh->nh_saddr;
1363 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1366 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1368 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1369 fib_prefsrc != cfg->fc_dst) {
1370 u32 tb_id = cfg->fc_table;
1373 if (tb_id == RT_TABLE_MAIN)
1374 tb_id = RT_TABLE_LOCAL;
1376 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1377 fib_prefsrc, tb_id);
1379 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1380 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1381 fib_prefsrc, RT_TABLE_LOCAL);
1384 if (rc != RTN_LOCAL)
1390 struct fib_info *fib_create_info(struct fib_config *cfg,
1391 struct netlink_ext_ack *extack)
1394 struct fib_info *fi = NULL;
1395 struct nexthop *nh = NULL;
1396 struct fib_info *ofi;
1398 struct net *net = cfg->fc_nlinfo.nl_net;
1400 if (cfg->fc_type > RTN_MAX)
1403 /* Fast check to catch the most weird cases */
1404 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1405 NL_SET_ERR_MSG(extack, "Invalid scope");
1409 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1410 NL_SET_ERR_MSG(extack,
1411 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1415 if (cfg->fc_nh_id) {
1417 fi = fib_find_info_nh(net, cfg);
1419 refcount_inc(&fi->fib_treeref);
1424 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1426 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1432 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1434 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1442 /* Paired with WRITE_ONCE() in fib_release_info() */
1443 if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
1444 unsigned int new_size = fib_info_hash_size << 1;
1445 struct hlist_head *new_info_hash;
1446 struct hlist_head *new_laddrhash;
1451 bytes = new_size * sizeof(struct hlist_head *);
1452 new_info_hash = fib_info_hash_alloc(bytes);
1453 new_laddrhash = fib_info_hash_alloc(bytes);
1454 if (!new_info_hash || !new_laddrhash) {
1455 fib_info_hash_free(new_info_hash, bytes);
1456 fib_info_hash_free(new_laddrhash, bytes);
1458 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1460 if (!fib_info_hash_size)
1464 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1467 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1468 cfg->fc_mx_len, extack);
1469 if (IS_ERR(fi->fib_metrics)) {
1470 err = PTR_ERR(fi->fib_metrics);
1472 return ERR_PTR(err);
1476 fi->fib_protocol = cfg->fc_protocol;
1477 fi->fib_scope = cfg->fc_scope;
1478 fi->fib_flags = cfg->fc_flags;
1479 fi->fib_priority = cfg->fc_priority;
1480 fi->fib_prefsrc = cfg->fc_prefsrc;
1481 fi->fib_type = cfg->fc_type;
1482 fi->fib_tb_id = cfg->fc_table;
1486 if (!nexthop_get(nh)) {
1487 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1494 change_nexthops(fi) {
1495 nexthop_nh->nh_parent = fi;
1496 } endfor_nexthops(fi)
1499 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1502 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1508 if (fib_props[cfg->fc_type].error) {
1509 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1510 NL_SET_ERR_MSG(extack,
1511 "Gateway, device and multipath can not be specified for this route type");
1516 switch (cfg->fc_type) {
1524 NL_SET_ERR_MSG(extack, "Invalid route type");
1529 if (cfg->fc_scope > RT_SCOPE_HOST) {
1530 NL_SET_ERR_MSG(extack, "Invalid scope");
1535 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1538 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
1539 struct fib_nh *nh = fi->fib_nh;
1541 /* Local address is added. */
1543 NL_SET_ERR_MSG(extack,
1544 "Route with host scope can not have multiple nexthops");
1547 if (nh->fib_nh_gw_family) {
1548 NL_SET_ERR_MSG(extack,
1549 "Route with host scope can not have a gateway");
1552 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1553 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
1555 if (!nh->fib_nh_dev)
1557 netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1562 change_nexthops(fi) {
1563 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1564 cfg->fc_table, cfg->fc_scope,
1568 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1570 } endfor_nexthops(fi)
1571 if (linkdown == fi->fib_nhs)
1572 fi->fib_flags |= RTNH_F_LINKDOWN;
1575 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1576 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1581 change_nexthops(fi) {
1582 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1584 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1585 fi->fib_nh_is_v6 = true;
1586 } endfor_nexthops(fi)
1592 ofi = fib_find_info(fi);
1596 refcount_inc(&ofi->fib_treeref);
1600 refcount_set(&fi->fib_treeref, 1);
1601 refcount_set(&fi->fib_clntref, 1);
1602 spin_lock_bh(&fib_info_lock);
1604 hlist_add_head(&fi->fib_hash,
1605 &fib_info_hash[fib_info_hashfn(fi)]);
1606 if (fi->fib_prefsrc) {
1607 struct hlist_head *head;
1609 head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
1610 hlist_add_head(&fi->fib_lhash, head);
1613 list_add(&fi->nh_list, &nh->fi_list);
1615 change_nexthops(fi) {
1616 struct hlist_head *head;
1618 if (!nexthop_nh->fib_nh_dev)
1620 head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
1621 hlist_add_head(&nexthop_nh->nh_hash, head);
1622 } endfor_nexthops(fi)
1624 spin_unlock_bh(&fib_info_lock);
1636 return ERR_PTR(err);
1639 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1640 u8 rt_family, unsigned char *flags, bool skip_oif)
1642 if (nhc->nhc_flags & RTNH_F_DEAD)
1643 *flags |= RTNH_F_DEAD;
1645 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1646 *flags |= RTNH_F_LINKDOWN;
1649 switch (nhc->nhc_family) {
1651 if (ip_ignore_linkdown(nhc->nhc_dev))
1652 *flags |= RTNH_F_DEAD;
1655 if (ip6_ignore_linkdown(nhc->nhc_dev))
1656 *flags |= RTNH_F_DEAD;
1662 switch (nhc->nhc_gw_family) {
1664 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1665 goto nla_put_failure;
1668 /* if gateway family does not match nexthop family
1669 * gateway is encoded as RTA_VIA
1671 if (rt_family != nhc->nhc_gw_family) {
1672 int alen = sizeof(struct in6_addr);
1676 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1678 goto nla_put_failure;
1680 via = nla_data(nla);
1681 via->rtvia_family = AF_INET6;
1682 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1683 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1684 &nhc->nhc_gw.ipv6) < 0) {
1685 goto nla_put_failure;
1690 *flags |= (nhc->nhc_flags &
1691 (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
1693 if (!skip_oif && nhc->nhc_dev &&
1694 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1695 goto nla_put_failure;
1697 if (nhc->nhc_lwtstate &&
1698 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1699 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1700 goto nla_put_failure;
1707 EXPORT_SYMBOL_GPL(fib_nexthop_info);
1709 #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1710 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1711 int nh_weight, u8 rt_family, u32 nh_tclassid)
1713 const struct net_device *dev = nhc->nhc_dev;
1714 struct rtnexthop *rtnh;
1715 unsigned char flags = 0;
1717 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1719 goto nla_put_failure;
1721 rtnh->rtnh_hops = nh_weight - 1;
1722 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1724 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1725 goto nla_put_failure;
1727 rtnh->rtnh_flags = flags;
1729 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1730 goto nla_put_failure;
1732 /* length of rtnetlink header + attributes */
1733 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1740 EXPORT_SYMBOL_GPL(fib_add_nexthop);
1743 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1744 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1748 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1750 goto nla_put_failure;
1752 if (unlikely(fi->nh)) {
1753 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1754 goto nla_put_failure;
1759 u32 nh_tclassid = 0;
1760 #ifdef CONFIG_IP_ROUTE_CLASSID
1761 nh_tclassid = nh->nh_tclassid;
1763 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1764 AF_INET, nh_tclassid) < 0)
1765 goto nla_put_failure;
1766 } endfor_nexthops(fi);
1769 nla_nest_end(skb, mp);
1777 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1783 int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1784 const struct fib_rt_info *fri, unsigned int flags)
1786 unsigned int nhs = fib_info_num_path(fri->fi);
1787 struct fib_info *fi = fri->fi;
1788 u32 tb_id = fri->tb_id;
1789 struct nlmsghdr *nlh;
1792 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1796 rtm = nlmsg_data(nlh);
1797 rtm->rtm_family = AF_INET;
1798 rtm->rtm_dst_len = fri->dst_len;
1799 rtm->rtm_src_len = 0;
1800 rtm->rtm_tos = fri->tos;
1802 rtm->rtm_table = tb_id;
1804 rtm->rtm_table = RT_TABLE_COMPAT;
1805 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1806 goto nla_put_failure;
1807 rtm->rtm_type = fri->type;
1808 rtm->rtm_flags = fi->fib_flags;
1809 rtm->rtm_scope = fi->fib_scope;
1810 rtm->rtm_protocol = fi->fib_protocol;
1812 if (rtm->rtm_dst_len &&
1813 nla_put_in_addr(skb, RTA_DST, fri->dst))
1814 goto nla_put_failure;
1815 if (fi->fib_priority &&
1816 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1817 goto nla_put_failure;
1818 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1819 goto nla_put_failure;
1821 if (fi->fib_prefsrc &&
1822 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1823 goto nla_put_failure;
1826 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
1827 goto nla_put_failure;
1828 if (nexthop_is_blackhole(fi->nh))
1829 rtm->rtm_type = RTN_BLACKHOLE;
1830 if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode)
1835 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1836 unsigned char flags = 0;
1838 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1839 goto nla_put_failure;
1841 rtm->rtm_flags = flags;
1842 #ifdef CONFIG_IP_ROUTE_CLASSID
1843 if (nhc->nhc_family == AF_INET) {
1846 nh = container_of(nhc, struct fib_nh, nh_common);
1847 if (nh->nh_tclassid &&
1848 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1849 goto nla_put_failure;
1853 if (fib_add_multipath(skb, fi) < 0)
1854 goto nla_put_failure;
1859 rtm->rtm_flags |= RTM_F_OFFLOAD;
1861 rtm->rtm_flags |= RTM_F_TRAP;
1862 if (fri->offload_failed)
1863 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
1865 nlmsg_end(skb, nlh);
1869 nlmsg_cancel(skb, nlh);
1875 * - local address disappeared -> we must delete all the entries
1877 * - device went down -> we must shutdown all nexthops going via it.
1879 int fib_sync_down_addr(struct net_device *dev, __be32 local)
1881 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
1882 struct net *net = dev_net(dev);
1883 struct hlist_head *head;
1884 struct fib_info *fi;
1887 if (!fib_info_laddrhash || local == 0)
1890 head = fib_info_laddrhash_bucket(net, local);
1891 hlist_for_each_entry(fi, head, fib_lhash) {
1892 if (!net_eq(fi->fib_net, net) ||
1893 fi->fib_tb_id != tb_id)
1895 if (fi->fib_prefsrc == local) {
1896 fi->fib_flags |= RTNH_F_DEAD;
1903 static int call_fib_nh_notifiers(struct fib_nh *nh,
1904 enum fib_event_type event_type)
1906 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1907 struct fib_nh_notifier_info info = {
1911 switch (event_type) {
1912 case FIB_EVENT_NH_ADD:
1913 if (nh->fib_nh_flags & RTNH_F_DEAD)
1915 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1917 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1919 case FIB_EVENT_NH_DEL:
1920 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1921 (nh->fib_nh_flags & RTNH_F_DEAD))
1922 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1923 event_type, &info.info);
1932 /* Update the PMTU of exceptions when:
1933 * - the new MTU of the first hop becomes smaller than the PMTU
1934 * - the old MTU was the same as the PMTU, and it limited discovery of
1935 * larger MTUs on the path. With that limit raised, we can now
1936 * discover larger MTUs
1937 * A special case is locked exceptions, for which the PMTU is smaller
1938 * than the minimal accepted PMTU:
1939 * - if the new MTU is greater than the PMTU, don't make any change
1940 * - otherwise, unlock and set PMTU
1942 void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1944 struct fnhe_hash_bucket *bucket;
1947 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1951 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1952 struct fib_nh_exception *fnhe;
1954 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1956 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1957 if (fnhe->fnhe_mtu_locked) {
1958 if (new <= fnhe->fnhe_pmtu) {
1959 fnhe->fnhe_pmtu = new;
1960 fnhe->fnhe_mtu_locked = false;
1962 } else if (new < fnhe->fnhe_pmtu ||
1963 orig == fnhe->fnhe_pmtu) {
1964 fnhe->fnhe_pmtu = new;
1970 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1972 struct hlist_head *head = fib_info_devhash_bucket(dev);
1975 hlist_for_each_entry(nh, head, nh_hash) {
1976 if (nh->fib_nh_dev == dev)
1977 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1981 /* Event force Flags Description
1982 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1983 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1984 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1985 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1987 * only used when fib_nh is built into fib_info
1989 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1991 struct hlist_head *head = fib_info_devhash_bucket(dev);
1992 struct fib_info *prev_fi = NULL;
1993 int scope = RT_SCOPE_NOWHERE;
2000 hlist_for_each_entry(nh, head, nh_hash) {
2001 struct fib_info *fi = nh->nh_parent;
2004 BUG_ON(!fi->fib_nhs);
2005 if (nh->fib_nh_dev != dev || fi == prev_fi)
2009 change_nexthops(fi) {
2010 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
2012 else if (nexthop_nh->fib_nh_dev == dev &&
2013 nexthop_nh->fib_nh_scope != scope) {
2016 case NETDEV_UNREGISTER:
2017 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
2020 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
2023 call_fib_nh_notifiers(nexthop_nh,
2027 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2028 if (event == NETDEV_UNREGISTER &&
2029 nexthop_nh->fib_nh_dev == dev) {
2034 } endfor_nexthops(fi)
2035 if (dead == fi->fib_nhs) {
2038 case NETDEV_UNREGISTER:
2039 fi->fib_flags |= RTNH_F_DEAD;
2042 fi->fib_flags |= RTNH_F_LINKDOWN;
2054 /* Must be invoked inside of an RCU protected region. */
2055 static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
2057 struct fib_info *fi = NULL, *last_resort = NULL;
2058 struct hlist_head *fa_head = res->fa_head;
2059 struct fib_table *tb = res->table;
2060 u8 slen = 32 - res->prefixlen;
2061 int order = -1, last_idx = -1;
2062 struct fib_alias *fa, *fa1 = NULL;
2063 u32 last_prio = res->fi->fib_priority;
2066 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
2067 struct fib_info *next_fi = fa->fa_info;
2068 struct fib_nh_common *nhc;
2070 if (fa->fa_slen != slen)
2072 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2074 if (fa->tb_id != tb->tb_id)
2076 if (next_fi->fib_priority > last_prio &&
2077 fa->fa_tos == last_tos) {
2082 if (next_fi->fib_flags & RTNH_F_DEAD)
2084 last_tos = fa->fa_tos;
2085 last_prio = next_fi->fib_priority;
2087 if (next_fi->fib_scope != res->scope ||
2088 fa->fa_type != RTN_UNICAST)
2091 nhc = fib_info_nhc(next_fi, 0);
2092 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
2095 fib_alias_accessed(fa);
2098 if (next_fi != res->fi)
2101 } else if (!fib_detect_death(fi, order, &last_resort,
2102 &last_idx, fa1->fa_default)) {
2103 fib_result_assign(res, fi);
2104 fa1->fa_default = order;
2111 if (order <= 0 || !fi) {
2113 fa1->fa_default = -1;
2117 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2119 fib_result_assign(res, fi);
2120 fa1->fa_default = order;
2125 fib_result_assign(res, last_resort);
2126 fa1->fa_default = last_idx;
2132 * Dead device goes up. We wake up dead nexthops.
2133 * It takes sense only on multipath routes.
2135 * only used when fib_nh is built into fib_info
2137 int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
2139 struct fib_info *prev_fi;
2140 struct hlist_head *head;
2144 if (!(dev->flags & IFF_UP))
2147 if (nh_flags & RTNH_F_DEAD) {
2148 unsigned int flags = dev_get_flags(dev);
2150 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2151 nh_flags |= RTNH_F_LINKDOWN;
2155 head = fib_info_devhash_bucket(dev);
2158 hlist_for_each_entry(nh, head, nh_hash) {
2159 struct fib_info *fi = nh->nh_parent;
2162 BUG_ON(!fi->fib_nhs);
2163 if (nh->fib_nh_dev != dev || fi == prev_fi)
2168 change_nexthops(fi) {
2169 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
2173 if (!nexthop_nh->fib_nh_dev ||
2174 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
2176 if (nexthop_nh->fib_nh_dev != dev ||
2177 !__in_dev_get_rtnl(dev))
2180 nexthop_nh->fib_nh_flags &= ~nh_flags;
2181 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
2182 } endfor_nexthops(fi)
2185 fi->fib_flags &= ~nh_flags;
2195 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2196 static bool fib_good_nh(const struct fib_nh *nh)
2198 int state = NUD_REACHABLE;
2200 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2201 struct neighbour *n;
2205 if (likely(nh->fib_nh_gw_family == AF_INET))
2206 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2207 (__force u32)nh->fib_nh_gw4);
2208 else if (nh->fib_nh_gw_family == AF_INET6)
2209 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2214 state = n->nud_state;
2216 rcu_read_unlock_bh();
2219 return !!(state & NUD_VALID);
2222 void fib_select_multipath(struct fib_result *res, int hash)
2224 struct fib_info *fi = res->fi;
2225 struct net *net = fi->fib_net;
2228 if (unlikely(res->fi->nh)) {
2229 nexthop_path_fib_result(res, hash);
2233 change_nexthops(fi) {
2234 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
2235 if (!fib_good_nh(nexthop_nh))
2238 res->nh_sel = nhsel;
2239 res->nhc = &nexthop_nh->nh_common;
2244 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
2247 res->nh_sel = nhsel;
2248 res->nhc = &nexthop_nh->nh_common;
2250 } endfor_nexthops(fi);
2254 void fib_select_path(struct net *net, struct fib_result *res,
2255 struct flowi4 *fl4, const struct sk_buff *skb)
2257 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2260 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2261 if (fib_info_num_path(res->fi) > 1) {
2262 int h = fib_multipath_hash(net, fl4, skb, NULL);
2264 fib_select_multipath(res, h);
2268 if (!res->prefixlen &&
2269 res->table->tb_num_default > 1 &&
2270 res->type == RTN_UNICAST)
2271 fib_select_default(fl4, res);
2275 fl4->saddr = fib_result_prefsrc(net, res);