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"
87 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
88 * UAPI for MAC sync. */
89 #ifndef _UAPI_LINUX_IF_BRIDGE_H
90 #define BR_SPH_LIST_SIZE 10
93 static vlanid_t filter_vlan
= 0;
95 /* We capture whether the current kernel supports nexthop ids; by
96 * default, we'll use them if possible. There's also a configuration
97 * available to _disable_ use of kernel nexthops.
99 static bool supports_nh
;
107 static const char ipv4_ll_buf
[16] = "169.254.0.1";
108 static struct in_addr ipv4_ll
;
110 /* Is this a ipv4 over ipv6 route? */
111 static bool is_route_v4_over_v6(unsigned char rtm_family
,
112 enum nexthop_types_t nexthop_type
)
114 if (rtm_family
== AF_INET
115 && (nexthop_type
== NEXTHOP_TYPE_IPV6
116 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
122 /* Helper to control use of kernel-level nexthop ids */
123 static bool kernel_nexthops_supported(void)
125 return (supports_nh
&& !vrf_is_backend_netns()
126 && zebra_nhg_kernel_nexthops_enabled());
130 * Some people may only want to use NHGs created by protos and not
131 * implicitly created by Zebra. This check accounts for that.
133 static bool proto_nexthops_only(void)
135 return zebra_nhg_proto_nexthops_only();
138 /* Is this a proto created NHG? */
139 static bool is_proto_nhg(uint32_t id
, int type
)
141 /* If type is available, use it as the source of truth */
143 if (type
!= ZEBRA_ROUTE_NHG
)
148 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
155 * The ipv4_ll data structure is used for all 5549
156 * additions to the kernel. Let's figure out the
157 * correct value one time instead for every
158 * install/remove of a 5549 type route
160 void rt_netlink_init(void)
162 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
166 * Mapping from dataplane neighbor flags to netlink flags
168 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
172 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
173 flags
|= NTF_EXT_LEARNED
;
174 if (dplane_flags
& DPLANE_NTF_ROUTER
)
176 if (dplane_flags
& DPLANE_NTF_USE
)
183 * Mapping from dataplane neighbor state to netlink state
185 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
189 if (dplane_state
& DPLANE_NUD_REACHABLE
)
190 state
|= NUD_REACHABLE
;
191 if (dplane_state
& DPLANE_NUD_STALE
)
193 if (dplane_state
& DPLANE_NUD_NOARP
)
195 if (dplane_state
& DPLANE_NUD_PROBE
)
197 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
198 state
|= NUD_INCOMPLETE
;
199 if (dplane_state
& DPLANE_NUD_PERMANENT
)
200 state
|= NUD_PERMANENT
;
201 if (dplane_state
& DPLANE_NUD_FAILED
)
208 static inline bool is_selfroute(int proto
)
210 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
211 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
212 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
213 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
214 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
215 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
216 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
217 || (proto
== RTPROT_SRTE
)) {
224 static inline int zebra2proto(int proto
)
227 case ZEBRA_ROUTE_BABEL
:
228 proto
= RTPROT_BABEL
;
230 case ZEBRA_ROUTE_BGP
:
233 case ZEBRA_ROUTE_OSPF
:
234 case ZEBRA_ROUTE_OSPF6
:
237 case ZEBRA_ROUTE_STATIC
:
238 proto
= RTPROT_ZSTATIC
;
240 case ZEBRA_ROUTE_ISIS
:
243 case ZEBRA_ROUTE_RIP
:
246 case ZEBRA_ROUTE_RIPNG
:
247 proto
= RTPROT_RIPNG
;
249 case ZEBRA_ROUTE_NHRP
:
252 case ZEBRA_ROUTE_EIGRP
:
253 proto
= RTPROT_EIGRP
;
255 case ZEBRA_ROUTE_LDP
:
258 case ZEBRA_ROUTE_SHARP
:
259 proto
= RTPROT_SHARP
;
261 case ZEBRA_ROUTE_PBR
:
264 case ZEBRA_ROUTE_OPENFABRIC
:
265 proto
= RTPROT_OPENFABRIC
;
267 case ZEBRA_ROUTE_SRTE
:
270 case ZEBRA_ROUTE_TABLE
:
271 case ZEBRA_ROUTE_NHG
:
272 proto
= RTPROT_ZEBRA
;
274 case ZEBRA_ROUTE_CONNECT
:
275 case ZEBRA_ROUTE_KERNEL
:
276 proto
= RTPROT_KERNEL
;
280 * When a user adds a new protocol this will show up
281 * to let them know to do something about it. This
282 * is intentionally a warn because we should see
283 * this as part of development of a new protocol
286 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
288 proto
= RTPROT_ZEBRA
;
295 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
299 proto
= ZEBRA_ROUTE_BABEL
;
302 proto
= ZEBRA_ROUTE_BGP
;
305 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
309 proto
= ZEBRA_ROUTE_ISIS
;
312 proto
= ZEBRA_ROUTE_RIP
;
315 proto
= ZEBRA_ROUTE_RIPNG
;
318 proto
= ZEBRA_ROUTE_NHRP
;
321 proto
= ZEBRA_ROUTE_EIGRP
;
324 proto
= ZEBRA_ROUTE_LDP
;
328 proto
= ZEBRA_ROUTE_STATIC
;
331 proto
= ZEBRA_ROUTE_SHARP
;
334 proto
= ZEBRA_ROUTE_PBR
;
336 case RTPROT_OPENFABRIC
:
337 proto
= ZEBRA_ROUTE_OPENFABRIC
;
340 proto
= ZEBRA_ROUTE_SRTE
;
344 proto
= ZEBRA_ROUTE_NHG
;
347 /* Intentional fall thru */
350 * When a user adds a new protocol this will show up
351 * to let them know to do something about it. This
352 * is intentionally a warn because we should see
353 * this as part of development of a new protocol
356 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
358 proto
= ZEBRA_ROUTE_KERNEL
;
365 Pending: create an efficient table_id (in a tree/hash) based lookup)
367 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
370 struct zebra_vrf
*zvrf
;
372 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
376 /* case vrf with netns : match the netnsid */
377 if (vrf_is_backend_netns()) {
378 if (ns_id
== zvrf_id(zvrf
))
379 return zvrf_id(zvrf
);
381 /* VRF is VRF_BACKEND_VRF_LITE */
382 if (zvrf
->table_id
!= table_id
)
384 return zvrf_id(zvrf
);
392 * @parse_encap_mpls() - Parses encapsulated mpls attributes
393 * @tb: Pointer to rtattr to look for nested items in.
394 * @labels: Pointer to store labels in.
396 * Return: Number of mpls labels found.
398 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
400 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
401 mpls_lse_t
*lses
= NULL
;
406 mpls_label_t label
= 0;
408 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
409 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
410 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
411 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
412 labels
[num_labels
++] = label
;
418 static enum seg6local_action_t
419 parse_encap_seg6local(struct rtattr
*tb
,
420 struct seg6local_context
*ctx
)
422 struct rtattr
*tb_encap
[256] = {};
423 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
425 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
427 if (tb_encap
[SEG6_LOCAL_ACTION
])
428 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
430 if (tb_encap
[SEG6_LOCAL_NH4
])
431 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
432 tb_encap
[SEG6_LOCAL_NH4
]);
434 if (tb_encap
[SEG6_LOCAL_NH6
])
435 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
436 tb_encap
[SEG6_LOCAL_NH6
]);
438 if (tb_encap
[SEG6_LOCAL_TABLE
])
439 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
444 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
446 struct rtattr
*tb_encap
[256] = {};
447 struct seg6_iptunnel_encap
*ipt
= NULL
;
448 struct in6_addr
*segments
= NULL
;
450 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
453 * TODO: It's not support multiple SID list.
455 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
456 ipt
= (struct seg6_iptunnel_encap
*)
457 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
458 segments
= ipt
->srh
[0].segments
;
467 static struct nexthop
468 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
469 enum blackhole_type bh_type
, int index
, void *prefsrc
,
470 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
472 struct interface
*ifp
= NULL
;
473 struct nexthop nh
= {0};
474 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
476 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
477 struct seg6local_context seg6l_ctx
= {};
478 struct in6_addr seg6_segs
= {};
481 vrf_id_t nh_vrf_id
= vrf_id
;
482 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
484 if (bh_type
== BLACKHOLE_UNSPEC
) {
486 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
487 else if (index
&& gate
)
488 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
489 : NEXTHOP_TYPE_IPV6_IFINDEX
;
490 else if (!index
&& gate
)
491 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
494 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
495 nh
.bh_type
= bh_type
;
498 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
499 nh
.bh_type
= bh_type
;
503 memcpy(&nh
.src
, prefsrc
, sz
);
505 memcpy(&nh
.gate
, gate
, sz
);
508 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
510 nh_vrf_id
= ifp
->vrf_id
;
512 nh
.vrf_id
= nh_vrf_id
;
514 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
515 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
516 == LWTUNNEL_ENCAP_MPLS
) {
517 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
519 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
520 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
521 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
522 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
524 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
525 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
526 == LWTUNNEL_ENCAP_SEG6
) {
527 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
530 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
531 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
534 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
536 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
537 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
540 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
545 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
546 struct nexthop_group
*ng
,
548 struct rtnexthop
*rtnh
,
550 void *prefsrc
, vrf_id_t vrf_id
)
553 struct interface
*ifp
= NULL
;
556 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
558 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
559 struct seg6local_context seg6l_ctx
= {};
560 struct in6_addr seg6_segs
= {};
562 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
564 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
565 vrf_id_t nh_vrf_id
= vrf_id
;
568 struct nexthop
*nh
= NULL
;
570 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
573 index
= rtnh
->rtnh_ifindex
;
576 * Yes we are looking this up
577 * for every nexthop and just
578 * using the last one looked
581 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
584 nh_vrf_id
= ifp
->vrf_id
;
587 EC_ZEBRA_UNKNOWN_INTERFACE
,
588 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
590 nh_vrf_id
= VRF_DEFAULT
;
595 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
596 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
597 rtnh
->rtnh_len
- sizeof(*rtnh
));
598 if (rtnh_tb
[RTA_GATEWAY
])
599 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
600 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
601 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
602 == LWTUNNEL_ENCAP_MPLS
) {
603 num_labels
= parse_encap_mpls(
604 rtnh_tb
[RTA_ENCAP
], labels
);
606 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
607 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
608 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
609 seg6l_act
= parse_encap_seg6local(
610 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
612 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
613 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
614 == LWTUNNEL_ENCAP_SEG6
) {
615 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
620 if (gate
&& rtm
->rtm_family
== AF_INET
) {
622 nh
= nexthop_from_ipv4_ifindex(
623 gate
, prefsrc
, index
, nh_vrf_id
);
625 nh
= nexthop_from_ipv4(gate
, prefsrc
,
627 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
629 nh
= nexthop_from_ipv6_ifindex(
630 gate
, index
, nh_vrf_id
);
632 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
634 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
637 nh
->weight
= rtnh
->rtnh_hops
+ 1;
640 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
643 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
644 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
648 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
650 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
651 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
653 /* Add to temporary list */
654 nexthop_group_add_sorted(ng
, nh
);
657 if (rtnh
->rtnh_len
== 0)
660 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
661 rtnh
= RTNH_NEXT(rtnh
);
664 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
669 /* Looking up routing table by netlink interface. */
670 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
675 struct rtattr
*tb
[RTA_MAX
+ 1];
678 struct prefix_ipv6 src_p
= {};
682 char anyaddr
[16] = {0};
684 int proto
= ZEBRA_ROUTE_KERNEL
;
689 uint8_t distance
= 0;
695 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
696 void *src
= NULL
; /* IPv6 srcdest source prefix */
697 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
701 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
703 switch (rtm
->rtm_type
) {
707 bh_type
= BLACKHOLE_NULL
;
709 case RTN_UNREACHABLE
:
710 bh_type
= BLACKHOLE_REJECT
;
713 bh_type
= BLACKHOLE_ADMINPROHIB
;
716 if (IS_ZEBRA_DEBUG_KERNEL
)
717 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
718 nl_rttype_to_str(rtm
->rtm_type
),
723 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
726 "%s: Message received from netlink is of a broken size %d %zu",
727 __func__
, h
->nlmsg_len
,
728 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
732 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
734 if (rtm
->rtm_flags
& RTM_F_CLONED
)
736 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
738 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
741 selfroute
= is_selfroute(rtm
->rtm_protocol
);
743 if (!startup
&& selfroute
744 && h
->nlmsg_type
== RTM_NEWROUTE
745 && !zrouter
.asic_offloaded
) {
746 if (IS_ZEBRA_DEBUG_KERNEL
)
747 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
752 /* We don't care about change notifications for the MPLS table. */
753 /* TODO: Revisit this. */
754 if (rtm
->rtm_family
== AF_MPLS
)
757 /* Table corresponding to route. */
759 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
761 table
= rtm
->rtm_table
;
764 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
765 if (vrf_id
== VRF_DEFAULT
) {
766 if (!is_zebra_valid_kernel_table(table
)
767 && !is_zebra_main_routing_table(table
))
771 if (rtm
->rtm_flags
& RTM_F_TRAP
)
772 flags
|= ZEBRA_FLAG_TRAPPED
;
773 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
774 flags
|= ZEBRA_FLAG_OFFLOADED
;
775 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
776 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
778 /* Route which inserted by Zebra. */
780 flags
|= ZEBRA_FLAG_SELFROUTE
;
781 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
784 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
787 dest
= RTA_DATA(tb
[RTA_DST
]);
792 src
= RTA_DATA(tb
[RTA_SRC
]);
797 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
800 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
803 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
805 if (tb
[RTA_PRIORITY
])
806 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
808 #if defined(SUPPORT_REALMS)
810 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
813 if (tb
[RTA_METRICS
]) {
814 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
816 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
817 RTA_PAYLOAD(tb
[RTA_METRICS
]));
820 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
823 if (rtm
->rtm_family
== AF_INET
) {
825 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
827 "Invalid destination prefix length: %u received from kernel route change",
831 memcpy(&p
.u
.prefix4
, dest
, 4);
832 p
.prefixlen
= rtm
->rtm_dst_len
;
834 if (rtm
->rtm_src_len
!= 0) {
836 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
837 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
842 /* Force debug below to not display anything for source */
844 } else if (rtm
->rtm_family
== AF_INET6
) {
846 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
848 "Invalid destination prefix length: %u received from kernel route change",
852 memcpy(&p
.u
.prefix6
, dest
, 16);
853 p
.prefixlen
= rtm
->rtm_dst_len
;
855 src_p
.family
= AF_INET6
;
856 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
858 "Invalid source prefix length: %u received from kernel route change",
862 memcpy(&src_p
.prefix
, src
, 16);
863 src_p
.prefixlen
= rtm
->rtm_src_len
;
865 /* We only handle the AFs we handle... */
866 if (IS_ZEBRA_DEBUG_KERNEL
)
867 zlog_debug("%s: unknown address-family %u", __func__
,
873 * For ZEBRA_ROUTE_KERNEL types:
875 * The metric/priority of the route received from the kernel
876 * is a 32 bit number. We are going to interpret the high
877 * order byte as the Admin Distance and the low order 3 bytes
880 * This will allow us to do two things:
881 * 1) Allow the creation of kernel routes that can be
882 * overridden by zebra.
883 * 2) Allow the old behavior for 'most' kernel route types
884 * if a user enters 'ip route ...' v4 routes get a metric
885 * of 0 and v6 routes get a metric of 1024. Both of these
886 * values will end up with a admin distance of 0, which
887 * will cause them to win for the purposes of zebra.
889 if (proto
== ZEBRA_ROUTE_KERNEL
) {
890 distance
= (metric
>> 24) & 0xFF;
891 metric
= (metric
& 0x00FFFFFF);
894 if (IS_ZEBRA_DEBUG_KERNEL
) {
895 char buf2
[PREFIX_STRLEN
];
898 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
899 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
900 src_p
.prefixlen
? " from " : "",
901 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
903 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
908 if (rtm
->rtm_family
== AF_INET6
)
911 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
913 if (!tb
[RTA_MULTIPATH
]) {
914 struct nexthop nh
= {0};
917 nh
= parse_nexthop_unicast(
918 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
921 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
922 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
925 /* This is a multipath route */
926 struct route_entry
*re
;
927 struct nexthop_group
*ng
= NULL
;
928 struct rtnexthop
*rtnh
=
929 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
931 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
933 re
->distance
= distance
;
939 re
->uptime
= monotime(NULL
);
946 /* Use temporary list of nexthops; parse
947 * message payload's nexthops.
949 ng
= nexthop_group_new();
951 parse_multipath_nexthops_unicast(
952 ns_id
, ng
, rtm
, rtnh
, tb
,
955 zserv_nexthop_num_warn(
956 __func__
, (const struct prefix
*)&p
,
960 nexthop_group_delete(&ng
);
966 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
973 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
974 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
977 if (!tb
[RTA_MULTIPATH
]) {
980 nh
= parse_nexthop_unicast(
981 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
983 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
984 flags
, &p
, &src_p
, &nh
, 0, table
,
985 metric
, distance
, true);
987 /* XXX: need to compare the entire list of
988 * nexthops here for NLM_F_APPEND stupidity */
989 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
990 flags
, &p
, &src_p
, NULL
, 0, table
,
991 metric
, distance
, true);
999 static struct mcast_route_data
*mroute
= NULL
;
1001 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1002 ns_id_t ns_id
, int startup
)
1006 struct rtattr
*tb
[RTA_MAX
+ 1];
1007 struct mcast_route_data
*m
;
1008 struct mcast_route_data mr
;
1013 char oif_list
[256] = "\0";
1020 memset(&mr
, 0, sizeof(mr
));
1024 rtm
= NLMSG_DATA(h
);
1026 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1028 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1031 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1033 table
= rtm
->rtm_table
;
1035 vrf
= vrf_lookup_by_table(table
, ns_id
);
1038 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1041 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1044 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1046 if (tb
[RTA_EXPIRES
])
1047 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1049 if (tb
[RTA_MULTIPATH
]) {
1050 struct rtnexthop
*rtnh
=
1051 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1053 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1055 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1058 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1061 if (rtnh
->rtnh_len
== 0)
1064 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1065 rtnh
= RTNH_NEXT(rtnh
);
1069 if (IS_ZEBRA_DEBUG_KERNEL
) {
1070 struct interface
*ifp
= NULL
;
1071 struct zebra_vrf
*zvrf
= NULL
;
1073 for (count
= 0; count
< oif_count
; count
++) {
1074 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1077 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1078 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1079 strlcat(oif_list
, temp
, sizeof(oif_list
));
1081 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1082 ifp
= if_lookup_by_index(iif
, vrf
);
1084 "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld",
1085 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1086 &m
->sg
.src
, &m
->sg
.grp
, ifp
? ifp
->name
: "Unknown",
1093 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1098 rtm
= NLMSG_DATA(h
);
1100 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1101 /* If this is not route add/delete message print warning. */
1102 zlog_debug("Kernel message: %s NS %u",
1103 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1107 if (!(rtm
->rtm_family
== AF_INET
||
1108 rtm
->rtm_family
== AF_INET6
||
1109 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1111 EC_ZEBRA_UNKNOWN_FAMILY
,
1112 "Invalid address family: %u received from kernel route change: %s",
1113 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1117 /* Connected route. */
1118 if (IS_ZEBRA_DEBUG_KERNEL
)
1119 zlog_debug("%s %s %s proto %s NS %u",
1120 nl_msg_type_to_str(h
->nlmsg_type
),
1121 nl_family_to_str(rtm
->rtm_family
),
1122 nl_rttype_to_str(rtm
->rtm_type
),
1123 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1126 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1129 "%s: Message received from netlink is of a broken size: %d %zu",
1130 __func__
, h
->nlmsg_len
,
1131 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1135 if (rtm
->rtm_type
== RTN_MULTICAST
)
1136 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1138 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1142 /* Request for specific route information from the kernel */
1143 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1150 /* Form the request, specifying filter (rtattr) if needed. */
1151 memset(&req
, 0, sizeof(req
));
1152 req
.n
.nlmsg_type
= type
;
1153 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1154 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1155 req
.rtm
.rtm_family
= family
;
1157 return netlink_request(&zns
->netlink_cmd
, &req
);
1160 /* Routing table read function using netlink interface. Only called
1162 int netlink_route_read(struct zebra_ns
*zns
)
1165 struct zebra_dplane_info dp_info
;
1167 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1169 /* Get IPv4 routing table. */
1170 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1173 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1174 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1178 /* Get IPv6 routing table. */
1179 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1182 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1183 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1191 * The function returns true if the gateway info could be added
1192 * to the message, otherwise false is returned.
1194 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1196 struct nlmsghdr
*nlmsg
,
1197 size_t req_size
, int bytelen
,
1198 const struct nexthop
*nexthop
)
1200 if (route_family
== AF_MPLS
) {
1201 struct gw_family_t gw_fam
;
1203 gw_fam
.family
= gw_family
;
1204 if (gw_family
== AF_INET
)
1205 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1207 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1208 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1212 if (!(nexthop
->rparent
1213 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1214 if (gw_family
== AF_INET
) {
1215 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1216 &nexthop
->gate
.ipv4
, bytelen
))
1219 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1220 &nexthop
->gate
.ipv6
, bytelen
))
1229 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1230 mpls_lse_t
*out_lse
, char *label_buf
,
1231 size_t label_buf_size
)
1233 char label_buf1
[20];
1236 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1237 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1240 if (IS_ZEBRA_DEBUG_KERNEL
) {
1242 snprintf(label_buf
, label_buf_size
, "label %u",
1243 nh_label
->label
[i
]);
1245 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1246 nh_label
->label
[i
]);
1247 strlcat(label_buf
, label_buf1
, label_buf_size
);
1251 out_lse
[num_labels
] =
1252 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1259 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1260 struct nlmsghdr
*nlmsg
,
1261 size_t buflen
, struct rtmsg
*rtmsg
,
1263 size_t label_buf_size
)
1265 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1269 * label_buf is *only* currently used within debugging.
1270 * As such when we assign it we are guarding it inside
1271 * a debug test. If you want to change this make sure
1272 * you fix this assumption
1274 label_buf
[0] = '\0';
1277 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1280 /* Set the BoS bit */
1281 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1283 if (rtmsg
->rtm_family
== AF_MPLS
) {
1284 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1285 num_labels
* sizeof(mpls_lse_t
)))
1288 struct rtattr
*nest
;
1290 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1291 LWTUNNEL_ENCAP_MPLS
))
1294 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1298 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1300 num_labels
* sizeof(mpls_lse_t
)))
1302 nl_attr_nest_end(nlmsg
, nest
);
1309 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1311 struct nlmsghdr
*nlmsg
,
1312 size_t buflen
, int bytelen
)
1314 if (family
== AF_INET
) {
1315 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1316 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1317 &nexthop
->rmap_src
.ipv4
, bytelen
))
1319 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1320 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1321 &nexthop
->src
.ipv4
, bytelen
))
1324 } else if (family
== AF_INET6
) {
1325 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1326 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1327 &nexthop
->rmap_src
.ipv6
, bytelen
))
1329 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1330 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1331 &nexthop
->src
.ipv6
, bytelen
))
1339 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1340 const struct in6_addr
*seg
)
1342 struct seg6_iptunnel_encap
*ipt
;
1343 struct ipv6_sr_hdr
*srh
;
1344 const size_t srhlen
= 24;
1347 * Caution: Support only SINGLE-SID, not MULTI-SID
1348 * This function only supports the case where segs represents
1349 * a single SID. If you want to extend the SRv6 functionality,
1350 * you should improve the Boundary Check.
1351 * Ex. In case of set a SID-List include multiple-SIDs as an
1352 * argument of the Transit Behavior, we must support variable
1353 * boundary check for buflen.
1355 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1356 sizeof(struct ipv6_sr_hdr
) + 16))
1359 memset(buffer
, 0, buflen
);
1361 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1362 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1364 srh
->hdrlen
= (srhlen
>> 3) - 1;
1366 srh
->segments_left
= 0;
1367 srh
->first_segment
= 0;
1368 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1373 /* This function takes a nexthop as argument and adds
1374 * the appropriate netlink attributes to an existing
1377 * @param routedesc: Human readable description of route type
1378 * (direct/recursive, single-/multipath)
1379 * @param bytelen: Length of addresses in bytes.
1380 * @param nexthop: Nexthop information
1381 * @param nlmsg: nlmsghdr structure to fill in.
1382 * @param req_size: The size allocated for the message.
1384 * The function returns true if the nexthop could be added
1385 * to the message, otherwise false is returned.
1387 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1388 const char *routedesc
, int bytelen
,
1389 const struct nexthop
*nexthop
,
1390 struct nlmsghdr
*nlmsg
,
1391 struct rtmsg
*rtmsg
,
1392 size_t req_size
, int cmd
)
1395 char label_buf
[256];
1397 char addrstr
[INET6_ADDRSTRLEN
];
1401 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1403 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1404 req_size
, rtmsg
, label_buf
,
1408 if (nexthop
->nh_srv6
) {
1409 if (nexthop
->nh_srv6
->seg6local_action
!=
1410 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1411 struct rtattr
*nest
;
1412 const struct seg6local_context
*ctx
;
1414 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1415 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1416 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1419 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1423 switch (nexthop
->nh_srv6
->seg6local_action
) {
1424 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1425 if (!nl_attr_put32(nlmsg
, req_size
,
1427 SEG6_LOCAL_ACTION_END
))
1430 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1431 if (!nl_attr_put32(nlmsg
, req_size
,
1433 SEG6_LOCAL_ACTION_END_X
))
1435 if (!nl_attr_put(nlmsg
, req_size
,
1436 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1437 sizeof(struct in6_addr
)))
1440 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1441 if (!nl_attr_put32(nlmsg
, req_size
,
1443 SEG6_LOCAL_ACTION_END_T
))
1445 if (!nl_attr_put32(nlmsg
, req_size
,
1450 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1451 if (!nl_attr_put32(nlmsg
, req_size
,
1453 SEG6_LOCAL_ACTION_END_DX4
))
1455 if (!nl_attr_put(nlmsg
, req_size
,
1456 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1457 sizeof(struct in_addr
)))
1460 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1461 if (!nl_attr_put32(nlmsg
, req_size
,
1463 SEG6_LOCAL_ACTION_END_DT6
))
1465 if (!nl_attr_put32(nlmsg
, req_size
,
1471 zlog_err("%s: unsupport seg6local behaviour action=%u",
1473 nexthop
->nh_srv6
->seg6local_action
);
1476 nl_attr_nest_end(nlmsg
, nest
);
1479 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1482 struct rtattr
*nest
;
1484 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1485 LWTUNNEL_ENCAP_SEG6
))
1487 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1490 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1491 &nexthop
->nh_srv6
->seg6_segs
);
1494 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1497 nl_attr_nest_end(nlmsg
, nest
);
1501 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1502 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1504 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1505 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1506 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1508 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1511 if (cmd
== RTM_NEWROUTE
) {
1512 if (!_netlink_route_encode_nexthop_src(
1513 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1517 if (IS_ZEBRA_DEBUG_KERNEL
)
1518 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1519 __func__
, routedesc
, p
, ipv4_ll_buf
,
1520 label_buf
, nexthop
->ifindex
,
1521 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1525 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1526 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1527 /* Send deletes to the kernel without specifying the next-hop */
1528 if (cmd
!= RTM_DELROUTE
) {
1529 if (!_netlink_route_add_gateway_info(
1530 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1535 if (cmd
== RTM_NEWROUTE
) {
1536 if (!_netlink_route_encode_nexthop_src(
1537 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1541 if (IS_ZEBRA_DEBUG_KERNEL
) {
1542 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1544 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1545 __func__
, routedesc
, p
, addrstr
, label_buf
,
1546 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1551 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1552 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1553 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1554 AF_INET6
, nlmsg
, req_size
,
1558 if (cmd
== RTM_NEWROUTE
) {
1559 if (!_netlink_route_encode_nexthop_src(
1560 nexthop
, AF_INET6
, nlmsg
, req_size
,
1565 if (IS_ZEBRA_DEBUG_KERNEL
) {
1566 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1568 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1569 __func__
, routedesc
, p
, addrstr
, label_buf
,
1570 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1576 * We have the ifindex so we should always send it
1577 * This is especially useful if we are doing route
1580 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1581 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1585 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1586 if (cmd
== RTM_NEWROUTE
) {
1587 if (!_netlink_route_encode_nexthop_src(
1588 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1592 if (IS_ZEBRA_DEBUG_KERNEL
)
1593 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1594 __func__
, routedesc
, p
, nexthop
->ifindex
,
1595 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1601 /* This function takes a nexthop as argument and
1602 * appends to the given netlink msg. If the nexthop
1603 * defines a preferred source, the src parameter
1604 * will be modified to point to that src, otherwise
1605 * it will be kept unmodified.
1607 * @param routedesc: Human readable description of route type
1608 * (direct/recursive, single-/multipath)
1609 * @param bytelen: Length of addresses in bytes.
1610 * @param nexthop: Nexthop information
1611 * @param nlmsg: nlmsghdr structure to fill in.
1612 * @param req_size: The size allocated for the message.
1613 * @param src: pointer pointing to a location where
1614 * the prefsrc should be stored.
1616 * The function returns true if the nexthop could be added
1617 * to the message, otherwise false is returned.
1619 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1620 const char *routedesc
, int bytelen
,
1621 const struct nexthop
*nexthop
,
1622 struct nlmsghdr
*nlmsg
,
1623 size_t req_size
, struct rtmsg
*rtmsg
,
1624 const union g_addr
**src
)
1626 char label_buf
[256];
1628 struct rtnexthop
*rtnh
;
1630 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1636 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1638 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1639 req_size
, rtmsg
, label_buf
,
1643 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1644 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1646 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1647 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1648 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1650 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1651 if (nexthop
->weight
)
1652 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1654 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1655 *src
= &nexthop
->rmap_src
;
1656 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1657 *src
= &nexthop
->src
;
1659 if (IS_ZEBRA_DEBUG_KERNEL
)
1661 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1662 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1663 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1665 nl_attr_rtnh_end(nlmsg
, rtnh
);
1669 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1670 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1671 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1672 nlmsg
, req_size
, bytelen
,
1676 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1677 *src
= &nexthop
->rmap_src
;
1678 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1679 *src
= &nexthop
->src
;
1681 if (IS_ZEBRA_DEBUG_KERNEL
)
1682 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1683 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1684 label_buf
, nexthop
->ifindex
,
1685 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1687 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1688 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1689 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1690 AF_INET6
, nlmsg
, req_size
,
1694 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1695 *src
= &nexthop
->rmap_src
;
1696 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1697 *src
= &nexthop
->src
;
1699 if (IS_ZEBRA_DEBUG_KERNEL
)
1700 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1701 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1702 label_buf
, nexthop
->ifindex
,
1703 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1707 * We have figured out the ifindex so we should always send it
1708 * This is especially useful if we are doing route
1711 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1712 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1715 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1716 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1717 *src
= &nexthop
->rmap_src
;
1718 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1719 *src
= &nexthop
->src
;
1721 if (IS_ZEBRA_DEBUG_KERNEL
)
1722 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1723 __func__
, routedesc
, p
, nexthop
->ifindex
,
1724 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1727 if (nexthop
->weight
)
1728 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1730 nl_attr_rtnh_end(nlmsg
, rtnh
);
1735 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1736 const struct zebra_nhlfe
*nhlfe
,
1737 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1738 size_t req_size
, int cmd
)
1743 family
= NHLFE_FAMILY(nhlfe
);
1744 bytelen
= (family
== AF_INET
? 4 : 16);
1745 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1746 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1752 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1753 const struct zebra_nhlfe
*nhlfe
,
1754 struct nlmsghdr
*nlmsg
, size_t req_size
,
1755 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1760 family
= NHLFE_FAMILY(nhlfe
);
1761 bytelen
= (family
== AF_INET
? 4 : 16);
1762 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1763 nhlfe
->nexthop
, nlmsg
, req_size
,
1767 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1769 if (IS_ZEBRA_DEBUG_KERNEL
)
1770 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1771 routedesc
, nl_msg_type_to_str(cmd
), label
);
1774 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1775 int llalen
, ns_id_t ns_id
, uint8_t family
,
1776 bool permanent
, uint8_t protocol
)
1784 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1786 memset(&req
, 0, sizeof(req
));
1788 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1789 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1790 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1791 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1793 req
.ndm
.ndm_family
= family
;
1794 req
.ndm
.ndm_ifindex
= ifindex
;
1795 req
.ndm
.ndm_type
= RTN_UNICAST
;
1796 if (cmd
== RTM_NEWNEIGH
) {
1798 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1800 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1802 req
.ndm
.ndm_state
= NUD_FAILED
;
1804 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1806 req
.ndm
.ndm_type
= RTN_UNICAST
;
1807 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
1808 family2addrsize(family
));
1810 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1812 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1816 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1819 if (family
== AF_INET
) {
1820 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1821 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1823 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1824 src
->ipv4
= nexthop
->src
.ipv4
;
1827 } else if (family
== AF_INET6
) {
1828 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1829 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1831 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1832 src
->ipv6
= nexthop
->src
.ipv6
;
1841 * The function returns true if the attribute could be added
1842 * to the message, otherwise false is returned.
1844 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1847 struct rtattr
*nest
;
1849 switch (nh
->nh_encap_type
) {
1851 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1854 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1858 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1861 nl_attr_nest_end(n
, nest
);
1869 * Routing table change via netlink interface, using a dataplane context object
1871 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1872 * otherwise the number of bytes written to buf.
1874 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1875 struct zebra_dplane_ctx
*ctx
,
1876 uint8_t *data
, size_t datalen
,
1877 bool fpm
, bool force_nhg
)
1880 struct nexthop
*nexthop
= NULL
;
1881 unsigned int nexthop_num
;
1882 const char *routedesc
;
1883 bool setsrc
= false;
1885 const struct prefix
*p
, *src_p
;
1892 } *req
= (void *)data
;
1894 p
= dplane_ctx_get_dest(ctx
);
1895 src_p
= dplane_ctx_get_src(ctx
);
1897 if (datalen
< sizeof(*req
))
1900 memset(req
, 0, sizeof(*req
));
1902 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1904 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1905 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1907 if ((cmd
== RTM_NEWROUTE
) &&
1908 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1909 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1911 req
->n
.nlmsg_type
= cmd
;
1913 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1915 req
->r
.rtm_family
= p
->family
;
1916 req
->r
.rtm_dst_len
= p
->prefixlen
;
1917 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1918 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1920 if (cmd
== RTM_DELROUTE
)
1921 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1923 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1926 * blackhole routes are not RTN_UNICAST, they are
1927 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1928 * so setting this value as a RTN_UNICAST would
1929 * cause the route lookup of just the prefix
1930 * to fail. So no need to specify this for
1931 * the RTM_DELROUTE case
1933 if (cmd
!= RTM_DELROUTE
)
1934 req
->r
.rtm_type
= RTN_UNICAST
;
1936 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1939 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1945 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1947 * either by the kernel or by zebra. Its purely for calculating best
1949 * by the routing protocol and for communicating with protocol peers.
1951 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1952 NL_DEFAULT_ROUTE_METRIC
))
1955 #if defined(SUPPORT_REALMS)
1959 if (cmd
== RTM_DELROUTE
)
1960 tag
= dplane_ctx_get_old_tag(ctx
);
1962 tag
= dplane_ctx_get_tag(ctx
);
1964 if (tag
> 0 && tag
<= 255) {
1965 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
1970 /* Table corresponding to this route. */
1971 table_id
= dplane_ctx_get_table(ctx
);
1973 req
->r
.rtm_table
= table_id
;
1975 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
1976 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
1980 if (IS_ZEBRA_DEBUG_KERNEL
)
1982 "%s: %s %pFX vrf %u(%u)", __func__
,
1983 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
1987 * If we are not updating the route and we have received
1988 * a route delete, then all we need to fill in is the
1989 * prefix information to tell the kernel to schwack
1992 if (cmd
== RTM_DELROUTE
)
1993 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1995 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1996 struct rtattr
*nest
;
1997 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1998 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2000 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2003 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2007 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2009 nl_attr_nest_end(&req
->n
, nest
);
2013 * Always install blackhole routes without using nexthops, because of
2014 * the following kernel problems:
2015 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2016 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2018 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2020 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2021 nexthop
= nexthop
->resolved
;
2023 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2024 switch (nexthop
->bh_type
) {
2025 case BLACKHOLE_ADMINPROHIB
:
2026 req
->r
.rtm_type
= RTN_PROHIBIT
;
2028 case BLACKHOLE_REJECT
:
2029 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2032 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2035 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2039 if ((!fpm
&& kernel_nexthops_supported()
2040 && (!proto_nexthops_only()
2041 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2042 || (fpm
&& force_nhg
)) {
2043 /* Kernel supports nexthop objects */
2044 if (IS_ZEBRA_DEBUG_KERNEL
)
2045 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2046 dplane_ctx_get_nhe_id(ctx
));
2048 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2049 dplane_ctx_get_nhe_id(ctx
)))
2052 /* Have to determine src still */
2053 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2057 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2061 if (p
->family
== AF_INET
) {
2062 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2063 &src
.ipv4
, bytelen
))
2065 } else if (p
->family
== AF_INET6
) {
2066 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2067 &src
.ipv6
, bytelen
))
2072 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2075 /* Count overall nexthops so we can decide whether to use singlepath
2076 * or multipath case.
2079 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2080 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2082 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2088 /* Singlepath case. */
2089 if (nexthop_num
== 1) {
2091 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2092 if (CHECK_FLAG(nexthop
->flags
,
2093 NEXTHOP_FLAG_RECURSIVE
)) {
2098 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2103 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2104 routedesc
= nexthop
->rparent
2105 ? "recursive, single-path"
2108 if (!_netlink_route_build_singlepath(
2109 p
, routedesc
, bytelen
, nexthop
,
2110 &req
->n
, &req
->r
, datalen
, cmd
))
2117 * Add encapsulation information when installing via
2121 if (!netlink_route_nexthop_encap(
2122 &req
->n
, datalen
, nexthop
))
2128 if (p
->family
== AF_INET
) {
2129 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2130 &src
.ipv4
, bytelen
))
2132 } else if (p
->family
== AF_INET6
) {
2133 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2134 &src
.ipv6
, bytelen
))
2138 } else { /* Multipath case */
2139 struct rtattr
*nest
;
2140 const union g_addr
*src1
= NULL
;
2142 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2147 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2148 if (CHECK_FLAG(nexthop
->flags
,
2149 NEXTHOP_FLAG_RECURSIVE
)) {
2150 /* This only works for IPv4 now */
2154 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2159 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2160 routedesc
= nexthop
->rparent
2161 ? "recursive, multipath"
2165 if (!_netlink_route_build_multipath(
2166 p
, routedesc
, bytelen
, nexthop
,
2167 &req
->n
, datalen
, &req
->r
, &src1
))
2170 if (!setsrc
&& src1
) {
2171 if (p
->family
== AF_INET
)
2172 src
.ipv4
= src1
->ipv4
;
2173 else if (p
->family
== AF_INET6
)
2174 src
.ipv6
= src1
->ipv6
;
2181 nl_attr_nest_end(&req
->n
, nest
);
2184 * Add encapsulation information when installing via
2188 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2190 if (CHECK_FLAG(nexthop
->flags
,
2191 NEXTHOP_FLAG_RECURSIVE
))
2193 if (!netlink_route_nexthop_encap(
2194 &req
->n
, datalen
, nexthop
))
2201 if (p
->family
== AF_INET
) {
2202 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2203 &src
.ipv4
, bytelen
))
2205 } else if (p
->family
== AF_INET6
) {
2206 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2207 &src
.ipv6
, bytelen
))
2210 if (IS_ZEBRA_DEBUG_KERNEL
)
2211 zlog_debug("Setting source");
2215 /* If there is no useful nexthop then return. */
2216 if (nexthop_num
== 0) {
2217 if (IS_ZEBRA_DEBUG_KERNEL
)
2218 zlog_debug("%s: No useful nexthop.", __func__
);
2221 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2224 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2226 uint32_t actual_table
;
2228 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2236 struct zebra_ns
*zns
;
2239 memset(&req
, 0, sizeof(req
));
2241 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2242 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2243 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2245 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2246 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2248 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2249 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2250 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2251 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2255 * So during the namespace cleanup we started storing
2256 * the zvrf table_id for the default table as RT_TABLE_MAIN
2257 * which is what the normal routing table for ip routing is.
2258 * This change caused this to break our lookups of sg data
2259 * because prior to this change the zvrf->table_id was 0
2260 * and when the pim multicast kernel code saw a 0,
2261 * it was auto-translated to RT_TABLE_DEFAULT. But since
2262 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2263 * and the kernel goes screw you and the delicious cookies you
2264 * are trying to give me. So now we have this little hack.
2266 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2268 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2270 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2271 &zns
->netlink_cmd
, zns
, 0);
2277 /* Char length to debug ID with */
2278 #define ID_LENGTH 10
2280 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2282 const struct nh_grp
*z_grp
,
2283 const uint8_t count
)
2285 struct nexthop_grp grp
[count
];
2286 /* Need space for max group size, "/", and null term */
2287 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2288 char buf1
[ID_LENGTH
+ 2];
2292 memset(grp
, 0, sizeof(grp
));
2295 for (int i
= 0; i
< count
; i
++) {
2296 grp
[i
].id
= z_grp
[i
].id
;
2297 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2299 if (IS_ZEBRA_DEBUG_KERNEL
) {
2301 snprintf(buf
, sizeof(buf1
), "group %u",
2304 snprintf(buf1
, sizeof(buf1
), "/%u",
2306 strlcat(buf
, buf1
, sizeof(buf
));
2310 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2311 count
* sizeof(*grp
)))
2315 if (IS_ZEBRA_DEBUG_KERNEL
)
2316 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2322 * Next hop packet encoding helper function.
2324 * \param[in] cmd netlink command.
2325 * \param[in] ctx dataplane context (information snapshot).
2326 * \param[out] buf buffer to hold the packet.
2327 * \param[in] buflen amount of buffer bytes.
2329 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2330 * otherwise the number of bytes written to buf.
2332 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2333 const struct zebra_dplane_ctx
*ctx
,
2334 void *buf
, size_t buflen
)
2342 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2343 char label_buf
[256];
2345 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2346 int type
= dplane_ctx_get_nhe_type(ctx
);
2347 struct rtattr
*nest
;
2352 EC_ZEBRA_NHG_FIB_UPDATE
,
2353 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2358 * Nothing to do if the kernel doesn't support nexthop objects or
2359 * we dont want to install this type of NHG
2361 if (!kernel_nexthops_supported()) {
2362 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2364 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2365 __func__
, id
, zebra_route_string(type
));
2369 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2370 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2372 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2373 __func__
, id
, zebra_route_string(type
));
2377 label_buf
[0] = '\0';
2379 if (buflen
< sizeof(*req
))
2382 memset(req
, 0, sizeof(*req
));
2384 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2385 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2387 if (cmd
== RTM_NEWNEXTHOP
)
2388 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2390 req
->n
.nlmsg_type
= cmd
;
2391 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2393 req
->nhm
.nh_family
= AF_UNSPEC
;
2396 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2399 if (cmd
== RTM_NEWNEXTHOP
) {
2401 * We distinguish between a "group", which is a collection
2402 * of ids, and a singleton nexthop with an id. The
2403 * group is installed as an id that just refers to a list of
2406 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2407 if (!_netlink_nexthop_build_group(
2408 &req
->n
, buflen
, id
,
2409 dplane_ctx_get_nhe_nh_grp(ctx
),
2410 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2413 const struct nexthop
*nh
=
2414 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2415 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2418 req
->nhm
.nh_family
= AF_INET
;
2419 else if (afi
== AFI_IP6
)
2420 req
->nhm
.nh_family
= AF_INET6
;
2423 case NEXTHOP_TYPE_IPV4
:
2424 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2425 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2430 case NEXTHOP_TYPE_IPV6
:
2431 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2432 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2437 case NEXTHOP_TYPE_BLACKHOLE
:
2438 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2441 /* Blackhole shouldn't have anymore attributes
2444 case NEXTHOP_TYPE_IFINDEX
:
2445 /* Don't need anymore info for this */
2451 EC_ZEBRA_NHG_FIB_UPDATE
,
2452 "Context received for kernel nexthop update without an interface");
2456 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2460 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2461 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2464 build_label_stack(nh
->nh_label
, out_lse
,
2465 label_buf
, sizeof(label_buf
));
2468 /* Set the BoS bit */
2469 out_lse
[num_labels
- 1] |=
2470 htonl(1 << MPLS_LS_S_SHIFT
);
2473 * TODO: MPLS unsupported for now in kernel.
2475 if (req
->nhm
.nh_family
== AF_MPLS
)
2478 encap
= LWTUNNEL_ENCAP_MPLS
;
2479 if (!nl_attr_put16(&req
->n
, buflen
,
2480 NHA_ENCAP_TYPE
, encap
))
2482 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2486 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2488 num_labels
* sizeof(mpls_lse_t
)))
2491 nl_attr_nest_end(&req
->n
, nest
);
2495 if (nh
->nh_srv6
->seg6local_action
!=
2496 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2499 struct rtattr
*nest
;
2500 const struct seg6local_context
*ctx
;
2502 req
->nhm
.nh_family
= AF_INET6
;
2503 action
= nh
->nh_srv6
->seg6local_action
;
2504 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2505 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2506 if (!nl_attr_put(&req
->n
, buflen
,
2512 nest
= nl_attr_nest(&req
->n
, buflen
,
2513 NHA_ENCAP
| NLA_F_NESTED
);
2518 case SEG6_LOCAL_ACTION_END
:
2522 SEG6_LOCAL_ACTION_END
))
2525 case SEG6_LOCAL_ACTION_END_X
:
2529 SEG6_LOCAL_ACTION_END_X
))
2533 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2534 sizeof(struct in6_addr
)))
2537 case SEG6_LOCAL_ACTION_END_T
:
2541 SEG6_LOCAL_ACTION_END_T
))
2549 case SEG6_LOCAL_ACTION_END_DX4
:
2553 SEG6_LOCAL_ACTION_END_DX4
))
2557 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2558 sizeof(struct in_addr
)))
2561 case SEG6_LOCAL_ACTION_END_DT6
:
2565 SEG6_LOCAL_ACTION_END_DT6
))
2574 zlog_err("%s: unsupport seg6local behaviour action=%u",
2578 nl_attr_nest_end(&req
->n
, nest
);
2581 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2584 struct rtattr
*nest
;
2586 if (!nl_attr_put16(&req
->n
, buflen
,
2588 LWTUNNEL_ENCAP_SEG6
))
2590 nest
= nl_attr_nest(&req
->n
, buflen
,
2591 NHA_ENCAP
| NLA_F_NESTED
);
2594 tun_len
= fill_seg6ipt_encap(tun_buf
,
2596 &nh
->nh_srv6
->seg6_segs
);
2599 if (!nl_attr_put(&req
->n
, buflen
,
2603 nl_attr_nest_end(&req
->n
, nest
);
2609 if (IS_ZEBRA_DEBUG_KERNEL
)
2610 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2611 __func__
, id
, nh
, nh
->ifindex
,
2612 vrf_id_to_name(nh
->vrf_id
),
2613 nh
->vrf_id
, label_buf
);
2616 req
->nhm
.nh_protocol
= zebra2proto(type
);
2618 } else if (cmd
!= RTM_DELNEXTHOP
) {
2620 EC_ZEBRA_NHG_FIB_UPDATE
,
2621 "Nexthop group kernel update command (%d) does not exist",
2626 if (IS_ZEBRA_DEBUG_KERNEL
)
2627 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2630 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2633 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2634 void *buf
, size_t buflen
)
2636 enum dplane_op_e op
;
2639 op
= dplane_ctx_get_op(ctx
);
2640 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2641 cmd
= RTM_NEWNEXTHOP
;
2642 else if (op
== DPLANE_OP_NH_DELETE
)
2643 cmd
= RTM_DELNEXTHOP
;
2645 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2646 "Context received for kernel nexthop update with incorrect OP code (%u)",
2651 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2654 enum netlink_msg_status
2655 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2656 struct zebra_dplane_ctx
*ctx
)
2658 /* Nothing to do if the kernel doesn't support nexthop objects */
2659 if (!kernel_nexthops_supported())
2660 return FRR_NETLINK_SUCCESS
;
2662 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2666 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2667 void *buf
, size_t buflen
)
2669 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2670 buflen
, false, false);
2673 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2674 void *buf
, size_t buflen
)
2676 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2677 buflen
, false, false);
2680 enum netlink_msg_status
2681 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2684 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2686 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2688 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2690 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2692 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2693 /* Single 'replace' operation */
2696 * With route replace semantics in place
2697 * for v4 routes and the new route is a system
2698 * route we do not install anything.
2699 * The problem here is that the new system
2700 * route should cause us to withdraw from
2701 * the kernel the old non-system route
2703 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2704 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2705 netlink_batch_add_msg(
2706 bth
, ctx
, netlink_delroute_msg_encoder
,
2710 * So v6 route replace semantics are not in
2711 * the kernel at this point as I understand it.
2712 * so let's do a delete then an add.
2713 * In the future once v6 route replace semantics
2714 * are in we can figure out what to do here to
2715 * allow working with old and new kernels.
2717 * I'm also intentionally ignoring the failure case
2718 * of the route delete. If that happens yeah we're
2721 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2722 netlink_batch_add_msg(
2723 bth
, ctx
, netlink_delroute_msg_encoder
,
2729 return FRR_NETLINK_ERROR
;
2731 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2732 return FRR_NETLINK_SUCCESS
;
2734 return netlink_batch_add_msg(bth
, ctx
,
2736 ? netlink_newroute_msg_encoder
2737 : netlink_delroute_msg_encoder
,
2742 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2744 * @tb: Netlink RTA data
2745 * @family: Address family in the nhmsg
2746 * @ifp: Interface connected - this should be NULL, we fill it in
2747 * @ns_id: Namspace id
2749 * Return: New nexthop
2751 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2752 unsigned char family
,
2753 struct interface
**ifp
,
2756 struct nexthop nh
= {};
2758 enum nexthop_types_t type
= 0;
2761 struct interface
*ifp_lookup
;
2763 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2766 if (tb
[NHA_GATEWAY
]) {
2769 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2773 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2778 EC_ZEBRA_BAD_NHG_MESSAGE
,
2779 "Nexthop gateway with bad address family (%d) received from kernel",
2783 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2785 type
= NEXTHOP_TYPE_IFINDEX
;
2791 memcpy(&(nh
.gate
), gate
, sz
);
2794 nh
.ifindex
= if_index
;
2797 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2802 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2805 EC_ZEBRA_UNKNOWN_INTERFACE
,
2806 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2807 __func__
, nh
.ifindex
);
2809 nh
.vrf_id
= VRF_DEFAULT
;
2812 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2813 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2816 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2818 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2819 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2822 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2829 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2830 struct nh_grp
*z_grp
, int z_grp_size
)
2833 /* linux/nexthop.h group struct */
2834 struct nexthop_grp
*n_grp
= NULL
;
2836 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2837 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2839 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2840 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2841 "Invalid nexthop group received from the kernel");
2845 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2846 z_grp
[i
].id
= n_grp
[i
].id
;
2847 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2853 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2855 * @h: Netlink message header
2856 * @ns_id: Namspace id
2857 * @startup: Are we reading under startup conditions?
2859 * Return: Result status
2861 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2864 /* nexthop group id */
2866 unsigned char family
;
2868 afi_t afi
= AFI_UNSPEC
;
2869 vrf_id_t vrf_id
= VRF_DEFAULT
;
2870 struct interface
*ifp
= NULL
;
2871 struct nhmsg
*nhm
= NULL
;
2872 struct nexthop nh
= {};
2873 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2874 /* Count of nexthops in group array */
2875 uint8_t grp_count
= 0;
2876 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2878 nhm
= NLMSG_DATA(h
);
2883 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2886 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2889 "%s: Message received from netlink is of a broken size %d %zu",
2890 __func__
, h
->nlmsg_len
,
2891 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2895 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2900 EC_ZEBRA_BAD_NHG_MESSAGE
,
2901 "Nexthop group without an ID received from the kernel");
2905 /* We use the ID key'd nhg table for kernel updates */
2906 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2908 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2909 /* If this is a L2 NH just ignore it */
2910 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2911 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2917 family
= nhm
->nh_family
;
2918 afi
= family2afi(family
);
2920 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2922 if (IS_ZEBRA_DEBUG_KERNEL
)
2923 zlog_debug("%s ID (%u) %s NS %u",
2924 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2925 nl_family_to_str(family
), ns_id
);
2928 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2929 if (tb
[NHA_GROUP
]) {
2931 * If this is a group message its only going to have
2932 * an array of nexthop IDs associated with it
2934 grp_count
= netlink_nexthop_process_group(
2935 tb
, grp
, array_size(grp
));
2937 if (tb
[NHA_BLACKHOLE
]) {
2939 * This nexthop is just for blackhole-ing
2940 * traffic, it should not have an OIF, GATEWAY,
2943 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2944 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2945 } else if (tb
[NHA_OIF
])
2947 * This is a true new nexthop, so we need
2948 * to parse the gateway and device info
2950 nh
= netlink_nexthop_process_nh(tb
, family
,
2955 EC_ZEBRA_BAD_NHG_MESSAGE
,
2956 "Invalid Nexthop message received from the kernel with ID (%u)",
2960 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2961 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2962 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2966 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2970 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2971 zebra_nhg_kernel_del(id
, vrf_id
);
2977 * netlink_request_nexthop() - Request nextop information from the kernel
2978 * @zns: Zebra namespace
2979 * @family: AF_* netlink family
2980 * @type: RTM_* route type
2982 * Return: Result status
2984 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2991 /* Form the request, specifying filter (rtattr) if needed. */
2992 memset(&req
, 0, sizeof(req
));
2993 req
.n
.nlmsg_type
= type
;
2994 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2995 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2996 req
.nhm
.nh_family
= family
;
2998 return netlink_request(&zns
->netlink_cmd
, &req
);
3003 * netlink_nexthop_read() - Nexthop read function using netlink interface
3005 * @zns: Zebra name space
3007 * Return: Result status
3008 * Only called at bootstrap time.
3010 int netlink_nexthop_read(struct zebra_ns
*zns
)
3013 struct zebra_dplane_info dp_info
;
3015 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3017 /* Get nexthop objects */
3018 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3021 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3025 /* If we succesfully read in nexthop objects,
3026 * this kernel must support them.
3030 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3031 zlog_debug("Nexthop objects %ssupported on this kernel",
3032 supports_nh
? "" : "not ");
3038 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3039 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3041 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3042 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3047 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3048 * evpn neighbor update as netlink messages using dataplane context object.
3049 * Here, a neighbor refers to a bridge forwarding database entry for
3050 * either unicast forwarding or head-end replication or an IP neighbor
3052 * @ctx: Dataplane context
3053 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3054 * @lla: A pointer to neighbor cache link layer address
3055 * @llalen: Length of the pointer to neighbor cache link layer
3057 * @ip: A neighbor cache n/w layer destination address
3058 * In the case of bridge FDB, this represnts the remote
3060 * @replace_obj: Whether NEW request should replace existing object or
3061 * add to the end of the list
3062 * @family: AF_* netlink family
3063 * @type: RTN_* route type
3064 * @flags: NTF_* flags
3065 * @state: NUD_* states
3066 * @data: data buffer pointer
3067 * @datalen: total amount of data buffer space
3068 * @protocol: protocol information
3070 * Return: 0 when the msg doesn't fit entirely in the buffer
3071 * otherwise the number of bytes written to buf.
3073 static ssize_t
netlink_neigh_update_msg_encode(
3074 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3075 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3076 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3077 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3078 size_t datalen
, uint8_t protocol
)
3086 enum dplane_op_e op
;
3088 if (datalen
< sizeof(*req
))
3090 memset(req
, 0, sizeof(*req
));
3092 op
= dplane_ctx_get_op(ctx
);
3094 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3095 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3096 if (cmd
== RTM_NEWNEIGH
)
3097 req
->n
.nlmsg_flags
|=
3099 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3100 req
->n
.nlmsg_type
= cmd
;
3101 req
->ndm
.ndm_family
= family
;
3102 req
->ndm
.ndm_type
= type
;
3103 req
->ndm
.ndm_state
= state
;
3104 req
->ndm
.ndm_flags
= flags
;
3105 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3107 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3112 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3117 struct rtattr
*nest
;
3119 nest
= nl_attr_nest(&req
->n
, datalen
,
3120 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3124 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3125 &nfy_flags
, sizeof(nfy_flags
)))
3127 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3130 nl_attr_nest_end(&req
->n
, nest
);
3135 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3141 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3145 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3146 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3151 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3152 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3155 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3159 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3160 dplane_ctx_mac_get_br_ifindex(ctx
)))
3164 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3168 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3169 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3172 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3173 void *buf
, size_t buflen
)
3175 struct ethaddr dst_mac
= {.octet
= {0}};
3176 int proto
= RTPROT_ZEBRA
;
3178 if (dplane_ctx_get_type(ctx
) != 0)
3179 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3181 return netlink_neigh_update_msg_encode(
3182 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3183 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3184 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3185 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3190 #define NDA_RTA(r) \
3191 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3194 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3197 struct interface
*ifp
;
3198 struct zebra_if
*zif
;
3199 struct rtattr
*tb
[NDA_MAX
+ 1];
3200 struct interface
*br_if
;
3203 struct in_addr vtep_ip
;
3204 int vid_present
= 0, dst_present
= 0;
3208 bool local_inactive
= false;
3209 bool dp_static
= false;
3210 uint32_t nhg_id
= 0;
3212 ndm
= NLMSG_DATA(h
);
3214 /* We only process macfdb notifications if EVPN is enabled */
3215 if (!is_evpn_enabled())
3218 /* Parse attributes and extract fields of interest. Do basic
3219 * validation of the fields.
3221 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3224 if (!tb
[NDA_LLADDR
]) {
3225 if (IS_ZEBRA_DEBUG_KERNEL
)
3226 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3227 nl_msg_type_to_str(h
->nlmsg_type
),
3232 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3233 if (IS_ZEBRA_DEBUG_KERNEL
)
3235 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3236 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3237 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3241 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3243 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
3245 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3246 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3250 /* TODO: Only IPv4 supported now. */
3252 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3254 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3259 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3261 if (ndm
->ndm_state
& NUD_STALE
)
3262 local_inactive
= true;
3264 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3265 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3266 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3268 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3269 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3272 nfy_flags
= *(uint8_t *)RTA_DATA(
3273 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3274 if (nfy_flags
& FDB_NOTIFY_BIT
)
3276 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3277 local_inactive
= true;
3281 if (IS_ZEBRA_DEBUG_KERNEL
)
3282 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
3283 nl_msg_type_to_str(h
->nlmsg_type
),
3284 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
3285 ndm
->ndm_state
, ndm
->ndm_flags
, &mac
,
3286 dst_present
? dst_buf
: "", nhg_id
);
3288 /* The interface should exist. */
3289 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3291 if (!ifp
|| !ifp
->info
)
3294 /* The interface should be something we're interested in. */
3295 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3298 zif
= (struct zebra_if
*)ifp
->info
;
3299 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3300 if (IS_ZEBRA_DEBUG_KERNEL
)
3302 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3303 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3305 zif
->brslave_info
.bridge_ifindex
);
3309 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3311 if (filter_vlan
&& vid
!= filter_vlan
) {
3312 if (IS_ZEBRA_DEBUG_KERNEL
)
3313 zlog_debug(" Filtered due to filter vlan: %d",
3318 /* If add or update, do accordingly if learnt on a "local" interface; if
3319 * the notification is over VxLAN, this has to be related to
3321 * so perform an implicit delete of any local entry (if it exists).
3323 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3324 /* Drop "permanent" entries. */
3325 if (ndm
->ndm_state
& NUD_PERMANENT
) {
3326 if (IS_ZEBRA_DEBUG_KERNEL
)
3328 " Dropping entry because of NUD_PERMANENT");
3332 if (IS_ZEBRA_IF_VXLAN(ifp
))
3333 return zebra_vxlan_dp_network_mac_add(
3334 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
3335 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3337 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3338 sticky
, local_inactive
, dp_static
);
3341 /* This is a delete notification.
3342 * Ignore the notification with IP dest as it may just signify that the
3343 * MAC has moved from remote to local. The exception is the special
3344 * all-zeros MAC that represents the BUM flooding entry; we may have
3345 * to readd it. Otherwise,
3346 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3347 * 2. For a MAC over "local" interface, delete the mac
3348 * Note: We will get notifications from both bridge driver and VxLAN
3355 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
3357 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
3358 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
3362 if (IS_ZEBRA_IF_VXLAN(ifp
))
3363 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3365 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3368 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3373 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3376 /* Length validity. */
3377 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3381 /* We are interested only in AF_BRIDGE notifications. */
3382 ndm
= NLMSG_DATA(h
);
3383 if (ndm
->ndm_family
!= AF_BRIDGE
)
3386 return netlink_macfdb_change(h
, len
, ns_id
);
3389 /* Request for MAC FDB information from the kernel */
3390 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3391 int type
, ifindex_t master_ifindex
)
3395 struct ifinfomsg ifm
;
3399 /* Form the request, specifying filter (rtattr) if needed. */
3400 memset(&req
, 0, sizeof(req
));
3401 req
.n
.nlmsg_type
= type
;
3402 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3403 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3404 req
.ifm
.ifi_family
= family
;
3406 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3408 return netlink_request(netlink_cmd
, &req
);
3412 * MAC forwarding database read using netlink interface. This is invoked
3415 int netlink_macfdb_read(struct zebra_ns
*zns
)
3418 struct zebra_dplane_info dp_info
;
3420 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3422 /* Get bridge FDB table. */
3423 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3427 /* We are reading entire table. */
3429 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3436 * MAC forwarding database read using netlink interface. This is for a
3437 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3439 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3440 struct interface
*br_if
)
3442 struct zebra_if
*br_zif
;
3443 struct zebra_if
*zif
;
3444 struct zebra_l2info_vxlan
*vxl
;
3445 struct zebra_dplane_info dp_info
;
3448 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3450 /* Save VLAN we're filtering on, if needed. */
3451 br_zif
= (struct zebra_if
*)br_if
->info
;
3452 zif
= (struct zebra_if
*)ifp
->info
;
3453 vxl
= &zif
->l2info
.vxl
;
3454 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3455 filter_vlan
= vxl
->access_vlan
;
3457 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3459 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3463 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3466 /* Reset VLAN filter. */
3472 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3473 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3474 int family
, int type
,
3475 struct interface
*br_if
,
3476 const struct ethaddr
*mac
,
3484 struct zebra_if
*br_zif
;
3486 memset(&req
, 0, sizeof(req
));
3487 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3488 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3489 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3490 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3491 /* req.ndm.ndm_state = NUD_REACHABLE; */
3493 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3495 br_zif
= (struct zebra_if
*)br_if
->info
;
3496 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3497 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3499 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3501 if (IS_ZEBRA_DEBUG_KERNEL
)
3503 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
3504 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3505 br_if
->name
, br_if
->ifindex
,
3506 vrf_id_to_name(br_if
->vrf_id
), br_if
->vrf_id
, mac
, vid
);
3508 return netlink_request(&zns
->netlink_cmd
, &req
);
3511 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3512 struct interface
*br_if
,
3513 const struct ethaddr
*mac
, vlanid_t vid
)
3516 struct zebra_dplane_info dp_info
;
3518 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3520 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3522 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3528 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3535 * Netlink-specific handler for MAC updates using dataplane context object.
3537 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3540 struct ipaddr vtep_ip
;
3547 uint32_t update_flags
;
3549 uint8_t nfy_flags
= 0;
3550 int proto
= RTPROT_ZEBRA
;
3552 if (dplane_ctx_get_type(ctx
) != 0)
3553 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3555 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3556 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3559 state
= NUD_REACHABLE
;
3561 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3562 if (update_flags
& DPLANE_MAC_REMOTE
) {
3564 if (dplane_ctx_mac_is_sticky(ctx
)) {
3565 /* NUD_NOARP prevents the entry from expiring */
3567 /* sticky the entry from moving */
3568 flags
|= NTF_STICKY
;
3570 flags
|= NTF_EXT_LEARNED
;
3572 /* if it was static-local previously we need to clear the
3573 * notify flags on replace with remote
3575 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3579 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3580 nfy_flags
|= FDB_NOTIFY_BIT
;
3584 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3585 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3590 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3591 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3592 SET_IPADDR_V4(&vtep_ip
);
3594 if (IS_ZEBRA_DEBUG_KERNEL
) {
3596 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3598 vid
= dplane_ctx_mac_get_vlan(ctx
);
3600 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3605 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
3606 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3607 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3608 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3609 mac
, &vtep_ip
, nhg_id
,
3610 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3611 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3613 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3614 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3619 total
= netlink_neigh_update_msg_encode(
3620 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
3621 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
3622 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
3629 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3630 * 5549 support, re-install them.
3632 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3633 struct interface
*ifp
, struct ipaddr
*ip
,
3636 if (ndm
->ndm_family
!= AF_INET
)
3639 if (!zif
->v6_2_v4_ll_neigh_entry
)
3642 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3645 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3646 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3651 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3655 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3657 #define NUD_LOCAL_ACTIVE \
3658 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3660 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
3662 /* an exact match is done between
3663 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
3664 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
3665 * (see in lib/zclient.h)
3669 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3672 struct interface
*ifp
;
3673 struct zebra_if
*zif
;
3674 struct rtattr
*tb
[NDA_MAX
+ 1];
3675 struct interface
*link_if
;
3679 char buf
[ETHER_ADDR_STRLEN
];
3680 int mac_present
= 0;
3683 bool local_inactive
;
3684 uint32_t ext_flags
= 0;
3685 bool dp_static
= false;
3689 ndm
= NLMSG_DATA(h
);
3691 /* The interface should exist. */
3692 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3694 if (!ifp
|| !ifp
->info
)
3697 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
3698 zif
= (struct zebra_if
*)ifp
->info
;
3700 /* Parse attributes and extract fields of interest. */
3701 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3704 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3705 nl_msg_type_to_str(h
->nlmsg_type
),
3706 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3707 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
);
3711 memset(&ip
, 0, sizeof(struct ipaddr
));
3712 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3713 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3715 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3716 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3717 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3718 if (IS_ZEBRA_DEBUG_KERNEL
)
3720 " Neighbor Entry Received is a 5549 entry, finished");
3724 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3725 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3726 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3728 /* we send link layer information to client:
3729 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
3730 * - struct ipaddr ( for DEL and GET)
3731 * - struct ethaddr mac; (for NEW)
3733 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
3734 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
3735 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
3736 cmd
= ZEBRA_NHRP_NEIGH_GET
;
3737 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
3738 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
3740 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
3744 if (tb
[NDA_LLADDR
]) {
3745 /* copy LLADDR information */
3746 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
3747 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
3749 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
3750 union sockunion link_layer_ipv4
;
3753 sockunion_family(&link_layer_ipv4
) = AF_INET
;
3754 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
3757 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
3758 zsend_nhrp_neighbor_notify(
3760 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
3764 if (h
->nlmsg_type
== RTM_GETNEIGH
)
3767 /* The neighbor is present on an SVI. From this, we locate the
3769 * bridge because we're only interested in neighbors on a VxLAN bridge.
3770 * The bridge is located based on the nature of the SVI:
3771 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3773 * and is linked to the bridge
3774 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3778 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3779 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3783 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3786 if (IS_ZEBRA_DEBUG_KERNEL
)
3788 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3792 memset(&mac
, 0, sizeof(struct ethaddr
));
3793 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3794 if (tb
[NDA_LLADDR
]) {
3795 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3796 if (IS_ZEBRA_DEBUG_KERNEL
)
3798 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3803 ifp
->name
, ndm
->ndm_ifindex
,
3804 VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3805 (unsigned long)RTA_PAYLOAD(
3811 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3814 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3815 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3817 if (tb
[NDA_EXT_FLAGS
]) {
3818 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3819 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3823 if (IS_ZEBRA_DEBUG_KERNEL
)
3825 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3826 nl_msg_type_to_str(h
->nlmsg_type
),
3827 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3828 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3831 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3833 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3835 /* If the neighbor state is valid for use, process as an add or
3837 * else process as a delete. Note that the delete handling may
3839 * in re-adding the neighbor if it is a valid "remote" neighbor.
3841 if (ndm
->ndm_state
& NUD_VALID
) {
3842 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3844 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3846 /* If EVPN-MH is not enabled we treat STALE
3847 * neighbors as locally-active and advertise
3850 local_inactive
= false;
3852 return zebra_vxlan_handle_kernel_neigh_update(
3853 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3854 is_router
, local_inactive
, dp_static
);
3857 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3860 if (IS_ZEBRA_DEBUG_KERNEL
)
3861 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
3862 nl_msg_type_to_str(h
->nlmsg_type
),
3863 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3864 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3867 /* Process the delete - it may result in re-adding the neighbor if it is
3868 * a valid "remote" neighbor.
3870 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3873 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3878 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3881 /* Length validity. */
3882 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3886 /* We are interested only in AF_INET or AF_INET6 notifications. */
3887 ndm
= NLMSG_DATA(h
);
3888 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3891 return netlink_neigh_change(h
, len
);
3894 /* Request for IP neighbor information from the kernel */
3895 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3896 int type
, ifindex_t ifindex
)
3904 /* Form the request, specifying filter (rtattr) if needed. */
3905 memset(&req
, 0, sizeof(req
));
3906 req
.n
.nlmsg_type
= type
;
3907 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3908 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3909 req
.ndm
.ndm_family
= family
;
3911 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3913 return netlink_request(netlink_cmd
, &req
);
3917 * IP Neighbor table read using netlink interface. This is invoked
3920 int netlink_neigh_read(struct zebra_ns
*zns
)
3923 struct zebra_dplane_info dp_info
;
3925 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3927 /* Get IP neighbor table. */
3928 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3932 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3939 * IP Neighbor table read using netlink interface. This is for a specific
3942 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3945 struct zebra_dplane_info dp_info
;
3947 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3949 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3953 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3960 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3961 * read using netlink interface.
3963 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3965 const struct ipaddr
*ip
,
3975 /* Form the request, specifying filter (rtattr) if needed. */
3976 memset(&req
, 0, sizeof(req
));
3977 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3978 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3979 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3980 req
.ndm
.ndm_ifindex
= ifindex
;
3982 if (IS_IPADDR_V4(ip
)) {
3983 ipa_len
= IPV4_MAX_BYTELEN
;
3984 req
.ndm
.ndm_family
= AF_INET
;
3987 ipa_len
= IPV6_MAX_BYTELEN
;
3988 req
.ndm
.ndm_family
= AF_INET6
;
3991 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3993 if (IS_ZEBRA_DEBUG_KERNEL
)
3994 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
3995 __func__
, nl_msg_type_to_str(type
),
3996 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
3999 return netlink_request(&zns
->netlink_cmd
, &req
);
4002 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4003 struct interface
*vlan_if
)
4006 struct zebra_ns
*zns
;
4007 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
4008 struct zebra_dplane_info dp_info
;
4012 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4014 if (IS_ZEBRA_DEBUG_KERNEL
)
4015 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4016 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4017 vrf_id_to_name(vlan_if
->vrf_id
), vlan_if
->vrf_id
);
4019 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4024 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4030 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4035 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4036 || h
->nlmsg_type
== RTM_GETNEIGH
))
4039 /* Length validity. */
4040 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4043 "%s: Message received from netlink is of a broken size %d %zu",
4044 __func__
, h
->nlmsg_len
,
4045 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4049 /* Is this a notification for the MAC FDB or IP neighbor table? */
4050 ndm
= NLMSG_DATA(h
);
4051 if (ndm
->ndm_family
== AF_BRIDGE
)
4052 return netlink_macfdb_change(h
, len
, ns_id
);
4054 if (ndm
->ndm_type
!= RTN_UNICAST
)
4057 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4058 return netlink_ipneigh_change(h
, len
, ns_id
);
4061 EC_ZEBRA_UNKNOWN_FAMILY
,
4062 "Invalid address family: %u received from kernel neighbor change: %s",
4063 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4071 * Utility neighbor-update function, using info from dplane context.
4073 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4074 int cmd
, void *buf
, size_t buflen
)
4076 const struct ipaddr
*ip
;
4077 const struct ethaddr
*mac
= NULL
;
4078 const struct ipaddr
*link_ip
= NULL
;
4079 const void *link_ptr
= NULL
;
4080 char buf2
[ETHER_ADDR_STRLEN
];
4086 uint32_t update_flags
;
4087 uint32_t ext_flags
= 0;
4089 int proto
= RTPROT_ZEBRA
;
4091 if (dplane_ctx_get_type(ctx
) != 0)
4092 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4094 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4096 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4097 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4098 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4099 llalen
= IPADDRSZ(link_ip
);
4100 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4101 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4103 mac
= dplane_ctx_neigh_get_mac(ctx
);
4105 link_ptr
= (const void *)mac
;
4106 if (is_zero_mac(mac
))
4109 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4111 snprintf(buf2
, sizeof(buf2
), "null");
4113 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4114 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4115 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4117 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4119 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4120 flags
|= NTF_EXT_LEARNED
;
4121 /* if it was static-local previously we need to clear the
4122 * ext flags on replace with remote
4124 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4126 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4129 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4130 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4132 if (IS_ZEBRA_DEBUG_KERNEL
)
4134 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4135 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4136 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4137 ip
, link_ip
? "Link " : "MAC ", buf2
, flags
, state
,
4138 ext
? "ext " : "", ext_flags
);
4140 return netlink_neigh_update_msg_encode(
4141 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4142 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4143 ext_flags
, buf
, buflen
, proto
);
4146 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4147 void *data
, size_t datalen
)
4154 struct rtattr
*nest
;
4159 if (datalen
< sizeof(*req
))
4161 memset(req
, 0, sizeof(*req
));
4162 family
= dplane_ctx_neightable_get_family(ctx
);
4163 idx
= dplane_ctx_get_ifindex(ctx
);
4165 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4166 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4167 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4168 req
->ndtm
.ndtm_family
= family
;
4170 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4171 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4172 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4175 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4177 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4178 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4180 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4181 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4184 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4185 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4188 nl_attr_nest_end(&req
->n
, nest
);
4190 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4193 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4194 void *buf
, size_t buflen
)
4198 switch (dplane_ctx_get_op(ctx
)) {
4199 case DPLANE_OP_NEIGH_INSTALL
:
4200 case DPLANE_OP_NEIGH_UPDATE
:
4201 case DPLANE_OP_NEIGH_DISCOVER
:
4202 case DPLANE_OP_NEIGH_IP_INSTALL
:
4203 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4205 case DPLANE_OP_NEIGH_DELETE
:
4206 case DPLANE_OP_NEIGH_IP_DELETE
:
4207 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4209 case DPLANE_OP_VTEP_ADD
:
4210 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4213 case DPLANE_OP_VTEP_DELETE
:
4214 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4217 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4218 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4228 * Update MAC, using dataplane context object.
4231 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4232 struct zebra_dplane_ctx
*ctx
)
4234 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4238 enum netlink_msg_status
4239 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4241 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4246 * MPLS label forwarding table change via netlink interface, using dataplane
4247 * context information.
4249 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4250 void *buf
, size_t buflen
)
4253 const struct nhlfe_list_head
*head
;
4254 const struct zebra_nhlfe
*nhlfe
;
4255 struct nexthop
*nexthop
= NULL
;
4256 unsigned int nexthop_num
;
4257 const char *routedesc
;
4259 struct prefix p
= {0};
4267 if (buflen
< sizeof(*req
))
4270 memset(req
, 0, sizeof(*req
));
4273 * Count # nexthops so we can decide whether to use singlepath
4274 * or multipath case.
4277 head
= dplane_ctx_get_nhlfe_list(ctx
);
4278 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4279 nexthop
= nhlfe
->nexthop
;
4282 if (cmd
== RTM_NEWROUTE
) {
4283 /* Count all selected NHLFEs */
4284 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4285 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4288 /* Count all installed NHLFEs */
4289 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4290 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4295 if ((nexthop_num
== 0) ||
4296 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4299 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4300 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4301 req
->n
.nlmsg_type
= cmd
;
4302 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
4304 req
->r
.rtm_family
= AF_MPLS
;
4305 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4306 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4307 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4308 req
->r
.rtm_type
= RTN_UNICAST
;
4310 if (cmd
== RTM_NEWROUTE
) {
4311 /* We do a replace to handle update. */
4312 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4314 /* set the protocol value if installing */
4315 route_type
= re_type_from_lsp_type(
4316 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4317 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4320 /* Fill destination */
4321 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4322 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4325 /* Fill nexthops (paths) based on single-path or multipath. The paths
4326 * chosen depend on the operation.
4328 if (nexthop_num
== 1) {
4329 routedesc
= "single-path";
4330 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4334 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4335 nexthop
= nhlfe
->nexthop
;
4339 if ((cmd
== RTM_NEWROUTE
4340 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4341 && CHECK_FLAG(nexthop
->flags
,
4342 NEXTHOP_FLAG_ACTIVE
)))
4343 || (cmd
== RTM_DELROUTE
4344 && (CHECK_FLAG(nhlfe
->flags
,
4345 NHLFE_FLAG_INSTALLED
)
4346 && CHECK_FLAG(nexthop
->flags
,
4347 NEXTHOP_FLAG_FIB
)))) {
4348 /* Add the gateway */
4349 if (!_netlink_mpls_build_singlepath(
4350 &p
, routedesc
, nhlfe
, &req
->n
,
4351 &req
->r
, buflen
, cmd
))
4358 } else { /* Multipath case */
4359 struct rtattr
*nest
;
4360 const union g_addr
*src1
= NULL
;
4362 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4366 routedesc
= "multipath";
4367 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4371 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4372 nexthop
= nhlfe
->nexthop
;
4376 if ((cmd
== RTM_NEWROUTE
4377 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4378 && CHECK_FLAG(nexthop
->flags
,
4379 NEXTHOP_FLAG_ACTIVE
)))
4380 || (cmd
== RTM_DELROUTE
4381 && (CHECK_FLAG(nhlfe
->flags
,
4382 NHLFE_FLAG_INSTALLED
)
4383 && CHECK_FLAG(nexthop
->flags
,
4384 NEXTHOP_FLAG_FIB
)))) {
4387 /* Build the multipath */
4388 if (!_netlink_mpls_build_multipath(
4389 &p
, routedesc
, nhlfe
, &req
->n
,
4390 buflen
, &req
->r
, &src1
))
4395 /* Add the multipath */
4396 nl_attr_nest_end(&req
->n
, nest
);
4399 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4402 /****************************************************************************
4403 * This code was developed in a branch that didn't have dplane APIs for
4404 * MAC updates. Hence the use of the legacy style. It will be moved to
4405 * the new dplane style pre-merge to master. XXX
4407 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4414 int cmd
= RTM_NEWNEXTHOP
;
4415 struct zebra_vrf
*zvrf
;
4416 struct zebra_ns
*zns
;
4418 zvrf
= zebra_vrf_get_evpn();
4423 memset(&req
, 0, sizeof(req
));
4425 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4426 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4427 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4428 req
.n
.nlmsg_type
= cmd
;
4429 req
.nhm
.nh_family
= AF_INET
;
4431 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4433 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4435 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4436 &vtep_ip
, IPV4_MAX_BYTELEN
))
4439 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4440 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4441 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4444 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4448 static int netlink_fdb_nh_del(uint32_t nh_id
)
4455 int cmd
= RTM_DELNEXTHOP
;
4456 struct zebra_vrf
*zvrf
;
4457 struct zebra_ns
*zns
;
4459 zvrf
= zebra_vrf_get_evpn();
4464 memset(&req
, 0, sizeof(req
));
4466 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4467 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4468 req
.n
.nlmsg_type
= cmd
;
4469 req
.nhm
.nh_family
= AF_UNSPEC
;
4471 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4474 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4475 zlog_debug("Tx %s fdb-nh 0x%x",
4476 nl_msg_type_to_str(cmd
), nh_id
);
4479 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4483 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4484 struct nh_grp
*nh_ids
)
4491 int cmd
= RTM_NEWNEXTHOP
;
4492 struct zebra_vrf
*zvrf
;
4493 struct zebra_ns
*zns
;
4494 struct nexthop_grp grp
[nh_cnt
];
4497 zvrf
= zebra_vrf_get_evpn();
4502 memset(&req
, 0, sizeof(req
));
4504 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4505 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4506 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4507 req
.n
.nlmsg_type
= cmd
;
4508 req
.nhm
.nh_family
= AF_UNSPEC
;
4510 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4512 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4514 memset(&grp
, 0, sizeof(grp
));
4515 for (i
= 0; i
< nh_cnt
; ++i
) {
4516 grp
[i
].id
= nh_ids
[i
].id
;
4517 grp
[i
].weight
= nh_ids
[i
].weight
;
4519 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4520 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4524 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4525 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4529 for (i
= 0; i
< nh_cnt
; ++i
) {
4530 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4532 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4535 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4536 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4539 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4543 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4545 return netlink_fdb_nh_del(nhg_id
);
4548 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4550 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4553 int kernel_del_mac_nh(uint32_t nh_id
)
4555 return netlink_fdb_nh_del(nh_id
);
4558 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4559 struct nh_grp
*nh_ids
)
4561 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4564 int kernel_del_mac_nhg(uint32_t nhg_id
)
4566 return netlink_fdb_nhg_del(nhg_id
);
4569 #endif /* HAVE_NETLINK */