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 #include <net/if_arp.h>
26 #include <linux/lwtunnel.h>
27 #include <linux/mpls_iptunnel.h>
28 #include <linux/neighbour.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/nexthop.h>
32 /* Hack for GNU libc version 2. */
34 #define MSG_TRUNC 0x20
35 #endif /* MSG_TRUNC */
41 #include "connected.h"
44 #include "zebra_memory.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_ns.h"
57 #include "zebra/zebra_vrf.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/interface.h"
61 #include "zebra/debug.h"
62 #include "zebra/rtadv.h"
63 #include "zebra/zebra_ptm.h"
64 #include "zebra/zebra_mpls.h"
65 #include "zebra/kernel_netlink.h"
66 #include "zebra/rt_netlink.h"
67 #include "zebra/zebra_nhg.h"
68 #include "zebra/zebra_mroute.h"
69 #include "zebra/zebra_vxlan.h"
70 #include "zebra/zebra_errors.h"
71 #include "zebra/zebra_evpn_mh.h"
77 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
78 * UAPI for MAC sync. */
79 #ifndef _UAPI_LINUX_IF_BRIDGE_H
80 #define BR_SPH_LIST_SIZE 10
83 static vlanid_t filter_vlan
= 0;
85 /* We capture whether the current kernel supports nexthop ids; by
86 * default, we'll use them if possible. There's also a configuration
87 * available to _disable_ use of kernel nexthops.
89 static bool supports_nh
;
97 static const char ipv4_ll_buf
[16] = "169.254.0.1";
98 static struct in_addr ipv4_ll
;
100 /* Is this a ipv4 over ipv6 route? */
101 static bool is_route_v4_over_v6(unsigned char rtm_family
,
102 enum nexthop_types_t nexthop_type
)
104 if (rtm_family
== AF_INET
105 && (nexthop_type
== NEXTHOP_TYPE_IPV6
106 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
112 /* Helper to control use of kernel-level nexthop ids */
113 static bool kernel_nexthops_supported(void)
115 return (supports_nh
&& !vrf_is_backend_netns()
116 && zebra_nhg_kernel_nexthops_enabled());
120 * Some people may only want to use NHGs created by protos and not
121 * implicitly created by Zebra. This check accounts for that.
123 static bool proto_nexthops_only(void)
125 return zebra_nhg_proto_nexthops_only();
128 /* Is this a proto created NHG? */
129 static bool is_proto_nhg(uint32_t id
, int type
)
131 /* If type is available, use it as the source of truth */
133 if (type
!= ZEBRA_ROUTE_NHG
)
138 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
145 * The ipv4_ll data structure is used for all 5549
146 * additions to the kernel. Let's figure out the
147 * correct value one time instead for every
148 * install/remove of a 5549 type route
150 void rt_netlink_init(void)
152 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
156 * Mapping from dataplane neighbor flags to netlink flags
158 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
162 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
163 flags
|= NTF_EXT_LEARNED
;
164 if (dplane_flags
& DPLANE_NTF_ROUTER
)
166 if (dplane_flags
& DPLANE_NTF_USE
)
173 * Mapping from dataplane neighbor state to netlink state
175 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
179 if (dplane_state
& DPLANE_NUD_REACHABLE
)
180 state
|= NUD_REACHABLE
;
181 if (dplane_state
& DPLANE_NUD_STALE
)
183 if (dplane_state
& DPLANE_NUD_NOARP
)
185 if (dplane_state
& DPLANE_NUD_PROBE
)
187 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
188 state
|= NUD_INCOMPLETE
;
194 static inline bool is_selfroute(int proto
)
196 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
197 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
198 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
199 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
200 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
201 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
202 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
203 || (proto
== RTPROT_SRTE
)) {
210 static inline int zebra2proto(int proto
)
213 case ZEBRA_ROUTE_BABEL
:
214 proto
= RTPROT_BABEL
;
216 case ZEBRA_ROUTE_BGP
:
219 case ZEBRA_ROUTE_OSPF
:
220 case ZEBRA_ROUTE_OSPF6
:
223 case ZEBRA_ROUTE_STATIC
:
224 proto
= RTPROT_ZSTATIC
;
226 case ZEBRA_ROUTE_ISIS
:
229 case ZEBRA_ROUTE_RIP
:
232 case ZEBRA_ROUTE_RIPNG
:
233 proto
= RTPROT_RIPNG
;
235 case ZEBRA_ROUTE_NHRP
:
238 case ZEBRA_ROUTE_EIGRP
:
239 proto
= RTPROT_EIGRP
;
241 case ZEBRA_ROUTE_LDP
:
244 case ZEBRA_ROUTE_SHARP
:
245 proto
= RTPROT_SHARP
;
247 case ZEBRA_ROUTE_PBR
:
250 case ZEBRA_ROUTE_OPENFABRIC
:
251 proto
= RTPROT_OPENFABRIC
;
253 case ZEBRA_ROUTE_SRTE
:
256 case ZEBRA_ROUTE_TABLE
:
257 case ZEBRA_ROUTE_NHG
:
258 proto
= RTPROT_ZEBRA
;
260 case ZEBRA_ROUTE_CONNECT
:
261 case ZEBRA_ROUTE_KERNEL
:
262 proto
= RTPROT_KERNEL
;
266 * When a user adds a new protocol this will show up
267 * to let them know to do something about it. This
268 * is intentionally a warn because we should see
269 * this as part of development of a new protocol
272 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
274 proto
= RTPROT_ZEBRA
;
281 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
285 proto
= ZEBRA_ROUTE_BABEL
;
288 proto
= ZEBRA_ROUTE_BGP
;
291 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
295 proto
= ZEBRA_ROUTE_ISIS
;
298 proto
= ZEBRA_ROUTE_RIP
;
301 proto
= ZEBRA_ROUTE_RIPNG
;
304 proto
= ZEBRA_ROUTE_NHRP
;
307 proto
= ZEBRA_ROUTE_EIGRP
;
310 proto
= ZEBRA_ROUTE_LDP
;
314 proto
= ZEBRA_ROUTE_STATIC
;
317 proto
= ZEBRA_ROUTE_SHARP
;
320 proto
= ZEBRA_ROUTE_PBR
;
322 case RTPROT_OPENFABRIC
:
323 proto
= ZEBRA_ROUTE_OPENFABRIC
;
326 proto
= ZEBRA_ROUTE_SRTE
;
330 proto
= ZEBRA_ROUTE_NHG
;
333 /* Intentional fall thru */
336 * When a user adds a new protocol this will show up
337 * to let them know to do something about it. This
338 * is intentionally a warn because we should see
339 * this as part of development of a new protocol
342 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
344 proto
= ZEBRA_ROUTE_KERNEL
;
351 Pending: create an efficient table_id (in a tree/hash) based lookup)
353 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
356 struct zebra_vrf
*zvrf
;
358 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
362 /* case vrf with netns : match the netnsid */
363 if (vrf_is_backend_netns()) {
364 if (ns_id
== zvrf_id(zvrf
))
365 return zvrf_id(zvrf
);
367 /* VRF is VRF_BACKEND_VRF_LITE */
368 if (zvrf
->table_id
!= table_id
)
370 return zvrf_id(zvrf
);
378 * @parse_encap_mpls() - Parses encapsulated mpls attributes
379 * @tb: Pointer to rtattr to look for nested items in.
380 * @labels: Pointer to store labels in.
382 * Return: Number of mpls labels found.
384 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
386 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
387 mpls_lse_t
*lses
= NULL
;
392 mpls_label_t label
= 0;
394 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
395 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
396 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
397 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
398 labels
[num_labels
++] = label
;
404 static struct nexthop
405 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
406 enum blackhole_type bh_type
, int index
, void *prefsrc
,
407 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
409 struct interface
*ifp
= NULL
;
410 struct nexthop nh
= {0};
411 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
414 vrf_id_t nh_vrf_id
= vrf_id
;
415 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
417 if (bh_type
== BLACKHOLE_UNSPEC
) {
419 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
420 else if (index
&& gate
)
421 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
422 : NEXTHOP_TYPE_IPV6_IFINDEX
;
423 else if (!index
&& gate
)
424 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
427 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
428 nh
.bh_type
= bh_type
;
431 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
432 nh
.bh_type
= bh_type
;
436 memcpy(&nh
.src
, prefsrc
, sz
);
438 memcpy(&nh
.gate
, gate
, sz
);
441 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
443 nh_vrf_id
= ifp
->vrf_id
;
445 nh
.vrf_id
= nh_vrf_id
;
447 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
448 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
449 == LWTUNNEL_ENCAP_MPLS
) {
450 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
453 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
454 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
457 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
462 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
463 struct nexthop_group
*ng
,
465 struct rtnexthop
*rtnh
,
467 void *prefsrc
, vrf_id_t vrf_id
)
470 struct interface
*ifp
= NULL
;
473 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
475 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
477 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
478 vrf_id_t nh_vrf_id
= vrf_id
;
481 struct nexthop
*nh
= NULL
;
483 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
486 index
= rtnh
->rtnh_ifindex
;
489 * Yes we are looking this up
490 * for every nexthop and just
491 * using the last one looked
494 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
497 nh_vrf_id
= ifp
->vrf_id
;
500 EC_ZEBRA_UNKNOWN_INTERFACE
,
501 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
503 nh_vrf_id
= VRF_DEFAULT
;
508 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
509 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
511 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
512 rtnh
->rtnh_len
- sizeof(*rtnh
));
513 if (rtnh_tb
[RTA_GATEWAY
])
514 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
515 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
516 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
517 == LWTUNNEL_ENCAP_MPLS
) {
518 num_labels
= parse_encap_mpls(
519 rtnh_tb
[RTA_ENCAP
], labels
);
523 if (gate
&& rtm
->rtm_family
== AF_INET
) {
525 nh
= nexthop_from_ipv4_ifindex(
526 gate
, prefsrc
, index
, nh_vrf_id
);
528 nh
= nexthop_from_ipv4(gate
, prefsrc
,
530 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
532 nh
= nexthop_from_ipv6_ifindex(
533 gate
, index
, nh_vrf_id
);
535 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
537 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
540 nh
->weight
= rtnh
->rtnh_hops
+ 1;
543 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
546 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
547 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
549 /* Add to temporary list */
550 nexthop_group_add_sorted(ng
, nh
);
553 if (rtnh
->rtnh_len
== 0)
556 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
557 rtnh
= RTNH_NEXT(rtnh
);
560 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
565 /* Looking up routing table by netlink interface. */
566 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
571 struct rtattr
*tb
[RTA_MAX
+ 1];
574 struct prefix_ipv6 src_p
= {};
578 char anyaddr
[16] = {0};
580 int proto
= ZEBRA_ROUTE_KERNEL
;
585 uint8_t distance
= 0;
591 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
592 void *src
= NULL
; /* IPv6 srcdest source prefix */
593 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
597 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
599 switch (rtm
->rtm_type
) {
603 bh_type
= BLACKHOLE_NULL
;
605 case RTN_UNREACHABLE
:
606 bh_type
= BLACKHOLE_REJECT
;
609 bh_type
= BLACKHOLE_ADMINPROHIB
;
612 if (IS_ZEBRA_DEBUG_KERNEL
)
613 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
614 nl_rttype_to_str(rtm
->rtm_type
),
619 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
622 "%s: Message received from netlink is of a broken size %d %zu",
623 __func__
, h
->nlmsg_len
,
624 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
628 memset(tb
, 0, sizeof(tb
));
629 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
631 if (rtm
->rtm_flags
& RTM_F_CLONED
)
633 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
635 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
638 selfroute
= is_selfroute(rtm
->rtm_protocol
);
640 if (!startup
&& selfroute
641 && h
->nlmsg_type
== RTM_NEWROUTE
642 && !zrouter
.asic_offloaded
) {
643 if (IS_ZEBRA_DEBUG_KERNEL
)
644 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
649 /* We don't care about change notifications for the MPLS table. */
650 /* TODO: Revisit this. */
651 if (rtm
->rtm_family
== AF_MPLS
)
654 /* Table corresponding to route. */
656 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
658 table
= rtm
->rtm_table
;
661 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
662 if (vrf_id
== VRF_DEFAULT
) {
663 if (!is_zebra_valid_kernel_table(table
)
664 && !is_zebra_main_routing_table(table
))
668 if (rtm
->rtm_flags
& RTM_F_TRAP
)
669 flags
|= ZEBRA_FLAG_TRAPPED
;
670 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
671 flags
|= ZEBRA_FLAG_OFFLOADED
;
672 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
673 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
675 /* Route which inserted by Zebra. */
677 flags
|= ZEBRA_FLAG_SELFROUTE
;
678 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
681 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
684 dest
= RTA_DATA(tb
[RTA_DST
]);
689 src
= RTA_DATA(tb
[RTA_SRC
]);
694 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
697 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
700 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
702 if (tb
[RTA_PRIORITY
])
703 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
705 #if defined(SUPPORT_REALMS)
707 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
710 if (tb
[RTA_METRICS
]) {
711 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
713 memset(mxrta
, 0, sizeof(mxrta
));
714 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
715 RTA_PAYLOAD(tb
[RTA_METRICS
]));
718 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
721 if (rtm
->rtm_family
== AF_INET
) {
723 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
725 "Invalid destination prefix length: %u received from kernel route change",
729 memcpy(&p
.u
.prefix4
, dest
, 4);
730 p
.prefixlen
= rtm
->rtm_dst_len
;
732 if (rtm
->rtm_src_len
!= 0) {
734 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
735 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
740 /* Force debug below to not display anything for source */
742 } else if (rtm
->rtm_family
== AF_INET6
) {
744 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
746 "Invalid destination prefix length: %u received from kernel route change",
750 memcpy(&p
.u
.prefix6
, dest
, 16);
751 p
.prefixlen
= rtm
->rtm_dst_len
;
753 src_p
.family
= AF_INET6
;
754 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
756 "Invalid source prefix length: %u received from kernel route change",
760 memcpy(&src_p
.prefix
, src
, 16);
761 src_p
.prefixlen
= rtm
->rtm_src_len
;
765 * For ZEBRA_ROUTE_KERNEL types:
767 * The metric/priority of the route received from the kernel
768 * is a 32 bit number. We are going to interpret the high
769 * order byte as the Admin Distance and the low order 3 bytes
772 * This will allow us to do two things:
773 * 1) Allow the creation of kernel routes that can be
774 * overridden by zebra.
775 * 2) Allow the old behavior for 'most' kernel route types
776 * if a user enters 'ip route ...' v4 routes get a metric
777 * of 0 and v6 routes get a metric of 1024. Both of these
778 * values will end up with a admin distance of 0, which
779 * will cause them to win for the purposes of zebra.
781 if (proto
== ZEBRA_ROUTE_KERNEL
) {
782 distance
= (metric
>> 24) & 0xFF;
783 metric
= (metric
& 0x00FFFFFF);
786 if (IS_ZEBRA_DEBUG_KERNEL
) {
787 char buf2
[PREFIX_STRLEN
];
790 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
791 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
792 src_p
.prefixlen
? " from " : "",
793 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
795 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
800 if (rtm
->rtm_family
== AF_INET6
)
803 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
805 if (!tb
[RTA_MULTIPATH
]) {
806 struct nexthop nh
= {0};
809 nh
= parse_nexthop_unicast(
810 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
813 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
814 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
817 /* This is a multipath route */
818 struct route_entry
*re
;
819 struct nexthop_group
*ng
= NULL
;
820 struct rtnexthop
*rtnh
=
821 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
823 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
825 re
->distance
= distance
;
831 re
->uptime
= monotime(NULL
);
838 /* Use temporary list of nexthops; parse
839 * message payload's nexthops.
841 ng
= nexthop_group_new();
843 parse_multipath_nexthops_unicast(
844 ns_id
, ng
, rtm
, rtnh
, tb
,
847 zserv_nexthop_num_warn(
848 __func__
, (const struct prefix
*)&p
,
852 nexthop_group_delete(&ng
);
858 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
865 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
866 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
869 if (!tb
[RTA_MULTIPATH
]) {
872 nh
= parse_nexthop_unicast(
873 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
875 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
876 flags
, &p
, &src_p
, &nh
, 0, table
,
877 metric
, distance
, true);
879 /* XXX: need to compare the entire list of
880 * nexthops here for NLM_F_APPEND stupidity */
881 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
882 flags
, &p
, &src_p
, NULL
, 0, table
,
883 metric
, distance
, true);
891 static struct mcast_route_data
*mroute
= NULL
;
893 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
894 ns_id_t ns_id
, int startup
)
898 struct rtattr
*tb
[RTA_MAX
+ 1];
899 struct mcast_route_data
*m
;
900 struct mcast_route_data mr
;
905 char oif_list
[256] = "\0";
912 memset(&mr
, 0, sizeof(mr
));
918 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
920 memset(tb
, 0, sizeof(tb
));
921 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
924 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
926 table
= rtm
->rtm_table
;
928 vrf
= vrf_lookup_by_table(table
, ns_id
);
931 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
934 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
937 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
940 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
942 if (tb
[RTA_MULTIPATH
]) {
943 struct rtnexthop
*rtnh
=
944 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
946 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
948 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
951 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
954 if (rtnh
->rtnh_len
== 0)
957 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
958 rtnh
= RTNH_NEXT(rtnh
);
962 if (IS_ZEBRA_DEBUG_KERNEL
) {
963 struct interface
*ifp
= NULL
;
964 struct zebra_vrf
*zvrf
= NULL
;
966 for (count
= 0; count
< oif_count
; count
++) {
967 ifp
= if_lookup_by_index(oif
[count
], vrf
);
970 snprintf(temp
, sizeof(temp
), "%s(%d) ",
971 ifp
? ifp
->name
: "Unknown", oif
[count
]);
972 strlcat(oif_list
, temp
, sizeof(oif_list
));
974 zvrf
= zebra_vrf_lookup_by_id(vrf
);
975 ifp
= if_lookup_by_index(iif
, vrf
);
977 "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld",
978 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
979 &m
->sg
.src
, &m
->sg
.grp
, ifp
? ifp
->name
: "Unknown",
986 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
993 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
994 /* If this is not route add/delete message print warning. */
995 zlog_debug("Kernel message: %s NS %u",
996 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1000 if (!(rtm
->rtm_family
== AF_INET
||
1001 rtm
->rtm_family
== AF_INET6
||
1002 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1004 EC_ZEBRA_UNKNOWN_FAMILY
,
1005 "Invalid address family: %u received from kernel route change: %s",
1006 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1010 /* Connected route. */
1011 if (IS_ZEBRA_DEBUG_KERNEL
)
1012 zlog_debug("%s %s %s proto %s NS %u",
1013 nl_msg_type_to_str(h
->nlmsg_type
),
1014 nl_family_to_str(rtm
->rtm_family
),
1015 nl_rttype_to_str(rtm
->rtm_type
),
1016 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1019 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1022 "%s: Message received from netlink is of a broken size: %d %zu",
1023 __func__
, h
->nlmsg_len
,
1024 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1028 if (rtm
->rtm_type
== RTN_MULTICAST
)
1029 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1031 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1035 /* Request for specific route information from the kernel */
1036 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1043 /* Form the request, specifying filter (rtattr) if needed. */
1044 memset(&req
, 0, sizeof(req
));
1045 req
.n
.nlmsg_type
= type
;
1046 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1047 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1048 req
.rtm
.rtm_family
= family
;
1050 return netlink_request(&zns
->netlink_cmd
, &req
);
1053 /* Routing table read function using netlink interface. Only called
1055 int netlink_route_read(struct zebra_ns
*zns
)
1058 struct zebra_dplane_info dp_info
;
1060 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1062 /* Get IPv4 routing table. */
1063 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1066 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1067 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1071 /* Get IPv6 routing table. */
1072 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1075 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1076 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1084 * The function returns true if the gateway info could be added
1085 * to the message, otherwise false is returned.
1087 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1089 struct nlmsghdr
*nlmsg
,
1090 size_t req_size
, int bytelen
,
1091 const struct nexthop
*nexthop
)
1093 if (route_family
== AF_MPLS
) {
1094 struct gw_family_t gw_fam
;
1096 gw_fam
.family
= gw_family
;
1097 if (gw_family
== AF_INET
)
1098 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1100 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1101 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1105 if (!(nexthop
->rparent
1106 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1107 if (gw_family
== AF_INET
) {
1108 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1109 &nexthop
->gate
.ipv4
, bytelen
))
1112 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1113 &nexthop
->gate
.ipv6
, bytelen
))
1122 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1123 mpls_lse_t
*out_lse
, char *label_buf
,
1124 size_t label_buf_size
)
1126 char label_buf1
[20];
1129 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1130 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1133 if (IS_ZEBRA_DEBUG_KERNEL
) {
1135 snprintf(label_buf
, label_buf_size
, "label %u",
1136 nh_label
->label
[i
]);
1138 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1139 nh_label
->label
[i
]);
1140 strlcat(label_buf
, label_buf1
, label_buf_size
);
1144 out_lse
[num_labels
] =
1145 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1152 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1153 struct nlmsghdr
*nlmsg
,
1154 size_t buflen
, struct rtmsg
*rtmsg
,
1156 size_t label_buf_size
)
1158 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1162 * label_buf is *only* currently used within debugging.
1163 * As such when we assign it we are guarding it inside
1164 * a debug test. If you want to change this make sure
1165 * you fix this assumption
1167 label_buf
[0] = '\0';
1170 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1173 /* Set the BoS bit */
1174 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1176 if (rtmsg
->rtm_family
== AF_MPLS
) {
1177 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1178 num_labels
* sizeof(mpls_lse_t
)))
1181 struct rtattr
*nest
;
1183 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1184 LWTUNNEL_ENCAP_MPLS
))
1187 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1191 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1193 num_labels
* sizeof(mpls_lse_t
)))
1195 nl_attr_nest_end(nlmsg
, nest
);
1202 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1204 struct nlmsghdr
*nlmsg
,
1205 size_t buflen
, int bytelen
)
1207 if (family
== AF_INET
) {
1208 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1209 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1210 &nexthop
->rmap_src
.ipv4
, bytelen
))
1212 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1213 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1214 &nexthop
->src
.ipv4
, bytelen
))
1217 } else if (family
== AF_INET6
) {
1218 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1219 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1220 &nexthop
->rmap_src
.ipv6
, bytelen
))
1222 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1223 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1224 &nexthop
->src
.ipv6
, bytelen
))
1232 /* This function takes a nexthop as argument and adds
1233 * the appropriate netlink attributes to an existing
1236 * @param routedesc: Human readable description of route type
1237 * (direct/recursive, single-/multipath)
1238 * @param bytelen: Length of addresses in bytes.
1239 * @param nexthop: Nexthop information
1240 * @param nlmsg: nlmsghdr structure to fill in.
1241 * @param req_size: The size allocated for the message.
1243 * The function returns true if the nexthop could be added
1244 * to the message, otherwise false is returned.
1246 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1247 const char *routedesc
, int bytelen
,
1248 const struct nexthop
*nexthop
,
1249 struct nlmsghdr
*nlmsg
,
1250 struct rtmsg
*rtmsg
,
1251 size_t req_size
, int cmd
)
1254 char label_buf
[256];
1256 char addrstr
[INET6_ADDRSTRLEN
];
1260 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1262 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1263 req_size
, rtmsg
, label_buf
,
1267 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1268 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1270 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1271 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1272 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1274 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1277 if (cmd
== RTM_NEWROUTE
) {
1278 if (!_netlink_route_encode_nexthop_src(
1279 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1283 if (IS_ZEBRA_DEBUG_KERNEL
)
1284 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1285 __func__
, routedesc
, p
, ipv4_ll_buf
,
1286 label_buf
, nexthop
->ifindex
,
1287 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1291 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1292 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1293 /* Send deletes to the kernel without specifying the next-hop */
1294 if (cmd
!= RTM_DELROUTE
) {
1295 if (!_netlink_route_add_gateway_info(
1296 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1301 if (cmd
== RTM_NEWROUTE
) {
1302 if (!_netlink_route_encode_nexthop_src(
1303 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1307 if (IS_ZEBRA_DEBUG_KERNEL
) {
1308 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1310 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1311 __func__
, routedesc
, p
, addrstr
, label_buf
,
1312 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1317 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1318 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1319 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1320 AF_INET6
, nlmsg
, req_size
,
1324 if (cmd
== RTM_NEWROUTE
) {
1325 if (!_netlink_route_encode_nexthop_src(
1326 nexthop
, AF_INET6
, nlmsg
, req_size
,
1331 if (IS_ZEBRA_DEBUG_KERNEL
) {
1332 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1334 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1335 __func__
, routedesc
, p
, addrstr
, label_buf
,
1336 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1342 * We have the ifindex so we should always send it
1343 * This is especially useful if we are doing route
1346 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1347 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1351 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1352 if (cmd
== RTM_NEWROUTE
) {
1353 if (!_netlink_route_encode_nexthop_src(
1354 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1358 if (IS_ZEBRA_DEBUG_KERNEL
)
1359 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1360 __func__
, routedesc
, p
, nexthop
->ifindex
,
1361 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1367 /* This function takes a nexthop as argument and
1368 * appends to the given netlink msg. If the nexthop
1369 * defines a preferred source, the src parameter
1370 * will be modified to point to that src, otherwise
1371 * it will be kept unmodified.
1373 * @param routedesc: Human readable description of route type
1374 * (direct/recursive, single-/multipath)
1375 * @param bytelen: Length of addresses in bytes.
1376 * @param nexthop: Nexthop information
1377 * @param nlmsg: nlmsghdr structure to fill in.
1378 * @param req_size: The size allocated for the message.
1379 * @param src: pointer pointing to a location where
1380 * the prefsrc should be stored.
1382 * The function returns true if the nexthop could be added
1383 * to the message, otherwise false is returned.
1385 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1386 const char *routedesc
, int bytelen
,
1387 const struct nexthop
*nexthop
,
1388 struct nlmsghdr
*nlmsg
,
1389 size_t req_size
, struct rtmsg
*rtmsg
,
1390 const union g_addr
**src
)
1392 char label_buf
[256];
1394 struct rtnexthop
*rtnh
;
1396 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1402 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1404 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1405 req_size
, rtmsg
, label_buf
,
1409 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1410 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1412 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1413 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1414 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1416 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1417 if (nexthop
->weight
)
1418 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1420 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1421 *src
= &nexthop
->rmap_src
;
1422 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1423 *src
= &nexthop
->src
;
1425 if (IS_ZEBRA_DEBUG_KERNEL
)
1427 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1428 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1429 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1431 nl_attr_rtnh_end(nlmsg
, rtnh
);
1435 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1436 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1437 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1438 nlmsg
, req_size
, bytelen
,
1442 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1443 *src
= &nexthop
->rmap_src
;
1444 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1445 *src
= &nexthop
->src
;
1447 if (IS_ZEBRA_DEBUG_KERNEL
)
1448 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1449 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1450 label_buf
, nexthop
->ifindex
,
1451 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1453 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1454 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1455 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1456 AF_INET6
, nlmsg
, req_size
,
1460 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1461 *src
= &nexthop
->rmap_src
;
1462 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1463 *src
= &nexthop
->src
;
1465 if (IS_ZEBRA_DEBUG_KERNEL
)
1466 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1467 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1468 label_buf
, nexthop
->ifindex
,
1469 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1473 * We have figured out the ifindex so we should always send it
1474 * This is especially useful if we are doing route
1477 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1478 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1481 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1482 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1483 *src
= &nexthop
->rmap_src
;
1484 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1485 *src
= &nexthop
->src
;
1487 if (IS_ZEBRA_DEBUG_KERNEL
)
1488 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1489 __func__
, routedesc
, p
, nexthop
->ifindex
,
1490 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1493 if (nexthop
->weight
)
1494 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1496 nl_attr_rtnh_end(nlmsg
, rtnh
);
1500 static inline bool _netlink_mpls_build_singlepath(const struct prefix
*p
,
1501 const char *routedesc
,
1502 const zebra_nhlfe_t
*nhlfe
,
1503 struct nlmsghdr
*nlmsg
,
1504 struct rtmsg
*rtmsg
,
1505 size_t req_size
, int cmd
)
1510 family
= NHLFE_FAMILY(nhlfe
);
1511 bytelen
= (family
== AF_INET
? 4 : 16);
1512 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1513 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1519 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1520 const zebra_nhlfe_t
*nhlfe
,
1521 struct nlmsghdr
*nlmsg
, size_t req_size
,
1522 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1527 family
= NHLFE_FAMILY(nhlfe
);
1528 bytelen
= (family
== AF_INET
? 4 : 16);
1529 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1530 nhlfe
->nexthop
, nlmsg
, req_size
,
1534 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1536 if (IS_ZEBRA_DEBUG_KERNEL
)
1537 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1538 routedesc
, nl_msg_type_to_str(cmd
), label
);
1541 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1542 int llalen
, ns_id_t ns_id
)
1544 uint8_t protocol
= RTPROT_ZEBRA
;
1551 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1553 memset(&req
, 0, sizeof(req
));
1555 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1556 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1557 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1558 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1560 req
.ndm
.ndm_family
= AF_INET
;
1561 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1562 req
.ndm
.ndm_ifindex
= ifindex
;
1563 req
.ndm
.ndm_type
= RTN_UNICAST
;
1565 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1567 nl_attr_put32(&req
.n
, sizeof(req
), NDA_DST
, addr
);
1568 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1570 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1574 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1577 if (family
== AF_INET
) {
1578 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1579 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1581 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1582 src
->ipv4
= nexthop
->src
.ipv4
;
1585 } else if (family
== AF_INET6
) {
1586 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1587 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1589 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1590 src
->ipv6
= nexthop
->src
.ipv6
;
1599 * The function returns true if the attribute could be added
1600 * to the message, otherwise false is returned.
1602 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1605 struct rtattr
*nest
;
1607 switch (nh
->nh_encap_type
) {
1609 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1612 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1616 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1619 nl_attr_nest_end(n
, nest
);
1627 * Routing table change via netlink interface, using a dataplane context object
1629 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1630 * otherwise the number of bytes written to buf.
1632 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1633 struct zebra_dplane_ctx
*ctx
,
1634 uint8_t *data
, size_t datalen
,
1635 bool fpm
, bool force_nhg
)
1638 struct nexthop
*nexthop
= NULL
;
1639 unsigned int nexthop_num
;
1640 const char *routedesc
;
1641 bool setsrc
= false;
1643 const struct prefix
*p
, *src_p
;
1650 } *req
= (void *)data
;
1652 p
= dplane_ctx_get_dest(ctx
);
1653 src_p
= dplane_ctx_get_src(ctx
);
1655 if (datalen
< sizeof(*req
))
1658 memset(req
, 0, sizeof(*req
));
1660 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1662 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1663 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1665 if ((cmd
== RTM_NEWROUTE
) &&
1666 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1667 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1669 req
->n
.nlmsg_type
= cmd
;
1671 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1673 req
->r
.rtm_family
= p
->family
;
1674 req
->r
.rtm_dst_len
= p
->prefixlen
;
1675 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1676 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1678 if (cmd
== RTM_DELROUTE
)
1679 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1681 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1684 * blackhole routes are not RTN_UNICAST, they are
1685 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1686 * so setting this value as a RTN_UNICAST would
1687 * cause the route lookup of just the prefix
1688 * to fail. So no need to specify this for
1689 * the RTM_DELROUTE case
1691 if (cmd
!= RTM_DELROUTE
)
1692 req
->r
.rtm_type
= RTN_UNICAST
;
1694 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1697 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1703 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1705 * either by the kernel or by zebra. Its purely for calculating best
1707 * by the routing protocol and for communicating with protocol peers.
1709 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1710 NL_DEFAULT_ROUTE_METRIC
))
1713 #if defined(SUPPORT_REALMS)
1717 if (cmd
== RTM_DELROUTE
)
1718 tag
= dplane_ctx_get_old_tag(ctx
);
1720 tag
= dplane_ctx_get_tag(ctx
);
1722 if (tag
> 0 && tag
<= 255) {
1723 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
1728 /* Table corresponding to this route. */
1729 table_id
= dplane_ctx_get_table(ctx
);
1731 req
->r
.rtm_table
= table_id
;
1733 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
1734 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
1738 if (IS_ZEBRA_DEBUG_KERNEL
)
1740 "%s: %s %pFX vrf %u(%u)", __func__
,
1741 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
1745 * If we are not updating the route and we have received
1746 * a route delete, then all we need to fill in is the
1747 * prefix information to tell the kernel to schwack
1750 if (cmd
== RTM_DELROUTE
)
1751 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1753 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1754 struct rtattr
*nest
;
1755 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1756 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1758 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1761 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
1765 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
1767 nl_attr_nest_end(&req
->n
, nest
);
1770 if ((!fpm
&& kernel_nexthops_supported()
1771 && (!proto_nexthops_only()
1772 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
1773 || (fpm
&& force_nhg
)) {
1774 /* Kernel supports nexthop objects */
1775 if (IS_ZEBRA_DEBUG_KERNEL
)
1776 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
1777 dplane_ctx_get_nhe_id(ctx
));
1779 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
1780 dplane_ctx_get_nhe_id(ctx
)))
1783 /* Have to determine src still */
1784 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1788 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
1792 if (p
->family
== AF_INET
) {
1793 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1794 &src
.ipv4
, bytelen
))
1796 } else if (p
->family
== AF_INET6
) {
1797 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1798 &src
.ipv6
, bytelen
))
1803 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1806 /* Count overall nexthops so we can decide whether to use singlepath
1807 * or multipath case.
1810 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1811 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1813 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1819 /* Singlepath case. */
1820 if (nexthop_num
== 1) {
1822 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1824 * So we want to cover 2 types of blackhole
1826 * 1) A normal blackhole route( ala from a static
1828 * 2) A recursively resolved blackhole route
1830 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1831 switch (nexthop
->bh_type
) {
1832 case BLACKHOLE_ADMINPROHIB
:
1833 req
->r
.rtm_type
= RTN_PROHIBIT
;
1835 case BLACKHOLE_REJECT
:
1836 req
->r
.rtm_type
= RTN_UNREACHABLE
;
1839 req
->r
.rtm_type
= RTN_BLACKHOLE
;
1842 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1844 if (CHECK_FLAG(nexthop
->flags
,
1845 NEXTHOP_FLAG_RECURSIVE
)) {
1850 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1855 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1856 routedesc
= nexthop
->rparent
1857 ? "recursive, single-path"
1860 if (!_netlink_route_build_singlepath(
1861 p
, routedesc
, bytelen
, nexthop
,
1862 &req
->n
, &req
->r
, datalen
, cmd
))
1869 * Add encapsulation information when installing via
1873 if (!netlink_route_nexthop_encap(
1874 &req
->n
, datalen
, nexthop
))
1880 if (p
->family
== AF_INET
) {
1881 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1882 &src
.ipv4
, bytelen
))
1884 } else if (p
->family
== AF_INET6
) {
1885 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1886 &src
.ipv6
, bytelen
))
1890 } else { /* Multipath case */
1891 struct rtattr
*nest
;
1892 const union g_addr
*src1
= NULL
;
1894 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
1899 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1900 if (CHECK_FLAG(nexthop
->flags
,
1901 NEXTHOP_FLAG_RECURSIVE
)) {
1902 /* This only works for IPv4 now */
1906 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1911 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1912 routedesc
= nexthop
->rparent
1913 ? "recursive, multipath"
1917 if (!_netlink_route_build_multipath(
1918 p
, routedesc
, bytelen
, nexthop
,
1919 &req
->n
, datalen
, &req
->r
, &src1
))
1922 if (!setsrc
&& src1
) {
1923 if (p
->family
== AF_INET
)
1924 src
.ipv4
= src1
->ipv4
;
1925 else if (p
->family
== AF_INET6
)
1926 src
.ipv6
= src1
->ipv6
;
1933 nl_attr_nest_end(&req
->n
, nest
);
1936 * Add encapsulation information when installing via
1940 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1942 if (CHECK_FLAG(nexthop
->flags
,
1943 NEXTHOP_FLAG_RECURSIVE
))
1945 if (!netlink_route_nexthop_encap(
1946 &req
->n
, datalen
, nexthop
))
1953 if (p
->family
== AF_INET
) {
1954 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1955 &src
.ipv4
, bytelen
))
1957 } else if (p
->family
== AF_INET6
) {
1958 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1959 &src
.ipv6
, bytelen
))
1962 if (IS_ZEBRA_DEBUG_KERNEL
)
1963 zlog_debug("Setting source");
1967 /* If there is no useful nexthop then return. */
1968 if (nexthop_num
== 0) {
1969 if (IS_ZEBRA_DEBUG_KERNEL
)
1970 zlog_debug("%s: No useful nexthop.", __func__
);
1973 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1976 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1978 uint32_t actual_table
;
1980 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1988 struct zebra_ns
*zns
;
1991 memset(&req
, 0, sizeof(req
));
1993 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1994 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1995 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1997 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
1998 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2000 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2001 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2002 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2003 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2007 * So during the namespace cleanup we started storing
2008 * the zvrf table_id for the default table as RT_TABLE_MAIN
2009 * which is what the normal routing table for ip routing is.
2010 * This change caused this to break our lookups of sg data
2011 * because prior to this change the zvrf->table_id was 0
2012 * and when the pim multicast kernel code saw a 0,
2013 * it was auto-translated to RT_TABLE_DEFAULT. But since
2014 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2015 * and the kernel goes screw you and the delicious cookies you
2016 * are trying to give me. So now we have this little hack.
2018 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2020 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2022 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2023 &zns
->netlink_cmd
, zns
, 0);
2029 /* Char length to debug ID with */
2030 #define ID_LENGTH 10
2032 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2034 const struct nh_grp
*z_grp
,
2035 const uint8_t count
)
2037 struct nexthop_grp grp
[count
];
2038 /* Need space for max group size, "/", and null term */
2039 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2040 char buf1
[ID_LENGTH
+ 2];
2044 memset(grp
, 0, sizeof(grp
));
2047 for (int i
= 0; i
< count
; i
++) {
2048 grp
[i
].id
= z_grp
[i
].id
;
2049 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2051 if (IS_ZEBRA_DEBUG_KERNEL
) {
2053 snprintf(buf
, sizeof(buf1
), "group %u",
2056 snprintf(buf1
, sizeof(buf1
), "/%u",
2058 strlcat(buf
, buf1
, sizeof(buf
));
2062 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2063 count
* sizeof(*grp
)))
2067 if (IS_ZEBRA_DEBUG_KERNEL
)
2068 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2074 * Next hop packet encoding helper function.
2076 * \param[in] cmd netlink command.
2077 * \param[in] ctx dataplane context (information snapshot).
2078 * \param[out] buf buffer to hold the packet.
2079 * \param[in] buflen amount of buffer bytes.
2081 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2082 * otherwise the number of bytes written to buf.
2084 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2085 const struct zebra_dplane_ctx
*ctx
,
2086 void *buf
, size_t buflen
)
2094 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2095 char label_buf
[256];
2097 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2098 int type
= dplane_ctx_get_nhe_type(ctx
);
2099 struct rtattr
*nest
;
2104 EC_ZEBRA_NHG_FIB_UPDATE
,
2105 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2110 * Nothing to do if the kernel doesn't support nexthop objects or
2111 * we dont want to install this type of NHG
2113 if (!kernel_nexthops_supported()) {
2114 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2116 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2117 __func__
, id
, zebra_route_string(type
));
2121 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2122 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2124 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2125 __func__
, id
, zebra_route_string(type
));
2129 label_buf
[0] = '\0';
2131 if (buflen
< sizeof(*req
))
2134 memset(req
, 0, sizeof(*req
));
2136 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2137 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2139 if (cmd
== RTM_NEWNEXTHOP
)
2140 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2142 req
->n
.nlmsg_type
= cmd
;
2143 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2145 req
->nhm
.nh_family
= AF_UNSPEC
;
2148 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2151 if (cmd
== RTM_NEWNEXTHOP
) {
2153 * We distinguish between a "group", which is a collection
2154 * of ids, and a singleton nexthop with an id. The
2155 * group is installed as an id that just refers to a list of
2158 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2159 if (!_netlink_nexthop_build_group(
2160 &req
->n
, buflen
, id
,
2161 dplane_ctx_get_nhe_nh_grp(ctx
),
2162 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2165 const struct nexthop
*nh
=
2166 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2167 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2170 req
->nhm
.nh_family
= AF_INET
;
2171 else if (afi
== AFI_IP6
)
2172 req
->nhm
.nh_family
= AF_INET6
;
2175 case NEXTHOP_TYPE_IPV4
:
2176 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2177 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2182 case NEXTHOP_TYPE_IPV6
:
2183 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2184 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2189 case NEXTHOP_TYPE_BLACKHOLE
:
2190 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2193 /* Blackhole shouldn't have anymore attributes
2196 case NEXTHOP_TYPE_IFINDEX
:
2197 /* Don't need anymore info for this */
2203 EC_ZEBRA_NHG_FIB_UPDATE
,
2204 "Context received for kernel nexthop update without an interface");
2208 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2212 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2213 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2216 build_label_stack(nh
->nh_label
, out_lse
,
2217 label_buf
, sizeof(label_buf
));
2220 /* Set the BoS bit */
2221 out_lse
[num_labels
- 1] |=
2222 htonl(1 << MPLS_LS_S_SHIFT
);
2225 * TODO: MPLS unsupported for now in kernel.
2227 if (req
->nhm
.nh_family
== AF_MPLS
)
2230 encap
= LWTUNNEL_ENCAP_MPLS
;
2231 if (!nl_attr_put16(&req
->n
, buflen
,
2232 NHA_ENCAP_TYPE
, encap
))
2234 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2238 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2240 num_labels
* sizeof(mpls_lse_t
)))
2243 nl_attr_nest_end(&req
->n
, nest
);
2248 if (IS_ZEBRA_DEBUG_KERNEL
)
2249 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2250 __func__
, id
, nh
, nh
->ifindex
,
2251 vrf_id_to_name(nh
->vrf_id
),
2252 nh
->vrf_id
, label_buf
);
2255 req
->nhm
.nh_protocol
= zebra2proto(type
);
2257 } else if (cmd
!= RTM_DELNEXTHOP
) {
2259 EC_ZEBRA_NHG_FIB_UPDATE
,
2260 "Nexthop group kernel update command (%d) does not exist",
2265 if (IS_ZEBRA_DEBUG_KERNEL
)
2266 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2269 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2272 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2273 void *buf
, size_t buflen
)
2275 enum dplane_op_e op
;
2278 op
= dplane_ctx_get_op(ctx
);
2279 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2280 cmd
= RTM_NEWNEXTHOP
;
2281 else if (op
== DPLANE_OP_NH_DELETE
)
2282 cmd
= RTM_DELNEXTHOP
;
2284 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2285 "Context received for kernel nexthop update with incorrect OP code (%u)",
2290 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2293 enum netlink_msg_status
2294 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2295 struct zebra_dplane_ctx
*ctx
)
2297 /* Nothing to do if the kernel doesn't support nexthop objects */
2298 if (!kernel_nexthops_supported())
2299 return FRR_NETLINK_SUCCESS
;
2301 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2305 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2306 void *buf
, size_t buflen
)
2308 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2309 buflen
, false, false);
2312 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2313 void *buf
, size_t buflen
)
2315 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2316 buflen
, false, false);
2319 enum netlink_msg_status
2320 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2323 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2325 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2327 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2329 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2331 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2332 /* Single 'replace' operation */
2335 * With route replace semantics in place
2336 * for v4 routes and the new route is a system
2337 * route we do not install anything.
2338 * The problem here is that the new system
2339 * route should cause us to withdraw from
2340 * the kernel the old non-system route
2342 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2343 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2344 netlink_batch_add_msg(
2345 bth
, ctx
, netlink_delroute_msg_encoder
,
2349 * So v6 route replace semantics are not in
2350 * the kernel at this point as I understand it.
2351 * so let's do a delete then an add.
2352 * In the future once v6 route replace semantics
2353 * are in we can figure out what to do here to
2354 * allow working with old and new kernels.
2356 * I'm also intentionally ignoring the failure case
2357 * of the route delete. If that happens yeah we're
2360 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2361 netlink_batch_add_msg(
2362 bth
, ctx
, netlink_delroute_msg_encoder
,
2368 return FRR_NETLINK_ERROR
;
2370 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2371 return FRR_NETLINK_SUCCESS
;
2373 return netlink_batch_add_msg(bth
, ctx
,
2375 ? netlink_newroute_msg_encoder
2376 : netlink_delroute_msg_encoder
,
2381 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2383 * @tb: Netlink RTA data
2384 * @family: Address family in the nhmsg
2385 * @ifp: Interface connected - this should be NULL, we fill it in
2386 * @ns_id: Namspace id
2388 * Return: New nexthop
2390 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2391 unsigned char family
,
2392 struct interface
**ifp
,
2395 struct nexthop nh
= {};
2397 enum nexthop_types_t type
= 0;
2400 struct interface
*ifp_lookup
;
2402 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2405 if (tb
[NHA_GATEWAY
]) {
2408 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2412 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2417 EC_ZEBRA_BAD_NHG_MESSAGE
,
2418 "Nexthop gateway with bad address family (%d) received from kernel",
2422 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2424 type
= NEXTHOP_TYPE_IFINDEX
;
2430 memcpy(&(nh
.gate
), gate
, sz
);
2433 nh
.ifindex
= if_index
;
2436 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2441 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2444 EC_ZEBRA_UNKNOWN_INTERFACE
,
2445 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2446 __func__
, nh
.ifindex
);
2448 nh
.vrf_id
= VRF_DEFAULT
;
2451 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2452 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2455 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2457 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2458 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2461 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2468 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2469 struct nh_grp
*z_grp
, int z_grp_size
)
2472 /* linux/nexthop.h group struct */
2473 struct nexthop_grp
*n_grp
= NULL
;
2475 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2476 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2478 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2479 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2480 "Invalid nexthop group received from the kernel");
2484 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2485 z_grp
[i
].id
= n_grp
[i
].id
;
2486 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2492 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2494 * @h: Netlink message header
2495 * @ns_id: Namspace id
2496 * @startup: Are we reading under startup conditions?
2498 * Return: Result status
2500 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2503 /* nexthop group id */
2505 unsigned char family
;
2507 afi_t afi
= AFI_UNSPEC
;
2508 vrf_id_t vrf_id
= VRF_DEFAULT
;
2509 struct interface
*ifp
= NULL
;
2510 struct nhmsg
*nhm
= NULL
;
2511 struct nexthop nh
= {};
2512 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2513 /* Count of nexthops in group array */
2514 uint8_t grp_count
= 0;
2515 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2517 nhm
= NLMSG_DATA(h
);
2522 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2525 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2528 "%s: Message received from netlink is of a broken size %d %zu",
2529 __func__
, h
->nlmsg_len
,
2530 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2534 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2539 EC_ZEBRA_BAD_NHG_MESSAGE
,
2540 "Nexthop group without an ID received from the kernel");
2544 /* We use the ID key'd nhg table for kernel updates */
2545 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2547 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2548 /* If this is a L2 NH just ignore it */
2549 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2550 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2556 family
= nhm
->nh_family
;
2557 afi
= family2afi(family
);
2559 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2561 if (IS_ZEBRA_DEBUG_KERNEL
)
2562 zlog_debug("%s ID (%u) %s NS %u",
2563 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2564 nl_family_to_str(family
), ns_id
);
2567 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2568 if (tb
[NHA_GROUP
]) {
2570 * If this is a group message its only going to have
2571 * an array of nexthop IDs associated with it
2573 grp_count
= netlink_nexthop_process_group(
2574 tb
, grp
, array_size(grp
));
2576 if (tb
[NHA_BLACKHOLE
]) {
2578 * This nexthop is just for blackhole-ing
2579 * traffic, it should not have an OIF, GATEWAY,
2582 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2583 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2584 } else if (tb
[NHA_OIF
])
2586 * This is a true new nexthop, so we need
2587 * to parse the gateway and device info
2589 nh
= netlink_nexthop_process_nh(tb
, family
,
2594 EC_ZEBRA_BAD_NHG_MESSAGE
,
2595 "Invalid Nexthop message received from the kernel with ID (%u)",
2599 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2600 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2601 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2605 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2609 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2610 zebra_nhg_kernel_del(id
, vrf_id
);
2616 * netlink_request_nexthop() - Request nextop information from the kernel
2617 * @zns: Zebra namespace
2618 * @family: AF_* netlink family
2619 * @type: RTM_* route type
2621 * Return: Result status
2623 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2630 /* Form the request, specifying filter (rtattr) if needed. */
2631 memset(&req
, 0, sizeof(req
));
2632 req
.n
.nlmsg_type
= type
;
2633 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2634 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2635 req
.nhm
.nh_family
= family
;
2637 return netlink_request(&zns
->netlink_cmd
, &req
);
2642 * netlink_nexthop_read() - Nexthop read function using netlink interface
2644 * @zns: Zebra name space
2646 * Return: Result status
2647 * Only called at bootstrap time.
2649 int netlink_nexthop_read(struct zebra_ns
*zns
)
2652 struct zebra_dplane_info dp_info
;
2654 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2656 /* Get nexthop objects */
2657 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2660 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2664 /* If we succesfully read in nexthop objects,
2665 * this kernel must support them.
2669 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2670 zlog_debug("Nexthop objects %ssupported on this kernel",
2671 supports_nh
? "" : "not ");
2677 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2678 int llalen
, ns_id_t ns_id
)
2680 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2681 addr
, lla
, llalen
, ns_id
);
2685 * netlink_neigh_update_msg_encode() - Common helper api for encoding
2686 * evpn neighbor update as netlink messages using dataplane context object.
2687 * Here, a neighbor refers to a bridge forwarding database entry for
2688 * either unicast forwarding or head-end replication or an IP neighbor
2690 * @ctx: Dataplane context
2691 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
2692 * @mac: A neighbor cache link layer address
2693 * @ip: A neighbor cache n/w layer destination address
2694 * In the case of bridge FDB, this represnts the remote
2696 * @replace_obj: Whether NEW request should replace existing object or
2697 * add to the end of the list
2698 * @family: AF_* netlink family
2699 * @type: RTN_* route type
2700 * @flags: NTF_* flags
2701 * @state: NUD_* states
2702 * @data: data buffer pointer
2703 * @datalen: total amount of data buffer space
2705 * Return: 0 when the msg doesn't fit entirely in the buffer
2706 * otherwise the number of bytes written to buf.
2708 static ssize_t
netlink_neigh_update_msg_encode(
2709 const struct zebra_dplane_ctx
*ctx
, int cmd
, const struct ethaddr
*mac
,
2710 const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
, uint8_t type
,
2711 uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
2712 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
2715 uint8_t protocol
= RTPROT_ZEBRA
;
2722 enum dplane_op_e op
;
2724 if (datalen
< sizeof(*req
))
2726 memset(req
, 0, sizeof(*req
));
2728 op
= dplane_ctx_get_op(ctx
);
2730 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2731 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2732 if (cmd
== RTM_NEWNEIGH
)
2733 req
->n
.nlmsg_flags
|=
2735 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
2736 req
->n
.nlmsg_type
= cmd
;
2737 req
->ndm
.ndm_family
= family
;
2738 req
->ndm
.ndm_type
= type
;
2739 req
->ndm
.ndm_state
= state
;
2740 req
->ndm
.ndm_flags
= flags
;
2741 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2743 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
2748 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, mac
, 6))
2753 struct rtattr
*nest
;
2755 nest
= nl_attr_nest(&req
->n
, datalen
,
2756 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
2760 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
2761 &nfy_flags
, sizeof(nfy_flags
)))
2763 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
2766 nl_attr_nest_end(&req
->n
, nest
);
2771 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
2777 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
2781 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2782 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
2787 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
2788 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
2791 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
2795 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
2796 dplane_ctx_mac_get_br_ifindex(ctx
)))
2800 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2804 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2805 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2808 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
2809 void *buf
, size_t buflen
)
2811 struct ethaddr dst_mac
= {.octet
= {0}};
2813 return netlink_neigh_update_msg_encode(
2814 ctx
, cmd
, &dst_mac
, dplane_ctx_neigh_get_ipaddr(ctx
), false,
2815 PF_BRIDGE
, 0, NTF_SELF
, (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/,
2816 false /*nfy*/, 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/,
2821 #define NDA_RTA(r) \
2822 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2825 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2828 struct interface
*ifp
;
2829 struct zebra_if
*zif
;
2830 struct rtattr
*tb
[NDA_MAX
+ 1];
2831 struct interface
*br_if
;
2834 struct in_addr vtep_ip
;
2835 int vid_present
= 0, dst_present
= 0;
2836 char buf
[ETHER_ADDR_STRLEN
];
2840 bool local_inactive
= false;
2841 bool dp_static
= false;
2842 uint32_t nhg_id
= 0;
2844 ndm
= NLMSG_DATA(h
);
2846 /* We only process macfdb notifications if EVPN is enabled */
2847 if (!is_evpn_enabled())
2850 /* Parse attributes and extract fields of interest. Do basic
2851 * validation of the fields.
2853 memset(tb
, 0, sizeof tb
);
2854 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
2857 if (!tb
[NDA_LLADDR
]) {
2858 if (IS_ZEBRA_DEBUG_KERNEL
)
2859 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
2860 nl_msg_type_to_str(h
->nlmsg_type
),
2865 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2866 if (IS_ZEBRA_DEBUG_KERNEL
)
2868 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
2869 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
2870 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2874 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2876 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2878 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2879 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2883 /* TODO: Only IPv4 supported now. */
2885 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
2887 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
2892 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
2894 if (ndm
->ndm_state
& NUD_STALE
)
2895 local_inactive
= true;
2897 if (tb
[NDA_FDB_EXT_ATTRS
]) {
2898 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
2899 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
2901 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
2902 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
2905 nfy_flags
= *(uint8_t *)RTA_DATA(
2906 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
2907 if (nfy_flags
& FDB_NOTIFY_BIT
)
2909 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
2910 local_inactive
= true;
2914 if (IS_ZEBRA_DEBUG_KERNEL
)
2915 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %s%s nhg %d",
2916 nl_msg_type_to_str(h
->nlmsg_type
),
2917 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2918 ndm
->ndm_state
, ndm
->ndm_flags
,
2919 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2920 dst_present
? dst_buf
: "", nhg_id
);
2922 /* The interface should exist. */
2923 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2925 if (!ifp
|| !ifp
->info
)
2928 /* The interface should be something we're interested in. */
2929 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2932 zif
= (struct zebra_if
*)ifp
->info
;
2933 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2934 if (IS_ZEBRA_DEBUG_KERNEL
)
2936 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
2937 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
2939 zif
->brslave_info
.bridge_ifindex
);
2943 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
2945 if (filter_vlan
&& vid
!= filter_vlan
) {
2946 if (IS_ZEBRA_DEBUG_KERNEL
)
2947 zlog_debug(" Filtered due to filter vlan: %d",
2952 /* If add or update, do accordingly if learnt on a "local" interface; if
2953 * the notification is over VxLAN, this has to be related to
2955 * so perform an implicit delete of any local entry (if it exists).
2957 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2958 /* Drop "permanent" entries. */
2959 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2960 if (IS_ZEBRA_DEBUG_KERNEL
)
2962 " Dropping entry because of NUD_PERMANENT");
2966 if (IS_ZEBRA_IF_VXLAN(ifp
))
2967 return zebra_vxlan_dp_network_mac_add(
2968 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
2969 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
2971 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2972 sticky
, local_inactive
, dp_static
);
2975 /* This is a delete notification.
2976 * Ignore the notification with IP dest as it may just signify that the
2977 * MAC has moved from remote to local. The exception is the special
2978 * all-zeros MAC that represents the BUM flooding entry; we may have
2979 * to readd it. Otherwise,
2980 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2981 * 2. For a MAC over "local" interface, delete the mac
2982 * Note: We will get notifications from both bridge driver and VxLAN
2989 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2991 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
2992 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
2996 if (IS_ZEBRA_IF_VXLAN(ifp
))
2997 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
2999 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3002 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3007 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3010 /* Length validity. */
3011 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3015 /* We are interested only in AF_BRIDGE notifications. */
3016 ndm
= NLMSG_DATA(h
);
3017 if (ndm
->ndm_family
!= AF_BRIDGE
)
3020 return netlink_macfdb_change(h
, len
, ns_id
);
3023 /* Request for MAC FDB information from the kernel */
3024 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3025 int type
, ifindex_t master_ifindex
)
3029 struct ifinfomsg ifm
;
3033 /* Form the request, specifying filter (rtattr) if needed. */
3034 memset(&req
, 0, sizeof(req
));
3035 req
.n
.nlmsg_type
= type
;
3036 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3037 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3038 req
.ifm
.ifi_family
= family
;
3040 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3042 return netlink_request(netlink_cmd
, &req
);
3046 * MAC forwarding database read using netlink interface. This is invoked
3049 int netlink_macfdb_read(struct zebra_ns
*zns
)
3052 struct zebra_dplane_info dp_info
;
3054 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3056 /* Get bridge FDB table. */
3057 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3061 /* We are reading entire table. */
3063 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3070 * MAC forwarding database read using netlink interface. This is for a
3071 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3073 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3074 struct interface
*br_if
)
3076 struct zebra_if
*br_zif
;
3077 struct zebra_if
*zif
;
3078 struct zebra_l2info_vxlan
*vxl
;
3079 struct zebra_dplane_info dp_info
;
3082 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3084 /* Save VLAN we're filtering on, if needed. */
3085 br_zif
= (struct zebra_if
*)br_if
->info
;
3086 zif
= (struct zebra_if
*)ifp
->info
;
3087 vxl
= &zif
->l2info
.vxl
;
3088 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3089 filter_vlan
= vxl
->access_vlan
;
3091 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3093 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3097 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3100 /* Reset VLAN filter. */
3106 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3107 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3110 struct interface
*br_if
,
3111 struct ethaddr
*mac
,
3119 struct zebra_if
*br_zif
;
3120 char buf
[ETHER_ADDR_STRLEN
];
3122 memset(&req
, 0, sizeof(req
));
3123 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3124 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3125 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3126 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3127 /* req.ndm.ndm_state = NUD_REACHABLE; */
3129 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3131 br_zif
= (struct zebra_if
*)br_if
->info
;
3132 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3133 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3135 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3137 if (IS_ZEBRA_DEBUG_KERNEL
)
3139 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %s vid %u",
3140 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3141 br_if
->name
, br_if
->ifindex
,
3142 vrf_id_to_name(br_if
->vrf_id
), br_if
->vrf_id
,
3143 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
3145 return netlink_request(&zns
->netlink_cmd
, &req
);
3148 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3149 struct interface
*br_if
,
3150 struct ethaddr
*mac
, vlanid_t vid
)
3153 struct zebra_dplane_info dp_info
;
3155 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3157 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3159 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3165 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3172 * Netlink-specific handler for MAC updates using dataplane context object.
3174 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3177 struct ipaddr vtep_ip
;
3184 uint32_t update_flags
;
3186 uint8_t nfy_flags
= 0;
3188 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3189 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3192 state
= NUD_REACHABLE
;
3194 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3195 if (update_flags
& DPLANE_MAC_REMOTE
) {
3197 if (dplane_ctx_mac_is_sticky(ctx
)) {
3198 /* NUD_NOARP prevents the entry from expiring */
3200 /* sticky the entry from moving */
3201 flags
|= NTF_STICKY
;
3203 flags
|= NTF_EXT_LEARNED
;
3205 /* if it was static-local previously we need to clear the
3206 * notify flags on replace with remote
3208 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3212 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3213 nfy_flags
|= FDB_NOTIFY_BIT
;
3217 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3218 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3223 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3224 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3225 SET_IPADDR_V4(&vtep_ip
);
3227 if (IS_ZEBRA_DEBUG_KERNEL
) {
3228 char ipbuf
[PREFIX_STRLEN
];
3229 char buf
[ETHER_ADDR_STRLEN
];
3231 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3233 vid
= dplane_ctx_mac_get_vlan(ctx
);
3235 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3240 "Tx %s family %s IF %s(%u)%s %sMAC %s dst %s nhg %u%s%s%s%s%s",
3241 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3242 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3243 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3244 prefix_mac2str(mac
, buf
, sizeof(buf
)),
3245 ipaddr2str(&vtep_ip
, ipbuf
, sizeof(ipbuf
)), nhg_id
,
3246 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3247 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3249 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3250 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3255 total
= netlink_neigh_update_msg_encode(
3256 ctx
, cmd
, dplane_ctx_mac_get_addr(ctx
), &vtep_ip
, true,
3257 AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
, nfy_flags
,
3258 false /*ext*/, 0 /*ext_flags*/, data
, datalen
);
3264 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3265 * 5549 support, re-install them.
3267 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3268 struct interface
*ifp
, struct ipaddr
*ip
,
3271 if (ndm
->ndm_family
!= AF_INET
)
3274 if (!zif
->v6_2_v4_ll_neigh_entry
)
3277 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3280 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3281 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3286 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3290 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3292 #define NUD_LOCAL_ACTIVE \
3293 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3295 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3298 struct interface
*ifp
;
3299 struct zebra_if
*zif
;
3300 struct rtattr
*tb
[NDA_MAX
+ 1];
3301 struct interface
*link_if
;
3305 char buf
[ETHER_ADDR_STRLEN
];
3306 char buf2
[INET6_ADDRSTRLEN
];
3307 int mac_present
= 0;
3310 bool local_inactive
;
3311 uint32_t ext_flags
= 0;
3312 bool dp_static
= false;
3314 ndm
= NLMSG_DATA(h
);
3316 /* The interface should exist. */
3317 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3319 if (!ifp
|| !ifp
->info
)
3322 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
3323 zif
= (struct zebra_if
*)ifp
->info
;
3325 /* Parse attributes and extract fields of interest. */
3326 memset(tb
, 0, sizeof(tb
));
3327 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3330 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3331 nl_msg_type_to_str(h
->nlmsg_type
),
3332 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3333 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
);
3337 memset(&ip
, 0, sizeof(struct ipaddr
));
3338 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3339 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3341 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3342 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3343 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3344 if (IS_ZEBRA_DEBUG_KERNEL
)
3346 " Neighbor Entry Received is a 5549 entry, finished");
3350 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3351 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3352 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3354 /* The neighbor is present on an SVI. From this, we locate the
3356 * bridge because we're only interested in neighbors on a VxLAN bridge.
3357 * The bridge is located based on the nature of the SVI:
3358 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3360 * and is linked to the bridge
3361 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3365 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3366 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3370 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3373 if (IS_ZEBRA_DEBUG_KERNEL
)
3375 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3379 memset(&mac
, 0, sizeof(struct ethaddr
));
3380 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3381 if (tb
[NDA_LLADDR
]) {
3382 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3383 if (IS_ZEBRA_DEBUG_KERNEL
)
3385 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3390 ifp
->name
, ndm
->ndm_ifindex
,
3391 VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3392 (unsigned long)RTA_PAYLOAD(
3398 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3401 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3402 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3404 if (tb
[NDA_EXT_FLAGS
]) {
3405 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3406 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3410 if (IS_ZEBRA_DEBUG_KERNEL
)
3412 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %s MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3413 nl_msg_type_to_str(h
->nlmsg_type
),
3414 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3415 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3416 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3418 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3420 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3422 /* If the neighbor state is valid for use, process as an add or
3424 * else process as a delete. Note that the delete handling may
3426 * in re-adding the neighbor if it is a valid "remote" neighbor.
3428 if (ndm
->ndm_state
& NUD_VALID
) {
3429 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3431 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3433 /* If EVPN-MH is not enabled we treat STALE
3434 * neighbors as locally-active and advertise
3437 local_inactive
= false;
3439 return zebra_vxlan_handle_kernel_neigh_update(
3440 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3441 is_router
, local_inactive
, dp_static
);
3444 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3447 if (IS_ZEBRA_DEBUG_KERNEL
)
3448 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %s",
3449 nl_msg_type_to_str(h
->nlmsg_type
),
3450 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3451 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3452 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3454 /* Process the delete - it may result in re-adding the neighbor if it is
3455 * a valid "remote" neighbor.
3457 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3460 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3465 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3468 /* Length validity. */
3469 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3473 /* We are interested only in AF_INET or AF_INET6 notifications. */
3474 ndm
= NLMSG_DATA(h
);
3475 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3478 return netlink_neigh_change(h
, len
);
3481 /* Request for IP neighbor information from the kernel */
3482 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3483 int type
, ifindex_t ifindex
)
3491 /* Form the request, specifying filter (rtattr) if needed. */
3492 memset(&req
, 0, sizeof(req
));
3493 req
.n
.nlmsg_type
= type
;
3494 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3495 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3496 req
.ndm
.ndm_family
= family
;
3498 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3500 return netlink_request(netlink_cmd
, &req
);
3504 * IP Neighbor table read using netlink interface. This is invoked
3507 int netlink_neigh_read(struct zebra_ns
*zns
)
3510 struct zebra_dplane_info dp_info
;
3512 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3514 /* Get IP neighbor table. */
3515 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3519 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3526 * IP Neighbor table read using netlink interface. This is for a specific
3529 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3532 struct zebra_dplane_info dp_info
;
3534 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3536 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3540 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3547 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3548 * read using netlink interface.
3550 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3551 int type
, struct ipaddr
*ip
,
3561 /* Form the request, specifying filter (rtattr) if needed. */
3562 memset(&req
, 0, sizeof(req
));
3563 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3564 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3565 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3566 req
.ndm
.ndm_ifindex
= ifindex
;
3568 if (IS_IPADDR_V4(ip
)) {
3569 ipa_len
= IPV4_MAX_BYTELEN
;
3570 req
.ndm
.ndm_family
= AF_INET
;
3573 ipa_len
= IPV6_MAX_BYTELEN
;
3574 req
.ndm
.ndm_family
= AF_INET6
;
3577 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3579 if (IS_ZEBRA_DEBUG_KERNEL
) {
3580 char buf
[INET6_ADDRSTRLEN
];
3582 zlog_debug("%s: Tx %s family %s IF %u IP %s flags 0x%x",
3583 __func__
, nl_msg_type_to_str(type
),
3584 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
,
3585 ipaddr2str(ip
, buf
, sizeof(buf
)), req
.n
.nlmsg_flags
);
3588 return netlink_request(&zns
->netlink_cmd
, &req
);
3591 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3592 struct interface
*vlan_if
)
3595 struct zebra_ns
*zns
;
3596 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3597 char buf
[INET6_ADDRSTRLEN
];
3598 struct zebra_dplane_info dp_info
;
3602 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3604 if (IS_ZEBRA_DEBUG_KERNEL
)
3605 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf %s(%u)",
3606 __func__
, vlan_if
->name
, vlan_if
->ifindex
,
3607 ipaddr2str(ip
, buf
, sizeof(buf
)),
3608 vrf_id_to_name(vlan_if
->vrf_id
), vlan_if
->vrf_id
);
3610 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3615 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3621 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3626 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3629 /* Length validity. */
3630 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3633 "%s: Message received from netlink is of a broken size %d %zu",
3634 __func__
, h
->nlmsg_len
,
3635 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3639 /* Is this a notification for the MAC FDB or IP neighbor table? */
3640 ndm
= NLMSG_DATA(h
);
3641 if (ndm
->ndm_family
== AF_BRIDGE
)
3642 return netlink_macfdb_change(h
, len
, ns_id
);
3644 if (ndm
->ndm_type
!= RTN_UNICAST
)
3647 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3648 return netlink_ipneigh_change(h
, len
, ns_id
);
3651 EC_ZEBRA_UNKNOWN_FAMILY
,
3652 "Invalid address family: %u received from kernel neighbor change: %s",
3653 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3661 * Utility neighbor-update function, using info from dplane context.
3663 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3664 int cmd
, void *buf
, size_t buflen
)
3666 const struct ipaddr
*ip
;
3667 const struct ethaddr
*mac
;
3671 uint32_t update_flags
;
3672 uint32_t ext_flags
= 0;
3675 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3676 mac
= dplane_ctx_neigh_get_mac(ctx
);
3677 if (is_zero_mac(mac
))
3680 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
3681 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3682 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3684 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3686 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
3687 flags
|= NTF_EXT_LEARNED
;
3688 /* if it was static-local previously we need to clear the
3689 * ext flags on replace with remote
3691 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
3696 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
3697 ext_flags
|= NTF_E_MH_PEER_SYNC
;
3699 /* the ndm_state set for local entries can be REACHABLE or
3700 * STALE. if the dataplane has already establish reachability
3701 * (in the meantime) FRR must not over-write it with STALE.
3702 * this accidental race/over-write is avoided by using the
3703 * WEAK_OVERRIDE_STATE
3705 ext_flags
|= NTF_E_WEAK_OVERRIDE_STATE
;
3707 if (IS_ZEBRA_DEBUG_KERNEL
) {
3708 char buf
[INET6_ADDRSTRLEN
];
3709 char buf2
[ETHER_ADDR_STRLEN
];
3712 "Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x %sext_flags 0x%x",
3713 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
3714 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3715 ipaddr2str(ip
, buf
, sizeof(buf
)),
3716 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
)) : "null",
3717 flags
, state
, ext
? "ext " : "", ext_flags
);
3720 return netlink_neigh_update_msg_encode(
3721 ctx
, cmd
, mac
, ip
, true, family
, RTN_UNICAST
, flags
, state
,
3722 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
, ext_flags
, buf
,
3726 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
3727 void *buf
, size_t buflen
)
3731 switch (dplane_ctx_get_op(ctx
)) {
3732 case DPLANE_OP_NEIGH_INSTALL
:
3733 case DPLANE_OP_NEIGH_UPDATE
:
3734 case DPLANE_OP_NEIGH_DISCOVER
:
3735 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
3737 case DPLANE_OP_NEIGH_DELETE
:
3738 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
3740 case DPLANE_OP_VTEP_ADD
:
3741 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
3744 case DPLANE_OP_VTEP_DELETE
:
3745 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
3756 * Update MAC, using dataplane context object.
3759 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
3760 struct zebra_dplane_ctx
*ctx
)
3762 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
3766 enum netlink_msg_status
3767 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
3769 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
3774 * MPLS label forwarding table change via netlink interface, using dataplane
3775 * context information.
3777 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
3778 void *buf
, size_t buflen
)
3781 const struct nhlfe_list_head
*head
;
3782 const zebra_nhlfe_t
*nhlfe
;
3783 struct nexthop
*nexthop
= NULL
;
3784 unsigned int nexthop_num
;
3785 const char *routedesc
;
3787 struct prefix p
= {0};
3795 if (buflen
< sizeof(*req
))
3798 memset(req
, 0, sizeof(*req
));
3801 * Count # nexthops so we can decide whether to use singlepath
3802 * or multipath case.
3805 head
= dplane_ctx_get_nhlfe_list(ctx
);
3806 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3807 nexthop
= nhlfe
->nexthop
;
3810 if (cmd
== RTM_NEWROUTE
) {
3811 /* Count all selected NHLFEs */
3812 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3813 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3816 /* Count all installed NHLFEs */
3817 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3818 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3823 if ((nexthop_num
== 0) ||
3824 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3827 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3828 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3829 req
->n
.nlmsg_type
= cmd
;
3830 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3832 req
->r
.rtm_family
= AF_MPLS
;
3833 req
->r
.rtm_table
= RT_TABLE_MAIN
;
3834 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3835 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3836 req
->r
.rtm_type
= RTN_UNICAST
;
3838 if (cmd
== RTM_NEWROUTE
) {
3839 /* We do a replace to handle update. */
3840 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
3842 /* set the protocol value if installing */
3843 route_type
= re_type_from_lsp_type(
3844 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3845 req
->r
.rtm_protocol
= zebra2proto(route_type
);
3848 /* Fill destination */
3849 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3850 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
3853 /* Fill nexthops (paths) based on single-path or multipath. The paths
3854 * chosen depend on the operation.
3856 if (nexthop_num
== 1) {
3857 routedesc
= "single-path";
3858 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3862 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3863 nexthop
= nhlfe
->nexthop
;
3867 if ((cmd
== RTM_NEWROUTE
3868 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3869 && CHECK_FLAG(nexthop
->flags
,
3870 NEXTHOP_FLAG_ACTIVE
)))
3871 || (cmd
== RTM_DELROUTE
3872 && (CHECK_FLAG(nhlfe
->flags
,
3873 NHLFE_FLAG_INSTALLED
)
3874 && CHECK_FLAG(nexthop
->flags
,
3875 NEXTHOP_FLAG_FIB
)))) {
3876 /* Add the gateway */
3877 if (!_netlink_mpls_build_singlepath(
3878 &p
, routedesc
, nhlfe
, &req
->n
,
3879 &req
->r
, buflen
, cmd
))
3886 } else { /* Multipath case */
3887 struct rtattr
*nest
;
3888 const union g_addr
*src1
= NULL
;
3890 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
3894 routedesc
= "multipath";
3895 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3899 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3900 nexthop
= nhlfe
->nexthop
;
3904 if ((cmd
== RTM_NEWROUTE
3905 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3906 && CHECK_FLAG(nexthop
->flags
,
3907 NEXTHOP_FLAG_ACTIVE
)))
3908 || (cmd
== RTM_DELROUTE
3909 && (CHECK_FLAG(nhlfe
->flags
,
3910 NHLFE_FLAG_INSTALLED
)
3911 && CHECK_FLAG(nexthop
->flags
,
3912 NEXTHOP_FLAG_FIB
)))) {
3915 /* Build the multipath */
3916 if (!_netlink_mpls_build_multipath(
3917 &p
, routedesc
, nhlfe
, &req
->n
,
3918 buflen
, &req
->r
, &src1
))
3923 /* Add the multipath */
3924 nl_attr_nest_end(&req
->n
, nest
);
3927 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3930 /****************************************************************************
3931 * This code was developed in a branch that didn't have dplane APIs for
3932 * MAC updates. Hence the use of the legacy style. It will be moved to
3933 * the new dplane style pre-merge to master. XXX
3935 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
3942 int cmd
= RTM_NEWNEXTHOP
;
3943 struct zebra_vrf
*zvrf
;
3944 struct zebra_ns
*zns
;
3946 zvrf
= zebra_vrf_get_evpn();
3951 memset(&req
, 0, sizeof(req
));
3953 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3954 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3955 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3956 req
.n
.nlmsg_type
= cmd
;
3957 req
.nhm
.nh_family
= AF_INET
;
3959 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
3961 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
3963 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
3964 &vtep_ip
, IPV4_MAX_BYTELEN
))
3967 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3968 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
3969 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
3972 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3976 static int netlink_fdb_nh_del(uint32_t nh_id
)
3983 int cmd
= RTM_DELNEXTHOP
;
3984 struct zebra_vrf
*zvrf
;
3985 struct zebra_ns
*zns
;
3987 zvrf
= zebra_vrf_get_evpn();
3992 memset(&req
, 0, sizeof(req
));
3994 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3995 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3996 req
.n
.nlmsg_type
= cmd
;
3997 req
.nhm
.nh_family
= AF_UNSPEC
;
3999 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4002 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4003 zlog_debug("Tx %s fdb-nh 0x%x",
4004 nl_msg_type_to_str(cmd
), nh_id
);
4007 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4011 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4012 struct nh_grp
*nh_ids
)
4019 int cmd
= RTM_NEWNEXTHOP
;
4020 struct zebra_vrf
*zvrf
;
4021 struct zebra_ns
*zns
;
4022 struct nexthop_grp grp
[nh_cnt
];
4025 zvrf
= zebra_vrf_get_evpn();
4030 memset(&req
, 0, sizeof(req
));
4032 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4033 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4034 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4035 req
.n
.nlmsg_type
= cmd
;
4036 req
.nhm
.nh_family
= AF_UNSPEC
;
4038 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4040 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4042 memset(&grp
, 0, sizeof(grp
));
4043 for (i
= 0; i
< nh_cnt
; ++i
) {
4044 grp
[i
].id
= nh_ids
[i
].id
;
4045 grp
[i
].weight
= nh_ids
[i
].weight
;
4047 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4048 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4052 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4053 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4057 for (i
= 0; i
< nh_cnt
; ++i
) {
4058 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4060 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4063 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4064 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4067 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4071 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4073 return netlink_fdb_nh_del(nhg_id
);
4076 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4078 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4081 int kernel_del_mac_nh(uint32_t nh_id
)
4083 return netlink_fdb_nh_del(nh_id
);
4086 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4087 struct nh_grp
*nh_ids
)
4089 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4092 int kernel_del_mac_nhg(uint32_t nhg_id
)
4094 return netlink_fdb_nhg_del(nhg_id
);
4097 #endif /* HAVE_NETLINK */