1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel routing table updates using netlink over GNU/Linux system.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
10 /* The following definition is to workaround an issue in the Linux kernel
11 * header files with redefinition of 'struct in6_addr' in both
12 * netinet/in.h and linux/in6.h.
13 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
17 #include <net/if_arp.h>
18 #include <linux/lwtunnel.h>
19 #include <linux/mpls_iptunnel.h>
20 #include <linux/seg6_iptunnel.h>
21 #include <linux/seg6_local.h>
22 #include <linux/neighbour.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/nexthop.h>
26 /* Hack for GNU libc version 2. */
28 #define MSG_TRUNC 0x20
29 #endif /* MSG_TRUNC */
36 #include "plist_int.h"
37 #include "connected.h"
50 #include "zebra/zapi_msg.h"
51 #include "zebra/zebra_ns.h"
52 #include "zebra/zebra_vrf.h"
54 #include "zebra/redistribute.h"
55 #include "zebra/interface.h"
56 #include "zebra/debug.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_ptm.h"
59 #include "zebra/zebra_mpls.h"
60 #include "zebra/kernel_netlink.h"
61 #include "zebra/rt_netlink.h"
62 #include "zebra/zebra_nhg.h"
63 #include "zebra/zebra_mroute.h"
64 #include "zebra/zebra_vxlan.h"
65 #include "zebra/zebra_errors.h"
66 #include "zebra/zebra_evpn_mh.h"
67 #include "zebra/zebra_trace.h"
68 #include "zebra/zebra_neigh.h"
74 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
75 * UAPI for MAC sync. */
76 #ifndef _UAPI_LINUX_IF_BRIDGE_H
77 #define BR_SPH_LIST_SIZE 10
80 static vlanid_t filter_vlan
= 0;
82 /* We capture whether the current kernel supports nexthop ids; by
83 * default, we'll use them if possible. There's also a configuration
84 * available to _disable_ use of kernel nexthops.
86 static bool supports_nh
;
94 static const char ipv4_ll_buf
[16] = "169.254.0.1";
95 static struct in_addr ipv4_ll
;
97 /* Is this a ipv4 over ipv6 route? */
98 static bool is_route_v4_over_v6(unsigned char rtm_family
,
99 enum nexthop_types_t nexthop_type
)
101 if (rtm_family
== AF_INET
102 && (nexthop_type
== NEXTHOP_TYPE_IPV6
103 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
109 /* Helper to control use of kernel-level nexthop ids */
110 static bool kernel_nexthops_supported(void)
112 return (supports_nh
&& !vrf_is_backend_netns()
113 && zebra_nhg_kernel_nexthops_enabled());
117 * Some people may only want to use NHGs created by protos and not
118 * implicitly created by Zebra. This check accounts for that.
120 static bool proto_nexthops_only(void)
122 return zebra_nhg_proto_nexthops_only();
125 /* Is this a proto created NHG? */
126 static bool is_proto_nhg(uint32_t id
, int type
)
128 /* If type is available, use it as the source of truth */
130 if (type
!= ZEBRA_ROUTE_NHG
)
135 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
141 /* Is vni mcast group */
142 static bool is_mac_vni_mcast_group(struct ethaddr
*mac
, vni_t vni
,
143 struct in_addr grp_addr
)
148 if (!is_zero_mac(mac
))
151 if (!IN_MULTICAST(ntohl(grp_addr
.s_addr
)))
158 * The ipv4_ll data structure is used for all 5549
159 * additions to the kernel. Let's figure out the
160 * correct value one time instead for every
161 * install/remove of a 5549 type route
163 void rt_netlink_init(void)
165 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
169 * Mapping from dataplane neighbor flags to netlink flags
171 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
175 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
176 flags
|= NTF_EXT_LEARNED
;
177 if (dplane_flags
& DPLANE_NTF_ROUTER
)
179 if (dplane_flags
& DPLANE_NTF_USE
)
186 * Mapping from dataplane neighbor state to netlink state
188 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
192 if (dplane_state
& DPLANE_NUD_REACHABLE
)
193 state
|= NUD_REACHABLE
;
194 if (dplane_state
& DPLANE_NUD_STALE
)
196 if (dplane_state
& DPLANE_NUD_NOARP
)
198 if (dplane_state
& DPLANE_NUD_PROBE
)
200 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
201 state
|= NUD_INCOMPLETE
;
202 if (dplane_state
& DPLANE_NUD_PERMANENT
)
203 state
|= NUD_PERMANENT
;
204 if (dplane_state
& DPLANE_NUD_FAILED
)
211 static inline bool is_selfroute(int proto
)
213 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
214 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
215 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
216 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
217 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
218 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
219 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
220 || (proto
== RTPROT_SRTE
)) {
227 int zebra2proto(int proto
)
230 case ZEBRA_ROUTE_BABEL
:
231 proto
= RTPROT_BABEL
;
233 case ZEBRA_ROUTE_BGP
:
236 case ZEBRA_ROUTE_OSPF
:
237 case ZEBRA_ROUTE_OSPF6
:
240 case ZEBRA_ROUTE_STATIC
:
241 proto
= RTPROT_ZSTATIC
;
243 case ZEBRA_ROUTE_ISIS
:
246 case ZEBRA_ROUTE_RIP
:
249 case ZEBRA_ROUTE_RIPNG
:
250 proto
= RTPROT_RIPNG
;
252 case ZEBRA_ROUTE_NHRP
:
255 case ZEBRA_ROUTE_EIGRP
:
256 proto
= RTPROT_EIGRP
;
258 case ZEBRA_ROUTE_LDP
:
261 case ZEBRA_ROUTE_SHARP
:
262 proto
= RTPROT_SHARP
;
264 case ZEBRA_ROUTE_PBR
:
267 case ZEBRA_ROUTE_OPENFABRIC
:
268 proto
= RTPROT_OPENFABRIC
;
270 case ZEBRA_ROUTE_SRTE
:
273 case ZEBRA_ROUTE_TABLE
:
274 case ZEBRA_ROUTE_NHG
:
275 proto
= RTPROT_ZEBRA
;
277 case ZEBRA_ROUTE_CONNECT
:
278 case ZEBRA_ROUTE_KERNEL
:
279 proto
= RTPROT_KERNEL
;
283 * When a user adds a new protocol this will show up
284 * to let them know to do something about it. This
285 * is intentionally a warn because we should see
286 * this as part of development of a new protocol
289 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
291 proto
= RTPROT_ZEBRA
;
298 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
302 proto
= ZEBRA_ROUTE_BABEL
;
305 proto
= ZEBRA_ROUTE_BGP
;
308 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
312 proto
= ZEBRA_ROUTE_ISIS
;
315 proto
= ZEBRA_ROUTE_RIP
;
318 proto
= ZEBRA_ROUTE_RIPNG
;
321 proto
= ZEBRA_ROUTE_NHRP
;
324 proto
= ZEBRA_ROUTE_EIGRP
;
327 proto
= ZEBRA_ROUTE_LDP
;
331 proto
= ZEBRA_ROUTE_STATIC
;
334 proto
= ZEBRA_ROUTE_SHARP
;
337 proto
= ZEBRA_ROUTE_PBR
;
339 case RTPROT_OPENFABRIC
:
340 proto
= ZEBRA_ROUTE_OPENFABRIC
;
343 proto
= ZEBRA_ROUTE_SRTE
;
346 case RTPROT_REDIRECT
:
353 case RTPROT_DNROUTED
:
357 case RTPROT_KEEPALIVED
:
359 proto
= ZEBRA_ROUTE_KERNEL
;
363 proto
= ZEBRA_ROUTE_NHG
;
366 /* Intentional fall thru */
369 * When a user adds a new protocol this will show up
370 * to let them know to do something about it. This
371 * is intentionally a warn because we should see
372 * this as part of development of a new protocol
375 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
377 proto
= ZEBRA_ROUTE_KERNEL
;
384 Pending: create an efficient table_id (in a tree/hash) based lookup)
386 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
389 struct zebra_vrf
*zvrf
;
391 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
395 /* case vrf with netns : match the netnsid */
396 if (vrf_is_backend_netns()) {
397 if (ns_id
== zvrf_id(zvrf
))
398 return zvrf_id(zvrf
);
400 /* VRF is VRF_BACKEND_VRF_LITE */
401 if (zvrf
->table_id
!= table_id
)
403 return zvrf_id(zvrf
);
411 * @parse_encap_mpls() - Parses encapsulated mpls attributes
412 * @tb: Pointer to rtattr to look for nested items in.
413 * @labels: Pointer to store labels in.
415 * Return: Number of mpls labels found.
417 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
419 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
420 mpls_lse_t
*lses
= NULL
;
425 mpls_label_t label
= 0;
427 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
428 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
429 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
430 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
431 labels
[num_labels
++] = label
;
437 static enum seg6local_action_t
438 parse_encap_seg6local(struct rtattr
*tb
,
439 struct seg6local_context
*ctx
)
441 struct rtattr
*tb_encap
[SEG6_LOCAL_MAX
+ 1] = {};
442 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
444 netlink_parse_rtattr_nested(tb_encap
, SEG6_LOCAL_MAX
, tb
);
446 if (tb_encap
[SEG6_LOCAL_ACTION
])
447 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
449 if (tb_encap
[SEG6_LOCAL_NH4
])
450 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
451 tb_encap
[SEG6_LOCAL_NH4
]);
453 if (tb_encap
[SEG6_LOCAL_NH6
])
454 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
455 tb_encap
[SEG6_LOCAL_NH6
]);
457 if (tb_encap
[SEG6_LOCAL_TABLE
])
458 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
460 if (tb_encap
[SEG6_LOCAL_VRFTABLE
])
462 *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_VRFTABLE
]);
467 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
469 struct rtattr
*tb_encap
[SEG6_IPTUNNEL_MAX
+ 1] = {};
470 struct seg6_iptunnel_encap
*ipt
= NULL
;
471 struct in6_addr
*segments
= NULL
;
473 netlink_parse_rtattr_nested(tb_encap
, SEG6_IPTUNNEL_MAX
, tb
);
476 * TODO: It's not support multiple SID list.
478 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
479 ipt
= (struct seg6_iptunnel_encap
*)
480 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
481 segments
= ipt
->srh
[0].segments
;
490 static struct nexthop
491 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
492 enum blackhole_type bh_type
, int index
, void *prefsrc
,
493 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
495 struct interface
*ifp
= NULL
;
496 struct nexthop nh
= {0};
497 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
499 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
500 struct seg6local_context seg6l_ctx
= {};
501 struct in6_addr seg6_segs
= {};
504 vrf_id_t nh_vrf_id
= vrf_id
;
505 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
507 if (bh_type
== BLACKHOLE_UNSPEC
) {
509 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
510 else if (index
&& gate
)
511 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
512 : NEXTHOP_TYPE_IPV6_IFINDEX
;
513 else if (!index
&& gate
)
514 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
517 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
518 nh
.bh_type
= bh_type
;
521 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
522 nh
.bh_type
= bh_type
;
526 memcpy(&nh
.src
, prefsrc
, sz
);
528 memcpy(&nh
.gate
, gate
, sz
);
531 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
533 nh_vrf_id
= ifp
->vrf
->vrf_id
;
535 nh
.vrf_id
= nh_vrf_id
;
537 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
538 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
539 == LWTUNNEL_ENCAP_MPLS
) {
540 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
542 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
543 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
544 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
545 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
547 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
548 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
549 == LWTUNNEL_ENCAP_SEG6
) {
550 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
553 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
554 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
556 if (rtm
->rtm_flags
& RTNH_F_LINKDOWN
)
557 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_LINKDOWN
);
560 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
562 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
563 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
566 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
571 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
572 struct nexthop_group
*ng
,
574 struct rtnexthop
*rtnh
,
576 void *prefsrc
, vrf_id_t vrf_id
)
579 struct interface
*ifp
= NULL
;
582 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
584 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
585 struct seg6local_context seg6l_ctx
= {};
586 struct in6_addr seg6_segs
= {};
588 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
590 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
591 vrf_id_t nh_vrf_id
= vrf_id
;
594 struct nexthop
*nh
= NULL
;
596 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
599 index
= rtnh
->rtnh_ifindex
;
602 * Yes we are looking this up
603 * for every nexthop and just
604 * using the last one looked
607 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
610 nh_vrf_id
= ifp
->vrf
->vrf_id
;
613 EC_ZEBRA_UNKNOWN_INTERFACE
,
614 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
616 nh_vrf_id
= VRF_DEFAULT
;
621 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
622 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
623 rtnh
->rtnh_len
- sizeof(*rtnh
));
624 if (rtnh_tb
[RTA_GATEWAY
])
625 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
626 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
627 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
628 == LWTUNNEL_ENCAP_MPLS
) {
629 num_labels
= parse_encap_mpls(
630 rtnh_tb
[RTA_ENCAP
], labels
);
632 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
633 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
634 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
635 seg6l_act
= parse_encap_seg6local(
636 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
638 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
639 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
640 == LWTUNNEL_ENCAP_SEG6
) {
641 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
646 if (gate
&& rtm
->rtm_family
== AF_INET
) {
648 nh
= nexthop_from_ipv4_ifindex(
649 gate
, prefsrc
, index
, nh_vrf_id
);
651 nh
= nexthop_from_ipv4(gate
, prefsrc
,
653 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
655 nh
= nexthop_from_ipv6_ifindex(
656 gate
, index
, nh_vrf_id
);
658 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
660 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
663 nh
->weight
= rtnh
->rtnh_hops
+ 1;
666 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
669 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
670 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
674 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
676 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
677 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
679 /* Add to temporary list */
680 nexthop_group_add_sorted(ng
, nh
);
683 if (rtnh
->rtnh_len
== 0)
686 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
687 rtnh
= RTNH_NEXT(rtnh
);
690 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
695 /* Looking up routing table by netlink interface. */
696 int netlink_route_change_read_unicast_internal(struct nlmsghdr
*h
,
697 ns_id_t ns_id
, int startup
,
698 struct zebra_dplane_ctx
*ctx
)
702 struct rtattr
*tb
[RTA_MAX
+ 1];
705 struct prefix_ipv6 src_p
= {};
709 char anyaddr
[16] = {0};
711 int proto
= ZEBRA_ROUTE_KERNEL
;
716 uint8_t distance
= 0;
722 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
723 void *src
= NULL
; /* IPv6 srcdest source prefix */
724 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
726 frrtrace(3, frr_zebra
, netlink_route_change_read_unicast
, h
, ns_id
,
731 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
733 switch (rtm
->rtm_type
) {
737 bh_type
= BLACKHOLE_NULL
;
739 case RTN_UNREACHABLE
:
740 bh_type
= BLACKHOLE_REJECT
;
743 bh_type
= BLACKHOLE_ADMINPROHIB
;
746 if (IS_ZEBRA_DEBUG_KERNEL
)
747 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
748 nl_rttype_to_str(rtm
->rtm_type
),
753 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
756 "%s: Message received from netlink is of a broken size %d %zu",
757 __func__
, h
->nlmsg_len
,
758 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
762 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
764 if (rtm
->rtm_flags
& RTM_F_CLONED
)
766 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
768 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
771 selfroute
= is_selfroute(rtm
->rtm_protocol
);
773 if (!startup
&& selfroute
&& h
->nlmsg_type
== RTM_NEWROUTE
&&
774 !zrouter
.asic_offloaded
&& !ctx
) {
775 if (IS_ZEBRA_DEBUG_KERNEL
)
776 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
781 /* We don't care about change notifications for the MPLS table. */
782 /* TODO: Revisit this. */
783 if (rtm
->rtm_family
== AF_MPLS
)
786 /* Table corresponding to route. */
788 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
790 table
= rtm
->rtm_table
;
793 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
794 if (vrf_id
== VRF_DEFAULT
) {
795 if (!is_zebra_valid_kernel_table(table
)
796 && !is_zebra_main_routing_table(table
))
800 if (rtm
->rtm_flags
& RTM_F_TRAP
)
801 flags
|= ZEBRA_FLAG_TRAPPED
;
802 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
803 flags
|= ZEBRA_FLAG_OFFLOADED
;
804 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
805 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
807 if (h
->nlmsg_flags
& NLM_F_APPEND
)
808 flags
|= ZEBRA_FLAG_OUTOFSYNC
;
810 /* Route which inserted by Zebra. */
812 flags
|= ZEBRA_FLAG_SELFROUTE
;
813 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
816 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
819 dest
= RTA_DATA(tb
[RTA_DST
]);
824 src
= RTA_DATA(tb
[RTA_SRC
]);
829 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
832 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
835 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
837 if (tb
[RTA_PRIORITY
])
838 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
840 #if defined(SUPPORT_REALMS)
842 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
845 if (tb
[RTA_METRICS
]) {
846 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
848 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
849 RTA_PAYLOAD(tb
[RTA_METRICS
]));
852 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
855 if (rtm
->rtm_family
== AF_INET
) {
857 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
859 "Invalid destination prefix length: %u received from kernel route change",
863 memcpy(&p
.u
.prefix4
, dest
, 4);
864 p
.prefixlen
= rtm
->rtm_dst_len
;
866 if (rtm
->rtm_src_len
!= 0) {
868 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
869 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
874 /* Force debug below to not display anything for source */
876 } else if (rtm
->rtm_family
== AF_INET6
) {
878 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
880 "Invalid destination prefix length: %u received from kernel route change",
884 memcpy(&p
.u
.prefix6
, dest
, 16);
885 p
.prefixlen
= rtm
->rtm_dst_len
;
887 src_p
.family
= AF_INET6
;
888 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
890 "Invalid source prefix length: %u received from kernel route change",
894 memcpy(&src_p
.prefix
, src
, 16);
895 src_p
.prefixlen
= rtm
->rtm_src_len
;
897 /* We only handle the AFs we handle... */
898 if (IS_ZEBRA_DEBUG_KERNEL
)
899 zlog_debug("%s: unknown address-family %u", __func__
,
905 * For ZEBRA_ROUTE_KERNEL types:
907 * The metric/priority of the route received from the kernel
908 * is a 32 bit number. We are going to interpret the high
909 * order byte as the Admin Distance and the low order 3 bytes
912 * This will allow us to do two things:
913 * 1) Allow the creation of kernel routes that can be
914 * overridden by zebra.
915 * 2) Allow the old behavior for 'most' kernel route types
916 * if a user enters 'ip route ...' v4 routes get a metric
917 * of 0 and v6 routes get a metric of 1024. Both of these
918 * values will end up with a admin distance of 0, which
919 * will cause them to win for the purposes of zebra.
921 if (proto
== ZEBRA_ROUTE_KERNEL
) {
922 distance
= (metric
>> 24) & 0xFF;
923 metric
= (metric
& 0x00FFFFFF);
926 if (IS_ZEBRA_DEBUG_KERNEL
) {
927 char buf2
[PREFIX_STRLEN
];
930 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
931 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
932 src_p
.prefixlen
? " from " : "",
933 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
935 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
940 if (rtm
->rtm_family
== AF_INET6
)
943 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
944 struct route_entry
*re
;
945 struct nexthop_group
*ng
= NULL
;
947 re
= zebra_rib_route_entry_new(vrf_id
, proto
, 0, flags
, nhe_id
,
948 table
, metric
, mtu
, distance
,
951 ng
= nexthop_group_new();
953 if (!tb
[RTA_MULTIPATH
]) {
954 struct nexthop
*nexthop
, nh
;
957 nh
= parse_nexthop_unicast(
958 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
961 nexthop
= nexthop_new();
963 nexthop_group_add_sorted(ng
, nexthop
);
966 /* This is a multipath route */
967 struct rtnexthop
*rtnh
=
968 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
973 /* Use temporary list of nexthops; parse
974 * message payload's nexthops.
977 parse_multipath_nexthops_unicast(
978 ns_id
, ng
, rtm
, rtnh
, tb
,
981 zserv_nexthop_num_warn(
982 __func__
, (const struct prefix
*)&p
,
986 nexthop_group_delete(&ng
);
992 dplane_rib_add_multipath(afi
, SAFI_UNICAST
, &p
, &src_p
,
993 re
, ng
, startup
, ctx
);
995 nexthop_group_delete(&ng
);
998 * I really don't see how this is possible
999 * but since we are testing for it let's
1000 * let the end user know why the route
1001 * that was just received was swallowed
1005 "%s: %pFX multipath RTM_NEWROUTE has a invalid nexthop group from the kernel",
1007 XFREE(MTYPE_RE
, re
);
1012 "%s: %pFX RTM_DELROUTE received but received a context as well",
1018 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
1019 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
1022 if (!tb
[RTA_MULTIPATH
]) {
1025 nh
= parse_nexthop_unicast(
1026 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
1028 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
1029 flags
, &p
, &src_p
, &nh
, 0, table
,
1030 metric
, distance
, true);
1032 /* XXX: need to compare the entire list of
1033 * nexthops here for NLM_F_APPEND stupidity */
1034 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
1035 flags
, &p
, &src_p
, NULL
, 0, table
,
1036 metric
, distance
, true);
1044 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
1047 return netlink_route_change_read_unicast_internal(h
, ns_id
, startup
,
1051 static struct mcast_route_data
*mroute
= NULL
;
1053 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1054 ns_id_t ns_id
, int startup
)
1058 struct rtattr
*tb
[RTA_MAX
+ 1];
1059 struct mcast_route_data
*m
;
1064 char oif_list
[256] = "\0";
1071 rtm
= NLMSG_DATA(h
);
1073 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1075 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1078 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1080 table
= rtm
->rtm_table
;
1082 vrf
= vrf_lookup_by_table(table
, ns_id
);
1085 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1088 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1090 *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1093 *(struct in6_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1097 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1099 *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1102 *(struct in6_addr
*)RTA_DATA(tb
[RTA_DST
]);
1105 if (tb
[RTA_EXPIRES
])
1106 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1108 if (tb
[RTA_MULTIPATH
]) {
1109 struct rtnexthop
*rtnh
=
1110 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1112 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1114 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1117 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1120 if (rtnh
->rtnh_len
== 0)
1123 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1124 rtnh
= RTNH_NEXT(rtnh
);
1128 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
) {
1129 SET_IPADDR_V4(&m
->src
);
1130 SET_IPADDR_V4(&m
->grp
);
1131 } else if (rtm
->rtm_family
== RTNL_FAMILY_IP6MR
) {
1132 SET_IPADDR_V6(&m
->src
);
1133 SET_IPADDR_V6(&m
->grp
);
1135 zlog_warn("%s: Invalid rtm_family received", __func__
);
1139 if (IS_ZEBRA_DEBUG_KERNEL
) {
1140 struct interface
*ifp
= NULL
;
1141 struct zebra_vrf
*zvrf
= NULL
;
1143 for (count
= 0; count
< oif_count
; count
++) {
1144 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1147 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1148 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1149 strlcat(oif_list
, temp
, sizeof(oif_list
));
1151 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1152 ifp
= if_lookup_by_index(iif
, vrf
);
1154 "MCAST VRF: %s(%d) %s (%pIA,%pIA) IIF: %s(%d) OIF: %s jiffies: %lld",
1155 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1156 &m
->src
, &m
->grp
, ifp
? ifp
->name
: "Unknown", iif
,
1157 oif_list
, m
->lastused
);
1162 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1167 rtm
= NLMSG_DATA(h
);
1169 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1170 /* If this is not route add/delete message print warning. */
1171 zlog_debug("Kernel message: %s NS %u",
1172 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1176 switch (rtm
->rtm_family
) {
1181 case RTNL_FAMILY_IPMR
:
1182 case RTNL_FAMILY_IP6MR
:
1183 /* notifications on IPMR are irrelevant to zebra, we only care
1184 * about responses to RTM_GETROUTE requests we sent.
1190 EC_ZEBRA_UNKNOWN_FAMILY
,
1191 "Invalid address family: %u received from kernel route change: %s",
1192 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1196 /* Connected route. */
1197 if (IS_ZEBRA_DEBUG_KERNEL
)
1198 zlog_debug("%s %s %s proto %s NS %u",
1199 nl_msg_type_to_str(h
->nlmsg_type
),
1200 nl_family_to_str(rtm
->rtm_family
),
1201 nl_rttype_to_str(rtm
->rtm_type
),
1202 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1205 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1208 "%s: Message received from netlink is of a broken size: %d %zu",
1209 __func__
, h
->nlmsg_len
,
1210 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1214 /* these are "magic" kernel-managed *unicast* routes used for
1215 * outputting locally generated multicast traffic (which uses unicast
1216 * handling on Linux because ~reasons~.
1218 if (rtm
->rtm_type
== RTN_MULTICAST
)
1221 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1225 /* Request for specific route information from the kernel */
1226 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1233 /* Form the request, specifying filter (rtattr) if needed. */
1234 memset(&req
, 0, sizeof(req
));
1235 req
.n
.nlmsg_type
= type
;
1236 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1237 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1238 req
.rtm
.rtm_family
= family
;
1240 return netlink_request(&zns
->netlink_cmd
, &req
);
1243 /* Routing table read function using netlink interface. Only called
1245 int netlink_route_read(struct zebra_ns
*zns
)
1248 struct zebra_dplane_info dp_info
;
1250 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1252 /* Get IPv4 routing table. */
1253 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1256 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1257 &zns
->netlink_cmd
, &dp_info
, 0, true);
1261 /* Get IPv6 routing table. */
1262 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1265 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1266 &zns
->netlink_cmd
, &dp_info
, 0, true);
1274 * The function returns true if the gateway info could be added
1275 * to the message, otherwise false is returned.
1277 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1279 struct nlmsghdr
*nlmsg
,
1280 size_t req_size
, int bytelen
,
1281 const struct nexthop
*nexthop
)
1283 if (route_family
== AF_MPLS
) {
1284 struct gw_family_t gw_fam
;
1286 gw_fam
.family
= gw_family
;
1287 if (gw_family
== AF_INET
)
1288 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1290 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1291 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1295 if (!(nexthop
->rparent
1296 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1297 if (gw_family
== AF_INET
) {
1298 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1299 &nexthop
->gate
.ipv4
, bytelen
))
1302 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1303 &nexthop
->gate
.ipv6
, bytelen
))
1312 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1313 enum lsp_types_t nh_label_type
,
1314 mpls_lse_t
*out_lse
, char *label_buf
,
1315 size_t label_buf_size
)
1317 char label_buf1
[20];
1320 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1321 if (nh_label_type
!= ZEBRA_LSP_EVPN
&&
1322 nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1325 if (IS_ZEBRA_DEBUG_KERNEL
) {
1327 snprintf(label_buf
, label_buf_size
, "label %u",
1328 nh_label
->label
[i
]);
1330 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1331 nh_label
->label
[i
]);
1332 strlcat(label_buf
, label_buf1
, label_buf_size
);
1336 if (nh_label_type
== ZEBRA_LSP_EVPN
)
1337 out_lse
[num_labels
] = label2vni(&nh_label
->label
[i
]);
1339 out_lse
[num_labels
] =
1340 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1347 static bool _netlink_nexthop_encode_dvni_label(const struct nexthop
*nexthop
,
1348 struct nlmsghdr
*nlmsg
,
1349 mpls_lse_t
*out_lse
,
1350 size_t buflen
, char *label_buf
)
1352 struct in_addr ipv4
;
1354 if (!nl_attr_put64(nlmsg
, buflen
, LWTUNNEL_IP_ID
,
1355 htonll((uint64_t)out_lse
[0])))
1358 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1359 if (!nl_attr_put(nlmsg
, buflen
, LWTUNNEL_IP_DST
,
1360 &nexthop
->gate
.ipv4
, 4))
1363 } else if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1364 if (IS_MAPPED_IPV6(&nexthop
->gate
.ipv6
)) {
1365 ipv4_mapped_ipv6_to_ipv4(&nexthop
->gate
.ipv6
, &ipv4
);
1366 if (!nl_attr_put(nlmsg
, buflen
, LWTUNNEL_IP_DST
, &ipv4
,
1371 if (!nl_attr_put(nlmsg
, buflen
, LWTUNNEL_IP_DST
,
1372 &nexthop
->gate
.ipv6
, 16))
1376 if (IS_ZEBRA_DEBUG_KERNEL
)
1378 "%s: nexthop %pNHv %s must NEXTHOP_TYPE_IPV*_IFINDEX to be vxlan encapped",
1379 __func__
, nexthop
, label_buf
);
1387 static bool _netlink_route_encode_label_info(const struct nexthop
*nexthop
,
1388 struct nlmsghdr
*nlmsg
,
1389 size_t buflen
, struct rtmsg
*rtmsg
,
1391 size_t label_buf_size
)
1393 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1395 struct rtattr
*nest
;
1396 struct mpls_label_stack
*nh_label
;
1397 enum lsp_types_t nh_label_type
;
1399 nh_label
= nexthop
->nh_label
;
1400 nh_label_type
= nexthop
->nh_label_type
;
1403 * label_buf is *only* currently used within debugging.
1404 * As such when we assign it we are guarding it inside
1405 * a debug test. If you want to change this make sure
1406 * you fix this assumption
1408 label_buf
[0] = '\0';
1410 num_labels
= build_label_stack(nh_label
, nh_label_type
, out_lse
,
1411 label_buf
, label_buf_size
);
1413 if (num_labels
&& nh_label_type
== ZEBRA_LSP_EVPN
) {
1414 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1418 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1422 if (_netlink_nexthop_encode_dvni_label(nexthop
, nlmsg
, out_lse
,
1424 label_buf
) == false)
1427 nl_attr_nest_end(nlmsg
, nest
);
1429 } else if (num_labels
) {
1430 /* Set the BoS bit */
1431 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1433 if (rtmsg
->rtm_family
== AF_MPLS
) {
1434 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1435 num_labels
* sizeof(mpls_lse_t
)))
1438 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1439 LWTUNNEL_ENCAP_MPLS
))
1442 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1446 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1448 num_labels
* sizeof(mpls_lse_t
)))
1450 nl_attr_nest_end(nlmsg
, nest
);
1457 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1459 struct nlmsghdr
*nlmsg
,
1460 size_t buflen
, int bytelen
)
1462 if (family
== AF_INET
) {
1463 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1464 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1465 &nexthop
->rmap_src
.ipv4
, bytelen
))
1467 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1468 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1469 &nexthop
->src
.ipv4
, bytelen
))
1472 } else if (family
== AF_INET6
) {
1473 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1474 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1475 &nexthop
->rmap_src
.ipv6
, bytelen
))
1477 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1478 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1479 &nexthop
->src
.ipv6
, bytelen
))
1487 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1488 const struct in6_addr
*seg
)
1490 struct seg6_iptunnel_encap
*ipt
;
1491 struct ipv6_sr_hdr
*srh
;
1492 const size_t srhlen
= 24;
1495 * Caution: Support only SINGLE-SID, not MULTI-SID
1496 * This function only supports the case where segs represents
1497 * a single SID. If you want to extend the SRv6 functionality,
1498 * you should improve the Boundary Check.
1499 * Ex. In case of set a SID-List include multiple-SIDs as an
1500 * argument of the Transit Behavior, we must support variable
1501 * boundary check for buflen.
1503 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1504 sizeof(struct ipv6_sr_hdr
) + 16))
1507 memset(buffer
, 0, buflen
);
1509 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1510 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1512 srh
->hdrlen
= (srhlen
>> 3) - 1;
1514 srh
->segments_left
= 0;
1515 srh
->first_segment
= 0;
1516 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1521 /* This function takes a nexthop as argument and adds
1522 * the appropriate netlink attributes to an existing
1525 * @param routedesc: Human readable description of route type
1526 * (direct/recursive, single-/multipath)
1527 * @param bytelen: Length of addresses in bytes.
1528 * @param nexthop: Nexthop information
1529 * @param nlmsg: nlmsghdr structure to fill in.
1530 * @param req_size: The size allocated for the message.
1532 * The function returns true if the nexthop could be added
1533 * to the message, otherwise false is returned.
1535 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1536 const char *routedesc
, int bytelen
,
1537 const struct nexthop
*nexthop
,
1538 struct nlmsghdr
*nlmsg
,
1539 struct rtmsg
*rtmsg
,
1540 size_t req_size
, int cmd
)
1543 char label_buf
[256];
1545 char addrstr
[INET6_ADDRSTRLEN
];
1549 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1551 if (!_netlink_route_encode_label_info(nexthop
, nlmsg
, req_size
, rtmsg
,
1552 label_buf
, sizeof(label_buf
)))
1555 if (nexthop
->nh_srv6
) {
1556 if (nexthop
->nh_srv6
->seg6local_action
!=
1557 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1558 struct rtattr
*nest
;
1559 const struct seg6local_context
*ctx
;
1561 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1562 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1563 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1566 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1570 switch (nexthop
->nh_srv6
->seg6local_action
) {
1571 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1572 if (!nl_attr_put32(nlmsg
, req_size
,
1574 SEG6_LOCAL_ACTION_END
))
1577 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1578 if (!nl_attr_put32(nlmsg
, req_size
,
1580 SEG6_LOCAL_ACTION_END_X
))
1582 if (!nl_attr_put(nlmsg
, req_size
,
1583 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1584 sizeof(struct in6_addr
)))
1587 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1588 if (!nl_attr_put32(nlmsg
, req_size
,
1590 SEG6_LOCAL_ACTION_END_T
))
1592 if (!nl_attr_put32(nlmsg
, req_size
,
1597 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1598 if (!nl_attr_put32(nlmsg
, req_size
,
1600 SEG6_LOCAL_ACTION_END_DX4
))
1602 if (!nl_attr_put(nlmsg
, req_size
,
1603 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1604 sizeof(struct in_addr
)))
1607 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1608 if (!nl_attr_put32(nlmsg
, req_size
,
1610 SEG6_LOCAL_ACTION_END_DT6
))
1612 if (!nl_attr_put32(nlmsg
, req_size
,
1617 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
1618 if (!nl_attr_put32(nlmsg
, req_size
,
1620 SEG6_LOCAL_ACTION_END_DT4
))
1622 if (!nl_attr_put32(nlmsg
, req_size
,
1623 SEG6_LOCAL_VRFTABLE
,
1627 case ZEBRA_SEG6_LOCAL_ACTION_END_DT46
:
1628 if (!nl_attr_put32(nlmsg
, req_size
,
1630 SEG6_LOCAL_ACTION_END_DT46
))
1632 if (!nl_attr_put32(nlmsg
, req_size
,
1633 SEG6_LOCAL_VRFTABLE
,
1637 case ZEBRA_SEG6_LOCAL_ACTION_END_DX2
:
1638 case ZEBRA_SEG6_LOCAL_ACTION_END_DX6
:
1639 case ZEBRA_SEG6_LOCAL_ACTION_END_B6
:
1640 case ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP
:
1641 case ZEBRA_SEG6_LOCAL_ACTION_END_BM
:
1642 case ZEBRA_SEG6_LOCAL_ACTION_END_S
:
1643 case ZEBRA_SEG6_LOCAL_ACTION_END_AS
:
1644 case ZEBRA_SEG6_LOCAL_ACTION_END_AM
:
1645 case ZEBRA_SEG6_LOCAL_ACTION_END_BPF
:
1646 case ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
:
1647 zlog_err("%s: unsupport seg6local behaviour action=%u",
1649 nexthop
->nh_srv6
->seg6local_action
);
1652 nl_attr_nest_end(nlmsg
, nest
);
1655 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1658 struct rtattr
*nest
;
1660 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1661 LWTUNNEL_ENCAP_SEG6
))
1663 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1666 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1667 &nexthop
->nh_srv6
->seg6_segs
);
1670 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1673 nl_attr_nest_end(nlmsg
, nest
);
1677 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1678 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1680 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1681 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1682 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1684 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1687 if (cmd
== RTM_NEWROUTE
) {
1688 if (!_netlink_route_encode_nexthop_src(
1689 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1693 if (IS_ZEBRA_DEBUG_KERNEL
)
1694 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1695 __func__
, routedesc
, p
, ipv4_ll_buf
,
1696 label_buf
, nexthop
->ifindex
,
1697 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1701 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1702 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1703 /* Send deletes to the kernel without specifying the next-hop */
1704 if (cmd
!= RTM_DELROUTE
) {
1705 if (!_netlink_route_add_gateway_info(
1706 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1711 if (cmd
== RTM_NEWROUTE
) {
1712 if (!_netlink_route_encode_nexthop_src(
1713 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1717 if (IS_ZEBRA_DEBUG_KERNEL
) {
1718 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1720 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1721 __func__
, routedesc
, p
, addrstr
, label_buf
,
1722 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1727 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1728 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1729 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1730 AF_INET6
, nlmsg
, req_size
,
1734 if (cmd
== RTM_NEWROUTE
) {
1735 if (!_netlink_route_encode_nexthop_src(
1736 nexthop
, AF_INET6
, nlmsg
, req_size
,
1741 if (IS_ZEBRA_DEBUG_KERNEL
) {
1742 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1744 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1745 __func__
, routedesc
, p
, addrstr
, label_buf
,
1746 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1752 * We have the ifindex so we should always send it
1753 * This is especially useful if we are doing route
1756 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1757 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1761 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1762 if (cmd
== RTM_NEWROUTE
) {
1763 if (!_netlink_route_encode_nexthop_src(
1764 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1768 if (IS_ZEBRA_DEBUG_KERNEL
)
1769 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1770 __func__
, routedesc
, p
, nexthop
->ifindex
,
1771 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1777 /* This function appends tag value as rtnl flow attribute
1778 * to the given netlink msg only if value is less than 256.
1779 * Used only if SUPPORT_REALMS enabled.
1781 * @param nlmsg: nlmsghdr structure to fill in.
1782 * @param maxlen: The size allocated for the message.
1783 * @param tag: The route tag.
1785 * The function returns true if the flow attribute could
1786 * be added to the message, otherwise false is returned.
1788 static inline bool _netlink_set_tag(struct nlmsghdr
*n
, unsigned int maxlen
,
1791 if (tag
> 0 && tag
<= 255) {
1792 if (!nl_attr_put32(n
, maxlen
, RTA_FLOW
, tag
))
1798 /* This function takes a nexthop as argument and
1799 * appends to the given netlink msg. If the nexthop
1800 * defines a preferred source, the src parameter
1801 * will be modified to point to that src, otherwise
1802 * it will be kept unmodified.
1804 * @param routedesc: Human readable description of route type
1805 * (direct/recursive, single-/multipath)
1806 * @param bytelen: Length of addresses in bytes.
1807 * @param nexthop: Nexthop information
1808 * @param nlmsg: nlmsghdr structure to fill in.
1809 * @param req_size: The size allocated for the message.
1810 * @param src: pointer pointing to a location where
1811 * the prefsrc should be stored.
1813 * The function returns true if the nexthop could be added
1814 * to the message, otherwise false is returned.
1816 static bool _netlink_route_build_multipath(
1817 const struct prefix
*p
, const char *routedesc
, int bytelen
,
1818 const struct nexthop
*nexthop
, struct nlmsghdr
*nlmsg
, size_t req_size
,
1819 struct rtmsg
*rtmsg
, const union g_addr
**src
, route_tag_t tag
)
1821 char label_buf
[256];
1823 struct rtnexthop
*rtnh
;
1825 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1831 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1833 if (!_netlink_route_encode_label_info(nexthop
, nlmsg
, req_size
, rtmsg
,
1834 label_buf
, sizeof(label_buf
)))
1837 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1838 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1840 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1841 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1842 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1844 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1845 if (nexthop
->weight
)
1846 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1848 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1849 *src
= &nexthop
->rmap_src
;
1850 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1851 *src
= &nexthop
->src
;
1853 if (IS_ZEBRA_DEBUG_KERNEL
)
1855 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1856 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1857 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1859 nl_attr_rtnh_end(nlmsg
, rtnh
);
1863 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1864 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1865 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1866 nlmsg
, req_size
, bytelen
,
1870 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1871 *src
= &nexthop
->rmap_src
;
1872 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1873 *src
= &nexthop
->src
;
1875 if (IS_ZEBRA_DEBUG_KERNEL
)
1876 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1877 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1878 label_buf
, nexthop
->ifindex
,
1879 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1881 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1882 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1883 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1884 AF_INET6
, nlmsg
, req_size
,
1888 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1889 *src
= &nexthop
->rmap_src
;
1890 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1891 *src
= &nexthop
->src
;
1893 if (IS_ZEBRA_DEBUG_KERNEL
)
1894 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1895 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1896 label_buf
, nexthop
->ifindex
,
1897 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1901 * We have figured out the ifindex so we should always send it
1902 * This is especially useful if we are doing route
1905 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1906 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1909 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1910 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1911 *src
= &nexthop
->rmap_src
;
1912 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1913 *src
= &nexthop
->src
;
1915 if (IS_ZEBRA_DEBUG_KERNEL
)
1916 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1917 __func__
, routedesc
, p
, nexthop
->ifindex
,
1918 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1921 if (nexthop
->weight
)
1922 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1924 if (!_netlink_set_tag(nlmsg
, req_size
, tag
))
1927 nl_attr_rtnh_end(nlmsg
, rtnh
);
1932 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1933 const struct zebra_nhlfe
*nhlfe
,
1934 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1935 size_t req_size
, int cmd
)
1940 family
= NHLFE_FAMILY(nhlfe
);
1941 bytelen
= (family
== AF_INET
? 4 : 16);
1942 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1943 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1949 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1950 const struct zebra_nhlfe
*nhlfe
,
1951 struct nlmsghdr
*nlmsg
, size_t req_size
,
1952 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1957 family
= NHLFE_FAMILY(nhlfe
);
1958 bytelen
= (family
== AF_INET
? 4 : 16);
1959 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1960 nhlfe
->nexthop
, nlmsg
, req_size
,
1964 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1966 if (IS_ZEBRA_DEBUG_KERNEL
)
1967 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1968 routedesc
, nl_msg_type_to_str(cmd
), label
);
1971 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1972 int llalen
, ns_id_t ns_id
, uint8_t family
,
1973 bool permanent
, uint8_t protocol
)
1981 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1983 memset(&req
, 0, sizeof(req
));
1985 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1986 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1987 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1988 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1990 req
.ndm
.ndm_family
= family
;
1991 req
.ndm
.ndm_ifindex
= ifindex
;
1992 req
.ndm
.ndm_type
= RTN_UNICAST
;
1993 if (cmd
== RTM_NEWNEIGH
) {
1995 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1997 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1999 req
.ndm
.ndm_state
= NUD_FAILED
;
2001 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
2003 req
.ndm
.ndm_type
= RTN_UNICAST
;
2004 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
2005 family2addrsize(family
));
2007 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
2009 if (IS_ZEBRA_DEBUG_KERNEL
) {
2010 char ip_str
[INET6_ADDRSTRLEN
+ 8];
2011 struct interface
*ifp
= if_lookup_by_index_per_ns(
2012 zebra_ns_lookup(ns_id
), ifindex
);
2014 if (family
== AF_INET6
)
2015 snprintfrr(ip_str
, sizeof(ip_str
), "ipv6 %pI6",
2016 (struct in6_addr
*)addr
);
2018 snprintfrr(ip_str
, sizeof(ip_str
), "ipv4 %pI4",
2021 "%s: %s ifname %s ifindex %u addr %s mac %pEA vrf %s(%u)",
2022 __func__
, nl_msg_type_to_str(cmd
), ifp
->name
,
2023 ifindex
, ip_str
, (struct ethaddr
*)lla
,
2024 vrf_id_to_name(ifp
->vrf
->vrf_id
),
2028 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2032 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
2035 if (family
== AF_INET
) {
2036 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
2037 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
2039 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
2040 src
->ipv4
= nexthop
->src
.ipv4
;
2043 } else if (family
== AF_INET6
) {
2044 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
2045 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
2047 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
2048 src
->ipv6
= nexthop
->src
.ipv6
;
2057 * The function returns true if the attribute could be added
2058 * to the message, otherwise false is returned.
2060 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
2063 struct rtattr
*nest
;
2065 switch (nh
->nh_encap_type
) {
2067 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
2070 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
2074 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
2077 nl_attr_nest_end(n
, nest
);
2085 * Routing table change via netlink interface, using a dataplane context object
2087 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2088 * otherwise the number of bytes written to buf.
2090 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
2091 struct zebra_dplane_ctx
*ctx
,
2092 uint8_t *data
, size_t datalen
,
2093 bool fpm
, bool force_nhg
)
2096 struct nexthop
*nexthop
= NULL
;
2097 unsigned int nexthop_num
;
2098 const char *routedesc
;
2099 bool setsrc
= false;
2101 const struct prefix
*p
, *src_p
;
2104 route_tag_t tag
= 0;
2110 } *req
= (void *)data
;
2112 p
= dplane_ctx_get_dest(ctx
);
2113 src_p
= dplane_ctx_get_src(ctx
);
2115 if (datalen
< sizeof(*req
))
2118 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2120 memset(req
, 0, sizeof(*req
));
2122 bytelen
= (p
->family
== AF_INET
? 4 : 16);
2124 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2125 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2127 if ((cmd
== RTM_NEWROUTE
) &&
2128 ((p
->family
== AF_INET
) || v6_rr_semantics
))
2129 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2131 req
->n
.nlmsg_type
= cmd
;
2133 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2135 req
->r
.rtm_family
= p
->family
;
2136 req
->r
.rtm_dst_len
= p
->prefixlen
;
2137 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
2138 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2140 if (cmd
== RTM_DELROUTE
)
2141 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
2143 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
2146 * blackhole routes are not RTN_UNICAST, they are
2147 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
2148 * so setting this value as a RTN_UNICAST would
2149 * cause the route lookup of just the prefix
2150 * to fail. So no need to specify this for
2151 * the RTM_DELROUTE case
2153 if (cmd
!= RTM_DELROUTE
)
2154 req
->r
.rtm_type
= RTN_UNICAST
;
2156 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
2159 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
2165 /* Hardcode the metric for all routes coming from zebra. Metric isn't
2167 * either by the kernel or by zebra. Its purely for calculating best
2169 * by the routing protocol and for communicating with protocol peers.
2171 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
2172 ROUTE_INSTALLATION_METRIC
))
2175 #if defined(SUPPORT_REALMS)
2176 if (cmd
== RTM_DELROUTE
)
2177 tag
= dplane_ctx_get_old_tag(ctx
);
2179 tag
= dplane_ctx_get_tag(ctx
);
2182 /* Table corresponding to this route. */
2183 table_id
= dplane_ctx_get_table(ctx
);
2185 req
->r
.rtm_table
= table_id
;
2187 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
2188 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
2192 if (IS_ZEBRA_DEBUG_KERNEL
)
2194 "%s: %s %pFX vrf %u(%u)", __func__
,
2195 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
2199 * If we are not updating the route and we have received
2200 * a route delete, then all we need to fill in is the
2201 * prefix information to tell the kernel to schwack
2204 if (cmd
== RTM_DELROUTE
) {
2205 if (!_netlink_set_tag(&req
->n
, datalen
, tag
))
2207 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2210 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
2211 struct rtattr
*nest
;
2212 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
2213 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2215 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2218 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2222 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2224 nl_attr_nest_end(&req
->n
, nest
);
2228 * Always install blackhole routes without using nexthops, because of
2229 * the following kernel problems:
2230 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2231 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2233 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2235 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2236 nexthop
= nexthop
->resolved
;
2238 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2239 switch (nexthop
->bh_type
) {
2240 case BLACKHOLE_ADMINPROHIB
:
2241 req
->r
.rtm_type
= RTN_PROHIBIT
;
2243 case BLACKHOLE_REJECT
:
2244 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2246 case BLACKHOLE_UNSPEC
:
2247 case BLACKHOLE_NULL
:
2248 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2251 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2255 if ((!fpm
&& kernel_nexthops_supported()
2256 && (!proto_nexthops_only()
2257 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2258 || (fpm
&& force_nhg
)) {
2259 /* Kernel supports nexthop objects */
2260 if (IS_ZEBRA_DEBUG_KERNEL
)
2261 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2262 dplane_ctx_get_nhe_id(ctx
));
2264 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2265 dplane_ctx_get_nhe_id(ctx
)))
2268 /* Have to determine src still */
2269 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2273 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2277 if (p
->family
== AF_INET
) {
2278 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2279 &src
.ipv4
, bytelen
))
2281 } else if (p
->family
== AF_INET6
) {
2282 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2283 &src
.ipv6
, bytelen
))
2288 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2291 /* Count overall nexthops so we can decide whether to use singlepath
2292 * or multipath case.
2295 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2296 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2298 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2304 /* Singlepath case. */
2305 if (nexthop_num
== 1) {
2307 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2308 if (CHECK_FLAG(nexthop
->flags
,
2309 NEXTHOP_FLAG_RECURSIVE
)) {
2314 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2319 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2320 routedesc
= nexthop
->rparent
2321 ? "recursive, single-path"
2324 if (!_netlink_set_tag(&req
->n
, datalen
, tag
))
2327 if (!_netlink_route_build_singlepath(
2328 p
, routedesc
, bytelen
, nexthop
,
2329 &req
->n
, &req
->r
, datalen
, cmd
))
2336 * Add encapsulation information when installing via
2340 if (!netlink_route_nexthop_encap(
2341 &req
->n
, datalen
, nexthop
))
2347 if (p
->family
== AF_INET
) {
2348 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2349 &src
.ipv4
, bytelen
))
2351 } else if (p
->family
== AF_INET6
) {
2352 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2353 &src
.ipv6
, bytelen
))
2357 } else { /* Multipath case */
2358 struct rtattr
*nest
;
2359 const union g_addr
*src1
= NULL
;
2361 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2366 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2367 if (CHECK_FLAG(nexthop
->flags
,
2368 NEXTHOP_FLAG_RECURSIVE
)) {
2369 /* This only works for IPv4 now */
2373 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2378 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2379 routedesc
= nexthop
->rparent
2380 ? "recursive, multipath"
2384 if (!_netlink_route_build_multipath(
2385 p
, routedesc
, bytelen
, nexthop
,
2386 &req
->n
, datalen
, &req
->r
, &src1
,
2390 if (!setsrc
&& src1
) {
2391 if (p
->family
== AF_INET
)
2392 src
.ipv4
= src1
->ipv4
;
2393 else if (p
->family
== AF_INET6
)
2394 src
.ipv6
= src1
->ipv6
;
2401 nl_attr_nest_end(&req
->n
, nest
);
2404 * Add encapsulation information when installing via
2408 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2410 if (CHECK_FLAG(nexthop
->flags
,
2411 NEXTHOP_FLAG_RECURSIVE
))
2413 if (!netlink_route_nexthop_encap(
2414 &req
->n
, datalen
, nexthop
))
2421 if (p
->family
== AF_INET
) {
2422 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2423 &src
.ipv4
, bytelen
))
2425 } else if (p
->family
== AF_INET6
) {
2426 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2427 &src
.ipv6
, bytelen
))
2430 if (IS_ZEBRA_DEBUG_KERNEL
)
2431 zlog_debug("Setting source");
2435 /* If there is no useful nexthop then return. */
2436 if (nexthop_num
== 0) {
2437 if (IS_ZEBRA_DEBUG_KERNEL
)
2438 zlog_debug("%s: No useful nexthop.", __func__
);
2441 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2444 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2446 uint32_t actual_table
;
2448 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2456 struct zebra_ns
*zns
;
2459 memset(&req
, 0, sizeof(req
));
2461 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2462 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2463 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2465 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2467 if (mroute
->family
== AF_INET
) {
2468 req
.rtm
.rtm_family
= RTNL_FAMILY_IPMR
;
2469 req
.rtm
.rtm_dst_len
= IPV4_MAX_BITLEN
;
2470 req
.rtm
.rtm_src_len
= IPV4_MAX_BITLEN
;
2472 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2473 &mroute
->src
.ipaddr_v4
,
2474 sizeof(mroute
->src
.ipaddr_v4
));
2475 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2476 &mroute
->grp
.ipaddr_v4
,
2477 sizeof(mroute
->grp
.ipaddr_v4
));
2479 req
.rtm
.rtm_family
= RTNL_FAMILY_IP6MR
;
2480 req
.rtm
.rtm_dst_len
= IPV6_MAX_BITLEN
;
2481 req
.rtm
.rtm_src_len
= IPV6_MAX_BITLEN
;
2483 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2484 &mroute
->src
.ipaddr_v6
,
2485 sizeof(mroute
->src
.ipaddr_v6
));
2486 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2487 &mroute
->grp
.ipaddr_v6
,
2488 sizeof(mroute
->grp
.ipaddr_v6
));
2494 * So during the namespace cleanup we started storing
2495 * the zvrf table_id for the default table as RT_TABLE_MAIN
2496 * which is what the normal routing table for ip routing is.
2497 * This change caused this to break our lookups of sg data
2498 * because prior to this change the zvrf->table_id was 0
2499 * and when the pim multicast kernel code saw a 0,
2500 * it was auto-translated to RT_TABLE_DEFAULT. But since
2501 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2502 * and the kernel goes screw you and the delicious cookies you
2503 * are trying to give me. So now we have this little hack.
2505 if (mroute
->family
== AF_INET
)
2506 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
)
2510 actual_table
= zvrf
->table_id
;
2512 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2514 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2515 &zns
->netlink_cmd
, zns
, false);
2521 /* Char length to debug ID with */
2522 #define ID_LENGTH 10
2524 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2526 const struct nh_grp
*z_grp
,
2527 const uint8_t count
, bool resilient
,
2528 const struct nhg_resilience
*nhgr
)
2530 struct nexthop_grp grp
[count
];
2531 /* Need space for max group size, "/", and null term */
2532 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2533 char buf1
[ID_LENGTH
+ 2];
2537 memset(grp
, 0, sizeof(grp
));
2540 for (int i
= 0; i
< count
; i
++) {
2541 grp
[i
].id
= z_grp
[i
].id
;
2542 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2544 if (IS_ZEBRA_DEBUG_KERNEL
) {
2546 snprintf(buf
, sizeof(buf1
), "group %u",
2549 snprintf(buf1
, sizeof(buf1
), "/%u",
2551 strlcat(buf
, buf1
, sizeof(buf
));
2555 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2556 count
* sizeof(*grp
)))
2560 struct rtattr
*nest
;
2562 nest
= nl_attr_nest(n
, req_size
, NHA_RES_GROUP
);
2564 nl_attr_put16(n
, req_size
, NHA_RES_GROUP_BUCKETS
,
2566 nl_attr_put32(n
, req_size
, NHA_RES_GROUP_IDLE_TIMER
,
2567 nhgr
->idle_timer
* 1000);
2568 nl_attr_put32(n
, req_size
,
2569 NHA_RES_GROUP_UNBALANCED_TIMER
,
2570 nhgr
->unbalanced_timer
* 1000);
2571 nl_attr_nest_end(n
, nest
);
2573 nl_attr_put16(n
, req_size
, NHA_GROUP_TYPE
,
2574 NEXTHOP_GRP_TYPE_RES
);
2578 if (IS_ZEBRA_DEBUG_KERNEL
)
2579 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2585 * Next hop packet encoding helper function.
2587 * \param[in] cmd netlink command.
2588 * \param[in] ctx dataplane context (information snapshot).
2589 * \param[out] buf buffer to hold the packet.
2590 * \param[in] buflen amount of buffer bytes.
2592 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2593 * otherwise the number of bytes written to buf.
2595 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2596 const struct zebra_dplane_ctx
*ctx
,
2597 void *buf
, size_t buflen
, bool fpm
)
2605 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2606 char label_buf
[256];
2608 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2609 int type
= dplane_ctx_get_nhe_type(ctx
);
2610 struct rtattr
*nest
;
2613 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2617 EC_ZEBRA_NHG_FIB_UPDATE
,
2618 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2623 * Nothing to do if the kernel doesn't support nexthop objects or
2624 * we dont want to install this type of NHG, but FPM may possible to
2627 if (!fpm
&& !kernel_nexthops_supported()) {
2628 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2630 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2631 __func__
, id
, zebra_route_string(type
));
2635 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2636 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2638 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2639 __func__
, id
, zebra_route_string(type
));
2643 label_buf
[0] = '\0';
2645 if (buflen
< sizeof(*req
))
2648 memset(req
, 0, sizeof(*req
));
2650 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2651 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2653 if (cmd
== RTM_NEWNEXTHOP
)
2654 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2656 req
->n
.nlmsg_type
= cmd
;
2657 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2659 req
->nhm
.nh_family
= AF_UNSPEC
;
2662 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2665 if (cmd
== RTM_NEWNEXTHOP
) {
2667 * We distinguish between a "group", which is a collection
2668 * of ids, and a singleton nexthop with an id. The
2669 * group is installed as an id that just refers to a list of
2672 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2673 const struct nexthop_group
*nhg
;
2674 const struct nhg_resilience
*nhgr
;
2676 nhg
= dplane_ctx_get_nhe_ng(ctx
);
2678 if (!_netlink_nexthop_build_group(
2679 &req
->n
, buflen
, id
,
2680 dplane_ctx_get_nhe_nh_grp(ctx
),
2681 dplane_ctx_get_nhe_nh_grp_count(ctx
),
2682 !!nhgr
->buckets
, nhgr
))
2685 const struct nexthop
*nh
=
2686 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2687 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2690 req
->nhm
.nh_family
= AF_INET
;
2691 else if (afi
== AFI_IP6
)
2692 req
->nhm
.nh_family
= AF_INET6
;
2695 case NEXTHOP_TYPE_IPV4
:
2696 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2697 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2702 case NEXTHOP_TYPE_IPV6
:
2703 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2704 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2709 case NEXTHOP_TYPE_BLACKHOLE
:
2710 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2713 /* Blackhole shouldn't have anymore attributes
2716 case NEXTHOP_TYPE_IFINDEX
:
2717 /* Don't need anymore info for this */
2723 EC_ZEBRA_NHG_FIB_UPDATE
,
2724 "Context received for kernel nexthop update without an interface");
2728 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2732 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2733 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2735 num_labels
= build_label_stack(
2736 nh
->nh_label
, nh
->nh_label_type
, out_lse
,
2737 label_buf
, sizeof(label_buf
));
2739 if (num_labels
&& nh
->nh_label_type
== ZEBRA_LSP_EVPN
) {
2740 if (!nl_attr_put16(&req
->n
, buflen
,
2745 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2749 if (_netlink_nexthop_encode_dvni_label(
2750 nh
, &req
->n
, out_lse
, buflen
,
2751 label_buf
) == false)
2754 nl_attr_nest_end(&req
->n
, nest
);
2756 } else if (num_labels
) {
2757 /* Set the BoS bit */
2758 out_lse
[num_labels
- 1] |=
2759 htonl(1 << MPLS_LS_S_SHIFT
);
2762 * TODO: MPLS unsupported for now in kernel.
2764 if (req
->nhm
.nh_family
== AF_MPLS
)
2767 encap
= LWTUNNEL_ENCAP_MPLS
;
2768 if (!nl_attr_put16(&req
->n
, buflen
,
2769 NHA_ENCAP_TYPE
, encap
))
2771 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2775 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2777 num_labels
* sizeof(mpls_lse_t
)))
2780 nl_attr_nest_end(&req
->n
, nest
);
2784 if (nh
->nh_srv6
->seg6local_action
!=
2785 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2788 struct rtattr
*nest
;
2789 const struct seg6local_context
*ctx
;
2791 req
->nhm
.nh_family
= AF_INET6
;
2792 action
= nh
->nh_srv6
->seg6local_action
;
2793 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2794 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2795 if (!nl_attr_put(&req
->n
, buflen
,
2801 nest
= nl_attr_nest(&req
->n
, buflen
,
2802 NHA_ENCAP
| NLA_F_NESTED
);
2807 case SEG6_LOCAL_ACTION_END
:
2811 SEG6_LOCAL_ACTION_END
))
2814 case SEG6_LOCAL_ACTION_END_X
:
2818 SEG6_LOCAL_ACTION_END_X
))
2822 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2823 sizeof(struct in6_addr
)))
2826 case SEG6_LOCAL_ACTION_END_T
:
2830 SEG6_LOCAL_ACTION_END_T
))
2838 case SEG6_LOCAL_ACTION_END_DX4
:
2842 SEG6_LOCAL_ACTION_END_DX4
))
2846 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2847 sizeof(struct in_addr
)))
2850 case SEG6_LOCAL_ACTION_END_DT6
:
2854 SEG6_LOCAL_ACTION_END_DT6
))
2862 case SEG6_LOCAL_ACTION_END_DT4
:
2866 SEG6_LOCAL_ACTION_END_DT4
))
2870 SEG6_LOCAL_VRFTABLE
,
2874 case SEG6_LOCAL_ACTION_END_DT46
:
2878 SEG6_LOCAL_ACTION_END_DT46
))
2882 SEG6_LOCAL_VRFTABLE
,
2887 zlog_err("%s: unsupport seg6local behaviour action=%u",
2891 nl_attr_nest_end(&req
->n
, nest
);
2894 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2897 struct rtattr
*nest
;
2899 if (!nl_attr_put16(&req
->n
, buflen
,
2901 LWTUNNEL_ENCAP_SEG6
))
2903 nest
= nl_attr_nest(&req
->n
, buflen
,
2904 NHA_ENCAP
| NLA_F_NESTED
);
2907 tun_len
= fill_seg6ipt_encap(tun_buf
,
2909 &nh
->nh_srv6
->seg6_segs
);
2912 if (!nl_attr_put(&req
->n
, buflen
,
2916 nl_attr_nest_end(&req
->n
, nest
);
2922 if (IS_ZEBRA_DEBUG_KERNEL
)
2923 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2924 __func__
, id
, nh
, nh
->ifindex
,
2925 vrf_id_to_name(nh
->vrf_id
),
2926 nh
->vrf_id
, label_buf
);
2929 req
->nhm
.nh_protocol
= zebra2proto(type
);
2931 } else if (cmd
!= RTM_DELNEXTHOP
) {
2933 EC_ZEBRA_NHG_FIB_UPDATE
,
2934 "Nexthop group kernel update command (%d) does not exist",
2939 if (IS_ZEBRA_DEBUG_KERNEL
)
2940 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2943 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2946 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2947 void *buf
, size_t buflen
)
2949 enum dplane_op_e op
;
2952 op
= dplane_ctx_get_op(ctx
);
2953 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2954 cmd
= RTM_NEWNEXTHOP
;
2955 else if (op
== DPLANE_OP_NH_DELETE
)
2956 cmd
= RTM_DELNEXTHOP
;
2958 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2959 "Context received for kernel nexthop update with incorrect OP code (%u)",
2964 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
, false);
2967 enum netlink_msg_status
2968 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2969 struct zebra_dplane_ctx
*ctx
)
2971 /* Nothing to do if the kernel doesn't support nexthop objects */
2972 if (!kernel_nexthops_supported())
2973 return FRR_NETLINK_SUCCESS
;
2975 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2979 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2980 void *buf
, size_t buflen
)
2982 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2983 buflen
, false, false);
2986 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2987 void *buf
, size_t buflen
)
2989 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2990 buflen
, false, false);
2993 enum netlink_msg_status
2994 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2997 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2999 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
3001 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
3003 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
3005 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
3006 /* Single 'replace' operation */
3009 * With route replace semantics in place
3010 * for v4 routes and the new route is a system
3011 * route we do not install anything.
3012 * The problem here is that the new system
3013 * route should cause us to withdraw from
3014 * the kernel the old non-system route
3016 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
3017 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
3018 return netlink_batch_add_msg(
3019 bth
, ctx
, netlink_delroute_msg_encoder
,
3023 * So v6 route replace semantics are not in
3024 * the kernel at this point as I understand it.
3025 * so let's do a delete then an add.
3026 * In the future once v6 route replace semantics
3027 * are in we can figure out what to do here to
3028 * allow working with old and new kernels.
3030 * I'm also intentionally ignoring the failure case
3031 * of the route delete. If that happens yeah we're
3034 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
3035 netlink_batch_add_msg(
3036 bth
, ctx
, netlink_delroute_msg_encoder
,
3042 return FRR_NETLINK_ERROR
;
3044 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
3045 return FRR_NETLINK_SUCCESS
;
3047 return netlink_batch_add_msg(bth
, ctx
,
3049 ? netlink_newroute_msg_encoder
3050 : netlink_delroute_msg_encoder
,
3055 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
3057 * @tb: Netlink RTA data
3058 * @family: Address family in the nhmsg
3059 * @ifp: Interface connected - this should be NULL, we fill it in
3060 * @ns_id: Namspace id
3062 * Return: New nexthop
3064 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
3065 unsigned char family
,
3066 struct interface
**ifp
,
3069 struct nexthop nh
= {};
3071 enum nexthop_types_t type
= 0;
3074 struct interface
*ifp_lookup
;
3076 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
3079 if (tb
[NHA_GATEWAY
]) {
3082 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
3086 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
3091 EC_ZEBRA_BAD_NHG_MESSAGE
,
3092 "Nexthop gateway with bad address family (%d) received from kernel",
3096 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
3098 type
= NEXTHOP_TYPE_IFINDEX
;
3104 memcpy(&(nh
.gate
), gate
, sz
);
3107 nh
.ifindex
= if_index
;
3110 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
3115 nh
.vrf_id
= ifp_lookup
->vrf
->vrf_id
;
3118 EC_ZEBRA_UNKNOWN_INTERFACE
,
3119 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
3120 __func__
, nh
.ifindex
);
3122 nh
.vrf_id
= VRF_DEFAULT
;
3125 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
3126 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
3129 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
3131 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
3132 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
3135 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
3142 static int netlink_nexthop_process_group(struct rtattr
**tb
,
3143 struct nh_grp
*z_grp
, int z_grp_size
,
3144 struct nhg_resilience
*nhgr
)
3147 /* linux/nexthop.h group struct */
3148 struct nexthop_grp
*n_grp
= NULL
;
3150 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
3151 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
3153 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
3154 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
3155 "Invalid nexthop group received from the kernel");
3159 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
3160 z_grp
[i
].id
= n_grp
[i
].id
;
3161 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
3164 memset(nhgr
, 0, sizeof(*nhgr
));
3165 if (tb
[NHA_RES_GROUP
]) {
3166 struct rtattr
*tbn
[NHA_RES_GROUP_MAX
+ 1];
3168 struct rtattr
*res_group
= tb
[NHA_RES_GROUP
];
3170 netlink_parse_rtattr_nested(tbn
, NHA_RES_GROUP_MAX
, res_group
);
3172 if (tbn
[NHA_RES_GROUP_BUCKETS
]) {
3173 rta
= tbn
[NHA_RES_GROUP_BUCKETS
];
3174 nhgr
->buckets
= *(uint16_t *)RTA_DATA(rta
);
3177 if (tbn
[NHA_RES_GROUP_IDLE_TIMER
]) {
3178 rta
= tbn
[NHA_RES_GROUP_IDLE_TIMER
];
3179 nhgr
->idle_timer
= *(uint32_t *)RTA_DATA(rta
);
3182 if (tbn
[NHA_RES_GROUP_UNBALANCED_TIMER
]) {
3183 rta
= tbn
[NHA_RES_GROUP_UNBALANCED_TIMER
];
3184 nhgr
->unbalanced_timer
= *(uint32_t *)RTA_DATA(rta
);
3187 if (tbn
[NHA_RES_GROUP_UNBALANCED_TIME
]) {
3188 rta
= tbn
[NHA_RES_GROUP_UNBALANCED_TIME
];
3189 nhgr
->unbalanced_time
= *(uint64_t *)RTA_DATA(rta
);
3197 * netlink_nexthop_change() - Read in change about nexthops from the kernel
3199 * @h: Netlink message header
3200 * @ns_id: Namspace id
3201 * @startup: Are we reading under startup conditions?
3203 * Return: Result status
3205 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3208 /* nexthop group id */
3210 unsigned char family
;
3212 afi_t afi
= AFI_UNSPEC
;
3213 vrf_id_t vrf_id
= VRF_DEFAULT
;
3214 struct interface
*ifp
= NULL
;
3215 struct nhmsg
*nhm
= NULL
;
3216 struct nexthop nh
= {};
3217 struct nh_grp grp
[MULTIPATH_NUM
] = {};
3218 /* Count of nexthops in group array */
3219 uint8_t grp_count
= 0;
3220 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
3222 frrtrace(3, frr_zebra
, netlink_nexthop_change
, h
, ns_id
, startup
);
3224 nhm
= NLMSG_DATA(h
);
3229 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
3232 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
3235 "%s: Message received from netlink is of a broken size %d %zu",
3236 __func__
, h
->nlmsg_len
,
3237 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
3241 netlink_parse_rtattr_flags(tb
, NHA_MAX
, RTM_NHA(nhm
), len
,
3247 EC_ZEBRA_BAD_NHG_MESSAGE
,
3248 "Nexthop group without an ID received from the kernel");
3252 /* We use the ID key'd nhg table for kernel updates */
3253 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
3255 if (zebra_evpn_mh_is_fdb_nh(id
)) {
3256 /* If this is a L2 NH just ignore it */
3257 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3258 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
3264 family
= nhm
->nh_family
;
3265 afi
= family2afi(family
);
3267 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
3269 if (IS_ZEBRA_DEBUG_KERNEL
)
3270 zlog_debug("%s ID (%u) %s NS %u",
3271 nl_msg_type_to_str(h
->nlmsg_type
), id
,
3272 nl_family_to_str(family
), ns_id
);
3275 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
3276 struct nhg_resilience nhgr
= {};
3278 if (tb
[NHA_GROUP
]) {
3280 * If this is a group message its only going to have
3281 * an array of nexthop IDs associated with it
3283 grp_count
= netlink_nexthop_process_group(
3284 tb
, grp
, array_size(grp
), &nhgr
);
3286 if (tb
[NHA_BLACKHOLE
]) {
3288 * This nexthop is just for blackhole-ing
3289 * traffic, it should not have an OIF, GATEWAY,
3292 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
3293 nh
.bh_type
= BLACKHOLE_UNSPEC
;
3294 } else if (tb
[NHA_OIF
])
3296 * This is a true new nexthop, so we need
3297 * to parse the gateway and device info
3299 nh
= netlink_nexthop_process_nh(tb
, family
,
3304 EC_ZEBRA_BAD_NHG_MESSAGE
,
3305 "Invalid Nexthop message received from the kernel with ID (%u)",
3309 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
3310 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
3311 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
3315 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
3316 type
, startup
, &nhgr
))
3319 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
3320 zebra_nhg_kernel_del(id
, vrf_id
);
3326 * netlink_request_nexthop() - Request nextop information from the kernel
3327 * @zns: Zebra namespace
3328 * @family: AF_* netlink family
3329 * @type: RTM_* route type
3331 * Return: Result status
3333 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
3340 /* Form the request, specifying filter (rtattr) if needed. */
3341 memset(&req
, 0, sizeof(req
));
3342 req
.n
.nlmsg_type
= type
;
3343 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3344 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3345 req
.nhm
.nh_family
= family
;
3347 return netlink_request(&zns
->netlink_cmd
, &req
);
3352 * netlink_nexthop_read() - Nexthop read function using netlink interface
3354 * @zns: Zebra name space
3356 * Return: Result status
3357 * Only called at bootstrap time.
3359 int netlink_nexthop_read(struct zebra_ns
*zns
)
3362 struct zebra_dplane_info dp_info
;
3364 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3366 /* Get nexthop objects */
3367 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3370 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3374 /* If we succesfully read in nexthop objects,
3375 * this kernel must support them.
3378 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3379 zlog_debug("Nexthop objects %ssupported on this kernel",
3380 supports_nh
? "" : "not ");
3382 zebra_router_set_supports_nhgs(supports_nh
);
3388 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3389 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3391 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3392 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3397 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3398 * evpn neighbor update as netlink messages using dataplane context object.
3399 * Here, a neighbor refers to a bridge forwarding database entry for
3400 * either unicast forwarding or head-end replication or an IP neighbor
3402 * @ctx: Dataplane context
3403 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3404 * @lla: A pointer to neighbor cache link layer address
3405 * @llalen: Length of the pointer to neighbor cache link layer
3407 * @ip: A neighbor cache n/w layer destination address
3408 * In the case of bridge FDB, this represnts the remote
3410 * @replace_obj: Whether NEW request should replace existing object or
3411 * add to the end of the list
3412 * @family: AF_* netlink family
3413 * @type: RTN_* route type
3414 * @flags: NTF_* flags
3415 * @state: NUD_* states
3416 * @data: data buffer pointer
3417 * @datalen: total amount of data buffer space
3418 * @protocol: protocol information
3420 * Return: 0 when the msg doesn't fit entirely in the buffer
3421 * otherwise the number of bytes written to buf.
3423 static ssize_t
netlink_neigh_update_msg_encode(
3424 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3425 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3426 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3427 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3428 size_t datalen
, uint8_t protocol
)
3436 enum dplane_op_e op
;
3438 if (datalen
< sizeof(*req
))
3440 memset(req
, 0, sizeof(*req
));
3442 op
= dplane_ctx_get_op(ctx
);
3444 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3445 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3446 if (cmd
== RTM_NEWNEIGH
)
3447 req
->n
.nlmsg_flags
|=
3449 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3450 req
->n
.nlmsg_type
= cmd
;
3451 req
->ndm
.ndm_family
= family
;
3452 req
->ndm
.ndm_type
= type
;
3453 req
->ndm
.ndm_state
= state
;
3454 req
->ndm
.ndm_flags
= flags
;
3455 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3457 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3462 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3467 struct rtattr
*nest
;
3469 nest
= nl_attr_nest(&req
->n
, datalen
,
3470 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3474 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3475 &nfy_flags
, sizeof(nfy_flags
)))
3477 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3480 nl_attr_nest_end(&req
->n
, nest
);
3485 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3491 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3495 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3496 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3501 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3502 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3503 vni_t vni
= dplane_ctx_mac_get_vni(ctx
);
3506 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3511 if (!nl_attr_put32(&req
->n
, datalen
, NDA_SRC_VNI
, vni
))
3515 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3516 dplane_ctx_mac_get_br_ifindex(ctx
)))
3520 if (op
== DPLANE_OP_VTEP_ADD
|| op
== DPLANE_OP_VTEP_DELETE
) {
3521 vni_t vni
= dplane_ctx_neigh_get_vni(ctx
);
3524 if (!nl_attr_put32(&req
->n
, datalen
, NDA_SRC_VNI
, vni
))
3529 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3533 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3534 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3537 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3538 void *buf
, size_t buflen
)
3540 struct ethaddr dst_mac
= {.octet
= {0}};
3541 int proto
= RTPROT_ZEBRA
;
3543 if (dplane_ctx_get_type(ctx
) != 0)
3544 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3546 return netlink_neigh_update_msg_encode(
3547 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3548 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3549 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3550 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3555 #define NDA_RTA(r) \
3556 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3559 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3562 struct interface
*ifp
;
3563 struct zebra_if
*zif
;
3564 struct rtattr
*tb
[NDA_MAX
+ 1];
3565 struct interface
*br_if
;
3568 struct in_addr vtep_ip
;
3569 int vid_present
= 0, dst_present
= 0;
3573 bool local_inactive
= false;
3574 bool dp_static
= false;
3576 uint32_t nhg_id
= 0;
3577 bool vni_mcast_grp
= false;
3579 ndm
= NLMSG_DATA(h
);
3581 /* We only process macfdb notifications if EVPN is enabled */
3582 if (!is_evpn_enabled())
3585 /* Parse attributes and extract fields of interest. Do basic
3586 * validation of the fields.
3588 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3591 if (!tb
[NDA_LLADDR
]) {
3592 if (IS_ZEBRA_DEBUG_KERNEL
)
3593 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3594 nl_msg_type_to_str(h
->nlmsg_type
),
3599 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3600 if (IS_ZEBRA_DEBUG_KERNEL
)
3602 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3603 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3604 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3608 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3612 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3613 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3617 /* TODO: Only IPv4 supported now. */
3619 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3621 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3624 memset(&vtep_ip
, 0, sizeof(vtep_ip
));
3627 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3629 if (ndm
->ndm_state
& NUD_STALE
)
3630 local_inactive
= true;
3632 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3633 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3634 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3636 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3637 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3640 nfy_flags
= *(uint8_t *)RTA_DATA(
3641 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3642 if (nfy_flags
& FDB_NOTIFY_BIT
)
3644 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3645 local_inactive
= true;
3649 if (tb
[NDA_SRC_VNI
])
3650 vni
= *(vni_t
*)RTA_DATA(tb
[NDA_SRC_VNI
]);
3652 if (IS_ZEBRA_DEBUG_KERNEL
)
3654 "Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d vni %d",
3655 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3656 vid_present
? vid_buf
: "", ndm
->ndm_state
,
3657 ndm
->ndm_flags
, &mac
, dst_present
? dst_buf
: "",
3660 /* The interface should exist. */
3661 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3663 if (!ifp
|| !ifp
->info
)
3666 /* The interface should be something we're interested in. */
3667 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3670 zif
= (struct zebra_if
*)ifp
->info
;
3671 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3672 if (IS_ZEBRA_DEBUG_KERNEL
)
3674 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3675 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3677 zif
->brslave_info
.bridge_ifindex
);
3681 /* For per vni device, vni comes from device itself */
3682 if (IS_ZEBRA_IF_VXLAN(ifp
) && IS_ZEBRA_VXLAN_IF_VNI(zif
)) {
3683 struct zebra_vxlan_vni
*vnip
;
3685 vnip
= zebra_vxlan_if_vni_find(zif
, 0);
3689 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3691 if (filter_vlan
&& vid
!= filter_vlan
) {
3692 if (IS_ZEBRA_DEBUG_KERNEL
)
3693 zlog_debug(" Filtered due to filter vlan: %d",
3699 * Check if this is a mcast group update (svd case)
3701 vni_mcast_grp
= is_mac_vni_mcast_group(&mac
, vni
, vtep_ip
);
3703 /* If add or update, do accordingly if learnt on a "local" interface; if
3704 * the notification is over VxLAN, this has to be related to
3706 * so perform an implicit delete of any local entry (if it exists).
3708 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3709 /* Drop "permanent" entries. */
3710 if (!vni_mcast_grp
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3711 if (IS_ZEBRA_DEBUG_KERNEL
)
3713 " Dropping entry because of NUD_PERMANENT");
3717 if (IS_ZEBRA_IF_VXLAN(ifp
)) {
3722 return zebra_vxlan_if_vni_mcast_group_add_update(
3723 ifp
, vni
, &vtep_ip
);
3725 return zebra_vxlan_dp_network_mac_add(
3726 ifp
, br_if
, &mac
, vid
, vni
, nhg_id
, sticky
,
3727 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3730 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3731 sticky
, local_inactive
, dp_static
);
3734 /* This is a delete notification.
3735 * Ignore the notification with IP dest as it may just signify that the
3736 * MAC has moved from remote to local. The exception is the special
3737 * all-zeros MAC that represents the BUM flooding entry; we may have
3738 * to readd it. Otherwise,
3739 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3740 * 2. For a MAC over "local" interface, delete the mac
3741 * Note: We will get notifications from both bridge driver and VxLAN
3749 return zebra_vxlan_if_vni_mcast_group_del(ifp
, vni
,
3752 if (is_zero_mac(&mac
) && vni
)
3753 return zebra_vxlan_check_readd_vtep(ifp
, vni
, vtep_ip
);
3758 if (IS_ZEBRA_IF_VXLAN(ifp
))
3761 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3764 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3769 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3772 /* Length validity. */
3773 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3777 /* We are interested only in AF_BRIDGE notifications. */
3778 ndm
= NLMSG_DATA(h
);
3779 if (ndm
->ndm_family
!= AF_BRIDGE
)
3782 return netlink_macfdb_change(h
, len
, ns_id
);
3785 /* Request for MAC FDB information from the kernel */
3786 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3787 int type
, ifindex_t master_ifindex
)
3791 struct ifinfomsg ifm
;
3795 /* Form the request, specifying filter (rtattr) if needed. */
3796 memset(&req
, 0, sizeof(req
));
3797 req
.n
.nlmsg_type
= type
;
3798 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3799 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3800 req
.ifm
.ifi_family
= family
;
3802 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3804 return netlink_request(netlink_cmd
, &req
);
3808 * MAC forwarding database read using netlink interface. This is invoked
3811 int netlink_macfdb_read(struct zebra_ns
*zns
)
3814 struct zebra_dplane_info dp_info
;
3816 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3818 /* Get bridge FDB table. */
3819 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3823 /* We are reading entire table. */
3825 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3832 * MAC forwarding database read using netlink interface. This is for a
3833 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3835 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3836 struct interface
*br_if
, vlanid_t vid
)
3838 struct zebra_if
*br_zif
;
3839 struct zebra_dplane_info dp_info
;
3842 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3844 /* Save VLAN we're filtering on, if needed. */
3845 br_zif
= (struct zebra_if
*)br_if
->info
;
3846 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3849 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3851 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3855 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3856 &dp_info
, 0, false);
3858 /* Reset VLAN filter. */
3864 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3865 static int netlink_request_specific_mac(struct zebra_ns
*zns
, int family
,
3866 int type
, struct interface
*ifp
,
3867 const struct ethaddr
*mac
, vlanid_t vid
,
3868 vni_t vni
, uint8_t flags
)
3875 struct zebra_if
*zif
;
3877 memset(&req
, 0, sizeof(req
));
3878 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3879 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3880 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3881 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3882 req
.ndm
.ndm_flags
= flags
;
3883 /* req.ndm.ndm_state = NUD_REACHABLE; */
3885 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3887 zif
= (struct zebra_if
*)ifp
->info
;
3888 /* Is this a read on a VXLAN interface? */
3889 if (IS_ZEBRA_IF_VXLAN(ifp
)) {
3890 nl_attr_put32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
3891 /* TBD: Why is ifindex not filled in the non-vxlan case? */
3892 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
3894 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(zif
) && vid
> 0)
3895 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3896 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, ifp
->ifindex
);
3899 if (IS_ZEBRA_DEBUG_KERNEL
)
3900 zlog_debug("Tx %s %s IF %s(%u) MAC %pEA vid %u vni %u",
3901 nl_msg_type_to_str(type
),
3902 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
3903 ifp
->ifindex
, mac
, vid
, vni
);
3905 return netlink_request(&zns
->netlink_cmd
, &req
);
3908 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3909 struct interface
*br_if
,
3910 const struct ethaddr
*mac
, vlanid_t vid
)
3913 struct zebra_dplane_info dp_info
;
3915 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3917 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3919 ret
= netlink_request_specific_mac(zns
, AF_BRIDGE
, RTM_GETNEIGH
, br_if
,
3924 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3930 int netlink_macfdb_read_mcast_for_vni(struct zebra_ns
*zns
,
3931 struct interface
*ifp
, vni_t vni
)
3933 struct zebra_if
*zif
;
3934 struct ethaddr mac
= {.octet
= {0}};
3935 struct zebra_dplane_info dp_info
;
3939 if (IS_ZEBRA_VXLAN_IF_VNI(zif
))
3942 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3944 /* Get specific FDB entry for BUM handling, if any */
3945 ret
= netlink_request_specific_mac(zns
, AF_BRIDGE
, RTM_GETNEIGH
, ifp
,
3946 &mac
, 0, vni
, NTF_SELF
);
3950 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3951 &dp_info
, 1, false);
3957 * Netlink-specific handler for MAC updates using dataplane context object.
3959 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3962 struct ipaddr vtep_ip
;
3969 uint32_t update_flags
;
3971 uint8_t nfy_flags
= 0;
3972 int proto
= RTPROT_ZEBRA
;
3974 if (dplane_ctx_get_type(ctx
) != 0)
3975 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3977 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3978 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3981 state
= NUD_REACHABLE
;
3983 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3984 if (update_flags
& DPLANE_MAC_REMOTE
) {
3986 if (dplane_ctx_mac_is_sticky(ctx
)) {
3987 /* NUD_NOARP prevents the entry from expiring */
3989 /* sticky the entry from moving */
3990 flags
|= NTF_STICKY
;
3992 flags
|= NTF_EXT_LEARNED
;
3994 /* if it was static-local previously we need to clear the
3995 * notify flags on replace with remote
3997 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
4001 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
4002 nfy_flags
|= FDB_NOTIFY_BIT
;
4006 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
4007 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
4012 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
4013 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
4014 SET_IPADDR_V4(&vtep_ip
);
4016 if (IS_ZEBRA_DEBUG_KERNEL
) {
4018 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
4020 vid
= dplane_ctx_mac_get_vlan(ctx
);
4022 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
4027 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
4028 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
4029 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4030 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
4031 mac
, &vtep_ip
, nhg_id
,
4032 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
4033 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
4035 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
4036 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
4041 total
= netlink_neigh_update_msg_encode(
4042 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
4043 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
4044 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
4051 * In the event the kernel deletes ipv4 link-local neighbor entries created for
4052 * 5549 support, re-install them.
4054 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
4055 struct interface
*ifp
, struct ipaddr
*ip
,
4058 if (ndm
->ndm_family
!= AF_INET
)
4061 if (!zif
->v6_2_v4_ll_neigh_entry
)
4064 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
4067 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
4068 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
4073 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
4077 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
4079 #define NUD_LOCAL_ACTIVE \
4080 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
4082 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
4084 /* an exact match is done between
4085 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
4086 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
4087 * (see in lib/zclient.h)
4091 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
4094 struct interface
*ifp
;
4095 struct zebra_if
*zif
;
4096 struct rtattr
*tb
[NDA_MAX
+ 1];
4097 struct interface
*link_if
;
4100 char buf
[ETHER_ADDR_STRLEN
];
4101 int mac_present
= 0;
4104 bool local_inactive
;
4105 uint32_t ext_flags
= 0;
4106 bool dp_static
= false;
4110 ndm
= NLMSG_DATA(h
);
4112 /* The interface should exist. */
4113 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
4115 if (!ifp
|| !ifp
->info
)
4118 zif
= (struct zebra_if
*)ifp
->info
;
4120 /* Parse attributes and extract fields of interest. */
4121 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
4124 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
4125 nl_msg_type_to_str(h
->nlmsg_type
),
4126 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4127 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
4131 memset(&ip
, 0, sizeof(ip
));
4132 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
4133 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
4135 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
4136 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
4137 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
4138 if (IS_ZEBRA_DEBUG_KERNEL
)
4140 " Neighbor Entry Received is a 5549 entry, finished");
4144 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
4145 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
4146 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
4148 /* we send link layer information to client:
4149 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
4150 * - struct ipaddr ( for DEL and GET)
4151 * - struct ethaddr mac; (for NEW)
4153 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
4154 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
4155 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
4156 cmd
= ZEBRA_NHRP_NEIGH_GET
;
4157 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
4158 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
4160 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
4164 if (tb
[NDA_LLADDR
]) {
4165 /* copy LLADDR information */
4166 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
4168 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
4169 union sockunion link_layer_ipv4
;
4172 sockunion_family(&link_layer_ipv4
) = AF_INET
;
4173 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
4174 RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
4176 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
4177 zsend_nhrp_neighbor_notify(
4179 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
4183 if (h
->nlmsg_type
== RTM_GETNEIGH
)
4186 /* The neighbor is present on an SVI. From this, we locate the
4188 * bridge because we're only interested in neighbors on a VxLAN bridge.
4189 * The bridge is located based on the nature of the SVI:
4190 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
4192 * and is linked to the bridge
4193 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
4197 if (IS_ZEBRA_IF_VLAN(ifp
)) {
4198 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
4202 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
4206 if (IS_ZEBRA_DEBUG_KERNEL
)
4208 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
4211 memset(&mac
, 0, sizeof(mac
));
4212 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
4213 if (tb
[NDA_LLADDR
]) {
4214 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4215 if (IS_ZEBRA_DEBUG_KERNEL
)
4217 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
4222 ifp
->name
, ndm
->ndm_ifindex
,
4225 (unsigned long)RTA_PAYLOAD(
4231 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
4234 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
4235 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
4237 if (tb
[NDA_EXT_FLAGS
]) {
4238 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
4239 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
4243 if (IS_ZEBRA_DEBUG_KERNEL
)
4245 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
4246 nl_msg_type_to_str(h
->nlmsg_type
),
4247 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4248 ndm
->ndm_ifindex
, ifp
->vrf
->name
,
4249 ifp
->vrf
->vrf_id
, &ip
,
4251 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
4253 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
4255 /* If the neighbor state is valid for use, process as an add or
4257 * else process as a delete. Note that the delete handling may
4259 * in re-adding the neighbor if it is a valid "remote" neighbor.
4261 if (ndm
->ndm_state
& NUD_VALID
) {
4262 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
4264 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
4266 /* If EVPN-MH is not enabled we treat STALE
4267 * neighbors as locally-active and advertise
4270 local_inactive
= false;
4272 /* Add local neighbors to the l3 interface database */
4274 zebra_neigh_del(ifp
, &ip
);
4276 zebra_neigh_add(ifp
, &ip
, &mac
);
4279 zebra_vxlan_handle_kernel_neigh_update(
4280 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
4281 is_ext
, is_router
, local_inactive
,
4287 zebra_neigh_del(ifp
, &ip
);
4289 zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
4293 if (IS_ZEBRA_DEBUG_KERNEL
)
4294 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
4295 nl_msg_type_to_str(h
->nlmsg_type
),
4296 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4297 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
4300 /* Process the delete - it may result in re-adding the neighbor if it is
4301 * a valid "remote" neighbor.
4303 zebra_neigh_del(ifp
, &ip
);
4305 zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
4310 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
4315 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
4318 /* Length validity. */
4319 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4323 /* We are interested only in AF_INET or AF_INET6 notifications. */
4324 ndm
= NLMSG_DATA(h
);
4325 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
4328 return netlink_neigh_change(h
, len
);
4331 /* Request for IP neighbor information from the kernel */
4332 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
4333 int type
, ifindex_t ifindex
)
4341 /* Form the request, specifying filter (rtattr) if needed. */
4342 memset(&req
, 0, sizeof(req
));
4343 req
.n
.nlmsg_type
= type
;
4344 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
4345 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4346 req
.ndm
.ndm_family
= family
;
4348 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
4350 return netlink_request(netlink_cmd
, &req
);
4354 * IP Neighbor table read using netlink interface. This is invoked
4357 int netlink_neigh_read(struct zebra_ns
*zns
)
4360 struct zebra_dplane_info dp_info
;
4362 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4364 /* Get IP neighbor table. */
4365 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4369 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4376 * IP Neighbor table read using netlink interface. This is for a specific
4379 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
4382 struct zebra_dplane_info dp_info
;
4384 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4386 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4390 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4391 &dp_info
, 0, false);
4397 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
4398 * read using netlink interface.
4400 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
4402 const struct ipaddr
*ip
,
4412 /* Form the request, specifying filter (rtattr) if needed. */
4413 memset(&req
, 0, sizeof(req
));
4414 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4415 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4416 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
4417 req
.ndm
.ndm_ifindex
= ifindex
;
4419 if (IS_IPADDR_V4(ip
)) {
4420 ipa_len
= IPV4_MAX_BYTELEN
;
4421 req
.ndm
.ndm_family
= AF_INET
;
4424 ipa_len
= IPV6_MAX_BYTELEN
;
4425 req
.ndm
.ndm_family
= AF_INET6
;
4428 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
4430 if (IS_ZEBRA_DEBUG_KERNEL
)
4431 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
4432 __func__
, nl_msg_type_to_str(type
),
4433 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
4436 return netlink_request(&zns
->netlink_cmd
, &req
);
4439 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4440 struct interface
*vlan_if
)
4443 struct zebra_ns
*zns
;
4444 struct zebra_vrf
*zvrf
= vlan_if
->vrf
->info
;
4445 struct zebra_dplane_info dp_info
;
4449 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4451 if (IS_ZEBRA_DEBUG_KERNEL
)
4452 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4453 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4454 vlan_if
->vrf
->name
, vlan_if
->vrf
->vrf_id
);
4456 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4461 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4462 &dp_info
, 1, false);
4467 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4472 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4473 || h
->nlmsg_type
== RTM_GETNEIGH
))
4476 /* Length validity. */
4477 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4480 "%s: Message received from netlink is of a broken size %d %zu",
4481 __func__
, h
->nlmsg_len
,
4482 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4486 /* Is this a notification for the MAC FDB or IP neighbor table? */
4487 ndm
= NLMSG_DATA(h
);
4488 if (ndm
->ndm_family
== AF_BRIDGE
)
4489 return netlink_macfdb_change(h
, len
, ns_id
);
4491 if (ndm
->ndm_type
!= RTN_UNICAST
)
4494 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4495 return netlink_ipneigh_change(h
, len
, ns_id
);
4498 EC_ZEBRA_UNKNOWN_FAMILY
,
4499 "Invalid address family: %u received from kernel neighbor change: %s",
4500 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4508 * Utility neighbor-update function, using info from dplane context.
4510 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4511 int cmd
, void *buf
, size_t buflen
)
4513 const struct ipaddr
*ip
;
4514 const struct ethaddr
*mac
= NULL
;
4515 const struct ipaddr
*link_ip
= NULL
;
4516 const void *link_ptr
= NULL
;
4517 char buf2
[ETHER_ADDR_STRLEN
];
4523 uint32_t update_flags
;
4524 uint32_t ext_flags
= 0;
4526 int proto
= RTPROT_ZEBRA
;
4528 if (dplane_ctx_get_type(ctx
) != 0)
4529 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4531 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4533 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4534 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4535 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4536 llalen
= IPADDRSZ(link_ip
);
4537 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4538 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4540 mac
= dplane_ctx_neigh_get_mac(ctx
);
4542 link_ptr
= (const void *)mac
;
4543 if (is_zero_mac(mac
))
4546 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4548 snprintf(buf2
, sizeof(buf2
), "null");
4550 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4551 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4552 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4554 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4556 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4557 flags
|= NTF_EXT_LEARNED
;
4558 /* if it was static-local previously we need to clear the
4559 * ext flags on replace with remote
4561 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4563 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4566 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4567 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4569 if (IS_ZEBRA_DEBUG_KERNEL
)
4571 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4572 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4573 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4574 ip
, link_ip
? "Link" : "MAC", buf2
, flags
, state
,
4575 ext
? "ext " : "", ext_flags
);
4577 return netlink_neigh_update_msg_encode(
4578 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4579 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4580 ext_flags
, buf
, buflen
, proto
);
4583 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4584 void *data
, size_t datalen
)
4591 struct rtattr
*nest
;
4596 if (datalen
< sizeof(*req
))
4598 memset(req
, 0, sizeof(*req
));
4599 family
= dplane_ctx_neightable_get_family(ctx
);
4600 idx
= dplane_ctx_get_ifindex(ctx
);
4602 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4603 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4604 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4605 req
->ndtm
.ndtm_family
= family
;
4607 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4608 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4609 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4612 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4614 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4615 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4617 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4618 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4621 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4622 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4625 nl_attr_nest_end(&req
->n
, nest
);
4627 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4630 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4631 void *buf
, size_t buflen
)
4635 switch (dplane_ctx_get_op(ctx
)) {
4636 case DPLANE_OP_NEIGH_INSTALL
:
4637 case DPLANE_OP_NEIGH_UPDATE
:
4638 case DPLANE_OP_NEIGH_DISCOVER
:
4639 case DPLANE_OP_NEIGH_IP_INSTALL
:
4640 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4642 case DPLANE_OP_NEIGH_DELETE
:
4643 case DPLANE_OP_NEIGH_IP_DELETE
:
4644 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4646 case DPLANE_OP_VTEP_ADD
:
4647 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4650 case DPLANE_OP_VTEP_DELETE
:
4651 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4654 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4655 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4657 case DPLANE_OP_ROUTE_INSTALL
:
4658 case DPLANE_OP_ROUTE_UPDATE
:
4659 case DPLANE_OP_ROUTE_DELETE
:
4660 case DPLANE_OP_ROUTE_NOTIFY
:
4661 case DPLANE_OP_NH_INSTALL
:
4662 case DPLANE_OP_NH_UPDATE
:
4663 case DPLANE_OP_NH_DELETE
:
4664 case DPLANE_OP_LSP_INSTALL
:
4665 case DPLANE_OP_LSP_UPDATE
:
4666 case DPLANE_OP_LSP_DELETE
:
4667 case DPLANE_OP_LSP_NOTIFY
:
4668 case DPLANE_OP_PW_INSTALL
:
4669 case DPLANE_OP_PW_UNINSTALL
:
4670 case DPLANE_OP_SYS_ROUTE_ADD
:
4671 case DPLANE_OP_SYS_ROUTE_DELETE
:
4672 case DPLANE_OP_ADDR_INSTALL
:
4673 case DPLANE_OP_ADDR_UNINSTALL
:
4674 case DPLANE_OP_MAC_INSTALL
:
4675 case DPLANE_OP_MAC_DELETE
:
4676 case DPLANE_OP_RULE_ADD
:
4677 case DPLANE_OP_RULE_DELETE
:
4678 case DPLANE_OP_RULE_UPDATE
:
4679 case DPLANE_OP_BR_PORT_UPDATE
:
4680 case DPLANE_OP_IPTABLE_ADD
:
4681 case DPLANE_OP_IPTABLE_DELETE
:
4682 case DPLANE_OP_IPSET_ADD
:
4683 case DPLANE_OP_IPSET_DELETE
:
4684 case DPLANE_OP_IPSET_ENTRY_ADD
:
4685 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4686 case DPLANE_OP_GRE_SET
:
4687 case DPLANE_OP_INTF_ADDR_ADD
:
4688 case DPLANE_OP_INTF_ADDR_DEL
:
4689 case DPLANE_OP_INTF_NETCONFIG
:
4690 case DPLANE_OP_INTF_INSTALL
:
4691 case DPLANE_OP_INTF_UPDATE
:
4692 case DPLANE_OP_INTF_DELETE
:
4693 case DPLANE_OP_TC_QDISC_INSTALL
:
4694 case DPLANE_OP_TC_QDISC_UNINSTALL
:
4695 case DPLANE_OP_TC_CLASS_ADD
:
4696 case DPLANE_OP_TC_CLASS_DELETE
:
4697 case DPLANE_OP_TC_CLASS_UPDATE
:
4698 case DPLANE_OP_TC_FILTER_ADD
:
4699 case DPLANE_OP_TC_FILTER_DELETE
:
4700 case DPLANE_OP_TC_FILTER_UPDATE
:
4701 case DPLANE_OP_NONE
:
4709 * Update MAC, using dataplane context object.
4712 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4713 struct zebra_dplane_ctx
*ctx
)
4715 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4719 enum netlink_msg_status
4720 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4722 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4727 * MPLS label forwarding table change via netlink interface, using dataplane
4728 * context information.
4730 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4731 void *buf
, size_t buflen
)
4734 const struct nhlfe_list_head
*head
;
4735 const struct zebra_nhlfe
*nhlfe
;
4736 struct nexthop
*nexthop
= NULL
;
4737 unsigned int nexthop_num
;
4738 const char *routedesc
;
4740 struct prefix p
= {0};
4742 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
4750 if (buflen
< sizeof(*req
))
4753 memset(req
, 0, sizeof(*req
));
4756 * Count # nexthops so we can decide whether to use singlepath
4757 * or multipath case.
4760 head
= dplane_ctx_get_nhlfe_list(ctx
);
4761 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4762 nexthop
= nhlfe
->nexthop
;
4765 if (cmd
== RTM_NEWROUTE
) {
4766 /* Count all selected NHLFEs */
4767 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4768 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4771 /* Count all installed NHLFEs */
4772 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4773 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4778 if ((nexthop_num
== 0) ||
4779 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4782 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4783 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4784 req
->n
.nlmsg_type
= cmd
;
4785 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
4787 req
->r
.rtm_family
= AF_MPLS
;
4788 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4789 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4790 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4791 req
->r
.rtm_type
= RTN_UNICAST
;
4793 if (cmd
== RTM_NEWROUTE
) {
4794 /* We do a replace to handle update. */
4795 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4797 /* set the protocol value if installing */
4798 route_type
= re_type_from_lsp_type(
4799 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4800 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4803 /* Fill destination */
4804 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4805 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4808 /* Fill nexthops (paths) based on single-path or multipath. The paths
4809 * chosen depend on the operation.
4811 if (nexthop_num
== 1) {
4812 routedesc
= "single-path";
4813 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4817 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4818 nexthop
= nhlfe
->nexthop
;
4822 if ((cmd
== RTM_NEWROUTE
4823 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4824 && CHECK_FLAG(nexthop
->flags
,
4825 NEXTHOP_FLAG_ACTIVE
)))
4826 || (cmd
== RTM_DELROUTE
4827 && (CHECK_FLAG(nhlfe
->flags
,
4828 NHLFE_FLAG_INSTALLED
)
4829 && CHECK_FLAG(nexthop
->flags
,
4830 NEXTHOP_FLAG_FIB
)))) {
4831 /* Add the gateway */
4832 if (!_netlink_mpls_build_singlepath(
4833 &p
, routedesc
, nhlfe
, &req
->n
,
4834 &req
->r
, buflen
, cmd
))
4841 } else { /* Multipath case */
4842 struct rtattr
*nest
;
4843 const union g_addr
*src1
= NULL
;
4845 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4849 routedesc
= "multipath";
4850 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4854 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4855 nexthop
= nhlfe
->nexthop
;
4859 if ((cmd
== RTM_NEWROUTE
4860 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4861 && CHECK_FLAG(nexthop
->flags
,
4862 NEXTHOP_FLAG_ACTIVE
)))
4863 || (cmd
== RTM_DELROUTE
4864 && (CHECK_FLAG(nhlfe
->flags
,
4865 NHLFE_FLAG_INSTALLED
)
4866 && CHECK_FLAG(nexthop
->flags
,
4867 NEXTHOP_FLAG_FIB
)))) {
4870 /* Build the multipath */
4871 if (!_netlink_mpls_build_multipath(
4872 &p
, routedesc
, nhlfe
, &req
->n
,
4873 buflen
, &req
->r
, &src1
))
4878 /* Add the multipath */
4879 nl_attr_nest_end(&req
->n
, nest
);
4882 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4885 /****************************************************************************
4886 * This code was developed in a branch that didn't have dplane APIs for
4887 * MAC updates. Hence the use of the legacy style. It will be moved to
4888 * the new dplane style pre-merge to master. XXX
4890 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4897 int cmd
= RTM_NEWNEXTHOP
;
4898 struct zebra_vrf
*zvrf
;
4899 struct zebra_ns
*zns
;
4901 zvrf
= zebra_vrf_get_evpn();
4904 memset(&req
, 0, sizeof(req
));
4906 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4907 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4908 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4909 req
.n
.nlmsg_type
= cmd
;
4910 req
.nhm
.nh_family
= AF_INET
;
4912 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4914 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4916 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4917 &vtep_ip
, IPV4_MAX_BYTELEN
))
4920 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4921 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4922 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4925 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4929 static int netlink_fdb_nh_del(uint32_t nh_id
)
4936 int cmd
= RTM_DELNEXTHOP
;
4937 struct zebra_vrf
*zvrf
;
4938 struct zebra_ns
*zns
;
4940 zvrf
= zebra_vrf_get_evpn();
4943 memset(&req
, 0, sizeof(req
));
4945 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4946 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4947 req
.n
.nlmsg_type
= cmd
;
4948 req
.nhm
.nh_family
= AF_UNSPEC
;
4950 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4953 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4954 zlog_debug("Tx %s fdb-nh 0x%x",
4955 nl_msg_type_to_str(cmd
), nh_id
);
4958 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4962 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4963 struct nh_grp
*nh_ids
)
4970 int cmd
= RTM_NEWNEXTHOP
;
4971 struct zebra_vrf
*zvrf
;
4972 struct zebra_ns
*zns
;
4973 struct nexthop_grp grp
[nh_cnt
];
4976 zvrf
= zebra_vrf_get_evpn();
4979 memset(&req
, 0, sizeof(req
));
4981 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4982 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4983 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4984 req
.n
.nlmsg_type
= cmd
;
4985 req
.nhm
.nh_family
= AF_UNSPEC
;
4987 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4989 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4991 memset(&grp
, 0, sizeof(grp
));
4992 for (i
= 0; i
< nh_cnt
; ++i
) {
4993 grp
[i
].id
= nh_ids
[i
].id
;
4994 grp
[i
].weight
= nh_ids
[i
].weight
;
4996 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4997 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
5001 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
5002 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
5006 for (i
= 0; i
< nh_cnt
; ++i
) {
5007 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
5009 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
5012 zlog_debug("Tx %s fdb-nhg 0x%x %s",
5013 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
5016 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
5020 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
5022 return netlink_fdb_nh_del(nhg_id
);
5025 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
5027 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
5030 int kernel_del_mac_nh(uint32_t nh_id
)
5032 return netlink_fdb_nh_del(nh_id
);
5035 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
5036 struct nh_grp
*nh_ids
)
5038 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
5041 int kernel_del_mac_nhg(uint32_t nhg_id
)
5043 return netlink_fdb_nhg_del(nhg_id
);
5046 #endif /* HAVE_NETLINK */