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
)
157 * The ipv4_ll data structure is used for all 5549
158 * additions to the kernel. Let's figure out the
159 * correct value one time instead for every
160 * install/remove of a 5549 type route
162 void rt_netlink_init(void)
164 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
168 * Mapping from dataplane neighbor flags to netlink flags
170 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
174 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
175 flags
|= NTF_EXT_LEARNED
;
176 if (dplane_flags
& DPLANE_NTF_ROUTER
)
178 if (dplane_flags
& DPLANE_NTF_USE
)
185 * Mapping from dataplane neighbor state to netlink state
187 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
191 if (dplane_state
& DPLANE_NUD_REACHABLE
)
192 state
|= NUD_REACHABLE
;
193 if (dplane_state
& DPLANE_NUD_STALE
)
195 if (dplane_state
& DPLANE_NUD_NOARP
)
197 if (dplane_state
& DPLANE_NUD_PROBE
)
199 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
200 state
|= NUD_INCOMPLETE
;
201 if (dplane_state
& DPLANE_NUD_PERMANENT
)
202 state
|= NUD_PERMANENT
;
203 if (dplane_state
& DPLANE_NUD_FAILED
)
210 static inline bool is_selfroute(int proto
)
212 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
213 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
214 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
215 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
216 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
217 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
218 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
219 || (proto
== RTPROT_SRTE
)) {
226 static inline int zebra2proto(int proto
)
229 case ZEBRA_ROUTE_BABEL
:
230 proto
= RTPROT_BABEL
;
232 case ZEBRA_ROUTE_BGP
:
235 case ZEBRA_ROUTE_OSPF
:
236 case ZEBRA_ROUTE_OSPF6
:
239 case ZEBRA_ROUTE_STATIC
:
240 proto
= RTPROT_ZSTATIC
;
242 case ZEBRA_ROUTE_ISIS
:
245 case ZEBRA_ROUTE_RIP
:
248 case ZEBRA_ROUTE_RIPNG
:
249 proto
= RTPROT_RIPNG
;
251 case ZEBRA_ROUTE_NHRP
:
254 case ZEBRA_ROUTE_EIGRP
:
255 proto
= RTPROT_EIGRP
;
257 case ZEBRA_ROUTE_LDP
:
260 case ZEBRA_ROUTE_SHARP
:
261 proto
= RTPROT_SHARP
;
263 case ZEBRA_ROUTE_PBR
:
266 case ZEBRA_ROUTE_OPENFABRIC
:
267 proto
= RTPROT_OPENFABRIC
;
269 case ZEBRA_ROUTE_SRTE
:
272 case ZEBRA_ROUTE_TABLE
:
273 case ZEBRA_ROUTE_NHG
:
274 proto
= RTPROT_ZEBRA
;
276 case ZEBRA_ROUTE_CONNECT
:
277 case ZEBRA_ROUTE_KERNEL
:
278 proto
= RTPROT_KERNEL
;
282 * When a user adds a new protocol this will show up
283 * to let them know to do something about it. This
284 * is intentionally a warn because we should see
285 * this as part of development of a new protocol
288 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
290 proto
= RTPROT_ZEBRA
;
297 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
301 proto
= ZEBRA_ROUTE_BABEL
;
304 proto
= ZEBRA_ROUTE_BGP
;
307 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
311 proto
= ZEBRA_ROUTE_ISIS
;
314 proto
= ZEBRA_ROUTE_RIP
;
317 proto
= ZEBRA_ROUTE_RIPNG
;
320 proto
= ZEBRA_ROUTE_NHRP
;
323 proto
= ZEBRA_ROUTE_EIGRP
;
326 proto
= ZEBRA_ROUTE_LDP
;
330 proto
= ZEBRA_ROUTE_STATIC
;
333 proto
= ZEBRA_ROUTE_SHARP
;
336 proto
= ZEBRA_ROUTE_PBR
;
338 case RTPROT_OPENFABRIC
:
339 proto
= ZEBRA_ROUTE_OPENFABRIC
;
342 proto
= ZEBRA_ROUTE_SRTE
;
345 case RTPROT_REDIRECT
:
352 case RTPROT_DNROUTED
:
356 case RTPROT_KEEPALIVED
:
358 proto
= ZEBRA_ROUTE_KERNEL
;
362 proto
= ZEBRA_ROUTE_NHG
;
365 /* Intentional fall thru */
368 * When a user adds a new protocol this will show up
369 * to let them know to do something about it. This
370 * is intentionally a warn because we should see
371 * this as part of development of a new protocol
374 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
376 proto
= ZEBRA_ROUTE_KERNEL
;
383 Pending: create an efficient table_id (in a tree/hash) based lookup)
385 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
388 struct zebra_vrf
*zvrf
;
390 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
394 /* case vrf with netns : match the netnsid */
395 if (vrf_is_backend_netns()) {
396 if (ns_id
== zvrf_id(zvrf
))
397 return zvrf_id(zvrf
);
399 /* VRF is VRF_BACKEND_VRF_LITE */
400 if (zvrf
->table_id
!= table_id
)
402 return zvrf_id(zvrf
);
410 * @parse_encap_mpls() - Parses encapsulated mpls attributes
411 * @tb: Pointer to rtattr to look for nested items in.
412 * @labels: Pointer to store labels in.
414 * Return: Number of mpls labels found.
416 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
418 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
419 mpls_lse_t
*lses
= NULL
;
424 mpls_label_t label
= 0;
426 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
427 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
428 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
429 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
430 labels
[num_labels
++] = label
;
436 static enum seg6local_action_t
437 parse_encap_seg6local(struct rtattr
*tb
,
438 struct seg6local_context
*ctx
)
440 struct rtattr
*tb_encap
[SEG6_LOCAL_MAX
+ 1] = {};
441 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
443 netlink_parse_rtattr_nested(tb_encap
, SEG6_LOCAL_MAX
, tb
);
445 if (tb_encap
[SEG6_LOCAL_ACTION
])
446 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
448 if (tb_encap
[SEG6_LOCAL_NH4
])
449 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
450 tb_encap
[SEG6_LOCAL_NH4
]);
452 if (tb_encap
[SEG6_LOCAL_NH6
])
453 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
454 tb_encap
[SEG6_LOCAL_NH6
]);
456 if (tb_encap
[SEG6_LOCAL_TABLE
])
457 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
459 if (tb_encap
[SEG6_LOCAL_VRFTABLE
])
461 *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_VRFTABLE
]);
466 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
468 struct rtattr
*tb_encap
[SEG6_IPTUNNEL_MAX
+ 1] = {};
469 struct seg6_iptunnel_encap
*ipt
= NULL
;
470 struct in6_addr
*segments
= NULL
;
472 netlink_parse_rtattr_nested(tb_encap
, SEG6_IPTUNNEL_MAX
, tb
);
475 * TODO: It's not support multiple SID list.
477 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
478 ipt
= (struct seg6_iptunnel_encap
*)
479 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
480 segments
= ipt
->srh
[0].segments
;
489 static struct nexthop
490 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
491 enum blackhole_type bh_type
, int index
, void *prefsrc
,
492 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
494 struct interface
*ifp
= NULL
;
495 struct nexthop nh
= {0};
496 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
498 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
499 struct seg6local_context seg6l_ctx
= {};
500 struct in6_addr seg6_segs
= {};
503 vrf_id_t nh_vrf_id
= vrf_id
;
504 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
506 if (bh_type
== BLACKHOLE_UNSPEC
) {
508 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
509 else if (index
&& gate
)
510 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
511 : NEXTHOP_TYPE_IPV6_IFINDEX
;
512 else if (!index
&& gate
)
513 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
516 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
517 nh
.bh_type
= bh_type
;
520 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
521 nh
.bh_type
= bh_type
;
525 memcpy(&nh
.src
, prefsrc
, sz
);
527 memcpy(&nh
.gate
, gate
, sz
);
530 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
532 nh_vrf_id
= ifp
->vrf
->vrf_id
;
534 nh
.vrf_id
= nh_vrf_id
;
536 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
537 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
538 == LWTUNNEL_ENCAP_MPLS
) {
539 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
541 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
542 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
543 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
544 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
546 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
547 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
548 == LWTUNNEL_ENCAP_SEG6
) {
549 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
552 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
553 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
555 if (rtm
->rtm_flags
& RTNH_F_LINKDOWN
)
556 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_LINKDOWN
);
559 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
561 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
562 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
565 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
570 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
571 struct nexthop_group
*ng
,
573 struct rtnexthop
*rtnh
,
575 void *prefsrc
, vrf_id_t vrf_id
)
578 struct interface
*ifp
= NULL
;
581 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
583 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
584 struct seg6local_context seg6l_ctx
= {};
585 struct in6_addr seg6_segs
= {};
587 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
589 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
590 vrf_id_t nh_vrf_id
= vrf_id
;
593 struct nexthop
*nh
= NULL
;
595 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
598 index
= rtnh
->rtnh_ifindex
;
601 * Yes we are looking this up
602 * for every nexthop and just
603 * using the last one looked
606 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
609 nh_vrf_id
= ifp
->vrf
->vrf_id
;
612 EC_ZEBRA_UNKNOWN_INTERFACE
,
613 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
615 nh_vrf_id
= VRF_DEFAULT
;
620 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
621 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
622 rtnh
->rtnh_len
- sizeof(*rtnh
));
623 if (rtnh_tb
[RTA_GATEWAY
])
624 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
625 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
626 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
627 == LWTUNNEL_ENCAP_MPLS
) {
628 num_labels
= parse_encap_mpls(
629 rtnh_tb
[RTA_ENCAP
], labels
);
631 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
632 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
633 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
634 seg6l_act
= parse_encap_seg6local(
635 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
637 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
638 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
639 == LWTUNNEL_ENCAP_SEG6
) {
640 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
645 if (gate
&& rtm
->rtm_family
== AF_INET
) {
647 nh
= nexthop_from_ipv4_ifindex(
648 gate
, prefsrc
, index
, nh_vrf_id
);
650 nh
= nexthop_from_ipv4(gate
, prefsrc
,
652 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
654 nh
= nexthop_from_ipv6_ifindex(
655 gate
, index
, nh_vrf_id
);
657 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
659 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
662 nh
->weight
= rtnh
->rtnh_hops
+ 1;
665 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
668 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
669 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
673 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
675 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
676 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
678 /* Add to temporary list */
679 nexthop_group_add_sorted(ng
, nh
);
682 if (rtnh
->rtnh_len
== 0)
685 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
686 rtnh
= RTNH_NEXT(rtnh
);
689 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
694 /* Looking up routing table by netlink interface. */
695 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
700 struct rtattr
*tb
[RTA_MAX
+ 1];
703 struct prefix_ipv6 src_p
= {};
707 char anyaddr
[16] = {0};
709 int proto
= ZEBRA_ROUTE_KERNEL
;
714 uint8_t distance
= 0;
720 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
721 void *src
= NULL
; /* IPv6 srcdest source prefix */
722 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
724 frrtrace(3, frr_zebra
, netlink_route_change_read_unicast
, h
, ns_id
,
729 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
731 switch (rtm
->rtm_type
) {
735 bh_type
= BLACKHOLE_NULL
;
737 case RTN_UNREACHABLE
:
738 bh_type
= BLACKHOLE_REJECT
;
741 bh_type
= BLACKHOLE_ADMINPROHIB
;
744 if (IS_ZEBRA_DEBUG_KERNEL
)
745 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
746 nl_rttype_to_str(rtm
->rtm_type
),
751 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
754 "%s: Message received from netlink is of a broken size %d %zu",
755 __func__
, h
->nlmsg_len
,
756 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
760 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
762 if (rtm
->rtm_flags
& RTM_F_CLONED
)
764 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
766 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
769 selfroute
= is_selfroute(rtm
->rtm_protocol
);
771 if (!startup
&& selfroute
772 && h
->nlmsg_type
== RTM_NEWROUTE
773 && !zrouter
.asic_offloaded
) {
774 if (IS_ZEBRA_DEBUG_KERNEL
)
775 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
780 /* We don't care about change notifications for the MPLS table. */
781 /* TODO: Revisit this. */
782 if (rtm
->rtm_family
== AF_MPLS
)
785 /* Table corresponding to route. */
787 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
789 table
= rtm
->rtm_table
;
792 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
793 if (vrf_id
== VRF_DEFAULT
) {
794 if (!is_zebra_valid_kernel_table(table
)
795 && !is_zebra_main_routing_table(table
))
799 if (rtm
->rtm_flags
& RTM_F_TRAP
)
800 flags
|= ZEBRA_FLAG_TRAPPED
;
801 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
802 flags
|= ZEBRA_FLAG_OFFLOADED
;
803 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
804 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
806 if (h
->nlmsg_flags
& NLM_F_APPEND
)
807 flags
|= ZEBRA_FLAG_OUTOFSYNC
;
809 /* Route which inserted by Zebra. */
811 flags
|= ZEBRA_FLAG_SELFROUTE
;
812 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
815 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
818 dest
= RTA_DATA(tb
[RTA_DST
]);
823 src
= RTA_DATA(tb
[RTA_SRC
]);
828 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
831 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
834 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
836 if (tb
[RTA_PRIORITY
])
837 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
839 #if defined(SUPPORT_REALMS)
841 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
844 if (tb
[RTA_METRICS
]) {
845 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
847 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
848 RTA_PAYLOAD(tb
[RTA_METRICS
]));
851 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
854 if (rtm
->rtm_family
== AF_INET
) {
856 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
858 "Invalid destination prefix length: %u received from kernel route change",
862 memcpy(&p
.u
.prefix4
, dest
, 4);
863 p
.prefixlen
= rtm
->rtm_dst_len
;
865 if (rtm
->rtm_src_len
!= 0) {
867 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
868 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
873 /* Force debug below to not display anything for source */
875 } else if (rtm
->rtm_family
== AF_INET6
) {
877 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
879 "Invalid destination prefix length: %u received from kernel route change",
883 memcpy(&p
.u
.prefix6
, dest
, 16);
884 p
.prefixlen
= rtm
->rtm_dst_len
;
886 src_p
.family
= AF_INET6
;
887 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
889 "Invalid source prefix length: %u received from kernel route change",
893 memcpy(&src_p
.prefix
, src
, 16);
894 src_p
.prefixlen
= rtm
->rtm_src_len
;
896 /* We only handle the AFs we handle... */
897 if (IS_ZEBRA_DEBUG_KERNEL
)
898 zlog_debug("%s: unknown address-family %u", __func__
,
904 * For ZEBRA_ROUTE_KERNEL types:
906 * The metric/priority of the route received from the kernel
907 * is a 32 bit number. We are going to interpret the high
908 * order byte as the Admin Distance and the low order 3 bytes
911 * This will allow us to do two things:
912 * 1) Allow the creation of kernel routes that can be
913 * overridden by zebra.
914 * 2) Allow the old behavior for 'most' kernel route types
915 * if a user enters 'ip route ...' v4 routes get a metric
916 * of 0 and v6 routes get a metric of 1024. Both of these
917 * values will end up with a admin distance of 0, which
918 * will cause them to win for the purposes of zebra.
920 if (proto
== ZEBRA_ROUTE_KERNEL
) {
921 distance
= (metric
>> 24) & 0xFF;
922 metric
= (metric
& 0x00FFFFFF);
925 if (IS_ZEBRA_DEBUG_KERNEL
) {
926 char buf2
[PREFIX_STRLEN
];
929 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
930 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
931 src_p
.prefixlen
? " from " : "",
932 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
934 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
939 if (rtm
->rtm_family
== AF_INET6
)
942 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
943 struct route_entry
*re
;
944 struct nexthop_group
*ng
= NULL
;
946 re
= zebra_rib_route_entry_new(vrf_id
, proto
, 0, flags
, nhe_id
,
947 table
, metric
, mtu
, distance
,
950 ng
= nexthop_group_new();
952 if (!tb
[RTA_MULTIPATH
]) {
953 struct nexthop
*nexthop
, nh
;
956 nh
= parse_nexthop_unicast(
957 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
960 nexthop
= nexthop_new();
962 nexthop_group_add_sorted(ng
, nexthop
);
965 /* This is a multipath route */
966 struct rtnexthop
*rtnh
=
967 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
972 /* Use temporary list of nexthops; parse
973 * message payload's nexthops.
976 parse_multipath_nexthops_unicast(
977 ns_id
, ng
, rtm
, rtnh
, tb
,
980 zserv_nexthop_num_warn(
981 __func__
, (const struct prefix
*)&p
,
985 nexthop_group_delete(&ng
);
991 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, &src_p
, re
, ng
,
995 * I really don't see how this is possible
996 * but since we are testing for it let's
997 * let the end user know why the route
998 * that was just received was swallowed
1002 "%s: %pFX multipath RTM_NEWROUTE has a invalid nexthop group from the kernel",
1004 XFREE(MTYPE_RE
, re
);
1008 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
1009 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
1012 if (!tb
[RTA_MULTIPATH
]) {
1015 nh
= parse_nexthop_unicast(
1016 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
1018 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
1019 flags
, &p
, &src_p
, &nh
, 0, table
,
1020 metric
, distance
, true);
1022 /* XXX: need to compare the entire list of
1023 * nexthops here for NLM_F_APPEND stupidity */
1024 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
1025 flags
, &p
, &src_p
, NULL
, 0, table
,
1026 metric
, distance
, true);
1034 static struct mcast_route_data
*mroute
= NULL
;
1036 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1037 ns_id_t ns_id
, int startup
)
1041 struct rtattr
*tb
[RTA_MAX
+ 1];
1042 struct mcast_route_data
*m
;
1047 char oif_list
[256] = "\0";
1054 rtm
= NLMSG_DATA(h
);
1056 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1058 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1061 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1063 table
= rtm
->rtm_table
;
1065 vrf
= vrf_lookup_by_table(table
, ns_id
);
1068 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1071 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1073 *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1076 *(struct in6_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1080 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1082 *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1085 *(struct in6_addr
*)RTA_DATA(tb
[RTA_DST
]);
1088 if (tb
[RTA_EXPIRES
])
1089 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1091 if (tb
[RTA_MULTIPATH
]) {
1092 struct rtnexthop
*rtnh
=
1093 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1095 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1097 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1100 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1103 if (rtnh
->rtnh_len
== 0)
1106 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1107 rtnh
= RTNH_NEXT(rtnh
);
1111 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
) {
1112 SET_IPADDR_V4(&m
->src
);
1113 SET_IPADDR_V4(&m
->grp
);
1114 } else if (rtm
->rtm_family
== RTNL_FAMILY_IP6MR
) {
1115 SET_IPADDR_V6(&m
->src
);
1116 SET_IPADDR_V6(&m
->grp
);
1118 zlog_warn("%s: Invalid rtm_family received", __func__
);
1122 if (IS_ZEBRA_DEBUG_KERNEL
) {
1123 struct interface
*ifp
= NULL
;
1124 struct zebra_vrf
*zvrf
= NULL
;
1126 for (count
= 0; count
< oif_count
; count
++) {
1127 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1130 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1131 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1132 strlcat(oif_list
, temp
, sizeof(oif_list
));
1134 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1135 ifp
= if_lookup_by_index(iif
, vrf
);
1137 "MCAST VRF: %s(%d) %s (%pIA,%pIA) IIF: %s(%d) OIF: %s jiffies: %lld",
1138 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1139 &m
->src
, &m
->grp
, ifp
? ifp
->name
: "Unknown", iif
,
1140 oif_list
, m
->lastused
);
1145 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1150 rtm
= NLMSG_DATA(h
);
1152 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1153 /* If this is not route add/delete message print warning. */
1154 zlog_debug("Kernel message: %s NS %u",
1155 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1159 switch (rtm
->rtm_family
) {
1164 case RTNL_FAMILY_IPMR
:
1165 case RTNL_FAMILY_IP6MR
:
1166 /* notifications on IPMR are irrelevant to zebra, we only care
1167 * about responses to RTM_GETROUTE requests we sent.
1173 EC_ZEBRA_UNKNOWN_FAMILY
,
1174 "Invalid address family: %u received from kernel route change: %s",
1175 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1179 /* Connected route. */
1180 if (IS_ZEBRA_DEBUG_KERNEL
)
1181 zlog_debug("%s %s %s proto %s NS %u",
1182 nl_msg_type_to_str(h
->nlmsg_type
),
1183 nl_family_to_str(rtm
->rtm_family
),
1184 nl_rttype_to_str(rtm
->rtm_type
),
1185 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1188 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1191 "%s: Message received from netlink is of a broken size: %d %zu",
1192 __func__
, h
->nlmsg_len
,
1193 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1197 /* these are "magic" kernel-managed *unicast* routes used for
1198 * outputting locally generated multicast traffic (which uses unicast
1199 * handling on Linux because ~reasons~.
1201 if (rtm
->rtm_type
== RTN_MULTICAST
)
1204 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1208 /* Request for specific route information from the kernel */
1209 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1216 /* Form the request, specifying filter (rtattr) if needed. */
1217 memset(&req
, 0, sizeof(req
));
1218 req
.n
.nlmsg_type
= type
;
1219 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1220 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1221 req
.rtm
.rtm_family
= family
;
1223 return netlink_request(&zns
->netlink_cmd
, &req
);
1226 /* Routing table read function using netlink interface. Only called
1228 int netlink_route_read(struct zebra_ns
*zns
)
1231 struct zebra_dplane_info dp_info
;
1233 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1235 /* Get IPv4 routing table. */
1236 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1239 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1240 &zns
->netlink_cmd
, &dp_info
, 0, true);
1244 /* Get IPv6 routing table. */
1245 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1248 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1249 &zns
->netlink_cmd
, &dp_info
, 0, true);
1257 * The function returns true if the gateway info could be added
1258 * to the message, otherwise false is returned.
1260 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1262 struct nlmsghdr
*nlmsg
,
1263 size_t req_size
, int bytelen
,
1264 const struct nexthop
*nexthop
)
1266 if (route_family
== AF_MPLS
) {
1267 struct gw_family_t gw_fam
;
1269 gw_fam
.family
= gw_family
;
1270 if (gw_family
== AF_INET
)
1271 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1273 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1274 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1278 if (!(nexthop
->rparent
1279 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1280 if (gw_family
== AF_INET
) {
1281 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1282 &nexthop
->gate
.ipv4
, bytelen
))
1285 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1286 &nexthop
->gate
.ipv6
, bytelen
))
1295 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1296 mpls_lse_t
*out_lse
, char *label_buf
,
1297 size_t label_buf_size
)
1299 char label_buf1
[20];
1302 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1303 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1306 if (IS_ZEBRA_DEBUG_KERNEL
) {
1308 snprintf(label_buf
, label_buf_size
, "label %u",
1309 nh_label
->label
[i
]);
1311 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1312 nh_label
->label
[i
]);
1313 strlcat(label_buf
, label_buf1
, label_buf_size
);
1317 out_lse
[num_labels
] =
1318 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1325 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1326 struct nlmsghdr
*nlmsg
,
1327 size_t buflen
, struct rtmsg
*rtmsg
,
1329 size_t label_buf_size
)
1331 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1335 * label_buf is *only* currently used within debugging.
1336 * As such when we assign it we are guarding it inside
1337 * a debug test. If you want to change this make sure
1338 * you fix this assumption
1340 label_buf
[0] = '\0';
1343 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1346 /* Set the BoS bit */
1347 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1349 if (rtmsg
->rtm_family
== AF_MPLS
) {
1350 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1351 num_labels
* sizeof(mpls_lse_t
)))
1354 struct rtattr
*nest
;
1356 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1357 LWTUNNEL_ENCAP_MPLS
))
1360 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1364 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1366 num_labels
* sizeof(mpls_lse_t
)))
1368 nl_attr_nest_end(nlmsg
, nest
);
1375 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1377 struct nlmsghdr
*nlmsg
,
1378 size_t buflen
, int bytelen
)
1380 if (family
== AF_INET
) {
1381 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1382 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1383 &nexthop
->rmap_src
.ipv4
, bytelen
))
1385 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1386 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1387 &nexthop
->src
.ipv4
, bytelen
))
1390 } else if (family
== AF_INET6
) {
1391 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1392 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1393 &nexthop
->rmap_src
.ipv6
, bytelen
))
1395 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1396 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1397 &nexthop
->src
.ipv6
, bytelen
))
1405 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1406 const struct in6_addr
*seg
)
1408 struct seg6_iptunnel_encap
*ipt
;
1409 struct ipv6_sr_hdr
*srh
;
1410 const size_t srhlen
= 24;
1413 * Caution: Support only SINGLE-SID, not MULTI-SID
1414 * This function only supports the case where segs represents
1415 * a single SID. If you want to extend the SRv6 functionality,
1416 * you should improve the Boundary Check.
1417 * Ex. In case of set a SID-List include multiple-SIDs as an
1418 * argument of the Transit Behavior, we must support variable
1419 * boundary check for buflen.
1421 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1422 sizeof(struct ipv6_sr_hdr
) + 16))
1425 memset(buffer
, 0, buflen
);
1427 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1428 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1430 srh
->hdrlen
= (srhlen
>> 3) - 1;
1432 srh
->segments_left
= 0;
1433 srh
->first_segment
= 0;
1434 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1439 /* This function takes a nexthop as argument and adds
1440 * the appropriate netlink attributes to an existing
1443 * @param routedesc: Human readable description of route type
1444 * (direct/recursive, single-/multipath)
1445 * @param bytelen: Length of addresses in bytes.
1446 * @param nexthop: Nexthop information
1447 * @param nlmsg: nlmsghdr structure to fill in.
1448 * @param req_size: The size allocated for the message.
1450 * The function returns true if the nexthop could be added
1451 * to the message, otherwise false is returned.
1453 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1454 const char *routedesc
, int bytelen
,
1455 const struct nexthop
*nexthop
,
1456 struct nlmsghdr
*nlmsg
,
1457 struct rtmsg
*rtmsg
,
1458 size_t req_size
, int cmd
)
1461 char label_buf
[256];
1463 char addrstr
[INET6_ADDRSTRLEN
];
1467 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1469 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1470 req_size
, rtmsg
, label_buf
,
1474 if (nexthop
->nh_srv6
) {
1475 if (nexthop
->nh_srv6
->seg6local_action
!=
1476 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1477 struct rtattr
*nest
;
1478 const struct seg6local_context
*ctx
;
1480 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1481 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1482 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1485 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1489 switch (nexthop
->nh_srv6
->seg6local_action
) {
1490 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1491 if (!nl_attr_put32(nlmsg
, req_size
,
1493 SEG6_LOCAL_ACTION_END
))
1496 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1497 if (!nl_attr_put32(nlmsg
, req_size
,
1499 SEG6_LOCAL_ACTION_END_X
))
1501 if (!nl_attr_put(nlmsg
, req_size
,
1502 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1503 sizeof(struct in6_addr
)))
1506 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1507 if (!nl_attr_put32(nlmsg
, req_size
,
1509 SEG6_LOCAL_ACTION_END_T
))
1511 if (!nl_attr_put32(nlmsg
, req_size
,
1516 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1517 if (!nl_attr_put32(nlmsg
, req_size
,
1519 SEG6_LOCAL_ACTION_END_DX4
))
1521 if (!nl_attr_put(nlmsg
, req_size
,
1522 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1523 sizeof(struct in_addr
)))
1526 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1527 if (!nl_attr_put32(nlmsg
, req_size
,
1529 SEG6_LOCAL_ACTION_END_DT6
))
1531 if (!nl_attr_put32(nlmsg
, req_size
,
1536 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
1537 if (!nl_attr_put32(nlmsg
, req_size
,
1539 SEG6_LOCAL_ACTION_END_DT4
))
1541 if (!nl_attr_put32(nlmsg
, req_size
,
1542 SEG6_LOCAL_VRFTABLE
,
1546 case ZEBRA_SEG6_LOCAL_ACTION_END_DT46
:
1547 if (!nl_attr_put32(nlmsg
, req_size
,
1549 SEG6_LOCAL_ACTION_END_DT46
))
1551 if (!nl_attr_put32(nlmsg
, req_size
,
1552 SEG6_LOCAL_VRFTABLE
,
1557 zlog_err("%s: unsupport seg6local behaviour action=%u",
1559 nexthop
->nh_srv6
->seg6local_action
);
1562 nl_attr_nest_end(nlmsg
, nest
);
1565 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1568 struct rtattr
*nest
;
1570 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1571 LWTUNNEL_ENCAP_SEG6
))
1573 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1576 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1577 &nexthop
->nh_srv6
->seg6_segs
);
1580 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1583 nl_attr_nest_end(nlmsg
, nest
);
1587 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1588 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1590 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1591 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1592 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1594 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1597 if (cmd
== RTM_NEWROUTE
) {
1598 if (!_netlink_route_encode_nexthop_src(
1599 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1603 if (IS_ZEBRA_DEBUG_KERNEL
)
1604 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1605 __func__
, routedesc
, p
, ipv4_ll_buf
,
1606 label_buf
, nexthop
->ifindex
,
1607 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1611 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1612 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1613 /* Send deletes to the kernel without specifying the next-hop */
1614 if (cmd
!= RTM_DELROUTE
) {
1615 if (!_netlink_route_add_gateway_info(
1616 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1621 if (cmd
== RTM_NEWROUTE
) {
1622 if (!_netlink_route_encode_nexthop_src(
1623 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1627 if (IS_ZEBRA_DEBUG_KERNEL
) {
1628 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1630 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1631 __func__
, routedesc
, p
, addrstr
, label_buf
,
1632 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1637 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1638 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1639 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1640 AF_INET6
, nlmsg
, req_size
,
1644 if (cmd
== RTM_NEWROUTE
) {
1645 if (!_netlink_route_encode_nexthop_src(
1646 nexthop
, AF_INET6
, nlmsg
, req_size
,
1651 if (IS_ZEBRA_DEBUG_KERNEL
) {
1652 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1654 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1655 __func__
, routedesc
, p
, addrstr
, label_buf
,
1656 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1662 * We have the ifindex so we should always send it
1663 * This is especially useful if we are doing route
1666 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1667 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1671 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1672 if (cmd
== RTM_NEWROUTE
) {
1673 if (!_netlink_route_encode_nexthop_src(
1674 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1678 if (IS_ZEBRA_DEBUG_KERNEL
)
1679 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1680 __func__
, routedesc
, p
, nexthop
->ifindex
,
1681 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1687 /* This function appends tag value as rtnl flow attribute
1688 * to the given netlink msg only if value is less than 256.
1689 * Used only if SUPPORT_REALMS enabled.
1691 * @param nlmsg: nlmsghdr structure to fill in.
1692 * @param maxlen: The size allocated for the message.
1693 * @param tag: The route tag.
1695 * The function returns true if the flow attribute could
1696 * be added to the message, otherwise false is returned.
1698 static inline bool _netlink_set_tag(struct nlmsghdr
*n
, unsigned int maxlen
,
1701 if (tag
> 0 && tag
<= 255) {
1702 if (!nl_attr_put32(n
, maxlen
, RTA_FLOW
, tag
))
1708 /* This function takes a nexthop as argument and
1709 * appends to the given netlink msg. If the nexthop
1710 * defines a preferred source, the src parameter
1711 * will be modified to point to that src, otherwise
1712 * it will be kept unmodified.
1714 * @param routedesc: Human readable description of route type
1715 * (direct/recursive, single-/multipath)
1716 * @param bytelen: Length of addresses in bytes.
1717 * @param nexthop: Nexthop information
1718 * @param nlmsg: nlmsghdr structure to fill in.
1719 * @param req_size: The size allocated for the message.
1720 * @param src: pointer pointing to a location where
1721 * the prefsrc should be stored.
1723 * The function returns true if the nexthop could be added
1724 * to the message, otherwise false is returned.
1726 static bool _netlink_route_build_multipath(
1727 const struct prefix
*p
, const char *routedesc
, int bytelen
,
1728 const struct nexthop
*nexthop
, struct nlmsghdr
*nlmsg
, size_t req_size
,
1729 struct rtmsg
*rtmsg
, const union g_addr
**src
, route_tag_t tag
)
1731 char label_buf
[256];
1733 struct rtnexthop
*rtnh
;
1735 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1741 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1743 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1744 req_size
, rtmsg
, label_buf
,
1748 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1749 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1751 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1752 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1753 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1755 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1756 if (nexthop
->weight
)
1757 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1759 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1760 *src
= &nexthop
->rmap_src
;
1761 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1762 *src
= &nexthop
->src
;
1764 if (IS_ZEBRA_DEBUG_KERNEL
)
1766 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1767 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1768 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1770 nl_attr_rtnh_end(nlmsg
, rtnh
);
1774 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1775 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1776 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1777 nlmsg
, req_size
, bytelen
,
1781 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1782 *src
= &nexthop
->rmap_src
;
1783 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1784 *src
= &nexthop
->src
;
1786 if (IS_ZEBRA_DEBUG_KERNEL
)
1787 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1788 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1789 label_buf
, nexthop
->ifindex
,
1790 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1792 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1793 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1794 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1795 AF_INET6
, nlmsg
, req_size
,
1799 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1800 *src
= &nexthop
->rmap_src
;
1801 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1802 *src
= &nexthop
->src
;
1804 if (IS_ZEBRA_DEBUG_KERNEL
)
1805 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1806 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1807 label_buf
, nexthop
->ifindex
,
1808 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1812 * We have figured out the ifindex so we should always send it
1813 * This is especially useful if we are doing route
1816 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1817 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1820 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1821 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1822 *src
= &nexthop
->rmap_src
;
1823 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1824 *src
= &nexthop
->src
;
1826 if (IS_ZEBRA_DEBUG_KERNEL
)
1827 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1828 __func__
, routedesc
, p
, nexthop
->ifindex
,
1829 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1832 if (nexthop
->weight
)
1833 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1835 if (!_netlink_set_tag(nlmsg
, req_size
, tag
))
1838 nl_attr_rtnh_end(nlmsg
, rtnh
);
1843 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1844 const struct zebra_nhlfe
*nhlfe
,
1845 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1846 size_t req_size
, int cmd
)
1851 family
= NHLFE_FAMILY(nhlfe
);
1852 bytelen
= (family
== AF_INET
? 4 : 16);
1853 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1854 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1860 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1861 const struct zebra_nhlfe
*nhlfe
,
1862 struct nlmsghdr
*nlmsg
, size_t req_size
,
1863 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1868 family
= NHLFE_FAMILY(nhlfe
);
1869 bytelen
= (family
== AF_INET
? 4 : 16);
1870 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1871 nhlfe
->nexthop
, nlmsg
, req_size
,
1875 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1877 if (IS_ZEBRA_DEBUG_KERNEL
)
1878 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1879 routedesc
, nl_msg_type_to_str(cmd
), label
);
1882 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1883 int llalen
, ns_id_t ns_id
, uint8_t family
,
1884 bool permanent
, uint8_t protocol
)
1892 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1894 memset(&req
, 0, sizeof(req
));
1896 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1897 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1898 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1899 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1901 req
.ndm
.ndm_family
= family
;
1902 req
.ndm
.ndm_ifindex
= ifindex
;
1903 req
.ndm
.ndm_type
= RTN_UNICAST
;
1904 if (cmd
== RTM_NEWNEIGH
) {
1906 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1908 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1910 req
.ndm
.ndm_state
= NUD_FAILED
;
1912 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1914 req
.ndm
.ndm_type
= RTN_UNICAST
;
1915 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
1916 family2addrsize(family
));
1918 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1920 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1924 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1927 if (family
== AF_INET
) {
1928 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1929 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1931 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1932 src
->ipv4
= nexthop
->src
.ipv4
;
1935 } else if (family
== AF_INET6
) {
1936 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1937 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1939 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1940 src
->ipv6
= nexthop
->src
.ipv6
;
1949 * The function returns true if the attribute could be added
1950 * to the message, otherwise false is returned.
1952 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1955 struct rtattr
*nest
;
1957 switch (nh
->nh_encap_type
) {
1959 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1962 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1966 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1969 nl_attr_nest_end(n
, nest
);
1977 * Routing table change via netlink interface, using a dataplane context object
1979 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1980 * otherwise the number of bytes written to buf.
1982 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1983 struct zebra_dplane_ctx
*ctx
,
1984 uint8_t *data
, size_t datalen
,
1985 bool fpm
, bool force_nhg
)
1988 struct nexthop
*nexthop
= NULL
;
1989 unsigned int nexthop_num
;
1990 const char *routedesc
;
1991 bool setsrc
= false;
1993 const struct prefix
*p
, *src_p
;
1996 route_tag_t tag
= 0;
2002 } *req
= (void *)data
;
2004 p
= dplane_ctx_get_dest(ctx
);
2005 src_p
= dplane_ctx_get_src(ctx
);
2007 if (datalen
< sizeof(*req
))
2010 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2012 memset(req
, 0, sizeof(*req
));
2014 bytelen
= (p
->family
== AF_INET
? 4 : 16);
2016 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2017 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2019 if ((cmd
== RTM_NEWROUTE
) &&
2020 ((p
->family
== AF_INET
) || v6_rr_semantics
))
2021 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2023 req
->n
.nlmsg_type
= cmd
;
2025 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2027 req
->r
.rtm_family
= p
->family
;
2028 req
->r
.rtm_dst_len
= p
->prefixlen
;
2029 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
2030 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2032 if (cmd
== RTM_DELROUTE
)
2033 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
2035 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
2038 * blackhole routes are not RTN_UNICAST, they are
2039 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
2040 * so setting this value as a RTN_UNICAST would
2041 * cause the route lookup of just the prefix
2042 * to fail. So no need to specify this for
2043 * the RTM_DELROUTE case
2045 if (cmd
!= RTM_DELROUTE
)
2046 req
->r
.rtm_type
= RTN_UNICAST
;
2048 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
2051 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
2057 /* Hardcode the metric for all routes coming from zebra. Metric isn't
2059 * either by the kernel or by zebra. Its purely for calculating best
2061 * by the routing protocol and for communicating with protocol peers.
2063 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
2064 NL_DEFAULT_ROUTE_METRIC
))
2067 #if defined(SUPPORT_REALMS)
2068 if (cmd
== RTM_DELROUTE
)
2069 tag
= dplane_ctx_get_old_tag(ctx
);
2071 tag
= dplane_ctx_get_tag(ctx
);
2074 /* Table corresponding to this route. */
2075 table_id
= dplane_ctx_get_table(ctx
);
2077 req
->r
.rtm_table
= table_id
;
2079 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
2080 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
2084 if (IS_ZEBRA_DEBUG_KERNEL
)
2086 "%s: %s %pFX vrf %u(%u)", __func__
,
2087 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
2091 * If we are not updating the route and we have received
2092 * a route delete, then all we need to fill in is the
2093 * prefix information to tell the kernel to schwack
2096 if (cmd
== RTM_DELROUTE
) {
2097 if (!_netlink_set_tag(&req
->n
, datalen
, tag
))
2099 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2102 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
2103 struct rtattr
*nest
;
2104 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
2105 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2107 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2110 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2114 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2116 nl_attr_nest_end(&req
->n
, nest
);
2120 * Always install blackhole routes without using nexthops, because of
2121 * the following kernel problems:
2122 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2123 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2125 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2127 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2128 nexthop
= nexthop
->resolved
;
2130 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2131 switch (nexthop
->bh_type
) {
2132 case BLACKHOLE_ADMINPROHIB
:
2133 req
->r
.rtm_type
= RTN_PROHIBIT
;
2135 case BLACKHOLE_REJECT
:
2136 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2139 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2142 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2146 if ((!fpm
&& kernel_nexthops_supported()
2147 && (!proto_nexthops_only()
2148 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2149 || (fpm
&& force_nhg
)) {
2150 /* Kernel supports nexthop objects */
2151 if (IS_ZEBRA_DEBUG_KERNEL
)
2152 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2153 dplane_ctx_get_nhe_id(ctx
));
2155 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2156 dplane_ctx_get_nhe_id(ctx
)))
2159 /* Have to determine src still */
2160 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2164 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2168 if (p
->family
== AF_INET
) {
2169 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2170 &src
.ipv4
, bytelen
))
2172 } else if (p
->family
== AF_INET6
) {
2173 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2174 &src
.ipv6
, bytelen
))
2179 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2182 /* Count overall nexthops so we can decide whether to use singlepath
2183 * or multipath case.
2186 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2187 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2189 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2195 /* Singlepath case. */
2196 if (nexthop_num
== 1) {
2198 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2199 if (CHECK_FLAG(nexthop
->flags
,
2200 NEXTHOP_FLAG_RECURSIVE
)) {
2205 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2210 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2211 routedesc
= nexthop
->rparent
2212 ? "recursive, single-path"
2215 if (!_netlink_set_tag(&req
->n
, datalen
, tag
))
2218 if (!_netlink_route_build_singlepath(
2219 p
, routedesc
, bytelen
, nexthop
,
2220 &req
->n
, &req
->r
, datalen
, cmd
))
2227 * Add encapsulation information when installing via
2231 if (!netlink_route_nexthop_encap(
2232 &req
->n
, datalen
, nexthop
))
2238 if (p
->family
== AF_INET
) {
2239 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2240 &src
.ipv4
, bytelen
))
2242 } else if (p
->family
== AF_INET6
) {
2243 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2244 &src
.ipv6
, bytelen
))
2248 } else { /* Multipath case */
2249 struct rtattr
*nest
;
2250 const union g_addr
*src1
= NULL
;
2252 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2257 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2258 if (CHECK_FLAG(nexthop
->flags
,
2259 NEXTHOP_FLAG_RECURSIVE
)) {
2260 /* This only works for IPv4 now */
2264 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2269 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2270 routedesc
= nexthop
->rparent
2271 ? "recursive, multipath"
2275 if (!_netlink_route_build_multipath(
2276 p
, routedesc
, bytelen
, nexthop
,
2277 &req
->n
, datalen
, &req
->r
, &src1
,
2281 if (!setsrc
&& src1
) {
2282 if (p
->family
== AF_INET
)
2283 src
.ipv4
= src1
->ipv4
;
2284 else if (p
->family
== AF_INET6
)
2285 src
.ipv6
= src1
->ipv6
;
2292 nl_attr_nest_end(&req
->n
, nest
);
2295 * Add encapsulation information when installing via
2299 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2301 if (CHECK_FLAG(nexthop
->flags
,
2302 NEXTHOP_FLAG_RECURSIVE
))
2304 if (!netlink_route_nexthop_encap(
2305 &req
->n
, datalen
, nexthop
))
2312 if (p
->family
== AF_INET
) {
2313 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2314 &src
.ipv4
, bytelen
))
2316 } else if (p
->family
== AF_INET6
) {
2317 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2318 &src
.ipv6
, bytelen
))
2321 if (IS_ZEBRA_DEBUG_KERNEL
)
2322 zlog_debug("Setting source");
2326 /* If there is no useful nexthop then return. */
2327 if (nexthop_num
== 0) {
2328 if (IS_ZEBRA_DEBUG_KERNEL
)
2329 zlog_debug("%s: No useful nexthop.", __func__
);
2332 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2335 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2337 uint32_t actual_table
;
2339 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2347 struct zebra_ns
*zns
;
2350 memset(&req
, 0, sizeof(req
));
2352 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
2353 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2354 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2356 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2358 if (mroute
->family
== AF_INET
) {
2359 req
.rtm
.rtm_family
= RTNL_FAMILY_IPMR
;
2360 req
.rtm
.rtm_dst_len
= IPV4_MAX_BITLEN
;
2361 req
.rtm
.rtm_src_len
= IPV4_MAX_BITLEN
;
2363 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2364 &mroute
->src
.ipaddr_v4
,
2365 sizeof(mroute
->src
.ipaddr_v4
));
2366 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2367 &mroute
->grp
.ipaddr_v4
,
2368 sizeof(mroute
->grp
.ipaddr_v4
));
2370 req
.rtm
.rtm_family
= RTNL_FAMILY_IP6MR
;
2371 req
.rtm
.rtm_dst_len
= IPV6_MAX_BITLEN
;
2372 req
.rtm
.rtm_src_len
= IPV6_MAX_BITLEN
;
2374 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2375 &mroute
->src
.ipaddr_v6
,
2376 sizeof(mroute
->src
.ipaddr_v6
));
2377 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2378 &mroute
->grp
.ipaddr_v6
,
2379 sizeof(mroute
->grp
.ipaddr_v6
));
2385 * So during the namespace cleanup we started storing
2386 * the zvrf table_id for the default table as RT_TABLE_MAIN
2387 * which is what the normal routing table for ip routing is.
2388 * This change caused this to break our lookups of sg data
2389 * because prior to this change the zvrf->table_id was 0
2390 * and when the pim multicast kernel code saw a 0,
2391 * it was auto-translated to RT_TABLE_DEFAULT. But since
2392 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2393 * and the kernel goes screw you and the delicious cookies you
2394 * are trying to give me. So now we have this little hack.
2396 if (mroute
->family
== AF_INET
)
2397 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
)
2401 actual_table
= zvrf
->table_id
;
2403 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2405 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2406 &zns
->netlink_cmd
, zns
, false);
2412 /* Char length to debug ID with */
2413 #define ID_LENGTH 10
2415 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2417 const struct nh_grp
*z_grp
,
2418 const uint8_t count
, bool resilient
,
2419 const struct nhg_resilience
*nhgr
)
2421 struct nexthop_grp grp
[count
];
2422 /* Need space for max group size, "/", and null term */
2423 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2424 char buf1
[ID_LENGTH
+ 2];
2428 memset(grp
, 0, sizeof(grp
));
2431 for (int i
= 0; i
< count
; i
++) {
2432 grp
[i
].id
= z_grp
[i
].id
;
2433 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2435 if (IS_ZEBRA_DEBUG_KERNEL
) {
2437 snprintf(buf
, sizeof(buf1
), "group %u",
2440 snprintf(buf1
, sizeof(buf1
), "/%u",
2442 strlcat(buf
, buf1
, sizeof(buf
));
2446 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2447 count
* sizeof(*grp
)))
2451 struct rtattr
*nest
;
2453 nest
= nl_attr_nest(n
, req_size
, NHA_RES_GROUP
);
2455 nl_attr_put16(n
, req_size
, NHA_RES_GROUP_BUCKETS
,
2457 nl_attr_put32(n
, req_size
, NHA_RES_GROUP_IDLE_TIMER
,
2458 nhgr
->idle_timer
* 1000);
2459 nl_attr_put32(n
, req_size
,
2460 NHA_RES_GROUP_UNBALANCED_TIMER
,
2461 nhgr
->unbalanced_timer
* 1000);
2462 nl_attr_nest_end(n
, nest
);
2464 nl_attr_put16(n
, req_size
, NHA_GROUP_TYPE
,
2465 NEXTHOP_GRP_TYPE_RES
);
2469 if (IS_ZEBRA_DEBUG_KERNEL
)
2470 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2476 * Next hop packet encoding helper function.
2478 * \param[in] cmd netlink command.
2479 * \param[in] ctx dataplane context (information snapshot).
2480 * \param[out] buf buffer to hold the packet.
2481 * \param[in] buflen amount of buffer bytes.
2483 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2484 * otherwise the number of bytes written to buf.
2486 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2487 const struct zebra_dplane_ctx
*ctx
,
2488 void *buf
, size_t buflen
)
2496 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2497 char label_buf
[256];
2499 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2500 int type
= dplane_ctx_get_nhe_type(ctx
);
2501 struct rtattr
*nest
;
2504 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2508 EC_ZEBRA_NHG_FIB_UPDATE
,
2509 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2514 * Nothing to do if the kernel doesn't support nexthop objects or
2515 * we dont want to install this type of NHG
2517 if (!kernel_nexthops_supported()) {
2518 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2520 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2521 __func__
, id
, zebra_route_string(type
));
2525 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2526 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2528 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2529 __func__
, id
, zebra_route_string(type
));
2533 label_buf
[0] = '\0';
2535 if (buflen
< sizeof(*req
))
2538 memset(req
, 0, sizeof(*req
));
2540 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2541 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2543 if (cmd
== RTM_NEWNEXTHOP
)
2544 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2546 req
->n
.nlmsg_type
= cmd
;
2547 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2549 req
->nhm
.nh_family
= AF_UNSPEC
;
2552 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2555 if (cmd
== RTM_NEWNEXTHOP
) {
2557 * We distinguish between a "group", which is a collection
2558 * of ids, and a singleton nexthop with an id. The
2559 * group is installed as an id that just refers to a list of
2562 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2563 const struct nexthop_group
*nhg
;
2564 const struct nhg_resilience
*nhgr
;
2566 nhg
= dplane_ctx_get_nhe_ng(ctx
);
2568 if (!_netlink_nexthop_build_group(
2569 &req
->n
, buflen
, id
,
2570 dplane_ctx_get_nhe_nh_grp(ctx
),
2571 dplane_ctx_get_nhe_nh_grp_count(ctx
),
2572 !!nhgr
->buckets
, nhgr
))
2575 const struct nexthop
*nh
=
2576 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2577 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2580 req
->nhm
.nh_family
= AF_INET
;
2581 else if (afi
== AFI_IP6
)
2582 req
->nhm
.nh_family
= AF_INET6
;
2585 case NEXTHOP_TYPE_IPV4
:
2586 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2587 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2592 case NEXTHOP_TYPE_IPV6
:
2593 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2594 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2599 case NEXTHOP_TYPE_BLACKHOLE
:
2600 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2603 /* Blackhole shouldn't have anymore attributes
2606 case NEXTHOP_TYPE_IFINDEX
:
2607 /* Don't need anymore info for this */
2613 EC_ZEBRA_NHG_FIB_UPDATE
,
2614 "Context received for kernel nexthop update without an interface");
2618 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2622 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2623 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2626 build_label_stack(nh
->nh_label
, out_lse
,
2627 label_buf
, sizeof(label_buf
));
2630 /* Set the BoS bit */
2631 out_lse
[num_labels
- 1] |=
2632 htonl(1 << MPLS_LS_S_SHIFT
);
2635 * TODO: MPLS unsupported for now in kernel.
2637 if (req
->nhm
.nh_family
== AF_MPLS
)
2640 encap
= LWTUNNEL_ENCAP_MPLS
;
2641 if (!nl_attr_put16(&req
->n
, buflen
,
2642 NHA_ENCAP_TYPE
, encap
))
2644 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2648 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2650 num_labels
* sizeof(mpls_lse_t
)))
2653 nl_attr_nest_end(&req
->n
, nest
);
2657 if (nh
->nh_srv6
->seg6local_action
!=
2658 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2661 struct rtattr
*nest
;
2662 const struct seg6local_context
*ctx
;
2664 req
->nhm
.nh_family
= AF_INET6
;
2665 action
= nh
->nh_srv6
->seg6local_action
;
2666 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2667 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2668 if (!nl_attr_put(&req
->n
, buflen
,
2674 nest
= nl_attr_nest(&req
->n
, buflen
,
2675 NHA_ENCAP
| NLA_F_NESTED
);
2680 case SEG6_LOCAL_ACTION_END
:
2684 SEG6_LOCAL_ACTION_END
))
2687 case SEG6_LOCAL_ACTION_END_X
:
2691 SEG6_LOCAL_ACTION_END_X
))
2695 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2696 sizeof(struct in6_addr
)))
2699 case SEG6_LOCAL_ACTION_END_T
:
2703 SEG6_LOCAL_ACTION_END_T
))
2711 case SEG6_LOCAL_ACTION_END_DX4
:
2715 SEG6_LOCAL_ACTION_END_DX4
))
2719 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2720 sizeof(struct in_addr
)))
2723 case SEG6_LOCAL_ACTION_END_DT6
:
2727 SEG6_LOCAL_ACTION_END_DT6
))
2735 case SEG6_LOCAL_ACTION_END_DT4
:
2739 SEG6_LOCAL_ACTION_END_DT4
))
2743 SEG6_LOCAL_VRFTABLE
,
2747 case SEG6_LOCAL_ACTION_END_DT46
:
2751 SEG6_LOCAL_ACTION_END_DT46
))
2755 SEG6_LOCAL_VRFTABLE
,
2760 zlog_err("%s: unsupport seg6local behaviour action=%u",
2764 nl_attr_nest_end(&req
->n
, nest
);
2767 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2770 struct rtattr
*nest
;
2772 if (!nl_attr_put16(&req
->n
, buflen
,
2774 LWTUNNEL_ENCAP_SEG6
))
2776 nest
= nl_attr_nest(&req
->n
, buflen
,
2777 NHA_ENCAP
| NLA_F_NESTED
);
2780 tun_len
= fill_seg6ipt_encap(tun_buf
,
2782 &nh
->nh_srv6
->seg6_segs
);
2785 if (!nl_attr_put(&req
->n
, buflen
,
2789 nl_attr_nest_end(&req
->n
, nest
);
2795 if (IS_ZEBRA_DEBUG_KERNEL
)
2796 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2797 __func__
, id
, nh
, nh
->ifindex
,
2798 vrf_id_to_name(nh
->vrf_id
),
2799 nh
->vrf_id
, label_buf
);
2802 req
->nhm
.nh_protocol
= zebra2proto(type
);
2804 } else if (cmd
!= RTM_DELNEXTHOP
) {
2806 EC_ZEBRA_NHG_FIB_UPDATE
,
2807 "Nexthop group kernel update command (%d) does not exist",
2812 if (IS_ZEBRA_DEBUG_KERNEL
)
2813 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2816 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2819 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2820 void *buf
, size_t buflen
)
2822 enum dplane_op_e op
;
2825 op
= dplane_ctx_get_op(ctx
);
2826 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2827 cmd
= RTM_NEWNEXTHOP
;
2828 else if (op
== DPLANE_OP_NH_DELETE
)
2829 cmd
= RTM_DELNEXTHOP
;
2831 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2832 "Context received for kernel nexthop update with incorrect OP code (%u)",
2837 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2840 enum netlink_msg_status
2841 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2842 struct zebra_dplane_ctx
*ctx
)
2844 /* Nothing to do if the kernel doesn't support nexthop objects */
2845 if (!kernel_nexthops_supported())
2846 return FRR_NETLINK_SUCCESS
;
2848 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2852 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2853 void *buf
, size_t buflen
)
2855 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2856 buflen
, false, false);
2859 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2860 void *buf
, size_t buflen
)
2862 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2863 buflen
, false, false);
2866 enum netlink_msg_status
2867 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2870 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2872 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2874 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2876 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2878 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2879 /* Single 'replace' operation */
2882 * With route replace semantics in place
2883 * for v4 routes and the new route is a system
2884 * route we do not install anything.
2885 * The problem here is that the new system
2886 * route should cause us to withdraw from
2887 * the kernel the old non-system route
2889 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2890 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2891 return netlink_batch_add_msg(
2892 bth
, ctx
, netlink_delroute_msg_encoder
,
2896 * So v6 route replace semantics are not in
2897 * the kernel at this point as I understand it.
2898 * so let's do a delete then an add.
2899 * In the future once v6 route replace semantics
2900 * are in we can figure out what to do here to
2901 * allow working with old and new kernels.
2903 * I'm also intentionally ignoring the failure case
2904 * of the route delete. If that happens yeah we're
2907 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2908 netlink_batch_add_msg(
2909 bth
, ctx
, netlink_delroute_msg_encoder
,
2915 return FRR_NETLINK_ERROR
;
2917 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2918 return FRR_NETLINK_SUCCESS
;
2920 return netlink_batch_add_msg(bth
, ctx
,
2922 ? netlink_newroute_msg_encoder
2923 : netlink_delroute_msg_encoder
,
2928 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2930 * @tb: Netlink RTA data
2931 * @family: Address family in the nhmsg
2932 * @ifp: Interface connected - this should be NULL, we fill it in
2933 * @ns_id: Namspace id
2935 * Return: New nexthop
2937 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2938 unsigned char family
,
2939 struct interface
**ifp
,
2942 struct nexthop nh
= {};
2944 enum nexthop_types_t type
= 0;
2947 struct interface
*ifp_lookup
;
2949 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2952 if (tb
[NHA_GATEWAY
]) {
2955 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2959 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2964 EC_ZEBRA_BAD_NHG_MESSAGE
,
2965 "Nexthop gateway with bad address family (%d) received from kernel",
2969 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2971 type
= NEXTHOP_TYPE_IFINDEX
;
2977 memcpy(&(nh
.gate
), gate
, sz
);
2980 nh
.ifindex
= if_index
;
2983 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2988 nh
.vrf_id
= ifp_lookup
->vrf
->vrf_id
;
2991 EC_ZEBRA_UNKNOWN_INTERFACE
,
2992 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2993 __func__
, nh
.ifindex
);
2995 nh
.vrf_id
= VRF_DEFAULT
;
2998 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2999 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
3002 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
3004 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
3005 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
3008 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
3015 static int netlink_nexthop_process_group(struct rtattr
**tb
,
3016 struct nh_grp
*z_grp
, int z_grp_size
,
3017 struct nhg_resilience
*nhgr
)
3020 /* linux/nexthop.h group struct */
3021 struct nexthop_grp
*n_grp
= NULL
;
3023 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
3024 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
3026 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
3027 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
3028 "Invalid nexthop group received from the kernel");
3032 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
3033 z_grp
[i
].id
= n_grp
[i
].id
;
3034 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
3037 memset(nhgr
, 0, sizeof(*nhgr
));
3038 if (tb
[NHA_RES_GROUP
]) {
3039 struct rtattr
*tbn
[NHA_RES_GROUP_MAX
+ 1];
3041 struct rtattr
*res_group
= tb
[NHA_RES_GROUP
];
3043 netlink_parse_rtattr_nested(tbn
, NHA_RES_GROUP_MAX
, res_group
);
3045 if (tbn
[NHA_RES_GROUP_BUCKETS
]) {
3046 rta
= tbn
[NHA_RES_GROUP_BUCKETS
];
3047 nhgr
->buckets
= *(uint16_t *)RTA_DATA(rta
);
3050 if (tbn
[NHA_RES_GROUP_IDLE_TIMER
]) {
3051 rta
= tbn
[NHA_RES_GROUP_IDLE_TIMER
];
3052 nhgr
->idle_timer
= *(uint32_t *)RTA_DATA(rta
);
3055 if (tbn
[NHA_RES_GROUP_UNBALANCED_TIMER
]) {
3056 rta
= tbn
[NHA_RES_GROUP_UNBALANCED_TIMER
];
3057 nhgr
->unbalanced_timer
= *(uint32_t *)RTA_DATA(rta
);
3060 if (tbn
[NHA_RES_GROUP_UNBALANCED_TIME
]) {
3061 rta
= tbn
[NHA_RES_GROUP_UNBALANCED_TIME
];
3062 nhgr
->unbalanced_time
= *(uint64_t *)RTA_DATA(rta
);
3070 * netlink_nexthop_change() - Read in change about nexthops from the kernel
3072 * @h: Netlink message header
3073 * @ns_id: Namspace id
3074 * @startup: Are we reading under startup conditions?
3076 * Return: Result status
3078 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3081 /* nexthop group id */
3083 unsigned char family
;
3085 afi_t afi
= AFI_UNSPEC
;
3086 vrf_id_t vrf_id
= VRF_DEFAULT
;
3087 struct interface
*ifp
= NULL
;
3088 struct nhmsg
*nhm
= NULL
;
3089 struct nexthop nh
= {};
3090 struct nh_grp grp
[MULTIPATH_NUM
] = {};
3091 /* Count of nexthops in group array */
3092 uint8_t grp_count
= 0;
3093 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
3095 frrtrace(3, frr_zebra
, netlink_nexthop_change
, h
, ns_id
, startup
);
3097 nhm
= NLMSG_DATA(h
);
3102 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
3105 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
3108 "%s: Message received from netlink is of a broken size %d %zu",
3109 __func__
, h
->nlmsg_len
,
3110 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
3114 netlink_parse_rtattr_flags(tb
, NHA_MAX
, RTM_NHA(nhm
), len
,
3120 EC_ZEBRA_BAD_NHG_MESSAGE
,
3121 "Nexthop group without an ID received from the kernel");
3125 /* We use the ID key'd nhg table for kernel updates */
3126 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
3128 if (zebra_evpn_mh_is_fdb_nh(id
)) {
3129 /* If this is a L2 NH just ignore it */
3130 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3131 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
3137 family
= nhm
->nh_family
;
3138 afi
= family2afi(family
);
3140 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
3142 if (IS_ZEBRA_DEBUG_KERNEL
)
3143 zlog_debug("%s ID (%u) %s NS %u",
3144 nl_msg_type_to_str(h
->nlmsg_type
), id
,
3145 nl_family_to_str(family
), ns_id
);
3148 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
3149 struct nhg_resilience nhgr
= {};
3151 if (tb
[NHA_GROUP
]) {
3153 * If this is a group message its only going to have
3154 * an array of nexthop IDs associated with it
3156 grp_count
= netlink_nexthop_process_group(
3157 tb
, grp
, array_size(grp
), &nhgr
);
3159 if (tb
[NHA_BLACKHOLE
]) {
3161 * This nexthop is just for blackhole-ing
3162 * traffic, it should not have an OIF, GATEWAY,
3165 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
3166 nh
.bh_type
= BLACKHOLE_UNSPEC
;
3167 } else if (tb
[NHA_OIF
])
3169 * This is a true new nexthop, so we need
3170 * to parse the gateway and device info
3172 nh
= netlink_nexthop_process_nh(tb
, family
,
3177 EC_ZEBRA_BAD_NHG_MESSAGE
,
3178 "Invalid Nexthop message received from the kernel with ID (%u)",
3182 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
3183 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
3184 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
3188 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
3189 type
, startup
, &nhgr
))
3192 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
3193 zebra_nhg_kernel_del(id
, vrf_id
);
3199 * netlink_request_nexthop() - Request nextop information from the kernel
3200 * @zns: Zebra namespace
3201 * @family: AF_* netlink family
3202 * @type: RTM_* route type
3204 * Return: Result status
3206 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
3213 /* Form the request, specifying filter (rtattr) if needed. */
3214 memset(&req
, 0, sizeof(req
));
3215 req
.n
.nlmsg_type
= type
;
3216 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3217 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3218 req
.nhm
.nh_family
= family
;
3220 return netlink_request(&zns
->netlink_cmd
, &req
);
3225 * netlink_nexthop_read() - Nexthop read function using netlink interface
3227 * @zns: Zebra name space
3229 * Return: Result status
3230 * Only called at bootstrap time.
3232 int netlink_nexthop_read(struct zebra_ns
*zns
)
3235 struct zebra_dplane_info dp_info
;
3237 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3239 /* Get nexthop objects */
3240 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3243 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3247 /* If we succesfully read in nexthop objects,
3248 * this kernel must support them.
3251 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3252 zlog_debug("Nexthop objects %ssupported on this kernel",
3253 supports_nh
? "" : "not ");
3255 zebra_router_set_supports_nhgs(supports_nh
);
3261 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3262 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3264 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3265 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3270 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3271 * evpn neighbor update as netlink messages using dataplane context object.
3272 * Here, a neighbor refers to a bridge forwarding database entry for
3273 * either unicast forwarding or head-end replication or an IP neighbor
3275 * @ctx: Dataplane context
3276 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3277 * @lla: A pointer to neighbor cache link layer address
3278 * @llalen: Length of the pointer to neighbor cache link layer
3280 * @ip: A neighbor cache n/w layer destination address
3281 * In the case of bridge FDB, this represnts the remote
3283 * @replace_obj: Whether NEW request should replace existing object or
3284 * add to the end of the list
3285 * @family: AF_* netlink family
3286 * @type: RTN_* route type
3287 * @flags: NTF_* flags
3288 * @state: NUD_* states
3289 * @data: data buffer pointer
3290 * @datalen: total amount of data buffer space
3291 * @protocol: protocol information
3293 * Return: 0 when the msg doesn't fit entirely in the buffer
3294 * otherwise the number of bytes written to buf.
3296 static ssize_t
netlink_neigh_update_msg_encode(
3297 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3298 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3299 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3300 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3301 size_t datalen
, uint8_t protocol
)
3309 enum dplane_op_e op
;
3311 if (datalen
< sizeof(*req
))
3313 memset(req
, 0, sizeof(*req
));
3315 op
= dplane_ctx_get_op(ctx
);
3317 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3318 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3319 if (cmd
== RTM_NEWNEIGH
)
3320 req
->n
.nlmsg_flags
|=
3322 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3323 req
->n
.nlmsg_type
= cmd
;
3324 req
->ndm
.ndm_family
= family
;
3325 req
->ndm
.ndm_type
= type
;
3326 req
->ndm
.ndm_state
= state
;
3327 req
->ndm
.ndm_flags
= flags
;
3328 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3330 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3335 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3340 struct rtattr
*nest
;
3342 nest
= nl_attr_nest(&req
->n
, datalen
,
3343 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3347 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3348 &nfy_flags
, sizeof(nfy_flags
)))
3350 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3353 nl_attr_nest_end(&req
->n
, nest
);
3358 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3364 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3368 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3369 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3374 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3375 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3378 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3382 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3383 dplane_ctx_mac_get_br_ifindex(ctx
)))
3387 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3391 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3392 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3395 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3396 void *buf
, size_t buflen
)
3398 struct ethaddr dst_mac
= {.octet
= {0}};
3399 int proto
= RTPROT_ZEBRA
;
3401 if (dplane_ctx_get_type(ctx
) != 0)
3402 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3404 return netlink_neigh_update_msg_encode(
3405 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3406 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3407 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3408 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3413 #define NDA_RTA(r) \
3414 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3417 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3420 struct interface
*ifp
;
3421 struct zebra_if
*zif
;
3422 struct rtattr
*tb
[NDA_MAX
+ 1];
3423 struct interface
*br_if
;
3426 struct in_addr vtep_ip
;
3427 int vid_present
= 0, dst_present
= 0;
3431 bool local_inactive
= false;
3432 bool dp_static
= false;
3433 uint32_t nhg_id
= 0;
3435 ndm
= NLMSG_DATA(h
);
3437 /* We only process macfdb notifications if EVPN is enabled */
3438 if (!is_evpn_enabled())
3441 /* Parse attributes and extract fields of interest. Do basic
3442 * validation of the fields.
3444 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3447 if (!tb
[NDA_LLADDR
]) {
3448 if (IS_ZEBRA_DEBUG_KERNEL
)
3449 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3450 nl_msg_type_to_str(h
->nlmsg_type
),
3455 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3456 if (IS_ZEBRA_DEBUG_KERNEL
)
3458 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3459 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3460 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3464 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3468 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3469 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3473 /* TODO: Only IPv4 supported now. */
3475 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3477 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3482 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3484 if (ndm
->ndm_state
& NUD_STALE
)
3485 local_inactive
= true;
3487 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3488 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3489 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3491 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3492 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3495 nfy_flags
= *(uint8_t *)RTA_DATA(
3496 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3497 if (nfy_flags
& FDB_NOTIFY_BIT
)
3499 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3500 local_inactive
= true;
3504 if (IS_ZEBRA_DEBUG_KERNEL
)
3505 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
3506 nl_msg_type_to_str(h
->nlmsg_type
),
3507 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
3508 ndm
->ndm_state
, ndm
->ndm_flags
, &mac
,
3509 dst_present
? dst_buf
: "", nhg_id
);
3511 /* The interface should exist. */
3512 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3514 if (!ifp
|| !ifp
->info
)
3517 /* The interface should be something we're interested in. */
3518 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3521 zif
= (struct zebra_if
*)ifp
->info
;
3522 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3523 if (IS_ZEBRA_DEBUG_KERNEL
)
3525 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3526 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3528 zif
->brslave_info
.bridge_ifindex
);
3532 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3534 if (filter_vlan
&& vid
!= filter_vlan
) {
3535 if (IS_ZEBRA_DEBUG_KERNEL
)
3536 zlog_debug(" Filtered due to filter vlan: %d",
3541 /* If add or update, do accordingly if learnt on a "local" interface; if
3542 * the notification is over VxLAN, this has to be related to
3544 * so perform an implicit delete of any local entry (if it exists).
3546 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3547 /* Drop "permanent" entries. */
3548 if (ndm
->ndm_state
& NUD_PERMANENT
) {
3549 if (IS_ZEBRA_DEBUG_KERNEL
)
3551 " Dropping entry because of NUD_PERMANENT");
3555 if (IS_ZEBRA_IF_VXLAN(ifp
))
3556 return zebra_vxlan_dp_network_mac_add(
3557 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
3558 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3560 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3561 sticky
, local_inactive
, dp_static
);
3564 /* This is a delete notification.
3565 * Ignore the notification with IP dest as it may just signify that the
3566 * MAC has moved from remote to local. The exception is the special
3567 * all-zeros MAC that represents the BUM flooding entry; we may have
3568 * to readd it. Otherwise,
3569 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3570 * 2. For a MAC over "local" interface, delete the mac
3571 * Note: We will get notifications from both bridge driver and VxLAN
3578 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
3580 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
3581 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
3585 if (IS_ZEBRA_IF_VXLAN(ifp
))
3586 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3588 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3591 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3596 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3599 /* Length validity. */
3600 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3604 /* We are interested only in AF_BRIDGE notifications. */
3605 ndm
= NLMSG_DATA(h
);
3606 if (ndm
->ndm_family
!= AF_BRIDGE
)
3609 return netlink_macfdb_change(h
, len
, ns_id
);
3612 /* Request for MAC FDB information from the kernel */
3613 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3614 int type
, ifindex_t master_ifindex
)
3618 struct ifinfomsg ifm
;
3622 /* Form the request, specifying filter (rtattr) if needed. */
3623 memset(&req
, 0, sizeof(req
));
3624 req
.n
.nlmsg_type
= type
;
3625 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3626 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3627 req
.ifm
.ifi_family
= family
;
3629 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3631 return netlink_request(netlink_cmd
, &req
);
3635 * MAC forwarding database read using netlink interface. This is invoked
3638 int netlink_macfdb_read(struct zebra_ns
*zns
)
3641 struct zebra_dplane_info dp_info
;
3643 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3645 /* Get bridge FDB table. */
3646 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3650 /* We are reading entire table. */
3652 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3659 * MAC forwarding database read using netlink interface. This is for a
3660 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3662 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3663 struct interface
*br_if
)
3665 struct zebra_if
*br_zif
;
3666 struct zebra_if
*zif
;
3667 struct zebra_l2info_vxlan
*vxl
;
3668 struct zebra_dplane_info dp_info
;
3671 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3673 /* Save VLAN we're filtering on, if needed. */
3674 br_zif
= (struct zebra_if
*)br_if
->info
;
3675 zif
= (struct zebra_if
*)ifp
->info
;
3676 vxl
= &zif
->l2info
.vxl
;
3677 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3678 filter_vlan
= vxl
->access_vlan
;
3680 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3682 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3686 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3687 &dp_info
, 0, false);
3689 /* Reset VLAN filter. */
3695 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3696 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3697 int family
, int type
,
3698 struct interface
*br_if
,
3699 const struct ethaddr
*mac
,
3707 struct zebra_if
*br_zif
;
3709 memset(&req
, 0, sizeof(req
));
3710 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3711 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3712 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3713 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3714 /* req.ndm.ndm_state = NUD_REACHABLE; */
3716 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3718 br_zif
= (struct zebra_if
*)br_if
->info
;
3719 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3720 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3722 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3724 if (IS_ZEBRA_DEBUG_KERNEL
)
3726 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
3727 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3728 br_if
->name
, br_if
->ifindex
, br_if
->vrf
->name
,
3729 br_if
->vrf
->vrf_id
, mac
, vid
);
3731 return netlink_request(&zns
->netlink_cmd
, &req
);
3734 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3735 struct interface
*br_if
,
3736 const struct ethaddr
*mac
, vlanid_t vid
)
3739 struct zebra_dplane_info dp_info
;
3741 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3743 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3745 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3751 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3752 &dp_info
, 1, false);
3758 * Netlink-specific handler for MAC updates using dataplane context object.
3760 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3763 struct ipaddr vtep_ip
;
3770 uint32_t update_flags
;
3772 uint8_t nfy_flags
= 0;
3773 int proto
= RTPROT_ZEBRA
;
3775 if (dplane_ctx_get_type(ctx
) != 0)
3776 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3778 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3779 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3782 state
= NUD_REACHABLE
;
3784 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3785 if (update_flags
& DPLANE_MAC_REMOTE
) {
3787 if (dplane_ctx_mac_is_sticky(ctx
)) {
3788 /* NUD_NOARP prevents the entry from expiring */
3790 /* sticky the entry from moving */
3791 flags
|= NTF_STICKY
;
3793 flags
|= NTF_EXT_LEARNED
;
3795 /* if it was static-local previously we need to clear the
3796 * notify flags on replace with remote
3798 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3802 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3803 nfy_flags
|= FDB_NOTIFY_BIT
;
3807 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3808 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3813 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3814 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3815 SET_IPADDR_V4(&vtep_ip
);
3817 if (IS_ZEBRA_DEBUG_KERNEL
) {
3819 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3821 vid
= dplane_ctx_mac_get_vlan(ctx
);
3823 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3828 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
3829 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3830 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3831 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3832 mac
, &vtep_ip
, nhg_id
,
3833 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3834 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3836 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3837 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3842 total
= netlink_neigh_update_msg_encode(
3843 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
3844 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
3845 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
3852 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3853 * 5549 support, re-install them.
3855 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3856 struct interface
*ifp
, struct ipaddr
*ip
,
3859 if (ndm
->ndm_family
!= AF_INET
)
3862 if (!zif
->v6_2_v4_ll_neigh_entry
)
3865 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3868 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3869 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3874 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3878 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3880 #define NUD_LOCAL_ACTIVE \
3881 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3883 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
3885 /* an exact match is done between
3886 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
3887 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
3888 * (see in lib/zclient.h)
3892 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3895 struct interface
*ifp
;
3896 struct zebra_if
*zif
;
3897 struct rtattr
*tb
[NDA_MAX
+ 1];
3898 struct interface
*link_if
;
3901 char buf
[ETHER_ADDR_STRLEN
];
3902 int mac_present
= 0;
3905 bool local_inactive
;
3906 uint32_t ext_flags
= 0;
3907 bool dp_static
= false;
3911 ndm
= NLMSG_DATA(h
);
3913 /* The interface should exist. */
3914 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3916 if (!ifp
|| !ifp
->info
)
3919 zif
= (struct zebra_if
*)ifp
->info
;
3921 /* Parse attributes and extract fields of interest. */
3922 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3925 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3926 nl_msg_type_to_str(h
->nlmsg_type
),
3927 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3928 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
3932 memset(&ip
, 0, sizeof(ip
));
3933 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3934 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3936 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3937 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3938 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3939 if (IS_ZEBRA_DEBUG_KERNEL
)
3941 " Neighbor Entry Received is a 5549 entry, finished");
3945 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3946 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3947 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3949 /* we send link layer information to client:
3950 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
3951 * - struct ipaddr ( for DEL and GET)
3952 * - struct ethaddr mac; (for NEW)
3954 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
3955 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
3956 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
3957 cmd
= ZEBRA_NHRP_NEIGH_GET
;
3958 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
3959 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
3961 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
3965 if (tb
[NDA_LLADDR
]) {
3966 /* copy LLADDR information */
3967 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
3969 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
3970 union sockunion link_layer_ipv4
;
3973 sockunion_family(&link_layer_ipv4
) = AF_INET
;
3974 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
3975 RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
3977 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
3978 zsend_nhrp_neighbor_notify(
3980 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
3984 if (h
->nlmsg_type
== RTM_GETNEIGH
)
3987 /* The neighbor is present on an SVI. From this, we locate the
3989 * bridge because we're only interested in neighbors on a VxLAN bridge.
3990 * The bridge is located based on the nature of the SVI:
3991 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3993 * and is linked to the bridge
3994 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3998 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3999 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
4003 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
4007 if (IS_ZEBRA_DEBUG_KERNEL
)
4009 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
4012 memset(&mac
, 0, sizeof(mac
));
4013 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
4014 if (tb
[NDA_LLADDR
]) {
4015 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
4016 if (IS_ZEBRA_DEBUG_KERNEL
)
4018 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
4023 ifp
->name
, ndm
->ndm_ifindex
,
4026 (unsigned long)RTA_PAYLOAD(
4032 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
4035 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
4036 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
4038 if (tb
[NDA_EXT_FLAGS
]) {
4039 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
4040 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
4044 if (IS_ZEBRA_DEBUG_KERNEL
)
4046 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
4047 nl_msg_type_to_str(h
->nlmsg_type
),
4048 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4049 ndm
->ndm_ifindex
, ifp
->vrf
->name
,
4050 ifp
->vrf
->vrf_id
, &ip
,
4052 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
4054 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
4056 /* If the neighbor state is valid for use, process as an add or
4058 * else process as a delete. Note that the delete handling may
4060 * in re-adding the neighbor if it is a valid "remote" neighbor.
4062 if (ndm
->ndm_state
& NUD_VALID
) {
4063 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
4065 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
4067 /* If EVPN-MH is not enabled we treat STALE
4068 * neighbors as locally-active and advertise
4071 local_inactive
= false;
4073 /* Add local neighbors to the l3 interface database */
4075 zebra_neigh_del(ifp
, &ip
);
4077 zebra_neigh_add(ifp
, &ip
, &mac
);
4080 zebra_vxlan_handle_kernel_neigh_update(
4081 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
4082 is_ext
, is_router
, local_inactive
,
4088 zebra_neigh_del(ifp
, &ip
);
4090 zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
4094 if (IS_ZEBRA_DEBUG_KERNEL
)
4095 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
4096 nl_msg_type_to_str(h
->nlmsg_type
),
4097 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
4098 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
4101 /* Process the delete - it may result in re-adding the neighbor if it is
4102 * a valid "remote" neighbor.
4104 zebra_neigh_del(ifp
, &ip
);
4106 zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
4111 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
4116 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
4119 /* Length validity. */
4120 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4124 /* We are interested only in AF_INET or AF_INET6 notifications. */
4125 ndm
= NLMSG_DATA(h
);
4126 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
4129 return netlink_neigh_change(h
, len
);
4132 /* Request for IP neighbor information from the kernel */
4133 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
4134 int type
, ifindex_t ifindex
)
4142 /* Form the request, specifying filter (rtattr) if needed. */
4143 memset(&req
, 0, sizeof(req
));
4144 req
.n
.nlmsg_type
= type
;
4145 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
4146 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4147 req
.ndm
.ndm_family
= family
;
4149 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
4151 return netlink_request(netlink_cmd
, &req
);
4155 * IP Neighbor table read using netlink interface. This is invoked
4158 int netlink_neigh_read(struct zebra_ns
*zns
)
4161 struct zebra_dplane_info dp_info
;
4163 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4165 /* Get IP neighbor table. */
4166 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4170 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4177 * IP Neighbor table read using netlink interface. This is for a specific
4180 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
4183 struct zebra_dplane_info dp_info
;
4185 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4187 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4191 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4192 &dp_info
, 0, false);
4198 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
4199 * read using netlink interface.
4201 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
4203 const struct ipaddr
*ip
,
4213 /* Form the request, specifying filter (rtattr) if needed. */
4214 memset(&req
, 0, sizeof(req
));
4215 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4216 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4217 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
4218 req
.ndm
.ndm_ifindex
= ifindex
;
4220 if (IS_IPADDR_V4(ip
)) {
4221 ipa_len
= IPV4_MAX_BYTELEN
;
4222 req
.ndm
.ndm_family
= AF_INET
;
4225 ipa_len
= IPV6_MAX_BYTELEN
;
4226 req
.ndm
.ndm_family
= AF_INET6
;
4229 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
4231 if (IS_ZEBRA_DEBUG_KERNEL
)
4232 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
4233 __func__
, nl_msg_type_to_str(type
),
4234 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
4237 return netlink_request(&zns
->netlink_cmd
, &req
);
4240 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4241 struct interface
*vlan_if
)
4244 struct zebra_ns
*zns
;
4245 struct zebra_vrf
*zvrf
= vlan_if
->vrf
->info
;
4246 struct zebra_dplane_info dp_info
;
4250 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4252 if (IS_ZEBRA_DEBUG_KERNEL
)
4253 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4254 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4255 vlan_if
->vrf
->name
, vlan_if
->vrf
->vrf_id
);
4257 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4262 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4263 &dp_info
, 1, false);
4268 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4273 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4274 || h
->nlmsg_type
== RTM_GETNEIGH
))
4277 /* Length validity. */
4278 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4281 "%s: Message received from netlink is of a broken size %d %zu",
4282 __func__
, h
->nlmsg_len
,
4283 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4287 /* Is this a notification for the MAC FDB or IP neighbor table? */
4288 ndm
= NLMSG_DATA(h
);
4289 if (ndm
->ndm_family
== AF_BRIDGE
)
4290 return netlink_macfdb_change(h
, len
, ns_id
);
4292 if (ndm
->ndm_type
!= RTN_UNICAST
)
4295 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4296 return netlink_ipneigh_change(h
, len
, ns_id
);
4299 EC_ZEBRA_UNKNOWN_FAMILY
,
4300 "Invalid address family: %u received from kernel neighbor change: %s",
4301 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4309 * Utility neighbor-update function, using info from dplane context.
4311 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4312 int cmd
, void *buf
, size_t buflen
)
4314 const struct ipaddr
*ip
;
4315 const struct ethaddr
*mac
= NULL
;
4316 const struct ipaddr
*link_ip
= NULL
;
4317 const void *link_ptr
= NULL
;
4318 char buf2
[ETHER_ADDR_STRLEN
];
4324 uint32_t update_flags
;
4325 uint32_t ext_flags
= 0;
4327 int proto
= RTPROT_ZEBRA
;
4329 if (dplane_ctx_get_type(ctx
) != 0)
4330 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4332 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4334 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4335 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4336 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4337 llalen
= IPADDRSZ(link_ip
);
4338 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4339 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4341 mac
= dplane_ctx_neigh_get_mac(ctx
);
4343 link_ptr
= (const void *)mac
;
4344 if (is_zero_mac(mac
))
4347 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4349 snprintf(buf2
, sizeof(buf2
), "null");
4351 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4352 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4353 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4355 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4357 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4358 flags
|= NTF_EXT_LEARNED
;
4359 /* if it was static-local previously we need to clear the
4360 * ext flags on replace with remote
4362 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4364 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4367 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4368 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4370 if (IS_ZEBRA_DEBUG_KERNEL
)
4372 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4373 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4374 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4375 ip
, link_ip
? "Link " : "MAC ", buf2
, flags
, state
,
4376 ext
? "ext " : "", ext_flags
);
4378 return netlink_neigh_update_msg_encode(
4379 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4380 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4381 ext_flags
, buf
, buflen
, proto
);
4384 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4385 void *data
, size_t datalen
)
4392 struct rtattr
*nest
;
4397 if (datalen
< sizeof(*req
))
4399 memset(req
, 0, sizeof(*req
));
4400 family
= dplane_ctx_neightable_get_family(ctx
);
4401 idx
= dplane_ctx_get_ifindex(ctx
);
4403 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4404 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4405 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4406 req
->ndtm
.ndtm_family
= family
;
4408 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4409 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4410 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4413 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4415 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4416 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4418 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4419 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4422 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4423 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4426 nl_attr_nest_end(&req
->n
, nest
);
4428 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4431 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4432 void *buf
, size_t buflen
)
4436 switch (dplane_ctx_get_op(ctx
)) {
4437 case DPLANE_OP_NEIGH_INSTALL
:
4438 case DPLANE_OP_NEIGH_UPDATE
:
4439 case DPLANE_OP_NEIGH_DISCOVER
:
4440 case DPLANE_OP_NEIGH_IP_INSTALL
:
4441 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4443 case DPLANE_OP_NEIGH_DELETE
:
4444 case DPLANE_OP_NEIGH_IP_DELETE
:
4445 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4447 case DPLANE_OP_VTEP_ADD
:
4448 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4451 case DPLANE_OP_VTEP_DELETE
:
4452 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4455 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4456 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4466 * Update MAC, using dataplane context object.
4469 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4470 struct zebra_dplane_ctx
*ctx
)
4472 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4476 enum netlink_msg_status
4477 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4479 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4484 * MPLS label forwarding table change via netlink interface, using dataplane
4485 * context information.
4487 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4488 void *buf
, size_t buflen
)
4491 const struct nhlfe_list_head
*head
;
4492 const struct zebra_nhlfe
*nhlfe
;
4493 struct nexthop
*nexthop
= NULL
;
4494 unsigned int nexthop_num
;
4495 const char *routedesc
;
4497 struct prefix p
= {0};
4499 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
4507 if (buflen
< sizeof(*req
))
4510 memset(req
, 0, sizeof(*req
));
4513 * Count # nexthops so we can decide whether to use singlepath
4514 * or multipath case.
4517 head
= dplane_ctx_get_nhlfe_list(ctx
);
4518 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4519 nexthop
= nhlfe
->nexthop
;
4522 if (cmd
== RTM_NEWROUTE
) {
4523 /* Count all selected NHLFEs */
4524 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4525 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4528 /* Count all installed NHLFEs */
4529 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4530 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4535 if ((nexthop_num
== 0) ||
4536 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4539 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4540 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4541 req
->n
.nlmsg_type
= cmd
;
4542 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
4544 req
->r
.rtm_family
= AF_MPLS
;
4545 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4546 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4547 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4548 req
->r
.rtm_type
= RTN_UNICAST
;
4550 if (cmd
== RTM_NEWROUTE
) {
4551 /* We do a replace to handle update. */
4552 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4554 /* set the protocol value if installing */
4555 route_type
= re_type_from_lsp_type(
4556 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4557 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4560 /* Fill destination */
4561 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4562 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4565 /* Fill nexthops (paths) based on single-path or multipath. The paths
4566 * chosen depend on the operation.
4568 if (nexthop_num
== 1) {
4569 routedesc
= "single-path";
4570 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4574 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4575 nexthop
= nhlfe
->nexthop
;
4579 if ((cmd
== RTM_NEWROUTE
4580 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4581 && CHECK_FLAG(nexthop
->flags
,
4582 NEXTHOP_FLAG_ACTIVE
)))
4583 || (cmd
== RTM_DELROUTE
4584 && (CHECK_FLAG(nhlfe
->flags
,
4585 NHLFE_FLAG_INSTALLED
)
4586 && CHECK_FLAG(nexthop
->flags
,
4587 NEXTHOP_FLAG_FIB
)))) {
4588 /* Add the gateway */
4589 if (!_netlink_mpls_build_singlepath(
4590 &p
, routedesc
, nhlfe
, &req
->n
,
4591 &req
->r
, buflen
, cmd
))
4598 } else { /* Multipath case */
4599 struct rtattr
*nest
;
4600 const union g_addr
*src1
= NULL
;
4602 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4606 routedesc
= "multipath";
4607 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4611 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4612 nexthop
= nhlfe
->nexthop
;
4616 if ((cmd
== RTM_NEWROUTE
4617 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4618 && CHECK_FLAG(nexthop
->flags
,
4619 NEXTHOP_FLAG_ACTIVE
)))
4620 || (cmd
== RTM_DELROUTE
4621 && (CHECK_FLAG(nhlfe
->flags
,
4622 NHLFE_FLAG_INSTALLED
)
4623 && CHECK_FLAG(nexthop
->flags
,
4624 NEXTHOP_FLAG_FIB
)))) {
4627 /* Build the multipath */
4628 if (!_netlink_mpls_build_multipath(
4629 &p
, routedesc
, nhlfe
, &req
->n
,
4630 buflen
, &req
->r
, &src1
))
4635 /* Add the multipath */
4636 nl_attr_nest_end(&req
->n
, nest
);
4639 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4642 /****************************************************************************
4643 * This code was developed in a branch that didn't have dplane APIs for
4644 * MAC updates. Hence the use of the legacy style. It will be moved to
4645 * the new dplane style pre-merge to master. XXX
4647 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4654 int cmd
= RTM_NEWNEXTHOP
;
4655 struct zebra_vrf
*zvrf
;
4656 struct zebra_ns
*zns
;
4658 zvrf
= zebra_vrf_get_evpn();
4661 memset(&req
, 0, sizeof(req
));
4663 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4664 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4665 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4666 req
.n
.nlmsg_type
= cmd
;
4667 req
.nhm
.nh_family
= AF_INET
;
4669 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4671 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4673 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4674 &vtep_ip
, IPV4_MAX_BYTELEN
))
4677 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4678 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4679 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4682 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4686 static int netlink_fdb_nh_del(uint32_t nh_id
)
4693 int cmd
= RTM_DELNEXTHOP
;
4694 struct zebra_vrf
*zvrf
;
4695 struct zebra_ns
*zns
;
4697 zvrf
= zebra_vrf_get_evpn();
4700 memset(&req
, 0, sizeof(req
));
4702 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4703 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4704 req
.n
.nlmsg_type
= cmd
;
4705 req
.nhm
.nh_family
= AF_UNSPEC
;
4707 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4710 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4711 zlog_debug("Tx %s fdb-nh 0x%x",
4712 nl_msg_type_to_str(cmd
), nh_id
);
4715 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4719 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4720 struct nh_grp
*nh_ids
)
4727 int cmd
= RTM_NEWNEXTHOP
;
4728 struct zebra_vrf
*zvrf
;
4729 struct zebra_ns
*zns
;
4730 struct nexthop_grp grp
[nh_cnt
];
4733 zvrf
= zebra_vrf_get_evpn();
4736 memset(&req
, 0, sizeof(req
));
4738 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4739 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4740 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4741 req
.n
.nlmsg_type
= cmd
;
4742 req
.nhm
.nh_family
= AF_UNSPEC
;
4744 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4746 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4748 memset(&grp
, 0, sizeof(grp
));
4749 for (i
= 0; i
< nh_cnt
; ++i
) {
4750 grp
[i
].id
= nh_ids
[i
].id
;
4751 grp
[i
].weight
= nh_ids
[i
].weight
;
4753 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4754 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4758 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4759 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4763 for (i
= 0; i
< nh_cnt
; ++i
) {
4764 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4766 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4769 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4770 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4773 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4777 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4779 return netlink_fdb_nh_del(nhg_id
);
4782 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4784 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4787 int kernel_del_mac_nh(uint32_t nh_id
)
4789 return netlink_fdb_nh_del(nh_id
);
4792 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4793 struct nh_grp
*nh_ids
)
4795 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4798 int kernel_del_mac_nhg(uint32_t nhg_id
)
4800 return netlink_fdb_nhg_del(nhg_id
);
4803 #endif /* HAVE_NETLINK */