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
);
1771 * Always install blackhole routes without using nexthops, because of
1772 * the following kernel problems:
1773 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
1774 * 2. Blackhole kernel nexthops are deleted when loopback is down.
1776 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
1778 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1779 nexthop
= nexthop
->resolved
;
1781 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1782 switch (nexthop
->bh_type
) {
1783 case BLACKHOLE_ADMINPROHIB
:
1784 req
->r
.rtm_type
= RTN_PROHIBIT
;
1786 case BLACKHOLE_REJECT
:
1787 req
->r
.rtm_type
= RTN_UNREACHABLE
;
1790 req
->r
.rtm_type
= RTN_BLACKHOLE
;
1793 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1797 if ((!fpm
&& kernel_nexthops_supported()
1798 && (!proto_nexthops_only()
1799 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
1800 || (fpm
&& force_nhg
)) {
1801 /* Kernel supports nexthop objects */
1802 if (IS_ZEBRA_DEBUG_KERNEL
)
1803 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
1804 dplane_ctx_get_nhe_id(ctx
));
1806 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
1807 dplane_ctx_get_nhe_id(ctx
)))
1810 /* Have to determine src still */
1811 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1815 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
1819 if (p
->family
== AF_INET
) {
1820 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1821 &src
.ipv4
, bytelen
))
1823 } else if (p
->family
== AF_INET6
) {
1824 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1825 &src
.ipv6
, bytelen
))
1830 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1833 /* Count overall nexthops so we can decide whether to use singlepath
1834 * or multipath case.
1837 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1838 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1840 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1846 /* Singlepath case. */
1847 if (nexthop_num
== 1) {
1849 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1850 if (CHECK_FLAG(nexthop
->flags
,
1851 NEXTHOP_FLAG_RECURSIVE
)) {
1856 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1861 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1862 routedesc
= nexthop
->rparent
1863 ? "recursive, single-path"
1866 if (!_netlink_route_build_singlepath(
1867 p
, routedesc
, bytelen
, nexthop
,
1868 &req
->n
, &req
->r
, datalen
, cmd
))
1875 * Add encapsulation information when installing via
1879 if (!netlink_route_nexthop_encap(
1880 &req
->n
, datalen
, nexthop
))
1886 if (p
->family
== AF_INET
) {
1887 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1888 &src
.ipv4
, bytelen
))
1890 } else if (p
->family
== AF_INET6
) {
1891 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1892 &src
.ipv6
, bytelen
))
1896 } else { /* Multipath case */
1897 struct rtattr
*nest
;
1898 const union g_addr
*src1
= NULL
;
1900 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
1905 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1906 if (CHECK_FLAG(nexthop
->flags
,
1907 NEXTHOP_FLAG_RECURSIVE
)) {
1908 /* This only works for IPv4 now */
1912 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1917 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1918 routedesc
= nexthop
->rparent
1919 ? "recursive, multipath"
1923 if (!_netlink_route_build_multipath(
1924 p
, routedesc
, bytelen
, nexthop
,
1925 &req
->n
, datalen
, &req
->r
, &src1
))
1928 if (!setsrc
&& src1
) {
1929 if (p
->family
== AF_INET
)
1930 src
.ipv4
= src1
->ipv4
;
1931 else if (p
->family
== AF_INET6
)
1932 src
.ipv6
= src1
->ipv6
;
1939 nl_attr_nest_end(&req
->n
, nest
);
1942 * Add encapsulation information when installing via
1946 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1948 if (CHECK_FLAG(nexthop
->flags
,
1949 NEXTHOP_FLAG_RECURSIVE
))
1951 if (!netlink_route_nexthop_encap(
1952 &req
->n
, datalen
, nexthop
))
1959 if (p
->family
== AF_INET
) {
1960 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1961 &src
.ipv4
, bytelen
))
1963 } else if (p
->family
== AF_INET6
) {
1964 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1965 &src
.ipv6
, bytelen
))
1968 if (IS_ZEBRA_DEBUG_KERNEL
)
1969 zlog_debug("Setting source");
1973 /* If there is no useful nexthop then return. */
1974 if (nexthop_num
== 0) {
1975 if (IS_ZEBRA_DEBUG_KERNEL
)
1976 zlog_debug("%s: No useful nexthop.", __func__
);
1979 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1982 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1984 uint32_t actual_table
;
1986 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1994 struct zebra_ns
*zns
;
1997 memset(&req
, 0, sizeof(req
));
1999 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2000 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2001 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2003 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2004 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2006 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2007 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2008 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2009 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2013 * So during the namespace cleanup we started storing
2014 * the zvrf table_id for the default table as RT_TABLE_MAIN
2015 * which is what the normal routing table for ip routing is.
2016 * This change caused this to break our lookups of sg data
2017 * because prior to this change the zvrf->table_id was 0
2018 * and when the pim multicast kernel code saw a 0,
2019 * it was auto-translated to RT_TABLE_DEFAULT. But since
2020 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2021 * and the kernel goes screw you and the delicious cookies you
2022 * are trying to give me. So now we have this little hack.
2024 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2026 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2028 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2029 &zns
->netlink_cmd
, zns
, 0);
2035 /* Char length to debug ID with */
2036 #define ID_LENGTH 10
2038 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2040 const struct nh_grp
*z_grp
,
2041 const uint8_t count
)
2043 struct nexthop_grp grp
[count
];
2044 /* Need space for max group size, "/", and null term */
2045 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2046 char buf1
[ID_LENGTH
+ 2];
2050 memset(grp
, 0, sizeof(grp
));
2053 for (int i
= 0; i
< count
; i
++) {
2054 grp
[i
].id
= z_grp
[i
].id
;
2055 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2057 if (IS_ZEBRA_DEBUG_KERNEL
) {
2059 snprintf(buf
, sizeof(buf1
), "group %u",
2062 snprintf(buf1
, sizeof(buf1
), "/%u",
2064 strlcat(buf
, buf1
, sizeof(buf
));
2068 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2069 count
* sizeof(*grp
)))
2073 if (IS_ZEBRA_DEBUG_KERNEL
)
2074 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2080 * Next hop packet encoding helper function.
2082 * \param[in] cmd netlink command.
2083 * \param[in] ctx dataplane context (information snapshot).
2084 * \param[out] buf buffer to hold the packet.
2085 * \param[in] buflen amount of buffer bytes.
2087 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2088 * otherwise the number of bytes written to buf.
2090 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2091 const struct zebra_dplane_ctx
*ctx
,
2092 void *buf
, size_t buflen
)
2100 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2101 char label_buf
[256];
2103 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2104 int type
= dplane_ctx_get_nhe_type(ctx
);
2105 struct rtattr
*nest
;
2110 EC_ZEBRA_NHG_FIB_UPDATE
,
2111 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2116 * Nothing to do if the kernel doesn't support nexthop objects or
2117 * we dont want to install this type of NHG
2119 if (!kernel_nexthops_supported()) {
2120 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2122 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2123 __func__
, id
, zebra_route_string(type
));
2127 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2128 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2130 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2131 __func__
, id
, zebra_route_string(type
));
2135 label_buf
[0] = '\0';
2137 if (buflen
< sizeof(*req
))
2140 memset(req
, 0, sizeof(*req
));
2142 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2143 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2145 if (cmd
== RTM_NEWNEXTHOP
)
2146 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2148 req
->n
.nlmsg_type
= cmd
;
2149 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2151 req
->nhm
.nh_family
= AF_UNSPEC
;
2154 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2157 if (cmd
== RTM_NEWNEXTHOP
) {
2159 * We distinguish between a "group", which is a collection
2160 * of ids, and a singleton nexthop with an id. The
2161 * group is installed as an id that just refers to a list of
2164 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2165 if (!_netlink_nexthop_build_group(
2166 &req
->n
, buflen
, id
,
2167 dplane_ctx_get_nhe_nh_grp(ctx
),
2168 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2171 const struct nexthop
*nh
=
2172 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2173 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2176 req
->nhm
.nh_family
= AF_INET
;
2177 else if (afi
== AFI_IP6
)
2178 req
->nhm
.nh_family
= AF_INET6
;
2181 case NEXTHOP_TYPE_IPV4
:
2182 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2183 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2188 case NEXTHOP_TYPE_IPV6
:
2189 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2190 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2195 case NEXTHOP_TYPE_BLACKHOLE
:
2196 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2199 /* Blackhole shouldn't have anymore attributes
2202 case NEXTHOP_TYPE_IFINDEX
:
2203 /* Don't need anymore info for this */
2209 EC_ZEBRA_NHG_FIB_UPDATE
,
2210 "Context received for kernel nexthop update without an interface");
2214 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2218 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2219 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2222 build_label_stack(nh
->nh_label
, out_lse
,
2223 label_buf
, sizeof(label_buf
));
2226 /* Set the BoS bit */
2227 out_lse
[num_labels
- 1] |=
2228 htonl(1 << MPLS_LS_S_SHIFT
);
2231 * TODO: MPLS unsupported for now in kernel.
2233 if (req
->nhm
.nh_family
== AF_MPLS
)
2236 encap
= LWTUNNEL_ENCAP_MPLS
;
2237 if (!nl_attr_put16(&req
->n
, buflen
,
2238 NHA_ENCAP_TYPE
, encap
))
2240 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2244 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2246 num_labels
* sizeof(mpls_lse_t
)))
2249 nl_attr_nest_end(&req
->n
, nest
);
2254 if (IS_ZEBRA_DEBUG_KERNEL
)
2255 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2256 __func__
, id
, nh
, nh
->ifindex
,
2257 vrf_id_to_name(nh
->vrf_id
),
2258 nh
->vrf_id
, label_buf
);
2261 req
->nhm
.nh_protocol
= zebra2proto(type
);
2263 } else if (cmd
!= RTM_DELNEXTHOP
) {
2265 EC_ZEBRA_NHG_FIB_UPDATE
,
2266 "Nexthop group kernel update command (%d) does not exist",
2271 if (IS_ZEBRA_DEBUG_KERNEL
)
2272 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2275 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2278 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2279 void *buf
, size_t buflen
)
2281 enum dplane_op_e op
;
2284 op
= dplane_ctx_get_op(ctx
);
2285 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2286 cmd
= RTM_NEWNEXTHOP
;
2287 else if (op
== DPLANE_OP_NH_DELETE
)
2288 cmd
= RTM_DELNEXTHOP
;
2290 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2291 "Context received for kernel nexthop update with incorrect OP code (%u)",
2296 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2299 enum netlink_msg_status
2300 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2301 struct zebra_dplane_ctx
*ctx
)
2303 /* Nothing to do if the kernel doesn't support nexthop objects */
2304 if (!kernel_nexthops_supported())
2305 return FRR_NETLINK_SUCCESS
;
2307 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2311 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2312 void *buf
, size_t buflen
)
2314 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2315 buflen
, false, false);
2318 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2319 void *buf
, size_t buflen
)
2321 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2322 buflen
, false, false);
2325 enum netlink_msg_status
2326 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2329 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2331 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2333 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2335 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2337 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2338 /* Single 'replace' operation */
2341 * With route replace semantics in place
2342 * for v4 routes and the new route is a system
2343 * route we do not install anything.
2344 * The problem here is that the new system
2345 * route should cause us to withdraw from
2346 * the kernel the old non-system route
2348 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2349 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2350 netlink_batch_add_msg(
2351 bth
, ctx
, netlink_delroute_msg_encoder
,
2355 * So v6 route replace semantics are not in
2356 * the kernel at this point as I understand it.
2357 * so let's do a delete then an add.
2358 * In the future once v6 route replace semantics
2359 * are in we can figure out what to do here to
2360 * allow working with old and new kernels.
2362 * I'm also intentionally ignoring the failure case
2363 * of the route delete. If that happens yeah we're
2366 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2367 netlink_batch_add_msg(
2368 bth
, ctx
, netlink_delroute_msg_encoder
,
2374 return FRR_NETLINK_ERROR
;
2376 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2377 return FRR_NETLINK_SUCCESS
;
2379 return netlink_batch_add_msg(bth
, ctx
,
2381 ? netlink_newroute_msg_encoder
2382 : netlink_delroute_msg_encoder
,
2387 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2389 * @tb: Netlink RTA data
2390 * @family: Address family in the nhmsg
2391 * @ifp: Interface connected - this should be NULL, we fill it in
2392 * @ns_id: Namspace id
2394 * Return: New nexthop
2396 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2397 unsigned char family
,
2398 struct interface
**ifp
,
2401 struct nexthop nh
= {};
2403 enum nexthop_types_t type
= 0;
2406 struct interface
*ifp_lookup
;
2408 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2411 if (tb
[NHA_GATEWAY
]) {
2414 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2418 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2423 EC_ZEBRA_BAD_NHG_MESSAGE
,
2424 "Nexthop gateway with bad address family (%d) received from kernel",
2428 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2430 type
= NEXTHOP_TYPE_IFINDEX
;
2436 memcpy(&(nh
.gate
), gate
, sz
);
2439 nh
.ifindex
= if_index
;
2442 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2447 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2450 EC_ZEBRA_UNKNOWN_INTERFACE
,
2451 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2452 __func__
, nh
.ifindex
);
2454 nh
.vrf_id
= VRF_DEFAULT
;
2457 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2458 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2461 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2463 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2464 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2467 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2474 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2475 struct nh_grp
*z_grp
, int z_grp_size
)
2478 /* linux/nexthop.h group struct */
2479 struct nexthop_grp
*n_grp
= NULL
;
2481 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2482 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2484 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2485 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2486 "Invalid nexthop group received from the kernel");
2490 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2491 z_grp
[i
].id
= n_grp
[i
].id
;
2492 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2498 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2500 * @h: Netlink message header
2501 * @ns_id: Namspace id
2502 * @startup: Are we reading under startup conditions?
2504 * Return: Result status
2506 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2509 /* nexthop group id */
2511 unsigned char family
;
2513 afi_t afi
= AFI_UNSPEC
;
2514 vrf_id_t vrf_id
= VRF_DEFAULT
;
2515 struct interface
*ifp
= NULL
;
2516 struct nhmsg
*nhm
= NULL
;
2517 struct nexthop nh
= {};
2518 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2519 /* Count of nexthops in group array */
2520 uint8_t grp_count
= 0;
2521 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2523 nhm
= NLMSG_DATA(h
);
2528 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2531 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2534 "%s: Message received from netlink is of a broken size %d %zu",
2535 __func__
, h
->nlmsg_len
,
2536 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2540 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2545 EC_ZEBRA_BAD_NHG_MESSAGE
,
2546 "Nexthop group without an ID received from the kernel");
2550 /* We use the ID key'd nhg table for kernel updates */
2551 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2553 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2554 /* If this is a L2 NH just ignore it */
2555 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2556 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2562 family
= nhm
->nh_family
;
2563 afi
= family2afi(family
);
2565 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2567 if (IS_ZEBRA_DEBUG_KERNEL
)
2568 zlog_debug("%s ID (%u) %s NS %u",
2569 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2570 nl_family_to_str(family
), ns_id
);
2573 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2574 if (tb
[NHA_GROUP
]) {
2576 * If this is a group message its only going to have
2577 * an array of nexthop IDs associated with it
2579 grp_count
= netlink_nexthop_process_group(
2580 tb
, grp
, array_size(grp
));
2582 if (tb
[NHA_BLACKHOLE
]) {
2584 * This nexthop is just for blackhole-ing
2585 * traffic, it should not have an OIF, GATEWAY,
2588 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2589 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2590 } else if (tb
[NHA_OIF
])
2592 * This is a true new nexthop, so we need
2593 * to parse the gateway and device info
2595 nh
= netlink_nexthop_process_nh(tb
, family
,
2600 EC_ZEBRA_BAD_NHG_MESSAGE
,
2601 "Invalid Nexthop message received from the kernel with ID (%u)",
2605 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2606 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2607 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2611 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2615 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2616 zebra_nhg_kernel_del(id
, vrf_id
);
2622 * netlink_request_nexthop() - Request nextop information from the kernel
2623 * @zns: Zebra namespace
2624 * @family: AF_* netlink family
2625 * @type: RTM_* route type
2627 * Return: Result status
2629 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2636 /* Form the request, specifying filter (rtattr) if needed. */
2637 memset(&req
, 0, sizeof(req
));
2638 req
.n
.nlmsg_type
= type
;
2639 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2640 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2641 req
.nhm
.nh_family
= family
;
2643 return netlink_request(&zns
->netlink_cmd
, &req
);
2648 * netlink_nexthop_read() - Nexthop read function using netlink interface
2650 * @zns: Zebra name space
2652 * Return: Result status
2653 * Only called at bootstrap time.
2655 int netlink_nexthop_read(struct zebra_ns
*zns
)
2658 struct zebra_dplane_info dp_info
;
2660 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2662 /* Get nexthop objects */
2663 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2666 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2670 /* If we succesfully read in nexthop objects,
2671 * this kernel must support them.
2675 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2676 zlog_debug("Nexthop objects %ssupported on this kernel",
2677 supports_nh
? "" : "not ");
2683 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2684 int llalen
, ns_id_t ns_id
)
2686 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2687 addr
, lla
, llalen
, ns_id
);
2691 * netlink_neigh_update_msg_encode() - Common helper api for encoding
2692 * evpn neighbor update as netlink messages using dataplane context object.
2693 * Here, a neighbor refers to a bridge forwarding database entry for
2694 * either unicast forwarding or head-end replication or an IP neighbor
2696 * @ctx: Dataplane context
2697 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
2698 * @mac: A neighbor cache link layer address
2699 * @ip: A neighbor cache n/w layer destination address
2700 * In the case of bridge FDB, this represnts the remote
2702 * @replace_obj: Whether NEW request should replace existing object or
2703 * add to the end of the list
2704 * @family: AF_* netlink family
2705 * @type: RTN_* route type
2706 * @flags: NTF_* flags
2707 * @state: NUD_* states
2708 * @data: data buffer pointer
2709 * @datalen: total amount of data buffer space
2711 * Return: 0 when the msg doesn't fit entirely in the buffer
2712 * otherwise the number of bytes written to buf.
2714 static ssize_t
netlink_neigh_update_msg_encode(
2715 const struct zebra_dplane_ctx
*ctx
, int cmd
, const struct ethaddr
*mac
,
2716 const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
, uint8_t type
,
2717 uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
2718 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
2721 uint8_t protocol
= RTPROT_ZEBRA
;
2728 enum dplane_op_e op
;
2730 if (datalen
< sizeof(*req
))
2732 memset(req
, 0, sizeof(*req
));
2734 op
= dplane_ctx_get_op(ctx
);
2736 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2737 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2738 if (cmd
== RTM_NEWNEIGH
)
2739 req
->n
.nlmsg_flags
|=
2741 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
2742 req
->n
.nlmsg_type
= cmd
;
2743 req
->ndm
.ndm_family
= family
;
2744 req
->ndm
.ndm_type
= type
;
2745 req
->ndm
.ndm_state
= state
;
2746 req
->ndm
.ndm_flags
= flags
;
2747 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2749 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
2754 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, mac
, 6))
2759 struct rtattr
*nest
;
2761 nest
= nl_attr_nest(&req
->n
, datalen
,
2762 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
2766 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
2767 &nfy_flags
, sizeof(nfy_flags
)))
2769 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
2772 nl_attr_nest_end(&req
->n
, nest
);
2777 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
2783 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
2787 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2788 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
2793 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
2794 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
2797 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
2801 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
2802 dplane_ctx_mac_get_br_ifindex(ctx
)))
2806 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2810 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2811 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2814 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
2815 void *buf
, size_t buflen
)
2817 struct ethaddr dst_mac
= {.octet
= {0}};
2819 return netlink_neigh_update_msg_encode(
2820 ctx
, cmd
, &dst_mac
, dplane_ctx_neigh_get_ipaddr(ctx
), false,
2821 PF_BRIDGE
, 0, NTF_SELF
, (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/,
2822 false /*nfy*/, 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/,
2827 #define NDA_RTA(r) \
2828 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2831 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2834 struct interface
*ifp
;
2835 struct zebra_if
*zif
;
2836 struct rtattr
*tb
[NDA_MAX
+ 1];
2837 struct interface
*br_if
;
2840 struct in_addr vtep_ip
;
2841 int vid_present
= 0, dst_present
= 0;
2842 char buf
[ETHER_ADDR_STRLEN
];
2846 bool local_inactive
= false;
2847 bool dp_static
= false;
2848 uint32_t nhg_id
= 0;
2850 ndm
= NLMSG_DATA(h
);
2852 /* We only process macfdb notifications if EVPN is enabled */
2853 if (!is_evpn_enabled())
2856 /* Parse attributes and extract fields of interest. Do basic
2857 * validation of the fields.
2859 memset(tb
, 0, sizeof tb
);
2860 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
2863 if (!tb
[NDA_LLADDR
]) {
2864 if (IS_ZEBRA_DEBUG_KERNEL
)
2865 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
2866 nl_msg_type_to_str(h
->nlmsg_type
),
2871 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2872 if (IS_ZEBRA_DEBUG_KERNEL
)
2874 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
2875 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
2876 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2880 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2882 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2884 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2885 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2889 /* TODO: Only IPv4 supported now. */
2891 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
2893 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
2898 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
2900 if (ndm
->ndm_state
& NUD_STALE
)
2901 local_inactive
= true;
2903 if (tb
[NDA_FDB_EXT_ATTRS
]) {
2904 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
2905 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
2907 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
2908 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
2911 nfy_flags
= *(uint8_t *)RTA_DATA(
2912 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
2913 if (nfy_flags
& FDB_NOTIFY_BIT
)
2915 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
2916 local_inactive
= true;
2920 if (IS_ZEBRA_DEBUG_KERNEL
)
2921 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %s%s nhg %d",
2922 nl_msg_type_to_str(h
->nlmsg_type
),
2923 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2924 ndm
->ndm_state
, ndm
->ndm_flags
,
2925 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2926 dst_present
? dst_buf
: "", nhg_id
);
2928 /* The interface should exist. */
2929 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2931 if (!ifp
|| !ifp
->info
)
2934 /* The interface should be something we're interested in. */
2935 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2938 zif
= (struct zebra_if
*)ifp
->info
;
2939 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2940 if (IS_ZEBRA_DEBUG_KERNEL
)
2942 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
2943 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
2945 zif
->brslave_info
.bridge_ifindex
);
2949 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
2951 if (filter_vlan
&& vid
!= filter_vlan
) {
2952 if (IS_ZEBRA_DEBUG_KERNEL
)
2953 zlog_debug(" Filtered due to filter vlan: %d",
2958 /* If add or update, do accordingly if learnt on a "local" interface; if
2959 * the notification is over VxLAN, this has to be related to
2961 * so perform an implicit delete of any local entry (if it exists).
2963 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2964 /* Drop "permanent" entries. */
2965 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2966 if (IS_ZEBRA_DEBUG_KERNEL
)
2968 " Dropping entry because of NUD_PERMANENT");
2972 if (IS_ZEBRA_IF_VXLAN(ifp
))
2973 return zebra_vxlan_dp_network_mac_add(
2974 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
2975 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
2977 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2978 sticky
, local_inactive
, dp_static
);
2981 /* This is a delete notification.
2982 * Ignore the notification with IP dest as it may just signify that the
2983 * MAC has moved from remote to local. The exception is the special
2984 * all-zeros MAC that represents the BUM flooding entry; we may have
2985 * to readd it. Otherwise,
2986 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2987 * 2. For a MAC over "local" interface, delete the mac
2988 * Note: We will get notifications from both bridge driver and VxLAN
2995 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2997 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
2998 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
3002 if (IS_ZEBRA_IF_VXLAN(ifp
))
3003 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3005 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3008 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3013 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3016 /* Length validity. */
3017 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3021 /* We are interested only in AF_BRIDGE notifications. */
3022 ndm
= NLMSG_DATA(h
);
3023 if (ndm
->ndm_family
!= AF_BRIDGE
)
3026 return netlink_macfdb_change(h
, len
, ns_id
);
3029 /* Request for MAC FDB information from the kernel */
3030 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3031 int type
, ifindex_t master_ifindex
)
3035 struct ifinfomsg ifm
;
3039 /* Form the request, specifying filter (rtattr) if needed. */
3040 memset(&req
, 0, sizeof(req
));
3041 req
.n
.nlmsg_type
= type
;
3042 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3043 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3044 req
.ifm
.ifi_family
= family
;
3046 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3048 return netlink_request(netlink_cmd
, &req
);
3052 * MAC forwarding database read using netlink interface. This is invoked
3055 int netlink_macfdb_read(struct zebra_ns
*zns
)
3058 struct zebra_dplane_info dp_info
;
3060 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3062 /* Get bridge FDB table. */
3063 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3067 /* We are reading entire table. */
3069 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3076 * MAC forwarding database read using netlink interface. This is for a
3077 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3079 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3080 struct interface
*br_if
)
3082 struct zebra_if
*br_zif
;
3083 struct zebra_if
*zif
;
3084 struct zebra_l2info_vxlan
*vxl
;
3085 struct zebra_dplane_info dp_info
;
3088 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3090 /* Save VLAN we're filtering on, if needed. */
3091 br_zif
= (struct zebra_if
*)br_if
->info
;
3092 zif
= (struct zebra_if
*)ifp
->info
;
3093 vxl
= &zif
->l2info
.vxl
;
3094 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3095 filter_vlan
= vxl
->access_vlan
;
3097 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3099 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3103 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3106 /* Reset VLAN filter. */
3112 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3113 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3116 struct interface
*br_if
,
3117 struct ethaddr
*mac
,
3125 struct zebra_if
*br_zif
;
3126 char buf
[ETHER_ADDR_STRLEN
];
3128 memset(&req
, 0, sizeof(req
));
3129 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3130 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3131 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3132 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3133 /* req.ndm.ndm_state = NUD_REACHABLE; */
3135 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3137 br_zif
= (struct zebra_if
*)br_if
->info
;
3138 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3139 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3141 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3143 if (IS_ZEBRA_DEBUG_KERNEL
)
3145 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %s vid %u",
3146 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3147 br_if
->name
, br_if
->ifindex
,
3148 vrf_id_to_name(br_if
->vrf_id
), br_if
->vrf_id
,
3149 prefix_mac2str(mac
, buf
, sizeof(buf
)), vid
);
3151 return netlink_request(&zns
->netlink_cmd
, &req
);
3154 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3155 struct interface
*br_if
,
3156 struct ethaddr
*mac
, vlanid_t vid
)
3159 struct zebra_dplane_info dp_info
;
3161 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3163 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3165 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3171 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3178 * Netlink-specific handler for MAC updates using dataplane context object.
3180 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3183 struct ipaddr vtep_ip
;
3190 uint32_t update_flags
;
3192 uint8_t nfy_flags
= 0;
3194 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3195 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3198 state
= NUD_REACHABLE
;
3200 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3201 if (update_flags
& DPLANE_MAC_REMOTE
) {
3203 if (dplane_ctx_mac_is_sticky(ctx
)) {
3204 /* NUD_NOARP prevents the entry from expiring */
3206 /* sticky the entry from moving */
3207 flags
|= NTF_STICKY
;
3209 flags
|= NTF_EXT_LEARNED
;
3211 /* if it was static-local previously we need to clear the
3212 * notify flags on replace with remote
3214 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3218 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3219 nfy_flags
|= FDB_NOTIFY_BIT
;
3223 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3224 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3229 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3230 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3231 SET_IPADDR_V4(&vtep_ip
);
3233 if (IS_ZEBRA_DEBUG_KERNEL
) {
3234 char ipbuf
[PREFIX_STRLEN
];
3235 char buf
[ETHER_ADDR_STRLEN
];
3237 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3239 vid
= dplane_ctx_mac_get_vlan(ctx
);
3241 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3246 "Tx %s family %s IF %s(%u)%s %sMAC %s dst %s nhg %u%s%s%s%s%s",
3247 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3248 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3249 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3250 prefix_mac2str(mac
, buf
, sizeof(buf
)),
3251 ipaddr2str(&vtep_ip
, ipbuf
, sizeof(ipbuf
)), nhg_id
,
3252 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3253 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3255 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3256 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3261 total
= netlink_neigh_update_msg_encode(
3262 ctx
, cmd
, dplane_ctx_mac_get_addr(ctx
), &vtep_ip
, true,
3263 AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
, nfy_flags
,
3264 false /*ext*/, 0 /*ext_flags*/, data
, datalen
);
3270 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3271 * 5549 support, re-install them.
3273 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3274 struct interface
*ifp
, struct ipaddr
*ip
,
3277 if (ndm
->ndm_family
!= AF_INET
)
3280 if (!zif
->v6_2_v4_ll_neigh_entry
)
3283 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3286 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3287 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3292 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3296 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3298 #define NUD_LOCAL_ACTIVE \
3299 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3301 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3304 struct interface
*ifp
;
3305 struct zebra_if
*zif
;
3306 struct rtattr
*tb
[NDA_MAX
+ 1];
3307 struct interface
*link_if
;
3311 char buf
[ETHER_ADDR_STRLEN
];
3312 char buf2
[INET6_ADDRSTRLEN
];
3313 int mac_present
= 0;
3316 bool local_inactive
;
3317 uint32_t ext_flags
= 0;
3318 bool dp_static
= false;
3320 ndm
= NLMSG_DATA(h
);
3322 /* The interface should exist. */
3323 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3325 if (!ifp
|| !ifp
->info
)
3328 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
3329 zif
= (struct zebra_if
*)ifp
->info
;
3331 /* Parse attributes and extract fields of interest. */
3332 memset(tb
, 0, sizeof(tb
));
3333 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3336 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3337 nl_msg_type_to_str(h
->nlmsg_type
),
3338 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3339 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
);
3343 memset(&ip
, 0, sizeof(struct ipaddr
));
3344 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3345 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3347 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3348 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3349 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3350 if (IS_ZEBRA_DEBUG_KERNEL
)
3352 " Neighbor Entry Received is a 5549 entry, finished");
3356 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3357 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3358 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3360 /* The neighbor is present on an SVI. From this, we locate the
3362 * bridge because we're only interested in neighbors on a VxLAN bridge.
3363 * The bridge is located based on the nature of the SVI:
3364 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3366 * and is linked to the bridge
3367 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3371 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3372 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3376 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3379 if (IS_ZEBRA_DEBUG_KERNEL
)
3381 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3385 memset(&mac
, 0, sizeof(struct ethaddr
));
3386 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3387 if (tb
[NDA_LLADDR
]) {
3388 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3389 if (IS_ZEBRA_DEBUG_KERNEL
)
3391 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3396 ifp
->name
, ndm
->ndm_ifindex
,
3397 VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3398 (unsigned long)RTA_PAYLOAD(
3404 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3407 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3408 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3410 if (tb
[NDA_EXT_FLAGS
]) {
3411 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3412 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3416 if (IS_ZEBRA_DEBUG_KERNEL
)
3418 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %s MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3419 nl_msg_type_to_str(h
->nlmsg_type
),
3420 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3421 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3422 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3424 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3426 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3428 /* If the neighbor state is valid for use, process as an add or
3430 * else process as a delete. Note that the delete handling may
3432 * in re-adding the neighbor if it is a valid "remote" neighbor.
3434 if (ndm
->ndm_state
& NUD_VALID
) {
3435 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3437 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3439 /* If EVPN-MH is not enabled we treat STALE
3440 * neighbors as locally-active and advertise
3443 local_inactive
= false;
3445 return zebra_vxlan_handle_kernel_neigh_update(
3446 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3447 is_router
, local_inactive
, dp_static
);
3450 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3453 if (IS_ZEBRA_DEBUG_KERNEL
)
3454 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %s",
3455 nl_msg_type_to_str(h
->nlmsg_type
),
3456 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3457 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3458 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3460 /* Process the delete - it may result in re-adding the neighbor if it is
3461 * a valid "remote" neighbor.
3463 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3466 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3471 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3474 /* Length validity. */
3475 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3479 /* We are interested only in AF_INET or AF_INET6 notifications. */
3480 ndm
= NLMSG_DATA(h
);
3481 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3484 return netlink_neigh_change(h
, len
);
3487 /* Request for IP neighbor information from the kernel */
3488 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3489 int type
, ifindex_t ifindex
)
3497 /* Form the request, specifying filter (rtattr) if needed. */
3498 memset(&req
, 0, sizeof(req
));
3499 req
.n
.nlmsg_type
= type
;
3500 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3501 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3502 req
.ndm
.ndm_family
= family
;
3504 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3506 return netlink_request(netlink_cmd
, &req
);
3510 * IP Neighbor table read using netlink interface. This is invoked
3513 int netlink_neigh_read(struct zebra_ns
*zns
)
3516 struct zebra_dplane_info dp_info
;
3518 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3520 /* Get IP neighbor table. */
3521 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3525 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3532 * IP Neighbor table read using netlink interface. This is for a specific
3535 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3538 struct zebra_dplane_info dp_info
;
3540 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3542 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3546 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3553 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3554 * read using netlink interface.
3556 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3557 int type
, struct ipaddr
*ip
,
3567 /* Form the request, specifying filter (rtattr) if needed. */
3568 memset(&req
, 0, sizeof(req
));
3569 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3570 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3571 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3572 req
.ndm
.ndm_ifindex
= ifindex
;
3574 if (IS_IPADDR_V4(ip
)) {
3575 ipa_len
= IPV4_MAX_BYTELEN
;
3576 req
.ndm
.ndm_family
= AF_INET
;
3579 ipa_len
= IPV6_MAX_BYTELEN
;
3580 req
.ndm
.ndm_family
= AF_INET6
;
3583 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3585 if (IS_ZEBRA_DEBUG_KERNEL
) {
3586 char buf
[INET6_ADDRSTRLEN
];
3588 zlog_debug("%s: Tx %s family %s IF %u IP %s flags 0x%x",
3589 __func__
, nl_msg_type_to_str(type
),
3590 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
,
3591 ipaddr2str(ip
, buf
, sizeof(buf
)), req
.n
.nlmsg_flags
);
3594 return netlink_request(&zns
->netlink_cmd
, &req
);
3597 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3598 struct interface
*vlan_if
)
3601 struct zebra_ns
*zns
;
3602 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3603 char buf
[INET6_ADDRSTRLEN
];
3604 struct zebra_dplane_info dp_info
;
3608 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3610 if (IS_ZEBRA_DEBUG_KERNEL
)
3611 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf %s(%u)",
3612 __func__
, vlan_if
->name
, vlan_if
->ifindex
,
3613 ipaddr2str(ip
, buf
, sizeof(buf
)),
3614 vrf_id_to_name(vlan_if
->vrf_id
), vlan_if
->vrf_id
);
3616 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3621 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3627 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3632 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3635 /* Length validity. */
3636 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3639 "%s: Message received from netlink is of a broken size %d %zu",
3640 __func__
, h
->nlmsg_len
,
3641 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3645 /* Is this a notification for the MAC FDB or IP neighbor table? */
3646 ndm
= NLMSG_DATA(h
);
3647 if (ndm
->ndm_family
== AF_BRIDGE
)
3648 return netlink_macfdb_change(h
, len
, ns_id
);
3650 if (ndm
->ndm_type
!= RTN_UNICAST
)
3653 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3654 return netlink_ipneigh_change(h
, len
, ns_id
);
3657 EC_ZEBRA_UNKNOWN_FAMILY
,
3658 "Invalid address family: %u received from kernel neighbor change: %s",
3659 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3667 * Utility neighbor-update function, using info from dplane context.
3669 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3670 int cmd
, void *buf
, size_t buflen
)
3672 const struct ipaddr
*ip
;
3673 const struct ethaddr
*mac
;
3677 uint32_t update_flags
;
3678 uint32_t ext_flags
= 0;
3681 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3682 mac
= dplane_ctx_neigh_get_mac(ctx
);
3683 if (is_zero_mac(mac
))
3686 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
3687 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3688 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3690 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3692 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
3693 flags
|= NTF_EXT_LEARNED
;
3694 /* if it was static-local previously we need to clear the
3695 * ext flags on replace with remote
3697 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
3702 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
3703 ext_flags
|= NTF_E_MH_PEER_SYNC
;
3705 if (IS_ZEBRA_DEBUG_KERNEL
) {
3706 char buf
[INET6_ADDRSTRLEN
];
3707 char buf2
[ETHER_ADDR_STRLEN
];
3710 "Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x %sext_flags 0x%x",
3711 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
3712 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3713 ipaddr2str(ip
, buf
, sizeof(buf
)),
3714 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
)) : "null",
3715 flags
, state
, ext
? "ext " : "", ext_flags
);
3718 return netlink_neigh_update_msg_encode(
3719 ctx
, cmd
, mac
, ip
, true, family
, RTN_UNICAST
, flags
, state
,
3720 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
, ext_flags
, buf
,
3724 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
3725 void *buf
, size_t buflen
)
3729 switch (dplane_ctx_get_op(ctx
)) {
3730 case DPLANE_OP_NEIGH_INSTALL
:
3731 case DPLANE_OP_NEIGH_UPDATE
:
3732 case DPLANE_OP_NEIGH_DISCOVER
:
3733 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
3735 case DPLANE_OP_NEIGH_DELETE
:
3736 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
3738 case DPLANE_OP_VTEP_ADD
:
3739 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
3742 case DPLANE_OP_VTEP_DELETE
:
3743 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
3754 * Update MAC, using dataplane context object.
3757 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
3758 struct zebra_dplane_ctx
*ctx
)
3760 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
3764 enum netlink_msg_status
3765 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
3767 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
3772 * MPLS label forwarding table change via netlink interface, using dataplane
3773 * context information.
3775 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
3776 void *buf
, size_t buflen
)
3779 const struct nhlfe_list_head
*head
;
3780 const zebra_nhlfe_t
*nhlfe
;
3781 struct nexthop
*nexthop
= NULL
;
3782 unsigned int nexthop_num
;
3783 const char *routedesc
;
3785 struct prefix p
= {0};
3793 if (buflen
< sizeof(*req
))
3796 memset(req
, 0, sizeof(*req
));
3799 * Count # nexthops so we can decide whether to use singlepath
3800 * or multipath case.
3803 head
= dplane_ctx_get_nhlfe_list(ctx
);
3804 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3805 nexthop
= nhlfe
->nexthop
;
3808 if (cmd
== RTM_NEWROUTE
) {
3809 /* Count all selected NHLFEs */
3810 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3811 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3814 /* Count all installed NHLFEs */
3815 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3816 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3821 if ((nexthop_num
== 0) ||
3822 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3825 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3826 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3827 req
->n
.nlmsg_type
= cmd
;
3828 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3830 req
->r
.rtm_family
= AF_MPLS
;
3831 req
->r
.rtm_table
= RT_TABLE_MAIN
;
3832 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3833 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3834 req
->r
.rtm_type
= RTN_UNICAST
;
3836 if (cmd
== RTM_NEWROUTE
) {
3837 /* We do a replace to handle update. */
3838 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
3840 /* set the protocol value if installing */
3841 route_type
= re_type_from_lsp_type(
3842 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3843 req
->r
.rtm_protocol
= zebra2proto(route_type
);
3846 /* Fill destination */
3847 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3848 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
3851 /* Fill nexthops (paths) based on single-path or multipath. The paths
3852 * chosen depend on the operation.
3854 if (nexthop_num
== 1) {
3855 routedesc
= "single-path";
3856 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3860 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3861 nexthop
= nhlfe
->nexthop
;
3865 if ((cmd
== RTM_NEWROUTE
3866 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3867 && CHECK_FLAG(nexthop
->flags
,
3868 NEXTHOP_FLAG_ACTIVE
)))
3869 || (cmd
== RTM_DELROUTE
3870 && (CHECK_FLAG(nhlfe
->flags
,
3871 NHLFE_FLAG_INSTALLED
)
3872 && CHECK_FLAG(nexthop
->flags
,
3873 NEXTHOP_FLAG_FIB
)))) {
3874 /* Add the gateway */
3875 if (!_netlink_mpls_build_singlepath(
3876 &p
, routedesc
, nhlfe
, &req
->n
,
3877 &req
->r
, buflen
, cmd
))
3884 } else { /* Multipath case */
3885 struct rtattr
*nest
;
3886 const union g_addr
*src1
= NULL
;
3888 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
3892 routedesc
= "multipath";
3893 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3897 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3898 nexthop
= nhlfe
->nexthop
;
3902 if ((cmd
== RTM_NEWROUTE
3903 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3904 && CHECK_FLAG(nexthop
->flags
,
3905 NEXTHOP_FLAG_ACTIVE
)))
3906 || (cmd
== RTM_DELROUTE
3907 && (CHECK_FLAG(nhlfe
->flags
,
3908 NHLFE_FLAG_INSTALLED
)
3909 && CHECK_FLAG(nexthop
->flags
,
3910 NEXTHOP_FLAG_FIB
)))) {
3913 /* Build the multipath */
3914 if (!_netlink_mpls_build_multipath(
3915 &p
, routedesc
, nhlfe
, &req
->n
,
3916 buflen
, &req
->r
, &src1
))
3921 /* Add the multipath */
3922 nl_attr_nest_end(&req
->n
, nest
);
3925 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3928 /****************************************************************************
3929 * This code was developed in a branch that didn't have dplane APIs for
3930 * MAC updates. Hence the use of the legacy style. It will be moved to
3931 * the new dplane style pre-merge to master. XXX
3933 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
3940 int cmd
= RTM_NEWNEXTHOP
;
3941 struct zebra_vrf
*zvrf
;
3942 struct zebra_ns
*zns
;
3944 zvrf
= zebra_vrf_get_evpn();
3949 memset(&req
, 0, sizeof(req
));
3951 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3952 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3953 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3954 req
.n
.nlmsg_type
= cmd
;
3955 req
.nhm
.nh_family
= AF_INET
;
3957 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
3959 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
3961 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
3962 &vtep_ip
, IPV4_MAX_BYTELEN
))
3965 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3966 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
3967 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
3970 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3974 static int netlink_fdb_nh_del(uint32_t nh_id
)
3981 int cmd
= RTM_DELNEXTHOP
;
3982 struct zebra_vrf
*zvrf
;
3983 struct zebra_ns
*zns
;
3985 zvrf
= zebra_vrf_get_evpn();
3990 memset(&req
, 0, sizeof(req
));
3992 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3993 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3994 req
.n
.nlmsg_type
= cmd
;
3995 req
.nhm
.nh_family
= AF_UNSPEC
;
3997 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4000 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4001 zlog_debug("Tx %s fdb-nh 0x%x",
4002 nl_msg_type_to_str(cmd
), nh_id
);
4005 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4009 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4010 struct nh_grp
*nh_ids
)
4017 int cmd
= RTM_NEWNEXTHOP
;
4018 struct zebra_vrf
*zvrf
;
4019 struct zebra_ns
*zns
;
4020 struct nexthop_grp grp
[nh_cnt
];
4023 zvrf
= zebra_vrf_get_evpn();
4028 memset(&req
, 0, sizeof(req
));
4030 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4031 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4032 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4033 req
.n
.nlmsg_type
= cmd
;
4034 req
.nhm
.nh_family
= AF_UNSPEC
;
4036 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4038 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4040 memset(&grp
, 0, sizeof(grp
));
4041 for (i
= 0; i
< nh_cnt
; ++i
) {
4042 grp
[i
].id
= nh_ids
[i
].id
;
4043 grp
[i
].weight
= nh_ids
[i
].weight
;
4045 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4046 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4050 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4051 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4055 for (i
= 0; i
< nh_cnt
; ++i
) {
4056 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4058 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4061 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4062 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4065 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4069 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4071 return netlink_fdb_nh_del(nhg_id
);
4074 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4076 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4079 int kernel_del_mac_nh(uint32_t nh_id
)
4081 return netlink_fdb_nh_del(nh_id
);
4084 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4085 struct nh_grp
*nh_ids
)
4087 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4090 int kernel_del_mac_nhg(uint32_t nhg_id
)
4092 return netlink_fdb_nhg_del(nhg_id
);
4095 #endif /* HAVE_NETLINK */