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"
54 #include "zebra/zapi_msg.h"
55 #include "zebra/zebra_ns.h"
56 #include "zebra/zebra_vrf.h"
58 #include "zebra/redistribute.h"
59 #include "zebra/interface.h"
60 #include "zebra/debug.h"
61 #include "zebra/rtadv.h"
62 #include "zebra/zebra_ptm.h"
63 #include "zebra/zebra_mpls.h"
64 #include "zebra/kernel_netlink.h"
65 #include "zebra/rt_netlink.h"
66 #include "zebra/zebra_nhg.h"
67 #include "zebra/zebra_mroute.h"
68 #include "zebra/zebra_vxlan.h"
69 #include "zebra/zebra_errors.h"
70 #include "zebra/zebra_evpn_mh.h"
76 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
77 * UAPI for MAC sync. */
78 #ifndef _UAPI_LINUX_IF_BRIDGE_H
79 #define BR_SPH_LIST_SIZE 10
82 static vlanid_t filter_vlan
= 0;
84 /* We capture whether the current kernel supports nexthop ids; by
85 * default, we'll use them if possible. There's also a configuration
86 * available to _disable_ use of kernel nexthops.
88 static bool supports_nh
;
96 static const char ipv4_ll_buf
[16] = "169.254.0.1";
97 static struct in_addr ipv4_ll
;
99 /* Is this a ipv4 over ipv6 route? */
100 static bool is_route_v4_over_v6(unsigned char rtm_family
,
101 enum nexthop_types_t nexthop_type
)
103 if (rtm_family
== AF_INET
104 && (nexthop_type
== NEXTHOP_TYPE_IPV6
105 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
111 /* Helper to control use of kernel-level nexthop ids */
112 static bool kernel_nexthops_supported(void)
114 return (supports_nh
&& !vrf_is_backend_netns()
115 && zebra_nhg_kernel_nexthops_enabled());
119 * Some people may only want to use NHGs created by protos and not
120 * implicitly created by Zebra. This check accounts for that.
122 static bool proto_nexthops_only(void)
124 return zebra_nhg_proto_nexthops_only();
127 /* Is this a proto created NHG? */
128 static bool is_proto_nhg(uint32_t id
, int type
)
130 /* If type is available, use it as the source of truth */
132 if (type
!= ZEBRA_ROUTE_NHG
)
137 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
144 * The ipv4_ll data structure is used for all 5549
145 * additions to the kernel. Let's figure out the
146 * correct value one time instead for every
147 * install/remove of a 5549 type route
149 void rt_netlink_init(void)
151 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
155 * Mapping from dataplane neighbor flags to netlink flags
157 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
161 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
162 flags
|= NTF_EXT_LEARNED
;
163 if (dplane_flags
& DPLANE_NTF_ROUTER
)
165 if (dplane_flags
& DPLANE_NTF_USE
)
172 * Mapping from dataplane neighbor state to netlink state
174 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
178 if (dplane_state
& DPLANE_NUD_REACHABLE
)
179 state
|= NUD_REACHABLE
;
180 if (dplane_state
& DPLANE_NUD_STALE
)
182 if (dplane_state
& DPLANE_NUD_NOARP
)
184 if (dplane_state
& DPLANE_NUD_PROBE
)
186 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
187 state
|= NUD_INCOMPLETE
;
193 static inline bool is_selfroute(int proto
)
195 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
196 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
197 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
198 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
199 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
200 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
201 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
202 || (proto
== RTPROT_SRTE
)) {
209 static inline int zebra2proto(int proto
)
212 case ZEBRA_ROUTE_BABEL
:
213 proto
= RTPROT_BABEL
;
215 case ZEBRA_ROUTE_BGP
:
218 case ZEBRA_ROUTE_OSPF
:
219 case ZEBRA_ROUTE_OSPF6
:
222 case ZEBRA_ROUTE_STATIC
:
223 proto
= RTPROT_ZSTATIC
;
225 case ZEBRA_ROUTE_ISIS
:
228 case ZEBRA_ROUTE_RIP
:
231 case ZEBRA_ROUTE_RIPNG
:
232 proto
= RTPROT_RIPNG
;
234 case ZEBRA_ROUTE_NHRP
:
237 case ZEBRA_ROUTE_EIGRP
:
238 proto
= RTPROT_EIGRP
;
240 case ZEBRA_ROUTE_LDP
:
243 case ZEBRA_ROUTE_SHARP
:
244 proto
= RTPROT_SHARP
;
246 case ZEBRA_ROUTE_PBR
:
249 case ZEBRA_ROUTE_OPENFABRIC
:
250 proto
= RTPROT_OPENFABRIC
;
252 case ZEBRA_ROUTE_SRTE
:
255 case ZEBRA_ROUTE_TABLE
:
256 case ZEBRA_ROUTE_NHG
:
257 proto
= RTPROT_ZEBRA
;
259 case ZEBRA_ROUTE_CONNECT
:
260 case ZEBRA_ROUTE_KERNEL
:
261 proto
= RTPROT_KERNEL
;
265 * When a user adds a new protocol this will show up
266 * to let them know to do something about it. This
267 * is intentionally a warn because we should see
268 * this as part of development of a new protocol
271 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
273 proto
= RTPROT_ZEBRA
;
280 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
284 proto
= ZEBRA_ROUTE_BABEL
;
287 proto
= ZEBRA_ROUTE_BGP
;
290 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
294 proto
= ZEBRA_ROUTE_ISIS
;
297 proto
= ZEBRA_ROUTE_RIP
;
300 proto
= ZEBRA_ROUTE_RIPNG
;
303 proto
= ZEBRA_ROUTE_NHRP
;
306 proto
= ZEBRA_ROUTE_EIGRP
;
309 proto
= ZEBRA_ROUTE_LDP
;
313 proto
= ZEBRA_ROUTE_STATIC
;
316 proto
= ZEBRA_ROUTE_SHARP
;
319 proto
= ZEBRA_ROUTE_PBR
;
321 case RTPROT_OPENFABRIC
:
322 proto
= ZEBRA_ROUTE_OPENFABRIC
;
325 proto
= ZEBRA_ROUTE_SRTE
;
329 proto
= ZEBRA_ROUTE_NHG
;
332 /* Intentional fall thru */
335 * When a user adds a new protocol this will show up
336 * to let them know to do something about it. This
337 * is intentionally a warn because we should see
338 * this as part of development of a new protocol
341 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
343 proto
= ZEBRA_ROUTE_KERNEL
;
350 Pending: create an efficient table_id (in a tree/hash) based lookup)
352 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
355 struct zebra_vrf
*zvrf
;
357 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
361 /* case vrf with netns : match the netnsid */
362 if (vrf_is_backend_netns()) {
363 if (ns_id
== zvrf_id(zvrf
))
364 return zvrf_id(zvrf
);
366 /* VRF is VRF_BACKEND_VRF_LITE */
367 if (zvrf
->table_id
!= table_id
)
369 return zvrf_id(zvrf
);
377 * @parse_encap_mpls() - Parses encapsulated mpls attributes
378 * @tb: Pointer to rtattr to look for nested items in.
379 * @labels: Pointer to store labels in.
381 * Return: Number of mpls labels found.
383 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
385 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
386 mpls_lse_t
*lses
= NULL
;
391 mpls_label_t label
= 0;
393 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
394 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
395 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
396 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
397 labels
[num_labels
++] = label
;
403 static struct nexthop
404 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
405 enum blackhole_type bh_type
, int index
, void *prefsrc
,
406 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
408 struct interface
*ifp
= NULL
;
409 struct nexthop nh
= {0};
410 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
413 vrf_id_t nh_vrf_id
= vrf_id
;
414 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
416 if (bh_type
== BLACKHOLE_UNSPEC
) {
418 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
419 else if (index
&& gate
)
420 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
421 : NEXTHOP_TYPE_IPV6_IFINDEX
;
422 else if (!index
&& gate
)
423 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
426 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
427 nh
.bh_type
= bh_type
;
430 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
431 nh
.bh_type
= bh_type
;
435 memcpy(&nh
.src
, prefsrc
, sz
);
437 memcpy(&nh
.gate
, gate
, sz
);
440 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
442 nh_vrf_id
= ifp
->vrf_id
;
444 nh
.vrf_id
= nh_vrf_id
;
446 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
447 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
448 == LWTUNNEL_ENCAP_MPLS
) {
449 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
452 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
453 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
456 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
461 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
462 struct nexthop_group
*ng
,
464 struct rtnexthop
*rtnh
,
466 void *prefsrc
, vrf_id_t vrf_id
)
469 struct interface
*ifp
= NULL
;
472 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
474 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
476 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
477 vrf_id_t nh_vrf_id
= vrf_id
;
480 struct nexthop
*nh
= NULL
;
482 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
485 index
= rtnh
->rtnh_ifindex
;
488 * Yes we are looking this up
489 * for every nexthop and just
490 * using the last one looked
493 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
496 nh_vrf_id
= ifp
->vrf_id
;
499 EC_ZEBRA_UNKNOWN_INTERFACE
,
500 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
502 nh_vrf_id
= VRF_DEFAULT
;
507 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
508 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
510 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
511 rtnh
->rtnh_len
- sizeof(*rtnh
));
512 if (rtnh_tb
[RTA_GATEWAY
])
513 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
514 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
515 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
516 == LWTUNNEL_ENCAP_MPLS
) {
517 num_labels
= parse_encap_mpls(
518 rtnh_tb
[RTA_ENCAP
], labels
);
522 if (gate
&& rtm
->rtm_family
== AF_INET
) {
524 nh
= nexthop_from_ipv4_ifindex(
525 gate
, prefsrc
, index
, nh_vrf_id
);
527 nh
= nexthop_from_ipv4(gate
, prefsrc
,
529 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
531 nh
= nexthop_from_ipv6_ifindex(
532 gate
, index
, nh_vrf_id
);
534 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
536 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
539 nh
->weight
= rtnh
->rtnh_hops
+ 1;
542 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
545 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
546 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
548 /* Add to temporary list */
549 nexthop_group_add_sorted(ng
, nh
);
552 if (rtnh
->rtnh_len
== 0)
555 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
556 rtnh
= RTNH_NEXT(rtnh
);
559 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
564 /* Looking up routing table by netlink interface. */
565 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
570 struct rtattr
*tb
[RTA_MAX
+ 1];
573 struct prefix_ipv6 src_p
= {};
577 char anyaddr
[16] = {0};
579 int proto
= ZEBRA_ROUTE_KERNEL
;
584 uint8_t distance
= 0;
590 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
591 void *src
= NULL
; /* IPv6 srcdest source prefix */
592 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
596 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
598 switch (rtm
->rtm_type
) {
602 bh_type
= BLACKHOLE_NULL
;
604 case RTN_UNREACHABLE
:
605 bh_type
= BLACKHOLE_REJECT
;
608 bh_type
= BLACKHOLE_ADMINPROHIB
;
611 if (IS_ZEBRA_DEBUG_KERNEL
)
612 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
613 nl_rttype_to_str(rtm
->rtm_type
),
618 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
621 "%s: Message received from netlink is of a broken size %d %zu",
622 __func__
, h
->nlmsg_len
,
623 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
627 memset(tb
, 0, sizeof(tb
));
628 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
630 if (rtm
->rtm_flags
& RTM_F_CLONED
)
632 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
634 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
637 selfroute
= is_selfroute(rtm
->rtm_protocol
);
639 if (!startup
&& selfroute
640 && h
->nlmsg_type
== RTM_NEWROUTE
641 && !zrouter
.asic_offloaded
) {
642 if (IS_ZEBRA_DEBUG_KERNEL
)
643 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
648 /* We don't care about change notifications for the MPLS table. */
649 /* TODO: Revisit this. */
650 if (rtm
->rtm_family
== AF_MPLS
)
653 /* Table corresponding to route. */
655 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
657 table
= rtm
->rtm_table
;
660 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
661 if (vrf_id
== VRF_DEFAULT
) {
662 if (!is_zebra_valid_kernel_table(table
)
663 && !is_zebra_main_routing_table(table
))
667 if (rtm
->rtm_flags
& RTM_F_TRAP
)
668 flags
|= ZEBRA_FLAG_TRAPPED
;
669 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
670 flags
|= ZEBRA_FLAG_OFFLOADED
;
671 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
672 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
674 /* Route which inserted by Zebra. */
676 flags
|= ZEBRA_FLAG_SELFROUTE
;
677 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
680 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
683 dest
= RTA_DATA(tb
[RTA_DST
]);
688 src
= RTA_DATA(tb
[RTA_SRC
]);
693 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
696 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
699 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
701 if (tb
[RTA_PRIORITY
])
702 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
704 #if defined(SUPPORT_REALMS)
706 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
709 if (tb
[RTA_METRICS
]) {
710 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
712 memset(mxrta
, 0, sizeof(mxrta
));
713 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
714 RTA_PAYLOAD(tb
[RTA_METRICS
]));
717 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
720 if (rtm
->rtm_family
== AF_INET
) {
722 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
724 "Invalid destination prefix length: %u received from kernel route change",
728 memcpy(&p
.u
.prefix4
, dest
, 4);
729 p
.prefixlen
= rtm
->rtm_dst_len
;
731 if (rtm
->rtm_src_len
!= 0) {
733 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
734 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
739 /* Force debug below to not display anything for source */
741 } else if (rtm
->rtm_family
== AF_INET6
) {
743 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
745 "Invalid destination prefix length: %u received from kernel route change",
749 memcpy(&p
.u
.prefix6
, dest
, 16);
750 p
.prefixlen
= rtm
->rtm_dst_len
;
752 src_p
.family
= AF_INET6
;
753 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
755 "Invalid source prefix length: %u received from kernel route change",
759 memcpy(&src_p
.prefix
, src
, 16);
760 src_p
.prefixlen
= rtm
->rtm_src_len
;
764 * For ZEBRA_ROUTE_KERNEL types:
766 * The metric/priority of the route received from the kernel
767 * is a 32 bit number. We are going to interpret the high
768 * order byte as the Admin Distance and the low order 3 bytes
771 * This will allow us to do two things:
772 * 1) Allow the creation of kernel routes that can be
773 * overridden by zebra.
774 * 2) Allow the old behavior for 'most' kernel route types
775 * if a user enters 'ip route ...' v4 routes get a metric
776 * of 0 and v6 routes get a metric of 1024. Both of these
777 * values will end up with a admin distance of 0, which
778 * will cause them to win for the purposes of zebra.
780 if (proto
== ZEBRA_ROUTE_KERNEL
) {
781 distance
= (metric
>> 24) & 0xFF;
782 metric
= (metric
& 0x00FFFFFF);
785 if (IS_ZEBRA_DEBUG_KERNEL
) {
786 char buf2
[PREFIX_STRLEN
];
789 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
790 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
791 src_p
.prefixlen
? " from " : "",
792 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
794 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
799 if (rtm
->rtm_family
== AF_INET6
)
802 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
804 if (!tb
[RTA_MULTIPATH
]) {
805 struct nexthop nh
= {0};
808 nh
= parse_nexthop_unicast(
809 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
812 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
813 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
816 /* This is a multipath route */
817 struct route_entry
*re
;
818 struct nexthop_group
*ng
= NULL
;
819 struct rtnexthop
*rtnh
=
820 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
822 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
824 re
->distance
= distance
;
830 re
->uptime
= monotime(NULL
);
837 /* Use temporary list of nexthops; parse
838 * message payload's nexthops.
840 ng
= nexthop_group_new();
842 parse_multipath_nexthops_unicast(
843 ns_id
, ng
, rtm
, rtnh
, tb
,
846 zserv_nexthop_num_warn(
847 __func__
, (const struct prefix
*)&p
,
851 nexthop_group_delete(&ng
);
857 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
864 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
865 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
868 if (!tb
[RTA_MULTIPATH
]) {
871 nh
= parse_nexthop_unicast(
872 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
874 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
875 flags
, &p
, &src_p
, &nh
, 0, table
,
876 metric
, distance
, true);
878 /* XXX: need to compare the entire list of
879 * nexthops here for NLM_F_APPEND stupidity */
880 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
881 flags
, &p
, &src_p
, NULL
, 0, table
,
882 metric
, distance
, true);
890 static struct mcast_route_data
*mroute
= NULL
;
892 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
893 ns_id_t ns_id
, int startup
)
897 struct rtattr
*tb
[RTA_MAX
+ 1];
898 struct mcast_route_data
*m
;
899 struct mcast_route_data mr
;
904 char oif_list
[256] = "\0";
911 memset(&mr
, 0, sizeof(mr
));
917 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
919 memset(tb
, 0, sizeof(tb
));
920 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
923 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
925 table
= rtm
->rtm_table
;
927 vrf
= vrf_lookup_by_table(table
, ns_id
);
930 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
933 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
936 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
939 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
941 if (tb
[RTA_MULTIPATH
]) {
942 struct rtnexthop
*rtnh
=
943 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
945 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
947 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
950 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
953 if (rtnh
->rtnh_len
== 0)
956 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
957 rtnh
= RTNH_NEXT(rtnh
);
961 if (IS_ZEBRA_DEBUG_KERNEL
) {
962 struct interface
*ifp
= NULL
;
963 struct zebra_vrf
*zvrf
= NULL
;
965 for (count
= 0; count
< oif_count
; count
++) {
966 ifp
= if_lookup_by_index(oif
[count
], vrf
);
969 snprintf(temp
, sizeof(temp
), "%s(%d) ",
970 ifp
? ifp
->name
: "Unknown", oif
[count
]);
971 strlcat(oif_list
, temp
, sizeof(oif_list
));
973 zvrf
= zebra_vrf_lookup_by_id(vrf
);
974 ifp
= if_lookup_by_index(iif
, vrf
);
976 "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld",
977 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
978 &m
->sg
.src
, &m
->sg
.grp
, ifp
? ifp
->name
: "Unknown",
985 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
992 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
993 /* If this is not route add/delete message print warning. */
994 zlog_debug("Kernel message: %s NS %u",
995 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
999 if (!(rtm
->rtm_family
== AF_INET
||
1000 rtm
->rtm_family
== AF_INET6
||
1001 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1003 EC_ZEBRA_UNKNOWN_FAMILY
,
1004 "Invalid address family: %u received from kernel route change: %s",
1005 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1009 /* Connected route. */
1010 if (IS_ZEBRA_DEBUG_KERNEL
)
1011 zlog_debug("%s %s %s proto %s NS %u",
1012 nl_msg_type_to_str(h
->nlmsg_type
),
1013 nl_family_to_str(rtm
->rtm_family
),
1014 nl_rttype_to_str(rtm
->rtm_type
),
1015 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1018 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1021 "%s: Message received from netlink is of a broken size: %d %zu",
1022 __func__
, h
->nlmsg_len
,
1023 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1027 if (rtm
->rtm_type
== RTN_MULTICAST
)
1028 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1030 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1034 /* Request for specific route information from the kernel */
1035 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1042 /* Form the request, specifying filter (rtattr) if needed. */
1043 memset(&req
, 0, sizeof(req
));
1044 req
.n
.nlmsg_type
= type
;
1045 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1046 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1047 req
.rtm
.rtm_family
= family
;
1049 return netlink_request(&zns
->netlink_cmd
, &req
);
1052 /* Routing table read function using netlink interface. Only called
1054 int netlink_route_read(struct zebra_ns
*zns
)
1057 struct zebra_dplane_info dp_info
;
1059 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1061 /* Get IPv4 routing table. */
1062 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1065 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1066 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1070 /* Get IPv6 routing table. */
1071 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1074 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1075 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1083 * The function returns true if the gateway info could be added
1084 * to the message, otherwise false is returned.
1086 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1088 struct nlmsghdr
*nlmsg
,
1089 size_t req_size
, int bytelen
,
1090 const struct nexthop
*nexthop
)
1092 if (route_family
== AF_MPLS
) {
1093 struct gw_family_t gw_fam
;
1095 gw_fam
.family
= gw_family
;
1096 if (gw_family
== AF_INET
)
1097 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1099 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1100 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1104 if (!(nexthop
->rparent
1105 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1106 if (gw_family
== AF_INET
) {
1107 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1108 &nexthop
->gate
.ipv4
, bytelen
))
1111 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1112 &nexthop
->gate
.ipv6
, bytelen
))
1121 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1122 mpls_lse_t
*out_lse
, char *label_buf
,
1123 size_t label_buf_size
)
1125 char label_buf1
[20];
1128 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1129 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1132 if (IS_ZEBRA_DEBUG_KERNEL
) {
1134 snprintf(label_buf
, label_buf_size
, "label %u",
1135 nh_label
->label
[i
]);
1137 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1138 nh_label
->label
[i
]);
1139 strlcat(label_buf
, label_buf1
, label_buf_size
);
1143 out_lse
[num_labels
] =
1144 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1151 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1152 struct nlmsghdr
*nlmsg
,
1153 size_t buflen
, struct rtmsg
*rtmsg
,
1155 size_t label_buf_size
)
1157 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1161 * label_buf is *only* currently used within debugging.
1162 * As such when we assign it we are guarding it inside
1163 * a debug test. If you want to change this make sure
1164 * you fix this assumption
1166 label_buf
[0] = '\0';
1169 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1172 /* Set the BoS bit */
1173 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1175 if (rtmsg
->rtm_family
== AF_MPLS
) {
1176 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1177 num_labels
* sizeof(mpls_lse_t
)))
1180 struct rtattr
*nest
;
1182 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1183 LWTUNNEL_ENCAP_MPLS
))
1186 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1190 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1192 num_labels
* sizeof(mpls_lse_t
)))
1194 nl_attr_nest_end(nlmsg
, nest
);
1201 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1203 struct nlmsghdr
*nlmsg
,
1204 size_t buflen
, int bytelen
)
1206 if (family
== AF_INET
) {
1207 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1208 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1209 &nexthop
->rmap_src
.ipv4
, bytelen
))
1211 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1212 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1213 &nexthop
->src
.ipv4
, bytelen
))
1216 } else if (family
== AF_INET6
) {
1217 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1218 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1219 &nexthop
->rmap_src
.ipv6
, bytelen
))
1221 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1222 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1223 &nexthop
->src
.ipv6
, bytelen
))
1231 /* This function takes a nexthop as argument and adds
1232 * the appropriate netlink attributes to an existing
1235 * @param routedesc: Human readable description of route type
1236 * (direct/recursive, single-/multipath)
1237 * @param bytelen: Length of addresses in bytes.
1238 * @param nexthop: Nexthop information
1239 * @param nlmsg: nlmsghdr structure to fill in.
1240 * @param req_size: The size allocated for the message.
1242 * The function returns true if the nexthop could be added
1243 * to the message, otherwise false is returned.
1245 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1246 const char *routedesc
, int bytelen
,
1247 const struct nexthop
*nexthop
,
1248 struct nlmsghdr
*nlmsg
,
1249 struct rtmsg
*rtmsg
,
1250 size_t req_size
, int cmd
)
1253 char label_buf
[256];
1255 char addrstr
[INET6_ADDRSTRLEN
];
1259 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1261 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1262 req_size
, rtmsg
, label_buf
,
1266 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1267 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1269 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1270 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1271 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1273 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1276 if (cmd
== RTM_NEWROUTE
) {
1277 if (!_netlink_route_encode_nexthop_src(
1278 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1282 if (IS_ZEBRA_DEBUG_KERNEL
)
1283 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1284 __func__
, routedesc
, p
, ipv4_ll_buf
,
1285 label_buf
, nexthop
->ifindex
,
1286 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1290 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1291 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1292 /* Send deletes to the kernel without specifying the next-hop */
1293 if (cmd
!= RTM_DELROUTE
) {
1294 if (!_netlink_route_add_gateway_info(
1295 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1300 if (cmd
== RTM_NEWROUTE
) {
1301 if (!_netlink_route_encode_nexthop_src(
1302 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1306 if (IS_ZEBRA_DEBUG_KERNEL
) {
1307 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1309 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1310 __func__
, routedesc
, p
, addrstr
, label_buf
,
1311 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1316 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1317 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1318 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1319 AF_INET6
, nlmsg
, req_size
,
1323 if (cmd
== RTM_NEWROUTE
) {
1324 if (!_netlink_route_encode_nexthop_src(
1325 nexthop
, AF_INET6
, nlmsg
, req_size
,
1330 if (IS_ZEBRA_DEBUG_KERNEL
) {
1331 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1333 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1334 __func__
, routedesc
, p
, addrstr
, label_buf
,
1335 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1341 * We have the ifindex so we should always send it
1342 * This is especially useful if we are doing route
1345 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1346 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1350 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1351 if (cmd
== RTM_NEWROUTE
) {
1352 if (!_netlink_route_encode_nexthop_src(
1353 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1357 if (IS_ZEBRA_DEBUG_KERNEL
)
1358 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1359 __func__
, routedesc
, p
, nexthop
->ifindex
,
1360 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1366 /* This function takes a nexthop as argument and
1367 * appends to the given netlink msg. If the nexthop
1368 * defines a preferred source, the src parameter
1369 * will be modified to point to that src, otherwise
1370 * it will be kept unmodified.
1372 * @param routedesc: Human readable description of route type
1373 * (direct/recursive, single-/multipath)
1374 * @param bytelen: Length of addresses in bytes.
1375 * @param nexthop: Nexthop information
1376 * @param nlmsg: nlmsghdr structure to fill in.
1377 * @param req_size: The size allocated for the message.
1378 * @param src: pointer pointing to a location where
1379 * the prefsrc should be stored.
1381 * The function returns true if the nexthop could be added
1382 * to the message, otherwise false is returned.
1384 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1385 const char *routedesc
, int bytelen
,
1386 const struct nexthop
*nexthop
,
1387 struct nlmsghdr
*nlmsg
,
1388 size_t req_size
, struct rtmsg
*rtmsg
,
1389 const union g_addr
**src
)
1391 char label_buf
[256];
1393 struct rtnexthop
*rtnh
;
1395 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1401 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1403 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1404 req_size
, rtmsg
, label_buf
,
1408 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1409 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1411 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1412 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1413 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1415 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1416 if (nexthop
->weight
)
1417 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1419 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1420 *src
= &nexthop
->rmap_src
;
1421 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1422 *src
= &nexthop
->src
;
1424 if (IS_ZEBRA_DEBUG_KERNEL
)
1426 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1427 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1428 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1430 nl_attr_rtnh_end(nlmsg
, rtnh
);
1434 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1435 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1436 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1437 nlmsg
, req_size
, bytelen
,
1441 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1442 *src
= &nexthop
->rmap_src
;
1443 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1444 *src
= &nexthop
->src
;
1446 if (IS_ZEBRA_DEBUG_KERNEL
)
1447 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1448 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1449 label_buf
, nexthop
->ifindex
,
1450 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1452 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1453 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1454 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1455 AF_INET6
, nlmsg
, req_size
,
1459 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1460 *src
= &nexthop
->rmap_src
;
1461 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1462 *src
= &nexthop
->src
;
1464 if (IS_ZEBRA_DEBUG_KERNEL
)
1465 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1466 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1467 label_buf
, nexthop
->ifindex
,
1468 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1472 * We have figured out the ifindex so we should always send it
1473 * This is especially useful if we are doing route
1476 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1477 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1480 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1481 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1482 *src
= &nexthop
->rmap_src
;
1483 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1484 *src
= &nexthop
->src
;
1486 if (IS_ZEBRA_DEBUG_KERNEL
)
1487 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1488 __func__
, routedesc
, p
, nexthop
->ifindex
,
1489 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1492 if (nexthop
->weight
)
1493 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1495 nl_attr_rtnh_end(nlmsg
, rtnh
);
1499 static inline bool _netlink_mpls_build_singlepath(const struct prefix
*p
,
1500 const char *routedesc
,
1501 const zebra_nhlfe_t
*nhlfe
,
1502 struct nlmsghdr
*nlmsg
,
1503 struct rtmsg
*rtmsg
,
1504 size_t req_size
, int cmd
)
1509 family
= NHLFE_FAMILY(nhlfe
);
1510 bytelen
= (family
== AF_INET
? 4 : 16);
1511 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1512 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1518 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1519 const zebra_nhlfe_t
*nhlfe
,
1520 struct nlmsghdr
*nlmsg
, size_t req_size
,
1521 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1526 family
= NHLFE_FAMILY(nhlfe
);
1527 bytelen
= (family
== AF_INET
? 4 : 16);
1528 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1529 nhlfe
->nexthop
, nlmsg
, req_size
,
1533 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1535 if (IS_ZEBRA_DEBUG_KERNEL
)
1536 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1537 routedesc
, nl_msg_type_to_str(cmd
), label
);
1540 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1541 int llalen
, ns_id_t ns_id
)
1543 uint8_t protocol
= RTPROT_ZEBRA
;
1550 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1552 memset(&req
, 0, sizeof(req
));
1554 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1555 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1556 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1557 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1559 req
.ndm
.ndm_family
= AF_INET
;
1560 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1561 req
.ndm
.ndm_ifindex
= ifindex
;
1562 req
.ndm
.ndm_type
= RTN_UNICAST
;
1564 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1566 nl_attr_put32(&req
.n
, sizeof(req
), NDA_DST
, addr
);
1567 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1569 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1573 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1576 if (family
== AF_INET
) {
1577 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1578 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1580 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1581 src
->ipv4
= nexthop
->src
.ipv4
;
1584 } else if (family
== AF_INET6
) {
1585 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1586 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1588 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1589 src
->ipv6
= nexthop
->src
.ipv6
;
1598 * The function returns true if the attribute could be added
1599 * to the message, otherwise false is returned.
1601 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1604 struct rtattr
*nest
;
1606 switch (nh
->nh_encap_type
) {
1608 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1611 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1615 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1618 nl_attr_nest_end(n
, nest
);
1626 * Routing table change via netlink interface, using a dataplane context object
1628 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1629 * otherwise the number of bytes written to buf.
1631 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1632 struct zebra_dplane_ctx
*ctx
,
1633 uint8_t *data
, size_t datalen
,
1634 bool fpm
, bool force_nhg
)
1637 struct nexthop
*nexthop
= NULL
;
1638 unsigned int nexthop_num
;
1639 const char *routedesc
;
1640 bool setsrc
= false;
1642 const struct prefix
*p
, *src_p
;
1649 } *req
= (void *)data
;
1651 p
= dplane_ctx_get_dest(ctx
);
1652 src_p
= dplane_ctx_get_src(ctx
);
1654 if (datalen
< sizeof(*req
))
1657 memset(req
, 0, sizeof(*req
));
1659 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1661 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1662 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1664 if ((cmd
== RTM_NEWROUTE
) &&
1665 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1666 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1668 req
->n
.nlmsg_type
= cmd
;
1670 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1672 req
->r
.rtm_family
= p
->family
;
1673 req
->r
.rtm_dst_len
= p
->prefixlen
;
1674 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1675 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1677 if (cmd
== RTM_DELROUTE
)
1678 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1680 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1683 * blackhole routes are not RTN_UNICAST, they are
1684 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1685 * so setting this value as a RTN_UNICAST would
1686 * cause the route lookup of just the prefix
1687 * to fail. So no need to specify this for
1688 * the RTM_DELROUTE case
1690 if (cmd
!= RTM_DELROUTE
)
1691 req
->r
.rtm_type
= RTN_UNICAST
;
1693 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1696 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1702 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1704 * either by the kernel or by zebra. Its purely for calculating best
1706 * by the routing protocol and for communicating with protocol peers.
1708 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1709 NL_DEFAULT_ROUTE_METRIC
))
1712 #if defined(SUPPORT_REALMS)
1716 if (cmd
== RTM_DELROUTE
)
1717 tag
= dplane_ctx_get_old_tag(ctx
);
1719 tag
= dplane_ctx_get_tag(ctx
);
1721 if (tag
> 0 && tag
<= 255) {
1722 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
1727 /* Table corresponding to this route. */
1728 table_id
= dplane_ctx_get_table(ctx
);
1730 req
->r
.rtm_table
= table_id
;
1732 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
1733 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
1737 if (IS_ZEBRA_DEBUG_KERNEL
)
1739 "%s: %s %pFX vrf %u(%u)", __func__
,
1740 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
1744 * If we are not updating the route and we have received
1745 * a route delete, then all we need to fill in is the
1746 * prefix information to tell the kernel to schwack
1749 if (cmd
== RTM_DELROUTE
)
1750 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1752 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1753 struct rtattr
*nest
;
1754 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1755 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1757 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1760 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
1764 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
1766 nl_attr_nest_end(&req
->n
, nest
);
1770 * Always install blackhole routes without using nexthops, because of
1771 * the following kernel problems:
1772 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
1773 * 2. Blackhole kernel nexthops are deleted when loopback is down.
1775 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
1777 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1778 nexthop
= nexthop
->resolved
;
1780 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1781 switch (nexthop
->bh_type
) {
1782 case BLACKHOLE_ADMINPROHIB
:
1783 req
->r
.rtm_type
= RTN_PROHIBIT
;
1785 case BLACKHOLE_REJECT
:
1786 req
->r
.rtm_type
= RTN_UNREACHABLE
;
1789 req
->r
.rtm_type
= RTN_BLACKHOLE
;
1792 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1796 if ((!fpm
&& kernel_nexthops_supported()
1797 && (!proto_nexthops_only()
1798 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
1799 || (fpm
&& force_nhg
)) {
1800 /* Kernel supports nexthop objects */
1801 if (IS_ZEBRA_DEBUG_KERNEL
)
1802 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
1803 dplane_ctx_get_nhe_id(ctx
));
1805 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
1806 dplane_ctx_get_nhe_id(ctx
)))
1809 /* Have to determine src still */
1810 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1814 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
1818 if (p
->family
== AF_INET
) {
1819 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1820 &src
.ipv4
, bytelen
))
1822 } else if (p
->family
== AF_INET6
) {
1823 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1824 &src
.ipv6
, bytelen
))
1829 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1832 /* Count overall nexthops so we can decide whether to use singlepath
1833 * or multipath case.
1836 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1837 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1839 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1845 /* Singlepath case. */
1846 if (nexthop_num
== 1) {
1848 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1849 if (CHECK_FLAG(nexthop
->flags
,
1850 NEXTHOP_FLAG_RECURSIVE
)) {
1855 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1860 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1861 routedesc
= nexthop
->rparent
1862 ? "recursive, single-path"
1865 if (!_netlink_route_build_singlepath(
1866 p
, routedesc
, bytelen
, nexthop
,
1867 &req
->n
, &req
->r
, datalen
, cmd
))
1874 * Add encapsulation information when installing via
1878 if (!netlink_route_nexthop_encap(
1879 &req
->n
, datalen
, nexthop
))
1885 if (p
->family
== AF_INET
) {
1886 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1887 &src
.ipv4
, bytelen
))
1889 } else if (p
->family
== AF_INET6
) {
1890 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1891 &src
.ipv6
, bytelen
))
1895 } else { /* Multipath case */
1896 struct rtattr
*nest
;
1897 const union g_addr
*src1
= NULL
;
1899 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
1904 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1905 if (CHECK_FLAG(nexthop
->flags
,
1906 NEXTHOP_FLAG_RECURSIVE
)) {
1907 /* This only works for IPv4 now */
1911 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1916 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1917 routedesc
= nexthop
->rparent
1918 ? "recursive, multipath"
1922 if (!_netlink_route_build_multipath(
1923 p
, routedesc
, bytelen
, nexthop
,
1924 &req
->n
, datalen
, &req
->r
, &src1
))
1927 if (!setsrc
&& src1
) {
1928 if (p
->family
== AF_INET
)
1929 src
.ipv4
= src1
->ipv4
;
1930 else if (p
->family
== AF_INET6
)
1931 src
.ipv6
= src1
->ipv6
;
1938 nl_attr_nest_end(&req
->n
, nest
);
1941 * Add encapsulation information when installing via
1945 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1947 if (CHECK_FLAG(nexthop
->flags
,
1948 NEXTHOP_FLAG_RECURSIVE
))
1950 if (!netlink_route_nexthop_encap(
1951 &req
->n
, datalen
, nexthop
))
1958 if (p
->family
== AF_INET
) {
1959 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1960 &src
.ipv4
, bytelen
))
1962 } else if (p
->family
== AF_INET6
) {
1963 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1964 &src
.ipv6
, bytelen
))
1967 if (IS_ZEBRA_DEBUG_KERNEL
)
1968 zlog_debug("Setting source");
1972 /* If there is no useful nexthop then return. */
1973 if (nexthop_num
== 0) {
1974 if (IS_ZEBRA_DEBUG_KERNEL
)
1975 zlog_debug("%s: No useful nexthop.", __func__
);
1978 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1981 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1983 uint32_t actual_table
;
1985 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1993 struct zebra_ns
*zns
;
1996 memset(&req
, 0, sizeof(req
));
1998 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1999 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2000 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2002 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2003 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2005 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2006 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2007 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2008 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2012 * So during the namespace cleanup we started storing
2013 * the zvrf table_id for the default table as RT_TABLE_MAIN
2014 * which is what the normal routing table for ip routing is.
2015 * This change caused this to break our lookups of sg data
2016 * because prior to this change the zvrf->table_id was 0
2017 * and when the pim multicast kernel code saw a 0,
2018 * it was auto-translated to RT_TABLE_DEFAULT. But since
2019 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2020 * and the kernel goes screw you and the delicious cookies you
2021 * are trying to give me. So now we have this little hack.
2023 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2025 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2027 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2028 &zns
->netlink_cmd
, zns
, 0);
2034 /* Char length to debug ID with */
2035 #define ID_LENGTH 10
2037 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2039 const struct nh_grp
*z_grp
,
2040 const uint8_t count
)
2042 struct nexthop_grp grp
[count
];
2043 /* Need space for max group size, "/", and null term */
2044 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2045 char buf1
[ID_LENGTH
+ 2];
2049 memset(grp
, 0, sizeof(grp
));
2052 for (int i
= 0; i
< count
; i
++) {
2053 grp
[i
].id
= z_grp
[i
].id
;
2054 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2056 if (IS_ZEBRA_DEBUG_KERNEL
) {
2058 snprintf(buf
, sizeof(buf1
), "group %u",
2061 snprintf(buf1
, sizeof(buf1
), "/%u",
2063 strlcat(buf
, buf1
, sizeof(buf
));
2067 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2068 count
* sizeof(*grp
)))
2072 if (IS_ZEBRA_DEBUG_KERNEL
)
2073 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2079 * Next hop packet encoding helper function.
2081 * \param[in] cmd netlink command.
2082 * \param[in] ctx dataplane context (information snapshot).
2083 * \param[out] buf buffer to hold the packet.
2084 * \param[in] buflen amount of buffer bytes.
2086 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2087 * otherwise the number of bytes written to buf.
2089 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2090 const struct zebra_dplane_ctx
*ctx
,
2091 void *buf
, size_t buflen
)
2099 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2100 char label_buf
[256];
2102 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2103 int type
= dplane_ctx_get_nhe_type(ctx
);
2104 struct rtattr
*nest
;
2109 EC_ZEBRA_NHG_FIB_UPDATE
,
2110 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2115 * Nothing to do if the kernel doesn't support nexthop objects or
2116 * we dont want to install this type of NHG
2118 if (!kernel_nexthops_supported()) {
2119 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2121 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2122 __func__
, id
, zebra_route_string(type
));
2126 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2127 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2129 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2130 __func__
, id
, zebra_route_string(type
));
2134 label_buf
[0] = '\0';
2136 if (buflen
< sizeof(*req
))
2139 memset(req
, 0, sizeof(*req
));
2141 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2142 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2144 if (cmd
== RTM_NEWNEXTHOP
)
2145 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2147 req
->n
.nlmsg_type
= cmd
;
2148 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2150 req
->nhm
.nh_family
= AF_UNSPEC
;
2153 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2156 if (cmd
== RTM_NEWNEXTHOP
) {
2158 * We distinguish between a "group", which is a collection
2159 * of ids, and a singleton nexthop with an id. The
2160 * group is installed as an id that just refers to a list of
2163 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2164 if (!_netlink_nexthop_build_group(
2165 &req
->n
, buflen
, id
,
2166 dplane_ctx_get_nhe_nh_grp(ctx
),
2167 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2170 const struct nexthop
*nh
=
2171 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2172 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2175 req
->nhm
.nh_family
= AF_INET
;
2176 else if (afi
== AFI_IP6
)
2177 req
->nhm
.nh_family
= AF_INET6
;
2180 case NEXTHOP_TYPE_IPV4
:
2181 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2182 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2187 case NEXTHOP_TYPE_IPV6
:
2188 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2189 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2194 case NEXTHOP_TYPE_BLACKHOLE
:
2195 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2198 /* Blackhole shouldn't have anymore attributes
2201 case NEXTHOP_TYPE_IFINDEX
:
2202 /* Don't need anymore info for this */
2208 EC_ZEBRA_NHG_FIB_UPDATE
,
2209 "Context received for kernel nexthop update without an interface");
2213 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2217 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2218 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2221 build_label_stack(nh
->nh_label
, out_lse
,
2222 label_buf
, sizeof(label_buf
));
2225 /* Set the BoS bit */
2226 out_lse
[num_labels
- 1] |=
2227 htonl(1 << MPLS_LS_S_SHIFT
);
2230 * TODO: MPLS unsupported for now in kernel.
2232 if (req
->nhm
.nh_family
== AF_MPLS
)
2235 encap
= LWTUNNEL_ENCAP_MPLS
;
2236 if (!nl_attr_put16(&req
->n
, buflen
,
2237 NHA_ENCAP_TYPE
, encap
))
2239 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2243 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2245 num_labels
* sizeof(mpls_lse_t
)))
2248 nl_attr_nest_end(&req
->n
, nest
);
2253 if (IS_ZEBRA_DEBUG_KERNEL
)
2254 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2255 __func__
, id
, nh
, nh
->ifindex
,
2256 vrf_id_to_name(nh
->vrf_id
),
2257 nh
->vrf_id
, label_buf
);
2260 req
->nhm
.nh_protocol
= zebra2proto(type
);
2262 } else if (cmd
!= RTM_DELNEXTHOP
) {
2264 EC_ZEBRA_NHG_FIB_UPDATE
,
2265 "Nexthop group kernel update command (%d) does not exist",
2270 if (IS_ZEBRA_DEBUG_KERNEL
)
2271 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2274 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2277 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2278 void *buf
, size_t buflen
)
2280 enum dplane_op_e op
;
2283 op
= dplane_ctx_get_op(ctx
);
2284 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2285 cmd
= RTM_NEWNEXTHOP
;
2286 else if (op
== DPLANE_OP_NH_DELETE
)
2287 cmd
= RTM_DELNEXTHOP
;
2289 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2290 "Context received for kernel nexthop update with incorrect OP code (%u)",
2295 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2298 enum netlink_msg_status
2299 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2300 struct zebra_dplane_ctx
*ctx
)
2302 /* Nothing to do if the kernel doesn't support nexthop objects */
2303 if (!kernel_nexthops_supported())
2304 return FRR_NETLINK_SUCCESS
;
2306 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2310 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2311 void *buf
, size_t buflen
)
2313 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2314 buflen
, false, false);
2317 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2318 void *buf
, size_t buflen
)
2320 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2321 buflen
, false, false);
2324 enum netlink_msg_status
2325 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2328 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2330 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2332 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2334 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2336 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2337 /* Single 'replace' operation */
2340 * With route replace semantics in place
2341 * for v4 routes and the new route is a system
2342 * route we do not install anything.
2343 * The problem here is that the new system
2344 * route should cause us to withdraw from
2345 * the kernel the old non-system route
2347 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2348 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2349 netlink_batch_add_msg(
2350 bth
, ctx
, netlink_delroute_msg_encoder
,
2354 * So v6 route replace semantics are not in
2355 * the kernel at this point as I understand it.
2356 * so let's do a delete then an add.
2357 * In the future once v6 route replace semantics
2358 * are in we can figure out what to do here to
2359 * allow working with old and new kernels.
2361 * I'm also intentionally ignoring the failure case
2362 * of the route delete. If that happens yeah we're
2365 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2366 netlink_batch_add_msg(
2367 bth
, ctx
, netlink_delroute_msg_encoder
,
2373 return FRR_NETLINK_ERROR
;
2375 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2376 return FRR_NETLINK_SUCCESS
;
2378 return netlink_batch_add_msg(bth
, ctx
,
2380 ? netlink_newroute_msg_encoder
2381 : netlink_delroute_msg_encoder
,
2386 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2388 * @tb: Netlink RTA data
2389 * @family: Address family in the nhmsg
2390 * @ifp: Interface connected - this should be NULL, we fill it in
2391 * @ns_id: Namspace id
2393 * Return: New nexthop
2395 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2396 unsigned char family
,
2397 struct interface
**ifp
,
2400 struct nexthop nh
= {};
2402 enum nexthop_types_t type
= 0;
2405 struct interface
*ifp_lookup
;
2407 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2410 if (tb
[NHA_GATEWAY
]) {
2413 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2417 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2422 EC_ZEBRA_BAD_NHG_MESSAGE
,
2423 "Nexthop gateway with bad address family (%d) received from kernel",
2427 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2429 type
= NEXTHOP_TYPE_IFINDEX
;
2435 memcpy(&(nh
.gate
), gate
, sz
);
2438 nh
.ifindex
= if_index
;
2441 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2446 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2449 EC_ZEBRA_UNKNOWN_INTERFACE
,
2450 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2451 __func__
, nh
.ifindex
);
2453 nh
.vrf_id
= VRF_DEFAULT
;
2456 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2457 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2460 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2462 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2463 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2466 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2473 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2474 struct nh_grp
*z_grp
, int z_grp_size
)
2477 /* linux/nexthop.h group struct */
2478 struct nexthop_grp
*n_grp
= NULL
;
2480 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2481 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2483 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2484 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2485 "Invalid nexthop group received from the kernel");
2489 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2490 z_grp
[i
].id
= n_grp
[i
].id
;
2491 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2497 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2499 * @h: Netlink message header
2500 * @ns_id: Namspace id
2501 * @startup: Are we reading under startup conditions?
2503 * Return: Result status
2505 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2508 /* nexthop group id */
2510 unsigned char family
;
2512 afi_t afi
= AFI_UNSPEC
;
2513 vrf_id_t vrf_id
= VRF_DEFAULT
;
2514 struct interface
*ifp
= NULL
;
2515 struct nhmsg
*nhm
= NULL
;
2516 struct nexthop nh
= {};
2517 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2518 /* Count of nexthops in group array */
2519 uint8_t grp_count
= 0;
2520 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2522 nhm
= NLMSG_DATA(h
);
2527 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2530 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2533 "%s: Message received from netlink is of a broken size %d %zu",
2534 __func__
, h
->nlmsg_len
,
2535 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2539 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2544 EC_ZEBRA_BAD_NHG_MESSAGE
,
2545 "Nexthop group without an ID received from the kernel");
2549 /* We use the ID key'd nhg table for kernel updates */
2550 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2552 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2553 /* If this is a L2 NH just ignore it */
2554 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2555 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2561 family
= nhm
->nh_family
;
2562 afi
= family2afi(family
);
2564 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2566 if (IS_ZEBRA_DEBUG_KERNEL
)
2567 zlog_debug("%s ID (%u) %s NS %u",
2568 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2569 nl_family_to_str(family
), ns_id
);
2572 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2573 if (tb
[NHA_GROUP
]) {
2575 * If this is a group message its only going to have
2576 * an array of nexthop IDs associated with it
2578 grp_count
= netlink_nexthop_process_group(
2579 tb
, grp
, array_size(grp
));
2581 if (tb
[NHA_BLACKHOLE
]) {
2583 * This nexthop is just for blackhole-ing
2584 * traffic, it should not have an OIF, GATEWAY,
2587 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2588 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2589 } else if (tb
[NHA_OIF
])
2591 * This is a true new nexthop, so we need
2592 * to parse the gateway and device info
2594 nh
= netlink_nexthop_process_nh(tb
, family
,
2599 EC_ZEBRA_BAD_NHG_MESSAGE
,
2600 "Invalid Nexthop message received from the kernel with ID (%u)",
2604 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2605 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2606 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2610 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2614 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2615 zebra_nhg_kernel_del(id
, vrf_id
);
2621 * netlink_request_nexthop() - Request nextop information from the kernel
2622 * @zns: Zebra namespace
2623 * @family: AF_* netlink family
2624 * @type: RTM_* route type
2626 * Return: Result status
2628 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2635 /* Form the request, specifying filter (rtattr) if needed. */
2636 memset(&req
, 0, sizeof(req
));
2637 req
.n
.nlmsg_type
= type
;
2638 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2639 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2640 req
.nhm
.nh_family
= family
;
2642 return netlink_request(&zns
->netlink_cmd
, &req
);
2647 * netlink_nexthop_read() - Nexthop read function using netlink interface
2649 * @zns: Zebra name space
2651 * Return: Result status
2652 * Only called at bootstrap time.
2654 int netlink_nexthop_read(struct zebra_ns
*zns
)
2657 struct zebra_dplane_info dp_info
;
2659 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2661 /* Get nexthop objects */
2662 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2665 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2669 /* If we succesfully read in nexthop objects,
2670 * this kernel must support them.
2674 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2675 zlog_debug("Nexthop objects %ssupported on this kernel",
2676 supports_nh
? "" : "not ");
2682 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2683 int llalen
, ns_id_t ns_id
)
2685 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2686 addr
, lla
, llalen
, ns_id
);
2690 * netlink_neigh_update_msg_encode() - Common helper api for encoding
2691 * evpn neighbor update as netlink messages using dataplane context object.
2692 * Here, a neighbor refers to a bridge forwarding database entry for
2693 * either unicast forwarding or head-end replication or an IP neighbor
2695 * @ctx: Dataplane context
2696 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
2697 * @mac: A neighbor cache link layer address
2698 * @ip: A neighbor cache n/w layer destination address
2699 * In the case of bridge FDB, this represnts the remote
2701 * @replace_obj: Whether NEW request should replace existing object or
2702 * add to the end of the list
2703 * @family: AF_* netlink family
2704 * @type: RTN_* route type
2705 * @flags: NTF_* flags
2706 * @state: NUD_* states
2707 * @data: data buffer pointer
2708 * @datalen: total amount of data buffer space
2710 * Return: 0 when the msg doesn't fit entirely in the buffer
2711 * otherwise the number of bytes written to buf.
2713 static ssize_t
netlink_neigh_update_msg_encode(
2714 const struct zebra_dplane_ctx
*ctx
, int cmd
, const struct ethaddr
*mac
,
2715 const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
, uint8_t type
,
2716 uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
2717 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
2720 uint8_t protocol
= RTPROT_ZEBRA
;
2727 enum dplane_op_e op
;
2729 if (datalen
< sizeof(*req
))
2731 memset(req
, 0, sizeof(*req
));
2733 op
= dplane_ctx_get_op(ctx
);
2735 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2736 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2737 if (cmd
== RTM_NEWNEIGH
)
2738 req
->n
.nlmsg_flags
|=
2740 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
2741 req
->n
.nlmsg_type
= cmd
;
2742 req
->ndm
.ndm_family
= family
;
2743 req
->ndm
.ndm_type
= type
;
2744 req
->ndm
.ndm_state
= state
;
2745 req
->ndm
.ndm_flags
= flags
;
2746 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2748 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
2753 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, mac
, 6))
2758 struct rtattr
*nest
;
2760 nest
= nl_attr_nest(&req
->n
, datalen
,
2761 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
2765 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
2766 &nfy_flags
, sizeof(nfy_flags
)))
2768 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
2771 nl_attr_nest_end(&req
->n
, nest
);
2776 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
2782 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
2786 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2787 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
2792 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
2793 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
2796 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
2800 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
2801 dplane_ctx_mac_get_br_ifindex(ctx
)))
2805 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2809 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2810 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2813 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
2814 void *buf
, size_t buflen
)
2816 struct ethaddr dst_mac
= {.octet
= {0}};
2818 return netlink_neigh_update_msg_encode(
2819 ctx
, cmd
, &dst_mac
, dplane_ctx_neigh_get_ipaddr(ctx
), false,
2820 PF_BRIDGE
, 0, NTF_SELF
, (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/,
2821 false /*nfy*/, 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/,
2826 #define NDA_RTA(r) \
2827 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2830 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2833 struct interface
*ifp
;
2834 struct zebra_if
*zif
;
2835 struct rtattr
*tb
[NDA_MAX
+ 1];
2836 struct interface
*br_if
;
2839 struct in_addr vtep_ip
;
2840 int vid_present
= 0, dst_present
= 0;
2844 bool local_inactive
= false;
2845 bool dp_static
= false;
2846 uint32_t nhg_id
= 0;
2848 ndm
= NLMSG_DATA(h
);
2850 /* We only process macfdb notifications if EVPN is enabled */
2851 if (!is_evpn_enabled())
2854 /* Parse attributes and extract fields of interest. Do basic
2855 * validation of the fields.
2857 memset(tb
, 0, sizeof tb
);
2858 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
2861 if (!tb
[NDA_LLADDR
]) {
2862 if (IS_ZEBRA_DEBUG_KERNEL
)
2863 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
2864 nl_msg_type_to_str(h
->nlmsg_type
),
2869 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2870 if (IS_ZEBRA_DEBUG_KERNEL
)
2872 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
2873 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
2874 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2878 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2880 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2882 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2883 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2887 /* TODO: Only IPv4 supported now. */
2889 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
2891 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
2896 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
2898 if (ndm
->ndm_state
& NUD_STALE
)
2899 local_inactive
= true;
2901 if (tb
[NDA_FDB_EXT_ATTRS
]) {
2902 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
2903 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
2905 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
2906 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
2909 nfy_flags
= *(uint8_t *)RTA_DATA(
2910 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
2911 if (nfy_flags
& FDB_NOTIFY_BIT
)
2913 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
2914 local_inactive
= true;
2918 if (IS_ZEBRA_DEBUG_KERNEL
)
2919 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
2920 nl_msg_type_to_str(h
->nlmsg_type
),
2921 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2922 ndm
->ndm_state
, ndm
->ndm_flags
, &mac
,
2923 dst_present
? dst_buf
: "", nhg_id
);
2925 /* The interface should exist. */
2926 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2928 if (!ifp
|| !ifp
->info
)
2931 /* The interface should be something we're interested in. */
2932 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2935 zif
= (struct zebra_if
*)ifp
->info
;
2936 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2937 if (IS_ZEBRA_DEBUG_KERNEL
)
2939 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
2940 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
2942 zif
->brslave_info
.bridge_ifindex
);
2946 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
2948 if (filter_vlan
&& vid
!= filter_vlan
) {
2949 if (IS_ZEBRA_DEBUG_KERNEL
)
2950 zlog_debug(" Filtered due to filter vlan: %d",
2955 /* If add or update, do accordingly if learnt on a "local" interface; if
2956 * the notification is over VxLAN, this has to be related to
2958 * so perform an implicit delete of any local entry (if it exists).
2960 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2961 /* Drop "permanent" entries. */
2962 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2963 if (IS_ZEBRA_DEBUG_KERNEL
)
2965 " Dropping entry because of NUD_PERMANENT");
2969 if (IS_ZEBRA_IF_VXLAN(ifp
))
2970 return zebra_vxlan_dp_network_mac_add(
2971 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
2972 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
2974 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2975 sticky
, local_inactive
, dp_static
);
2978 /* This is a delete notification.
2979 * Ignore the notification with IP dest as it may just signify that the
2980 * MAC has moved from remote to local. The exception is the special
2981 * all-zeros MAC that represents the BUM flooding entry; we may have
2982 * to readd it. Otherwise,
2983 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2984 * 2. For a MAC over "local" interface, delete the mac
2985 * Note: We will get notifications from both bridge driver and VxLAN
2992 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2994 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
2995 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
2999 if (IS_ZEBRA_IF_VXLAN(ifp
))
3000 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3002 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3005 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3010 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3013 /* Length validity. */
3014 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3018 /* We are interested only in AF_BRIDGE notifications. */
3019 ndm
= NLMSG_DATA(h
);
3020 if (ndm
->ndm_family
!= AF_BRIDGE
)
3023 return netlink_macfdb_change(h
, len
, ns_id
);
3026 /* Request for MAC FDB information from the kernel */
3027 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3028 int type
, ifindex_t master_ifindex
)
3032 struct ifinfomsg ifm
;
3036 /* Form the request, specifying filter (rtattr) if needed. */
3037 memset(&req
, 0, sizeof(req
));
3038 req
.n
.nlmsg_type
= type
;
3039 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3040 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3041 req
.ifm
.ifi_family
= family
;
3043 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3045 return netlink_request(netlink_cmd
, &req
);
3049 * MAC forwarding database read using netlink interface. This is invoked
3052 int netlink_macfdb_read(struct zebra_ns
*zns
)
3055 struct zebra_dplane_info dp_info
;
3057 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3059 /* Get bridge FDB table. */
3060 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3064 /* We are reading entire table. */
3066 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3073 * MAC forwarding database read using netlink interface. This is for a
3074 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3076 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3077 struct interface
*br_if
)
3079 struct zebra_if
*br_zif
;
3080 struct zebra_if
*zif
;
3081 struct zebra_l2info_vxlan
*vxl
;
3082 struct zebra_dplane_info dp_info
;
3085 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3087 /* Save VLAN we're filtering on, if needed. */
3088 br_zif
= (struct zebra_if
*)br_if
->info
;
3089 zif
= (struct zebra_if
*)ifp
->info
;
3090 vxl
= &zif
->l2info
.vxl
;
3091 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3092 filter_vlan
= vxl
->access_vlan
;
3094 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3096 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3100 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3103 /* Reset VLAN filter. */
3109 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3110 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3113 struct interface
*br_if
,
3114 struct ethaddr
*mac
,
3122 struct zebra_if
*br_zif
;
3124 memset(&req
, 0, sizeof(req
));
3125 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3126 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3127 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3128 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3129 /* req.ndm.ndm_state = NUD_REACHABLE; */
3131 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3133 br_zif
= (struct zebra_if
*)br_if
->info
;
3134 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3135 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3137 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3139 if (IS_ZEBRA_DEBUG_KERNEL
)
3141 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
3142 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3143 br_if
->name
, br_if
->ifindex
,
3144 vrf_id_to_name(br_if
->vrf_id
), br_if
->vrf_id
, mac
, vid
);
3146 return netlink_request(&zns
->netlink_cmd
, &req
);
3149 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3150 struct interface
*br_if
,
3151 struct ethaddr
*mac
, vlanid_t vid
)
3154 struct zebra_dplane_info dp_info
;
3156 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3158 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3160 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3166 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3173 * Netlink-specific handler for MAC updates using dataplane context object.
3175 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3178 struct ipaddr vtep_ip
;
3185 uint32_t update_flags
;
3187 uint8_t nfy_flags
= 0;
3189 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3190 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3193 state
= NUD_REACHABLE
;
3195 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3196 if (update_flags
& DPLANE_MAC_REMOTE
) {
3198 if (dplane_ctx_mac_is_sticky(ctx
)) {
3199 /* NUD_NOARP prevents the entry from expiring */
3201 /* sticky the entry from moving */
3202 flags
|= NTF_STICKY
;
3204 flags
|= NTF_EXT_LEARNED
;
3206 /* if it was static-local previously we need to clear the
3207 * notify flags on replace with remote
3209 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3213 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3214 nfy_flags
|= FDB_NOTIFY_BIT
;
3218 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3219 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3224 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3225 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3226 SET_IPADDR_V4(&vtep_ip
);
3228 if (IS_ZEBRA_DEBUG_KERNEL
) {
3230 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3232 vid
= dplane_ctx_mac_get_vlan(ctx
);
3234 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3239 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
3240 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3241 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3242 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3243 mac
, &vtep_ip
, nhg_id
,
3244 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3245 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3247 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3248 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3253 total
= netlink_neigh_update_msg_encode(
3254 ctx
, cmd
, dplane_ctx_mac_get_addr(ctx
), &vtep_ip
, true,
3255 AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
, nfy_flags
,
3256 false /*ext*/, 0 /*ext_flags*/, data
, datalen
);
3262 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3263 * 5549 support, re-install them.
3265 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3266 struct interface
*ifp
, struct ipaddr
*ip
,
3269 if (ndm
->ndm_family
!= AF_INET
)
3272 if (!zif
->v6_2_v4_ll_neigh_entry
)
3275 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3278 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3279 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3284 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3288 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3290 #define NUD_LOCAL_ACTIVE \
3291 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3293 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3296 struct interface
*ifp
;
3297 struct zebra_if
*zif
;
3298 struct rtattr
*tb
[NDA_MAX
+ 1];
3299 struct interface
*link_if
;
3303 char buf
[ETHER_ADDR_STRLEN
];
3304 int mac_present
= 0;
3307 bool local_inactive
;
3308 uint32_t ext_flags
= 0;
3309 bool dp_static
= false;
3311 ndm
= NLMSG_DATA(h
);
3313 /* The interface should exist. */
3314 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3316 if (!ifp
|| !ifp
->info
)
3319 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
3320 zif
= (struct zebra_if
*)ifp
->info
;
3322 /* Parse attributes and extract fields of interest. */
3323 memset(tb
, 0, sizeof(tb
));
3324 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3327 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3328 nl_msg_type_to_str(h
->nlmsg_type
),
3329 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3330 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
);
3334 memset(&ip
, 0, sizeof(struct ipaddr
));
3335 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3336 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3338 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3339 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3340 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3341 if (IS_ZEBRA_DEBUG_KERNEL
)
3343 " Neighbor Entry Received is a 5549 entry, finished");
3347 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3348 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3349 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3351 /* The neighbor is present on an SVI. From this, we locate the
3353 * bridge because we're only interested in neighbors on a VxLAN bridge.
3354 * The bridge is located based on the nature of the SVI:
3355 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3357 * and is linked to the bridge
3358 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3362 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3363 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3367 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3370 if (IS_ZEBRA_DEBUG_KERNEL
)
3372 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3376 memset(&mac
, 0, sizeof(struct ethaddr
));
3377 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3378 if (tb
[NDA_LLADDR
]) {
3379 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3380 if (IS_ZEBRA_DEBUG_KERNEL
)
3382 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3387 ifp
->name
, ndm
->ndm_ifindex
,
3388 VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3389 (unsigned long)RTA_PAYLOAD(
3395 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3398 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3399 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3401 if (tb
[NDA_EXT_FLAGS
]) {
3402 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3403 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3407 if (IS_ZEBRA_DEBUG_KERNEL
)
3409 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3410 nl_msg_type_to_str(h
->nlmsg_type
),
3411 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3412 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3415 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3417 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3419 /* If the neighbor state is valid for use, process as an add or
3421 * else process as a delete. Note that the delete handling may
3423 * in re-adding the neighbor if it is a valid "remote" neighbor.
3425 if (ndm
->ndm_state
& NUD_VALID
) {
3426 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3428 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3430 /* If EVPN-MH is not enabled we treat STALE
3431 * neighbors as locally-active and advertise
3434 local_inactive
= false;
3436 return zebra_vxlan_handle_kernel_neigh_update(
3437 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3438 is_router
, local_inactive
, dp_static
);
3441 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3444 if (IS_ZEBRA_DEBUG_KERNEL
)
3445 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
3446 nl_msg_type_to_str(h
->nlmsg_type
),
3447 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3448 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3451 /* Process the delete - it may result in re-adding the neighbor if it is
3452 * a valid "remote" neighbor.
3454 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3457 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3462 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3465 /* Length validity. */
3466 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3470 /* We are interested only in AF_INET or AF_INET6 notifications. */
3471 ndm
= NLMSG_DATA(h
);
3472 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3475 return netlink_neigh_change(h
, len
);
3478 /* Request for IP neighbor information from the kernel */
3479 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3480 int type
, ifindex_t ifindex
)
3488 /* Form the request, specifying filter (rtattr) if needed. */
3489 memset(&req
, 0, sizeof(req
));
3490 req
.n
.nlmsg_type
= type
;
3491 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3492 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3493 req
.ndm
.ndm_family
= family
;
3495 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3497 return netlink_request(netlink_cmd
, &req
);
3501 * IP Neighbor table read using netlink interface. This is invoked
3504 int netlink_neigh_read(struct zebra_ns
*zns
)
3507 struct zebra_dplane_info dp_info
;
3509 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3511 /* Get IP neighbor table. */
3512 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3516 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3523 * IP Neighbor table read using netlink interface. This is for a specific
3526 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3529 struct zebra_dplane_info dp_info
;
3531 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3533 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3537 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3544 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3545 * read using netlink interface.
3547 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3548 int type
, struct ipaddr
*ip
,
3558 /* Form the request, specifying filter (rtattr) if needed. */
3559 memset(&req
, 0, sizeof(req
));
3560 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3561 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3562 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3563 req
.ndm
.ndm_ifindex
= ifindex
;
3565 if (IS_IPADDR_V4(ip
)) {
3566 ipa_len
= IPV4_MAX_BYTELEN
;
3567 req
.ndm
.ndm_family
= AF_INET
;
3570 ipa_len
= IPV6_MAX_BYTELEN
;
3571 req
.ndm
.ndm_family
= AF_INET6
;
3574 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3576 if (IS_ZEBRA_DEBUG_KERNEL
)
3577 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
3578 __func__
, nl_msg_type_to_str(type
),
3579 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
3582 return netlink_request(&zns
->netlink_cmd
, &req
);
3585 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3586 struct interface
*vlan_if
)
3589 struct zebra_ns
*zns
;
3590 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3591 struct zebra_dplane_info dp_info
;
3595 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3597 if (IS_ZEBRA_DEBUG_KERNEL
)
3598 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
3599 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
3600 vrf_id_to_name(vlan_if
->vrf_id
), vlan_if
->vrf_id
);
3602 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3607 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3613 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3618 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3621 /* Length validity. */
3622 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3625 "%s: Message received from netlink is of a broken size %d %zu",
3626 __func__
, h
->nlmsg_len
,
3627 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3631 /* Is this a notification for the MAC FDB or IP neighbor table? */
3632 ndm
= NLMSG_DATA(h
);
3633 if (ndm
->ndm_family
== AF_BRIDGE
)
3634 return netlink_macfdb_change(h
, len
, ns_id
);
3636 if (ndm
->ndm_type
!= RTN_UNICAST
)
3639 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3640 return netlink_ipneigh_change(h
, len
, ns_id
);
3643 EC_ZEBRA_UNKNOWN_FAMILY
,
3644 "Invalid address family: %u received from kernel neighbor change: %s",
3645 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3653 * Utility neighbor-update function, using info from dplane context.
3655 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3656 int cmd
, void *buf
, size_t buflen
)
3658 const struct ipaddr
*ip
;
3659 const struct ethaddr
*mac
;
3663 uint32_t update_flags
;
3664 uint32_t ext_flags
= 0;
3667 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3668 mac
= dplane_ctx_neigh_get_mac(ctx
);
3669 if (is_zero_mac(mac
))
3672 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
3673 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3674 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3676 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3678 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
3679 flags
|= NTF_EXT_LEARNED
;
3680 /* if it was static-local previously we need to clear the
3681 * ext flags on replace with remote
3683 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
3688 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
3689 ext_flags
|= NTF_E_MH_PEER_SYNC
;
3691 if (IS_ZEBRA_DEBUG_KERNEL
)
3693 "Tx %s family %s IF %s(%u) Neigh %pIA MAC %pEA flags 0x%x state 0x%x %sext_flags 0x%x",
3694 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
3695 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3696 ip
, mac
, flags
, state
, ext
? "ext " : "", ext_flags
);
3698 return netlink_neigh_update_msg_encode(
3699 ctx
, cmd
, mac
, ip
, true, family
, RTN_UNICAST
, flags
, state
,
3700 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
, ext_flags
, buf
,
3704 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
3705 void *buf
, size_t buflen
)
3709 switch (dplane_ctx_get_op(ctx
)) {
3710 case DPLANE_OP_NEIGH_INSTALL
:
3711 case DPLANE_OP_NEIGH_UPDATE
:
3712 case DPLANE_OP_NEIGH_DISCOVER
:
3713 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
3715 case DPLANE_OP_NEIGH_DELETE
:
3716 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
3718 case DPLANE_OP_VTEP_ADD
:
3719 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
3722 case DPLANE_OP_VTEP_DELETE
:
3723 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
3734 * Update MAC, using dataplane context object.
3737 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
3738 struct zebra_dplane_ctx
*ctx
)
3740 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
3744 enum netlink_msg_status
3745 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
3747 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
3752 * MPLS label forwarding table change via netlink interface, using dataplane
3753 * context information.
3755 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
3756 void *buf
, size_t buflen
)
3759 const struct nhlfe_list_head
*head
;
3760 const zebra_nhlfe_t
*nhlfe
;
3761 struct nexthop
*nexthop
= NULL
;
3762 unsigned int nexthop_num
;
3763 const char *routedesc
;
3765 struct prefix p
= {0};
3773 if (buflen
< sizeof(*req
))
3776 memset(req
, 0, sizeof(*req
));
3779 * Count # nexthops so we can decide whether to use singlepath
3780 * or multipath case.
3783 head
= dplane_ctx_get_nhlfe_list(ctx
);
3784 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3785 nexthop
= nhlfe
->nexthop
;
3788 if (cmd
== RTM_NEWROUTE
) {
3789 /* Count all selected NHLFEs */
3790 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3791 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3794 /* Count all installed NHLFEs */
3795 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3796 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3801 if ((nexthop_num
== 0) ||
3802 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3805 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3806 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3807 req
->n
.nlmsg_type
= cmd
;
3808 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3810 req
->r
.rtm_family
= AF_MPLS
;
3811 req
->r
.rtm_table
= RT_TABLE_MAIN
;
3812 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3813 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3814 req
->r
.rtm_type
= RTN_UNICAST
;
3816 if (cmd
== RTM_NEWROUTE
) {
3817 /* We do a replace to handle update. */
3818 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
3820 /* set the protocol value if installing */
3821 route_type
= re_type_from_lsp_type(
3822 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3823 req
->r
.rtm_protocol
= zebra2proto(route_type
);
3826 /* Fill destination */
3827 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3828 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
3831 /* Fill nexthops (paths) based on single-path or multipath. The paths
3832 * chosen depend on the operation.
3834 if (nexthop_num
== 1) {
3835 routedesc
= "single-path";
3836 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3840 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3841 nexthop
= nhlfe
->nexthop
;
3845 if ((cmd
== RTM_NEWROUTE
3846 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3847 && CHECK_FLAG(nexthop
->flags
,
3848 NEXTHOP_FLAG_ACTIVE
)))
3849 || (cmd
== RTM_DELROUTE
3850 && (CHECK_FLAG(nhlfe
->flags
,
3851 NHLFE_FLAG_INSTALLED
)
3852 && CHECK_FLAG(nexthop
->flags
,
3853 NEXTHOP_FLAG_FIB
)))) {
3854 /* Add the gateway */
3855 if (!_netlink_mpls_build_singlepath(
3856 &p
, routedesc
, nhlfe
, &req
->n
,
3857 &req
->r
, buflen
, cmd
))
3864 } else { /* Multipath case */
3865 struct rtattr
*nest
;
3866 const union g_addr
*src1
= NULL
;
3868 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
3872 routedesc
= "multipath";
3873 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3877 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3878 nexthop
= nhlfe
->nexthop
;
3882 if ((cmd
== RTM_NEWROUTE
3883 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3884 && CHECK_FLAG(nexthop
->flags
,
3885 NEXTHOP_FLAG_ACTIVE
)))
3886 || (cmd
== RTM_DELROUTE
3887 && (CHECK_FLAG(nhlfe
->flags
,
3888 NHLFE_FLAG_INSTALLED
)
3889 && CHECK_FLAG(nexthop
->flags
,
3890 NEXTHOP_FLAG_FIB
)))) {
3893 /* Build the multipath */
3894 if (!_netlink_mpls_build_multipath(
3895 &p
, routedesc
, nhlfe
, &req
->n
,
3896 buflen
, &req
->r
, &src1
))
3901 /* Add the multipath */
3902 nl_attr_nest_end(&req
->n
, nest
);
3905 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3908 /****************************************************************************
3909 * This code was developed in a branch that didn't have dplane APIs for
3910 * MAC updates. Hence the use of the legacy style. It will be moved to
3911 * the new dplane style pre-merge to master. XXX
3913 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
3920 int cmd
= RTM_NEWNEXTHOP
;
3921 struct zebra_vrf
*zvrf
;
3922 struct zebra_ns
*zns
;
3924 zvrf
= zebra_vrf_get_evpn();
3929 memset(&req
, 0, sizeof(req
));
3931 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3932 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3933 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3934 req
.n
.nlmsg_type
= cmd
;
3935 req
.nhm
.nh_family
= AF_INET
;
3937 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
3939 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
3941 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
3942 &vtep_ip
, IPV4_MAX_BYTELEN
))
3945 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3946 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
3947 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
3950 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3954 static int netlink_fdb_nh_del(uint32_t nh_id
)
3961 int cmd
= RTM_DELNEXTHOP
;
3962 struct zebra_vrf
*zvrf
;
3963 struct zebra_ns
*zns
;
3965 zvrf
= zebra_vrf_get_evpn();
3970 memset(&req
, 0, sizeof(req
));
3972 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3973 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3974 req
.n
.nlmsg_type
= cmd
;
3975 req
.nhm
.nh_family
= AF_UNSPEC
;
3977 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
3980 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3981 zlog_debug("Tx %s fdb-nh 0x%x",
3982 nl_msg_type_to_str(cmd
), nh_id
);
3985 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3989 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
3990 struct nh_grp
*nh_ids
)
3997 int cmd
= RTM_NEWNEXTHOP
;
3998 struct zebra_vrf
*zvrf
;
3999 struct zebra_ns
*zns
;
4000 struct nexthop_grp grp
[nh_cnt
];
4003 zvrf
= zebra_vrf_get_evpn();
4008 memset(&req
, 0, sizeof(req
));
4010 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4011 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4012 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4013 req
.n
.nlmsg_type
= cmd
;
4014 req
.nhm
.nh_family
= AF_UNSPEC
;
4016 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4018 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4020 memset(&grp
, 0, sizeof(grp
));
4021 for (i
= 0; i
< nh_cnt
; ++i
) {
4022 grp
[i
].id
= nh_ids
[i
].id
;
4023 grp
[i
].weight
= nh_ids
[i
].weight
;
4025 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4026 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4030 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4031 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4035 for (i
= 0; i
< nh_cnt
; ++i
) {
4036 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4038 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4041 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4042 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4045 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4049 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4051 return netlink_fdb_nh_del(nhg_id
);
4054 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4056 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4059 int kernel_del_mac_nh(uint32_t nh_id
)
4061 return netlink_fdb_nh_del(nh_id
);
4064 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4065 struct nh_grp
*nh_ids
)
4067 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4070 int kernel_del_mac_nhg(uint32_t nhg_id
)
4072 return netlink_fdb_nhg_del(nhg_id
);
4075 #endif /* HAVE_NETLINK */