1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <net/if_arp.h>
26 #include <linux/lwtunnel.h>
27 #include <linux/mpls_iptunnel.h>
28 #include <linux/neighbour.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/nexthop.h>
32 /* Hack for GNU libc version 2. */
34 #define MSG_TRUNC 0x20
35 #endif /* MSG_TRUNC */
41 #include "connected.h"
44 #include "zebra_memory.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_ns.h"
57 #include "zebra/zebra_vrf.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/interface.h"
61 #include "zebra/debug.h"
62 #include "zebra/rtadv.h"
63 #include "zebra/zebra_ptm.h"
64 #include "zebra/zebra_mpls.h"
65 #include "zebra/kernel_netlink.h"
66 #include "zebra/rt_netlink.h"
67 #include "zebra/zebra_nhg.h"
68 #include "zebra/zebra_mroute.h"
69 #include "zebra/zebra_vxlan.h"
70 #include "zebra/zebra_errors.h"
71 #include "zebra/zebra_evpn_mh.h"
77 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
78 * UAPI for MAC sync. */
79 #ifndef _UAPI_LINUX_IF_BRIDGE_H
80 /* FDB notification bits for NDA_NOTIFY:
81 * - BR_FDB_NFY_STATIC - notify on activity/expire even for a static entry
82 * - BR_FDB_NFY_INACTIVE - mark as inactive to avoid double notification,
83 * used with BR_FDB_NFY_STATIC (kernel controlled)
92 static vlanid_t filter_vlan
= 0;
94 /* We capture whether the current kernel supports nexthop ids; by
95 * default, we'll use them if possible. There's also a configuration
96 * available to _disable_ use of kernel nexthops.
98 static bool supports_nh
;
106 static const char ipv4_ll_buf
[16] = "169.254.0.1";
107 static struct in_addr ipv4_ll
;
109 /* Is this a ipv4 over ipv6 route? */
110 static bool is_route_v4_over_v6(unsigned char rtm_family
,
111 enum nexthop_types_t nexthop_type
)
113 if (rtm_family
== AF_INET
114 && (nexthop_type
== NEXTHOP_TYPE_IPV6
115 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
121 /* Helper to control use of kernel-level nexthop ids */
122 static bool kernel_nexthops_supported(void)
124 return (supports_nh
&& !vrf_is_backend_netns()
125 && zebra_nhg_kernel_nexthops_enabled());
129 * Some people may only want to use NHGs created by protos and not
130 * implicitly created by Zebra. This check accounts for that.
132 static bool proto_nexthops_only(void)
134 return zebra_nhg_proto_nexthops_only();
137 /* Is this a proto created NHG? */
138 static bool is_proto_nhg(uint32_t id
, int type
)
140 /* If type is available, use it as the source of truth */
142 if (type
!= ZEBRA_ROUTE_NHG
)
147 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
154 * The ipv4_ll data structure is used for all 5549
155 * additions to the kernel. Let's figure out the
156 * correct value one time instead for every
157 * install/remove of a 5549 type route
159 void rt_netlink_init(void)
161 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
165 * Mapping from dataplane neighbor flags to netlink flags
167 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
171 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
172 flags
|= NTF_EXT_LEARNED
;
173 if (dplane_flags
& DPLANE_NTF_ROUTER
)
175 if (dplane_flags
& DPLANE_NTF_USE
)
182 * Mapping from dataplane neighbor state to netlink state
184 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
188 if (dplane_state
& DPLANE_NUD_REACHABLE
)
189 state
|= NUD_REACHABLE
;
190 if (dplane_state
& DPLANE_NUD_STALE
)
192 if (dplane_state
& DPLANE_NUD_NOARP
)
194 if (dplane_state
& DPLANE_NUD_PROBE
)
196 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
197 state
|= NUD_INCOMPLETE
;
203 static inline bool is_selfroute(int proto
)
205 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
206 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
207 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
208 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
209 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
210 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
211 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
212 || (proto
== RTPROT_SRTE
)) {
219 static inline int zebra2proto(int proto
)
222 case ZEBRA_ROUTE_BABEL
:
223 proto
= RTPROT_BABEL
;
225 case ZEBRA_ROUTE_BGP
:
228 case ZEBRA_ROUTE_OSPF
:
229 case ZEBRA_ROUTE_OSPF6
:
232 case ZEBRA_ROUTE_STATIC
:
233 proto
= RTPROT_ZSTATIC
;
235 case ZEBRA_ROUTE_ISIS
:
238 case ZEBRA_ROUTE_RIP
:
241 case ZEBRA_ROUTE_RIPNG
:
242 proto
= RTPROT_RIPNG
;
244 case ZEBRA_ROUTE_NHRP
:
247 case ZEBRA_ROUTE_EIGRP
:
248 proto
= RTPROT_EIGRP
;
250 case ZEBRA_ROUTE_LDP
:
253 case ZEBRA_ROUTE_SHARP
:
254 proto
= RTPROT_SHARP
;
256 case ZEBRA_ROUTE_PBR
:
259 case ZEBRA_ROUTE_OPENFABRIC
:
260 proto
= RTPROT_OPENFABRIC
;
262 case ZEBRA_ROUTE_SRTE
:
265 case ZEBRA_ROUTE_TABLE
:
266 case ZEBRA_ROUTE_NHG
:
267 proto
= RTPROT_ZEBRA
;
271 * When a user adds a new protocol this will show up
272 * to let them know to do something about it. This
273 * is intentionally a warn because we should see
274 * this as part of development of a new protocol
277 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
279 proto
= RTPROT_ZEBRA
;
286 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
290 proto
= ZEBRA_ROUTE_BABEL
;
293 proto
= ZEBRA_ROUTE_BGP
;
296 proto
= (family
== AFI_IP
) ? ZEBRA_ROUTE_OSPF
300 proto
= ZEBRA_ROUTE_ISIS
;
303 proto
= ZEBRA_ROUTE_RIP
;
306 proto
= ZEBRA_ROUTE_RIPNG
;
309 proto
= ZEBRA_ROUTE_NHRP
;
312 proto
= ZEBRA_ROUTE_EIGRP
;
315 proto
= ZEBRA_ROUTE_LDP
;
319 proto
= ZEBRA_ROUTE_STATIC
;
322 proto
= ZEBRA_ROUTE_SHARP
;
325 proto
= ZEBRA_ROUTE_PBR
;
327 case RTPROT_OPENFABRIC
:
328 proto
= ZEBRA_ROUTE_OPENFABRIC
;
331 proto
= ZEBRA_ROUTE_SRTE
;
335 proto
= ZEBRA_ROUTE_NHG
;
338 /* Intentional fall thru */
341 * When a user adds a new protocol this will show up
342 * to let them know to do something about it. This
343 * is intentionally a warn because we should see
344 * this as part of development of a new protocol
347 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
349 proto
= ZEBRA_ROUTE_KERNEL
;
356 Pending: create an efficient table_id (in a tree/hash) based lookup)
358 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
361 struct zebra_vrf
*zvrf
;
363 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
367 /* case vrf with netns : match the netnsid */
368 if (vrf_is_backend_netns()) {
369 if (ns_id
== zvrf_id(zvrf
))
370 return zvrf_id(zvrf
);
372 /* VRF is VRF_BACKEND_VRF_LITE */
373 if (zvrf
->table_id
!= table_id
)
375 return zvrf_id(zvrf
);
383 * @parse_encap_mpls() - Parses encapsulated mpls attributes
384 * @tb: Pointer to rtattr to look for nested items in.
385 * @labels: Pointer to store labels in.
387 * Return: Number of mpls labels found.
389 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
391 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
392 mpls_lse_t
*lses
= NULL
;
397 mpls_label_t label
= 0;
399 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
400 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
401 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
402 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
403 labels
[num_labels
++] = label
;
409 static struct nexthop
410 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
411 enum blackhole_type bh_type
, int index
, void *prefsrc
,
412 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
414 struct interface
*ifp
= NULL
;
415 struct nexthop nh
= {0};
416 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
419 vrf_id_t nh_vrf_id
= vrf_id
;
420 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
422 if (bh_type
== BLACKHOLE_UNSPEC
) {
424 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
425 else if (index
&& gate
)
426 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
427 : NEXTHOP_TYPE_IPV6_IFINDEX
;
428 else if (!index
&& gate
)
429 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
432 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
433 nh
.bh_type
= bh_type
;
436 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
437 nh
.bh_type
= bh_type
;
441 memcpy(&nh
.src
, prefsrc
, sz
);
443 memcpy(&nh
.gate
, gate
, sz
);
446 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
448 nh_vrf_id
= ifp
->vrf_id
;
450 nh
.vrf_id
= nh_vrf_id
;
452 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
453 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
454 == LWTUNNEL_ENCAP_MPLS
) {
455 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
458 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
459 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
462 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
467 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
468 struct nexthop_group
*ng
,
470 struct rtnexthop
*rtnh
,
472 void *prefsrc
, vrf_id_t vrf_id
)
475 struct interface
*ifp
= NULL
;
478 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
480 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
482 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
483 vrf_id_t nh_vrf_id
= vrf_id
;
486 struct nexthop
*nh
= NULL
;
488 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
491 index
= rtnh
->rtnh_ifindex
;
494 * Yes we are looking this up
495 * for every nexthop and just
496 * using the last one looked
499 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
502 nh_vrf_id
= ifp
->vrf_id
;
505 EC_ZEBRA_UNKNOWN_INTERFACE
,
506 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
508 nh_vrf_id
= VRF_DEFAULT
;
513 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
514 memset(rtnh_tb
, 0, sizeof(rtnh_tb
));
516 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
517 rtnh
->rtnh_len
- sizeof(*rtnh
));
518 if (rtnh_tb
[RTA_GATEWAY
])
519 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
520 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
521 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
522 == LWTUNNEL_ENCAP_MPLS
) {
523 num_labels
= parse_encap_mpls(
524 rtnh_tb
[RTA_ENCAP
], labels
);
528 if (gate
&& rtm
->rtm_family
== AF_INET
) {
530 nh
= nexthop_from_ipv4_ifindex(
531 gate
, prefsrc
, index
, nh_vrf_id
);
533 nh
= nexthop_from_ipv4(gate
, prefsrc
,
535 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
537 nh
= nexthop_from_ipv6_ifindex(
538 gate
, index
, nh_vrf_id
);
540 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
542 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
545 nh
->weight
= rtnh
->rtnh_hops
+ 1;
548 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
551 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
552 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
554 /* Add to temporary list */
555 nexthop_group_add_sorted(ng
, nh
);
558 if (rtnh
->rtnh_len
== 0)
561 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
562 rtnh
= RTNH_NEXT(rtnh
);
565 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
570 /* Looking up routing table by netlink interface. */
571 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
576 struct rtattr
*tb
[RTA_MAX
+ 1];
579 struct prefix_ipv6 src_p
= {};
583 char anyaddr
[16] = {0};
585 int proto
= ZEBRA_ROUTE_KERNEL
;
590 uint8_t distance
= 0;
596 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
597 void *src
= NULL
; /* IPv6 srcdest source prefix */
598 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
602 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
604 switch (rtm
->rtm_type
) {
608 bh_type
= BLACKHOLE_NULL
;
610 case RTN_UNREACHABLE
:
611 bh_type
= BLACKHOLE_REJECT
;
614 bh_type
= BLACKHOLE_ADMINPROHIB
;
617 if (IS_ZEBRA_DEBUG_KERNEL
)
618 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
619 nl_rttype_to_str(rtm
->rtm_type
),
624 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
627 "%s: Message received from netlink is of a broken size %d %zu",
628 __func__
, h
->nlmsg_len
,
629 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
633 memset(tb
, 0, sizeof(tb
));
634 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
636 if (rtm
->rtm_flags
& RTM_F_CLONED
)
638 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
640 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
643 selfroute
= is_selfroute(rtm
->rtm_protocol
);
645 if (!startup
&& selfroute
&& h
->nlmsg_type
== RTM_NEWROUTE
) {
646 if (IS_ZEBRA_DEBUG_KERNEL
)
647 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
652 /* We don't care about change notifications for the MPLS table. */
653 /* TODO: Revisit this. */
654 if (rtm
->rtm_family
== AF_MPLS
)
657 /* Table corresponding to route. */
659 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
661 table
= rtm
->rtm_table
;
664 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
665 if (vrf_id
== VRF_DEFAULT
) {
666 if (!is_zebra_valid_kernel_table(table
)
667 && !is_zebra_main_routing_table(table
))
671 if (rtm
->rtm_flags
& RTM_F_TRAP
)
672 flags
|= ZEBRA_FLAG_TRAPPED
;
673 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
674 flags
|= ZEBRA_FLAG_OFFLOADED
;
676 /* Route which inserted by Zebra. */
678 flags
|= ZEBRA_FLAG_SELFROUTE
;
679 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
682 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
685 dest
= RTA_DATA(tb
[RTA_DST
]);
690 src
= RTA_DATA(tb
[RTA_SRC
]);
695 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
698 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
701 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
703 if (tb
[RTA_PRIORITY
])
704 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
706 #if defined(SUPPORT_REALMS)
708 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
711 if (tb
[RTA_METRICS
]) {
712 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
714 memset(mxrta
, 0, sizeof(mxrta
));
715 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
716 RTA_PAYLOAD(tb
[RTA_METRICS
]));
719 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
722 if (rtm
->rtm_family
== AF_INET
) {
724 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
726 "Invalid destination prefix length: %u received from kernel route change",
730 memcpy(&p
.u
.prefix4
, dest
, 4);
731 p
.prefixlen
= rtm
->rtm_dst_len
;
733 if (rtm
->rtm_src_len
!= 0) {
734 char buf
[PREFIX_STRLEN
];
736 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
737 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
738 prefix2str(&p
, buf
, sizeof(buf
)), vrf_id
);
742 /* Force debug below to not display anything for source */
744 } else if (rtm
->rtm_family
== AF_INET6
) {
746 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
748 "Invalid destination prefix length: %u received from kernel route change",
752 memcpy(&p
.u
.prefix6
, dest
, 16);
753 p
.prefixlen
= rtm
->rtm_dst_len
;
755 src_p
.family
= AF_INET6
;
756 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
758 "Invalid source prefix length: %u received from kernel route change",
762 memcpy(&src_p
.prefix
, src
, 16);
763 src_p
.prefixlen
= rtm
->rtm_src_len
;
767 * For ZEBRA_ROUTE_KERNEL types:
769 * The metric/priority of the route received from the kernel
770 * is a 32 bit number. We are going to interpret the high
771 * order byte as the Admin Distance and the low order 3 bytes
774 * This will allow us to do two things:
775 * 1) Allow the creation of kernel routes that can be
776 * overridden by zebra.
777 * 2) Allow the old behavior for 'most' kernel route types
778 * if a user enters 'ip route ...' v4 routes get a metric
779 * of 0 and v6 routes get a metric of 1024. Both of these
780 * values will end up with a admin distance of 0, which
781 * will cause them to win for the purposes of zebra.
783 if (proto
== ZEBRA_ROUTE_KERNEL
) {
784 distance
= (metric
>> 24) & 0xFF;
785 metric
= (metric
& 0x00FFFFFF);
788 if (IS_ZEBRA_DEBUG_KERNEL
) {
789 char buf
[PREFIX_STRLEN
];
790 char buf2
[PREFIX_STRLEN
];
792 "%s %s%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
793 nl_msg_type_to_str(h
->nlmsg_type
),
794 prefix2str(&p
, buf
, sizeof(buf
)),
795 src_p
.prefixlen
? " from " : "",
796 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
798 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
803 if (rtm
->rtm_family
== AF_INET6
)
806 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
808 if (!tb
[RTA_MULTIPATH
]) {
809 struct nexthop nh
= {0};
812 nh
= parse_nexthop_unicast(
813 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
816 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
817 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
820 /* This is a multipath route */
821 struct route_entry
*re
;
822 struct nexthop_group
*ng
= NULL
;
823 struct rtnexthop
*rtnh
=
824 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
826 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
828 re
->distance
= distance
;
834 re
->uptime
= monotime(NULL
);
841 /* Use temporary list of nexthops; parse
842 * message payload's nexthops.
844 ng
= nexthop_group_new();
846 parse_multipath_nexthops_unicast(
847 ns_id
, ng
, rtm
, rtnh
, tb
,
850 zserv_nexthop_num_warn(
851 __func__
, (const struct prefix
*)&p
,
855 nexthop_group_delete(&ng
);
861 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
868 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
869 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
870 distance
, true, false);
872 if (!tb
[RTA_MULTIPATH
]) {
875 nh
= parse_nexthop_unicast(
876 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
878 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
879 flags
, &p
, &src_p
, &nh
, 0, table
,
880 metric
, distance
, true, false);
882 /* XXX: need to compare the entire list of
883 * nexthops here for NLM_F_APPEND stupidity */
884 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
885 flags
, &p
, &src_p
, NULL
, 0, table
,
886 metric
, distance
, true, false);
894 static struct mcast_route_data
*mroute
= NULL
;
896 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
897 ns_id_t ns_id
, int startup
)
901 struct rtattr
*tb
[RTA_MAX
+ 1];
902 struct mcast_route_data
*m
;
903 struct mcast_route_data mr
;
910 char oif_list
[256] = "\0";
917 memset(&mr
, 0, sizeof(mr
));
923 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
925 memset(tb
, 0, sizeof(tb
));
926 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
929 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
931 table
= rtm
->rtm_table
;
933 vrf
= vrf_lookup_by_table(table
, ns_id
);
936 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
939 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
942 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
945 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
947 if (tb
[RTA_MULTIPATH
]) {
948 struct rtnexthop
*rtnh
=
949 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
951 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
953 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
956 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
959 if (rtnh
->rtnh_len
== 0)
962 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
963 rtnh
= RTNH_NEXT(rtnh
);
967 if (IS_ZEBRA_DEBUG_KERNEL
) {
968 struct interface
*ifp
= NULL
;
969 struct zebra_vrf
*zvrf
= NULL
;
971 strlcpy(sbuf
, inet_ntoa(m
->sg
.src
), sizeof(sbuf
));
972 strlcpy(gbuf
, inet_ntoa(m
->sg
.grp
), sizeof(gbuf
));
973 for (count
= 0; count
< oif_count
; count
++) {
974 ifp
= if_lookup_by_index(oif
[count
], vrf
);
977 snprintf(temp
, sizeof(temp
), "%s(%d) ",
978 ifp
? ifp
->name
: "Unknown", oif
[count
]);
979 strlcat(oif_list
, temp
, sizeof(oif_list
));
981 zvrf
= zebra_vrf_lookup_by_id(vrf
);
982 ifp
= if_lookup_by_index(iif
, vrf
);
984 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
985 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
986 sbuf
, gbuf
, ifp
? ifp
->name
: "Unknown", iif
, oif_list
,
992 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
999 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1000 /* If this is not route add/delete message print warning. */
1001 zlog_debug("Kernel message: %s NS %u",
1002 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1006 if (!(rtm
->rtm_family
== AF_INET
||
1007 rtm
->rtm_family
== AF_INET6
||
1008 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1010 EC_ZEBRA_UNKNOWN_FAMILY
,
1011 "Invalid address family: %u received from kernel route change: %s",
1012 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1016 /* Connected route. */
1017 if (IS_ZEBRA_DEBUG_KERNEL
)
1018 zlog_debug("%s %s %s proto %s NS %u",
1019 nl_msg_type_to_str(h
->nlmsg_type
),
1020 nl_family_to_str(rtm
->rtm_family
),
1021 nl_rttype_to_str(rtm
->rtm_type
),
1022 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1025 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1028 "%s: Message received from netlink is of a broken size: %d %zu",
1029 __func__
, h
->nlmsg_len
,
1030 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1034 if (rtm
->rtm_type
== RTN_MULTICAST
)
1035 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1037 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1041 /* Request for specific route information from the kernel */
1042 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1049 /* Form the request, specifying filter (rtattr) if needed. */
1050 memset(&req
, 0, sizeof(req
));
1051 req
.n
.nlmsg_type
= type
;
1052 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1053 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1054 req
.rtm
.rtm_family
= family
;
1056 return netlink_request(&zns
->netlink_cmd
, &req
);
1059 /* Routing table read function using netlink interface. Only called
1061 int netlink_route_read(struct zebra_ns
*zns
)
1064 struct zebra_dplane_info dp_info
;
1066 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1068 /* Get IPv4 routing table. */
1069 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1072 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1073 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1077 /* Get IPv6 routing table. */
1078 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1081 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1082 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1090 * The function returns true if the gateway info could be added
1091 * to the message, otherwise false is returned.
1093 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1095 struct nlmsghdr
*nlmsg
,
1096 size_t req_size
, int bytelen
,
1097 const struct nexthop
*nexthop
)
1099 if (route_family
== AF_MPLS
) {
1100 struct gw_family_t gw_fam
;
1102 gw_fam
.family
= gw_family
;
1103 if (gw_family
== AF_INET
)
1104 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1106 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1107 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1111 if (!(nexthop
->rparent
1112 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1113 if (gw_family
== AF_INET
) {
1114 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1115 &nexthop
->gate
.ipv4
, bytelen
))
1118 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1119 &nexthop
->gate
.ipv6
, bytelen
))
1128 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1129 mpls_lse_t
*out_lse
, char *label_buf
,
1130 size_t label_buf_size
)
1132 char label_buf1
[20];
1135 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1136 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1139 if (IS_ZEBRA_DEBUG_KERNEL
) {
1141 sprintf(label_buf
, "label %u",
1142 nh_label
->label
[i
]);
1144 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1145 nh_label
->label
[i
]);
1146 strlcat(label_buf
, label_buf1
, label_buf_size
);
1150 out_lse
[num_labels
] =
1151 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1158 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1159 struct nlmsghdr
*nlmsg
,
1160 size_t buflen
, struct rtmsg
*rtmsg
,
1162 size_t label_buf_size
)
1164 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1168 * label_buf is *only* currently used within debugging.
1169 * As such when we assign it we are guarding it inside
1170 * a debug test. If you want to change this make sure
1171 * you fix this assumption
1173 label_buf
[0] = '\0';
1176 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1179 /* Set the BoS bit */
1180 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1182 if (rtmsg
->rtm_family
== AF_MPLS
) {
1183 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1184 num_labels
* sizeof(mpls_lse_t
)))
1187 struct rtattr
*nest
;
1189 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1190 LWTUNNEL_ENCAP_MPLS
))
1193 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1197 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1199 num_labels
* sizeof(mpls_lse_t
)))
1201 nl_attr_nest_end(nlmsg
, nest
);
1208 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1210 struct nlmsghdr
*nlmsg
,
1211 size_t buflen
, int bytelen
)
1213 if (family
== AF_INET
) {
1214 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1215 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1216 &nexthop
->rmap_src
.ipv4
, bytelen
))
1218 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1219 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1220 &nexthop
->src
.ipv4
, bytelen
))
1223 } else if (family
== AF_INET6
) {
1224 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1225 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1226 &nexthop
->rmap_src
.ipv6
, bytelen
))
1228 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1229 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1230 &nexthop
->src
.ipv6
, bytelen
))
1238 /* This function takes a nexthop as argument and adds
1239 * the appropriate netlink attributes to an existing
1242 * @param routedesc: Human readable description of route type
1243 * (direct/recursive, single-/multipath)
1244 * @param bytelen: Length of addresses in bytes.
1245 * @param nexthop: Nexthop information
1246 * @param nlmsg: nlmsghdr structure to fill in.
1247 * @param req_size: The size allocated for the message.
1249 * The function returns true if the nexthop could be added
1250 * to the message, otherwise false is returned.
1252 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1253 const char *routedesc
, int bytelen
,
1254 const struct nexthop
*nexthop
,
1255 struct nlmsghdr
*nlmsg
,
1256 struct rtmsg
*rtmsg
,
1257 size_t req_size
, int cmd
)
1260 char label_buf
[256];
1262 char addrstr
[INET6_ADDRSTRLEN
];
1266 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1268 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1269 req_size
, rtmsg
, label_buf
,
1273 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1274 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1276 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1277 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1278 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1280 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1283 if (cmd
== RTM_NEWROUTE
) {
1284 if (!_netlink_route_encode_nexthop_src(
1285 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1289 if (IS_ZEBRA_DEBUG_KERNEL
)
1290 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1291 __func__
, routedesc
, p
, ipv4_ll_buf
,
1292 label_buf
, nexthop
->ifindex
,
1293 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1297 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1298 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1299 /* Send deletes to the kernel without specifying the next-hop */
1300 if (cmd
!= RTM_DELROUTE
) {
1301 if (!_netlink_route_add_gateway_info(
1302 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1307 if (cmd
== RTM_NEWROUTE
) {
1308 if (!_netlink_route_encode_nexthop_src(
1309 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1313 if (IS_ZEBRA_DEBUG_KERNEL
) {
1314 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1316 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1317 __func__
, routedesc
, p
, addrstr
, label_buf
,
1318 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1323 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1324 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1325 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1326 AF_INET6
, nlmsg
, req_size
,
1330 if (cmd
== RTM_NEWROUTE
) {
1331 if (!_netlink_route_encode_nexthop_src(
1332 nexthop
, AF_INET6
, nlmsg
, req_size
,
1337 if (IS_ZEBRA_DEBUG_KERNEL
) {
1338 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1340 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1341 __func__
, routedesc
, p
, addrstr
, label_buf
,
1342 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1348 * We have the ifindex so we should always send it
1349 * This is especially useful if we are doing route
1352 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1353 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1357 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1358 if (cmd
== RTM_NEWROUTE
) {
1359 if (!_netlink_route_encode_nexthop_src(
1360 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1364 if (IS_ZEBRA_DEBUG_KERNEL
)
1365 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1366 __func__
, routedesc
, p
, nexthop
->ifindex
,
1367 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1373 /* This function takes a nexthop as argument and
1374 * appends to the given netlink msg. If the nexthop
1375 * defines a preferred source, the src parameter
1376 * will be modified to point to that src, otherwise
1377 * it will be kept unmodified.
1379 * @param routedesc: Human readable description of route type
1380 * (direct/recursive, single-/multipath)
1381 * @param bytelen: Length of addresses in bytes.
1382 * @param nexthop: Nexthop information
1383 * @param nlmsg: nlmsghdr structure to fill in.
1384 * @param req_size: The size allocated for the message.
1385 * @param src: pointer pointing to a location where
1386 * the prefsrc should be stored.
1388 * The function returns true if the nexthop could be added
1389 * to the message, otherwise false is returned.
1391 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1392 const char *routedesc
, int bytelen
,
1393 const struct nexthop
*nexthop
,
1394 struct nlmsghdr
*nlmsg
,
1395 size_t req_size
, struct rtmsg
*rtmsg
,
1396 const union g_addr
**src
)
1398 char label_buf
[256];
1400 struct rtnexthop
*rtnh
;
1402 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1408 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1410 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1411 req_size
, rtmsg
, label_buf
,
1415 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1416 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1418 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1419 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1420 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1422 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1423 if (nexthop
->weight
)
1424 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1426 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1427 *src
= &nexthop
->rmap_src
;
1428 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1429 *src
= &nexthop
->src
;
1431 if (IS_ZEBRA_DEBUG_KERNEL
)
1433 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1434 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1435 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1437 nl_attr_rtnh_end(nlmsg
, rtnh
);
1441 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1442 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1443 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1444 nlmsg
, req_size
, bytelen
,
1448 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1449 *src
= &nexthop
->rmap_src
;
1450 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1451 *src
= &nexthop
->src
;
1453 if (IS_ZEBRA_DEBUG_KERNEL
)
1454 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1455 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1456 label_buf
, nexthop
->ifindex
,
1457 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1459 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1460 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1461 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1462 AF_INET6
, nlmsg
, req_size
,
1466 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1467 *src
= &nexthop
->rmap_src
;
1468 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1469 *src
= &nexthop
->src
;
1471 if (IS_ZEBRA_DEBUG_KERNEL
)
1472 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1473 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1474 label_buf
, nexthop
->ifindex
,
1475 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1479 * We have figured out the ifindex so we should always send it
1480 * This is especially useful if we are doing route
1483 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1484 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1487 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1488 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1489 *src
= &nexthop
->rmap_src
;
1490 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1491 *src
= &nexthop
->src
;
1493 if (IS_ZEBRA_DEBUG_KERNEL
)
1494 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1495 __func__
, routedesc
, p
, nexthop
->ifindex
,
1496 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1499 if (nexthop
->weight
)
1500 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1502 nl_attr_rtnh_end(nlmsg
, rtnh
);
1506 static inline bool _netlink_mpls_build_singlepath(const struct prefix
*p
,
1507 const char *routedesc
,
1508 const zebra_nhlfe_t
*nhlfe
,
1509 struct nlmsghdr
*nlmsg
,
1510 struct rtmsg
*rtmsg
,
1511 size_t req_size
, int cmd
)
1516 family
= NHLFE_FAMILY(nhlfe
);
1517 bytelen
= (family
== AF_INET
? 4 : 16);
1518 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1519 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1525 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1526 const zebra_nhlfe_t
*nhlfe
,
1527 struct nlmsghdr
*nlmsg
, size_t req_size
,
1528 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1533 family
= NHLFE_FAMILY(nhlfe
);
1534 bytelen
= (family
== AF_INET
? 4 : 16);
1535 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1536 nhlfe
->nexthop
, nlmsg
, req_size
,
1540 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1542 if (IS_ZEBRA_DEBUG_KERNEL
)
1543 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1544 routedesc
, nl_msg_type_to_str(cmd
), label
);
1547 static int netlink_neigh_update(int cmd
, int ifindex
, uint32_t addr
, char *lla
,
1548 int llalen
, ns_id_t ns_id
)
1550 uint8_t protocol
= RTPROT_ZEBRA
;
1557 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1559 memset(&req
, 0, sizeof(req
));
1561 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1562 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1563 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1564 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1566 req
.ndm
.ndm_family
= AF_INET
;
1567 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1568 req
.ndm
.ndm_ifindex
= ifindex
;
1569 req
.ndm
.ndm_type
= RTN_UNICAST
;
1571 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1573 nl_attr_put32(&req
.n
, sizeof(req
), NDA_DST
, addr
);
1574 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1576 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1580 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1583 if (family
== AF_INET
) {
1584 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1585 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1587 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1588 src
->ipv4
= nexthop
->src
.ipv4
;
1591 } else if (family
== AF_INET6
) {
1592 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1593 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1595 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1596 src
->ipv6
= nexthop
->src
.ipv6
;
1605 * The function returns true if the attribute could be added
1606 * to the message, otherwise false is returned.
1608 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1611 struct rtattr
*nest
;
1613 switch (nh
->nh_encap_type
) {
1615 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1618 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1622 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1625 nl_attr_nest_end(n
, nest
);
1633 * Routing table change via netlink interface, using a dataplane context object
1635 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1636 * otherwise the number of bytes written to buf.
1638 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1639 struct zebra_dplane_ctx
*ctx
,
1640 uint8_t *data
, size_t datalen
,
1641 bool fpm
, bool force_nhg
)
1644 struct nexthop
*nexthop
= NULL
;
1645 unsigned int nexthop_num
;
1646 const char *routedesc
;
1647 bool setsrc
= false;
1649 const struct prefix
*p
, *src_p
;
1656 } *req
= (void *)data
;
1658 p
= dplane_ctx_get_dest(ctx
);
1659 src_p
= dplane_ctx_get_src(ctx
);
1661 if (datalen
< sizeof(*req
))
1664 memset(req
, 0, sizeof(*req
));
1666 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1668 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1669 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1671 if ((cmd
== RTM_NEWROUTE
) &&
1672 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1673 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1675 req
->n
.nlmsg_type
= cmd
;
1677 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1679 req
->r
.rtm_family
= p
->family
;
1680 req
->r
.rtm_dst_len
= p
->prefixlen
;
1681 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1682 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1684 if (cmd
== RTM_DELROUTE
)
1685 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1687 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1690 * blackhole routes are not RTN_UNICAST, they are
1691 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1692 * so setting this value as a RTN_UNICAST would
1693 * cause the route lookup of just the prefix
1694 * to fail. So no need to specify this for
1695 * the RTM_DELROUTE case
1697 if (cmd
!= RTM_DELROUTE
)
1698 req
->r
.rtm_type
= RTN_UNICAST
;
1700 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1703 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1709 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1711 * either by the kernel or by zebra. Its purely for calculating best
1713 * by the routing protocol and for communicating with protocol peers.
1715 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1716 NL_DEFAULT_ROUTE_METRIC
))
1719 #if defined(SUPPORT_REALMS)
1723 if (cmd
== RTM_DELROUTE
)
1724 tag
= dplane_ctx_get_old_tag(ctx
);
1726 tag
= dplane_ctx_get_tag(ctx
);
1728 if (tag
> 0 && tag
<= 255) {
1729 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
1734 /* Table corresponding to this route. */
1735 table_id
= dplane_ctx_get_table(ctx
);
1737 req
->r
.rtm_table
= table_id
;
1739 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
1740 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
1744 if (IS_ZEBRA_DEBUG_KERNEL
)
1746 "%s: %s %pFX vrf %u(%u)", __func__
,
1747 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
1751 * If we are not updating the route and we have received
1752 * a route delete, then all we need to fill in is the
1753 * prefix information to tell the kernel to schwack
1756 if (cmd
== RTM_DELROUTE
)
1757 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1759 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
1760 struct rtattr
*nest
;
1761 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
1762 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
1764 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
1767 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
1771 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
1773 nl_attr_nest_end(&req
->n
, nest
);
1776 if ((!fpm
&& kernel_nexthops_supported()
1777 && (!proto_nexthops_only()
1778 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
1779 || (fpm
&& force_nhg
)) {
1780 /* Kernel supports nexthop objects */
1781 if (IS_ZEBRA_DEBUG_KERNEL
)
1782 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
1783 dplane_ctx_get_nhe_id(ctx
));
1785 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
1786 dplane_ctx_get_nhe_id(ctx
)))
1789 /* Have to determine src still */
1790 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1794 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
1798 if (p
->family
== AF_INET
) {
1799 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1800 &src
.ipv4
, bytelen
))
1802 } else if (p
->family
== AF_INET6
) {
1803 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1804 &src
.ipv6
, bytelen
))
1809 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1812 /* Count overall nexthops so we can decide whether to use singlepath
1813 * or multipath case.
1816 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1817 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1819 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
1825 /* Singlepath case. */
1826 if (nexthop_num
== 1) {
1828 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1830 * So we want to cover 2 types of blackhole
1832 * 1) A normal blackhole route( ala from a static
1834 * 2) A recursively resolved blackhole route
1836 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
1837 switch (nexthop
->bh_type
) {
1838 case BLACKHOLE_ADMINPROHIB
:
1839 req
->r
.rtm_type
= RTN_PROHIBIT
;
1841 case BLACKHOLE_REJECT
:
1842 req
->r
.rtm_type
= RTN_UNREACHABLE
;
1845 req
->r
.rtm_type
= RTN_BLACKHOLE
;
1848 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1850 if (CHECK_FLAG(nexthop
->flags
,
1851 NEXTHOP_FLAG_RECURSIVE
)) {
1856 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1861 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1862 routedesc
= nexthop
->rparent
1863 ? "recursive, single-path"
1866 if (!_netlink_route_build_singlepath(
1867 p
, routedesc
, bytelen
, nexthop
,
1868 &req
->n
, &req
->r
, datalen
, cmd
))
1875 * Add encapsulation information when installing via
1879 if (!netlink_route_nexthop_encap(
1880 &req
->n
, datalen
, nexthop
))
1886 if (p
->family
== AF_INET
) {
1887 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1888 &src
.ipv4
, bytelen
))
1890 } else if (p
->family
== AF_INET6
) {
1891 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1892 &src
.ipv6
, bytelen
))
1896 } else { /* Multipath case */
1897 struct rtattr
*nest
;
1898 const union g_addr
*src1
= NULL
;
1900 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
1905 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1906 if (CHECK_FLAG(nexthop
->flags
,
1907 NEXTHOP_FLAG_RECURSIVE
)) {
1908 /* This only works for IPv4 now */
1912 setsrc
= nexthop_set_src(nexthop
, p
->family
,
1917 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
1918 routedesc
= nexthop
->rparent
1919 ? "recursive, multipath"
1923 if (!_netlink_route_build_multipath(
1924 p
, routedesc
, bytelen
, nexthop
,
1925 &req
->n
, datalen
, &req
->r
, &src1
))
1928 if (!setsrc
&& src1
) {
1929 if (p
->family
== AF_INET
)
1930 src
.ipv4
= src1
->ipv4
;
1931 else if (p
->family
== AF_INET6
)
1932 src
.ipv6
= src1
->ipv6
;
1939 nl_attr_nest_end(&req
->n
, nest
);
1942 * Add encapsulation information when installing via
1946 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1948 if (CHECK_FLAG(nexthop
->flags
,
1949 NEXTHOP_FLAG_RECURSIVE
))
1951 if (!netlink_route_nexthop_encap(
1952 &req
->n
, datalen
, nexthop
))
1959 if (p
->family
== AF_INET
) {
1960 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1961 &src
.ipv4
, bytelen
))
1963 } else if (p
->family
== AF_INET6
) {
1964 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
1965 &src
.ipv6
, bytelen
))
1968 if (IS_ZEBRA_DEBUG_KERNEL
)
1969 zlog_debug("Setting source");
1973 /* If there is no useful nexthop then return. */
1974 if (nexthop_num
== 0) {
1975 if (IS_ZEBRA_DEBUG_KERNEL
)
1976 zlog_debug("%s: No useful nexthop.", __func__
);
1979 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1982 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
1984 uint32_t actual_table
;
1986 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
1994 struct zebra_ns
*zns
;
1997 memset(&req
, 0, sizeof(req
));
1999 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2000 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2001 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2003 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2004 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2006 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2007 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2008 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2009 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2013 * So during the namespace cleanup we started storing
2014 * the zvrf table_id for the default table as RT_TABLE_MAIN
2015 * which is what the normal routing table for ip routing is.
2016 * This change caused this to break our lookups of sg data
2017 * because prior to this change the zvrf->table_id was 0
2018 * and when the pim multicast kernel code saw a 0,
2019 * it was auto-translated to RT_TABLE_DEFAULT. But since
2020 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2021 * and the kernel goes screw you and the delicious cookies you
2022 * are trying to give me. So now we have this little hack.
2024 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2026 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2028 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2029 &zns
->netlink_cmd
, zns
, 0);
2035 /* Char length to debug ID with */
2036 #define ID_LENGTH 10
2038 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2040 const struct nh_grp
*z_grp
,
2041 const uint8_t count
)
2043 struct nexthop_grp grp
[count
];
2044 /* Need space for max group size, "/", and null term */
2045 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2046 char buf1
[ID_LENGTH
+ 2];
2050 memset(grp
, 0, sizeof(grp
));
2053 for (int i
= 0; i
< count
; i
++) {
2054 grp
[i
].id
= z_grp
[i
].id
;
2055 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2057 if (IS_ZEBRA_DEBUG_KERNEL
) {
2059 snprintf(buf
, sizeof(buf1
), "group %u",
2062 snprintf(buf1
, sizeof(buf1
), "/%u",
2064 strlcat(buf
, buf1
, sizeof(buf
));
2068 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2069 count
* sizeof(*grp
)))
2073 if (IS_ZEBRA_DEBUG_KERNEL
)
2074 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2080 * Next hop packet encoding helper function.
2082 * \param[in] cmd netlink command.
2083 * \param[in] ctx dataplane context (information snapshot).
2084 * \param[out] buf buffer to hold the packet.
2085 * \param[in] buflen amount of buffer bytes.
2087 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2088 * otherwise the number of bytes written to buf.
2090 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2091 const struct zebra_dplane_ctx
*ctx
,
2092 void *buf
, size_t buflen
)
2100 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2101 char label_buf
[256];
2103 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2104 int type
= dplane_ctx_get_nhe_type(ctx
);
2108 EC_ZEBRA_NHG_FIB_UPDATE
,
2109 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2114 * Nothing to do if the kernel doesn't support nexthop objects or
2115 * we dont want to install this type of NHG
2117 if (!kernel_nexthops_supported()) {
2118 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2120 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2121 __func__
, id
, zebra_route_string(type
));
2125 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2126 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2128 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2129 __func__
, id
, zebra_route_string(type
));
2133 label_buf
[0] = '\0';
2135 if (buflen
< sizeof(*req
))
2138 memset(req
, 0, sizeof(*req
));
2140 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2141 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2143 if (cmd
== RTM_NEWNEXTHOP
)
2144 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2146 req
->n
.nlmsg_type
= cmd
;
2147 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2149 req
->nhm
.nh_family
= AF_UNSPEC
;
2152 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2155 if (cmd
== RTM_NEWNEXTHOP
) {
2157 * We distinguish between a "group", which is a collection
2158 * of ids, and a singleton nexthop with an id. The
2159 * group is installed as an id that just refers to a list of
2162 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2163 if (!_netlink_nexthop_build_group(
2164 &req
->n
, buflen
, id
,
2165 dplane_ctx_get_nhe_nh_grp(ctx
),
2166 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2169 const struct nexthop
*nh
=
2170 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2171 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2174 req
->nhm
.nh_family
= AF_INET
;
2175 else if (afi
== AFI_IP6
)
2176 req
->nhm
.nh_family
= AF_INET6
;
2179 case NEXTHOP_TYPE_IPV4
:
2180 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2181 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2186 case NEXTHOP_TYPE_IPV6
:
2187 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2188 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2193 case NEXTHOP_TYPE_BLACKHOLE
:
2194 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2197 /* Blackhole shouldn't have anymore attributes
2200 case NEXTHOP_TYPE_IFINDEX
:
2201 /* Don't need anymore info for this */
2207 EC_ZEBRA_NHG_FIB_UPDATE
,
2208 "Context received for kernel nexthop update without an interface");
2212 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2216 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2217 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2220 build_label_stack(nh
->nh_label
, out_lse
,
2221 label_buf
, sizeof(label_buf
));
2224 /* Set the BoS bit */
2225 out_lse
[num_labels
- 1] |=
2226 htonl(1 << MPLS_LS_S_SHIFT
);
2229 * TODO: MPLS unsupported for now in kernel.
2231 if (req
->nhm
.nh_family
== AF_MPLS
)
2234 if (!nl_attr_put(&req
->n
, buflen
, NHA_NEWDST
,
2237 * sizeof(mpls_lse_t
)))
2241 struct rtattr
*nest
;
2242 uint16_t encap
= LWTUNNEL_ENCAP_MPLS
;
2244 if (!nl_attr_put16(&req
->n
, buflen
,
2248 nest
= nl_attr_nest(&req
->n
, buflen
,
2254 MPLS_IPTUNNEL_DST
, &out_lse
,
2259 nl_attr_nest_end(&req
->n
, nest
);
2265 if (IS_ZEBRA_DEBUG_KERNEL
)
2266 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2267 __func__
, id
, nh
, nh
->ifindex
,
2268 vrf_id_to_name(nh
->vrf_id
),
2269 nh
->vrf_id
, label_buf
);
2272 req
->nhm
.nh_protocol
= zebra2proto(type
);
2274 } else if (cmd
!= RTM_DELNEXTHOP
) {
2276 EC_ZEBRA_NHG_FIB_UPDATE
,
2277 "Nexthop group kernel update command (%d) does not exist",
2282 if (IS_ZEBRA_DEBUG_KERNEL
)
2283 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2286 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2289 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2290 void *buf
, size_t buflen
)
2292 enum dplane_op_e op
;
2295 op
= dplane_ctx_get_op(ctx
);
2296 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2297 cmd
= RTM_NEWNEXTHOP
;
2298 else if (op
== DPLANE_OP_NH_DELETE
)
2299 cmd
= RTM_DELNEXTHOP
;
2301 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2302 "Context received for kernel nexthop update with incorrect OP code (%u)",
2307 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2310 enum netlink_msg_status
2311 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2312 struct zebra_dplane_ctx
*ctx
)
2314 /* Nothing to do if the kernel doesn't support nexthop objects */
2315 if (!kernel_nexthops_supported())
2316 return FRR_NETLINK_SUCCESS
;
2318 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2322 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2323 void *buf
, size_t buflen
)
2325 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2326 buflen
, false, false);
2329 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2330 void *buf
, size_t buflen
)
2332 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2333 buflen
, false, false);
2336 enum netlink_msg_status
2337 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2340 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2342 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2344 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2346 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2348 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2349 /* Single 'replace' operation */
2352 * With route replace semantics in place
2353 * for v4 routes and the new route is a system
2354 * route we do not install anything.
2355 * The problem here is that the new system
2356 * route should cause us to withdraw from
2357 * the kernel the old non-system route
2359 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2360 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2361 netlink_batch_add_msg(
2362 bth
, ctx
, netlink_delroute_msg_encoder
,
2366 * So v6 route replace semantics are not in
2367 * the kernel at this point as I understand it.
2368 * so let's do a delete then an add.
2369 * In the future once v6 route replace semantics
2370 * are in we can figure out what to do here to
2371 * allow working with old and new kernels.
2373 * I'm also intentionally ignoring the failure case
2374 * of the route delete. If that happens yeah we're
2377 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2378 netlink_batch_add_msg(
2379 bth
, ctx
, netlink_delroute_msg_encoder
,
2385 return FRR_NETLINK_ERROR
;
2387 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2388 return FRR_NETLINK_SUCCESS
;
2390 return netlink_batch_add_msg(bth
, ctx
,
2392 ? netlink_newroute_msg_encoder
2393 : netlink_delroute_msg_encoder
,
2398 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2400 * @tb: Netlink RTA data
2401 * @family: Address family in the nhmsg
2402 * @ifp: Interface connected - this should be NULL, we fill it in
2403 * @ns_id: Namspace id
2405 * Return: New nexthop
2407 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2408 unsigned char family
,
2409 struct interface
**ifp
,
2412 struct nexthop nh
= {};
2414 enum nexthop_types_t type
= 0;
2417 struct interface
*ifp_lookup
;
2419 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2422 if (tb
[NHA_GATEWAY
]) {
2425 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2429 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2434 EC_ZEBRA_BAD_NHG_MESSAGE
,
2435 "Nexthop gateway with bad address family (%d) received from kernel",
2439 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2441 type
= NEXTHOP_TYPE_IFINDEX
;
2447 memcpy(&(nh
.gate
), gate
, sz
);
2450 nh
.ifindex
= if_index
;
2453 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2458 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2461 EC_ZEBRA_UNKNOWN_INTERFACE
,
2462 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2463 __func__
, nh
.ifindex
);
2465 nh
.vrf_id
= VRF_DEFAULT
;
2468 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2469 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2472 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2474 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2475 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2478 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2485 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2486 struct nh_grp
*z_grp
, int z_grp_size
)
2489 /* linux/nexthop.h group struct */
2490 struct nexthop_grp
*n_grp
= NULL
;
2492 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2493 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2495 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2496 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2497 "Invalid nexthop group received from the kernel");
2502 // TODO: Need type for something?
2503 zlog_debug("Nexthop group type: %d",
2504 *((uint16_t *)RTA_DATA(tb
[NHA_GROUP_TYPE
])));
2508 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2509 z_grp
[i
].id
= n_grp
[i
].id
;
2510 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2516 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2518 * @h: Netlink message header
2519 * @ns_id: Namspace id
2520 * @startup: Are we reading under startup conditions?
2522 * Return: Result status
2524 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2527 /* nexthop group id */
2529 unsigned char family
;
2531 afi_t afi
= AFI_UNSPEC
;
2532 vrf_id_t vrf_id
= VRF_DEFAULT
;
2533 struct interface
*ifp
= NULL
;
2534 struct nhmsg
*nhm
= NULL
;
2535 struct nexthop nh
= {};
2536 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2537 /* Count of nexthops in group array */
2538 uint8_t grp_count
= 0;
2539 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2541 nhm
= NLMSG_DATA(h
);
2546 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2549 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2552 "%s: Message received from netlink is of a broken size %d %zu",
2553 __func__
, h
->nlmsg_len
,
2554 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2558 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2563 EC_ZEBRA_BAD_NHG_MESSAGE
,
2564 "Nexthop group without an ID received from the kernel");
2568 /* We use the ID key'd nhg table for kernel updates */
2569 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2571 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2572 /* If this is a L2 NH just ignore it */
2573 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2574 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2580 family
= nhm
->nh_family
;
2581 afi
= family2afi(family
);
2583 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2585 if (IS_ZEBRA_DEBUG_KERNEL
)
2586 zlog_debug("%s ID (%u) %s NS %u",
2587 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2588 nl_family_to_str(family
), ns_id
);
2591 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2592 if (tb
[NHA_GROUP
]) {
2594 * If this is a group message its only going to have
2595 * an array of nexthop IDs associated with it
2597 grp_count
= netlink_nexthop_process_group(
2598 tb
, grp
, array_size(grp
));
2600 if (tb
[NHA_BLACKHOLE
]) {
2602 * This nexthop is just for blackhole-ing
2603 * traffic, it should not have an OIF, GATEWAY,
2606 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2607 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2608 } else if (tb
[NHA_OIF
])
2610 * This is a true new nexthop, so we need
2611 * to parse the gateway and device info
2613 nh
= netlink_nexthop_process_nh(tb
, family
,
2618 EC_ZEBRA_BAD_NHG_MESSAGE
,
2619 "Invalid Nexthop message received from the kernel with ID (%u)",
2623 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2624 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2625 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2629 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2633 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2634 zebra_nhg_kernel_del(id
, vrf_id
);
2640 * netlink_request_nexthop() - Request nextop information from the kernel
2641 * @zns: Zebra namespace
2642 * @family: AF_* netlink family
2643 * @type: RTM_* route type
2645 * Return: Result status
2647 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
2654 /* Form the request, specifying filter (rtattr) if needed. */
2655 memset(&req
, 0, sizeof(req
));
2656 req
.n
.nlmsg_type
= type
;
2657 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2658 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2659 req
.nhm
.nh_family
= family
;
2661 return netlink_request(&zns
->netlink_cmd
, &req
);
2666 * netlink_nexthop_read() - Nexthop read function using netlink interface
2668 * @zns: Zebra name space
2670 * Return: Result status
2671 * Only called at bootstrap time.
2673 int netlink_nexthop_read(struct zebra_ns
*zns
)
2676 struct zebra_dplane_info dp_info
;
2678 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2680 /* Get nexthop objects */
2681 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
2684 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
2688 /* If we succesfully read in nexthop objects,
2689 * this kernel must support them.
2693 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2694 zlog_debug("Nexthop objects %ssupported on this kernel",
2695 supports_nh
? "" : "not ");
2701 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
2702 int llalen
, ns_id_t ns_id
)
2704 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
2705 addr
, lla
, llalen
, ns_id
);
2709 * netlink_neigh_update_msg_encode() - Common helper api for encoding
2710 * evpn neighbor update as netlink messages using dataplane context object.
2711 * Here, a neighbor refers to a bridge forwarding database entry for
2712 * either unicast forwarding or head-end replication or an IP neighbor
2714 * @ctx: Dataplane context
2715 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
2716 * @mac: A neighbor cache link layer address
2717 * @ip: A neighbor cache n/w layer destination address
2718 * In the case of bridge FDB, this represnts the remote
2720 * @replace_obj: Whether NEW request should replace existing object or
2721 * add to the end of the list
2722 * @family: AF_* netlink family
2723 * @type: RTN_* route type
2724 * @flags: NTF_* flags
2725 * @state: NUD_* states
2726 * @data: data buffer pointer
2727 * @datalen: total amount of data buffer space
2729 * Return: 0 when the msg doesn't fit entirely in the buffer
2730 * otherwise the number of bytes written to buf.
2732 static ssize_t
netlink_neigh_update_msg_encode(
2733 const struct zebra_dplane_ctx
*ctx
, int cmd
, const struct ethaddr
*mac
,
2734 const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
, uint8_t type
,
2735 uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
2736 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
2739 uint8_t protocol
= RTPROT_ZEBRA
;
2746 enum dplane_op_e op
;
2748 if (datalen
< sizeof(*req
))
2750 memset(req
, 0, sizeof(*req
));
2752 op
= dplane_ctx_get_op(ctx
);
2754 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2755 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2756 if (cmd
== RTM_NEWNEIGH
)
2757 req
->n
.nlmsg_flags
|=
2759 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
2760 req
->n
.nlmsg_type
= cmd
;
2761 req
->ndm
.ndm_family
= family
;
2762 req
->ndm
.ndm_type
= type
;
2763 req
->ndm
.ndm_state
= state
;
2764 req
->ndm
.ndm_flags
= flags
;
2765 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
2767 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
2772 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, mac
, 6))
2777 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
2781 if (!nl_attr_put(&req
->n
, datalen
, NDA_NOTIFY
,
2782 &nfy_flags
, sizeof(nfy_flags
)))
2787 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
2792 ipa_len
= IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
2793 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
, ipa_len
))
2796 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
2797 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
2800 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
2804 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
2805 dplane_ctx_mac_get_br_ifindex(ctx
)))
2809 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2813 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
2814 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
2817 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
2818 void *buf
, size_t buflen
)
2820 struct ethaddr dst_mac
= {.octet
= {0}};
2822 return netlink_neigh_update_msg_encode(
2823 ctx
, cmd
, &dst_mac
, dplane_ctx_neigh_get_ipaddr(ctx
), false,
2824 PF_BRIDGE
, 0, NTF_SELF
, (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/,
2825 false /*nfy*/, 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/,
2830 #define NDA_RTA(r) \
2831 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2834 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
2837 struct interface
*ifp
;
2838 struct zebra_if
*zif
;
2839 struct rtattr
*tb
[NDA_MAX
+ 1];
2840 struct interface
*br_if
;
2843 struct in_addr vtep_ip
;
2844 int vid_present
= 0, dst_present
= 0;
2845 char buf
[ETHER_ADDR_STRLEN
];
2849 bool local_inactive
= false;
2850 bool dp_static
= false;
2851 uint32_t nhg_id
= 0;
2853 ndm
= NLMSG_DATA(h
);
2855 /* We only process macfdb notifications if EVPN is enabled */
2856 if (!is_evpn_enabled())
2859 /* Parse attributes and extract fields of interest. Do basic
2860 * validation of the fields.
2862 memset(tb
, 0, sizeof tb
);
2863 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
2865 if (!tb
[NDA_LLADDR
]) {
2866 if (IS_ZEBRA_DEBUG_KERNEL
)
2867 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
2868 nl_msg_type_to_str(h
->nlmsg_type
),
2873 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
2874 if (IS_ZEBRA_DEBUG_KERNEL
)
2876 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
2877 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
2878 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
2882 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
2884 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
2886 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
2887 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
2891 /* TODO: Only IPv4 supported now. */
2893 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
2895 snprintf(dst_buf
, sizeof(dst_buf
), " dst %s",
2896 inet_ntoa(vtep_ip
));
2900 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
2902 if (ndm
->ndm_state
& NUD_STALE
)
2903 local_inactive
= true;
2905 if (tb
[NDA_NOTIFY
]) {
2909 nfy_flags
= *(uint8_t *)RTA_DATA(tb
[NDA_NOTIFY
]);
2910 /* local activity has not been detected on the entry */
2911 if (nfy_flags
& (1 << BR_FDB_NFY_INACTIVE
))
2912 local_inactive
= true;
2915 if (IS_ZEBRA_DEBUG_KERNEL
)
2916 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %s%s nhg %d",
2917 nl_msg_type_to_str(h
->nlmsg_type
),
2918 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
2919 ndm
->ndm_state
, ndm
->ndm_flags
,
2920 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2921 dst_present
? dst_buf
: "", nhg_id
);
2923 /* The interface should exist. */
2924 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
2926 if (!ifp
|| !ifp
->info
)
2929 /* The interface should be something we're interested in. */
2930 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2933 zif
= (struct zebra_if
*)ifp
->info
;
2934 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
2935 if (IS_ZEBRA_DEBUG_KERNEL
)
2937 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
2938 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
2940 zif
->brslave_info
.bridge_ifindex
);
2944 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
2946 if (filter_vlan
&& vid
!= filter_vlan
) {
2947 if (IS_ZEBRA_DEBUG_KERNEL
)
2948 zlog_debug(" Filtered due to filter vlan: %d",
2953 /* If add or update, do accordingly if learnt on a "local" interface; if
2954 * the notification is over VxLAN, this has to be related to
2956 * so perform an implicit delete of any local entry (if it exists).
2958 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
2959 /* Drop "permanent" entries. */
2960 if (ndm
->ndm_state
& NUD_PERMANENT
) {
2961 if (IS_ZEBRA_DEBUG_KERNEL
)
2963 " Dropping entry because of NUD_PERMANENT");
2967 if (IS_ZEBRA_IF_VXLAN(ifp
))
2968 return zebra_vxlan_check_del_local_mac(ifp
, br_if
, &mac
,
2971 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
2972 sticky
, local_inactive
, dp_static
);
2975 /* This is a delete notification.
2976 * Ignore the notification with IP dest as it may just signify that the
2977 * MAC has moved from remote to local. The exception is the special
2978 * all-zeros MAC that represents the BUM flooding entry; we may have
2979 * to readd it. Otherwise,
2980 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2981 * 2. For a MAC over "local" interface, delete the mac
2982 * Note: We will get notifications from both bridge driver and VxLAN
2989 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
2991 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
2992 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
2996 if (IS_ZEBRA_IF_VXLAN(ifp
))
2997 return zebra_vxlan_check_readd_remote_mac(ifp
, br_if
, &mac
,
3000 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3003 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3008 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3011 /* Length validity. */
3012 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3016 /* We are interested only in AF_BRIDGE notifications. */
3017 ndm
= NLMSG_DATA(h
);
3018 if (ndm
->ndm_family
!= AF_BRIDGE
)
3021 return netlink_macfdb_change(h
, len
, ns_id
);
3024 /* Request for MAC FDB information from the kernel */
3025 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3026 int type
, ifindex_t master_ifindex
)
3030 struct ifinfomsg ifm
;
3034 /* Form the request, specifying filter (rtattr) if needed. */
3035 memset(&req
, 0, sizeof(req
));
3036 req
.n
.nlmsg_type
= type
;
3037 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3038 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3039 req
.ifm
.ifi_family
= family
;
3041 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3043 return netlink_request(netlink_cmd
, &req
);
3047 * MAC forwarding database read using netlink interface. This is invoked
3050 int netlink_macfdb_read(struct zebra_ns
*zns
)
3053 struct zebra_dplane_info dp_info
;
3055 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3057 /* Get bridge FDB table. */
3058 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3062 /* We are reading entire table. */
3064 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3071 * MAC forwarding database read using netlink interface. This is for a
3072 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3074 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3075 struct interface
*br_if
)
3077 struct zebra_if
*br_zif
;
3078 struct zebra_if
*zif
;
3079 struct zebra_l2info_vxlan
*vxl
;
3080 struct zebra_dplane_info dp_info
;
3083 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3085 /* Save VLAN we're filtering on, if needed. */
3086 br_zif
= (struct zebra_if
*)br_if
->info
;
3087 zif
= (struct zebra_if
*)ifp
->info
;
3088 vxl
= &zif
->l2info
.vxl
;
3089 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3090 filter_vlan
= vxl
->access_vlan
;
3092 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3094 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3098 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3101 /* Reset VLAN filter. */
3107 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3108 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3111 struct interface
*br_if
,
3112 struct ethaddr
*mac
,
3120 struct zebra_if
*br_zif
;
3121 char buf
[ETHER_ADDR_STRLEN
];
3123 memset(&req
, 0, sizeof(req
));
3124 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3125 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3126 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3127 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3128 /* req.ndm.ndm_state = NUD_REACHABLE; */
3130 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3132 br_zif
= (struct zebra_if
*)br_if
->info
;
3133 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3134 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3136 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3138 if (IS_ZEBRA_DEBUG_KERNEL
)
3140 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %s vid %u",
3141 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3142 br_if
->name
, br_if
->ifindex
,
3143 vrf_id_to_name(br_if
->vrf_id
), br_if
->vrf_id
,
3144 prefix_mac2str(mac
, buf
, sizeof(buf
)), 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 flags
|= NTF_STICKY
;
3201 flags
|= NTF_EXT_LEARNED
;
3202 /* if it was static-local previously we need to clear the
3203 * notify flags on replace with remote
3205 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3209 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3210 nfy_flags
|= (1 << BR_FDB_NFY_STATIC
);
3214 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3215 nfy_flags
|= (1 << BR_FDB_NFY_INACTIVE
);
3220 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3221 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3222 SET_IPADDR_V4(&vtep_ip
);
3224 if (IS_ZEBRA_DEBUG_KERNEL
) {
3225 char ipbuf
[PREFIX_STRLEN
];
3226 char buf
[ETHER_ADDR_STRLEN
];
3228 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3230 vid
= dplane_ctx_mac_get_vlan(ctx
);
3232 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3237 "Tx %s family %s IF %s(%u)%s %sMAC %s dst %s nhg %u%s%s%s%s%s",
3238 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3239 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3240 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3241 prefix_mac2str(mac
, buf
, sizeof(buf
)),
3242 ipaddr2str(&vtep_ip
, ipbuf
, sizeof(ipbuf
)), nhg_id
,
3243 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3244 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3246 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3247 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3252 total
= netlink_neigh_update_msg_encode(
3253 ctx
, cmd
, dplane_ctx_mac_get_addr(ctx
), &vtep_ip
, true,
3254 AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
, nfy_flags
,
3255 false /*ext*/, 0 /*ext_flags*/, data
, datalen
);
3261 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3262 * 5549 support, re-install them.
3264 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3265 struct interface
*ifp
, struct ipaddr
*ip
,
3268 if (ndm
->ndm_family
!= AF_INET
)
3271 if (!zif
->v6_2_v4_ll_neigh_entry
)
3274 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3277 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3278 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3283 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3287 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3289 #define NUD_LOCAL_ACTIVE \
3290 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3292 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3295 struct interface
*ifp
;
3296 struct zebra_if
*zif
;
3297 struct rtattr
*tb
[NDA_MAX
+ 1];
3298 struct interface
*link_if
;
3302 char buf
[ETHER_ADDR_STRLEN
];
3303 char buf2
[INET6_ADDRSTRLEN
];
3304 int mac_present
= 0;
3307 bool local_inactive
;
3309 ndm
= NLMSG_DATA(h
);
3311 /* The interface should exist. */
3312 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3314 if (!ifp
|| !ifp
->info
)
3317 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
3318 zif
= (struct zebra_if
*)ifp
->info
;
3320 /* Parse attributes and extract fields of interest. */
3321 memset(tb
, 0, sizeof(tb
));
3322 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3325 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3326 nl_msg_type_to_str(h
->nlmsg_type
),
3327 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3328 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
);
3332 memset(&ip
, 0, sizeof(struct ipaddr
));
3333 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3334 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3336 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3337 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3338 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3339 if (IS_ZEBRA_DEBUG_KERNEL
)
3341 "\tNeighbor Entry Received is a 5549 entry, finished");
3345 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3346 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3347 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3349 /* The neighbor is present on an SVI. From this, we locate the
3351 * bridge because we're only interested in neighbors on a VxLAN bridge.
3352 * The bridge is located based on the nature of the SVI:
3353 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3355 * and is linked to the bridge
3356 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3360 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3361 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3365 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3368 if (IS_ZEBRA_DEBUG_KERNEL
)
3370 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3374 memset(&mac
, 0, sizeof(struct ethaddr
));
3375 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3376 if (tb
[NDA_LLADDR
]) {
3377 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3378 if (IS_ZEBRA_DEBUG_KERNEL
)
3380 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3385 ifp
->name
, ndm
->ndm_ifindex
,
3386 VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3387 (unsigned long)RTA_PAYLOAD(
3393 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3396 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3397 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3399 if (IS_ZEBRA_DEBUG_KERNEL
)
3401 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
3402 nl_msg_type_to_str(h
->nlmsg_type
),
3403 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3404 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3405 ipaddr2str(&ip
, buf2
, sizeof(buf2
)),
3407 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3409 ndm
->ndm_state
, ndm
->ndm_flags
);
3411 /* If the neighbor state is valid for use, process as an add or
3413 * else process as a delete. Note that the delete handling may
3415 * in re-adding the neighbor if it is a valid "remote" neighbor.
3417 if (ndm
->ndm_state
& NUD_VALID
) {
3418 local_inactive
= !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3420 /* XXX - populate dp-static based on the sync flags
3423 return zebra_vxlan_handle_kernel_neigh_update(
3424 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
,
3425 is_ext
, is_router
, local_inactive
,
3426 false /* dp_static */);
3429 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3432 if (IS_ZEBRA_DEBUG_KERNEL
)
3433 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %s",
3434 nl_msg_type_to_str(h
->nlmsg_type
),
3435 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3436 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3437 ipaddr2str(&ip
, buf2
, sizeof(buf2
)));
3439 /* Process the delete - it may result in re-adding the neighbor if it is
3440 * a valid "remote" neighbor.
3442 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3445 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3450 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3453 /* Length validity. */
3454 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3458 /* We are interested only in AF_INET or AF_INET6 notifications. */
3459 ndm
= NLMSG_DATA(h
);
3460 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3463 return netlink_neigh_change(h
, len
);
3466 /* Request for IP neighbor information from the kernel */
3467 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3468 int type
, ifindex_t ifindex
)
3476 /* Form the request, specifying filter (rtattr) if needed. */
3477 memset(&req
, 0, sizeof(req
));
3478 req
.n
.nlmsg_type
= type
;
3479 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3480 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3481 req
.ndm
.ndm_family
= family
;
3483 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3485 return netlink_request(netlink_cmd
, &req
);
3489 * IP Neighbor table read using netlink interface. This is invoked
3492 int netlink_neigh_read(struct zebra_ns
*zns
)
3495 struct zebra_dplane_info dp_info
;
3497 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3499 /* Get IP neighbor table. */
3500 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3504 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3511 * IP Neighbor table read using netlink interface. This is for a specific
3514 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3517 struct zebra_dplane_info dp_info
;
3519 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3521 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3525 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3532 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3533 * read using netlink interface.
3535 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3536 int type
, struct ipaddr
*ip
,
3546 /* Form the request, specifying filter (rtattr) if needed. */
3547 memset(&req
, 0, sizeof(req
));
3548 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3549 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3550 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3551 req
.ndm
.ndm_ifindex
= ifindex
;
3553 if (IS_IPADDR_V4(ip
)) {
3554 ipa_len
= IPV4_MAX_BYTELEN
;
3555 req
.ndm
.ndm_family
= AF_INET
;
3558 ipa_len
= IPV6_MAX_BYTELEN
;
3559 req
.ndm
.ndm_family
= AF_INET6
;
3562 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
3564 if (IS_ZEBRA_DEBUG_KERNEL
) {
3565 char buf
[INET6_ADDRSTRLEN
];
3567 zlog_debug("%s: Tx %s family %s IF %u IP %s flags 0x%x",
3568 __func__
, nl_msg_type_to_str(type
),
3569 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
,
3570 ipaddr2str(ip
, buf
, sizeof(buf
)), req
.n
.nlmsg_flags
);
3573 return netlink_request(&zns
->netlink_cmd
, &req
);
3576 int netlink_neigh_read_specific_ip(struct ipaddr
*ip
,
3577 struct interface
*vlan_if
)
3580 struct zebra_ns
*zns
;
3581 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
3582 char buf
[INET6_ADDRSTRLEN
];
3583 struct zebra_dplane_info dp_info
;
3587 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3589 if (IS_ZEBRA_DEBUG_KERNEL
)
3590 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf %s(%u)",
3591 __func__
, vlan_if
->name
, vlan_if
->ifindex
,
3592 ipaddr2str(ip
, buf
, sizeof(buf
)),
3593 vrf_id_to_name(vlan_if
->vrf_id
), vlan_if
->vrf_id
);
3595 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
3600 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3606 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
3611 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
))
3614 /* Length validity. */
3615 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3618 "%s: Message received from netlink is of a broken size %d %zu",
3619 __func__
, h
->nlmsg_len
,
3620 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
3624 /* Is this a notification for the MAC FDB or IP neighbor table? */
3625 ndm
= NLMSG_DATA(h
);
3626 if (ndm
->ndm_family
== AF_BRIDGE
)
3627 return netlink_macfdb_change(h
, len
, ns_id
);
3629 if (ndm
->ndm_type
!= RTN_UNICAST
)
3632 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
3633 return netlink_ipneigh_change(h
, len
, ns_id
);
3636 EC_ZEBRA_UNKNOWN_FAMILY
,
3637 "Invalid address family: %u received from kernel neighbor change: %s",
3638 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
3646 * Utility neighbor-update function, using info from dplane context.
3648 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
3649 int cmd
, void *buf
, size_t buflen
)
3651 const struct ipaddr
*ip
;
3652 const struct ethaddr
*mac
;
3656 uint32_t update_flags
;
3657 uint32_t ext_flags
= 0;
3660 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
3661 mac
= dplane_ctx_neigh_get_mac(ctx
);
3662 if (is_zero_mac(mac
))
3665 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
3666 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
3667 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
3669 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
3671 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
3672 flags
|= NTF_EXT_LEARNED
;
3673 /* if it was static-local previously we need to clear the
3674 * ext flags on replace with remote
3676 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
3681 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
3682 ext_flags
|= NTF_E_MH_PEER_SYNC
;
3684 /* the ndm_state set for local entries can be REACHABLE or
3685 * STALE. if the dataplane has already establish reachability
3686 * (in the meantime) FRR must not over-write it with STALE.
3687 * this accidental race/over-write is avoided by using the
3688 * WEAK_OVERRIDE_STATE
3690 ext_flags
|= NTF_E_WEAK_OVERRIDE_STATE
;
3692 if (IS_ZEBRA_DEBUG_KERNEL
) {
3693 char buf
[INET6_ADDRSTRLEN
];
3694 char buf2
[ETHER_ADDR_STRLEN
];
3697 "Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
3698 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
3699 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3700 ipaddr2str(ip
, buf
, sizeof(buf
)),
3701 mac
? prefix_mac2str(mac
, buf2
, sizeof(buf2
)) : "null",
3705 return netlink_neigh_update_msg_encode(
3706 ctx
, cmd
, mac
, ip
, true, family
, RTN_UNICAST
, flags
, state
,
3707 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
, ext_flags
, buf
,
3711 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
3712 void *buf
, size_t buflen
)
3716 switch (dplane_ctx_get_op(ctx
)) {
3717 case DPLANE_OP_NEIGH_INSTALL
:
3718 case DPLANE_OP_NEIGH_UPDATE
:
3719 case DPLANE_OP_NEIGH_DISCOVER
:
3720 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
3722 case DPLANE_OP_NEIGH_DELETE
:
3723 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
3725 case DPLANE_OP_VTEP_ADD
:
3726 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
3729 case DPLANE_OP_VTEP_DELETE
:
3730 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
3741 * Update MAC, using dataplane context object.
3744 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
3745 struct zebra_dplane_ctx
*ctx
)
3747 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
3751 enum netlink_msg_status
3752 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
3754 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
3759 * MPLS label forwarding table change via netlink interface, using dataplane
3760 * context information.
3762 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
3763 void *buf
, size_t buflen
)
3766 const struct nhlfe_list_head
*head
;
3767 const zebra_nhlfe_t
*nhlfe
;
3768 struct nexthop
*nexthop
= NULL
;
3769 unsigned int nexthop_num
;
3770 const char *routedesc
;
3772 struct prefix p
= {0};
3780 if (buflen
< sizeof(*req
))
3783 memset(req
, 0, sizeof(*req
));
3786 * Count # nexthops so we can decide whether to use singlepath
3787 * or multipath case.
3790 head
= dplane_ctx_get_nhlfe_list(ctx
);
3791 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3792 nexthop
= nhlfe
->nexthop
;
3795 if (cmd
== RTM_NEWROUTE
) {
3796 /* Count all selected NHLFEs */
3797 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3798 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
3801 /* Count all installed NHLFEs */
3802 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
3803 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3808 if ((nexthop_num
== 0) ||
3809 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
3812 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
3813 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
3814 req
->n
.nlmsg_type
= cmd
;
3815 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
3817 req
->r
.rtm_family
= AF_MPLS
;
3818 req
->r
.rtm_table
= RT_TABLE_MAIN
;
3819 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
3820 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
3821 req
->r
.rtm_type
= RTN_UNICAST
;
3823 if (cmd
== RTM_NEWROUTE
) {
3824 /* We do a replace to handle update. */
3825 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
3827 /* set the protocol value if installing */
3828 route_type
= re_type_from_lsp_type(
3829 dplane_ctx_get_best_nhlfe(ctx
)->type
);
3830 req
->r
.rtm_protocol
= zebra2proto(route_type
);
3833 /* Fill destination */
3834 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
3835 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
3838 /* Fill nexthops (paths) based on single-path or multipath. The paths
3839 * chosen depend on the operation.
3841 if (nexthop_num
== 1) {
3842 routedesc
= "single-path";
3843 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3847 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3848 nexthop
= nhlfe
->nexthop
;
3852 if ((cmd
== RTM_NEWROUTE
3853 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3854 && CHECK_FLAG(nexthop
->flags
,
3855 NEXTHOP_FLAG_ACTIVE
)))
3856 || (cmd
== RTM_DELROUTE
3857 && (CHECK_FLAG(nhlfe
->flags
,
3858 NHLFE_FLAG_INSTALLED
)
3859 && CHECK_FLAG(nexthop
->flags
,
3860 NEXTHOP_FLAG_FIB
)))) {
3861 /* Add the gateway */
3862 if (!_netlink_mpls_build_singlepath(
3863 &p
, routedesc
, nhlfe
, &req
->n
,
3864 &req
->r
, buflen
, cmd
))
3871 } else { /* Multipath case */
3872 struct rtattr
*nest
;
3873 const union g_addr
*src1
= NULL
;
3875 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
3879 routedesc
= "multipath";
3880 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
3884 frr_each(nhlfe_list_const
, head
, nhlfe
) {
3885 nexthop
= nhlfe
->nexthop
;
3889 if ((cmd
== RTM_NEWROUTE
3890 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
3891 && CHECK_FLAG(nexthop
->flags
,
3892 NEXTHOP_FLAG_ACTIVE
)))
3893 || (cmd
== RTM_DELROUTE
3894 && (CHECK_FLAG(nhlfe
->flags
,
3895 NHLFE_FLAG_INSTALLED
)
3896 && CHECK_FLAG(nexthop
->flags
,
3897 NEXTHOP_FLAG_FIB
)))) {
3900 /* Build the multipath */
3901 if (!_netlink_mpls_build_multipath(
3902 &p
, routedesc
, nhlfe
, &req
->n
,
3903 buflen
, &req
->r
, &src1
))
3908 /* Add the multipath */
3909 nl_attr_nest_end(&req
->n
, nest
);
3912 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3915 /****************************************************************************
3916 * This code was developed in a branch that didn't have dplane APIs for
3917 * MAC updates. Hence the use of the legacy style. It will be moved to
3918 * the new dplane style pre-merge to master. XXX
3920 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
3927 int cmd
= RTM_NEWNEXTHOP
;
3928 struct zebra_vrf
*zvrf
;
3929 struct zebra_ns
*zns
;
3931 zvrf
= zebra_vrf_get_evpn();
3936 memset(&req
, 0, sizeof(req
));
3938 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3939 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3940 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
3941 req
.n
.nlmsg_type
= cmd
;
3942 req
.nhm
.nh_family
= AF_INET
;
3944 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
3946 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
3948 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
3949 &vtep_ip
, IPV4_MAX_BYTELEN
))
3952 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3953 zlog_debug("Tx %s fdb-nh 0x%x %s",
3954 nl_msg_type_to_str(cmd
), nh_id
, inet_ntoa(vtep_ip
));
3957 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3961 static int netlink_fdb_nh_del(uint32_t nh_id
)
3968 int cmd
= RTM_DELNEXTHOP
;
3969 struct zebra_vrf
*zvrf
;
3970 struct zebra_ns
*zns
;
3972 zvrf
= zebra_vrf_get_evpn();
3977 memset(&req
, 0, sizeof(req
));
3979 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3980 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3981 req
.n
.nlmsg_type
= cmd
;
3982 req
.nhm
.nh_family
= AF_UNSPEC
;
3984 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
3987 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
3988 zlog_debug("Tx %s fdb-nh 0x%x",
3989 nl_msg_type_to_str(cmd
), nh_id
);
3992 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
3996 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
3997 struct nh_grp
*nh_ids
)
4004 int cmd
= RTM_NEWNEXTHOP
;
4005 struct zebra_vrf
*zvrf
;
4006 struct zebra_ns
*zns
;
4007 struct nexthop_grp grp
[nh_cnt
];
4010 zvrf
= zebra_vrf_get_evpn();
4015 memset(&req
, 0, sizeof(req
));
4017 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4018 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4019 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4020 req
.n
.nlmsg_type
= cmd
;
4021 req
.nhm
.nh_family
= AF_UNSPEC
;
4023 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4025 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4027 memset(&grp
, 0, sizeof(grp
));
4028 for (i
= 0; i
< nh_cnt
; ++i
) {
4029 grp
[i
].id
= nh_ids
[i
].id
;
4030 grp
[i
].weight
= nh_ids
[i
].weight
;
4032 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4033 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4037 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4038 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4042 for (i
= 0; i
< nh_cnt
; ++i
) {
4043 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4045 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4048 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4049 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4052 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4056 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4058 return netlink_fdb_nh_del(nhg_id
);
4061 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4063 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4066 int kernel_del_mac_nh(uint32_t nh_id
)
4068 return netlink_fdb_nh_del(nh_id
);
4071 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4072 struct nh_grp
*nh_ids
)
4074 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4077 int kernel_del_mac_nhg(uint32_t nhg_id
)
4079 return netlink_fdb_nhg_del(nhg_id
);
4082 #endif /* HAVE_NETLINK */