1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 /* The following definition is to workaround an issue in the Linux kernel
26 * header files with redefinition of 'struct in6_addr' in both
27 * netinet/in.h and linux/in6.h.
28 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
32 #include <net/if_arp.h>
33 #include <linux/lwtunnel.h>
34 #include <linux/mpls_iptunnel.h>
35 #include <linux/seg6_iptunnel.h>
36 #include <linux/seg6_local.h>
37 #include <linux/neighbour.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/nexthop.h>
41 /* Hack for GNU libc version 2. */
43 #define MSG_TRUNC 0x20
44 #endif /* MSG_TRUNC */
51 #include "plist_int.h"
52 #include "connected.h"
65 #include "zebra/zapi_msg.h"
66 #include "zebra/zebra_ns.h"
67 #include "zebra/zebra_vrf.h"
69 #include "zebra/redistribute.h"
70 #include "zebra/interface.h"
71 #include "zebra/debug.h"
72 #include "zebra/rtadv.h"
73 #include "zebra/zebra_ptm.h"
74 #include "zebra/zebra_mpls.h"
75 #include "zebra/kernel_netlink.h"
76 #include "zebra/rt_netlink.h"
77 #include "zebra/zebra_nhg.h"
78 #include "zebra/zebra_mroute.h"
79 #include "zebra/zebra_vxlan.h"
80 #include "zebra/zebra_errors.h"
81 #include "zebra/zebra_evpn_mh.h"
82 #include "zebra/zebra_trace.h"
83 #include "zebra/zebra_neigh.h"
89 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
90 * UAPI for MAC sync. */
91 #ifndef _UAPI_LINUX_IF_BRIDGE_H
92 #define BR_SPH_LIST_SIZE 10
95 static vlanid_t filter_vlan
= 0;
97 /* We capture whether the current kernel supports nexthop ids; by
98 * default, we'll use them if possible. There's also a configuration
99 * available to _disable_ use of kernel nexthops.
101 static bool supports_nh
;
109 static const char ipv4_ll_buf
[16] = "169.254.0.1";
110 static struct in_addr ipv4_ll
;
112 /* Is this a ipv4 over ipv6 route? */
113 static bool is_route_v4_over_v6(unsigned char rtm_family
,
114 enum nexthop_types_t nexthop_type
)
116 if (rtm_family
== AF_INET
117 && (nexthop_type
== NEXTHOP_TYPE_IPV6
118 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
124 /* Helper to control use of kernel-level nexthop ids */
125 static bool kernel_nexthops_supported(void)
127 return (supports_nh
&& !vrf_is_backend_netns()
128 && zebra_nhg_kernel_nexthops_enabled());
132 * Some people may only want to use NHGs created by protos and not
133 * implicitly created by Zebra. This check accounts for that.
135 static bool proto_nexthops_only(void)
137 return zebra_nhg_proto_nexthops_only();
140 /* Is this a proto created NHG? */
141 static bool is_proto_nhg(uint32_t id
, int type
)
143 /* If type is available, use it as the source of truth */
145 if (type
!= ZEBRA_ROUTE_NHG
)
150 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
156 /* Is vni mcast group */
157 static bool is_mac_vni_mcast_group(struct ethaddr
*mac
, vni_t vni
,
158 struct in_addr grp_addr
)
163 if (!is_zero_mac(mac
))
166 if (!IN_MULTICAST(ntohl(grp_addr
.s_addr
)))
173 * The ipv4_ll data structure is used for all 5549
174 * additions to the kernel. Let's figure out the
175 * correct value one time instead for every
176 * install/remove of a 5549 type route
178 void rt_netlink_init(void)
180 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
184 * Mapping from dataplane neighbor flags to netlink flags
186 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
190 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
191 flags
|= NTF_EXT_LEARNED
;
192 if (dplane_flags
& DPLANE_NTF_ROUTER
)
194 if (dplane_flags
& DPLANE_NTF_USE
)
201 * Mapping from dataplane neighbor state to netlink state
203 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
207 if (dplane_state
& DPLANE_NUD_REACHABLE
)
208 state
|= NUD_REACHABLE
;
209 if (dplane_state
& DPLANE_NUD_STALE
)
211 if (dplane_state
& DPLANE_NUD_NOARP
)
213 if (dplane_state
& DPLANE_NUD_PROBE
)
215 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
216 state
|= NUD_INCOMPLETE
;
217 if (dplane_state
& DPLANE_NUD_PERMANENT
)
218 state
|= NUD_PERMANENT
;
219 if (dplane_state
& DPLANE_NUD_FAILED
)
226 static inline bool is_selfroute(int proto
)
228 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
229 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
230 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
231 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
232 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
233 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
234 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
235 || (proto
== RTPROT_SRTE
)) {
242 int zebra2proto(int proto
)
245 case ZEBRA_ROUTE_BABEL
:
246 proto
= RTPROT_BABEL
;
248 case ZEBRA_ROUTE_BGP
:
251 case ZEBRA_ROUTE_OSPF
:
252 case ZEBRA_ROUTE_OSPF6
:
255 case ZEBRA_ROUTE_STATIC
:
256 proto
= RTPROT_ZSTATIC
;
258 case ZEBRA_ROUTE_ISIS
:
261 case ZEBRA_ROUTE_RIP
:
264 case ZEBRA_ROUTE_RIPNG
:
265 proto
= RTPROT_RIPNG
;
267 case ZEBRA_ROUTE_NHRP
:
270 case ZEBRA_ROUTE_EIGRP
:
271 proto
= RTPROT_EIGRP
;
273 case ZEBRA_ROUTE_LDP
:
276 case ZEBRA_ROUTE_SHARP
:
277 proto
= RTPROT_SHARP
;
279 case ZEBRA_ROUTE_PBR
:
282 case ZEBRA_ROUTE_OPENFABRIC
:
283 proto
= RTPROT_OPENFABRIC
;
285 case ZEBRA_ROUTE_SRTE
:
288 case ZEBRA_ROUTE_TABLE
:
289 case ZEBRA_ROUTE_NHG
:
290 proto
= RTPROT_ZEBRA
;
292 case ZEBRA_ROUTE_CONNECT
:
293 case ZEBRA_ROUTE_KERNEL
:
294 proto
= RTPROT_KERNEL
;
298 * When a user adds a new protocol this will show up
299 * to let them know to do something about it. This
300 * is intentionally a warn because we should see
301 * this as part of development of a new protocol
304 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
306 proto
= RTPROT_ZEBRA
;
313 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
317 proto
= ZEBRA_ROUTE_BABEL
;
320 proto
= ZEBRA_ROUTE_BGP
;
323 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
327 proto
= ZEBRA_ROUTE_ISIS
;
330 proto
= ZEBRA_ROUTE_RIP
;
333 proto
= ZEBRA_ROUTE_RIPNG
;
336 proto
= ZEBRA_ROUTE_NHRP
;
339 proto
= ZEBRA_ROUTE_EIGRP
;
342 proto
= ZEBRA_ROUTE_LDP
;
346 proto
= ZEBRA_ROUTE_STATIC
;
349 proto
= ZEBRA_ROUTE_SHARP
;
352 proto
= ZEBRA_ROUTE_PBR
;
354 case RTPROT_OPENFABRIC
:
355 proto
= ZEBRA_ROUTE_OPENFABRIC
;
358 proto
= ZEBRA_ROUTE_SRTE
;
361 case RTPROT_REDIRECT
:
368 case RTPROT_DNROUTED
:
372 case RTPROT_KEEPALIVED
:
374 proto
= ZEBRA_ROUTE_KERNEL
;
378 proto
= ZEBRA_ROUTE_NHG
;
381 /* Intentional fall thru */
384 * When a user adds a new protocol this will show up
385 * to let them know to do something about it. This
386 * is intentionally a warn because we should see
387 * this as part of development of a new protocol
390 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
392 proto
= ZEBRA_ROUTE_KERNEL
;
399 Pending: create an efficient table_id (in a tree/hash) based lookup)
401 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
404 struct zebra_vrf
*zvrf
;
406 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
410 /* case vrf with netns : match the netnsid */
411 if (vrf_is_backend_netns()) {
412 if (ns_id
== zvrf_id(zvrf
))
413 return zvrf_id(zvrf
);
415 /* VRF is VRF_BACKEND_VRF_LITE */
416 if (zvrf
->table_id
!= table_id
)
418 return zvrf_id(zvrf
);
426 * @parse_encap_mpls() - Parses encapsulated mpls attributes
427 * @tb: Pointer to rtattr to look for nested items in.
428 * @labels: Pointer to store labels in.
430 * Return: Number of mpls labels found.
432 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
434 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
435 mpls_lse_t
*lses
= NULL
;
440 mpls_label_t label
= 0;
442 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
443 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
444 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
445 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
446 labels
[num_labels
++] = label
;
452 static enum seg6local_action_t
453 parse_encap_seg6local(struct rtattr
*tb
,
454 struct seg6local_context
*ctx
)
456 struct rtattr
*tb_encap
[SEG6_LOCAL_MAX
+ 1] = {};
457 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
459 netlink_parse_rtattr_nested(tb_encap
, SEG6_LOCAL_MAX
, tb
);
461 if (tb_encap
[SEG6_LOCAL_ACTION
])
462 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
464 if (tb_encap
[SEG6_LOCAL_NH4
])
465 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
466 tb_encap
[SEG6_LOCAL_NH4
]);
468 if (tb_encap
[SEG6_LOCAL_NH6
])
469 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
470 tb_encap
[SEG6_LOCAL_NH6
]);
472 if (tb_encap
[SEG6_LOCAL_TABLE
])
473 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
475 if (tb_encap
[SEG6_LOCAL_VRFTABLE
])
477 *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_VRFTABLE
]);
482 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
484 struct rtattr
*tb_encap
[SEG6_IPTUNNEL_MAX
+ 1] = {};
485 struct seg6_iptunnel_encap
*ipt
= NULL
;
486 struct in6_addr
*segments
= NULL
;
488 netlink_parse_rtattr_nested(tb_encap
, SEG6_IPTUNNEL_MAX
, tb
);
491 * TODO: It's not support multiple SID list.
493 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
494 ipt
= (struct seg6_iptunnel_encap
*)
495 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
496 segments
= ipt
->srh
[0].segments
;
505 static struct nexthop
506 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
507 enum blackhole_type bh_type
, int index
, void *prefsrc
,
508 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
510 struct interface
*ifp
= NULL
;
511 struct nexthop nh
= {0};
512 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
514 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
515 struct seg6local_context seg6l_ctx
= {};
516 struct in6_addr seg6_segs
= {};
519 vrf_id_t nh_vrf_id
= vrf_id
;
520 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
522 if (bh_type
== BLACKHOLE_UNSPEC
) {
524 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
525 else if (index
&& gate
)
526 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
527 : NEXTHOP_TYPE_IPV6_IFINDEX
;
528 else if (!index
&& gate
)
529 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
532 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
533 nh
.bh_type
= bh_type
;
536 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
537 nh
.bh_type
= bh_type
;
541 memcpy(&nh
.src
, prefsrc
, sz
);
543 memcpy(&nh
.gate
, gate
, sz
);
546 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
548 nh_vrf_id
= ifp
->vrf
->vrf_id
;
550 nh
.vrf_id
= nh_vrf_id
;
552 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
553 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
554 == LWTUNNEL_ENCAP_MPLS
) {
555 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
557 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
558 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
559 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
560 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
562 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
563 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
564 == LWTUNNEL_ENCAP_SEG6
) {
565 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
568 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
569 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
571 if (rtm
->rtm_flags
& RTNH_F_LINKDOWN
)
572 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_LINKDOWN
);
575 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
577 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
578 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
581 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
586 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
587 struct nexthop_group
*ng
,
589 struct rtnexthop
*rtnh
,
591 void *prefsrc
, vrf_id_t vrf_id
)
594 struct interface
*ifp
= NULL
;
597 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
599 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
600 struct seg6local_context seg6l_ctx
= {};
601 struct in6_addr seg6_segs
= {};
603 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
605 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
606 vrf_id_t nh_vrf_id
= vrf_id
;
609 struct nexthop
*nh
= NULL
;
611 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
614 index
= rtnh
->rtnh_ifindex
;
617 * Yes we are looking this up
618 * for every nexthop and just
619 * using the last one looked
622 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
625 nh_vrf_id
= ifp
->vrf
->vrf_id
;
628 EC_ZEBRA_UNKNOWN_INTERFACE
,
629 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
631 nh_vrf_id
= VRF_DEFAULT
;
636 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
637 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
638 rtnh
->rtnh_len
- sizeof(*rtnh
));
639 if (rtnh_tb
[RTA_GATEWAY
])
640 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
641 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
642 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
643 == LWTUNNEL_ENCAP_MPLS
) {
644 num_labels
= parse_encap_mpls(
645 rtnh_tb
[RTA_ENCAP
], labels
);
647 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
648 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
649 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
650 seg6l_act
= parse_encap_seg6local(
651 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
653 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
654 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
655 == LWTUNNEL_ENCAP_SEG6
) {
656 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
661 if (gate
&& rtm
->rtm_family
== AF_INET
) {
663 nh
= nexthop_from_ipv4_ifindex(
664 gate
, prefsrc
, index
, nh_vrf_id
);
666 nh
= nexthop_from_ipv4(gate
, prefsrc
,
668 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
670 nh
= nexthop_from_ipv6_ifindex(
671 gate
, index
, nh_vrf_id
);
673 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
675 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
678 nh
->weight
= rtnh
->rtnh_hops
+ 1;
681 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
684 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
685 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
689 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
691 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
692 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
694 /* Add to temporary list */
695 nexthop_group_add_sorted(ng
, nh
);
698 if (rtnh
->rtnh_len
== 0)
701 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
702 rtnh
= RTNH_NEXT(rtnh
);
705 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
710 /* Looking up routing table by netlink interface. */
711 int netlink_route_change_read_unicast_internal(struct nlmsghdr
*h
,
712 ns_id_t ns_id
, int startup
,
713 struct zebra_dplane_ctx
*ctx
)
717 struct rtattr
*tb
[RTA_MAX
+ 1];
720 struct prefix_ipv6 src_p
= {};
724 char anyaddr
[16] = {0};
726 int proto
= ZEBRA_ROUTE_KERNEL
;
731 uint8_t distance
= 0;
737 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
738 void *src
= NULL
; /* IPv6 srcdest source prefix */
739 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
741 frrtrace(3, frr_zebra
, netlink_route_change_read_unicast
, h
, ns_id
,
746 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
748 switch (rtm
->rtm_type
) {
752 bh_type
= BLACKHOLE_NULL
;
754 case RTN_UNREACHABLE
:
755 bh_type
= BLACKHOLE_REJECT
;
758 bh_type
= BLACKHOLE_ADMINPROHIB
;
761 if (IS_ZEBRA_DEBUG_KERNEL
)
762 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
763 nl_rttype_to_str(rtm
->rtm_type
),
768 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
771 "%s: Message received from netlink is of a broken size %d %zu",
772 __func__
, h
->nlmsg_len
,
773 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
777 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
779 if (rtm
->rtm_flags
& RTM_F_CLONED
)
781 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
783 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
786 selfroute
= is_selfroute(rtm
->rtm_protocol
);
788 if (!startup
&& selfroute
&& h
->nlmsg_type
== RTM_NEWROUTE
&&
789 !zrouter
.asic_offloaded
&& !ctx
) {
790 if (IS_ZEBRA_DEBUG_KERNEL
)
791 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
796 /* We don't care about change notifications for the MPLS table. */
797 /* TODO: Revisit this. */
798 if (rtm
->rtm_family
== AF_MPLS
)
801 /* Table corresponding to route. */
803 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
805 table
= rtm
->rtm_table
;
808 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
809 if (vrf_id
== VRF_DEFAULT
) {
810 if (!is_zebra_valid_kernel_table(table
)
811 && !is_zebra_main_routing_table(table
))
815 if (rtm
->rtm_flags
& RTM_F_TRAP
)
816 flags
|= ZEBRA_FLAG_TRAPPED
;
817 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
818 flags
|= ZEBRA_FLAG_OFFLOADED
;
819 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
820 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
822 if (h
->nlmsg_flags
& NLM_F_APPEND
)
823 flags
|= ZEBRA_FLAG_OUTOFSYNC
;
825 /* Route which inserted by Zebra. */
827 flags
|= ZEBRA_FLAG_SELFROUTE
;
828 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
831 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
834 dest
= RTA_DATA(tb
[RTA_DST
]);
839 src
= RTA_DATA(tb
[RTA_SRC
]);
844 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
847 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
850 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
852 if (tb
[RTA_PRIORITY
])
853 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
855 #if defined(SUPPORT_REALMS)
857 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
860 if (tb
[RTA_METRICS
]) {
861 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
863 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
864 RTA_PAYLOAD(tb
[RTA_METRICS
]));
867 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
870 if (rtm
->rtm_family
== AF_INET
) {
872 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
874 "Invalid destination prefix length: %u received from kernel route change",
878 memcpy(&p
.u
.prefix4
, dest
, 4);
879 p
.prefixlen
= rtm
->rtm_dst_len
;
881 if (rtm
->rtm_src_len
!= 0) {
883 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
884 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
889 /* Force debug below to not display anything for source */
891 } else if (rtm
->rtm_family
== AF_INET6
) {
893 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
895 "Invalid destination prefix length: %u received from kernel route change",
899 memcpy(&p
.u
.prefix6
, dest
, 16);
900 p
.prefixlen
= rtm
->rtm_dst_len
;
902 src_p
.family
= AF_INET6
;
903 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
905 "Invalid source prefix length: %u received from kernel route change",
909 memcpy(&src_p
.prefix
, src
, 16);
910 src_p
.prefixlen
= rtm
->rtm_src_len
;
912 /* We only handle the AFs we handle... */
913 if (IS_ZEBRA_DEBUG_KERNEL
)
914 zlog_debug("%s: unknown address-family %u", __func__
,
920 * For ZEBRA_ROUTE_KERNEL types:
922 * The metric/priority of the route received from the kernel
923 * is a 32 bit number. We are going to interpret the high
924 * order byte as the Admin Distance and the low order 3 bytes
927 * This will allow us to do two things:
928 * 1) Allow the creation of kernel routes that can be
929 * overridden by zebra.
930 * 2) Allow the old behavior for 'most' kernel route types
931 * if a user enters 'ip route ...' v4 routes get a metric
932 * of 0 and v6 routes get a metric of 1024. Both of these
933 * values will end up with a admin distance of 0, which
934 * will cause them to win for the purposes of zebra.
936 if (proto
== ZEBRA_ROUTE_KERNEL
) {
937 distance
= (metric
>> 24) & 0xFF;
938 metric
= (metric
& 0x00FFFFFF);
941 if (IS_ZEBRA_DEBUG_KERNEL
) {
942 char buf2
[PREFIX_STRLEN
];
945 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
946 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
947 src_p
.prefixlen
? " from " : "",
948 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
950 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
955 if (rtm
->rtm_family
== AF_INET6
)
958 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
959 struct route_entry
*re
;
960 struct nexthop_group
*ng
= NULL
;
962 re
= zebra_rib_route_entry_new(vrf_id
, proto
, 0, flags
, nhe_id
,
963 table
, metric
, mtu
, distance
,
966 ng
= nexthop_group_new();
968 if (!tb
[RTA_MULTIPATH
]) {
969 struct nexthop
*nexthop
, nh
;
972 nh
= parse_nexthop_unicast(
973 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
976 nexthop
= nexthop_new();
978 nexthop_group_add_sorted(ng
, nexthop
);
981 /* This is a multipath route */
982 struct rtnexthop
*rtnh
=
983 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
988 /* Use temporary list of nexthops; parse
989 * message payload's nexthops.
992 parse_multipath_nexthops_unicast(
993 ns_id
, ng
, rtm
, rtnh
, tb
,
996 zserv_nexthop_num_warn(
997 __func__
, (const struct prefix
*)&p
,
1000 if (nhop_num
== 0) {
1001 nexthop_group_delete(&ng
);
1007 dplane_rib_add_multipath(afi
, SAFI_UNICAST
, &p
, &src_p
,
1008 re
, ng
, startup
, ctx
);
1010 nexthop_group_delete(&ng
);
1013 * I really don't see how this is possible
1014 * but since we are testing for it let's
1015 * let the end user know why the route
1016 * that was just received was swallowed
1020 "%s: %pFX multipath RTM_NEWROUTE has a invalid nexthop group from the kernel",
1022 XFREE(MTYPE_RE
, re
);
1027 "%s: %pFX RTM_DELROUTE received but received a context as well",
1033 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
1034 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
1037 if (!tb
[RTA_MULTIPATH
]) {
1040 nh
= parse_nexthop_unicast(
1041 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
1043 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
1044 flags
, &p
, &src_p
, &nh
, 0, table
,
1045 metric
, distance
, true);
1047 /* XXX: need to compare the entire list of
1048 * nexthops here for NLM_F_APPEND stupidity */
1049 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
1050 flags
, &p
, &src_p
, NULL
, 0, table
,
1051 metric
, distance
, true);
1059 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
1062 return netlink_route_change_read_unicast_internal(h
, ns_id
, startup
,
1066 static struct mcast_route_data
*mroute
= NULL
;
1068 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1069 ns_id_t ns_id
, int startup
)
1073 struct rtattr
*tb
[RTA_MAX
+ 1];
1074 struct mcast_route_data
*m
;
1079 char oif_list
[256] = "\0";
1086 rtm
= NLMSG_DATA(h
);
1088 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1090 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1093 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1095 table
= rtm
->rtm_table
;
1097 vrf
= vrf_lookup_by_table(table
, ns_id
);
1100 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1103 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1105 *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1108 *(struct in6_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1112 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1114 *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1117 *(struct in6_addr
*)RTA_DATA(tb
[RTA_DST
]);
1120 if (tb
[RTA_EXPIRES
])
1121 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1123 if (tb
[RTA_MULTIPATH
]) {
1124 struct rtnexthop
*rtnh
=
1125 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1127 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1129 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1132 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1135 if (rtnh
->rtnh_len
== 0)
1138 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1139 rtnh
= RTNH_NEXT(rtnh
);
1143 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
) {
1144 SET_IPADDR_V4(&m
->src
);
1145 SET_IPADDR_V4(&m
->grp
);
1146 } else if (rtm
->rtm_family
== RTNL_FAMILY_IP6MR
) {
1147 SET_IPADDR_V6(&m
->src
);
1148 SET_IPADDR_V6(&m
->grp
);
1150 zlog_warn("%s: Invalid rtm_family received", __func__
);
1154 if (IS_ZEBRA_DEBUG_KERNEL
) {
1155 struct interface
*ifp
= NULL
;
1156 struct zebra_vrf
*zvrf
= NULL
;
1158 for (count
= 0; count
< oif_count
; count
++) {
1159 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1162 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1163 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1164 strlcat(oif_list
, temp
, sizeof(oif_list
));
1166 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1167 ifp
= if_lookup_by_index(iif
, vrf
);
1169 "MCAST VRF: %s(%d) %s (%pIA,%pIA) IIF: %s(%d) OIF: %s jiffies: %lld",
1170 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1171 &m
->src
, &m
->grp
, ifp
? ifp
->name
: "Unknown", iif
,
1172 oif_list
, m
->lastused
);
1177 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1182 rtm
= NLMSG_DATA(h
);
1184 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1185 /* If this is not route add/delete message print warning. */
1186 zlog_debug("Kernel message: %s NS %u",
1187 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1191 switch (rtm
->rtm_family
) {
1196 case RTNL_FAMILY_IPMR
:
1197 case RTNL_FAMILY_IP6MR
:
1198 /* notifications on IPMR are irrelevant to zebra, we only care
1199 * about responses to RTM_GETROUTE requests we sent.
1205 EC_ZEBRA_UNKNOWN_FAMILY
,
1206 "Invalid address family: %u received from kernel route change: %s",
1207 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1211 /* Connected route. */
1212 if (IS_ZEBRA_DEBUG_KERNEL
)
1213 zlog_debug("%s %s %s proto %s NS %u",
1214 nl_msg_type_to_str(h
->nlmsg_type
),
1215 nl_family_to_str(rtm
->rtm_family
),
1216 nl_rttype_to_str(rtm
->rtm_type
),
1217 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1220 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1223 "%s: Message received from netlink is of a broken size: %d %zu",
1224 __func__
, h
->nlmsg_len
,
1225 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1229 /* these are "magic" kernel-managed *unicast* routes used for
1230 * outputting locally generated multicast traffic (which uses unicast
1231 * handling on Linux because ~reasons~.
1233 if (rtm
->rtm_type
== RTN_MULTICAST
)
1236 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1240 /* Request for specific route information from the kernel */
1241 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1248 /* Form the request, specifying filter (rtattr) if needed. */
1249 memset(&req
, 0, sizeof(req
));
1250 req
.n
.nlmsg_type
= type
;
1251 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1252 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1253 req
.rtm
.rtm_family
= family
;
1255 return netlink_request(&zns
->netlink_cmd
, &req
);
1258 /* Routing table read function using netlink interface. Only called
1260 int netlink_route_read(struct zebra_ns
*zns
)
1263 struct zebra_dplane_info dp_info
;
1265 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1267 /* Get IPv4 routing table. */
1268 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1271 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1272 &zns
->netlink_cmd
, &dp_info
, 0, true);
1276 /* Get IPv6 routing table. */
1277 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1280 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1281 &zns
->netlink_cmd
, &dp_info
, 0, true);
1289 * The function returns true if the gateway info could be added
1290 * to the message, otherwise false is returned.
1292 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1294 struct nlmsghdr
*nlmsg
,
1295 size_t req_size
, int bytelen
,
1296 const struct nexthop
*nexthop
)
1298 if (route_family
== AF_MPLS
) {
1299 struct gw_family_t gw_fam
;
1301 gw_fam
.family
= gw_family
;
1302 if (gw_family
== AF_INET
)
1303 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1305 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1306 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1310 if (!(nexthop
->rparent
1311 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1312 if (gw_family
== AF_INET
) {
1313 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1314 &nexthop
->gate
.ipv4
, bytelen
))
1317 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1318 &nexthop
->gate
.ipv6
, bytelen
))
1327 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1328 enum lsp_types_t nh_label_type
,
1329 mpls_lse_t
*out_lse
, char *label_buf
,
1330 size_t label_buf_size
)
1332 char label_buf1
[20];
1335 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1336 if (nh_label_type
!= ZEBRA_LSP_EVPN
&&
1337 nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1340 if (IS_ZEBRA_DEBUG_KERNEL
) {
1342 snprintf(label_buf
, label_buf_size
, "label %u",
1343 nh_label
->label
[i
]);
1345 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1346 nh_label
->label
[i
]);
1347 strlcat(label_buf
, label_buf1
, label_buf_size
);
1351 if (nh_label_type
== ZEBRA_LSP_EVPN
)
1352 out_lse
[num_labels
] = label2vni(&nh_label
->label
[i
]);
1354 out_lse
[num_labels
] =
1355 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1362 static bool _netlink_nexthop_encode_dvni_label(const struct nexthop
*nexthop
,
1363 struct nlmsghdr
*nlmsg
,
1364 mpls_lse_t
*out_lse
,
1365 size_t buflen
, char *label_buf
)
1367 struct in_addr ipv4
;
1369 if (!nl_attr_put64(nlmsg
, buflen
, LWTUNNEL_IP_ID
,
1370 htonll((uint64_t)out_lse
[0])))
1373 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1374 if (!nl_attr_put(nlmsg
, buflen
, LWTUNNEL_IP_DST
,
1375 &nexthop
->gate
.ipv4
, 4))
1378 } else if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1379 if (IS_MAPPED_IPV6(&nexthop
->gate
.ipv6
)) {
1380 ipv4_mapped_ipv6_to_ipv4(&nexthop
->gate
.ipv6
, &ipv4
);
1381 if (!nl_attr_put(nlmsg
, buflen
, LWTUNNEL_IP_DST
, &ipv4
,
1386 if (!nl_attr_put(nlmsg
, buflen
, LWTUNNEL_IP_DST
,
1387 &nexthop
->gate
.ipv6
, 16))
1391 if (IS_ZEBRA_DEBUG_KERNEL
)
1393 "%s: nexthop %pNHv %s must NEXTHOP_TYPE_IPV*_IFINDEX to be vxlan encapped",
1394 __func__
, nexthop
, label_buf
);
1402 static bool _netlink_route_encode_label_info(const struct nexthop
*nexthop
,
1403 struct nlmsghdr
*nlmsg
,
1404 size_t buflen
, struct rtmsg
*rtmsg
,
1406 size_t label_buf_size
)
1408 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1410 struct rtattr
*nest
;
1411 struct mpls_label_stack
*nh_label
;
1412 enum lsp_types_t nh_label_type
;
1414 nh_label
= nexthop
->nh_label
;
1415 nh_label_type
= nexthop
->nh_label_type
;
1418 * label_buf is *only* currently used within debugging.
1419 * As such when we assign it we are guarding it inside
1420 * a debug test. If you want to change this make sure
1421 * you fix this assumption
1423 label_buf
[0] = '\0';
1425 num_labels
= build_label_stack(nh_label
, nh_label_type
, out_lse
,
1426 label_buf
, label_buf_size
);
1428 if (num_labels
&& nh_label_type
== ZEBRA_LSP_EVPN
) {
1429 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1433 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1437 if (_netlink_nexthop_encode_dvni_label(nexthop
, nlmsg
, out_lse
,
1439 label_buf
) == false)
1442 nl_attr_nest_end(nlmsg
, nest
);
1444 } else if (num_labels
) {
1445 /* Set the BoS bit */
1446 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1448 if (rtmsg
->rtm_family
== AF_MPLS
) {
1449 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1450 num_labels
* sizeof(mpls_lse_t
)))
1453 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1454 LWTUNNEL_ENCAP_MPLS
))
1457 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1461 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1463 num_labels
* sizeof(mpls_lse_t
)))
1465 nl_attr_nest_end(nlmsg
, nest
);
1472 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1474 struct nlmsghdr
*nlmsg
,
1475 size_t buflen
, int bytelen
)
1477 if (family
== AF_INET
) {
1478 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1479 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1480 &nexthop
->rmap_src
.ipv4
, bytelen
))
1482 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1483 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1484 &nexthop
->src
.ipv4
, bytelen
))
1487 } else if (family
== AF_INET6
) {
1488 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1489 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1490 &nexthop
->rmap_src
.ipv6
, bytelen
))
1492 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1493 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1494 &nexthop
->src
.ipv6
, bytelen
))
1502 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1503 const struct in6_addr
*seg
)
1505 struct seg6_iptunnel_encap
*ipt
;
1506 struct ipv6_sr_hdr
*srh
;
1507 const size_t srhlen
= 24;
1510 * Caution: Support only SINGLE-SID, not MULTI-SID
1511 * This function only supports the case where segs represents
1512 * a single SID. If you want to extend the SRv6 functionality,
1513 * you should improve the Boundary Check.
1514 * Ex. In case of set a SID-List include multiple-SIDs as an
1515 * argument of the Transit Behavior, we must support variable
1516 * boundary check for buflen.
1518 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1519 sizeof(struct ipv6_sr_hdr
) + 16))
1522 memset(buffer
, 0, buflen
);
1524 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1525 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1527 srh
->hdrlen
= (srhlen
>> 3) - 1;
1529 srh
->segments_left
= 0;
1530 srh
->first_segment
= 0;
1531 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1536 /* This function takes a nexthop as argument and adds
1537 * the appropriate netlink attributes to an existing
1540 * @param routedesc: Human readable description of route type
1541 * (direct/recursive, single-/multipath)
1542 * @param bytelen: Length of addresses in bytes.
1543 * @param nexthop: Nexthop information
1544 * @param nlmsg: nlmsghdr structure to fill in.
1545 * @param req_size: The size allocated for the message.
1547 * The function returns true if the nexthop could be added
1548 * to the message, otherwise false is returned.
1550 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1551 const char *routedesc
, int bytelen
,
1552 const struct nexthop
*nexthop
,
1553 struct nlmsghdr
*nlmsg
,
1554 struct rtmsg
*rtmsg
,
1555 size_t req_size
, int cmd
)
1558 char label_buf
[256];
1560 char addrstr
[INET6_ADDRSTRLEN
];
1564 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1566 if (!_netlink_route_encode_label_info(nexthop
, nlmsg
, req_size
, rtmsg
,
1567 label_buf
, sizeof(label_buf
)))
1570 if (nexthop
->nh_srv6
) {
1571 if (nexthop
->nh_srv6
->seg6local_action
!=
1572 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1573 struct rtattr
*nest
;
1574 const struct seg6local_context
*ctx
;
1576 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1577 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1578 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1581 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1585 switch (nexthop
->nh_srv6
->seg6local_action
) {
1586 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1587 if (!nl_attr_put32(nlmsg
, req_size
,
1589 SEG6_LOCAL_ACTION_END
))
1592 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1593 if (!nl_attr_put32(nlmsg
, req_size
,
1595 SEG6_LOCAL_ACTION_END_X
))
1597 if (!nl_attr_put(nlmsg
, req_size
,
1598 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1599 sizeof(struct in6_addr
)))
1602 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1603 if (!nl_attr_put32(nlmsg
, req_size
,
1605 SEG6_LOCAL_ACTION_END_T
))
1607 if (!nl_attr_put32(nlmsg
, req_size
,
1612 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1613 if (!nl_attr_put32(nlmsg
, req_size
,
1615 SEG6_LOCAL_ACTION_END_DX4
))
1617 if (!nl_attr_put(nlmsg
, req_size
,
1618 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1619 sizeof(struct in_addr
)))
1622 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1623 if (!nl_attr_put32(nlmsg
, req_size
,
1625 SEG6_LOCAL_ACTION_END_DT6
))
1627 if (!nl_attr_put32(nlmsg
, req_size
,
1632 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
1633 if (!nl_attr_put32(nlmsg
, req_size
,
1635 SEG6_LOCAL_ACTION_END_DT4
))
1637 if (!nl_attr_put32(nlmsg
, req_size
,
1638 SEG6_LOCAL_VRFTABLE
,
1642 case ZEBRA_SEG6_LOCAL_ACTION_END_DT46
:
1643 if (!nl_attr_put32(nlmsg
, req_size
,
1645 SEG6_LOCAL_ACTION_END_DT46
))
1647 if (!nl_attr_put32(nlmsg
, req_size
,
1648 SEG6_LOCAL_VRFTABLE
,
1652 case ZEBRA_SEG6_LOCAL_ACTION_END_DX2
:
1653 case ZEBRA_SEG6_LOCAL_ACTION_END_DX6
:
1654 case ZEBRA_SEG6_LOCAL_ACTION_END_B6
:
1655 case ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP
:
1656 case ZEBRA_SEG6_LOCAL_ACTION_END_BM
:
1657 case ZEBRA_SEG6_LOCAL_ACTION_END_S
:
1658 case ZEBRA_SEG6_LOCAL_ACTION_END_AS
:
1659 case ZEBRA_SEG6_LOCAL_ACTION_END_AM
:
1660 case ZEBRA_SEG6_LOCAL_ACTION_END_BPF
:
1661 case ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
:
1662 zlog_err("%s: unsupport seg6local behaviour action=%u",
1664 nexthop
->nh_srv6
->seg6local_action
);
1667 nl_attr_nest_end(nlmsg
, nest
);
1670 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1673 struct rtattr
*nest
;
1675 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1676 LWTUNNEL_ENCAP_SEG6
))
1678 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1681 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1682 &nexthop
->nh_srv6
->seg6_segs
);
1685 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1688 nl_attr_nest_end(nlmsg
, nest
);
1692 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1693 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1695 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1696 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1697 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1699 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1702 if (cmd
== RTM_NEWROUTE
) {
1703 if (!_netlink_route_encode_nexthop_src(
1704 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1708 if (IS_ZEBRA_DEBUG_KERNEL
)
1709 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1710 __func__
, routedesc
, p
, ipv4_ll_buf
,
1711 label_buf
, nexthop
->ifindex
,
1712 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1716 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1717 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1718 /* Send deletes to the kernel without specifying the next-hop */
1719 if (cmd
!= RTM_DELROUTE
) {
1720 if (!_netlink_route_add_gateway_info(
1721 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1726 if (cmd
== RTM_NEWROUTE
) {
1727 if (!_netlink_route_encode_nexthop_src(
1728 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1732 if (IS_ZEBRA_DEBUG_KERNEL
) {
1733 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1735 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1736 __func__
, routedesc
, p
, addrstr
, label_buf
,
1737 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1742 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1743 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1744 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1745 AF_INET6
, nlmsg
, req_size
,
1749 if (cmd
== RTM_NEWROUTE
) {
1750 if (!_netlink_route_encode_nexthop_src(
1751 nexthop
, AF_INET6
, nlmsg
, req_size
,
1756 if (IS_ZEBRA_DEBUG_KERNEL
) {
1757 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1759 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1760 __func__
, routedesc
, p
, addrstr
, label_buf
,
1761 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1767 * We have the ifindex so we should always send it
1768 * This is especially useful if we are doing route
1771 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1772 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1776 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1777 if (cmd
== RTM_NEWROUTE
) {
1778 if (!_netlink_route_encode_nexthop_src(
1779 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1783 if (IS_ZEBRA_DEBUG_KERNEL
)
1784 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1785 __func__
, routedesc
, p
, nexthop
->ifindex
,
1786 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1792 /* This function appends tag value as rtnl flow attribute
1793 * to the given netlink msg only if value is less than 256.
1794 * Used only if SUPPORT_REALMS enabled.
1796 * @param nlmsg: nlmsghdr structure to fill in.
1797 * @param maxlen: The size allocated for the message.
1798 * @param tag: The route tag.
1800 * The function returns true if the flow attribute could
1801 * be added to the message, otherwise false is returned.
1803 static inline bool _netlink_set_tag(struct nlmsghdr
*n
, unsigned int maxlen
,
1806 if (tag
> 0 && tag
<= 255) {
1807 if (!nl_attr_put32(n
, maxlen
, RTA_FLOW
, tag
))
1813 /* This function takes a nexthop as argument and
1814 * appends to the given netlink msg. If the nexthop
1815 * defines a preferred source, the src parameter
1816 * will be modified to point to that src, otherwise
1817 * it will be kept unmodified.
1819 * @param routedesc: Human readable description of route type
1820 * (direct/recursive, single-/multipath)
1821 * @param bytelen: Length of addresses in bytes.
1822 * @param nexthop: Nexthop information
1823 * @param nlmsg: nlmsghdr structure to fill in.
1824 * @param req_size: The size allocated for the message.
1825 * @param src: pointer pointing to a location where
1826 * the prefsrc should be stored.
1828 * The function returns true if the nexthop could be added
1829 * to the message, otherwise false is returned.
1831 static bool _netlink_route_build_multipath(
1832 const struct prefix
*p
, const char *routedesc
, int bytelen
,
1833 const struct nexthop
*nexthop
, struct nlmsghdr
*nlmsg
, size_t req_size
,
1834 struct rtmsg
*rtmsg
, const union g_addr
**src
, route_tag_t tag
)
1836 char label_buf
[256];
1838 struct rtnexthop
*rtnh
;
1840 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1846 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1848 if (!_netlink_route_encode_label_info(nexthop
, nlmsg
, req_size
, rtmsg
,
1849 label_buf
, sizeof(label_buf
)))
1852 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1853 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1855 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1856 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1857 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1859 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1860 if (nexthop
->weight
)
1861 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1863 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1864 *src
= &nexthop
->rmap_src
;
1865 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1866 *src
= &nexthop
->src
;
1868 if (IS_ZEBRA_DEBUG_KERNEL
)
1870 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1871 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1872 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1874 nl_attr_rtnh_end(nlmsg
, rtnh
);
1878 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1879 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1880 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1881 nlmsg
, req_size
, bytelen
,
1885 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1886 *src
= &nexthop
->rmap_src
;
1887 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1888 *src
= &nexthop
->src
;
1890 if (IS_ZEBRA_DEBUG_KERNEL
)
1891 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1892 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1893 label_buf
, nexthop
->ifindex
,
1894 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1896 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1897 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1898 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1899 AF_INET6
, nlmsg
, req_size
,
1903 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1904 *src
= &nexthop
->rmap_src
;
1905 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1906 *src
= &nexthop
->src
;
1908 if (IS_ZEBRA_DEBUG_KERNEL
)
1909 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1910 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1911 label_buf
, nexthop
->ifindex
,
1912 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1916 * We have figured out the ifindex so we should always send it
1917 * This is especially useful if we are doing route
1920 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1921 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1924 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1925 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1926 *src
= &nexthop
->rmap_src
;
1927 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1928 *src
= &nexthop
->src
;
1930 if (IS_ZEBRA_DEBUG_KERNEL
)
1931 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1932 __func__
, routedesc
, p
, nexthop
->ifindex
,
1933 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1936 if (nexthop
->weight
)
1937 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1939 if (!_netlink_set_tag(nlmsg
, req_size
, tag
))
1942 nl_attr_rtnh_end(nlmsg
, rtnh
);
1947 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1948 const struct zebra_nhlfe
*nhlfe
,
1949 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1950 size_t req_size
, int cmd
)
1955 family
= NHLFE_FAMILY(nhlfe
);
1956 bytelen
= (family
== AF_INET
? 4 : 16);
1957 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1958 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1964 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1965 const struct zebra_nhlfe
*nhlfe
,
1966 struct nlmsghdr
*nlmsg
, size_t req_size
,
1967 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1972 family
= NHLFE_FAMILY(nhlfe
);
1973 bytelen
= (family
== AF_INET
? 4 : 16);
1974 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1975 nhlfe
->nexthop
, nlmsg
, req_size
,
1979 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1981 if (IS_ZEBRA_DEBUG_KERNEL
)
1982 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1983 routedesc
, nl_msg_type_to_str(cmd
), label
);
1986 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1987 int llalen
, ns_id_t ns_id
, uint8_t family
,
1988 bool permanent
, uint8_t protocol
)
1996 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1998 memset(&req
, 0, sizeof(req
));
2000 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2001 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2002 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
2003 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2005 req
.ndm
.ndm_family
= family
;
2006 req
.ndm
.ndm_ifindex
= ifindex
;
2007 req
.ndm
.ndm_type
= RTN_UNICAST
;
2008 if (cmd
== RTM_NEWNEIGH
) {
2010 req
.ndm
.ndm_state
= NUD_REACHABLE
;
2012 req
.ndm
.ndm_state
= NUD_PERMANENT
;
2014 req
.ndm
.ndm_state
= NUD_FAILED
;
2016 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
2018 req
.ndm
.ndm_type
= RTN_UNICAST
;
2019 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
2020 family2addrsize(family
));
2022 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
2024 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2028 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
2031 if (family
== AF_INET
) {
2032 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
2033 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
2035 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
2036 src
->ipv4
= nexthop
->src
.ipv4
;
2039 } else if (family
== AF_INET6
) {
2040 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
2041 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
2043 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
2044 src
->ipv6
= nexthop
->src
.ipv6
;
2053 * The function returns true if the attribute could be added
2054 * to the message, otherwise false is returned.
2056 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
2059 struct rtattr
*nest
;
2061 switch (nh
->nh_encap_type
) {
2063 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
2066 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
2070 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
2073 nl_attr_nest_end(n
, nest
);
2081 * Routing table change via netlink interface, using a dataplane context object
2083 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2084 * otherwise the number of bytes written to buf.
2086 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
2087 struct zebra_dplane_ctx
*ctx
,
2088 uint8_t *data
, size_t datalen
,
2089 bool fpm
, bool force_nhg
)
2092 struct nexthop
*nexthop
= NULL
;
2093 unsigned int nexthop_num
;
2094 const char *routedesc
;
2095 bool setsrc
= false;
2097 const struct prefix
*p
, *src_p
;
2100 route_tag_t tag
= 0;
2106 } *req
= (void *)data
;
2108 p
= dplane_ctx_get_dest(ctx
);
2109 src_p
= dplane_ctx_get_src(ctx
);
2111 if (datalen
< sizeof(*req
))
2114 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2116 memset(req
, 0, sizeof(*req
));
2118 bytelen
= (p
->family
== AF_INET
? 4 : 16);
2120 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2121 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2123 if ((cmd
== RTM_NEWROUTE
) &&
2124 ((p
->family
== AF_INET
) || v6_rr_semantics
))
2125 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2127 req
->n
.nlmsg_type
= cmd
;
2129 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2131 req
->r
.rtm_family
= p
->family
;
2132 req
->r
.rtm_dst_len
= p
->prefixlen
;
2133 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
2134 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2136 if (cmd
== RTM_DELROUTE
)
2137 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
2139 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
2142 * blackhole routes are not RTN_UNICAST, they are
2143 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
2144 * so setting this value as a RTN_UNICAST would
2145 * cause the route lookup of just the prefix
2146 * to fail. So no need to specify this for
2147 * the RTM_DELROUTE case
2149 if (cmd
!= RTM_DELROUTE
)
2150 req
->r
.rtm_type
= RTN_UNICAST
;
2152 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
2155 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
2161 /* Hardcode the metric for all routes coming from zebra. Metric isn't
2163 * either by the kernel or by zebra. Its purely for calculating best
2165 * by the routing protocol and for communicating with protocol peers.
2167 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
2168 ROUTE_INSTALLATION_METRIC
))
2171 #if defined(SUPPORT_REALMS)
2172 if (cmd
== RTM_DELROUTE
)
2173 tag
= dplane_ctx_get_old_tag(ctx
);
2175 tag
= dplane_ctx_get_tag(ctx
);
2178 /* Table corresponding to this route. */
2179 table_id
= dplane_ctx_get_table(ctx
);
2181 req
->r
.rtm_table
= table_id
;
2183 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
2184 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
2188 if (IS_ZEBRA_DEBUG_KERNEL
)
2190 "%s: %s %pFX vrf %u(%u)", __func__
,
2191 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
2195 * If we are not updating the route and we have received
2196 * a route delete, then all we need to fill in is the
2197 * prefix information to tell the kernel to schwack
2200 if (cmd
== RTM_DELROUTE
) {
2201 if (!_netlink_set_tag(&req
->n
, datalen
, tag
))
2203 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2206 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
2207 struct rtattr
*nest
;
2208 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
2209 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2211 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2214 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2218 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2220 nl_attr_nest_end(&req
->n
, nest
);
2224 * Always install blackhole routes without using nexthops, because of
2225 * the following kernel problems:
2226 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2227 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2229 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2231 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2232 nexthop
= nexthop
->resolved
;
2234 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2235 switch (nexthop
->bh_type
) {
2236 case BLACKHOLE_ADMINPROHIB
:
2237 req
->r
.rtm_type
= RTN_PROHIBIT
;
2239 case BLACKHOLE_REJECT
:
2240 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2242 case BLACKHOLE_UNSPEC
:
2243 case BLACKHOLE_NULL
:
2244 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2247 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2251 if ((!fpm
&& kernel_nexthops_supported()
2252 && (!proto_nexthops_only()
2253 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2254 || (fpm
&& force_nhg
)) {
2255 /* Kernel supports nexthop objects */
2256 if (IS_ZEBRA_DEBUG_KERNEL
)
2257 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2258 dplane_ctx_get_nhe_id(ctx
));
2260 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2261 dplane_ctx_get_nhe_id(ctx
)))
2264 /* Have to determine src still */
2265 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2269 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2273 if (p
->family
== AF_INET
) {
2274 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2275 &src
.ipv4
, bytelen
))
2277 } else if (p
->family
== AF_INET6
) {
2278 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2279 &src
.ipv6
, bytelen
))
2284 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2287 /* Count overall nexthops so we can decide whether to use singlepath
2288 * or multipath case.
2291 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2292 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2294 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2300 /* Singlepath case. */
2301 if (nexthop_num
== 1) {
2303 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2304 if (CHECK_FLAG(nexthop
->flags
,
2305 NEXTHOP_FLAG_RECURSIVE
)) {
2310 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2315 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2316 routedesc
= nexthop
->rparent
2317 ? "recursive, single-path"
2320 if (!_netlink_set_tag(&req
->n
, datalen
, tag
))
2323 if (!_netlink_route_build_singlepath(
2324 p
, routedesc
, bytelen
, nexthop
,
2325 &req
->n
, &req
->r
, datalen
, cmd
))
2332 * Add encapsulation information when installing via
2336 if (!netlink_route_nexthop_encap(
2337 &req
->n
, datalen
, nexthop
))
2343 if (p
->family
== AF_INET
) {
2344 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2345 &src
.ipv4
, bytelen
))
2347 } else if (p
->family
== AF_INET6
) {
2348 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2349 &src
.ipv6
, bytelen
))
2353 } else { /* Multipath case */
2354 struct rtattr
*nest
;
2355 const union g_addr
*src1
= NULL
;
2357 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2362 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2363 if (CHECK_FLAG(nexthop
->flags
,
2364 NEXTHOP_FLAG_RECURSIVE
)) {
2365 /* This only works for IPv4 now */
2369 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2374 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2375 routedesc
= nexthop
->rparent
2376 ? "recursive, multipath"
2380 if (!_netlink_route_build_multipath(
2381 p
, routedesc
, bytelen
, nexthop
,
2382 &req
->n
, datalen
, &req
->r
, &src1
,
2386 if (!setsrc
&& src1
) {
2387 if (p
->family
== AF_INET
)
2388 src
.ipv4
= src1
->ipv4
;
2389 else if (p
->family
== AF_INET6
)
2390 src
.ipv6
= src1
->ipv6
;
2397 nl_attr_nest_end(&req
->n
, nest
);
2400 * Add encapsulation information when installing via
2404 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2406 if (CHECK_FLAG(nexthop
->flags
,
2407 NEXTHOP_FLAG_RECURSIVE
))
2409 if (!netlink_route_nexthop_encap(
2410 &req
->n
, datalen
, nexthop
))
2417 if (p
->family
== AF_INET
) {
2418 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2419 &src
.ipv4
, bytelen
))
2421 } else if (p
->family
== AF_INET6
) {
2422 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2423 &src
.ipv6
, bytelen
))
2426 if (IS_ZEBRA_DEBUG_KERNEL
)
2427 zlog_debug("Setting source");
2431 /* If there is no useful nexthop then return. */
2432 if (nexthop_num
== 0) {
2433 if (IS_ZEBRA_DEBUG_KERNEL
)
2434 zlog_debug("%s: No useful nexthop.", __func__
);
2437 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2440 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2442 uint32_t actual_table
;
2444 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2452 struct zebra_ns
*zns
;
2455 memset(&req
, 0, sizeof(req
));
2457 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2458 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2459 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2461 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2463 if (mroute
->family
== AF_INET
) {
2464 req
.rtm
.rtm_family
= RTNL_FAMILY_IPMR
;
2465 req
.rtm
.rtm_dst_len
= IPV4_MAX_BITLEN
;
2466 req
.rtm
.rtm_src_len
= IPV4_MAX_BITLEN
;
2468 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2469 &mroute
->src
.ipaddr_v4
,
2470 sizeof(mroute
->src
.ipaddr_v4
));
2471 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2472 &mroute
->grp
.ipaddr_v4
,
2473 sizeof(mroute
->grp
.ipaddr_v4
));
2475 req
.rtm
.rtm_family
= RTNL_FAMILY_IP6MR
;
2476 req
.rtm
.rtm_dst_len
= IPV6_MAX_BITLEN
;
2477 req
.rtm
.rtm_src_len
= IPV6_MAX_BITLEN
;
2479 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2480 &mroute
->src
.ipaddr_v6
,
2481 sizeof(mroute
->src
.ipaddr_v6
));
2482 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2483 &mroute
->grp
.ipaddr_v6
,
2484 sizeof(mroute
->grp
.ipaddr_v6
));
2490 * So during the namespace cleanup we started storing
2491 * the zvrf table_id for the default table as RT_TABLE_MAIN
2492 * which is what the normal routing table for ip routing is.
2493 * This change caused this to break our lookups of sg data
2494 * because prior to this change the zvrf->table_id was 0
2495 * and when the pim multicast kernel code saw a 0,
2496 * it was auto-translated to RT_TABLE_DEFAULT. But since
2497 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2498 * and the kernel goes screw you and the delicious cookies you
2499 * are trying to give me. So now we have this little hack.
2501 if (mroute
->family
== AF_INET
)
2502 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
)
2506 actual_table
= zvrf
->table_id
;
2508 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2510 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2511 &zns
->netlink_cmd
, zns
, false);
2517 /* Char length to debug ID with */
2518 #define ID_LENGTH 10
2520 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2522 const struct nh_grp
*z_grp
,
2523 const uint8_t count
, bool resilient
,
2524 const struct nhg_resilience
*nhgr
)
2526 struct nexthop_grp grp
[count
];
2527 /* Need space for max group size, "/", and null term */
2528 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2529 char buf1
[ID_LENGTH
+ 2];
2533 memset(grp
, 0, sizeof(grp
));
2536 for (int i
= 0; i
< count
; i
++) {
2537 grp
[i
].id
= z_grp
[i
].id
;
2538 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2540 if (IS_ZEBRA_DEBUG_KERNEL
) {
2542 snprintf(buf
, sizeof(buf1
), "group %u",
2545 snprintf(buf1
, sizeof(buf1
), "/%u",
2547 strlcat(buf
, buf1
, sizeof(buf
));
2551 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2552 count
* sizeof(*grp
)))
2556 struct rtattr
*nest
;
2558 nest
= nl_attr_nest(n
, req_size
, NHA_RES_GROUP
);
2560 nl_attr_put16(n
, req_size
, NHA_RES_GROUP_BUCKETS
,
2562 nl_attr_put32(n
, req_size
, NHA_RES_GROUP_IDLE_TIMER
,
2563 nhgr
->idle_timer
* 1000);
2564 nl_attr_put32(n
, req_size
,
2565 NHA_RES_GROUP_UNBALANCED_TIMER
,
2566 nhgr
->unbalanced_timer
* 1000);
2567 nl_attr_nest_end(n
, nest
);
2569 nl_attr_put16(n
, req_size
, NHA_GROUP_TYPE
,
2570 NEXTHOP_GRP_TYPE_RES
);
2574 if (IS_ZEBRA_DEBUG_KERNEL
)
2575 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2581 * Next hop packet encoding helper function.
2583 * \param[in] cmd netlink command.
2584 * \param[in] ctx dataplane context (information snapshot).
2585 * \param[out] buf buffer to hold the packet.
2586 * \param[in] buflen amount of buffer bytes.
2588 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2589 * otherwise the number of bytes written to buf.
2591 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2592 const struct zebra_dplane_ctx
*ctx
,
2593 void *buf
, size_t buflen
, bool fpm
)
2601 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2602 char label_buf
[256];
2604 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2605 int type
= dplane_ctx_get_nhe_type(ctx
);
2606 struct rtattr
*nest
;
2609 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2613 EC_ZEBRA_NHG_FIB_UPDATE
,
2614 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2619 * Nothing to do if the kernel doesn't support nexthop objects or
2620 * we dont want to install this type of NHG, but FPM may possible to
2623 if (!fpm
&& !kernel_nexthops_supported()) {
2624 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2626 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2627 __func__
, id
, zebra_route_string(type
));
2631 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2632 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2634 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2635 __func__
, id
, zebra_route_string(type
));
2639 label_buf
[0] = '\0';
2641 if (buflen
< sizeof(*req
))
2644 memset(req
, 0, sizeof(*req
));
2646 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2647 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2649 if (cmd
== RTM_NEWNEXTHOP
)
2650 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2652 req
->n
.nlmsg_type
= cmd
;
2653 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2655 req
->nhm
.nh_family
= AF_UNSPEC
;
2658 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2661 if (cmd
== RTM_NEWNEXTHOP
) {
2663 * We distinguish between a "group", which is a collection
2664 * of ids, and a singleton nexthop with an id. The
2665 * group is installed as an id that just refers to a list of
2668 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2669 const struct nexthop_group
*nhg
;
2670 const struct nhg_resilience
*nhgr
;
2672 nhg
= dplane_ctx_get_nhe_ng(ctx
);
2674 if (!_netlink_nexthop_build_group(
2675 &req
->n
, buflen
, id
,
2676 dplane_ctx_get_nhe_nh_grp(ctx
),
2677 dplane_ctx_get_nhe_nh_grp_count(ctx
),
2678 !!nhgr
->buckets
, nhgr
))
2681 const struct nexthop
*nh
=
2682 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2683 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2686 req
->nhm
.nh_family
= AF_INET
;
2687 else if (afi
== AFI_IP6
)
2688 req
->nhm
.nh_family
= AF_INET6
;
2691 case NEXTHOP_TYPE_IPV4
:
2692 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2693 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2698 case NEXTHOP_TYPE_IPV6
:
2699 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2700 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2705 case NEXTHOP_TYPE_BLACKHOLE
:
2706 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2709 /* Blackhole shouldn't have anymore attributes
2712 case NEXTHOP_TYPE_IFINDEX
:
2713 /* Don't need anymore info for this */
2719 EC_ZEBRA_NHG_FIB_UPDATE
,
2720 "Context received for kernel nexthop update without an interface");
2724 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2728 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2729 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2731 num_labels
= build_label_stack(
2732 nh
->nh_label
, nh
->nh_label_type
, out_lse
,
2733 label_buf
, sizeof(label_buf
));
2735 if (num_labels
&& nh
->nh_label_type
== ZEBRA_LSP_EVPN
) {
2736 if (!nl_attr_put16(&req
->n
, buflen
,
2741 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2745 if (_netlink_nexthop_encode_dvni_label(
2746 nh
, &req
->n
, out_lse
, buflen
,
2747 label_buf
) == false)
2750 nl_attr_nest_end(&req
->n
, nest
);
2752 } else if (num_labels
) {
2753 /* Set the BoS bit */
2754 out_lse
[num_labels
- 1] |=
2755 htonl(1 << MPLS_LS_S_SHIFT
);
2758 * TODO: MPLS unsupported for now in kernel.
2760 if (req
->nhm
.nh_family
== AF_MPLS
)
2763 encap
= LWTUNNEL_ENCAP_MPLS
;
2764 if (!nl_attr_put16(&req
->n
, buflen
,
2765 NHA_ENCAP_TYPE
, encap
))
2767 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2771 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2773 num_labels
* sizeof(mpls_lse_t
)))
2776 nl_attr_nest_end(&req
->n
, nest
);
2780 if (nh
->nh_srv6
->seg6local_action
!=
2781 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2784 struct rtattr
*nest
;
2785 const struct seg6local_context
*ctx
;
2787 req
->nhm
.nh_family
= AF_INET6
;
2788 action
= nh
->nh_srv6
->seg6local_action
;
2789 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2790 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2791 if (!nl_attr_put(&req
->n
, buflen
,
2797 nest
= nl_attr_nest(&req
->n
, buflen
,
2798 NHA_ENCAP
| NLA_F_NESTED
);
2803 case SEG6_LOCAL_ACTION_END
:
2807 SEG6_LOCAL_ACTION_END
))
2810 case SEG6_LOCAL_ACTION_END_X
:
2814 SEG6_LOCAL_ACTION_END_X
))
2818 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2819 sizeof(struct in6_addr
)))
2822 case SEG6_LOCAL_ACTION_END_T
:
2826 SEG6_LOCAL_ACTION_END_T
))
2834 case SEG6_LOCAL_ACTION_END_DX4
:
2838 SEG6_LOCAL_ACTION_END_DX4
))
2842 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2843 sizeof(struct in_addr
)))
2846 case SEG6_LOCAL_ACTION_END_DT6
:
2850 SEG6_LOCAL_ACTION_END_DT6
))
2858 case SEG6_LOCAL_ACTION_END_DT4
:
2862 SEG6_LOCAL_ACTION_END_DT4
))
2866 SEG6_LOCAL_VRFTABLE
,
2870 case SEG6_LOCAL_ACTION_END_DT46
:
2874 SEG6_LOCAL_ACTION_END_DT46
))
2878 SEG6_LOCAL_VRFTABLE
,
2883 zlog_err("%s: unsupport seg6local behaviour action=%u",
2887 nl_attr_nest_end(&req
->n
, nest
);
2890 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2893 struct rtattr
*nest
;
2895 if (!nl_attr_put16(&req
->n
, buflen
,
2897 LWTUNNEL_ENCAP_SEG6
))
2899 nest
= nl_attr_nest(&req
->n
, buflen
,
2900 NHA_ENCAP
| NLA_F_NESTED
);
2903 tun_len
= fill_seg6ipt_encap(tun_buf
,
2905 &nh
->nh_srv6
->seg6_segs
);
2908 if (!nl_attr_put(&req
->n
, buflen
,
2912 nl_attr_nest_end(&req
->n
, nest
);
2918 if (IS_ZEBRA_DEBUG_KERNEL
)
2919 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2920 __func__
, id
, nh
, nh
->ifindex
,
2921 vrf_id_to_name(nh
->vrf_id
),
2922 nh
->vrf_id
, label_buf
);
2925 req
->nhm
.nh_protocol
= zebra2proto(type
);
2927 } else if (cmd
!= RTM_DELNEXTHOP
) {
2929 EC_ZEBRA_NHG_FIB_UPDATE
,
2930 "Nexthop group kernel update command (%d) does not exist",
2935 if (IS_ZEBRA_DEBUG_KERNEL
)
2936 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2939 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2942 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2943 void *buf
, size_t buflen
)
2945 enum dplane_op_e op
;
2948 op
= dplane_ctx_get_op(ctx
);
2949 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2950 cmd
= RTM_NEWNEXTHOP
;
2951 else if (op
== DPLANE_OP_NH_DELETE
)
2952 cmd
= RTM_DELNEXTHOP
;
2954 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2955 "Context received for kernel nexthop update with incorrect OP code (%u)",
2960 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
, false);
2963 enum netlink_msg_status
2964 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2965 struct zebra_dplane_ctx
*ctx
)
2967 /* Nothing to do if the kernel doesn't support nexthop objects */
2968 if (!kernel_nexthops_supported())
2969 return FRR_NETLINK_SUCCESS
;
2971 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2975 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2976 void *buf
, size_t buflen
)
2978 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2979 buflen
, false, false);
2982 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2983 void *buf
, size_t buflen
)
2985 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2986 buflen
, false, false);
2989 enum netlink_msg_status
2990 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2993 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2995 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2997 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2999 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
3001 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
3002 /* Single 'replace' operation */
3005 * With route replace semantics in place
3006 * for v4 routes and the new route is a system
3007 * route we do not install anything.
3008 * The problem here is that the new system
3009 * route should cause us to withdraw from
3010 * the kernel the old non-system route
3012 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
3013 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
3014 return netlink_batch_add_msg(
3015 bth
, ctx
, netlink_delroute_msg_encoder
,
3019 * So v6 route replace semantics are not in
3020 * the kernel at this point as I understand it.
3021 * so let's do a delete then an add.
3022 * In the future once v6 route replace semantics
3023 * are in we can figure out what to do here to
3024 * allow working with old and new kernels.
3026 * I'm also intentionally ignoring the failure case
3027 * of the route delete. If that happens yeah we're
3030 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
3031 netlink_batch_add_msg(
3032 bth
, ctx
, netlink_delroute_msg_encoder
,
3038 return FRR_NETLINK_ERROR
;
3040 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
3041 return FRR_NETLINK_SUCCESS
;
3043 return netlink_batch_add_msg(bth
, ctx
,
3045 ? netlink_newroute_msg_encoder
3046 : netlink_delroute_msg_encoder
,
3051 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
3053 * @tb: Netlink RTA data
3054 * @family: Address family in the nhmsg
3055 * @ifp: Interface connected - this should be NULL, we fill it in
3056 * @ns_id: Namspace id
3058 * Return: New nexthop
3060 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
3061 unsigned char family
,
3062 struct interface
**ifp
,
3065 struct nexthop nh
= {};
3067 enum nexthop_types_t type
= 0;
3070 struct interface
*ifp_lookup
;
3072 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
3075 if (tb
[NHA_GATEWAY
]) {
3078 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
3082 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
3087 EC_ZEBRA_BAD_NHG_MESSAGE
,
3088 "Nexthop gateway with bad address family (%d) received from kernel",
3092 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
3094 type
= NEXTHOP_TYPE_IFINDEX
;
3100 memcpy(&(nh
.gate
), gate
, sz
);
3103 nh
.ifindex
= if_index
;
3106 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
3111 nh
.vrf_id
= ifp_lookup
->vrf
->vrf_id
;
3114 EC_ZEBRA_UNKNOWN_INTERFACE
,
3115 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
3116 __func__
, nh
.ifindex
);
3118 nh
.vrf_id
= VRF_DEFAULT
;
3121 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
3122 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
3125 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
3127 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
3128 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
3131 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
3138 static int netlink_nexthop_process_group(struct rtattr
**tb
,
3139 struct nh_grp
*z_grp
, int z_grp_size
,
3140 struct nhg_resilience
*nhgr
)
3143 /* linux/nexthop.h group struct */
3144 struct nexthop_grp
*n_grp
= NULL
;
3146 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
3147 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
3149 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
3150 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
3151 "Invalid nexthop group received from the kernel");
3155 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
3156 z_grp
[i
].id
= n_grp
[i
].id
;
3157 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
3160 memset(nhgr
, 0, sizeof(*nhgr
));
3161 if (tb
[NHA_RES_GROUP
]) {
3162 struct rtattr
*tbn
[NHA_RES_GROUP_MAX
+ 1];
3164 struct rtattr
*res_group
= tb
[NHA_RES_GROUP
];
3166 netlink_parse_rtattr_nested(tbn
, NHA_RES_GROUP_MAX
, res_group
);
3168 if (tbn
[NHA_RES_GROUP_BUCKETS
]) {
3169 rta
= tbn
[NHA_RES_GROUP_BUCKETS
];
3170 nhgr
->buckets
= *(uint16_t *)RTA_DATA(rta
);
3173 if (tbn
[NHA_RES_GROUP_IDLE_TIMER
]) {
3174 rta
= tbn
[NHA_RES_GROUP_IDLE_TIMER
];
3175 nhgr
->idle_timer
= *(uint32_t *)RTA_DATA(rta
);
3178 if (tbn
[NHA_RES_GROUP_UNBALANCED_TIMER
]) {
3179 rta
= tbn
[NHA_RES_GROUP_UNBALANCED_TIMER
];
3180 nhgr
->unbalanced_timer
= *(uint32_t *)RTA_DATA(rta
);
3183 if (tbn
[NHA_RES_GROUP_UNBALANCED_TIME
]) {
3184 rta
= tbn
[NHA_RES_GROUP_UNBALANCED_TIME
];
3185 nhgr
->unbalanced_time
= *(uint64_t *)RTA_DATA(rta
);
3193 * netlink_nexthop_change() - Read in change about nexthops from the kernel
3195 * @h: Netlink message header
3196 * @ns_id: Namspace id
3197 * @startup: Are we reading under startup conditions?
3199 * Return: Result status
3201 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3204 /* nexthop group id */
3206 unsigned char family
;
3208 afi_t afi
= AFI_UNSPEC
;
3209 vrf_id_t vrf_id
= VRF_DEFAULT
;
3210 struct interface
*ifp
= NULL
;
3211 struct nhmsg
*nhm
= NULL
;
3212 struct nexthop nh
= {};
3213 struct nh_grp grp
[MULTIPATH_NUM
] = {};
3214 /* Count of nexthops in group array */
3215 uint8_t grp_count
= 0;
3216 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
3218 frrtrace(3, frr_zebra
, netlink_nexthop_change
, h
, ns_id
, startup
);
3220 nhm
= NLMSG_DATA(h
);
3225 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
3228 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
3231 "%s: Message received from netlink is of a broken size %d %zu",
3232 __func__
, h
->nlmsg_len
,
3233 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
3237 netlink_parse_rtattr_flags(tb
, NHA_MAX
, RTM_NHA(nhm
), len
,
3243 EC_ZEBRA_BAD_NHG_MESSAGE
,
3244 "Nexthop group without an ID received from the kernel");
3248 /* We use the ID key'd nhg table for kernel updates */
3249 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
3251 if (zebra_evpn_mh_is_fdb_nh(id
)) {
3252 /* If this is a L2 NH just ignore it */
3253 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3254 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
3260 family
= nhm
->nh_family
;
3261 afi
= family2afi(family
);
3263 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
3265 if (IS_ZEBRA_DEBUG_KERNEL
)
3266 zlog_debug("%s ID (%u) %s NS %u",
3267 nl_msg_type_to_str(h
->nlmsg_type
), id
,
3268 nl_family_to_str(family
), ns_id
);
3271 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
3272 struct nhg_resilience nhgr
= {};
3274 if (tb
[NHA_GROUP
]) {
3276 * If this is a group message its only going to have
3277 * an array of nexthop IDs associated with it
3279 grp_count
= netlink_nexthop_process_group(
3280 tb
, grp
, array_size(grp
), &nhgr
);
3282 if (tb
[NHA_BLACKHOLE
]) {
3284 * This nexthop is just for blackhole-ing
3285 * traffic, it should not have an OIF, GATEWAY,
3288 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
3289 nh
.bh_type
= BLACKHOLE_UNSPEC
;
3290 } else if (tb
[NHA_OIF
])
3292 * This is a true new nexthop, so we need
3293 * to parse the gateway and device info
3295 nh
= netlink_nexthop_process_nh(tb
, family
,
3300 EC_ZEBRA_BAD_NHG_MESSAGE
,
3301 "Invalid Nexthop message received from the kernel with ID (%u)",
3305 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
3306 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
3307 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
3311 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
3312 type
, startup
, &nhgr
))
3315 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
3316 zebra_nhg_kernel_del(id
, vrf_id
);
3322 * netlink_request_nexthop() - Request nextop information from the kernel
3323 * @zns: Zebra namespace
3324 * @family: AF_* netlink family
3325 * @type: RTM_* route type
3327 * Return: Result status
3329 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
3336 /* Form the request, specifying filter (rtattr) if needed. */
3337 memset(&req
, 0, sizeof(req
));
3338 req
.n
.nlmsg_type
= type
;
3339 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3340 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3341 req
.nhm
.nh_family
= family
;
3343 return netlink_request(&zns
->netlink_cmd
, &req
);
3348 * netlink_nexthop_read() - Nexthop read function using netlink interface
3350 * @zns: Zebra name space
3352 * Return: Result status
3353 * Only called at bootstrap time.
3355 int netlink_nexthop_read(struct zebra_ns
*zns
)
3358 struct zebra_dplane_info dp_info
;
3360 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3362 /* Get nexthop objects */
3363 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3366 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3370 /* If we succesfully read in nexthop objects,
3371 * this kernel must support them.
3374 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3375 zlog_debug("Nexthop objects %ssupported on this kernel",
3376 supports_nh
? "" : "not ");
3378 zebra_router_set_supports_nhgs(supports_nh
);
3384 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3385 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3387 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3388 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3393 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3394 * evpn neighbor update as netlink messages using dataplane context object.
3395 * Here, a neighbor refers to a bridge forwarding database entry for
3396 * either unicast forwarding or head-end replication or an IP neighbor
3398 * @ctx: Dataplane context
3399 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3400 * @lla: A pointer to neighbor cache link layer address
3401 * @llalen: Length of the pointer to neighbor cache link layer
3403 * @ip: A neighbor cache n/w layer destination address
3404 * In the case of bridge FDB, this represnts the remote
3406 * @replace_obj: Whether NEW request should replace existing object or
3407 * add to the end of the list
3408 * @family: AF_* netlink family
3409 * @type: RTN_* route type
3410 * @flags: NTF_* flags
3411 * @state: NUD_* states
3412 * @data: data buffer pointer
3413 * @datalen: total amount of data buffer space
3414 * @protocol: protocol information
3416 * Return: 0 when the msg doesn't fit entirely in the buffer
3417 * otherwise the number of bytes written to buf.
3419 static ssize_t
netlink_neigh_update_msg_encode(
3420 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3421 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3422 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3423 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3424 size_t datalen
, uint8_t protocol
)
3432 enum dplane_op_e op
;
3434 if (datalen
< sizeof(*req
))
3436 memset(req
, 0, sizeof(*req
));
3438 op
= dplane_ctx_get_op(ctx
);
3440 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3441 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3442 if (cmd
== RTM_NEWNEIGH
)
3443 req
->n
.nlmsg_flags
|=
3445 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3446 req
->n
.nlmsg_type
= cmd
;
3447 req
->ndm
.ndm_family
= family
;
3448 req
->ndm
.ndm_type
= type
;
3449 req
->ndm
.ndm_state
= state
;
3450 req
->ndm
.ndm_flags
= flags
;
3451 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3453 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3458 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3463 struct rtattr
*nest
;
3465 nest
= nl_attr_nest(&req
->n
, datalen
,
3466 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3470 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3471 &nfy_flags
, sizeof(nfy_flags
)))
3473 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3476 nl_attr_nest_end(&req
->n
, nest
);
3481 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3487 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3491 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3492 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3497 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3498 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3499 vni_t vni
= dplane_ctx_mac_get_vni(ctx
);
3502 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3507 if (!nl_attr_put32(&req
->n
, datalen
, NDA_SRC_VNI
, vni
))
3511 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3512 dplane_ctx_mac_get_br_ifindex(ctx
)))
3516 if (op
== DPLANE_OP_VTEP_ADD
|| op
== DPLANE_OP_VTEP_DELETE
) {
3517 vni_t vni
= dplane_ctx_neigh_get_vni(ctx
);
3520 if (!nl_attr_put32(&req
->n
, datalen
, NDA_SRC_VNI
, vni
))
3525 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3529 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3530 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3533 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3534 void *buf
, size_t buflen
)
3536 struct ethaddr dst_mac
= {.octet
= {0}};
3537 int proto
= RTPROT_ZEBRA
;
3539 if (dplane_ctx_get_type(ctx
) != 0)
3540 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3542 return netlink_neigh_update_msg_encode(
3543 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3544 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3545 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3546 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3551 #define NDA_RTA(r) \
3552 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3555 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3558 struct interface
*ifp
;
3559 struct zebra_if
*zif
;
3560 struct rtattr
*tb
[NDA_MAX
+ 1];
3561 struct interface
*br_if
;
3564 struct in_addr vtep_ip
;
3565 int vid_present
= 0, dst_present
= 0;
3569 bool local_inactive
= false;
3570 bool dp_static
= false;
3572 uint32_t nhg_id
= 0;
3573 bool vni_mcast_grp
= false;
3575 ndm
= NLMSG_DATA(h
);
3577 /* We only process macfdb notifications if EVPN is enabled */
3578 if (!is_evpn_enabled())
3581 /* Parse attributes and extract fields of interest. Do basic
3582 * validation of the fields.
3584 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3587 if (!tb
[NDA_LLADDR
]) {
3588 if (IS_ZEBRA_DEBUG_KERNEL
)
3589 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3590 nl_msg_type_to_str(h
->nlmsg_type
),
3595 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3596 if (IS_ZEBRA_DEBUG_KERNEL
)
3598 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3599 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3600 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3604 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3608 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3609 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3613 /* TODO: Only IPv4 supported now. */
3615 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3617 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3620 memset(&vtep_ip
, 0, sizeof(vtep_ip
));
3623 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3625 if (ndm
->ndm_state
& NUD_STALE
)
3626 local_inactive
= true;
3628 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3629 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3630 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3632 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3633 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3636 nfy_flags
= *(uint8_t *)RTA_DATA(
3637 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3638 if (nfy_flags
& FDB_NOTIFY_BIT
)
3640 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3641 local_inactive
= true;
3645 if (tb
[NDA_SRC_VNI
])
3646 vni
= *(vni_t
*)RTA_DATA(tb
[NDA_SRC_VNI
]);
3648 if (IS_ZEBRA_DEBUG_KERNEL
)
3650 "Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d vni %d",
3651 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3652 vid_present
? vid_buf
: "", ndm
->ndm_state
,
3653 ndm
->ndm_flags
, &mac
, dst_present
? dst_buf
: "",
3656 /* The interface should exist. */
3657 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3659 if (!ifp
|| !ifp
->info
)
3662 /* The interface should be something we're interested in. */
3663 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3666 zif
= (struct zebra_if
*)ifp
->info
;
3667 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3668 if (IS_ZEBRA_DEBUG_KERNEL
)
3670 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3671 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3673 zif
->brslave_info
.bridge_ifindex
);
3677 /* For per vni device, vni comes from device itself */
3678 if (IS_ZEBRA_IF_VXLAN(ifp
) && IS_ZEBRA_VXLAN_IF_VNI(zif
)) {
3679 struct zebra_vxlan_vni
*vnip
;
3681 vnip
= zebra_vxlan_if_vni_find(zif
, 0);
3685 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3687 if (filter_vlan
&& vid
!= filter_vlan
) {
3688 if (IS_ZEBRA_DEBUG_KERNEL
)
3689 zlog_debug(" Filtered due to filter vlan: %d",
3695 * Check if this is a mcast group update (svd case)
3697 vni_mcast_grp
= is_mac_vni_mcast_group(&mac
, vni
, vtep_ip
);
3699 /* If add or update, do accordingly if learnt on a "local" interface; if
3700 * the notification is over VxLAN, this has to be related to
3702 * so perform an implicit delete of any local entry (if it exists).
3704 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3705 /* Drop "permanent" entries. */
3706 if (!vni_mcast_grp
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3707 if (IS_ZEBRA_DEBUG_KERNEL
)
3709 " Dropping entry because of NUD_PERMANENT");
3713 if (IS_ZEBRA_IF_VXLAN(ifp
)) {
3718 return zebra_vxlan_if_vni_mcast_group_update(
3719 ifp
, vni
, &vtep_ip
);
3721 return zebra_vxlan_dp_network_mac_add(
3722 ifp
, br_if
, &mac
, vid
, vni
, nhg_id
, sticky
,
3723 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3726 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3727 sticky
, local_inactive
, dp_static
);
3730 /* This is a delete notification.
3731 * Ignore the notification with IP dest as it may just signify that the
3732 * MAC has moved from remote to local. The exception is the special
3733 * all-zeros MAC that represents the BUM flooding entry; we may have
3734 * to readd it. Otherwise,
3735 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3736 * 2. For a MAC over "local" interface, delete the mac
3737 * Note: We will get notifications from both bridge driver and VxLAN
3745 return zebra_vxlan_if_vni_mcast_group_update(ifp
, vni
,
3748 if (is_zero_mac(&mac
) && vni
)
3749 return zebra_vxlan_check_readd_vtep(ifp
, vni
, vtep_ip
);
3754 if (IS_ZEBRA_IF_VXLAN(ifp
))
3757 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3760 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3765 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3768 /* Length validity. */
3769 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3773 /* We are interested only in AF_BRIDGE notifications. */
3774 ndm
= NLMSG_DATA(h
);
3775 if (ndm
->ndm_family
!= AF_BRIDGE
)
3778 return netlink_macfdb_change(h
, len
, ns_id
);
3781 /* Request for MAC FDB information from the kernel */
3782 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3783 int type
, ifindex_t master_ifindex
)
3787 struct ifinfomsg ifm
;
3791 /* Form the request, specifying filter (rtattr) if needed. */
3792 memset(&req
, 0, sizeof(req
));
3793 req
.n
.nlmsg_type
= type
;
3794 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3795 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3796 req
.ifm
.ifi_family
= family
;
3798 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3800 return netlink_request(netlink_cmd
, &req
);
3804 * MAC forwarding database read using netlink interface. This is invoked
3807 int netlink_macfdb_read(struct zebra_ns
*zns
)
3810 struct zebra_dplane_info dp_info
;
3812 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3814 /* Get bridge FDB table. */
3815 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3819 /* We are reading entire table. */
3821 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3828 * MAC forwarding database read using netlink interface. This is for a
3829 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3831 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3832 struct interface
*br_if
, vlanid_t vid
)
3834 struct zebra_if
*br_zif
;
3835 struct zebra_dplane_info dp_info
;
3838 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3840 /* Save VLAN we're filtering on, if needed. */
3841 br_zif
= (struct zebra_if
*)br_if
->info
;
3842 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3845 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3847 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3851 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3852 &dp_info
, 0, false);
3854 /* Reset VLAN filter. */
3860 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3861 static int netlink_request_specific_mac(struct zebra_ns
*zns
, int family
,
3862 int type
, struct interface
*ifp
,
3863 const struct ethaddr
*mac
, vlanid_t vid
,
3864 vni_t vni
, uint8_t flags
)
3871 struct zebra_if
*zif
;
3873 memset(&req
, 0, sizeof(req
));
3874 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3875 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3876 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3877 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3878 req
.ndm
.ndm_flags
= flags
;
3879 /* req.ndm.ndm_state = NUD_REACHABLE; */
3881 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3883 zif
= (struct zebra_if
*)ifp
->info
;
3884 /* Is this a read on a VXLAN interface? */
3885 if (IS_ZEBRA_IF_VXLAN(ifp
)) {
3886 nl_attr_put32(&req
.n
, sizeof(req
), NDA_VNI
, vni
);
3887 /* TBD: Why is ifindex not filled in the non-vxlan case? */
3888 req
.ndm
.ndm_ifindex
= ifp
->ifindex
;
3890 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(zif
) && vid
> 0)
3891 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3892 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, ifp
->ifindex
);
3895 if (IS_ZEBRA_DEBUG_KERNEL
)
3896 zlog_debug("Tx %s %s IF %s(%u) MAC %pEA vid %u vni %u",
3897 nl_msg_type_to_str(type
),
3898 nl_family_to_str(req
.ndm
.ndm_family
), ifp
->name
,
3899 ifp
->ifindex
, mac
, vid
, vni
);
3901 return netlink_request(&zns
->netlink_cmd
, &req
);
3904 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3905 struct interface
*br_if
,
3906 const struct ethaddr
*mac
, vlanid_t vid
)
3909 struct zebra_dplane_info dp_info
;
3911 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3913 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3915 ret
= netlink_request_specific_mac(zns
, AF_BRIDGE
, RTM_GETNEIGH
, br_if
,
3920 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3926 int netlink_macfdb_read_mcast_for_vni(struct zebra_ns
*zns
,
3927 struct interface
*ifp
, vni_t vni
)
3929 struct zebra_if
*zif
;
3930 struct ethaddr mac
= {.octet
= {0}};
3931 struct zebra_dplane_info dp_info
;
3935 if (IS_ZEBRA_VXLAN_IF_VNI(zif
))
3938 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3940 /* Get specific FDB entry for BUM handling, if any */
3941 ret
= netlink_request_specific_mac(zns
, AF_BRIDGE
, RTM_GETNEIGH
, ifp
,
3942 &mac
, 0, vni
, NTF_SELF
);
3946 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3947 &dp_info
, 1, false);
3953 * Netlink-specific handler for MAC updates using dataplane context object.
3955 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3958 struct ipaddr vtep_ip
;
3965 uint32_t update_flags
;
3967 uint8_t nfy_flags
= 0;
3968 int proto
= RTPROT_ZEBRA
;
3970 if (dplane_ctx_get_type(ctx
) != 0)
3971 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3973 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3974 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3977 state
= NUD_REACHABLE
;
3979 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3980 if (update_flags
& DPLANE_MAC_REMOTE
) {
3982 if (dplane_ctx_mac_is_sticky(ctx
)) {
3983 /* NUD_NOARP prevents the entry from expiring */
3985 /* sticky the entry from moving */
3986 flags
|= NTF_STICKY
;
3988 flags
|= NTF_EXT_LEARNED
;
3990 /* if it was static-local previously we need to clear the
3991 * notify flags on replace with remote
3993 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3997 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3998 nfy_flags
|= FDB_NOTIFY_BIT
;
4002 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
4003 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
4008 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
4009 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
4010 SET_IPADDR_V4(&vtep_ip
);
4012 if (IS_ZEBRA_DEBUG_KERNEL
) {
4014 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
4016 vid
= dplane_ctx_mac_get_vlan(ctx
);
4018 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
4023 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
4024 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
4025 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4026 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
4027 mac
, &vtep_ip
, nhg_id
,
4028 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
4029 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
4031 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
4032 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
4037 total
= netlink_neigh_update_msg_encode(
4038 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
4039 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
4040 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
4047 * In the event the kernel deletes ipv4 link-local neighbor entries created for
4048 * 5549 support, re-install them.
4050 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
4051 struct interface
*ifp
, struct ipaddr
*ip
,
4054 if (ndm
->ndm_family
!= AF_INET
)
4057 if (!zif
->v6_2_v4_ll_neigh_entry
)
4060 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
4063 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
4064 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
4069 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
4073 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
4075 #define NUD_LOCAL_ACTIVE \
4076 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
4078 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
4080 /* an exact match is done between
4081 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
4082 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
4083 * (see in lib/zclient.h)
4087 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
4090 struct interface
*ifp
;
4091 struct zebra_if
*zif
;
4092 struct rtattr
*tb
[NDA_MAX
+ 1];
4093 struct interface
*link_if
;
4096 char buf
[ETHER_ADDR_STRLEN
];
4097 int mac_present
= 0;
4100 bool local_inactive
;
4101 uint32_t ext_flags
= 0;
4102 bool dp_static
= false;
4106 ndm
= NLMSG_DATA(h
);
4108 /* The interface should exist. */
4109 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
4111 if (!ifp
|| !ifp
->info
)
4114 zif
= (struct zebra_if
*)ifp
->info
;
4116 /* Parse attributes and extract fields of interest. */
4117 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
4120 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
4121 nl_msg_type_to_str(h
->nlmsg_type
),
4122 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4123 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
4127 memset(&ip
, 0, sizeof(ip
));
4128 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
4129 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
4131 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
4132 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
4133 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
4134 if (IS_ZEBRA_DEBUG_KERNEL
)
4136 " Neighbor Entry Received is a 5549 entry, finished");
4140 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
4141 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
4142 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
4144 /* we send link layer information to client:
4145 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
4146 * - struct ipaddr ( for DEL and GET)
4147 * - struct ethaddr mac; (for NEW)
4149 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
4150 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
4151 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
4152 cmd
= ZEBRA_NHRP_NEIGH_GET
;
4153 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
4154 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
4156 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
4160 if (tb
[NDA_LLADDR
]) {
4161 /* copy LLADDR information */
4162 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
4164 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
4165 union sockunion link_layer_ipv4
;
4168 sockunion_family(&link_layer_ipv4
) = AF_INET
;
4169 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
4170 RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
4172 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
4173 zsend_nhrp_neighbor_notify(
4175 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
4179 if (h
->nlmsg_type
== RTM_GETNEIGH
)
4182 /* The neighbor is present on an SVI. From this, we locate the
4184 * bridge because we're only interested in neighbors on a VxLAN bridge.
4185 * The bridge is located based on the nature of the SVI:
4186 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
4188 * and is linked to the bridge
4189 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
4193 if (IS_ZEBRA_IF_VLAN(ifp
)) {
4194 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
4198 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
4202 if (IS_ZEBRA_DEBUG_KERNEL
)
4204 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
4207 memset(&mac
, 0, sizeof(mac
));
4208 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
4209 if (tb
[NDA_LLADDR
]) {
4210 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4211 if (IS_ZEBRA_DEBUG_KERNEL
)
4213 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
4218 ifp
->name
, ndm
->ndm_ifindex
,
4221 (unsigned long)RTA_PAYLOAD(
4227 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
4230 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
4231 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
4233 if (tb
[NDA_EXT_FLAGS
]) {
4234 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
4235 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
4239 if (IS_ZEBRA_DEBUG_KERNEL
)
4241 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
4242 nl_msg_type_to_str(h
->nlmsg_type
),
4243 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4244 ndm
->ndm_ifindex
, ifp
->vrf
->name
,
4245 ifp
->vrf
->vrf_id
, &ip
,
4247 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
4249 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
4251 /* If the neighbor state is valid for use, process as an add or
4253 * else process as a delete. Note that the delete handling may
4255 * in re-adding the neighbor if it is a valid "remote" neighbor.
4257 if (ndm
->ndm_state
& NUD_VALID
) {
4258 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
4260 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
4262 /* If EVPN-MH is not enabled we treat STALE
4263 * neighbors as locally-active and advertise
4266 local_inactive
= false;
4268 /* Add local neighbors to the l3 interface database */
4270 zebra_neigh_del(ifp
, &ip
);
4272 zebra_neigh_add(ifp
, &ip
, &mac
);
4275 zebra_vxlan_handle_kernel_neigh_update(
4276 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
4277 is_ext
, is_router
, local_inactive
,
4283 zebra_neigh_del(ifp
, &ip
);
4285 zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
4289 if (IS_ZEBRA_DEBUG_KERNEL
)
4290 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
4291 nl_msg_type_to_str(h
->nlmsg_type
),
4292 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4293 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
4296 /* Process the delete - it may result in re-adding the neighbor if it is
4297 * a valid "remote" neighbor.
4299 zebra_neigh_del(ifp
, &ip
);
4301 zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
4306 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
4311 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
4314 /* Length validity. */
4315 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4319 /* We are interested only in AF_INET or AF_INET6 notifications. */
4320 ndm
= NLMSG_DATA(h
);
4321 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
4324 return netlink_neigh_change(h
, len
);
4327 /* Request for IP neighbor information from the kernel */
4328 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
4329 int type
, ifindex_t ifindex
)
4337 /* Form the request, specifying filter (rtattr) if needed. */
4338 memset(&req
, 0, sizeof(req
));
4339 req
.n
.nlmsg_type
= type
;
4340 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
4341 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4342 req
.ndm
.ndm_family
= family
;
4344 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
4346 return netlink_request(netlink_cmd
, &req
);
4350 * IP Neighbor table read using netlink interface. This is invoked
4353 int netlink_neigh_read(struct zebra_ns
*zns
)
4356 struct zebra_dplane_info dp_info
;
4358 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4360 /* Get IP neighbor table. */
4361 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4365 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4372 * IP Neighbor table read using netlink interface. This is for a specific
4375 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
4378 struct zebra_dplane_info dp_info
;
4380 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4382 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4386 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4387 &dp_info
, 0, false);
4393 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
4394 * read using netlink interface.
4396 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
4398 const struct ipaddr
*ip
,
4408 /* Form the request, specifying filter (rtattr) if needed. */
4409 memset(&req
, 0, sizeof(req
));
4410 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4411 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4412 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
4413 req
.ndm
.ndm_ifindex
= ifindex
;
4415 if (IS_IPADDR_V4(ip
)) {
4416 ipa_len
= IPV4_MAX_BYTELEN
;
4417 req
.ndm
.ndm_family
= AF_INET
;
4420 ipa_len
= IPV6_MAX_BYTELEN
;
4421 req
.ndm
.ndm_family
= AF_INET6
;
4424 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
4426 if (IS_ZEBRA_DEBUG_KERNEL
)
4427 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
4428 __func__
, nl_msg_type_to_str(type
),
4429 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
4432 return netlink_request(&zns
->netlink_cmd
, &req
);
4435 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4436 struct interface
*vlan_if
)
4439 struct zebra_ns
*zns
;
4440 struct zebra_vrf
*zvrf
= vlan_if
->vrf
->info
;
4441 struct zebra_dplane_info dp_info
;
4445 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4447 if (IS_ZEBRA_DEBUG_KERNEL
)
4448 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4449 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4450 vlan_if
->vrf
->name
, vlan_if
->vrf
->vrf_id
);
4452 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4457 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4458 &dp_info
, 1, false);
4463 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4468 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4469 || h
->nlmsg_type
== RTM_GETNEIGH
))
4472 /* Length validity. */
4473 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4476 "%s: Message received from netlink is of a broken size %d %zu",
4477 __func__
, h
->nlmsg_len
,
4478 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4482 /* Is this a notification for the MAC FDB or IP neighbor table? */
4483 ndm
= NLMSG_DATA(h
);
4484 if (ndm
->ndm_family
== AF_BRIDGE
)
4485 return netlink_macfdb_change(h
, len
, ns_id
);
4487 if (ndm
->ndm_type
!= RTN_UNICAST
)
4490 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4491 return netlink_ipneigh_change(h
, len
, ns_id
);
4494 EC_ZEBRA_UNKNOWN_FAMILY
,
4495 "Invalid address family: %u received from kernel neighbor change: %s",
4496 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4504 * Utility neighbor-update function, using info from dplane context.
4506 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4507 int cmd
, void *buf
, size_t buflen
)
4509 const struct ipaddr
*ip
;
4510 const struct ethaddr
*mac
= NULL
;
4511 const struct ipaddr
*link_ip
= NULL
;
4512 const void *link_ptr
= NULL
;
4513 char buf2
[ETHER_ADDR_STRLEN
];
4519 uint32_t update_flags
;
4520 uint32_t ext_flags
= 0;
4522 int proto
= RTPROT_ZEBRA
;
4524 if (dplane_ctx_get_type(ctx
) != 0)
4525 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4527 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4529 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4530 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4531 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4532 llalen
= IPADDRSZ(link_ip
);
4533 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4534 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4536 mac
= dplane_ctx_neigh_get_mac(ctx
);
4538 link_ptr
= (const void *)mac
;
4539 if (is_zero_mac(mac
))
4542 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4544 snprintf(buf2
, sizeof(buf2
), "null");
4546 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4547 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4548 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4550 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4552 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4553 flags
|= NTF_EXT_LEARNED
;
4554 /* if it was static-local previously we need to clear the
4555 * ext flags on replace with remote
4557 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4559 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4562 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4563 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4565 if (IS_ZEBRA_DEBUG_KERNEL
)
4567 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4568 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4569 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4570 ip
, link_ip
? "Link" : "MAC", buf2
, flags
, state
,
4571 ext
? "ext " : "", ext_flags
);
4573 return netlink_neigh_update_msg_encode(
4574 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4575 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4576 ext_flags
, buf
, buflen
, proto
);
4579 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4580 void *data
, size_t datalen
)
4587 struct rtattr
*nest
;
4592 if (datalen
< sizeof(*req
))
4594 memset(req
, 0, sizeof(*req
));
4595 family
= dplane_ctx_neightable_get_family(ctx
);
4596 idx
= dplane_ctx_get_ifindex(ctx
);
4598 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4599 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4600 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4601 req
->ndtm
.ndtm_family
= family
;
4603 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4604 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4605 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4608 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4610 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4611 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4613 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4614 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4617 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4618 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4621 nl_attr_nest_end(&req
->n
, nest
);
4623 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4626 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4627 void *buf
, size_t buflen
)
4631 switch (dplane_ctx_get_op(ctx
)) {
4632 case DPLANE_OP_NEIGH_INSTALL
:
4633 case DPLANE_OP_NEIGH_UPDATE
:
4634 case DPLANE_OP_NEIGH_DISCOVER
:
4635 case DPLANE_OP_NEIGH_IP_INSTALL
:
4636 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4638 case DPLANE_OP_NEIGH_DELETE
:
4639 case DPLANE_OP_NEIGH_IP_DELETE
:
4640 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4642 case DPLANE_OP_VTEP_ADD
:
4643 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4646 case DPLANE_OP_VTEP_DELETE
:
4647 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4650 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4651 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4653 case DPLANE_OP_ROUTE_INSTALL
:
4654 case DPLANE_OP_ROUTE_UPDATE
:
4655 case DPLANE_OP_ROUTE_DELETE
:
4656 case DPLANE_OP_ROUTE_NOTIFY
:
4657 case DPLANE_OP_NH_INSTALL
:
4658 case DPLANE_OP_NH_UPDATE
:
4659 case DPLANE_OP_NH_DELETE
:
4660 case DPLANE_OP_LSP_INSTALL
:
4661 case DPLANE_OP_LSP_UPDATE
:
4662 case DPLANE_OP_LSP_DELETE
:
4663 case DPLANE_OP_LSP_NOTIFY
:
4664 case DPLANE_OP_PW_INSTALL
:
4665 case DPLANE_OP_PW_UNINSTALL
:
4666 case DPLANE_OP_SYS_ROUTE_ADD
:
4667 case DPLANE_OP_SYS_ROUTE_DELETE
:
4668 case DPLANE_OP_ADDR_INSTALL
:
4669 case DPLANE_OP_ADDR_UNINSTALL
:
4670 case DPLANE_OP_MAC_INSTALL
:
4671 case DPLANE_OP_MAC_DELETE
:
4672 case DPLANE_OP_RULE_ADD
:
4673 case DPLANE_OP_RULE_DELETE
:
4674 case DPLANE_OP_RULE_UPDATE
:
4675 case DPLANE_OP_BR_PORT_UPDATE
:
4676 case DPLANE_OP_IPTABLE_ADD
:
4677 case DPLANE_OP_IPTABLE_DELETE
:
4678 case DPLANE_OP_IPSET_ADD
:
4679 case DPLANE_OP_IPSET_DELETE
:
4680 case DPLANE_OP_IPSET_ENTRY_ADD
:
4681 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4682 case DPLANE_OP_GRE_SET
:
4683 case DPLANE_OP_INTF_ADDR_ADD
:
4684 case DPLANE_OP_INTF_ADDR_DEL
:
4685 case DPLANE_OP_INTF_NETCONFIG
:
4686 case DPLANE_OP_INTF_INSTALL
:
4687 case DPLANE_OP_INTF_UPDATE
:
4688 case DPLANE_OP_INTF_DELETE
:
4689 case DPLANE_OP_TC_QDISC_INSTALL
:
4690 case DPLANE_OP_TC_QDISC_UNINSTALL
:
4691 case DPLANE_OP_TC_CLASS_ADD
:
4692 case DPLANE_OP_TC_CLASS_DELETE
:
4693 case DPLANE_OP_TC_CLASS_UPDATE
:
4694 case DPLANE_OP_TC_FILTER_ADD
:
4695 case DPLANE_OP_TC_FILTER_DELETE
:
4696 case DPLANE_OP_TC_FILTER_UPDATE
:
4697 case DPLANE_OP_NONE
:
4705 * Update MAC, using dataplane context object.
4708 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4709 struct zebra_dplane_ctx
*ctx
)
4711 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4715 enum netlink_msg_status
4716 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4718 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4723 * MPLS label forwarding table change via netlink interface, using dataplane
4724 * context information.
4726 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4727 void *buf
, size_t buflen
)
4730 const struct nhlfe_list_head
*head
;
4731 const struct zebra_nhlfe
*nhlfe
;
4732 struct nexthop
*nexthop
= NULL
;
4733 unsigned int nexthop_num
;
4734 const char *routedesc
;
4736 struct prefix p
= {0};
4738 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
4746 if (buflen
< sizeof(*req
))
4749 memset(req
, 0, sizeof(*req
));
4752 * Count # nexthops so we can decide whether to use singlepath
4753 * or multipath case.
4756 head
= dplane_ctx_get_nhlfe_list(ctx
);
4757 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4758 nexthop
= nhlfe
->nexthop
;
4761 if (cmd
== RTM_NEWROUTE
) {
4762 /* Count all selected NHLFEs */
4763 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4764 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4767 /* Count all installed NHLFEs */
4768 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4769 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4774 if ((nexthop_num
== 0) ||
4775 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4778 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4779 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4780 req
->n
.nlmsg_type
= cmd
;
4781 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
4783 req
->r
.rtm_family
= AF_MPLS
;
4784 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4785 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4786 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4787 req
->r
.rtm_type
= RTN_UNICAST
;
4789 if (cmd
== RTM_NEWROUTE
) {
4790 /* We do a replace to handle update. */
4791 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4793 /* set the protocol value if installing */
4794 route_type
= re_type_from_lsp_type(
4795 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4796 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4799 /* Fill destination */
4800 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4801 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4804 /* Fill nexthops (paths) based on single-path or multipath. The paths
4805 * chosen depend on the operation.
4807 if (nexthop_num
== 1) {
4808 routedesc
= "single-path";
4809 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4813 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4814 nexthop
= nhlfe
->nexthop
;
4818 if ((cmd
== RTM_NEWROUTE
4819 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4820 && CHECK_FLAG(nexthop
->flags
,
4821 NEXTHOP_FLAG_ACTIVE
)))
4822 || (cmd
== RTM_DELROUTE
4823 && (CHECK_FLAG(nhlfe
->flags
,
4824 NHLFE_FLAG_INSTALLED
)
4825 && CHECK_FLAG(nexthop
->flags
,
4826 NEXTHOP_FLAG_FIB
)))) {
4827 /* Add the gateway */
4828 if (!_netlink_mpls_build_singlepath(
4829 &p
, routedesc
, nhlfe
, &req
->n
,
4830 &req
->r
, buflen
, cmd
))
4837 } else { /* Multipath case */
4838 struct rtattr
*nest
;
4839 const union g_addr
*src1
= NULL
;
4841 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4845 routedesc
= "multipath";
4846 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4850 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4851 nexthop
= nhlfe
->nexthop
;
4855 if ((cmd
== RTM_NEWROUTE
4856 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4857 && CHECK_FLAG(nexthop
->flags
,
4858 NEXTHOP_FLAG_ACTIVE
)))
4859 || (cmd
== RTM_DELROUTE
4860 && (CHECK_FLAG(nhlfe
->flags
,
4861 NHLFE_FLAG_INSTALLED
)
4862 && CHECK_FLAG(nexthop
->flags
,
4863 NEXTHOP_FLAG_FIB
)))) {
4866 /* Build the multipath */
4867 if (!_netlink_mpls_build_multipath(
4868 &p
, routedesc
, nhlfe
, &req
->n
,
4869 buflen
, &req
->r
, &src1
))
4874 /* Add the multipath */
4875 nl_attr_nest_end(&req
->n
, nest
);
4878 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4881 /****************************************************************************
4882 * This code was developed in a branch that didn't have dplane APIs for
4883 * MAC updates. Hence the use of the legacy style. It will be moved to
4884 * the new dplane style pre-merge to master. XXX
4886 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4893 int cmd
= RTM_NEWNEXTHOP
;
4894 struct zebra_vrf
*zvrf
;
4895 struct zebra_ns
*zns
;
4897 zvrf
= zebra_vrf_get_evpn();
4900 memset(&req
, 0, sizeof(req
));
4902 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4903 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4904 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4905 req
.n
.nlmsg_type
= cmd
;
4906 req
.nhm
.nh_family
= AF_INET
;
4908 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4910 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4912 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4913 &vtep_ip
, IPV4_MAX_BYTELEN
))
4916 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4917 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4918 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4921 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4925 static int netlink_fdb_nh_del(uint32_t nh_id
)
4932 int cmd
= RTM_DELNEXTHOP
;
4933 struct zebra_vrf
*zvrf
;
4934 struct zebra_ns
*zns
;
4936 zvrf
= zebra_vrf_get_evpn();
4939 memset(&req
, 0, sizeof(req
));
4941 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4942 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4943 req
.n
.nlmsg_type
= cmd
;
4944 req
.nhm
.nh_family
= AF_UNSPEC
;
4946 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4949 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4950 zlog_debug("Tx %s fdb-nh 0x%x",
4951 nl_msg_type_to_str(cmd
), nh_id
);
4954 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4958 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4959 struct nh_grp
*nh_ids
)
4966 int cmd
= RTM_NEWNEXTHOP
;
4967 struct zebra_vrf
*zvrf
;
4968 struct zebra_ns
*zns
;
4969 struct nexthop_grp grp
[nh_cnt
];
4972 zvrf
= zebra_vrf_get_evpn();
4975 memset(&req
, 0, sizeof(req
));
4977 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4978 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4979 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4980 req
.n
.nlmsg_type
= cmd
;
4981 req
.nhm
.nh_family
= AF_UNSPEC
;
4983 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4985 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4987 memset(&grp
, 0, sizeof(grp
));
4988 for (i
= 0; i
< nh_cnt
; ++i
) {
4989 grp
[i
].id
= nh_ids
[i
].id
;
4990 grp
[i
].weight
= nh_ids
[i
].weight
;
4992 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4993 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4997 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4998 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
5002 for (i
= 0; i
< nh_cnt
; ++i
) {
5003 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
5005 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
5008 zlog_debug("Tx %s fdb-nhg 0x%x %s",
5009 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
5012 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
5016 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
5018 return netlink_fdb_nh_del(nhg_id
);
5021 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
5023 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
5026 int kernel_del_mac_nh(uint32_t nh_id
)
5028 return netlink_fdb_nh_del(nh_id
);
5031 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
5032 struct nh_grp
*nh_ids
)
5034 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
5037 int kernel_del_mac_nhg(uint32_t nhg_id
)
5039 return netlink_fdb_nhg_del(nhg_id
);
5042 #endif /* HAVE_NETLINK */